blob: 5d627646601e4d34d7f6e61043cc9dcb4ec87503 [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;
Russell King96f60e32012-08-15 13:59:49 +0100218 int ret;
219
220 ret = drm_vblank_get(dev, dcrtc->num);
221 if (ret) {
222 DRM_ERROR("failed to acquire vblank counter\n");
223 return ret;
224 }
225
Russell King709ffd82015-07-15 18:09:38 +0100226 if (cmpxchg(&dcrtc->frame_work, NULL, work)) {
Russell King96f60e32012-08-15 13:59:49 +0100227 drm_vblank_put(dev, dcrtc->num);
Russell King709ffd82015-07-15 18:09:38 +0100228 ret = -EBUSY;
229 }
Russell King96f60e32012-08-15 13:59:49 +0100230
231 return ret;
232}
233
Russell King709ffd82015-07-15 18:09:38 +0100234static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
235 struct armada_frame_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100236{
237 struct drm_device *dev = dcrtc->crtc.dev;
Russell King709ffd82015-07-15 18:09:38 +0100238 unsigned long flags;
Russell King96f60e32012-08-15 13:59:49 +0100239
Russell King709ffd82015-07-15 18:09:38 +0100240 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100241 armada_drm_crtc_update_regs(dcrtc, work->regs);
Russell King709ffd82015-07-15 18:09:38 +0100242 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100243
Russell King709ffd82015-07-15 18:09:38 +0100244 if (work->event) {
245 spin_lock_irqsave(&dev->event_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100246 drm_send_vblank_event(dev, dcrtc->num, work->event);
Russell King709ffd82015-07-15 18:09:38 +0100247 spin_unlock_irqrestore(&dev->event_lock, flags);
248 }
Russell King96f60e32012-08-15 13:59:49 +0100249
250 drm_vblank_put(dev, dcrtc->num);
251
252 /* Finally, queue the process-half of the cleanup. */
253 __armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb);
254 kfree(work);
255}
256
257static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
258 struct drm_framebuffer *fb, bool force)
259{
260 struct armada_frame_work *work;
261
262 if (!fb)
263 return;
264
265 if (force) {
266 /* Display is disabled, so just drop the old fb */
267 drm_framebuffer_unreference(fb);
268 return;
269 }
270
271 work = kmalloc(sizeof(*work), GFP_KERNEL);
272 if (work) {
273 int i = 0;
274 work->event = NULL;
275 work->old_fb = fb;
276 armada_reg_queue_end(work->regs, i);
277
278 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
279 return;
280
281 kfree(work);
282 }
283
284 /*
285 * Oops - just drop the reference immediately and hope for
286 * the best. The worst that will happen is the buffer gets
287 * reused before it has finished being displayed.
288 */
289 drm_framebuffer_unreference(fb);
290}
291
292static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
293{
Russell King709ffd82015-07-15 18:09:38 +0100294 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +0100295
296 /*
297 * Tell the DRM core that vblank IRQs aren't going to happen for
298 * a while. This cleans up any pending vblank events for us.
299 */
Russell King178e5612014-10-11 23:57:04 +0100300 drm_crtc_vblank_off(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100301
302 /* Handle any pending flip event. */
Russell King709ffd82015-07-15 18:09:38 +0100303 work = xchg(&dcrtc->frame_work, NULL);
304 if (work)
305 armada_drm_crtc_complete_frame_work(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +0100306}
307
308void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
309 int idx)
310{
311}
312
313void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
314 int idx)
315{
316}
317
318/* The mode_config.mutex will be held for this call */
319static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
320{
321 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
322
323 if (dcrtc->dpms != dpms) {
324 dcrtc->dpms = dpms;
Russell Kinge0ac5e92015-06-29 18:01:38 +0100325 if (!IS_ERR(dcrtc->clk) && !dpms_blanked(dpms))
326 WARN_ON(clk_prepare_enable(dcrtc->clk));
Russell King96f60e32012-08-15 13:59:49 +0100327 armada_drm_crtc_update(dcrtc);
Russell Kinge0ac5e92015-06-29 18:01:38 +0100328 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dpms))
329 clk_disable_unprepare(dcrtc->clk);
Russell King96f60e32012-08-15 13:59:49 +0100330 if (dpms_blanked(dpms))
331 armada_drm_vblank_off(dcrtc);
Russell King178e5612014-10-11 23:57:04 +0100332 else
333 drm_crtc_vblank_on(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100334 }
335}
336
337/*
338 * Prepare for a mode set. Turn off overlay to ensure that we don't end
339 * up with the overlay size being bigger than the active screen size.
340 * We rely upon X refreshing this state after the mode set has completed.
341 *
342 * The mode_config.mutex will be held for this call
343 */
344static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
345{
346 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
347 struct drm_plane *plane;
348
349 /*
350 * If we have an overlay plane associated with this CRTC, disable
351 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100352 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100353 */
354 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100355 if (plane)
356 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100357}
358
359/* The mode_config.mutex will be held for this call */
360static void armada_drm_crtc_commit(struct drm_crtc *crtc)
361{
362 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
363
364 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
365 dcrtc->dpms = DRM_MODE_DPMS_ON;
366 armada_drm_crtc_update(dcrtc);
367 }
368}
369
370/* The mode_config.mutex will be held for this call */
371static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
372 const struct drm_display_mode *mode, struct drm_display_mode *adj)
373{
Russell King96f60e32012-08-15 13:59:49 +0100374 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
375 int ret;
376
377 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100378 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100379 adj->flags & DRM_MODE_FLAG_INTERLACE)
380 return false;
381
382 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100383 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100384 if (ret)
385 return false;
386
387 return true;
388}
389
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100390static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100391{
Russell King96f60e32012-08-15 13:59:49 +0100392 void __iomem *base = dcrtc->base;
393
394 if (stat & DMA_FF_UNDERFLOW)
395 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
396 if (stat & GRA_FF_UNDERFLOW)
397 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
398
399 if (stat & VSYNC_IRQ)
400 drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
401
402 spin_lock(&dcrtc->irq_lock);
Russell King7c8f7e12015-06-29 17:52:16 +0100403 armada_drm_vbl_event_run(dcrtc);
Russell King96f60e32012-08-15 13:59:49 +0100404
405 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
406 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
407 uint32_t val;
408
409 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
410 writel_relaxed(dcrtc->v[i].spu_v_h_total,
411 base + LCD_SPUT_V_H_TOTAL);
412
413 val = readl_relaxed(base + LCD_SPU_ADV_REG);
414 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
415 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100416 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100417 }
Russell King662af0d2013-05-19 10:55:17 +0100418
419 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
420 writel_relaxed(dcrtc->cursor_hw_pos,
421 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
422 writel_relaxed(dcrtc->cursor_hw_sz,
423 base + LCD_SPU_HWC_HPXL_VLN);
424 armada_updatel(CFG_HWC_ENA,
425 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
426 base + LCD_SPU_DMA_CTRL0);
427 dcrtc->cursor_update = false;
428 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
429 }
430
Russell King96f60e32012-08-15 13:59:49 +0100431 spin_unlock(&dcrtc->irq_lock);
432
433 if (stat & GRA_FRAME_IRQ) {
Russell King709ffd82015-07-15 18:09:38 +0100434 struct armada_frame_work *work = xchg(&dcrtc->frame_work, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100435
Russell King709ffd82015-07-15 18:09:38 +0100436 if (work)
437 armada_drm_crtc_complete_frame_work(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +0100438
439 wake_up(&dcrtc->frame_wait);
440 }
441}
442
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100443static irqreturn_t armada_drm_irq(int irq, void *arg)
444{
445 struct armada_crtc *dcrtc = arg;
446 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
447
448 /*
449 * This is rediculous - rather than writing bits to clear, we
450 * have to set the actual status register value. This is racy.
451 */
452 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
453
454 /* Mask out those interrupts we haven't enabled */
455 v = stat & dcrtc->irq_ena;
456
457 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
458 armada_drm_crtc_irq(dcrtc, stat);
459 return IRQ_HANDLED;
460 }
461 return IRQ_NONE;
462}
463
Russell King96f60e32012-08-15 13:59:49 +0100464/* These are locked by dev->vbl_lock */
465void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
466{
467 if (dcrtc->irq_ena & mask) {
468 dcrtc->irq_ena &= ~mask;
469 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
470 }
471}
472
473void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
474{
475 if ((dcrtc->irq_ena & mask) != mask) {
476 dcrtc->irq_ena |= mask;
477 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
478 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
479 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
480 }
481}
482
483static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
484{
485 struct drm_display_mode *adj = &dcrtc->crtc.mode;
486 uint32_t val = 0;
487
488 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
489 val |= CFG_CSC_YUV_CCIR709;
490 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
491 val |= CFG_CSC_RGB_STUDIO;
492
493 /*
494 * In auto mode, set the colorimetry, based upon the HDMI spec.
495 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
496 * ITU601. It may be more appropriate to set this depending on
497 * the source - but what if the graphic frame is YUV and the
498 * video frame is RGB?
499 */
500 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
501 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
502 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
503 if (dcrtc->csc_yuv_mode == CSC_AUTO)
504 val |= CFG_CSC_YUV_CCIR709;
505 }
506
507 /*
508 * We assume we're connected to a TV-like device, so the YUV->RGB
509 * conversion should produce a limited range. We should set this
510 * depending on the connectors attached to this CRTC, and what
511 * kind of device they report being connected.
512 */
513 if (dcrtc->csc_rgb_mode == CSC_AUTO)
514 val |= CFG_CSC_RGB_STUDIO;
515
516 return val;
517}
518
519/* The mode_config.mutex will be held for this call */
520static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
521 struct drm_display_mode *mode, struct drm_display_mode *adj,
522 int x, int y, struct drm_framebuffer *old_fb)
523{
Russell King96f60e32012-08-15 13:59:49 +0100524 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
525 struct armada_regs regs[17];
526 uint32_t lm, rm, tm, bm, val, sclk;
527 unsigned long flags;
528 unsigned i;
529 bool interlaced;
530
Matt Roperf4510a22014-04-01 15:22:40 -0700531 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100532
533 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
534
Matt Roperf4510a22014-04-01 15:22:40 -0700535 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
536 x, y, regs, interlaced);
Russell King96f60e32012-08-15 13:59:49 +0100537
538 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
539 lm = adj->crtc_htotal - adj->crtc_hsync_end;
540 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
541 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
542
543 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
544 adj->crtc_hdisplay,
545 adj->crtc_hsync_start,
546 adj->crtc_hsync_end,
547 adj->crtc_htotal, lm, rm);
548 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
549 adj->crtc_vdisplay,
550 adj->crtc_vsync_start,
551 adj->crtc_vsync_end,
552 adj->crtc_vtotal, tm, bm);
553
554 /* Wait for pending flips to complete */
555 wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
556
Russell King178e5612014-10-11 23:57:04 +0100557 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100558
Russell King96f60e32012-08-15 13:59:49 +0100559 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
560 if (val != dcrtc->dumb_ctrl) {
561 dcrtc->dumb_ctrl = val;
562 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
563 }
564
Russell Kinge0ac5e92015-06-29 18:01:38 +0100565 /*
566 * If we are blanked, we would have disabled the clock. Re-enable
567 * it so that compute_clock() does the right thing.
568 */
569 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
570 WARN_ON(clk_prepare_enable(dcrtc->clk));
571
Russell King96f60e32012-08-15 13:59:49 +0100572 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100573 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100574
575 /* Ensure graphic fifo is enabled */
576 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
577 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
578
579 if (interlaced ^ dcrtc->interlaced) {
580 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
581 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
582 else
583 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
584 dcrtc->interlaced = interlaced;
585 }
586
587 spin_lock_irqsave(&dcrtc->irq_lock, flags);
588
589 /* Even interlaced/progressive frame */
590 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
591 adj->crtc_htotal;
592 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
593 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100594 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100595 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100596
597 if (interlaced) {
598 /* Odd interlaced frame */
599 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
600 (1 << 16);
601 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
602 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100603 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100604 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100605 } else {
606 dcrtc->v[0] = dcrtc->v[1];
607 }
608
609 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
610
611 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
612 armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
613 armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
614 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
615 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
616 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
617 LCD_SPUT_V_H_TOTAL);
618
Russell King42e62ba2014-04-22 15:24:03 +0100619 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100620 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
621 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
622 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100623 }
Russell King96f60e32012-08-15 13:59:49 +0100624
625 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
Matt Roperf4510a22014-04-01 15:22:40 -0700626 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
627 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100628
Matt Roperf4510a22014-04-01 15:22:40 -0700629 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
Russell King96f60e32012-08-15 13:59:49 +0100630 val |= CFG_PALETTE_ENA;
631
632 if (interlaced)
633 val |= CFG_GRA_FTOGGLE;
634
635 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
636 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
637 CFG_SWAPYU | CFG_YUV2RGB) |
638 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
639 LCD_SPU_DMA_CTRL0);
640
641 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
642 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
643
644 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
645 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
646 armada_reg_queue_end(regs, i);
647
648 armada_drm_crtc_update_regs(dcrtc, regs);
649 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
650
651 armada_drm_crtc_update(dcrtc);
652
Russell King178e5612014-10-11 23:57:04 +0100653 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100654 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
655
656 return 0;
657}
658
659/* The mode_config.mutex will be held for this call */
660static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
661 struct drm_framebuffer *old_fb)
662{
663 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
664 struct armada_regs regs[4];
665 unsigned i;
666
Matt Roperf4510a22014-04-01 15:22:40 -0700667 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100668 dcrtc->interlaced);
669 armada_reg_queue_end(regs, i);
670
671 /* Wait for pending flips to complete */
672 wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
673
674 /* Take a reference to the new fb as we're using it */
Matt Roperf4510a22014-04-01 15:22:40 -0700675 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100676
677 /* Update the base in the CRTC */
678 armada_drm_crtc_update_regs(dcrtc, regs);
679
680 /* Drop our previously held reference */
681 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
682
683 return 0;
684}
685
Russell King96f60e32012-08-15 13:59:49 +0100686/* The mode_config.mutex will be held for this call */
687static void armada_drm_crtc_disable(struct drm_crtc *crtc)
688{
689 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
690
691 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -0700692 armada_drm_crtc_finish_fb(dcrtc, crtc->primary->fb, true);
Russell King96f60e32012-08-15 13:59:49 +0100693
694 /* Power down most RAMs and FIFOs */
695 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
696 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
697 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
698}
699
700static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
701 .dpms = armada_drm_crtc_dpms,
702 .prepare = armada_drm_crtc_prepare,
703 .commit = armada_drm_crtc_commit,
704 .mode_fixup = armada_drm_crtc_mode_fixup,
705 .mode_set = armada_drm_crtc_mode_set,
706 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100707 .disable = armada_drm_crtc_disable,
708};
709
Russell King662af0d2013-05-19 10:55:17 +0100710static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
711 unsigned stride, unsigned width, unsigned height)
712{
713 uint32_t addr;
714 unsigned y;
715
716 addr = SRAM_HWC32_RAM1;
717 for (y = 0; y < height; y++) {
718 uint32_t *p = &pix[y * stride];
719 unsigned x;
720
721 for (x = 0; x < width; x++, p++) {
722 uint32_t val = *p;
723
724 val = (val & 0xff00ff00) |
725 (val & 0x000000ff) << 16 |
726 (val & 0x00ff0000) >> 16;
727
728 writel_relaxed(val,
729 base + LCD_SPU_SRAM_WRDAT);
730 writel_relaxed(addr | SRAM_WRITE,
731 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100732 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100733 addr += 1;
734 if ((addr & 0x00ff) == 0)
735 addr += 0xf00;
736 if ((addr & 0x30ff) == 0)
737 addr = SRAM_HWC32_RAM2;
738 }
739 }
740}
741
742static void armada_drm_crtc_cursor_tran(void __iomem *base)
743{
744 unsigned addr;
745
746 for (addr = 0; addr < 256; addr++) {
747 /* write the default value */
748 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
749 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
750 base + LCD_SPU_SRAM_CTRL);
751 }
752}
753
754static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
755{
756 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
757 uint32_t yoff, yscr, h = dcrtc->cursor_h;
758 uint32_t para1;
759
760 /*
761 * Calculate the visible width and height of the cursor,
762 * screen position, and the position in the cursor bitmap.
763 */
764 if (dcrtc->cursor_x < 0) {
765 xoff = -dcrtc->cursor_x;
766 xscr = 0;
767 w -= min(xoff, w);
768 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
769 xoff = 0;
770 xscr = dcrtc->cursor_x;
771 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
772 } else {
773 xoff = 0;
774 xscr = dcrtc->cursor_x;
775 }
776
777 if (dcrtc->cursor_y < 0) {
778 yoff = -dcrtc->cursor_y;
779 yscr = 0;
780 h -= min(yoff, h);
781 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
782 yoff = 0;
783 yscr = dcrtc->cursor_y;
784 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
785 } else {
786 yoff = 0;
787 yscr = dcrtc->cursor_y;
788 }
789
790 /* On interlaced modes, the vertical cursor size must be halved */
791 s = dcrtc->cursor_w;
792 if (dcrtc->interlaced) {
793 s *= 2;
794 yscr /= 2;
795 h /= 2;
796 }
797
798 if (!dcrtc->cursor_obj || !h || !w) {
799 spin_lock_irq(&dcrtc->irq_lock);
800 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
801 dcrtc->cursor_update = false;
802 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
803 spin_unlock_irq(&dcrtc->irq_lock);
804 return 0;
805 }
806
807 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
808 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
809 dcrtc->base + LCD_SPU_SRAM_PARA1);
810
811 /*
812 * Initialize the transparency if the SRAM was powered down.
813 * We must also reload the cursor data as well.
814 */
815 if (!(para1 & CFG_CSB_256x32)) {
816 armada_drm_crtc_cursor_tran(dcrtc->base);
817 reload = true;
818 }
819
820 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
821 spin_lock_irq(&dcrtc->irq_lock);
822 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
823 dcrtc->cursor_update = false;
824 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
825 spin_unlock_irq(&dcrtc->irq_lock);
826 reload = true;
827 }
828 if (reload) {
829 struct armada_gem_object *obj = dcrtc->cursor_obj;
830 uint32_t *pix;
831 /* Set the top-left corner of the cursor image */
832 pix = obj->addr;
833 pix += yoff * s + xoff;
834 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
835 }
836
837 /* Reload the cursor position, size and enable in the IRQ handler */
838 spin_lock_irq(&dcrtc->irq_lock);
839 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
840 dcrtc->cursor_hw_sz = h << 16 | w;
841 dcrtc->cursor_update = true;
842 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
843 spin_unlock_irq(&dcrtc->irq_lock);
844
845 return 0;
846}
847
848static void cursor_update(void *data)
849{
850 armada_drm_crtc_cursor_update(data, true);
851}
852
853static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
854 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
855{
856 struct drm_device *dev = crtc->dev;
857 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100858 struct armada_gem_object *obj = NULL;
859 int ret;
860
861 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100862 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100863 return -ENXIO;
864
865 if (handle && w > 0 && h > 0) {
866 /* maximum size is 64x32 or 32x64 */
867 if (w > 64 || h > 64 || (w > 32 && h > 32))
868 return -ENOMEM;
869
870 obj = armada_gem_object_lookup(dev, file, handle);
871 if (!obj)
872 return -ENOENT;
873
874 /* Must be a kernel-mapped object */
875 if (!obj->addr) {
876 drm_gem_object_unreference_unlocked(&obj->obj);
877 return -EINVAL;
878 }
879
880 if (obj->obj.size < w * h * 4) {
881 DRM_ERROR("buffer is too small\n");
882 drm_gem_object_unreference_unlocked(&obj->obj);
883 return -ENOMEM;
884 }
885 }
886
887 mutex_lock(&dev->struct_mutex);
888 if (dcrtc->cursor_obj) {
889 dcrtc->cursor_obj->update = NULL;
890 dcrtc->cursor_obj->update_data = NULL;
891 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
892 }
893 dcrtc->cursor_obj = obj;
894 dcrtc->cursor_w = w;
895 dcrtc->cursor_h = h;
896 ret = armada_drm_crtc_cursor_update(dcrtc, true);
897 if (obj) {
898 obj->update_data = dcrtc;
899 obj->update = cursor_update;
900 }
901 mutex_unlock(&dev->struct_mutex);
902
903 return ret;
904}
905
906static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
907{
908 struct drm_device *dev = crtc->dev;
909 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100910 int ret;
911
912 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100913 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100914 return -EFAULT;
915
916 mutex_lock(&dev->struct_mutex);
917 dcrtc->cursor_x = x;
918 dcrtc->cursor_y = y;
919 ret = armada_drm_crtc_cursor_update(dcrtc, false);
920 mutex_unlock(&dev->struct_mutex);
921
922 return ret;
923}
924
Russell King96f60e32012-08-15 13:59:49 +0100925static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
926{
927 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
928 struct armada_private *priv = crtc->dev->dev_private;
929
Russell King662af0d2013-05-19 10:55:17 +0100930 if (dcrtc->cursor_obj)
931 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
932
Russell King96f60e32012-08-15 13:59:49 +0100933 priv->dcrtc[dcrtc->num] = NULL;
934 drm_crtc_cleanup(&dcrtc->crtc);
935
936 if (!IS_ERR(dcrtc->clk))
937 clk_disable_unprepare(dcrtc->clk);
938
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100939 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
940
Russell King9611cb92014-06-15 11:21:23 +0100941 of_node_put(dcrtc->crtc.port);
942
Russell King96f60e32012-08-15 13:59:49 +0100943 kfree(dcrtc);
944}
945
946/*
947 * The mode_config lock is held here, to prevent races between this
948 * and a mode_set.
949 */
950static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Dave Airlie5e4e3ba2013-10-22 09:38:18 +0100951 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Russell King96f60e32012-08-15 13:59:49 +0100952{
953 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
954 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +0100955 unsigned i;
956 int ret;
957
958 /* We don't support changing the pixel format */
Matt Roperf4510a22014-04-01 15:22:40 -0700959 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Russell King96f60e32012-08-15 13:59:49 +0100960 return -EINVAL;
961
962 work = kmalloc(sizeof(*work), GFP_KERNEL);
963 if (!work)
964 return -ENOMEM;
965
966 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -0700967 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +0100968
969 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
970 dcrtc->interlaced);
971 armada_reg_queue_end(work->regs, i);
972
973 /*
Russell Kingc5488302014-10-11 23:53:35 +0100974 * Ensure that we hold a reference on the new framebuffer.
975 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +0100976 */
Russell Kingc5488302014-10-11 23:53:35 +0100977 drm_framebuffer_reference(fb);
Russell King96f60e32012-08-15 13:59:49 +0100978
979 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
980 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +0100981 /* Undo our reference above */
982 drm_framebuffer_unreference(fb);
Russell King96f60e32012-08-15 13:59:49 +0100983 kfree(work);
984 return ret;
985 }
986
987 /*
988 * Don't take a reference on the new framebuffer;
989 * drm_mode_page_flip_ioctl() has already grabbed a reference and
990 * will _not_ drop that reference on successful return from this
991 * function. Simply mark this new framebuffer as the current one.
992 */
Matt Roperf4510a22014-04-01 15:22:40 -0700993 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +0100994
995 /*
996 * Finally, if the display is blanked, we won't receive an
997 * interrupt, so complete it now.
998 */
999 if (dpms_blanked(dcrtc->dpms)) {
Russell King709ffd82015-07-15 18:09:38 +01001000 struct armada_frame_work *work = xchg(&dcrtc->frame_work, NULL);
1001
1002 if (work)
1003 armada_drm_crtc_complete_frame_work(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +01001004 }
1005
1006 return 0;
1007}
1008
1009static int
1010armada_drm_crtc_set_property(struct drm_crtc *crtc,
1011 struct drm_property *property, uint64_t val)
1012{
1013 struct armada_private *priv = crtc->dev->dev_private;
1014 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1015 bool update_csc = false;
1016
1017 if (property == priv->csc_yuv_prop) {
1018 dcrtc->csc_yuv_mode = val;
1019 update_csc = true;
1020 } else if (property == priv->csc_rgb_prop) {
1021 dcrtc->csc_rgb_mode = val;
1022 update_csc = true;
1023 }
1024
1025 if (update_csc) {
1026 uint32_t val;
1027
1028 val = dcrtc->spu_iopad_ctrl |
1029 armada_drm_crtc_calculate_csc(dcrtc);
1030 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1031 }
1032
1033 return 0;
1034}
1035
1036static struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001037 .cursor_set = armada_drm_crtc_cursor_set,
1038 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001039 .destroy = armada_drm_crtc_destroy,
1040 .set_config = drm_crtc_helper_set_config,
1041 .page_flip = armada_drm_crtc_page_flip,
1042 .set_property = armada_drm_crtc_set_property,
1043};
1044
1045static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1046 { CSC_AUTO, "Auto" },
1047 { CSC_YUV_CCIR601, "CCIR601" },
1048 { CSC_YUV_CCIR709, "CCIR709" },
1049};
1050
1051static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1052 { CSC_AUTO, "Auto" },
1053 { CSC_RGB_COMPUTER, "Computer system" },
1054 { CSC_RGB_STUDIO, "Studio" },
1055};
1056
1057static int armada_drm_crtc_create_properties(struct drm_device *dev)
1058{
1059 struct armada_private *priv = dev->dev_private;
1060
1061 if (priv->csc_yuv_prop)
1062 return 0;
1063
1064 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1065 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1066 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1067 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1068 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1069 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1070
1071 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1072 return -ENOMEM;
1073
1074 return 0;
1075}
1076
Russell King0fb29702015-06-06 21:46:53 +01001077static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001078 struct resource *res, int irq, const struct armada_variant *variant,
1079 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001080{
Russell Kingd8c96082014-04-22 11:10:15 +01001081 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001082 struct armada_crtc *dcrtc;
1083 void __iomem *base;
1084 int ret;
1085
Russell Kingd8c96082014-04-22 11:10:15 +01001086 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001087 if (ret)
1088 return ret;
1089
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001090 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001091 if (IS_ERR(base))
1092 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001093
1094 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1095 if (!dcrtc) {
1096 DRM_ERROR("failed to allocate Armada crtc\n");
1097 return -ENOMEM;
1098 }
1099
Russell Kingd8c96082014-04-22 11:10:15 +01001100 if (dev != drm->dev)
1101 dev_set_drvdata(dev, dcrtc);
1102
Russell King42e62ba2014-04-22 15:24:03 +01001103 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001104 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001105 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001106 dcrtc->clk = ERR_PTR(-EINVAL);
1107 dcrtc->csc_yuv_mode = CSC_AUTO;
1108 dcrtc->csc_rgb_mode = CSC_AUTO;
1109 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1110 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1111 spin_lock_init(&dcrtc->irq_lock);
1112 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1113 INIT_LIST_HEAD(&dcrtc->vbl_list);
1114 init_waitqueue_head(&dcrtc->frame_wait);
1115
1116 /* Initialize some registers which we don't otherwise set */
1117 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1118 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1119 writel_relaxed(dcrtc->spu_iopad_ctrl,
1120 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1121 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1122 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1123 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1124 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1125 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1126 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001127 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1128 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001129
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001130 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1131 dcrtc);
1132 if (ret < 0) {
1133 kfree(dcrtc);
1134 return ret;
1135 }
Russell King96f60e32012-08-15 13:59:49 +01001136
Russell King42e62ba2014-04-22 15:24:03 +01001137 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001138 ret = dcrtc->variant->init(dcrtc, dev);
Russell King96f60e32012-08-15 13:59:49 +01001139 if (ret) {
1140 kfree(dcrtc);
1141 return ret;
1142 }
1143 }
1144
1145 /* Ensure AXI pipeline is enabled */
1146 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1147
1148 priv->dcrtc[dcrtc->num] = dcrtc;
1149
Russell King9611cb92014-06-15 11:21:23 +01001150 dcrtc->crtc.port = port;
Russell Kingd8c96082014-04-22 11:10:15 +01001151 drm_crtc_init(drm, &dcrtc->crtc, &armada_crtc_funcs);
Russell King96f60e32012-08-15 13:59:49 +01001152 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1153
1154 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1155 dcrtc->csc_yuv_mode);
1156 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1157 dcrtc->csc_rgb_mode);
1158
Russell Kingd8c96082014-04-22 11:10:15 +01001159 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King96f60e32012-08-15 13:59:49 +01001160}
Russell Kingd8c96082014-04-22 11:10:15 +01001161
1162static int
1163armada_lcd_bind(struct device *dev, struct device *master, void *data)
1164{
1165 struct platform_device *pdev = to_platform_device(dev);
1166 struct drm_device *drm = data;
1167 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1168 int irq = platform_get_irq(pdev, 0);
1169 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001170 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001171
1172 if (irq < 0)
1173 return irq;
1174
1175 if (!dev->of_node) {
1176 const struct platform_device_id *id;
1177
1178 id = platform_get_device_id(pdev);
1179 if (!id)
1180 return -ENXIO;
1181
1182 variant = (const struct armada_variant *)id->driver_data;
1183 } else {
1184 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001185 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001186
1187 match = of_match_device(dev->driver->of_match_table, dev);
1188 if (!match)
1189 return -ENXIO;
1190
Russell King9611cb92014-06-15 11:21:23 +01001191 np = of_get_child_by_name(parent, "ports");
1192 if (np)
1193 parent = np;
1194 port = of_get_child_by_name(parent, "port");
1195 of_node_put(np);
1196 if (!port) {
1197 dev_err(dev, "no port node found in %s\n",
1198 parent->full_name);
1199 return -ENXIO;
1200 }
1201
Russell Kingd8c96082014-04-22 11:10:15 +01001202 variant = match->data;
1203 }
1204
Russell King9611cb92014-06-15 11:21:23 +01001205 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001206}
1207
1208static void
1209armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1210{
1211 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1212
1213 armada_drm_crtc_destroy(&dcrtc->crtc);
1214}
1215
1216static const struct component_ops armada_lcd_ops = {
1217 .bind = armada_lcd_bind,
1218 .unbind = armada_lcd_unbind,
1219};
1220
1221static int armada_lcd_probe(struct platform_device *pdev)
1222{
1223 return component_add(&pdev->dev, &armada_lcd_ops);
1224}
1225
1226static int armada_lcd_remove(struct platform_device *pdev)
1227{
1228 component_del(&pdev->dev, &armada_lcd_ops);
1229 return 0;
1230}
1231
1232static struct of_device_id armada_lcd_of_match[] = {
1233 {
1234 .compatible = "marvell,dove-lcd",
1235 .data = &armada510_ops,
1236 },
1237 {}
1238};
1239MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1240
1241static const struct platform_device_id armada_lcd_platform_ids[] = {
1242 {
1243 .name = "armada-lcd",
1244 .driver_data = (unsigned long)&armada510_ops,
1245 }, {
1246 .name = "armada-510-lcd",
1247 .driver_data = (unsigned long)&armada510_ops,
1248 },
1249 { },
1250};
1251MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1252
1253struct platform_driver armada_lcd_platform_driver = {
1254 .probe = armada_lcd_probe,
1255 .remove = armada_lcd_remove,
1256 .driver = {
1257 .name = "armada-lcd",
1258 .owner = THIS_MODULE,
1259 .of_match_table = armada_lcd_of_match,
1260 },
1261 .id_table = armada_lcd_platform_ids,
1262};