blob: 61d1c4897a45988c314e292a3890e906fc365935 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_crtc.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "omap_drv.h"
21
Andy Grossb9ed9f02012-10-16 00:17:40 -050022#include <drm/drm_mode.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060023#include "drm_crtc.h"
24#include "drm_crtc_helper.h"
25
26#define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
27
28struct omap_crtc {
29 struct drm_crtc base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060030 struct drm_plane *plane;
Rob Clarkf5f94542012-12-04 13:59:12 -060031
Rob Clarkbb5c2d92012-01-16 12:51:16 -060032 const char *name;
Rob Clarkf5f94542012-12-04 13:59:12 -060033 int pipe;
34 enum omap_channel channel;
35 struct omap_overlay_manager_info info;
Tomi Valkeinenc7aef122014-04-03 16:30:03 +030036 struct drm_encoder *current_encoder;
Rob Clarkf5f94542012-12-04 13:59:12 -060037
38 /*
39 * Temporary: eventually this will go away, but it is needed
40 * for now to keep the output's happy. (They only need
41 * mgr->id.) Eventually this will be replaced w/ something
42 * more common-panel-framework-y
43 */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +030044 struct omap_overlay_manager *mgr;
Rob Clarkf5f94542012-12-04 13:59:12 -060045
46 struct omap_video_timings timings;
47 bool enabled;
48 bool full_update;
49
50 struct omap_drm_apply apply;
51
52 struct omap_drm_irq apply_irq;
53 struct omap_drm_irq error_irq;
54
55 /* list of in-progress apply's: */
56 struct list_head pending_applies;
57
58 /* list of queued apply's: */
59 struct list_head queued_applies;
60
61 /* for handling queued and in-progress applies: */
62 struct work_struct apply_work;
Rob Clarkcd5351f2011-11-12 12:09:40 -060063
Rob Clarkbb5c2d92012-01-16 12:51:16 -060064 /* if there is a pending flip, these will be non-null: */
Rob Clarkcd5351f2011-11-12 12:09:40 -060065 struct drm_pending_vblank_event *event;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060066 struct drm_framebuffer *old_fb;
Rob Clarkf5f94542012-12-04 13:59:12 -060067
68 /* for handling page flips without caring about what
69 * the callback is called from. Possibly we should just
70 * make omap_gem always call the cb from the worker so
71 * we don't have to care about this..
72 *
73 * XXX maybe fold into apply_work??
74 */
75 struct work_struct page_flip_work;
Rob Clarkcd5351f2011-11-12 12:09:40 -060076};
77
Archit Taneja0d8f3712013-03-26 19:15:19 +053078uint32_t pipe2vbl(struct drm_crtc *crtc)
79{
80 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
81
82 return dispc_mgr_get_vsync_irq(omap_crtc->channel);
83}
84
Rob Clarkf5f94542012-12-04 13:59:12 -060085/*
86 * Manager-ops, callbacks from output when they need to configure
87 * the upstream part of the video pipe.
88 *
89 * Most of these we can ignore until we add support for command-mode
90 * panels.. for video-mode the crtc-helpers already do an adequate
91 * job of sequencing the setup of the video pipe in the proper order
92 */
93
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +030094/* ovl-mgr-id -> crtc */
95static struct omap_crtc *omap_crtcs[8];
96
Rob Clarkf5f94542012-12-04 13:59:12 -060097/* we can probably ignore these until we support command-mode panels: */
Tomi Valkeinena7e71e72013-05-08 16:23:32 +030098static int omap_crtc_connect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +030099 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300100{
101 if (mgr->output)
102 return -EINVAL;
103
104 if ((mgr->supported_outputs & dst->id) == 0)
105 return -EINVAL;
106
107 dst->manager = mgr;
108 mgr->output = dst;
109
110 return 0;
111}
112
113static void omap_crtc_disconnect(struct omap_overlay_manager *mgr,
Tomi Valkeinen1f68d9c2013-04-19 15:09:34 +0300114 struct omap_dss_device *dst)
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300115{
116 mgr->output->manager = NULL;
117 mgr->output = NULL;
118}
119
Rob Clarkf5f94542012-12-04 13:59:12 -0600120static void omap_crtc_start_update(struct omap_overlay_manager *mgr)
121{
122}
123
124static int omap_crtc_enable(struct omap_overlay_manager *mgr)
125{
126 return 0;
127}
128
129static void omap_crtc_disable(struct omap_overlay_manager *mgr)
130{
131}
132
133static void omap_crtc_set_timings(struct omap_overlay_manager *mgr,
134 const struct omap_video_timings *timings)
135{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300136 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600137 DBG("%s", omap_crtc->name);
138 omap_crtc->timings = *timings;
139 omap_crtc->full_update = true;
140}
141
142static void omap_crtc_set_lcd_config(struct omap_overlay_manager *mgr,
143 const struct dss_lcd_mgr_config *config)
144{
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300145 struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
Rob Clarkf5f94542012-12-04 13:59:12 -0600146 DBG("%s", omap_crtc->name);
147 dispc_mgr_set_lcd_config(omap_crtc->channel, config);
148}
149
150static int omap_crtc_register_framedone_handler(
151 struct omap_overlay_manager *mgr,
152 void (*handler)(void *), void *data)
153{
154 return 0;
155}
156
157static void omap_crtc_unregister_framedone_handler(
158 struct omap_overlay_manager *mgr,
159 void (*handler)(void *), void *data)
160{
161}
162
163static const struct dss_mgr_ops mgr_ops = {
Tomi Valkeinena7e71e72013-05-08 16:23:32 +0300164 .connect = omap_crtc_connect,
165 .disconnect = omap_crtc_disconnect,
Rob Clarkf5f94542012-12-04 13:59:12 -0600166 .start_update = omap_crtc_start_update,
167 .enable = omap_crtc_enable,
168 .disable = omap_crtc_disable,
169 .set_timings = omap_crtc_set_timings,
170 .set_lcd_config = omap_crtc_set_lcd_config,
171 .register_framedone_handler = omap_crtc_register_framedone_handler,
172 .unregister_framedone_handler = omap_crtc_unregister_framedone_handler,
173};
174
175/*
176 * CRTC funcs:
177 */
178
Rob Clarkcd5351f2011-11-12 12:09:40 -0600179static void omap_crtc_destroy(struct drm_crtc *crtc)
180{
181 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600182
183 DBG("%s", omap_crtc->name);
184
185 WARN_ON(omap_crtc->apply_irq.registered);
186 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
187
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600188 omap_crtc->plane->funcs->destroy(omap_crtc->plane);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600189 drm_crtc_cleanup(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600190
Rob Clarkcd5351f2011-11-12 12:09:40 -0600191 kfree(omap_crtc);
192}
193
194static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
195{
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600196 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600197 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600198 bool enabled = (mode == DRM_MODE_DPMS_ON);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600199 int i;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600200
Rob Clarkf5f94542012-12-04 13:59:12 -0600201 DBG("%s: %d", omap_crtc->name, mode);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600202
Rob Clarkf5f94542012-12-04 13:59:12 -0600203 if (enabled != omap_crtc->enabled) {
204 omap_crtc->enabled = enabled;
205 omap_crtc->full_update = true;
206 omap_crtc_apply(crtc, &omap_crtc->apply);
207
208 /* also enable our private plane: */
209 WARN_ON(omap_plane_dpms(omap_crtc->plane, mode));
210
211 /* and any attached overlay planes: */
212 for (i = 0; i < priv->num_planes; i++) {
213 struct drm_plane *plane = priv->planes[i];
214 if (plane->crtc == crtc)
215 WARN_ON(omap_plane_dpms(plane, mode));
216 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600217 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600218}
219
220static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200221 const struct drm_display_mode *mode,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600222 struct drm_display_mode *adjusted_mode)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600223{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600224 return true;
225}
226
227static int omap_crtc_mode_set(struct drm_crtc *crtc,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600228 struct drm_display_mode *mode,
229 struct drm_display_mode *adjusted_mode,
230 int x, int y,
231 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600232{
233 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
234
Rob Clarkf5f94542012-12-04 13:59:12 -0600235 mode = adjusted_mode;
236
237 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
238 omap_crtc->name, mode->base.id, mode->name,
239 mode->vrefresh, mode->clock,
240 mode->hdisplay, mode->hsync_start,
241 mode->hsync_end, mode->htotal,
242 mode->vdisplay, mode->vsync_start,
243 mode->vsync_end, mode->vtotal,
244 mode->type, mode->flags);
245
246 copy_timings_drm_to_omap(&omap_crtc->timings, mode);
247 omap_crtc->full_update = true;
248
Matt Roperf4510a22014-04-01 15:22:40 -0700249 return omap_plane_mode_set(omap_crtc->plane, crtc, crtc->primary->fb,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600250 0, 0, mode->hdisplay, mode->vdisplay,
251 x << 16, y << 16,
Rob Clarkf5f94542012-12-04 13:59:12 -0600252 mode->hdisplay << 16, mode->vdisplay << 16,
253 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600254}
255
256static void omap_crtc_prepare(struct drm_crtc *crtc)
257{
258 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600259 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600260 omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
261}
262
263static void omap_crtc_commit(struct drm_crtc *crtc)
264{
265 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600266 DBG("%s", omap_crtc->name);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600267 omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
268}
269
270static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600271 struct drm_framebuffer *old_fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600272{
273 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600274 struct drm_plane *plane = omap_crtc->plane;
275 struct drm_display_mode *mode = &crtc->mode;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600276
Matt Roperf4510a22014-04-01 15:22:40 -0700277 return omap_plane_mode_set(plane, crtc, crtc->primary->fb,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600278 0, 0, mode->hdisplay, mode->vdisplay,
279 x << 16, y << 16,
Rob Clarkf5f94542012-12-04 13:59:12 -0600280 mode->hdisplay << 16, mode->vdisplay << 16,
281 NULL, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600282}
283
Rob Clark72d0c332012-03-11 21:11:21 -0500284static void vblank_cb(void *arg)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600285{
286 struct drm_crtc *crtc = arg;
287 struct drm_device *dev = crtc->dev;
288 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600289 unsigned long flags;
290
Rob Clarkf5f94542012-12-04 13:59:12 -0600291 spin_lock_irqsave(&dev->event_lock, flags);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600292
293 /* wakeup userspace */
Rob Clarkf5f94542012-12-04 13:59:12 -0600294 if (omap_crtc->event)
295 drm_send_vblank_event(dev, omap_crtc->pipe, omap_crtc->event);
Rob Clark7411f9c2012-03-11 21:11:22 -0500296
Rob Clarkf5f94542012-12-04 13:59:12 -0600297 omap_crtc->event = NULL;
298 omap_crtc->old_fb = NULL;
299
300 spin_unlock_irqrestore(&dev->event_lock, flags);
301}
302
303static void page_flip_worker(struct work_struct *work)
304{
305 struct omap_crtc *omap_crtc =
306 container_of(work, struct omap_crtc, page_flip_work);
307 struct drm_crtc *crtc = &omap_crtc->base;
Rob Clarkf5f94542012-12-04 13:59:12 -0600308 struct drm_display_mode *mode = &crtc->mode;
309 struct drm_gem_object *bo;
310
Daniel Vetter16ef3df2013-01-24 17:20:33 +0100311 mutex_lock(&crtc->mutex);
Matt Roperf4510a22014-04-01 15:22:40 -0700312 omap_plane_mode_set(omap_crtc->plane, crtc, crtc->primary->fb,
Rob Clarkf5f94542012-12-04 13:59:12 -0600313 0, 0, mode->hdisplay, mode->vdisplay,
314 crtc->x << 16, crtc->y << 16,
315 mode->hdisplay << 16, mode->vdisplay << 16,
316 vblank_cb, crtc);
Daniel Vetter16ef3df2013-01-24 17:20:33 +0100317 mutex_unlock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600318
Matt Roperf4510a22014-04-01 15:22:40 -0700319 bo = omap_framebuffer_bo(crtc->primary->fb, 0);
Rob Clarkf5f94542012-12-04 13:59:12 -0600320 drm_gem_object_unreference_unlocked(bo);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600321}
322
Rob Clark72d0c332012-03-11 21:11:21 -0500323static void page_flip_cb(void *arg)
324{
325 struct drm_crtc *crtc = arg;
326 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600327 struct omap_drm_private *priv = crtc->dev->dev_private;
Rob Clark72d0c332012-03-11 21:11:21 -0500328
Rob Clarkf5f94542012-12-04 13:59:12 -0600329 /* avoid assumptions about what ctxt we are called from: */
330 queue_work(priv->wq, &omap_crtc->page_flip_work);
Rob Clark72d0c332012-03-11 21:11:21 -0500331}
332
Rob Clarkcd5351f2011-11-12 12:09:40 -0600333static int omap_crtc_page_flip_locked(struct drm_crtc *crtc,
334 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700335 struct drm_pending_vblank_event *event,
336 uint32_t page_flip_flags)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600337{
338 struct drm_device *dev = crtc->dev;
339 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700340 struct drm_plane *primary = crtc->primary;
Rob Clark119c0812012-09-04 17:46:22 -0500341 struct drm_gem_object *bo;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600342
Matt Roperf4510a22014-04-01 15:22:40 -0700343 DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
Rob Clarkf5f94542012-12-04 13:59:12 -0600344 fb->base.id, event);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600345
Rob Clarkf5f94542012-12-04 13:59:12 -0600346 if (omap_crtc->old_fb) {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600347 dev_err(dev->dev, "already a pending flip\n");
348 return -EINVAL;
349 }
350
Rob Clarkcd5351f2011-11-12 12:09:40 -0600351 omap_crtc->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -0700352 primary->fb = fb;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600353
Rob Clark119c0812012-09-04 17:46:22 -0500354 /*
355 * Hold a reference temporarily until the crtc is updated
356 * and takes the reference to the bo. This avoids it
357 * getting freed from under us:
358 */
359 bo = omap_framebuffer_bo(fb, 0);
360 drm_gem_object_reference(bo);
361
362 omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600363
364 return 0;
365}
366
Rob Clark3c810c62012-08-15 15:18:01 -0500367static int omap_crtc_set_property(struct drm_crtc *crtc,
368 struct drm_property *property, uint64_t val)
369{
370 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clark1e0fdfc2012-09-04 11:36:20 -0500371 struct omap_drm_private *priv = crtc->dev->dev_private;
372
373 if (property == priv->rotation_prop) {
374 crtc->invert_dimensions =
375 !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
376 }
377
Rob Clark3c810c62012-08-15 15:18:01 -0500378 return omap_plane_set_property(omap_crtc->plane, property, val);
379}
380
Rob Clarkcd5351f2011-11-12 12:09:40 -0600381static const struct drm_crtc_funcs omap_crtc_funcs = {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600382 .set_config = drm_crtc_helper_set_config,
383 .destroy = omap_crtc_destroy,
384 .page_flip = omap_crtc_page_flip_locked,
Rob Clark3c810c62012-08-15 15:18:01 -0500385 .set_property = omap_crtc_set_property,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600386};
387
388static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
389 .dpms = omap_crtc_dpms,
390 .mode_fixup = omap_crtc_mode_fixup,
391 .mode_set = omap_crtc_mode_set,
392 .prepare = omap_crtc_prepare,
393 .commit = omap_crtc_commit,
394 .mode_set_base = omap_crtc_mode_set_base,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600395};
396
Rob Clarkf5f94542012-12-04 13:59:12 -0600397const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
398{
399 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
400 return &omap_crtc->timings;
401}
402
403enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
404{
405 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
406 return omap_crtc->channel;
407}
408
409static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
410{
411 struct omap_crtc *omap_crtc =
412 container_of(irq, struct omap_crtc, error_irq);
413 struct drm_crtc *crtc = &omap_crtc->base;
414 DRM_ERROR("%s: errors: %08x\n", omap_crtc->name, irqstatus);
415 /* avoid getting in a flood, unregister the irq until next vblank */
Tomi Valkeinen6da9f892013-10-24 09:50:50 +0300416 __omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
Rob Clarkf5f94542012-12-04 13:59:12 -0600417}
418
419static void omap_crtc_apply_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
420{
421 struct omap_crtc *omap_crtc =
422 container_of(irq, struct omap_crtc, apply_irq);
423 struct drm_crtc *crtc = &omap_crtc->base;
424
425 if (!omap_crtc->error_irq.registered)
Tomi Valkeinen6da9f892013-10-24 09:50:50 +0300426 __omap_irq_register(crtc->dev, &omap_crtc->error_irq);
Rob Clarkf5f94542012-12-04 13:59:12 -0600427
428 if (!dispc_mgr_go_busy(omap_crtc->channel)) {
429 struct omap_drm_private *priv =
430 crtc->dev->dev_private;
431 DBG("%s: apply done", omap_crtc->name);
Tomi Valkeinen6da9f892013-10-24 09:50:50 +0300432 __omap_irq_unregister(crtc->dev, &omap_crtc->apply_irq);
Rob Clarkf5f94542012-12-04 13:59:12 -0600433 queue_work(priv->wq, &omap_crtc->apply_work);
434 }
435}
436
437static void apply_worker(struct work_struct *work)
438{
439 struct omap_crtc *omap_crtc =
440 container_of(work, struct omap_crtc, apply_work);
441 struct drm_crtc *crtc = &omap_crtc->base;
442 struct drm_device *dev = crtc->dev;
443 struct omap_drm_apply *apply, *n;
444 bool need_apply;
445
446 /*
447 * Synchronize everything on mode_config.mutex, to keep
448 * the callbacks and list modification all serialized
449 * with respect to modesetting ioctls from userspace.
450 */
Daniel Vetter16ef3df2013-01-24 17:20:33 +0100451 mutex_lock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600452 dispc_runtime_get();
453
454 /*
455 * If we are still pending a previous update, wait.. when the
456 * pending update completes, we get kicked again.
457 */
458 if (omap_crtc->apply_irq.registered)
459 goto out;
460
461 /* finish up previous apply's: */
462 list_for_each_entry_safe(apply, n,
463 &omap_crtc->pending_applies, pending_node) {
464 apply->post_apply(apply);
465 list_del(&apply->pending_node);
466 }
467
468 need_apply = !list_empty(&omap_crtc->queued_applies);
469
470 /* then handle the next round of of queued apply's: */
471 list_for_each_entry_safe(apply, n,
472 &omap_crtc->queued_applies, queued_node) {
473 apply->pre_apply(apply);
474 list_del(&apply->queued_node);
475 apply->queued = false;
476 list_add_tail(&apply->pending_node,
477 &omap_crtc->pending_applies);
478 }
479
480 if (need_apply) {
481 enum omap_channel channel = omap_crtc->channel;
482
483 DBG("%s: GO", omap_crtc->name);
484
485 if (dispc_mgr_is_enabled(channel)) {
486 omap_irq_register(dev, &omap_crtc->apply_irq);
487 dispc_mgr_go(channel);
488 } else {
489 struct omap_drm_private *priv = dev->dev_private;
490 queue_work(priv->wq, &omap_crtc->apply_work);
491 }
492 }
493
494out:
495 dispc_runtime_put();
Daniel Vetter16ef3df2013-01-24 17:20:33 +0100496 mutex_unlock(&crtc->mutex);
Rob Clarkf5f94542012-12-04 13:59:12 -0600497}
498
499int omap_crtc_apply(struct drm_crtc *crtc,
500 struct omap_drm_apply *apply)
501{
502 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600503
Daniel Vetter16ef3df2013-01-24 17:20:33 +0100504 WARN_ON(!mutex_is_locked(&crtc->mutex));
Rob Clarkf5f94542012-12-04 13:59:12 -0600505
506 /* no need to queue it again if it is already queued: */
507 if (apply->queued)
508 return 0;
509
510 apply->queued = true;
511 list_add_tail(&apply->queued_node, &omap_crtc->queued_applies);
512
513 /*
514 * If there are no currently pending updates, then go ahead and
515 * kick the worker immediately, otherwise it will run again when
516 * the current update finishes.
517 */
518 if (list_empty(&omap_crtc->pending_applies)) {
519 struct omap_drm_private *priv = crtc->dev->dev_private;
520 queue_work(priv->wq, &omap_crtc->apply_work);
521 }
522
523 return 0;
524}
525
526/* called only from apply */
527static void set_enabled(struct drm_crtc *crtc, bool enable)
528{
529 struct drm_device *dev = crtc->dev;
530 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
531 enum omap_channel channel = omap_crtc->channel;
Tomi Valkeinen2ec8e372014-04-02 11:37:06 +0300532 struct omap_irq_wait *wait;
533 u32 framedone_irq, vsync_irq;
534 int ret;
Rob Clarkf5f94542012-12-04 13:59:12 -0600535
536 if (dispc_mgr_is_enabled(channel) == enable)
537 return;
538
Tomi Valkeinen2ec8e372014-04-02 11:37:06 +0300539 /*
540 * Digit output produces some sync lost interrupts during the first
541 * frame when enabling, so we need to ignore those.
542 */
Rob Clarkf5f94542012-12-04 13:59:12 -0600543 omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
544
Tomi Valkeinen2ec8e372014-04-02 11:37:06 +0300545 framedone_irq = dispc_mgr_get_framedone_irq(channel);
546 vsync_irq = dispc_mgr_get_vsync_irq(channel);
547
548 if (enable) {
549 wait = omap_irq_wait_init(dev, vsync_irq, 1);
Rob Clarkf5f94542012-12-04 13:59:12 -0600550 } else {
551 /*
Tomi Valkeinen2ec8e372014-04-02 11:37:06 +0300552 * When we disable the digit output, we need to wait for
553 * FRAMEDONE to know that DISPC has finished with the output.
554 *
555 * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
556 * that case we need to use vsync interrupt, and wait for both
557 * even and odd frames.
Rob Clarkf5f94542012-12-04 13:59:12 -0600558 */
Tomi Valkeinen2ec8e372014-04-02 11:37:06 +0300559
560 if (framedone_irq)
561 wait = omap_irq_wait_init(dev, framedone_irq, 1);
562 else
563 wait = omap_irq_wait_init(dev, vsync_irq, 2);
Rob Clarkf5f94542012-12-04 13:59:12 -0600564 }
565
566 dispc_mgr_enable(channel, enable);
567
Tomi Valkeinen2ec8e372014-04-02 11:37:06 +0300568 ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
569 if (ret) {
570 dev_err(dev->dev, "%s: timeout waiting for %s\n",
571 omap_crtc->name, enable ? "enable" : "disable");
Rob Clarkf5f94542012-12-04 13:59:12 -0600572 }
573
574 omap_irq_register(crtc->dev, &omap_crtc->error_irq);
575}
576
577static void omap_crtc_pre_apply(struct omap_drm_apply *apply)
578{
579 struct omap_crtc *omap_crtc =
580 container_of(apply, struct omap_crtc, apply);
581 struct drm_crtc *crtc = &omap_crtc->base;
582 struct drm_encoder *encoder = NULL;
583
584 DBG("%s: enabled=%d, full=%d", omap_crtc->name,
585 omap_crtc->enabled, omap_crtc->full_update);
586
587 if (omap_crtc->full_update) {
588 struct omap_drm_private *priv = crtc->dev->dev_private;
589 int i;
590 for (i = 0; i < priv->num_encoders; i++) {
591 if (priv->encoders[i]->crtc == crtc) {
592 encoder = priv->encoders[i];
593 break;
594 }
595 }
596 }
597
Tomi Valkeinenc7aef122014-04-03 16:30:03 +0300598 if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
599 omap_encoder_set_enabled(omap_crtc->current_encoder, false);
600
601 omap_crtc->current_encoder = encoder;
602
Rob Clarkf5f94542012-12-04 13:59:12 -0600603 if (!omap_crtc->enabled) {
604 set_enabled(&omap_crtc->base, false);
605 if (encoder)
606 omap_encoder_set_enabled(encoder, false);
607 } else {
608 if (encoder) {
609 omap_encoder_set_enabled(encoder, false);
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300610 omap_encoder_update(encoder, omap_crtc->mgr,
Rob Clarkf5f94542012-12-04 13:59:12 -0600611 &omap_crtc->timings);
612 omap_encoder_set_enabled(encoder, true);
613 omap_crtc->full_update = false;
614 }
615
616 dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
617 dispc_mgr_set_timings(omap_crtc->channel,
618 &omap_crtc->timings);
619 set_enabled(&omap_crtc->base, true);
620 }
621
622 omap_crtc->full_update = false;
623}
624
625static void omap_crtc_post_apply(struct omap_drm_apply *apply)
626{
627 /* nothing needed for post-apply */
628}
629
Tomi Valkeinene2f8fd72014-04-02 14:31:57 +0300630void omap_crtc_flush(struct drm_crtc *crtc)
631{
632 struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
633 int loops = 0;
634
635 while (!list_empty(&omap_crtc->pending_applies) ||
636 !list_empty(&omap_crtc->queued_applies) ||
637 omap_crtc->event || omap_crtc->old_fb) {
638
639 if (++loops > 10) {
640 dev_err(crtc->dev->dev,
641 "omap_crtc_flush() timeout\n");
642 break;
643 }
644
645 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
646 }
647}
648
Rob Clarkf5f94542012-12-04 13:59:12 -0600649static const char *channel_names[] = {
650 [OMAP_DSS_CHANNEL_LCD] = "lcd",
651 [OMAP_DSS_CHANNEL_DIGIT] = "tv",
652 [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
653};
654
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300655void omap_crtc_pre_init(void)
656{
657 dss_install_mgr_ops(&mgr_ops);
658}
659
Archit Taneja3a01ab22014-01-02 14:49:51 +0530660void omap_crtc_pre_uninit(void)
661{
662 dss_uninstall_mgr_ops();
663}
664
Rob Clarkcd5351f2011-11-12 12:09:40 -0600665/* initialize crtc */
666struct drm_crtc *omap_crtc_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600667 struct drm_plane *plane, enum omap_channel channel, int id)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600668{
669 struct drm_crtc *crtc = NULL;
Rob Clarkf5f94542012-12-04 13:59:12 -0600670 struct omap_crtc *omap_crtc;
671 struct omap_overlay_manager_info *info;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600672
Rob Clarkf5f94542012-12-04 13:59:12 -0600673 DBG("%s", channel_names[channel]);
674
675 omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800676 if (!omap_crtc)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600677 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600678
Rob Clarkcd5351f2011-11-12 12:09:40 -0600679 crtc = &omap_crtc->base;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600680
Rob Clarkf5f94542012-12-04 13:59:12 -0600681 INIT_WORK(&omap_crtc->page_flip_work, page_flip_worker);
682 INIT_WORK(&omap_crtc->apply_work, apply_worker);
683
684 INIT_LIST_HEAD(&omap_crtc->pending_applies);
685 INIT_LIST_HEAD(&omap_crtc->queued_applies);
686
687 omap_crtc->apply.pre_apply = omap_crtc_pre_apply;
688 omap_crtc->apply.post_apply = omap_crtc_post_apply;
689
Archit Taneja0d8f3712013-03-26 19:15:19 +0530690 omap_crtc->channel = channel;
691 omap_crtc->plane = plane;
692 omap_crtc->plane->crtc = crtc;
693 omap_crtc->name = channel_names[channel];
694 omap_crtc->pipe = id;
695
696 omap_crtc->apply_irq.irqmask = pipe2vbl(crtc);
Rob Clarkf5f94542012-12-04 13:59:12 -0600697 omap_crtc->apply_irq.irq = omap_crtc_apply_irq;
698
699 omap_crtc->error_irq.irqmask =
700 dispc_mgr_get_sync_lost_irq(channel);
701 omap_crtc->error_irq.irq = omap_crtc_error_irq;
702 omap_irq_register(dev, &omap_crtc->error_irq);
703
Rob Clarkf5f94542012-12-04 13:59:12 -0600704 /* temporary: */
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300705 omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
Rob Clarkf5f94542012-12-04 13:59:12 -0600706
707 /* TODO: fix hard-coded setup.. add properties! */
708 info = &omap_crtc->info;
709 info->default_color = 0x00000000;
710 info->trans_key = 0x00000000;
711 info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
712 info->trans_enabled = false;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600713
Rob Clarkcd5351f2011-11-12 12:09:40 -0600714 drm_crtc_init(dev, crtc, &omap_crtc_funcs);
715 drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
716
Rob Clark3c810c62012-08-15 15:18:01 -0500717 omap_plane_install_properties(omap_crtc->plane, &crtc->base);
718
Tomi Valkeinen04b1fc02013-05-14 10:55:19 +0300719 omap_crtcs[channel] = omap_crtc;
720
Rob Clarkcd5351f2011-11-12 12:09:40 -0600721 return crtc;
722
723fail:
YAMANE Toshiakid21a9d32012-11-14 19:30:23 +0900724 if (crtc)
Rob Clark65b0bd02011-12-09 23:26:07 -0600725 omap_crtc_destroy(crtc);
YAMANE Toshiakid21a9d32012-11-14 19:30:23 +0900726
Rob Clarkcd5351f2011-11-12 12:09:40 -0600727 return NULL;
728}