blob: f10ab0275ce7a2de169a0f20ba1eb46360eabe59 [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
Russell Kingf0b24872016-08-16 22:09:11 +0100168void armada_drm_plane_calc_addrs(u32 *addrs, struct drm_framebuffer *fb,
169 int x, int y)
170{
Russell Kingd6a48962017-12-08 12:16:22 +0000171 const struct drm_format_info *format = fb->format;
172 unsigned int num_planes = format->num_planes;
Russell Kingf0b24872016-08-16 22:09:11 +0100173 u32 addr = drm_fb_obj(fb)->dev_addr;
Russell Kingf0b24872016-08-16 22:09:11 +0100174 int i;
175
176 if (num_planes > 3)
177 num_planes = 3;
178
Russell Kingde0ea9a2017-12-08 12:16:22 +0000179 addrs[0] = addr + fb->offsets[0] + y * fb->pitches[0] +
180 x * format->cpp[0];
181
182 y /= format->vsub;
183 x /= format->hsub;
184
185 for (i = 1; i < num_planes; i++)
Russell Kingf0b24872016-08-16 22:09:11 +0100186 addrs[i] = addr + fb->offsets[i] + y * fb->pitches[i] +
Russell Kingd6a48962017-12-08 12:16:22 +0000187 x * format->cpp[i];
Russell Kingf0b24872016-08-16 22:09:11 +0100188 for (; i < 3; i++)
189 addrs[i] = 0;
190}
191
Russell King96f60e32012-08-15 13:59:49 +0100192static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
193 int x, int y, struct armada_regs *regs, bool interlaced)
194{
Russell King96f60e32012-08-15 13:59:49 +0100195 unsigned pitch = fb->pitches[0];
Russell Kingf0b24872016-08-16 22:09:11 +0100196 u32 addrs[3], addr_odd, addr_even;
Russell King96f60e32012-08-15 13:59:49 +0100197 unsigned i = 0;
198
199 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
Ville Syrjälä272725c2016-12-14 23:32:20 +0200200 pitch, x, y, fb->format->cpp[0] * 8);
Russell King96f60e32012-08-15 13:59:49 +0100201
Russell Kingf0b24872016-08-16 22:09:11 +0100202 armada_drm_plane_calc_addrs(addrs, fb, x, y);
203
204 addr_odd = addr_even = addrs[0];
Russell King96f60e32012-08-15 13:59:49 +0100205
206 if (interlaced) {
207 addr_even += pitch;
208 pitch *= 2;
209 }
210
211 /* write offset, base, and pitch */
212 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
213 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
214 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
215
216 return i;
217}
218
Russell King2839d452017-07-07 15:56:20 +0100219static void armada_drm_plane_work_call(struct armada_crtc *dcrtc,
220 struct armada_plane_work *work,
221 void (*fn)(struct armada_crtc *, struct armada_plane_work *))
222{
223 struct armada_plane *dplane = drm_to_armada_plane(work->plane);
224
225 if (fn)
226 fn(dcrtc, work);
227 drm_crtc_vblank_put(&dcrtc->crtc);
228
229 wake_up(&dplane->frame_wait);
230}
231
Russell King4b5dda82015-08-06 16:37:18 +0100232static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
Russell Kingec6fb152016-07-25 15:16:11 +0100233 struct drm_plane *plane)
Russell King4b5dda82015-08-06 16:37:18 +0100234{
Russell Kingec6fb152016-07-25 15:16:11 +0100235 struct armada_plane *dplane = drm_to_armada_plane(plane);
236 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King4b5dda82015-08-06 16:37:18 +0100237
238 /* Handle any pending frame work. */
Russell King2839d452017-07-07 15:56:20 +0100239 if (work)
240 armada_drm_plane_work_call(dcrtc, work, work->fn);
Russell King4b5dda82015-08-06 16:37:18 +0100241}
242
243int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
Russell Kingeaab0132017-07-07 15:55:53 +0100244 struct armada_plane_work *work)
Russell King4b5dda82015-08-06 16:37:18 +0100245{
Russell Kingeaab0132017-07-07 15:55:53 +0100246 struct armada_plane *plane = drm_to_armada_plane(work->plane);
Russell King4b5dda82015-08-06 16:37:18 +0100247 int ret;
248
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300249 ret = drm_crtc_vblank_get(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100250 if (ret) {
251 DRM_ERROR("failed to acquire vblank counter\n");
252 return ret;
253 }
254
255 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
256 if (ret)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300257 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100258
259 return ret;
260}
261
262int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
263{
264 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
265}
266
Russell Kingd3b84212017-07-07 15:55:40 +0100267void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc,
268 struct armada_plane *dplane)
Russell King7c8f7e12015-06-29 17:52:16 +0100269{
Russell Kingd3b84212017-07-07 15:55:40 +0100270 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King7c8f7e12015-06-29 17:52:16 +0100271
Russell King4a8506d2015-08-07 09:33:05 +0100272 if (work)
Russell King2839d452017-07-07 15:56:20 +0100273 armada_drm_plane_work_call(dcrtc, work, work->cancel);
Russell King7c8f7e12015-06-29 17:52:16 +0100274}
275
Russell King709ffd82015-07-15 18:09:38 +0100276static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
Russell Kingeaab0132017-07-07 15:55:53 +0100277 struct armada_plane_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100278{
Russell King4b5dda82015-08-06 16:37:18 +0100279 struct armada_frame_work *fwork = container_of(work, struct armada_frame_work, work);
Russell King96f60e32012-08-15 13:59:49 +0100280 struct drm_device *dev = dcrtc->crtc.dev;
Russell King709ffd82015-07-15 18:09:38 +0100281 unsigned long flags;
Russell King96f60e32012-08-15 13:59:49 +0100282
Russell King709ffd82015-07-15 18:09:38 +0100283 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell King4b5dda82015-08-06 16:37:18 +0100284 armada_drm_crtc_update_regs(dcrtc, fwork->regs);
Russell King709ffd82015-07-15 18:09:38 +0100285 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100286
Russell King4b5dda82015-08-06 16:37:18 +0100287 if (fwork->event) {
Russell King709ffd82015-07-15 18:09:38 +0100288 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovandd54b802016-06-06 11:41:33 -0300289 drm_crtc_send_vblank_event(&dcrtc->crtc, fwork->event);
Russell King709ffd82015-07-15 18:09:38 +0100290 spin_unlock_irqrestore(&dev->event_lock, flags);
291 }
Russell King96f60e32012-08-15 13:59:49 +0100292
Russell King96f60e32012-08-15 13:59:49 +0100293 /* Finally, queue the process-half of the cleanup. */
Russell King4b5dda82015-08-06 16:37:18 +0100294 __armada_drm_queue_unref_work(dcrtc->crtc.dev, fwork->old_fb);
295 kfree(fwork);
Russell King96f60e32012-08-15 13:59:49 +0100296}
297
Russell Kingeaab0132017-07-07 15:55:53 +0100298static struct armada_frame_work *
299armada_drm_crtc_alloc_frame_work(struct drm_plane *plane)
Russell King901bb882017-07-07 15:55:45 +0100300{
301 struct armada_frame_work *work;
302 int i = 0;
303
304 work = kzalloc(sizeof(*work), GFP_KERNEL);
305 if (!work)
306 return NULL;
307
Russell Kingeaab0132017-07-07 15:55:53 +0100308 work->work.plane = plane;
Russell King901bb882017-07-07 15:55:45 +0100309 work->work.fn = armada_drm_crtc_complete_frame_work;
310 armada_reg_queue_end(work->regs, i);
311
312 return work;
313}
314
Russell King96f60e32012-08-15 13:59:49 +0100315static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
316 struct drm_framebuffer *fb, bool force)
317{
318 struct armada_frame_work *work;
319
320 if (!fb)
321 return;
322
323 if (force) {
324 /* Display is disabled, so just drop the old fb */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600325 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100326 return;
327 }
328
Russell Kingeaab0132017-07-07 15:55:53 +0100329 work = armada_drm_crtc_alloc_frame_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100330 if (work) {
Russell King96f60e32012-08-15 13:59:49 +0100331 work->old_fb = fb;
Russell King96f60e32012-08-15 13:59:49 +0100332
Russell Kingeaab0132017-07-07 15:55:53 +0100333 if (armada_drm_plane_work_queue(dcrtc, work) == 0)
Russell King96f60e32012-08-15 13:59:49 +0100334 return;
335
336 kfree(work);
337 }
338
339 /*
340 * Oops - just drop the reference immediately and hope for
341 * the best. The worst that will happen is the buffer gets
342 * reused before it has finished being displayed.
343 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600344 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100345}
346
347static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
348{
Russell King96f60e32012-08-15 13:59:49 +0100349 /*
350 * Tell the DRM core that vblank IRQs aren't going to happen for
351 * a while. This cleans up any pending vblank events for us.
352 */
Russell King178e5612014-10-11 23:57:04 +0100353 drm_crtc_vblank_off(&dcrtc->crtc);
Russell Kingec6fb152016-07-25 15:16:11 +0100354 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100355}
356
Russell King96f60e32012-08-15 13:59:49 +0100357/* The mode_config.mutex will be held for this call */
358static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
359{
360 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
361
Russell Kingea908ba2016-10-04 22:19:57 +0100362 if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
Russell King96f60e32012-08-15 13:59:49 +0100363 if (dpms_blanked(dpms))
364 armada_drm_vblank_off(dcrtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100365 else if (!IS_ERR(dcrtc->clk))
366 WARN_ON(clk_prepare_enable(dcrtc->clk));
367 dcrtc->dpms = dpms;
368 armada_drm_crtc_update(dcrtc);
369 if (!dpms_blanked(dpms))
Russell King178e5612014-10-11 23:57:04 +0100370 drm_crtc_vblank_on(&dcrtc->crtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100371 else if (!IS_ERR(dcrtc->clk))
372 clk_disable_unprepare(dcrtc->clk);
373 } else if (dcrtc->dpms != dpms) {
374 dcrtc->dpms = dpms;
Russell King96f60e32012-08-15 13:59:49 +0100375 }
376}
377
378/*
379 * Prepare for a mode set. Turn off overlay to ensure that we don't end
380 * up with the overlay size being bigger than the active screen size.
381 * We rely upon X refreshing this state after the mode set has completed.
382 *
383 * The mode_config.mutex will be held for this call
384 */
385static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
386{
387 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
388 struct drm_plane *plane;
389
390 /*
391 * If we have an overlay plane associated with this CRTC, disable
392 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100393 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100394 */
395 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100396 if (plane)
397 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100398}
399
400/* The mode_config.mutex will be held for this call */
401static void armada_drm_crtc_commit(struct drm_crtc *crtc)
402{
403 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
404
405 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
406 dcrtc->dpms = DRM_MODE_DPMS_ON;
407 armada_drm_crtc_update(dcrtc);
408 }
409}
410
411/* The mode_config.mutex will be held for this call */
412static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
413 const struct drm_display_mode *mode, struct drm_display_mode *adj)
414{
Russell King96f60e32012-08-15 13:59:49 +0100415 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
416 int ret;
417
418 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100419 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100420 adj->flags & DRM_MODE_FLAG_INTERLACE)
421 return false;
422
423 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100424 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100425 if (ret)
426 return false;
427
428 return true;
429}
430
Shawn Guo5922a7d2017-02-07 17:16:18 +0800431/* These are locked by dev->vbl_lock */
432static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
433{
434 if (dcrtc->irq_ena & mask) {
435 dcrtc->irq_ena &= ~mask;
436 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
437 }
438}
439
440static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
441{
442 if ((dcrtc->irq_ena & mask) != mask) {
443 dcrtc->irq_ena |= mask;
444 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
445 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
446 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
447 }
448}
449
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100450static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100451{
Russell King96f60e32012-08-15 13:59:49 +0100452 void __iomem *base = dcrtc->base;
Russell King4a8506d2015-08-07 09:33:05 +0100453 struct drm_plane *ovl_plane;
Russell King96f60e32012-08-15 13:59:49 +0100454
455 if (stat & DMA_FF_UNDERFLOW)
456 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
457 if (stat & GRA_FF_UNDERFLOW)
458 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
459
460 if (stat & VSYNC_IRQ)
Gustavo Padovan0ac28c52016-07-04 21:04:48 -0300461 drm_crtc_handle_vblank(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100462
463 spin_lock(&dcrtc->irq_lock);
Russell King4a8506d2015-08-07 09:33:05 +0100464 ovl_plane = dcrtc->plane;
Russell Kingec6fb152016-07-25 15:16:11 +0100465 if (ovl_plane)
466 armada_drm_plane_work_run(dcrtc, ovl_plane);
Russell King96f60e32012-08-15 13:59:49 +0100467
468 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
469 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
470 uint32_t val;
471
472 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
473 writel_relaxed(dcrtc->v[i].spu_v_h_total,
474 base + LCD_SPUT_V_H_TOTAL);
475
476 val = readl_relaxed(base + LCD_SPU_ADV_REG);
477 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
478 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100479 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100480 }
Russell King662af0d2013-05-19 10:55:17 +0100481
482 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
483 writel_relaxed(dcrtc->cursor_hw_pos,
484 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
485 writel_relaxed(dcrtc->cursor_hw_sz,
486 base + LCD_SPU_HWC_HPXL_VLN);
487 armada_updatel(CFG_HWC_ENA,
488 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
489 base + LCD_SPU_DMA_CTRL0);
490 dcrtc->cursor_update = false;
491 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
492 }
493
Russell King96f60e32012-08-15 13:59:49 +0100494 spin_unlock(&dcrtc->irq_lock);
495
Russell Kingec6fb152016-07-25 15:16:11 +0100496 if (stat & GRA_FRAME_IRQ)
497 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100498}
499
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100500static irqreturn_t armada_drm_irq(int irq, void *arg)
501{
502 struct armada_crtc *dcrtc = arg;
503 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
504
505 /*
506 * This is rediculous - rather than writing bits to clear, we
507 * have to set the actual status register value. This is racy.
508 */
509 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
510
Russell Kingc8a220c2016-05-17 13:51:08 +0100511 trace_armada_drm_irq(&dcrtc->crtc, stat);
512
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100513 /* Mask out those interrupts we haven't enabled */
514 v = stat & dcrtc->irq_ena;
515
516 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
517 armada_drm_crtc_irq(dcrtc, stat);
518 return IRQ_HANDLED;
519 }
520 return IRQ_NONE;
521}
522
Russell King96f60e32012-08-15 13:59:49 +0100523static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
524{
525 struct drm_display_mode *adj = &dcrtc->crtc.mode;
526 uint32_t val = 0;
527
528 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
529 val |= CFG_CSC_YUV_CCIR709;
530 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
531 val |= CFG_CSC_RGB_STUDIO;
532
533 /*
534 * In auto mode, set the colorimetry, based upon the HDMI spec.
535 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
536 * ITU601. It may be more appropriate to set this depending on
537 * the source - but what if the graphic frame is YUV and the
538 * video frame is RGB?
539 */
540 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
541 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
542 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
543 if (dcrtc->csc_yuv_mode == CSC_AUTO)
544 val |= CFG_CSC_YUV_CCIR709;
545 }
546
547 /*
548 * We assume we're connected to a TV-like device, so the YUV->RGB
549 * conversion should produce a limited range. We should set this
550 * depending on the connectors attached to this CRTC, and what
551 * kind of device they report being connected.
552 */
553 if (dcrtc->csc_rgb_mode == CSC_AUTO)
554 val |= CFG_CSC_RGB_STUDIO;
555
556 return val;
557}
558
Russell King37af35c2016-08-16 22:09:09 +0100559static void armada_drm_primary_set(struct drm_crtc *crtc,
560 struct drm_plane *plane, int x, int y)
561{
562 struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
563 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King2925db02016-08-16 22:09:10 +0100564 struct armada_regs regs[8];
Russell King37af35c2016-08-16 22:09:09 +0100565 bool interlaced = dcrtc->interlaced;
566 unsigned i;
Russell King2925db02016-08-16 22:09:10 +0100567 u32 ctrl0;
Russell King37af35c2016-08-16 22:09:09 +0100568
569 i = armada_drm_crtc_calc_fb(plane->fb, x, y, regs, interlaced);
570
Russell King2925db02016-08-16 22:09:10 +0100571 armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell King37af35c2016-08-16 22:09:09 +0100572 armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
573 armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
574
575 ctrl0 = state->ctrl0;
576 if (interlaced)
577 ctrl0 |= CFG_GRA_FTOGGLE;
578
579 armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
580 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
581 CFG_SWAPYU | CFG_YUV2RGB) |
582 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
583 LCD_SPU_DMA_CTRL0);
584 armada_reg_queue_end(regs, i);
585 armada_drm_crtc_update_regs(dcrtc, regs);
586}
587
Russell King96f60e32012-08-15 13:59:49 +0100588/* The mode_config.mutex will be held for this call */
589static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
590 struct drm_display_mode *mode, struct drm_display_mode *adj,
591 int x, int y, struct drm_framebuffer *old_fb)
592{
Russell King96f60e32012-08-15 13:59:49 +0100593 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
594 struct armada_regs regs[17];
595 uint32_t lm, rm, tm, bm, val, sclk;
596 unsigned long flags;
597 unsigned i;
598 bool interlaced;
599
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600600 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100601
602 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
603
Russell King8be523d2016-08-16 22:09:08 +0100604 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
605 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
606 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100607
Russell King8be523d2016-08-16 22:09:08 +0100608 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
609 val |= CFG_PALETTE_ENA;
610
611 drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
612 drm_to_armada_plane(crtc->primary)->state.src_hw =
613 drm_to_armada_plane(crtc->primary)->state.dst_hw =
Russell King37af35c2016-08-16 22:09:09 +0100614 adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
Russell King8be523d2016-08-16 22:09:08 +0100615 drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
616
Russell King37af35c2016-08-16 22:09:09 +0100617 i = 0;
Russell King96f60e32012-08-15 13:59:49 +0100618 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
619 lm = adj->crtc_htotal - adj->crtc_hsync_end;
620 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
621 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
622
623 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
624 adj->crtc_hdisplay,
625 adj->crtc_hsync_start,
626 adj->crtc_hsync_end,
627 adj->crtc_htotal, lm, rm);
628 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
629 adj->crtc_vdisplay,
630 adj->crtc_vsync_start,
631 adj->crtc_vsync_end,
632 adj->crtc_vtotal, tm, bm);
633
634 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100635 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
636 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100637
Russell King178e5612014-10-11 23:57:04 +0100638 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100639
Russell King96f60e32012-08-15 13:59:49 +0100640 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
641 if (val != dcrtc->dumb_ctrl) {
642 dcrtc->dumb_ctrl = val;
643 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
644 }
645
Russell Kinge0ac5e92015-06-29 18:01:38 +0100646 /*
647 * If we are blanked, we would have disabled the clock. Re-enable
648 * it so that compute_clock() does the right thing.
649 */
650 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
651 WARN_ON(clk_prepare_enable(dcrtc->clk));
652
Russell King96f60e32012-08-15 13:59:49 +0100653 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100654 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100655
656 /* Ensure graphic fifo is enabled */
657 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
658 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
659
660 if (interlaced ^ dcrtc->interlaced) {
661 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300662 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100663 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300664 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100665 dcrtc->interlaced = interlaced;
666 }
667
668 spin_lock_irqsave(&dcrtc->irq_lock, flags);
669
670 /* Even interlaced/progressive frame */
671 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
672 adj->crtc_htotal;
673 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
674 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100675 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100676 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100677
678 if (interlaced) {
679 /* Odd interlaced frame */
680 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
681 (1 << 16);
682 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
683 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100684 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100685 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100686 } else {
687 dcrtc->v[0] = dcrtc->v[1];
688 }
689
690 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
691
692 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King96f60e32012-08-15 13:59:49 +0100693 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
694 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
695 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
696 LCD_SPUT_V_H_TOTAL);
697
Russell King42e62ba2014-04-22 15:24:03 +0100698 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100699 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
700 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
701 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100702 }
Russell King96f60e32012-08-15 13:59:49 +0100703
Russell King96f60e32012-08-15 13:59:49 +0100704 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
705 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
706
707 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
708 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
709 armada_reg_queue_end(regs, i);
710
711 armada_drm_crtc_update_regs(dcrtc, regs);
Russell King37af35c2016-08-16 22:09:09 +0100712
713 armada_drm_primary_set(crtc, crtc->primary, x, y);
Russell King96f60e32012-08-15 13:59:49 +0100714 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
715
716 armada_drm_crtc_update(dcrtc);
717
Russell King178e5612014-10-11 23:57:04 +0100718 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100719 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
720
721 return 0;
722}
723
724/* The mode_config.mutex will be held for this call */
725static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
726 struct drm_framebuffer *old_fb)
727{
728 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
729 struct armada_regs regs[4];
730 unsigned i;
731
Matt Roperf4510a22014-04-01 15:22:40 -0700732 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100733 dcrtc->interlaced);
734 armada_reg_queue_end(regs, i);
735
736 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100737 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
738 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100739
740 /* Take a reference to the new fb as we're using it */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600741 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100742
743 /* Update the base in the CRTC */
744 armada_drm_crtc_update_regs(dcrtc, regs);
745
746 /* Drop our previously held reference */
747 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
748
749 return 0;
750}
751
Russell King58326802015-07-15 18:11:25 +0100752void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
753 struct drm_plane *plane)
754{
Russell King9099ea12015-07-15 18:11:25 +0100755 u32 sram_para1, dma_ctrl0_mask;
Russell King58326802015-07-15 18:11:25 +0100756
757 /*
758 * Drop our reference on any framebuffer attached to this plane.
759 * We don't need to NULL this out as drm_plane_force_disable(),
760 * and __setplane_internal() will do so for an overlay plane, and
761 * __drm_helper_disable_unused_functions() will do so for the
762 * primary plane.
763 */
764 if (plane->fb)
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600765 drm_framebuffer_put(plane->fb);
Russell King58326802015-07-15 18:11:25 +0100766
Russell King58326802015-07-15 18:11:25 +0100767 /* Power down most RAMs and FIFOs if this is the primary plane */
Russell King9099ea12015-07-15 18:11:25 +0100768 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
Russell King2bf57432017-12-08 12:16:22 +0000769 sram_para1 = CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
770 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell King9099ea12015-07-15 18:11:25 +0100771 dma_ctrl0_mask = CFG_GRA_ENA;
772 } else {
Russell King2bf57432017-12-08 12:16:22 +0000773 /* Power down the Y/U/V FIFOs */
774 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
Russell King9099ea12015-07-15 18:11:25 +0100775 dma_ctrl0_mask = CFG_DMA_ENA;
776 }
777
778 spin_lock_irq(&dcrtc->irq_lock);
779 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
780 spin_unlock_irq(&dcrtc->irq_lock);
Russell King58326802015-07-15 18:11:25 +0100781
782 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
783}
784
Russell King96f60e32012-08-15 13:59:49 +0100785/* The mode_config.mutex will be held for this call */
786static void armada_drm_crtc_disable(struct drm_crtc *crtc)
787{
788 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
789
790 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King58326802015-07-15 18:11:25 +0100791 armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
Russell King96f60e32012-08-15 13:59:49 +0100792}
793
794static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
795 .dpms = armada_drm_crtc_dpms,
796 .prepare = armada_drm_crtc_prepare,
797 .commit = armada_drm_crtc_commit,
798 .mode_fixup = armada_drm_crtc_mode_fixup,
799 .mode_set = armada_drm_crtc_mode_set,
800 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100801 .disable = armada_drm_crtc_disable,
802};
803
Russell King662af0d2013-05-19 10:55:17 +0100804static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
805 unsigned stride, unsigned width, unsigned height)
806{
807 uint32_t addr;
808 unsigned y;
809
810 addr = SRAM_HWC32_RAM1;
811 for (y = 0; y < height; y++) {
812 uint32_t *p = &pix[y * stride];
813 unsigned x;
814
815 for (x = 0; x < width; x++, p++) {
816 uint32_t val = *p;
817
818 val = (val & 0xff00ff00) |
819 (val & 0x000000ff) << 16 |
820 (val & 0x00ff0000) >> 16;
821
822 writel_relaxed(val,
823 base + LCD_SPU_SRAM_WRDAT);
824 writel_relaxed(addr | SRAM_WRITE,
825 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100826 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100827 addr += 1;
828 if ((addr & 0x00ff) == 0)
829 addr += 0xf00;
830 if ((addr & 0x30ff) == 0)
831 addr = SRAM_HWC32_RAM2;
832 }
833 }
834}
835
836static void armada_drm_crtc_cursor_tran(void __iomem *base)
837{
838 unsigned addr;
839
840 for (addr = 0; addr < 256; addr++) {
841 /* write the default value */
842 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
843 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
844 base + LCD_SPU_SRAM_CTRL);
845 }
846}
847
848static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
849{
850 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
851 uint32_t yoff, yscr, h = dcrtc->cursor_h;
852 uint32_t para1;
853
854 /*
855 * Calculate the visible width and height of the cursor,
856 * screen position, and the position in the cursor bitmap.
857 */
858 if (dcrtc->cursor_x < 0) {
859 xoff = -dcrtc->cursor_x;
860 xscr = 0;
861 w -= min(xoff, w);
862 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
863 xoff = 0;
864 xscr = dcrtc->cursor_x;
865 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
866 } else {
867 xoff = 0;
868 xscr = dcrtc->cursor_x;
869 }
870
871 if (dcrtc->cursor_y < 0) {
872 yoff = -dcrtc->cursor_y;
873 yscr = 0;
874 h -= min(yoff, h);
875 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
876 yoff = 0;
877 yscr = dcrtc->cursor_y;
878 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
879 } else {
880 yoff = 0;
881 yscr = dcrtc->cursor_y;
882 }
883
884 /* On interlaced modes, the vertical cursor size must be halved */
885 s = dcrtc->cursor_w;
886 if (dcrtc->interlaced) {
887 s *= 2;
888 yscr /= 2;
889 h /= 2;
890 }
891
892 if (!dcrtc->cursor_obj || !h || !w) {
893 spin_lock_irq(&dcrtc->irq_lock);
894 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
895 dcrtc->cursor_update = false;
896 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
897 spin_unlock_irq(&dcrtc->irq_lock);
898 return 0;
899 }
900
901 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
902 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
903 dcrtc->base + LCD_SPU_SRAM_PARA1);
904
905 /*
906 * Initialize the transparency if the SRAM was powered down.
907 * We must also reload the cursor data as well.
908 */
909 if (!(para1 & CFG_CSB_256x32)) {
910 armada_drm_crtc_cursor_tran(dcrtc->base);
911 reload = true;
912 }
913
914 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
915 spin_lock_irq(&dcrtc->irq_lock);
916 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
917 dcrtc->cursor_update = false;
918 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
919 spin_unlock_irq(&dcrtc->irq_lock);
920 reload = true;
921 }
922 if (reload) {
923 struct armada_gem_object *obj = dcrtc->cursor_obj;
924 uint32_t *pix;
925 /* Set the top-left corner of the cursor image */
926 pix = obj->addr;
927 pix += yoff * s + xoff;
928 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
929 }
930
931 /* Reload the cursor position, size and enable in the IRQ handler */
932 spin_lock_irq(&dcrtc->irq_lock);
933 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
934 dcrtc->cursor_hw_sz = h << 16 | w;
935 dcrtc->cursor_update = true;
936 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
937 spin_unlock_irq(&dcrtc->irq_lock);
938
939 return 0;
940}
941
942static void cursor_update(void *data)
943{
944 armada_drm_crtc_cursor_update(data, true);
945}
946
947static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
948 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
949{
Russell King662af0d2013-05-19 10:55:17 +0100950 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100951 struct armada_gem_object *obj = NULL;
952 int ret;
953
954 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100955 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100956 return -ENXIO;
957
958 if (handle && w > 0 && h > 0) {
959 /* maximum size is 64x32 or 32x64 */
960 if (w > 64 || h > 64 || (w > 32 && h > 32))
961 return -ENOMEM;
962
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100963 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100964 if (!obj)
965 return -ENOENT;
966
967 /* Must be a kernel-mapped object */
968 if (!obj->addr) {
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600969 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100970 return -EINVAL;
971 }
972
973 if (obj->obj.size < w * h * 4) {
974 DRM_ERROR("buffer is too small\n");
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600975 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100976 return -ENOMEM;
977 }
978 }
979
Russell King662af0d2013-05-19 10:55:17 +0100980 if (dcrtc->cursor_obj) {
981 dcrtc->cursor_obj->update = NULL;
982 dcrtc->cursor_obj->update_data = NULL;
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600983 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100984 }
985 dcrtc->cursor_obj = obj;
986 dcrtc->cursor_w = w;
987 dcrtc->cursor_h = h;
988 ret = armada_drm_crtc_cursor_update(dcrtc, true);
989 if (obj) {
990 obj->update_data = dcrtc;
991 obj->update = cursor_update;
992 }
Russell King662af0d2013-05-19 10:55:17 +0100993
994 return ret;
995}
996
997static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
998{
Russell King662af0d2013-05-19 10:55:17 +0100999 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +01001000 int ret;
1001
1002 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +01001003 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +01001004 return -EFAULT;
1005
Russell King662af0d2013-05-19 10:55:17 +01001006 dcrtc->cursor_x = x;
1007 dcrtc->cursor_y = y;
1008 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +01001009
1010 return ret;
1011}
1012
Russell King96f60e32012-08-15 13:59:49 +01001013static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
1014{
1015 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1016 struct armada_private *priv = crtc->dev->dev_private;
1017
Russell King662af0d2013-05-19 10:55:17 +01001018 if (dcrtc->cursor_obj)
Haneen Mohammed4c3cf372017-09-20 12:54:48 -06001019 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +01001020
Russell King96f60e32012-08-15 13:59:49 +01001021 priv->dcrtc[dcrtc->num] = NULL;
1022 drm_crtc_cleanup(&dcrtc->crtc);
1023
1024 if (!IS_ERR(dcrtc->clk))
1025 clk_disable_unprepare(dcrtc->clk);
1026
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001027 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1028
Russell King9611cb92014-06-15 11:21:23 +01001029 of_node_put(dcrtc->crtc.port);
1030
Russell King96f60e32012-08-15 13:59:49 +01001031 kfree(dcrtc);
1032}
1033
1034/*
1035 * The mode_config lock is held here, to prevent races between this
1036 * and a mode_set.
1037 */
1038static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01001039 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags,
1040 struct drm_modeset_acquire_ctx *ctx)
Russell King96f60e32012-08-15 13:59:49 +01001041{
1042 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1043 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001044 unsigned i;
1045 int ret;
1046
1047 /* We don't support changing the pixel format */
Ville Syrjälädbd4d572016-11-18 21:53:10 +02001048 if (fb->format != crtc->primary->fb->format)
Russell King96f60e32012-08-15 13:59:49 +01001049 return -EINVAL;
1050
Russell Kingeaab0132017-07-07 15:55:53 +01001051 work = armada_drm_crtc_alloc_frame_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001052 if (!work)
1053 return -ENOMEM;
1054
1055 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -07001056 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001057
1058 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1059 dcrtc->interlaced);
1060 armada_reg_queue_end(work->regs, i);
1061
1062 /*
Russell Kingc5488302014-10-11 23:53:35 +01001063 * Ensure that we hold a reference on the new framebuffer.
1064 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001065 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001066 drm_framebuffer_get(fb);
Russell King96f60e32012-08-15 13:59:49 +01001067
Russell Kingeaab0132017-07-07 15:55:53 +01001068 ret = armada_drm_plane_work_queue(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +01001069 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001070 /* Undo our reference above */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001071 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +01001072 kfree(work);
1073 return ret;
1074 }
1075
1076 /*
1077 * Don't take a reference on the new framebuffer;
1078 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1079 * will _not_ drop that reference on successful return from this
1080 * function. Simply mark this new framebuffer as the current one.
1081 */
Matt Roperf4510a22014-04-01 15:22:40 -07001082 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001083
1084 /*
1085 * Finally, if the display is blanked, we won't receive an
1086 * interrupt, so complete it now.
1087 */
Russell King4b5dda82015-08-06 16:37:18 +01001088 if (dpms_blanked(dcrtc->dpms))
Russell Kingec6fb152016-07-25 15:16:11 +01001089 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001090
1091 return 0;
1092}
1093
1094static int
1095armada_drm_crtc_set_property(struct drm_crtc *crtc,
1096 struct drm_property *property, uint64_t val)
1097{
1098 struct armada_private *priv = crtc->dev->dev_private;
1099 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1100 bool update_csc = false;
1101
1102 if (property == priv->csc_yuv_prop) {
1103 dcrtc->csc_yuv_mode = val;
1104 update_csc = true;
1105 } else if (property == priv->csc_rgb_prop) {
1106 dcrtc->csc_rgb_mode = val;
1107 update_csc = true;
1108 }
1109
1110 if (update_csc) {
1111 uint32_t val;
1112
1113 val = dcrtc->spu_iopad_ctrl |
1114 armada_drm_crtc_calculate_csc(dcrtc);
1115 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1116 }
1117
1118 return 0;
1119}
1120
Shawn Guo5922a7d2017-02-07 17:16:18 +08001121/* These are called under the vbl_lock. */
1122static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
1123{
1124 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1125
1126 armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
1127 return 0;
1128}
1129
1130static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
1131{
1132 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1133
1134 armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
1135}
1136
Ville Syrjäläa02fb902015-12-15 12:20:59 +01001137static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001138 .cursor_set = armada_drm_crtc_cursor_set,
1139 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001140 .destroy = armada_drm_crtc_destroy,
1141 .set_config = drm_crtc_helper_set_config,
1142 .page_flip = armada_drm_crtc_page_flip,
1143 .set_property = armada_drm_crtc_set_property,
Shawn Guo5922a7d2017-02-07 17:16:18 +08001144 .enable_vblank = armada_drm_crtc_enable_vblank,
1145 .disable_vblank = armada_drm_crtc_disable_vblank,
Russell King96f60e32012-08-15 13:59:49 +01001146};
1147
Russell Kingde323012015-07-15 18:11:24 +01001148static const struct drm_plane_funcs armada_primary_plane_funcs = {
1149 .update_plane = drm_primary_helper_update,
1150 .disable_plane = drm_primary_helper_disable,
1151 .destroy = drm_primary_helper_destroy,
1152};
1153
Russell King5740d272015-07-15 18:11:25 +01001154int armada_drm_plane_init(struct armada_plane *plane)
1155{
1156 init_waitqueue_head(&plane->frame_wait);
1157
1158 return 0;
1159}
1160
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301161static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001162 { CSC_AUTO, "Auto" },
1163 { CSC_YUV_CCIR601, "CCIR601" },
1164 { CSC_YUV_CCIR709, "CCIR709" },
1165};
1166
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301167static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001168 { CSC_AUTO, "Auto" },
1169 { CSC_RGB_COMPUTER, "Computer system" },
1170 { CSC_RGB_STUDIO, "Studio" },
1171};
1172
1173static int armada_drm_crtc_create_properties(struct drm_device *dev)
1174{
1175 struct armada_private *priv = dev->dev_private;
1176
1177 if (priv->csc_yuv_prop)
1178 return 0;
1179
1180 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1181 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1182 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1183 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1184 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1185 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1186
1187 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1188 return -ENOMEM;
1189
1190 return 0;
1191}
1192
Russell King0fb29702015-06-06 21:46:53 +01001193static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001194 struct resource *res, int irq, const struct armada_variant *variant,
1195 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001196{
Russell Kingd8c96082014-04-22 11:10:15 +01001197 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001198 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001199 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001200 void __iomem *base;
1201 int ret;
1202
Russell Kingd8c96082014-04-22 11:10:15 +01001203 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001204 if (ret)
1205 return ret;
1206
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001207 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001208 if (IS_ERR(base))
1209 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001210
1211 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1212 if (!dcrtc) {
1213 DRM_ERROR("failed to allocate Armada crtc\n");
1214 return -ENOMEM;
1215 }
1216
Russell Kingd8c96082014-04-22 11:10:15 +01001217 if (dev != drm->dev)
1218 dev_set_drvdata(dev, dcrtc);
1219
Russell King42e62ba2014-04-22 15:24:03 +01001220 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001221 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001222 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001223 dcrtc->clk = ERR_PTR(-EINVAL);
1224 dcrtc->csc_yuv_mode = CSC_AUTO;
1225 dcrtc->csc_rgb_mode = CSC_AUTO;
1226 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1227 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1228 spin_lock_init(&dcrtc->irq_lock);
1229 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +01001230
1231 /* Initialize some registers which we don't otherwise set */
1232 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1233 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1234 writel_relaxed(dcrtc->spu_iopad_ctrl,
1235 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1236 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1237 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1238 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1239 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1240 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001241 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1242 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001243
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001244 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1245 dcrtc);
Russell King33cd3c02017-12-08 12:16:22 +00001246 if (ret < 0)
1247 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001248
Russell King42e62ba2014-04-22 15:24:03 +01001249 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001250 ret = dcrtc->variant->init(dcrtc, dev);
Russell King33cd3c02017-12-08 12:16:22 +00001251 if (ret)
1252 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001253 }
1254
1255 /* Ensure AXI pipeline is enabled */
1256 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1257
1258 priv->dcrtc[dcrtc->num] = dcrtc;
1259
Russell King9611cb92014-06-15 11:21:23 +01001260 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001261
Russell Kingde323012015-07-15 18:11:24 +01001262 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King33cd3c02017-12-08 12:16:22 +00001263 if (!primary) {
1264 ret = -ENOMEM;
1265 goto err_crtc;
1266 }
Russell King1c914ce2015-07-15 18:11:24 +01001267
Russell King5740d272015-07-15 18:11:25 +01001268 ret = armada_drm_plane_init(primary);
1269 if (ret) {
1270 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001271 goto err_crtc;
Russell King5740d272015-07-15 18:11:25 +01001272 }
1273
Russell Kingde323012015-07-15 18:11:24 +01001274 ret = drm_universal_plane_init(drm, &primary->base, 0,
1275 &armada_primary_plane_funcs,
1276 armada_primary_formats,
1277 ARRAY_SIZE(armada_primary_formats),
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001278 NULL,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001279 DRM_PLANE_TYPE_PRIMARY, NULL);
Russell Kingde323012015-07-15 18:11:24 +01001280 if (ret) {
1281 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001282 goto err_crtc;
Russell Kingde323012015-07-15 18:11:24 +01001283 }
1284
1285 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001286 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001287 if (ret)
1288 goto err_crtc_init;
1289
Russell King96f60e32012-08-15 13:59:49 +01001290 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1291
1292 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1293 dcrtc->csc_yuv_mode);
1294 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1295 dcrtc->csc_rgb_mode);
1296
Russell Kingd8c96082014-04-22 11:10:15 +01001297 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001298
1299err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001300 primary->base.funcs->destroy(&primary->base);
Russell King33cd3c02017-12-08 12:16:22 +00001301err_crtc:
1302 kfree(dcrtc);
1303
Russell King1c914ce2015-07-15 18:11:24 +01001304 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001305}
Russell Kingd8c96082014-04-22 11:10:15 +01001306
1307static int
1308armada_lcd_bind(struct device *dev, struct device *master, void *data)
1309{
1310 struct platform_device *pdev = to_platform_device(dev);
1311 struct drm_device *drm = data;
1312 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1313 int irq = platform_get_irq(pdev, 0);
1314 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001315 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001316
1317 if (irq < 0)
1318 return irq;
1319
1320 if (!dev->of_node) {
1321 const struct platform_device_id *id;
1322
1323 id = platform_get_device_id(pdev);
1324 if (!id)
1325 return -ENXIO;
1326
1327 variant = (const struct armada_variant *)id->driver_data;
1328 } else {
1329 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001330 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001331
1332 match = of_match_device(dev->driver->of_match_table, dev);
1333 if (!match)
1334 return -ENXIO;
1335
Russell King9611cb92014-06-15 11:21:23 +01001336 np = of_get_child_by_name(parent, "ports");
1337 if (np)
1338 parent = np;
1339 port = of_get_child_by_name(parent, "port");
1340 of_node_put(np);
1341 if (!port) {
Rob Herring4bf99142017-07-18 16:43:04 -05001342 dev_err(dev, "no port node found in %pOF\n", parent);
Russell King9611cb92014-06-15 11:21:23 +01001343 return -ENXIO;
1344 }
1345
Russell Kingd8c96082014-04-22 11:10:15 +01001346 variant = match->data;
1347 }
1348
Russell King9611cb92014-06-15 11:21:23 +01001349 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001350}
1351
1352static void
1353armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1354{
1355 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1356
1357 armada_drm_crtc_destroy(&dcrtc->crtc);
1358}
1359
1360static const struct component_ops armada_lcd_ops = {
1361 .bind = armada_lcd_bind,
1362 .unbind = armada_lcd_unbind,
1363};
1364
1365static int armada_lcd_probe(struct platform_device *pdev)
1366{
1367 return component_add(&pdev->dev, &armada_lcd_ops);
1368}
1369
1370static int armada_lcd_remove(struct platform_device *pdev)
1371{
1372 component_del(&pdev->dev, &armada_lcd_ops);
1373 return 0;
1374}
1375
Arvind Yadav85909712017-06-20 10:44:33 +05301376static const struct of_device_id armada_lcd_of_match[] = {
Russell Kingd8c96082014-04-22 11:10:15 +01001377 {
1378 .compatible = "marvell,dove-lcd",
1379 .data = &armada510_ops,
1380 },
1381 {}
1382};
1383MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1384
1385static const struct platform_device_id armada_lcd_platform_ids[] = {
1386 {
1387 .name = "armada-lcd",
1388 .driver_data = (unsigned long)&armada510_ops,
1389 }, {
1390 .name = "armada-510-lcd",
1391 .driver_data = (unsigned long)&armada510_ops,
1392 },
1393 { },
1394};
1395MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1396
1397struct platform_driver armada_lcd_platform_driver = {
1398 .probe = armada_lcd_probe,
1399 .remove = armada_lcd_remove,
1400 .driver = {
1401 .name = "armada-lcd",
1402 .owner = THIS_MODULE,
1403 .of_match_table = armada_lcd_of_match,
1404 },
1405 .id_table = armada_lcd_platform_ids,
1406};