blob: bbf5ff785cd29f06d95b2f3e431ec1b62ca46578 [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
36/*
37 * A note about interlacing. Let's consider HDMI 1920x1080i.
38 * The timing parameters we have from X are:
39 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
40 * 1920 2448 2492 2640 1080 1084 1094 1125
41 * Which get translated to:
42 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
43 * 1920 2448 2492 2640 540 542 547 562
44 *
45 * This is how it is defined by CEA-861-D - line and pixel numbers are
46 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
47 * line: 2640. The odd frame, the first active line is at line 21, and
48 * the even frame, the first active line is 584.
49 *
50 * LN: 560 561 562 563 567 568 569
51 * DE: ~~~|____________________________//__________________________
52 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
53 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
54 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
55 *
56 * LN: 1123 1124 1125 1 5 6 7
57 * DE: ~~~|____________________________//__________________________
58 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
59 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
60 * 23 blanking lines
61 *
62 * The Armada LCD Controller line and pixel numbers are, like X timings,
63 * referenced to the top left of the active frame.
64 *
65 * So, translating these to our LCD controller:
66 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
67 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
68 * Note: Vsync front porch remains constant!
69 *
70 * if (odd_frame) {
71 * vtotal = mode->crtc_vtotal + 1;
72 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
73 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
74 * } else {
75 * vtotal = mode->crtc_vtotal;
76 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
77 * vhorizpos = mode->crtc_hsync_start;
78 * }
79 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
80 *
81 * So, we need to reprogram these registers on each vsync event:
82 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
83 *
84 * Note: we do not use the frame done interrupts because these appear
85 * to happen too early, and lead to jitter on the display (presumably
86 * they occur at the end of the last active line, before the vsync back
87 * porch, which we're reprogramming.)
88 */
89
90void
91armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
92{
93 while (regs->offset != ~0) {
94 void __iomem *reg = dcrtc->base + regs->offset;
95 uint32_t val;
96
97 val = regs->mask;
98 if (val != 0)
99 val &= readl_relaxed(reg);
100 writel_relaxed(val | regs->val, reg);
101 ++regs;
102 }
103}
104
105#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
106
107static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
108{
109 uint32_t dumb_ctrl;
110
111 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
112
113 if (!dpms_blanked(dcrtc->dpms))
114 dumb_ctrl |= CFG_DUMB_ENA;
115
116 /*
117 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
118 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
119 * force LCD_D[23:0] to output blank color, overriding the GPIO or
120 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
121 */
122 if (dpms_blanked(dcrtc->dpms) &&
123 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
124 dumb_ctrl &= ~DUMB_MASK;
125 dumb_ctrl |= DUMB_BLANK;
126 }
127
128 /*
129 * The documentation doesn't indicate what the normal state of
130 * the sync signals are. Sebastian Hesselbart kindly probed
131 * these signals on his board to determine their state.
132 *
133 * The non-inverted state of the sync signals is active high.
134 * Setting these bits makes the appropriate signal active low.
135 */
136 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
137 dumb_ctrl |= CFG_INV_CSYNC;
138 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
139 dumb_ctrl |= CFG_INV_HSYNC;
140 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
141 dumb_ctrl |= CFG_INV_VSYNC;
142
143 if (dcrtc->dumb_ctrl != dumb_ctrl) {
144 dcrtc->dumb_ctrl = dumb_ctrl;
145 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
146 }
147}
148
149static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
150 int x, int y, struct armada_regs *regs, bool interlaced)
151{
152 struct armada_gem_object *obj = drm_fb_obj(fb);
153 unsigned pitch = fb->pitches[0];
154 unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
155 uint32_t addr_odd, addr_even;
156 unsigned i = 0;
157
158 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
159 pitch, x, y, fb->bits_per_pixel);
160
161 addr_odd = addr_even = obj->dev_addr + offset;
162
163 if (interlaced) {
164 addr_even += pitch;
165 pitch *= 2;
166 }
167
168 /* write offset, base, and pitch */
169 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
170 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
171 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
172
173 return i;
174}
175
Russell King7c8f7e12015-06-29 17:52:16 +0100176void armada_drm_vbl_event_add(struct armada_crtc *dcrtc,
177 struct armada_vbl_event *evt)
178{
179 unsigned long flags;
180 bool not_on_list;
181
182 WARN_ON(drm_vblank_get(dcrtc->crtc.dev, dcrtc->num));
183
184 spin_lock_irqsave(&dcrtc->irq_lock, flags);
185 not_on_list = list_empty(&evt->node);
186 if (not_on_list)
187 list_add_tail(&evt->node, &dcrtc->vbl_list);
188 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
189
190 if (!not_on_list)
191 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
192}
193
194void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc,
195 struct armada_vbl_event *evt)
196{
197 if (!list_empty(&evt->node)) {
198 list_del_init(&evt->node);
199 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
200 }
201}
202
203static void armada_drm_vbl_event_run(struct armada_crtc *dcrtc)
204{
205 struct armada_vbl_event *e, *n;
206
207 list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
208 list_del_init(&e->node);
209 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
210 e->fn(dcrtc, e->data);
211 }
212}
213
Russell King96f60e32012-08-15 13:59:49 +0100214static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
215 struct armada_frame_work *work)
216{
217 struct drm_device *dev = dcrtc->crtc.dev;
218 unsigned long flags;
219 int ret;
220
221 ret = drm_vblank_get(dev, dcrtc->num);
222 if (ret) {
223 DRM_ERROR("failed to acquire vblank counter\n");
224 return ret;
225 }
226
227 spin_lock_irqsave(&dev->event_lock, flags);
228 if (!dcrtc->frame_work)
229 dcrtc->frame_work = work;
230 else
231 ret = -EBUSY;
232 spin_unlock_irqrestore(&dev->event_lock, flags);
233
234 if (ret)
235 drm_vblank_put(dev, dcrtc->num);
236
237 return ret;
238}
239
240static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc)
241{
242 struct drm_device *dev = dcrtc->crtc.dev;
243 struct armada_frame_work *work = dcrtc->frame_work;
244
245 dcrtc->frame_work = NULL;
246
247 armada_drm_crtc_update_regs(dcrtc, work->regs);
248
249 if (work->event)
250 drm_send_vblank_event(dev, dcrtc->num, work->event);
251
252 drm_vblank_put(dev, dcrtc->num);
253
254 /* Finally, queue the process-half of the cleanup. */
255 __armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb);
256 kfree(work);
257}
258
259static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
260 struct drm_framebuffer *fb, bool force)
261{
262 struct armada_frame_work *work;
263
264 if (!fb)
265 return;
266
267 if (force) {
268 /* Display is disabled, so just drop the old fb */
269 drm_framebuffer_unreference(fb);
270 return;
271 }
272
273 work = kmalloc(sizeof(*work), GFP_KERNEL);
274 if (work) {
275 int i = 0;
276 work->event = NULL;
277 work->old_fb = fb;
278 armada_reg_queue_end(work->regs, i);
279
280 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
281 return;
282
283 kfree(work);
284 }
285
286 /*
287 * Oops - just drop the reference immediately and hope for
288 * the best. The worst that will happen is the buffer gets
289 * reused before it has finished being displayed.
290 */
291 drm_framebuffer_unreference(fb);
292}
293
294static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
295{
296 struct drm_device *dev = dcrtc->crtc.dev;
297
298 /*
299 * Tell the DRM core that vblank IRQs aren't going to happen for
300 * a while. This cleans up any pending vblank events for us.
301 */
Russell King178e5612014-10-11 23:57:04 +0100302 drm_crtc_vblank_off(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100303
304 /* Handle any pending flip event. */
305 spin_lock_irq(&dev->event_lock);
306 if (dcrtc->frame_work)
307 armada_drm_crtc_complete_frame_work(dcrtc);
308 spin_unlock_irq(&dev->event_lock);
309}
310
311void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
312 int idx)
313{
314}
315
316void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
317 int idx)
318{
319}
320
321/* The mode_config.mutex will be held for this call */
322static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
323{
324 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
325
326 if (dcrtc->dpms != dpms) {
327 dcrtc->dpms = dpms;
328 armada_drm_crtc_update(dcrtc);
329 if (dpms_blanked(dpms))
330 armada_drm_vblank_off(dcrtc);
Russell King178e5612014-10-11 23:57:04 +0100331 else
332 drm_crtc_vblank_on(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100333 }
334}
335
336/*
337 * Prepare for a mode set. Turn off overlay to ensure that we don't end
338 * up with the overlay size being bigger than the active screen size.
339 * We rely upon X refreshing this state after the mode set has completed.
340 *
341 * The mode_config.mutex will be held for this call
342 */
343static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
344{
345 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
346 struct drm_plane *plane;
347
348 /*
349 * If we have an overlay plane associated with this CRTC, disable
350 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100351 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100352 */
353 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100354 if (plane)
355 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100356}
357
358/* The mode_config.mutex will be held for this call */
359static void armada_drm_crtc_commit(struct drm_crtc *crtc)
360{
361 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
362
363 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
364 dcrtc->dpms = DRM_MODE_DPMS_ON;
365 armada_drm_crtc_update(dcrtc);
366 }
367}
368
369/* The mode_config.mutex will be held for this call */
370static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
371 const struct drm_display_mode *mode, struct drm_display_mode *adj)
372{
Russell King96f60e32012-08-15 13:59:49 +0100373 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
374 int ret;
375
376 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100377 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100378 adj->flags & DRM_MODE_FLAG_INTERLACE)
379 return false;
380
381 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100382 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100383 if (ret)
384 return false;
385
386 return true;
387}
388
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100389static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100390{
Russell King96f60e32012-08-15 13:59:49 +0100391 void __iomem *base = dcrtc->base;
392
393 if (stat & DMA_FF_UNDERFLOW)
394 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
395 if (stat & GRA_FF_UNDERFLOW)
396 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
397
398 if (stat & VSYNC_IRQ)
399 drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
400
401 spin_lock(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100402 armada_drm_vbl_event_run(dcrtc);
Russell King96f60e32012-08-15 13:59:49 +0100403
404 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
405 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
406 uint32_t val;
407
408 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
409 writel_relaxed(dcrtc->v[i].spu_v_h_total,
410 base + LCD_SPUT_V_H_TOTAL);
411
412 val = readl_relaxed(base + LCD_SPU_ADV_REG);
413 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
414 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100415 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100416 }
Russell King662af0d2013-05-19 10:55:17 +0100417
418 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
419 writel_relaxed(dcrtc->cursor_hw_pos,
420 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
421 writel_relaxed(dcrtc->cursor_hw_sz,
422 base + LCD_SPU_HWC_HPXL_VLN);
423 armada_updatel(CFG_HWC_ENA,
424 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
425 base + LCD_SPU_DMA_CTRL0);
426 dcrtc->cursor_update = false;
427 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
428 }
429
Russell King96f60e32012-08-15 13:59:49 +0100430 spin_unlock(&dcrtc->irq_lock);
431
432 if (stat & GRA_FRAME_IRQ) {
433 struct drm_device *dev = dcrtc->crtc.dev;
434
435 spin_lock(&dev->event_lock);
436 if (dcrtc->frame_work)
437 armada_drm_crtc_complete_frame_work(dcrtc);
438 spin_unlock(&dev->event_lock);
439
440 wake_up(&dcrtc->frame_wait);
441 }
442}
443
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100444static irqreturn_t armada_drm_irq(int irq, void *arg)
445{
446 struct armada_crtc *dcrtc = arg;
447 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
448
449 /*
450 * This is rediculous - rather than writing bits to clear, we
451 * have to set the actual status register value. This is racy.
452 */
453 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
454
455 /* Mask out those interrupts we haven't enabled */
456 v = stat & dcrtc->irq_ena;
457
458 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
459 armada_drm_crtc_irq(dcrtc, stat);
460 return IRQ_HANDLED;
461 }
462 return IRQ_NONE;
463}
464
Russell King96f60e32012-08-15 13:59:49 +0100465/* These are locked by dev->vbl_lock */
466void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
467{
468 if (dcrtc->irq_ena & mask) {
469 dcrtc->irq_ena &= ~mask;
470 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
471 }
472}
473
474void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
475{
476 if ((dcrtc->irq_ena & mask) != mask) {
477 dcrtc->irq_ena |= mask;
478 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
479 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
480 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
481 }
482}
483
484static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
485{
486 struct drm_display_mode *adj = &dcrtc->crtc.mode;
487 uint32_t val = 0;
488
489 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
490 val |= CFG_CSC_YUV_CCIR709;
491 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
492 val |= CFG_CSC_RGB_STUDIO;
493
494 /*
495 * In auto mode, set the colorimetry, based upon the HDMI spec.
496 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
497 * ITU601. It may be more appropriate to set this depending on
498 * the source - but what if the graphic frame is YUV and the
499 * video frame is RGB?
500 */
501 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
502 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
503 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
504 if (dcrtc->csc_yuv_mode == CSC_AUTO)
505 val |= CFG_CSC_YUV_CCIR709;
506 }
507
508 /*
509 * We assume we're connected to a TV-like device, so the YUV->RGB
510 * conversion should produce a limited range. We should set this
511 * depending on the connectors attached to this CRTC, and what
512 * kind of device they report being connected.
513 */
514 if (dcrtc->csc_rgb_mode == CSC_AUTO)
515 val |= CFG_CSC_RGB_STUDIO;
516
517 return val;
518}
519
520/* The mode_config.mutex will be held for this call */
521static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
522 struct drm_display_mode *mode, struct drm_display_mode *adj,
523 int x, int y, struct drm_framebuffer *old_fb)
524{
Russell King96f60e32012-08-15 13:59:49 +0100525 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
526 struct armada_regs regs[17];
527 uint32_t lm, rm, tm, bm, val, sclk;
528 unsigned long flags;
529 unsigned i;
530 bool interlaced;
531
Matt Roperf4510a22014-04-01 15:22:40 -0700532 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100533
534 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
535
Matt Roperf4510a22014-04-01 15:22:40 -0700536 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
537 x, y, regs, interlaced);
Russell King96f60e32012-08-15 13:59:49 +0100538
539 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
540 lm = adj->crtc_htotal - adj->crtc_hsync_end;
541 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
542 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
543
544 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
545 adj->crtc_hdisplay,
546 adj->crtc_hsync_start,
547 adj->crtc_hsync_end,
548 adj->crtc_htotal, lm, rm);
549 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
550 adj->crtc_vdisplay,
551 adj->crtc_vsync_start,
552 adj->crtc_vsync_end,
553 adj->crtc_vtotal, tm, bm);
554
555 /* Wait for pending flips to complete */
556 wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
557
Russell King178e5612014-10-11 23:57:04 +0100558 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100559
Russell King96f60e32012-08-15 13:59:49 +0100560 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
561 if (val != dcrtc->dumb_ctrl) {
562 dcrtc->dumb_ctrl = val;
563 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
564 }
565
566 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100567 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100568
569 /* Ensure graphic fifo is enabled */
570 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
571 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
572
573 if (interlaced ^ dcrtc->interlaced) {
574 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
575 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
576 else
577 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
578 dcrtc->interlaced = interlaced;
579 }
580
581 spin_lock_irqsave(&dcrtc->irq_lock, flags);
582
583 /* Even interlaced/progressive frame */
584 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
585 adj->crtc_htotal;
586 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
587 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100588 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100589 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100590
591 if (interlaced) {
592 /* Odd interlaced frame */
593 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
594 (1 << 16);
595 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
596 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100597 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100598 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100599 } else {
600 dcrtc->v[0] = dcrtc->v[1];
601 }
602
603 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
604
605 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
606 armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
607 armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
608 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
609 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
610 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
611 LCD_SPUT_V_H_TOTAL);
612
Russell King42e62ba2014-04-22 15:24:03 +0100613 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100614 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
615 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
616 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100617 }
Russell King96f60e32012-08-15 13:59:49 +0100618
619 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
Matt Roperf4510a22014-04-01 15:22:40 -0700620 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
621 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100622
Matt Roperf4510a22014-04-01 15:22:40 -0700623 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
Russell King96f60e32012-08-15 13:59:49 +0100624 val |= CFG_PALETTE_ENA;
625
626 if (interlaced)
627 val |= CFG_GRA_FTOGGLE;
628
629 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
630 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
631 CFG_SWAPYU | CFG_YUV2RGB) |
632 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
633 LCD_SPU_DMA_CTRL0);
634
635 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
636 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
637
638 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
639 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
640 armada_reg_queue_end(regs, i);
641
642 armada_drm_crtc_update_regs(dcrtc, regs);
643 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
644
645 armada_drm_crtc_update(dcrtc);
646
Russell King178e5612014-10-11 23:57:04 +0100647 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100648 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
649
650 return 0;
651}
652
653/* The mode_config.mutex will be held for this call */
654static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
655 struct drm_framebuffer *old_fb)
656{
657 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
658 struct armada_regs regs[4];
659 unsigned i;
660
Matt Roperf4510a22014-04-01 15:22:40 -0700661 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100662 dcrtc->interlaced);
663 armada_reg_queue_end(regs, i);
664
665 /* Wait for pending flips to complete */
666 wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
667
668 /* Take a reference to the new fb as we're using it */
Matt Roperf4510a22014-04-01 15:22:40 -0700669 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100670
671 /* Update the base in the CRTC */
672 armada_drm_crtc_update_regs(dcrtc, regs);
673
674 /* Drop our previously held reference */
675 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
676
677 return 0;
678}
679
Russell King96f60e32012-08-15 13:59:49 +0100680/* The mode_config.mutex will be held for this call */
681static void armada_drm_crtc_disable(struct drm_crtc *crtc)
682{
683 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
684
685 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -0700686 armada_drm_crtc_finish_fb(dcrtc, crtc->primary->fb, true);
Russell King96f60e32012-08-15 13:59:49 +0100687
688 /* Power down most RAMs and FIFOs */
689 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
690 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
691 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
692}
693
694static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
695 .dpms = armada_drm_crtc_dpms,
696 .prepare = armada_drm_crtc_prepare,
697 .commit = armada_drm_crtc_commit,
698 .mode_fixup = armada_drm_crtc_mode_fixup,
699 .mode_set = armada_drm_crtc_mode_set,
700 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100701 .disable = armada_drm_crtc_disable,
702};
703
Russell King662af0d2013-05-19 10:55:17 +0100704static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
705 unsigned stride, unsigned width, unsigned height)
706{
707 uint32_t addr;
708 unsigned y;
709
710 addr = SRAM_HWC32_RAM1;
711 for (y = 0; y < height; y++) {
712 uint32_t *p = &pix[y * stride];
713 unsigned x;
714
715 for (x = 0; x < width; x++, p++) {
716 uint32_t val = *p;
717
718 val = (val & 0xff00ff00) |
719 (val & 0x000000ff) << 16 |
720 (val & 0x00ff0000) >> 16;
721
722 writel_relaxed(val,
723 base + LCD_SPU_SRAM_WRDAT);
724 writel_relaxed(addr | SRAM_WRITE,
725 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100726 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100727 addr += 1;
728 if ((addr & 0x00ff) == 0)
729 addr += 0xf00;
730 if ((addr & 0x30ff) == 0)
731 addr = SRAM_HWC32_RAM2;
732 }
733 }
734}
735
736static void armada_drm_crtc_cursor_tran(void __iomem *base)
737{
738 unsigned addr;
739
740 for (addr = 0; addr < 256; addr++) {
741 /* write the default value */
742 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
743 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
744 base + LCD_SPU_SRAM_CTRL);
745 }
746}
747
748static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
749{
750 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
751 uint32_t yoff, yscr, h = dcrtc->cursor_h;
752 uint32_t para1;
753
754 /*
755 * Calculate the visible width and height of the cursor,
756 * screen position, and the position in the cursor bitmap.
757 */
758 if (dcrtc->cursor_x < 0) {
759 xoff = -dcrtc->cursor_x;
760 xscr = 0;
761 w -= min(xoff, w);
762 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
763 xoff = 0;
764 xscr = dcrtc->cursor_x;
765 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
766 } else {
767 xoff = 0;
768 xscr = dcrtc->cursor_x;
769 }
770
771 if (dcrtc->cursor_y < 0) {
772 yoff = -dcrtc->cursor_y;
773 yscr = 0;
774 h -= min(yoff, h);
775 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
776 yoff = 0;
777 yscr = dcrtc->cursor_y;
778 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
779 } else {
780 yoff = 0;
781 yscr = dcrtc->cursor_y;
782 }
783
784 /* On interlaced modes, the vertical cursor size must be halved */
785 s = dcrtc->cursor_w;
786 if (dcrtc->interlaced) {
787 s *= 2;
788 yscr /= 2;
789 h /= 2;
790 }
791
792 if (!dcrtc->cursor_obj || !h || !w) {
793 spin_lock_irq(&dcrtc->irq_lock);
794 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
795 dcrtc->cursor_update = false;
796 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
797 spin_unlock_irq(&dcrtc->irq_lock);
798 return 0;
799 }
800
801 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
802 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
803 dcrtc->base + LCD_SPU_SRAM_PARA1);
804
805 /*
806 * Initialize the transparency if the SRAM was powered down.
807 * We must also reload the cursor data as well.
808 */
809 if (!(para1 & CFG_CSB_256x32)) {
810 armada_drm_crtc_cursor_tran(dcrtc->base);
811 reload = true;
812 }
813
814 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
815 spin_lock_irq(&dcrtc->irq_lock);
816 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
817 dcrtc->cursor_update = false;
818 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
819 spin_unlock_irq(&dcrtc->irq_lock);
820 reload = true;
821 }
822 if (reload) {
823 struct armada_gem_object *obj = dcrtc->cursor_obj;
824 uint32_t *pix;
825 /* Set the top-left corner of the cursor image */
826 pix = obj->addr;
827 pix += yoff * s + xoff;
828 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
829 }
830
831 /* Reload the cursor position, size and enable in the IRQ handler */
832 spin_lock_irq(&dcrtc->irq_lock);
833 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
834 dcrtc->cursor_hw_sz = h << 16 | w;
835 dcrtc->cursor_update = true;
836 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
837 spin_unlock_irq(&dcrtc->irq_lock);
838
839 return 0;
840}
841
842static void cursor_update(void *data)
843{
844 armada_drm_crtc_cursor_update(data, true);
845}
846
847static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
848 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
849{
850 struct drm_device *dev = crtc->dev;
851 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100852 struct armada_gem_object *obj = NULL;
853 int ret;
854
855 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100856 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100857 return -ENXIO;
858
859 if (handle && w > 0 && h > 0) {
860 /* maximum size is 64x32 or 32x64 */
861 if (w > 64 || h > 64 || (w > 32 && h > 32))
862 return -ENOMEM;
863
864 obj = armada_gem_object_lookup(dev, file, handle);
865 if (!obj)
866 return -ENOENT;
867
868 /* Must be a kernel-mapped object */
869 if (!obj->addr) {
870 drm_gem_object_unreference_unlocked(&obj->obj);
871 return -EINVAL;
872 }
873
874 if (obj->obj.size < w * h * 4) {
875 DRM_ERROR("buffer is too small\n");
876 drm_gem_object_unreference_unlocked(&obj->obj);
877 return -ENOMEM;
878 }
879 }
880
881 mutex_lock(&dev->struct_mutex);
882 if (dcrtc->cursor_obj) {
883 dcrtc->cursor_obj->update = NULL;
884 dcrtc->cursor_obj->update_data = NULL;
885 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
886 }
887 dcrtc->cursor_obj = obj;
888 dcrtc->cursor_w = w;
889 dcrtc->cursor_h = h;
890 ret = armada_drm_crtc_cursor_update(dcrtc, true);
891 if (obj) {
892 obj->update_data = dcrtc;
893 obj->update = cursor_update;
894 }
895 mutex_unlock(&dev->struct_mutex);
896
897 return ret;
898}
899
900static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
901{
902 struct drm_device *dev = crtc->dev;
903 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100904 int ret;
905
906 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100907 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100908 return -EFAULT;
909
910 mutex_lock(&dev->struct_mutex);
911 dcrtc->cursor_x = x;
912 dcrtc->cursor_y = y;
913 ret = armada_drm_crtc_cursor_update(dcrtc, false);
914 mutex_unlock(&dev->struct_mutex);
915
916 return ret;
917}
918
Russell King96f60e32012-08-15 13:59:49 +0100919static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
920{
921 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
922 struct armada_private *priv = crtc->dev->dev_private;
923
Russell King662af0d2013-05-19 10:55:17 +0100924 if (dcrtc->cursor_obj)
925 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
926
Russell King96f60e32012-08-15 13:59:49 +0100927 priv->dcrtc[dcrtc->num] = NULL;
928 drm_crtc_cleanup(&dcrtc->crtc);
929
930 if (!IS_ERR(dcrtc->clk))
931 clk_disable_unprepare(dcrtc->clk);
932
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100933 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
934
Russell King9611cb92014-06-15 11:21:23 +0100935 of_node_put(dcrtc->crtc.port);
936
Russell King96f60e32012-08-15 13:59:49 +0100937 kfree(dcrtc);
938}
939
940/*
941 * The mode_config lock is held here, to prevent races between this
942 * and a mode_set.
943 */
944static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Dave Airlie5e4e3ba2013-10-22 09:38:18 +0100945 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Russell King96f60e32012-08-15 13:59:49 +0100946{
947 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
948 struct armada_frame_work *work;
949 struct drm_device *dev = crtc->dev;
950 unsigned long flags;
951 unsigned i;
952 int ret;
953
954 /* We don't support changing the pixel format */
Matt Roperf4510a22014-04-01 15:22:40 -0700955 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Russell King96f60e32012-08-15 13:59:49 +0100956 return -EINVAL;
957
958 work = kmalloc(sizeof(*work), GFP_KERNEL);
959 if (!work)
960 return -ENOMEM;
961
962 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -0700963 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +0100964
965 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
966 dcrtc->interlaced);
967 armada_reg_queue_end(work->regs, i);
968
969 /*
Russell Kingc5488302014-10-11 23:53:35 +0100970 * Ensure that we hold a reference on the new framebuffer.
971 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +0100972 */
Russell Kingc5488302014-10-11 23:53:35 +0100973 drm_framebuffer_reference(fb);
Russell King96f60e32012-08-15 13:59:49 +0100974
975 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
976 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +0100977 /* Undo our reference above */
978 drm_framebuffer_unreference(fb);
Russell King96f60e32012-08-15 13:59:49 +0100979 kfree(work);
980 return ret;
981 }
982
983 /*
984 * Don't take a reference on the new framebuffer;
985 * drm_mode_page_flip_ioctl() has already grabbed a reference and
986 * will _not_ drop that reference on successful return from this
987 * function. Simply mark this new framebuffer as the current one.
988 */
Matt Roperf4510a22014-04-01 15:22:40 -0700989 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +0100990
991 /*
992 * Finally, if the display is blanked, we won't receive an
993 * interrupt, so complete it now.
994 */
995 if (dpms_blanked(dcrtc->dpms)) {
996 spin_lock_irqsave(&dev->event_lock, flags);
997 if (dcrtc->frame_work)
998 armada_drm_crtc_complete_frame_work(dcrtc);
999 spin_unlock_irqrestore(&dev->event_lock, flags);
1000 }
1001
1002 return 0;
1003}
1004
1005static int
1006armada_drm_crtc_set_property(struct drm_crtc *crtc,
1007 struct drm_property *property, uint64_t val)
1008{
1009 struct armada_private *priv = crtc->dev->dev_private;
1010 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1011 bool update_csc = false;
1012
1013 if (property == priv->csc_yuv_prop) {
1014 dcrtc->csc_yuv_mode = val;
1015 update_csc = true;
1016 } else if (property == priv->csc_rgb_prop) {
1017 dcrtc->csc_rgb_mode = val;
1018 update_csc = true;
1019 }
1020
1021 if (update_csc) {
1022 uint32_t val;
1023
1024 val = dcrtc->spu_iopad_ctrl |
1025 armada_drm_crtc_calculate_csc(dcrtc);
1026 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1027 }
1028
1029 return 0;
1030}
1031
1032static struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001033 .cursor_set = armada_drm_crtc_cursor_set,
1034 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001035 .destroy = armada_drm_crtc_destroy,
1036 .set_config = drm_crtc_helper_set_config,
1037 .page_flip = armada_drm_crtc_page_flip,
1038 .set_property = armada_drm_crtc_set_property,
1039};
1040
1041static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1042 { CSC_AUTO, "Auto" },
1043 { CSC_YUV_CCIR601, "CCIR601" },
1044 { CSC_YUV_CCIR709, "CCIR709" },
1045};
1046
1047static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1048 { CSC_AUTO, "Auto" },
1049 { CSC_RGB_COMPUTER, "Computer system" },
1050 { CSC_RGB_STUDIO, "Studio" },
1051};
1052
1053static int armada_drm_crtc_create_properties(struct drm_device *dev)
1054{
1055 struct armada_private *priv = dev->dev_private;
1056
1057 if (priv->csc_yuv_prop)
1058 return 0;
1059
1060 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1061 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1062 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1063 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1064 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1065 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1066
1067 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1068 return -ENOMEM;
1069
1070 return 0;
1071}
1072
Russell King0fb29702015-06-06 21:46:53 +01001073static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001074 struct resource *res, int irq, const struct armada_variant *variant,
1075 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001076{
Russell Kingd8c96082014-04-22 11:10:15 +01001077 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001078 struct armada_crtc *dcrtc;
1079 void __iomem *base;
1080 int ret;
1081
Russell Kingd8c96082014-04-22 11:10:15 +01001082 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001083 if (ret)
1084 return ret;
1085
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001086 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001087 if (IS_ERR(base))
1088 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001089
1090 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1091 if (!dcrtc) {
1092 DRM_ERROR("failed to allocate Armada crtc\n");
1093 return -ENOMEM;
1094 }
1095
Russell Kingd8c96082014-04-22 11:10:15 +01001096 if (dev != drm->dev)
1097 dev_set_drvdata(dev, dcrtc);
1098
Russell King42e62ba2014-04-22 15:24:03 +01001099 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001100 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001101 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001102 dcrtc->clk = ERR_PTR(-EINVAL);
1103 dcrtc->csc_yuv_mode = CSC_AUTO;
1104 dcrtc->csc_rgb_mode = CSC_AUTO;
1105 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1106 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1107 spin_lock_init(&dcrtc->irq_lock);
1108 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1109 INIT_LIST_HEAD(&dcrtc->vbl_list);
1110 init_waitqueue_head(&dcrtc->frame_wait);
1111
1112 /* Initialize some registers which we don't otherwise set */
1113 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1114 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1115 writel_relaxed(dcrtc->spu_iopad_ctrl,
1116 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1117 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1118 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1119 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1120 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1121 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1122 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001123 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1124 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001125
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001126 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1127 dcrtc);
1128 if (ret < 0) {
1129 kfree(dcrtc);
1130 return ret;
1131 }
Russell King96f60e32012-08-15 13:59:49 +01001132
Russell King42e62ba2014-04-22 15:24:03 +01001133 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001134 ret = dcrtc->variant->init(dcrtc, dev);
Russell King96f60e32012-08-15 13:59:49 +01001135 if (ret) {
1136 kfree(dcrtc);
1137 return ret;
1138 }
1139 }
1140
1141 /* Ensure AXI pipeline is enabled */
1142 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1143
1144 priv->dcrtc[dcrtc->num] = dcrtc;
1145
Russell King9611cb92014-06-15 11:21:23 +01001146 dcrtc->crtc.port = port;
Russell Kingd8c96082014-04-22 11:10:15 +01001147 drm_crtc_init(drm, &dcrtc->crtc, &armada_crtc_funcs);
Russell King96f60e32012-08-15 13:59:49 +01001148 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1149
1150 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1151 dcrtc->csc_yuv_mode);
1152 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1153 dcrtc->csc_rgb_mode);
1154
Russell Kingd8c96082014-04-22 11:10:15 +01001155 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King96f60e32012-08-15 13:59:49 +01001156}
Russell Kingd8c96082014-04-22 11:10:15 +01001157
1158static int
1159armada_lcd_bind(struct device *dev, struct device *master, void *data)
1160{
1161 struct platform_device *pdev = to_platform_device(dev);
1162 struct drm_device *drm = data;
1163 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1164 int irq = platform_get_irq(pdev, 0);
1165 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001166 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001167
1168 if (irq < 0)
1169 return irq;
1170
1171 if (!dev->of_node) {
1172 const struct platform_device_id *id;
1173
1174 id = platform_get_device_id(pdev);
1175 if (!id)
1176 return -ENXIO;
1177
1178 variant = (const struct armada_variant *)id->driver_data;
1179 } else {
1180 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001181 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001182
1183 match = of_match_device(dev->driver->of_match_table, dev);
1184 if (!match)
1185 return -ENXIO;
1186
Russell King9611cb92014-06-15 11:21:23 +01001187 np = of_get_child_by_name(parent, "ports");
1188 if (np)
1189 parent = np;
1190 port = of_get_child_by_name(parent, "port");
1191 of_node_put(np);
1192 if (!port) {
1193 dev_err(dev, "no port node found in %s\n",
1194 parent->full_name);
1195 return -ENXIO;
1196 }
1197
Russell Kingd8c96082014-04-22 11:10:15 +01001198 variant = match->data;
1199 }
1200
Russell King9611cb92014-06-15 11:21:23 +01001201 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001202}
1203
1204static void
1205armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1206{
1207 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1208
1209 armada_drm_crtc_destroy(&dcrtc->crtc);
1210}
1211
1212static const struct component_ops armada_lcd_ops = {
1213 .bind = armada_lcd_bind,
1214 .unbind = armada_lcd_unbind,
1215};
1216
1217static int armada_lcd_probe(struct platform_device *pdev)
1218{
1219 return component_add(&pdev->dev, &armada_lcd_ops);
1220}
1221
1222static int armada_lcd_remove(struct platform_device *pdev)
1223{
1224 component_del(&pdev->dev, &armada_lcd_ops);
1225 return 0;
1226}
1227
1228static struct of_device_id armada_lcd_of_match[] = {
1229 {
1230 .compatible = "marvell,dove-lcd",
1231 .data = &armada510_ops,
1232 },
1233 {}
1234};
1235MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1236
1237static const struct platform_device_id armada_lcd_platform_ids[] = {
1238 {
1239 .name = "armada-lcd",
1240 .driver_data = (unsigned long)&armada510_ops,
1241 }, {
1242 .name = "armada-510-lcd",
1243 .driver_data = (unsigned long)&armada510_ops,
1244 },
1245 { },
1246};
1247MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1248
1249struct platform_driver armada_lcd_platform_driver = {
1250 .probe = armada_lcd_probe,
1251 .remove = armada_lcd_remove,
1252 .driver = {
1253 .name = "armada-lcd",
1254 .owner = THIS_MODULE,
1255 .of_match_table = armada_lcd_of_match,
1256 },
1257 .id_table = armada_lcd_platform_ids,
1258};