blob: 9ad966caf08cc6fa75149445e94ea6ece64e1a48 [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>
Russell Kingde503dd2018-07-30 11:52:34 +010014#include <drm/drm_atomic.h>
Russell King96f60e32012-08-15 13:59:49 +010015#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010016#include <drm/drm_plane_helper.h>
Dave Airliebcd21a42018-01-05 09:43:46 +100017#include <drm/drm_atomic_helper.h>
Russell King96f60e32012-08-15 13:59:49 +010018#include "armada_crtc.h"
19#include "armada_drm.h"
20#include "armada_fb.h"
21#include "armada_gem.h"
22#include "armada_hw.h"
Russell Kingd40af7b2018-07-30 11:52:34 +010023#include "armada_plane.h"
Russell Kingc8a220c2016-05-17 13:51:08 +010024#include "armada_trace.h"
Russell King96f60e32012-08-15 13:59:49 +010025
Russell King96f60e32012-08-15 13:59:49 +010026/*
27 * A note about interlacing. Let's consider HDMI 1920x1080i.
28 * The timing parameters we have from X are:
29 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
30 * 1920 2448 2492 2640 1080 1084 1094 1125
31 * Which get translated to:
32 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
33 * 1920 2448 2492 2640 540 542 547 562
34 *
35 * This is how it is defined by CEA-861-D - line and pixel numbers are
36 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
37 * line: 2640. The odd frame, the first active line is at line 21, and
38 * the even frame, the first active line is 584.
39 *
40 * LN: 560 561 562 563 567 568 569
41 * DE: ~~~|____________________________//__________________________
42 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
43 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
44 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
45 *
46 * LN: 1123 1124 1125 1 5 6 7
47 * DE: ~~~|____________________________//__________________________
48 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
49 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
50 * 23 blanking lines
51 *
52 * The Armada LCD Controller line and pixel numbers are, like X timings,
53 * referenced to the top left of the active frame.
54 *
55 * So, translating these to our LCD controller:
56 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
57 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
58 * Note: Vsync front porch remains constant!
59 *
60 * if (odd_frame) {
61 * vtotal = mode->crtc_vtotal + 1;
62 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
63 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
64 * } else {
65 * vtotal = mode->crtc_vtotal;
66 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
67 * vhorizpos = mode->crtc_hsync_start;
68 * }
69 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
70 *
71 * So, we need to reprogram these registers on each vsync event:
72 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
73 *
74 * Note: we do not use the frame done interrupts because these appear
75 * to happen too early, and lead to jitter on the display (presumably
76 * they occur at the end of the last active line, before the vsync back
77 * porch, which we're reprogramming.)
78 */
79
80void
81armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
82{
83 while (regs->offset != ~0) {
84 void __iomem *reg = dcrtc->base + regs->offset;
85 uint32_t val;
86
87 val = regs->mask;
88 if (val != 0)
89 val &= readl_relaxed(reg);
90 writel_relaxed(val | regs->val, reg);
91 ++regs;
92 }
93}
94
95#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
96
97static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
98{
99 uint32_t dumb_ctrl;
100
101 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
102
103 if (!dpms_blanked(dcrtc->dpms))
104 dumb_ctrl |= CFG_DUMB_ENA;
105
106 /*
107 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
108 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
109 * force LCD_D[23:0] to output blank color, overriding the GPIO or
110 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
111 */
112 if (dpms_blanked(dcrtc->dpms) &&
113 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
114 dumb_ctrl &= ~DUMB_MASK;
115 dumb_ctrl |= DUMB_BLANK;
116 }
117
Russell King155b8292018-07-30 11:52:34 +0100118 armada_updatel(dumb_ctrl,
119 ~(CFG_INV_CSYNC | CFG_INV_HSYNC | CFG_INV_VSYNC),
120 dcrtc->base + LCD_SPU_DUMB_CTRL);
Russell King96f60e32012-08-15 13:59:49 +0100121}
122
Russell King2839d452017-07-07 15:56:20 +0100123static void armada_drm_plane_work_call(struct armada_crtc *dcrtc,
124 struct armada_plane_work *work,
125 void (*fn)(struct armada_crtc *, struct armada_plane_work *))
126{
127 struct armada_plane *dplane = drm_to_armada_plane(work->plane);
Russell Kingd9241552017-07-08 10:22:25 +0100128 struct drm_pending_vblank_event *event;
129 struct drm_framebuffer *fb;
Russell King2839d452017-07-07 15:56:20 +0100130
131 if (fn)
132 fn(dcrtc, work);
133 drm_crtc_vblank_put(&dcrtc->crtc);
134
Russell Kingd9241552017-07-08 10:22:25 +0100135 event = work->event;
136 fb = work->old_fb;
Russell Kingeb19be52017-07-08 10:16:53 +0100137 if (event || fb) {
138 struct drm_device *dev = dcrtc->crtc.dev;
139 unsigned long flags;
140
141 spin_lock_irqsave(&dev->event_lock, flags);
142 if (event)
143 drm_crtc_send_vblank_event(&dcrtc->crtc, event);
144 if (fb)
145 __armada_drm_queue_unref_work(dev, fb);
146 spin_unlock_irqrestore(&dev->event_lock, flags);
147 }
Russell Kingb972a802017-07-08 10:16:52 +0100148
Russell Kingd9241552017-07-08 10:22:25 +0100149 if (work->need_kfree)
150 kfree(work);
151
Russell King2839d452017-07-07 15:56:20 +0100152 wake_up(&dplane->frame_wait);
153}
154
Russell King4b5dda82015-08-06 16:37:18 +0100155static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
Russell Kingec6fb152016-07-25 15:16:11 +0100156 struct drm_plane *plane)
Russell King4b5dda82015-08-06 16:37:18 +0100157{
Russell Kingec6fb152016-07-25 15:16:11 +0100158 struct armada_plane *dplane = drm_to_armada_plane(plane);
159 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King4b5dda82015-08-06 16:37:18 +0100160
161 /* Handle any pending frame work. */
Russell King2839d452017-07-07 15:56:20 +0100162 if (work)
163 armada_drm_plane_work_call(dcrtc, work, work->fn);
Russell King4b5dda82015-08-06 16:37:18 +0100164}
165
166int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
Russell Kingeaab0132017-07-07 15:55:53 +0100167 struct armada_plane_work *work)
Russell King4b5dda82015-08-06 16:37:18 +0100168{
Russell Kingeaab0132017-07-07 15:55:53 +0100169 struct armada_plane *plane = drm_to_armada_plane(work->plane);
Russell King4b5dda82015-08-06 16:37:18 +0100170 int ret;
171
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300172 ret = drm_crtc_vblank_get(&dcrtc->crtc);
Russell Kingc93dfdc2017-07-08 10:22:23 +0100173 if (ret)
Russell King4b5dda82015-08-06 16:37:18 +0100174 return ret;
Russell King4b5dda82015-08-06 16:37:18 +0100175
176 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
177 if (ret)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300178 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King4b5dda82015-08-06 16:37:18 +0100179
180 return ret;
181}
182
183int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
184{
185 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
186}
187
Russell Kingd3b84212017-07-07 15:55:40 +0100188void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc,
189 struct armada_plane *dplane)
Russell King7c8f7e12015-06-29 17:52:16 +0100190{
Russell Kingd3b84212017-07-07 15:55:40 +0100191 struct armada_plane_work *work = xchg(&dplane->work, NULL);
Russell King7c8f7e12015-06-29 17:52:16 +0100192
Russell King4a8506d2015-08-07 09:33:05 +0100193 if (work)
Russell King2839d452017-07-07 15:56:20 +0100194 armada_drm_plane_work_call(dcrtc, work, work->cancel);
Russell King96f60e32012-08-15 13:59:49 +0100195}
196
Russell King709ffd82015-07-15 18:09:38 +0100197static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
Russell King65724a12017-07-07 15:56:24 +0100198 struct armada_plane_work *work)
Russell King96f60e32012-08-15 13:59:49 +0100199{
Russell King709ffd82015-07-15 18:09:38 +0100200 unsigned long flags;
Russell King96f60e32012-08-15 13:59:49 +0100201
Russell King709ffd82015-07-15 18:09:38 +0100202 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Russell Kingeaa66272017-07-08 10:22:10 +0100203 armada_drm_crtc_update_regs(dcrtc, work->regs);
Russell King709ffd82015-07-15 18:09:38 +0100204 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King65724a12017-07-07 15:56:24 +0100205}
Russell King96f60e32012-08-15 13:59:49 +0100206
Russell Kingeaa66272017-07-08 10:22:10 +0100207static struct armada_plane_work *
208armada_drm_crtc_alloc_plane_work(struct drm_plane *plane)
Russell King901bb882017-07-07 15:55:45 +0100209{
Russell Kingeaa66272017-07-08 10:22:10 +0100210 struct armada_plane_work *work;
Russell King901bb882017-07-07 15:55:45 +0100211 int i = 0;
212
213 work = kzalloc(sizeof(*work), GFP_KERNEL);
214 if (!work)
215 return NULL;
216
Russell Kingeaa66272017-07-08 10:22:10 +0100217 work->plane = plane;
218 work->fn = armada_drm_crtc_complete_frame_work;
Russell Kingd9241552017-07-08 10:22:25 +0100219 work->need_kfree = true;
Russell King901bb882017-07-07 15:55:45 +0100220 armada_reg_queue_end(work->regs, i);
221
222 return work;
Russell King96f60e32012-08-15 13:59:49 +0100223}
224
Russell King96f60e32012-08-15 13:59:49 +0100225static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
226{
Russell King96f60e32012-08-15 13:59:49 +0100227 /*
228 * Tell the DRM core that vblank IRQs aren't going to happen for
229 * a while. This cleans up any pending vblank events for us.
230 */
Russell King178e5612014-10-11 23:57:04 +0100231 drm_crtc_vblank_off(&dcrtc->crtc);
Russell Kingec6fb152016-07-25 15:16:11 +0100232 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100233}
234
Russell King96f60e32012-08-15 13:59:49 +0100235/* The mode_config.mutex will be held for this call */
236static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
237{
238 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
239
Russell Kingea908ba2016-10-04 22:19:57 +0100240 if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
Russell King96f60e32012-08-15 13:59:49 +0100241 if (dpms_blanked(dpms))
242 armada_drm_vblank_off(dcrtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100243 else if (!IS_ERR(dcrtc->clk))
244 WARN_ON(clk_prepare_enable(dcrtc->clk));
245 dcrtc->dpms = dpms;
246 armada_drm_crtc_update(dcrtc);
247 if (!dpms_blanked(dpms))
Russell King178e5612014-10-11 23:57:04 +0100248 drm_crtc_vblank_on(&dcrtc->crtc);
Russell Kingea908ba2016-10-04 22:19:57 +0100249 else if (!IS_ERR(dcrtc->clk))
250 clk_disable_unprepare(dcrtc->clk);
251 } else if (dcrtc->dpms != dpms) {
252 dcrtc->dpms = dpms;
Russell King96f60e32012-08-15 13:59:49 +0100253 }
254}
255
256/*
257 * Prepare for a mode set. Turn off overlay to ensure that we don't end
258 * up with the overlay size being bigger than the active screen size.
259 * We rely upon X refreshing this state after the mode set has completed.
260 *
261 * The mode_config.mutex will be held for this call
262 */
263static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
264{
265 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
266 struct drm_plane *plane;
267
268 /*
269 * If we have an overlay plane associated with this CRTC, disable
270 * it before the modeset to avoid its coordinates being outside
Russell Kingf8e14062015-06-29 17:52:42 +0100271 * the new mode parameters.
Russell King96f60e32012-08-15 13:59:49 +0100272 */
273 plane = dcrtc->plane;
Russell King890ca8d2017-07-08 10:22:27 +0100274 if (plane) {
Russell Kingf8e14062015-06-29 17:52:42 +0100275 drm_plane_force_disable(plane);
Russell King890ca8d2017-07-08 10:22:27 +0100276 WARN_ON(!armada_drm_plane_work_wait(drm_to_armada_plane(plane),
277 HZ));
278 }
Russell Kingf9a13bb2018-07-30 11:52:34 +0100279
280 /* Wait for pending flips to complete */
281 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
282 MAX_SCHEDULE_TIMEOUT);
283
284 drm_crtc_vblank_off(crtc);
285
Russell King155b8292018-07-30 11:52:34 +0100286 armada_updatel(0, CFG_DUMB_ENA, dcrtc->base + LCD_SPU_DUMB_CTRL);
Russell King96f60e32012-08-15 13:59:49 +0100287}
288
289/* The mode_config.mutex will be held for this call */
290static void armada_drm_crtc_commit(struct drm_crtc *crtc)
291{
292 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
293
Russell Kingf9a13bb2018-07-30 11:52:34 +0100294 dcrtc->dpms = DRM_MODE_DPMS_ON;
295 armada_drm_crtc_update(dcrtc);
296 drm_crtc_vblank_on(crtc);
Russell King96f60e32012-08-15 13:59:49 +0100297}
298
299/* The mode_config.mutex will be held for this call */
300static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
301 const struct drm_display_mode *mode, struct drm_display_mode *adj)
302{
Russell King96f60e32012-08-15 13:59:49 +0100303 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
304 int ret;
305
306 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
Russell King42e62ba2014-04-22 15:24:03 +0100307 if (!dcrtc->variant->has_spu_adv_reg &&
Russell King96f60e32012-08-15 13:59:49 +0100308 adj->flags & DRM_MODE_FLAG_INTERLACE)
309 return false;
310
311 /* Check whether the display mode is possible */
Russell King42e62ba2014-04-22 15:24:03 +0100312 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100313 if (ret)
314 return false;
315
316 return true;
317}
318
Shawn Guo5922a7d2017-02-07 17:16:18 +0800319/* These are locked by dev->vbl_lock */
320static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
321{
322 if (dcrtc->irq_ena & mask) {
323 dcrtc->irq_ena &= ~mask;
324 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
325 }
326}
327
328static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
329{
330 if ((dcrtc->irq_ena & mask) != mask) {
331 dcrtc->irq_ena |= mask;
332 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
333 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
334 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
335 }
336}
337
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100338static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
Russell King96f60e32012-08-15 13:59:49 +0100339{
Russell King96f60e32012-08-15 13:59:49 +0100340 void __iomem *base = dcrtc->base;
Russell King4a8506d2015-08-07 09:33:05 +0100341 struct drm_plane *ovl_plane;
Russell King96f60e32012-08-15 13:59:49 +0100342
343 if (stat & DMA_FF_UNDERFLOW)
344 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
345 if (stat & GRA_FF_UNDERFLOW)
346 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
347
348 if (stat & VSYNC_IRQ)
Gustavo Padovan0ac28c52016-07-04 21:04:48 -0300349 drm_crtc_handle_vblank(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100350
Russell King4a8506d2015-08-07 09:33:05 +0100351 ovl_plane = dcrtc->plane;
Russell Kingec6fb152016-07-25 15:16:11 +0100352 if (ovl_plane)
353 armada_drm_plane_work_run(dcrtc, ovl_plane);
Russell King96f60e32012-08-15 13:59:49 +0100354
Russell Kinga3f6a182017-07-08 10:16:48 +0100355 spin_lock(&dcrtc->irq_lock);
Russell King96f60e32012-08-15 13:59:49 +0100356 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
357 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
358 uint32_t val;
359
360 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
361 writel_relaxed(dcrtc->v[i].spu_v_h_total,
362 base + LCD_SPUT_V_H_TOTAL);
363
364 val = readl_relaxed(base + LCD_SPU_ADV_REG);
365 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
366 val |= dcrtc->v[i].spu_adv_reg;
Russell King662af0d2013-05-19 10:55:17 +0100367 writel_relaxed(val, base + LCD_SPU_ADV_REG);
Russell King96f60e32012-08-15 13:59:49 +0100368 }
Russell King662af0d2013-05-19 10:55:17 +0100369
370 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
371 writel_relaxed(dcrtc->cursor_hw_pos,
372 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
373 writel_relaxed(dcrtc->cursor_hw_sz,
374 base + LCD_SPU_HWC_HPXL_VLN);
375 armada_updatel(CFG_HWC_ENA,
376 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
377 base + LCD_SPU_DMA_CTRL0);
378 dcrtc->cursor_update = false;
379 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
380 }
381
Russell King96f60e32012-08-15 13:59:49 +0100382 spin_unlock(&dcrtc->irq_lock);
383
Russell Kingec6fb152016-07-25 15:16:11 +0100384 if (stat & GRA_FRAME_IRQ)
385 armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
Russell King96f60e32012-08-15 13:59:49 +0100386}
387
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100388static irqreturn_t armada_drm_irq(int irq, void *arg)
389{
390 struct armada_crtc *dcrtc = arg;
391 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
392
393 /*
Russell King92298c12018-06-26 17:06:06 +0100394 * Reading the ISR appears to clear bits provided CLEAN_SPU_IRQ_ISR
395 * is set. Writing has some other effect to acknowledge the IRQ -
396 * without this, we only get a single IRQ.
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100397 */
398 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
399
Russell Kingc8a220c2016-05-17 13:51:08 +0100400 trace_armada_drm_irq(&dcrtc->crtc, stat);
401
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100402 /* Mask out those interrupts we haven't enabled */
403 v = stat & dcrtc->irq_ena;
404
405 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
406 armada_drm_crtc_irq(dcrtc, stat);
407 return IRQ_HANDLED;
408 }
409 return IRQ_NONE;
410}
411
Russell King96f60e32012-08-15 13:59:49 +0100412/* The mode_config.mutex will be held for this call */
Russell Kingc36045e2018-07-30 11:52:34 +0100413static void armada_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
Russell King96f60e32012-08-15 13:59:49 +0100414{
Russell Kingc36045e2018-07-30 11:52:34 +0100415 struct drm_display_mode *adj = &crtc->state->adjusted_mode;
Russell King96f60e32012-08-15 13:59:49 +0100416 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
417 struct armada_regs regs[17];
418 uint32_t lm, rm, tm, bm, val, sclk;
419 unsigned long flags;
420 unsigned i;
Russell Kingc36045e2018-07-30 11:52:34 +0100421 bool interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
Russell King96f60e32012-08-15 13:59:49 +0100422
Russell King37af35c2016-08-16 22:09:09 +0100423 i = 0;
Russell King96f60e32012-08-15 13:59:49 +0100424 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
425 lm = adj->crtc_htotal - adj->crtc_hsync_end;
426 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
427 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
428
Russell Kinga61c3922018-07-30 11:52:34 +0100429 DRM_DEBUG_KMS("[CRTC:%d:%s] mode " DRM_MODE_FMT "\n",
430 crtc->base.id, crtc->name,
431 adj->base.id, adj->name, adj->vrefresh, adj->clock,
432 adj->crtc_hdisplay, adj->crtc_hsync_start,
433 adj->crtc_hsync_end, adj->crtc_htotal,
434 adj->crtc_vdisplay, adj->crtc_vsync_start,
435 adj->crtc_vsync_end, adj->crtc_vtotal,
436 adj->type, adj->flags);
437 DRM_DEBUG_KMS("lm %d rm %d tm %d bm %d\n", lm, rm, tm, bm);
Russell King96f60e32012-08-15 13:59:49 +0100438
Russell Kinge0ac5e92015-06-29 18:01:38 +0100439 /*
440 * If we are blanked, we would have disabled the clock. Re-enable
441 * it so that compute_clock() does the right thing.
442 */
443 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
444 WARN_ON(clk_prepare_enable(dcrtc->clk));
445
Russell King96f60e32012-08-15 13:59:49 +0100446 /* Now compute the divider for real */
Russell King42e62ba2014-04-22 15:24:03 +0100447 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
Russell King96f60e32012-08-15 13:59:49 +0100448
Russell King96f60e32012-08-15 13:59:49 +0100449 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
450
451 if (interlaced ^ dcrtc->interlaced) {
452 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300453 drm_crtc_vblank_get(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100454 else
Gustavo Padovanaccbaf62016-06-06 11:41:40 -0300455 drm_crtc_vblank_put(&dcrtc->crtc);
Russell King96f60e32012-08-15 13:59:49 +0100456 dcrtc->interlaced = interlaced;
457 }
458
459 spin_lock_irqsave(&dcrtc->irq_lock, flags);
460
461 /* Even interlaced/progressive frame */
462 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
463 adj->crtc_htotal;
464 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
465 val = adj->crtc_hsync_start;
Russell King662af0d2013-05-19 10:55:17 +0100466 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100467 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100468
469 if (interlaced) {
470 /* Odd interlaced frame */
471 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
472 (1 << 16);
473 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
474 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
Russell King662af0d2013-05-19 10:55:17 +0100475 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
Russell King42e62ba2014-04-22 15:24:03 +0100476 dcrtc->variant->spu_adv_reg;
Russell King96f60e32012-08-15 13:59:49 +0100477 } else {
478 dcrtc->v[0] = dcrtc->v[1];
479 }
480
481 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
482
483 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
Russell King96f60e32012-08-15 13:59:49 +0100484 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
485 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
486 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
487 LCD_SPUT_V_H_TOTAL);
488
Russell King42e62ba2014-04-22 15:24:03 +0100489 if (dcrtc->variant->has_spu_adv_reg) {
Russell King96f60e32012-08-15 13:59:49 +0100490 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
491 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
492 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
Russell King662af0d2013-05-19 10:55:17 +0100493 }
Russell King96f60e32012-08-15 13:59:49 +0100494
Russell King96f60e32012-08-15 13:59:49 +0100495 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
496 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
Russell King155b8292018-07-30 11:52:34 +0100497
498 /*
499 * The documentation doesn't indicate what the normal state of
500 * the sync signals are. Sebastian Hesselbart kindly probed
501 * these signals on his board to determine their state.
502 *
503 * The non-inverted state of the sync signals is active high.
504 * Setting these bits makes the appropriate signal active low.
505 */
506 val = 0;
507 if (adj->flags & DRM_MODE_FLAG_NCSYNC)
508 val |= CFG_INV_CSYNC;
509 if (adj->flags & DRM_MODE_FLAG_NHSYNC)
510 val |= CFG_INV_HSYNC;
511 if (adj->flags & DRM_MODE_FLAG_NVSYNC)
512 val |= CFG_INV_VSYNC;
513 armada_reg_queue_mod(regs, i, val, CFG_INV_CSYNC | CFG_INV_HSYNC |
514 CFG_INV_VSYNC, LCD_SPU_DUMB_CTRL);
Russell King96f60e32012-08-15 13:59:49 +0100515 armada_reg_queue_end(regs, i);
516
517 armada_drm_crtc_update_regs(dcrtc, regs);
518 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Russell King96f60e32012-08-15 13:59:49 +0100519}
520
Russell King96f60e32012-08-15 13:59:49 +0100521/* The mode_config.mutex will be held for this call */
522static void armada_drm_crtc_disable(struct drm_crtc *crtc)
523{
Russell King96f60e32012-08-15 13:59:49 +0100524 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Russell King28b30432017-07-08 10:16:40 +0100525
526 /* Disable our primary plane when we disable the CRTC. */
527 crtc->primary->funcs->disable_plane(crtc->primary, NULL);
Russell King96f60e32012-08-15 13:59:49 +0100528}
529
Russell Kingc36045e2018-07-30 11:52:34 +0100530static void armada_drm_crtc_atomic_begin(struct drm_crtc *crtc,
531 struct drm_crtc_state *old_crtc_state)
532{
533 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
534 struct armada_plane *dplane;
535
536 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
537
538 /* Wait 100ms for any plane works to complete */
539 dplane = drm_to_armada_plane(crtc->primary);
540 if (WARN_ON(armada_drm_plane_work_wait(dplane, HZ / 10) == 0))
541 armada_drm_plane_work_cancel(dcrtc, dplane);
542
543 dcrtc->regs_idx = 0;
544 dcrtc->regs = dcrtc->atomic_regs;
545}
546
547static void armada_drm_crtc_atomic_flush(struct drm_crtc *crtc,
548 struct drm_crtc_state *old_crtc_state)
549{
550 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
551 unsigned long flags;
552
553 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
554
555 armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx);
556
557 spin_lock_irqsave(&dcrtc->irq_lock, flags);
558 armada_drm_crtc_update_regs(dcrtc, dcrtc->regs);
559 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
560}
561
Russell King96f60e32012-08-15 13:59:49 +0100562static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
563 .dpms = armada_drm_crtc_dpms,
564 .prepare = armada_drm_crtc_prepare,
565 .commit = armada_drm_crtc_commit,
566 .mode_fixup = armada_drm_crtc_mode_fixup,
Russell Kingc36045e2018-07-30 11:52:34 +0100567 .mode_set = drm_helper_crtc_mode_set,
568 .mode_set_nofb = armada_drm_crtc_mode_set_nofb,
569 .mode_set_base = drm_helper_crtc_mode_set_base,
Russell King96f60e32012-08-15 13:59:49 +0100570 .disable = armada_drm_crtc_disable,
Russell Kingc36045e2018-07-30 11:52:34 +0100571 .atomic_begin = armada_drm_crtc_atomic_begin,
572 .atomic_flush = armada_drm_crtc_atomic_flush,
Russell King96f60e32012-08-15 13:59:49 +0100573};
574
Russell King662af0d2013-05-19 10:55:17 +0100575static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
576 unsigned stride, unsigned width, unsigned height)
577{
578 uint32_t addr;
579 unsigned y;
580
581 addr = SRAM_HWC32_RAM1;
582 for (y = 0; y < height; y++) {
583 uint32_t *p = &pix[y * stride];
584 unsigned x;
585
586 for (x = 0; x < width; x++, p++) {
587 uint32_t val = *p;
588
589 val = (val & 0xff00ff00) |
590 (val & 0x000000ff) << 16 |
591 (val & 0x00ff0000) >> 16;
592
593 writel_relaxed(val,
594 base + LCD_SPU_SRAM_WRDAT);
595 writel_relaxed(addr | SRAM_WRITE,
596 base + LCD_SPU_SRAM_CTRL);
Russell Kingc39b0692014-04-07 12:00:17 +0100597 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
Russell King662af0d2013-05-19 10:55:17 +0100598 addr += 1;
599 if ((addr & 0x00ff) == 0)
600 addr += 0xf00;
601 if ((addr & 0x30ff) == 0)
602 addr = SRAM_HWC32_RAM2;
603 }
604 }
605}
606
607static void armada_drm_crtc_cursor_tran(void __iomem *base)
608{
609 unsigned addr;
610
611 for (addr = 0; addr < 256; addr++) {
612 /* write the default value */
613 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
614 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
615 base + LCD_SPU_SRAM_CTRL);
616 }
617}
618
619static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
620{
621 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
622 uint32_t yoff, yscr, h = dcrtc->cursor_h;
623 uint32_t para1;
624
625 /*
626 * Calculate the visible width and height of the cursor,
627 * screen position, and the position in the cursor bitmap.
628 */
629 if (dcrtc->cursor_x < 0) {
630 xoff = -dcrtc->cursor_x;
631 xscr = 0;
632 w -= min(xoff, w);
633 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
634 xoff = 0;
635 xscr = dcrtc->cursor_x;
636 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
637 } else {
638 xoff = 0;
639 xscr = dcrtc->cursor_x;
640 }
641
642 if (dcrtc->cursor_y < 0) {
643 yoff = -dcrtc->cursor_y;
644 yscr = 0;
645 h -= min(yoff, h);
646 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
647 yoff = 0;
648 yscr = dcrtc->cursor_y;
649 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
650 } else {
651 yoff = 0;
652 yscr = dcrtc->cursor_y;
653 }
654
655 /* On interlaced modes, the vertical cursor size must be halved */
656 s = dcrtc->cursor_w;
657 if (dcrtc->interlaced) {
658 s *= 2;
659 yscr /= 2;
660 h /= 2;
661 }
662
663 if (!dcrtc->cursor_obj || !h || !w) {
664 spin_lock_irq(&dcrtc->irq_lock);
665 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
666 dcrtc->cursor_update = false;
667 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
668 spin_unlock_irq(&dcrtc->irq_lock);
669 return 0;
670 }
671
Russell King214612f2017-07-08 10:22:15 +0100672 spin_lock_irq(&dcrtc->irq_lock);
Russell King662af0d2013-05-19 10:55:17 +0100673 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
674 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
675 dcrtc->base + LCD_SPU_SRAM_PARA1);
Russell King214612f2017-07-08 10:22:15 +0100676 spin_unlock_irq(&dcrtc->irq_lock);
Russell King662af0d2013-05-19 10:55:17 +0100677
678 /*
679 * Initialize the transparency if the SRAM was powered down.
680 * We must also reload the cursor data as well.
681 */
682 if (!(para1 & CFG_CSB_256x32)) {
683 armada_drm_crtc_cursor_tran(dcrtc->base);
684 reload = true;
685 }
686
687 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
688 spin_lock_irq(&dcrtc->irq_lock);
689 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
690 dcrtc->cursor_update = false;
691 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
692 spin_unlock_irq(&dcrtc->irq_lock);
693 reload = true;
694 }
695 if (reload) {
696 struct armada_gem_object *obj = dcrtc->cursor_obj;
697 uint32_t *pix;
698 /* Set the top-left corner of the cursor image */
699 pix = obj->addr;
700 pix += yoff * s + xoff;
701 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
702 }
703
704 /* Reload the cursor position, size and enable in the IRQ handler */
705 spin_lock_irq(&dcrtc->irq_lock);
706 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
707 dcrtc->cursor_hw_sz = h << 16 | w;
708 dcrtc->cursor_update = true;
709 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
710 spin_unlock_irq(&dcrtc->irq_lock);
711
712 return 0;
713}
714
715static void cursor_update(void *data)
716{
717 armada_drm_crtc_cursor_update(data, true);
718}
719
720static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
721 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
722{
Russell King662af0d2013-05-19 10:55:17 +0100723 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100724 struct armada_gem_object *obj = NULL;
725 int ret;
726
727 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100728 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100729 return -ENXIO;
730
731 if (handle && w > 0 && h > 0) {
732 /* maximum size is 64x32 or 32x64 */
733 if (w > 64 || h > 64 || (w > 32 && h > 32))
734 return -ENOMEM;
735
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100736 obj = armada_gem_object_lookup(file, handle);
Russell King662af0d2013-05-19 10:55:17 +0100737 if (!obj)
738 return -ENOENT;
739
740 /* Must be a kernel-mapped object */
741 if (!obj->addr) {
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600742 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100743 return -EINVAL;
744 }
745
746 if (obj->obj.size < w * h * 4) {
747 DRM_ERROR("buffer is too small\n");
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600748 drm_gem_object_put_unlocked(&obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100749 return -ENOMEM;
750 }
751 }
752
Russell King662af0d2013-05-19 10:55:17 +0100753 if (dcrtc->cursor_obj) {
754 dcrtc->cursor_obj->update = NULL;
755 dcrtc->cursor_obj->update_data = NULL;
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600756 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100757 }
758 dcrtc->cursor_obj = obj;
759 dcrtc->cursor_w = w;
760 dcrtc->cursor_h = h;
761 ret = armada_drm_crtc_cursor_update(dcrtc, true);
762 if (obj) {
763 obj->update_data = dcrtc;
764 obj->update = cursor_update;
765 }
Russell King662af0d2013-05-19 10:55:17 +0100766
767 return ret;
768}
769
770static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
771{
Russell King662af0d2013-05-19 10:55:17 +0100772 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King662af0d2013-05-19 10:55:17 +0100773 int ret;
774
775 /* If no cursor support, replicate drm's return value */
Russell King42e62ba2014-04-22 15:24:03 +0100776 if (!dcrtc->variant->has_spu_adv_reg)
Russell King662af0d2013-05-19 10:55:17 +0100777 return -EFAULT;
778
Russell King662af0d2013-05-19 10:55:17 +0100779 dcrtc->cursor_x = x;
780 dcrtc->cursor_y = y;
781 ret = armada_drm_crtc_cursor_update(dcrtc, false);
Russell King662af0d2013-05-19 10:55:17 +0100782
783 return ret;
784}
785
Russell King96f60e32012-08-15 13:59:49 +0100786static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
787{
788 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
789 struct armada_private *priv = crtc->dev->dev_private;
790
Russell King662af0d2013-05-19 10:55:17 +0100791 if (dcrtc->cursor_obj)
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600792 drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
Russell King662af0d2013-05-19 10:55:17 +0100793
Russell King96f60e32012-08-15 13:59:49 +0100794 priv->dcrtc[dcrtc->num] = NULL;
795 drm_crtc_cleanup(&dcrtc->crtc);
796
797 if (!IS_ERR(dcrtc->clk))
798 clk_disable_unprepare(dcrtc->clk);
799
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100800 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
801
Russell King9611cb92014-06-15 11:21:23 +0100802 of_node_put(dcrtc->crtc.port);
803
Russell King96f60e32012-08-15 13:59:49 +0100804 kfree(dcrtc);
805}
806
807/*
808 * The mode_config lock is held here, to prevent races between this
809 * and a mode_set.
810 */
811static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
Russell Kingde503dd2018-07-30 11:52:34 +0100812 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event,
813 uint32_t page_flip_flags, struct drm_modeset_acquire_ctx *ctx)
Russell King96f60e32012-08-15 13:59:49 +0100814{
815 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell Kingde503dd2018-07-30 11:52:34 +0100816 struct drm_plane *plane = crtc->primary;
817 const struct drm_plane_helper_funcs *plane_funcs;
818 struct drm_plane_state *state;
Russell Kingeaa66272017-07-08 10:22:10 +0100819 struct armada_plane_work *work;
Russell King96f60e32012-08-15 13:59:49 +0100820 int ret;
821
Russell Kingde503dd2018-07-30 11:52:34 +0100822 /* Construct new state for the primary plane */
823 state = drm_atomic_helper_plane_duplicate_state(plane);
824 if (!state)
Russell King96f60e32012-08-15 13:59:49 +0100825 return -ENOMEM;
826
Russell Kingde503dd2018-07-30 11:52:34 +0100827 drm_atomic_set_fb_for_plane(state, fb);
Russell King96f60e32012-08-15 13:59:49 +0100828
Russell Kingde503dd2018-07-30 11:52:34 +0100829 work = armada_drm_crtc_alloc_plane_work(plane);
830 if (!work) {
831 ret = -ENOMEM;
832 goto put_state;
Russell King96f60e32012-08-15 13:59:49 +0100833 }
834
Russell Kingde503dd2018-07-30 11:52:34 +0100835 /* Make sure we can get vblank interrupts */
836 ret = drm_crtc_vblank_get(crtc);
837 if (ret)
838 goto put_work;
839
Russell King96f60e32012-08-15 13:59:49 +0100840 /*
Russell Kingde503dd2018-07-30 11:52:34 +0100841 * If we have another work pending, we can't process this flip.
842 * The modeset locks protect us from another user queuing a work
843 * while we're setting up.
Russell Kingc36045e2018-07-30 11:52:34 +0100844 */
Russell Kingde503dd2018-07-30 11:52:34 +0100845 if (drm_to_armada_plane(plane)->work) {
846 ret = -EBUSY;
847 goto put_vblank;
848 }
849
850 work->event = event;
851 work->old_fb = plane->state->fb;
852
853 /*
854 * Hold a ref on the new fb while it's being displayed by the
855 * hardware. The old fb refcount will be released in the worker.
856 */
857 drm_framebuffer_get(state->fb);
858
859 /* Point of no return */
860 swap(plane->state, state);
861
862 dcrtc->regs_idx = 0;
863 dcrtc->regs = work->regs;
864
865 plane_funcs = plane->helper_private;
866 plane_funcs->atomic_update(plane, state);
867 armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx);
868
869 /* Queue the work - this should never fail */
870 WARN_ON(armada_drm_plane_work_queue(dcrtc, work));
871 work = NULL;
Russell Kingc36045e2018-07-30 11:52:34 +0100872
873 /*
Russell King96f60e32012-08-15 13:59:49 +0100874 * Finally, if the display is blanked, we won't receive an
875 * interrupt, so complete it now.
876 */
Russell King4b5dda82015-08-06 16:37:18 +0100877 if (dpms_blanked(dcrtc->dpms))
Russell Kingde503dd2018-07-30 11:52:34 +0100878 armada_drm_plane_work_run(dcrtc, plane);
Russell King96f60e32012-08-15 13:59:49 +0100879
Russell Kingde503dd2018-07-30 11:52:34 +0100880put_vblank:
881 drm_crtc_vblank_put(crtc);
882put_work:
883 kfree(work);
884put_state:
885 drm_atomic_helper_plane_destroy_state(plane, state);
886 return ret;
Russell King96f60e32012-08-15 13:59:49 +0100887}
888
Shawn Guo5922a7d2017-02-07 17:16:18 +0800889/* These are called under the vbl_lock. */
890static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
891{
892 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King92298c12018-06-26 17:06:06 +0100893 unsigned long flags;
Shawn Guo5922a7d2017-02-07 17:16:18 +0800894
Russell King92298c12018-06-26 17:06:06 +0100895 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Shawn Guo5922a7d2017-02-07 17:16:18 +0800896 armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
Russell King92298c12018-06-26 17:06:06 +0100897 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Shawn Guo5922a7d2017-02-07 17:16:18 +0800898 return 0;
899}
900
901static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
902{
903 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
Russell King92298c12018-06-26 17:06:06 +0100904 unsigned long flags;
Shawn Guo5922a7d2017-02-07 17:16:18 +0800905
Russell King92298c12018-06-26 17:06:06 +0100906 spin_lock_irqsave(&dcrtc->irq_lock, flags);
Shawn Guo5922a7d2017-02-07 17:16:18 +0800907 armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
Russell King92298c12018-06-26 17:06:06 +0100908 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
Shawn Guo5922a7d2017-02-07 17:16:18 +0800909}
910
Ville Syrjäläa02fb902015-12-15 12:20:59 +0100911static const struct drm_crtc_funcs armada_crtc_funcs = {
Russell Kingc36045e2018-07-30 11:52:34 +0100912 .reset = drm_atomic_helper_crtc_reset,
Russell King662af0d2013-05-19 10:55:17 +0100913 .cursor_set = armada_drm_crtc_cursor_set,
914 .cursor_move = armada_drm_crtc_cursor_move,
Russell King96f60e32012-08-15 13:59:49 +0100915 .destroy = armada_drm_crtc_destroy,
916 .set_config = drm_crtc_helper_set_config,
917 .page_flip = armada_drm_crtc_page_flip,
Russell Kingc36045e2018-07-30 11:52:34 +0100918 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
919 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Shawn Guo5922a7d2017-02-07 17:16:18 +0800920 .enable_vblank = armada_drm_crtc_enable_vblank,
921 .disable_vblank = armada_drm_crtc_disable_vblank,
Russell King96f60e32012-08-15 13:59:49 +0100922};
923
Russell King0fb29702015-06-06 21:46:53 +0100924static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
Russell King9611cb92014-06-15 11:21:23 +0100925 struct resource *res, int irq, const struct armada_variant *variant,
926 struct device_node *port)
Russell King96f60e32012-08-15 13:59:49 +0100927{
Russell Kingd8c96082014-04-22 11:10:15 +0100928 struct armada_private *priv = drm->dev_private;
Russell King96f60e32012-08-15 13:59:49 +0100929 struct armada_crtc *dcrtc;
Russell Kingde323012015-07-15 18:11:24 +0100930 struct armada_plane *primary;
Russell King96f60e32012-08-15 13:59:49 +0100931 void __iomem *base;
932 int ret;
933
Linus Torvaldsa7d7a142014-08-07 17:36:12 -0700934 base = devm_ioremap_resource(dev, res);
Jingoo Hanc9d53c02014-06-11 14:00:05 +0900935 if (IS_ERR(base))
936 return PTR_ERR(base);
Russell King96f60e32012-08-15 13:59:49 +0100937
938 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
939 if (!dcrtc) {
940 DRM_ERROR("failed to allocate Armada crtc\n");
941 return -ENOMEM;
942 }
943
Russell Kingd8c96082014-04-22 11:10:15 +0100944 if (dev != drm->dev)
945 dev_set_drvdata(dev, dcrtc);
946
Russell King42e62ba2014-04-22 15:24:03 +0100947 dcrtc->variant = variant;
Russell King96f60e32012-08-15 13:59:49 +0100948 dcrtc->base = base;
Russell Kingd8c96082014-04-22 11:10:15 +0100949 dcrtc->num = drm->mode_config.num_crtc;
Russell King96f60e32012-08-15 13:59:49 +0100950 dcrtc->clk = ERR_PTR(-EINVAL);
Russell King96f60e32012-08-15 13:59:49 +0100951 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
952 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
953 spin_lock_init(&dcrtc->irq_lock);
954 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
Russell King96f60e32012-08-15 13:59:49 +0100955
956 /* Initialize some registers which we don't otherwise set */
957 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
958 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
959 writel_relaxed(dcrtc->spu_iopad_ctrl,
960 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
961 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
962 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
963 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
964 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
965 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100966 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
Russell King92298c12018-06-26 17:06:06 +0100967 readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100968 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
Russell King96f60e32012-08-15 13:59:49 +0100969
Russell Kinge5d9ddf2014-04-26 15:19:38 +0100970 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
971 dcrtc);
Russell King33cd3c02017-12-08 12:16:22 +0000972 if (ret < 0)
973 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +0100974
Russell King42e62ba2014-04-22 15:24:03 +0100975 if (dcrtc->variant->init) {
Russell Kingd8c96082014-04-22 11:10:15 +0100976 ret = dcrtc->variant->init(dcrtc, dev);
Russell King33cd3c02017-12-08 12:16:22 +0000977 if (ret)
978 goto err_crtc;
Russell King96f60e32012-08-15 13:59:49 +0100979 }
980
981 /* Ensure AXI pipeline is enabled */
982 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
983
984 priv->dcrtc[dcrtc->num] = dcrtc;
985
Russell King9611cb92014-06-15 11:21:23 +0100986 dcrtc->crtc.port = port;
Russell King1c914ce2015-07-15 18:11:24 +0100987
Russell Kingde323012015-07-15 18:11:24 +0100988 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Russell King33cd3c02017-12-08 12:16:22 +0000989 if (!primary) {
990 ret = -ENOMEM;
991 goto err_crtc;
992 }
Russell King1c914ce2015-07-15 18:11:24 +0100993
Russell Kingd40af7b2018-07-30 11:52:34 +0100994 ret = armada_drm_primary_plane_init(drm, primary);
Russell Kingde323012015-07-15 18:11:24 +0100995 if (ret) {
996 kfree(primary);
Russell King33cd3c02017-12-08 12:16:22 +0000997 goto err_crtc;
Russell Kingde323012015-07-15 18:11:24 +0100998 }
999
1000 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001001 &armada_crtc_funcs, NULL);
Russell King1c914ce2015-07-15 18:11:24 +01001002 if (ret)
1003 goto err_crtc_init;
1004
Russell King96f60e32012-08-15 13:59:49 +01001005 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1006
Russell Kingd8c96082014-04-22 11:10:15 +01001007 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
Russell King1c914ce2015-07-15 18:11:24 +01001008
1009err_crtc_init:
Russell Kingde323012015-07-15 18:11:24 +01001010 primary->base.funcs->destroy(&primary->base);
Russell King33cd3c02017-12-08 12:16:22 +00001011err_crtc:
1012 kfree(dcrtc);
1013
Russell King1c914ce2015-07-15 18:11:24 +01001014 return ret;
Russell King96f60e32012-08-15 13:59:49 +01001015}
Russell Kingd8c96082014-04-22 11:10:15 +01001016
1017static int
1018armada_lcd_bind(struct device *dev, struct device *master, void *data)
1019{
1020 struct platform_device *pdev = to_platform_device(dev);
1021 struct drm_device *drm = data;
1022 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1023 int irq = platform_get_irq(pdev, 0);
1024 const struct armada_variant *variant;
Russell King9611cb92014-06-15 11:21:23 +01001025 struct device_node *port = NULL;
Russell Kingd8c96082014-04-22 11:10:15 +01001026
1027 if (irq < 0)
1028 return irq;
1029
1030 if (!dev->of_node) {
1031 const struct platform_device_id *id;
1032
1033 id = platform_get_device_id(pdev);
1034 if (!id)
1035 return -ENXIO;
1036
1037 variant = (const struct armada_variant *)id->driver_data;
1038 } else {
1039 const struct of_device_id *match;
Russell King9611cb92014-06-15 11:21:23 +01001040 struct device_node *np, *parent = dev->of_node;
Russell Kingd8c96082014-04-22 11:10:15 +01001041
1042 match = of_match_device(dev->driver->of_match_table, dev);
1043 if (!match)
1044 return -ENXIO;
1045
Russell King9611cb92014-06-15 11:21:23 +01001046 np = of_get_child_by_name(parent, "ports");
1047 if (np)
1048 parent = np;
1049 port = of_get_child_by_name(parent, "port");
1050 of_node_put(np);
1051 if (!port) {
Rob Herring4bf99142017-07-18 16:43:04 -05001052 dev_err(dev, "no port node found in %pOF\n", parent);
Russell King9611cb92014-06-15 11:21:23 +01001053 return -ENXIO;
1054 }
1055
Russell Kingd8c96082014-04-22 11:10:15 +01001056 variant = match->data;
1057 }
1058
Russell King9611cb92014-06-15 11:21:23 +01001059 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
Russell Kingd8c96082014-04-22 11:10:15 +01001060}
1061
1062static void
1063armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1064{
1065 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1066
1067 armada_drm_crtc_destroy(&dcrtc->crtc);
1068}
1069
1070static const struct component_ops armada_lcd_ops = {
1071 .bind = armada_lcd_bind,
1072 .unbind = armada_lcd_unbind,
1073};
1074
1075static int armada_lcd_probe(struct platform_device *pdev)
1076{
1077 return component_add(&pdev->dev, &armada_lcd_ops);
1078}
1079
1080static int armada_lcd_remove(struct platform_device *pdev)
1081{
1082 component_del(&pdev->dev, &armada_lcd_ops);
1083 return 0;
1084}
1085
Arvind Yadav85909712017-06-20 10:44:33 +05301086static const struct of_device_id armada_lcd_of_match[] = {
Russell Kingd8c96082014-04-22 11:10:15 +01001087 {
1088 .compatible = "marvell,dove-lcd",
1089 .data = &armada510_ops,
1090 },
1091 {}
1092};
1093MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1094
1095static const struct platform_device_id armada_lcd_platform_ids[] = {
1096 {
1097 .name = "armada-lcd",
1098 .driver_data = (unsigned long)&armada510_ops,
1099 }, {
1100 .name = "armada-510-lcd",
1101 .driver_data = (unsigned long)&armada510_ops,
1102 },
1103 { },
1104};
1105MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1106
1107struct platform_driver armada_lcd_platform_driver = {
1108 .probe = armada_lcd_probe,
1109 .remove = armada_lcd_remove,
1110 .driver = {
1111 .name = "armada-lcd",
1112 .owner = THIS_MODULE,
1113 .of_match_table = armada_lcd_of_match,
1114 },
1115 .id_table = armada_lcd_platform_ids,
1116};