blob: c38a1409a14ecce88f35f023044ac8602f72df9a [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 Kingd9241552017-07-08 10:22:25 +0100217 struct drm_pending_vblank_event *event;
218 struct drm_framebuffer *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 Kingd9241552017-07-08 10:22:25 +0100224 event = work->event;
225 fb = work->old_fb;
Russell Kingeb19be52017-07-08 10:16:53 +0100226 if (event || fb) {
227 struct drm_device *dev = dcrtc->crtc.dev;
228 unsigned long flags;
229
230 spin_lock_irqsave(&dev->event_lock, flags);
231 if (event)
232 drm_crtc_send_vblank_event(&dcrtc->crtc, event);
233 if (fb)
234 __armada_drm_queue_unref_work(dev, fb);
235 spin_unlock_irqrestore(&dev->event_lock, flags);
236 }
Russell Kingb972a802017-07-08 10:16:52 +0100237
Russell Kingd9241552017-07-08 10:22:25 +0100238 if (work->need_kfree)
239 kfree(work);
240
Russell King2839d452017-07-07 15:56:20 +0100241 wake_up(&dplane->frame_wait);
242}
243
Russell King4b5dda82015-08-06 16:37:18 +0100244static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
Russell Kingec6fb152016-07-25 15:16:11 +0100245 struct drm_plane *plane)
Russell King4b5dda82015-08-06 16:37:18 +0100246{
Russell Kingec6fb152016-07-25 15:16:11 +0100247 struct armada_plane *dplane = drm_to_armada_plane(plane);
248 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King4b5dda82015-08-06 16:37:18 +0100249
250 /* Handle any pending frame work. */
Russell King2839d452017-07-07 15:56:20 +0100251 if (work)
252 armada_drm_plane_work_call(dcrtc, work, work->fn);
Russell King4b5dda82015-08-06 16:37:18 +0100253}
254
255int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
Russell Kingeaab0132017-07-07 15:55:53 +0100256 struct armada_plane_work *work)
Russell King4b5dda82015-08-06 16:37:18 +0100257{
Russell Kingeaab0132017-07-07 15:55:53 +0100258 struct armada_plane *plane = drm_to_armada_plane(work->plane);
Russell King4b5dda82015-08-06 16:37:18 +0100259 int ret;
260
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300261 ret = drm_crtc_vblank_get(&dcrtc->crtc);
Russell Kingc93dfdc2017-07-08 10:22:23 +0100262 if (ret)
Russell King4b5dda82015-08-06 16:37:18 +0100263 return ret;
Russell King4b5dda82015-08-06 16:37:18 +0100264
265 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
266 if (ret)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300267 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100268
269 return ret;
270}
271
272int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
273{
274 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
275}
276
Russell Kingd3b84212017-07-07 15:55:40 +0100277void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc,
278 struct armada_plane *dplane)
Russell King7c8f7e12015-06-29 17:52:16 +0100279{
Russell Kingd3b84212017-07-07 15:55:40 +0100280 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King7c8f7e12015-06-29 17:52:16 +0100281
Russell King4a8506d2015-08-07 09:33:05 +0100282 if (work)
Russell King2839d452017-07-07 15:56:20 +0100283 armada_drm_plane_work_call(dcrtc, work, work->cancel);
Russell King7c8f7e12015-06-29 17:52:16 +0100284}
285
Russell King65724a12017-07-07 15:56:24 +0100286static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
287 struct armada_plane_work *work)
288{
Russell King65724a12017-07-07 15:56:24 +0100289 unsigned long flags;
290
291 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell Kingeaa66272017-07-08 10:22:10 +0100292 armada_drm_crtc_update_regs(dcrtc, work->regs);
Russell King65724a12017-07-07 15:56:24 +0100293 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King65724a12017-07-07 15:56:24 +0100294}
295
Russell King890ca8d2017-07-08 10:22:27 +0100296static void armada_drm_crtc_complete_disable_work(struct armada_crtc *dcrtc,
297 struct armada_plane_work *work)
298{
299 unsigned long flags;
300
301 if (dcrtc->plane == work->plane)
302 dcrtc->plane = NULL;
303
304 spin_lock_irqsave(&dcrtc->irq_lock, flags);
305 armada_drm_crtc_update_regs(dcrtc, work->regs);
306 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
307}
308
Russell Kingeaa66272017-07-08 10:22:10 +0100309static struct armada_plane_work *
310armada_drm_crtc_alloc_plane_work(struct drm_plane *plane)
Russell King901bb882017-07-07 15:55:45 +0100311{
Russell Kingeaa66272017-07-08 10:22:10 +0100312 struct armada_plane_work *work;
Russell King901bb882017-07-07 15:55:45 +0100313 int i = 0;
314
315 work = kzalloc(sizeof(*work), GFP_KERNEL);
316 if (!work)
317 return NULL;
318
Russell Kingeaa66272017-07-08 10:22:10 +0100319 work->plane = plane;
320 work->fn = armada_drm_crtc_complete_frame_work;
Russell Kingd9241552017-07-08 10:22:25 +0100321 work->need_kfree = true;
Russell King901bb882017-07-07 15:55:45 +0100322 armada_reg_queue_end(work->regs, i);
323
324 return work;
325}
326
Russell King96f60e32012-08-15 13:59:49 +0100327static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
328 struct drm_framebuffer *fb, bool force)
329{
Russell Kingeaa66272017-07-08 10:22:10 +0100330 struct armada_plane_work *work;
Russell King96f60e32012-08-15 13:59:49 +0100331
332 if (!fb)
333 return;
334
335 if (force) {
336 /* Display is disabled, so just drop the old fb */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600337 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100338 return;
339 }
340
Russell Kingeaa66272017-07-08 10:22:10 +0100341 work = armada_drm_crtc_alloc_plane_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100342 if (work) {
Russell Kingeaa66272017-07-08 10:22:10 +0100343 work->old_fb = fb;
Russell King96f60e32012-08-15 13:59:49 +0100344
Russell Kingeaa66272017-07-08 10:22:10 +0100345 if (armada_drm_plane_work_queue(dcrtc, work) == 0)
Russell King96f60e32012-08-15 13:59:49 +0100346 return;
347
348 kfree(work);
349 }
350
351 /*
352 * Oops - just drop the reference immediately and hope for
353 * the best. The worst that will happen is the buffer gets
354 * reused before it has finished being displayed.
355 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600356 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +0100357}
358
359static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
360{
Russell King96f60e32012-08-15 13:59:49 +0100361 /*
362 * Tell the DRM core that vblank IRQs aren't going to happen for
363 * a while. This cleans up any pending vblank events for us.
364 */
Russell King178e5612014-10-11 23:57:04 +0100365 drm_crtc_vblank_off(&dcrtc->crtc);
Russell Kingec6fb152016-07-25 15:16:11 +0100366 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100367}
368
Russell King96f60e32012-08-15 13:59:49 +0100369/* The mode_config.mutex will be held for this call */
370static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
371{
372 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
373
Russell Kingea908ba2016-10-04 22:19:57 +0100374 if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
Russell King96f60e32012-08-15 13:59:49 +0100375 if (dpms_blanked(dpms))
376 armada_drm_vblank_off(dcrtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100377 else if (!IS_ERR(dcrtc->clk))
378 WARN_ON(clk_prepare_enable(dcrtc->clk));
379 dcrtc->dpms = dpms;
380 armada_drm_crtc_update(dcrtc);
381 if (!dpms_blanked(dpms))
Russell King178e5612014-10-11 23:57:04 +0100382 drm_crtc_vblank_on(&dcrtc->crtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100383 else if (!IS_ERR(dcrtc->clk))
384 clk_disable_unprepare(dcrtc->clk);
385 } else if (dcrtc->dpms != dpms) {
386 dcrtc->dpms = dpms;
Russell King96f60e32012-08-15 13:59:49 +0100387 }
388}
389
390/*
391 * Prepare for a mode set. Turn off overlay to ensure that we don't end
392 * up with the overlay size being bigger than the active screen size.
393 * We rely upon X refreshing this state after the mode set has completed.
394 *
395 * The mode_config.mutex will be held for this call
396 */
397static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
398{
399 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
400 struct drm_plane *plane;
401
402 /*
403 * If we have an overlay plane associated with this CRTC, disable
404 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100405 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100406 */
407 plane = dcrtc->plane;
Russell King890ca8d2017-07-08 10:22:27 +0100408 if (plane) {
Russell Kingf8e14062015-06-29 17:52:42 +0100409 drm_plane_force_disable(plane);
Russell King890ca8d2017-07-08 10:22:27 +0100410 WARN_ON(!armada_drm_plane_work_wait(drm_to_armada_plane(plane),
411 HZ));
412 }
Russell King96f60e32012-08-15 13:59:49 +0100413}
414
415/* The mode_config.mutex will be held for this call */
416static void armada_drm_crtc_commit(struct drm_crtc *crtc)
417{
418 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
419
420 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
421 dcrtc->dpms = DRM_MODE_DPMS_ON;
422 armada_drm_crtc_update(dcrtc);
423 }
424}
425
426/* The mode_config.mutex will be held for this call */
427static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
428 const struct drm_display_mode *mode, struct drm_display_mode *adj)
429{
Russell King96f60e32012-08-15 13:59:49 +0100430 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
431 int ret;
432
433 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100434 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100435 adj->flags & DRM_MODE_FLAG_INTERLACE)
436 return false;
437
438 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100439 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100440 if (ret)
441 return false;
442
443 return true;
444}
445
Shawn Guo5922a7d2017-02-07 17:16:18 +0800446/* These are locked by dev->vbl_lock */
447static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
448{
449 if (dcrtc->irq_ena & mask) {
450 dcrtc->irq_ena &= ~mask;
451 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
452 }
453}
454
455static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
456{
457 if ((dcrtc->irq_ena & mask) != mask) {
458 dcrtc->irq_ena |= mask;
459 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
460 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
461 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
462 }
463}
464
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100465static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100466{
Russell King96f60e32012-08-15 13:59:49 +0100467 void __iomem *base = dcrtc->base;
Russell King4a8506d2015-08-07 09:33:05 +0100468 struct drm_plane *ovl_plane;
Russell King96f60e32012-08-15 13:59:49 +0100469
470 if (stat & DMA_FF_UNDERFLOW)
471 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
472 if (stat & GRA_FF_UNDERFLOW)
473 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
474
475 if (stat & VSYNC_IRQ)
Gustavo Padovan0ac28c52016-07-04 21:04:48 -0300476 drm_crtc_handle_vblank(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100477
Russell King4a8506d2015-08-07 09:33:05 +0100478 ovl_plane = dcrtc->plane;
Russell Kingec6fb152016-07-25 15:16:11 +0100479 if (ovl_plane)
480 armada_drm_plane_work_run(dcrtc, ovl_plane);
Russell King96f60e32012-08-15 13:59:49 +0100481
Russell Kinga3f6a182017-07-08 10:16:48 +0100482 spin_lock(&dcrtc->irq_lock);
Russell King96f60e32012-08-15 13:59:49 +0100483 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
484 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
485 uint32_t val;
486
487 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
488 writel_relaxed(dcrtc->v[i].spu_v_h_total,
489 base + LCD_SPUT_V_H_TOTAL);
490
491 val = readl_relaxed(base + LCD_SPU_ADV_REG);
492 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
493 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100494 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100495 }
Russell King662af0d2013-05-19 10:55:17 +0100496
497 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
498 writel_relaxed(dcrtc->cursor_hw_pos,
499 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
500 writel_relaxed(dcrtc->cursor_hw_sz,
501 base + LCD_SPU_HWC_HPXL_VLN);
502 armada_updatel(CFG_HWC_ENA,
503 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
504 base + LCD_SPU_DMA_CTRL0);
505 dcrtc->cursor_update = false;
506 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
507 }
508
Russell King96f60e32012-08-15 13:59:49 +0100509 spin_unlock(&dcrtc->irq_lock);
510
Russell Kingec6fb152016-07-25 15:16:11 +0100511 if (stat & GRA_FRAME_IRQ)
512 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100513}
514
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100515static irqreturn_t armada_drm_irq(int irq, void *arg)
516{
517 struct armada_crtc *dcrtc = arg;
518 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
519
520 /*
521 * This is rediculous - rather than writing bits to clear, we
522 * have to set the actual status register value. This is racy.
523 */
524 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
525
Russell Kingc8a220c2016-05-17 13:51:08 +0100526 trace_armada_drm_irq(&dcrtc->crtc, stat);
527
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100528 /* Mask out those interrupts we haven't enabled */
529 v = stat & dcrtc->irq_ena;
530
531 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
532 armada_drm_crtc_irq(dcrtc, stat);
533 return IRQ_HANDLED;
534 }
535 return IRQ_NONE;
536}
537
Russell King96f60e32012-08-15 13:59:49 +0100538static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
539{
540 struct drm_display_mode *adj = &dcrtc->crtc.mode;
541 uint32_t val = 0;
542
543 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
544 val |= CFG_CSC_YUV_CCIR709;
545 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
546 val |= CFG_CSC_RGB_STUDIO;
547
548 /*
549 * In auto mode, set the colorimetry, based upon the HDMI spec.
550 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
551 * ITU601. It may be more appropriate to set this depending on
552 * the source - but what if the graphic frame is YUV and the
553 * video frame is RGB?
554 */
555 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
556 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
557 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
558 if (dcrtc->csc_yuv_mode == CSC_AUTO)
559 val |= CFG_CSC_YUV_CCIR709;
560 }
561
562 /*
563 * We assume we're connected to a TV-like device, so the YUV->RGB
564 * conversion should produce a limited range. We should set this
565 * depending on the connectors attached to this CRTC, and what
566 * kind of device they report being connected.
567 */
568 if (dcrtc->csc_rgb_mode == CSC_AUTO)
569 val |= CFG_CSC_RGB_STUDIO;
570
571 return val;
572}
573
Russell King37af35c2016-08-16 22:09:09 +0100574static void armada_drm_primary_set(struct drm_crtc *crtc,
575 struct drm_plane *plane, int x, int y)
576{
577 struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
578 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King2925db02016-08-16 22:09:10 +0100579 struct armada_regs regs[8];
Russell King37af35c2016-08-16 22:09:09 +0100580 bool interlaced = dcrtc->interlaced;
581 unsigned i;
Russell King2925db02016-08-16 22:09:10 +0100582 u32 ctrl0;
Russell King37af35c2016-08-16 22:09:09 +0100583
584 i = armada_drm_crtc_calc_fb(plane->fb, x, y, regs, interlaced);
585
Russell King2925db02016-08-16 22:09:10 +0100586 armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
Russell King37af35c2016-08-16 22:09:09 +0100587 armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
588 armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
589
590 ctrl0 = state->ctrl0;
591 if (interlaced)
592 ctrl0 |= CFG_GRA_FTOGGLE;
593
594 armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
595 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
596 CFG_SWAPYU | CFG_YUV2RGB) |
Russell King73c51ab2017-07-08 10:22:19 +0100597 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE |
598 CFG_GRA_HSMOOTH | CFG_GRA_ENA,
Russell King37af35c2016-08-16 22:09:09 +0100599 LCD_SPU_DMA_CTRL0);
600 armada_reg_queue_end(regs, i);
601 armada_drm_crtc_update_regs(dcrtc, regs);
602}
603
Russell King96f60e32012-08-15 13:59:49 +0100604/* The mode_config.mutex will be held for this call */
605static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
606 struct drm_display_mode *mode, struct drm_display_mode *adj,
607 int x, int y, struct drm_framebuffer *old_fb)
608{
Russell King96f60e32012-08-15 13:59:49 +0100609 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
610 struct armada_regs regs[17];
611 uint32_t lm, rm, tm, bm, val, sclk;
612 unsigned long flags;
613 unsigned i;
614 bool interlaced;
615
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600616 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100617
618 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
619
Russell King73c51ab2017-07-08 10:22:19 +0100620 val = CFG_GRA_ENA;
Russell King8be523d2016-08-16 22:09:08 +0100621 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
622 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
Russell King96f60e32012-08-15 13:59:49 +0100623
Russell King8be523d2016-08-16 22:09:08 +0100624 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
625 val |= CFG_PALETTE_ENA;
626
627 drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
628 drm_to_armada_plane(crtc->primary)->state.src_hw =
629 drm_to_armada_plane(crtc->primary)->state.dst_hw =
Russell King37af35c2016-08-16 22:09:09 +0100630 adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
Russell King8be523d2016-08-16 22:09:08 +0100631 drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
632
Russell King37af35c2016-08-16 22:09:09 +0100633 i = 0;
Russell King96f60e32012-08-15 13:59:49 +0100634 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
635 lm = adj->crtc_htotal - adj->crtc_hsync_end;
636 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
637 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
638
639 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
640 adj->crtc_hdisplay,
641 adj->crtc_hsync_start,
642 adj->crtc_hsync_end,
643 adj->crtc_htotal, lm, rm);
644 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
645 adj->crtc_vdisplay,
646 adj->crtc_vsync_start,
647 adj->crtc_vsync_end,
648 adj->crtc_vtotal, tm, bm);
649
650 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100651 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
652 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100653
Russell King178e5612014-10-11 23:57:04 +0100654 drm_crtc_vblank_off(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100655
Russell King96f60e32012-08-15 13:59:49 +0100656 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
657 if (val != dcrtc->dumb_ctrl) {
658 dcrtc->dumb_ctrl = val;
659 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
660 }
661
Russell Kinge0ac5e92015-06-29 18:01:38 +0100662 /*
663 * If we are blanked, we would have disabled the clock. Re-enable
664 * it so that compute_clock() does the right thing.
665 */
666 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
667 WARN_ON(clk_prepare_enable(dcrtc->clk));
668
Russell King96f60e32012-08-15 13:59:49 +0100669 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100670 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100671
Russell King96f60e32012-08-15 13:59:49 +0100672 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
673
674 if (interlaced ^ dcrtc->interlaced) {
675 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300676 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100677 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300678 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100679 dcrtc->interlaced = interlaced;
680 }
681
682 spin_lock_irqsave(&dcrtc->irq_lock, flags);
683
Russell King214612f2017-07-08 10:22:15 +0100684 /* Ensure graphic fifo is enabled */
685 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
686
Russell King96f60e32012-08-15 13:59:49 +0100687 /* Even interlaced/progressive frame */
688 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
689 adj->crtc_htotal;
690 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
691 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100692 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100693 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100694
695 if (interlaced) {
696 /* Odd interlaced frame */
697 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
698 (1 << 16);
699 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
700 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100701 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100702 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100703 } else {
704 dcrtc->v[0] = dcrtc->v[1];
705 }
706
707 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
708
709 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King96f60e32012-08-15 13:59:49 +0100710 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
711 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
712 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
713 LCD_SPUT_V_H_TOTAL);
714
Russell King42e62ba2014-04-22 15:24:03 +0100715 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100716 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
717 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
718 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100719 }
Russell King96f60e32012-08-15 13:59:49 +0100720
Russell King96f60e32012-08-15 13:59:49 +0100721 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
722 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
723
724 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
725 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
726 armada_reg_queue_end(regs, i);
727
728 armada_drm_crtc_update_regs(dcrtc, regs);
Russell King37af35c2016-08-16 22:09:09 +0100729
730 armada_drm_primary_set(crtc, crtc->primary, x, y);
Russell King96f60e32012-08-15 13:59:49 +0100731 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
732
733 armada_drm_crtc_update(dcrtc);
734
Russell King178e5612014-10-11 23:57:04 +0100735 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100736 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
737
738 return 0;
739}
740
741/* The mode_config.mutex will be held for this call */
742static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
743 struct drm_framebuffer *old_fb)
744{
745 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
746 struct armada_regs regs[4];
747 unsigned i;
748
Matt Roperf4510a22014-04-01 15:22:40 -0700749 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
Russell King96f60e32012-08-15 13:59:49 +0100750 dcrtc->interlaced);
751 armada_reg_queue_end(regs, i);
752
753 /* Wait for pending flips to complete */
Russell King4b5dda82015-08-06 16:37:18 +0100754 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
755 MAX_SCHEDULE_TIMEOUT);
Russell King96f60e32012-08-15 13:59:49 +0100756
757 /* Take a reference to the new fb as we're using it */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -0600758 drm_framebuffer_get(crtc->primary->fb);
Russell King96f60e32012-08-15 13:59:49 +0100759
760 /* Update the base in the CRTC */
761 armada_drm_crtc_update_regs(dcrtc, regs);
762
763 /* Drop our previously held reference */
764 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
765
766 return 0;
767}
768
Russell King96f60e32012-08-15 13:59:49 +0100769/* The mode_config.mutex will be held for this call */
770static void armada_drm_crtc_disable(struct drm_crtc *crtc)
771{
Russell King96f60e32012-08-15 13:59:49 +0100772 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King28b30432017-07-08 10:16:40 +0100773
774 /* Disable our primary plane when we disable the CRTC. */
775 crtc->primary->funcs->disable_plane(crtc->primary, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100776}
777
778static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
779 .dpms = armada_drm_crtc_dpms,
780 .prepare = armada_drm_crtc_prepare,
781 .commit = armada_drm_crtc_commit,
782 .mode_fixup = armada_drm_crtc_mode_fixup,
783 .mode_set = armada_drm_crtc_mode_set,
784 .mode_set_base = armada_drm_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100785 .disable = armada_drm_crtc_disable,
786};
787
Russell King662af0d2013-05-19 10:55:17 +0100788static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
789 unsigned stride, unsigned width, unsigned height)
790{
791 uint32_t addr;
792 unsigned y;
793
794 addr = SRAM_HWC32_RAM1;
795 for (y = 0; y < height; y++) {
796 uint32_t *p = &pix[y * stride];
797 unsigned x;
798
799 for (x = 0; x < width; x++, p++) {
800 uint32_t val = *p;
801
802 val = (val & 0xff00ff00) |
803 (val & 0x000000ff) << 16 |
804 (val & 0x00ff0000) >> 16;
805
806 writel_relaxed(val,
807 base + LCD_SPU_SRAM_WRDAT);
808 writel_relaxed(addr | SRAM_WRITE,
809 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100810 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100811 addr += 1;
812 if ((addr & 0x00ff) == 0)
813 addr += 0xf00;
814 if ((addr & 0x30ff) == 0)
815 addr = SRAM_HWC32_RAM2;
816 }
817 }
818}
819
820static void armada_drm_crtc_cursor_tran(void __iomem *base)
821{
822 unsigned addr;
823
824 for (addr = 0; addr < 256; addr++) {
825 /* write the default value */
826 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
827 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
828 base + LCD_SPU_SRAM_CTRL);
829 }
830}
831
832static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
833{
834 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
835 uint32_t yoff, yscr, h = dcrtc->cursor_h;
836 uint32_t para1;
837
838 /*
839 * Calculate the visible width and height of the cursor,
840 * screen position, and the position in the cursor bitmap.
841 */
842 if (dcrtc->cursor_x < 0) {
843 xoff = -dcrtc->cursor_x;
844 xscr = 0;
845 w -= min(xoff, w);
846 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
847 xoff = 0;
848 xscr = dcrtc->cursor_x;
849 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
850 } else {
851 xoff = 0;
852 xscr = dcrtc->cursor_x;
853 }
854
855 if (dcrtc->cursor_y < 0) {
856 yoff = -dcrtc->cursor_y;
857 yscr = 0;
858 h -= min(yoff, h);
859 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
860 yoff = 0;
861 yscr = dcrtc->cursor_y;
862 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
863 } else {
864 yoff = 0;
865 yscr = dcrtc->cursor_y;
866 }
867
868 /* On interlaced modes, the vertical cursor size must be halved */
869 s = dcrtc->cursor_w;
870 if (dcrtc->interlaced) {
871 s *= 2;
872 yscr /= 2;
873 h /= 2;
874 }
875
876 if (!dcrtc->cursor_obj || !h || !w) {
877 spin_lock_irq(&dcrtc->irq_lock);
878 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
879 dcrtc->cursor_update = false;
880 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
881 spin_unlock_irq(&dcrtc->irq_lock);
882 return 0;
883 }
884
Russell King214612f2017-07-08 10:22:15 +0100885 spin_lock_irq(&dcrtc->irq_lock);
Russell King662af0d2013-05-19 10:55:17 +0100886 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
887 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
888 dcrtc->base + LCD_SPU_SRAM_PARA1);
Russell King214612f2017-07-08 10:22:15 +0100889 spin_unlock_irq(&dcrtc->irq_lock);
Russell King662af0d2013-05-19 10:55:17 +0100890
891 /*
892 * Initialize the transparency if the SRAM was powered down.
893 * We must also reload the cursor data as well.
894 */
895 if (!(para1 & CFG_CSB_256x32)) {
896 armada_drm_crtc_cursor_tran(dcrtc->base);
897 reload = true;
898 }
899
900 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
901 spin_lock_irq(&dcrtc->irq_lock);
902 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
903 dcrtc->cursor_update = false;
904 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
905 spin_unlock_irq(&dcrtc->irq_lock);
906 reload = true;
907 }
908 if (reload) {
909 struct armada_gem_object *obj = dcrtc->cursor_obj;
910 uint32_t *pix;
911 /* Set the top-left corner of the cursor image */
912 pix = obj->addr;
913 pix += yoff * s + xoff;
914 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
915 }
916
917 /* Reload the cursor position, size and enable in the IRQ handler */
918 spin_lock_irq(&dcrtc->irq_lock);
919 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
920 dcrtc->cursor_hw_sz = h << 16 | w;
921 dcrtc->cursor_update = true;
922 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
923 spin_unlock_irq(&dcrtc->irq_lock);
924
925 return 0;
926}
927
928static void cursor_update(void *data)
929{
930 armada_drm_crtc_cursor_update(data, true);
931}
932
933static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
934 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
935{
Russell King662af0d2013-05-19 10:55:17 +0100936 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100937 struct armada_gem_object *obj = NULL;
938 int ret;
939
940 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100941 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100942 return -ENXIO;
943
944 if (handle && w > 0 && h > 0) {
945 /* maximum size is 64x32 or 32x64 */
946 if (w > 64 || h > 64 || (w > 32 && h > 32))
947 return -ENOMEM;
948
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100949 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100950 if (!obj)
951 return -ENOENT;
952
953 /* Must be a kernel-mapped object */
954 if (!obj->addr) {
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600955 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100956 return -EINVAL;
957 }
958
959 if (obj->obj.size < w * h * 4) {
960 DRM_ERROR("buffer is too small\n");
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600961 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100962 return -ENOMEM;
963 }
964 }
965
Russell King662af0d2013-05-19 10:55:17 +0100966 if (dcrtc->cursor_obj) {
967 dcrtc->cursor_obj->update = NULL;
968 dcrtc->cursor_obj->update_data = NULL;
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600969 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100970 }
971 dcrtc->cursor_obj = obj;
972 dcrtc->cursor_w = w;
973 dcrtc->cursor_h = h;
974 ret = armada_drm_crtc_cursor_update(dcrtc, true);
975 if (obj) {
976 obj->update_data = dcrtc;
977 obj->update = cursor_update;
978 }
Russell King662af0d2013-05-19 10:55:17 +0100979
980 return ret;
981}
982
983static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
984{
Russell King662af0d2013-05-19 10:55:17 +0100985 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100986 int ret;
987
988 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100989 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100990 return -EFAULT;
991
Russell King662af0d2013-05-19 10:55:17 +0100992 dcrtc->cursor_x = x;
993 dcrtc->cursor_y = y;
994 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +0100995
996 return ret;
997}
998
Russell King96f60e32012-08-15 13:59:49 +0100999static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
1000{
1001 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1002 struct armada_private *priv = crtc->dev->dev_private;
1003
Russell King662af0d2013-05-19 10:55:17 +01001004 if (dcrtc->cursor_obj)
Haneen Mohammed4c3cf372017-09-20 12:54:48 -06001005 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +01001006
Russell King96f60e32012-08-15 13:59:49 +01001007 priv->dcrtc[dcrtc->num] = NULL;
1008 drm_crtc_cleanup(&dcrtc->crtc);
1009
1010 if (!IS_ERR(dcrtc->clk))
1011 clk_disable_unprepare(dcrtc->clk);
1012
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001013 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1014
Russell King9611cb92014-06-15 11:21:23 +01001015 of_node_put(dcrtc->crtc.port);
1016
Russell King96f60e32012-08-15 13:59:49 +01001017 kfree(dcrtc);
1018}
1019
1020/*
1021 * The mode_config lock is held here, to prevent races between this
1022 * and a mode_set.
1023 */
1024static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01001025 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags,
1026 struct drm_modeset_acquire_ctx *ctx)
Russell King96f60e32012-08-15 13:59:49 +01001027{
1028 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell Kingeaa66272017-07-08 10:22:10 +01001029 struct armada_plane_work *work;
Russell King96f60e32012-08-15 13:59:49 +01001030 unsigned i;
1031 int ret;
1032
1033 /* We don't support changing the pixel format */
Ville Syrjälädbd4d572016-11-18 21:53:10 +02001034 if (fb->format != crtc->primary->fb->format)
Russell King96f60e32012-08-15 13:59:49 +01001035 return -EINVAL;
1036
Russell Kingeaa66272017-07-08 10:22:10 +01001037 work = armada_drm_crtc_alloc_plane_work(dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001038 if (!work)
1039 return -ENOMEM;
1040
Russell Kingeaa66272017-07-08 10:22:10 +01001041 work->event = event;
1042 work->old_fb = dcrtc->crtc.primary->fb;
Russell King96f60e32012-08-15 13:59:49 +01001043
1044 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1045 dcrtc->interlaced);
1046 armada_reg_queue_end(work->regs, i);
1047
1048 /*
Russell Kingc5488302014-10-11 23:53:35 +01001049 * Ensure that we hold a reference on the new framebuffer.
1050 * This has to match the behaviour in mode_set.
Russell King96f60e32012-08-15 13:59:49 +01001051 */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001052 drm_framebuffer_get(fb);
Russell King96f60e32012-08-15 13:59:49 +01001053
Russell Kingeaa66272017-07-08 10:22:10 +01001054 ret = armada_drm_plane_work_queue(dcrtc, work);
Russell King96f60e32012-08-15 13:59:49 +01001055 if (ret) {
Russell Kingc5488302014-10-11 23:53:35 +01001056 /* Undo our reference above */
Haneen Mohammeda52ff2a2017-09-20 12:57:16 -06001057 drm_framebuffer_put(fb);
Russell King96f60e32012-08-15 13:59:49 +01001058 kfree(work);
1059 return ret;
1060 }
1061
1062 /*
1063 * Don't take a reference on the new framebuffer;
1064 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1065 * will _not_ drop that reference on successful return from this
1066 * function. Simply mark this new framebuffer as the current one.
1067 */
Matt Roperf4510a22014-04-01 15:22:40 -07001068 dcrtc->crtc.primary->fb = fb;
Russell King96f60e32012-08-15 13:59:49 +01001069
1070 /*
1071 * Finally, if the display is blanked, we won't receive an
1072 * interrupt, so complete it now.
1073 */
Russell King4b5dda82015-08-06 16:37:18 +01001074 if (dpms_blanked(dcrtc->dpms))
Russell Kingec6fb152016-07-25 15:16:11 +01001075 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +01001076
1077 return 0;
1078}
1079
1080static int
1081armada_drm_crtc_set_property(struct drm_crtc *crtc,
1082 struct drm_property *property, uint64_t val)
1083{
1084 struct armada_private *priv = crtc->dev->dev_private;
1085 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1086 bool update_csc = false;
1087
1088 if (property == priv->csc_yuv_prop) {
1089 dcrtc->csc_yuv_mode = val;
1090 update_csc = true;
1091 } else if (property == priv->csc_rgb_prop) {
1092 dcrtc->csc_rgb_mode = val;
1093 update_csc = true;
1094 }
1095
1096 if (update_csc) {
1097 uint32_t val;
1098
1099 val = dcrtc->spu_iopad_ctrl |
1100 armada_drm_crtc_calculate_csc(dcrtc);
1101 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1102 }
1103
1104 return 0;
1105}
1106
Shawn Guo5922a7d2017-02-07 17:16:18 +08001107/* These are called under the vbl_lock. */
1108static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
1109{
1110 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1111
1112 armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
1113 return 0;
1114}
1115
1116static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
1117{
1118 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1119
1120 armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
1121}
1122
Ville Syrjäläa02fb902015-12-15 12:20:59 +01001123static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell King662af0d2013-05-19 10:55:17 +01001124 .cursor_set = armada_drm_crtc_cursor_set,
1125 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +01001126 .destroy = armada_drm_crtc_destroy,
1127 .set_config = drm_crtc_helper_set_config,
1128 .page_flip = armada_drm_crtc_page_flip,
1129 .set_property = armada_drm_crtc_set_property,
Shawn Guo5922a7d2017-02-07 17:16:18 +08001130 .enable_vblank = armada_drm_crtc_enable_vblank,
1131 .disable_vblank = armada_drm_crtc_disable_vblank,
Russell King96f60e32012-08-15 13:59:49 +01001132};
1133
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001134int armada_drm_plane_disable(struct drm_plane *plane,
1135 struct drm_modeset_acquire_ctx *ctx)
Russell King28b30432017-07-08 10:16:40 +01001136{
1137 struct armada_plane *dplane = drm_to_armada_plane(plane);
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001138 struct armada_crtc *dcrtc;
Russell King890ca8d2017-07-08 10:22:27 +01001139 struct armada_plane_work *work;
1140 unsigned int idx = 0;
Russell Kingd76dcc72017-07-08 10:16:47 +01001141 u32 sram_para1, enable_mask;
Russell King28b30432017-07-08 10:16:40 +01001142
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001143 if (!plane->crtc)
1144 return 0;
1145
Russell King28b30432017-07-08 10:16:40 +01001146 /*
Russell King890ca8d2017-07-08 10:22:27 +01001147 * Arrange to power down most RAMs and FIFOs if this is the primary
1148 * plane, otherwise just the YUV FIFOs for the overlay plane.
Russell King28b30432017-07-08 10:16:40 +01001149 */
Russell King28b30432017-07-08 10:16:40 +01001150 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
1151 sram_para1 = CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1152 CFG_PDWN32x32 | CFG_PDWN64x66;
Russell Kingd76dcc72017-07-08 10:16:47 +01001153 enable_mask = CFG_GRA_ENA;
Russell King28b30432017-07-08 10:16:40 +01001154 } else {
Russell King28b30432017-07-08 10:16:40 +01001155 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
Russell Kingd76dcc72017-07-08 10:16:47 +01001156 enable_mask = CFG_DMA_ENA;
Russell King28b30432017-07-08 10:16:40 +01001157 }
1158
Russell Kingd76dcc72017-07-08 10:16:47 +01001159 dplane->state.ctrl0 &= ~enable_mask;
1160
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001161 dcrtc = drm_to_armada_crtc(plane->crtc);
1162
Russell King890ca8d2017-07-08 10:22:27 +01001163 /*
1164 * Try to disable the plane and drop our ref on the framebuffer
1165 * at the next frame update. If we fail for any reason, disable
1166 * the plane immediately.
1167 */
1168 work = &dplane->works[dplane->next_work];
1169 work->fn = armada_drm_crtc_complete_disable_work;
1170 work->cancel = armada_drm_crtc_complete_disable_work;
1171 work->old_fb = plane->fb;
1172
1173 armada_reg_queue_mod(work->regs, idx,
1174 0, enable_mask, LCD_SPU_DMA_CTRL0);
1175 armada_reg_queue_mod(work->regs, idx,
1176 sram_para1, 0, LCD_SPU_SRAM_PARA1);
1177 armada_reg_queue_end(work->regs, idx);
1178
Russell King28b30432017-07-08 10:16:40 +01001179 /* Wait for any preceding work to complete, but don't wedge */
1180 if (WARN_ON(!armada_drm_plane_work_wait(dplane, HZ)))
1181 armada_drm_plane_work_cancel(dcrtc, dplane);
1182
Russell King890ca8d2017-07-08 10:22:27 +01001183 if (armada_drm_plane_work_queue(dcrtc, work)) {
1184 work->fn(dcrtc, work);
1185 if (work->old_fb)
1186 drm_framebuffer_unreference(work->old_fb);
1187 }
1188
1189 dplane->next_work = !dplane->next_work;
Russell King28b30432017-07-08 10:16:40 +01001190
Russell King28b30432017-07-08 10:16:40 +01001191 return 0;
1192}
1193
Russell Kingde323012015-07-15 18:11:24 +01001194static const struct drm_plane_funcs armada_primary_plane_funcs = {
1195 .update_plane = drm_primary_helper_update,
Russell Kingf1f1bffc2017-07-08 10:16:42 +01001196 .disable_plane = armada_drm_plane_disable,
Russell Kingde323012015-07-15 18:11:24 +01001197 .destroy = drm_primary_helper_destroy,
1198};
1199
Russell King5740d272015-07-15 18:11:25 +01001200int armada_drm_plane_init(struct armada_plane *plane)
1201{
Russell Kingd9241552017-07-08 10:22:25 +01001202 unsigned int i;
1203
1204 for (i = 0; i < ARRAY_SIZE(plane->works); i++)
1205 plane->works[i].plane = &plane->base;
1206
Russell King5740d272015-07-15 18:11:25 +01001207 init_waitqueue_head(&plane->frame_wait);
1208
1209 return 0;
1210}
1211
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301212static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001213 { CSC_AUTO, "Auto" },
1214 { CSC_YUV_CCIR601, "CCIR601" },
1215 { CSC_YUV_CCIR709, "CCIR709" },
1216};
1217
Arvind Yadavaaaf2f12017-07-01 15:30:15 +05301218static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
Russell King96f60e32012-08-15 13:59:49 +01001219 { CSC_AUTO, "Auto" },
1220 { CSC_RGB_COMPUTER, "Computer system" },
1221 { CSC_RGB_STUDIO, "Studio" },
1222};
1223
1224static int armada_drm_crtc_create_properties(struct drm_device *dev)
1225{
1226 struct armada_private *priv = dev->dev_private;
1227
1228 if (priv->csc_yuv_prop)
1229 return 0;
1230
1231 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1232 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1233 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1234 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1235 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1236 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1237
1238 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1239 return -ENOMEM;
1240
1241 return 0;
1242}
1243
Russell King0fb29702015-06-06 21:46:53 +01001244static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +01001245 struct resource *res, int irq, const struct armada_variant *variant,
1246 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +01001247{
Russell Kingd8c96082014-04-22 11:10:15 +01001248 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +01001249 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +01001250 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +01001251 void __iomem *base;
1252 int ret;
1253
Russell Kingd8c96082014-04-22 11:10:15 +01001254 ret = armada_drm_crtc_create_properties(drm);
Russell King96f60e32012-08-15 13:59:49 +01001255 if (ret)
1256 return ret;
1257
Linus Torvaldsa7d7a142014-08-07 17:36:12 -07001258 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +09001259 if (IS_ERR(base))
1260 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +01001261
1262 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1263 if (!dcrtc) {
1264 DRM_ERROR("failed to allocate Armada crtc\n");
1265 return -ENOMEM;
1266 }
1267
Russell Kingd8c96082014-04-22 11:10:15 +01001268 if (dev != drm->dev)
1269 dev_set_drvdata(dev, dcrtc);
1270
Russell King42e62ba2014-04-22 15:24:03 +01001271 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +01001272 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +01001273 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001274 dcrtc->clk = ERR_PTR(-EINVAL);
1275 dcrtc->csc_yuv_mode = CSC_AUTO;
1276 dcrtc->csc_rgb_mode = CSC_AUTO;
1277 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1278 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1279 spin_lock_init(&dcrtc->irq_lock);
1280 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +01001281
1282 /* Initialize some registers which we don't otherwise set */
1283 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1284 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1285 writel_relaxed(dcrtc->spu_iopad_ctrl,
1286 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1287 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1288 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1289 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1290 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1291 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001292 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1293 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +01001294
Russell Kinge5d9ddf2014-04-26 15:19:38 +01001295 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1296 dcrtc);
Russell King33cd3c02017-12-08 12:16:22 +00001297 if (ret < 0)
1298 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001299
Russell King42e62ba2014-04-22 15:24:03 +01001300 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +01001301 ret = dcrtc->variant->init(dcrtc, dev);
Russell King33cd3c02017-12-08 12:16:22 +00001302 if (ret)
1303 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +01001304 }
1305
1306 /* Ensure AXI pipeline is enabled */
1307 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1308
1309 priv->dcrtc[dcrtc->num] = dcrtc;
1310
Russell King9611cb92014-06-15 11:21:23 +01001311 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +01001312
Russell Kingde323012015-07-15 18:11:24 +01001313 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King33cd3c02017-12-08 12:16:22 +00001314 if (!primary) {
1315 ret = -ENOMEM;
1316 goto err_crtc;
1317 }
Russell King1c914ce2015-07-15 18:11:24 +01001318
Russell King5740d272015-07-15 18:11:25 +01001319 ret = armada_drm_plane_init(primary);
1320 if (ret) {
1321 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001322 goto err_crtc;
Russell King5740d272015-07-15 18:11:25 +01001323 }
1324
Russell Kingde323012015-07-15 18:11:24 +01001325 ret = drm_universal_plane_init(drm, &primary->base, 0,
1326 &armada_primary_plane_funcs,
1327 armada_primary_formats,
1328 ARRAY_SIZE(armada_primary_formats),
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001329 NULL,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001330 DRM_PLANE_TYPE_PRIMARY, NULL);
Russell Kingde323012015-07-15 18:11:24 +01001331 if (ret) {
1332 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +00001333 goto err_crtc;
Russell Kingde323012015-07-15 18:11:24 +01001334 }
1335
1336 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001337 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001338 if (ret)
1339 goto err_crtc_init;
1340
Russell King96f60e32012-08-15 13:59:49 +01001341 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1342
1343 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1344 dcrtc->csc_yuv_mode);
1345 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1346 dcrtc->csc_rgb_mode);
1347
Russell Kingd8c96082014-04-22 11:10:15 +01001348 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001349
1350err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001351 primary->base.funcs->destroy(&primary->base);
Russell King33cd3c02017-12-08 12:16:22 +00001352err_crtc:
1353 kfree(dcrtc);
1354
Russell King1c914ce2015-07-15 18:11:24 +01001355 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001356}
Russell Kingd8c96082014-04-22 11:10:15 +01001357
1358static int
1359armada_lcd_bind(struct device *dev, struct device *master, void *data)
1360{
1361 struct platform_device *pdev = to_platform_device(dev);
1362 struct drm_device *drm = data;
1363 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1364 int irq = platform_get_irq(pdev, 0);
1365 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001366 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001367
1368 if (irq < 0)
1369 return irq;
1370
1371 if (!dev->of_node) {
1372 const struct platform_device_id *id;
1373
1374 id = platform_get_device_id(pdev);
1375 if (!id)
1376 return -ENXIO;
1377
1378 variant = (const struct armada_variant *)id->driver_data;
1379 } else {
1380 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001381 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001382
1383 match = of_match_device(dev->driver->of_match_table, dev);
1384 if (!match)
1385 return -ENXIO;
1386
Russell King9611cb92014-06-15 11:21:23 +01001387 np = of_get_child_by_name(parent, "ports");
1388 if (np)
1389 parent = np;
1390 port = of_get_child_by_name(parent, "port");
1391 of_node_put(np);
1392 if (!port) {
Rob Herring4bf99142017-07-18 16:43:04 -05001393 dev_err(dev, "no port node found in %pOF\n", parent);
Russell King9611cb92014-06-15 11:21:23 +01001394 return -ENXIO;
1395 }
1396
Russell Kingd8c96082014-04-22 11:10:15 +01001397 variant = match->data;
1398 }
1399
Russell King9611cb92014-06-15 11:21:23 +01001400 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001401}
1402
1403static void
1404armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1405{
1406 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1407
1408 armada_drm_crtc_destroy(&dcrtc->crtc);
1409}
1410
1411static const struct component_ops armada_lcd_ops = {
1412 .bind = armada_lcd_bind,
1413 .unbind = armada_lcd_unbind,
1414};
1415
1416static int armada_lcd_probe(struct platform_device *pdev)
1417{
1418 return component_add(&pdev->dev, &armada_lcd_ops);
1419}
1420
1421static int armada_lcd_remove(struct platform_device *pdev)
1422{
1423 component_del(&pdev->dev, &armada_lcd_ops);
1424 return 0;
1425}
1426
Arvind Yadav85909712017-06-20 10:44:33 +05301427static const struct of_device_id armada_lcd_of_match[] = {
Russell Kingd8c96082014-04-22 11:10:15 +01001428 {
1429 .compatible = "marvell,dove-lcd",
1430 .data = &armada510_ops,
1431 },
1432 {}
1433};
1434MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1435
1436static const struct platform_device_id armada_lcd_platform_ids[] = {
1437 {
1438 .name = "armada-lcd",
1439 .driver_data = (unsigned long)&armada510_ops,
1440 }, {
1441 .name = "armada-510-lcd",
1442 .driver_data = (unsigned long)&armada510_ops,
1443 },
1444 { },
1445};
1446MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1447
1448struct platform_driver armada_lcd_platform_driver = {
1449 .probe = armada_lcd_probe,
1450 .remove = armada_lcd_remove,
1451 .driver = {
1452 .name = "armada-lcd",
1453 .owner = THIS_MODULE,
1454 .of_match_table = armada_lcd_of_match,
1455 },
1456 .id_table = armada_lcd_platform_ids,
1457};