blob: 46d932bc767855b6eeefe77ba509bfd91e36b372 [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 {
23 struct drm_pending_vblank_event *event;
24 struct armada_regs regs[4];
25 struct drm_framebuffer *old_fb;
26};
27
28enum csc_mode {
29 CSC_AUTO = 0,
30 CSC_YUV_CCIR601 = 1,
31 CSC_YUV_CCIR709 = 2,
32 CSC_RGB_COMPUTER = 1,
33 CSC_RGB_STUDIO = 2,
34};
35
Russell King1c914ce2015-07-15 18:11:24 +010036static const uint32_t armada_primary_formats[] = {
37 DRM_FORMAT_UYVY,
38 DRM_FORMAT_YUYV,
39 DRM_FORMAT_VYUY,
40 DRM_FORMAT_YVYU,
41 DRM_FORMAT_ARGB8888,
42 DRM_FORMAT_ABGR8888,
43 DRM_FORMAT_XRGB8888,
44 DRM_FORMAT_XBGR8888,
45 DRM_FORMAT_RGB888,
46 DRM_FORMAT_BGR888,
47 DRM_FORMAT_ARGB1555,
48 DRM_FORMAT_ABGR1555,
49 DRM_FORMAT_RGB565,
50 DRM_FORMAT_BGR565,
51};
52
Russell King96f60e32012-08-15 13:59:49 +010053/*
54 * A note about interlacing. Let's consider HDMI 1920x1080i.
55 * The timing parameters we have from X are:
56 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
57 * 1920 2448 2492 2640 1080 1084 1094 1125
58 * Which get translated to:
59 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
60 * 1920 2448 2492 2640 540 542 547 562
61 *
62 * This is how it is defined by CEA-861-D - line and pixel numbers are
63 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
64 * line: 2640. The odd frame, the first active line is at line 21, and
65 * the even frame, the first active line is 584.
66 *
67 * LN: 560 561 562 563 567 568 569
68 * DE: ~~~|____________________________//__________________________
69 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
70 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
71 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
72 *
73 * LN: 1123 1124 1125 1 5 6 7
74 * DE: ~~~|____________________________//__________________________
75 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
76 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
77 * 23 blanking lines
78 *
79 * The Armada LCD Controller line and pixel numbers are, like X timings,
80 * referenced to the top left of the active frame.
81 *
82 * So, translating these to our LCD controller:
83 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
84 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
85 * Note: Vsync front porch remains constant!
86 *
87 * if (odd_frame) {
88 * vtotal = mode->crtc_vtotal + 1;
89 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
90 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
91 * } else {
92 * vtotal = mode->crtc_vtotal;
93 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
94 * vhorizpos = mode->crtc_hsync_start;
95 * }
96 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
97 *
98 * So, we need to reprogram these registers on each vsync event:
99 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
100 *
101 * Note: we do not use the frame done interrupts because these appear
102 * to happen too early, and lead to jitter on the display (presumably
103 * they occur at the end of the last active line, before the vsync back
104 * porch, which we're reprogramming.)
105 */
106
107void
108armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
109{
110 while (regs->offset != ~0) {
111 void __iomem *reg = dcrtc->base + regs->offset;
112 uint32_t val;
113
114 val = regs->mask;
115 if (val != 0)
116 val &= readl_relaxed(reg);
117 writel_relaxed(val | regs->val, reg);
118 ++regs;
119 }
120}
121
122#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
123
124static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
125{
126 uint32_t dumb_ctrl;
127
128 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
129
130 if (!dpms_blanked(dcrtc->dpms))
131 dumb_ctrl |= CFG_DUMB_ENA;
132
133 /*
134 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
135 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
136 * force LCD_D[23:0] to output blank color, overriding the GPIO or
137 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
138 */
139 if (dpms_blanked(dcrtc->dpms) &&
140 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
141 dumb_ctrl &= ~DUMB_MASK;
142 dumb_ctrl |= DUMB_BLANK;
143 }
144
145 /*
146 * The documentation doesn't indicate what the normal state of
147 * the sync signals are. Sebastian Hesselbart kindly probed
148 * these signals on his board to determine their state.
149 *
150 * The non-inverted state of the sync signals is active high.
151 * Setting these bits makes the appropriate signal active low.
152 */
153 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
154 dumb_ctrl |= CFG_INV_CSYNC;
155 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
156 dumb_ctrl |= CFG_INV_HSYNC;
157 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
158 dumb_ctrl |= CFG_INV_VSYNC;
159
160 if (dcrtc->dumb_ctrl != dumb_ctrl) {
161 dcrtc->dumb_ctrl = dumb_ctrl;
162 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
163 }
164}
165
166static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
167 int x, int y, struct armada_regs *regs, bool interlaced)
168{
169 struct armada_gem_object *obj = drm_fb_obj(fb);
170 unsigned pitch = fb->pitches[0];
171 unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
172 uint32_t addr_odd, addr_even;
173 unsigned i = 0;
174
175 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
176 pitch, x, y, fb->bits_per_pixel);
177
178 addr_odd = addr_even = obj->dev_addr + offset;
179
180 if (interlaced) {
181 addr_even += pitch;
182 pitch *= 2;
183 }
184
185 /* write offset, base, and pitch */
186 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
187 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
188 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
189
190 return i;
191}
192
Russell King7c8f7e12015-06-29 17:52:16 +0100193void armada_drm_vbl_event_add(struct armada_crtc *dcrtc,
194 struct armada_vbl_event *evt)
195{
196 unsigned long flags;
197 bool not_on_list;
198
199 WARN_ON(drm_vblank_get(dcrtc->crtc.dev, dcrtc->num));
200
201 spin_lock_irqsave(&dcrtc->irq_lock, flags);
202 not_on_list = list_empty(&evt->node);
203 if (not_on_list)
204 list_add_tail(&evt->node, &dcrtc->vbl_list);
205 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
206
207 if (!not_on_list)
208 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
209}
210
211void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc,
212 struct armada_vbl_event *evt)
213{
Russell King6908cf72015-07-15 18:11:25 +0100214 spin_lock_irq(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100215 if (!list_empty(&evt->node)) {
216 list_del_init(&evt->node);
217 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
218 }
Russell King6908cf72015-07-15 18:11:25 +0100219 spin_unlock_irq(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100220}
221
222static void armada_drm_vbl_event_run(struct armada_crtc *dcrtc)
223{
224 struct armada_vbl_event *e, *n;
225
226 list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
227 list_del_init(&e->node);
228 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
229 e->fn(dcrtc, e->data);
230 }
231}
232
Russell King96f60e32012-08-15 13:59:49 +0100233static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
234 struct armada_frame_work *work)
235{
236 struct drm_device *dev = dcrtc->crtc.dev;
Russell King96f60e32012-08-15 13:59:49 +0100237 int ret;
238
239 ret = drm_vblank_get(dev, dcrtc->num);
240 if (ret) {
241 DRM_ERROR("failed to acquire vblank counter\n");
242 return ret;
243 }
244
Russell King709ffd82015-07-15 18:09:38 +0100245 if (cmpxchg(&dcrtc->frame_work, NULL, work)) {
Russell King96f60e32012-08-15 13:59:49 +0100246 drm_vblank_put(dev, dcrtc->num);
Russell King709ffd82015-07-15 18:09:38 +0100247 ret = -EBUSY;
248 }
Russell King96f60e32012-08-15 13:59:49 +0100249
250 return ret;
251}
252
Russell King709ffd82015-07-15 18:09:38 +0100253static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
254 struct armada_frame_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100255{
256 struct drm_device *dev = dcrtc->crtc.dev;
Russell King709ffd82015-07-15 18:09:38 +0100257 unsigned long flags;
Russell King96f60e32012-08-15 13:59:49 +0100258
Russell King709ffd82015-07-15 18:09:38 +0100259 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100260 armada_drm_crtc_update_regs(dcrtc, work->regs);
Russell King709ffd82015-07-15 18:09:38 +0100261 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100262
Russell King709ffd82015-07-15 18:09:38 +0100263 if (work->event) {
264 spin_lock_irqsave(&dev->event_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100265 drm_send_vblank_event(dev, dcrtc->num, work->event);
Russell King709ffd82015-07-15 18:09:38 +0100266 spin_unlock_irqrestore(&dev->event_lock, flags);
267 }
Russell King96f60e32012-08-15 13:59:49 +0100268
269 drm_vblank_put(dev, dcrtc->num);
270
271 /* Finally, queue the process-half of the cleanup. */
272 __armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb);
273 kfree(work);
274}
275
276static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
277 struct drm_framebuffer *fb, bool force)
278{
279 struct armada_frame_work *work;
280
281 if (!fb)
282 return;
283
284 if (force) {
285 /* Display is disabled, so just drop the old fb */
286 drm_framebuffer_unreference(fb);
287 return;
288 }
289
290 work = kmalloc(sizeof(*work), GFP_KERNEL);
291 if (work) {
292 int i = 0;
293 work->event = NULL;
294 work->old_fb = fb;
295 armada_reg_queue_end(work->regs, i);
296
297 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
298 return;
299
300 kfree(work);
301 }
302
303 /*
304 * Oops - just drop the reference immediately and hope for
305 * the best. The worst that will happen is the buffer gets
306 * reused before it has finished being displayed.
307 */
308 drm_framebuffer_unreference(fb);
309}
310
311static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
312{
Russell King709ffd82015-07-15 18:09:38 +0100313 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +0100314
315 /*
316 * Tell the DRM core that vblank IRQs aren't going to happen for
317 * a while. This cleans up any pending vblank events for us.
318 */
Russell King178e5612014-10-11 23:57:04 +0100319 drm_crtc_vblank_off(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100320
321 /* Handle any pending flip event. */
Russell King709ffd82015-07-15 18:09:38 +0100322 work = xchg(&dcrtc->frame_work, NULL);
323 if (work)
324 armada_drm_crtc_complete_frame_work(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +0100325}
326
327void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
328 int idx)
329{
330}
331
332void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
333 int idx)
334{
335}
336
337/* The mode_config.mutex will be held for this call */
338static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
339{
340 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
341
342 if (dcrtc->dpms != dpms) {
343 dcrtc->dpms = dpms;
Russell Kinge0ac5e92015-06-29 18:01:38 +0100344 if (!IS_ERR(dcrtc->clk) && !dpms_blanked(dpms))
345 WARN_ON(clk_prepare_enable(dcrtc->clk));
Russell King96f60e32012-08-15 13:59:49 +0100346 armada_drm_crtc_update(dcrtc);
Russell Kinge0ac5e92015-06-29 18:01:38 +0100347 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dpms))
348 clk_disable_unprepare(dcrtc->clk);
Russell King96f60e32012-08-15 13:59:49 +0100349 if (dpms_blanked(dpms))
350 armada_drm_vblank_off(dcrtc);
Russell King178e5612014-10-11 23:57:04 +0100351 else
352 drm_crtc_vblank_on(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100353 }
354}
355
356/*
357 * Prepare for a mode set. Turn off overlay to ensure that we don't end
358 * up with the overlay size being bigger than the active screen size.
359 * We rely upon X refreshing this state after the mode set has completed.
360 *
361 * The mode_config.mutex will be held for this call
362 */
363static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
364{
365 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
366 struct drm_plane *plane;
367
368 /*
369 * If we have an overlay plane associated with this CRTC, disable
370 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100371 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100372 */
373 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100374 if (plane)
375 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100376}
377
378/* The mode_config.mutex will be held for this call */
379static void armada_drm_crtc_commit(struct drm_crtc *crtc)
380{
381 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
382
383 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
384 dcrtc->dpms = DRM_MODE_DPMS_ON;
385 armada_drm_crtc_update(dcrtc);
386 }
387}
388
389/* The mode_config.mutex will be held for this call */
390static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
391 const struct drm_display_mode *mode, struct drm_display_mode *adj)
392{
Russell King96f60e32012-08-15 13:59:49 +0100393 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
394 int ret;
395
396 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100397 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100398 adj->flags & DRM_MODE_FLAG_INTERLACE)
399 return false;
400
401 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100402 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100403 if (ret)
404 return false;
405
406 return true;
407}
408
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100409static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100410{
Russell King96f60e32012-08-15 13:59:49 +0100411 void __iomem *base = dcrtc->base;
412
413 if (stat & DMA_FF_UNDERFLOW)
414 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
415 if (stat & GRA_FF_UNDERFLOW)
416 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
417
418 if (stat & VSYNC_IRQ)
419 drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
420
421 spin_lock(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100422 armada_drm_vbl_event_run(dcrtc);
Russell King96f60e32012-08-15 13:59:49 +0100423
424 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
425 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
426 uint32_t val;
427
428 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
429 writel_relaxed(dcrtc->v[i].spu_v_h_total,
430 base + LCD_SPUT_V_H_TOTAL);
431
432 val = readl_relaxed(base + LCD_SPU_ADV_REG);
433 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
434 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100435 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100436 }
Russell King662af0d2013-05-19 10:55:17 +0100437
438 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
439 writel_relaxed(dcrtc->cursor_hw_pos,
440 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
441 writel_relaxed(dcrtc->cursor_hw_sz,
442 base + LCD_SPU_HWC_HPXL_VLN);
443 armada_updatel(CFG_HWC_ENA,
444 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
445 base + LCD_SPU_DMA_CTRL0);
446 dcrtc->cursor_update = false;
447 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
448 }
449
Russell King96f60e32012-08-15 13:59:49 +0100450 spin_unlock(&dcrtc->irq_lock);
451
452 if (stat & GRA_FRAME_IRQ) {
Russell King709ffd82015-07-15 18:09:38 +0100453 struct armada_frame_work *work = xchg(&dcrtc->frame_work, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100454
Russell King709ffd82015-07-15 18:09:38 +0100455 if (work)
456 armada_drm_crtc_complete_frame_work(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +0100457
Russell King5740d272015-07-15 18:11:25 +0100458 wake_up(&drm_to_armada_plane(dcrtc->crtc.primary)->frame_wait);
Russell King96f60e32012-08-15 13:59:49 +0100459 }
460}
461
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100462static irqreturn_t armada_drm_irq(int irq, void *arg)
463{
464 struct armada_crtc *dcrtc = arg;
465 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
466
467 /*
468 * This is rediculous - rather than writing bits to clear, we
469 * have to set the actual status register value. This is racy.
470 */
471 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
472
473 /* Mask out those interrupts we haven't enabled */
474 v = stat & dcrtc->irq_ena;
475
476 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
477 armada_drm_crtc_irq(dcrtc, stat);
478 return IRQ_HANDLED;
479 }
480 return IRQ_NONE;
481}
482
Russell King96f60e32012-08-15 13:59:49 +0100483/* These are locked by dev->vbl_lock */
484void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
485{
486 if (dcrtc->irq_ena & mask) {
487 dcrtc->irq_ena &= ~mask;
488 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
489 }
490}
491
492void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
493{
494 if ((dcrtc->irq_ena & mask) != mask) {
495 dcrtc->irq_ena |= mask;
496 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
497 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
498 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
499 }
500}
501
502static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
503{
504 struct drm_display_mode *adj = &dcrtc->crtc.mode;
505 uint32_t val = 0;
506
507 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
508 val |= CFG_CSC_YUV_CCIR709;
509 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
510 val |= CFG_CSC_RGB_STUDIO;
511
512 /*
513 * In auto mode, set the colorimetry, based upon the HDMI spec.
514 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
515 * ITU601. It may be more appropriate to set this depending on
516 * the source - but what if the graphic frame is YUV and the
517 * video frame is RGB?
518 */
519 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
520 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
521 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
522 if (dcrtc->csc_yuv_mode == CSC_AUTO)
523 val |= CFG_CSC_YUV_CCIR709;
524 }
525
526 /*
527 * We assume we're connected to a TV-like device, so the YUV->RGB
528 * conversion should produce a limited range. We should set this
529 * depending on the connectors attached to this CRTC, and what
530 * kind of device they report being connected.
531 */
532 if (dcrtc->csc_rgb_mode == CSC_AUTO)
533 val |= CFG_CSC_RGB_STUDIO;
534
535 return val;
536}
537
538/* The mode_config.mutex will be held for this call */
539static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
540 struct drm_display_mode *mode, struct drm_display_mode *adj,
541 int x, int y, struct drm_framebuffer *old_fb)
542{
Russell King96f60e32012-08-15 13:59:49 +0100543 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
544 struct armada_regs regs[17];
545 uint32_t lm, rm, tm, bm, val, sclk;
546 unsigned long flags;
547 unsigned i;
548 bool interlaced;
549
Matt Roperf4510a22014-04-01 15:22:40 -0700550 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100551
552 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
553
Matt Roperf4510a22014-04-01 15:22:40 -0700554 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
555 x, y, regs, interlaced);
Russell King96f60e32012-08-15 13:59:49 +0100556
557 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
558 lm = adj->crtc_htotal - adj->crtc_hsync_end;
559 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
560 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
561
562 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
563 adj->crtc_hdisplay,
564 adj->crtc_hsync_start,
565 adj->crtc_hsync_end,
566 adj->crtc_htotal, lm, rm);
567 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
568 adj->crtc_vdisplay,
569 adj->crtc_vsync_start,
570 adj->crtc_vsync_end,
571 adj->crtc_vtotal, tm, bm);
572
573 /* Wait for pending flips to complete */
Russell King5740d272015-07-15 18:11:25 +0100574 wait_event(drm_to_armada_plane(dcrtc->crtc.primary)->frame_wait,
575 !dcrtc->frame_work);
Russell King96f60e32012-08-15 13:59:49 +0100576
Russell King178e5612014-10-11 23:57:04 +0100577 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100578
Russell King96f60e32012-08-15 13:59:49 +0100579 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
580 if (val != dcrtc->dumb_ctrl) {
581 dcrtc->dumb_ctrl = val;
582 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
583 }
584
Russell Kinge0ac5e92015-06-29 18:01:38 +0100585 /*
586 * If we are blanked, we would have disabled the clock. Re-enable
587 * it so that compute_clock() does the right thing.
588 */
589 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
590 WARN_ON(clk_prepare_enable(dcrtc->clk));
591
Russell King96f60e32012-08-15 13:59:49 +0100592 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100593 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100594
595 /* Ensure graphic fifo is enabled */
596 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
597 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
598
599 if (interlaced ^ dcrtc->interlaced) {
600 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
601 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
602 else
603 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
604 dcrtc->interlaced = interlaced;
605 }
606
607 spin_lock_irqsave(&dcrtc->irq_lock, flags);
608
609 /* Even interlaced/progressive frame */
610 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
611 adj->crtc_htotal;
612 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
613 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100614 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100615 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100616
617 if (interlaced) {
618 /* Odd interlaced frame */
619 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
620 (1 << 16);
621 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
622 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100623 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100624 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100625 } else {
626 dcrtc->v[0] = dcrtc->v[1];
627 }
628
629 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
630
631 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
632 armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
633 armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
634 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
635 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
636 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
637 LCD_SPUT_V_H_TOTAL);
638
Russell King42e62ba2014-04-22 15:24:03 +0100639 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100640 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
641 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
642 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100643 }
Russell King96f60e32012-08-15 13:59:49 +0100644
645 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
Matt Roperf4510a22014-04-01 15:22:40 -0700646 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
647 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100648
Matt Roperf4510a22014-04-01 15:22:40 -0700649 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
Russell King96f60e32012-08-15 13:59:49 +0100650 val |= CFG_PALETTE_ENA;
651
652 if (interlaced)
653 val |= CFG_GRA_FTOGGLE;
654
655 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
656 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
657 CFG_SWAPYU | CFG_YUV2RGB) |
658 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
659 LCD_SPU_DMA_CTRL0);
660
661 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
662 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
663
664 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
665 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
666 armada_reg_queue_end(regs, i);
667
668 armada_drm_crtc_update_regs(dcrtc, regs);
669 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
670
671 armada_drm_crtc_update(dcrtc);
672
Russell King178e5612014-10-11 23:57:04 +0100673 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100674 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
675
676 return 0;
677}
678
679/* The mode_config.mutex will be held for this call */
680static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
681 struct drm_framebuffer *old_fb)
682{
683 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
684 struct armada_regs regs[4];
685 unsigned i;
686
Matt Roperf4510a22014-04-01 15:22:40 -0700687 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100688 dcrtc->interlaced);
689 armada_reg_queue_end(regs, i);
690
691 /* Wait for pending flips to complete */
Russell King5740d272015-07-15 18:11:25 +0100692 wait_event(drm_to_armada_plane(dcrtc->crtc.primary)->frame_wait,
693 !dcrtc->frame_work);
Russell King96f60e32012-08-15 13:59:49 +0100694
695 /* Take a reference to the new fb as we're using it */
Matt Roperf4510a22014-04-01 15:22:40 -0700696 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100697
698 /* Update the base in the CRTC */
699 armada_drm_crtc_update_regs(dcrtc, regs);
700
701 /* Drop our previously held reference */
702 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
703
704 return 0;
705}
706
Russell King58326802015-07-15 18:11:25 +0100707void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
708 struct drm_plane *plane)
709{
Russell King9099ea12015-07-15 18:11:25 +0100710 u32 sram_para1, dma_ctrl0_mask;
Russell King58326802015-07-15 18:11:25 +0100711
712 /*
713 * Drop our reference on any framebuffer attached to this plane.
714 * We don't need to NULL this out as drm_plane_force_disable(),
715 * and __setplane_internal() will do so for an overlay plane, and
716 * __drm_helper_disable_unused_functions() will do so for the
717 * primary plane.
718 */
719 if (plane->fb)
720 drm_framebuffer_unreference(plane->fb);
721
722 /* Power down the Y/U/V FIFOs */
723 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
724
725 /* Power down most RAMs and FIFOs if this is the primary plane */
Russell King9099ea12015-07-15 18:11:25 +0100726 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
Russell King58326802015-07-15 18:11:25 +0100727 sram_para1 |= CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
728 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell King9099ea12015-07-15 18:11:25 +0100729 dma_ctrl0_mask = CFG_GRA_ENA;
730 } else {
731 dma_ctrl0_mask = CFG_DMA_ENA;
732 }
733
734 spin_lock_irq(&dcrtc->irq_lock);
735 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
736 spin_unlock_irq(&dcrtc->irq_lock);
Russell King58326802015-07-15 18:11:25 +0100737
738 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
739}
740
Russell King96f60e32012-08-15 13:59:49 +0100741/* The mode_config.mutex will be held for this call */
742static void armada_drm_crtc_disable(struct drm_crtc *crtc)
743{
744 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
745
746 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King58326802015-07-15 18:11:25 +0100747 armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
Russell King96f60e32012-08-15 13:59:49 +0100748}
749
750static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
751 .dpms = armada_drm_crtc_dpms,
752 .prepare = armada_drm_crtc_prepare,
753 .commit = armada_drm_crtc_commit,
754 .mode_fixup = armada_drm_crtc_mode_fixup,
755 .mode_set = armada_drm_crtc_mode_set,
756 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100757 .disable = armada_drm_crtc_disable,
758};
759
Russell King662af0d2013-05-19 10:55:17 +0100760static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
761 unsigned stride, unsigned width, unsigned height)
762{
763 uint32_t addr;
764 unsigned y;
765
766 addr = SRAM_HWC32_RAM1;
767 for (y = 0; y < height; y++) {
768 uint32_t *p = &pix[y * stride];
769 unsigned x;
770
771 for (x = 0; x < width; x++, p++) {
772 uint32_t val = *p;
773
774 val = (val & 0xff00ff00) |
775 (val & 0x000000ff) << 16 |
776 (val & 0x00ff0000) >> 16;
777
778 writel_relaxed(val,
779 base + LCD_SPU_SRAM_WRDAT);
780 writel_relaxed(addr | SRAM_WRITE,
781 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100782 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100783 addr += 1;
784 if ((addr & 0x00ff) == 0)
785 addr += 0xf00;
786 if ((addr & 0x30ff) == 0)
787 addr = SRAM_HWC32_RAM2;
788 }
789 }
790}
791
792static void armada_drm_crtc_cursor_tran(void __iomem *base)
793{
794 unsigned addr;
795
796 for (addr = 0; addr < 256; addr++) {
797 /* write the default value */
798 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
799 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
800 base + LCD_SPU_SRAM_CTRL);
801 }
802}
803
804static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
805{
806 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
807 uint32_t yoff, yscr, h = dcrtc->cursor_h;
808 uint32_t para1;
809
810 /*
811 * Calculate the visible width and height of the cursor,
812 * screen position, and the position in the cursor bitmap.
813 */
814 if (dcrtc->cursor_x < 0) {
815 xoff = -dcrtc->cursor_x;
816 xscr = 0;
817 w -= min(xoff, w);
818 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
819 xoff = 0;
820 xscr = dcrtc->cursor_x;
821 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
822 } else {
823 xoff = 0;
824 xscr = dcrtc->cursor_x;
825 }
826
827 if (dcrtc->cursor_y < 0) {
828 yoff = -dcrtc->cursor_y;
829 yscr = 0;
830 h -= min(yoff, h);
831 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
832 yoff = 0;
833 yscr = dcrtc->cursor_y;
834 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
835 } else {
836 yoff = 0;
837 yscr = dcrtc->cursor_y;
838 }
839
840 /* On interlaced modes, the vertical cursor size must be halved */
841 s = dcrtc->cursor_w;
842 if (dcrtc->interlaced) {
843 s *= 2;
844 yscr /= 2;
845 h /= 2;
846 }
847
848 if (!dcrtc->cursor_obj || !h || !w) {
849 spin_lock_irq(&dcrtc->irq_lock);
850 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
851 dcrtc->cursor_update = false;
852 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
853 spin_unlock_irq(&dcrtc->irq_lock);
854 return 0;
855 }
856
857 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
858 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
859 dcrtc->base + LCD_SPU_SRAM_PARA1);
860
861 /*
862 * Initialize the transparency if the SRAM was powered down.
863 * We must also reload the cursor data as well.
864 */
865 if (!(para1 & CFG_CSB_256x32)) {
866 armada_drm_crtc_cursor_tran(dcrtc->base);
867 reload = true;
868 }
869
870 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
871 spin_lock_irq(&dcrtc->irq_lock);
872 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
873 dcrtc->cursor_update = false;
874 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
875 spin_unlock_irq(&dcrtc->irq_lock);
876 reload = true;
877 }
878 if (reload) {
879 struct armada_gem_object *obj = dcrtc->cursor_obj;
880 uint32_t *pix;
881 /* Set the top-left corner of the cursor image */
882 pix = obj->addr;
883 pix += yoff * s + xoff;
884 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
885 }
886
887 /* Reload the cursor position, size and enable in the IRQ handler */
888 spin_lock_irq(&dcrtc->irq_lock);
889 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
890 dcrtc->cursor_hw_sz = h << 16 | w;
891 dcrtc->cursor_update = true;
892 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
893 spin_unlock_irq(&dcrtc->irq_lock);
894
895 return 0;
896}
897
898static void cursor_update(void *data)
899{
900 armada_drm_crtc_cursor_update(data, true);
901}
902
903static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
904 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
905{
906 struct drm_device *dev = crtc->dev;
907 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100908 struct armada_gem_object *obj = NULL;
909 int ret;
910
911 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100912 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100913 return -ENXIO;
914
915 if (handle && w > 0 && h > 0) {
916 /* maximum size is 64x32 or 32x64 */
917 if (w > 64 || h > 64 || (w > 32 && h > 32))
918 return -ENOMEM;
919
920 obj = armada_gem_object_lookup(dev, file, handle);
921 if (!obj)
922 return -ENOENT;
923
924 /* Must be a kernel-mapped object */
925 if (!obj->addr) {
926 drm_gem_object_unreference_unlocked(&obj->obj);
927 return -EINVAL;
928 }
929
930 if (obj->obj.size < w * h * 4) {
931 DRM_ERROR("buffer is too small\n");
932 drm_gem_object_unreference_unlocked(&obj->obj);
933 return -ENOMEM;
934 }
935 }
936
937 mutex_lock(&dev->struct_mutex);
938 if (dcrtc->cursor_obj) {
939 dcrtc->cursor_obj->update = NULL;
940 dcrtc->cursor_obj->update_data = NULL;
941 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
942 }
943 dcrtc->cursor_obj = obj;
944 dcrtc->cursor_w = w;
945 dcrtc->cursor_h = h;
946 ret = armada_drm_crtc_cursor_update(dcrtc, true);
947 if (obj) {
948 obj->update_data = dcrtc;
949 obj->update = cursor_update;
950 }
951 mutex_unlock(&dev->struct_mutex);
952
953 return ret;
954}
955
956static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
957{
958 struct drm_device *dev = crtc->dev;
959 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100960 int ret;
961
962 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100963 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100964 return -EFAULT;
965
966 mutex_lock(&dev->struct_mutex);
967 dcrtc->cursor_x = x;
968 dcrtc->cursor_y = y;
969 ret = armada_drm_crtc_cursor_update(dcrtc, false);
970 mutex_unlock(&dev->struct_mutex);
971
972 return ret;
973}
974
Russell King96f60e32012-08-15 13:59:49 +0100975static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
976{
977 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
978 struct armada_private *priv = crtc->dev->dev_private;
979
Russell King662af0d2013-05-19 10:55:17 +0100980 if (dcrtc->cursor_obj)
981 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
982
Russell King96f60e32012-08-15 13:59:49 +0100983 priv->dcrtc[dcrtc->num] = NULL;
984 drm_crtc_cleanup(&dcrtc->crtc);
985
986 if (!IS_ERR(dcrtc->clk))
987 clk_disable_unprepare(dcrtc->clk);
988
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100989 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
990
Russell King9611cb92014-06-15 11:21:23 +0100991 of_node_put(dcrtc->crtc.port);
992
Russell King96f60e32012-08-15 13:59:49 +0100993 kfree(dcrtc);
994}
995
996/*
997 * The mode_config lock is held here, to prevent races between this
998 * and a mode_set.
999 */
1000static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Dave Airlie5e4e3ba2013-10-22 09:38:18 +01001001 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Russell King96f60e32012-08-15 13:59:49 +01001002{
1003 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1004 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001005 unsigned i;
1006 int ret;
1007
1008 /* We don't support changing the pixel format */
Matt Roperf4510a22014-04-01 15:22:40 -07001009 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Russell King96f60e32012-08-15 13:59:49 +01001010 return -EINVAL;
1011
1012 work = kmalloc(sizeof(*work), GFP_KERNEL);
1013 if (!work)
1014 return -ENOMEM;
1015
1016 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -07001017 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001018
1019 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1020 dcrtc->interlaced);
1021 armada_reg_queue_end(work->regs, i);
1022
1023 /*
Russell Kingc5488302014-10-11 23:53:35 +01001024 * Ensure that we hold a reference on the new framebuffer.
1025 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001026 */
Russell Kingc5488302014-10-11 23:53:35 +01001027 drm_framebuffer_reference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001028
1029 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
1030 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001031 /* Undo our reference above */
1032 drm_framebuffer_unreference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001033 kfree(work);
1034 return ret;
1035 }
1036
1037 /*
1038 * Don't take a reference on the new framebuffer;
1039 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1040 * will _not_ drop that reference on successful return from this
1041 * function. Simply mark this new framebuffer as the current one.
1042 */
Matt Roperf4510a22014-04-01 15:22:40 -07001043 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001044
1045 /*
1046 * Finally, if the display is blanked, we won't receive an
1047 * interrupt, so complete it now.
1048 */
1049 if (dpms_blanked(dcrtc->dpms)) {
Russell King709ffd82015-07-15 18:09:38 +01001050 struct armada_frame_work *work = xchg(&dcrtc->frame_work, NULL);
1051
1052 if (work)
1053 armada_drm_crtc_complete_frame_work(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +01001054 }
1055
1056 return 0;
1057}
1058
1059static int
1060armada_drm_crtc_set_property(struct drm_crtc *crtc,
1061 struct drm_property *property, uint64_t val)
1062{
1063 struct armada_private *priv = crtc->dev->dev_private;
1064 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1065 bool update_csc = false;
1066
1067 if (property == priv->csc_yuv_prop) {
1068 dcrtc->csc_yuv_mode = val;
1069 update_csc = true;
1070 } else if (property == priv->csc_rgb_prop) {
1071 dcrtc->csc_rgb_mode = val;
1072 update_csc = true;
1073 }
1074
1075 if (update_csc) {
1076 uint32_t val;
1077
1078 val = dcrtc->spu_iopad_ctrl |
1079 armada_drm_crtc_calculate_csc(dcrtc);
1080 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1081 }
1082
1083 return 0;
1084}
1085
1086static struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001087 .cursor_set = armada_drm_crtc_cursor_set,
1088 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001089 .destroy = armada_drm_crtc_destroy,
1090 .set_config = drm_crtc_helper_set_config,
1091 .page_flip = armada_drm_crtc_page_flip,
1092 .set_property = armada_drm_crtc_set_property,
1093};
1094
Russell Kingde323012015-07-15 18:11:24 +01001095static const struct drm_plane_funcs armada_primary_plane_funcs = {
1096 .update_plane = drm_primary_helper_update,
1097 .disable_plane = drm_primary_helper_disable,
1098 .destroy = drm_primary_helper_destroy,
1099};
1100
Russell King5740d272015-07-15 18:11:25 +01001101int armada_drm_plane_init(struct armada_plane *plane)
1102{
1103 init_waitqueue_head(&plane->frame_wait);
1104
1105 return 0;
1106}
1107
Russell King96f60e32012-08-15 13:59:49 +01001108static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1109 { CSC_AUTO, "Auto" },
1110 { CSC_YUV_CCIR601, "CCIR601" },
1111 { CSC_YUV_CCIR709, "CCIR709" },
1112};
1113
1114static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1115 { CSC_AUTO, "Auto" },
1116 { CSC_RGB_COMPUTER, "Computer system" },
1117 { CSC_RGB_STUDIO, "Studio" },
1118};
1119
1120static int armada_drm_crtc_create_properties(struct drm_device *dev)
1121{
1122 struct armada_private *priv = dev->dev_private;
1123
1124 if (priv->csc_yuv_prop)
1125 return 0;
1126
1127 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1128 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1129 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1130 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1131 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1132 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1133
1134 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1135 return -ENOMEM;
1136
1137 return 0;
1138}
1139
Russell King0fb29702015-06-06 21:46:53 +01001140static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001141 struct resource *res, int irq, const struct armada_variant *variant,
1142 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001143{
Russell Kingd8c96082014-04-22 11:10:15 +01001144 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001145 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001146 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001147 void __iomem *base;
1148 int ret;
1149
Russell Kingd8c96082014-04-22 11:10:15 +01001150 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001151 if (ret)
1152 return ret;
1153
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001154 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001155 if (IS_ERR(base))
1156 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001157
1158 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1159 if (!dcrtc) {
1160 DRM_ERROR("failed to allocate Armada crtc\n");
1161 return -ENOMEM;
1162 }
1163
Russell Kingd8c96082014-04-22 11:10:15 +01001164 if (dev != drm->dev)
1165 dev_set_drvdata(dev, dcrtc);
1166
Russell King42e62ba2014-04-22 15:24:03 +01001167 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001168 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001169 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001170 dcrtc->clk = ERR_PTR(-EINVAL);
1171 dcrtc->csc_yuv_mode = CSC_AUTO;
1172 dcrtc->csc_rgb_mode = CSC_AUTO;
1173 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1174 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1175 spin_lock_init(&dcrtc->irq_lock);
1176 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1177 INIT_LIST_HEAD(&dcrtc->vbl_list);
Russell King96f60e32012-08-15 13:59:49 +01001178
1179 /* Initialize some registers which we don't otherwise set */
1180 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1181 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1182 writel_relaxed(dcrtc->spu_iopad_ctrl,
1183 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1184 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1185 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1186 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1187 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1188 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1189 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001190 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1191 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001192
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001193 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1194 dcrtc);
1195 if (ret < 0) {
1196 kfree(dcrtc);
1197 return ret;
1198 }
Russell King96f60e32012-08-15 13:59:49 +01001199
Russell King42e62ba2014-04-22 15:24:03 +01001200 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001201 ret = dcrtc->variant->init(dcrtc, dev);
Russell King96f60e32012-08-15 13:59:49 +01001202 if (ret) {
1203 kfree(dcrtc);
1204 return ret;
1205 }
1206 }
1207
1208 /* Ensure AXI pipeline is enabled */
1209 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1210
1211 priv->dcrtc[dcrtc->num] = dcrtc;
1212
Russell King9611cb92014-06-15 11:21:23 +01001213 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001214
Russell Kingde323012015-07-15 18:11:24 +01001215 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King1c914ce2015-07-15 18:11:24 +01001216 if (!primary)
1217 return -ENOMEM;
1218
Russell King5740d272015-07-15 18:11:25 +01001219 ret = armada_drm_plane_init(primary);
1220 if (ret) {
1221 kfree(primary);
1222 return ret;
1223 }
1224
Russell Kingde323012015-07-15 18:11:24 +01001225 ret = drm_universal_plane_init(drm, &primary->base, 0,
1226 &armada_primary_plane_funcs,
1227 armada_primary_formats,
1228 ARRAY_SIZE(armada_primary_formats),
1229 DRM_PLANE_TYPE_PRIMARY);
1230 if (ret) {
1231 kfree(primary);
1232 return ret;
1233 }
1234
1235 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Russell King1c914ce2015-07-15 18:11:24 +01001236 &armada_crtc_funcs);
1237 if (ret)
1238 goto err_crtc_init;
1239
Russell King96f60e32012-08-15 13:59:49 +01001240 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1241
1242 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1243 dcrtc->csc_yuv_mode);
1244 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1245 dcrtc->csc_rgb_mode);
1246
Russell Kingd8c96082014-04-22 11:10:15 +01001247 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001248
1249err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001250 primary->base.funcs->destroy(&primary->base);
Russell King1c914ce2015-07-15 18:11:24 +01001251 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001252}
Russell Kingd8c96082014-04-22 11:10:15 +01001253
1254static int
1255armada_lcd_bind(struct device *dev, struct device *master, void *data)
1256{
1257 struct platform_device *pdev = to_platform_device(dev);
1258 struct drm_device *drm = data;
1259 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1260 int irq = platform_get_irq(pdev, 0);
1261 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001262 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001263
1264 if (irq < 0)
1265 return irq;
1266
1267 if (!dev->of_node) {
1268 const struct platform_device_id *id;
1269
1270 id = platform_get_device_id(pdev);
1271 if (!id)
1272 return -ENXIO;
1273
1274 variant = (const struct armada_variant *)id->driver_data;
1275 } else {
1276 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001277 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001278
1279 match = of_match_device(dev->driver->of_match_table, dev);
1280 if (!match)
1281 return -ENXIO;
1282
Russell King9611cb92014-06-15 11:21:23 +01001283 np = of_get_child_by_name(parent, "ports");
1284 if (np)
1285 parent = np;
1286 port = of_get_child_by_name(parent, "port");
1287 of_node_put(np);
1288 if (!port) {
1289 dev_err(dev, "no port node found in %s\n",
1290 parent->full_name);
1291 return -ENXIO;
1292 }
1293
Russell Kingd8c96082014-04-22 11:10:15 +01001294 variant = match->data;
1295 }
1296
Russell King9611cb92014-06-15 11:21:23 +01001297 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001298}
1299
1300static void
1301armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1302{
1303 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1304
1305 armada_drm_crtc_destroy(&dcrtc->crtc);
1306}
1307
1308static const struct component_ops armada_lcd_ops = {
1309 .bind = armada_lcd_bind,
1310 .unbind = armada_lcd_unbind,
1311};
1312
1313static int armada_lcd_probe(struct platform_device *pdev)
1314{
1315 return component_add(&pdev->dev, &armada_lcd_ops);
1316}
1317
1318static int armada_lcd_remove(struct platform_device *pdev)
1319{
1320 component_del(&pdev->dev, &armada_lcd_ops);
1321 return 0;
1322}
1323
1324static struct of_device_id armada_lcd_of_match[] = {
1325 {
1326 .compatible = "marvell,dove-lcd",
1327 .data = &armada510_ops,
1328 },
1329 {}
1330};
1331MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1332
1333static const struct platform_device_id armada_lcd_platform_ids[] = {
1334 {
1335 .name = "armada-lcd",
1336 .driver_data = (unsigned long)&armada510_ops,
1337 }, {
1338 .name = "armada-510-lcd",
1339 .driver_data = (unsigned long)&armada510_ops,
1340 },
1341 { },
1342};
1343MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1344
1345struct platform_driver armada_lcd_platform_driver = {
1346 .probe = armada_lcd_probe,
1347 .remove = armada_lcd_remove,
1348 .driver = {
1349 .name = "armada-lcd",
1350 .owner = THIS_MODULE,
1351 .of_match_table = armada_lcd_of_match,
1352 },
1353 .id_table = armada_lcd_platform_ids,
1354};