blob: 3287b72e48cc3e3aafaac97f058846a3a25aa5e1 [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 King65724a12017-07-07 15:56:24 +0100276static void armada_drm_crtc_finish_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 King709ffd82015-07-15 18:09:38 +0100280 unsigned long flags;
Russell King96f60e32012-08-15 13:59:49 +0100281
Russell King4b5dda82015-08-06 16:37:18 +0100282 if (fwork->event) {
Russell King65724a12017-07-07 15:56:24 +0100283 struct drm_device *dev = dcrtc->crtc.dev;
284
Russell King709ffd82015-07-15 18:09:38 +0100285 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovandd54b802016-06-06 11:41:33 -0300286 drm_crtc_send_vblank_event(&dcrtc->crtc, fwork->event);
Russell King709ffd82015-07-15 18:09:38 +0100287 spin_unlock_irqrestore(&dev->event_lock, flags);
288 }
Russell King96f60e32012-08-15 13:59:49 +0100289
Russell King96f60e32012-08-15 13:59:49 +0100290 /* Finally, queue the process-half of the cleanup. */
Russell King4b5dda82015-08-06 16:37:18 +0100291 __armada_drm_queue_unref_work(dcrtc->crtc.dev, fwork->old_fb);
292 kfree(fwork);
Russell King96f60e32012-08-15 13:59:49 +0100293}
294
Russell King65724a12017-07-07 15:56:24 +0100295static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
296 struct armada_plane_work *work)
297{
298 struct armada_frame_work *fwork = container_of(work, struct armada_frame_work, work);
299 unsigned long flags;
300
301 spin_lock_irqsave(&dcrtc->irq_lock, flags);
302 armada_drm_crtc_update_regs(dcrtc, fwork->regs);
303 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
304
305 armada_drm_crtc_finish_frame_work(dcrtc, work);
306}
307
Russell Kingeaab0132017-07-07 15:55:53 +0100308static struct armada_frame_work *
309armada_drm_crtc_alloc_frame_work(struct drm_plane *plane)
Russell King901bb882017-07-07 15:55:45 +0100310{
311 struct armada_frame_work *work;
312 int i = 0;
313
314 work = kzalloc(sizeof(*work), GFP_KERNEL);
315 if (!work)
316 return NULL;
317
Russell Kingeaab0132017-07-07 15:55:53 +0100318 work->work.plane = plane;
Russell King901bb882017-07-07 15:55:45 +0100319 work->work.fn = armada_drm_crtc_complete_frame_work;
Russell King65724a12017-07-07 15:56:24 +0100320 work->work.cancel = armada_drm_crtc_finish_frame_work;
Russell King901bb882017-07-07 15:55:45 +0100321 armada_reg_queue_end(work->regs, i);
322
323 return work;
324}
325
Russell King96f60e32012-08-15 13:59:49 +0100326static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
327 struct drm_framebuffer *fb, bool force)
328{
329 struct armada_frame_work *work;
330
331 if (!fb)
332 return;
333
334 if (force) {
335 /* Display is disabled, so just drop the old fb */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600336 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100337 return;
338 }
339
Russell Kingeaab0132017-07-07 15:55:53 +0100340 work = armada_drm_crtc_alloc_frame_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100341 if (work) {
Russell King96f60e32012-08-15 13:59:49 +0100342 work->old_fb = fb;
Russell King96f60e32012-08-15 13:59:49 +0100343
Russell King28b30432017-07-08 10:16:40 +0100344 if (armada_drm_plane_work_queue(dcrtc, &work->work) == 0)
Russell King96f60e32012-08-15 13:59:49 +0100345 return;
346
347 kfree(work);
348 }
349
350 /*
351 * Oops - just drop the reference immediately and hope for
352 * the best. The worst that will happen is the buffer gets
353 * reused before it has finished being displayed.
354 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600355 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100356}
357
358static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
359{
Russell King96f60e32012-08-15 13:59:49 +0100360 /*
361 * Tell the DRM core that vblank IRQs aren't going to happen for
362 * a while. This cleans up any pending vblank events for us.
363 */
Russell King178e5612014-10-11 23:57:04 +0100364 drm_crtc_vblank_off(&dcrtc->crtc);
Russell Kingec6fb152016-07-25 15:16:11 +0100365 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100366}
367
Russell King96f60e32012-08-15 13:59:49 +0100368/* The mode_config.mutex will be held for this call */
369static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
370{
371 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
372
Russell Kingea908ba2016-10-04 22:19:57 +0100373 if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
Russell King96f60e32012-08-15 13:59:49 +0100374 if (dpms_blanked(dpms))
375 armada_drm_vblank_off(dcrtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100376 else if (!IS_ERR(dcrtc->clk))
377 WARN_ON(clk_prepare_enable(dcrtc->clk));
378 dcrtc->dpms = dpms;
379 armada_drm_crtc_update(dcrtc);
380 if (!dpms_blanked(dpms))
Russell King178e5612014-10-11 23:57:04 +0100381 drm_crtc_vblank_on(&dcrtc->crtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100382 else if (!IS_ERR(dcrtc->clk))
383 clk_disable_unprepare(dcrtc->clk);
384 } else if (dcrtc->dpms != dpms) {
385 dcrtc->dpms = dpms;
Russell King96f60e32012-08-15 13:59:49 +0100386 }
387}
388
389/*
390 * Prepare for a mode set. Turn off overlay to ensure that we don't end
391 * up with the overlay size being bigger than the active screen size.
392 * We rely upon X refreshing this state after the mode set has completed.
393 *
394 * The mode_config.mutex will be held for this call
395 */
396static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
397{
398 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
399 struct drm_plane *plane;
400
401 /*
402 * If we have an overlay plane associated with this CRTC, disable
403 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100404 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100405 */
406 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100407 if (plane)
408 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100409}
410
411/* The mode_config.mutex will be held for this call */
412static void armada_drm_crtc_commit(struct drm_crtc *crtc)
413{
414 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
415
416 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
417 dcrtc->dpms = DRM_MODE_DPMS_ON;
418 armada_drm_crtc_update(dcrtc);
419 }
420}
421
422/* The mode_config.mutex will be held for this call */
423static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
424 const struct drm_display_mode *mode, struct drm_display_mode *adj)
425{
Russell King96f60e32012-08-15 13:59:49 +0100426 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
427 int ret;
428
429 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100430 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100431 adj->flags & DRM_MODE_FLAG_INTERLACE)
432 return false;
433
434 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100435 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100436 if (ret)
437 return false;
438
439 return true;
440}
441
Shawn Guo5922a7d2017-02-07 17:16:18 +0800442/* These are locked by dev->vbl_lock */
443static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
444{
445 if (dcrtc->irq_ena & mask) {
446 dcrtc->irq_ena &= ~mask;
447 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
448 }
449}
450
451static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
452{
453 if ((dcrtc->irq_ena & mask) != mask) {
454 dcrtc->irq_ena |= mask;
455 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
456 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
457 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
458 }
459}
460
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100461static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100462{
Russell King96f60e32012-08-15 13:59:49 +0100463 void __iomem *base = dcrtc->base;
Russell King4a8506d2015-08-07 09:33:05 +0100464 struct drm_plane *ovl_plane;
Russell King96f60e32012-08-15 13:59:49 +0100465
466 if (stat & DMA_FF_UNDERFLOW)
467 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
468 if (stat & GRA_FF_UNDERFLOW)
469 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
470
471 if (stat & VSYNC_IRQ)
Gustavo Padovan0ac28c52016-07-04 21:04:48 -0300472 drm_crtc_handle_vblank(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100473
474 spin_lock(&dcrtc->irq_lock);
Russell King4a8506d2015-08-07 09:33:05 +0100475 ovl_plane = dcrtc->plane;
Russell Kingec6fb152016-07-25 15:16:11 +0100476 if (ovl_plane)
477 armada_drm_plane_work_run(dcrtc, ovl_plane);
Russell King96f60e32012-08-15 13:59:49 +0100478
479 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
480 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
481 uint32_t val;
482
483 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
484 writel_relaxed(dcrtc->v[i].spu_v_h_total,
485 base + LCD_SPUT_V_H_TOTAL);
486
487 val = readl_relaxed(base + LCD_SPU_ADV_REG);
488 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
489 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100490 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100491 }
Russell King662af0d2013-05-19 10:55:17 +0100492
493 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
494 writel_relaxed(dcrtc->cursor_hw_pos,
495 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
496 writel_relaxed(dcrtc->cursor_hw_sz,
497 base + LCD_SPU_HWC_HPXL_VLN);
498 armada_updatel(CFG_HWC_ENA,
499 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
500 base + LCD_SPU_DMA_CTRL0);
501 dcrtc->cursor_update = false;
502 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
503 }
504
Russell King96f60e32012-08-15 13:59:49 +0100505 spin_unlock(&dcrtc->irq_lock);
506
Russell Kingec6fb152016-07-25 15:16:11 +0100507 if (stat & GRA_FRAME_IRQ)
508 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100509}
510
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100511static irqreturn_t armada_drm_irq(int irq, void *arg)
512{
513 struct armada_crtc *dcrtc = arg;
514 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
515
516 /*
517 * This is rediculous - rather than writing bits to clear, we
518 * have to set the actual status register value. This is racy.
519 */
520 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
521
Russell Kingc8a220c2016-05-17 13:51:08 +0100522 trace_armada_drm_irq(&dcrtc->crtc, stat);
523
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100524 /* Mask out those interrupts we haven't enabled */
525 v = stat & dcrtc->irq_ena;
526
527 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
528 armada_drm_crtc_irq(dcrtc, stat);
529 return IRQ_HANDLED;
530 }
531 return IRQ_NONE;
532}
533
Russell King96f60e32012-08-15 13:59:49 +0100534static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
535{
536 struct drm_display_mode *adj = &dcrtc->crtc.mode;
537 uint32_t val = 0;
538
539 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
540 val |= CFG_CSC_YUV_CCIR709;
541 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
542 val |= CFG_CSC_RGB_STUDIO;
543
544 /*
545 * In auto mode, set the colorimetry, based upon the HDMI spec.
546 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
547 * ITU601. It may be more appropriate to set this depending on
548 * the source - but what if the graphic frame is YUV and the
549 * video frame is RGB?
550 */
551 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
552 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
553 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
554 if (dcrtc->csc_yuv_mode == CSC_AUTO)
555 val |= CFG_CSC_YUV_CCIR709;
556 }
557
558 /*
559 * We assume we're connected to a TV-like device, so the YUV->RGB
560 * conversion should produce a limited range. We should set this
561 * depending on the connectors attached to this CRTC, and what
562 * kind of device they report being connected.
563 */
564 if (dcrtc->csc_rgb_mode == CSC_AUTO)
565 val |= CFG_CSC_RGB_STUDIO;
566
567 return val;
568}
569
Russell King37af35c2016-08-16 22:09:09 +0100570static void armada_drm_primary_set(struct drm_crtc *crtc,
571 struct drm_plane *plane, int x, int y)
572{
573 struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
574 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King2925db02016-08-16 22:09:10 +0100575 struct armada_regs regs[8];
Russell King37af35c2016-08-16 22:09:09 +0100576 bool interlaced = dcrtc->interlaced;
577 unsigned i;
Russell King2925db02016-08-16 22:09:10 +0100578 u32 ctrl0;
Russell King37af35c2016-08-16 22:09:09 +0100579
580 i = armada_drm_crtc_calc_fb(plane->fb, x, y, regs, interlaced);
581
Russell King2925db02016-08-16 22:09:10 +0100582 armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell King37af35c2016-08-16 22:09:09 +0100583 armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
584 armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
585
586 ctrl0 = state->ctrl0;
587 if (interlaced)
588 ctrl0 |= CFG_GRA_FTOGGLE;
589
590 armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
591 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
592 CFG_SWAPYU | CFG_YUV2RGB) |
593 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
594 LCD_SPU_DMA_CTRL0);
595 armada_reg_queue_end(regs, i);
596 armada_drm_crtc_update_regs(dcrtc, regs);
597}
598
Russell King96f60e32012-08-15 13:59:49 +0100599/* The mode_config.mutex will be held for this call */
600static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
601 struct drm_display_mode *mode, struct drm_display_mode *adj,
602 int x, int y, struct drm_framebuffer *old_fb)
603{
Russell King96f60e32012-08-15 13:59:49 +0100604 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
605 struct armada_regs regs[17];
606 uint32_t lm, rm, tm, bm, val, sclk;
607 unsigned long flags;
608 unsigned i;
609 bool interlaced;
610
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600611 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100612
613 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
614
Russell King8be523d2016-08-16 22:09:08 +0100615 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
616 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
617 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100618
Russell King8be523d2016-08-16 22:09:08 +0100619 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
620 val |= CFG_PALETTE_ENA;
621
622 drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
623 drm_to_armada_plane(crtc->primary)->state.src_hw =
624 drm_to_armada_plane(crtc->primary)->state.dst_hw =
Russell King37af35c2016-08-16 22:09:09 +0100625 adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
Russell King8be523d2016-08-16 22:09:08 +0100626 drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
627
Russell King37af35c2016-08-16 22:09:09 +0100628 i = 0;
Russell King96f60e32012-08-15 13:59:49 +0100629 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
630 lm = adj->crtc_htotal - adj->crtc_hsync_end;
631 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
632 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
633
634 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
635 adj->crtc_hdisplay,
636 adj->crtc_hsync_start,
637 adj->crtc_hsync_end,
638 adj->crtc_htotal, lm, rm);
639 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
640 adj->crtc_vdisplay,
641 adj->crtc_vsync_start,
642 adj->crtc_vsync_end,
643 adj->crtc_vtotal, tm, bm);
644
645 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100646 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
647 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100648
Russell King178e5612014-10-11 23:57:04 +0100649 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100650
Russell King96f60e32012-08-15 13:59:49 +0100651 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
652 if (val != dcrtc->dumb_ctrl) {
653 dcrtc->dumb_ctrl = val;
654 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
655 }
656
Russell Kinge0ac5e92015-06-29 18:01:38 +0100657 /*
658 * If we are blanked, we would have disabled the clock. Re-enable
659 * it so that compute_clock() does the right thing.
660 */
661 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
662 WARN_ON(clk_prepare_enable(dcrtc->clk));
663
Russell King96f60e32012-08-15 13:59:49 +0100664 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100665 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100666
667 /* Ensure graphic fifo is enabled */
668 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
669 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
670
671 if (interlaced ^ dcrtc->interlaced) {
672 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300673 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100674 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300675 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100676 dcrtc->interlaced = interlaced;
677 }
678
679 spin_lock_irqsave(&dcrtc->irq_lock, flags);
680
681 /* Even interlaced/progressive frame */
682 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
683 adj->crtc_htotal;
684 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
685 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100686 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100687 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100688
689 if (interlaced) {
690 /* Odd interlaced frame */
691 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
692 (1 << 16);
693 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
694 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100695 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100696 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100697 } else {
698 dcrtc->v[0] = dcrtc->v[1];
699 }
700
701 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
702
703 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King96f60e32012-08-15 13:59:49 +0100704 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
705 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
706 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
707 LCD_SPUT_V_H_TOTAL);
708
Russell King42e62ba2014-04-22 15:24:03 +0100709 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100710 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
711 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
712 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100713 }
Russell King96f60e32012-08-15 13:59:49 +0100714
Russell King96f60e32012-08-15 13:59:49 +0100715 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
716 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
717
718 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
719 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
720 armada_reg_queue_end(regs, i);
721
722 armada_drm_crtc_update_regs(dcrtc, regs);
Russell King37af35c2016-08-16 22:09:09 +0100723
724 armada_drm_primary_set(crtc, crtc->primary, x, y);
Russell King96f60e32012-08-15 13:59:49 +0100725 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
726
727 armada_drm_crtc_update(dcrtc);
728
Russell King178e5612014-10-11 23:57:04 +0100729 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100730 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
731
732 return 0;
733}
734
735/* The mode_config.mutex will be held for this call */
736static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
737 struct drm_framebuffer *old_fb)
738{
739 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
740 struct armada_regs regs[4];
741 unsigned i;
742
Matt Roperf4510a22014-04-01 15:22:40 -0700743 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100744 dcrtc->interlaced);
745 armada_reg_queue_end(regs, i);
746
747 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100748 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
749 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100750
751 /* Take a reference to the new fb as we're using it */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600752 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100753
754 /* Update the base in the CRTC */
755 armada_drm_crtc_update_regs(dcrtc, regs);
756
757 /* Drop our previously held reference */
758 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
759
760 return 0;
761}
762
Russell King96f60e32012-08-15 13:59:49 +0100763/* The mode_config.mutex will be held for this call */
764static void armada_drm_crtc_disable(struct drm_crtc *crtc)
765{
Russell King96f60e32012-08-15 13:59:49 +0100766 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King28b30432017-07-08 10:16:40 +0100767
768 /* Disable our primary plane when we disable the CRTC. */
769 crtc->primary->funcs->disable_plane(crtc->primary, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100770}
771
772static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
773 .dpms = armada_drm_crtc_dpms,
774 .prepare = armada_drm_crtc_prepare,
775 .commit = armada_drm_crtc_commit,
776 .mode_fixup = armada_drm_crtc_mode_fixup,
777 .mode_set = armada_drm_crtc_mode_set,
778 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100779 .disable = armada_drm_crtc_disable,
780};
781
Russell King662af0d2013-05-19 10:55:17 +0100782static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
783 unsigned stride, unsigned width, unsigned height)
784{
785 uint32_t addr;
786 unsigned y;
787
788 addr = SRAM_HWC32_RAM1;
789 for (y = 0; y < height; y++) {
790 uint32_t *p = &pix[y * stride];
791 unsigned x;
792
793 for (x = 0; x < width; x++, p++) {
794 uint32_t val = *p;
795
796 val = (val & 0xff00ff00) |
797 (val & 0x000000ff) << 16 |
798 (val & 0x00ff0000) >> 16;
799
800 writel_relaxed(val,
801 base + LCD_SPU_SRAM_WRDAT);
802 writel_relaxed(addr | SRAM_WRITE,
803 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100804 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100805 addr += 1;
806 if ((addr & 0x00ff) == 0)
807 addr += 0xf00;
808 if ((addr & 0x30ff) == 0)
809 addr = SRAM_HWC32_RAM2;
810 }
811 }
812}
813
814static void armada_drm_crtc_cursor_tran(void __iomem *base)
815{
816 unsigned addr;
817
818 for (addr = 0; addr < 256; addr++) {
819 /* write the default value */
820 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
821 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
822 base + LCD_SPU_SRAM_CTRL);
823 }
824}
825
826static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
827{
828 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
829 uint32_t yoff, yscr, h = dcrtc->cursor_h;
830 uint32_t para1;
831
832 /*
833 * Calculate the visible width and height of the cursor,
834 * screen position, and the position in the cursor bitmap.
835 */
836 if (dcrtc->cursor_x < 0) {
837 xoff = -dcrtc->cursor_x;
838 xscr = 0;
839 w -= min(xoff, w);
840 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
841 xoff = 0;
842 xscr = dcrtc->cursor_x;
843 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
844 } else {
845 xoff = 0;
846 xscr = dcrtc->cursor_x;
847 }
848
849 if (dcrtc->cursor_y < 0) {
850 yoff = -dcrtc->cursor_y;
851 yscr = 0;
852 h -= min(yoff, h);
853 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
854 yoff = 0;
855 yscr = dcrtc->cursor_y;
856 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
857 } else {
858 yoff = 0;
859 yscr = dcrtc->cursor_y;
860 }
861
862 /* On interlaced modes, the vertical cursor size must be halved */
863 s = dcrtc->cursor_w;
864 if (dcrtc->interlaced) {
865 s *= 2;
866 yscr /= 2;
867 h /= 2;
868 }
869
870 if (!dcrtc->cursor_obj || !h || !w) {
871 spin_lock_irq(&dcrtc->irq_lock);
872 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
873 dcrtc->cursor_update = false;
874 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
875 spin_unlock_irq(&dcrtc->irq_lock);
876 return 0;
877 }
878
879 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
880 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
881 dcrtc->base + LCD_SPU_SRAM_PARA1);
882
883 /*
884 * Initialize the transparency if the SRAM was powered down.
885 * We must also reload the cursor data as well.
886 */
887 if (!(para1 & CFG_CSB_256x32)) {
888 armada_drm_crtc_cursor_tran(dcrtc->base);
889 reload = true;
890 }
891
892 if (dcrtc->cursor_hw_sz != (h << 16 | 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 reload = true;
899 }
900 if (reload) {
901 struct armada_gem_object *obj = dcrtc->cursor_obj;
902 uint32_t *pix;
903 /* Set the top-left corner of the cursor image */
904 pix = obj->addr;
905 pix += yoff * s + xoff;
906 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
907 }
908
909 /* Reload the cursor position, size and enable in the IRQ handler */
910 spin_lock_irq(&dcrtc->irq_lock);
911 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
912 dcrtc->cursor_hw_sz = h << 16 | w;
913 dcrtc->cursor_update = true;
914 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
915 spin_unlock_irq(&dcrtc->irq_lock);
916
917 return 0;
918}
919
920static void cursor_update(void *data)
921{
922 armada_drm_crtc_cursor_update(data, true);
923}
924
925static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
926 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
927{
Russell King662af0d2013-05-19 10:55:17 +0100928 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100929 struct armada_gem_object *obj = NULL;
930 int ret;
931
932 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100933 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100934 return -ENXIO;
935
936 if (handle && w > 0 && h > 0) {
937 /* maximum size is 64x32 or 32x64 */
938 if (w > 64 || h > 64 || (w > 32 && h > 32))
939 return -ENOMEM;
940
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100941 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100942 if (!obj)
943 return -ENOENT;
944
945 /* Must be a kernel-mapped object */
946 if (!obj->addr) {
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600947 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100948 return -EINVAL;
949 }
950
951 if (obj->obj.size < w * h * 4) {
952 DRM_ERROR("buffer is too small\n");
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600953 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100954 return -ENOMEM;
955 }
956 }
957
Russell King662af0d2013-05-19 10:55:17 +0100958 if (dcrtc->cursor_obj) {
959 dcrtc->cursor_obj->update = NULL;
960 dcrtc->cursor_obj->update_data = NULL;
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600961 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100962 }
963 dcrtc->cursor_obj = obj;
964 dcrtc->cursor_w = w;
965 dcrtc->cursor_h = h;
966 ret = armada_drm_crtc_cursor_update(dcrtc, true);
967 if (obj) {
968 obj->update_data = dcrtc;
969 obj->update = cursor_update;
970 }
Russell King662af0d2013-05-19 10:55:17 +0100971
972 return ret;
973}
974
975static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
976{
Russell King662af0d2013-05-19 10:55:17 +0100977 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100978 int ret;
979
980 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100981 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100982 return -EFAULT;
983
Russell King662af0d2013-05-19 10:55:17 +0100984 dcrtc->cursor_x = x;
985 dcrtc->cursor_y = y;
986 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +0100987
988 return ret;
989}
990
Russell King96f60e32012-08-15 13:59:49 +0100991static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
992{
993 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
994 struct armada_private *priv = crtc->dev->dev_private;
995
Russell King662af0d2013-05-19 10:55:17 +0100996 if (dcrtc->cursor_obj)
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600997 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100998
Russell King96f60e32012-08-15 13:59:49 +0100999 priv->dcrtc[dcrtc->num] = NULL;
1000 drm_crtc_cleanup(&dcrtc->crtc);
1001
1002 if (!IS_ERR(dcrtc->clk))
1003 clk_disable_unprepare(dcrtc->clk);
1004
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001005 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1006
Russell King9611cb92014-06-15 11:21:23 +01001007 of_node_put(dcrtc->crtc.port);
1008
Russell King96f60e32012-08-15 13:59:49 +01001009 kfree(dcrtc);
1010}
1011
1012/*
1013 * The mode_config lock is held here, to prevent races between this
1014 * and a mode_set.
1015 */
1016static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01001017 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags,
1018 struct drm_modeset_acquire_ctx *ctx)
Russell King96f60e32012-08-15 13:59:49 +01001019{
1020 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1021 struct armada_frame_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001022 unsigned i;
1023 int ret;
1024
1025 /* We don't support changing the pixel format */
Ville Syrjälädbd4d572016-11-18 21:53:10 +02001026 if (fb->format != crtc->primary->fb->format)
Russell King96f60e32012-08-15 13:59:49 +01001027 return -EINVAL;
1028
Russell Kingeaab0132017-07-07 15:55:53 +01001029 work = armada_drm_crtc_alloc_frame_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001030 if (!work)
1031 return -ENOMEM;
1032
1033 work->event = event;
Matt Roperf4510a22014-04-01 15:22:40 -07001034 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001035
1036 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1037 dcrtc->interlaced);
1038 armada_reg_queue_end(work->regs, i);
1039
1040 /*
Russell Kingc5488302014-10-11 23:53:35 +01001041 * Ensure that we hold a reference on the new framebuffer.
1042 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001043 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001044 drm_framebuffer_get(fb);
Russell King96f60e32012-08-15 13:59:49 +01001045
Russell King28b30432017-07-08 10:16:40 +01001046 ret = armada_drm_plane_work_queue(dcrtc, &work->work);
Russell King96f60e32012-08-15 13:59:49 +01001047 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001048 /* Undo our reference above */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001049 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +01001050 kfree(work);
1051 return ret;
1052 }
1053
1054 /*
1055 * Don't take a reference on the new framebuffer;
1056 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1057 * will _not_ drop that reference on successful return from this
1058 * function. Simply mark this new framebuffer as the current one.
1059 */
Matt Roperf4510a22014-04-01 15:22:40 -07001060 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001061
1062 /*
1063 * Finally, if the display is blanked, we won't receive an
1064 * interrupt, so complete it now.
1065 */
Russell King4b5dda82015-08-06 16:37:18 +01001066 if (dpms_blanked(dcrtc->dpms))
Russell Kingec6fb152016-07-25 15:16:11 +01001067 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001068
1069 return 0;
1070}
1071
1072static int
1073armada_drm_crtc_set_property(struct drm_crtc *crtc,
1074 struct drm_property *property, uint64_t val)
1075{
1076 struct armada_private *priv = crtc->dev->dev_private;
1077 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1078 bool update_csc = false;
1079
1080 if (property == priv->csc_yuv_prop) {
1081 dcrtc->csc_yuv_mode = val;
1082 update_csc = true;
1083 } else if (property == priv->csc_rgb_prop) {
1084 dcrtc->csc_rgb_mode = val;
1085 update_csc = true;
1086 }
1087
1088 if (update_csc) {
1089 uint32_t val;
1090
1091 val = dcrtc->spu_iopad_ctrl |
1092 armada_drm_crtc_calculate_csc(dcrtc);
1093 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1094 }
1095
1096 return 0;
1097}
1098
Shawn Guo5922a7d2017-02-07 17:16:18 +08001099/* These are called under the vbl_lock. */
1100static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
1101{
1102 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1103
1104 armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
1105 return 0;
1106}
1107
1108static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
1109{
1110 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1111
1112 armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
1113}
1114
Ville Syrjäläa02fb902015-12-15 12:20:59 +01001115static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001116 .cursor_set = armada_drm_crtc_cursor_set,
1117 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001118 .destroy = armada_drm_crtc_destroy,
1119 .set_config = drm_crtc_helper_set_config,
1120 .page_flip = armada_drm_crtc_page_flip,
1121 .set_property = armada_drm_crtc_set_property,
Shawn Guo5922a7d2017-02-07 17:16:18 +08001122 .enable_vblank = armada_drm_crtc_enable_vblank,
1123 .disable_vblank = armada_drm_crtc_disable_vblank,
Russell King96f60e32012-08-15 13:59:49 +01001124};
1125
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001126int armada_drm_plane_disable(struct drm_plane *plane,
1127 struct drm_modeset_acquire_ctx *ctx)
Russell King28b30432017-07-08 10:16:40 +01001128{
1129 struct armada_plane *dplane = drm_to_armada_plane(plane);
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001130 struct armada_crtc *dcrtc;
Russell King28b30432017-07-08 10:16:40 +01001131 u32 sram_para1, dma_ctrl0_mask;
1132
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001133 if (!plane->crtc)
1134 return 0;
1135
Russell King28b30432017-07-08 10:16:40 +01001136 /*
1137 * Drop our reference on any framebuffer attached to this plane.
1138 * We don't need to NULL this out as drm_plane_force_disable(),
1139 * and __setplane_internal() will do so for an overlay plane, and
1140 * __drm_helper_disable_unused_functions() will do so for the
1141 * primary plane.
1142 */
1143 if (plane->fb)
1144 drm_framebuffer_put(plane->fb);
1145
1146 /* Power down most RAMs and FIFOs if this is the primary plane */
1147 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
1148 sram_para1 = CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1149 CFG_PDWN32x32 | CFG_PDWN64x66;
1150 dma_ctrl0_mask = CFG_GRA_ENA;
1151 } else {
1152 /* Power down the Y/U/V FIFOs */
1153 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
1154 dma_ctrl0_mask = CFG_DMA_ENA;
1155 }
1156
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001157 dcrtc = drm_to_armada_crtc(plane->crtc);
1158
Russell King28b30432017-07-08 10:16:40 +01001159 /* Wait for any preceding work to complete, but don't wedge */
1160 if (WARN_ON(!armada_drm_plane_work_wait(dplane, HZ)))
1161 armada_drm_plane_work_cancel(dcrtc, dplane);
1162
1163 spin_lock_irq(&dcrtc->irq_lock);
1164 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
1165 spin_unlock_irq(&dcrtc->irq_lock);
1166
1167 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
Russell King28b30432017-07-08 10:16:40 +01001168
Russell King28b30432017-07-08 10:16:40 +01001169 return 0;
1170}
1171
Russell Kingde323012015-07-15 18:11:24 +01001172static const struct drm_plane_funcs armada_primary_plane_funcs = {
1173 .update_plane = drm_primary_helper_update,
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001174 .disable_plane = armada_drm_plane_disable,
Russell Kingde323012015-07-15 18:11:24 +01001175 .destroy = drm_primary_helper_destroy,
1176};
1177
Russell King5740d272015-07-15 18:11:25 +01001178int armada_drm_plane_init(struct armada_plane *plane)
1179{
1180 init_waitqueue_head(&plane->frame_wait);
1181
1182 return 0;
1183}
1184
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301185static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001186 { CSC_AUTO, "Auto" },
1187 { CSC_YUV_CCIR601, "CCIR601" },
1188 { CSC_YUV_CCIR709, "CCIR709" },
1189};
1190
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301191static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001192 { CSC_AUTO, "Auto" },
1193 { CSC_RGB_COMPUTER, "Computer system" },
1194 { CSC_RGB_STUDIO, "Studio" },
1195};
1196
1197static int armada_drm_crtc_create_properties(struct drm_device *dev)
1198{
1199 struct armada_private *priv = dev->dev_private;
1200
1201 if (priv->csc_yuv_prop)
1202 return 0;
1203
1204 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1205 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1206 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1207 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1208 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1209 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1210
1211 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1212 return -ENOMEM;
1213
1214 return 0;
1215}
1216
Russell King0fb29702015-06-06 21:46:53 +01001217static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001218 struct resource *res, int irq, const struct armada_variant *variant,
1219 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001220{
Russell Kingd8c96082014-04-22 11:10:15 +01001221 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001222 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001223 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001224 void __iomem *base;
1225 int ret;
1226
Russell Kingd8c96082014-04-22 11:10:15 +01001227 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001228 if (ret)
1229 return ret;
1230
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001231 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001232 if (IS_ERR(base))
1233 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001234
1235 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1236 if (!dcrtc) {
1237 DRM_ERROR("failed to allocate Armada crtc\n");
1238 return -ENOMEM;
1239 }
1240
Russell Kingd8c96082014-04-22 11:10:15 +01001241 if (dev != drm->dev)
1242 dev_set_drvdata(dev, dcrtc);
1243
Russell King42e62ba2014-04-22 15:24:03 +01001244 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001245 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001246 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001247 dcrtc->clk = ERR_PTR(-EINVAL);
1248 dcrtc->csc_yuv_mode = CSC_AUTO;
1249 dcrtc->csc_rgb_mode = CSC_AUTO;
1250 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1251 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1252 spin_lock_init(&dcrtc->irq_lock);
1253 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +01001254
1255 /* Initialize some registers which we don't otherwise set */
1256 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1257 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1258 writel_relaxed(dcrtc->spu_iopad_ctrl,
1259 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1260 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1261 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1262 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1263 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1264 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001265 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1266 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001267
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001268 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1269 dcrtc);
Russell King33cd3c02017-12-08 12:16:22 +00001270 if (ret < 0)
1271 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001272
Russell King42e62ba2014-04-22 15:24:03 +01001273 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001274 ret = dcrtc->variant->init(dcrtc, dev);
Russell King33cd3c02017-12-08 12:16:22 +00001275 if (ret)
1276 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001277 }
1278
1279 /* Ensure AXI pipeline is enabled */
1280 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1281
1282 priv->dcrtc[dcrtc->num] = dcrtc;
1283
Russell King9611cb92014-06-15 11:21:23 +01001284 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001285
Russell Kingde323012015-07-15 18:11:24 +01001286 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King33cd3c02017-12-08 12:16:22 +00001287 if (!primary) {
1288 ret = -ENOMEM;
1289 goto err_crtc;
1290 }
Russell King1c914ce2015-07-15 18:11:24 +01001291
Russell King5740d272015-07-15 18:11:25 +01001292 ret = armada_drm_plane_init(primary);
1293 if (ret) {
1294 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001295 goto err_crtc;
Russell King5740d272015-07-15 18:11:25 +01001296 }
1297
Russell Kingde323012015-07-15 18:11:24 +01001298 ret = drm_universal_plane_init(drm, &primary->base, 0,
1299 &armada_primary_plane_funcs,
1300 armada_primary_formats,
1301 ARRAY_SIZE(armada_primary_formats),
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001302 NULL,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001303 DRM_PLANE_TYPE_PRIMARY, NULL);
Russell Kingde323012015-07-15 18:11:24 +01001304 if (ret) {
1305 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001306 goto err_crtc;
Russell Kingde323012015-07-15 18:11:24 +01001307 }
1308
1309 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001310 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001311 if (ret)
1312 goto err_crtc_init;
1313
Russell King96f60e32012-08-15 13:59:49 +01001314 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1315
1316 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1317 dcrtc->csc_yuv_mode);
1318 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1319 dcrtc->csc_rgb_mode);
1320
Russell Kingd8c96082014-04-22 11:10:15 +01001321 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001322
1323err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001324 primary->base.funcs->destroy(&primary->base);
Russell King33cd3c02017-12-08 12:16:22 +00001325err_crtc:
1326 kfree(dcrtc);
1327
Russell King1c914ce2015-07-15 18:11:24 +01001328 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001329}
Russell Kingd8c96082014-04-22 11:10:15 +01001330
1331static int
1332armada_lcd_bind(struct device *dev, struct device *master, void *data)
1333{
1334 struct platform_device *pdev = to_platform_device(dev);
1335 struct drm_device *drm = data;
1336 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1337 int irq = platform_get_irq(pdev, 0);
1338 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001339 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001340
1341 if (irq < 0)
1342 return irq;
1343
1344 if (!dev->of_node) {
1345 const struct platform_device_id *id;
1346
1347 id = platform_get_device_id(pdev);
1348 if (!id)
1349 return -ENXIO;
1350
1351 variant = (const struct armada_variant *)id->driver_data;
1352 } else {
1353 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001354 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001355
1356 match = of_match_device(dev->driver->of_match_table, dev);
1357 if (!match)
1358 return -ENXIO;
1359
Russell King9611cb92014-06-15 11:21:23 +01001360 np = of_get_child_by_name(parent, "ports");
1361 if (np)
1362 parent = np;
1363 port = of_get_child_by_name(parent, "port");
1364 of_node_put(np);
1365 if (!port) {
Rob Herring4bf99142017-07-18 16:43:04 -05001366 dev_err(dev, "no port node found in %pOF\n", parent);
Russell King9611cb92014-06-15 11:21:23 +01001367 return -ENXIO;
1368 }
1369
Russell Kingd8c96082014-04-22 11:10:15 +01001370 variant = match->data;
1371 }
1372
Russell King9611cb92014-06-15 11:21:23 +01001373 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001374}
1375
1376static void
1377armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1378{
1379 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1380
1381 armada_drm_crtc_destroy(&dcrtc->crtc);
1382}
1383
1384static const struct component_ops armada_lcd_ops = {
1385 .bind = armada_lcd_bind,
1386 .unbind = armada_lcd_unbind,
1387};
1388
1389static int armada_lcd_probe(struct platform_device *pdev)
1390{
1391 return component_add(&pdev->dev, &armada_lcd_ops);
1392}
1393
1394static int armada_lcd_remove(struct platform_device *pdev)
1395{
1396 component_del(&pdev->dev, &armada_lcd_ops);
1397 return 0;
1398}
1399
Arvind Yadav85909712017-06-20 10:44:33 +05301400static const struct of_device_id armada_lcd_of_match[] = {
Russell Kingd8c96082014-04-22 11:10:15 +01001401 {
1402 .compatible = "marvell,dove-lcd",
1403 .data = &armada510_ops,
1404 },
1405 {}
1406};
1407MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1408
1409static const struct platform_device_id armada_lcd_platform_ids[] = {
1410 {
1411 .name = "armada-lcd",
1412 .driver_data = (unsigned long)&armada510_ops,
1413 }, {
1414 .name = "armada-510-lcd",
1415 .driver_data = (unsigned long)&armada510_ops,
1416 },
1417 { },
1418};
1419MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1420
1421struct platform_driver armada_lcd_platform_driver = {
1422 .probe = armada_lcd_probe,
1423 .remove = armada_lcd_remove,
1424 .driver = {
1425 .name = "armada-lcd",
1426 .owner = THIS_MODULE,
1427 .of_match_table = armada_lcd_of_match,
1428 },
1429 .id_table = armada_lcd_platform_ids,
1430};