blob: 02eefdc6f062ccb033dcbbec32eac65eafa3f171 [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
Russell King96f60e32012-08-15 13:59:49 +010023enum csc_mode {
24 CSC_AUTO = 0,
25 CSC_YUV_CCIR601 = 1,
26 CSC_YUV_CCIR709 = 2,
27 CSC_RGB_COMPUTER = 1,
28 CSC_RGB_STUDIO = 2,
29};
30
Russell King1c914ce2015-07-15 18:11:24 +010031static const uint32_t armada_primary_formats[] = {
32 DRM_FORMAT_UYVY,
33 DRM_FORMAT_YUYV,
34 DRM_FORMAT_VYUY,
35 DRM_FORMAT_YVYU,
36 DRM_FORMAT_ARGB8888,
37 DRM_FORMAT_ABGR8888,
38 DRM_FORMAT_XRGB8888,
39 DRM_FORMAT_XBGR8888,
40 DRM_FORMAT_RGB888,
41 DRM_FORMAT_BGR888,
42 DRM_FORMAT_ARGB1555,
43 DRM_FORMAT_ABGR1555,
44 DRM_FORMAT_RGB565,
45 DRM_FORMAT_BGR565,
46};
47
Russell King96f60e32012-08-15 13:59:49 +010048/*
49 * A note about interlacing. Let's consider HDMI 1920x1080i.
50 * The timing parameters we have from X are:
51 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
52 * 1920 2448 2492 2640 1080 1084 1094 1125
53 * Which get translated to:
54 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
55 * 1920 2448 2492 2640 540 542 547 562
56 *
57 * This is how it is defined by CEA-861-D - line and pixel numbers are
58 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
59 * line: 2640. The odd frame, the first active line is at line 21, and
60 * the even frame, the first active line is 584.
61 *
62 * LN: 560 561 562 563 567 568 569
63 * DE: ~~~|____________________________//__________________________
64 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
65 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
66 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
67 *
68 * LN: 1123 1124 1125 1 5 6 7
69 * DE: ~~~|____________________________//__________________________
70 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
71 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
72 * 23 blanking lines
73 *
74 * The Armada LCD Controller line and pixel numbers are, like X timings,
75 * referenced to the top left of the active frame.
76 *
77 * So, translating these to our LCD controller:
78 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
79 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
80 * Note: Vsync front porch remains constant!
81 *
82 * if (odd_frame) {
83 * vtotal = mode->crtc_vtotal + 1;
84 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
85 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
86 * } else {
87 * vtotal = mode->crtc_vtotal;
88 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
89 * vhorizpos = mode->crtc_hsync_start;
90 * }
91 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
92 *
93 * So, we need to reprogram these registers on each vsync event:
94 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
95 *
96 * Note: we do not use the frame done interrupts because these appear
97 * to happen too early, and lead to jitter on the display (presumably
98 * they occur at the end of the last active line, before the vsync back
99 * porch, which we're reprogramming.)
100 */
101
102void
103armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
104{
105 while (regs->offset != ~0) {
106 void __iomem *reg = dcrtc->base + regs->offset;
107 uint32_t val;
108
109 val = regs->mask;
110 if (val != 0)
111 val &= readl_relaxed(reg);
112 writel_relaxed(val | regs->val, reg);
113 ++regs;
114 }
115}
116
117#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
118
119static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
120{
121 uint32_t dumb_ctrl;
122
123 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
124
125 if (!dpms_blanked(dcrtc->dpms))
126 dumb_ctrl |= CFG_DUMB_ENA;
127
128 /*
129 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
130 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
131 * force LCD_D[23:0] to output blank color, overriding the GPIO or
132 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
133 */
134 if (dpms_blanked(dcrtc->dpms) &&
135 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
136 dumb_ctrl &= ~DUMB_MASK;
137 dumb_ctrl |= DUMB_BLANK;
138 }
139
140 /*
141 * The documentation doesn't indicate what the normal state of
142 * the sync signals are. Sebastian Hesselbart kindly probed
143 * these signals on his board to determine their state.
144 *
145 * The non-inverted state of the sync signals is active high.
146 * Setting these bits makes the appropriate signal active low.
147 */
148 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
149 dumb_ctrl |= CFG_INV_CSYNC;
150 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
151 dumb_ctrl |= CFG_INV_HSYNC;
152 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
153 dumb_ctrl |= CFG_INV_VSYNC;
154
155 if (dcrtc->dumb_ctrl != dumb_ctrl) {
156 dcrtc->dumb_ctrl = dumb_ctrl;
157 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
158 }
159}
160
Russell Kingf0b24872016-08-16 22:09:11 +0100161void armada_drm_plane_calc_addrs(u32 *addrs, struct drm_framebuffer *fb,
162 int x, int y)
163{
Russell Kingd6a48962017-12-08 12:16:22 +0000164 const struct drm_format_info *format = fb->format;
165 unsigned int num_planes = format->num_planes;
Russell Kingf0b24872016-08-16 22:09:11 +0100166 u32 addr = drm_fb_obj(fb)->dev_addr;
Russell Kingf0b24872016-08-16 22:09:11 +0100167 int i;
168
169 if (num_planes > 3)
170 num_planes = 3;
171
Russell Kingde0ea9a2017-12-08 12:16:22 +0000172 addrs[0] = addr + fb->offsets[0] + y * fb->pitches[0] +
173 x * format->cpp[0];
174
175 y /= format->vsub;
176 x /= format->hsub;
177
178 for (i = 1; i < num_planes; i++)
Russell Kingf0b24872016-08-16 22:09:11 +0100179 addrs[i] = addr + fb->offsets[i] + y * fb->pitches[i] +
Russell Kingd6a48962017-12-08 12:16:22 +0000180 x * format->cpp[i];
Russell Kingf0b24872016-08-16 22:09:11 +0100181 for (; i < 3; i++)
182 addrs[i] = 0;
183}
184
Russell King96f60e32012-08-15 13:59:49 +0100185static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
186 int x, int y, struct armada_regs *regs, bool interlaced)
187{
Russell King96f60e32012-08-15 13:59:49 +0100188 unsigned pitch = fb->pitches[0];
Russell Kingf0b24872016-08-16 22:09:11 +0100189 u32 addrs[3], addr_odd, addr_even;
Russell King96f60e32012-08-15 13:59:49 +0100190 unsigned i = 0;
191
192 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
Ville Syrjälä272725c2016-12-14 23:32:20 +0200193 pitch, x, y, fb->format->cpp[0] * 8);
Russell King96f60e32012-08-15 13:59:49 +0100194
Russell Kingf0b24872016-08-16 22:09:11 +0100195 armada_drm_plane_calc_addrs(addrs, fb, x, y);
196
197 addr_odd = addr_even = addrs[0];
Russell King96f60e32012-08-15 13:59:49 +0100198
199 if (interlaced) {
200 addr_even += pitch;
201 pitch *= 2;
202 }
203
204 /* write offset, base, and pitch */
205 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
206 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
207 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
208
209 return i;
210}
211
Russell King2839d452017-07-07 15:56:20 +0100212static void armada_drm_plane_work_call(struct armada_crtc *dcrtc,
213 struct armada_plane_work *work,
214 void (*fn)(struct armada_crtc *, struct armada_plane_work *))
215{
216 struct armada_plane *dplane = drm_to_armada_plane(work->plane);
Russell Kingeb19be52017-07-08 10:16:53 +0100217 struct drm_pending_vblank_event *event = work->event;
Russell Kingb972a802017-07-08 10:16:52 +0100218 struct drm_framebuffer *fb = work->old_fb;
Russell King2839d452017-07-07 15:56:20 +0100219
220 if (fn)
221 fn(dcrtc, work);
222 drm_crtc_vblank_put(&dcrtc->crtc);
223
Russell Kingeb19be52017-07-08 10:16:53 +0100224 if (event || fb) {
225 struct drm_device *dev = dcrtc->crtc.dev;
226 unsigned long flags;
227
228 spin_lock_irqsave(&dev->event_lock, flags);
229 if (event)
230 drm_crtc_send_vblank_event(&dcrtc->crtc, event);
231 if (fb)
232 __armada_drm_queue_unref_work(dev, fb);
233 spin_unlock_irqrestore(&dev->event_lock, flags);
234 }
Russell Kingb972a802017-07-08 10:16:52 +0100235
Russell King2839d452017-07-07 15:56:20 +0100236 wake_up(&dplane->frame_wait);
237}
238
Russell King4b5dda82015-08-06 16:37:18 +0100239static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
Russell Kingec6fb152016-07-25 15:16:11 +0100240 struct drm_plane *plane)
Russell King4b5dda82015-08-06 16:37:18 +0100241{
Russell Kingec6fb152016-07-25 15:16:11 +0100242 struct armada_plane *dplane = drm_to_armada_plane(plane);
243 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King4b5dda82015-08-06 16:37:18 +0100244
245 /* Handle any pending frame work. */
Russell King2839d452017-07-07 15:56:20 +0100246 if (work)
247 armada_drm_plane_work_call(dcrtc, work, work->fn);
Russell King4b5dda82015-08-06 16:37:18 +0100248}
249
250int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
Russell Kingeaab0132017-07-07 15:55:53 +0100251 struct armada_plane_work *work)
Russell King4b5dda82015-08-06 16:37:18 +0100252{
Russell Kingeaab0132017-07-07 15:55:53 +0100253 struct armada_plane *plane = drm_to_armada_plane(work->plane);
Russell King4b5dda82015-08-06 16:37:18 +0100254 int ret;
255
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300256 ret = drm_crtc_vblank_get(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100257 if (ret) {
258 DRM_ERROR("failed to acquire vblank counter\n");
259 return ret;
260 }
261
262 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
263 if (ret)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300264 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100265
266 return ret;
267}
268
269int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
270{
271 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
272}
273
Russell Kingd3b84212017-07-07 15:55:40 +0100274void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc,
275 struct armada_plane *dplane)
Russell King7c8f7e12015-06-29 17:52:16 +0100276{
Russell Kingd3b84212017-07-07 15:55:40 +0100277 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King7c8f7e12015-06-29 17:52:16 +0100278
Russell King4a8506d2015-08-07 09:33:05 +0100279 if (work)
Russell King2839d452017-07-07 15:56:20 +0100280 armada_drm_plane_work_call(dcrtc, work, work->cancel);
Russell King7c8f7e12015-06-29 17:52:16 +0100281}
282
Russell King65724a12017-07-07 15:56:24 +0100283static void armada_drm_crtc_finish_frame_work(struct armada_crtc *dcrtc,
Russell Kingeaab0132017-07-07 15:55:53 +0100284 struct armada_plane_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100285{
Russell Kingeaa66272017-07-08 10:22:10 +0100286 kfree(work);
Russell King96f60e32012-08-15 13:59:49 +0100287}
288
Russell King65724a12017-07-07 15:56:24 +0100289static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
290 struct armada_plane_work *work)
291{
Russell King65724a12017-07-07 15:56:24 +0100292 unsigned long flags;
293
294 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell Kingeaa66272017-07-08 10:22:10 +0100295 armada_drm_crtc_update_regs(dcrtc, work->regs);
Russell King65724a12017-07-07 15:56:24 +0100296 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
297
298 armada_drm_crtc_finish_frame_work(dcrtc, work);
299}
300
Russell Kingeaa66272017-07-08 10:22:10 +0100301static struct armada_plane_work *
302armada_drm_crtc_alloc_plane_work(struct drm_plane *plane)
Russell King901bb882017-07-07 15:55:45 +0100303{
Russell Kingeaa66272017-07-08 10:22:10 +0100304 struct armada_plane_work *work;
Russell King901bb882017-07-07 15:55:45 +0100305 int i = 0;
306
307 work = kzalloc(sizeof(*work), GFP_KERNEL);
308 if (!work)
309 return NULL;
310
Russell Kingeaa66272017-07-08 10:22:10 +0100311 work->plane = plane;
312 work->fn = armada_drm_crtc_complete_frame_work;
313 work->cancel = armada_drm_crtc_finish_frame_work;
Russell King901bb882017-07-07 15:55:45 +0100314 armada_reg_queue_end(work->regs, i);
315
316 return work;
317}
318
Russell King96f60e32012-08-15 13:59:49 +0100319static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
320 struct drm_framebuffer *fb, bool force)
321{
Russell Kingeaa66272017-07-08 10:22:10 +0100322 struct armada_plane_work *work;
Russell King96f60e32012-08-15 13:59:49 +0100323
324 if (!fb)
325 return;
326
327 if (force) {
328 /* Display is disabled, so just drop the old fb */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600329 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100330 return;
331 }
332
Russell Kingeaa66272017-07-08 10:22:10 +0100333 work = armada_drm_crtc_alloc_plane_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100334 if (work) {
Russell Kingeaa66272017-07-08 10:22:10 +0100335 work->old_fb = fb;
Russell King96f60e32012-08-15 13:59:49 +0100336
Russell Kingeaa66272017-07-08 10:22:10 +0100337 if (armada_drm_plane_work_queue(dcrtc, work) == 0)
Russell King96f60e32012-08-15 13:59:49 +0100338 return;
339
340 kfree(work);
341 }
342
343 /*
344 * Oops - just drop the reference immediately and hope for
345 * the best. The worst that will happen is the buffer gets
346 * reused before it has finished being displayed.
347 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600348 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100349}
350
351static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
352{
Russell King96f60e32012-08-15 13:59:49 +0100353 /*
354 * Tell the DRM core that vblank IRQs aren't going to happen for
355 * a while. This cleans up any pending vblank events for us.
356 */
Russell King178e5612014-10-11 23:57:04 +0100357 drm_crtc_vblank_off(&dcrtc->crtc);
Russell Kingec6fb152016-07-25 15:16:11 +0100358 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100359}
360
Russell King96f60e32012-08-15 13:59:49 +0100361/* The mode_config.mutex will be held for this call */
362static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
363{
364 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
365
Russell Kingea908ba2016-10-04 22:19:57 +0100366 if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
Russell King96f60e32012-08-15 13:59:49 +0100367 if (dpms_blanked(dpms))
368 armada_drm_vblank_off(dcrtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100369 else if (!IS_ERR(dcrtc->clk))
370 WARN_ON(clk_prepare_enable(dcrtc->clk));
371 dcrtc->dpms = dpms;
372 armada_drm_crtc_update(dcrtc);
373 if (!dpms_blanked(dpms))
Russell King178e5612014-10-11 23:57:04 +0100374 drm_crtc_vblank_on(&dcrtc->crtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100375 else if (!IS_ERR(dcrtc->clk))
376 clk_disable_unprepare(dcrtc->clk);
377 } else if (dcrtc->dpms != dpms) {
378 dcrtc->dpms = dpms;
Russell King96f60e32012-08-15 13:59:49 +0100379 }
380}
381
382/*
383 * Prepare for a mode set. Turn off overlay to ensure that we don't end
384 * up with the overlay size being bigger than the active screen size.
385 * We rely upon X refreshing this state after the mode set has completed.
386 *
387 * The mode_config.mutex will be held for this call
388 */
389static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
390{
391 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
392 struct drm_plane *plane;
393
394 /*
395 * If we have an overlay plane associated with this CRTC, disable
396 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100397 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100398 */
399 plane = dcrtc->plane;
Russell Kingf8e14062015-06-29 17:52:42 +0100400 if (plane)
401 drm_plane_force_disable(plane);
Russell King96f60e32012-08-15 13:59:49 +0100402}
403
404/* The mode_config.mutex will be held for this call */
405static void armada_drm_crtc_commit(struct drm_crtc *crtc)
406{
407 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
408
409 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
410 dcrtc->dpms = DRM_MODE_DPMS_ON;
411 armada_drm_crtc_update(dcrtc);
412 }
413}
414
415/* The mode_config.mutex will be held for this call */
416static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
417 const struct drm_display_mode *mode, struct drm_display_mode *adj)
418{
Russell King96f60e32012-08-15 13:59:49 +0100419 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
420 int ret;
421
422 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100423 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100424 adj->flags & DRM_MODE_FLAG_INTERLACE)
425 return false;
426
427 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100428 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100429 if (ret)
430 return false;
431
432 return true;
433}
434
Shawn Guo5922a7d2017-02-07 17:16:18 +0800435/* These are locked by dev->vbl_lock */
436static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
437{
438 if (dcrtc->irq_ena & mask) {
439 dcrtc->irq_ena &= ~mask;
440 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
441 }
442}
443
444static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
445{
446 if ((dcrtc->irq_ena & mask) != mask) {
447 dcrtc->irq_ena |= mask;
448 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
449 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
450 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
451 }
452}
453
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100454static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100455{
Russell King96f60e32012-08-15 13:59:49 +0100456 void __iomem *base = dcrtc->base;
Russell King4a8506d2015-08-07 09:33:05 +0100457 struct drm_plane *ovl_plane;
Russell King96f60e32012-08-15 13:59:49 +0100458
459 if (stat & DMA_FF_UNDERFLOW)
460 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
461 if (stat & GRA_FF_UNDERFLOW)
462 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
463
464 if (stat & VSYNC_IRQ)
Gustavo Padovan0ac28c52016-07-04 21:04:48 -0300465 drm_crtc_handle_vblank(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100466
Russell King4a8506d2015-08-07 09:33:05 +0100467 ovl_plane = dcrtc->plane;
Russell Kingec6fb152016-07-25 15:16:11 +0100468 if (ovl_plane)
469 armada_drm_plane_work_run(dcrtc, ovl_plane);
Russell King96f60e32012-08-15 13:59:49 +0100470
Russell Kinga3f6a182017-07-08 10:16:48 +0100471 spin_lock(&dcrtc->irq_lock);
Russell King96f60e32012-08-15 13:59:49 +0100472 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
473 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
474 uint32_t val;
475
476 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
477 writel_relaxed(dcrtc->v[i].spu_v_h_total,
478 base + LCD_SPUT_V_H_TOTAL);
479
480 val = readl_relaxed(base + LCD_SPU_ADV_REG);
481 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
482 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100483 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100484 }
Russell King662af0d2013-05-19 10:55:17 +0100485
486 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
487 writel_relaxed(dcrtc->cursor_hw_pos,
488 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
489 writel_relaxed(dcrtc->cursor_hw_sz,
490 base + LCD_SPU_HWC_HPXL_VLN);
491 armada_updatel(CFG_HWC_ENA,
492 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
493 base + LCD_SPU_DMA_CTRL0);
494 dcrtc->cursor_update = false;
495 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
496 }
497
Russell King96f60e32012-08-15 13:59:49 +0100498 spin_unlock(&dcrtc->irq_lock);
499
Russell Kingec6fb152016-07-25 15:16:11 +0100500 if (stat & GRA_FRAME_IRQ)
501 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100502}
503
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100504static irqreturn_t armada_drm_irq(int irq, void *arg)
505{
506 struct armada_crtc *dcrtc = arg;
507 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
508
509 /*
510 * This is rediculous - rather than writing bits to clear, we
511 * have to set the actual status register value. This is racy.
512 */
513 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
514
Russell Kingc8a220c2016-05-17 13:51:08 +0100515 trace_armada_drm_irq(&dcrtc->crtc, stat);
516
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100517 /* Mask out those interrupts we haven't enabled */
518 v = stat & dcrtc->irq_ena;
519
520 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
521 armada_drm_crtc_irq(dcrtc, stat);
522 return IRQ_HANDLED;
523 }
524 return IRQ_NONE;
525}
526
Russell King96f60e32012-08-15 13:59:49 +0100527static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
528{
529 struct drm_display_mode *adj = &dcrtc->crtc.mode;
530 uint32_t val = 0;
531
532 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
533 val |= CFG_CSC_YUV_CCIR709;
534 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
535 val |= CFG_CSC_RGB_STUDIO;
536
537 /*
538 * In auto mode, set the colorimetry, based upon the HDMI spec.
539 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
540 * ITU601. It may be more appropriate to set this depending on
541 * the source - but what if the graphic frame is YUV and the
542 * video frame is RGB?
543 */
544 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
545 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
546 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
547 if (dcrtc->csc_yuv_mode == CSC_AUTO)
548 val |= CFG_CSC_YUV_CCIR709;
549 }
550
551 /*
552 * We assume we're connected to a TV-like device, so the YUV->RGB
553 * conversion should produce a limited range. We should set this
554 * depending on the connectors attached to this CRTC, and what
555 * kind of device they report being connected.
556 */
557 if (dcrtc->csc_rgb_mode == CSC_AUTO)
558 val |= CFG_CSC_RGB_STUDIO;
559
560 return val;
561}
562
Russell King37af35c2016-08-16 22:09:09 +0100563static void armada_drm_primary_set(struct drm_crtc *crtc,
564 struct drm_plane *plane, int x, int y)
565{
566 struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
567 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King2925db02016-08-16 22:09:10 +0100568 struct armada_regs regs[8];
Russell King37af35c2016-08-16 22:09:09 +0100569 bool interlaced = dcrtc->interlaced;
570 unsigned i;
Russell King2925db02016-08-16 22:09:10 +0100571 u32 ctrl0;
Russell King37af35c2016-08-16 22:09:09 +0100572
573 i = armada_drm_crtc_calc_fb(plane->fb, x, y, regs, interlaced);
574
Russell King2925db02016-08-16 22:09:10 +0100575 armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell King37af35c2016-08-16 22:09:09 +0100576 armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
577 armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
578
579 ctrl0 = state->ctrl0;
580 if (interlaced)
581 ctrl0 |= CFG_GRA_FTOGGLE;
582
583 armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
584 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
585 CFG_SWAPYU | CFG_YUV2RGB) |
586 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
587 LCD_SPU_DMA_CTRL0);
588 armada_reg_queue_end(regs, i);
589 armada_drm_crtc_update_regs(dcrtc, regs);
590}
591
Russell King96f60e32012-08-15 13:59:49 +0100592/* The mode_config.mutex will be held for this call */
593static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
594 struct drm_display_mode *mode, struct drm_display_mode *adj,
595 int x, int y, struct drm_framebuffer *old_fb)
596{
Russell King96f60e32012-08-15 13:59:49 +0100597 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
598 struct armada_regs regs[17];
599 uint32_t lm, rm, tm, bm, val, sclk;
600 unsigned long flags;
601 unsigned i;
602 bool interlaced;
603
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600604 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100605
606 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
607
Russell King8be523d2016-08-16 22:09:08 +0100608 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
609 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
610 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100611
Russell King8be523d2016-08-16 22:09:08 +0100612 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
613 val |= CFG_PALETTE_ENA;
614
615 drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
616 drm_to_armada_plane(crtc->primary)->state.src_hw =
617 drm_to_armada_plane(crtc->primary)->state.dst_hw =
Russell King37af35c2016-08-16 22:09:09 +0100618 adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
Russell King8be523d2016-08-16 22:09:08 +0100619 drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
620
Russell King37af35c2016-08-16 22:09:09 +0100621 i = 0;
Russell King96f60e32012-08-15 13:59:49 +0100622 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
623 lm = adj->crtc_htotal - adj->crtc_hsync_end;
624 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
625 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
626
627 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
628 adj->crtc_hdisplay,
629 adj->crtc_hsync_start,
630 adj->crtc_hsync_end,
631 adj->crtc_htotal, lm, rm);
632 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
633 adj->crtc_vdisplay,
634 adj->crtc_vsync_start,
635 adj->crtc_vsync_end,
636 adj->crtc_vtotal, tm, bm);
637
638 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100639 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
640 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100641
Russell King178e5612014-10-11 23:57:04 +0100642 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100643
Russell King96f60e32012-08-15 13:59:49 +0100644 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
645 if (val != dcrtc->dumb_ctrl) {
646 dcrtc->dumb_ctrl = val;
647 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
648 }
649
Russell Kinge0ac5e92015-06-29 18:01:38 +0100650 /*
651 * If we are blanked, we would have disabled the clock. Re-enable
652 * it so that compute_clock() does the right thing.
653 */
654 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
655 WARN_ON(clk_prepare_enable(dcrtc->clk));
656
Russell King96f60e32012-08-15 13:59:49 +0100657 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100658 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100659
660 /* Ensure graphic fifo is enabled */
661 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
662 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
663
664 if (interlaced ^ dcrtc->interlaced) {
665 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300666 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100667 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300668 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100669 dcrtc->interlaced = interlaced;
670 }
671
672 spin_lock_irqsave(&dcrtc->irq_lock, flags);
673
674 /* Even interlaced/progressive frame */
675 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
676 adj->crtc_htotal;
677 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
678 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100679 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100680 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100681
682 if (interlaced) {
683 /* Odd interlaced frame */
684 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
685 (1 << 16);
686 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
687 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100688 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100689 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100690 } else {
691 dcrtc->v[0] = dcrtc->v[1];
692 }
693
694 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
695
696 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King96f60e32012-08-15 13:59:49 +0100697 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
698 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
699 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
700 LCD_SPUT_V_H_TOTAL);
701
Russell King42e62ba2014-04-22 15:24:03 +0100702 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100703 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
704 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
705 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100706 }
Russell King96f60e32012-08-15 13:59:49 +0100707
Russell King96f60e32012-08-15 13:59:49 +0100708 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
709 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
710
711 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
712 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
713 armada_reg_queue_end(regs, i);
714
715 armada_drm_crtc_update_regs(dcrtc, regs);
Russell King37af35c2016-08-16 22:09:09 +0100716
717 armada_drm_primary_set(crtc, crtc->primary, x, y);
Russell King96f60e32012-08-15 13:59:49 +0100718 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
719
720 armada_drm_crtc_update(dcrtc);
721
Russell King178e5612014-10-11 23:57:04 +0100722 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100723 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
724
725 return 0;
726}
727
728/* The mode_config.mutex will be held for this call */
729static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
730 struct drm_framebuffer *old_fb)
731{
732 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
733 struct armada_regs regs[4];
734 unsigned i;
735
Matt Roperf4510a22014-04-01 15:22:40 -0700736 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100737 dcrtc->interlaced);
738 armada_reg_queue_end(regs, i);
739
740 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100741 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
742 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100743
744 /* Take a reference to the new fb as we're using it */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600745 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100746
747 /* Update the base in the CRTC */
748 armada_drm_crtc_update_regs(dcrtc, regs);
749
750 /* Drop our previously held reference */
751 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
752
753 return 0;
754}
755
Russell King96f60e32012-08-15 13:59:49 +0100756/* The mode_config.mutex will be held for this call */
757static void armada_drm_crtc_disable(struct drm_crtc *crtc)
758{
Russell King96f60e32012-08-15 13:59:49 +0100759 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King28b30432017-07-08 10:16:40 +0100760
761 /* Disable our primary plane when we disable the CRTC. */
762 crtc->primary->funcs->disable_plane(crtc->primary, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100763}
764
765static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
766 .dpms = armada_drm_crtc_dpms,
767 .prepare = armada_drm_crtc_prepare,
768 .commit = armada_drm_crtc_commit,
769 .mode_fixup = armada_drm_crtc_mode_fixup,
770 .mode_set = armada_drm_crtc_mode_set,
771 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100772 .disable = armada_drm_crtc_disable,
773};
774
Russell King662af0d2013-05-19 10:55:17 +0100775static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
776 unsigned stride, unsigned width, unsigned height)
777{
778 uint32_t addr;
779 unsigned y;
780
781 addr = SRAM_HWC32_RAM1;
782 for (y = 0; y < height; y++) {
783 uint32_t *p = &pix[y * stride];
784 unsigned x;
785
786 for (x = 0; x < width; x++, p++) {
787 uint32_t val = *p;
788
789 val = (val & 0xff00ff00) |
790 (val & 0x000000ff) << 16 |
791 (val & 0x00ff0000) >> 16;
792
793 writel_relaxed(val,
794 base + LCD_SPU_SRAM_WRDAT);
795 writel_relaxed(addr | SRAM_WRITE,
796 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100797 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100798 addr += 1;
799 if ((addr & 0x00ff) == 0)
800 addr += 0xf00;
801 if ((addr & 0x30ff) == 0)
802 addr = SRAM_HWC32_RAM2;
803 }
804 }
805}
806
807static void armada_drm_crtc_cursor_tran(void __iomem *base)
808{
809 unsigned addr;
810
811 for (addr = 0; addr < 256; addr++) {
812 /* write the default value */
813 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
814 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
815 base + LCD_SPU_SRAM_CTRL);
816 }
817}
818
819static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
820{
821 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
822 uint32_t yoff, yscr, h = dcrtc->cursor_h;
823 uint32_t para1;
824
825 /*
826 * Calculate the visible width and height of the cursor,
827 * screen position, and the position in the cursor bitmap.
828 */
829 if (dcrtc->cursor_x < 0) {
830 xoff = -dcrtc->cursor_x;
831 xscr = 0;
832 w -= min(xoff, w);
833 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
834 xoff = 0;
835 xscr = dcrtc->cursor_x;
836 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
837 } else {
838 xoff = 0;
839 xscr = dcrtc->cursor_x;
840 }
841
842 if (dcrtc->cursor_y < 0) {
843 yoff = -dcrtc->cursor_y;
844 yscr = 0;
845 h -= min(yoff, h);
846 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
847 yoff = 0;
848 yscr = dcrtc->cursor_y;
849 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
850 } else {
851 yoff = 0;
852 yscr = dcrtc->cursor_y;
853 }
854
855 /* On interlaced modes, the vertical cursor size must be halved */
856 s = dcrtc->cursor_w;
857 if (dcrtc->interlaced) {
858 s *= 2;
859 yscr /= 2;
860 h /= 2;
861 }
862
863 if (!dcrtc->cursor_obj || !h || !w) {
864 spin_lock_irq(&dcrtc->irq_lock);
865 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
866 dcrtc->cursor_update = false;
867 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
868 spin_unlock_irq(&dcrtc->irq_lock);
869 return 0;
870 }
871
872 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
873 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
874 dcrtc->base + LCD_SPU_SRAM_PARA1);
875
876 /*
877 * Initialize the transparency if the SRAM was powered down.
878 * We must also reload the cursor data as well.
879 */
880 if (!(para1 & CFG_CSB_256x32)) {
881 armada_drm_crtc_cursor_tran(dcrtc->base);
882 reload = true;
883 }
884
885 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
886 spin_lock_irq(&dcrtc->irq_lock);
887 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
888 dcrtc->cursor_update = false;
889 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
890 spin_unlock_irq(&dcrtc->irq_lock);
891 reload = true;
892 }
893 if (reload) {
894 struct armada_gem_object *obj = dcrtc->cursor_obj;
895 uint32_t *pix;
896 /* Set the top-left corner of the cursor image */
897 pix = obj->addr;
898 pix += yoff * s + xoff;
899 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
900 }
901
902 /* Reload the cursor position, size and enable in the IRQ handler */
903 spin_lock_irq(&dcrtc->irq_lock);
904 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
905 dcrtc->cursor_hw_sz = h << 16 | w;
906 dcrtc->cursor_update = true;
907 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
908 spin_unlock_irq(&dcrtc->irq_lock);
909
910 return 0;
911}
912
913static void cursor_update(void *data)
914{
915 armada_drm_crtc_cursor_update(data, true);
916}
917
918static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
919 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
920{
Russell King662af0d2013-05-19 10:55:17 +0100921 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100922 struct armada_gem_object *obj = NULL;
923 int ret;
924
925 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100926 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100927 return -ENXIO;
928
929 if (handle && w > 0 && h > 0) {
930 /* maximum size is 64x32 or 32x64 */
931 if (w > 64 || h > 64 || (w > 32 && h > 32))
932 return -ENOMEM;
933
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100934 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100935 if (!obj)
936 return -ENOENT;
937
938 /* Must be a kernel-mapped object */
939 if (!obj->addr) {
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600940 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100941 return -EINVAL;
942 }
943
944 if (obj->obj.size < w * h * 4) {
945 DRM_ERROR("buffer is too small\n");
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600946 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100947 return -ENOMEM;
948 }
949 }
950
Russell King662af0d2013-05-19 10:55:17 +0100951 if (dcrtc->cursor_obj) {
952 dcrtc->cursor_obj->update = NULL;
953 dcrtc->cursor_obj->update_data = NULL;
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600954 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100955 }
956 dcrtc->cursor_obj = obj;
957 dcrtc->cursor_w = w;
958 dcrtc->cursor_h = h;
959 ret = armada_drm_crtc_cursor_update(dcrtc, true);
960 if (obj) {
961 obj->update_data = dcrtc;
962 obj->update = cursor_update;
963 }
Russell King662af0d2013-05-19 10:55:17 +0100964
965 return ret;
966}
967
968static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
969{
Russell King662af0d2013-05-19 10:55:17 +0100970 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100971 int ret;
972
973 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100974 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100975 return -EFAULT;
976
Russell King662af0d2013-05-19 10:55:17 +0100977 dcrtc->cursor_x = x;
978 dcrtc->cursor_y = y;
979 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +0100980
981 return ret;
982}
983
Russell King96f60e32012-08-15 13:59:49 +0100984static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
985{
986 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
987 struct armada_private *priv = crtc->dev->dev_private;
988
Russell King662af0d2013-05-19 10:55:17 +0100989 if (dcrtc->cursor_obj)
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600990 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100991
Russell King96f60e32012-08-15 13:59:49 +0100992 priv->dcrtc[dcrtc->num] = NULL;
993 drm_crtc_cleanup(&dcrtc->crtc);
994
995 if (!IS_ERR(dcrtc->clk))
996 clk_disable_unprepare(dcrtc->clk);
997
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100998 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
999
Russell King9611cb92014-06-15 11:21:23 +01001000 of_node_put(dcrtc->crtc.port);
1001
Russell King96f60e32012-08-15 13:59:49 +01001002 kfree(dcrtc);
1003}
1004
1005/*
1006 * The mode_config lock is held here, to prevent races between this
1007 * and a mode_set.
1008 */
1009static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01001010 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags,
1011 struct drm_modeset_acquire_ctx *ctx)
Russell King96f60e32012-08-15 13:59:49 +01001012{
1013 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell Kingeaa66272017-07-08 10:22:10 +01001014 struct armada_plane_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001015 unsigned i;
1016 int ret;
1017
1018 /* We don't support changing the pixel format */
Ville Syrjälädbd4d572016-11-18 21:53:10 +02001019 if (fb->format != crtc->primary->fb->format)
Russell King96f60e32012-08-15 13:59:49 +01001020 return -EINVAL;
1021
Russell Kingeaa66272017-07-08 10:22:10 +01001022 work = armada_drm_crtc_alloc_plane_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001023 if (!work)
1024 return -ENOMEM;
1025
Russell Kingeaa66272017-07-08 10:22:10 +01001026 work->event = event;
1027 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001028
1029 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1030 dcrtc->interlaced);
1031 armada_reg_queue_end(work->regs, i);
1032
1033 /*
Russell Kingc5488302014-10-11 23:53:35 +01001034 * Ensure that we hold a reference on the new framebuffer.
1035 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001036 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001037 drm_framebuffer_get(fb);
Russell King96f60e32012-08-15 13:59:49 +01001038
Russell Kingeaa66272017-07-08 10:22:10 +01001039 ret = armada_drm_plane_work_queue(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +01001040 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001041 /* Undo our reference above */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001042 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +01001043 kfree(work);
1044 return ret;
1045 }
1046
1047 /*
1048 * Don't take a reference on the new framebuffer;
1049 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1050 * will _not_ drop that reference on successful return from this
1051 * function. Simply mark this new framebuffer as the current one.
1052 */
Matt Roperf4510a22014-04-01 15:22:40 -07001053 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001054
1055 /*
1056 * Finally, if the display is blanked, we won't receive an
1057 * interrupt, so complete it now.
1058 */
Russell King4b5dda82015-08-06 16:37:18 +01001059 if (dpms_blanked(dcrtc->dpms))
Russell Kingec6fb152016-07-25 15:16:11 +01001060 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001061
1062 return 0;
1063}
1064
1065static int
1066armada_drm_crtc_set_property(struct drm_crtc *crtc,
1067 struct drm_property *property, uint64_t val)
1068{
1069 struct armada_private *priv = crtc->dev->dev_private;
1070 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1071 bool update_csc = false;
1072
1073 if (property == priv->csc_yuv_prop) {
1074 dcrtc->csc_yuv_mode = val;
1075 update_csc = true;
1076 } else if (property == priv->csc_rgb_prop) {
1077 dcrtc->csc_rgb_mode = val;
1078 update_csc = true;
1079 }
1080
1081 if (update_csc) {
1082 uint32_t val;
1083
1084 val = dcrtc->spu_iopad_ctrl |
1085 armada_drm_crtc_calculate_csc(dcrtc);
1086 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1087 }
1088
1089 return 0;
1090}
1091
Shawn Guo5922a7d2017-02-07 17:16:18 +08001092/* These are called under the vbl_lock. */
1093static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
1094{
1095 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1096
1097 armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
1098 return 0;
1099}
1100
1101static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
1102{
1103 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1104
1105 armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
1106}
1107
Ville Syrjäläa02fb902015-12-15 12:20:59 +01001108static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001109 .cursor_set = armada_drm_crtc_cursor_set,
1110 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001111 .destroy = armada_drm_crtc_destroy,
1112 .set_config = drm_crtc_helper_set_config,
1113 .page_flip = armada_drm_crtc_page_flip,
1114 .set_property = armada_drm_crtc_set_property,
Shawn Guo5922a7d2017-02-07 17:16:18 +08001115 .enable_vblank = armada_drm_crtc_enable_vblank,
1116 .disable_vblank = armada_drm_crtc_disable_vblank,
Russell King96f60e32012-08-15 13:59:49 +01001117};
1118
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001119int armada_drm_plane_disable(struct drm_plane *plane,
1120 struct drm_modeset_acquire_ctx *ctx)
Russell King28b30432017-07-08 10:16:40 +01001121{
1122 struct armada_plane *dplane = drm_to_armada_plane(plane);
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001123 struct armada_crtc *dcrtc;
Russell Kingd76dcc72017-07-08 10:16:47 +01001124 u32 sram_para1, enable_mask;
Russell King28b30432017-07-08 10:16:40 +01001125
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001126 if (!plane->crtc)
1127 return 0;
1128
Russell King28b30432017-07-08 10:16:40 +01001129 /*
1130 * Drop our reference on any framebuffer attached to this plane.
1131 * We don't need to NULL this out as drm_plane_force_disable(),
1132 * and __setplane_internal() will do so for an overlay plane, and
1133 * __drm_helper_disable_unused_functions() will do so for the
1134 * primary plane.
1135 */
1136 if (plane->fb)
1137 drm_framebuffer_put(plane->fb);
1138
1139 /* Power down most RAMs and FIFOs if this is the primary plane */
1140 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
1141 sram_para1 = CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1142 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell Kingd76dcc72017-07-08 10:16:47 +01001143 enable_mask = CFG_GRA_ENA;
Russell King28b30432017-07-08 10:16:40 +01001144 } else {
1145 /* Power down the Y/U/V FIFOs */
1146 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
Russell Kingd76dcc72017-07-08 10:16:47 +01001147 enable_mask = CFG_DMA_ENA;
Russell King28b30432017-07-08 10:16:40 +01001148 }
1149
Russell Kingd76dcc72017-07-08 10:16:47 +01001150 dplane->state.ctrl0 &= ~enable_mask;
1151
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001152 dcrtc = drm_to_armada_crtc(plane->crtc);
1153
Russell King28b30432017-07-08 10:16:40 +01001154 /* Wait for any preceding work to complete, but don't wedge */
1155 if (WARN_ON(!armada_drm_plane_work_wait(dplane, HZ)))
1156 armada_drm_plane_work_cancel(dcrtc, dplane);
1157
1158 spin_lock_irq(&dcrtc->irq_lock);
Russell Kingd76dcc72017-07-08 10:16:47 +01001159 armada_updatel(0, enable_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
Russell King28b30432017-07-08 10:16:40 +01001160 spin_unlock_irq(&dcrtc->irq_lock);
1161
1162 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
Russell King28b30432017-07-08 10:16:40 +01001163
Russell King28b30432017-07-08 10:16:40 +01001164 return 0;
1165}
1166
Russell Kingde323012015-07-15 18:11:24 +01001167static const struct drm_plane_funcs armada_primary_plane_funcs = {
1168 .update_plane = drm_primary_helper_update,
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001169 .disable_plane = armada_drm_plane_disable,
Russell Kingde323012015-07-15 18:11:24 +01001170 .destroy = drm_primary_helper_destroy,
1171};
1172
Russell King5740d272015-07-15 18:11:25 +01001173int armada_drm_plane_init(struct armada_plane *plane)
1174{
1175 init_waitqueue_head(&plane->frame_wait);
1176
1177 return 0;
1178}
1179
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301180static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001181 { CSC_AUTO, "Auto" },
1182 { CSC_YUV_CCIR601, "CCIR601" },
1183 { CSC_YUV_CCIR709, "CCIR709" },
1184};
1185
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301186static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001187 { CSC_AUTO, "Auto" },
1188 { CSC_RGB_COMPUTER, "Computer system" },
1189 { CSC_RGB_STUDIO, "Studio" },
1190};
1191
1192static int armada_drm_crtc_create_properties(struct drm_device *dev)
1193{
1194 struct armada_private *priv = dev->dev_private;
1195
1196 if (priv->csc_yuv_prop)
1197 return 0;
1198
1199 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1200 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1201 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1202 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1203 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1204 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1205
1206 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1207 return -ENOMEM;
1208
1209 return 0;
1210}
1211
Russell King0fb29702015-06-06 21:46:53 +01001212static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001213 struct resource *res, int irq, const struct armada_variant *variant,
1214 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001215{
Russell Kingd8c96082014-04-22 11:10:15 +01001216 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001217 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001218 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001219 void __iomem *base;
1220 int ret;
1221
Russell Kingd8c96082014-04-22 11:10:15 +01001222 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001223 if (ret)
1224 return ret;
1225
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001226 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001227 if (IS_ERR(base))
1228 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001229
1230 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1231 if (!dcrtc) {
1232 DRM_ERROR("failed to allocate Armada crtc\n");
1233 return -ENOMEM;
1234 }
1235
Russell Kingd8c96082014-04-22 11:10:15 +01001236 if (dev != drm->dev)
1237 dev_set_drvdata(dev, dcrtc);
1238
Russell King42e62ba2014-04-22 15:24:03 +01001239 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001240 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001241 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001242 dcrtc->clk = ERR_PTR(-EINVAL);
1243 dcrtc->csc_yuv_mode = CSC_AUTO;
1244 dcrtc->csc_rgb_mode = CSC_AUTO;
1245 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1246 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1247 spin_lock_init(&dcrtc->irq_lock);
1248 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +01001249
1250 /* Initialize some registers which we don't otherwise set */
1251 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1252 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1253 writel_relaxed(dcrtc->spu_iopad_ctrl,
1254 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1255 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1256 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1257 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1258 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1259 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001260 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1261 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001262
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001263 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1264 dcrtc);
Russell King33cd3c02017-12-08 12:16:22 +00001265 if (ret < 0)
1266 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001267
Russell King42e62ba2014-04-22 15:24:03 +01001268 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001269 ret = dcrtc->variant->init(dcrtc, dev);
Russell King33cd3c02017-12-08 12:16:22 +00001270 if (ret)
1271 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001272 }
1273
1274 /* Ensure AXI pipeline is enabled */
1275 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1276
1277 priv->dcrtc[dcrtc->num] = dcrtc;
1278
Russell King9611cb92014-06-15 11:21:23 +01001279 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001280
Russell Kingde323012015-07-15 18:11:24 +01001281 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King33cd3c02017-12-08 12:16:22 +00001282 if (!primary) {
1283 ret = -ENOMEM;
1284 goto err_crtc;
1285 }
Russell King1c914ce2015-07-15 18:11:24 +01001286
Russell King5740d272015-07-15 18:11:25 +01001287 ret = armada_drm_plane_init(primary);
1288 if (ret) {
1289 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001290 goto err_crtc;
Russell King5740d272015-07-15 18:11:25 +01001291 }
1292
Russell Kingde323012015-07-15 18:11:24 +01001293 ret = drm_universal_plane_init(drm, &primary->base, 0,
1294 &armada_primary_plane_funcs,
1295 armada_primary_formats,
1296 ARRAY_SIZE(armada_primary_formats),
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001297 NULL,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001298 DRM_PLANE_TYPE_PRIMARY, NULL);
Russell Kingde323012015-07-15 18:11:24 +01001299 if (ret) {
1300 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001301 goto err_crtc;
Russell Kingde323012015-07-15 18:11:24 +01001302 }
1303
1304 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001305 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001306 if (ret)
1307 goto err_crtc_init;
1308
Russell King96f60e32012-08-15 13:59:49 +01001309 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1310
1311 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1312 dcrtc->csc_yuv_mode);
1313 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1314 dcrtc->csc_rgb_mode);
1315
Russell Kingd8c96082014-04-22 11:10:15 +01001316 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001317
1318err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001319 primary->base.funcs->destroy(&primary->base);
Russell King33cd3c02017-12-08 12:16:22 +00001320err_crtc:
1321 kfree(dcrtc);
1322
Russell King1c914ce2015-07-15 18:11:24 +01001323 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001324}
Russell Kingd8c96082014-04-22 11:10:15 +01001325
1326static int
1327armada_lcd_bind(struct device *dev, struct device *master, void *data)
1328{
1329 struct platform_device *pdev = to_platform_device(dev);
1330 struct drm_device *drm = data;
1331 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1332 int irq = platform_get_irq(pdev, 0);
1333 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001334 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001335
1336 if (irq < 0)
1337 return irq;
1338
1339 if (!dev->of_node) {
1340 const struct platform_device_id *id;
1341
1342 id = platform_get_device_id(pdev);
1343 if (!id)
1344 return -ENXIO;
1345
1346 variant = (const struct armada_variant *)id->driver_data;
1347 } else {
1348 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001349 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001350
1351 match = of_match_device(dev->driver->of_match_table, dev);
1352 if (!match)
1353 return -ENXIO;
1354
Russell King9611cb92014-06-15 11:21:23 +01001355 np = of_get_child_by_name(parent, "ports");
1356 if (np)
1357 parent = np;
1358 port = of_get_child_by_name(parent, "port");
1359 of_node_put(np);
1360 if (!port) {
Rob Herring4bf99142017-07-18 16:43:04 -05001361 dev_err(dev, "no port node found in %pOF\n", parent);
Russell King9611cb92014-06-15 11:21:23 +01001362 return -ENXIO;
1363 }
1364
Russell Kingd8c96082014-04-22 11:10:15 +01001365 variant = match->data;
1366 }
1367
Russell King9611cb92014-06-15 11:21:23 +01001368 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001369}
1370
1371static void
1372armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1373{
1374 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1375
1376 armada_drm_crtc_destroy(&dcrtc->crtc);
1377}
1378
1379static const struct component_ops armada_lcd_ops = {
1380 .bind = armada_lcd_bind,
1381 .unbind = armada_lcd_unbind,
1382};
1383
1384static int armada_lcd_probe(struct platform_device *pdev)
1385{
1386 return component_add(&pdev->dev, &armada_lcd_ops);
1387}
1388
1389static int armada_lcd_remove(struct platform_device *pdev)
1390{
1391 component_del(&pdev->dev, &armada_lcd_ops);
1392 return 0;
1393}
1394
Arvind Yadav85909712017-06-20 10:44:33 +05301395static const struct of_device_id armada_lcd_of_match[] = {
Russell Kingd8c96082014-04-22 11:10:15 +01001396 {
1397 .compatible = "marvell,dove-lcd",
1398 .data = &armada510_ops,
1399 },
1400 {}
1401};
1402MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1403
1404static const struct platform_device_id armada_lcd_platform_ids[] = {
1405 {
1406 .name = "armada-lcd",
1407 .driver_data = (unsigned long)&armada510_ops,
1408 }, {
1409 .name = "armada-510-lcd",
1410 .driver_data = (unsigned long)&armada510_ops,
1411 },
1412 { },
1413};
1414MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1415
1416struct platform_driver armada_lcd_platform_driver = {
1417 .probe = armada_lcd_probe,
1418 .remove = armada_lcd_remove,
1419 .driver = {
1420 .name = "armada-lcd",
1421 .owner = THIS_MODULE,
1422 .of_match_table = armada_lcd_of_match,
1423 },
1424 .id_table = armada_lcd_platform_ids,
1425};