blob: 6d3b0edde8d712fa3255f9ef2301a6c39d68098e [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"
Russell Kingc8a220c2016-05-17 13:51:08 +010021#include "armada_trace.h"
Russell King96f60e32012-08-15 13:59:49 +010022
23struct armada_frame_work {
Russell King4b5dda82015-08-06 16:37:18 +010024 struct armada_plane_work work;
Russell King96f60e32012-08-15 13:59:49 +010025 struct drm_pending_vblank_event *event;
26 struct armada_regs regs[4];
27 struct drm_framebuffer *old_fb;
28};
29
30enum csc_mode {
31 CSC_AUTO = 0,
32 CSC_YUV_CCIR601 = 1,
33 CSC_YUV_CCIR709 = 2,
34 CSC_RGB_COMPUTER = 1,
35 CSC_RGB_STUDIO = 2,
36};
37
Russell King1c914ce2015-07-15 18:11:24 +010038static const uint32_t armada_primary_formats[] = {
39 DRM_FORMAT_UYVY,
40 DRM_FORMAT_YUYV,
41 DRM_FORMAT_VYUY,
42 DRM_FORMAT_YVYU,
43 DRM_FORMAT_ARGB8888,
44 DRM_FORMAT_ABGR8888,
45 DRM_FORMAT_XRGB8888,
46 DRM_FORMAT_XBGR8888,
47 DRM_FORMAT_RGB888,
48 DRM_FORMAT_BGR888,
49 DRM_FORMAT_ARGB1555,
50 DRM_FORMAT_ABGR1555,
51 DRM_FORMAT_RGB565,
52 DRM_FORMAT_BGR565,
53};
54
Russell King96f60e32012-08-15 13:59:49 +010055/*
56 * A note about interlacing. Let's consider HDMI 1920x1080i.
57 * The timing parameters we have from X are:
58 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
59 * 1920 2448 2492 2640 1080 1084 1094 1125
60 * Which get translated to:
61 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
62 * 1920 2448 2492 2640 540 542 547 562
63 *
64 * This is how it is defined by CEA-861-D - line and pixel numbers are
65 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
66 * line: 2640. The odd frame, the first active line is at line 21, and
67 * the even frame, the first active line is 584.
68 *
69 * LN: 560 561 562 563 567 568 569
70 * DE: ~~~|____________________________//__________________________
71 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
72 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
73 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
74 *
75 * LN: 1123 1124 1125 1 5 6 7
76 * DE: ~~~|____________________________//__________________________
77 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
78 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
79 * 23 blanking lines
80 *
81 * The Armada LCD Controller line and pixel numbers are, like X timings,
82 * referenced to the top left of the active frame.
83 *
84 * So, translating these to our LCD controller:
85 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
86 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
87 * Note: Vsync front porch remains constant!
88 *
89 * if (odd_frame) {
90 * vtotal = mode->crtc_vtotal + 1;
91 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
92 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
93 * } else {
94 * vtotal = mode->crtc_vtotal;
95 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
96 * vhorizpos = mode->crtc_hsync_start;
97 * }
98 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
99 *
100 * So, we need to reprogram these registers on each vsync event:
101 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
102 *
103 * Note: we do not use the frame done interrupts because these appear
104 * to happen too early, and lead to jitter on the display (presumably
105 * they occur at the end of the last active line, before the vsync back
106 * porch, which we're reprogramming.)
107 */
108
109void
110armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
111{
112 while (regs->offset != ~0) {
113 void __iomem *reg = dcrtc->base + regs->offset;
114 uint32_t val;
115
116 val = regs->mask;
117 if (val != 0)
118 val &= readl_relaxed(reg);
119 writel_relaxed(val | regs->val, reg);
120 ++regs;
121 }
122}
123
124#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
125
126static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
127{
128 uint32_t dumb_ctrl;
129
130 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
131
132 if (!dpms_blanked(dcrtc->dpms))
133 dumb_ctrl |= CFG_DUMB_ENA;
134
135 /*
136 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
137 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
138 * force LCD_D[23:0] to output blank color, overriding the GPIO or
139 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
140 */
141 if (dpms_blanked(dcrtc->dpms) &&
142 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
143 dumb_ctrl &= ~DUMB_MASK;
144 dumb_ctrl |= DUMB_BLANK;
145 }
146
147 /*
148 * The documentation doesn't indicate what the normal state of
149 * the sync signals are. Sebastian Hesselbart kindly probed
150 * these signals on his board to determine their state.
151 *
152 * The non-inverted state of the sync signals is active high.
153 * Setting these bits makes the appropriate signal active low.
154 */
155 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
156 dumb_ctrl |= CFG_INV_CSYNC;
157 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
158 dumb_ctrl |= CFG_INV_HSYNC;
159 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
160 dumb_ctrl |= CFG_INV_VSYNC;
161
162 if (dcrtc->dumb_ctrl != dumb_ctrl) {
163 dcrtc->dumb_ctrl = dumb_ctrl;
164 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
165 }
166}
167
168static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
169 int x, int y, struct armada_regs *regs, bool interlaced)
170{
171 struct armada_gem_object *obj = drm_fb_obj(fb);
172 unsigned pitch = fb->pitches[0];
173 unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
174 uint32_t addr_odd, addr_even;
175 unsigned i = 0;
176
177 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
178 pitch, x, y, fb->bits_per_pixel);
179
180 addr_odd = addr_even = obj->dev_addr + offset;
181
182 if (interlaced) {
183 addr_even += pitch;
184 pitch *= 2;
185 }
186
187 /* write offset, base, and pitch */
188 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
189 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
190 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
191
192 return i;
193}
194
Russell King4b5dda82015-08-06 16:37:18 +0100195static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
Russell Kingec6fb152016-07-25 15:16:11 +0100196 struct drm_plane *plane)
Russell King4b5dda82015-08-06 16:37:18 +0100197{
Russell Kingec6fb152016-07-25 15:16:11 +0100198 struct armada_plane *dplane = drm_to_armada_plane(plane);
199 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King4b5dda82015-08-06 16:37:18 +0100200
201 /* Handle any pending frame work. */
202 if (work) {
Russell Kingec6fb152016-07-25 15:16:11 +0100203 work->fn(dcrtc, dplane, work);
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300204 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100205 }
Russell King7cb410c2015-08-07 13:34:26 +0100206
Russell Kingec6fb152016-07-25 15:16:11 +0100207 wake_up(&dplane->frame_wait);
Russell King4b5dda82015-08-06 16:37:18 +0100208}
209
210int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
211 struct armada_plane *plane, struct armada_plane_work *work)
212{
213 int ret;
214
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300215 ret = drm_crtc_vblank_get(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100216 if (ret) {
217 DRM_ERROR("failed to acquire vblank counter\n");
218 return ret;
219 }
220
221 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
222 if (ret)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300223 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100224
225 return ret;
226}
227
228int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
229{
230 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
231}
232
Russell King4a8506d2015-08-07 09:33:05 +0100233struct armada_plane_work *armada_drm_plane_work_cancel(
234 struct armada_crtc *dcrtc, struct armada_plane *plane)
Russell King7c8f7e12015-06-29 17:52:16 +0100235{
Russell King4a8506d2015-08-07 09:33:05 +0100236 struct armada_plane_work *work = xchg(&plane->work, NULL);
Russell King7c8f7e12015-06-29 17:52:16 +0100237
Russell King4a8506d2015-08-07 09:33:05 +0100238 if (work)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300239 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King7c8f7e12015-06-29 17:52:16 +0100240
Russell King4a8506d2015-08-07 09:33:05 +0100241 return work;
Russell King7c8f7e12015-06-29 17:52:16 +0100242}
243
Russell King96f60e32012-08-15 13:59:49 +0100244static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
245 struct armada_frame_work *work)
246{
Russell King4b5dda82015-08-06 16:37:18 +0100247 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100248
Russell King4b5dda82015-08-06 16:37:18 +0100249 return armada_drm_plane_work_queue(dcrtc, plane, &work->work);
Russell King96f60e32012-08-15 13:59:49 +0100250}
251
Russell King709ffd82015-07-15 18:09:38 +0100252static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
Russell King4b5dda82015-08-06 16:37:18 +0100253 struct armada_plane *plane, struct armada_plane_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100254{
Russell King4b5dda82015-08-06 16:37:18 +0100255 struct armada_frame_work *fwork = container_of(work, struct armada_frame_work, work);
Russell King96f60e32012-08-15 13:59:49 +0100256 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 King4b5dda82015-08-06 16:37:18 +0100260 armada_drm_crtc_update_regs(dcrtc, fwork->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 King4b5dda82015-08-06 16:37:18 +0100263 if (fwork->event) {
Russell King709ffd82015-07-15 18:09:38 +0100264 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovandd54b802016-06-06 11:41:33 -0300265 drm_crtc_send_vblank_event(&dcrtc->crtc, fwork->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
Russell King96f60e32012-08-15 13:59:49 +0100269 /* Finally, queue the process-half of the cleanup. */
Russell King4b5dda82015-08-06 16:37:18 +0100270 __armada_drm_queue_unref_work(dcrtc->crtc.dev, fwork->old_fb);
271 kfree(fwork);
Russell King96f60e32012-08-15 13:59:49 +0100272}
273
274static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
275 struct drm_framebuffer *fb, bool force)
276{
277 struct armada_frame_work *work;
278
279 if (!fb)
280 return;
281
282 if (force) {
283 /* Display is disabled, so just drop the old fb */
284 drm_framebuffer_unreference(fb);
285 return;
286 }
287
288 work = kmalloc(sizeof(*work), GFP_KERNEL);
289 if (work) {
290 int i = 0;
Russell King4b5dda82015-08-06 16:37:18 +0100291 work->work.fn = armada_drm_crtc_complete_frame_work;
Russell King96f60e32012-08-15 13:59:49 +0100292 work->event = NULL;
293 work->old_fb = fb;
294 armada_reg_queue_end(work->regs, i);
295
296 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
297 return;
298
299 kfree(work);
300 }
301
302 /*
303 * Oops - just drop the reference immediately and hope for
304 * the best. The worst that will happen is the buffer gets
305 * reused before it has finished being displayed.
306 */
307 drm_framebuffer_unreference(fb);
308}
309
310static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
311{
Russell King96f60e32012-08-15 13:59:49 +0100312 /*
313 * Tell the DRM core that vblank IRQs aren't going to happen for
314 * a while. This cleans up any pending vblank events for us.
315 */
Russell King178e5612014-10-11 23:57:04 +0100316 drm_crtc_vblank_off(&dcrtc->crtc);
Russell Kingec6fb152016-07-25 15:16:11 +0100317 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100318}
319
320void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
321 int idx)
322{
323}
324
325void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
326 int idx)
327{
328}
329
330/* The mode_config.mutex will be held for this call */
331static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
332{
333 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
334
335 if (dcrtc->dpms != dpms) {
336 dcrtc->dpms = dpms;
Russell Kinge0ac5e92015-06-29 18:01:38 +0100337 if (!IS_ERR(dcrtc->clk) && !dpms_blanked(dpms))
338 WARN_ON(clk_prepare_enable(dcrtc->clk));
Russell King96f60e32012-08-15 13:59:49 +0100339 armada_drm_crtc_update(dcrtc);
Russell Kinge0ac5e92015-06-29 18:01:38 +0100340 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dpms))
341 clk_disable_unprepare(dcrtc->clk);
Russell King96f60e32012-08-15 13:59:49 +0100342 if (dpms_blanked(dpms))
343 armada_drm_vblank_off(dcrtc);
Russell King178e5612014-10-11 23:57:04 +0100344 else
345 drm_crtc_vblank_on(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100346 }
347}
348
349/*
350 * Prepare for a mode set. Turn off overlay to ensure that we don't end
351 * up with the overlay size being bigger than the active screen size.
352 * We rely upon X refreshing this state after the mode set has completed.
353 *
354 * The mode_config.mutex will be held for this call
355 */
356static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
357{
358 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
359 struct drm_plane *plane;
360
361 /*
362 * If we have an overlay plane associated with this CRTC, disable
363 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100364 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100365 */
366 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100367 if (plane)
368 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100369}
370
371/* The mode_config.mutex will be held for this call */
372static void armada_drm_crtc_commit(struct drm_crtc *crtc)
373{
374 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
375
376 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
377 dcrtc->dpms = DRM_MODE_DPMS_ON;
378 armada_drm_crtc_update(dcrtc);
379 }
380}
381
382/* The mode_config.mutex will be held for this call */
383static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
384 const struct drm_display_mode *mode, struct drm_display_mode *adj)
385{
Russell King96f60e32012-08-15 13:59:49 +0100386 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
387 int ret;
388
389 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100390 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100391 adj->flags & DRM_MODE_FLAG_INTERLACE)
392 return false;
393
394 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100395 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100396 if (ret)
397 return false;
398
399 return true;
400}
401
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100402static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100403{
Russell King96f60e32012-08-15 13:59:49 +0100404 void __iomem *base = dcrtc->base;
Russell King4a8506d2015-08-07 09:33:05 +0100405 struct drm_plane *ovl_plane;
Russell King96f60e32012-08-15 13:59:49 +0100406
407 if (stat & DMA_FF_UNDERFLOW)
408 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
409 if (stat & GRA_FF_UNDERFLOW)
410 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
411
412 if (stat & VSYNC_IRQ)
Gustavo Padovan0ac28c52016-07-04 21:04:48 -0300413 drm_crtc_handle_vblank(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100414
415 spin_lock(&dcrtc->irq_lock);
Russell King4a8506d2015-08-07 09:33:05 +0100416 ovl_plane = dcrtc->plane;
Russell Kingec6fb152016-07-25 15:16:11 +0100417 if (ovl_plane)
418 armada_drm_plane_work_run(dcrtc, ovl_plane);
Russell King96f60e32012-08-15 13:59:49 +0100419
420 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
421 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
422 uint32_t val;
423
424 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
425 writel_relaxed(dcrtc->v[i].spu_v_h_total,
426 base + LCD_SPUT_V_H_TOTAL);
427
428 val = readl_relaxed(base + LCD_SPU_ADV_REG);
429 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
430 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100431 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100432 }
Russell King662af0d2013-05-19 10:55:17 +0100433
434 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
435 writel_relaxed(dcrtc->cursor_hw_pos,
436 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
437 writel_relaxed(dcrtc->cursor_hw_sz,
438 base + LCD_SPU_HWC_HPXL_VLN);
439 armada_updatel(CFG_HWC_ENA,
440 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
441 base + LCD_SPU_DMA_CTRL0);
442 dcrtc->cursor_update = false;
443 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
444 }
445
Russell King96f60e32012-08-15 13:59:49 +0100446 spin_unlock(&dcrtc->irq_lock);
447
Russell Kingec6fb152016-07-25 15:16:11 +0100448 if (stat & GRA_FRAME_IRQ)
449 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100450}
451
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100452static irqreturn_t armada_drm_irq(int irq, void *arg)
453{
454 struct armada_crtc *dcrtc = arg;
455 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
456
457 /*
458 * This is rediculous - rather than writing bits to clear, we
459 * have to set the actual status register value. This is racy.
460 */
461 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
462
Russell Kingc8a220c2016-05-17 13:51:08 +0100463 trace_armada_drm_irq(&dcrtc->crtc, stat);
464
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100465 /* Mask out those interrupts we haven't enabled */
466 v = stat & dcrtc->irq_ena;
467
468 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
469 armada_drm_crtc_irq(dcrtc, stat);
470 return IRQ_HANDLED;
471 }
472 return IRQ_NONE;
473}
474
Russell King96f60e32012-08-15 13:59:49 +0100475/* These are locked by dev->vbl_lock */
476void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
477{
478 if (dcrtc->irq_ena & mask) {
479 dcrtc->irq_ena &= ~mask;
480 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
481 }
482}
483
484void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
485{
486 if ((dcrtc->irq_ena & mask) != mask) {
487 dcrtc->irq_ena |= mask;
488 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
489 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
490 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
491 }
492}
493
494static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
495{
496 struct drm_display_mode *adj = &dcrtc->crtc.mode;
497 uint32_t val = 0;
498
499 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
500 val |= CFG_CSC_YUV_CCIR709;
501 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
502 val |= CFG_CSC_RGB_STUDIO;
503
504 /*
505 * In auto mode, set the colorimetry, based upon the HDMI spec.
506 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
507 * ITU601. It may be more appropriate to set this depending on
508 * the source - but what if the graphic frame is YUV and the
509 * video frame is RGB?
510 */
511 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
512 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
513 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
514 if (dcrtc->csc_yuv_mode == CSC_AUTO)
515 val |= CFG_CSC_YUV_CCIR709;
516 }
517
518 /*
519 * We assume we're connected to a TV-like device, so the YUV->RGB
520 * conversion should produce a limited range. We should set this
521 * depending on the connectors attached to this CRTC, and what
522 * kind of device they report being connected.
523 */
524 if (dcrtc->csc_rgb_mode == CSC_AUTO)
525 val |= CFG_CSC_RGB_STUDIO;
526
527 return val;
528}
529
Russell King37af35c2016-08-16 22:09:09 +0100530static void armada_drm_primary_set(struct drm_crtc *crtc,
531 struct drm_plane *plane, int x, int y)
532{
533 struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
534 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King2925db02016-08-16 22:09:10 +0100535 struct armada_regs regs[8];
Russell King37af35c2016-08-16 22:09:09 +0100536 bool interlaced = dcrtc->interlaced;
537 unsigned i;
Russell King2925db02016-08-16 22:09:10 +0100538 u32 ctrl0;
Russell King37af35c2016-08-16 22:09:09 +0100539
540 i = armada_drm_crtc_calc_fb(plane->fb, x, y, regs, interlaced);
541
Russell King2925db02016-08-16 22:09:10 +0100542 armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell King37af35c2016-08-16 22:09:09 +0100543 armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
544 armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
545
546 ctrl0 = state->ctrl0;
547 if (interlaced)
548 ctrl0 |= CFG_GRA_FTOGGLE;
549
550 armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
551 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
552 CFG_SWAPYU | CFG_YUV2RGB) |
553 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
554 LCD_SPU_DMA_CTRL0);
555 armada_reg_queue_end(regs, i);
556 armada_drm_crtc_update_regs(dcrtc, regs);
557}
558
Russell King96f60e32012-08-15 13:59:49 +0100559/* The mode_config.mutex will be held for this call */
560static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
561 struct drm_display_mode *mode, struct drm_display_mode *adj,
562 int x, int y, struct drm_framebuffer *old_fb)
563{
Russell King96f60e32012-08-15 13:59:49 +0100564 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
565 struct armada_regs regs[17];
566 uint32_t lm, rm, tm, bm, val, sclk;
567 unsigned long flags;
568 unsigned i;
569 bool interlaced;
570
Matt Roperf4510a22014-04-01 15:22:40 -0700571 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100572
573 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
574
Russell King8be523d2016-08-16 22:09:08 +0100575 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
576 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
577 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
578
579 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
580 val |= CFG_PALETTE_ENA;
581
582 drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
583 drm_to_armada_plane(crtc->primary)->state.src_hw =
584 drm_to_armada_plane(crtc->primary)->state.dst_hw =
Russell King37af35c2016-08-16 22:09:09 +0100585 adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
Russell King8be523d2016-08-16 22:09:08 +0100586 drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
587
Russell King37af35c2016-08-16 22:09:09 +0100588 i = 0;
Russell King96f60e32012-08-15 13:59:49 +0100589 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
590 lm = adj->crtc_htotal - adj->crtc_hsync_end;
591 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
592 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
593
594 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
595 adj->crtc_hdisplay,
596 adj->crtc_hsync_start,
597 adj->crtc_hsync_end,
598 adj->crtc_htotal, lm, rm);
599 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
600 adj->crtc_vdisplay,
601 adj->crtc_vsync_start,
602 adj->crtc_vsync_end,
603 adj->crtc_vtotal, tm, bm);
604
605 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100606 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
607 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100608
Russell King178e5612014-10-11 23:57:04 +0100609 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100610
Russell King96f60e32012-08-15 13:59:49 +0100611 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
612 if (val != dcrtc->dumb_ctrl) {
613 dcrtc->dumb_ctrl = val;
614 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
615 }
616
Russell Kinge0ac5e92015-06-29 18:01:38 +0100617 /*
618 * If we are blanked, we would have disabled the clock. Re-enable
619 * it so that compute_clock() does the right thing.
620 */
621 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
622 WARN_ON(clk_prepare_enable(dcrtc->clk));
623
Russell King96f60e32012-08-15 13:59:49 +0100624 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100625 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100626
627 /* Ensure graphic fifo is enabled */
628 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
629 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
630
631 if (interlaced ^ dcrtc->interlaced) {
632 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300633 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100634 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300635 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100636 dcrtc->interlaced = interlaced;
637 }
638
639 spin_lock_irqsave(&dcrtc->irq_lock, flags);
640
641 /* Even interlaced/progressive frame */
642 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
643 adj->crtc_htotal;
644 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
645 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100646 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100647 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100648
649 if (interlaced) {
650 /* Odd interlaced frame */
651 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
652 (1 << 16);
653 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
654 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100655 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100656 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100657 } else {
658 dcrtc->v[0] = dcrtc->v[1];
659 }
660
661 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
662
663 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King96f60e32012-08-15 13:59:49 +0100664 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
665 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
666 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
667 LCD_SPUT_V_H_TOTAL);
668
Russell King42e62ba2014-04-22 15:24:03 +0100669 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100670 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
671 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
672 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100673 }
Russell King96f60e32012-08-15 13:59:49 +0100674
Russell King96f60e32012-08-15 13:59:49 +0100675 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
676 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
677
678 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
679 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
680 armada_reg_queue_end(regs, i);
681
682 armada_drm_crtc_update_regs(dcrtc, regs);
Russell King37af35c2016-08-16 22:09:09 +0100683
684 armada_drm_primary_set(crtc, crtc->primary, x, y);
Russell King96f60e32012-08-15 13:59:49 +0100685 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
686
687 armada_drm_crtc_update(dcrtc);
688
Russell King178e5612014-10-11 23:57:04 +0100689 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100690 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
691
692 return 0;
693}
694
695/* The mode_config.mutex will be held for this call */
696static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
697 struct drm_framebuffer *old_fb)
698{
699 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
700 struct armada_regs regs[4];
701 unsigned i;
702
Matt Roperf4510a22014-04-01 15:22:40 -0700703 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100704 dcrtc->interlaced);
705 armada_reg_queue_end(regs, i);
706
707 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100708 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
709 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100710
711 /* Take a reference to the new fb as we're using it */
Matt Roperf4510a22014-04-01 15:22:40 -0700712 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100713
714 /* Update the base in the CRTC */
715 armada_drm_crtc_update_regs(dcrtc, regs);
716
717 /* Drop our previously held reference */
718 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
719
720 return 0;
721}
722
Russell King58326802015-07-15 18:11:25 +0100723void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
724 struct drm_plane *plane)
725{
Russell King9099ea12015-07-15 18:11:25 +0100726 u32 sram_para1, dma_ctrl0_mask;
Russell King58326802015-07-15 18:11:25 +0100727
728 /*
729 * Drop our reference on any framebuffer attached to this plane.
730 * We don't need to NULL this out as drm_plane_force_disable(),
731 * and __setplane_internal() will do so for an overlay plane, and
732 * __drm_helper_disable_unused_functions() will do so for the
733 * primary plane.
734 */
735 if (plane->fb)
736 drm_framebuffer_unreference(plane->fb);
737
738 /* Power down the Y/U/V FIFOs */
739 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
740
741 /* Power down most RAMs and FIFOs if this is the primary plane */
Russell King9099ea12015-07-15 18:11:25 +0100742 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
Russell King58326802015-07-15 18:11:25 +0100743 sram_para1 |= CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
744 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell King9099ea12015-07-15 18:11:25 +0100745 dma_ctrl0_mask = CFG_GRA_ENA;
746 } else {
747 dma_ctrl0_mask = CFG_DMA_ENA;
748 }
749
750 spin_lock_irq(&dcrtc->irq_lock);
751 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
752 spin_unlock_irq(&dcrtc->irq_lock);
Russell King58326802015-07-15 18:11:25 +0100753
754 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
755}
756
Russell King96f60e32012-08-15 13:59:49 +0100757/* The mode_config.mutex will be held for this call */
758static void armada_drm_crtc_disable(struct drm_crtc *crtc)
759{
760 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
761
762 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King58326802015-07-15 18:11:25 +0100763 armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
Russell King96f60e32012-08-15 13:59:49 +0100764}
765
766static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
767 .dpms = armada_drm_crtc_dpms,
768 .prepare = armada_drm_crtc_prepare,
769 .commit = armada_drm_crtc_commit,
770 .mode_fixup = armada_drm_crtc_mode_fixup,
771 .mode_set = armada_drm_crtc_mode_set,
772 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100773 .disable = armada_drm_crtc_disable,
774};
775
Russell King662af0d2013-05-19 10:55:17 +0100776static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
777 unsigned stride, unsigned width, unsigned height)
778{
779 uint32_t addr;
780 unsigned y;
781
782 addr = SRAM_HWC32_RAM1;
783 for (y = 0; y < height; y++) {
784 uint32_t *p = &pix[y * stride];
785 unsigned x;
786
787 for (x = 0; x < width; x++, p++) {
788 uint32_t val = *p;
789
790 val = (val & 0xff00ff00) |
791 (val & 0x000000ff) << 16 |
792 (val & 0x00ff0000) >> 16;
793
794 writel_relaxed(val,
795 base + LCD_SPU_SRAM_WRDAT);
796 writel_relaxed(addr | SRAM_WRITE,
797 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100798 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100799 addr += 1;
800 if ((addr & 0x00ff) == 0)
801 addr += 0xf00;
802 if ((addr & 0x30ff) == 0)
803 addr = SRAM_HWC32_RAM2;
804 }
805 }
806}
807
808static void armada_drm_crtc_cursor_tran(void __iomem *base)
809{
810 unsigned addr;
811
812 for (addr = 0; addr < 256; addr++) {
813 /* write the default value */
814 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
815 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
816 base + LCD_SPU_SRAM_CTRL);
817 }
818}
819
820static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
821{
822 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
823 uint32_t yoff, yscr, h = dcrtc->cursor_h;
824 uint32_t para1;
825
826 /*
827 * Calculate the visible width and height of the cursor,
828 * screen position, and the position in the cursor bitmap.
829 */
830 if (dcrtc->cursor_x < 0) {
831 xoff = -dcrtc->cursor_x;
832 xscr = 0;
833 w -= min(xoff, w);
834 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
835 xoff = 0;
836 xscr = dcrtc->cursor_x;
837 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
838 } else {
839 xoff = 0;
840 xscr = dcrtc->cursor_x;
841 }
842
843 if (dcrtc->cursor_y < 0) {
844 yoff = -dcrtc->cursor_y;
845 yscr = 0;
846 h -= min(yoff, h);
847 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
848 yoff = 0;
849 yscr = dcrtc->cursor_y;
850 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
851 } else {
852 yoff = 0;
853 yscr = dcrtc->cursor_y;
854 }
855
856 /* On interlaced modes, the vertical cursor size must be halved */
857 s = dcrtc->cursor_w;
858 if (dcrtc->interlaced) {
859 s *= 2;
860 yscr /= 2;
861 h /= 2;
862 }
863
864 if (!dcrtc->cursor_obj || !h || !w) {
865 spin_lock_irq(&dcrtc->irq_lock);
866 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
867 dcrtc->cursor_update = false;
868 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
869 spin_unlock_irq(&dcrtc->irq_lock);
870 return 0;
871 }
872
873 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
874 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
875 dcrtc->base + LCD_SPU_SRAM_PARA1);
876
877 /*
878 * Initialize the transparency if the SRAM was powered down.
879 * We must also reload the cursor data as well.
880 */
881 if (!(para1 & CFG_CSB_256x32)) {
882 armada_drm_crtc_cursor_tran(dcrtc->base);
883 reload = true;
884 }
885
886 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
887 spin_lock_irq(&dcrtc->irq_lock);
888 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
889 dcrtc->cursor_update = false;
890 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
891 spin_unlock_irq(&dcrtc->irq_lock);
892 reload = true;
893 }
894 if (reload) {
895 struct armada_gem_object *obj = dcrtc->cursor_obj;
896 uint32_t *pix;
897 /* Set the top-left corner of the cursor image */
898 pix = obj->addr;
899 pix += yoff * s + xoff;
900 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
901 }
902
903 /* Reload the cursor position, size and enable in the IRQ handler */
904 spin_lock_irq(&dcrtc->irq_lock);
905 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
906 dcrtc->cursor_hw_sz = h << 16 | w;
907 dcrtc->cursor_update = true;
908 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
909 spin_unlock_irq(&dcrtc->irq_lock);
910
911 return 0;
912}
913
914static void cursor_update(void *data)
915{
916 armada_drm_crtc_cursor_update(data, true);
917}
918
919static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
920 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
921{
Russell King662af0d2013-05-19 10:55:17 +0100922 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100923 struct armada_gem_object *obj = NULL;
924 int ret;
925
926 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100927 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100928 return -ENXIO;
929
930 if (handle && w > 0 && h > 0) {
931 /* maximum size is 64x32 or 32x64 */
932 if (w > 64 || h > 64 || (w > 32 && h > 32))
933 return -ENOMEM;
934
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100935 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100936 if (!obj)
937 return -ENOENT;
938
939 /* Must be a kernel-mapped object */
940 if (!obj->addr) {
941 drm_gem_object_unreference_unlocked(&obj->obj);
942 return -EINVAL;
943 }
944
945 if (obj->obj.size < w * h * 4) {
946 DRM_ERROR("buffer is too small\n");
947 drm_gem_object_unreference_unlocked(&obj->obj);
948 return -ENOMEM;
949 }
950 }
951
Russell King662af0d2013-05-19 10:55:17 +0100952 if (dcrtc->cursor_obj) {
953 dcrtc->cursor_obj->update = NULL;
954 dcrtc->cursor_obj->update_data = NULL;
Daniel Vetter4bd3fd42015-11-23 10:32:45 +0100955 drm_gem_object_unreference_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100956 }
957 dcrtc->cursor_obj = obj;
958 dcrtc->cursor_w = w;
959 dcrtc->cursor_h = h;
960 ret = armada_drm_crtc_cursor_update(dcrtc, true);
961 if (obj) {
962 obj->update_data = dcrtc;
963 obj->update = cursor_update;
964 }
Russell King662af0d2013-05-19 10:55:17 +0100965
966 return ret;
967}
968
969static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
970{
Russell King662af0d2013-05-19 10:55:17 +0100971 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100972 int ret;
973
974 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100975 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100976 return -EFAULT;
977
Russell King662af0d2013-05-19 10:55:17 +0100978 dcrtc->cursor_x = x;
979 dcrtc->cursor_y = y;
980 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +0100981
982 return ret;
983}
984
Russell King96f60e32012-08-15 13:59:49 +0100985static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
986{
987 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
988 struct armada_private *priv = crtc->dev->dev_private;
989
Russell King662af0d2013-05-19 10:55:17 +0100990 if (dcrtc->cursor_obj)
Daniel Vetter7a6f7132015-11-23 10:32:34 +0100991 drm_gem_object_unreference_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100992
Russell King96f60e32012-08-15 13:59:49 +0100993 priv->dcrtc[dcrtc->num] = NULL;
994 drm_crtc_cleanup(&dcrtc->crtc);
995
996 if (!IS_ERR(dcrtc->clk))
997 clk_disable_unprepare(dcrtc->clk);
998
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100999 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1000
Russell King9611cb92014-06-15 11:21:23 +01001001 of_node_put(dcrtc->crtc.port);
1002
Russell King96f60e32012-08-15 13:59:49 +01001003 kfree(dcrtc);
1004}
1005
1006/*
1007 * The mode_config lock is held here, to prevent races between this
1008 * and a mode_set.
1009 */
1010static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Dave Airlie5e4e3ba2013-10-22 09:38:18 +01001011 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Russell King96f60e32012-08-15 13:59:49 +01001012{
1013 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1014 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001015 unsigned i;
1016 int ret;
1017
1018 /* We don't support changing the pixel format */
Matt Roperf4510a22014-04-01 15:22:40 -07001019 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Russell King96f60e32012-08-15 13:59:49 +01001020 return -EINVAL;
1021
1022 work = kmalloc(sizeof(*work), GFP_KERNEL);
1023 if (!work)
1024 return -ENOMEM;
1025
Russell King4b5dda82015-08-06 16:37:18 +01001026 work->work.fn = armada_drm_crtc_complete_frame_work;
Russell King96f60e32012-08-15 13:59:49 +01001027 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -07001028 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001029
1030 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1031 dcrtc->interlaced);
1032 armada_reg_queue_end(work->regs, i);
1033
1034 /*
Russell Kingc5488302014-10-11 23:53:35 +01001035 * Ensure that we hold a reference on the new framebuffer.
1036 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001037 */
Russell Kingc5488302014-10-11 23:53:35 +01001038 drm_framebuffer_reference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001039
1040 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
1041 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001042 /* Undo our reference above */
1043 drm_framebuffer_unreference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001044 kfree(work);
1045 return ret;
1046 }
1047
1048 /*
1049 * Don't take a reference on the new framebuffer;
1050 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1051 * will _not_ drop that reference on successful return from this
1052 * function. Simply mark this new framebuffer as the current one.
1053 */
Matt Roperf4510a22014-04-01 15:22:40 -07001054 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001055
1056 /*
1057 * Finally, if the display is blanked, we won't receive an
1058 * interrupt, so complete it now.
1059 */
Russell King4b5dda82015-08-06 16:37:18 +01001060 if (dpms_blanked(dcrtc->dpms))
Russell Kingec6fb152016-07-25 15:16:11 +01001061 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001062
1063 return 0;
1064}
1065
1066static int
1067armada_drm_crtc_set_property(struct drm_crtc *crtc,
1068 struct drm_property *property, uint64_t val)
1069{
1070 struct armada_private *priv = crtc->dev->dev_private;
1071 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1072 bool update_csc = false;
1073
1074 if (property == priv->csc_yuv_prop) {
1075 dcrtc->csc_yuv_mode = val;
1076 update_csc = true;
1077 } else if (property == priv->csc_rgb_prop) {
1078 dcrtc->csc_rgb_mode = val;
1079 update_csc = true;
1080 }
1081
1082 if (update_csc) {
1083 uint32_t val;
1084
1085 val = dcrtc->spu_iopad_ctrl |
1086 armada_drm_crtc_calculate_csc(dcrtc);
1087 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1088 }
1089
1090 return 0;
1091}
1092
Ville Syrjäläa02fb902015-12-15 12:20:59 +01001093static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001094 .cursor_set = armada_drm_crtc_cursor_set,
1095 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001096 .destroy = armada_drm_crtc_destroy,
1097 .set_config = drm_crtc_helper_set_config,
1098 .page_flip = armada_drm_crtc_page_flip,
1099 .set_property = armada_drm_crtc_set_property,
1100};
1101
Russell Kingde323012015-07-15 18:11:24 +01001102static const struct drm_plane_funcs armada_primary_plane_funcs = {
1103 .update_plane = drm_primary_helper_update,
1104 .disable_plane = drm_primary_helper_disable,
1105 .destroy = drm_primary_helper_destroy,
1106};
1107
Russell King5740d272015-07-15 18:11:25 +01001108int armada_drm_plane_init(struct armada_plane *plane)
1109{
1110 init_waitqueue_head(&plane->frame_wait);
1111
1112 return 0;
1113}
1114
Russell King96f60e32012-08-15 13:59:49 +01001115static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1116 { CSC_AUTO, "Auto" },
1117 { CSC_YUV_CCIR601, "CCIR601" },
1118 { CSC_YUV_CCIR709, "CCIR709" },
1119};
1120
1121static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1122 { CSC_AUTO, "Auto" },
1123 { CSC_RGB_COMPUTER, "Computer system" },
1124 { CSC_RGB_STUDIO, "Studio" },
1125};
1126
1127static int armada_drm_crtc_create_properties(struct drm_device *dev)
1128{
1129 struct armada_private *priv = dev->dev_private;
1130
1131 if (priv->csc_yuv_prop)
1132 return 0;
1133
1134 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1135 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1136 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1137 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1138 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1139 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1140
1141 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1142 return -ENOMEM;
1143
1144 return 0;
1145}
1146
Russell King0fb29702015-06-06 21:46:53 +01001147static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001148 struct resource *res, int irq, const struct armada_variant *variant,
1149 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001150{
Russell Kingd8c96082014-04-22 11:10:15 +01001151 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001152 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001153 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001154 void __iomem *base;
1155 int ret;
1156
Russell Kingd8c96082014-04-22 11:10:15 +01001157 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001158 if (ret)
1159 return ret;
1160
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001161 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001162 if (IS_ERR(base))
1163 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001164
1165 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1166 if (!dcrtc) {
1167 DRM_ERROR("failed to allocate Armada crtc\n");
1168 return -ENOMEM;
1169 }
1170
Russell Kingd8c96082014-04-22 11:10:15 +01001171 if (dev != drm->dev)
1172 dev_set_drvdata(dev, dcrtc);
1173
Russell King42e62ba2014-04-22 15:24:03 +01001174 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001175 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001176 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001177 dcrtc->clk = ERR_PTR(-EINVAL);
1178 dcrtc->csc_yuv_mode = CSC_AUTO;
1179 dcrtc->csc_rgb_mode = CSC_AUTO;
1180 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1181 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1182 spin_lock_init(&dcrtc->irq_lock);
1183 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +01001184
1185 /* Initialize some registers which we don't otherwise set */
1186 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1187 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1188 writel_relaxed(dcrtc->spu_iopad_ctrl,
1189 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1190 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1191 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1192 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1193 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1194 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001195 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1196 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001197
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001198 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1199 dcrtc);
1200 if (ret < 0) {
1201 kfree(dcrtc);
1202 return ret;
1203 }
Russell King96f60e32012-08-15 13:59:49 +01001204
Russell King42e62ba2014-04-22 15:24:03 +01001205 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001206 ret = dcrtc->variant->init(dcrtc, dev);
Russell King96f60e32012-08-15 13:59:49 +01001207 if (ret) {
1208 kfree(dcrtc);
1209 return ret;
1210 }
1211 }
1212
1213 /* Ensure AXI pipeline is enabled */
1214 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1215
1216 priv->dcrtc[dcrtc->num] = dcrtc;
1217
Russell King9611cb92014-06-15 11:21:23 +01001218 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001219
Russell Kingde323012015-07-15 18:11:24 +01001220 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King1c914ce2015-07-15 18:11:24 +01001221 if (!primary)
1222 return -ENOMEM;
1223
Russell King5740d272015-07-15 18:11:25 +01001224 ret = armada_drm_plane_init(primary);
1225 if (ret) {
1226 kfree(primary);
1227 return ret;
1228 }
1229
Russell Kingde323012015-07-15 18:11:24 +01001230 ret = drm_universal_plane_init(drm, &primary->base, 0,
1231 &armada_primary_plane_funcs,
1232 armada_primary_formats,
1233 ARRAY_SIZE(armada_primary_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001234 DRM_PLANE_TYPE_PRIMARY, NULL);
Russell Kingde323012015-07-15 18:11:24 +01001235 if (ret) {
1236 kfree(primary);
1237 return ret;
1238 }
1239
1240 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001241 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001242 if (ret)
1243 goto err_crtc_init;
1244
Russell King96f60e32012-08-15 13:59:49 +01001245 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1246
1247 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1248 dcrtc->csc_yuv_mode);
1249 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1250 dcrtc->csc_rgb_mode);
1251
Russell Kingd8c96082014-04-22 11:10:15 +01001252 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001253
1254err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001255 primary->base.funcs->destroy(&primary->base);
Russell King1c914ce2015-07-15 18:11:24 +01001256 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001257}
Russell Kingd8c96082014-04-22 11:10:15 +01001258
1259static int
1260armada_lcd_bind(struct device *dev, struct device *master, void *data)
1261{
1262 struct platform_device *pdev = to_platform_device(dev);
1263 struct drm_device *drm = data;
1264 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1265 int irq = platform_get_irq(pdev, 0);
1266 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001267 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001268
1269 if (irq < 0)
1270 return irq;
1271
1272 if (!dev->of_node) {
1273 const struct platform_device_id *id;
1274
1275 id = platform_get_device_id(pdev);
1276 if (!id)
1277 return -ENXIO;
1278
1279 variant = (const struct armada_variant *)id->driver_data;
1280 } else {
1281 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001282 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001283
1284 match = of_match_device(dev->driver->of_match_table, dev);
1285 if (!match)
1286 return -ENXIO;
1287
Russell King9611cb92014-06-15 11:21:23 +01001288 np = of_get_child_by_name(parent, "ports");
1289 if (np)
1290 parent = np;
1291 port = of_get_child_by_name(parent, "port");
1292 of_node_put(np);
1293 if (!port) {
1294 dev_err(dev, "no port node found in %s\n",
1295 parent->full_name);
1296 return -ENXIO;
1297 }
1298
Russell Kingd8c96082014-04-22 11:10:15 +01001299 variant = match->data;
1300 }
1301
Russell King9611cb92014-06-15 11:21:23 +01001302 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001303}
1304
1305static void
1306armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1307{
1308 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1309
1310 armada_drm_crtc_destroy(&dcrtc->crtc);
1311}
1312
1313static const struct component_ops armada_lcd_ops = {
1314 .bind = armada_lcd_bind,
1315 .unbind = armada_lcd_unbind,
1316};
1317
1318static int armada_lcd_probe(struct platform_device *pdev)
1319{
1320 return component_add(&pdev->dev, &armada_lcd_ops);
1321}
1322
1323static int armada_lcd_remove(struct platform_device *pdev)
1324{
1325 component_del(&pdev->dev, &armada_lcd_ops);
1326 return 0;
1327}
1328
1329static struct of_device_id armada_lcd_of_match[] = {
1330 {
1331 .compatible = "marvell,dove-lcd",
1332 .data = &armada510_ops,
1333 },
1334 {}
1335};
1336MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1337
1338static const struct platform_device_id armada_lcd_platform_ids[] = {
1339 {
1340 .name = "armada-lcd",
1341 .driver_data = (unsigned long)&armada510_ops,
1342 }, {
1343 .name = "armada-510-lcd",
1344 .driver_data = (unsigned long)&armada510_ops,
1345 },
1346 { },
1347};
1348MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1349
1350struct platform_driver armada_lcd_platform_driver = {
1351 .probe = armada_lcd_probe,
1352 .remove = armada_lcd_remove,
1353 .driver = {
1354 .name = "armada-lcd",
1355 .owner = THIS_MODULE,
1356 .of_match_table = armada_lcd_of_match,
1357 },
1358 .id_table = armada_lcd_platform_ids,
1359};