xref: /openbmc/linux/drivers/gpu/drm/armada/armada_crtc.c (revision 4c3cf375bc4042eb8682cc6f4314efe66990421a)
196f60e37SRussell King /*
296f60e37SRussell King  * Copyright (C) 2012 Russell King
396f60e37SRussell King  *  Rewritten from the dovefb driver, and Armada510 manuals.
496f60e37SRussell King  *
596f60e37SRussell King  * This program is free software; you can redistribute it and/or modify
696f60e37SRussell King  * it under the terms of the GNU General Public License version 2 as
796f60e37SRussell King  * published by the Free Software Foundation.
896f60e37SRussell King  */
996f60e37SRussell King #include <linux/clk.h>
10d8c96083SRussell King #include <linux/component.h>
11d8c96083SRussell King #include <linux/of_device.h>
12d8c96083SRussell King #include <linux/platform_device.h>
1396f60e37SRussell King #include <drm/drmP.h>
1496f60e37SRussell King #include <drm/drm_crtc_helper.h>
153cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
1696f60e37SRussell King #include "armada_crtc.h"
1796f60e37SRussell King #include "armada_drm.h"
1896f60e37SRussell King #include "armada_fb.h"
1996f60e37SRussell King #include "armada_gem.h"
2096f60e37SRussell King #include "armada_hw.h"
21c8a220c6SRussell King #include "armada_trace.h"
2296f60e37SRussell King 
2396f60e37SRussell King struct armada_frame_work {
244b5dda82SRussell King 	struct armada_plane_work work;
2596f60e37SRussell King 	struct drm_pending_vblank_event *event;
2696f60e37SRussell King 	struct armada_regs regs[4];
2796f60e37SRussell King 	struct drm_framebuffer *old_fb;
2896f60e37SRussell King };
2996f60e37SRussell King 
3096f60e37SRussell King enum csc_mode {
3196f60e37SRussell King 	CSC_AUTO = 0,
3296f60e37SRussell King 	CSC_YUV_CCIR601 = 1,
3396f60e37SRussell King 	CSC_YUV_CCIR709 = 2,
3496f60e37SRussell King 	CSC_RGB_COMPUTER = 1,
3596f60e37SRussell King 	CSC_RGB_STUDIO = 2,
3696f60e37SRussell King };
3796f60e37SRussell King 
381c914cecSRussell King static const uint32_t armada_primary_formats[] = {
391c914cecSRussell King 	DRM_FORMAT_UYVY,
401c914cecSRussell King 	DRM_FORMAT_YUYV,
411c914cecSRussell King 	DRM_FORMAT_VYUY,
421c914cecSRussell King 	DRM_FORMAT_YVYU,
431c914cecSRussell King 	DRM_FORMAT_ARGB8888,
441c914cecSRussell King 	DRM_FORMAT_ABGR8888,
451c914cecSRussell King 	DRM_FORMAT_XRGB8888,
461c914cecSRussell King 	DRM_FORMAT_XBGR8888,
471c914cecSRussell King 	DRM_FORMAT_RGB888,
481c914cecSRussell King 	DRM_FORMAT_BGR888,
491c914cecSRussell King 	DRM_FORMAT_ARGB1555,
501c914cecSRussell King 	DRM_FORMAT_ABGR1555,
511c914cecSRussell King 	DRM_FORMAT_RGB565,
521c914cecSRussell King 	DRM_FORMAT_BGR565,
531c914cecSRussell King };
541c914cecSRussell King 
5596f60e37SRussell King /*
5696f60e37SRussell King  * A note about interlacing.  Let's consider HDMI 1920x1080i.
5796f60e37SRussell King  * The timing parameters we have from X are:
5896f60e37SRussell King  *  Hact HsyA HsyI Htot  Vact VsyA VsyI Vtot
5996f60e37SRussell King  *  1920 2448 2492 2640  1080 1084 1094 1125
6096f60e37SRussell King  * Which get translated to:
6196f60e37SRussell King  *  Hact HsyA HsyI Htot  Vact VsyA VsyI Vtot
6296f60e37SRussell King  *  1920 2448 2492 2640   540  542  547  562
6396f60e37SRussell King  *
6496f60e37SRussell King  * This is how it is defined by CEA-861-D - line and pixel numbers are
6596f60e37SRussell King  * referenced to the rising edge of VSYNC and HSYNC.  Total clocks per
6696f60e37SRussell King  * line: 2640.  The odd frame, the first active line is at line 21, and
6796f60e37SRussell King  * the even frame, the first active line is 584.
6896f60e37SRussell King  *
6996f60e37SRussell King  * LN:    560     561     562     563             567     568    569
7096f60e37SRussell King  * DE:    ~~~|____________________________//__________________________
7196f60e37SRussell King  * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
7296f60e37SRussell King  * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
7396f60e37SRussell King  *  22 blanking lines.  VSYNC at 1320 (referenced to the HSYNC rising edge).
7496f60e37SRussell King  *
7596f60e37SRussell King  * LN:    1123   1124    1125      1               5       6      7
7696f60e37SRussell King  * DE:    ~~~|____________________________//__________________________
7796f60e37SRussell King  * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
7896f60e37SRussell King  * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
7996f60e37SRussell King  *  23 blanking lines
8096f60e37SRussell King  *
8196f60e37SRussell King  * The Armada LCD Controller line and pixel numbers are, like X timings,
8296f60e37SRussell King  * referenced to the top left of the active frame.
8396f60e37SRussell King  *
8496f60e37SRussell King  * So, translating these to our LCD controller:
8596f60e37SRussell King  *  Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
8696f60e37SRussell King  *  Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
8796f60e37SRussell King  * Note: Vsync front porch remains constant!
8896f60e37SRussell King  *
8996f60e37SRussell King  * if (odd_frame) {
9096f60e37SRussell King  *   vtotal = mode->crtc_vtotal + 1;
9196f60e37SRussell King  *   vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
9296f60e37SRussell King  *   vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
9396f60e37SRussell King  * } else {
9496f60e37SRussell King  *   vtotal = mode->crtc_vtotal;
9596f60e37SRussell King  *   vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
9696f60e37SRussell King  *   vhorizpos = mode->crtc_hsync_start;
9796f60e37SRussell King  * }
9896f60e37SRussell King  * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
9996f60e37SRussell King  *
10096f60e37SRussell King  * So, we need to reprogram these registers on each vsync event:
10196f60e37SRussell King  *  LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
10296f60e37SRussell King  *
10396f60e37SRussell King  * Note: we do not use the frame done interrupts because these appear
10496f60e37SRussell King  * to happen too early, and lead to jitter on the display (presumably
10596f60e37SRussell King  * they occur at the end of the last active line, before the vsync back
10696f60e37SRussell King  * porch, which we're reprogramming.)
10796f60e37SRussell King  */
10896f60e37SRussell King 
10996f60e37SRussell King void
11096f60e37SRussell King armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
11196f60e37SRussell King {
11296f60e37SRussell King 	while (regs->offset != ~0) {
11396f60e37SRussell King 		void __iomem *reg = dcrtc->base + regs->offset;
11496f60e37SRussell King 		uint32_t val;
11596f60e37SRussell King 
11696f60e37SRussell King 		val = regs->mask;
11796f60e37SRussell King 		if (val != 0)
11896f60e37SRussell King 			val &= readl_relaxed(reg);
11996f60e37SRussell King 		writel_relaxed(val | regs->val, reg);
12096f60e37SRussell King 		++regs;
12196f60e37SRussell King 	}
12296f60e37SRussell King }
12396f60e37SRussell King 
12496f60e37SRussell King #define dpms_blanked(dpms)	((dpms) != DRM_MODE_DPMS_ON)
12596f60e37SRussell King 
12696f60e37SRussell King static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
12796f60e37SRussell King {
12896f60e37SRussell King 	uint32_t dumb_ctrl;
12996f60e37SRussell King 
13096f60e37SRussell King 	dumb_ctrl = dcrtc->cfg_dumb_ctrl;
13196f60e37SRussell King 
13296f60e37SRussell King 	if (!dpms_blanked(dcrtc->dpms))
13396f60e37SRussell King 		dumb_ctrl |= CFG_DUMB_ENA;
13496f60e37SRussell King 
13596f60e37SRussell King 	/*
13696f60e37SRussell King 	 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
13796f60e37SRussell King 	 * be using SPI or GPIO.  If we set this to DUMB_BLANK, we will
13896f60e37SRussell King 	 * force LCD_D[23:0] to output blank color, overriding the GPIO or
13996f60e37SRussell King 	 * SPI usage.  So leave it as-is unless in DUMB24_RGB888_0 mode.
14096f60e37SRussell King 	 */
14196f60e37SRussell King 	if (dpms_blanked(dcrtc->dpms) &&
14296f60e37SRussell King 	    (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
14396f60e37SRussell King 		dumb_ctrl &= ~DUMB_MASK;
14496f60e37SRussell King 		dumb_ctrl |= DUMB_BLANK;
14596f60e37SRussell King 	}
14696f60e37SRussell King 
14796f60e37SRussell King 	/*
14896f60e37SRussell King 	 * The documentation doesn't indicate what the normal state of
14996f60e37SRussell King 	 * the sync signals are.  Sebastian Hesselbart kindly probed
15096f60e37SRussell King 	 * these signals on his board to determine their state.
15196f60e37SRussell King 	 *
15296f60e37SRussell King 	 * The non-inverted state of the sync signals is active high.
15396f60e37SRussell King 	 * Setting these bits makes the appropriate signal active low.
15496f60e37SRussell King 	 */
15596f60e37SRussell King 	if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
15696f60e37SRussell King 		dumb_ctrl |= CFG_INV_CSYNC;
15796f60e37SRussell King 	if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
15896f60e37SRussell King 		dumb_ctrl |= CFG_INV_HSYNC;
15996f60e37SRussell King 	if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
16096f60e37SRussell King 		dumb_ctrl |= CFG_INV_VSYNC;
16196f60e37SRussell King 
16296f60e37SRussell King 	if (dcrtc->dumb_ctrl != dumb_ctrl) {
16396f60e37SRussell King 		dcrtc->dumb_ctrl = dumb_ctrl;
16496f60e37SRussell King 		writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
16596f60e37SRussell King 	}
16696f60e37SRussell King }
16796f60e37SRussell King 
168f0b24871SRussell King void armada_drm_plane_calc_addrs(u32 *addrs, struct drm_framebuffer *fb,
169f0b24871SRussell King 	int x, int y)
170f0b24871SRussell King {
171f0b24871SRussell King 	u32 addr = drm_fb_obj(fb)->dev_addr;
172bcb0b461SVille Syrjälä 	int num_planes = fb->format->num_planes;
173f0b24871SRussell King 	int i;
174f0b24871SRussell King 
175f0b24871SRussell King 	if (num_planes > 3)
176f0b24871SRussell King 		num_planes = 3;
177f0b24871SRussell King 
178f0b24871SRussell King 	for (i = 0; i < num_planes; i++)
179f0b24871SRussell King 		addrs[i] = addr + fb->offsets[i] + y * fb->pitches[i] +
180353c8598SVille Syrjälä 			     x * fb->format->cpp[i];
181f0b24871SRussell King 	for (; i < 3; i++)
182f0b24871SRussell King 		addrs[i] = 0;
183f0b24871SRussell King }
184f0b24871SRussell King 
18596f60e37SRussell King static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
18696f60e37SRussell King 	int x, int y, struct armada_regs *regs, bool interlaced)
18796f60e37SRussell King {
18896f60e37SRussell King 	unsigned pitch = fb->pitches[0];
189f0b24871SRussell King 	u32 addrs[3], addr_odd, addr_even;
19096f60e37SRussell King 	unsigned i = 0;
19196f60e37SRussell King 
19296f60e37SRussell King 	DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
193272725c7SVille Syrjälä 		pitch, x, y, fb->format->cpp[0] * 8);
19496f60e37SRussell King 
195f0b24871SRussell King 	armada_drm_plane_calc_addrs(addrs, fb, x, y);
196f0b24871SRussell King 
197f0b24871SRussell King 	addr_odd = addr_even = addrs[0];
19896f60e37SRussell King 
19996f60e37SRussell King 	if (interlaced) {
20096f60e37SRussell King 		addr_even += pitch;
20196f60e37SRussell King 		pitch *= 2;
20296f60e37SRussell King 	}
20396f60e37SRussell King 
20496f60e37SRussell King 	/* write offset, base, and pitch */
20596f60e37SRussell King 	armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
20696f60e37SRussell King 	armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
20796f60e37SRussell King 	armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
20896f60e37SRussell King 
20996f60e37SRussell King 	return i;
21096f60e37SRussell King }
21196f60e37SRussell King 
2124b5dda82SRussell King static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
213ec6fb159SRussell King 	struct drm_plane *plane)
2144b5dda82SRussell King {
215ec6fb159SRussell King 	struct armada_plane *dplane = drm_to_armada_plane(plane);
216ec6fb159SRussell King 	struct armada_plane_work *work = xchg(&dplane->work, NULL);
2174b5dda82SRussell King 
2184b5dda82SRussell King 	/* Handle any pending frame work. */
2194b5dda82SRussell King 	if (work) {
220ec6fb159SRussell King 		work->fn(dcrtc, dplane, work);
221accbaf6eSGustavo Padovan 		drm_crtc_vblank_put(&dcrtc->crtc);
2224b5dda82SRussell King 	}
2237cb410cdSRussell King 
224ec6fb159SRussell King 	wake_up(&dplane->frame_wait);
2254b5dda82SRussell King }
2264b5dda82SRussell King 
2274b5dda82SRussell King int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
2284b5dda82SRussell King 	struct armada_plane *plane, struct armada_plane_work *work)
2294b5dda82SRussell King {
2304b5dda82SRussell King 	int ret;
2314b5dda82SRussell King 
232accbaf6eSGustavo Padovan 	ret = drm_crtc_vblank_get(&dcrtc->crtc);
2334b5dda82SRussell King 	if (ret) {
2344b5dda82SRussell King 		DRM_ERROR("failed to acquire vblank counter\n");
2354b5dda82SRussell King 		return ret;
2364b5dda82SRussell King 	}
2374b5dda82SRussell King 
2384b5dda82SRussell King 	ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
2394b5dda82SRussell King 	if (ret)
240accbaf6eSGustavo Padovan 		drm_crtc_vblank_put(&dcrtc->crtc);
2414b5dda82SRussell King 
2424b5dda82SRussell King 	return ret;
2434b5dda82SRussell King }
2444b5dda82SRussell King 
2454b5dda82SRussell King int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
2464b5dda82SRussell King {
2474b5dda82SRussell King 	return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
2484b5dda82SRussell King }
2494b5dda82SRussell King 
2504a8506d2SRussell King struct armada_plane_work *armada_drm_plane_work_cancel(
2514a8506d2SRussell King 	struct armada_crtc *dcrtc, struct armada_plane *plane)
2527c8f7e1aSRussell King {
2534a8506d2SRussell King 	struct armada_plane_work *work = xchg(&plane->work, NULL);
2547c8f7e1aSRussell King 
2554a8506d2SRussell King 	if (work)
256accbaf6eSGustavo Padovan 		drm_crtc_vblank_put(&dcrtc->crtc);
2577c8f7e1aSRussell King 
2584a8506d2SRussell King 	return work;
2597c8f7e1aSRussell King }
2607c8f7e1aSRussell King 
26196f60e37SRussell King static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
26296f60e37SRussell King 	struct armada_frame_work *work)
26396f60e37SRussell King {
2644b5dda82SRussell King 	struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
26596f60e37SRussell King 
2664b5dda82SRussell King 	return armada_drm_plane_work_queue(dcrtc, plane, &work->work);
26796f60e37SRussell King }
26896f60e37SRussell King 
269709ffd82SRussell King static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
2704b5dda82SRussell King 	struct armada_plane *plane, struct armada_plane_work *work)
27196f60e37SRussell King {
2724b5dda82SRussell King 	struct armada_frame_work *fwork = container_of(work, struct armada_frame_work, work);
27396f60e37SRussell King 	struct drm_device *dev = dcrtc->crtc.dev;
274709ffd82SRussell King 	unsigned long flags;
27596f60e37SRussell King 
276709ffd82SRussell King 	spin_lock_irqsave(&dcrtc->irq_lock, flags);
2774b5dda82SRussell King 	armada_drm_crtc_update_regs(dcrtc, fwork->regs);
278709ffd82SRussell King 	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
27996f60e37SRussell King 
2804b5dda82SRussell King 	if (fwork->event) {
281709ffd82SRussell King 		spin_lock_irqsave(&dev->event_lock, flags);
282dd54b806SGustavo Padovan 		drm_crtc_send_vblank_event(&dcrtc->crtc, fwork->event);
283709ffd82SRussell King 		spin_unlock_irqrestore(&dev->event_lock, flags);
284709ffd82SRussell King 	}
28596f60e37SRussell King 
28696f60e37SRussell King 	/* Finally, queue the process-half of the cleanup. */
2874b5dda82SRussell King 	__armada_drm_queue_unref_work(dcrtc->crtc.dev, fwork->old_fb);
2884b5dda82SRussell King 	kfree(fwork);
28996f60e37SRussell King }
29096f60e37SRussell King 
29196f60e37SRussell King static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
29296f60e37SRussell King 	struct drm_framebuffer *fb, bool force)
29396f60e37SRussell King {
29496f60e37SRussell King 	struct armada_frame_work *work;
29596f60e37SRussell King 
29696f60e37SRussell King 	if (!fb)
29796f60e37SRussell King 		return;
29896f60e37SRussell King 
29996f60e37SRussell King 	if (force) {
30096f60e37SRussell King 		/* Display is disabled, so just drop the old fb */
30196f60e37SRussell King 		drm_framebuffer_unreference(fb);
30296f60e37SRussell King 		return;
30396f60e37SRussell King 	}
30496f60e37SRussell King 
30596f60e37SRussell King 	work = kmalloc(sizeof(*work), GFP_KERNEL);
30696f60e37SRussell King 	if (work) {
30796f60e37SRussell King 		int i = 0;
3084b5dda82SRussell King 		work->work.fn = armada_drm_crtc_complete_frame_work;
30996f60e37SRussell King 		work->event = NULL;
31096f60e37SRussell King 		work->old_fb = fb;
31196f60e37SRussell King 		armada_reg_queue_end(work->regs, i);
31296f60e37SRussell King 
31396f60e37SRussell King 		if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
31496f60e37SRussell King 			return;
31596f60e37SRussell King 
31696f60e37SRussell King 		kfree(work);
31796f60e37SRussell King 	}
31896f60e37SRussell King 
31996f60e37SRussell King 	/*
32096f60e37SRussell King 	 * Oops - just drop the reference immediately and hope for
32196f60e37SRussell King 	 * the best.  The worst that will happen is the buffer gets
32296f60e37SRussell King 	 * reused before it has finished being displayed.
32396f60e37SRussell King 	 */
32496f60e37SRussell King 	drm_framebuffer_unreference(fb);
32596f60e37SRussell King }
32696f60e37SRussell King 
32796f60e37SRussell King static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
32896f60e37SRussell King {
32996f60e37SRussell King 	/*
33096f60e37SRussell King 	 * Tell the DRM core that vblank IRQs aren't going to happen for
33196f60e37SRussell King 	 * a while.  This cleans up any pending vblank events for us.
33296f60e37SRussell King 	 */
333178e561fSRussell King 	drm_crtc_vblank_off(&dcrtc->crtc);
334ec6fb159SRussell King 	armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
33596f60e37SRussell King }
33696f60e37SRussell King 
33796f60e37SRussell King /* The mode_config.mutex will be held for this call */
33896f60e37SRussell King static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
33996f60e37SRussell King {
34096f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
34196f60e37SRussell King 
342ea908ba8SRussell King 	if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
34396f60e37SRussell King 		if (dpms_blanked(dpms))
34496f60e37SRussell King 			armada_drm_vblank_off(dcrtc);
345ea908ba8SRussell King 		else if (!IS_ERR(dcrtc->clk))
346ea908ba8SRussell King 			WARN_ON(clk_prepare_enable(dcrtc->clk));
347ea908ba8SRussell King 		dcrtc->dpms = dpms;
348ea908ba8SRussell King 		armada_drm_crtc_update(dcrtc);
349ea908ba8SRussell King 		if (!dpms_blanked(dpms))
350178e561fSRussell King 			drm_crtc_vblank_on(&dcrtc->crtc);
351ea908ba8SRussell King 		else if (!IS_ERR(dcrtc->clk))
352ea908ba8SRussell King 			clk_disable_unprepare(dcrtc->clk);
353ea908ba8SRussell King 	} else if (dcrtc->dpms != dpms) {
354ea908ba8SRussell King 		dcrtc->dpms = dpms;
35596f60e37SRussell King 	}
35696f60e37SRussell King }
35796f60e37SRussell King 
35896f60e37SRussell King /*
35996f60e37SRussell King  * Prepare for a mode set.  Turn off overlay to ensure that we don't end
36096f60e37SRussell King  * up with the overlay size being bigger than the active screen size.
36196f60e37SRussell King  * We rely upon X refreshing this state after the mode set has completed.
36296f60e37SRussell King  *
36396f60e37SRussell King  * The mode_config.mutex will be held for this call
36496f60e37SRussell King  */
36596f60e37SRussell King static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
36696f60e37SRussell King {
36796f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
36896f60e37SRussell King 	struct drm_plane *plane;
36996f60e37SRussell King 
37096f60e37SRussell King 	/*
37196f60e37SRussell King 	 * If we have an overlay plane associated with this CRTC, disable
37296f60e37SRussell King 	 * it before the modeset to avoid its coordinates being outside
373f8e14069SRussell King 	 * the new mode parameters.
37496f60e37SRussell King 	 */
37596f60e37SRussell King 	plane = dcrtc->plane;
376f8e14069SRussell King 	if (plane)
377f8e14069SRussell King 		drm_plane_force_disable(plane);
37896f60e37SRussell King }
37996f60e37SRussell King 
38096f60e37SRussell King /* The mode_config.mutex will be held for this call */
38196f60e37SRussell King static void armada_drm_crtc_commit(struct drm_crtc *crtc)
38296f60e37SRussell King {
38396f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
38496f60e37SRussell King 
38596f60e37SRussell King 	if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
38696f60e37SRussell King 		dcrtc->dpms = DRM_MODE_DPMS_ON;
38796f60e37SRussell King 		armada_drm_crtc_update(dcrtc);
38896f60e37SRussell King 	}
38996f60e37SRussell King }
39096f60e37SRussell King 
39196f60e37SRussell King /* The mode_config.mutex will be held for this call */
39296f60e37SRussell King static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
39396f60e37SRussell King 	const struct drm_display_mode *mode, struct drm_display_mode *adj)
39496f60e37SRussell King {
39596f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
39696f60e37SRussell King 	int ret;
39796f60e37SRussell King 
39896f60e37SRussell King 	/* We can't do interlaced modes if we don't have the SPU_ADV_REG */
39942e62ba7SRussell King 	if (!dcrtc->variant->has_spu_adv_reg &&
40096f60e37SRussell King 	    adj->flags & DRM_MODE_FLAG_INTERLACE)
40196f60e37SRussell King 		return false;
40296f60e37SRussell King 
40396f60e37SRussell King 	/* Check whether the display mode is possible */
40442e62ba7SRussell King 	ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
40596f60e37SRussell King 	if (ret)
40696f60e37SRussell King 		return false;
40796f60e37SRussell King 
40896f60e37SRussell King 	return true;
40996f60e37SRussell King }
41096f60e37SRussell King 
4115922a7d0SShawn Guo /* These are locked by dev->vbl_lock */
4125922a7d0SShawn Guo static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
4135922a7d0SShawn Guo {
4145922a7d0SShawn Guo 	if (dcrtc->irq_ena & mask) {
4155922a7d0SShawn Guo 		dcrtc->irq_ena &= ~mask;
4165922a7d0SShawn Guo 		writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
4175922a7d0SShawn Guo 	}
4185922a7d0SShawn Guo }
4195922a7d0SShawn Guo 
4205922a7d0SShawn Guo static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
4215922a7d0SShawn Guo {
4225922a7d0SShawn Guo 	if ((dcrtc->irq_ena & mask) != mask) {
4235922a7d0SShawn Guo 		dcrtc->irq_ena |= mask;
4245922a7d0SShawn Guo 		writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
4255922a7d0SShawn Guo 		if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
4265922a7d0SShawn Guo 			writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
4275922a7d0SShawn Guo 	}
4285922a7d0SShawn Guo }
4295922a7d0SShawn Guo 
430e5d9ddfbSRussell King static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
43196f60e37SRussell King {
43296f60e37SRussell King 	void __iomem *base = dcrtc->base;
4334a8506d2SRussell King 	struct drm_plane *ovl_plane;
43496f60e37SRussell King 
43596f60e37SRussell King 	if (stat & DMA_FF_UNDERFLOW)
43696f60e37SRussell King 		DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
43796f60e37SRussell King 	if (stat & GRA_FF_UNDERFLOW)
43896f60e37SRussell King 		DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
43996f60e37SRussell King 
44096f60e37SRussell King 	if (stat & VSYNC_IRQ)
4410ac28c57SGustavo Padovan 		drm_crtc_handle_vblank(&dcrtc->crtc);
44296f60e37SRussell King 
44396f60e37SRussell King 	spin_lock(&dcrtc->irq_lock);
4444a8506d2SRussell King 	ovl_plane = dcrtc->plane;
445ec6fb159SRussell King 	if (ovl_plane)
446ec6fb159SRussell King 		armada_drm_plane_work_run(dcrtc, ovl_plane);
44796f60e37SRussell King 
44896f60e37SRussell King 	if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
44996f60e37SRussell King 		int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
45096f60e37SRussell King 		uint32_t val;
45196f60e37SRussell King 
45296f60e37SRussell King 		writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
45396f60e37SRussell King 		writel_relaxed(dcrtc->v[i].spu_v_h_total,
45496f60e37SRussell King 			       base + LCD_SPUT_V_H_TOTAL);
45596f60e37SRussell King 
45696f60e37SRussell King 		val = readl_relaxed(base + LCD_SPU_ADV_REG);
45796f60e37SRussell King 		val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
45896f60e37SRussell King 		val |= dcrtc->v[i].spu_adv_reg;
459662af0d8SRussell King 		writel_relaxed(val, base + LCD_SPU_ADV_REG);
46096f60e37SRussell King 	}
461662af0d8SRussell King 
462662af0d8SRussell King 	if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
463662af0d8SRussell King 		writel_relaxed(dcrtc->cursor_hw_pos,
464662af0d8SRussell King 			       base + LCD_SPU_HWC_OVSA_HPXL_VLN);
465662af0d8SRussell King 		writel_relaxed(dcrtc->cursor_hw_sz,
466662af0d8SRussell King 			       base + LCD_SPU_HWC_HPXL_VLN);
467662af0d8SRussell King 		armada_updatel(CFG_HWC_ENA,
468662af0d8SRussell King 			       CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
469662af0d8SRussell King 			       base + LCD_SPU_DMA_CTRL0);
470662af0d8SRussell King 		dcrtc->cursor_update = false;
471662af0d8SRussell King 		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
472662af0d8SRussell King 	}
473662af0d8SRussell King 
47496f60e37SRussell King 	spin_unlock(&dcrtc->irq_lock);
47596f60e37SRussell King 
476ec6fb159SRussell King 	if (stat & GRA_FRAME_IRQ)
477ec6fb159SRussell King 		armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
47896f60e37SRussell King }
47996f60e37SRussell King 
480e5d9ddfbSRussell King static irqreturn_t armada_drm_irq(int irq, void *arg)
481e5d9ddfbSRussell King {
482e5d9ddfbSRussell King 	struct armada_crtc *dcrtc = arg;
483e5d9ddfbSRussell King 	u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
484e5d9ddfbSRussell King 
485e5d9ddfbSRussell King 	/*
486e5d9ddfbSRussell King 	 * This is rediculous - rather than writing bits to clear, we
487e5d9ddfbSRussell King 	 * have to set the actual status register value.  This is racy.
488e5d9ddfbSRussell King 	 */
489e5d9ddfbSRussell King 	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
490e5d9ddfbSRussell King 
491c8a220c6SRussell King 	trace_armada_drm_irq(&dcrtc->crtc, stat);
492c8a220c6SRussell King 
493e5d9ddfbSRussell King 	/* Mask out those interrupts we haven't enabled */
494e5d9ddfbSRussell King 	v = stat & dcrtc->irq_ena;
495e5d9ddfbSRussell King 
496e5d9ddfbSRussell King 	if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
497e5d9ddfbSRussell King 		armada_drm_crtc_irq(dcrtc, stat);
498e5d9ddfbSRussell King 		return IRQ_HANDLED;
499e5d9ddfbSRussell King 	}
500e5d9ddfbSRussell King 	return IRQ_NONE;
501e5d9ddfbSRussell King }
502e5d9ddfbSRussell King 
50396f60e37SRussell King static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
50496f60e37SRussell King {
50596f60e37SRussell King 	struct drm_display_mode *adj = &dcrtc->crtc.mode;
50696f60e37SRussell King 	uint32_t val = 0;
50796f60e37SRussell King 
50896f60e37SRussell King 	if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
50996f60e37SRussell King 		val |= CFG_CSC_YUV_CCIR709;
51096f60e37SRussell King 	if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
51196f60e37SRussell King 		val |= CFG_CSC_RGB_STUDIO;
51296f60e37SRussell King 
51396f60e37SRussell King 	/*
51496f60e37SRussell King 	 * In auto mode, set the colorimetry, based upon the HDMI spec.
51596f60e37SRussell King 	 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
51696f60e37SRussell King 	 * ITU601.  It may be more appropriate to set this depending on
51796f60e37SRussell King 	 * the source - but what if the graphic frame is YUV and the
51896f60e37SRussell King 	 * video frame is RGB?
51996f60e37SRussell King 	 */
52096f60e37SRussell King 	if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
52196f60e37SRussell King 	     !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
52296f60e37SRussell King 	    (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
52396f60e37SRussell King 		if (dcrtc->csc_yuv_mode == CSC_AUTO)
52496f60e37SRussell King 			val |= CFG_CSC_YUV_CCIR709;
52596f60e37SRussell King 	}
52696f60e37SRussell King 
52796f60e37SRussell King 	/*
52896f60e37SRussell King 	 * We assume we're connected to a TV-like device, so the YUV->RGB
52996f60e37SRussell King 	 * conversion should produce a limited range.  We should set this
53096f60e37SRussell King 	 * depending on the connectors attached to this CRTC, and what
53196f60e37SRussell King 	 * kind of device they report being connected.
53296f60e37SRussell King 	 */
53396f60e37SRussell King 	if (dcrtc->csc_rgb_mode == CSC_AUTO)
53496f60e37SRussell King 		val |= CFG_CSC_RGB_STUDIO;
53596f60e37SRussell King 
53696f60e37SRussell King 	return val;
53796f60e37SRussell King }
53896f60e37SRussell King 
53937af35c7SRussell King static void armada_drm_primary_set(struct drm_crtc *crtc,
54037af35c7SRussell King 	struct drm_plane *plane, int x, int y)
54137af35c7SRussell King {
54237af35c7SRussell King 	struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
54337af35c7SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
5442925db08SRussell King 	struct armada_regs regs[8];
54537af35c7SRussell King 	bool interlaced = dcrtc->interlaced;
54637af35c7SRussell King 	unsigned i;
5472925db08SRussell King 	u32 ctrl0;
54837af35c7SRussell King 
54937af35c7SRussell King 	i = armada_drm_crtc_calc_fb(plane->fb, x, y, regs, interlaced);
55037af35c7SRussell King 
5512925db08SRussell King 	armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
55237af35c7SRussell King 	armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
55337af35c7SRussell King 	armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
55437af35c7SRussell King 
55537af35c7SRussell King 	ctrl0 = state->ctrl0;
55637af35c7SRussell King 	if (interlaced)
55737af35c7SRussell King 		ctrl0 |= CFG_GRA_FTOGGLE;
55837af35c7SRussell King 
55937af35c7SRussell King 	armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
56037af35c7SRussell King 			     CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
56137af35c7SRussell King 					 CFG_SWAPYU | CFG_YUV2RGB) |
56237af35c7SRussell King 			     CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
56337af35c7SRussell King 			     LCD_SPU_DMA_CTRL0);
56437af35c7SRussell King 	armada_reg_queue_end(regs, i);
56537af35c7SRussell King 	armada_drm_crtc_update_regs(dcrtc, regs);
56637af35c7SRussell King }
56737af35c7SRussell King 
56896f60e37SRussell King /* The mode_config.mutex will be held for this call */
56996f60e37SRussell King static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
57096f60e37SRussell King 	struct drm_display_mode *mode, struct drm_display_mode *adj,
57196f60e37SRussell King 	int x, int y, struct drm_framebuffer *old_fb)
57296f60e37SRussell King {
57396f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
57496f60e37SRussell King 	struct armada_regs regs[17];
57596f60e37SRussell King 	uint32_t lm, rm, tm, bm, val, sclk;
57696f60e37SRussell King 	unsigned long flags;
57796f60e37SRussell King 	unsigned i;
57896f60e37SRussell King 	bool interlaced;
57996f60e37SRussell King 
580f4510a27SMatt Roper 	drm_framebuffer_reference(crtc->primary->fb);
58196f60e37SRussell King 
58296f60e37SRussell King 	interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
58396f60e37SRussell King 
5848be523dbSRussell King 	val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
5858be523dbSRussell King 	val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
5868be523dbSRussell King 	val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
58796f60e37SRussell King 
5888be523dbSRussell King 	if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
5898be523dbSRussell King 		val |= CFG_PALETTE_ENA;
5908be523dbSRussell King 
5918be523dbSRussell King 	drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
5928be523dbSRussell King 	drm_to_armada_plane(crtc->primary)->state.src_hw =
5938be523dbSRussell King 	drm_to_armada_plane(crtc->primary)->state.dst_hw =
59437af35c7SRussell King 		adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
5958be523dbSRussell King 	drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
5968be523dbSRussell King 
59737af35c7SRussell King 	i = 0;
59896f60e37SRussell King 	rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
59996f60e37SRussell King 	lm = adj->crtc_htotal - adj->crtc_hsync_end;
60096f60e37SRussell King 	bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
60196f60e37SRussell King 	tm = adj->crtc_vtotal - adj->crtc_vsync_end;
60296f60e37SRussell King 
60396f60e37SRussell King 	DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
60496f60e37SRussell King 		adj->crtc_hdisplay,
60596f60e37SRussell King 		adj->crtc_hsync_start,
60696f60e37SRussell King 		adj->crtc_hsync_end,
60796f60e37SRussell King 		adj->crtc_htotal, lm, rm);
60896f60e37SRussell King 	DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
60996f60e37SRussell King 		adj->crtc_vdisplay,
61096f60e37SRussell King 		adj->crtc_vsync_start,
61196f60e37SRussell King 		adj->crtc_vsync_end,
61296f60e37SRussell King 		adj->crtc_vtotal, tm, bm);
61396f60e37SRussell King 
61496f60e37SRussell King 	/* Wait for pending flips to complete */
6154b5dda82SRussell King 	armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
6164b5dda82SRussell King 				   MAX_SCHEDULE_TIMEOUT);
61796f60e37SRussell King 
618178e561fSRussell King 	drm_crtc_vblank_off(crtc);
61996f60e37SRussell King 
62096f60e37SRussell King 	val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
62196f60e37SRussell King 	if (val != dcrtc->dumb_ctrl) {
62296f60e37SRussell King 		dcrtc->dumb_ctrl = val;
62396f60e37SRussell King 		writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
62496f60e37SRussell King 	}
62596f60e37SRussell King 
626e0ac5e9bSRussell King 	/*
627e0ac5e9bSRussell King 	 * If we are blanked, we would have disabled the clock.  Re-enable
628e0ac5e9bSRussell King 	 * it so that compute_clock() does the right thing.
629e0ac5e9bSRussell King 	 */
630e0ac5e9bSRussell King 	if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
631e0ac5e9bSRussell King 		WARN_ON(clk_prepare_enable(dcrtc->clk));
632e0ac5e9bSRussell King 
63396f60e37SRussell King 	/* Now compute the divider for real */
63442e62ba7SRussell King 	dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
63596f60e37SRussell King 
63696f60e37SRussell King 	/* Ensure graphic fifo is enabled */
63796f60e37SRussell King 	armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
63896f60e37SRussell King 	armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
63996f60e37SRussell King 
64096f60e37SRussell King 	if (interlaced ^ dcrtc->interlaced) {
64196f60e37SRussell King 		if (adj->flags & DRM_MODE_FLAG_INTERLACE)
642accbaf6eSGustavo Padovan 			drm_crtc_vblank_get(&dcrtc->crtc);
64396f60e37SRussell King 		else
644accbaf6eSGustavo Padovan 			drm_crtc_vblank_put(&dcrtc->crtc);
64596f60e37SRussell King 		dcrtc->interlaced = interlaced;
64696f60e37SRussell King 	}
64796f60e37SRussell King 
64896f60e37SRussell King 	spin_lock_irqsave(&dcrtc->irq_lock, flags);
64996f60e37SRussell King 
65096f60e37SRussell King 	/* Even interlaced/progressive frame */
65196f60e37SRussell King 	dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
65296f60e37SRussell King 				    adj->crtc_htotal;
65396f60e37SRussell King 	dcrtc->v[1].spu_v_porch = tm << 16 | bm;
65496f60e37SRussell King 	val = adj->crtc_hsync_start;
655662af0d8SRussell King 	dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
65642e62ba7SRussell King 		dcrtc->variant->spu_adv_reg;
65796f60e37SRussell King 
65896f60e37SRussell King 	if (interlaced) {
65996f60e37SRussell King 		/* Odd interlaced frame */
66096f60e37SRussell King 		dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
66196f60e37SRussell King 						(1 << 16);
66296f60e37SRussell King 		dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
66396f60e37SRussell King 		val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
664662af0d8SRussell King 		dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
66542e62ba7SRussell King 			dcrtc->variant->spu_adv_reg;
66696f60e37SRussell King 	} else {
66796f60e37SRussell King 		dcrtc->v[0] = dcrtc->v[1];
66896f60e37SRussell King 	}
66996f60e37SRussell King 
67096f60e37SRussell King 	val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
67196f60e37SRussell King 
67296f60e37SRussell King 	armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
67396f60e37SRussell King 	armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
67496f60e37SRussell King 	armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
67596f60e37SRussell King 	armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
67696f60e37SRussell King 			   LCD_SPUT_V_H_TOTAL);
67796f60e37SRussell King 
67842e62ba7SRussell King 	if (dcrtc->variant->has_spu_adv_reg) {
67996f60e37SRussell King 		armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
68096f60e37SRussell King 				     ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
68196f60e37SRussell King 				     ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
682662af0d8SRussell King 	}
68396f60e37SRussell King 
68496f60e37SRussell King 	val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
68596f60e37SRussell King 	armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
68696f60e37SRussell King 
68796f60e37SRussell King 	val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
68896f60e37SRussell King 	armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
68996f60e37SRussell King 	armada_reg_queue_end(regs, i);
69096f60e37SRussell King 
69196f60e37SRussell King 	armada_drm_crtc_update_regs(dcrtc, regs);
69237af35c7SRussell King 
69337af35c7SRussell King 	armada_drm_primary_set(crtc, crtc->primary, x, y);
69496f60e37SRussell King 	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
69596f60e37SRussell King 
69696f60e37SRussell King 	armada_drm_crtc_update(dcrtc);
69796f60e37SRussell King 
698178e561fSRussell King 	drm_crtc_vblank_on(crtc);
69996f60e37SRussell King 	armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
70096f60e37SRussell King 
70196f60e37SRussell King 	return 0;
70296f60e37SRussell King }
70396f60e37SRussell King 
70496f60e37SRussell King /* The mode_config.mutex will be held for this call */
70596f60e37SRussell King static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
70696f60e37SRussell King 	struct drm_framebuffer *old_fb)
70796f60e37SRussell King {
70896f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
70996f60e37SRussell King 	struct armada_regs regs[4];
71096f60e37SRussell King 	unsigned i;
71196f60e37SRussell King 
712f4510a27SMatt Roper 	i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
71396f60e37SRussell King 				    dcrtc->interlaced);
71496f60e37SRussell King 	armada_reg_queue_end(regs, i);
71596f60e37SRussell King 
71696f60e37SRussell King 	/* Wait for pending flips to complete */
7174b5dda82SRussell King 	armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
7184b5dda82SRussell King 				   MAX_SCHEDULE_TIMEOUT);
71996f60e37SRussell King 
72096f60e37SRussell King 	/* Take a reference to the new fb as we're using it */
721f4510a27SMatt Roper 	drm_framebuffer_reference(crtc->primary->fb);
72296f60e37SRussell King 
72396f60e37SRussell King 	/* Update the base in the CRTC */
72496f60e37SRussell King 	armada_drm_crtc_update_regs(dcrtc, regs);
72596f60e37SRussell King 
72696f60e37SRussell King 	/* Drop our previously held reference */
72796f60e37SRussell King 	armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
72896f60e37SRussell King 
72996f60e37SRussell King 	return 0;
73096f60e37SRussell King }
73196f60e37SRussell King 
73258326803SRussell King void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
73358326803SRussell King 	struct drm_plane *plane)
73458326803SRussell King {
7359099ea19SRussell King 	u32 sram_para1, dma_ctrl0_mask;
73658326803SRussell King 
73758326803SRussell King 	/*
73858326803SRussell King 	 * Drop our reference on any framebuffer attached to this plane.
73958326803SRussell King 	 * We don't need to NULL this out as drm_plane_force_disable(),
74058326803SRussell King 	 * and __setplane_internal() will do so for an overlay plane, and
74158326803SRussell King 	 * __drm_helper_disable_unused_functions() will do so for the
74258326803SRussell King 	 * primary plane.
74358326803SRussell King 	 */
74458326803SRussell King 	if (plane->fb)
74558326803SRussell King 		drm_framebuffer_unreference(plane->fb);
74658326803SRussell King 
74758326803SRussell King 	/* Power down the Y/U/V FIFOs */
74858326803SRussell King 	sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
74958326803SRussell King 
75058326803SRussell King 	/* Power down most RAMs and FIFOs if this is the primary plane */
7519099ea19SRussell King 	if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
75258326803SRussell King 		sram_para1 |= CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
75358326803SRussell King 			      CFG_PDWN32x32 | CFG_PDWN64x66;
7549099ea19SRussell King 		dma_ctrl0_mask = CFG_GRA_ENA;
7559099ea19SRussell King 	} else {
7569099ea19SRussell King 		dma_ctrl0_mask = CFG_DMA_ENA;
7579099ea19SRussell King 	}
7589099ea19SRussell King 
7599099ea19SRussell King 	spin_lock_irq(&dcrtc->irq_lock);
7609099ea19SRussell King 	armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
7619099ea19SRussell King 	spin_unlock_irq(&dcrtc->irq_lock);
76258326803SRussell King 
76358326803SRussell King 	armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
76458326803SRussell King }
76558326803SRussell King 
76696f60e37SRussell King /* The mode_config.mutex will be held for this call */
76796f60e37SRussell King static void armada_drm_crtc_disable(struct drm_crtc *crtc)
76896f60e37SRussell King {
76996f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
77096f60e37SRussell King 
77196f60e37SRussell King 	armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
77258326803SRussell King 	armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
77396f60e37SRussell King }
77496f60e37SRussell King 
77596f60e37SRussell King static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
77696f60e37SRussell King 	.dpms		= armada_drm_crtc_dpms,
77796f60e37SRussell King 	.prepare	= armada_drm_crtc_prepare,
77896f60e37SRussell King 	.commit		= armada_drm_crtc_commit,
77996f60e37SRussell King 	.mode_fixup	= armada_drm_crtc_mode_fixup,
78096f60e37SRussell King 	.mode_set	= armada_drm_crtc_mode_set,
78196f60e37SRussell King 	.mode_set_base	= armada_drm_crtc_mode_set_base,
78296f60e37SRussell King 	.disable	= armada_drm_crtc_disable,
78396f60e37SRussell King };
78496f60e37SRussell King 
785662af0d8SRussell King static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
786662af0d8SRussell King 	unsigned stride, unsigned width, unsigned height)
787662af0d8SRussell King {
788662af0d8SRussell King 	uint32_t addr;
789662af0d8SRussell King 	unsigned y;
790662af0d8SRussell King 
791662af0d8SRussell King 	addr = SRAM_HWC32_RAM1;
792662af0d8SRussell King 	for (y = 0; y < height; y++) {
793662af0d8SRussell King 		uint32_t *p = &pix[y * stride];
794662af0d8SRussell King 		unsigned x;
795662af0d8SRussell King 
796662af0d8SRussell King 		for (x = 0; x < width; x++, p++) {
797662af0d8SRussell King 			uint32_t val = *p;
798662af0d8SRussell King 
799662af0d8SRussell King 			val = (val & 0xff00ff00) |
800662af0d8SRussell King 			      (val & 0x000000ff) << 16 |
801662af0d8SRussell King 			      (val & 0x00ff0000) >> 16;
802662af0d8SRussell King 
803662af0d8SRussell King 			writel_relaxed(val,
804662af0d8SRussell King 				       base + LCD_SPU_SRAM_WRDAT);
805662af0d8SRussell King 			writel_relaxed(addr | SRAM_WRITE,
806662af0d8SRussell King 				       base + LCD_SPU_SRAM_CTRL);
807c39b0695SRussell King 			readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
808662af0d8SRussell King 			addr += 1;
809662af0d8SRussell King 			if ((addr & 0x00ff) == 0)
810662af0d8SRussell King 				addr += 0xf00;
811662af0d8SRussell King 			if ((addr & 0x30ff) == 0)
812662af0d8SRussell King 				addr = SRAM_HWC32_RAM2;
813662af0d8SRussell King 		}
814662af0d8SRussell King 	}
815662af0d8SRussell King }
816662af0d8SRussell King 
817662af0d8SRussell King static void armada_drm_crtc_cursor_tran(void __iomem *base)
818662af0d8SRussell King {
819662af0d8SRussell King 	unsigned addr;
820662af0d8SRussell King 
821662af0d8SRussell King 	for (addr = 0; addr < 256; addr++) {
822662af0d8SRussell King 		/* write the default value */
823662af0d8SRussell King 		writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
824662af0d8SRussell King 		writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
825662af0d8SRussell King 			       base + LCD_SPU_SRAM_CTRL);
826662af0d8SRussell King 	}
827662af0d8SRussell King }
828662af0d8SRussell King 
829662af0d8SRussell King static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
830662af0d8SRussell King {
831662af0d8SRussell King 	uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
832662af0d8SRussell King 	uint32_t yoff, yscr, h = dcrtc->cursor_h;
833662af0d8SRussell King 	uint32_t para1;
834662af0d8SRussell King 
835662af0d8SRussell King 	/*
836662af0d8SRussell King 	 * Calculate the visible width and height of the cursor,
837662af0d8SRussell King 	 * screen position, and the position in the cursor bitmap.
838662af0d8SRussell King 	 */
839662af0d8SRussell King 	if (dcrtc->cursor_x < 0) {
840662af0d8SRussell King 		xoff = -dcrtc->cursor_x;
841662af0d8SRussell King 		xscr = 0;
842662af0d8SRussell King 		w -= min(xoff, w);
843662af0d8SRussell King 	} else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
844662af0d8SRussell King 		xoff = 0;
845662af0d8SRussell King 		xscr = dcrtc->cursor_x;
846662af0d8SRussell King 		w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
847662af0d8SRussell King 	} else {
848662af0d8SRussell King 		xoff = 0;
849662af0d8SRussell King 		xscr = dcrtc->cursor_x;
850662af0d8SRussell King 	}
851662af0d8SRussell King 
852662af0d8SRussell King 	if (dcrtc->cursor_y < 0) {
853662af0d8SRussell King 		yoff = -dcrtc->cursor_y;
854662af0d8SRussell King 		yscr = 0;
855662af0d8SRussell King 		h -= min(yoff, h);
856662af0d8SRussell King 	} else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
857662af0d8SRussell King 		yoff = 0;
858662af0d8SRussell King 		yscr = dcrtc->cursor_y;
859662af0d8SRussell King 		h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
860662af0d8SRussell King 	} else {
861662af0d8SRussell King 		yoff = 0;
862662af0d8SRussell King 		yscr = dcrtc->cursor_y;
863662af0d8SRussell King 	}
864662af0d8SRussell King 
865662af0d8SRussell King 	/* On interlaced modes, the vertical cursor size must be halved */
866662af0d8SRussell King 	s = dcrtc->cursor_w;
867662af0d8SRussell King 	if (dcrtc->interlaced) {
868662af0d8SRussell King 		s *= 2;
869662af0d8SRussell King 		yscr /= 2;
870662af0d8SRussell King 		h /= 2;
871662af0d8SRussell King 	}
872662af0d8SRussell King 
873662af0d8SRussell King 	if (!dcrtc->cursor_obj || !h || !w) {
874662af0d8SRussell King 		spin_lock_irq(&dcrtc->irq_lock);
875662af0d8SRussell King 		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
876662af0d8SRussell King 		dcrtc->cursor_update = false;
877662af0d8SRussell King 		armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
878662af0d8SRussell King 		spin_unlock_irq(&dcrtc->irq_lock);
879662af0d8SRussell King 		return 0;
880662af0d8SRussell King 	}
881662af0d8SRussell King 
882662af0d8SRussell King 	para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
883662af0d8SRussell King 	armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
884662af0d8SRussell King 		       dcrtc->base + LCD_SPU_SRAM_PARA1);
885662af0d8SRussell King 
886662af0d8SRussell King 	/*
887662af0d8SRussell King 	 * Initialize the transparency if the SRAM was powered down.
888662af0d8SRussell King 	 * We must also reload the cursor data as well.
889662af0d8SRussell King 	 */
890662af0d8SRussell King 	if (!(para1 & CFG_CSB_256x32)) {
891662af0d8SRussell King 		armada_drm_crtc_cursor_tran(dcrtc->base);
892662af0d8SRussell King 		reload = true;
893662af0d8SRussell King 	}
894662af0d8SRussell King 
895662af0d8SRussell King 	if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
896662af0d8SRussell King 		spin_lock_irq(&dcrtc->irq_lock);
897662af0d8SRussell King 		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
898662af0d8SRussell King 		dcrtc->cursor_update = false;
899662af0d8SRussell King 		armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
900662af0d8SRussell King 		spin_unlock_irq(&dcrtc->irq_lock);
901662af0d8SRussell King 		reload = true;
902662af0d8SRussell King 	}
903662af0d8SRussell King 	if (reload) {
904662af0d8SRussell King 		struct armada_gem_object *obj = dcrtc->cursor_obj;
905662af0d8SRussell King 		uint32_t *pix;
906662af0d8SRussell King 		/* Set the top-left corner of the cursor image */
907662af0d8SRussell King 		pix = obj->addr;
908662af0d8SRussell King 		pix += yoff * s + xoff;
909662af0d8SRussell King 		armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
910662af0d8SRussell King 	}
911662af0d8SRussell King 
912662af0d8SRussell King 	/* Reload the cursor position, size and enable in the IRQ handler */
913662af0d8SRussell King 	spin_lock_irq(&dcrtc->irq_lock);
914662af0d8SRussell King 	dcrtc->cursor_hw_pos = yscr << 16 | xscr;
915662af0d8SRussell King 	dcrtc->cursor_hw_sz = h << 16 | w;
916662af0d8SRussell King 	dcrtc->cursor_update = true;
917662af0d8SRussell King 	armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
918662af0d8SRussell King 	spin_unlock_irq(&dcrtc->irq_lock);
919662af0d8SRussell King 
920662af0d8SRussell King 	return 0;
921662af0d8SRussell King }
922662af0d8SRussell King 
923662af0d8SRussell King static void cursor_update(void *data)
924662af0d8SRussell King {
925662af0d8SRussell King 	armada_drm_crtc_cursor_update(data, true);
926662af0d8SRussell King }
927662af0d8SRussell King 
928662af0d8SRussell King static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
929662af0d8SRussell King 	struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
930662af0d8SRussell King {
931662af0d8SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
932662af0d8SRussell King 	struct armada_gem_object *obj = NULL;
933662af0d8SRussell King 	int ret;
934662af0d8SRussell King 
935662af0d8SRussell King 	/* If no cursor support, replicate drm's return value */
93642e62ba7SRussell King 	if (!dcrtc->variant->has_spu_adv_reg)
937662af0d8SRussell King 		return -ENXIO;
938662af0d8SRussell King 
939662af0d8SRussell King 	if (handle && w > 0 && h > 0) {
940662af0d8SRussell King 		/* maximum size is 64x32 or 32x64 */
941662af0d8SRussell King 		if (w > 64 || h > 64 || (w > 32 && h > 32))
942662af0d8SRussell King 			return -ENOMEM;
943662af0d8SRussell King 
944a8ad0bd8SChris Wilson 		obj = armada_gem_object_lookup(file, handle);
945662af0d8SRussell King 		if (!obj)
946662af0d8SRussell King 			return -ENOENT;
947662af0d8SRussell King 
948662af0d8SRussell King 		/* Must be a kernel-mapped object */
949662af0d8SRussell King 		if (!obj->addr) {
950*4c3cf375SHaneen Mohammed 			drm_gem_object_put_unlocked(&obj->obj);
951662af0d8SRussell King 			return -EINVAL;
952662af0d8SRussell King 		}
953662af0d8SRussell King 
954662af0d8SRussell King 		if (obj->obj.size < w * h * 4) {
955662af0d8SRussell King 			DRM_ERROR("buffer is too small\n");
956*4c3cf375SHaneen Mohammed 			drm_gem_object_put_unlocked(&obj->obj);
957662af0d8SRussell King 			return -ENOMEM;
958662af0d8SRussell King 		}
959662af0d8SRussell King 	}
960662af0d8SRussell King 
961662af0d8SRussell King 	if (dcrtc->cursor_obj) {
962662af0d8SRussell King 		dcrtc->cursor_obj->update = NULL;
963662af0d8SRussell King 		dcrtc->cursor_obj->update_data = NULL;
964*4c3cf375SHaneen Mohammed 		drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
965662af0d8SRussell King 	}
966662af0d8SRussell King 	dcrtc->cursor_obj = obj;
967662af0d8SRussell King 	dcrtc->cursor_w = w;
968662af0d8SRussell King 	dcrtc->cursor_h = h;
969662af0d8SRussell King 	ret = armada_drm_crtc_cursor_update(dcrtc, true);
970662af0d8SRussell King 	if (obj) {
971662af0d8SRussell King 		obj->update_data = dcrtc;
972662af0d8SRussell King 		obj->update = cursor_update;
973662af0d8SRussell King 	}
974662af0d8SRussell King 
975662af0d8SRussell King 	return ret;
976662af0d8SRussell King }
977662af0d8SRussell King 
978662af0d8SRussell King static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
979662af0d8SRussell King {
980662af0d8SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
981662af0d8SRussell King 	int ret;
982662af0d8SRussell King 
983662af0d8SRussell King 	/* If no cursor support, replicate drm's return value */
98442e62ba7SRussell King 	if (!dcrtc->variant->has_spu_adv_reg)
985662af0d8SRussell King 		return -EFAULT;
986662af0d8SRussell King 
987662af0d8SRussell King 	dcrtc->cursor_x = x;
988662af0d8SRussell King 	dcrtc->cursor_y = y;
989662af0d8SRussell King 	ret = armada_drm_crtc_cursor_update(dcrtc, false);
990662af0d8SRussell King 
991662af0d8SRussell King 	return ret;
992662af0d8SRussell King }
993662af0d8SRussell King 
99496f60e37SRussell King static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
99596f60e37SRussell King {
99696f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
99796f60e37SRussell King 	struct armada_private *priv = crtc->dev->dev_private;
99896f60e37SRussell King 
999662af0d8SRussell King 	if (dcrtc->cursor_obj)
1000*4c3cf375SHaneen Mohammed 		drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
1001662af0d8SRussell King 
100296f60e37SRussell King 	priv->dcrtc[dcrtc->num] = NULL;
100396f60e37SRussell King 	drm_crtc_cleanup(&dcrtc->crtc);
100496f60e37SRussell King 
100596f60e37SRussell King 	if (!IS_ERR(dcrtc->clk))
100696f60e37SRussell King 		clk_disable_unprepare(dcrtc->clk);
100796f60e37SRussell King 
1008e5d9ddfbSRussell King 	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1009e5d9ddfbSRussell King 
10109611cb93SRussell King 	of_node_put(dcrtc->crtc.port);
10119611cb93SRussell King 
101296f60e37SRussell King 	kfree(dcrtc);
101396f60e37SRussell King }
101496f60e37SRussell King 
101596f60e37SRussell King /*
101696f60e37SRussell King  * The mode_config lock is held here, to prevent races between this
101796f60e37SRussell King  * and a mode_set.
101896f60e37SRussell King  */
101996f60e37SRussell King static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
102041292b1fSDaniel Vetter 	struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags,
102141292b1fSDaniel Vetter 	struct drm_modeset_acquire_ctx *ctx)
102296f60e37SRussell King {
102396f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
102496f60e37SRussell King 	struct armada_frame_work *work;
102596f60e37SRussell King 	unsigned i;
102696f60e37SRussell King 	int ret;
102796f60e37SRussell King 
102896f60e37SRussell King 	/* We don't support changing the pixel format */
1029dbd4d576SVille Syrjälä 	if (fb->format != crtc->primary->fb->format)
103096f60e37SRussell King 		return -EINVAL;
103196f60e37SRussell King 
103296f60e37SRussell King 	work = kmalloc(sizeof(*work), GFP_KERNEL);
103396f60e37SRussell King 	if (!work)
103496f60e37SRussell King 		return -ENOMEM;
103596f60e37SRussell King 
10364b5dda82SRussell King 	work->work.fn = armada_drm_crtc_complete_frame_work;
103796f60e37SRussell King 	work->event = event;
1038f4510a27SMatt Roper 	work->old_fb = dcrtc->crtc.primary->fb;
103996f60e37SRussell King 
104096f60e37SRussell King 	i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
104196f60e37SRussell King 				    dcrtc->interlaced);
104296f60e37SRussell King 	armada_reg_queue_end(work->regs, i);
104396f60e37SRussell King 
104496f60e37SRussell King 	/*
1045c5488307SRussell King 	 * Ensure that we hold a reference on the new framebuffer.
1046c5488307SRussell King 	 * This has to match the behaviour in mode_set.
104796f60e37SRussell King 	 */
1048c5488307SRussell King 	drm_framebuffer_reference(fb);
104996f60e37SRussell King 
105096f60e37SRussell King 	ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
105196f60e37SRussell King 	if (ret) {
1052c5488307SRussell King 		/* Undo our reference above */
1053c5488307SRussell King 		drm_framebuffer_unreference(fb);
105496f60e37SRussell King 		kfree(work);
105596f60e37SRussell King 		return ret;
105696f60e37SRussell King 	}
105796f60e37SRussell King 
105896f60e37SRussell King 	/*
105996f60e37SRussell King 	 * Don't take a reference on the new framebuffer;
106096f60e37SRussell King 	 * drm_mode_page_flip_ioctl() has already grabbed a reference and
106196f60e37SRussell King 	 * will _not_ drop that reference on successful return from this
106296f60e37SRussell King 	 * function.  Simply mark this new framebuffer as the current one.
106396f60e37SRussell King 	 */
1064f4510a27SMatt Roper 	dcrtc->crtc.primary->fb = fb;
106596f60e37SRussell King 
106696f60e37SRussell King 	/*
106796f60e37SRussell King 	 * Finally, if the display is blanked, we won't receive an
106896f60e37SRussell King 	 * interrupt, so complete it now.
106996f60e37SRussell King 	 */
10704b5dda82SRussell King 	if (dpms_blanked(dcrtc->dpms))
1071ec6fb159SRussell King 		armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
107296f60e37SRussell King 
107396f60e37SRussell King 	return 0;
107496f60e37SRussell King }
107596f60e37SRussell King 
107696f60e37SRussell King static int
107796f60e37SRussell King armada_drm_crtc_set_property(struct drm_crtc *crtc,
107896f60e37SRussell King 	struct drm_property *property, uint64_t val)
107996f60e37SRussell King {
108096f60e37SRussell King 	struct armada_private *priv = crtc->dev->dev_private;
108196f60e37SRussell King 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
108296f60e37SRussell King 	bool update_csc = false;
108396f60e37SRussell King 
108496f60e37SRussell King 	if (property == priv->csc_yuv_prop) {
108596f60e37SRussell King 		dcrtc->csc_yuv_mode = val;
108696f60e37SRussell King 		update_csc = true;
108796f60e37SRussell King 	} else if (property == priv->csc_rgb_prop) {
108896f60e37SRussell King 		dcrtc->csc_rgb_mode = val;
108996f60e37SRussell King 		update_csc = true;
109096f60e37SRussell King 	}
109196f60e37SRussell King 
109296f60e37SRussell King 	if (update_csc) {
109396f60e37SRussell King 		uint32_t val;
109496f60e37SRussell King 
109596f60e37SRussell King 		val = dcrtc->spu_iopad_ctrl |
109696f60e37SRussell King 		      armada_drm_crtc_calculate_csc(dcrtc);
109796f60e37SRussell King 		writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
109896f60e37SRussell King 	}
109996f60e37SRussell King 
110096f60e37SRussell King 	return 0;
110196f60e37SRussell King }
110296f60e37SRussell King 
11035922a7d0SShawn Guo /* These are called under the vbl_lock. */
11045922a7d0SShawn Guo static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
11055922a7d0SShawn Guo {
11065922a7d0SShawn Guo 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
11075922a7d0SShawn Guo 
11085922a7d0SShawn Guo 	armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
11095922a7d0SShawn Guo 	return 0;
11105922a7d0SShawn Guo }
11115922a7d0SShawn Guo 
11125922a7d0SShawn Guo static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
11135922a7d0SShawn Guo {
11145922a7d0SShawn Guo 	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
11155922a7d0SShawn Guo 
11165922a7d0SShawn Guo 	armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
11175922a7d0SShawn Guo }
11185922a7d0SShawn Guo 
1119a02fb90aSVille Syrjälä static const struct drm_crtc_funcs armada_crtc_funcs = {
1120662af0d8SRussell King 	.cursor_set	= armada_drm_crtc_cursor_set,
1121662af0d8SRussell King 	.cursor_move	= armada_drm_crtc_cursor_move,
112296f60e37SRussell King 	.destroy	= armada_drm_crtc_destroy,
112396f60e37SRussell King 	.set_config	= drm_crtc_helper_set_config,
112496f60e37SRussell King 	.page_flip	= armada_drm_crtc_page_flip,
112596f60e37SRussell King 	.set_property	= armada_drm_crtc_set_property,
11265922a7d0SShawn Guo 	.enable_vblank	= armada_drm_crtc_enable_vblank,
11275922a7d0SShawn Guo 	.disable_vblank	= armada_drm_crtc_disable_vblank,
112896f60e37SRussell King };
112996f60e37SRussell King 
1130de32301bSRussell King static const struct drm_plane_funcs armada_primary_plane_funcs = {
1131de32301bSRussell King 	.update_plane	= drm_primary_helper_update,
1132de32301bSRussell King 	.disable_plane	= drm_primary_helper_disable,
1133de32301bSRussell King 	.destroy	= drm_primary_helper_destroy,
1134de32301bSRussell King };
1135de32301bSRussell King 
11365740d27fSRussell King int armada_drm_plane_init(struct armada_plane *plane)
11375740d27fSRussell King {
11385740d27fSRussell King 	init_waitqueue_head(&plane->frame_wait);
11395740d27fSRussell King 
11405740d27fSRussell King 	return 0;
11415740d27fSRussell King }
11425740d27fSRussell King 
1143aaaf2f12SArvind Yadav static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
114496f60e37SRussell King 	{ CSC_AUTO,        "Auto" },
114596f60e37SRussell King 	{ CSC_YUV_CCIR601, "CCIR601" },
114696f60e37SRussell King 	{ CSC_YUV_CCIR709, "CCIR709" },
114796f60e37SRussell King };
114896f60e37SRussell King 
1149aaaf2f12SArvind Yadav static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
115096f60e37SRussell King 	{ CSC_AUTO,         "Auto" },
115196f60e37SRussell King 	{ CSC_RGB_COMPUTER, "Computer system" },
115296f60e37SRussell King 	{ CSC_RGB_STUDIO,   "Studio" },
115396f60e37SRussell King };
115496f60e37SRussell King 
115596f60e37SRussell King static int armada_drm_crtc_create_properties(struct drm_device *dev)
115696f60e37SRussell King {
115796f60e37SRussell King 	struct armada_private *priv = dev->dev_private;
115896f60e37SRussell King 
115996f60e37SRussell King 	if (priv->csc_yuv_prop)
116096f60e37SRussell King 		return 0;
116196f60e37SRussell King 
116296f60e37SRussell King 	priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
116396f60e37SRussell King 				"CSC_YUV", armada_drm_csc_yuv_enum_list,
116496f60e37SRussell King 				ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
116596f60e37SRussell King 	priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
116696f60e37SRussell King 				"CSC_RGB", armada_drm_csc_rgb_enum_list,
116796f60e37SRussell King 				ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
116896f60e37SRussell King 
116996f60e37SRussell King 	if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
117096f60e37SRussell King 		return -ENOMEM;
117196f60e37SRussell King 
117296f60e37SRussell King 	return 0;
117396f60e37SRussell King }
117496f60e37SRussell King 
11750fb2970bSRussell King static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
11769611cb93SRussell King 	struct resource *res, int irq, const struct armada_variant *variant,
11779611cb93SRussell King 	struct device_node *port)
117896f60e37SRussell King {
1179d8c96083SRussell King 	struct armada_private *priv = drm->dev_private;
118096f60e37SRussell King 	struct armada_crtc *dcrtc;
1181de32301bSRussell King 	struct armada_plane *primary;
118296f60e37SRussell King 	void __iomem *base;
118396f60e37SRussell King 	int ret;
118496f60e37SRussell King 
1185d8c96083SRussell King 	ret = armada_drm_crtc_create_properties(drm);
118696f60e37SRussell King 	if (ret)
118796f60e37SRussell King 		return ret;
118896f60e37SRussell King 
1189a7d7a143SLinus Torvalds 	base = devm_ioremap_resource(dev, res);
1190c9d53c0fSJingoo Han 	if (IS_ERR(base))
1191c9d53c0fSJingoo Han 		return PTR_ERR(base);
119296f60e37SRussell King 
119396f60e37SRussell King 	dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
119496f60e37SRussell King 	if (!dcrtc) {
119596f60e37SRussell King 		DRM_ERROR("failed to allocate Armada crtc\n");
119696f60e37SRussell King 		return -ENOMEM;
119796f60e37SRussell King 	}
119896f60e37SRussell King 
1199d8c96083SRussell King 	if (dev != drm->dev)
1200d8c96083SRussell King 		dev_set_drvdata(dev, dcrtc);
1201d8c96083SRussell King 
120242e62ba7SRussell King 	dcrtc->variant = variant;
120396f60e37SRussell King 	dcrtc->base = base;
1204d8c96083SRussell King 	dcrtc->num = drm->mode_config.num_crtc;
120596f60e37SRussell King 	dcrtc->clk = ERR_PTR(-EINVAL);
120696f60e37SRussell King 	dcrtc->csc_yuv_mode = CSC_AUTO;
120796f60e37SRussell King 	dcrtc->csc_rgb_mode = CSC_AUTO;
120896f60e37SRussell King 	dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
120996f60e37SRussell King 	dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
121096f60e37SRussell King 	spin_lock_init(&dcrtc->irq_lock);
121196f60e37SRussell King 	dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
121296f60e37SRussell King 
121396f60e37SRussell King 	/* Initialize some registers which we don't otherwise set */
121496f60e37SRussell King 	writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
121596f60e37SRussell King 	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
121696f60e37SRussell King 	writel_relaxed(dcrtc->spu_iopad_ctrl,
121796f60e37SRussell King 		       dcrtc->base + LCD_SPU_IOPAD_CONTROL);
121896f60e37SRussell King 	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
121996f60e37SRussell King 	writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
122096f60e37SRussell King 		       CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
122196f60e37SRussell King 		       CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
122296f60e37SRussell King 	writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1223e5d9ddfbSRussell King 	writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1224e5d9ddfbSRussell King 	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
122596f60e37SRussell King 
1226e5d9ddfbSRussell King 	ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1227e5d9ddfbSRussell King 			       dcrtc);
1228e5d9ddfbSRussell King 	if (ret < 0) {
1229e5d9ddfbSRussell King 		kfree(dcrtc);
1230e5d9ddfbSRussell King 		return ret;
1231e5d9ddfbSRussell King 	}
123296f60e37SRussell King 
123342e62ba7SRussell King 	if (dcrtc->variant->init) {
1234d8c96083SRussell King 		ret = dcrtc->variant->init(dcrtc, dev);
123596f60e37SRussell King 		if (ret) {
123696f60e37SRussell King 			kfree(dcrtc);
123796f60e37SRussell King 			return ret;
123896f60e37SRussell King 		}
123996f60e37SRussell King 	}
124096f60e37SRussell King 
124196f60e37SRussell King 	/* Ensure AXI pipeline is enabled */
124296f60e37SRussell King 	armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
124396f60e37SRussell King 
124496f60e37SRussell King 	priv->dcrtc[dcrtc->num] = dcrtc;
124596f60e37SRussell King 
12469611cb93SRussell King 	dcrtc->crtc.port = port;
12471c914cecSRussell King 
1248de32301bSRussell King 	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12491c914cecSRussell King 	if (!primary)
12501c914cecSRussell King 		return -ENOMEM;
12511c914cecSRussell King 
12525740d27fSRussell King 	ret = armada_drm_plane_init(primary);
12535740d27fSRussell King 	if (ret) {
12545740d27fSRussell King 		kfree(primary);
12555740d27fSRussell King 		return ret;
12565740d27fSRussell King 	}
12575740d27fSRussell King 
1258de32301bSRussell King 	ret = drm_universal_plane_init(drm, &primary->base, 0,
1259de32301bSRussell King 				       &armada_primary_plane_funcs,
1260de32301bSRussell King 				       armada_primary_formats,
1261de32301bSRussell King 				       ARRAY_SIZE(armada_primary_formats),
1262e6fc3b68SBen Widawsky 				       NULL,
1263b0b3b795SVille Syrjälä 				       DRM_PLANE_TYPE_PRIMARY, NULL);
1264de32301bSRussell King 	if (ret) {
1265de32301bSRussell King 		kfree(primary);
1266de32301bSRussell King 		return ret;
1267de32301bSRussell King 	}
1268de32301bSRussell King 
1269de32301bSRussell King 	ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
1270f9882876SVille Syrjälä 					&armada_crtc_funcs, NULL);
12711c914cecSRussell King 	if (ret)
12721c914cecSRussell King 		goto err_crtc_init;
12731c914cecSRussell King 
127496f60e37SRussell King 	drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
127596f60e37SRussell King 
127696f60e37SRussell King 	drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
127796f60e37SRussell King 				   dcrtc->csc_yuv_mode);
127896f60e37SRussell King 	drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
127996f60e37SRussell King 				   dcrtc->csc_rgb_mode);
128096f60e37SRussell King 
1281d8c96083SRussell King 	return armada_overlay_plane_create(drm, 1 << dcrtc->num);
12821c914cecSRussell King 
12831c914cecSRussell King err_crtc_init:
1284de32301bSRussell King 	primary->base.funcs->destroy(&primary->base);
12851c914cecSRussell King 	return ret;
128696f60e37SRussell King }
1287d8c96083SRussell King 
1288d8c96083SRussell King static int
1289d8c96083SRussell King armada_lcd_bind(struct device *dev, struct device *master, void *data)
1290d8c96083SRussell King {
1291d8c96083SRussell King 	struct platform_device *pdev = to_platform_device(dev);
1292d8c96083SRussell King 	struct drm_device *drm = data;
1293d8c96083SRussell King 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1294d8c96083SRussell King 	int irq = platform_get_irq(pdev, 0);
1295d8c96083SRussell King 	const struct armada_variant *variant;
12969611cb93SRussell King 	struct device_node *port = NULL;
1297d8c96083SRussell King 
1298d8c96083SRussell King 	if (irq < 0)
1299d8c96083SRussell King 		return irq;
1300d8c96083SRussell King 
1301d8c96083SRussell King 	if (!dev->of_node) {
1302d8c96083SRussell King 		const struct platform_device_id *id;
1303d8c96083SRussell King 
1304d8c96083SRussell King 		id = platform_get_device_id(pdev);
1305d8c96083SRussell King 		if (!id)
1306d8c96083SRussell King 			return -ENXIO;
1307d8c96083SRussell King 
1308d8c96083SRussell King 		variant = (const struct armada_variant *)id->driver_data;
1309d8c96083SRussell King 	} else {
1310d8c96083SRussell King 		const struct of_device_id *match;
13119611cb93SRussell King 		struct device_node *np, *parent = dev->of_node;
1312d8c96083SRussell King 
1313d8c96083SRussell King 		match = of_match_device(dev->driver->of_match_table, dev);
1314d8c96083SRussell King 		if (!match)
1315d8c96083SRussell King 			return -ENXIO;
1316d8c96083SRussell King 
13179611cb93SRussell King 		np = of_get_child_by_name(parent, "ports");
13189611cb93SRussell King 		if (np)
13199611cb93SRussell King 			parent = np;
13209611cb93SRussell King 		port = of_get_child_by_name(parent, "port");
13219611cb93SRussell King 		of_node_put(np);
13229611cb93SRussell King 		if (!port) {
13234bf99144SRob Herring 			dev_err(dev, "no port node found in %pOF\n", parent);
13249611cb93SRussell King 			return -ENXIO;
13259611cb93SRussell King 		}
13269611cb93SRussell King 
1327d8c96083SRussell King 		variant = match->data;
1328d8c96083SRussell King 	}
1329d8c96083SRussell King 
13309611cb93SRussell King 	return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
1331d8c96083SRussell King }
1332d8c96083SRussell King 
1333d8c96083SRussell King static void
1334d8c96083SRussell King armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1335d8c96083SRussell King {
1336d8c96083SRussell King 	struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1337d8c96083SRussell King 
1338d8c96083SRussell King 	armada_drm_crtc_destroy(&dcrtc->crtc);
1339d8c96083SRussell King }
1340d8c96083SRussell King 
1341d8c96083SRussell King static const struct component_ops armada_lcd_ops = {
1342d8c96083SRussell King 	.bind = armada_lcd_bind,
1343d8c96083SRussell King 	.unbind = armada_lcd_unbind,
1344d8c96083SRussell King };
1345d8c96083SRussell King 
1346d8c96083SRussell King static int armada_lcd_probe(struct platform_device *pdev)
1347d8c96083SRussell King {
1348d8c96083SRussell King 	return component_add(&pdev->dev, &armada_lcd_ops);
1349d8c96083SRussell King }
1350d8c96083SRussell King 
1351d8c96083SRussell King static int armada_lcd_remove(struct platform_device *pdev)
1352d8c96083SRussell King {
1353d8c96083SRussell King 	component_del(&pdev->dev, &armada_lcd_ops);
1354d8c96083SRussell King 	return 0;
1355d8c96083SRussell King }
1356d8c96083SRussell King 
135785909716SArvind Yadav static const struct of_device_id armada_lcd_of_match[] = {
1358d8c96083SRussell King 	{
1359d8c96083SRussell King 		.compatible	= "marvell,dove-lcd",
1360d8c96083SRussell King 		.data		= &armada510_ops,
1361d8c96083SRussell King 	},
1362d8c96083SRussell King 	{}
1363d8c96083SRussell King };
1364d8c96083SRussell King MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1365d8c96083SRussell King 
1366d8c96083SRussell King static const struct platform_device_id armada_lcd_platform_ids[] = {
1367d8c96083SRussell King 	{
1368d8c96083SRussell King 		.name		= "armada-lcd",
1369d8c96083SRussell King 		.driver_data	= (unsigned long)&armada510_ops,
1370d8c96083SRussell King 	}, {
1371d8c96083SRussell King 		.name		= "armada-510-lcd",
1372d8c96083SRussell King 		.driver_data	= (unsigned long)&armada510_ops,
1373d8c96083SRussell King 	},
1374d8c96083SRussell King 	{ },
1375d8c96083SRussell King };
1376d8c96083SRussell King MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1377d8c96083SRussell King 
1378d8c96083SRussell King struct platform_driver armada_lcd_platform_driver = {
1379d8c96083SRussell King 	.probe	= armada_lcd_probe,
1380d8c96083SRussell King 	.remove	= armada_lcd_remove,
1381d8c96083SRussell King 	.driver = {
1382d8c96083SRussell King 		.name	= "armada-lcd",
1383d8c96083SRussell King 		.owner	=  THIS_MODULE,
1384d8c96083SRussell King 		.of_match_table = armada_lcd_of_match,
1385d8c96083SRussell King 	},
1386d8c96083SRussell King 	.id_table = armada_lcd_platform_ids,
1387d8c96083SRussell King };
1388