blob: 0c1a1524f5d515bd9e1891133fb58155e189d307 [file] [log] [blame]
Russell King96f60e32012-08-15 13:59:49 +01001/*
2 * Copyright (C) 2012 Russell King
3 * Rewritten from the dovefb driver, and Armada510 manuals.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/clk.h>
Russell Kingd8c96082014-04-22 11:10:15 +010010#include <linux/component.h>
11#include <linux/of_device.h>
12#include <linux/platform_device.h>
Russell King96f60e32012-08-15 13:59:49 +010013#include <drm/drmP.h>
14#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010015#include <drm/drm_plane_helper.h>
Russell King96f60e32012-08-15 13:59:49 +010016#include "armada_crtc.h"
17#include "armada_drm.h"
18#include "armada_fb.h"
19#include "armada_gem.h"
20#include "armada_hw.h"
21
22struct armada_frame_work {
Russell King4b5dda82015-08-06 16:37:18 +010023 struct armada_plane_work work;
Russell King96f60e32012-08-15 13:59:49 +010024 struct drm_pending_vblank_event *event;
25 struct armada_regs regs[4];
26 struct drm_framebuffer *old_fb;
27};
28
29enum csc_mode {
30 CSC_AUTO = 0,
31 CSC_YUV_CCIR601 = 1,
32 CSC_YUV_CCIR709 = 2,
33 CSC_RGB_COMPUTER = 1,
34 CSC_RGB_STUDIO = 2,
35};
36
Russell King1c914ce2015-07-15 18:11:24 +010037static const uint32_t armada_primary_formats[] = {
38 DRM_FORMAT_UYVY,
39 DRM_FORMAT_YUYV,
40 DRM_FORMAT_VYUY,
41 DRM_FORMAT_YVYU,
42 DRM_FORMAT_ARGB8888,
43 DRM_FORMAT_ABGR8888,
44 DRM_FORMAT_XRGB8888,
45 DRM_FORMAT_XBGR8888,
46 DRM_FORMAT_RGB888,
47 DRM_FORMAT_BGR888,
48 DRM_FORMAT_ARGB1555,
49 DRM_FORMAT_ABGR1555,
50 DRM_FORMAT_RGB565,
51 DRM_FORMAT_BGR565,
52};
53
Russell King96f60e32012-08-15 13:59:49 +010054/*
55 * A note about interlacing. Let's consider HDMI 1920x1080i.
56 * The timing parameters we have from X are:
57 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
58 * 1920 2448 2492 2640 1080 1084 1094 1125
59 * Which get translated to:
60 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
61 * 1920 2448 2492 2640 540 542 547 562
62 *
63 * This is how it is defined by CEA-861-D - line and pixel numbers are
64 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
65 * line: 2640. The odd frame, the first active line is at line 21, and
66 * the even frame, the first active line is 584.
67 *
68 * LN: 560 561 562 563 567 568 569
69 * DE: ~~~|____________________________//__________________________
70 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
71 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
72 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
73 *
74 * LN: 1123 1124 1125 1 5 6 7
75 * DE: ~~~|____________________________//__________________________
76 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
77 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
78 * 23 blanking lines
79 *
80 * The Armada LCD Controller line and pixel numbers are, like X timings,
81 * referenced to the top left of the active frame.
82 *
83 * So, translating these to our LCD controller:
84 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
85 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
86 * Note: Vsync front porch remains constant!
87 *
88 * if (odd_frame) {
89 * vtotal = mode->crtc_vtotal + 1;
90 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
91 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
92 * } else {
93 * vtotal = mode->crtc_vtotal;
94 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
95 * vhorizpos = mode->crtc_hsync_start;
96 * }
97 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
98 *
99 * So, we need to reprogram these registers on each vsync event:
100 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
101 *
102 * Note: we do not use the frame done interrupts because these appear
103 * to happen too early, and lead to jitter on the display (presumably
104 * they occur at the end of the last active line, before the vsync back
105 * porch, which we're reprogramming.)
106 */
107
108void
109armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
110{
111 while (regs->offset != ~0) {
112 void __iomem *reg = dcrtc->base + regs->offset;
113 uint32_t val;
114
115 val = regs->mask;
116 if (val != 0)
117 val &= readl_relaxed(reg);
118 writel_relaxed(val | regs->val, reg);
119 ++regs;
120 }
121}
122
123#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
124
125static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
126{
127 uint32_t dumb_ctrl;
128
129 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
130
131 if (!dpms_blanked(dcrtc->dpms))
132 dumb_ctrl |= CFG_DUMB_ENA;
133
134 /*
135 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
136 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
137 * force LCD_D[23:0] to output blank color, overriding the GPIO or
138 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
139 */
140 if (dpms_blanked(dcrtc->dpms) &&
141 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
142 dumb_ctrl &= ~DUMB_MASK;
143 dumb_ctrl |= DUMB_BLANK;
144 }
145
146 /*
147 * The documentation doesn't indicate what the normal state of
148 * the sync signals are. Sebastian Hesselbart kindly probed
149 * these signals on his board to determine their state.
150 *
151 * The non-inverted state of the sync signals is active high.
152 * Setting these bits makes the appropriate signal active low.
153 */
154 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
155 dumb_ctrl |= CFG_INV_CSYNC;
156 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
157 dumb_ctrl |= CFG_INV_HSYNC;
158 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
159 dumb_ctrl |= CFG_INV_VSYNC;
160
161 if (dcrtc->dumb_ctrl != dumb_ctrl) {
162 dcrtc->dumb_ctrl = dumb_ctrl;
163 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
164 }
165}
166
167static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
168 int x, int y, struct armada_regs *regs, bool interlaced)
169{
170 struct armada_gem_object *obj = drm_fb_obj(fb);
171 unsigned pitch = fb->pitches[0];
172 unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
173 uint32_t addr_odd, addr_even;
174 unsigned i = 0;
175
176 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
177 pitch, x, y, fb->bits_per_pixel);
178
179 addr_odd = addr_even = obj->dev_addr + offset;
180
181 if (interlaced) {
182 addr_even += pitch;
183 pitch *= 2;
184 }
185
186 /* write offset, base, and pitch */
187 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
188 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
189 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
190
191 return i;
192}
193
Russell King4b5dda82015-08-06 16:37:18 +0100194static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
195 struct armada_plane *plane)
196{
197 struct armada_plane_work *work = xchg(&plane->work, NULL);
198
199 /* Handle any pending frame work. */
200 if (work) {
201 work->fn(dcrtc, plane, work);
202 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
203 }
204}
205
206int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
207 struct armada_plane *plane, struct armada_plane_work *work)
208{
209 int ret;
210
211 ret = drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
212 if (ret) {
213 DRM_ERROR("failed to acquire vblank counter\n");
214 return ret;
215 }
216
217 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
218 if (ret)
219 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
220
221 return ret;
222}
223
224int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
225{
226 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
227}
228
Russell King7c8f7e12015-06-29 17:52:16 +0100229void armada_drm_vbl_event_add(struct armada_crtc *dcrtc,
230 struct armada_vbl_event *evt)
231{
232 unsigned long flags;
233 bool not_on_list;
234
235 WARN_ON(drm_vblank_get(dcrtc->crtc.dev, dcrtc->num));
236
237 spin_lock_irqsave(&dcrtc->irq_lock, flags);
238 not_on_list = list_empty(&evt->node);
239 if (not_on_list)
240 list_add_tail(&evt->node, &dcrtc->vbl_list);
241 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
242
243 if (!not_on_list)
244 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
245}
246
247void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc,
248 struct armada_vbl_event *evt)
249{
Russell King6908cf72015-07-15 18:11:25 +0100250 spin_lock_irq(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100251 if (!list_empty(&evt->node)) {
252 list_del_init(&evt->node);
253 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
254 }
Russell King6908cf72015-07-15 18:11:25 +0100255 spin_unlock_irq(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100256}
257
258static void armada_drm_vbl_event_run(struct armada_crtc *dcrtc)
259{
260 struct armada_vbl_event *e, *n;
261
262 list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
263 list_del_init(&e->node);
264 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
265 e->fn(dcrtc, e->data);
266 }
267}
268
Russell King96f60e32012-08-15 13:59:49 +0100269static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
270 struct armada_frame_work *work)
271{
Russell King4b5dda82015-08-06 16:37:18 +0100272 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100273
Russell King4b5dda82015-08-06 16:37:18 +0100274 return armada_drm_plane_work_queue(dcrtc, plane, &work->work);
Russell King96f60e32012-08-15 13:59:49 +0100275}
276
Russell King709ffd82015-07-15 18:09:38 +0100277static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
Russell King4b5dda82015-08-06 16:37:18 +0100278 struct armada_plane *plane, struct armada_plane_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100279{
Russell King4b5dda82015-08-06 16:37:18 +0100280 struct armada_frame_work *fwork = container_of(work, struct armada_frame_work, work);
Russell King96f60e32012-08-15 13:59:49 +0100281 struct drm_device *dev = dcrtc->crtc.dev;
Russell King709ffd82015-07-15 18:09:38 +0100282 unsigned long flags;
Russell King96f60e32012-08-15 13:59:49 +0100283
Russell King709ffd82015-07-15 18:09:38 +0100284 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell King4b5dda82015-08-06 16:37:18 +0100285 armada_drm_crtc_update_regs(dcrtc, fwork->regs);
Russell King709ffd82015-07-15 18:09:38 +0100286 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100287
Russell King4b5dda82015-08-06 16:37:18 +0100288 if (fwork->event) {
Russell King709ffd82015-07-15 18:09:38 +0100289 spin_lock_irqsave(&dev->event_lock, flags);
Russell King4b5dda82015-08-06 16:37:18 +0100290 drm_send_vblank_event(dev, dcrtc->num, fwork->event);
Russell King709ffd82015-07-15 18:09:38 +0100291 spin_unlock_irqrestore(&dev->event_lock, flags);
292 }
Russell King96f60e32012-08-15 13:59:49 +0100293
Russell King96f60e32012-08-15 13:59:49 +0100294 /* Finally, queue the process-half of the cleanup. */
Russell King4b5dda82015-08-06 16:37:18 +0100295 __armada_drm_queue_unref_work(dcrtc->crtc.dev, fwork->old_fb);
296 kfree(fwork);
Russell King96f60e32012-08-15 13:59:49 +0100297}
298
299static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
300 struct drm_framebuffer *fb, bool force)
301{
302 struct armada_frame_work *work;
303
304 if (!fb)
305 return;
306
307 if (force) {
308 /* Display is disabled, so just drop the old fb */
309 drm_framebuffer_unreference(fb);
310 return;
311 }
312
313 work = kmalloc(sizeof(*work), GFP_KERNEL);
314 if (work) {
315 int i = 0;
Russell King4b5dda82015-08-06 16:37:18 +0100316 work->work.fn = armada_drm_crtc_complete_frame_work;
Russell King96f60e32012-08-15 13:59:49 +0100317 work->event = NULL;
318 work->old_fb = fb;
319 armada_reg_queue_end(work->regs, i);
320
321 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
322 return;
323
324 kfree(work);
325 }
326
327 /*
328 * Oops - just drop the reference immediately and hope for
329 * the best. The worst that will happen is the buffer gets
330 * reused before it has finished being displayed.
331 */
332 drm_framebuffer_unreference(fb);
333}
334
335static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
336{
Russell King4b5dda82015-08-06 16:37:18 +0100337 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100338
339 /*
340 * Tell the DRM core that vblank IRQs aren't going to happen for
341 * a while. This cleans up any pending vblank events for us.
342 */
Russell King178e5612014-10-11 23:57:04 +0100343 drm_crtc_vblank_off(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100344 armada_drm_plane_work_run(dcrtc, plane);
Russell King96f60e32012-08-15 13:59:49 +0100345}
346
347void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
348 int idx)
349{
350}
351
352void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
353 int idx)
354{
355}
356
357/* The mode_config.mutex will be held for this call */
358static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
359{
360 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
361
362 if (dcrtc->dpms != dpms) {
363 dcrtc->dpms = dpms;
Russell Kinge0ac5e92015-06-29 18:01:38 +0100364 if (!IS_ERR(dcrtc->clk) && !dpms_blanked(dpms))
365 WARN_ON(clk_prepare_enable(dcrtc->clk));
Russell King96f60e32012-08-15 13:59:49 +0100366 armada_drm_crtc_update(dcrtc);
Russell Kinge0ac5e92015-06-29 18:01:38 +0100367 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dpms))
368 clk_disable_unprepare(dcrtc->clk);
Russell King96f60e32012-08-15 13:59:49 +0100369 if (dpms_blanked(dpms))
370 armada_drm_vblank_off(dcrtc);
Russell King178e5612014-10-11 23:57:04 +0100371 else
372 drm_crtc_vblank_on(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100373 }
374}
375
376/*
377 * Prepare for a mode set. Turn off overlay to ensure that we don't end
378 * up with the overlay size being bigger than the active screen size.
379 * We rely upon X refreshing this state after the mode set has completed.
380 *
381 * The mode_config.mutex will be held for this call
382 */
383static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
384{
385 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
386 struct drm_plane *plane;
387
388 /*
389 * If we have an overlay plane associated with this CRTC, disable
390 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100391 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100392 */
393 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100394 if (plane)
395 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100396}
397
398/* The mode_config.mutex will be held for this call */
399static void armada_drm_crtc_commit(struct drm_crtc *crtc)
400{
401 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
402
403 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
404 dcrtc->dpms = DRM_MODE_DPMS_ON;
405 armada_drm_crtc_update(dcrtc);
406 }
407}
408
409/* The mode_config.mutex will be held for this call */
410static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
411 const struct drm_display_mode *mode, struct drm_display_mode *adj)
412{
Russell King96f60e32012-08-15 13:59:49 +0100413 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
414 int ret;
415
416 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100417 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100418 adj->flags & DRM_MODE_FLAG_INTERLACE)
419 return false;
420
421 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100422 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100423 if (ret)
424 return false;
425
426 return true;
427}
428
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100429static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100430{
Russell King96f60e32012-08-15 13:59:49 +0100431 void __iomem *base = dcrtc->base;
432
433 if (stat & DMA_FF_UNDERFLOW)
434 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
435 if (stat & GRA_FF_UNDERFLOW)
436 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
437
438 if (stat & VSYNC_IRQ)
439 drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
440
441 spin_lock(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100442 armada_drm_vbl_event_run(dcrtc);
Russell King96f60e32012-08-15 13:59:49 +0100443
444 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
445 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
446 uint32_t val;
447
448 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
449 writel_relaxed(dcrtc->v[i].spu_v_h_total,
450 base + LCD_SPUT_V_H_TOTAL);
451
452 val = readl_relaxed(base + LCD_SPU_ADV_REG);
453 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
454 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100455 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100456 }
Russell King662af0d2013-05-19 10:55:17 +0100457
458 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
459 writel_relaxed(dcrtc->cursor_hw_pos,
460 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
461 writel_relaxed(dcrtc->cursor_hw_sz,
462 base + LCD_SPU_HWC_HPXL_VLN);
463 armada_updatel(CFG_HWC_ENA,
464 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
465 base + LCD_SPU_DMA_CTRL0);
466 dcrtc->cursor_update = false;
467 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
468 }
469
Russell King96f60e32012-08-15 13:59:49 +0100470 spin_unlock(&dcrtc->irq_lock);
471
472 if (stat & GRA_FRAME_IRQ) {
Russell King4b5dda82015-08-06 16:37:18 +0100473 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
474 armada_drm_plane_work_run(dcrtc, plane);
475 wake_up(&plane->frame_wait);
Russell King96f60e32012-08-15 13:59:49 +0100476 }
477}
478
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100479static irqreturn_t armada_drm_irq(int irq, void *arg)
480{
481 struct armada_crtc *dcrtc = arg;
482 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
483
484 /*
485 * This is rediculous - rather than writing bits to clear, we
486 * have to set the actual status register value. This is racy.
487 */
488 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
489
490 /* Mask out those interrupts we haven't enabled */
491 v = stat & dcrtc->irq_ena;
492
493 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
494 armada_drm_crtc_irq(dcrtc, stat);
495 return IRQ_HANDLED;
496 }
497 return IRQ_NONE;
498}
499
Russell King96f60e32012-08-15 13:59:49 +0100500/* These are locked by dev->vbl_lock */
501void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
502{
503 if (dcrtc->irq_ena & mask) {
504 dcrtc->irq_ena &= ~mask;
505 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
506 }
507}
508
509void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
510{
511 if ((dcrtc->irq_ena & mask) != mask) {
512 dcrtc->irq_ena |= mask;
513 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
514 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
515 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
516 }
517}
518
519static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
520{
521 struct drm_display_mode *adj = &dcrtc->crtc.mode;
522 uint32_t val = 0;
523
524 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
525 val |= CFG_CSC_YUV_CCIR709;
526 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
527 val |= CFG_CSC_RGB_STUDIO;
528
529 /*
530 * In auto mode, set the colorimetry, based upon the HDMI spec.
531 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
532 * ITU601. It may be more appropriate to set this depending on
533 * the source - but what if the graphic frame is YUV and the
534 * video frame is RGB?
535 */
536 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
537 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
538 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
539 if (dcrtc->csc_yuv_mode == CSC_AUTO)
540 val |= CFG_CSC_YUV_CCIR709;
541 }
542
543 /*
544 * We assume we're connected to a TV-like device, so the YUV->RGB
545 * conversion should produce a limited range. We should set this
546 * depending on the connectors attached to this CRTC, and what
547 * kind of device they report being connected.
548 */
549 if (dcrtc->csc_rgb_mode == CSC_AUTO)
550 val |= CFG_CSC_RGB_STUDIO;
551
552 return val;
553}
554
555/* The mode_config.mutex will be held for this call */
556static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
557 struct drm_display_mode *mode, struct drm_display_mode *adj,
558 int x, int y, struct drm_framebuffer *old_fb)
559{
Russell King96f60e32012-08-15 13:59:49 +0100560 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
561 struct armada_regs regs[17];
562 uint32_t lm, rm, tm, bm, val, sclk;
563 unsigned long flags;
564 unsigned i;
565 bool interlaced;
566
Matt Roperf4510a22014-04-01 15:22:40 -0700567 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100568
569 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
570
Matt Roperf4510a22014-04-01 15:22:40 -0700571 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
572 x, y, regs, interlaced);
Russell King96f60e32012-08-15 13:59:49 +0100573
574 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
575 lm = adj->crtc_htotal - adj->crtc_hsync_end;
576 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
577 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
578
579 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
580 adj->crtc_hdisplay,
581 adj->crtc_hsync_start,
582 adj->crtc_hsync_end,
583 adj->crtc_htotal, lm, rm);
584 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
585 adj->crtc_vdisplay,
586 adj->crtc_vsync_start,
587 adj->crtc_vsync_end,
588 adj->crtc_vtotal, tm, bm);
589
590 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100591 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
592 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100593
Russell King178e5612014-10-11 23:57:04 +0100594 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100595
Russell King96f60e32012-08-15 13:59:49 +0100596 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
597 if (val != dcrtc->dumb_ctrl) {
598 dcrtc->dumb_ctrl = val;
599 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
600 }
601
Russell Kinge0ac5e92015-06-29 18:01:38 +0100602 /*
603 * If we are blanked, we would have disabled the clock. Re-enable
604 * it so that compute_clock() does the right thing.
605 */
606 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
607 WARN_ON(clk_prepare_enable(dcrtc->clk));
608
Russell King96f60e32012-08-15 13:59:49 +0100609 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100610 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100611
612 /* Ensure graphic fifo is enabled */
613 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
614 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
615
616 if (interlaced ^ dcrtc->interlaced) {
617 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
618 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
619 else
620 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
621 dcrtc->interlaced = interlaced;
622 }
623
624 spin_lock_irqsave(&dcrtc->irq_lock, flags);
625
626 /* Even interlaced/progressive frame */
627 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
628 adj->crtc_htotal;
629 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
630 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100631 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100632 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100633
634 if (interlaced) {
635 /* Odd interlaced frame */
636 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
637 (1 << 16);
638 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
639 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100640 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100641 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100642 } else {
643 dcrtc->v[0] = dcrtc->v[1];
644 }
645
646 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
647
648 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
649 armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
650 armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
651 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
652 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
653 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
654 LCD_SPUT_V_H_TOTAL);
655
Russell King42e62ba2014-04-22 15:24:03 +0100656 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100657 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
658 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
659 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100660 }
Russell King96f60e32012-08-15 13:59:49 +0100661
662 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
Matt Roperf4510a22014-04-01 15:22:40 -0700663 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
664 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100665
Matt Roperf4510a22014-04-01 15:22:40 -0700666 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
Russell King96f60e32012-08-15 13:59:49 +0100667 val |= CFG_PALETTE_ENA;
668
669 if (interlaced)
670 val |= CFG_GRA_FTOGGLE;
671
672 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
673 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
674 CFG_SWAPYU | CFG_YUV2RGB) |
675 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
676 LCD_SPU_DMA_CTRL0);
677
678 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
679 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
680
681 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
682 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
683 armada_reg_queue_end(regs, i);
684
685 armada_drm_crtc_update_regs(dcrtc, regs);
686 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
687
688 armada_drm_crtc_update(dcrtc);
689
Russell King178e5612014-10-11 23:57:04 +0100690 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100691 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
692
693 return 0;
694}
695
696/* The mode_config.mutex will be held for this call */
697static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
698 struct drm_framebuffer *old_fb)
699{
700 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
701 struct armada_regs regs[4];
702 unsigned i;
703
Matt Roperf4510a22014-04-01 15:22:40 -0700704 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100705 dcrtc->interlaced);
706 armada_reg_queue_end(regs, i);
707
708 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100709 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
710 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100711
712 /* Take a reference to the new fb as we're using it */
Matt Roperf4510a22014-04-01 15:22:40 -0700713 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100714
715 /* Update the base in the CRTC */
716 armada_drm_crtc_update_regs(dcrtc, regs);
717
718 /* Drop our previously held reference */
719 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
720
721 return 0;
722}
723
Russell King58326802015-07-15 18:11:25 +0100724void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
725 struct drm_plane *plane)
726{
Russell King9099ea12015-07-15 18:11:25 +0100727 u32 sram_para1, dma_ctrl0_mask;
Russell King58326802015-07-15 18:11:25 +0100728
729 /*
730 * Drop our reference on any framebuffer attached to this plane.
731 * We don't need to NULL this out as drm_plane_force_disable(),
732 * and __setplane_internal() will do so for an overlay plane, and
733 * __drm_helper_disable_unused_functions() will do so for the
734 * primary plane.
735 */
736 if (plane->fb)
737 drm_framebuffer_unreference(plane->fb);
738
739 /* Power down the Y/U/V FIFOs */
740 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
741
742 /* Power down most RAMs and FIFOs if this is the primary plane */
Russell King9099ea12015-07-15 18:11:25 +0100743 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
Russell King58326802015-07-15 18:11:25 +0100744 sram_para1 |= CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
745 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell King9099ea12015-07-15 18:11:25 +0100746 dma_ctrl0_mask = CFG_GRA_ENA;
747 } else {
748 dma_ctrl0_mask = CFG_DMA_ENA;
749 }
750
751 spin_lock_irq(&dcrtc->irq_lock);
752 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
753 spin_unlock_irq(&dcrtc->irq_lock);
Russell King58326802015-07-15 18:11:25 +0100754
755 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
756}
757
Russell King96f60e32012-08-15 13:59:49 +0100758/* The mode_config.mutex will be held for this call */
759static void armada_drm_crtc_disable(struct drm_crtc *crtc)
760{
761 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
762
763 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King58326802015-07-15 18:11:25 +0100764 armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
Russell King96f60e32012-08-15 13:59:49 +0100765}
766
767static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
768 .dpms = armada_drm_crtc_dpms,
769 .prepare = armada_drm_crtc_prepare,
770 .commit = armada_drm_crtc_commit,
771 .mode_fixup = armada_drm_crtc_mode_fixup,
772 .mode_set = armada_drm_crtc_mode_set,
773 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100774 .disable = armada_drm_crtc_disable,
775};
776
Russell King662af0d2013-05-19 10:55:17 +0100777static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
778 unsigned stride, unsigned width, unsigned height)
779{
780 uint32_t addr;
781 unsigned y;
782
783 addr = SRAM_HWC32_RAM1;
784 for (y = 0; y < height; y++) {
785 uint32_t *p = &pix[y * stride];
786 unsigned x;
787
788 for (x = 0; x < width; x++, p++) {
789 uint32_t val = *p;
790
791 val = (val & 0xff00ff00) |
792 (val & 0x000000ff) << 16 |
793 (val & 0x00ff0000) >> 16;
794
795 writel_relaxed(val,
796 base + LCD_SPU_SRAM_WRDAT);
797 writel_relaxed(addr | SRAM_WRITE,
798 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100799 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100800 addr += 1;
801 if ((addr & 0x00ff) == 0)
802 addr += 0xf00;
803 if ((addr & 0x30ff) == 0)
804 addr = SRAM_HWC32_RAM2;
805 }
806 }
807}
808
809static void armada_drm_crtc_cursor_tran(void __iomem *base)
810{
811 unsigned addr;
812
813 for (addr = 0; addr < 256; addr++) {
814 /* write the default value */
815 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
816 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
817 base + LCD_SPU_SRAM_CTRL);
818 }
819}
820
821static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
822{
823 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
824 uint32_t yoff, yscr, h = dcrtc->cursor_h;
825 uint32_t para1;
826
827 /*
828 * Calculate the visible width and height of the cursor,
829 * screen position, and the position in the cursor bitmap.
830 */
831 if (dcrtc->cursor_x < 0) {
832 xoff = -dcrtc->cursor_x;
833 xscr = 0;
834 w -= min(xoff, w);
835 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
836 xoff = 0;
837 xscr = dcrtc->cursor_x;
838 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
839 } else {
840 xoff = 0;
841 xscr = dcrtc->cursor_x;
842 }
843
844 if (dcrtc->cursor_y < 0) {
845 yoff = -dcrtc->cursor_y;
846 yscr = 0;
847 h -= min(yoff, h);
848 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
849 yoff = 0;
850 yscr = dcrtc->cursor_y;
851 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
852 } else {
853 yoff = 0;
854 yscr = dcrtc->cursor_y;
855 }
856
857 /* On interlaced modes, the vertical cursor size must be halved */
858 s = dcrtc->cursor_w;
859 if (dcrtc->interlaced) {
860 s *= 2;
861 yscr /= 2;
862 h /= 2;
863 }
864
865 if (!dcrtc->cursor_obj || !h || !w) {
866 spin_lock_irq(&dcrtc->irq_lock);
867 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
868 dcrtc->cursor_update = false;
869 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
870 spin_unlock_irq(&dcrtc->irq_lock);
871 return 0;
872 }
873
874 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
875 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
876 dcrtc->base + LCD_SPU_SRAM_PARA1);
877
878 /*
879 * Initialize the transparency if the SRAM was powered down.
880 * We must also reload the cursor data as well.
881 */
882 if (!(para1 & CFG_CSB_256x32)) {
883 armada_drm_crtc_cursor_tran(dcrtc->base);
884 reload = true;
885 }
886
887 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
888 spin_lock_irq(&dcrtc->irq_lock);
889 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
890 dcrtc->cursor_update = false;
891 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
892 spin_unlock_irq(&dcrtc->irq_lock);
893 reload = true;
894 }
895 if (reload) {
896 struct armada_gem_object *obj = dcrtc->cursor_obj;
897 uint32_t *pix;
898 /* Set the top-left corner of the cursor image */
899 pix = obj->addr;
900 pix += yoff * s + xoff;
901 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
902 }
903
904 /* Reload the cursor position, size and enable in the IRQ handler */
905 spin_lock_irq(&dcrtc->irq_lock);
906 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
907 dcrtc->cursor_hw_sz = h << 16 | w;
908 dcrtc->cursor_update = true;
909 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
910 spin_unlock_irq(&dcrtc->irq_lock);
911
912 return 0;
913}
914
915static void cursor_update(void *data)
916{
917 armada_drm_crtc_cursor_update(data, true);
918}
919
920static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
921 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
922{
923 struct drm_device *dev = crtc->dev;
924 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100925 struct armada_gem_object *obj = NULL;
926 int ret;
927
928 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100929 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100930 return -ENXIO;
931
932 if (handle && w > 0 && h > 0) {
933 /* maximum size is 64x32 or 32x64 */
934 if (w > 64 || h > 64 || (w > 32 && h > 32))
935 return -ENOMEM;
936
937 obj = armada_gem_object_lookup(dev, file, handle);
938 if (!obj)
939 return -ENOENT;
940
941 /* Must be a kernel-mapped object */
942 if (!obj->addr) {
943 drm_gem_object_unreference_unlocked(&obj->obj);
944 return -EINVAL;
945 }
946
947 if (obj->obj.size < w * h * 4) {
948 DRM_ERROR("buffer is too small\n");
949 drm_gem_object_unreference_unlocked(&obj->obj);
950 return -ENOMEM;
951 }
952 }
953
954 mutex_lock(&dev->struct_mutex);
955 if (dcrtc->cursor_obj) {
956 dcrtc->cursor_obj->update = NULL;
957 dcrtc->cursor_obj->update_data = NULL;
958 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
959 }
960 dcrtc->cursor_obj = obj;
961 dcrtc->cursor_w = w;
962 dcrtc->cursor_h = h;
963 ret = armada_drm_crtc_cursor_update(dcrtc, true);
964 if (obj) {
965 obj->update_data = dcrtc;
966 obj->update = cursor_update;
967 }
968 mutex_unlock(&dev->struct_mutex);
969
970 return ret;
971}
972
973static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
974{
975 struct drm_device *dev = crtc->dev;
976 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100977 int ret;
978
979 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100980 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100981 return -EFAULT;
982
983 mutex_lock(&dev->struct_mutex);
984 dcrtc->cursor_x = x;
985 dcrtc->cursor_y = y;
986 ret = armada_drm_crtc_cursor_update(dcrtc, false);
987 mutex_unlock(&dev->struct_mutex);
988
989 return ret;
990}
991
Russell King96f60e32012-08-15 13:59:49 +0100992static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
993{
994 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
995 struct armada_private *priv = crtc->dev->dev_private;
996
Russell King662af0d2013-05-19 10:55:17 +0100997 if (dcrtc->cursor_obj)
998 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
999
Russell King96f60e32012-08-15 13:59:49 +01001000 priv->dcrtc[dcrtc->num] = NULL;
1001 drm_crtc_cleanup(&dcrtc->crtc);
1002
1003 if (!IS_ERR(dcrtc->clk))
1004 clk_disable_unprepare(dcrtc->clk);
1005
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001006 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1007
Russell King9611cb92014-06-15 11:21:23 +01001008 of_node_put(dcrtc->crtc.port);
1009
Russell King96f60e32012-08-15 13:59:49 +01001010 kfree(dcrtc);
1011}
1012
1013/*
1014 * The mode_config lock is held here, to prevent races between this
1015 * and a mode_set.
1016 */
1017static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Dave Airlie5e4e3ba2013-10-22 09:38:18 +01001018 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Russell King96f60e32012-08-15 13:59:49 +01001019{
1020 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1021 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001022 unsigned i;
1023 int ret;
1024
1025 /* We don't support changing the pixel format */
Matt Roperf4510a22014-04-01 15:22:40 -07001026 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Russell King96f60e32012-08-15 13:59:49 +01001027 return -EINVAL;
1028
1029 work = kmalloc(sizeof(*work), GFP_KERNEL);
1030 if (!work)
1031 return -ENOMEM;
1032
Russell King4b5dda82015-08-06 16:37:18 +01001033 work->work.fn = armada_drm_crtc_complete_frame_work;
Russell King96f60e32012-08-15 13:59:49 +01001034 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -07001035 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001036
1037 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1038 dcrtc->interlaced);
1039 armada_reg_queue_end(work->regs, i);
1040
1041 /*
Russell Kingc5488302014-10-11 23:53:35 +01001042 * Ensure that we hold a reference on the new framebuffer.
1043 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001044 */
Russell Kingc5488302014-10-11 23:53:35 +01001045 drm_framebuffer_reference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001046
1047 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
1048 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001049 /* Undo our reference above */
1050 drm_framebuffer_unreference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001051 kfree(work);
1052 return ret;
1053 }
1054
1055 /*
1056 * Don't take a reference on the new framebuffer;
1057 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1058 * will _not_ drop that reference on successful return from this
1059 * function. Simply mark this new framebuffer as the current one.
1060 */
Matt Roperf4510a22014-04-01 15:22:40 -07001061 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001062
1063 /*
1064 * Finally, if the display is blanked, we won't receive an
1065 * interrupt, so complete it now.
1066 */
Russell King4b5dda82015-08-06 16:37:18 +01001067 if (dpms_blanked(dcrtc->dpms))
1068 armada_drm_plane_work_run(dcrtc, drm_to_armada_plane(dcrtc->crtc.primary));
Russell King96f60e32012-08-15 13:59:49 +01001069
1070 return 0;
1071}
1072
1073static int
1074armada_drm_crtc_set_property(struct drm_crtc *crtc,
1075 struct drm_property *property, uint64_t val)
1076{
1077 struct armada_private *priv = crtc->dev->dev_private;
1078 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1079 bool update_csc = false;
1080
1081 if (property == priv->csc_yuv_prop) {
1082 dcrtc->csc_yuv_mode = val;
1083 update_csc = true;
1084 } else if (property == priv->csc_rgb_prop) {
1085 dcrtc->csc_rgb_mode = val;
1086 update_csc = true;
1087 }
1088
1089 if (update_csc) {
1090 uint32_t val;
1091
1092 val = dcrtc->spu_iopad_ctrl |
1093 armada_drm_crtc_calculate_csc(dcrtc);
1094 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1095 }
1096
1097 return 0;
1098}
1099
1100static struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001101 .cursor_set = armada_drm_crtc_cursor_set,
1102 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001103 .destroy = armada_drm_crtc_destroy,
1104 .set_config = drm_crtc_helper_set_config,
1105 .page_flip = armada_drm_crtc_page_flip,
1106 .set_property = armada_drm_crtc_set_property,
1107};
1108
Russell Kingde323012015-07-15 18:11:24 +01001109static const struct drm_plane_funcs armada_primary_plane_funcs = {
1110 .update_plane = drm_primary_helper_update,
1111 .disable_plane = drm_primary_helper_disable,
1112 .destroy = drm_primary_helper_destroy,
1113};
1114
Russell King5740d272015-07-15 18:11:25 +01001115int armada_drm_plane_init(struct armada_plane *plane)
1116{
1117 init_waitqueue_head(&plane->frame_wait);
1118
1119 return 0;
1120}
1121
Russell King96f60e32012-08-15 13:59:49 +01001122static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1123 { CSC_AUTO, "Auto" },
1124 { CSC_YUV_CCIR601, "CCIR601" },
1125 { CSC_YUV_CCIR709, "CCIR709" },
1126};
1127
1128static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1129 { CSC_AUTO, "Auto" },
1130 { CSC_RGB_COMPUTER, "Computer system" },
1131 { CSC_RGB_STUDIO, "Studio" },
1132};
1133
1134static int armada_drm_crtc_create_properties(struct drm_device *dev)
1135{
1136 struct armada_private *priv = dev->dev_private;
1137
1138 if (priv->csc_yuv_prop)
1139 return 0;
1140
1141 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1142 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1143 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1144 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1145 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1146 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1147
1148 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1149 return -ENOMEM;
1150
1151 return 0;
1152}
1153
Russell King0fb29702015-06-06 21:46:53 +01001154static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001155 struct resource *res, int irq, const struct armada_variant *variant,
1156 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001157{
Russell Kingd8c96082014-04-22 11:10:15 +01001158 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001159 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001160 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001161 void __iomem *base;
1162 int ret;
1163
Russell Kingd8c96082014-04-22 11:10:15 +01001164 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001165 if (ret)
1166 return ret;
1167
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001168 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001169 if (IS_ERR(base))
1170 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001171
1172 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1173 if (!dcrtc) {
1174 DRM_ERROR("failed to allocate Armada crtc\n");
1175 return -ENOMEM;
1176 }
1177
Russell Kingd8c96082014-04-22 11:10:15 +01001178 if (dev != drm->dev)
1179 dev_set_drvdata(dev, dcrtc);
1180
Russell King42e62ba2014-04-22 15:24:03 +01001181 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001182 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001183 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001184 dcrtc->clk = ERR_PTR(-EINVAL);
1185 dcrtc->csc_yuv_mode = CSC_AUTO;
1186 dcrtc->csc_rgb_mode = CSC_AUTO;
1187 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1188 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1189 spin_lock_init(&dcrtc->irq_lock);
1190 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1191 INIT_LIST_HEAD(&dcrtc->vbl_list);
Russell King96f60e32012-08-15 13:59:49 +01001192
1193 /* Initialize some registers which we don't otherwise set */
1194 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1195 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1196 writel_relaxed(dcrtc->spu_iopad_ctrl,
1197 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1198 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1199 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1200 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1201 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1202 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1203 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001204 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1205 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001206
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001207 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1208 dcrtc);
1209 if (ret < 0) {
1210 kfree(dcrtc);
1211 return ret;
1212 }
Russell King96f60e32012-08-15 13:59:49 +01001213
Russell King42e62ba2014-04-22 15:24:03 +01001214 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001215 ret = dcrtc->variant->init(dcrtc, dev);
Russell King96f60e32012-08-15 13:59:49 +01001216 if (ret) {
1217 kfree(dcrtc);
1218 return ret;
1219 }
1220 }
1221
1222 /* Ensure AXI pipeline is enabled */
1223 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1224
1225 priv->dcrtc[dcrtc->num] = dcrtc;
1226
Russell King9611cb92014-06-15 11:21:23 +01001227 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001228
Russell Kingde323012015-07-15 18:11:24 +01001229 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King1c914ce2015-07-15 18:11:24 +01001230 if (!primary)
1231 return -ENOMEM;
1232
Russell King5740d272015-07-15 18:11:25 +01001233 ret = armada_drm_plane_init(primary);
1234 if (ret) {
1235 kfree(primary);
1236 return ret;
1237 }
1238
Russell Kingde323012015-07-15 18:11:24 +01001239 ret = drm_universal_plane_init(drm, &primary->base, 0,
1240 &armada_primary_plane_funcs,
1241 armada_primary_formats,
1242 ARRAY_SIZE(armada_primary_formats),
1243 DRM_PLANE_TYPE_PRIMARY);
1244 if (ret) {
1245 kfree(primary);
1246 return ret;
1247 }
1248
1249 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Russell King1c914ce2015-07-15 18:11:24 +01001250 &armada_crtc_funcs);
1251 if (ret)
1252 goto err_crtc_init;
1253
Russell King96f60e32012-08-15 13:59:49 +01001254 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1255
1256 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1257 dcrtc->csc_yuv_mode);
1258 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1259 dcrtc->csc_rgb_mode);
1260
Russell Kingd8c96082014-04-22 11:10:15 +01001261 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001262
1263err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001264 primary->base.funcs->destroy(&primary->base);
Russell King1c914ce2015-07-15 18:11:24 +01001265 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001266}
Russell Kingd8c96082014-04-22 11:10:15 +01001267
1268static int
1269armada_lcd_bind(struct device *dev, struct device *master, void *data)
1270{
1271 struct platform_device *pdev = to_platform_device(dev);
1272 struct drm_device *drm = data;
1273 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1274 int irq = platform_get_irq(pdev, 0);
1275 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001276 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001277
1278 if (irq < 0)
1279 return irq;
1280
1281 if (!dev->of_node) {
1282 const struct platform_device_id *id;
1283
1284 id = platform_get_device_id(pdev);
1285 if (!id)
1286 return -ENXIO;
1287
1288 variant = (const struct armada_variant *)id->driver_data;
1289 } else {
1290 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001291 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001292
1293 match = of_match_device(dev->driver->of_match_table, dev);
1294 if (!match)
1295 return -ENXIO;
1296
Russell King9611cb92014-06-15 11:21:23 +01001297 np = of_get_child_by_name(parent, "ports");
1298 if (np)
1299 parent = np;
1300 port = of_get_child_by_name(parent, "port");
1301 of_node_put(np);
1302 if (!port) {
1303 dev_err(dev, "no port node found in %s\n",
1304 parent->full_name);
1305 return -ENXIO;
1306 }
1307
Russell Kingd8c96082014-04-22 11:10:15 +01001308 variant = match->data;
1309 }
1310
Russell King9611cb92014-06-15 11:21:23 +01001311 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001312}
1313
1314static void
1315armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1316{
1317 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1318
1319 armada_drm_crtc_destroy(&dcrtc->crtc);
1320}
1321
1322static const struct component_ops armada_lcd_ops = {
1323 .bind = armada_lcd_bind,
1324 .unbind = armada_lcd_unbind,
1325};
1326
1327static int armada_lcd_probe(struct platform_device *pdev)
1328{
1329 return component_add(&pdev->dev, &armada_lcd_ops);
1330}
1331
1332static int armada_lcd_remove(struct platform_device *pdev)
1333{
1334 component_del(&pdev->dev, &armada_lcd_ops);
1335 return 0;
1336}
1337
1338static struct of_device_id armada_lcd_of_match[] = {
1339 {
1340 .compatible = "marvell,dove-lcd",
1341 .data = &armada510_ops,
1342 },
1343 {}
1344};
1345MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1346
1347static const struct platform_device_id armada_lcd_platform_ids[] = {
1348 {
1349 .name = "armada-lcd",
1350 .driver_data = (unsigned long)&armada510_ops,
1351 }, {
1352 .name = "armada-510-lcd",
1353 .driver_data = (unsigned long)&armada510_ops,
1354 },
1355 { },
1356};
1357MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1358
1359struct platform_driver armada_lcd_platform_driver = {
1360 .probe = armada_lcd_probe,
1361 .remove = armada_lcd_remove,
1362 .driver = {
1363 .name = "armada-lcd",
1364 .owner = THIS_MODULE,
1365 .of_match_table = armada_lcd_of_match,
1366 },
1367 .id_table = armada_lcd_platform_ids,
1368};