xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/omapdrm/omap_crtc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4*4882a593Smuzhiyun  * Author: Rob Clark <rob@ti.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/math64.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <drm/drm_atomic.h>
10*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
11*4882a593Smuzhiyun #include <drm/drm_crtc.h>
12*4882a593Smuzhiyun #include <drm/drm_mode.h>
13*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
14*4882a593Smuzhiyun #include <drm/drm_vblank.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "omap_drv.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct omap_crtc_state {
21*4882a593Smuzhiyun 	/* Must be first. */
22*4882a593Smuzhiyun 	struct drm_crtc_state base;
23*4882a593Smuzhiyun 	/* Shadow values for legacy userspace support. */
24*4882a593Smuzhiyun 	unsigned int rotation;
25*4882a593Smuzhiyun 	unsigned int zpos;
26*4882a593Smuzhiyun 	bool manually_updated;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun struct omap_crtc {
32*4882a593Smuzhiyun 	struct drm_crtc base;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	const char *name;
35*4882a593Smuzhiyun 	struct omap_drm_pipeline *pipe;
36*4882a593Smuzhiyun 	enum omap_channel channel;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	struct videomode vm;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	bool ignore_digit_sync_lost;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	bool enabled;
43*4882a593Smuzhiyun 	bool pending;
44*4882a593Smuzhiyun 	wait_queue_head_t pending_wait;
45*4882a593Smuzhiyun 	struct drm_pending_vblank_event *event;
46*4882a593Smuzhiyun 	struct delayed_work update_work;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	void (*framedone_handler)(void *);
49*4882a593Smuzhiyun 	void *framedone_handler_data;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
53*4882a593Smuzhiyun  * Helper Functions
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun 
omap_crtc_timings(struct drm_crtc * crtc)56*4882a593Smuzhiyun struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
59*4882a593Smuzhiyun 	return &omap_crtc->vm;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
omap_crtc_channel(struct drm_crtc * crtc)62*4882a593Smuzhiyun enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
65*4882a593Smuzhiyun 	return omap_crtc->channel;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
omap_crtc_is_pending(struct drm_crtc * crtc)68*4882a593Smuzhiyun static bool omap_crtc_is_pending(struct drm_crtc *crtc)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
71*4882a593Smuzhiyun 	unsigned long flags;
72*4882a593Smuzhiyun 	bool pending;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
75*4882a593Smuzhiyun 	pending = omap_crtc->pending;
76*4882a593Smuzhiyun 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	return pending;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
omap_crtc_wait_pending(struct drm_crtc * crtc)81*4882a593Smuzhiyun int omap_crtc_wait_pending(struct drm_crtc *crtc)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/*
86*4882a593Smuzhiyun 	 * Timeout is set to a "sufficiently" high value, which should cover
87*4882a593Smuzhiyun 	 * a single frame refresh even on slower displays.
88*4882a593Smuzhiyun 	 */
89*4882a593Smuzhiyun 	return wait_event_timeout(omap_crtc->pending_wait,
90*4882a593Smuzhiyun 				  !omap_crtc_is_pending(crtc),
91*4882a593Smuzhiyun 				  msecs_to_jiffies(250));
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
95*4882a593Smuzhiyun  * DSS Manager Functions
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun  * Manager-ops, callbacks from output when they need to configure
100*4882a593Smuzhiyun  * the upstream part of the video pipe.
101*4882a593Smuzhiyun  */
102*4882a593Smuzhiyun 
omap_crtc_dss_start_update(struct omap_drm_private * priv,enum omap_channel channel)103*4882a593Smuzhiyun static void omap_crtc_dss_start_update(struct omap_drm_private *priv,
104*4882a593Smuzhiyun 				       enum omap_channel channel)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	priv->dispc_ops->mgr_enable(priv->dispc, channel, true);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* Called only from the encoder enable/disable and suspend/resume handlers. */
omap_crtc_set_enabled(struct drm_crtc * crtc,bool enable)110*4882a593Smuzhiyun static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
113*4882a593Smuzhiyun 	struct drm_device *dev = crtc->dev;
114*4882a593Smuzhiyun 	struct omap_drm_private *priv = dev->dev_private;
115*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
116*4882a593Smuzhiyun 	enum omap_channel channel = omap_crtc->channel;
117*4882a593Smuzhiyun 	struct omap_irq_wait *wait;
118*4882a593Smuzhiyun 	u32 framedone_irq, vsync_irq;
119*4882a593Smuzhiyun 	int ret;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (WARN_ON(omap_crtc->enabled == enable))
122*4882a593Smuzhiyun 		return;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	if (omap_state->manually_updated) {
125*4882a593Smuzhiyun 		omap_irq_enable_framedone(crtc, enable);
126*4882a593Smuzhiyun 		omap_crtc->enabled = enable;
127*4882a593Smuzhiyun 		return;
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (omap_crtc->pipe->output->type == OMAP_DISPLAY_TYPE_HDMI) {
131*4882a593Smuzhiyun 		priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
132*4882a593Smuzhiyun 		omap_crtc->enabled = enable;
133*4882a593Smuzhiyun 		return;
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
137*4882a593Smuzhiyun 		/*
138*4882a593Smuzhiyun 		 * Digit output produces some sync lost interrupts during the
139*4882a593Smuzhiyun 		 * first frame when enabling, so we need to ignore those.
140*4882a593Smuzhiyun 		 */
141*4882a593Smuzhiyun 		omap_crtc->ignore_digit_sync_lost = true;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	framedone_irq = priv->dispc_ops->mgr_get_framedone_irq(priv->dispc,
145*4882a593Smuzhiyun 							       channel);
146*4882a593Smuzhiyun 	vsync_irq = priv->dispc_ops->mgr_get_vsync_irq(priv->dispc, channel);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	if (enable) {
149*4882a593Smuzhiyun 		wait = omap_irq_wait_init(dev, vsync_irq, 1);
150*4882a593Smuzhiyun 	} else {
151*4882a593Smuzhiyun 		/*
152*4882a593Smuzhiyun 		 * When we disable the digit output, we need to wait for
153*4882a593Smuzhiyun 		 * FRAMEDONE to know that DISPC has finished with the output.
154*4882a593Smuzhiyun 		 *
155*4882a593Smuzhiyun 		 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
156*4882a593Smuzhiyun 		 * that case we need to use vsync interrupt, and wait for both
157*4882a593Smuzhiyun 		 * even and odd frames.
158*4882a593Smuzhiyun 		 */
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		if (framedone_irq)
161*4882a593Smuzhiyun 			wait = omap_irq_wait_init(dev, framedone_irq, 1);
162*4882a593Smuzhiyun 		else
163*4882a593Smuzhiyun 			wait = omap_irq_wait_init(dev, vsync_irq, 2);
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
167*4882a593Smuzhiyun 	omap_crtc->enabled = enable;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
170*4882a593Smuzhiyun 	if (ret) {
171*4882a593Smuzhiyun 		dev_err(dev->dev, "%s: timeout waiting for %s\n",
172*4882a593Smuzhiyun 				omap_crtc->name, enable ? "enable" : "disable");
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
176*4882a593Smuzhiyun 		omap_crtc->ignore_digit_sync_lost = false;
177*4882a593Smuzhiyun 		/* make sure the irq handler sees the value above */
178*4882a593Smuzhiyun 		mb();
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 
omap_crtc_dss_enable(struct omap_drm_private * priv,enum omap_channel channel)183*4882a593Smuzhiyun static int omap_crtc_dss_enable(struct omap_drm_private *priv,
184*4882a593Smuzhiyun 				enum omap_channel channel)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct drm_crtc *crtc = priv->channels[channel]->crtc;
187*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	priv->dispc_ops->mgr_set_timings(priv->dispc, omap_crtc->channel,
190*4882a593Smuzhiyun 					 &omap_crtc->vm);
191*4882a593Smuzhiyun 	omap_crtc_set_enabled(&omap_crtc->base, true);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	return 0;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
omap_crtc_dss_disable(struct omap_drm_private * priv,enum omap_channel channel)196*4882a593Smuzhiyun static void omap_crtc_dss_disable(struct omap_drm_private *priv,
197*4882a593Smuzhiyun 				  enum omap_channel channel)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	struct drm_crtc *crtc = priv->channels[channel]->crtc;
200*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	omap_crtc_set_enabled(&omap_crtc->base, false);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
omap_crtc_dss_set_timings(struct omap_drm_private * priv,enum omap_channel channel,const struct videomode * vm)205*4882a593Smuzhiyun static void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
206*4882a593Smuzhiyun 		enum omap_channel channel,
207*4882a593Smuzhiyun 		const struct videomode *vm)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	struct drm_crtc *crtc = priv->channels[channel]->crtc;
210*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	DBG("%s", omap_crtc->name);
213*4882a593Smuzhiyun 	omap_crtc->vm = *vm;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
omap_crtc_dss_set_lcd_config(struct omap_drm_private * priv,enum omap_channel channel,const struct dss_lcd_mgr_config * config)216*4882a593Smuzhiyun static void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
217*4882a593Smuzhiyun 		enum omap_channel channel,
218*4882a593Smuzhiyun 		const struct dss_lcd_mgr_config *config)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct drm_crtc *crtc = priv->channels[channel]->crtc;
221*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	DBG("%s", omap_crtc->name);
224*4882a593Smuzhiyun 	priv->dispc_ops->mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
225*4882a593Smuzhiyun 					    config);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
omap_crtc_dss_register_framedone(struct omap_drm_private * priv,enum omap_channel channel,void (* handler)(void *),void * data)228*4882a593Smuzhiyun static int omap_crtc_dss_register_framedone(
229*4882a593Smuzhiyun 		struct omap_drm_private *priv, enum omap_channel channel,
230*4882a593Smuzhiyun 		void (*handler)(void *), void *data)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct drm_crtc *crtc = priv->channels[channel]->crtc;
233*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
234*4882a593Smuzhiyun 	struct drm_device *dev = omap_crtc->base.dev;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (omap_crtc->framedone_handler)
237*4882a593Smuzhiyun 		return -EBUSY;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	dev_dbg(dev->dev, "register framedone %s", omap_crtc->name);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	omap_crtc->framedone_handler = handler;
242*4882a593Smuzhiyun 	omap_crtc->framedone_handler_data = data;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
omap_crtc_dss_unregister_framedone(struct omap_drm_private * priv,enum omap_channel channel,void (* handler)(void *),void * data)247*4882a593Smuzhiyun static void omap_crtc_dss_unregister_framedone(
248*4882a593Smuzhiyun 		struct omap_drm_private *priv, enum omap_channel channel,
249*4882a593Smuzhiyun 		void (*handler)(void *), void *data)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	struct drm_crtc *crtc = priv->channels[channel]->crtc;
252*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
253*4882a593Smuzhiyun 	struct drm_device *dev = omap_crtc->base.dev;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	dev_dbg(dev->dev, "unregister framedone %s", omap_crtc->name);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	WARN_ON(omap_crtc->framedone_handler != handler);
258*4882a593Smuzhiyun 	WARN_ON(omap_crtc->framedone_handler_data != data);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	omap_crtc->framedone_handler = NULL;
261*4882a593Smuzhiyun 	omap_crtc->framedone_handler_data = NULL;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun static const struct dss_mgr_ops mgr_ops = {
265*4882a593Smuzhiyun 	.start_update = omap_crtc_dss_start_update,
266*4882a593Smuzhiyun 	.enable = omap_crtc_dss_enable,
267*4882a593Smuzhiyun 	.disable = omap_crtc_dss_disable,
268*4882a593Smuzhiyun 	.set_timings = omap_crtc_dss_set_timings,
269*4882a593Smuzhiyun 	.set_lcd_config = omap_crtc_dss_set_lcd_config,
270*4882a593Smuzhiyun 	.register_framedone_handler = omap_crtc_dss_register_framedone,
271*4882a593Smuzhiyun 	.unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
272*4882a593Smuzhiyun };
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
275*4882a593Smuzhiyun  * Setup, Flush and Page Flip
276*4882a593Smuzhiyun  */
277*4882a593Smuzhiyun 
omap_crtc_error_irq(struct drm_crtc * crtc,u32 irqstatus)278*4882a593Smuzhiyun void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (omap_crtc->ignore_digit_sync_lost) {
283*4882a593Smuzhiyun 		irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
284*4882a593Smuzhiyun 		if (!irqstatus)
285*4882a593Smuzhiyun 			return;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
omap_crtc_vblank_irq(struct drm_crtc * crtc)291*4882a593Smuzhiyun void omap_crtc_vblank_irq(struct drm_crtc *crtc)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
294*4882a593Smuzhiyun 	struct drm_device *dev = omap_crtc->base.dev;
295*4882a593Smuzhiyun 	struct omap_drm_private *priv = dev->dev_private;
296*4882a593Smuzhiyun 	bool pending;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	spin_lock(&crtc->dev->event_lock);
299*4882a593Smuzhiyun 	/*
300*4882a593Smuzhiyun 	 * If the dispc is busy we're racing the flush operation. Try again on
301*4882a593Smuzhiyun 	 * the next vblank interrupt.
302*4882a593Smuzhiyun 	 */
303*4882a593Smuzhiyun 	if (priv->dispc_ops->mgr_go_busy(priv->dispc, omap_crtc->channel)) {
304*4882a593Smuzhiyun 		spin_unlock(&crtc->dev->event_lock);
305*4882a593Smuzhiyun 		return;
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/* Send the vblank event if one has been requested. */
309*4882a593Smuzhiyun 	if (omap_crtc->event) {
310*4882a593Smuzhiyun 		drm_crtc_send_vblank_event(crtc, omap_crtc->event);
311*4882a593Smuzhiyun 		omap_crtc->event = NULL;
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	pending = omap_crtc->pending;
315*4882a593Smuzhiyun 	omap_crtc->pending = false;
316*4882a593Smuzhiyun 	spin_unlock(&crtc->dev->event_lock);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	if (pending)
319*4882a593Smuzhiyun 		drm_crtc_vblank_put(crtc);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	/* Wake up omap_atomic_complete. */
322*4882a593Smuzhiyun 	wake_up(&omap_crtc->pending_wait);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	DBG("%s: apply done", omap_crtc->name);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
omap_crtc_framedone_irq(struct drm_crtc * crtc,uint32_t irqstatus)327*4882a593Smuzhiyun void omap_crtc_framedone_irq(struct drm_crtc *crtc, uint32_t irqstatus)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (!omap_crtc->framedone_handler)
332*4882a593Smuzhiyun 		return;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	omap_crtc->framedone_handler(omap_crtc->framedone_handler_data);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	spin_lock(&crtc->dev->event_lock);
337*4882a593Smuzhiyun 	/* Send the vblank event if one has been requested. */
338*4882a593Smuzhiyun 	if (omap_crtc->event) {
339*4882a593Smuzhiyun 		drm_crtc_send_vblank_event(crtc, omap_crtc->event);
340*4882a593Smuzhiyun 		omap_crtc->event = NULL;
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun 	omap_crtc->pending = false;
343*4882a593Smuzhiyun 	spin_unlock(&crtc->dev->event_lock);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	/* Wake up omap_atomic_complete. */
346*4882a593Smuzhiyun 	wake_up(&omap_crtc->pending_wait);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
omap_crtc_flush(struct drm_crtc * crtc)349*4882a593Smuzhiyun void omap_crtc_flush(struct drm_crtc *crtc)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
352*4882a593Smuzhiyun 	struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	if (!omap_state->manually_updated)
355*4882a593Smuzhiyun 		return;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (!delayed_work_pending(&omap_crtc->update_work))
358*4882a593Smuzhiyun 		schedule_delayed_work(&omap_crtc->update_work, 0);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
omap_crtc_manual_display_update(struct work_struct * data)361*4882a593Smuzhiyun static void omap_crtc_manual_display_update(struct work_struct *data)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc =
364*4882a593Smuzhiyun 			container_of(data, struct omap_crtc, update_work.work);
365*4882a593Smuzhiyun 	struct drm_display_mode *mode = &omap_crtc->pipe->crtc->mode;
366*4882a593Smuzhiyun 	struct omap_dss_device *dssdev = omap_crtc->pipe->output->next;
367*4882a593Smuzhiyun 	struct drm_device *dev = omap_crtc->base.dev;
368*4882a593Smuzhiyun 	const struct omap_dss_driver *dssdrv;
369*4882a593Smuzhiyun 	int ret;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	if (!dssdev) {
372*4882a593Smuzhiyun 		dev_err_once(dev->dev, "missing display dssdev!");
373*4882a593Smuzhiyun 		return;
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	dssdrv = dssdev->driver;
377*4882a593Smuzhiyun 	if (!dssdrv || !dssdrv->update) {
378*4882a593Smuzhiyun 		dev_err_once(dev->dev, "missing or incorrect dssdrv!");
379*4882a593Smuzhiyun 		return;
380*4882a593Smuzhiyun 	}
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	if (dssdrv->sync)
383*4882a593Smuzhiyun 		dssdrv->sync(dssdev);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	ret = dssdrv->update(dssdev, 0, 0, mode->hdisplay, mode->vdisplay);
386*4882a593Smuzhiyun 	if (ret < 0) {
387*4882a593Smuzhiyun 		spin_lock_irq(&dev->event_lock);
388*4882a593Smuzhiyun 		omap_crtc->pending = false;
389*4882a593Smuzhiyun 		spin_unlock_irq(&dev->event_lock);
390*4882a593Smuzhiyun 		wake_up(&omap_crtc->pending_wait);
391*4882a593Smuzhiyun 	}
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
omap_crtc_write_crtc_properties(struct drm_crtc * crtc)394*4882a593Smuzhiyun static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
397*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
398*4882a593Smuzhiyun 	struct omap_overlay_manager_info info;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	memset(&info, 0, sizeof(info));
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	info.default_color = 0x000000;
403*4882a593Smuzhiyun 	info.trans_enabled = false;
404*4882a593Smuzhiyun 	info.partial_alpha_enabled = false;
405*4882a593Smuzhiyun 	info.cpr_enable = false;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	priv->dispc_ops->mgr_setup(priv->dispc, omap_crtc->channel, &info);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
411*4882a593Smuzhiyun  * CRTC Functions
412*4882a593Smuzhiyun  */
413*4882a593Smuzhiyun 
omap_crtc_destroy(struct drm_crtc * crtc)414*4882a593Smuzhiyun static void omap_crtc_destroy(struct drm_crtc *crtc)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	DBG("%s", omap_crtc->name);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	drm_crtc_cleanup(crtc);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	kfree(omap_crtc);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
omap_crtc_arm_event(struct drm_crtc * crtc)425*4882a593Smuzhiyun static void omap_crtc_arm_event(struct drm_crtc *crtc)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	WARN_ON(omap_crtc->pending);
430*4882a593Smuzhiyun 	omap_crtc->pending = true;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (crtc->state->event) {
433*4882a593Smuzhiyun 		omap_crtc->event = crtc->state->event;
434*4882a593Smuzhiyun 		crtc->state->event = NULL;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
omap_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)438*4882a593Smuzhiyun static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
439*4882a593Smuzhiyun 				    struct drm_crtc_state *old_state)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
442*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
443*4882a593Smuzhiyun 	struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
444*4882a593Smuzhiyun 	int ret;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	DBG("%s", omap_crtc->name);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	priv->dispc_ops->runtime_get(priv->dispc);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	/* manual updated display will not trigger vsync irq */
451*4882a593Smuzhiyun 	if (omap_state->manually_updated)
452*4882a593Smuzhiyun 		return;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	drm_crtc_vblank_on(crtc);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	ret = drm_crtc_vblank_get(crtc);
457*4882a593Smuzhiyun 	WARN_ON(ret != 0);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	spin_lock_irq(&crtc->dev->event_lock);
460*4882a593Smuzhiyun 	omap_crtc_arm_event(crtc);
461*4882a593Smuzhiyun 	spin_unlock_irq(&crtc->dev->event_lock);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
omap_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)464*4882a593Smuzhiyun static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
465*4882a593Smuzhiyun 				     struct drm_crtc_state *old_state)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
468*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
469*4882a593Smuzhiyun 	struct drm_device *dev = crtc->dev;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	DBG("%s", omap_crtc->name);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	spin_lock_irq(&crtc->dev->event_lock);
474*4882a593Smuzhiyun 	if (crtc->state->event) {
475*4882a593Smuzhiyun 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
476*4882a593Smuzhiyun 		crtc->state->event = NULL;
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 	spin_unlock_irq(&crtc->dev->event_lock);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	cancel_delayed_work(&omap_crtc->update_work);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (!omap_crtc_wait_pending(crtc))
483*4882a593Smuzhiyun 		dev_warn(dev->dev, "manual display update did not finish!");
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	drm_crtc_vblank_off(crtc);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	priv->dispc_ops->runtime_put(priv->dispc);
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
omap_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)490*4882a593Smuzhiyun static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
491*4882a593Smuzhiyun 					const struct drm_display_mode *mode)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
494*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
495*4882a593Smuzhiyun 	struct videomode vm = {0};
496*4882a593Smuzhiyun 	int r;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	drm_display_mode_to_videomode(mode, &vm);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	/*
501*4882a593Smuzhiyun 	 * DSI might not call this, since the supplied mode is not a
502*4882a593Smuzhiyun 	 * valid DISPC mode. DSI will calculate and configure the
503*4882a593Smuzhiyun 	 * proper DISPC mode later.
504*4882a593Smuzhiyun 	 */
505*4882a593Smuzhiyun 	if (omap_crtc->pipe->output->next == NULL ||
506*4882a593Smuzhiyun 	    omap_crtc->pipe->output->next->type != OMAP_DISPLAY_TYPE_DSI) {
507*4882a593Smuzhiyun 		r = priv->dispc_ops->mgr_check_timings(priv->dispc,
508*4882a593Smuzhiyun 						       omap_crtc->channel,
509*4882a593Smuzhiyun 						       &vm);
510*4882a593Smuzhiyun 		if (r)
511*4882a593Smuzhiyun 			return r;
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	/* Check for bandwidth limit */
515*4882a593Smuzhiyun 	if (priv->max_bandwidth) {
516*4882a593Smuzhiyun 		/*
517*4882a593Smuzhiyun 		 * Estimation for the bandwidth need of a given mode with one
518*4882a593Smuzhiyun 		 * full screen plane:
519*4882a593Smuzhiyun 		 * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
520*4882a593Smuzhiyun 		 *					^^ Refresh rate ^^
521*4882a593Smuzhiyun 		 *
522*4882a593Smuzhiyun 		 * The interlaced mode is taken into account by using the
523*4882a593Smuzhiyun 		 * pixelclock in the calculation.
524*4882a593Smuzhiyun 		 *
525*4882a593Smuzhiyun 		 * The equation is rearranged for 64bit arithmetic.
526*4882a593Smuzhiyun 		 */
527*4882a593Smuzhiyun 		uint64_t bandwidth = mode->clock * 1000;
528*4882a593Smuzhiyun 		unsigned int bpp = 4;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
531*4882a593Smuzhiyun 		bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 		/*
534*4882a593Smuzhiyun 		 * Reject modes which would need more bandwidth if used with one
535*4882a593Smuzhiyun 		 * full resolution plane (most common use case).
536*4882a593Smuzhiyun 		 */
537*4882a593Smuzhiyun 		if (priv->max_bandwidth < bandwidth)
538*4882a593Smuzhiyun 			return MODE_BAD;
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	return MODE_OK;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun 
omap_crtc_mode_set_nofb(struct drm_crtc * crtc)544*4882a593Smuzhiyun static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
547*4882a593Smuzhiyun 	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	DBG("%s: set mode: " DRM_MODE_FMT,
550*4882a593Smuzhiyun 	    omap_crtc->name, DRM_MODE_ARG(mode));
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	drm_display_mode_to_videomode(mode, &omap_crtc->vm);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
omap_crtc_is_manually_updated(struct drm_crtc * crtc)555*4882a593Smuzhiyun static bool omap_crtc_is_manually_updated(struct drm_crtc *crtc)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
558*4882a593Smuzhiyun 	struct omap_dss_device *display = omap_crtc->pipe->output->next;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	if (!display)
561*4882a593Smuzhiyun 		return false;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	if (display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
564*4882a593Smuzhiyun 		DBG("detected manually updated display!");
565*4882a593Smuzhiyun 		return true;
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	return false;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
omap_crtc_atomic_check(struct drm_crtc * crtc,struct drm_crtc_state * state)571*4882a593Smuzhiyun static int omap_crtc_atomic_check(struct drm_crtc *crtc,
572*4882a593Smuzhiyun 				struct drm_crtc_state *state)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun 	struct drm_plane_state *pri_state;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	if (state->color_mgmt_changed && state->gamma_lut) {
577*4882a593Smuzhiyun 		unsigned int length = state->gamma_lut->length /
578*4882a593Smuzhiyun 			sizeof(struct drm_color_lut);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 		if (length < 2)
581*4882a593Smuzhiyun 			return -EINVAL;
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	pri_state = drm_atomic_get_new_plane_state(state->state, crtc->primary);
585*4882a593Smuzhiyun 	if (pri_state) {
586*4882a593Smuzhiyun 		struct omap_crtc_state *omap_crtc_state =
587*4882a593Smuzhiyun 			to_omap_crtc_state(state);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		/* Mirror new values for zpos and rotation in omap_crtc_state */
590*4882a593Smuzhiyun 		omap_crtc_state->zpos = pri_state->zpos;
591*4882a593Smuzhiyun 		omap_crtc_state->rotation = pri_state->rotation;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 		/* Check if this CRTC is for a manually updated display */
594*4882a593Smuzhiyun 		omap_crtc_state->manually_updated = omap_crtc_is_manually_updated(crtc);
595*4882a593Smuzhiyun 	}
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	return 0;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun 
omap_crtc_atomic_begin(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)600*4882a593Smuzhiyun static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
601*4882a593Smuzhiyun 				   struct drm_crtc_state *old_crtc_state)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun 
omap_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)605*4882a593Smuzhiyun static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
606*4882a593Smuzhiyun 				   struct drm_crtc_state *old_crtc_state)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
609*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
610*4882a593Smuzhiyun 	struct omap_crtc_state *omap_crtc_state = to_omap_crtc_state(crtc->state);
611*4882a593Smuzhiyun 	int ret;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	if (crtc->state->color_mgmt_changed) {
614*4882a593Smuzhiyun 		struct drm_color_lut *lut = NULL;
615*4882a593Smuzhiyun 		unsigned int length = 0;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 		if (crtc->state->gamma_lut) {
618*4882a593Smuzhiyun 			lut = (struct drm_color_lut *)
619*4882a593Smuzhiyun 				crtc->state->gamma_lut->data;
620*4882a593Smuzhiyun 			length = crtc->state->gamma_lut->length /
621*4882a593Smuzhiyun 				sizeof(*lut);
622*4882a593Smuzhiyun 		}
623*4882a593Smuzhiyun 		priv->dispc_ops->mgr_set_gamma(priv->dispc, omap_crtc->channel,
624*4882a593Smuzhiyun 					       lut, length);
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	omap_crtc_write_crtc_properties(crtc);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/* Only flush the CRTC if it is currently enabled. */
630*4882a593Smuzhiyun 	if (!omap_crtc->enabled)
631*4882a593Smuzhiyun 		return;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	DBG("%s: GO", omap_crtc->name);
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	if (omap_crtc_state->manually_updated) {
636*4882a593Smuzhiyun 		/* send new image for page flips and modeset changes */
637*4882a593Smuzhiyun 		spin_lock_irq(&crtc->dev->event_lock);
638*4882a593Smuzhiyun 		omap_crtc_flush(crtc);
639*4882a593Smuzhiyun 		omap_crtc_arm_event(crtc);
640*4882a593Smuzhiyun 		spin_unlock_irq(&crtc->dev->event_lock);
641*4882a593Smuzhiyun 		return;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	ret = drm_crtc_vblank_get(crtc);
645*4882a593Smuzhiyun 	WARN_ON(ret != 0);
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	spin_lock_irq(&crtc->dev->event_lock);
648*4882a593Smuzhiyun 	priv->dispc_ops->mgr_go(priv->dispc, omap_crtc->channel);
649*4882a593Smuzhiyun 	omap_crtc_arm_event(crtc);
650*4882a593Smuzhiyun 	spin_unlock_irq(&crtc->dev->event_lock);
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
omap_crtc_atomic_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,u64 val)653*4882a593Smuzhiyun static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
654*4882a593Smuzhiyun 					 struct drm_crtc_state *state,
655*4882a593Smuzhiyun 					 struct drm_property *property,
656*4882a593Smuzhiyun 					 u64 val)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
659*4882a593Smuzhiyun 	struct drm_plane_state *plane_state;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	/*
662*4882a593Smuzhiyun 	 * Delegate property set to the primary plane. Get the plane state and
663*4882a593Smuzhiyun 	 * set the property directly, the shadow copy will be assigned in the
664*4882a593Smuzhiyun 	 * omap_crtc_atomic_check callback. This way updates to plane state will
665*4882a593Smuzhiyun 	 * always be mirrored in the crtc state correctly.
666*4882a593Smuzhiyun 	 */
667*4882a593Smuzhiyun 	plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
668*4882a593Smuzhiyun 	if (IS_ERR(plane_state))
669*4882a593Smuzhiyun 		return PTR_ERR(plane_state);
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	if (property == crtc->primary->rotation_property)
672*4882a593Smuzhiyun 		plane_state->rotation = val;
673*4882a593Smuzhiyun 	else if (property == priv->zorder_prop)
674*4882a593Smuzhiyun 		plane_state->zpos = val;
675*4882a593Smuzhiyun 	else
676*4882a593Smuzhiyun 		return -EINVAL;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
omap_crtc_atomic_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,u64 * val)681*4882a593Smuzhiyun static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
682*4882a593Smuzhiyun 					 const struct drm_crtc_state *state,
683*4882a593Smuzhiyun 					 struct drm_property *property,
684*4882a593Smuzhiyun 					 u64 *val)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	struct omap_drm_private *priv = crtc->dev->dev_private;
687*4882a593Smuzhiyun 	struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	if (property == crtc->primary->rotation_property)
690*4882a593Smuzhiyun 		*val = omap_state->rotation;
691*4882a593Smuzhiyun 	else if (property == priv->zorder_prop)
692*4882a593Smuzhiyun 		*val = omap_state->zpos;
693*4882a593Smuzhiyun 	else
694*4882a593Smuzhiyun 		return -EINVAL;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	return 0;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
omap_crtc_reset(struct drm_crtc * crtc)699*4882a593Smuzhiyun static void omap_crtc_reset(struct drm_crtc *crtc)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	struct omap_crtc_state *state;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	if (crtc->state)
704*4882a593Smuzhiyun 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	kfree(crtc->state);
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	state = kzalloc(sizeof(*state), GFP_KERNEL);
709*4882a593Smuzhiyun 	if (state)
710*4882a593Smuzhiyun 		__drm_atomic_helper_crtc_reset(crtc, &state->base);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun static struct drm_crtc_state *
omap_crtc_duplicate_state(struct drm_crtc * crtc)714*4882a593Smuzhiyun omap_crtc_duplicate_state(struct drm_crtc *crtc)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	struct omap_crtc_state *state, *current_state;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	if (WARN_ON(!crtc->state))
719*4882a593Smuzhiyun 		return NULL;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	current_state = to_omap_crtc_state(crtc->state);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	state = kmalloc(sizeof(*state), GFP_KERNEL);
724*4882a593Smuzhiyun 	if (!state)
725*4882a593Smuzhiyun 		return NULL;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	state->zpos = current_state->zpos;
730*4882a593Smuzhiyun 	state->rotation = current_state->rotation;
731*4882a593Smuzhiyun 	state->manually_updated = current_state->manually_updated;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	return &state->base;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun static const struct drm_crtc_funcs omap_crtc_funcs = {
737*4882a593Smuzhiyun 	.reset = omap_crtc_reset,
738*4882a593Smuzhiyun 	.set_config = drm_atomic_helper_set_config,
739*4882a593Smuzhiyun 	.destroy = omap_crtc_destroy,
740*4882a593Smuzhiyun 	.page_flip = drm_atomic_helper_page_flip,
741*4882a593Smuzhiyun 	.gamma_set = drm_atomic_helper_legacy_gamma_set,
742*4882a593Smuzhiyun 	.atomic_duplicate_state = omap_crtc_duplicate_state,
743*4882a593Smuzhiyun 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
744*4882a593Smuzhiyun 	.atomic_set_property = omap_crtc_atomic_set_property,
745*4882a593Smuzhiyun 	.atomic_get_property = omap_crtc_atomic_get_property,
746*4882a593Smuzhiyun 	.enable_vblank = omap_irq_enable_vblank,
747*4882a593Smuzhiyun 	.disable_vblank = omap_irq_disable_vblank,
748*4882a593Smuzhiyun };
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
751*4882a593Smuzhiyun 	.mode_set_nofb = omap_crtc_mode_set_nofb,
752*4882a593Smuzhiyun 	.atomic_check = omap_crtc_atomic_check,
753*4882a593Smuzhiyun 	.atomic_begin = omap_crtc_atomic_begin,
754*4882a593Smuzhiyun 	.atomic_flush = omap_crtc_atomic_flush,
755*4882a593Smuzhiyun 	.atomic_enable = omap_crtc_atomic_enable,
756*4882a593Smuzhiyun 	.atomic_disable = omap_crtc_atomic_disable,
757*4882a593Smuzhiyun 	.mode_valid = omap_crtc_mode_valid,
758*4882a593Smuzhiyun };
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
761*4882a593Smuzhiyun  * Init and Cleanup
762*4882a593Smuzhiyun  */
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun static const char *channel_names[] = {
765*4882a593Smuzhiyun 	[OMAP_DSS_CHANNEL_LCD] = "lcd",
766*4882a593Smuzhiyun 	[OMAP_DSS_CHANNEL_DIGIT] = "tv",
767*4882a593Smuzhiyun 	[OMAP_DSS_CHANNEL_LCD2] = "lcd2",
768*4882a593Smuzhiyun 	[OMAP_DSS_CHANNEL_LCD3] = "lcd3",
769*4882a593Smuzhiyun };
770*4882a593Smuzhiyun 
omap_crtc_pre_init(struct omap_drm_private * priv)771*4882a593Smuzhiyun void omap_crtc_pre_init(struct omap_drm_private *priv)
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun 	dss_install_mgr_ops(priv->dss, &mgr_ops, priv);
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
omap_crtc_pre_uninit(struct omap_drm_private * priv)776*4882a593Smuzhiyun void omap_crtc_pre_uninit(struct omap_drm_private *priv)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun 	dss_uninstall_mgr_ops(priv->dss);
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun /* initialize crtc */
omap_crtc_init(struct drm_device * dev,struct omap_drm_pipeline * pipe,struct drm_plane * plane)782*4882a593Smuzhiyun struct drm_crtc *omap_crtc_init(struct drm_device *dev,
783*4882a593Smuzhiyun 				struct omap_drm_pipeline *pipe,
784*4882a593Smuzhiyun 				struct drm_plane *plane)
785*4882a593Smuzhiyun {
786*4882a593Smuzhiyun 	struct omap_drm_private *priv = dev->dev_private;
787*4882a593Smuzhiyun 	struct drm_crtc *crtc = NULL;
788*4882a593Smuzhiyun 	struct omap_crtc *omap_crtc;
789*4882a593Smuzhiyun 	enum omap_channel channel;
790*4882a593Smuzhiyun 	int ret;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	channel = pipe->output->dispc_channel;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	DBG("%s", channel_names[channel]);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
797*4882a593Smuzhiyun 	if (!omap_crtc)
798*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	crtc = &omap_crtc->base;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	init_waitqueue_head(&omap_crtc->pending_wait);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	omap_crtc->pipe = pipe;
805*4882a593Smuzhiyun 	omap_crtc->channel = channel;
806*4882a593Smuzhiyun 	omap_crtc->name = channel_names[channel];
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	/*
809*4882a593Smuzhiyun 	 * We want to refresh manually updated displays from dirty callback,
810*4882a593Smuzhiyun 	 * which is called quite often (e.g. for each drawn line). This will
811*4882a593Smuzhiyun 	 * be used to do the display update asynchronously to avoid blocking
812*4882a593Smuzhiyun 	 * the rendering process and merges multiple dirty calls into one
813*4882a593Smuzhiyun 	 * update if they arrive very fast. We also call this function for
814*4882a593Smuzhiyun 	 * atomic display updates (e.g. for page flips), which means we do
815*4882a593Smuzhiyun 	 * not need extra locking. Atomic updates should be synchronous, but
816*4882a593Smuzhiyun 	 * need to wait for the framedone interrupt anyways.
817*4882a593Smuzhiyun 	 */
818*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&omap_crtc->update_work,
819*4882a593Smuzhiyun 			  omap_crtc_manual_display_update);
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
822*4882a593Smuzhiyun 					&omap_crtc_funcs, NULL);
823*4882a593Smuzhiyun 	if (ret < 0) {
824*4882a593Smuzhiyun 		dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
825*4882a593Smuzhiyun 			__func__, pipe->output->name);
826*4882a593Smuzhiyun 		kfree(omap_crtc);
827*4882a593Smuzhiyun 		return ERR_PTR(ret);
828*4882a593Smuzhiyun 	}
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	/* The dispc API adapts to what ever size, but the HW supports
833*4882a593Smuzhiyun 	 * 256 element gamma table for LCDs and 1024 element table for
834*4882a593Smuzhiyun 	 * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
835*4882a593Smuzhiyun 	 * tables so lets use that. Size of HW gamma table can be
836*4882a593Smuzhiyun 	 * extracted with dispc_mgr_gamma_size(). If it returns 0
837*4882a593Smuzhiyun 	 * gamma table is not supported.
838*4882a593Smuzhiyun 	 */
839*4882a593Smuzhiyun 	if (priv->dispc_ops->mgr_gamma_size(priv->dispc, channel)) {
840*4882a593Smuzhiyun 		unsigned int gamma_lut_size = 256;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 		drm_crtc_enable_color_mgmt(crtc, 0, false, gamma_lut_size);
843*4882a593Smuzhiyun 		drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
844*4882a593Smuzhiyun 	}
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	omap_plane_install_properties(crtc->primary, &crtc->base);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	return crtc;
849*4882a593Smuzhiyun }
850