xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision b317fa3ba11a10eee45c3e64d6396015ff87e8dc)
19026e0d1SMaxime Ripard /*
29026e0d1SMaxime Ripard  * Copyright (C) 2015 Free Electrons
39026e0d1SMaxime Ripard  * Copyright (C) 2015 NextThing Co
49026e0d1SMaxime Ripard  *
59026e0d1SMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
69026e0d1SMaxime Ripard  *
79026e0d1SMaxime Ripard  * This program is free software; you can redistribute it and/or
89026e0d1SMaxime Ripard  * modify it under the terms of the GNU General Public License as
99026e0d1SMaxime Ripard  * published by the Free Software Foundation; either version 2 of
109026e0d1SMaxime Ripard  * the License, or (at your option) any later version.
119026e0d1SMaxime Ripard  */
129026e0d1SMaxime Ripard 
139026e0d1SMaxime Ripard #include <drm/drmP.h>
149026e0d1SMaxime Ripard #include <drm/drm_atomic_helper.h>
159026e0d1SMaxime Ripard #include <drm/drm_crtc.h>
169026e0d1SMaxime Ripard #include <drm/drm_crtc_helper.h>
179026e0d1SMaxime Ripard #include <drm/drm_modes.h>
18ebc94461SRob Herring #include <drm/drm_of.h>
199026e0d1SMaxime Ripard 
209026e0d1SMaxime Ripard #include <linux/component.h>
219026e0d1SMaxime Ripard #include <linux/ioport.h>
229026e0d1SMaxime Ripard #include <linux/of_address.h>
2391ea2f29SChen-Yu Tsai #include <linux/of_device.h>
249026e0d1SMaxime Ripard #include <linux/of_irq.h>
259026e0d1SMaxime Ripard #include <linux/regmap.h>
269026e0d1SMaxime Ripard #include <linux/reset.h>
279026e0d1SMaxime Ripard 
2880a58240SChen-Yu Tsai #include "sun4i_backend.h"
299026e0d1SMaxime Ripard #include "sun4i_crtc.h"
309026e0d1SMaxime Ripard #include "sun4i_dotclock.h"
319026e0d1SMaxime Ripard #include "sun4i_drv.h"
3229e57fabSMaxime Ripard #include "sun4i_rgb.h"
339026e0d1SMaxime Ripard #include "sun4i_tcon.h"
349026e0d1SMaxime Ripard 
359026e0d1SMaxime Ripard void sun4i_tcon_disable(struct sun4i_tcon *tcon)
369026e0d1SMaxime Ripard {
379026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Disabling TCON\n");
389026e0d1SMaxime Ripard 
399026e0d1SMaxime Ripard 	/* Disable the TCON */
409026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
419026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE, 0);
429026e0d1SMaxime Ripard }
439026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_disable);
449026e0d1SMaxime Ripard 
459026e0d1SMaxime Ripard void sun4i_tcon_enable(struct sun4i_tcon *tcon)
469026e0d1SMaxime Ripard {
479026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Enabling TCON\n");
489026e0d1SMaxime Ripard 
499026e0d1SMaxime Ripard 	/* Enable the TCON */
509026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
519026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE,
529026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE);
539026e0d1SMaxime Ripard }
549026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable);
559026e0d1SMaxime Ripard 
569026e0d1SMaxime Ripard void sun4i_tcon_channel_disable(struct sun4i_tcon *tcon, int channel)
579026e0d1SMaxime Ripard {
589026e0d1SMaxime Ripard 	/* Disable the TCON's channel */
599026e0d1SMaxime Ripard 	if (channel == 0) {
609026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
619026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE, 0);
629026e0d1SMaxime Ripard 		clk_disable_unprepare(tcon->dclk);
638e924047SMaxime Ripard 		return;
648e924047SMaxime Ripard 	}
658e924047SMaxime Ripard 
6691ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
679026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
689026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_TCON_ENABLE, 0);
699026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->sclk1);
709026e0d1SMaxime Ripard }
719026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_channel_disable);
729026e0d1SMaxime Ripard 
739026e0d1SMaxime Ripard void sun4i_tcon_channel_enable(struct sun4i_tcon *tcon, int channel)
749026e0d1SMaxime Ripard {
759026e0d1SMaxime Ripard 	/* Enable the TCON's channel */
769026e0d1SMaxime Ripard 	if (channel == 0) {
779026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
789026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE,
799026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE);
809026e0d1SMaxime Ripard 		clk_prepare_enable(tcon->dclk);
818e924047SMaxime Ripard 		return;
828e924047SMaxime Ripard 	}
838e924047SMaxime Ripard 
8491ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
859026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
869026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_TCON_ENABLE,
879026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_TCON_ENABLE);
889026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->sclk1);
899026e0d1SMaxime Ripard }
909026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_channel_enable);
919026e0d1SMaxime Ripard 
929026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
939026e0d1SMaxime Ripard {
949026e0d1SMaxime Ripard 	u32 mask, val = 0;
959026e0d1SMaxime Ripard 
969026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
979026e0d1SMaxime Ripard 
989026e0d1SMaxime Ripard 	mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
999026e0d1SMaxime Ripard 	       SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
1009026e0d1SMaxime Ripard 
1019026e0d1SMaxime Ripard 	if (enable)
1029026e0d1SMaxime Ripard 		val = mask;
1039026e0d1SMaxime Ripard 
1049026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
1059026e0d1SMaxime Ripard }
1069026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
1079026e0d1SMaxime Ripard 
1089026e0d1SMaxime Ripard static int sun4i_tcon_get_clk_delay(struct drm_display_mode *mode,
1099026e0d1SMaxime Ripard 				    int channel)
1109026e0d1SMaxime Ripard {
1119026e0d1SMaxime Ripard 	int delay = mode->vtotal - mode->vdisplay;
1129026e0d1SMaxime Ripard 
1139026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1149026e0d1SMaxime Ripard 		delay /= 2;
1159026e0d1SMaxime Ripard 
1169026e0d1SMaxime Ripard 	if (channel == 1)
1179026e0d1SMaxime Ripard 		delay -= 2;
1189026e0d1SMaxime Ripard 
1199026e0d1SMaxime Ripard 	delay = min(delay, 30);
1209026e0d1SMaxime Ripard 
1219026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
1229026e0d1SMaxime Ripard 
1239026e0d1SMaxime Ripard 	return delay;
1249026e0d1SMaxime Ripard }
1259026e0d1SMaxime Ripard 
1269026e0d1SMaxime Ripard void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon,
1279026e0d1SMaxime Ripard 			  struct drm_display_mode *mode)
1289026e0d1SMaxime Ripard {
1299026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
1309026e0d1SMaxime Ripard 	u8 clk_delay;
1319026e0d1SMaxime Ripard 	u32 val = 0;
1329026e0d1SMaxime Ripard 
1339026e0d1SMaxime Ripard 	/* Adjust clock delay */
1349026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
1359026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
1369026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
1379026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
1389026e0d1SMaxime Ripard 
1399026e0d1SMaxime Ripard 	/* Set the resolution */
1409026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
1419026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
1429026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
1439026e0d1SMaxime Ripard 
1449026e0d1SMaxime Ripard 	/*
1459026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
14623a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
1479026e0d1SMaxime Ripard 	 */
1489026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
1499026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
1509026e0d1SMaxime Ripard 			 mode->crtc_htotal, bp);
1519026e0d1SMaxime Ripard 
1529026e0d1SMaxime Ripard 	/* Set horizontal display timings */
1539026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
1549026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
1559026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
1569026e0d1SMaxime Ripard 
1579026e0d1SMaxime Ripard 	/*
1589026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
15923a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
1609026e0d1SMaxime Ripard 	 */
1619026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
1629026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
1639026e0d1SMaxime Ripard 			 mode->crtc_vtotal, bp);
1649026e0d1SMaxime Ripard 
1659026e0d1SMaxime Ripard 	/* Set vertical display timings */
1669026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
1679026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal) |
1689026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
1699026e0d1SMaxime Ripard 
1709026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
1719026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
1729026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
1739026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
1749026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
1759026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
1769026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
1779026e0d1SMaxime Ripard 
1789026e0d1SMaxime Ripard 	/* Setup the polarity of the various signals */
1799026e0d1SMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
1809026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
1819026e0d1SMaxime Ripard 
1829026e0d1SMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
1839026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
1849026e0d1SMaxime Ripard 
1859026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
1869026e0d1SMaxime Ripard 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
1879026e0d1SMaxime Ripard 			   val);
1889026e0d1SMaxime Ripard 
1899026e0d1SMaxime Ripard 	/* Map output pins to channel 0 */
1909026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
1919026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
1929026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
1939026e0d1SMaxime Ripard 
1949026e0d1SMaxime Ripard 	/* Enable the output on the pins */
1959026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
1969026e0d1SMaxime Ripard }
1979026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon0_mode_set);
1989026e0d1SMaxime Ripard 
1999026e0d1SMaxime Ripard void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
2009026e0d1SMaxime Ripard 			  struct drm_display_mode *mode)
2019026e0d1SMaxime Ripard {
2029026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
2039026e0d1SMaxime Ripard 	u8 clk_delay;
2049026e0d1SMaxime Ripard 	u32 val;
2059026e0d1SMaxime Ripard 
20691ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
2078e924047SMaxime Ripard 
2089026e0d1SMaxime Ripard 	/* Adjust clock delay */
2099026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
2109026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
2119026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
2129026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
2139026e0d1SMaxime Ripard 
2149026e0d1SMaxime Ripard 	/* Set interlaced mode */
2159026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2169026e0d1SMaxime Ripard 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
2179026e0d1SMaxime Ripard 	else
2189026e0d1SMaxime Ripard 		val = 0;
2199026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
2209026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
2219026e0d1SMaxime Ripard 			   val);
2229026e0d1SMaxime Ripard 
2239026e0d1SMaxime Ripard 	/* Set the input resolution */
2249026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
2259026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
2269026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
2279026e0d1SMaxime Ripard 
2289026e0d1SMaxime Ripard 	/* Set the upscaling resolution */
2299026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
2309026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
2319026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
2329026e0d1SMaxime Ripard 
2339026e0d1SMaxime Ripard 	/* Set the output resolution */
2349026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
2359026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
2369026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
2379026e0d1SMaxime Ripard 
2389026e0d1SMaxime Ripard 	/* Set horizontal display timings */
2399026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_end;
2409026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
2419026e0d1SMaxime Ripard 			 mode->htotal, bp);
2429026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
2439026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
2449026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
2459026e0d1SMaxime Ripard 
2469026e0d1SMaxime Ripard 	/* Set vertical display timings */
2479026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_end;
2489026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
2499026e0d1SMaxime Ripard 			 mode->vtotal, bp);
2509026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
2519026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_TOTAL(mode->vtotal) |
2529026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
2539026e0d1SMaxime Ripard 
2549026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
2559026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
2569026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
2579026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
2589026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
2599026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
2609026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
2619026e0d1SMaxime Ripard 
2629026e0d1SMaxime Ripard 	/* Map output pins to channel 1 */
2639026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
2649026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
2659026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
2669026e0d1SMaxime Ripard 
2679026e0d1SMaxime Ripard 	/*
2689026e0d1SMaxime Ripard 	 * FIXME: Undocumented bits
2699026e0d1SMaxime Ripard 	 */
27091ea2f29SChen-Yu Tsai 	if (tcon->quirks->has_unknown_mux)
2719026e0d1SMaxime Ripard 		regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, 1);
2729026e0d1SMaxime Ripard }
2739026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon1_mode_set);
2749026e0d1SMaxime Ripard 
2759026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
2769026e0d1SMaxime Ripard 					struct sun4i_crtc *scrtc)
2779026e0d1SMaxime Ripard {
2789026e0d1SMaxime Ripard 	unsigned long flags;
2799026e0d1SMaxime Ripard 
2809026e0d1SMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
2819026e0d1SMaxime Ripard 	if (scrtc->event) {
2829026e0d1SMaxime Ripard 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
2839026e0d1SMaxime Ripard 		drm_crtc_vblank_put(&scrtc->crtc);
2849026e0d1SMaxime Ripard 		scrtc->event = NULL;
2859026e0d1SMaxime Ripard 	}
2869026e0d1SMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
2879026e0d1SMaxime Ripard }
2889026e0d1SMaxime Ripard 
2899026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private)
2909026e0d1SMaxime Ripard {
2919026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = private;
2929026e0d1SMaxime Ripard 	struct drm_device *drm = tcon->drm;
29346cce6daSChen-Yu Tsai 	struct sun4i_crtc *scrtc = tcon->crtc;
2949026e0d1SMaxime Ripard 	unsigned int status;
2959026e0d1SMaxime Ripard 
2969026e0d1SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
2979026e0d1SMaxime Ripard 
2989026e0d1SMaxime Ripard 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
2999026e0d1SMaxime Ripard 			SUN4I_TCON_GINT0_VBLANK_INT(1))))
3009026e0d1SMaxime Ripard 		return IRQ_NONE;
3019026e0d1SMaxime Ripard 
3029026e0d1SMaxime Ripard 	drm_crtc_handle_vblank(&scrtc->crtc);
3039026e0d1SMaxime Ripard 	sun4i_tcon_finish_page_flip(drm, scrtc);
3049026e0d1SMaxime Ripard 
3059026e0d1SMaxime Ripard 	/* Acknowledge the interrupt */
3069026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
3079026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
3089026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(1),
3099026e0d1SMaxime Ripard 			   0);
3109026e0d1SMaxime Ripard 
3119026e0d1SMaxime Ripard 	return IRQ_HANDLED;
3129026e0d1SMaxime Ripard }
3139026e0d1SMaxime Ripard 
3149026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev,
3159026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
3169026e0d1SMaxime Ripard {
3179026e0d1SMaxime Ripard 	tcon->clk = devm_clk_get(dev, "ahb");
3189026e0d1SMaxime Ripard 	if (IS_ERR(tcon->clk)) {
3199026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON bus clock\n");
3209026e0d1SMaxime Ripard 		return PTR_ERR(tcon->clk);
3219026e0d1SMaxime Ripard 	}
3229026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->clk);
3239026e0d1SMaxime Ripard 
3249026e0d1SMaxime Ripard 	tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
3259026e0d1SMaxime Ripard 	if (IS_ERR(tcon->sclk0)) {
3269026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
3279026e0d1SMaxime Ripard 		return PTR_ERR(tcon->sclk0);
3289026e0d1SMaxime Ripard 	}
3299026e0d1SMaxime Ripard 
33091ea2f29SChen-Yu Tsai 	if (tcon->quirks->has_channel_1) {
3319026e0d1SMaxime Ripard 		tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
3329026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk1)) {
3339026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
3349026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk1);
3359026e0d1SMaxime Ripard 		}
3368e924047SMaxime Ripard 	}
3379026e0d1SMaxime Ripard 
3384c7f16d1SChen-Yu Tsai 	return 0;
3399026e0d1SMaxime Ripard }
3409026e0d1SMaxime Ripard 
3419026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
3429026e0d1SMaxime Ripard {
3439026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->clk);
3449026e0d1SMaxime Ripard }
3459026e0d1SMaxime Ripard 
3469026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev,
3479026e0d1SMaxime Ripard 			       struct sun4i_tcon *tcon)
3489026e0d1SMaxime Ripard {
3499026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
3509026e0d1SMaxime Ripard 	int irq, ret;
3519026e0d1SMaxime Ripard 
3529026e0d1SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
3539026e0d1SMaxime Ripard 	if (irq < 0) {
3549026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
3559026e0d1SMaxime Ripard 		return irq;
3569026e0d1SMaxime Ripard 	}
3579026e0d1SMaxime Ripard 
3589026e0d1SMaxime Ripard 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
3599026e0d1SMaxime Ripard 			       dev_name(dev), tcon);
3609026e0d1SMaxime Ripard 	if (ret) {
3619026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't request the IRQ\n");
3629026e0d1SMaxime Ripard 		return ret;
3639026e0d1SMaxime Ripard 	}
3649026e0d1SMaxime Ripard 
3659026e0d1SMaxime Ripard 	return 0;
3669026e0d1SMaxime Ripard }
3679026e0d1SMaxime Ripard 
3689026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = {
3699026e0d1SMaxime Ripard 	.reg_bits	= 32,
3709026e0d1SMaxime Ripard 	.val_bits	= 32,
3719026e0d1SMaxime Ripard 	.reg_stride	= 4,
3729026e0d1SMaxime Ripard 	.max_register	= 0x800,
3739026e0d1SMaxime Ripard };
3749026e0d1SMaxime Ripard 
3759026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev,
3769026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
3779026e0d1SMaxime Ripard {
3789026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
3799026e0d1SMaxime Ripard 	struct resource *res;
3809026e0d1SMaxime Ripard 	void __iomem *regs;
3819026e0d1SMaxime Ripard 
3829026e0d1SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3839026e0d1SMaxime Ripard 	regs = devm_ioremap_resource(dev, res);
384af346f55SWei Yongjun 	if (IS_ERR(regs))
3859026e0d1SMaxime Ripard 		return PTR_ERR(regs);
3869026e0d1SMaxime Ripard 
3879026e0d1SMaxime Ripard 	tcon->regs = devm_regmap_init_mmio(dev, regs,
3889026e0d1SMaxime Ripard 					   &sun4i_tcon_regmap_config);
3899026e0d1SMaxime Ripard 	if (IS_ERR(tcon->regs)) {
3909026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't create the TCON regmap\n");
3919026e0d1SMaxime Ripard 		return PTR_ERR(tcon->regs);
3929026e0d1SMaxime Ripard 	}
3939026e0d1SMaxime Ripard 
3949026e0d1SMaxime Ripard 	/* Make sure the TCON is disabled and all IRQs are off */
3959026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
3969026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
3979026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
3989026e0d1SMaxime Ripard 
3999026e0d1SMaxime Ripard 	/* Disable IO lines and set them to tristate */
4009026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
4019026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
4029026e0d1SMaxime Ripard 
4039026e0d1SMaxime Ripard 	return 0;
4049026e0d1SMaxime Ripard }
4059026e0d1SMaxime Ripard 
406*b317fa3bSChen-Yu Tsai /*
407*b317fa3bSChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
408*b317fa3bSChen-Yu Tsai  * the TCON is always tied to just one backend. Hence we can traverse
409*b317fa3bSChen-Yu Tsai  * the of_graph upwards to find the backend our tcon is connected to,
410*b317fa3bSChen-Yu Tsai  * and take its ID as our own.
411*b317fa3bSChen-Yu Tsai  *
412*b317fa3bSChen-Yu Tsai  * We can either identify backends from their compatible strings, which
413*b317fa3bSChen-Yu Tsai  * means maintaining a large list of them. Or, since the backend is
414*b317fa3bSChen-Yu Tsai  * registered and binded before the TCON, we can just go through the
415*b317fa3bSChen-Yu Tsai  * list of registered backends and compare the device node.
416*b317fa3bSChen-Yu Tsai  */
417*b317fa3bSChen-Yu Tsai static struct sun4i_backend *sun4i_tcon_find_backend(struct sun4i_drv *drv,
418*b317fa3bSChen-Yu Tsai 						     struct device_node *node)
419*b317fa3bSChen-Yu Tsai {
420*b317fa3bSChen-Yu Tsai 	struct device_node *port, *ep, *remote;
421*b317fa3bSChen-Yu Tsai 	struct sun4i_backend *backend;
422*b317fa3bSChen-Yu Tsai 
423*b317fa3bSChen-Yu Tsai 	port = of_graph_get_port_by_id(node, 0);
424*b317fa3bSChen-Yu Tsai 	if (!port)
425*b317fa3bSChen-Yu Tsai 		return ERR_PTR(-EINVAL);
426*b317fa3bSChen-Yu Tsai 
427*b317fa3bSChen-Yu Tsai 	for_each_available_child_of_node(port, ep) {
428*b317fa3bSChen-Yu Tsai 		remote = of_graph_get_remote_port_parent(ep);
429*b317fa3bSChen-Yu Tsai 		if (!remote)
430*b317fa3bSChen-Yu Tsai 			continue;
431*b317fa3bSChen-Yu Tsai 
432*b317fa3bSChen-Yu Tsai 		/* does this node match any registered backends? */
433*b317fa3bSChen-Yu Tsai 		list_for_each_entry(backend, &drv->backend_list, list) {
434*b317fa3bSChen-Yu Tsai 			if (remote == backend->node) {
435*b317fa3bSChen-Yu Tsai 				of_node_put(remote);
436*b317fa3bSChen-Yu Tsai 				of_node_put(port);
437*b317fa3bSChen-Yu Tsai 				return backend;
438*b317fa3bSChen-Yu Tsai 			}
439*b317fa3bSChen-Yu Tsai 		}
440*b317fa3bSChen-Yu Tsai 
441*b317fa3bSChen-Yu Tsai 		/* keep looking through upstream ports */
442*b317fa3bSChen-Yu Tsai 		backend = sun4i_tcon_find_backend(drv, remote);
443*b317fa3bSChen-Yu Tsai 		if (!IS_ERR(backend)) {
444*b317fa3bSChen-Yu Tsai 			of_node_put(remote);
445*b317fa3bSChen-Yu Tsai 			of_node_put(port);
446*b317fa3bSChen-Yu Tsai 			return backend;
447*b317fa3bSChen-Yu Tsai 		}
448*b317fa3bSChen-Yu Tsai 	}
449*b317fa3bSChen-Yu Tsai 
450*b317fa3bSChen-Yu Tsai 	return ERR_PTR(-EINVAL);
451*b317fa3bSChen-Yu Tsai }
452*b317fa3bSChen-Yu Tsai 
4539026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master,
4549026e0d1SMaxime Ripard 			   void *data)
4559026e0d1SMaxime Ripard {
4569026e0d1SMaxime Ripard 	struct drm_device *drm = data;
4579026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
45880a58240SChen-Yu Tsai 	struct sun4i_backend *backend;
4599026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon;
4609026e0d1SMaxime Ripard 	int ret;
4619026e0d1SMaxime Ripard 
462*b317fa3bSChen-Yu Tsai 	backend = sun4i_tcon_find_backend(drv, dev->of_node);
463*b317fa3bSChen-Yu Tsai 	if (IS_ERR(backend)) {
464*b317fa3bSChen-Yu Tsai 		dev_err(dev, "Couldn't find matching backend\n");
46580a58240SChen-Yu Tsai 		return -EPROBE_DEFER;
466*b317fa3bSChen-Yu Tsai 	}
46780a58240SChen-Yu Tsai 
4689026e0d1SMaxime Ripard 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
4699026e0d1SMaxime Ripard 	if (!tcon)
4709026e0d1SMaxime Ripard 		return -ENOMEM;
4719026e0d1SMaxime Ripard 	dev_set_drvdata(dev, tcon);
4729026e0d1SMaxime Ripard 	tcon->drm = drm;
473ae558110SMaxime Ripard 	tcon->dev = dev;
47491ea2f29SChen-Yu Tsai 	tcon->quirks = of_device_get_match_data(dev);
4759026e0d1SMaxime Ripard 
4769026e0d1SMaxime Ripard 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
4779026e0d1SMaxime Ripard 	if (IS_ERR(tcon->lcd_rst)) {
4789026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
4799026e0d1SMaxime Ripard 		return PTR_ERR(tcon->lcd_rst);
4809026e0d1SMaxime Ripard 	}
4819026e0d1SMaxime Ripard 
4829026e0d1SMaxime Ripard 	/* Make sure our TCON is reset */
4839026e0d1SMaxime Ripard 	if (!reset_control_status(tcon->lcd_rst))
4849026e0d1SMaxime Ripard 		reset_control_assert(tcon->lcd_rst);
4859026e0d1SMaxime Ripard 
4869026e0d1SMaxime Ripard 	ret = reset_control_deassert(tcon->lcd_rst);
4879026e0d1SMaxime Ripard 	if (ret) {
4889026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't deassert our reset line\n");
4899026e0d1SMaxime Ripard 		return ret;
4909026e0d1SMaxime Ripard 	}
4919026e0d1SMaxime Ripard 
4929026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_clocks(dev, tcon);
4939026e0d1SMaxime Ripard 	if (ret) {
4949026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON clocks\n");
4959026e0d1SMaxime Ripard 		goto err_assert_reset;
4969026e0d1SMaxime Ripard 	}
4979026e0d1SMaxime Ripard 
4984c7f16d1SChen-Yu Tsai 	ret = sun4i_tcon_init_regmap(dev, tcon);
4999026e0d1SMaxime Ripard 	if (ret) {
5004c7f16d1SChen-Yu Tsai 		dev_err(dev, "Couldn't init our TCON regmap\n");
5019026e0d1SMaxime Ripard 		goto err_free_clocks;
5029026e0d1SMaxime Ripard 	}
5039026e0d1SMaxime Ripard 
5044c7f16d1SChen-Yu Tsai 	ret = sun4i_dclk_create(dev, tcon);
5054c7f16d1SChen-Yu Tsai 	if (ret) {
5064c7f16d1SChen-Yu Tsai 		dev_err(dev, "Couldn't create our TCON dot clock\n");
5074c7f16d1SChen-Yu Tsai 		goto err_free_clocks;
5084c7f16d1SChen-Yu Tsai 	}
5094c7f16d1SChen-Yu Tsai 
5109026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_irq(dev, tcon);
5119026e0d1SMaxime Ripard 	if (ret) {
5129026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON interrupts\n");
5134c7f16d1SChen-Yu Tsai 		goto err_free_dotclock;
5149026e0d1SMaxime Ripard 	}
5159026e0d1SMaxime Ripard 
51680a58240SChen-Yu Tsai 	tcon->crtc = sun4i_crtc_init(drm, backend, tcon);
51746cce6daSChen-Yu Tsai 	if (IS_ERR(tcon->crtc)) {
51846cce6daSChen-Yu Tsai 		dev_err(dev, "Couldn't create our CRTC\n");
51946cce6daSChen-Yu Tsai 		ret = PTR_ERR(tcon->crtc);
52046cce6daSChen-Yu Tsai 		goto err_free_clocks;
52146cce6daSChen-Yu Tsai 	}
52246cce6daSChen-Yu Tsai 
523b9c8506cSChen-Yu Tsai 	ret = sun4i_rgb_init(drm, tcon);
52413fef095SChen-Yu Tsai 	if (ret < 0)
52513fef095SChen-Yu Tsai 		goto err_free_clocks;
52613fef095SChen-Yu Tsai 
52780a58240SChen-Yu Tsai 	list_add_tail(&tcon->list, &drv->tcon_list);
52880a58240SChen-Yu Tsai 
52913fef095SChen-Yu Tsai 	return 0;
5309026e0d1SMaxime Ripard 
5314c7f16d1SChen-Yu Tsai err_free_dotclock:
5324c7f16d1SChen-Yu Tsai 	sun4i_dclk_free(tcon);
5339026e0d1SMaxime Ripard err_free_clocks:
5349026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
5359026e0d1SMaxime Ripard err_assert_reset:
5369026e0d1SMaxime Ripard 	reset_control_assert(tcon->lcd_rst);
5379026e0d1SMaxime Ripard 	return ret;
5389026e0d1SMaxime Ripard }
5399026e0d1SMaxime Ripard 
5409026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master,
5419026e0d1SMaxime Ripard 			      void *data)
5429026e0d1SMaxime Ripard {
5439026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
5449026e0d1SMaxime Ripard 
54580a58240SChen-Yu Tsai 	list_del(&tcon->list);
5464c7f16d1SChen-Yu Tsai 	sun4i_dclk_free(tcon);
5479026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
5489026e0d1SMaxime Ripard }
5499026e0d1SMaxime Ripard 
550dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = {
5519026e0d1SMaxime Ripard 	.bind	= sun4i_tcon_bind,
5529026e0d1SMaxime Ripard 	.unbind	= sun4i_tcon_unbind,
5539026e0d1SMaxime Ripard };
5549026e0d1SMaxime Ripard 
5559026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev)
5569026e0d1SMaxime Ripard {
55729e57fabSMaxime Ripard 	struct device_node *node = pdev->dev.of_node;
558894f5a9fSMaxime Ripard 	struct drm_bridge *bridge;
55929e57fabSMaxime Ripard 	struct drm_panel *panel;
560ebc94461SRob Herring 	int ret;
56129e57fabSMaxime Ripard 
562ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
563ebc94461SRob Herring 	if (ret == -EPROBE_DEFER)
564ebc94461SRob Herring 		return ret;
56529e57fabSMaxime Ripard 
5669026e0d1SMaxime Ripard 	return component_add(&pdev->dev, &sun4i_tcon_ops);
5679026e0d1SMaxime Ripard }
5689026e0d1SMaxime Ripard 
5699026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev)
5709026e0d1SMaxime Ripard {
5719026e0d1SMaxime Ripard 	component_del(&pdev->dev, &sun4i_tcon_ops);
5729026e0d1SMaxime Ripard 
5739026e0d1SMaxime Ripard 	return 0;
5749026e0d1SMaxime Ripard }
5759026e0d1SMaxime Ripard 
57691ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
57791ea2f29SChen-Yu Tsai 	.has_unknown_mux = true,
57891ea2f29SChen-Yu Tsai 	.has_channel_1	= true,
57991ea2f29SChen-Yu Tsai };
58091ea2f29SChen-Yu Tsai 
58193a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
58293a5ec14SChen-Yu Tsai 	.has_channel_1	= true,
58393a5ec14SChen-Yu Tsai };
58493a5ec14SChen-Yu Tsai 
58593a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
58693a5ec14SChen-Yu Tsai 	.has_channel_1	= true,
58793a5ec14SChen-Yu Tsai };
58893a5ec14SChen-Yu Tsai 
58991ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
59091ea2f29SChen-Yu Tsai 	/* nothing is supported */
59191ea2f29SChen-Yu Tsai };
59291ea2f29SChen-Yu Tsai 
5939026e0d1SMaxime Ripard static const struct of_device_id sun4i_tcon_of_table[] = {
59491ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
59593a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
59693a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
59791ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
5989026e0d1SMaxime Ripard 	{ }
5999026e0d1SMaxime Ripard };
6009026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
6019026e0d1SMaxime Ripard 
6029026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = {
6039026e0d1SMaxime Ripard 	.probe		= sun4i_tcon_probe,
6049026e0d1SMaxime Ripard 	.remove		= sun4i_tcon_remove,
6059026e0d1SMaxime Ripard 	.driver		= {
6069026e0d1SMaxime Ripard 		.name		= "sun4i-tcon",
6079026e0d1SMaxime Ripard 		.of_match_table	= sun4i_tcon_of_table,
6089026e0d1SMaxime Ripard 	},
6099026e0d1SMaxime Ripard };
6109026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver);
6119026e0d1SMaxime Ripard 
6129026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
6139026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
6149026e0d1SMaxime Ripard MODULE_LICENSE("GPL");
615