blob: 719873be3beb07564786812df2da4ad11b43a8ad [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
530/* The mode_config.mutex will be held for this call */
531static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
532 struct drm_display_mode *mode, struct drm_display_mode *adj,
533 int x, int y, struct drm_framebuffer *old_fb)
534{
Russell King96f60e32012-08-15 13:59:49 +0100535 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
536 struct armada_regs regs[17];
537 uint32_t lm, rm, tm, bm, val, sclk;
538 unsigned long flags;
539 unsigned i;
540 bool interlaced;
541
Matt Roperf4510a22014-04-01 15:22:40 -0700542 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100543
544 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
545
Russell King8be523d2016-08-16 22:09:08 +0100546 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
547 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
548 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
549
550 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
551 val |= CFG_PALETTE_ENA;
552
553 drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
554 drm_to_armada_plane(crtc->primary)->state.src_hw =
555 drm_to_armada_plane(crtc->primary)->state.dst_hw =
556 adj->crtc_hdisplay << 16 | adj->crtc_vdisplay;
557 drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
558
Matt Roperf4510a22014-04-01 15:22:40 -0700559 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
560 x, y, regs, interlaced);
Russell King96f60e32012-08-15 13:59:49 +0100561
562 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
563 lm = adj->crtc_htotal - adj->crtc_hsync_end;
564 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
565 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
566
567 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
568 adj->crtc_hdisplay,
569 adj->crtc_hsync_start,
570 adj->crtc_hsync_end,
571 adj->crtc_htotal, lm, rm);
572 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
573 adj->crtc_vdisplay,
574 adj->crtc_vsync_start,
575 adj->crtc_vsync_end,
576 adj->crtc_vtotal, tm, bm);
577
578 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100579 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
580 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100581
Russell King178e5612014-10-11 23:57:04 +0100582 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100583
Russell King96f60e32012-08-15 13:59:49 +0100584 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
585 if (val != dcrtc->dumb_ctrl) {
586 dcrtc->dumb_ctrl = val;
587 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
588 }
589
Russell Kinge0ac5e92015-06-29 18:01:38 +0100590 /*
591 * If we are blanked, we would have disabled the clock. Re-enable
592 * it so that compute_clock() does the right thing.
593 */
594 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
595 WARN_ON(clk_prepare_enable(dcrtc->clk));
596
Russell King96f60e32012-08-15 13:59:49 +0100597 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100598 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100599
600 /* Ensure graphic fifo is enabled */
601 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
602 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
603
604 if (interlaced ^ dcrtc->interlaced) {
605 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300606 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100607 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300608 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100609 dcrtc->interlaced = interlaced;
610 }
611
612 spin_lock_irqsave(&dcrtc->irq_lock, flags);
613
614 /* Even interlaced/progressive frame */
615 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
616 adj->crtc_htotal;
617 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
618 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100619 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100620 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100621
622 if (interlaced) {
623 /* Odd interlaced frame */
624 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
625 (1 << 16);
626 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
627 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100628 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100629 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100630 } else {
631 dcrtc->v[0] = dcrtc->v[1];
632 }
633
634 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
635
636 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King8be523d2016-08-16 22:09:08 +0100637 armada_reg_queue_set(regs, i,
638 drm_to_armada_plane(crtc->primary)->state.src_hw,
639 LCD_SPU_GRA_HPXL_VLN);
640 armada_reg_queue_set(regs, i,
641 drm_to_armada_plane(crtc->primary)->state.dst_hw,
642 LCD_SPU_GZM_HPXL_VLN);
Russell King96f60e32012-08-15 13:59:49 +0100643 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
644 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
645 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
646 LCD_SPUT_V_H_TOTAL);
647
Russell King42e62ba2014-04-22 15:24:03 +0100648 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100649 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
650 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
651 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100652 }
Russell King96f60e32012-08-15 13:59:49 +0100653
Russell King8be523d2016-08-16 22:09:08 +0100654 val = drm_to_armada_plane(crtc->primary)->state.ctrl0;
Russell King96f60e32012-08-15 13:59:49 +0100655 if (interlaced)
656 val |= CFG_GRA_FTOGGLE;
657
658 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
659 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
660 CFG_SWAPYU | CFG_YUV2RGB) |
661 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
662 LCD_SPU_DMA_CTRL0);
663
664 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
665 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
666
667 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
668 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
669 armada_reg_queue_end(regs, i);
670
671 armada_drm_crtc_update_regs(dcrtc, regs);
672 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
673
674 armada_drm_crtc_update(dcrtc);
675
Russell King178e5612014-10-11 23:57:04 +0100676 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100677 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
678
679 return 0;
680}
681
682/* The mode_config.mutex will be held for this call */
683static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
684 struct drm_framebuffer *old_fb)
685{
686 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
687 struct armada_regs regs[4];
688 unsigned i;
689
Matt Roperf4510a22014-04-01 15:22:40 -0700690 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100691 dcrtc->interlaced);
692 armada_reg_queue_end(regs, i);
693
694 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100695 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
696 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100697
698 /* Take a reference to the new fb as we're using it */
Matt Roperf4510a22014-04-01 15:22:40 -0700699 drm_framebuffer_reference(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100700
701 /* Update the base in the CRTC */
702 armada_drm_crtc_update_regs(dcrtc, regs);
703
704 /* Drop our previously held reference */
705 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
706
707 return 0;
708}
709
Russell King58326802015-07-15 18:11:25 +0100710void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
711 struct drm_plane *plane)
712{
Russell King9099ea12015-07-15 18:11:25 +0100713 u32 sram_para1, dma_ctrl0_mask;
Russell King58326802015-07-15 18:11:25 +0100714
715 /*
716 * Drop our reference on any framebuffer attached to this plane.
717 * We don't need to NULL this out as drm_plane_force_disable(),
718 * and __setplane_internal() will do so for an overlay plane, and
719 * __drm_helper_disable_unused_functions() will do so for the
720 * primary plane.
721 */
722 if (plane->fb)
723 drm_framebuffer_unreference(plane->fb);
724
725 /* Power down the Y/U/V FIFOs */
726 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
727
728 /* Power down most RAMs and FIFOs if this is the primary plane */
Russell King9099ea12015-07-15 18:11:25 +0100729 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
Russell King58326802015-07-15 18:11:25 +0100730 sram_para1 |= CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
731 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell King9099ea12015-07-15 18:11:25 +0100732 dma_ctrl0_mask = CFG_GRA_ENA;
733 } else {
734 dma_ctrl0_mask = CFG_DMA_ENA;
735 }
736
737 spin_lock_irq(&dcrtc->irq_lock);
738 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
739 spin_unlock_irq(&dcrtc->irq_lock);
Russell King58326802015-07-15 18:11:25 +0100740
741 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
742}
743
Russell King96f60e32012-08-15 13:59:49 +0100744/* The mode_config.mutex will be held for this call */
745static void armada_drm_crtc_disable(struct drm_crtc *crtc)
746{
747 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
748
749 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King58326802015-07-15 18:11:25 +0100750 armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
Russell King96f60e32012-08-15 13:59:49 +0100751}
752
753static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
754 .dpms = armada_drm_crtc_dpms,
755 .prepare = armada_drm_crtc_prepare,
756 .commit = armada_drm_crtc_commit,
757 .mode_fixup = armada_drm_crtc_mode_fixup,
758 .mode_set = armada_drm_crtc_mode_set,
759 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100760 .disable = armada_drm_crtc_disable,
761};
762
Russell King662af0d2013-05-19 10:55:17 +0100763static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
764 unsigned stride, unsigned width, unsigned height)
765{
766 uint32_t addr;
767 unsigned y;
768
769 addr = SRAM_HWC32_RAM1;
770 for (y = 0; y < height; y++) {
771 uint32_t *p = &pix[y * stride];
772 unsigned x;
773
774 for (x = 0; x < width; x++, p++) {
775 uint32_t val = *p;
776
777 val = (val & 0xff00ff00) |
778 (val & 0x000000ff) << 16 |
779 (val & 0x00ff0000) >> 16;
780
781 writel_relaxed(val,
782 base + LCD_SPU_SRAM_WRDAT);
783 writel_relaxed(addr | SRAM_WRITE,
784 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100785 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100786 addr += 1;
787 if ((addr & 0x00ff) == 0)
788 addr += 0xf00;
789 if ((addr & 0x30ff) == 0)
790 addr = SRAM_HWC32_RAM2;
791 }
792 }
793}
794
795static void armada_drm_crtc_cursor_tran(void __iomem *base)
796{
797 unsigned addr;
798
799 for (addr = 0; addr < 256; addr++) {
800 /* write the default value */
801 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
802 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
803 base + LCD_SPU_SRAM_CTRL);
804 }
805}
806
807static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
808{
809 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
810 uint32_t yoff, yscr, h = dcrtc->cursor_h;
811 uint32_t para1;
812
813 /*
814 * Calculate the visible width and height of the cursor,
815 * screen position, and the position in the cursor bitmap.
816 */
817 if (dcrtc->cursor_x < 0) {
818 xoff = -dcrtc->cursor_x;
819 xscr = 0;
820 w -= min(xoff, w);
821 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
822 xoff = 0;
823 xscr = dcrtc->cursor_x;
824 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
825 } else {
826 xoff = 0;
827 xscr = dcrtc->cursor_x;
828 }
829
830 if (dcrtc->cursor_y < 0) {
831 yoff = -dcrtc->cursor_y;
832 yscr = 0;
833 h -= min(yoff, h);
834 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
835 yoff = 0;
836 yscr = dcrtc->cursor_y;
837 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
838 } else {
839 yoff = 0;
840 yscr = dcrtc->cursor_y;
841 }
842
843 /* On interlaced modes, the vertical cursor size must be halved */
844 s = dcrtc->cursor_w;
845 if (dcrtc->interlaced) {
846 s *= 2;
847 yscr /= 2;
848 h /= 2;
849 }
850
851 if (!dcrtc->cursor_obj || !h || !w) {
852 spin_lock_irq(&dcrtc->irq_lock);
853 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
854 dcrtc->cursor_update = false;
855 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
856 spin_unlock_irq(&dcrtc->irq_lock);
857 return 0;
858 }
859
860 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
861 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
862 dcrtc->base + LCD_SPU_SRAM_PARA1);
863
864 /*
865 * Initialize the transparency if the SRAM was powered down.
866 * We must also reload the cursor data as well.
867 */
868 if (!(para1 & CFG_CSB_256x32)) {
869 armada_drm_crtc_cursor_tran(dcrtc->base);
870 reload = true;
871 }
872
873 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
874 spin_lock_irq(&dcrtc->irq_lock);
875 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
876 dcrtc->cursor_update = false;
877 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
878 spin_unlock_irq(&dcrtc->irq_lock);
879 reload = true;
880 }
881 if (reload) {
882 struct armada_gem_object *obj = dcrtc->cursor_obj;
883 uint32_t *pix;
884 /* Set the top-left corner of the cursor image */
885 pix = obj->addr;
886 pix += yoff * s + xoff;
887 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
888 }
889
890 /* Reload the cursor position, size and enable in the IRQ handler */
891 spin_lock_irq(&dcrtc->irq_lock);
892 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
893 dcrtc->cursor_hw_sz = h << 16 | w;
894 dcrtc->cursor_update = true;
895 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
896 spin_unlock_irq(&dcrtc->irq_lock);
897
898 return 0;
899}
900
901static void cursor_update(void *data)
902{
903 armada_drm_crtc_cursor_update(data, true);
904}
905
906static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
907 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
908{
Russell King662af0d2013-05-19 10:55:17 +0100909 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100910 struct armada_gem_object *obj = NULL;
911 int ret;
912
913 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100914 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100915 return -ENXIO;
916
917 if (handle && w > 0 && h > 0) {
918 /* maximum size is 64x32 or 32x64 */
919 if (w > 64 || h > 64 || (w > 32 && h > 32))
920 return -ENOMEM;
921
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100922 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100923 if (!obj)
924 return -ENOENT;
925
926 /* Must be a kernel-mapped object */
927 if (!obj->addr) {
928 drm_gem_object_unreference_unlocked(&obj->obj);
929 return -EINVAL;
930 }
931
932 if (obj->obj.size < w * h * 4) {
933 DRM_ERROR("buffer is too small\n");
934 drm_gem_object_unreference_unlocked(&obj->obj);
935 return -ENOMEM;
936 }
937 }
938
Russell King662af0d2013-05-19 10:55:17 +0100939 if (dcrtc->cursor_obj) {
940 dcrtc->cursor_obj->update = NULL;
941 dcrtc->cursor_obj->update_data = NULL;
Daniel Vetter4bd3fd42015-11-23 10:32:45 +0100942 drm_gem_object_unreference_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100943 }
944 dcrtc->cursor_obj = obj;
945 dcrtc->cursor_w = w;
946 dcrtc->cursor_h = h;
947 ret = armada_drm_crtc_cursor_update(dcrtc, true);
948 if (obj) {
949 obj->update_data = dcrtc;
950 obj->update = cursor_update;
951 }
Russell King662af0d2013-05-19 10:55:17 +0100952
953 return ret;
954}
955
956static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
957{
Russell King662af0d2013-05-19 10:55:17 +0100958 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100959 int ret;
960
961 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100962 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100963 return -EFAULT;
964
Russell King662af0d2013-05-19 10:55:17 +0100965 dcrtc->cursor_x = x;
966 dcrtc->cursor_y = y;
967 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +0100968
969 return ret;
970}
971
Russell King96f60e32012-08-15 13:59:49 +0100972static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
973{
974 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
975 struct armada_private *priv = crtc->dev->dev_private;
976
Russell King662af0d2013-05-19 10:55:17 +0100977 if (dcrtc->cursor_obj)
Daniel Vetter7a6f7132015-11-23 10:32:34 +0100978 drm_gem_object_unreference_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100979
Russell King96f60e32012-08-15 13:59:49 +0100980 priv->dcrtc[dcrtc->num] = NULL;
981 drm_crtc_cleanup(&dcrtc->crtc);
982
983 if (!IS_ERR(dcrtc->clk))
984 clk_disable_unprepare(dcrtc->clk);
985
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100986 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
987
Russell King9611cb92014-06-15 11:21:23 +0100988 of_node_put(dcrtc->crtc.port);
989
Russell King96f60e32012-08-15 13:59:49 +0100990 kfree(dcrtc);
991}
992
993/*
994 * The mode_config lock is held here, to prevent races between this
995 * and a mode_set.
996 */
997static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Dave Airlie5e4e3ba2013-10-22 09:38:18 +0100998 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Russell King96f60e32012-08-15 13:59:49 +0100999{
1000 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1001 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001002 unsigned i;
1003 int ret;
1004
1005 /* We don't support changing the pixel format */
Matt Roperf4510a22014-04-01 15:22:40 -07001006 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Russell King96f60e32012-08-15 13:59:49 +01001007 return -EINVAL;
1008
1009 work = kmalloc(sizeof(*work), GFP_KERNEL);
1010 if (!work)
1011 return -ENOMEM;
1012
Russell King4b5dda82015-08-06 16:37:18 +01001013 work->work.fn = armada_drm_crtc_complete_frame_work;
Russell King96f60e32012-08-15 13:59:49 +01001014 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -07001015 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001016
1017 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1018 dcrtc->interlaced);
1019 armada_reg_queue_end(work->regs, i);
1020
1021 /*
Russell Kingc5488302014-10-11 23:53:35 +01001022 * Ensure that we hold a reference on the new framebuffer.
1023 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001024 */
Russell Kingc5488302014-10-11 23:53:35 +01001025 drm_framebuffer_reference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001026
1027 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
1028 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001029 /* Undo our reference above */
1030 drm_framebuffer_unreference(fb);
Russell King96f60e32012-08-15 13:59:49 +01001031 kfree(work);
1032 return ret;
1033 }
1034
1035 /*
1036 * Don't take a reference on the new framebuffer;
1037 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1038 * will _not_ drop that reference on successful return from this
1039 * function. Simply mark this new framebuffer as the current one.
1040 */
Matt Roperf4510a22014-04-01 15:22:40 -07001041 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001042
1043 /*
1044 * Finally, if the display is blanked, we won't receive an
1045 * interrupt, so complete it now.
1046 */
Russell King4b5dda82015-08-06 16:37:18 +01001047 if (dpms_blanked(dcrtc->dpms))
Russell Kingec6fb152016-07-25 15:16:11 +01001048 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001049
1050 return 0;
1051}
1052
1053static int
1054armada_drm_crtc_set_property(struct drm_crtc *crtc,
1055 struct drm_property *property, uint64_t val)
1056{
1057 struct armada_private *priv = crtc->dev->dev_private;
1058 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1059 bool update_csc = false;
1060
1061 if (property == priv->csc_yuv_prop) {
1062 dcrtc->csc_yuv_mode = val;
1063 update_csc = true;
1064 } else if (property == priv->csc_rgb_prop) {
1065 dcrtc->csc_rgb_mode = val;
1066 update_csc = true;
1067 }
1068
1069 if (update_csc) {
1070 uint32_t val;
1071
1072 val = dcrtc->spu_iopad_ctrl |
1073 armada_drm_crtc_calculate_csc(dcrtc);
1074 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1075 }
1076
1077 return 0;
1078}
1079
Ville Syrjäläa02fb902015-12-15 12:20:59 +01001080static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001081 .cursor_set = armada_drm_crtc_cursor_set,
1082 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001083 .destroy = armada_drm_crtc_destroy,
1084 .set_config = drm_crtc_helper_set_config,
1085 .page_flip = armada_drm_crtc_page_flip,
1086 .set_property = armada_drm_crtc_set_property,
1087};
1088
Russell Kingde323012015-07-15 18:11:24 +01001089static const struct drm_plane_funcs armada_primary_plane_funcs = {
1090 .update_plane = drm_primary_helper_update,
1091 .disable_plane = drm_primary_helper_disable,
1092 .destroy = drm_primary_helper_destroy,
1093};
1094
Russell King5740d272015-07-15 18:11:25 +01001095int armada_drm_plane_init(struct armada_plane *plane)
1096{
1097 init_waitqueue_head(&plane->frame_wait);
1098
1099 return 0;
1100}
1101
Russell King96f60e32012-08-15 13:59:49 +01001102static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1103 { CSC_AUTO, "Auto" },
1104 { CSC_YUV_CCIR601, "CCIR601" },
1105 { CSC_YUV_CCIR709, "CCIR709" },
1106};
1107
1108static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1109 { CSC_AUTO, "Auto" },
1110 { CSC_RGB_COMPUTER, "Computer system" },
1111 { CSC_RGB_STUDIO, "Studio" },
1112};
1113
1114static int armada_drm_crtc_create_properties(struct drm_device *dev)
1115{
1116 struct armada_private *priv = dev->dev_private;
1117
1118 if (priv->csc_yuv_prop)
1119 return 0;
1120
1121 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1122 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1123 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1124 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1125 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1126 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1127
1128 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1129 return -ENOMEM;
1130
1131 return 0;
1132}
1133
Russell King0fb29702015-06-06 21:46:53 +01001134static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001135 struct resource *res, int irq, const struct armada_variant *variant,
1136 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001137{
Russell Kingd8c96082014-04-22 11:10:15 +01001138 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001139 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001140 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001141 void __iomem *base;
1142 int ret;
1143
Russell Kingd8c96082014-04-22 11:10:15 +01001144 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001145 if (ret)
1146 return ret;
1147
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001148 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001149 if (IS_ERR(base))
1150 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001151
1152 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1153 if (!dcrtc) {
1154 DRM_ERROR("failed to allocate Armada crtc\n");
1155 return -ENOMEM;
1156 }
1157
Russell Kingd8c96082014-04-22 11:10:15 +01001158 if (dev != drm->dev)
1159 dev_set_drvdata(dev, dcrtc);
1160
Russell King42e62ba2014-04-22 15:24:03 +01001161 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001162 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001163 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001164 dcrtc->clk = ERR_PTR(-EINVAL);
1165 dcrtc->csc_yuv_mode = CSC_AUTO;
1166 dcrtc->csc_rgb_mode = CSC_AUTO;
1167 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1168 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1169 spin_lock_init(&dcrtc->irq_lock);
1170 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +01001171
1172 /* Initialize some registers which we don't otherwise set */
1173 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1174 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1175 writel_relaxed(dcrtc->spu_iopad_ctrl,
1176 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1177 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1178 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1179 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1180 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1181 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1182 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001183 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1184 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001185
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001186 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1187 dcrtc);
1188 if (ret < 0) {
1189 kfree(dcrtc);
1190 return ret;
1191 }
Russell King96f60e32012-08-15 13:59:49 +01001192
Russell King42e62ba2014-04-22 15:24:03 +01001193 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001194 ret = dcrtc->variant->init(dcrtc, dev);
Russell King96f60e32012-08-15 13:59:49 +01001195 if (ret) {
1196 kfree(dcrtc);
1197 return ret;
1198 }
1199 }
1200
1201 /* Ensure AXI pipeline is enabled */
1202 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1203
1204 priv->dcrtc[dcrtc->num] = dcrtc;
1205
Russell King9611cb92014-06-15 11:21:23 +01001206 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001207
Russell Kingde323012015-07-15 18:11:24 +01001208 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King1c914ce2015-07-15 18:11:24 +01001209 if (!primary)
1210 return -ENOMEM;
1211
Russell King5740d272015-07-15 18:11:25 +01001212 ret = armada_drm_plane_init(primary);
1213 if (ret) {
1214 kfree(primary);
1215 return ret;
1216 }
1217
Russell Kingde323012015-07-15 18:11:24 +01001218 ret = drm_universal_plane_init(drm, &primary->base, 0,
1219 &armada_primary_plane_funcs,
1220 armada_primary_formats,
1221 ARRAY_SIZE(armada_primary_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001222 DRM_PLANE_TYPE_PRIMARY, NULL);
Russell Kingde323012015-07-15 18:11:24 +01001223 if (ret) {
1224 kfree(primary);
1225 return ret;
1226 }
1227
1228 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001229 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001230 if (ret)
1231 goto err_crtc_init;
1232
Russell King96f60e32012-08-15 13:59:49 +01001233 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1234
1235 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1236 dcrtc->csc_yuv_mode);
1237 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1238 dcrtc->csc_rgb_mode);
1239
Russell Kingd8c96082014-04-22 11:10:15 +01001240 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001241
1242err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001243 primary->base.funcs->destroy(&primary->base);
Russell King1c914ce2015-07-15 18:11:24 +01001244 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001245}
Russell Kingd8c96082014-04-22 11:10:15 +01001246
1247static int
1248armada_lcd_bind(struct device *dev, struct device *master, void *data)
1249{
1250 struct platform_device *pdev = to_platform_device(dev);
1251 struct drm_device *drm = data;
1252 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1253 int irq = platform_get_irq(pdev, 0);
1254 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001255 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001256
1257 if (irq < 0)
1258 return irq;
1259
1260 if (!dev->of_node) {
1261 const struct platform_device_id *id;
1262
1263 id = platform_get_device_id(pdev);
1264 if (!id)
1265 return -ENXIO;
1266
1267 variant = (const struct armada_variant *)id->driver_data;
1268 } else {
1269 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001270 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001271
1272 match = of_match_device(dev->driver->of_match_table, dev);
1273 if (!match)
1274 return -ENXIO;
1275
Russell King9611cb92014-06-15 11:21:23 +01001276 np = of_get_child_by_name(parent, "ports");
1277 if (np)
1278 parent = np;
1279 port = of_get_child_by_name(parent, "port");
1280 of_node_put(np);
1281 if (!port) {
1282 dev_err(dev, "no port node found in %s\n",
1283 parent->full_name);
1284 return -ENXIO;
1285 }
1286
Russell Kingd8c96082014-04-22 11:10:15 +01001287 variant = match->data;
1288 }
1289
Russell King9611cb92014-06-15 11:21:23 +01001290 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001291}
1292
1293static void
1294armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1295{
1296 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1297
1298 armada_drm_crtc_destroy(&dcrtc->crtc);
1299}
1300
1301static const struct component_ops armada_lcd_ops = {
1302 .bind = armada_lcd_bind,
1303 .unbind = armada_lcd_unbind,
1304};
1305
1306static int armada_lcd_probe(struct platform_device *pdev)
1307{
1308 return component_add(&pdev->dev, &armada_lcd_ops);
1309}
1310
1311static int armada_lcd_remove(struct platform_device *pdev)
1312{
1313 component_del(&pdev->dev, &armada_lcd_ops);
1314 return 0;
1315}
1316
1317static struct of_device_id armada_lcd_of_match[] = {
1318 {
1319 .compatible = "marvell,dove-lcd",
1320 .data = &armada510_ops,
1321 },
1322 {}
1323};
1324MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1325
1326static const struct platform_device_id armada_lcd_platform_ids[] = {
1327 {
1328 .name = "armada-lcd",
1329 .driver_data = (unsigned long)&armada510_ops,
1330 }, {
1331 .name = "armada-510-lcd",
1332 .driver_data = (unsigned long)&armada510_ops,
1333 },
1334 { },
1335};
1336MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1337
1338struct platform_driver armada_lcd_platform_driver = {
1339 .probe = armada_lcd_probe,
1340 .remove = armada_lcd_remove,
1341 .driver = {
1342 .name = "armada-lcd",
1343 .owner = THIS_MODULE,
1344 .of_match_table = armada_lcd_of_match,
1345 },
1346 .id_table = armada_lcd_platform_ids,
1347};