xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision 34d698f6e349df991effe424b9500abbf0d43ff0)
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>
17ad537fb2SChen-Yu Tsai #include <drm/drm_encoder.h>
189026e0d1SMaxime Ripard #include <drm/drm_modes.h>
19ebc94461SRob Herring #include <drm/drm_of.h>
209026e0d1SMaxime Ripard 
21ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h>
22ad537fb2SChen-Yu Tsai 
239026e0d1SMaxime Ripard #include <linux/component.h>
249026e0d1SMaxime Ripard #include <linux/ioport.h>
259026e0d1SMaxime Ripard #include <linux/of_address.h>
2691ea2f29SChen-Yu Tsai #include <linux/of_device.h>
279026e0d1SMaxime Ripard #include <linux/of_irq.h>
289026e0d1SMaxime Ripard #include <linux/regmap.h>
299026e0d1SMaxime Ripard #include <linux/reset.h>
309026e0d1SMaxime Ripard 
319026e0d1SMaxime Ripard #include "sun4i_crtc.h"
329026e0d1SMaxime Ripard #include "sun4i_dotclock.h"
339026e0d1SMaxime Ripard #include "sun4i_drv.h"
34a0c1214eSMaxime Ripard #include "sun4i_lvds.h"
3529e57fabSMaxime Ripard #include "sun4i_rgb.h"
369026e0d1SMaxime Ripard #include "sun4i_tcon.h"
3787969338SIcenowy Zheng #include "sunxi_engine.h"
389026e0d1SMaxime Ripard 
39a0c1214eSMaxime Ripard static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder)
40a0c1214eSMaxime Ripard {
41a0c1214eSMaxime Ripard 	struct drm_connector *connector;
42a0c1214eSMaxime Ripard 	struct drm_connector_list_iter iter;
43a0c1214eSMaxime Ripard 
44a0c1214eSMaxime Ripard 	drm_connector_list_iter_begin(encoder->dev, &iter);
45a0c1214eSMaxime Ripard 	drm_for_each_connector_iter(connector, &iter)
46a0c1214eSMaxime Ripard 		if (connector->encoder == encoder) {
47a0c1214eSMaxime Ripard 			drm_connector_list_iter_end(&iter);
48a0c1214eSMaxime Ripard 			return connector;
49a0c1214eSMaxime Ripard 		}
50a0c1214eSMaxime Ripard 	drm_connector_list_iter_end(&iter);
51a0c1214eSMaxime Ripard 
52a0c1214eSMaxime Ripard 	return NULL;
53a0c1214eSMaxime Ripard }
54a0c1214eSMaxime Ripard 
55a0c1214eSMaxime Ripard static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder)
56a0c1214eSMaxime Ripard {
57a0c1214eSMaxime Ripard 	struct drm_connector *connector;
58a0c1214eSMaxime Ripard 	struct drm_display_info *info;
59a0c1214eSMaxime Ripard 
60a0c1214eSMaxime Ripard 	connector = sun4i_tcon_get_connector(encoder);
61a0c1214eSMaxime Ripard 	if (!connector)
62a0c1214eSMaxime Ripard 		return -EINVAL;
63a0c1214eSMaxime Ripard 
64a0c1214eSMaxime Ripard 	info = &connector->display_info;
65a0c1214eSMaxime Ripard 	if (info->num_bus_formats != 1)
66a0c1214eSMaxime Ripard 		return -EINVAL;
67a0c1214eSMaxime Ripard 
68a0c1214eSMaxime Ripard 	switch (info->bus_formats[0]) {
69a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
70a0c1214eSMaxime Ripard 		return 18;
71a0c1214eSMaxime Ripard 
72a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
73a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
74a0c1214eSMaxime Ripard 		return 24;
75a0c1214eSMaxime Ripard 	}
76a0c1214eSMaxime Ripard 
77a0c1214eSMaxime Ripard 	return -EINVAL;
78a0c1214eSMaxime Ripard }
79a0c1214eSMaxime Ripard 
8045e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
8145e88f99SMaxime Ripard 					  bool enabled)
829026e0d1SMaxime Ripard {
8345e88f99SMaxime Ripard 	struct clk *clk;
849026e0d1SMaxime Ripard 
8545e88f99SMaxime Ripard 	switch (channel) {
8645e88f99SMaxime Ripard 	case 0:
87*34d698f6SJernej Skrabec 		WARN_ON(!tcon->quirks->has_channel_0);
889026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
899026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE,
9045e88f99SMaxime Ripard 				   enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
9145e88f99SMaxime Ripard 		clk = tcon->dclk;
9245e88f99SMaxime Ripard 		break;
9345e88f99SMaxime Ripard 	case 1:
9491ea2f29SChen-Yu Tsai 		WARN_ON(!tcon->quirks->has_channel_1);
959026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
969026e0d1SMaxime Ripard 				   SUN4I_TCON1_CTL_TCON_ENABLE,
9745e88f99SMaxime Ripard 				   enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
9845e88f99SMaxime Ripard 		clk = tcon->sclk1;
9945e88f99SMaxime Ripard 		break;
10045e88f99SMaxime Ripard 	default:
10145e88f99SMaxime Ripard 		DRM_WARN("Unknown channel... doing nothing\n");
10245e88f99SMaxime Ripard 		return;
1039026e0d1SMaxime Ripard 	}
10445e88f99SMaxime Ripard 
10545e88f99SMaxime Ripard 	if (enabled)
10645e88f99SMaxime Ripard 		clk_prepare_enable(clk);
10745e88f99SMaxime Ripard 	else
10845e88f99SMaxime Ripard 		clk_disable_unprepare(clk);
10945e88f99SMaxime Ripard }
11045e88f99SMaxime Ripard 
111a0c1214eSMaxime Ripard static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon,
112a0c1214eSMaxime Ripard 				       const struct drm_encoder *encoder,
113a0c1214eSMaxime Ripard 				       bool enabled)
114a0c1214eSMaxime Ripard {
115a0c1214eSMaxime Ripard 	if (enabled) {
116a0c1214eSMaxime Ripard 		u8 val;
117a0c1214eSMaxime Ripard 
118a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
119a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN,
120a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN);
121a0c1214eSMaxime Ripard 
122a0c1214eSMaxime Ripard 		/*
123a0c1214eSMaxime Ripard 		 * As their name suggest, these values only apply to the A31
124a0c1214eSMaxime Ripard 		 * and later SoCs. We'll have to rework this when merging
125a0c1214eSMaxime Ripard 		 * support for the older SoCs.
126a0c1214eSMaxime Ripard 		 */
127a0c1214eSMaxime Ripard 		regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
128a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_C(2) |
129a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_V(3) |
130a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_PD(2) |
131a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_EN_LDO);
132a0c1214eSMaxime Ripard 		udelay(2);
133a0c1214eSMaxime Ripard 
134a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
135a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_MB,
136a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_MB);
137a0c1214eSMaxime Ripard 		udelay(2);
138a0c1214eSMaxime Ripard 
139a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
140a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC,
141a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC);
142a0c1214eSMaxime Ripard 
143a0c1214eSMaxime Ripard 		if (sun4i_tcon_get_pixel_depth(encoder) == 18)
144a0c1214eSMaxime Ripard 			val = 7;
145a0c1214eSMaxime Ripard 		else
146a0c1214eSMaxime Ripard 			val = 0xf;
147a0c1214eSMaxime Ripard 
148a0c1214eSMaxime Ripard 		regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
149a0c1214eSMaxime Ripard 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf),
150a0c1214eSMaxime Ripard 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val));
151a0c1214eSMaxime Ripard 	} else {
152a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
153a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN, 0);
154a0c1214eSMaxime Ripard 	}
155a0c1214eSMaxime Ripard }
156a0c1214eSMaxime Ripard 
15745e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
15845e88f99SMaxime Ripard 			   const struct drm_encoder *encoder,
15945e88f99SMaxime Ripard 			   bool enabled)
16045e88f99SMaxime Ripard {
161a0c1214eSMaxime Ripard 	bool is_lvds = false;
16245e88f99SMaxime Ripard 	int channel;
16345e88f99SMaxime Ripard 
16445e88f99SMaxime Ripard 	switch (encoder->encoder_type) {
165a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
166a0c1214eSMaxime Ripard 		is_lvds = true;
167a0c1214eSMaxime Ripard 		/* Fallthrough */
16845e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
16945e88f99SMaxime Ripard 		channel = 0;
17045e88f99SMaxime Ripard 		break;
17145e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
17245e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
17345e88f99SMaxime Ripard 		channel = 1;
17445e88f99SMaxime Ripard 		break;
17545e88f99SMaxime Ripard 	default:
17645e88f99SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
17745e88f99SMaxime Ripard 		return;
17845e88f99SMaxime Ripard 	}
17945e88f99SMaxime Ripard 
180a0c1214eSMaxime Ripard 	if (is_lvds && !enabled)
181a0c1214eSMaxime Ripard 		sun4i_tcon_lvds_set_status(tcon, encoder, false);
182a0c1214eSMaxime Ripard 
18345e88f99SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
18445e88f99SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE,
18545e88f99SMaxime Ripard 			   enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
18645e88f99SMaxime Ripard 
187a0c1214eSMaxime Ripard 	if (is_lvds && enabled)
188a0c1214eSMaxime Ripard 		sun4i_tcon_lvds_set_status(tcon, encoder, true);
189a0c1214eSMaxime Ripard 
19045e88f99SMaxime Ripard 	sun4i_tcon_channel_set_status(tcon, channel, enabled);
19145e88f99SMaxime Ripard }
1929026e0d1SMaxime Ripard 
1939026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
1949026e0d1SMaxime Ripard {
1959026e0d1SMaxime Ripard 	u32 mask, val = 0;
1969026e0d1SMaxime Ripard 
1979026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
1989026e0d1SMaxime Ripard 
1999026e0d1SMaxime Ripard 	mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
2009026e0d1SMaxime Ripard 	       SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
2019026e0d1SMaxime Ripard 
2029026e0d1SMaxime Ripard 	if (enable)
2039026e0d1SMaxime Ripard 		val = mask;
2049026e0d1SMaxime Ripard 
2059026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
2069026e0d1SMaxime Ripard }
2079026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
2089026e0d1SMaxime Ripard 
20967e32645SChen-Yu Tsai /*
21067e32645SChen-Yu Tsai  * This function is a helper for TCON output muxing. The TCON output
21167e32645SChen-Yu Tsai  * muxing control register in earlier SoCs (without the TCON TOP block)
21267e32645SChen-Yu Tsai  * are located in TCON0. This helper returns a pointer to TCON0's
21367e32645SChen-Yu Tsai  * sun4i_tcon structure, or NULL if not found.
21467e32645SChen-Yu Tsai  */
21567e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
21667e32645SChen-Yu Tsai {
21767e32645SChen-Yu Tsai 	struct sun4i_drv *drv = drm->dev_private;
21867e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon;
21967e32645SChen-Yu Tsai 
22067e32645SChen-Yu Tsai 	list_for_each_entry(tcon, &drv->tcon_list, list)
22167e32645SChen-Yu Tsai 		if (tcon->id == 0)
22267e32645SChen-Yu Tsai 			return tcon;
22367e32645SChen-Yu Tsai 
22467e32645SChen-Yu Tsai 	dev_warn(drm->dev,
22567e32645SChen-Yu Tsai 		 "TCON0 not found, display output muxing may not work\n");
22667e32645SChen-Yu Tsai 
22767e32645SChen-Yu Tsai 	return NULL;
22867e32645SChen-Yu Tsai }
22967e32645SChen-Yu Tsai 
230f8c73f4fSMaxime Ripard void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
231abcb8766SMaxime Ripard 			const struct drm_encoder *encoder)
232f8c73f4fSMaxime Ripard {
233ad537fb2SChen-Yu Tsai 	int ret = -ENOTSUPP;
234b7cb9b91SMaxime Ripard 
235ad537fb2SChen-Yu Tsai 	if (tcon->quirks->set_mux)
236ad537fb2SChen-Yu Tsai 		ret = tcon->quirks->set_mux(tcon, encoder);
237f8c73f4fSMaxime Ripard 
238ad537fb2SChen-Yu Tsai 	DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
239ad537fb2SChen-Yu Tsai 			 encoder->name, encoder->crtc->name, ret);
240f8c73f4fSMaxime Ripard }
241f8c73f4fSMaxime Ripard 
242961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
2439026e0d1SMaxime Ripard 				    int channel)
2449026e0d1SMaxime Ripard {
2459026e0d1SMaxime Ripard 	int delay = mode->vtotal - mode->vdisplay;
2469026e0d1SMaxime Ripard 
2479026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2489026e0d1SMaxime Ripard 		delay /= 2;
2499026e0d1SMaxime Ripard 
2509026e0d1SMaxime Ripard 	if (channel == 1)
2519026e0d1SMaxime Ripard 		delay -= 2;
2529026e0d1SMaxime Ripard 
2539026e0d1SMaxime Ripard 	delay = min(delay, 30);
2549026e0d1SMaxime Ripard 
2559026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
2569026e0d1SMaxime Ripard 
2579026e0d1SMaxime Ripard 	return delay;
2589026e0d1SMaxime Ripard }
2599026e0d1SMaxime Ripard 
260ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon,
261ba19c537SMaxime Ripard 					const struct drm_display_mode *mode)
262ba19c537SMaxime Ripard {
263ba19c537SMaxime Ripard 	/* Configure the dot clock */
264ba19c537SMaxime Ripard 	clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
265ba19c537SMaxime Ripard 
266ba19c537SMaxime Ripard 	/* Set the resolution */
267ba19c537SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
268ba19c537SMaxime Ripard 		     SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
269ba19c537SMaxime Ripard 		     SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
270ba19c537SMaxime Ripard }
271ba19c537SMaxime Ripard 
272a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
273a0c1214eSMaxime Ripard 				      const struct drm_encoder *encoder,
274a0c1214eSMaxime Ripard 				      const struct drm_display_mode *mode)
275a0c1214eSMaxime Ripard {
276a0c1214eSMaxime Ripard 	unsigned int bp;
277a0c1214eSMaxime Ripard 	u8 clk_delay;
278a0c1214eSMaxime Ripard 	u32 reg, val = 0;
279a0c1214eSMaxime Ripard 
280*34d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
281*34d698f6SJernej Skrabec 
282a0c1214eSMaxime Ripard 	tcon->dclk_min_div = 7;
283a0c1214eSMaxime Ripard 	tcon->dclk_max_div = 7;
284a0c1214eSMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
285a0c1214eSMaxime Ripard 
286a0c1214eSMaxime Ripard 	/* Adjust clock delay */
287a0c1214eSMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
288a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
289a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
290a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
291a0c1214eSMaxime Ripard 
292a0c1214eSMaxime Ripard 	/*
293a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
294a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
295a0c1214eSMaxime Ripard 	 */
296a0c1214eSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
297a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
298a0c1214eSMaxime Ripard 			 mode->crtc_htotal, bp);
299a0c1214eSMaxime Ripard 
300a0c1214eSMaxime Ripard 	/* Set horizontal display timings */
301a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
302a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
303a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
304a0c1214eSMaxime Ripard 
305a0c1214eSMaxime Ripard 	/*
306a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
307a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
308a0c1214eSMaxime Ripard 	 */
309a0c1214eSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
310a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
311a0c1214eSMaxime Ripard 			 mode->crtc_vtotal, bp);
312a0c1214eSMaxime Ripard 
313a0c1214eSMaxime Ripard 	/* Set vertical display timings */
314a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
315a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
316a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
317a0c1214eSMaxime Ripard 
318a0c1214eSMaxime Ripard 	reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
319a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
320a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
321a0c1214eSMaxime Ripard 	if (sun4i_tcon_get_pixel_depth(encoder) == 24)
322a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
323a0c1214eSMaxime Ripard 	else
324a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
325a0c1214eSMaxime Ripard 
326a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
327a0c1214eSMaxime Ripard 
328a0c1214eSMaxime Ripard 	/* Setup the polarity of the various signals */
329a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
330a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
331a0c1214eSMaxime Ripard 
332a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
333a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
334a0c1214eSMaxime Ripard 
335a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
336a0c1214eSMaxime Ripard 
337a0c1214eSMaxime Ripard 	/* Map output pins to channel 0 */
338a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
339a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
340a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
341a0c1214eSMaxime Ripard }
342a0c1214eSMaxime Ripard 
343ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
3445b8f0910SMaxime Ripard 				     const struct drm_display_mode *mode)
3459026e0d1SMaxime Ripard {
3469026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
3479026e0d1SMaxime Ripard 	u8 clk_delay;
3489026e0d1SMaxime Ripard 	u32 val = 0;
3499026e0d1SMaxime Ripard 
350*34d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
351*34d698f6SJernej Skrabec 
352ec08d596SMaxime Ripard 	tcon->dclk_min_div = 6;
353ec08d596SMaxime Ripard 	tcon->dclk_max_div = 127;
354ba19c537SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
35586cf6788SChen-Yu Tsai 
3569026e0d1SMaxime Ripard 	/* Adjust clock delay */
3579026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
3589026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
3599026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
3609026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
3619026e0d1SMaxime Ripard 
3629026e0d1SMaxime Ripard 	/*
3639026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
36423a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
3659026e0d1SMaxime Ripard 	 */
3669026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
3679026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
3689026e0d1SMaxime Ripard 			 mode->crtc_htotal, bp);
3699026e0d1SMaxime Ripard 
3709026e0d1SMaxime Ripard 	/* Set horizontal display timings */
3719026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
3729026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
3739026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
3749026e0d1SMaxime Ripard 
3759026e0d1SMaxime Ripard 	/*
3769026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
37723a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
3789026e0d1SMaxime Ripard 	 */
3799026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
3809026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
3819026e0d1SMaxime Ripard 			 mode->crtc_vtotal, bp);
3829026e0d1SMaxime Ripard 
3839026e0d1SMaxime Ripard 	/* Set vertical display timings */
3849026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
385a88cbbd4SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
3869026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
3879026e0d1SMaxime Ripard 
3889026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
3899026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
3909026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
3919026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
3929026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
3939026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
3949026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
3959026e0d1SMaxime Ripard 
3969026e0d1SMaxime Ripard 	/* Setup the polarity of the various signals */
3979026e0d1SMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
3989026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
3999026e0d1SMaxime Ripard 
4009026e0d1SMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
4019026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
4029026e0d1SMaxime Ripard 
4039026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
4049026e0d1SMaxime Ripard 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
4059026e0d1SMaxime Ripard 			   val);
4069026e0d1SMaxime Ripard 
4079026e0d1SMaxime Ripard 	/* Map output pins to channel 0 */
4089026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
4099026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
4109026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
4119026e0d1SMaxime Ripard 
4129026e0d1SMaxime Ripard 	/* Enable the output on the pins */
4139026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
4149026e0d1SMaxime Ripard }
4159026e0d1SMaxime Ripard 
4165b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
4175b8f0910SMaxime Ripard 				 const struct drm_display_mode *mode)
4189026e0d1SMaxime Ripard {
419b8317a3dSMaxime Ripard 	unsigned int bp, hsync, vsync, vtotal;
4209026e0d1SMaxime Ripard 	u8 clk_delay;
4219026e0d1SMaxime Ripard 	u32 val;
4229026e0d1SMaxime Ripard 
42391ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
4248e924047SMaxime Ripard 
42586cf6788SChen-Yu Tsai 	/* Configure the dot clock */
42686cf6788SChen-Yu Tsai 	clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
42786cf6788SChen-Yu Tsai 
4289026e0d1SMaxime Ripard 	/* Adjust clock delay */
4299026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
4309026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
4319026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
4329026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
4339026e0d1SMaxime Ripard 
4349026e0d1SMaxime Ripard 	/* Set interlaced mode */
4359026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
4369026e0d1SMaxime Ripard 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
4379026e0d1SMaxime Ripard 	else
4389026e0d1SMaxime Ripard 		val = 0;
4399026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
4409026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
4419026e0d1SMaxime Ripard 			   val);
4429026e0d1SMaxime Ripard 
4439026e0d1SMaxime Ripard 	/* Set the input resolution */
4449026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
4459026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
4469026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
4479026e0d1SMaxime Ripard 
4489026e0d1SMaxime Ripard 	/* Set the upscaling resolution */
4499026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
4509026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
4519026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
4529026e0d1SMaxime Ripard 
4539026e0d1SMaxime Ripard 	/* Set the output resolution */
4549026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
4559026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
4569026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
4579026e0d1SMaxime Ripard 
4589026e0d1SMaxime Ripard 	/* Set horizontal display timings */
4593cb2f46bSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
4609026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
4619026e0d1SMaxime Ripard 			 mode->htotal, bp);
4629026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
4639026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
4649026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
4659026e0d1SMaxime Ripard 
4663cb2f46bSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
4679026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
468b8317a3dSMaxime Ripard 			 mode->crtc_vtotal, bp);
469b8317a3dSMaxime Ripard 
470b8317a3dSMaxime Ripard 	/*
471b8317a3dSMaxime Ripard 	 * The vertical resolution needs to be doubled in all
472b8317a3dSMaxime Ripard 	 * cases. We could use crtc_vtotal and always multiply by two,
473b8317a3dSMaxime Ripard 	 * but that leads to a rounding error in interlace when vtotal
474b8317a3dSMaxime Ripard 	 * is odd.
475b8317a3dSMaxime Ripard 	 *
476b8317a3dSMaxime Ripard 	 * This happens with TV's PAL for example, where vtotal will
477b8317a3dSMaxime Ripard 	 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
478b8317a3dSMaxime Ripard 	 * 624, which apparently confuses the hardware.
479b8317a3dSMaxime Ripard 	 *
480b8317a3dSMaxime Ripard 	 * To work around this, we will always use vtotal, and
481b8317a3dSMaxime Ripard 	 * multiply by two only if we're not in interlace.
482b8317a3dSMaxime Ripard 	 */
483b8317a3dSMaxime Ripard 	vtotal = mode->vtotal;
484b8317a3dSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
485b8317a3dSMaxime Ripard 		vtotal = vtotal * 2;
486b8317a3dSMaxime Ripard 
487b8317a3dSMaxime Ripard 	/* Set vertical display timings */
4889026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
489b8317a3dSMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
4909026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
4919026e0d1SMaxime Ripard 
4929026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
4939026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
4949026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
4959026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
4969026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
4979026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
4989026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
4999026e0d1SMaxime Ripard 
5009026e0d1SMaxime Ripard 	/* Map output pins to channel 1 */
5019026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
5029026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
5039026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
5049026e0d1SMaxime Ripard }
5055b8f0910SMaxime Ripard 
5065b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
5075b8f0910SMaxime Ripard 			 const struct drm_encoder *encoder,
5085b8f0910SMaxime Ripard 			 const struct drm_display_mode *mode)
5095b8f0910SMaxime Ripard {
5105b8f0910SMaxime Ripard 	switch (encoder->encoder_type) {
511a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
512a0c1214eSMaxime Ripard 		sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
513a0c1214eSMaxime Ripard 		break;
5145b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
515ba19c537SMaxime Ripard 		sun4i_tcon0_mode_set_rgb(tcon, mode);
5165b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 0, encoder);
5175b8f0910SMaxime Ripard 		break;
5185b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
5195b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
5205b8f0910SMaxime Ripard 		sun4i_tcon1_mode_set(tcon, mode);
5215b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 1, encoder);
5225b8f0910SMaxime Ripard 		break;
5235b8f0910SMaxime Ripard 	default:
5245b8f0910SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
5255b8f0910SMaxime Ripard 	}
5265b8f0910SMaxime Ripard }
5275b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set);
5289026e0d1SMaxime Ripard 
5299026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
5309026e0d1SMaxime Ripard 					struct sun4i_crtc *scrtc)
5319026e0d1SMaxime Ripard {
5329026e0d1SMaxime Ripard 	unsigned long flags;
5339026e0d1SMaxime Ripard 
5349026e0d1SMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
5359026e0d1SMaxime Ripard 	if (scrtc->event) {
5369026e0d1SMaxime Ripard 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
5379026e0d1SMaxime Ripard 		drm_crtc_vblank_put(&scrtc->crtc);
5389026e0d1SMaxime Ripard 		scrtc->event = NULL;
5399026e0d1SMaxime Ripard 	}
5409026e0d1SMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
5419026e0d1SMaxime Ripard }
5429026e0d1SMaxime Ripard 
5439026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private)
5449026e0d1SMaxime Ripard {
5459026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = private;
5469026e0d1SMaxime Ripard 	struct drm_device *drm = tcon->drm;
54746cce6daSChen-Yu Tsai 	struct sun4i_crtc *scrtc = tcon->crtc;
5483004f75fSMaxime Ripard 	struct sunxi_engine *engine = scrtc->engine;
5499026e0d1SMaxime Ripard 	unsigned int status;
5509026e0d1SMaxime Ripard 
5519026e0d1SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
5529026e0d1SMaxime Ripard 
5539026e0d1SMaxime Ripard 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
5549026e0d1SMaxime Ripard 			SUN4I_TCON_GINT0_VBLANK_INT(1))))
5559026e0d1SMaxime Ripard 		return IRQ_NONE;
5569026e0d1SMaxime Ripard 
5579026e0d1SMaxime Ripard 	drm_crtc_handle_vblank(&scrtc->crtc);
5589026e0d1SMaxime Ripard 	sun4i_tcon_finish_page_flip(drm, scrtc);
5599026e0d1SMaxime Ripard 
5609026e0d1SMaxime Ripard 	/* Acknowledge the interrupt */
5619026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
5629026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
5639026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(1),
5649026e0d1SMaxime Ripard 			   0);
5659026e0d1SMaxime Ripard 
5663004f75fSMaxime Ripard 	if (engine->ops->vblank_quirk)
5673004f75fSMaxime Ripard 		engine->ops->vblank_quirk(engine);
5683004f75fSMaxime Ripard 
5699026e0d1SMaxime Ripard 	return IRQ_HANDLED;
5709026e0d1SMaxime Ripard }
5719026e0d1SMaxime Ripard 
5729026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev,
5739026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
5749026e0d1SMaxime Ripard {
5759026e0d1SMaxime Ripard 	tcon->clk = devm_clk_get(dev, "ahb");
5769026e0d1SMaxime Ripard 	if (IS_ERR(tcon->clk)) {
5779026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON bus clock\n");
5789026e0d1SMaxime Ripard 		return PTR_ERR(tcon->clk);
5799026e0d1SMaxime Ripard 	}
5809026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->clk);
5819026e0d1SMaxime Ripard 
582*34d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
5839026e0d1SMaxime Ripard 		tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
5849026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk0)) {
5859026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
5869026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk0);
5879026e0d1SMaxime Ripard 		}
588*34d698f6SJernej Skrabec 	}
5899026e0d1SMaxime Ripard 
59091ea2f29SChen-Yu Tsai 	if (tcon->quirks->has_channel_1) {
5919026e0d1SMaxime Ripard 		tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
5929026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk1)) {
5939026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
5949026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk1);
5959026e0d1SMaxime Ripard 		}
5968e924047SMaxime Ripard 	}
5979026e0d1SMaxime Ripard 
5984c7f16d1SChen-Yu Tsai 	return 0;
5999026e0d1SMaxime Ripard }
6009026e0d1SMaxime Ripard 
6019026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
6029026e0d1SMaxime Ripard {
6039026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->clk);
6049026e0d1SMaxime Ripard }
6059026e0d1SMaxime Ripard 
6069026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev,
6079026e0d1SMaxime Ripard 			       struct sun4i_tcon *tcon)
6089026e0d1SMaxime Ripard {
6099026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
6109026e0d1SMaxime Ripard 	int irq, ret;
6119026e0d1SMaxime Ripard 
6129026e0d1SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
6139026e0d1SMaxime Ripard 	if (irq < 0) {
6149026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
6159026e0d1SMaxime Ripard 		return irq;
6169026e0d1SMaxime Ripard 	}
6179026e0d1SMaxime Ripard 
6189026e0d1SMaxime Ripard 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
6199026e0d1SMaxime Ripard 			       dev_name(dev), tcon);
6209026e0d1SMaxime Ripard 	if (ret) {
6219026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't request the IRQ\n");
6229026e0d1SMaxime Ripard 		return ret;
6239026e0d1SMaxime Ripard 	}
6249026e0d1SMaxime Ripard 
6259026e0d1SMaxime Ripard 	return 0;
6269026e0d1SMaxime Ripard }
6279026e0d1SMaxime Ripard 
6289026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = {
6299026e0d1SMaxime Ripard 	.reg_bits	= 32,
6309026e0d1SMaxime Ripard 	.val_bits	= 32,
6319026e0d1SMaxime Ripard 	.reg_stride	= 4,
6329026e0d1SMaxime Ripard 	.max_register	= 0x800,
6339026e0d1SMaxime Ripard };
6349026e0d1SMaxime Ripard 
6359026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev,
6369026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
6379026e0d1SMaxime Ripard {
6389026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
6399026e0d1SMaxime Ripard 	struct resource *res;
6409026e0d1SMaxime Ripard 	void __iomem *regs;
6419026e0d1SMaxime Ripard 
6429026e0d1SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6439026e0d1SMaxime Ripard 	regs = devm_ioremap_resource(dev, res);
644af346f55SWei Yongjun 	if (IS_ERR(regs))
6459026e0d1SMaxime Ripard 		return PTR_ERR(regs);
6469026e0d1SMaxime Ripard 
6479026e0d1SMaxime Ripard 	tcon->regs = devm_regmap_init_mmio(dev, regs,
6489026e0d1SMaxime Ripard 					   &sun4i_tcon_regmap_config);
6499026e0d1SMaxime Ripard 	if (IS_ERR(tcon->regs)) {
6509026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't create the TCON regmap\n");
6519026e0d1SMaxime Ripard 		return PTR_ERR(tcon->regs);
6529026e0d1SMaxime Ripard 	}
6539026e0d1SMaxime Ripard 
6549026e0d1SMaxime Ripard 	/* Make sure the TCON is disabled and all IRQs are off */
6559026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
6569026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
6579026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
6589026e0d1SMaxime Ripard 
6599026e0d1SMaxime Ripard 	/* Disable IO lines and set them to tristate */
6609026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
6619026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
6629026e0d1SMaxime Ripard 
6639026e0d1SMaxime Ripard 	return 0;
6649026e0d1SMaxime Ripard }
6659026e0d1SMaxime Ripard 
666b317fa3bSChen-Yu Tsai /*
667b317fa3bSChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
668b317fa3bSChen-Yu Tsai  * the TCON is always tied to just one backend. Hence we can traverse
669b317fa3bSChen-Yu Tsai  * the of_graph upwards to find the backend our tcon is connected to,
670b317fa3bSChen-Yu Tsai  * and take its ID as our own.
671b317fa3bSChen-Yu Tsai  *
672b317fa3bSChen-Yu Tsai  * We can either identify backends from their compatible strings, which
673b317fa3bSChen-Yu Tsai  * means maintaining a large list of them. Or, since the backend is
674b317fa3bSChen-Yu Tsai  * registered and binded before the TCON, we can just go through the
675b317fa3bSChen-Yu Tsai  * list of registered backends and compare the device node.
67687969338SIcenowy Zheng  *
67787969338SIcenowy Zheng  * As the structures now store engines instead of backends, here this
67887969338SIcenowy Zheng  * function in fact searches the corresponding engine, and the ID is
67987969338SIcenowy Zheng  * requested via the get_id function of the engine.
680b317fa3bSChen-Yu Tsai  */
681e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *
682e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
683b317fa3bSChen-Yu Tsai 				struct device_node *node)
684b317fa3bSChen-Yu Tsai {
685b317fa3bSChen-Yu Tsai 	struct device_node *port, *ep, *remote;
686be3fe0f9SChen-Yu Tsai 	struct sunxi_engine *engine = ERR_PTR(-EINVAL);
687b317fa3bSChen-Yu Tsai 
688b317fa3bSChen-Yu Tsai 	port = of_graph_get_port_by_id(node, 0);
689b317fa3bSChen-Yu Tsai 	if (!port)
690b317fa3bSChen-Yu Tsai 		return ERR_PTR(-EINVAL);
691b317fa3bSChen-Yu Tsai 
6921469619dSChen-Yu Tsai 	/*
6931469619dSChen-Yu Tsai 	 * This only works if there is only one path from the TCON
6941469619dSChen-Yu Tsai 	 * to any display engine. Otherwise the probe order of the
6951469619dSChen-Yu Tsai 	 * TCONs and display engines is not guaranteed. They may
6961469619dSChen-Yu Tsai 	 * either bind to the wrong one, or worse, bind to the same
6971469619dSChen-Yu Tsai 	 * one if additional checks are not done.
6981469619dSChen-Yu Tsai 	 *
6991469619dSChen-Yu Tsai 	 * Bail out if there are multiple input connections.
7001469619dSChen-Yu Tsai 	 */
701be3fe0f9SChen-Yu Tsai 	if (of_get_available_child_count(port) != 1)
702be3fe0f9SChen-Yu Tsai 		goto out_put_port;
7031469619dSChen-Yu Tsai 
704be3fe0f9SChen-Yu Tsai 	/* Get the first connection without specifying an ID */
705be3fe0f9SChen-Yu Tsai 	ep = of_get_next_available_child(port, NULL);
706be3fe0f9SChen-Yu Tsai 	if (!ep)
707be3fe0f9SChen-Yu Tsai 		goto out_put_port;
708be3fe0f9SChen-Yu Tsai 
709b317fa3bSChen-Yu Tsai 	remote = of_graph_get_remote_port_parent(ep);
710b317fa3bSChen-Yu Tsai 	if (!remote)
711be3fe0f9SChen-Yu Tsai 		goto out_put_ep;
712b317fa3bSChen-Yu Tsai 
71387969338SIcenowy Zheng 	/* does this node match any registered engines? */
714be3fe0f9SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
715be3fe0f9SChen-Yu Tsai 		if (remote == engine->node)
716be3fe0f9SChen-Yu Tsai 			goto out_put_remote;
717b317fa3bSChen-Yu Tsai 
718b317fa3bSChen-Yu Tsai 	/* keep looking through upstream ports */
719e8d5bbf7SChen-Yu Tsai 	engine = sun4i_tcon_find_engine_traverse(drv, remote);
720b317fa3bSChen-Yu Tsai 
721be3fe0f9SChen-Yu Tsai out_put_remote:
722be3fe0f9SChen-Yu Tsai 	of_node_put(remote);
723be3fe0f9SChen-Yu Tsai out_put_ep:
724be3fe0f9SChen-Yu Tsai 	of_node_put(ep);
725be3fe0f9SChen-Yu Tsai out_put_port:
726be3fe0f9SChen-Yu Tsai 	of_node_put(port);
727be3fe0f9SChen-Yu Tsai 
728be3fe0f9SChen-Yu Tsai 	return engine;
729b317fa3bSChen-Yu Tsai }
730b317fa3bSChen-Yu Tsai 
731e8d5bbf7SChen-Yu Tsai /*
732e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
733e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
734e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
735e8d5bbf7SChen-Yu Tsai  * component. Thus we can look at any one of the input connections of
736e8d5bbf7SChen-Yu Tsai  * the TCONs, and use that connection's remote endpoint ID as our own.
737e8d5bbf7SChen-Yu Tsai  *
738e8d5bbf7SChen-Yu Tsai  * Since the user of this function already finds the input port,
739e8d5bbf7SChen-Yu Tsai  * the port is passed in directly without further checks.
740e8d5bbf7SChen-Yu Tsai  */
741e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
742e8d5bbf7SChen-Yu Tsai {
743e8d5bbf7SChen-Yu Tsai 	struct device_node *ep;
744e8d5bbf7SChen-Yu Tsai 	int ret = -EINVAL;
745e8d5bbf7SChen-Yu Tsai 
746e8d5bbf7SChen-Yu Tsai 	/* try finding an upstream endpoint */
747e8d5bbf7SChen-Yu Tsai 	for_each_available_child_of_node(port, ep) {
748e8d5bbf7SChen-Yu Tsai 		struct device_node *remote;
749e8d5bbf7SChen-Yu Tsai 		u32 reg;
750e8d5bbf7SChen-Yu Tsai 
751e8d5bbf7SChen-Yu Tsai 		remote = of_graph_get_remote_endpoint(ep);
752e8d5bbf7SChen-Yu Tsai 		if (!remote)
753e8d5bbf7SChen-Yu Tsai 			continue;
754e8d5bbf7SChen-Yu Tsai 
755e8d5bbf7SChen-Yu Tsai 		ret = of_property_read_u32(remote, "reg", &reg);
756e8d5bbf7SChen-Yu Tsai 		if (ret)
757e8d5bbf7SChen-Yu Tsai 			continue;
758e8d5bbf7SChen-Yu Tsai 
759e8d5bbf7SChen-Yu Tsai 		ret = reg;
760e8d5bbf7SChen-Yu Tsai 	}
761e8d5bbf7SChen-Yu Tsai 
762e8d5bbf7SChen-Yu Tsai 	return ret;
763e8d5bbf7SChen-Yu Tsai }
764e8d5bbf7SChen-Yu Tsai 
765e8d5bbf7SChen-Yu Tsai /*
766e8d5bbf7SChen-Yu Tsai  * Once we know the TCON's id, we can look through the list of
767e8d5bbf7SChen-Yu Tsai  * engines to find a matching one. We assume all engines have
768e8d5bbf7SChen-Yu Tsai  * been probed and added to the list.
769e8d5bbf7SChen-Yu Tsai  */
770e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
771e8d5bbf7SChen-Yu Tsai 							int id)
772e8d5bbf7SChen-Yu Tsai {
773e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
774e8d5bbf7SChen-Yu Tsai 
775e8d5bbf7SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
776e8d5bbf7SChen-Yu Tsai 		if (engine->id == id)
777e8d5bbf7SChen-Yu Tsai 			return engine;
778e8d5bbf7SChen-Yu Tsai 
779e8d5bbf7SChen-Yu Tsai 	return ERR_PTR(-EINVAL);
780e8d5bbf7SChen-Yu Tsai }
781e8d5bbf7SChen-Yu Tsai 
782e8d5bbf7SChen-Yu Tsai /*
783e8d5bbf7SChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
784e8d5bbf7SChen-Yu Tsai  * we assumed the TCON was always tied to just one backend. However
785e8d5bbf7SChen-Yu Tsai  * this proved not to be the case. On the A31, the TCON can select
786e8d5bbf7SChen-Yu Tsai  * either backend as its source. On the A20 (and likely on the A10),
787e8d5bbf7SChen-Yu Tsai  * the backend can choose which TCON to output to.
788e8d5bbf7SChen-Yu Tsai  *
789e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
790e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
791e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
792e8d5bbf7SChen-Yu Tsai  * component. Thus we should be able to look at any one of the input
793e8d5bbf7SChen-Yu Tsai  * connections of the TCONs, and use that connection's remote endpoint
794e8d5bbf7SChen-Yu Tsai  * ID as our own.
795e8d5bbf7SChen-Yu Tsai  *
796e8d5bbf7SChen-Yu Tsai  * However  the connections between the backend and TCON were assumed
797e8d5bbf7SChen-Yu Tsai  * to be always singular, and their endpoit IDs were all incorrectly
798e8d5bbf7SChen-Yu Tsai  * set to 0. This means for these old device trees, we cannot just look
799e8d5bbf7SChen-Yu Tsai  * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
800e8d5bbf7SChen-Yu Tsai  * incorrectly identified as TCON0.
801e8d5bbf7SChen-Yu Tsai  *
802e8d5bbf7SChen-Yu Tsai  * This function first checks if the TCON node has 2 input endpoints.
803e8d5bbf7SChen-Yu Tsai  * If so, then the device tree is a corrected version, and it will use
804e8d5bbf7SChen-Yu Tsai  * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
805e8d5bbf7SChen-Yu Tsai  * to fetch the ID and engine directly. If not, then it is likely an
806e8d5bbf7SChen-Yu Tsai  * old device trees, where the endpoint IDs were incorrect, but did not
807e8d5bbf7SChen-Yu Tsai  * have endpoint connections between the backend and TCON across
808e8d5bbf7SChen-Yu Tsai  * different display pipelines. It will fall back to the old method of
809e8d5bbf7SChen-Yu Tsai  * traversing the  of_graph to try and find a matching engine by device
810e8d5bbf7SChen-Yu Tsai  * node.
811e8d5bbf7SChen-Yu Tsai  *
812e8d5bbf7SChen-Yu Tsai  * In the case of single display pipeline device trees, either method
813e8d5bbf7SChen-Yu Tsai  * works.
814e8d5bbf7SChen-Yu Tsai  */
815e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
816e8d5bbf7SChen-Yu Tsai 						   struct device_node *node)
817e8d5bbf7SChen-Yu Tsai {
818e8d5bbf7SChen-Yu Tsai 	struct device_node *port;
819e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
820e8d5bbf7SChen-Yu Tsai 
821e8d5bbf7SChen-Yu Tsai 	port = of_graph_get_port_by_id(node, 0);
822e8d5bbf7SChen-Yu Tsai 	if (!port)
823e8d5bbf7SChen-Yu Tsai 		return ERR_PTR(-EINVAL);
824e8d5bbf7SChen-Yu Tsai 
825e8d5bbf7SChen-Yu Tsai 	/*
826e8d5bbf7SChen-Yu Tsai 	 * Is this a corrected device tree with cross pipeline
827e8d5bbf7SChen-Yu Tsai 	 * connections between the backend and TCON?
828e8d5bbf7SChen-Yu Tsai 	 */
829e8d5bbf7SChen-Yu Tsai 	if (of_get_child_count(port) > 1) {
830e8d5bbf7SChen-Yu Tsai 		/* Get our ID directly from an upstream endpoint */
831e8d5bbf7SChen-Yu Tsai 		int id = sun4i_tcon_of_get_id_from_port(port);
832e8d5bbf7SChen-Yu Tsai 
833e8d5bbf7SChen-Yu Tsai 		/* Get our engine by matching our ID */
834e8d5bbf7SChen-Yu Tsai 		engine = sun4i_tcon_get_engine_by_id(drv, id);
835e8d5bbf7SChen-Yu Tsai 
836e8d5bbf7SChen-Yu Tsai 		of_node_put(port);
837e8d5bbf7SChen-Yu Tsai 		return engine;
838e8d5bbf7SChen-Yu Tsai 	}
839e8d5bbf7SChen-Yu Tsai 
840e8d5bbf7SChen-Yu Tsai 	/* Fallback to old method by traversing input endpoints */
841e8d5bbf7SChen-Yu Tsai 	of_node_put(port);
842e8d5bbf7SChen-Yu Tsai 	return sun4i_tcon_find_engine_traverse(drv, node);
843e8d5bbf7SChen-Yu Tsai }
844e8d5bbf7SChen-Yu Tsai 
8459026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master,
8469026e0d1SMaxime Ripard 			   void *data)
8479026e0d1SMaxime Ripard {
8489026e0d1SMaxime Ripard 	struct drm_device *drm = data;
8499026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
85087969338SIcenowy Zheng 	struct sunxi_engine *engine;
851a0c1214eSMaxime Ripard 	struct device_node *remote;
8529026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon;
853a0c1214eSMaxime Ripard 	bool has_lvds_rst, has_lvds_alt, can_lvds;
8549026e0d1SMaxime Ripard 	int ret;
8559026e0d1SMaxime Ripard 
85687969338SIcenowy Zheng 	engine = sun4i_tcon_find_engine(drv, dev->of_node);
85787969338SIcenowy Zheng 	if (IS_ERR(engine)) {
85887969338SIcenowy Zheng 		dev_err(dev, "Couldn't find matching engine\n");
85980a58240SChen-Yu Tsai 		return -EPROBE_DEFER;
860b317fa3bSChen-Yu Tsai 	}
86180a58240SChen-Yu Tsai 
8629026e0d1SMaxime Ripard 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
8639026e0d1SMaxime Ripard 	if (!tcon)
8649026e0d1SMaxime Ripard 		return -ENOMEM;
8659026e0d1SMaxime Ripard 	dev_set_drvdata(dev, tcon);
8669026e0d1SMaxime Ripard 	tcon->drm = drm;
867ae558110SMaxime Ripard 	tcon->dev = dev;
86887969338SIcenowy Zheng 	tcon->id = engine->id;
86991ea2f29SChen-Yu Tsai 	tcon->quirks = of_device_get_match_data(dev);
8709026e0d1SMaxime Ripard 
8719026e0d1SMaxime Ripard 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
8729026e0d1SMaxime Ripard 	if (IS_ERR(tcon->lcd_rst)) {
8739026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
8749026e0d1SMaxime Ripard 		return PTR_ERR(tcon->lcd_rst);
8759026e0d1SMaxime Ripard 	}
8769026e0d1SMaxime Ripard 
8779026e0d1SMaxime Ripard 	/* Make sure our TCON is reset */
878d57294c1SChen-Yu Tsai 	ret = reset_control_reset(tcon->lcd_rst);
8799026e0d1SMaxime Ripard 	if (ret) {
8809026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't deassert our reset line\n");
8819026e0d1SMaxime Ripard 		return ret;
8829026e0d1SMaxime Ripard 	}
8839026e0d1SMaxime Ripard 
884a0c1214eSMaxime Ripard 	/*
885a0c1214eSMaxime Ripard 	 * This can only be made optional since we've had DT nodes
886a0c1214eSMaxime Ripard 	 * without the LVDS reset properties.
887a0c1214eSMaxime Ripard 	 *
888a0c1214eSMaxime Ripard 	 * If the property is missing, just disable LVDS, and print a
889a0c1214eSMaxime Ripard 	 * warning.
890a0c1214eSMaxime Ripard 	 */
891a0c1214eSMaxime Ripard 	tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
892a0c1214eSMaxime Ripard 	if (IS_ERR(tcon->lvds_rst)) {
893a0c1214eSMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
894a0c1214eSMaxime Ripard 		return PTR_ERR(tcon->lvds_rst);
895a0c1214eSMaxime Ripard 	} else if (tcon->lvds_rst) {
896a0c1214eSMaxime Ripard 		has_lvds_rst = true;
897a0c1214eSMaxime Ripard 		reset_control_reset(tcon->lvds_rst);
898a0c1214eSMaxime Ripard 	} else {
899a0c1214eSMaxime Ripard 		has_lvds_rst = false;
900a0c1214eSMaxime Ripard 	}
901a0c1214eSMaxime Ripard 
902a0c1214eSMaxime Ripard 	/*
903a0c1214eSMaxime Ripard 	 * This can only be made optional since we've had DT nodes
904a0c1214eSMaxime Ripard 	 * without the LVDS reset properties.
905a0c1214eSMaxime Ripard 	 *
906a0c1214eSMaxime Ripard 	 * If the property is missing, just disable LVDS, and print a
907a0c1214eSMaxime Ripard 	 * warning.
908a0c1214eSMaxime Ripard 	 */
909a0c1214eSMaxime Ripard 	if (tcon->quirks->has_lvds_alt) {
910a0c1214eSMaxime Ripard 		tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
911a0c1214eSMaxime Ripard 		if (IS_ERR(tcon->lvds_pll)) {
912a0c1214eSMaxime Ripard 			if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
913a0c1214eSMaxime Ripard 				has_lvds_alt = false;
914a0c1214eSMaxime Ripard 			} else {
915a0c1214eSMaxime Ripard 				dev_err(dev, "Couldn't get the LVDS PLL\n");
916a0c1214eSMaxime Ripard 				return PTR_ERR(tcon->lvds_rst);
917a0c1214eSMaxime Ripard 			}
918a0c1214eSMaxime Ripard 		} else {
919a0c1214eSMaxime Ripard 			has_lvds_alt = true;
920a0c1214eSMaxime Ripard 		}
921a0c1214eSMaxime Ripard 	}
922a0c1214eSMaxime Ripard 
923a0c1214eSMaxime Ripard 	if (!has_lvds_rst || (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
924a0c1214eSMaxime Ripard 		dev_warn(dev,
925a0c1214eSMaxime Ripard 			 "Missing LVDS properties, Please upgrade your DT\n");
926a0c1214eSMaxime Ripard 		dev_warn(dev, "LVDS output disabled\n");
927a0c1214eSMaxime Ripard 		can_lvds = false;
928a0c1214eSMaxime Ripard 	} else {
929a0c1214eSMaxime Ripard 		can_lvds = true;
930a0c1214eSMaxime Ripard 	}
931a0c1214eSMaxime Ripard 
9329026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_clocks(dev, tcon);
9339026e0d1SMaxime Ripard 	if (ret) {
9349026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON clocks\n");
9359026e0d1SMaxime Ripard 		goto err_assert_reset;
9369026e0d1SMaxime Ripard 	}
9379026e0d1SMaxime Ripard 
9384c7f16d1SChen-Yu Tsai 	ret = sun4i_tcon_init_regmap(dev, tcon);
9399026e0d1SMaxime Ripard 	if (ret) {
9404c7f16d1SChen-Yu Tsai 		dev_err(dev, "Couldn't init our TCON regmap\n");
9419026e0d1SMaxime Ripard 		goto err_free_clocks;
9429026e0d1SMaxime Ripard 	}
9439026e0d1SMaxime Ripard 
944*34d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
9454c7f16d1SChen-Yu Tsai 		ret = sun4i_dclk_create(dev, tcon);
9464c7f16d1SChen-Yu Tsai 		if (ret) {
9474c7f16d1SChen-Yu Tsai 			dev_err(dev, "Couldn't create our TCON dot clock\n");
9484c7f16d1SChen-Yu Tsai 			goto err_free_clocks;
9494c7f16d1SChen-Yu Tsai 		}
950*34d698f6SJernej Skrabec 	}
9514c7f16d1SChen-Yu Tsai 
9529026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_irq(dev, tcon);
9539026e0d1SMaxime Ripard 	if (ret) {
9549026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON interrupts\n");
9554c7f16d1SChen-Yu Tsai 		goto err_free_dotclock;
9569026e0d1SMaxime Ripard 	}
9579026e0d1SMaxime Ripard 
95887969338SIcenowy Zheng 	tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
95946cce6daSChen-Yu Tsai 	if (IS_ERR(tcon->crtc)) {
96046cce6daSChen-Yu Tsai 		dev_err(dev, "Couldn't create our CRTC\n");
96146cce6daSChen-Yu Tsai 		ret = PTR_ERR(tcon->crtc);
96292411f6dSMaxime Ripard 		goto err_free_dotclock;
96346cce6daSChen-Yu Tsai 	}
96446cce6daSChen-Yu Tsai 
965a0c1214eSMaxime Ripard 	/*
966a0c1214eSMaxime Ripard 	 * If we have an LVDS panel connected to the TCON, we should
967a0c1214eSMaxime Ripard 	 * just probe the LVDS connector. Otherwise, just probe RGB as
968a0c1214eSMaxime Ripard 	 * we used to.
969a0c1214eSMaxime Ripard 	 */
970a0c1214eSMaxime Ripard 	remote = of_graph_get_remote_node(dev->of_node, 1, 0);
971a0c1214eSMaxime Ripard 	if (of_device_is_compatible(remote, "panel-lvds"))
972a0c1214eSMaxime Ripard 		if (can_lvds)
973a0c1214eSMaxime Ripard 			ret = sun4i_lvds_init(drm, tcon);
974a0c1214eSMaxime Ripard 		else
975a0c1214eSMaxime Ripard 			ret = -EINVAL;
976a0c1214eSMaxime Ripard 	else
977b9c8506cSChen-Yu Tsai 		ret = sun4i_rgb_init(drm, tcon);
978a0c1214eSMaxime Ripard 	of_node_put(remote);
979a0c1214eSMaxime Ripard 
98013fef095SChen-Yu Tsai 	if (ret < 0)
98192411f6dSMaxime Ripard 		goto err_free_dotclock;
98213fef095SChen-Yu Tsai 
98327e18de7SChen-Yu Tsai 	if (tcon->quirks->needs_de_be_mux) {
98427e18de7SChen-Yu Tsai 		/*
98527e18de7SChen-Yu Tsai 		 * We assume there is no dynamic muxing of backends
98627e18de7SChen-Yu Tsai 		 * and TCONs, so we select the backend with same ID.
98727e18de7SChen-Yu Tsai 		 *
98827e18de7SChen-Yu Tsai 		 * While dynamic selection might be interesting, since
98927e18de7SChen-Yu Tsai 		 * the CRTC is tied to the TCON, while the layers are
99027e18de7SChen-Yu Tsai 		 * tied to the backends, this means, we will need to
99127e18de7SChen-Yu Tsai 		 * switch between groups of layers. There might not be
99227e18de7SChen-Yu Tsai 		 * a way to represent this constraint in DRM.
99327e18de7SChen-Yu Tsai 		 */
99427e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
99527e18de7SChen-Yu Tsai 				   SUN4I_TCON0_CTL_SRC_SEL_MASK,
99627e18de7SChen-Yu Tsai 				   tcon->id);
99727e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
99827e18de7SChen-Yu Tsai 				   SUN4I_TCON1_CTL_SRC_SEL_MASK,
99927e18de7SChen-Yu Tsai 				   tcon->id);
100027e18de7SChen-Yu Tsai 	}
100127e18de7SChen-Yu Tsai 
100280a58240SChen-Yu Tsai 	list_add_tail(&tcon->list, &drv->tcon_list);
100380a58240SChen-Yu Tsai 
100413fef095SChen-Yu Tsai 	return 0;
10059026e0d1SMaxime Ripard 
10064c7f16d1SChen-Yu Tsai err_free_dotclock:
1007*34d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
10084c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
10099026e0d1SMaxime Ripard err_free_clocks:
10109026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
10119026e0d1SMaxime Ripard err_assert_reset:
10129026e0d1SMaxime Ripard 	reset_control_assert(tcon->lcd_rst);
10139026e0d1SMaxime Ripard 	return ret;
10149026e0d1SMaxime Ripard }
10159026e0d1SMaxime Ripard 
10169026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master,
10179026e0d1SMaxime Ripard 			      void *data)
10189026e0d1SMaxime Ripard {
10199026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
10209026e0d1SMaxime Ripard 
102180a58240SChen-Yu Tsai 	list_del(&tcon->list);
1022*34d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
10234c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
10249026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
10259026e0d1SMaxime Ripard }
10269026e0d1SMaxime Ripard 
1027dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = {
10289026e0d1SMaxime Ripard 	.bind	= sun4i_tcon_bind,
10299026e0d1SMaxime Ripard 	.unbind	= sun4i_tcon_unbind,
10309026e0d1SMaxime Ripard };
10319026e0d1SMaxime Ripard 
10329026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev)
10339026e0d1SMaxime Ripard {
103429e57fabSMaxime Ripard 	struct device_node *node = pdev->dev.of_node;
1035894f5a9fSMaxime Ripard 	struct drm_bridge *bridge;
103629e57fabSMaxime Ripard 	struct drm_panel *panel;
1037ebc94461SRob Herring 	int ret;
103829e57fabSMaxime Ripard 
1039ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1040ebc94461SRob Herring 	if (ret == -EPROBE_DEFER)
1041ebc94461SRob Herring 		return ret;
104229e57fabSMaxime Ripard 
10439026e0d1SMaxime Ripard 	return component_add(&pdev->dev, &sun4i_tcon_ops);
10449026e0d1SMaxime Ripard }
10459026e0d1SMaxime Ripard 
10469026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev)
10479026e0d1SMaxime Ripard {
10489026e0d1SMaxime Ripard 	component_del(&pdev->dev, &sun4i_tcon_ops);
10499026e0d1SMaxime Ripard 
10509026e0d1SMaxime Ripard 	return 0;
10519026e0d1SMaxime Ripard }
10529026e0d1SMaxime Ripard 
1053ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */
10544bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
10554bb206bfSJonathan Liu 				  const struct drm_encoder *encoder)
10564bb206bfSJonathan Liu {
10574bb206bfSJonathan Liu 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
10584bb206bfSJonathan Liu 	u32 shift;
10594bb206bfSJonathan Liu 
10604bb206bfSJonathan Liu 	if (!tcon0)
10614bb206bfSJonathan Liu 		return -EINVAL;
10624bb206bfSJonathan Liu 
10634bb206bfSJonathan Liu 	switch (encoder->encoder_type) {
10644bb206bfSJonathan Liu 	case DRM_MODE_ENCODER_TMDS:
10654bb206bfSJonathan Liu 		/* HDMI */
10664bb206bfSJonathan Liu 		shift = 8;
10674bb206bfSJonathan Liu 		break;
10684bb206bfSJonathan Liu 	default:
10694bb206bfSJonathan Liu 		return -EINVAL;
10704bb206bfSJonathan Liu 	}
10714bb206bfSJonathan Liu 
10724bb206bfSJonathan Liu 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
10734bb206bfSJonathan Liu 			   0x3 << shift, tcon->id << shift);
10744bb206bfSJonathan Liu 
10754bb206bfSJonathan Liu 	return 0;
10764bb206bfSJonathan Liu }
10774bb206bfSJonathan Liu 
1078ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
1079abcb8766SMaxime Ripard 				  const struct drm_encoder *encoder)
1080ad537fb2SChen-Yu Tsai {
1081ad537fb2SChen-Yu Tsai 	u32 val;
1082ad537fb2SChen-Yu Tsai 
1083ad537fb2SChen-Yu Tsai 	if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1084ad537fb2SChen-Yu Tsai 		val = 1;
1085ad537fb2SChen-Yu Tsai 	else
1086ad537fb2SChen-Yu Tsai 		val = 0;
1087ad537fb2SChen-Yu Tsai 
1088ad537fb2SChen-Yu Tsai 	/*
1089ad537fb2SChen-Yu Tsai 	 * FIXME: Undocumented bits
1090ad537fb2SChen-Yu Tsai 	 */
1091ad537fb2SChen-Yu Tsai 	return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1092ad537fb2SChen-Yu Tsai }
1093ad537fb2SChen-Yu Tsai 
109467e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
1095abcb8766SMaxime Ripard 			      const struct drm_encoder *encoder)
109667e32645SChen-Yu Tsai {
109767e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
109867e32645SChen-Yu Tsai 	u32 shift;
109967e32645SChen-Yu Tsai 
110067e32645SChen-Yu Tsai 	if (!tcon0)
110167e32645SChen-Yu Tsai 		return -EINVAL;
110267e32645SChen-Yu Tsai 
110367e32645SChen-Yu Tsai 	switch (encoder->encoder_type) {
110467e32645SChen-Yu Tsai 	case DRM_MODE_ENCODER_TMDS:
110567e32645SChen-Yu Tsai 		/* HDMI */
110667e32645SChen-Yu Tsai 		shift = 8;
110767e32645SChen-Yu Tsai 		break;
110867e32645SChen-Yu Tsai 	default:
110967e32645SChen-Yu Tsai 		/* TODO A31 has MIPI DSI but A31s does not */
111067e32645SChen-Yu Tsai 		return -EINVAL;
111167e32645SChen-Yu Tsai 	}
111267e32645SChen-Yu Tsai 
111367e32645SChen-Yu Tsai 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
111467e32645SChen-Yu Tsai 			   0x3 << shift, tcon->id << shift);
111567e32645SChen-Yu Tsai 
111667e32645SChen-Yu Tsai 	return 0;
111767e32645SChen-Yu Tsai }
111867e32645SChen-Yu Tsai 
11194bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
1120*34d698f6SJernej Skrabec 	.has_channel_0		= true,
11214bb206bfSJonathan Liu 	.has_channel_1		= true,
11224bb206bfSJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
11234bb206bfSJonathan Liu };
11244bb206bfSJonathan Liu 
112591ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
1126*34d698f6SJernej Skrabec 	.has_channel_0		= true,
112791ea2f29SChen-Yu Tsai 	.has_channel_1		= true,
1128ad537fb2SChen-Yu Tsai 	.set_mux		= sun5i_a13_tcon_set_mux,
112991ea2f29SChen-Yu Tsai };
113091ea2f29SChen-Yu Tsai 
113193a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
1132*34d698f6SJernej Skrabec 	.has_channel_0		= true,
113393a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
1134a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
113527e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
113667e32645SChen-Yu Tsai 	.set_mux		= sun6i_tcon_set_mux,
113793a5ec14SChen-Yu Tsai };
113893a5ec14SChen-Yu Tsai 
113993a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
1140*34d698f6SJernej Skrabec 	.has_channel_0		= true,
114193a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
114227e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
114393a5ec14SChen-Yu Tsai };
114493a5ec14SChen-Yu Tsai 
1145aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
1146*34d698f6SJernej Skrabec 	.has_channel_0		= true,
1147aaddb6d2SJonathan Liu 	.has_channel_1		= true,
1148aaddb6d2SJonathan Liu 	/* Same display pipeline structure as A10 */
1149aaddb6d2SJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
1150aaddb6d2SJonathan Liu };
1151aaddb6d2SJonathan Liu 
115291ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
1153*34d698f6SJernej Skrabec 	.has_channel_0		= true,
1154a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
115591ea2f29SChen-Yu Tsai };
115691ea2f29SChen-Yu Tsai 
11572f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
1158*34d698f6SJernej Skrabec 	.has_channel_0		= true,
11592f0d7bb1SMaxime Ripard };
11602f0d7bb1SMaxime Ripard 
11611a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
1162*34d698f6SJernej Skrabec 	.has_channel_0		= true,
11631a0edb3fSIcenowy Zheng };
11641a0edb3fSIcenowy Zheng 
1165ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */
1166ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = {
11674bb206bfSJonathan Liu 	{ .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
116891ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
116993a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
117093a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
1171aaddb6d2SJonathan Liu 	{ .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
117291ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
11732f0d7bb1SMaxime Ripard 	{ .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
11741a0edb3fSIcenowy Zheng 	{ .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
11759026e0d1SMaxime Ripard 	{ }
11769026e0d1SMaxime Ripard };
11779026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
1178ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table);
11799026e0d1SMaxime Ripard 
11809026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = {
11819026e0d1SMaxime Ripard 	.probe		= sun4i_tcon_probe,
11829026e0d1SMaxime Ripard 	.remove		= sun4i_tcon_remove,
11839026e0d1SMaxime Ripard 	.driver		= {
11849026e0d1SMaxime Ripard 		.name		= "sun4i-tcon",
11859026e0d1SMaxime Ripard 		.of_match_table	= sun4i_tcon_of_table,
11869026e0d1SMaxime Ripard 	},
11879026e0d1SMaxime Ripard };
11889026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver);
11899026e0d1SMaxime Ripard 
11909026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
11919026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
11929026e0d1SMaxime Ripard MODULE_LICENSE("GPL");
1193