xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision af346f5570f208dcfb319f1214fcf8ca310c6fdd)
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>
1829e57fabSMaxime Ripard #include <drm/drm_panel.h>
199026e0d1SMaxime Ripard 
209026e0d1SMaxime Ripard #include <linux/component.h>
219026e0d1SMaxime Ripard #include <linux/ioport.h>
229026e0d1SMaxime Ripard #include <linux/of_address.h>
2329e57fabSMaxime Ripard #include <linux/of_graph.h>
249026e0d1SMaxime Ripard #include <linux/of_irq.h>
259026e0d1SMaxime Ripard #include <linux/regmap.h>
269026e0d1SMaxime Ripard #include <linux/reset.h>
279026e0d1SMaxime Ripard 
289026e0d1SMaxime Ripard #include "sun4i_crtc.h"
299026e0d1SMaxime Ripard #include "sun4i_dotclock.h"
309026e0d1SMaxime Ripard #include "sun4i_drv.h"
3129e57fabSMaxime Ripard #include "sun4i_rgb.h"
329026e0d1SMaxime Ripard #include "sun4i_tcon.h"
339026e0d1SMaxime Ripard 
349026e0d1SMaxime Ripard void sun4i_tcon_disable(struct sun4i_tcon *tcon)
359026e0d1SMaxime Ripard {
369026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Disabling TCON\n");
379026e0d1SMaxime Ripard 
389026e0d1SMaxime Ripard 	/* Disable the TCON */
399026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
409026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE, 0);
419026e0d1SMaxime Ripard }
429026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_disable);
439026e0d1SMaxime Ripard 
449026e0d1SMaxime Ripard void sun4i_tcon_enable(struct sun4i_tcon *tcon)
459026e0d1SMaxime Ripard {
469026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Enabling TCON\n");
479026e0d1SMaxime Ripard 
489026e0d1SMaxime Ripard 	/* Enable the TCON */
499026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
509026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE,
519026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE);
529026e0d1SMaxime Ripard }
539026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable);
549026e0d1SMaxime Ripard 
559026e0d1SMaxime Ripard void sun4i_tcon_channel_disable(struct sun4i_tcon *tcon, int channel)
569026e0d1SMaxime Ripard {
579026e0d1SMaxime Ripard 	/* Disable the TCON's channel */
589026e0d1SMaxime Ripard 	if (channel == 0) {
599026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
609026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE, 0);
619026e0d1SMaxime Ripard 		clk_disable_unprepare(tcon->dclk);
629026e0d1SMaxime Ripard 	} else if (channel == 1) {
639026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
649026e0d1SMaxime Ripard 				   SUN4I_TCON1_CTL_TCON_ENABLE, 0);
659026e0d1SMaxime Ripard 		clk_disable_unprepare(tcon->sclk1);
669026e0d1SMaxime Ripard 	}
679026e0d1SMaxime Ripard }
689026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_channel_disable);
699026e0d1SMaxime Ripard 
709026e0d1SMaxime Ripard void sun4i_tcon_channel_enable(struct sun4i_tcon *tcon, int channel)
719026e0d1SMaxime Ripard {
729026e0d1SMaxime Ripard 	/* Enable the TCON's channel */
739026e0d1SMaxime Ripard 	if (channel == 0) {
749026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
759026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE,
769026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE);
779026e0d1SMaxime Ripard 		clk_prepare_enable(tcon->dclk);
789026e0d1SMaxime Ripard 	} else if (channel == 1) {
799026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
809026e0d1SMaxime Ripard 				   SUN4I_TCON1_CTL_TCON_ENABLE,
819026e0d1SMaxime Ripard 				   SUN4I_TCON1_CTL_TCON_ENABLE);
829026e0d1SMaxime Ripard 		clk_prepare_enable(tcon->sclk1);
839026e0d1SMaxime Ripard 	}
849026e0d1SMaxime Ripard }
859026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_channel_enable);
869026e0d1SMaxime Ripard 
879026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
889026e0d1SMaxime Ripard {
899026e0d1SMaxime Ripard 	u32 mask, val = 0;
909026e0d1SMaxime Ripard 
919026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
929026e0d1SMaxime Ripard 
939026e0d1SMaxime Ripard 	mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
949026e0d1SMaxime Ripard 	       SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
959026e0d1SMaxime Ripard 
969026e0d1SMaxime Ripard 	if (enable)
979026e0d1SMaxime Ripard 		val = mask;
989026e0d1SMaxime Ripard 
999026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
1009026e0d1SMaxime Ripard }
1019026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
1029026e0d1SMaxime Ripard 
1039026e0d1SMaxime Ripard static int sun4i_tcon_get_clk_delay(struct drm_display_mode *mode,
1049026e0d1SMaxime Ripard 				    int channel)
1059026e0d1SMaxime Ripard {
1069026e0d1SMaxime Ripard 	int delay = mode->vtotal - mode->vdisplay;
1079026e0d1SMaxime Ripard 
1089026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1099026e0d1SMaxime Ripard 		delay /= 2;
1109026e0d1SMaxime Ripard 
1119026e0d1SMaxime Ripard 	if (channel == 1)
1129026e0d1SMaxime Ripard 		delay -= 2;
1139026e0d1SMaxime Ripard 
1149026e0d1SMaxime Ripard 	delay = min(delay, 30);
1159026e0d1SMaxime Ripard 
1169026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
1179026e0d1SMaxime Ripard 
1189026e0d1SMaxime Ripard 	return delay;
1199026e0d1SMaxime Ripard }
1209026e0d1SMaxime Ripard 
1219026e0d1SMaxime Ripard void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon,
1229026e0d1SMaxime Ripard 			  struct drm_display_mode *mode)
1239026e0d1SMaxime Ripard {
1249026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
1259026e0d1SMaxime Ripard 	u8 clk_delay;
1269026e0d1SMaxime Ripard 	u32 val = 0;
1279026e0d1SMaxime Ripard 
1289026e0d1SMaxime Ripard 	/* Adjust clock delay */
1299026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
1309026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
1319026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
1329026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
1339026e0d1SMaxime Ripard 
1349026e0d1SMaxime Ripard 	/* Set the resolution */
1359026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
1369026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
1379026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
1389026e0d1SMaxime Ripard 
1399026e0d1SMaxime Ripard 	/*
1409026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
1419026e0d1SMaxime Ripard 	 * but it really is the front porch + hsync
1429026e0d1SMaxime Ripard 	 */
1439026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
1449026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
1459026e0d1SMaxime Ripard 			 mode->crtc_htotal, bp);
1469026e0d1SMaxime Ripard 
1479026e0d1SMaxime Ripard 	/* Set horizontal display timings */
1489026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
1499026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
1509026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
1519026e0d1SMaxime Ripard 
1529026e0d1SMaxime Ripard 	/*
1539026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
1549026e0d1SMaxime Ripard 	 * but it really is the front porch + hsync
1559026e0d1SMaxime Ripard 	 */
1569026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
1579026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
1589026e0d1SMaxime Ripard 			 mode->crtc_vtotal, bp);
1599026e0d1SMaxime Ripard 
1609026e0d1SMaxime Ripard 	/* Set vertical display timings */
1619026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
1629026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal) |
1639026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
1649026e0d1SMaxime Ripard 
1659026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
1669026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
1679026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
1689026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
1699026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
1709026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
1719026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
1729026e0d1SMaxime Ripard 
1739026e0d1SMaxime Ripard 	/* Setup the polarity of the various signals */
1749026e0d1SMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
1759026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
1769026e0d1SMaxime Ripard 
1779026e0d1SMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
1789026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
1799026e0d1SMaxime Ripard 
1809026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
1819026e0d1SMaxime Ripard 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
1829026e0d1SMaxime Ripard 			   val);
1839026e0d1SMaxime Ripard 
1849026e0d1SMaxime Ripard 	/* Map output pins to channel 0 */
1859026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
1869026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
1879026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
1889026e0d1SMaxime Ripard 
1899026e0d1SMaxime Ripard 	/* Enable the output on the pins */
1909026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
1919026e0d1SMaxime Ripard }
1929026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon0_mode_set);
1939026e0d1SMaxime Ripard 
1949026e0d1SMaxime Ripard void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
1959026e0d1SMaxime Ripard 			  struct drm_display_mode *mode)
1969026e0d1SMaxime Ripard {
1979026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
1989026e0d1SMaxime Ripard 	u8 clk_delay;
1999026e0d1SMaxime Ripard 	u32 val;
2009026e0d1SMaxime Ripard 
2019026e0d1SMaxime Ripard 	/* Adjust clock delay */
2029026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
2039026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
2049026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
2059026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
2069026e0d1SMaxime Ripard 
2079026e0d1SMaxime Ripard 	/* Set interlaced mode */
2089026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2099026e0d1SMaxime Ripard 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
2109026e0d1SMaxime Ripard 	else
2119026e0d1SMaxime Ripard 		val = 0;
2129026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
2139026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
2149026e0d1SMaxime Ripard 			   val);
2159026e0d1SMaxime Ripard 
2169026e0d1SMaxime Ripard 	/* Set the input resolution */
2179026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
2189026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
2199026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
2209026e0d1SMaxime Ripard 
2219026e0d1SMaxime Ripard 	/* Set the upscaling resolution */
2229026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
2239026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
2249026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
2259026e0d1SMaxime Ripard 
2269026e0d1SMaxime Ripard 	/* Set the output resolution */
2279026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
2289026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
2299026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
2309026e0d1SMaxime Ripard 
2319026e0d1SMaxime Ripard 	/* Set horizontal display timings */
2329026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_end;
2339026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
2349026e0d1SMaxime Ripard 			 mode->htotal, bp);
2359026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
2369026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
2379026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
2389026e0d1SMaxime Ripard 
2399026e0d1SMaxime Ripard 	/* Set vertical display timings */
2409026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_end;
2419026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
2429026e0d1SMaxime Ripard 			 mode->vtotal, bp);
2439026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
2449026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_TOTAL(mode->vtotal) |
2459026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
2469026e0d1SMaxime Ripard 
2479026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
2489026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
2499026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
2509026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
2519026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
2529026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
2539026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
2549026e0d1SMaxime Ripard 
2559026e0d1SMaxime Ripard 	/* Map output pins to channel 1 */
2569026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
2579026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
2589026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
2599026e0d1SMaxime Ripard 
2609026e0d1SMaxime Ripard 	/*
2619026e0d1SMaxime Ripard 	 * FIXME: Undocumented bits
2629026e0d1SMaxime Ripard 	 */
2639026e0d1SMaxime Ripard 	if (tcon->has_mux)
2649026e0d1SMaxime Ripard 		regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, 1);
2659026e0d1SMaxime Ripard }
2669026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon1_mode_set);
2679026e0d1SMaxime Ripard 
2689026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
2699026e0d1SMaxime Ripard 					struct sun4i_crtc *scrtc)
2709026e0d1SMaxime Ripard {
2719026e0d1SMaxime Ripard 	unsigned long flags;
2729026e0d1SMaxime Ripard 
2739026e0d1SMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
2749026e0d1SMaxime Ripard 	if (scrtc->event) {
2759026e0d1SMaxime Ripard 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
2769026e0d1SMaxime Ripard 		drm_crtc_vblank_put(&scrtc->crtc);
2779026e0d1SMaxime Ripard 		scrtc->event = NULL;
2789026e0d1SMaxime Ripard 	}
2799026e0d1SMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
2809026e0d1SMaxime Ripard }
2819026e0d1SMaxime Ripard 
2829026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private)
2839026e0d1SMaxime Ripard {
2849026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = private;
2859026e0d1SMaxime Ripard 	struct drm_device *drm = tcon->drm;
2869026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
2879026e0d1SMaxime Ripard 	struct sun4i_crtc *scrtc = drv->crtc;
2889026e0d1SMaxime Ripard 	unsigned int status;
2899026e0d1SMaxime Ripard 
2909026e0d1SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
2919026e0d1SMaxime Ripard 
2929026e0d1SMaxime Ripard 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
2939026e0d1SMaxime Ripard 			SUN4I_TCON_GINT0_VBLANK_INT(1))))
2949026e0d1SMaxime Ripard 		return IRQ_NONE;
2959026e0d1SMaxime Ripard 
2969026e0d1SMaxime Ripard 	drm_crtc_handle_vblank(&scrtc->crtc);
2979026e0d1SMaxime Ripard 	sun4i_tcon_finish_page_flip(drm, scrtc);
2989026e0d1SMaxime Ripard 
2999026e0d1SMaxime Ripard 	/* Acknowledge the interrupt */
3009026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
3019026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
3029026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(1),
3039026e0d1SMaxime Ripard 			   0);
3049026e0d1SMaxime Ripard 
3059026e0d1SMaxime Ripard 	return IRQ_HANDLED;
3069026e0d1SMaxime Ripard }
3079026e0d1SMaxime Ripard 
3089026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev,
3099026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
3109026e0d1SMaxime Ripard {
3119026e0d1SMaxime Ripard 	tcon->clk = devm_clk_get(dev, "ahb");
3129026e0d1SMaxime Ripard 	if (IS_ERR(tcon->clk)) {
3139026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON bus clock\n");
3149026e0d1SMaxime Ripard 		return PTR_ERR(tcon->clk);
3159026e0d1SMaxime Ripard 	}
3169026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->clk);
3179026e0d1SMaxime Ripard 
3189026e0d1SMaxime Ripard 	tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
3199026e0d1SMaxime Ripard 	if (IS_ERR(tcon->sclk0)) {
3209026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
3219026e0d1SMaxime Ripard 		return PTR_ERR(tcon->sclk0);
3229026e0d1SMaxime Ripard 	}
3239026e0d1SMaxime Ripard 
3249026e0d1SMaxime Ripard 	tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
3259026e0d1SMaxime Ripard 	if (IS_ERR(tcon->sclk1)) {
3269026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
3279026e0d1SMaxime Ripard 		return PTR_ERR(tcon->sclk1);
3289026e0d1SMaxime Ripard 	}
3299026e0d1SMaxime Ripard 
3309026e0d1SMaxime Ripard 	return sun4i_dclk_create(dev, tcon);
3319026e0d1SMaxime Ripard }
3329026e0d1SMaxime Ripard 
3339026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
3349026e0d1SMaxime Ripard {
3359026e0d1SMaxime Ripard 	sun4i_dclk_free(tcon);
3369026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->clk);
3379026e0d1SMaxime Ripard }
3389026e0d1SMaxime Ripard 
3399026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev,
3409026e0d1SMaxime Ripard 			       struct sun4i_tcon *tcon)
3419026e0d1SMaxime Ripard {
3429026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
3439026e0d1SMaxime Ripard 	int irq, ret;
3449026e0d1SMaxime Ripard 
3459026e0d1SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
3469026e0d1SMaxime Ripard 	if (irq < 0) {
3479026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
3489026e0d1SMaxime Ripard 		return irq;
3499026e0d1SMaxime Ripard 	}
3509026e0d1SMaxime Ripard 
3519026e0d1SMaxime Ripard 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
3529026e0d1SMaxime Ripard 			       dev_name(dev), tcon);
3539026e0d1SMaxime Ripard 	if (ret) {
3549026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't request the IRQ\n");
3559026e0d1SMaxime Ripard 		return ret;
3569026e0d1SMaxime Ripard 	}
3579026e0d1SMaxime Ripard 
3589026e0d1SMaxime Ripard 	return 0;
3599026e0d1SMaxime Ripard }
3609026e0d1SMaxime Ripard 
3619026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = {
3629026e0d1SMaxime Ripard 	.reg_bits	= 32,
3639026e0d1SMaxime Ripard 	.val_bits	= 32,
3649026e0d1SMaxime Ripard 	.reg_stride	= 4,
3659026e0d1SMaxime Ripard 	.max_register	= 0x800,
3669026e0d1SMaxime Ripard };
3679026e0d1SMaxime Ripard 
3689026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev,
3699026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
3709026e0d1SMaxime Ripard {
3719026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
3729026e0d1SMaxime Ripard 	struct resource *res;
3739026e0d1SMaxime Ripard 	void __iomem *regs;
3749026e0d1SMaxime Ripard 
3759026e0d1SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3769026e0d1SMaxime Ripard 	regs = devm_ioremap_resource(dev, res);
377*af346f55SWei Yongjun 	if (IS_ERR(regs))
3789026e0d1SMaxime Ripard 		return PTR_ERR(regs);
3799026e0d1SMaxime Ripard 
3809026e0d1SMaxime Ripard 	tcon->regs = devm_regmap_init_mmio(dev, regs,
3819026e0d1SMaxime Ripard 					   &sun4i_tcon_regmap_config);
3829026e0d1SMaxime Ripard 	if (IS_ERR(tcon->regs)) {
3839026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't create the TCON regmap\n");
3849026e0d1SMaxime Ripard 		return PTR_ERR(tcon->regs);
3859026e0d1SMaxime Ripard 	}
3869026e0d1SMaxime Ripard 
3879026e0d1SMaxime Ripard 	/* Make sure the TCON is disabled and all IRQs are off */
3889026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
3899026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
3909026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
3919026e0d1SMaxime Ripard 
3929026e0d1SMaxime Ripard 	/* Disable IO lines and set them to tristate */
3939026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
3949026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
3959026e0d1SMaxime Ripard 
3969026e0d1SMaxime Ripard 	return 0;
3979026e0d1SMaxime Ripard }
3989026e0d1SMaxime Ripard 
399a8444c7eSMaxime Ripard struct drm_panel *sun4i_tcon_find_panel(struct device_node *node)
40029e57fabSMaxime Ripard {
40129e57fabSMaxime Ripard 	struct device_node *port, *remote, *child;
40229e57fabSMaxime Ripard 	struct device_node *end_node = NULL;
40329e57fabSMaxime Ripard 
40429e57fabSMaxime Ripard 	/* Inputs are listed first, then outputs */
40529e57fabSMaxime Ripard 	port = of_graph_get_port_by_id(node, 1);
40629e57fabSMaxime Ripard 
40729e57fabSMaxime Ripard 	/*
40829e57fabSMaxime Ripard 	 * Our first output is the RGB interface where the panel will
40929e57fabSMaxime Ripard 	 * be connected.
41029e57fabSMaxime Ripard 	 */
41129e57fabSMaxime Ripard 	for_each_child_of_node(port, child) {
41229e57fabSMaxime Ripard 		u32 reg;
41329e57fabSMaxime Ripard 
41429e57fabSMaxime Ripard 		of_property_read_u32(child, "reg", &reg);
41529e57fabSMaxime Ripard 		if (reg == 0)
41629e57fabSMaxime Ripard 			end_node = child;
41729e57fabSMaxime Ripard 	}
41829e57fabSMaxime Ripard 
41929e57fabSMaxime Ripard 	if (!end_node) {
42029e57fabSMaxime Ripard 		DRM_DEBUG_DRIVER("Missing panel endpoint\n");
42129e57fabSMaxime Ripard 		return ERR_PTR(-ENODEV);
42229e57fabSMaxime Ripard 	}
42329e57fabSMaxime Ripard 
42429e57fabSMaxime Ripard 	remote = of_graph_get_remote_port_parent(end_node);
42529e57fabSMaxime Ripard 	if (!remote) {
4260bbbb00bSMaxime Ripard 		DRM_DEBUG_DRIVER("Unable to parse remote node\n");
42729e57fabSMaxime Ripard 		return ERR_PTR(-EINVAL);
42829e57fabSMaxime Ripard 	}
42929e57fabSMaxime Ripard 
4300bbbb00bSMaxime Ripard 	return of_drm_find_panel(remote) ?: ERR_PTR(-EPROBE_DEFER);
43129e57fabSMaxime Ripard }
43229e57fabSMaxime Ripard 
433894f5a9fSMaxime Ripard struct drm_bridge *sun4i_tcon_find_bridge(struct device_node *node)
434894f5a9fSMaxime Ripard {
435894f5a9fSMaxime Ripard 	struct device_node *port, *remote, *child;
436894f5a9fSMaxime Ripard 	struct device_node *end_node = NULL;
437894f5a9fSMaxime Ripard 
438894f5a9fSMaxime Ripard 	/* Inputs are listed first, then outputs */
439894f5a9fSMaxime Ripard 	port = of_graph_get_port_by_id(node, 1);
440894f5a9fSMaxime Ripard 
441894f5a9fSMaxime Ripard 	/*
442894f5a9fSMaxime Ripard 	 * Our first output is the RGB interface where the panel will
443894f5a9fSMaxime Ripard 	 * be connected.
444894f5a9fSMaxime Ripard 	 */
445894f5a9fSMaxime Ripard 	for_each_child_of_node(port, child) {
446894f5a9fSMaxime Ripard 		u32 reg;
447894f5a9fSMaxime Ripard 
448894f5a9fSMaxime Ripard 		of_property_read_u32(child, "reg", &reg);
449894f5a9fSMaxime Ripard 		if (reg == 0)
450894f5a9fSMaxime Ripard 			end_node = child;
451894f5a9fSMaxime Ripard 	}
452894f5a9fSMaxime Ripard 
453894f5a9fSMaxime Ripard 	if (!end_node) {
454894f5a9fSMaxime Ripard 		DRM_DEBUG_DRIVER("Missing bridge endpoint\n");
455894f5a9fSMaxime Ripard 		return ERR_PTR(-ENODEV);
456894f5a9fSMaxime Ripard 	}
457894f5a9fSMaxime Ripard 
458894f5a9fSMaxime Ripard 	remote = of_graph_get_remote_port_parent(end_node);
459894f5a9fSMaxime Ripard 	if (!remote) {
460894f5a9fSMaxime Ripard 		DRM_DEBUG_DRIVER("Enable to parse remote node\n");
461894f5a9fSMaxime Ripard 		return ERR_PTR(-EINVAL);
462894f5a9fSMaxime Ripard 	}
463894f5a9fSMaxime Ripard 
464894f5a9fSMaxime Ripard 	return of_drm_find_bridge(remote) ?: ERR_PTR(-EPROBE_DEFER);
465894f5a9fSMaxime Ripard }
466894f5a9fSMaxime Ripard 
4679026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master,
4689026e0d1SMaxime Ripard 			   void *data)
4699026e0d1SMaxime Ripard {
4709026e0d1SMaxime Ripard 	struct drm_device *drm = data;
4719026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
4729026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon;
4739026e0d1SMaxime Ripard 	int ret;
4749026e0d1SMaxime Ripard 
4759026e0d1SMaxime Ripard 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
4769026e0d1SMaxime Ripard 	if (!tcon)
4779026e0d1SMaxime Ripard 		return -ENOMEM;
4789026e0d1SMaxime Ripard 	dev_set_drvdata(dev, tcon);
4799026e0d1SMaxime Ripard 	drv->tcon = tcon;
4809026e0d1SMaxime Ripard 	tcon->drm = drm;
481ae558110SMaxime Ripard 	tcon->dev = dev;
4829026e0d1SMaxime Ripard 
4839026e0d1SMaxime Ripard 	if (of_device_is_compatible(dev->of_node, "allwinner,sun5i-a13-tcon"))
4849026e0d1SMaxime Ripard 		tcon->has_mux = true;
4859026e0d1SMaxime Ripard 
4869026e0d1SMaxime Ripard 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
4879026e0d1SMaxime Ripard 	if (IS_ERR(tcon->lcd_rst)) {
4889026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
4899026e0d1SMaxime Ripard 		return PTR_ERR(tcon->lcd_rst);
4909026e0d1SMaxime Ripard 	}
4919026e0d1SMaxime Ripard 
4929026e0d1SMaxime Ripard 	/* Make sure our TCON is reset */
4939026e0d1SMaxime Ripard 	if (!reset_control_status(tcon->lcd_rst))
4949026e0d1SMaxime Ripard 		reset_control_assert(tcon->lcd_rst);
4959026e0d1SMaxime Ripard 
4969026e0d1SMaxime Ripard 	ret = reset_control_deassert(tcon->lcd_rst);
4979026e0d1SMaxime Ripard 	if (ret) {
4989026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't deassert our reset line\n");
4999026e0d1SMaxime Ripard 		return ret;
5009026e0d1SMaxime Ripard 	}
5019026e0d1SMaxime Ripard 
5029026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_regmap(dev, tcon);
5039026e0d1SMaxime Ripard 	if (ret) {
5049026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON regmap\n");
5059026e0d1SMaxime Ripard 		goto err_assert_reset;
5069026e0d1SMaxime Ripard 	}
5079026e0d1SMaxime Ripard 
5089026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_clocks(dev, tcon);
5099026e0d1SMaxime Ripard 	if (ret) {
5109026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON clocks\n");
5119026e0d1SMaxime Ripard 		goto err_assert_reset;
5129026e0d1SMaxime Ripard 	}
5139026e0d1SMaxime Ripard 
5149026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_irq(dev, tcon);
5159026e0d1SMaxime Ripard 	if (ret) {
5169026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON interrupts\n");
5179026e0d1SMaxime Ripard 		goto err_free_clocks;
5189026e0d1SMaxime Ripard 	}
5199026e0d1SMaxime Ripard 
52013fef095SChen-Yu Tsai 	ret = sun4i_rgb_init(drm);
52113fef095SChen-Yu Tsai 	if (ret < 0)
52213fef095SChen-Yu Tsai 		goto err_free_clocks;
52313fef095SChen-Yu Tsai 
52413fef095SChen-Yu Tsai 	return 0;
5259026e0d1SMaxime Ripard 
5269026e0d1SMaxime Ripard err_free_clocks:
5279026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
5289026e0d1SMaxime Ripard err_assert_reset:
5299026e0d1SMaxime Ripard 	reset_control_assert(tcon->lcd_rst);
5309026e0d1SMaxime Ripard 	return ret;
5319026e0d1SMaxime Ripard }
5329026e0d1SMaxime Ripard 
5339026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master,
5349026e0d1SMaxime Ripard 			      void *data)
5359026e0d1SMaxime Ripard {
5369026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
5379026e0d1SMaxime Ripard 
5389026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
5399026e0d1SMaxime Ripard }
5409026e0d1SMaxime Ripard 
5419026e0d1SMaxime Ripard static struct component_ops sun4i_tcon_ops = {
5429026e0d1SMaxime Ripard 	.bind	= sun4i_tcon_bind,
5439026e0d1SMaxime Ripard 	.unbind	= sun4i_tcon_unbind,
5449026e0d1SMaxime Ripard };
5459026e0d1SMaxime Ripard 
5469026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev)
5479026e0d1SMaxime Ripard {
54829e57fabSMaxime Ripard 	struct device_node *node = pdev->dev.of_node;
549894f5a9fSMaxime Ripard 	struct drm_bridge *bridge;
55029e57fabSMaxime Ripard 	struct drm_panel *panel;
55129e57fabSMaxime Ripard 
55229e57fabSMaxime Ripard 	/*
553894f5a9fSMaxime Ripard 	 * Neither the bridge or the panel is ready.
55429e57fabSMaxime Ripard 	 * Defer the probe.
55529e57fabSMaxime Ripard 	 */
55629e57fabSMaxime Ripard 	panel = sun4i_tcon_find_panel(node);
557894f5a9fSMaxime Ripard 	bridge = sun4i_tcon_find_bridge(node);
5580bbbb00bSMaxime Ripard 
55929e57fabSMaxime Ripard 	/*
56029e57fabSMaxime Ripard 	 * If we don't have a panel endpoint, just go on
56129e57fabSMaxime Ripard 	 */
562894f5a9fSMaxime Ripard 	if ((PTR_ERR(panel) == -EPROBE_DEFER) &&
563894f5a9fSMaxime Ripard 	    (PTR_ERR(bridge) == -EPROBE_DEFER)) {
564894f5a9fSMaxime Ripard 		DRM_DEBUG_DRIVER("Still waiting for our panel/bridge. Deferring...\n");
56529e57fabSMaxime Ripard 		return -EPROBE_DEFER;
56629e57fabSMaxime Ripard 	}
56729e57fabSMaxime Ripard 
5689026e0d1SMaxime Ripard 	return component_add(&pdev->dev, &sun4i_tcon_ops);
5699026e0d1SMaxime Ripard }
5709026e0d1SMaxime Ripard 
5719026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev)
5729026e0d1SMaxime Ripard {
5739026e0d1SMaxime Ripard 	component_del(&pdev->dev, &sun4i_tcon_ops);
5749026e0d1SMaxime Ripard 
5759026e0d1SMaxime Ripard 	return 0;
5769026e0d1SMaxime Ripard }
5779026e0d1SMaxime Ripard 
5789026e0d1SMaxime Ripard static const struct of_device_id sun4i_tcon_of_table[] = {
5799026e0d1SMaxime Ripard 	{ .compatible = "allwinner,sun5i-a13-tcon" },
5809026e0d1SMaxime Ripard 	{ }
5819026e0d1SMaxime Ripard };
5829026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
5839026e0d1SMaxime Ripard 
5849026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = {
5859026e0d1SMaxime Ripard 	.probe		= sun4i_tcon_probe,
5869026e0d1SMaxime Ripard 	.remove		= sun4i_tcon_remove,
5879026e0d1SMaxime Ripard 	.driver		= {
5889026e0d1SMaxime Ripard 		.name		= "sun4i-tcon",
5899026e0d1SMaxime Ripard 		.of_match_table	= sun4i_tcon_of_table,
5909026e0d1SMaxime Ripard 	},
5919026e0d1SMaxime Ripard };
5929026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver);
5939026e0d1SMaxime Ripard 
5949026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
5959026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
5969026e0d1SMaxime Ripard MODULE_LICENSE("GPL");
597