xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/tegra/hub.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2017 NVIDIA CORPORATION.  All rights reserved.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/clk.h>
7*4882a593Smuzhiyun #include <linux/delay.h>
8*4882a593Smuzhiyun #include <linux/host1x.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/of.h>
11*4882a593Smuzhiyun #include <linux/of_device.h>
12*4882a593Smuzhiyun #include <linux/of_graph.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <linux/pm_runtime.h>
15*4882a593Smuzhiyun #include <linux/reset.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <drm/drm_atomic.h>
18*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
19*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
20*4882a593Smuzhiyun #include <drm/drm_probe_helper.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "drm.h"
23*4882a593Smuzhiyun #include "dc.h"
24*4882a593Smuzhiyun #include "plane.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static const u32 tegra_shared_plane_formats[] = {
27*4882a593Smuzhiyun 	DRM_FORMAT_ARGB1555,
28*4882a593Smuzhiyun 	DRM_FORMAT_RGB565,
29*4882a593Smuzhiyun 	DRM_FORMAT_RGBA5551,
30*4882a593Smuzhiyun 	DRM_FORMAT_ARGB8888,
31*4882a593Smuzhiyun 	DRM_FORMAT_ABGR8888,
32*4882a593Smuzhiyun 	/* new on Tegra114 */
33*4882a593Smuzhiyun 	DRM_FORMAT_ABGR4444,
34*4882a593Smuzhiyun 	DRM_FORMAT_ABGR1555,
35*4882a593Smuzhiyun 	DRM_FORMAT_BGRA5551,
36*4882a593Smuzhiyun 	DRM_FORMAT_XRGB1555,
37*4882a593Smuzhiyun 	DRM_FORMAT_RGBX5551,
38*4882a593Smuzhiyun 	DRM_FORMAT_XBGR1555,
39*4882a593Smuzhiyun 	DRM_FORMAT_BGRX5551,
40*4882a593Smuzhiyun 	DRM_FORMAT_BGR565,
41*4882a593Smuzhiyun 	DRM_FORMAT_XRGB8888,
42*4882a593Smuzhiyun 	DRM_FORMAT_XBGR8888,
43*4882a593Smuzhiyun 	/* planar formats */
44*4882a593Smuzhiyun 	DRM_FORMAT_UYVY,
45*4882a593Smuzhiyun 	DRM_FORMAT_YUYV,
46*4882a593Smuzhiyun 	DRM_FORMAT_YUV420,
47*4882a593Smuzhiyun 	DRM_FORMAT_YUV422,
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static const u64 tegra_shared_plane_modifiers[] = {
51*4882a593Smuzhiyun 	DRM_FORMAT_MOD_LINEAR,
52*4882a593Smuzhiyun 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
53*4882a593Smuzhiyun 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
54*4882a593Smuzhiyun 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
55*4882a593Smuzhiyun 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
56*4882a593Smuzhiyun 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
57*4882a593Smuzhiyun 	DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
58*4882a593Smuzhiyun 	DRM_FORMAT_MOD_INVALID
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
tegra_plane_offset(struct tegra_plane * plane,unsigned int offset)61*4882a593Smuzhiyun static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
62*4882a593Smuzhiyun 					      unsigned int offset)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	if (offset >= 0x500 && offset <= 0x581) {
65*4882a593Smuzhiyun 		offset = 0x000 + (offset - 0x500);
66*4882a593Smuzhiyun 		return plane->offset + offset;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	if (offset >= 0x700 && offset <= 0x73c) {
70*4882a593Smuzhiyun 		offset = 0x180 + (offset - 0x700);
71*4882a593Smuzhiyun 		return plane->offset + offset;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (offset >= 0x800 && offset <= 0x83e) {
75*4882a593Smuzhiyun 		offset = 0x1c0 + (offset - 0x800);
76*4882a593Smuzhiyun 		return plane->offset + offset;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	return plane->offset + offset;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
tegra_plane_readl(struct tegra_plane * plane,unsigned int offset)84*4882a593Smuzhiyun static inline u32 tegra_plane_readl(struct tegra_plane *plane,
85*4882a593Smuzhiyun 				    unsigned int offset)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
tegra_plane_writel(struct tegra_plane * plane,u32 value,unsigned int offset)90*4882a593Smuzhiyun static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
91*4882a593Smuzhiyun 				      unsigned int offset)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
tegra_windowgroup_enable(struct tegra_windowgroup * wgrp)96*4882a593Smuzhiyun static int tegra_windowgroup_enable(struct tegra_windowgroup *wgrp)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	int err = 0;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	mutex_lock(&wgrp->lock);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	if (wgrp->usecount == 0) {
103*4882a593Smuzhiyun 		err = host1x_client_resume(wgrp->parent);
104*4882a593Smuzhiyun 		if (err < 0) {
105*4882a593Smuzhiyun 			dev_err(wgrp->parent->dev, "failed to resume: %d\n", err);
106*4882a593Smuzhiyun 			goto unlock;
107*4882a593Smuzhiyun 		}
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		reset_control_deassert(wgrp->rst);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	wgrp->usecount++;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun unlock:
115*4882a593Smuzhiyun 	mutex_unlock(&wgrp->lock);
116*4882a593Smuzhiyun 	return err;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
tegra_windowgroup_disable(struct tegra_windowgroup * wgrp)119*4882a593Smuzhiyun static void tegra_windowgroup_disable(struct tegra_windowgroup *wgrp)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	int err;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	mutex_lock(&wgrp->lock);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (wgrp->usecount == 1) {
126*4882a593Smuzhiyun 		err = reset_control_assert(wgrp->rst);
127*4882a593Smuzhiyun 		if (err < 0) {
128*4882a593Smuzhiyun 			pr_err("failed to assert reset for window group %u\n",
129*4882a593Smuzhiyun 			       wgrp->index);
130*4882a593Smuzhiyun 		}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		host1x_client_suspend(wgrp->parent);
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	wgrp->usecount--;
136*4882a593Smuzhiyun 	mutex_unlock(&wgrp->lock);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
tegra_display_hub_prepare(struct tegra_display_hub * hub)139*4882a593Smuzhiyun int tegra_display_hub_prepare(struct tegra_display_hub *hub)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	unsigned int i;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	/*
144*4882a593Smuzhiyun 	 * XXX Enabling/disabling windowgroups needs to happen when the owner
145*4882a593Smuzhiyun 	 * display controller is disabled. There's currently no good point at
146*4882a593Smuzhiyun 	 * which this could be executed, so unconditionally enable all window
147*4882a593Smuzhiyun 	 * groups for now.
148*4882a593Smuzhiyun 	 */
149*4882a593Smuzhiyun 	for (i = 0; i < hub->soc->num_wgrps; i++) {
150*4882a593Smuzhiyun 		struct tegra_windowgroup *wgrp = &hub->wgrps[i];
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		/* Skip orphaned window group whose parent DC is disabled */
153*4882a593Smuzhiyun 		if (wgrp->parent)
154*4882a593Smuzhiyun 			tegra_windowgroup_enable(wgrp);
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
tegra_display_hub_cleanup(struct tegra_display_hub * hub)160*4882a593Smuzhiyun void tegra_display_hub_cleanup(struct tegra_display_hub *hub)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	unsigned int i;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/*
165*4882a593Smuzhiyun 	 * XXX Remove this once window groups can be more fine-grainedly
166*4882a593Smuzhiyun 	 * enabled and disabled.
167*4882a593Smuzhiyun 	 */
168*4882a593Smuzhiyun 	for (i = 0; i < hub->soc->num_wgrps; i++) {
169*4882a593Smuzhiyun 		struct tegra_windowgroup *wgrp = &hub->wgrps[i];
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		/* Skip orphaned window group whose parent DC is disabled */
172*4882a593Smuzhiyun 		if (wgrp->parent)
173*4882a593Smuzhiyun 			tegra_windowgroup_disable(wgrp);
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
tegra_shared_plane_update(struct tegra_plane * plane)177*4882a593Smuzhiyun static void tegra_shared_plane_update(struct tegra_plane *plane)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	struct tegra_dc *dc = plane->dc;
180*4882a593Smuzhiyun 	unsigned long timeout;
181*4882a593Smuzhiyun 	u32 mask, value;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	mask = COMMON_UPDATE | WIN_A_UPDATE << plane->base.index;
184*4882a593Smuzhiyun 	tegra_dc_writel(dc, mask, DC_CMD_STATE_CONTROL);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	timeout = jiffies + msecs_to_jiffies(1000);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	while (time_before(jiffies, timeout)) {
189*4882a593Smuzhiyun 		value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
190*4882a593Smuzhiyun 		if ((value & mask) == 0)
191*4882a593Smuzhiyun 			break;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		usleep_range(100, 400);
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
tegra_shared_plane_activate(struct tegra_plane * plane)197*4882a593Smuzhiyun static void tegra_shared_plane_activate(struct tegra_plane *plane)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	struct tegra_dc *dc = plane->dc;
200*4882a593Smuzhiyun 	unsigned long timeout;
201*4882a593Smuzhiyun 	u32 mask, value;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	mask = COMMON_ACTREQ | WIN_A_ACT_REQ << plane->base.index;
204*4882a593Smuzhiyun 	tegra_dc_writel(dc, mask, DC_CMD_STATE_CONTROL);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	timeout = jiffies + msecs_to_jiffies(1000);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	while (time_before(jiffies, timeout)) {
209*4882a593Smuzhiyun 		value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
210*4882a593Smuzhiyun 		if ((value & mask) == 0)
211*4882a593Smuzhiyun 			break;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		usleep_range(100, 400);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun static unsigned int
tegra_shared_plane_get_owner(struct tegra_plane * plane,struct tegra_dc * dc)218*4882a593Smuzhiyun tegra_shared_plane_get_owner(struct tegra_plane *plane, struct tegra_dc *dc)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	unsigned int offset =
221*4882a593Smuzhiyun 		tegra_plane_offset(plane, DC_WIN_CORE_WINDOWGROUP_SET_CONTROL);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	return tegra_dc_readl(dc, offset) & OWNER_MASK;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
tegra_dc_owns_shared_plane(struct tegra_dc * dc,struct tegra_plane * plane)226*4882a593Smuzhiyun static bool tegra_dc_owns_shared_plane(struct tegra_dc *dc,
227*4882a593Smuzhiyun 				       struct tegra_plane *plane)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	struct device *dev = dc->dev;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	if (tegra_shared_plane_get_owner(plane, dc) == dc->pipe) {
232*4882a593Smuzhiyun 		if (plane->dc == dc)
233*4882a593Smuzhiyun 			return true;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 		dev_WARN(dev, "head %u owns window %u but is not attached\n",
236*4882a593Smuzhiyun 			 dc->pipe, plane->index);
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	return false;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
tegra_shared_plane_set_owner(struct tegra_plane * plane,struct tegra_dc * new)242*4882a593Smuzhiyun static int tegra_shared_plane_set_owner(struct tegra_plane *plane,
243*4882a593Smuzhiyun 					struct tegra_dc *new)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	unsigned int offset =
246*4882a593Smuzhiyun 		tegra_plane_offset(plane, DC_WIN_CORE_WINDOWGROUP_SET_CONTROL);
247*4882a593Smuzhiyun 	struct tegra_dc *old = plane->dc, *dc = new ? new : old;
248*4882a593Smuzhiyun 	struct device *dev = new ? new->dev : old->dev;
249*4882a593Smuzhiyun 	unsigned int owner, index = plane->index;
250*4882a593Smuzhiyun 	u32 value;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	value = tegra_dc_readl(dc, offset);
253*4882a593Smuzhiyun 	owner = value & OWNER_MASK;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (new && (owner != OWNER_MASK && owner != new->pipe)) {
256*4882a593Smuzhiyun 		dev_WARN(dev, "window %u owned by head %u\n", index, owner);
257*4882a593Smuzhiyun 		return -EBUSY;
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/*
261*4882a593Smuzhiyun 	 * This seems to happen whenever the head has been disabled with one
262*4882a593Smuzhiyun 	 * or more windows being active. This is harmless because we'll just
263*4882a593Smuzhiyun 	 * reassign the window to the new head anyway.
264*4882a593Smuzhiyun 	 */
265*4882a593Smuzhiyun 	if (old && owner == OWNER_MASK)
266*4882a593Smuzhiyun 		dev_dbg(dev, "window %u not owned by head %u but %u\n", index,
267*4882a593Smuzhiyun 			old->pipe, owner);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	value &= ~OWNER_MASK;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	if (new)
272*4882a593Smuzhiyun 		value |= OWNER(new->pipe);
273*4882a593Smuzhiyun 	else
274*4882a593Smuzhiyun 		value |= OWNER_MASK;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	tegra_dc_writel(dc, value, offset);
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	plane->dc = new;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
tegra_dc_assign_shared_plane(struct tegra_dc * dc,struct tegra_plane * plane)283*4882a593Smuzhiyun static void tegra_dc_assign_shared_plane(struct tegra_dc *dc,
284*4882a593Smuzhiyun 					 struct tegra_plane *plane)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	u32 value;
287*4882a593Smuzhiyun 	int err;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	if (!tegra_dc_owns_shared_plane(dc, plane)) {
290*4882a593Smuzhiyun 		err = tegra_shared_plane_set_owner(plane, dc);
291*4882a593Smuzhiyun 		if (err < 0)
292*4882a593Smuzhiyun 			return;
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_LINEBUF_CONFIG);
296*4882a593Smuzhiyun 	value |= MODE_FOUR_LINES;
297*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_LINEBUF_CONFIG);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_FETCH_METER);
300*4882a593Smuzhiyun 	value = SLOTS(1);
301*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_FETCH_METER);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/* disable watermark */
304*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLA);
305*4882a593Smuzhiyun 	value &= ~LATENCY_CTL_MODE_ENABLE;
306*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLA);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLB);
309*4882a593Smuzhiyun 	value |= WATERMARK_MASK;
310*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLB);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	/* pipe meter */
313*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_PRECOMP_WGRP_PIPE_METER);
314*4882a593Smuzhiyun 	value = PIPE_METER_INT(0) | PIPE_METER_FRAC(0);
315*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_PRECOMP_WGRP_PIPE_METER);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* mempool entries */
318*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_POOL_CONFIG);
319*4882a593Smuzhiyun 	value = MEMPOOL_ENTRIES(0x331);
320*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_POOL_CONFIG);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_THREAD_GROUP);
323*4882a593Smuzhiyun 	value &= ~THREAD_NUM_MASK;
324*4882a593Smuzhiyun 	value |= THREAD_NUM(plane->base.index);
325*4882a593Smuzhiyun 	value |= THREAD_GROUP_ENABLE;
326*4882a593Smuzhiyun 	tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_THREAD_GROUP);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	tegra_shared_plane_update(plane);
329*4882a593Smuzhiyun 	tegra_shared_plane_activate(plane);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
tegra_dc_remove_shared_plane(struct tegra_dc * dc,struct tegra_plane * plane)332*4882a593Smuzhiyun static void tegra_dc_remove_shared_plane(struct tegra_dc *dc,
333*4882a593Smuzhiyun 					 struct tegra_plane *plane)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	tegra_shared_plane_set_owner(plane, NULL);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
tegra_shared_plane_atomic_check(struct drm_plane * plane,struct drm_plane_state * state)338*4882a593Smuzhiyun static int tegra_shared_plane_atomic_check(struct drm_plane *plane,
339*4882a593Smuzhiyun 					   struct drm_plane_state *state)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
342*4882a593Smuzhiyun 	struct tegra_shared_plane *tegra = to_tegra_shared_plane(plane);
343*4882a593Smuzhiyun 	struct tegra_bo_tiling *tiling = &plane_state->tiling;
344*4882a593Smuzhiyun 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
345*4882a593Smuzhiyun 	int err;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* no need for further checks if the plane is being disabled */
348*4882a593Smuzhiyun 	if (!state->crtc || !state->fb)
349*4882a593Smuzhiyun 		return 0;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	err = tegra_plane_format(state->fb->format->format,
352*4882a593Smuzhiyun 				 &plane_state->format,
353*4882a593Smuzhiyun 				 &plane_state->swap);
354*4882a593Smuzhiyun 	if (err < 0)
355*4882a593Smuzhiyun 		return err;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	err = tegra_fb_get_tiling(state->fb, tiling);
358*4882a593Smuzhiyun 	if (err < 0)
359*4882a593Smuzhiyun 		return err;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
362*4882a593Smuzhiyun 	    !dc->soc->supports_block_linear) {
363*4882a593Smuzhiyun 		DRM_ERROR("hardware doesn't support block linear mode\n");
364*4882a593Smuzhiyun 		return -EINVAL;
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/*
368*4882a593Smuzhiyun 	 * Tegra doesn't support different strides for U and V planes so we
369*4882a593Smuzhiyun 	 * error out if the user tries to display a framebuffer with such a
370*4882a593Smuzhiyun 	 * configuration.
371*4882a593Smuzhiyun 	 */
372*4882a593Smuzhiyun 	if (state->fb->format->num_planes > 2) {
373*4882a593Smuzhiyun 		if (state->fb->pitches[2] != state->fb->pitches[1]) {
374*4882a593Smuzhiyun 			DRM_ERROR("unsupported UV-plane configuration\n");
375*4882a593Smuzhiyun 			return -EINVAL;
376*4882a593Smuzhiyun 		}
377*4882a593Smuzhiyun 	}
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/* XXX scaling is not yet supported, add a check here */
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	err = tegra_plane_state_add(&tegra->base, state);
382*4882a593Smuzhiyun 	if (err < 0)
383*4882a593Smuzhiyun 		return err;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	return 0;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
tegra_shared_plane_atomic_disable(struct drm_plane * plane,struct drm_plane_state * old_state)388*4882a593Smuzhiyun static void tegra_shared_plane_atomic_disable(struct drm_plane *plane,
389*4882a593Smuzhiyun 					      struct drm_plane_state *old_state)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	struct tegra_plane *p = to_tegra_plane(plane);
392*4882a593Smuzhiyun 	struct tegra_dc *dc;
393*4882a593Smuzhiyun 	u32 value;
394*4882a593Smuzhiyun 	int err;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	/* rien ne va plus */
397*4882a593Smuzhiyun 	if (!old_state || !old_state->crtc)
398*4882a593Smuzhiyun 		return;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	dc = to_tegra_dc(old_state->crtc);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	err = host1x_client_resume(&dc->client);
403*4882a593Smuzhiyun 	if (err < 0) {
404*4882a593Smuzhiyun 		dev_err(dc->dev, "failed to resume: %d\n", err);
405*4882a593Smuzhiyun 		return;
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/*
409*4882a593Smuzhiyun 	 * XXX Legacy helpers seem to sometimes call ->atomic_disable() even
410*4882a593Smuzhiyun 	 * on planes that are already disabled. Make sure we fallback to the
411*4882a593Smuzhiyun 	 * head for this particular state instead of crashing.
412*4882a593Smuzhiyun 	 */
413*4882a593Smuzhiyun 	if (WARN_ON(p->dc == NULL))
414*4882a593Smuzhiyun 		p->dc = dc;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
417*4882a593Smuzhiyun 	value &= ~WIN_ENABLE;
418*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	tegra_dc_remove_shared_plane(dc, p);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	host1x_client_suspend(&dc->client);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
tegra_shared_plane_atomic_update(struct drm_plane * plane,struct drm_plane_state * old_state)425*4882a593Smuzhiyun static void tegra_shared_plane_atomic_update(struct drm_plane *plane,
426*4882a593Smuzhiyun 					     struct drm_plane_state *old_state)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
429*4882a593Smuzhiyun 	struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
430*4882a593Smuzhiyun 	unsigned int zpos = plane->state->normalized_zpos;
431*4882a593Smuzhiyun 	struct drm_framebuffer *fb = plane->state->fb;
432*4882a593Smuzhiyun 	struct tegra_plane *p = to_tegra_plane(plane);
433*4882a593Smuzhiyun 	dma_addr_t base;
434*4882a593Smuzhiyun 	u32 value;
435*4882a593Smuzhiyun 	int err;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	/* rien ne va plus */
438*4882a593Smuzhiyun 	if (!plane->state->crtc || !plane->state->fb)
439*4882a593Smuzhiyun 		return;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (!plane->state->visible) {
442*4882a593Smuzhiyun 		tegra_shared_plane_atomic_disable(plane, old_state);
443*4882a593Smuzhiyun 		return;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	err = host1x_client_resume(&dc->client);
447*4882a593Smuzhiyun 	if (err < 0) {
448*4882a593Smuzhiyun 		dev_err(dc->dev, "failed to resume: %d\n", err);
449*4882a593Smuzhiyun 		return;
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	tegra_dc_assign_shared_plane(dc, p);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	tegra_plane_writel(p, VCOUNTER, DC_WIN_CORE_ACT_CONTROL);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	/* blending */
457*4882a593Smuzhiyun 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
458*4882a593Smuzhiyun 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
459*4882a593Smuzhiyun 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
460*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_BLEND_MATCH_SELECT);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
463*4882a593Smuzhiyun 		BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
464*4882a593Smuzhiyun 		BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
465*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_BLEND_NOMATCH_SELECT);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - zpos);
468*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_BLEND_LAYER_CONTROL);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	/* bypass scaling */
471*4882a593Smuzhiyun 	value = HORIZONTAL_TAPS_5 | VERTICAL_TAPS_5;
472*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_WINDOWGROUP_SET_CONTROL_INPUT_SCALER);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	value = INPUT_SCALER_VBYPASS | INPUT_SCALER_HBYPASS;
475*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_WINDOWGROUP_SET_INPUT_SCALER_USAGE);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	/* disable compression */
478*4882a593Smuzhiyun 	tegra_plane_writel(p, 0, DC_WINBUF_CDE_CONTROL);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	base = state->iova[0] + fb->offsets[0];
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	tegra_plane_writel(p, state->format, DC_WIN_COLOR_DEPTH);
483*4882a593Smuzhiyun 	tegra_plane_writel(p, 0, DC_WIN_PRECOMP_WGRP_PARAMS);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	value = V_POSITION(plane->state->crtc_y) |
486*4882a593Smuzhiyun 		H_POSITION(plane->state->crtc_x);
487*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_POSITION);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	value = V_SIZE(plane->state->crtc_h) | H_SIZE(plane->state->crtc_w);
490*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_SIZE);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	value = WIN_ENABLE | COLOR_EXPAND;
493*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	value = V_SIZE(plane->state->crtc_h) | H_SIZE(plane->state->crtc_w);
496*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_CROPPED_SIZE);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	tegra_plane_writel(p, upper_32_bits(base), DC_WINBUF_START_ADDR_HI);
499*4882a593Smuzhiyun 	tegra_plane_writel(p, lower_32_bits(base), DC_WINBUF_START_ADDR);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	value = PITCH(fb->pitches[0]);
502*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_PLANAR_STORAGE);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	value = CLAMP_BEFORE_BLEND | DEGAMMA_SRGB | INPUT_RANGE_FULL;
505*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_SET_PARAMS);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	value = OFFSET_X(plane->state->src_y >> 16) |
508*4882a593Smuzhiyun 		OFFSET_Y(plane->state->src_x >> 16);
509*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WINBUF_CROPPED_POINT);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if (dc->soc->supports_block_linear) {
512*4882a593Smuzhiyun 		unsigned long height = state->tiling.value;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 		/* XXX */
515*4882a593Smuzhiyun 		switch (state->tiling.mode) {
516*4882a593Smuzhiyun 		case TEGRA_BO_TILING_MODE_PITCH:
517*4882a593Smuzhiyun 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(0) |
518*4882a593Smuzhiyun 				DC_WINBUF_SURFACE_KIND_PITCH;
519*4882a593Smuzhiyun 			break;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 		/* XXX not supported on Tegra186 and later */
522*4882a593Smuzhiyun 		case TEGRA_BO_TILING_MODE_TILED:
523*4882a593Smuzhiyun 			value = DC_WINBUF_SURFACE_KIND_TILED;
524*4882a593Smuzhiyun 			break;
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 		case TEGRA_BO_TILING_MODE_BLOCK:
527*4882a593Smuzhiyun 			value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
528*4882a593Smuzhiyun 				DC_WINBUF_SURFACE_KIND_BLOCK;
529*4882a593Smuzhiyun 			break;
530*4882a593Smuzhiyun 		}
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 		tegra_plane_writel(p, value, DC_WINBUF_SURFACE_KIND);
533*4882a593Smuzhiyun 	}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/* disable gamut CSC */
536*4882a593Smuzhiyun 	value = tegra_plane_readl(p, DC_WIN_WINDOW_SET_CONTROL);
537*4882a593Smuzhiyun 	value &= ~CONTROL_CSC_ENABLE;
538*4882a593Smuzhiyun 	tegra_plane_writel(p, value, DC_WIN_WINDOW_SET_CONTROL);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	host1x_client_suspend(&dc->client);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun static const struct drm_plane_helper_funcs tegra_shared_plane_helper_funcs = {
544*4882a593Smuzhiyun 	.prepare_fb = tegra_plane_prepare_fb,
545*4882a593Smuzhiyun 	.cleanup_fb = tegra_plane_cleanup_fb,
546*4882a593Smuzhiyun 	.atomic_check = tegra_shared_plane_atomic_check,
547*4882a593Smuzhiyun 	.atomic_update = tegra_shared_plane_atomic_update,
548*4882a593Smuzhiyun 	.atomic_disable = tegra_shared_plane_atomic_disable,
549*4882a593Smuzhiyun };
550*4882a593Smuzhiyun 
tegra_shared_plane_create(struct drm_device * drm,struct tegra_dc * dc,unsigned int wgrp,unsigned int index)551*4882a593Smuzhiyun struct drm_plane *tegra_shared_plane_create(struct drm_device *drm,
552*4882a593Smuzhiyun 					    struct tegra_dc *dc,
553*4882a593Smuzhiyun 					    unsigned int wgrp,
554*4882a593Smuzhiyun 					    unsigned int index)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
557*4882a593Smuzhiyun 	struct tegra_drm *tegra = drm->dev_private;
558*4882a593Smuzhiyun 	struct tegra_display_hub *hub = tegra->hub;
559*4882a593Smuzhiyun 	/* planes can be assigned to arbitrary CRTCs */
560*4882a593Smuzhiyun 	unsigned int possible_crtcs = 0x7;
561*4882a593Smuzhiyun 	struct tegra_shared_plane *plane;
562*4882a593Smuzhiyun 	unsigned int num_formats;
563*4882a593Smuzhiyun 	const u64 *modifiers;
564*4882a593Smuzhiyun 	struct drm_plane *p;
565*4882a593Smuzhiyun 	const u32 *formats;
566*4882a593Smuzhiyun 	int err;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
569*4882a593Smuzhiyun 	if (!plane)
570*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	plane->base.offset = 0x0a00 + 0x0300 * index;
573*4882a593Smuzhiyun 	plane->base.index = index;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	plane->wgrp = &hub->wgrps[wgrp];
576*4882a593Smuzhiyun 	plane->wgrp->parent = &dc->client;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	p = &plane->base.base;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	num_formats = ARRAY_SIZE(tegra_shared_plane_formats);
581*4882a593Smuzhiyun 	formats = tegra_shared_plane_formats;
582*4882a593Smuzhiyun 	modifiers = tegra_shared_plane_modifiers;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	err = drm_universal_plane_init(drm, p, possible_crtcs,
585*4882a593Smuzhiyun 				       &tegra_plane_funcs, formats,
586*4882a593Smuzhiyun 				       num_formats, modifiers, type, NULL);
587*4882a593Smuzhiyun 	if (err < 0) {
588*4882a593Smuzhiyun 		kfree(plane);
589*4882a593Smuzhiyun 		return ERR_PTR(err);
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	drm_plane_helper_add(p, &tegra_shared_plane_helper_funcs);
593*4882a593Smuzhiyun 	drm_plane_create_zpos_property(p, 0, 0, 255);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	return p;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun static struct drm_private_state *
tegra_display_hub_duplicate_state(struct drm_private_obj * obj)599*4882a593Smuzhiyun tegra_display_hub_duplicate_state(struct drm_private_obj *obj)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun 	struct tegra_display_hub_state *state;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
604*4882a593Smuzhiyun 	if (!state)
605*4882a593Smuzhiyun 		return NULL;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	return &state->base;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun 
tegra_display_hub_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)612*4882a593Smuzhiyun static void tegra_display_hub_destroy_state(struct drm_private_obj *obj,
613*4882a593Smuzhiyun 					    struct drm_private_state *state)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun 	struct tegra_display_hub_state *hub_state =
616*4882a593Smuzhiyun 		to_tegra_display_hub_state(state);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	kfree(hub_state);
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun static const struct drm_private_state_funcs tegra_display_hub_state_funcs = {
622*4882a593Smuzhiyun 	.atomic_duplicate_state = tegra_display_hub_duplicate_state,
623*4882a593Smuzhiyun 	.atomic_destroy_state = tegra_display_hub_destroy_state,
624*4882a593Smuzhiyun };
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun static struct tegra_display_hub_state *
tegra_display_hub_get_state(struct tegra_display_hub * hub,struct drm_atomic_state * state)627*4882a593Smuzhiyun tegra_display_hub_get_state(struct tegra_display_hub *hub,
628*4882a593Smuzhiyun 			    struct drm_atomic_state *state)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	struct drm_private_state *priv;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	priv = drm_atomic_get_private_obj_state(state, &hub->base);
633*4882a593Smuzhiyun 	if (IS_ERR(priv))
634*4882a593Smuzhiyun 		return ERR_CAST(priv);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	return to_tegra_display_hub_state(priv);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
tegra_display_hub_atomic_check(struct drm_device * drm,struct drm_atomic_state * state)639*4882a593Smuzhiyun int tegra_display_hub_atomic_check(struct drm_device *drm,
640*4882a593Smuzhiyun 				   struct drm_atomic_state *state)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun 	struct tegra_drm *tegra = drm->dev_private;
643*4882a593Smuzhiyun 	struct tegra_display_hub_state *hub_state;
644*4882a593Smuzhiyun 	struct drm_crtc_state *old, *new;
645*4882a593Smuzhiyun 	struct drm_crtc *crtc;
646*4882a593Smuzhiyun 	unsigned int i;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	if (!tegra->hub)
649*4882a593Smuzhiyun 		return 0;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	hub_state = tegra_display_hub_get_state(tegra->hub, state);
652*4882a593Smuzhiyun 	if (IS_ERR(hub_state))
653*4882a593Smuzhiyun 		return PTR_ERR(hub_state);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	/*
656*4882a593Smuzhiyun 	 * The display hub display clock needs to be fed by the display clock
657*4882a593Smuzhiyun 	 * with the highest frequency to ensure proper functioning of all the
658*4882a593Smuzhiyun 	 * displays.
659*4882a593Smuzhiyun 	 *
660*4882a593Smuzhiyun 	 * Note that this isn't used before Tegra186, but it doesn't hurt and
661*4882a593Smuzhiyun 	 * conditionalizing it would make the code less clean.
662*4882a593Smuzhiyun 	 */
663*4882a593Smuzhiyun 	for_each_oldnew_crtc_in_state(state, crtc, old, new, i) {
664*4882a593Smuzhiyun 		struct tegra_dc_state *dc = to_dc_state(new);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 		if (new->active) {
667*4882a593Smuzhiyun 			if (!hub_state->clk || dc->pclk > hub_state->rate) {
668*4882a593Smuzhiyun 				hub_state->dc = to_tegra_dc(dc->base.crtc);
669*4882a593Smuzhiyun 				hub_state->clk = hub_state->dc->clk;
670*4882a593Smuzhiyun 				hub_state->rate = dc->pclk;
671*4882a593Smuzhiyun 			}
672*4882a593Smuzhiyun 		}
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	return 0;
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun 
tegra_display_hub_update(struct tegra_dc * dc)678*4882a593Smuzhiyun static void tegra_display_hub_update(struct tegra_dc *dc)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	u32 value;
681*4882a593Smuzhiyun 	int err;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	err = host1x_client_resume(&dc->client);
684*4882a593Smuzhiyun 	if (err < 0) {
685*4882a593Smuzhiyun 		dev_err(dc->dev, "failed to resume: %d\n", err);
686*4882a593Smuzhiyun 		return;
687*4882a593Smuzhiyun 	}
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	value = tegra_dc_readl(dc, DC_CMD_IHUB_COMMON_MISC_CTL);
690*4882a593Smuzhiyun 	value &= ~LATENCY_EVENT;
691*4882a593Smuzhiyun 	tegra_dc_writel(dc, value, DC_CMD_IHUB_COMMON_MISC_CTL);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	value = tegra_dc_readl(dc, DC_DISP_IHUB_COMMON_DISPLAY_FETCH_METER);
694*4882a593Smuzhiyun 	value = CURS_SLOTS(1) | WGRP_SLOTS(1);
695*4882a593Smuzhiyun 	tegra_dc_writel(dc, value, DC_DISP_IHUB_COMMON_DISPLAY_FETCH_METER);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	tegra_dc_writel(dc, COMMON_UPDATE, DC_CMD_STATE_CONTROL);
698*4882a593Smuzhiyun 	tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
699*4882a593Smuzhiyun 	tegra_dc_writel(dc, COMMON_ACTREQ, DC_CMD_STATE_CONTROL);
700*4882a593Smuzhiyun 	tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	host1x_client_suspend(&dc->client);
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
tegra_display_hub_atomic_commit(struct drm_device * drm,struct drm_atomic_state * state)705*4882a593Smuzhiyun void tegra_display_hub_atomic_commit(struct drm_device *drm,
706*4882a593Smuzhiyun 				     struct drm_atomic_state *state)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun 	struct tegra_drm *tegra = drm->dev_private;
709*4882a593Smuzhiyun 	struct tegra_display_hub *hub = tegra->hub;
710*4882a593Smuzhiyun 	struct tegra_display_hub_state *hub_state;
711*4882a593Smuzhiyun 	struct device *dev = hub->client.dev;
712*4882a593Smuzhiyun 	int err;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	hub_state = to_tegra_display_hub_state(hub->base.state);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	if (hub_state->clk) {
717*4882a593Smuzhiyun 		err = clk_set_rate(hub_state->clk, hub_state->rate);
718*4882a593Smuzhiyun 		if (err < 0)
719*4882a593Smuzhiyun 			dev_err(dev, "failed to set rate of %pC to %lu Hz\n",
720*4882a593Smuzhiyun 				hub_state->clk, hub_state->rate);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 		err = clk_set_parent(hub->clk_disp, hub_state->clk);
723*4882a593Smuzhiyun 		if (err < 0)
724*4882a593Smuzhiyun 			dev_err(dev, "failed to set parent of %pC to %pC: %d\n",
725*4882a593Smuzhiyun 				hub->clk_disp, hub_state->clk, err);
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (hub_state->dc)
729*4882a593Smuzhiyun 		tegra_display_hub_update(hub_state->dc);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun 
tegra_display_hub_init(struct host1x_client * client)732*4882a593Smuzhiyun static int tegra_display_hub_init(struct host1x_client *client)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	struct tegra_display_hub *hub = to_tegra_display_hub(client);
735*4882a593Smuzhiyun 	struct drm_device *drm = dev_get_drvdata(client->host);
736*4882a593Smuzhiyun 	struct tegra_drm *tegra = drm->dev_private;
737*4882a593Smuzhiyun 	struct tegra_display_hub_state *state;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	state = kzalloc(sizeof(*state), GFP_KERNEL);
740*4882a593Smuzhiyun 	if (!state)
741*4882a593Smuzhiyun 		return -ENOMEM;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	drm_atomic_private_obj_init(drm, &hub->base, &state->base,
744*4882a593Smuzhiyun 				    &tegra_display_hub_state_funcs);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	tegra->hub = hub;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	return 0;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
tegra_display_hub_exit(struct host1x_client * client)751*4882a593Smuzhiyun static int tegra_display_hub_exit(struct host1x_client *client)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	struct drm_device *drm = dev_get_drvdata(client->host);
754*4882a593Smuzhiyun 	struct tegra_drm *tegra = drm->dev_private;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	drm_atomic_private_obj_fini(&tegra->hub->base);
757*4882a593Smuzhiyun 	tegra->hub = NULL;
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	return 0;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun 
tegra_display_hub_runtime_suspend(struct host1x_client * client)762*4882a593Smuzhiyun static int tegra_display_hub_runtime_suspend(struct host1x_client *client)
763*4882a593Smuzhiyun {
764*4882a593Smuzhiyun 	struct tegra_display_hub *hub = to_tegra_display_hub(client);
765*4882a593Smuzhiyun 	struct device *dev = client->dev;
766*4882a593Smuzhiyun 	unsigned int i = hub->num_heads;
767*4882a593Smuzhiyun 	int err;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	err = reset_control_assert(hub->rst);
770*4882a593Smuzhiyun 	if (err < 0)
771*4882a593Smuzhiyun 		return err;
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	while (i--)
774*4882a593Smuzhiyun 		clk_disable_unprepare(hub->clk_heads[i]);
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	clk_disable_unprepare(hub->clk_hub);
777*4882a593Smuzhiyun 	clk_disable_unprepare(hub->clk_dsc);
778*4882a593Smuzhiyun 	clk_disable_unprepare(hub->clk_disp);
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	pm_runtime_put_sync(dev);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	return 0;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
tegra_display_hub_runtime_resume(struct host1x_client * client)785*4882a593Smuzhiyun static int tegra_display_hub_runtime_resume(struct host1x_client *client)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	struct tegra_display_hub *hub = to_tegra_display_hub(client);
788*4882a593Smuzhiyun 	struct device *dev = client->dev;
789*4882a593Smuzhiyun 	unsigned int i;
790*4882a593Smuzhiyun 	int err;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	err = pm_runtime_resume_and_get(dev);
793*4882a593Smuzhiyun 	if (err < 0) {
794*4882a593Smuzhiyun 		dev_err(dev, "failed to get runtime PM: %d\n", err);
795*4882a593Smuzhiyun 		return err;
796*4882a593Smuzhiyun 	}
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	err = clk_prepare_enable(hub->clk_disp);
799*4882a593Smuzhiyun 	if (err < 0)
800*4882a593Smuzhiyun 		goto put_rpm;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	err = clk_prepare_enable(hub->clk_dsc);
803*4882a593Smuzhiyun 	if (err < 0)
804*4882a593Smuzhiyun 		goto disable_disp;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	err = clk_prepare_enable(hub->clk_hub);
807*4882a593Smuzhiyun 	if (err < 0)
808*4882a593Smuzhiyun 		goto disable_dsc;
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	for (i = 0; i < hub->num_heads; i++) {
811*4882a593Smuzhiyun 		err = clk_prepare_enable(hub->clk_heads[i]);
812*4882a593Smuzhiyun 		if (err < 0)
813*4882a593Smuzhiyun 			goto disable_heads;
814*4882a593Smuzhiyun 	}
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	err = reset_control_deassert(hub->rst);
817*4882a593Smuzhiyun 	if (err < 0)
818*4882a593Smuzhiyun 		goto disable_heads;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	return 0;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun disable_heads:
823*4882a593Smuzhiyun 	while (i--)
824*4882a593Smuzhiyun 		clk_disable_unprepare(hub->clk_heads[i]);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	clk_disable_unprepare(hub->clk_hub);
827*4882a593Smuzhiyun disable_dsc:
828*4882a593Smuzhiyun 	clk_disable_unprepare(hub->clk_dsc);
829*4882a593Smuzhiyun disable_disp:
830*4882a593Smuzhiyun 	clk_disable_unprepare(hub->clk_disp);
831*4882a593Smuzhiyun put_rpm:
832*4882a593Smuzhiyun 	pm_runtime_put_sync(dev);
833*4882a593Smuzhiyun 	return err;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun static const struct host1x_client_ops tegra_display_hub_ops = {
837*4882a593Smuzhiyun 	.init = tegra_display_hub_init,
838*4882a593Smuzhiyun 	.exit = tegra_display_hub_exit,
839*4882a593Smuzhiyun 	.suspend = tegra_display_hub_runtime_suspend,
840*4882a593Smuzhiyun 	.resume = tegra_display_hub_runtime_resume,
841*4882a593Smuzhiyun };
842*4882a593Smuzhiyun 
tegra_display_hub_probe(struct platform_device * pdev)843*4882a593Smuzhiyun static int tegra_display_hub_probe(struct platform_device *pdev)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	struct device_node *child = NULL;
846*4882a593Smuzhiyun 	struct tegra_display_hub *hub;
847*4882a593Smuzhiyun 	struct clk *clk;
848*4882a593Smuzhiyun 	unsigned int i;
849*4882a593Smuzhiyun 	int err;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	hub = devm_kzalloc(&pdev->dev, sizeof(*hub), GFP_KERNEL);
852*4882a593Smuzhiyun 	if (!hub)
853*4882a593Smuzhiyun 		return -ENOMEM;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	hub->soc = of_device_get_match_data(&pdev->dev);
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	hub->clk_disp = devm_clk_get(&pdev->dev, "disp");
858*4882a593Smuzhiyun 	if (IS_ERR(hub->clk_disp)) {
859*4882a593Smuzhiyun 		err = PTR_ERR(hub->clk_disp);
860*4882a593Smuzhiyun 		return err;
861*4882a593Smuzhiyun 	}
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	if (hub->soc->supports_dsc) {
864*4882a593Smuzhiyun 		hub->clk_dsc = devm_clk_get(&pdev->dev, "dsc");
865*4882a593Smuzhiyun 		if (IS_ERR(hub->clk_dsc)) {
866*4882a593Smuzhiyun 			err = PTR_ERR(hub->clk_dsc);
867*4882a593Smuzhiyun 			return err;
868*4882a593Smuzhiyun 		}
869*4882a593Smuzhiyun 	}
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	hub->clk_hub = devm_clk_get(&pdev->dev, "hub");
872*4882a593Smuzhiyun 	if (IS_ERR(hub->clk_hub)) {
873*4882a593Smuzhiyun 		err = PTR_ERR(hub->clk_hub);
874*4882a593Smuzhiyun 		return err;
875*4882a593Smuzhiyun 	}
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	hub->rst = devm_reset_control_get(&pdev->dev, "misc");
878*4882a593Smuzhiyun 	if (IS_ERR(hub->rst)) {
879*4882a593Smuzhiyun 		err = PTR_ERR(hub->rst);
880*4882a593Smuzhiyun 		return err;
881*4882a593Smuzhiyun 	}
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	hub->wgrps = devm_kcalloc(&pdev->dev, hub->soc->num_wgrps,
884*4882a593Smuzhiyun 				  sizeof(*hub->wgrps), GFP_KERNEL);
885*4882a593Smuzhiyun 	if (!hub->wgrps)
886*4882a593Smuzhiyun 		return -ENOMEM;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	for (i = 0; i < hub->soc->num_wgrps; i++) {
889*4882a593Smuzhiyun 		struct tegra_windowgroup *wgrp = &hub->wgrps[i];
890*4882a593Smuzhiyun 		char id[8];
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 		snprintf(id, sizeof(id), "wgrp%u", i);
893*4882a593Smuzhiyun 		mutex_init(&wgrp->lock);
894*4882a593Smuzhiyun 		wgrp->usecount = 0;
895*4882a593Smuzhiyun 		wgrp->index = i;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		wgrp->rst = devm_reset_control_get(&pdev->dev, id);
898*4882a593Smuzhiyun 		if (IS_ERR(wgrp->rst))
899*4882a593Smuzhiyun 			return PTR_ERR(wgrp->rst);
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 		err = reset_control_assert(wgrp->rst);
902*4882a593Smuzhiyun 		if (err < 0)
903*4882a593Smuzhiyun 			return err;
904*4882a593Smuzhiyun 	}
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	hub->num_heads = of_get_child_count(pdev->dev.of_node);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	hub->clk_heads = devm_kcalloc(&pdev->dev, hub->num_heads, sizeof(clk),
909*4882a593Smuzhiyun 				      GFP_KERNEL);
910*4882a593Smuzhiyun 	if (!hub->clk_heads)
911*4882a593Smuzhiyun 		return -ENOMEM;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	for (i = 0; i < hub->num_heads; i++) {
914*4882a593Smuzhiyun 		child = of_get_next_child(pdev->dev.of_node, child);
915*4882a593Smuzhiyun 		if (!child) {
916*4882a593Smuzhiyun 			dev_err(&pdev->dev, "failed to find node for head %u\n",
917*4882a593Smuzhiyun 				i);
918*4882a593Smuzhiyun 			return -ENODEV;
919*4882a593Smuzhiyun 		}
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 		clk = devm_get_clk_from_child(&pdev->dev, child, "dc");
922*4882a593Smuzhiyun 		if (IS_ERR(clk)) {
923*4882a593Smuzhiyun 			dev_err(&pdev->dev, "failed to get clock for head %u\n",
924*4882a593Smuzhiyun 				i);
925*4882a593Smuzhiyun 			of_node_put(child);
926*4882a593Smuzhiyun 			return PTR_ERR(clk);
927*4882a593Smuzhiyun 		}
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 		hub->clk_heads[i] = clk;
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	of_node_put(child);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	/* XXX: enable clock across reset? */
935*4882a593Smuzhiyun 	err = reset_control_assert(hub->rst);
936*4882a593Smuzhiyun 	if (err < 0)
937*4882a593Smuzhiyun 		return err;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	platform_set_drvdata(pdev, hub);
940*4882a593Smuzhiyun 	pm_runtime_enable(&pdev->dev);
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	INIT_LIST_HEAD(&hub->client.list);
943*4882a593Smuzhiyun 	hub->client.ops = &tegra_display_hub_ops;
944*4882a593Smuzhiyun 	hub->client.dev = &pdev->dev;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	err = host1x_client_register(&hub->client);
947*4882a593Smuzhiyun 	if (err < 0)
948*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
949*4882a593Smuzhiyun 			err);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	err = devm_of_platform_populate(&pdev->dev);
952*4882a593Smuzhiyun 	if (err < 0)
953*4882a593Smuzhiyun 		goto unregister;
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	return err;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun unregister:
958*4882a593Smuzhiyun 	host1x_client_unregister(&hub->client);
959*4882a593Smuzhiyun 	pm_runtime_disable(&pdev->dev);
960*4882a593Smuzhiyun 	return err;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun 
tegra_display_hub_remove(struct platform_device * pdev)963*4882a593Smuzhiyun static int tegra_display_hub_remove(struct platform_device *pdev)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	struct tegra_display_hub *hub = platform_get_drvdata(pdev);
966*4882a593Smuzhiyun 	unsigned int i;
967*4882a593Smuzhiyun 	int err;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	err = host1x_client_unregister(&hub->client);
970*4882a593Smuzhiyun 	if (err < 0) {
971*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
972*4882a593Smuzhiyun 			err);
973*4882a593Smuzhiyun 	}
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	for (i = 0; i < hub->soc->num_wgrps; i++) {
976*4882a593Smuzhiyun 		struct tegra_windowgroup *wgrp = &hub->wgrps[i];
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 		mutex_destroy(&wgrp->lock);
979*4882a593Smuzhiyun 	}
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	pm_runtime_disable(&pdev->dev);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	return err;
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun static const struct tegra_display_hub_soc tegra186_display_hub = {
987*4882a593Smuzhiyun 	.num_wgrps = 6,
988*4882a593Smuzhiyun 	.supports_dsc = true,
989*4882a593Smuzhiyun };
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun static const struct tegra_display_hub_soc tegra194_display_hub = {
992*4882a593Smuzhiyun 	.num_wgrps = 6,
993*4882a593Smuzhiyun 	.supports_dsc = false,
994*4882a593Smuzhiyun };
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun static const struct of_device_id tegra_display_hub_of_match[] = {
997*4882a593Smuzhiyun 	{
998*4882a593Smuzhiyun 		.compatible = "nvidia,tegra194-display",
999*4882a593Smuzhiyun 		.data = &tegra194_display_hub
1000*4882a593Smuzhiyun 	}, {
1001*4882a593Smuzhiyun 		.compatible = "nvidia,tegra186-display",
1002*4882a593Smuzhiyun 		.data = &tegra186_display_hub
1003*4882a593Smuzhiyun 	}, {
1004*4882a593Smuzhiyun 		/* sentinel */
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun };
1007*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tegra_display_hub_of_match);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun struct platform_driver tegra_display_hub_driver = {
1010*4882a593Smuzhiyun 	.driver = {
1011*4882a593Smuzhiyun 		.name = "tegra-display-hub",
1012*4882a593Smuzhiyun 		.of_match_table = tegra_display_hub_of_match,
1013*4882a593Smuzhiyun 	},
1014*4882a593Smuzhiyun 	.probe = tegra_display_hub_probe,
1015*4882a593Smuzhiyun 	.remove = tegra_display_hub_remove,
1016*4882a593Smuzhiyun };
1017