xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/armada/armada_crtc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2012 Russell King
4*4882a593Smuzhiyun  *  Rewritten from the dovefb driver, and Armada510 manuals.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/clk.h>
8*4882a593Smuzhiyun #include <linux/component.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/of_device.h>
11*4882a593Smuzhiyun #include <linux/platform_device.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <drm/drm_atomic.h>
14*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
15*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
16*4882a593Smuzhiyun #include <drm/drm_probe_helper.h>
17*4882a593Smuzhiyun #include <drm/drm_vblank.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "armada_crtc.h"
20*4882a593Smuzhiyun #include "armada_drm.h"
21*4882a593Smuzhiyun #include "armada_fb.h"
22*4882a593Smuzhiyun #include "armada_gem.h"
23*4882a593Smuzhiyun #include "armada_hw.h"
24*4882a593Smuzhiyun #include "armada_plane.h"
25*4882a593Smuzhiyun #include "armada_trace.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  * A note about interlacing.  Let's consider HDMI 1920x1080i.
29*4882a593Smuzhiyun  * The timing parameters we have from X are:
30*4882a593Smuzhiyun  *  Hact HsyA HsyI Htot  Vact VsyA VsyI Vtot
31*4882a593Smuzhiyun  *  1920 2448 2492 2640  1080 1084 1094 1125
32*4882a593Smuzhiyun  * Which get translated to:
33*4882a593Smuzhiyun  *  Hact HsyA HsyI Htot  Vact VsyA VsyI Vtot
34*4882a593Smuzhiyun  *  1920 2448 2492 2640   540  542  547  562
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * This is how it is defined by CEA-861-D - line and pixel numbers are
37*4882a593Smuzhiyun  * referenced to the rising edge of VSYNC and HSYNC.  Total clocks per
38*4882a593Smuzhiyun  * line: 2640.  The odd frame, the first active line is at line 21, and
39*4882a593Smuzhiyun  * the even frame, the first active line is 584.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * LN:    560     561     562     563             567     568    569
42*4882a593Smuzhiyun  * DE:    ~~~|____________________________//__________________________
43*4882a593Smuzhiyun  * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
44*4882a593Smuzhiyun  * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
45*4882a593Smuzhiyun  *  22 blanking lines.  VSYNC at 1320 (referenced to the HSYNC rising edge).
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * LN:    1123   1124    1125      1               5       6      7
48*4882a593Smuzhiyun  * DE:    ~~~|____________________________//__________________________
49*4882a593Smuzhiyun  * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
50*4882a593Smuzhiyun  * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
51*4882a593Smuzhiyun  *  23 blanking lines
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * The Armada LCD Controller line and pixel numbers are, like X timings,
54*4882a593Smuzhiyun  * referenced to the top left of the active frame.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * So, translating these to our LCD controller:
57*4882a593Smuzhiyun  *  Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
58*4882a593Smuzhiyun  *  Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
59*4882a593Smuzhiyun  * Note: Vsync front porch remains constant!
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * if (odd_frame) {
62*4882a593Smuzhiyun  *   vtotal = mode->crtc_vtotal + 1;
63*4882a593Smuzhiyun  *   vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
64*4882a593Smuzhiyun  *   vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
65*4882a593Smuzhiyun  * } else {
66*4882a593Smuzhiyun  *   vtotal = mode->crtc_vtotal;
67*4882a593Smuzhiyun  *   vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
68*4882a593Smuzhiyun  *   vhorizpos = mode->crtc_hsync_start;
69*4882a593Smuzhiyun  * }
70*4882a593Smuzhiyun  * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * So, we need to reprogram these registers on each vsync event:
73*4882a593Smuzhiyun  *  LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * Note: we do not use the frame done interrupts because these appear
76*4882a593Smuzhiyun  * to happen too early, and lead to jitter on the display (presumably
77*4882a593Smuzhiyun  * they occur at the end of the last active line, before the vsync back
78*4882a593Smuzhiyun  * porch, which we're reprogramming.)
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun void
armada_drm_crtc_update_regs(struct armada_crtc * dcrtc,struct armada_regs * regs)82*4882a593Smuzhiyun armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	while (regs->offset != ~0) {
85*4882a593Smuzhiyun 		void __iomem *reg = dcrtc->base + regs->offset;
86*4882a593Smuzhiyun 		uint32_t val;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 		val = regs->mask;
89*4882a593Smuzhiyun 		if (val != 0)
90*4882a593Smuzhiyun 			val &= readl_relaxed(reg);
91*4882a593Smuzhiyun 		writel_relaxed(val | regs->val, reg);
92*4882a593Smuzhiyun 		++regs;
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
armada_drm_crtc_update(struct armada_crtc * dcrtc,bool enable)96*4882a593Smuzhiyun static void armada_drm_crtc_update(struct armada_crtc *dcrtc, bool enable)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	uint32_t dumb_ctrl;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	dumb_ctrl = dcrtc->cfg_dumb_ctrl;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	if (enable)
103*4882a593Smuzhiyun 		dumb_ctrl |= CFG_DUMB_ENA;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/*
106*4882a593Smuzhiyun 	 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
107*4882a593Smuzhiyun 	 * be using SPI or GPIO.  If we set this to DUMB_BLANK, we will
108*4882a593Smuzhiyun 	 * force LCD_D[23:0] to output blank color, overriding the GPIO or
109*4882a593Smuzhiyun 	 * SPI usage.  So leave it as-is unless in DUMB24_RGB888_0 mode.
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 	if (!enable && (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
112*4882a593Smuzhiyun 		dumb_ctrl &= ~DUMB_MASK;
113*4882a593Smuzhiyun 		dumb_ctrl |= DUMB_BLANK;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	armada_updatel(dumb_ctrl,
117*4882a593Smuzhiyun 		       ~(CFG_INV_CSYNC | CFG_INV_HSYNC | CFG_INV_VSYNC),
118*4882a593Smuzhiyun 		       dcrtc->base + LCD_SPU_DUMB_CTRL);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
armada_drm_crtc_queue_state_event(struct drm_crtc * crtc)121*4882a593Smuzhiyun static void armada_drm_crtc_queue_state_event(struct drm_crtc *crtc)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
124*4882a593Smuzhiyun 	struct drm_pending_vblank_event *event;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* If we have an event, we need vblank events enabled */
127*4882a593Smuzhiyun 	event = xchg(&crtc->state->event, NULL);
128*4882a593Smuzhiyun 	if (event) {
129*4882a593Smuzhiyun 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
130*4882a593Smuzhiyun 		dcrtc->event = event;
131*4882a593Smuzhiyun 	}
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
armada_drm_update_gamma(struct drm_crtc * crtc)134*4882a593Smuzhiyun static void armada_drm_update_gamma(struct drm_crtc *crtc)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct drm_property_blob *blob = crtc->state->gamma_lut;
137*4882a593Smuzhiyun 	void __iomem *base = drm_to_armada_crtc(crtc)->base;
138*4882a593Smuzhiyun 	int i;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (blob) {
141*4882a593Smuzhiyun 		struct drm_color_lut *lut = blob->data;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		armada_updatel(CFG_CSB_256x8, CFG_CSB_256x8 | CFG_PDWN256x8,
144*4882a593Smuzhiyun 			       base + LCD_SPU_SRAM_PARA1);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 		for (i = 0; i < 256; i++) {
147*4882a593Smuzhiyun 			writel_relaxed(drm_color_lut_extract(lut[i].red, 8),
148*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_WRDAT);
149*4882a593Smuzhiyun 			writel_relaxed(i | SRAM_WRITE | SRAM_GAMMA_YR,
150*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_CTRL);
151*4882a593Smuzhiyun 			readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
152*4882a593Smuzhiyun 			writel_relaxed(drm_color_lut_extract(lut[i].green, 8),
153*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_WRDAT);
154*4882a593Smuzhiyun 			writel_relaxed(i | SRAM_WRITE | SRAM_GAMMA_UG,
155*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_CTRL);
156*4882a593Smuzhiyun 			readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
157*4882a593Smuzhiyun 			writel_relaxed(drm_color_lut_extract(lut[i].blue, 8),
158*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_WRDAT);
159*4882a593Smuzhiyun 			writel_relaxed(i | SRAM_WRITE | SRAM_GAMMA_VB,
160*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_CTRL);
161*4882a593Smuzhiyun 			readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
162*4882a593Smuzhiyun 		}
163*4882a593Smuzhiyun 		armada_updatel(CFG_GAMMA_ENA, CFG_GAMMA_ENA,
164*4882a593Smuzhiyun 			       base + LCD_SPU_DMA_CTRL0);
165*4882a593Smuzhiyun 	} else {
166*4882a593Smuzhiyun 		armada_updatel(0, CFG_GAMMA_ENA, base + LCD_SPU_DMA_CTRL0);
167*4882a593Smuzhiyun 		armada_updatel(CFG_PDWN256x8, CFG_CSB_256x8 | CFG_PDWN256x8,
168*4882a593Smuzhiyun 			       base + LCD_SPU_SRAM_PARA1);
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
armada_drm_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)172*4882a593Smuzhiyun static enum drm_mode_status armada_drm_crtc_mode_valid(struct drm_crtc *crtc,
173*4882a593Smuzhiyun 	const struct drm_display_mode *mode)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	if (mode->vscan > 1)
178*4882a593Smuzhiyun 		return MODE_NO_VSCAN;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
181*4882a593Smuzhiyun 		return MODE_NO_DBLESCAN;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
184*4882a593Smuzhiyun 		return MODE_H_ILLEGAL;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	/* We can't do interlaced modes if we don't have the SPU_ADV_REG */
187*4882a593Smuzhiyun 	if (!dcrtc->variant->has_spu_adv_reg &&
188*4882a593Smuzhiyun 	    mode->flags & DRM_MODE_FLAG_INTERLACE)
189*4882a593Smuzhiyun 		return MODE_NO_INTERLACE;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	if (mode->flags & (DRM_MODE_FLAG_BCAST | DRM_MODE_FLAG_PIXMUX |
192*4882a593Smuzhiyun 			   DRM_MODE_FLAG_CLKDIV2))
193*4882a593Smuzhiyun 		return MODE_BAD;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	return MODE_OK;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun /* The mode_config.mutex will be held for this call */
armada_drm_crtc_mode_fixup(struct drm_crtc * crtc,const struct drm_display_mode * mode,struct drm_display_mode * adj)199*4882a593Smuzhiyun static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
200*4882a593Smuzhiyun 	const struct drm_display_mode *mode, struct drm_display_mode *adj)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
203*4882a593Smuzhiyun 	int ret;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/*
206*4882a593Smuzhiyun 	 * Set CRTC modesetting parameters for the adjusted mode.  This is
207*4882a593Smuzhiyun 	 * applied after the connectors, bridges, and encoders have fixed up
208*4882a593Smuzhiyun 	 * this mode, as described above drm_atomic_helper_check_modeset().
209*4882a593Smuzhiyun 	 */
210*4882a593Smuzhiyun 	drm_mode_set_crtcinfo(adj, CRTC_INTERLACE_HALVE_V);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/*
213*4882a593Smuzhiyun 	 * Validate the adjusted mode in case an encoder/bridge has set
214*4882a593Smuzhiyun 	 * something we don't support.
215*4882a593Smuzhiyun 	 */
216*4882a593Smuzhiyun 	if (armada_drm_crtc_mode_valid(crtc, adj) != MODE_OK)
217*4882a593Smuzhiyun 		return false;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	/* Check whether the display mode is possible */
220*4882a593Smuzhiyun 	ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
221*4882a593Smuzhiyun 	if (ret)
222*4882a593Smuzhiyun 		return false;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	return true;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /* These are locked by dev->vbl_lock */
armada_drm_crtc_disable_irq(struct armada_crtc * dcrtc,u32 mask)228*4882a593Smuzhiyun static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	if (dcrtc->irq_ena & mask) {
231*4882a593Smuzhiyun 		dcrtc->irq_ena &= ~mask;
232*4882a593Smuzhiyun 		writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
armada_drm_crtc_enable_irq(struct armada_crtc * dcrtc,u32 mask)236*4882a593Smuzhiyun static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	if ((dcrtc->irq_ena & mask) != mask) {
239*4882a593Smuzhiyun 		dcrtc->irq_ena |= mask;
240*4882a593Smuzhiyun 		writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
241*4882a593Smuzhiyun 		if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
242*4882a593Smuzhiyun 			writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
armada_drm_crtc_irq(struct armada_crtc * dcrtc,u32 stat)246*4882a593Smuzhiyun static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct drm_pending_vblank_event *event;
249*4882a593Smuzhiyun 	void __iomem *base = dcrtc->base;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	if (stat & DMA_FF_UNDERFLOW)
252*4882a593Smuzhiyun 		DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
253*4882a593Smuzhiyun 	if (stat & GRA_FF_UNDERFLOW)
254*4882a593Smuzhiyun 		DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	if (stat & VSYNC_IRQ)
257*4882a593Smuzhiyun 		drm_crtc_handle_vblank(&dcrtc->crtc);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	spin_lock(&dcrtc->irq_lock);
260*4882a593Smuzhiyun 	if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
261*4882a593Smuzhiyun 		int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
262*4882a593Smuzhiyun 		uint32_t val;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
265*4882a593Smuzhiyun 		writel_relaxed(dcrtc->v[i].spu_v_h_total,
266*4882a593Smuzhiyun 			       base + LCD_SPUT_V_H_TOTAL);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 		val = readl_relaxed(base + LCD_SPU_ADV_REG);
269*4882a593Smuzhiyun 		val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
270*4882a593Smuzhiyun 		val |= dcrtc->v[i].spu_adv_reg;
271*4882a593Smuzhiyun 		writel_relaxed(val, base + LCD_SPU_ADV_REG);
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	if (stat & dcrtc->irq_ena & DUMB_FRAMEDONE) {
275*4882a593Smuzhiyun 		if (dcrtc->update_pending) {
276*4882a593Smuzhiyun 			armada_drm_crtc_update_regs(dcrtc, dcrtc->regs);
277*4882a593Smuzhiyun 			dcrtc->update_pending = false;
278*4882a593Smuzhiyun 		}
279*4882a593Smuzhiyun 		if (dcrtc->cursor_update) {
280*4882a593Smuzhiyun 			writel_relaxed(dcrtc->cursor_hw_pos,
281*4882a593Smuzhiyun 				       base + LCD_SPU_HWC_OVSA_HPXL_VLN);
282*4882a593Smuzhiyun 			writel_relaxed(dcrtc->cursor_hw_sz,
283*4882a593Smuzhiyun 				       base + LCD_SPU_HWC_HPXL_VLN);
284*4882a593Smuzhiyun 			armada_updatel(CFG_HWC_ENA,
285*4882a593Smuzhiyun 				       CFG_HWC_ENA | CFG_HWC_1BITMOD |
286*4882a593Smuzhiyun 				       CFG_HWC_1BITENA,
287*4882a593Smuzhiyun 				       base + LCD_SPU_DMA_CTRL0);
288*4882a593Smuzhiyun 			dcrtc->cursor_update = false;
289*4882a593Smuzhiyun 		}
290*4882a593Smuzhiyun 		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 	spin_unlock(&dcrtc->irq_lock);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	if (stat & VSYNC_IRQ && !dcrtc->update_pending) {
295*4882a593Smuzhiyun 		event = xchg(&dcrtc->event, NULL);
296*4882a593Smuzhiyun 		if (event) {
297*4882a593Smuzhiyun 			spin_lock(&dcrtc->crtc.dev->event_lock);
298*4882a593Smuzhiyun 			drm_crtc_send_vblank_event(&dcrtc->crtc, event);
299*4882a593Smuzhiyun 			spin_unlock(&dcrtc->crtc.dev->event_lock);
300*4882a593Smuzhiyun 			drm_crtc_vblank_put(&dcrtc->crtc);
301*4882a593Smuzhiyun 		}
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
armada_drm_irq(int irq,void * arg)305*4882a593Smuzhiyun static irqreturn_t armada_drm_irq(int irq, void *arg)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = arg;
308*4882a593Smuzhiyun 	u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	/*
311*4882a593Smuzhiyun 	 * Reading the ISR appears to clear bits provided CLEAN_SPU_IRQ_ISR
312*4882a593Smuzhiyun 	 * is set.  Writing has some other effect to acknowledge the IRQ -
313*4882a593Smuzhiyun 	 * without this, we only get a single IRQ.
314*4882a593Smuzhiyun 	 */
315*4882a593Smuzhiyun 	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	trace_armada_drm_irq(&dcrtc->crtc, stat);
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	/* Mask out those interrupts we haven't enabled */
320*4882a593Smuzhiyun 	v = stat & dcrtc->irq_ena;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
323*4882a593Smuzhiyun 		armada_drm_crtc_irq(dcrtc, stat);
324*4882a593Smuzhiyun 		return IRQ_HANDLED;
325*4882a593Smuzhiyun 	}
326*4882a593Smuzhiyun 	return IRQ_NONE;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /* The mode_config.mutex will be held for this call */
armada_drm_crtc_mode_set_nofb(struct drm_crtc * crtc)330*4882a593Smuzhiyun static void armada_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	struct drm_display_mode *adj = &crtc->state->adjusted_mode;
333*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
334*4882a593Smuzhiyun 	struct armada_regs regs[17];
335*4882a593Smuzhiyun 	uint32_t lm, rm, tm, bm, val, sclk;
336*4882a593Smuzhiyun 	unsigned long flags;
337*4882a593Smuzhiyun 	unsigned i;
338*4882a593Smuzhiyun 	bool interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	i = 0;
341*4882a593Smuzhiyun 	rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
342*4882a593Smuzhiyun 	lm = adj->crtc_htotal - adj->crtc_hsync_end;
343*4882a593Smuzhiyun 	bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
344*4882a593Smuzhiyun 	tm = adj->crtc_vtotal - adj->crtc_vsync_end;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s] mode " DRM_MODE_FMT "\n",
347*4882a593Smuzhiyun 		      crtc->base.id, crtc->name, DRM_MODE_ARG(adj));
348*4882a593Smuzhiyun 	DRM_DEBUG_KMS("lm %d rm %d tm %d bm %d\n", lm, rm, tm, bm);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	/* Now compute the divider for real */
351*4882a593Smuzhiyun 	dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	spin_lock_irqsave(&dcrtc->irq_lock, flags);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	dcrtc->interlaced = interlaced;
358*4882a593Smuzhiyun 	/* Even interlaced/progressive frame */
359*4882a593Smuzhiyun 	dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
360*4882a593Smuzhiyun 				    adj->crtc_htotal;
361*4882a593Smuzhiyun 	dcrtc->v[1].spu_v_porch = tm << 16 | bm;
362*4882a593Smuzhiyun 	val = adj->crtc_hsync_start;
363*4882a593Smuzhiyun 	dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	if (interlaced) {
366*4882a593Smuzhiyun 		/* Odd interlaced frame */
367*4882a593Smuzhiyun 		val -= adj->crtc_htotal / 2;
368*4882a593Smuzhiyun 		dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN;
369*4882a593Smuzhiyun 		dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
370*4882a593Smuzhiyun 						(1 << 16);
371*4882a593Smuzhiyun 		dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
372*4882a593Smuzhiyun 	} else {
373*4882a593Smuzhiyun 		dcrtc->v[0] = dcrtc->v[1];
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
379*4882a593Smuzhiyun 	armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
380*4882a593Smuzhiyun 	armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
381*4882a593Smuzhiyun 	armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
382*4882a593Smuzhiyun 			   LCD_SPUT_V_H_TOTAL);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (dcrtc->variant->has_spu_adv_reg)
385*4882a593Smuzhiyun 		armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
386*4882a593Smuzhiyun 				     ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
387*4882a593Smuzhiyun 				     ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
390*4882a593Smuzhiyun 	armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/*
393*4882a593Smuzhiyun 	 * The documentation doesn't indicate what the normal state of
394*4882a593Smuzhiyun 	 * the sync signals are.  Sebastian Hesselbart kindly probed
395*4882a593Smuzhiyun 	 * these signals on his board to determine their state.
396*4882a593Smuzhiyun 	 *
397*4882a593Smuzhiyun 	 * The non-inverted state of the sync signals is active high.
398*4882a593Smuzhiyun 	 * Setting these bits makes the appropriate signal active low.
399*4882a593Smuzhiyun 	 */
400*4882a593Smuzhiyun 	val = 0;
401*4882a593Smuzhiyun 	if (adj->flags & DRM_MODE_FLAG_NCSYNC)
402*4882a593Smuzhiyun 		val |= CFG_INV_CSYNC;
403*4882a593Smuzhiyun 	if (adj->flags & DRM_MODE_FLAG_NHSYNC)
404*4882a593Smuzhiyun 		val |= CFG_INV_HSYNC;
405*4882a593Smuzhiyun 	if (adj->flags & DRM_MODE_FLAG_NVSYNC)
406*4882a593Smuzhiyun 		val |= CFG_INV_VSYNC;
407*4882a593Smuzhiyun 	armada_reg_queue_mod(regs, i, val, CFG_INV_CSYNC | CFG_INV_HSYNC |
408*4882a593Smuzhiyun 			     CFG_INV_VSYNC, LCD_SPU_DUMB_CTRL);
409*4882a593Smuzhiyun 	armada_reg_queue_end(regs, i);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	armada_drm_crtc_update_regs(dcrtc, regs);
412*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
armada_drm_crtc_atomic_check(struct drm_crtc * crtc,struct drm_crtc_state * state)415*4882a593Smuzhiyun static int armada_drm_crtc_atomic_check(struct drm_crtc *crtc,
416*4882a593Smuzhiyun 					struct drm_crtc_state *state)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (state->gamma_lut && drm_color_lut_size(state->gamma_lut) != 256)
421*4882a593Smuzhiyun 		return -EINVAL;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (state->color_mgmt_changed)
424*4882a593Smuzhiyun 		state->planes_changed = true;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	return 0;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
armada_drm_crtc_atomic_begin(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)429*4882a593Smuzhiyun static void armada_drm_crtc_atomic_begin(struct drm_crtc *crtc,
430*4882a593Smuzhiyun 					 struct drm_crtc_state *old_crtc_state)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	if (crtc->state->color_mgmt_changed)
437*4882a593Smuzhiyun 		armada_drm_update_gamma(crtc);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	dcrtc->regs_idx = 0;
440*4882a593Smuzhiyun 	dcrtc->regs = dcrtc->atomic_regs;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
armada_drm_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)443*4882a593Smuzhiyun static void armada_drm_crtc_atomic_flush(struct drm_crtc *crtc,
444*4882a593Smuzhiyun 					 struct drm_crtc_state *old_crtc_state)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/*
453*4882a593Smuzhiyun 	 * If we aren't doing a full modeset, then we need to queue
454*4882a593Smuzhiyun 	 * the event here.
455*4882a593Smuzhiyun 	 */
456*4882a593Smuzhiyun 	if (!drm_atomic_crtc_needs_modeset(crtc->state)) {
457*4882a593Smuzhiyun 		dcrtc->update_pending = true;
458*4882a593Smuzhiyun 		armada_drm_crtc_queue_state_event(crtc);
459*4882a593Smuzhiyun 		spin_lock_irq(&dcrtc->irq_lock);
460*4882a593Smuzhiyun 		armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
461*4882a593Smuzhiyun 		spin_unlock_irq(&dcrtc->irq_lock);
462*4882a593Smuzhiyun 	} else {
463*4882a593Smuzhiyun 		spin_lock_irq(&dcrtc->irq_lock);
464*4882a593Smuzhiyun 		armada_drm_crtc_update_regs(dcrtc, dcrtc->regs);
465*4882a593Smuzhiyun 		spin_unlock_irq(&dcrtc->irq_lock);
466*4882a593Smuzhiyun 	}
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
armada_drm_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)469*4882a593Smuzhiyun static void armada_drm_crtc_atomic_disable(struct drm_crtc *crtc,
470*4882a593Smuzhiyun 					   struct drm_crtc_state *old_state)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
473*4882a593Smuzhiyun 	struct drm_pending_vblank_event *event;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	if (old_state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
478*4882a593Smuzhiyun 		drm_crtc_vblank_put(crtc);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	drm_crtc_vblank_off(crtc);
481*4882a593Smuzhiyun 	armada_drm_crtc_update(dcrtc, false);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (!crtc->state->active) {
484*4882a593Smuzhiyun 		/*
485*4882a593Smuzhiyun 		 * This modeset will be leaving the CRTC disabled, so
486*4882a593Smuzhiyun 		 * call the backend to disable upstream clocks etc.
487*4882a593Smuzhiyun 		 */
488*4882a593Smuzhiyun 		if (dcrtc->variant->disable)
489*4882a593Smuzhiyun 			dcrtc->variant->disable(dcrtc);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		/*
492*4882a593Smuzhiyun 		 * We will not receive any further vblank events.
493*4882a593Smuzhiyun 		 * Send the flip_done event manually.
494*4882a593Smuzhiyun 		 */
495*4882a593Smuzhiyun 		event = crtc->state->event;
496*4882a593Smuzhiyun 		crtc->state->event = NULL;
497*4882a593Smuzhiyun 		if (event) {
498*4882a593Smuzhiyun 			spin_lock_irq(&crtc->dev->event_lock);
499*4882a593Smuzhiyun 			drm_crtc_send_vblank_event(crtc, event);
500*4882a593Smuzhiyun 			spin_unlock_irq(&crtc->dev->event_lock);
501*4882a593Smuzhiyun 		}
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
armada_drm_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)505*4882a593Smuzhiyun static void armada_drm_crtc_atomic_enable(struct drm_crtc *crtc,
506*4882a593Smuzhiyun 					  struct drm_crtc_state *old_state)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	if (!old_state->active) {
513*4882a593Smuzhiyun 		/*
514*4882a593Smuzhiyun 		 * This modeset is enabling the CRTC after it having
515*4882a593Smuzhiyun 		 * been disabled.  Reverse the call to ->disable in
516*4882a593Smuzhiyun 		 * the atomic_disable().
517*4882a593Smuzhiyun 		 */
518*4882a593Smuzhiyun 		if (dcrtc->variant->enable)
519*4882a593Smuzhiyun 			dcrtc->variant->enable(dcrtc, &crtc->state->adjusted_mode);
520*4882a593Smuzhiyun 	}
521*4882a593Smuzhiyun 	armada_drm_crtc_update(dcrtc, true);
522*4882a593Smuzhiyun 	drm_crtc_vblank_on(crtc);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	if (crtc->state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
525*4882a593Smuzhiyun 		WARN_ON(drm_crtc_vblank_get(crtc));
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	armada_drm_crtc_queue_state_event(crtc);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
531*4882a593Smuzhiyun 	.mode_valid	= armada_drm_crtc_mode_valid,
532*4882a593Smuzhiyun 	.mode_fixup	= armada_drm_crtc_mode_fixup,
533*4882a593Smuzhiyun 	.mode_set_nofb	= armada_drm_crtc_mode_set_nofb,
534*4882a593Smuzhiyun 	.atomic_check	= armada_drm_crtc_atomic_check,
535*4882a593Smuzhiyun 	.atomic_begin	= armada_drm_crtc_atomic_begin,
536*4882a593Smuzhiyun 	.atomic_flush	= armada_drm_crtc_atomic_flush,
537*4882a593Smuzhiyun 	.atomic_disable	= armada_drm_crtc_atomic_disable,
538*4882a593Smuzhiyun 	.atomic_enable	= armada_drm_crtc_atomic_enable,
539*4882a593Smuzhiyun };
540*4882a593Smuzhiyun 
armada_load_cursor_argb(void __iomem * base,uint32_t * pix,unsigned stride,unsigned width,unsigned height)541*4882a593Smuzhiyun static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
542*4882a593Smuzhiyun 	unsigned stride, unsigned width, unsigned height)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun 	uint32_t addr;
545*4882a593Smuzhiyun 	unsigned y;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	addr = SRAM_HWC32_RAM1;
548*4882a593Smuzhiyun 	for (y = 0; y < height; y++) {
549*4882a593Smuzhiyun 		uint32_t *p = &pix[y * stride];
550*4882a593Smuzhiyun 		unsigned x;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 		for (x = 0; x < width; x++, p++) {
553*4882a593Smuzhiyun 			uint32_t val = *p;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 			/*
556*4882a593Smuzhiyun 			 * In "ARGB888" (HWC32) mode, writing to the SRAM
557*4882a593Smuzhiyun 			 * requires these bits to contain:
558*4882a593Smuzhiyun 			 * 31:24 = alpha 23:16 = blue 15:8 = green 7:0 = red
559*4882a593Smuzhiyun 			 * So, it's actually ABGR8888.  This is independent
560*4882a593Smuzhiyun 			 * of the SWAPRB bits in DMA control register 0.
561*4882a593Smuzhiyun 			 */
562*4882a593Smuzhiyun 			val = (val & 0xff00ff00) |
563*4882a593Smuzhiyun 			      (val & 0x000000ff) << 16 |
564*4882a593Smuzhiyun 			      (val & 0x00ff0000) >> 16;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 			writel_relaxed(val,
567*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_WRDAT);
568*4882a593Smuzhiyun 			writel_relaxed(addr | SRAM_WRITE,
569*4882a593Smuzhiyun 				       base + LCD_SPU_SRAM_CTRL);
570*4882a593Smuzhiyun 			readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
571*4882a593Smuzhiyun 			addr += 1;
572*4882a593Smuzhiyun 			if ((addr & 0x00ff) == 0)
573*4882a593Smuzhiyun 				addr += 0xf00;
574*4882a593Smuzhiyun 			if ((addr & 0x30ff) == 0)
575*4882a593Smuzhiyun 				addr = SRAM_HWC32_RAM2;
576*4882a593Smuzhiyun 		}
577*4882a593Smuzhiyun 	}
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
armada_drm_crtc_cursor_tran(void __iomem * base)580*4882a593Smuzhiyun static void armada_drm_crtc_cursor_tran(void __iomem *base)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	unsigned addr;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	for (addr = 0; addr < 256; addr++) {
585*4882a593Smuzhiyun 		/* write the default value */
586*4882a593Smuzhiyun 		writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
587*4882a593Smuzhiyun 		writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
588*4882a593Smuzhiyun 			       base + LCD_SPU_SRAM_CTRL);
589*4882a593Smuzhiyun 	}
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
armada_drm_crtc_cursor_update(struct armada_crtc * dcrtc,bool reload)592*4882a593Smuzhiyun static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
595*4882a593Smuzhiyun 	uint32_t yoff, yscr, h = dcrtc->cursor_h;
596*4882a593Smuzhiyun 	uint32_t para1;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	/*
599*4882a593Smuzhiyun 	 * Calculate the visible width and height of the cursor,
600*4882a593Smuzhiyun 	 * screen position, and the position in the cursor bitmap.
601*4882a593Smuzhiyun 	 */
602*4882a593Smuzhiyun 	if (dcrtc->cursor_x < 0) {
603*4882a593Smuzhiyun 		xoff = -dcrtc->cursor_x;
604*4882a593Smuzhiyun 		xscr = 0;
605*4882a593Smuzhiyun 		w -= min(xoff, w);
606*4882a593Smuzhiyun 	} else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
607*4882a593Smuzhiyun 		xoff = 0;
608*4882a593Smuzhiyun 		xscr = dcrtc->cursor_x;
609*4882a593Smuzhiyun 		w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
610*4882a593Smuzhiyun 	} else {
611*4882a593Smuzhiyun 		xoff = 0;
612*4882a593Smuzhiyun 		xscr = dcrtc->cursor_x;
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (dcrtc->cursor_y < 0) {
616*4882a593Smuzhiyun 		yoff = -dcrtc->cursor_y;
617*4882a593Smuzhiyun 		yscr = 0;
618*4882a593Smuzhiyun 		h -= min(yoff, h);
619*4882a593Smuzhiyun 	} else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
620*4882a593Smuzhiyun 		yoff = 0;
621*4882a593Smuzhiyun 		yscr = dcrtc->cursor_y;
622*4882a593Smuzhiyun 		h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
623*4882a593Smuzhiyun 	} else {
624*4882a593Smuzhiyun 		yoff = 0;
625*4882a593Smuzhiyun 		yscr = dcrtc->cursor_y;
626*4882a593Smuzhiyun 	}
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	/* On interlaced modes, the vertical cursor size must be halved */
629*4882a593Smuzhiyun 	s = dcrtc->cursor_w;
630*4882a593Smuzhiyun 	if (dcrtc->interlaced) {
631*4882a593Smuzhiyun 		s *= 2;
632*4882a593Smuzhiyun 		yscr /= 2;
633*4882a593Smuzhiyun 		h /= 2;
634*4882a593Smuzhiyun 	}
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	if (!dcrtc->cursor_obj || !h || !w) {
637*4882a593Smuzhiyun 		spin_lock_irq(&dcrtc->irq_lock);
638*4882a593Smuzhiyun 		dcrtc->cursor_update = false;
639*4882a593Smuzhiyun 		armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
640*4882a593Smuzhiyun 		spin_unlock_irq(&dcrtc->irq_lock);
641*4882a593Smuzhiyun 		return 0;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	spin_lock_irq(&dcrtc->irq_lock);
645*4882a593Smuzhiyun 	para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
646*4882a593Smuzhiyun 	armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
647*4882a593Smuzhiyun 		       dcrtc->base + LCD_SPU_SRAM_PARA1);
648*4882a593Smuzhiyun 	spin_unlock_irq(&dcrtc->irq_lock);
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	/*
651*4882a593Smuzhiyun 	 * Initialize the transparency if the SRAM was powered down.
652*4882a593Smuzhiyun 	 * We must also reload the cursor data as well.
653*4882a593Smuzhiyun 	 */
654*4882a593Smuzhiyun 	if (!(para1 & CFG_CSB_256x32)) {
655*4882a593Smuzhiyun 		armada_drm_crtc_cursor_tran(dcrtc->base);
656*4882a593Smuzhiyun 		reload = true;
657*4882a593Smuzhiyun 	}
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
660*4882a593Smuzhiyun 		spin_lock_irq(&dcrtc->irq_lock);
661*4882a593Smuzhiyun 		dcrtc->cursor_update = false;
662*4882a593Smuzhiyun 		armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
663*4882a593Smuzhiyun 		spin_unlock_irq(&dcrtc->irq_lock);
664*4882a593Smuzhiyun 		reload = true;
665*4882a593Smuzhiyun 	}
666*4882a593Smuzhiyun 	if (reload) {
667*4882a593Smuzhiyun 		struct armada_gem_object *obj = dcrtc->cursor_obj;
668*4882a593Smuzhiyun 		uint32_t *pix;
669*4882a593Smuzhiyun 		/* Set the top-left corner of the cursor image */
670*4882a593Smuzhiyun 		pix = obj->addr;
671*4882a593Smuzhiyun 		pix += yoff * s + xoff;
672*4882a593Smuzhiyun 		armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	/* Reload the cursor position, size and enable in the IRQ handler */
676*4882a593Smuzhiyun 	spin_lock_irq(&dcrtc->irq_lock);
677*4882a593Smuzhiyun 	dcrtc->cursor_hw_pos = yscr << 16 | xscr;
678*4882a593Smuzhiyun 	dcrtc->cursor_hw_sz = h << 16 | w;
679*4882a593Smuzhiyun 	dcrtc->cursor_update = true;
680*4882a593Smuzhiyun 	armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
681*4882a593Smuzhiyun 	spin_unlock_irq(&dcrtc->irq_lock);
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	return 0;
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
cursor_update(void * data)686*4882a593Smuzhiyun static void cursor_update(void *data)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	armada_drm_crtc_cursor_update(data, true);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
armada_drm_crtc_cursor_set(struct drm_crtc * crtc,struct drm_file * file,uint32_t handle,uint32_t w,uint32_t h)691*4882a593Smuzhiyun static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
692*4882a593Smuzhiyun 	struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
695*4882a593Smuzhiyun 	struct armada_gem_object *obj = NULL;
696*4882a593Smuzhiyun 	int ret;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	/* If no cursor support, replicate drm's return value */
699*4882a593Smuzhiyun 	if (!dcrtc->variant->has_spu_adv_reg)
700*4882a593Smuzhiyun 		return -ENXIO;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	if (handle && w > 0 && h > 0) {
703*4882a593Smuzhiyun 		/* maximum size is 64x32 or 32x64 */
704*4882a593Smuzhiyun 		if (w > 64 || h > 64 || (w > 32 && h > 32))
705*4882a593Smuzhiyun 			return -ENOMEM;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 		obj = armada_gem_object_lookup(file, handle);
708*4882a593Smuzhiyun 		if (!obj)
709*4882a593Smuzhiyun 			return -ENOENT;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		/* Must be a kernel-mapped object */
712*4882a593Smuzhiyun 		if (!obj->addr) {
713*4882a593Smuzhiyun 			drm_gem_object_put(&obj->obj);
714*4882a593Smuzhiyun 			return -EINVAL;
715*4882a593Smuzhiyun 		}
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 		if (obj->obj.size < w * h * 4) {
718*4882a593Smuzhiyun 			DRM_ERROR("buffer is too small\n");
719*4882a593Smuzhiyun 			drm_gem_object_put(&obj->obj);
720*4882a593Smuzhiyun 			return -ENOMEM;
721*4882a593Smuzhiyun 		}
722*4882a593Smuzhiyun 	}
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	if (dcrtc->cursor_obj) {
725*4882a593Smuzhiyun 		dcrtc->cursor_obj->update = NULL;
726*4882a593Smuzhiyun 		dcrtc->cursor_obj->update_data = NULL;
727*4882a593Smuzhiyun 		drm_gem_object_put(&dcrtc->cursor_obj->obj);
728*4882a593Smuzhiyun 	}
729*4882a593Smuzhiyun 	dcrtc->cursor_obj = obj;
730*4882a593Smuzhiyun 	dcrtc->cursor_w = w;
731*4882a593Smuzhiyun 	dcrtc->cursor_h = h;
732*4882a593Smuzhiyun 	ret = armada_drm_crtc_cursor_update(dcrtc, true);
733*4882a593Smuzhiyun 	if (obj) {
734*4882a593Smuzhiyun 		obj->update_data = dcrtc;
735*4882a593Smuzhiyun 		obj->update = cursor_update;
736*4882a593Smuzhiyun 	}
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	return ret;
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun 
armada_drm_crtc_cursor_move(struct drm_crtc * crtc,int x,int y)741*4882a593Smuzhiyun static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
744*4882a593Smuzhiyun 	int ret;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	/* If no cursor support, replicate drm's return value */
747*4882a593Smuzhiyun 	if (!dcrtc->variant->has_spu_adv_reg)
748*4882a593Smuzhiyun 		return -EFAULT;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	dcrtc->cursor_x = x;
751*4882a593Smuzhiyun 	dcrtc->cursor_y = y;
752*4882a593Smuzhiyun 	ret = armada_drm_crtc_cursor_update(dcrtc, false);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	return ret;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun 
armada_drm_crtc_destroy(struct drm_crtc * crtc)757*4882a593Smuzhiyun static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
760*4882a593Smuzhiyun 	struct armada_private *priv = drm_to_armada_dev(crtc->dev);
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	if (dcrtc->cursor_obj)
763*4882a593Smuzhiyun 		drm_gem_object_put(&dcrtc->cursor_obj->obj);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	priv->dcrtc[dcrtc->num] = NULL;
766*4882a593Smuzhiyun 	drm_crtc_cleanup(&dcrtc->crtc);
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	if (dcrtc->variant->disable)
769*4882a593Smuzhiyun 		dcrtc->variant->disable(dcrtc);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	of_node_put(dcrtc->crtc.port);
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	kfree(dcrtc);
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun 
armada_drm_crtc_late_register(struct drm_crtc * crtc)778*4882a593Smuzhiyun static int armada_drm_crtc_late_register(struct drm_crtc *crtc)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_DEBUG_FS))
781*4882a593Smuzhiyun 		armada_drm_crtc_debugfs_init(drm_to_armada_crtc(crtc));
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	return 0;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun /* These are called under the vbl_lock. */
armada_drm_crtc_enable_vblank(struct drm_crtc * crtc)787*4882a593Smuzhiyun static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
790*4882a593Smuzhiyun 	unsigned long flags;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	spin_lock_irqsave(&dcrtc->irq_lock, flags);
793*4882a593Smuzhiyun 	armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
794*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
795*4882a593Smuzhiyun 	return 0;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
armada_drm_crtc_disable_vblank(struct drm_crtc * crtc)798*4882a593Smuzhiyun static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
801*4882a593Smuzhiyun 	unsigned long flags;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	spin_lock_irqsave(&dcrtc->irq_lock, flags);
804*4882a593Smuzhiyun 	armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
805*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun static const struct drm_crtc_funcs armada_crtc_funcs = {
809*4882a593Smuzhiyun 	.reset		= drm_atomic_helper_crtc_reset,
810*4882a593Smuzhiyun 	.cursor_set	= armada_drm_crtc_cursor_set,
811*4882a593Smuzhiyun 	.cursor_move	= armada_drm_crtc_cursor_move,
812*4882a593Smuzhiyun 	.destroy	= armada_drm_crtc_destroy,
813*4882a593Smuzhiyun 	.gamma_set	= drm_atomic_helper_legacy_gamma_set,
814*4882a593Smuzhiyun 	.set_config	= drm_atomic_helper_set_config,
815*4882a593Smuzhiyun 	.page_flip	= drm_atomic_helper_page_flip,
816*4882a593Smuzhiyun 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
817*4882a593Smuzhiyun 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
818*4882a593Smuzhiyun 	.late_register	= armada_drm_crtc_late_register,
819*4882a593Smuzhiyun 	.enable_vblank	= armada_drm_crtc_enable_vblank,
820*4882a593Smuzhiyun 	.disable_vblank	= armada_drm_crtc_disable_vblank,
821*4882a593Smuzhiyun };
822*4882a593Smuzhiyun 
armada_crtc_select_clock(struct armada_crtc * dcrtc,struct armada_clk_result * res,const struct armada_clocking_params * params,struct clk * clks[],size_t num_clks,unsigned long desired_khz)823*4882a593Smuzhiyun int armada_crtc_select_clock(struct armada_crtc *dcrtc,
824*4882a593Smuzhiyun 			     struct armada_clk_result *res,
825*4882a593Smuzhiyun 			     const struct armada_clocking_params *params,
826*4882a593Smuzhiyun 			     struct clk *clks[], size_t num_clks,
827*4882a593Smuzhiyun 			     unsigned long desired_khz)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	unsigned long desired_hz = desired_khz * 1000;
830*4882a593Smuzhiyun 	unsigned long desired_clk_hz;	// requested clk input
831*4882a593Smuzhiyun 	unsigned long real_clk_hz;	// actual clk input
832*4882a593Smuzhiyun 	unsigned long real_hz;		// actual pixel clk
833*4882a593Smuzhiyun 	unsigned long permillage;
834*4882a593Smuzhiyun 	struct clk *clk;
835*4882a593Smuzhiyun 	u32 div;
836*4882a593Smuzhiyun 	int i;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%u:%s] desired clock=%luHz\n",
839*4882a593Smuzhiyun 		      dcrtc->crtc.base.id, dcrtc->crtc.name, desired_hz);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	for (i = 0; i < num_clks; i++) {
842*4882a593Smuzhiyun 		clk = clks[i];
843*4882a593Smuzhiyun 		if (!clk)
844*4882a593Smuzhiyun 			continue;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 		if (params->settable & BIT(i)) {
847*4882a593Smuzhiyun 			real_clk_hz = clk_round_rate(clk, desired_hz);
848*4882a593Smuzhiyun 			desired_clk_hz = desired_hz;
849*4882a593Smuzhiyun 		} else {
850*4882a593Smuzhiyun 			real_clk_hz = clk_get_rate(clk);
851*4882a593Smuzhiyun 			desired_clk_hz = real_clk_hz;
852*4882a593Smuzhiyun 		}
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 		/* If the clock can do exactly the desired rate, we're done */
855*4882a593Smuzhiyun 		if (real_clk_hz == desired_hz) {
856*4882a593Smuzhiyun 			real_hz = real_clk_hz;
857*4882a593Smuzhiyun 			div = 1;
858*4882a593Smuzhiyun 			goto found;
859*4882a593Smuzhiyun 		}
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 		/* Calculate the divider - if invalid, we can't do this rate */
862*4882a593Smuzhiyun 		div = DIV_ROUND_CLOSEST(real_clk_hz, desired_hz);
863*4882a593Smuzhiyun 		if (div == 0 || div > params->div_max)
864*4882a593Smuzhiyun 			continue;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 		/* Calculate the actual rate - HDMI requires -0.6%..+0.5% */
867*4882a593Smuzhiyun 		real_hz = DIV_ROUND_CLOSEST(real_clk_hz, div);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 		DRM_DEBUG_KMS("[CRTC:%u:%s] clk=%u %luHz div=%u real=%luHz\n",
870*4882a593Smuzhiyun 			dcrtc->crtc.base.id, dcrtc->crtc.name,
871*4882a593Smuzhiyun 			i, real_clk_hz, div, real_hz);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 		/* Avoid repeated division */
874*4882a593Smuzhiyun 		if (real_hz < desired_hz) {
875*4882a593Smuzhiyun 			permillage = real_hz / desired_khz;
876*4882a593Smuzhiyun 			if (permillage < params->permillage_min)
877*4882a593Smuzhiyun 				continue;
878*4882a593Smuzhiyun 		} else {
879*4882a593Smuzhiyun 			permillage = DIV_ROUND_UP(real_hz, desired_khz);
880*4882a593Smuzhiyun 			if (permillage > params->permillage_max)
881*4882a593Smuzhiyun 				continue;
882*4882a593Smuzhiyun 		}
883*4882a593Smuzhiyun 		goto found;
884*4882a593Smuzhiyun 	}
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	return -ERANGE;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun found:
889*4882a593Smuzhiyun 	DRM_DEBUG_KMS("[CRTC:%u:%s] selected clk=%u %luHz div=%u real=%luHz\n",
890*4882a593Smuzhiyun 		dcrtc->crtc.base.id, dcrtc->crtc.name,
891*4882a593Smuzhiyun 		i, real_clk_hz, div, real_hz);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	res->desired_clk_hz = desired_clk_hz;
894*4882a593Smuzhiyun 	res->clk = clk;
895*4882a593Smuzhiyun 	res->div = div;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	return i;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
armada_drm_crtc_create(struct drm_device * drm,struct device * dev,struct resource * res,int irq,const struct armada_variant * variant,struct device_node * port)900*4882a593Smuzhiyun static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
901*4882a593Smuzhiyun 	struct resource *res, int irq, const struct armada_variant *variant,
902*4882a593Smuzhiyun 	struct device_node *port)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	struct armada_private *priv = drm_to_armada_dev(drm);
905*4882a593Smuzhiyun 	struct armada_crtc *dcrtc;
906*4882a593Smuzhiyun 	struct drm_plane *primary;
907*4882a593Smuzhiyun 	void __iomem *base;
908*4882a593Smuzhiyun 	int ret;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	base = devm_ioremap_resource(dev, res);
911*4882a593Smuzhiyun 	if (IS_ERR(base))
912*4882a593Smuzhiyun 		return PTR_ERR(base);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
915*4882a593Smuzhiyun 	if (!dcrtc) {
916*4882a593Smuzhiyun 		DRM_ERROR("failed to allocate Armada crtc\n");
917*4882a593Smuzhiyun 		return -ENOMEM;
918*4882a593Smuzhiyun 	}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	if (dev != drm->dev)
921*4882a593Smuzhiyun 		dev_set_drvdata(dev, dcrtc);
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	dcrtc->variant = variant;
924*4882a593Smuzhiyun 	dcrtc->base = base;
925*4882a593Smuzhiyun 	dcrtc->num = drm->mode_config.num_crtc;
926*4882a593Smuzhiyun 	dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
927*4882a593Smuzhiyun 	dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
928*4882a593Smuzhiyun 	spin_lock_init(&dcrtc->irq_lock);
929*4882a593Smuzhiyun 	dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	/* Initialize some registers which we don't otherwise set */
932*4882a593Smuzhiyun 	writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
933*4882a593Smuzhiyun 	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
934*4882a593Smuzhiyun 	writel_relaxed(dcrtc->spu_iopad_ctrl,
935*4882a593Smuzhiyun 		       dcrtc->base + LCD_SPU_IOPAD_CONTROL);
936*4882a593Smuzhiyun 	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
937*4882a593Smuzhiyun 	writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
938*4882a593Smuzhiyun 		       CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
939*4882a593Smuzhiyun 		       CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
940*4882a593Smuzhiyun 	writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
941*4882a593Smuzhiyun 	writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
942*4882a593Smuzhiyun 	readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
943*4882a593Smuzhiyun 	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
946*4882a593Smuzhiyun 			       dcrtc);
947*4882a593Smuzhiyun 	if (ret < 0)
948*4882a593Smuzhiyun 		goto err_crtc;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	if (dcrtc->variant->init) {
951*4882a593Smuzhiyun 		ret = dcrtc->variant->init(dcrtc, dev);
952*4882a593Smuzhiyun 		if (ret)
953*4882a593Smuzhiyun 			goto err_crtc;
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	/* Ensure AXI pipeline is enabled */
957*4882a593Smuzhiyun 	armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	priv->dcrtc[dcrtc->num] = dcrtc;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	dcrtc->crtc.port = port;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
964*4882a593Smuzhiyun 	if (!primary) {
965*4882a593Smuzhiyun 		ret = -ENOMEM;
966*4882a593Smuzhiyun 		goto err_crtc;
967*4882a593Smuzhiyun 	}
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	ret = armada_drm_primary_plane_init(drm, primary);
970*4882a593Smuzhiyun 	if (ret) {
971*4882a593Smuzhiyun 		kfree(primary);
972*4882a593Smuzhiyun 		goto err_crtc;
973*4882a593Smuzhiyun 	}
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, primary, NULL,
976*4882a593Smuzhiyun 					&armada_crtc_funcs, NULL);
977*4882a593Smuzhiyun 	if (ret)
978*4882a593Smuzhiyun 		goto err_crtc_init;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	ret = drm_mode_crtc_set_gamma_size(&dcrtc->crtc, 256);
983*4882a593Smuzhiyun 	if (ret)
984*4882a593Smuzhiyun 		return ret;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	drm_crtc_enable_color_mgmt(&dcrtc->crtc, 0, false, 256);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	return armada_overlay_plane_create(drm, 1 << dcrtc->num);
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun err_crtc_init:
991*4882a593Smuzhiyun 	primary->funcs->destroy(primary);
992*4882a593Smuzhiyun err_crtc:
993*4882a593Smuzhiyun 	kfree(dcrtc);
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	return ret;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun static int
armada_lcd_bind(struct device * dev,struct device * master,void * data)999*4882a593Smuzhiyun armada_lcd_bind(struct device *dev, struct device *master, void *data)
1000*4882a593Smuzhiyun {
1001*4882a593Smuzhiyun 	struct platform_device *pdev = to_platform_device(dev);
1002*4882a593Smuzhiyun 	struct drm_device *drm = data;
1003*4882a593Smuzhiyun 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1004*4882a593Smuzhiyun 	int irq = platform_get_irq(pdev, 0);
1005*4882a593Smuzhiyun 	const struct armada_variant *variant;
1006*4882a593Smuzhiyun 	struct device_node *port = NULL;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	if (irq < 0)
1009*4882a593Smuzhiyun 		return irq;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	if (!dev->of_node) {
1012*4882a593Smuzhiyun 		const struct platform_device_id *id;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 		id = platform_get_device_id(pdev);
1015*4882a593Smuzhiyun 		if (!id)
1016*4882a593Smuzhiyun 			return -ENXIO;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 		variant = (const struct armada_variant *)id->driver_data;
1019*4882a593Smuzhiyun 	} else {
1020*4882a593Smuzhiyun 		const struct of_device_id *match;
1021*4882a593Smuzhiyun 		struct device_node *np, *parent = dev->of_node;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 		match = of_match_device(dev->driver->of_match_table, dev);
1024*4882a593Smuzhiyun 		if (!match)
1025*4882a593Smuzhiyun 			return -ENXIO;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 		np = of_get_child_by_name(parent, "ports");
1028*4882a593Smuzhiyun 		if (np)
1029*4882a593Smuzhiyun 			parent = np;
1030*4882a593Smuzhiyun 		port = of_get_child_by_name(parent, "port");
1031*4882a593Smuzhiyun 		of_node_put(np);
1032*4882a593Smuzhiyun 		if (!port) {
1033*4882a593Smuzhiyun 			dev_err(dev, "no port node found in %pOF\n", parent);
1034*4882a593Smuzhiyun 			return -ENXIO;
1035*4882a593Smuzhiyun 		}
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 		variant = match->data;
1038*4882a593Smuzhiyun 	}
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun static void
armada_lcd_unbind(struct device * dev,struct device * master,void * data)1044*4882a593Smuzhiyun armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1045*4882a593Smuzhiyun {
1046*4882a593Smuzhiyun 	struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	armada_drm_crtc_destroy(&dcrtc->crtc);
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun static const struct component_ops armada_lcd_ops = {
1052*4882a593Smuzhiyun 	.bind = armada_lcd_bind,
1053*4882a593Smuzhiyun 	.unbind = armada_lcd_unbind,
1054*4882a593Smuzhiyun };
1055*4882a593Smuzhiyun 
armada_lcd_probe(struct platform_device * pdev)1056*4882a593Smuzhiyun static int armada_lcd_probe(struct platform_device *pdev)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun 	return component_add(&pdev->dev, &armada_lcd_ops);
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun 
armada_lcd_remove(struct platform_device * pdev)1061*4882a593Smuzhiyun static int armada_lcd_remove(struct platform_device *pdev)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun 	component_del(&pdev->dev, &armada_lcd_ops);
1064*4882a593Smuzhiyun 	return 0;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun static const struct of_device_id armada_lcd_of_match[] = {
1068*4882a593Smuzhiyun 	{
1069*4882a593Smuzhiyun 		.compatible	= "marvell,dove-lcd",
1070*4882a593Smuzhiyun 		.data		= &armada510_ops,
1071*4882a593Smuzhiyun 	},
1072*4882a593Smuzhiyun 	{}
1073*4882a593Smuzhiyun };
1074*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun static const struct platform_device_id armada_lcd_platform_ids[] = {
1077*4882a593Smuzhiyun 	{
1078*4882a593Smuzhiyun 		.name		= "armada-lcd",
1079*4882a593Smuzhiyun 		.driver_data	= (unsigned long)&armada510_ops,
1080*4882a593Smuzhiyun 	}, {
1081*4882a593Smuzhiyun 		.name		= "armada-510-lcd",
1082*4882a593Smuzhiyun 		.driver_data	= (unsigned long)&armada510_ops,
1083*4882a593Smuzhiyun 	},
1084*4882a593Smuzhiyun 	{ },
1085*4882a593Smuzhiyun };
1086*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun struct platform_driver armada_lcd_platform_driver = {
1089*4882a593Smuzhiyun 	.probe	= armada_lcd_probe,
1090*4882a593Smuzhiyun 	.remove	= armada_lcd_remove,
1091*4882a593Smuzhiyun 	.driver = {
1092*4882a593Smuzhiyun 		.name	= "armada-lcd",
1093*4882a593Smuzhiyun 		.owner	=  THIS_MODULE,
1094*4882a593Smuzhiyun 		.of_match_table = armada_lcd_of_match,
1095*4882a593Smuzhiyun 	},
1096*4882a593Smuzhiyun 	.id_table = armada_lcd_platform_ids,
1097*4882a593Smuzhiyun };
1098