xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision 63d6310f6cc63735f08b86b3c8390b9f066a2221)
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>
202c17a436SGiulio Benetti #include <drm/drm_panel.h>
219026e0d1SMaxime Ripard 
22ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h>
23ad537fb2SChen-Yu Tsai 
249026e0d1SMaxime Ripard #include <linux/component.h>
259026e0d1SMaxime Ripard #include <linux/ioport.h>
269026e0d1SMaxime Ripard #include <linux/of_address.h>
2791ea2f29SChen-Yu Tsai #include <linux/of_device.h>
289026e0d1SMaxime Ripard #include <linux/of_irq.h>
299026e0d1SMaxime Ripard #include <linux/regmap.h>
309026e0d1SMaxime Ripard #include <linux/reset.h>
319026e0d1SMaxime Ripard 
329026e0d1SMaxime Ripard #include "sun4i_crtc.h"
339026e0d1SMaxime Ripard #include "sun4i_dotclock.h"
349026e0d1SMaxime Ripard #include "sun4i_drv.h"
35a0c1214eSMaxime Ripard #include "sun4i_lvds.h"
3629e57fabSMaxime Ripard #include "sun4i_rgb.h"
379026e0d1SMaxime Ripard #include "sun4i_tcon.h"
38a08fc7c8SMaxime Ripard #include "sun6i_mipi_dsi.h"
3987969338SIcenowy Zheng #include "sunxi_engine.h"
409026e0d1SMaxime Ripard 
41a0c1214eSMaxime Ripard static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder)
42a0c1214eSMaxime Ripard {
43a0c1214eSMaxime Ripard 	struct drm_connector *connector;
44a0c1214eSMaxime Ripard 	struct drm_connector_list_iter iter;
45a0c1214eSMaxime Ripard 
46a0c1214eSMaxime Ripard 	drm_connector_list_iter_begin(encoder->dev, &iter);
47a0c1214eSMaxime Ripard 	drm_for_each_connector_iter(connector, &iter)
48a0c1214eSMaxime Ripard 		if (connector->encoder == encoder) {
49a0c1214eSMaxime Ripard 			drm_connector_list_iter_end(&iter);
50a0c1214eSMaxime Ripard 			return connector;
51a0c1214eSMaxime Ripard 		}
52a0c1214eSMaxime Ripard 	drm_connector_list_iter_end(&iter);
53a0c1214eSMaxime Ripard 
54a0c1214eSMaxime Ripard 	return NULL;
55a0c1214eSMaxime Ripard }
56a0c1214eSMaxime Ripard 
57a0c1214eSMaxime Ripard static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder)
58a0c1214eSMaxime Ripard {
59a0c1214eSMaxime Ripard 	struct drm_connector *connector;
60a0c1214eSMaxime Ripard 	struct drm_display_info *info;
61a0c1214eSMaxime Ripard 
62a0c1214eSMaxime Ripard 	connector = sun4i_tcon_get_connector(encoder);
63a0c1214eSMaxime Ripard 	if (!connector)
64a0c1214eSMaxime Ripard 		return -EINVAL;
65a0c1214eSMaxime Ripard 
66a0c1214eSMaxime Ripard 	info = &connector->display_info;
67a0c1214eSMaxime Ripard 	if (info->num_bus_formats != 1)
68a0c1214eSMaxime Ripard 		return -EINVAL;
69a0c1214eSMaxime Ripard 
70a0c1214eSMaxime Ripard 	switch (info->bus_formats[0]) {
71a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
72a0c1214eSMaxime Ripard 		return 18;
73a0c1214eSMaxime Ripard 
74a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
75a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
76a0c1214eSMaxime Ripard 		return 24;
77a0c1214eSMaxime Ripard 	}
78a0c1214eSMaxime Ripard 
79a0c1214eSMaxime Ripard 	return -EINVAL;
80a0c1214eSMaxime Ripard }
81a0c1214eSMaxime Ripard 
8245e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
8345e88f99SMaxime Ripard 					  bool enabled)
849026e0d1SMaxime Ripard {
8545e88f99SMaxime Ripard 	struct clk *clk;
869026e0d1SMaxime Ripard 
8745e88f99SMaxime Ripard 	switch (channel) {
8845e88f99SMaxime Ripard 	case 0:
8934d698f6SJernej Skrabec 		WARN_ON(!tcon->quirks->has_channel_0);
909026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
919026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE,
9245e88f99SMaxime Ripard 				   enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
9345e88f99SMaxime Ripard 		clk = tcon->dclk;
9445e88f99SMaxime Ripard 		break;
9545e88f99SMaxime Ripard 	case 1:
9691ea2f29SChen-Yu Tsai 		WARN_ON(!tcon->quirks->has_channel_1);
979026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
989026e0d1SMaxime Ripard 				   SUN4I_TCON1_CTL_TCON_ENABLE,
9945e88f99SMaxime Ripard 				   enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
10045e88f99SMaxime Ripard 		clk = tcon->sclk1;
10145e88f99SMaxime Ripard 		break;
10245e88f99SMaxime Ripard 	default:
10345e88f99SMaxime Ripard 		DRM_WARN("Unknown channel... doing nothing\n");
10445e88f99SMaxime Ripard 		return;
1059026e0d1SMaxime Ripard 	}
10645e88f99SMaxime Ripard 
107f3e5feebSJernej Skrabec 	if (enabled) {
10845e88f99SMaxime Ripard 		clk_prepare_enable(clk);
1097035046dSOndrej Jirman 		clk_rate_exclusive_get(clk);
110f3e5feebSJernej Skrabec 	} else {
111f3e5feebSJernej Skrabec 		clk_rate_exclusive_put(clk);
11245e88f99SMaxime Ripard 		clk_disable_unprepare(clk);
11345e88f99SMaxime Ripard 	}
114f3e5feebSJernej Skrabec }
11545e88f99SMaxime Ripard 
116a0c1214eSMaxime Ripard static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon,
117a0c1214eSMaxime Ripard 				       const struct drm_encoder *encoder,
118a0c1214eSMaxime Ripard 				       bool enabled)
119a0c1214eSMaxime Ripard {
120a0c1214eSMaxime Ripard 	if (enabled) {
121a0c1214eSMaxime Ripard 		u8 val;
122a0c1214eSMaxime Ripard 
123a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
124a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN,
125a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN);
126a0c1214eSMaxime Ripard 
127a0c1214eSMaxime Ripard 		/*
128a0c1214eSMaxime Ripard 		 * As their name suggest, these values only apply to the A31
129a0c1214eSMaxime Ripard 		 * and later SoCs. We'll have to rework this when merging
130a0c1214eSMaxime Ripard 		 * support for the older SoCs.
131a0c1214eSMaxime Ripard 		 */
132a0c1214eSMaxime Ripard 		regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
133a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_C(2) |
134a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_V(3) |
135a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_PD(2) |
136a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_EN_LDO);
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_MB,
141a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_MB);
142a0c1214eSMaxime Ripard 		udelay(2);
143a0c1214eSMaxime Ripard 
144a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
145a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC,
146a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC);
147a0c1214eSMaxime Ripard 
148a0c1214eSMaxime Ripard 		if (sun4i_tcon_get_pixel_depth(encoder) == 18)
149a0c1214eSMaxime Ripard 			val = 7;
150a0c1214eSMaxime Ripard 		else
151a0c1214eSMaxime Ripard 			val = 0xf;
152a0c1214eSMaxime Ripard 
153a0c1214eSMaxime Ripard 		regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
154a0c1214eSMaxime Ripard 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf),
155a0c1214eSMaxime Ripard 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val));
156a0c1214eSMaxime Ripard 	} else {
157a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
158a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN, 0);
159a0c1214eSMaxime Ripard 	}
160a0c1214eSMaxime Ripard }
161a0c1214eSMaxime Ripard 
16245e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
16345e88f99SMaxime Ripard 			   const struct drm_encoder *encoder,
16445e88f99SMaxime Ripard 			   bool enabled)
16545e88f99SMaxime Ripard {
166a0c1214eSMaxime Ripard 	bool is_lvds = false;
16745e88f99SMaxime Ripard 	int channel;
16845e88f99SMaxime Ripard 
16945e88f99SMaxime Ripard 	switch (encoder->encoder_type) {
170a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
171a0c1214eSMaxime Ripard 		is_lvds = true;
172a0c1214eSMaxime Ripard 		/* Fallthrough */
173a08fc7c8SMaxime Ripard 	case DRM_MODE_ENCODER_DSI:
17445e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
17545e88f99SMaxime Ripard 		channel = 0;
17645e88f99SMaxime Ripard 		break;
17745e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
17845e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
17945e88f99SMaxime Ripard 		channel = 1;
18045e88f99SMaxime Ripard 		break;
18145e88f99SMaxime Ripard 	default:
18245e88f99SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
18345e88f99SMaxime Ripard 		return;
18445e88f99SMaxime Ripard 	}
18545e88f99SMaxime Ripard 
186a0c1214eSMaxime Ripard 	if (is_lvds && !enabled)
187a0c1214eSMaxime Ripard 		sun4i_tcon_lvds_set_status(tcon, encoder, false);
188a0c1214eSMaxime Ripard 
18945e88f99SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
19045e88f99SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE,
19145e88f99SMaxime Ripard 			   enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
19245e88f99SMaxime Ripard 
193a0c1214eSMaxime Ripard 	if (is_lvds && enabled)
194a0c1214eSMaxime Ripard 		sun4i_tcon_lvds_set_status(tcon, encoder, true);
195a0c1214eSMaxime Ripard 
19645e88f99SMaxime Ripard 	sun4i_tcon_channel_set_status(tcon, channel, enabled);
19745e88f99SMaxime Ripard }
1989026e0d1SMaxime Ripard 
1999026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
2009026e0d1SMaxime Ripard {
2019026e0d1SMaxime Ripard 	u32 mask, val = 0;
2029026e0d1SMaxime Ripard 
2039026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
2049026e0d1SMaxime Ripard 
2059026e0d1SMaxime Ripard 	mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
206a493ceaeSMaxime Ripard 		SUN4I_TCON_GINT0_VBLANK_ENABLE(1) |
207a493ceaeSMaxime Ripard 		SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE;
2089026e0d1SMaxime Ripard 
2099026e0d1SMaxime Ripard 	if (enable)
2109026e0d1SMaxime Ripard 		val = mask;
2119026e0d1SMaxime Ripard 
2129026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
2139026e0d1SMaxime Ripard }
2149026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
2159026e0d1SMaxime Ripard 
21667e32645SChen-Yu Tsai /*
21767e32645SChen-Yu Tsai  * This function is a helper for TCON output muxing. The TCON output
21867e32645SChen-Yu Tsai  * muxing control register in earlier SoCs (without the TCON TOP block)
21967e32645SChen-Yu Tsai  * are located in TCON0. This helper returns a pointer to TCON0's
22067e32645SChen-Yu Tsai  * sun4i_tcon structure, or NULL if not found.
22167e32645SChen-Yu Tsai  */
22267e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
22367e32645SChen-Yu Tsai {
22467e32645SChen-Yu Tsai 	struct sun4i_drv *drv = drm->dev_private;
22567e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon;
22667e32645SChen-Yu Tsai 
22767e32645SChen-Yu Tsai 	list_for_each_entry(tcon, &drv->tcon_list, list)
22867e32645SChen-Yu Tsai 		if (tcon->id == 0)
22967e32645SChen-Yu Tsai 			return tcon;
23067e32645SChen-Yu Tsai 
23167e32645SChen-Yu Tsai 	dev_warn(drm->dev,
23267e32645SChen-Yu Tsai 		 "TCON0 not found, display output muxing may not work\n");
23367e32645SChen-Yu Tsai 
23467e32645SChen-Yu Tsai 	return NULL;
23567e32645SChen-Yu Tsai }
23667e32645SChen-Yu Tsai 
237f8c73f4fSMaxime Ripard void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
238abcb8766SMaxime Ripard 			const struct drm_encoder *encoder)
239f8c73f4fSMaxime Ripard {
240ad537fb2SChen-Yu Tsai 	int ret = -ENOTSUPP;
241b7cb9b91SMaxime Ripard 
242ad537fb2SChen-Yu Tsai 	if (tcon->quirks->set_mux)
243ad537fb2SChen-Yu Tsai 		ret = tcon->quirks->set_mux(tcon, encoder);
244f8c73f4fSMaxime Ripard 
245ad537fb2SChen-Yu Tsai 	DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
246ad537fb2SChen-Yu Tsai 			 encoder->name, encoder->crtc->name, ret);
247f8c73f4fSMaxime Ripard }
248f8c73f4fSMaxime Ripard 
249961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
2509026e0d1SMaxime Ripard 				    int channel)
2519026e0d1SMaxime Ripard {
2529026e0d1SMaxime Ripard 	int delay = mode->vtotal - mode->vdisplay;
2539026e0d1SMaxime Ripard 
2549026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2559026e0d1SMaxime Ripard 		delay /= 2;
2569026e0d1SMaxime Ripard 
2579026e0d1SMaxime Ripard 	if (channel == 1)
2589026e0d1SMaxime Ripard 		delay -= 2;
2599026e0d1SMaxime Ripard 
2609026e0d1SMaxime Ripard 	delay = min(delay, 30);
2619026e0d1SMaxime Ripard 
2629026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
2639026e0d1SMaxime Ripard 
2649026e0d1SMaxime Ripard 	return delay;
2659026e0d1SMaxime Ripard }
2669026e0d1SMaxime Ripard 
267ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon,
268ba19c537SMaxime Ripard 					const struct drm_display_mode *mode)
269ba19c537SMaxime Ripard {
270ba19c537SMaxime Ripard 	/* Configure the dot clock */
271ba19c537SMaxime Ripard 	clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
272ba19c537SMaxime Ripard 
273ba19c537SMaxime Ripard 	/* Set the resolution */
274ba19c537SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
275ba19c537SMaxime Ripard 		     SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
276ba19c537SMaxime Ripard 		     SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
277ba19c537SMaxime Ripard }
278ba19c537SMaxime Ripard 
279a08fc7c8SMaxime Ripard static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon,
280a08fc7c8SMaxime Ripard 				     struct mipi_dsi_device *device,
281a08fc7c8SMaxime Ripard 				     const struct drm_display_mode *mode)
282a08fc7c8SMaxime Ripard {
283a08fc7c8SMaxime Ripard 	u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format);
284a08fc7c8SMaxime Ripard 	u8 lanes = device->lanes;
285a08fc7c8SMaxime Ripard 	u32 block_space, start_delay;
286a08fc7c8SMaxime Ripard 	u32 tcon_div;
287a08fc7c8SMaxime Ripard 
288a08fc7c8SMaxime Ripard 	tcon->dclk_min_div = 4;
289a08fc7c8SMaxime Ripard 	tcon->dclk_max_div = 127;
290a08fc7c8SMaxime Ripard 
291a08fc7c8SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
292a08fc7c8SMaxime Ripard 
293a08fc7c8SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
294a08fc7c8SMaxime Ripard 			   SUN4I_TCON0_CTL_IF_MASK,
295a08fc7c8SMaxime Ripard 			   SUN4I_TCON0_CTL_IF_8080);
296a08fc7c8SMaxime Ripard 
297a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG,
298a08fc7c8SMaxime Ripard 		     SUN4I_TCON_ECC_FIFO_EN);
299a08fc7c8SMaxime Ripard 
300a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG,
301a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_MODE_DSI |
302a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH |
303a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_EN |
304a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_EN);
305a08fc7c8SMaxime Ripard 
306a08fc7c8SMaxime Ripard 	/*
307a08fc7c8SMaxime Ripard 	 * This looks suspicious, but it works...
308a08fc7c8SMaxime Ripard 	 *
309a08fc7c8SMaxime Ripard 	 * The datasheet says that this should be set higher than 20 *
310a08fc7c8SMaxime Ripard 	 * pixel cycle, but it's not clear what a pixel cycle is.
311a08fc7c8SMaxime Ripard 	 */
312a08fc7c8SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div);
313a08fc7c8SMaxime Ripard 	tcon_div &= GENMASK(6, 0);
314a08fc7c8SMaxime Ripard 	block_space = mode->htotal * bpp / (tcon_div * lanes);
315a08fc7c8SMaxime Ripard 	block_space -= mode->hdisplay + 40;
316a08fc7c8SMaxime Ripard 
317a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG,
318a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) |
319a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay));
320a08fc7c8SMaxime Ripard 
321a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG,
322a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay));
323a08fc7c8SMaxime Ripard 
324a08fc7c8SMaxime Ripard 	start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1);
325a08fc7c8SMaxime Ripard 	start_delay = start_delay * mode->crtc_htotal * 149;
326a08fc7c8SMaxime Ripard 	start_delay = start_delay / (mode->crtc_clock / 1000) / 8;
327a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG,
328a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) |
329a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay));
330a08fc7c8SMaxime Ripard 
331a08fc7c8SMaxime Ripard 	/*
332a08fc7c8SMaxime Ripard 	 * The Allwinner BSP has a comment that the period should be
333a08fc7c8SMaxime Ripard 	 * the display clock * 15, but uses an hardcoded 3000...
334a08fc7c8SMaxime Ripard 	 */
335a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG,
336a08fc7c8SMaxime Ripard 		     SUN4I_TCON_SAFE_PERIOD_NUM(3000) |
337a08fc7c8SMaxime Ripard 		     SUN4I_TCON_SAFE_PERIOD_MODE(3));
338a08fc7c8SMaxime Ripard 
339a08fc7c8SMaxime Ripard 	/* Enable the output on the pins */
340a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG,
341a08fc7c8SMaxime Ripard 		     0xe0000000);
342a08fc7c8SMaxime Ripard }
343a08fc7c8SMaxime Ripard 
344a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
345a0c1214eSMaxime Ripard 				      const struct drm_encoder *encoder,
346a0c1214eSMaxime Ripard 				      const struct drm_display_mode *mode)
347a0c1214eSMaxime Ripard {
348a0c1214eSMaxime Ripard 	unsigned int bp;
349a0c1214eSMaxime Ripard 	u8 clk_delay;
350a0c1214eSMaxime Ripard 	u32 reg, val = 0;
351a0c1214eSMaxime Ripard 
35234d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
35334d698f6SJernej Skrabec 
354a0c1214eSMaxime Ripard 	tcon->dclk_min_div = 7;
355a0c1214eSMaxime Ripard 	tcon->dclk_max_div = 7;
356a0c1214eSMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
357a0c1214eSMaxime Ripard 
358a0c1214eSMaxime Ripard 	/* Adjust clock delay */
359a0c1214eSMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
360a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
361a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
362a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
363a0c1214eSMaxime Ripard 
364a0c1214eSMaxime Ripard 	/*
365a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
366a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
367a0c1214eSMaxime Ripard 	 */
368a0c1214eSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
369a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
370a0c1214eSMaxime Ripard 			 mode->crtc_htotal, bp);
371a0c1214eSMaxime Ripard 
372a0c1214eSMaxime Ripard 	/* Set horizontal display timings */
373a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
374a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
375a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
376a0c1214eSMaxime Ripard 
377a0c1214eSMaxime Ripard 	/*
378a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
379a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
380a0c1214eSMaxime Ripard 	 */
381a0c1214eSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
382a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
383a0c1214eSMaxime Ripard 			 mode->crtc_vtotal, bp);
384a0c1214eSMaxime Ripard 
385a0c1214eSMaxime Ripard 	/* Set vertical display timings */
386a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
387a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
388a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
389a0c1214eSMaxime Ripard 
390a0c1214eSMaxime Ripard 	reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
391a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
392a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
393a0c1214eSMaxime Ripard 	if (sun4i_tcon_get_pixel_depth(encoder) == 24)
394a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
395a0c1214eSMaxime Ripard 	else
396a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
397a0c1214eSMaxime Ripard 
398a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
399a0c1214eSMaxime Ripard 
400a0c1214eSMaxime Ripard 	/* Setup the polarity of the various signals */
401a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
402a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
403a0c1214eSMaxime Ripard 
404a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
405a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
406a0c1214eSMaxime Ripard 
407a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
408a0c1214eSMaxime Ripard 
409a0c1214eSMaxime Ripard 	/* Map output pins to channel 0 */
410a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
411a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
412a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
41380b79e31SOndrej Jirman 
41480b79e31SOndrej Jirman 	/* Enable the output on the pins */
41580b79e31SOndrej Jirman 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000);
416a0c1214eSMaxime Ripard }
417a0c1214eSMaxime Ripard 
418ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
4195b8f0910SMaxime Ripard 				     const struct drm_display_mode *mode)
4209026e0d1SMaxime Ripard {
4212c17a436SGiulio Benetti 	struct drm_panel *panel = tcon->panel;
4222c17a436SGiulio Benetti 	struct drm_connector *connector = panel->connector;
4232c17a436SGiulio Benetti 	struct drm_display_info display_info = connector->display_info;
4249026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
4259026e0d1SMaxime Ripard 	u8 clk_delay;
4269026e0d1SMaxime Ripard 	u32 val = 0;
4279026e0d1SMaxime Ripard 
42834d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
42934d698f6SJernej Skrabec 
430ec08d596SMaxime Ripard 	tcon->dclk_min_div = 6;
431ec08d596SMaxime Ripard 	tcon->dclk_max_div = 127;
432ba19c537SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
43386cf6788SChen-Yu Tsai 
4349026e0d1SMaxime Ripard 	/* Adjust clock delay */
4359026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
4369026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
4379026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
4389026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
4399026e0d1SMaxime Ripard 
4409026e0d1SMaxime Ripard 	/*
4419026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
44223a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
4439026e0d1SMaxime Ripard 	 */
4449026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
4459026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
4469026e0d1SMaxime Ripard 			 mode->crtc_htotal, bp);
4479026e0d1SMaxime Ripard 
4489026e0d1SMaxime Ripard 	/* Set horizontal display timings */
4499026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
4509026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
4519026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
4529026e0d1SMaxime Ripard 
4539026e0d1SMaxime Ripard 	/*
4549026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
45523a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
4569026e0d1SMaxime Ripard 	 */
4579026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
4589026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
4599026e0d1SMaxime Ripard 			 mode->crtc_vtotal, bp);
4609026e0d1SMaxime Ripard 
4619026e0d1SMaxime Ripard 	/* Set vertical display timings */
4629026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
463a88cbbd4SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
4649026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
4659026e0d1SMaxime Ripard 
4669026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
4679026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
4689026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
4699026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
4709026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
4719026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
4729026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
4739026e0d1SMaxime Ripard 
4749026e0d1SMaxime Ripard 	/* Setup the polarity of the various signals */
475fa4127c5SGiulio Benetti 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
4769026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
4779026e0d1SMaxime Ripard 
478fa4127c5SGiulio Benetti 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
4799026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
4809026e0d1SMaxime Ripard 
4812c17a436SGiulio Benetti 	/*
4822c17a436SGiulio Benetti 	 * On A20 and similar SoCs, the only way to achieve Positive Edge
4832c17a436SGiulio Benetti 	 * (Rising Edge), is setting dclk clock phase to 2/3(240°).
4842c17a436SGiulio Benetti 	 * By default TCON works in Negative Edge(Falling Edge),
4852c17a436SGiulio Benetti 	 * this is why phase is set to 0 in that case.
4862c17a436SGiulio Benetti 	 * Unfortunately there's no way to logically invert dclk through
4872c17a436SGiulio Benetti 	 * IO_POL register.
4882c17a436SGiulio Benetti 	 * The only acceptable way to work, triple checked with scope,
4892c17a436SGiulio Benetti 	 * is using clock phase set to 0° for Negative Edge and set to 240°
4902c17a436SGiulio Benetti 	 * for Positive Edge.
4912c17a436SGiulio Benetti 	 * On A33 and similar SoCs there would be a 90° phase option,
4922c17a436SGiulio Benetti 	 * but it divides also dclk by 2.
4932c17a436SGiulio Benetti 	 * Following code is a way to avoid quirks all around TCON
4942c17a436SGiulio Benetti 	 * and DOTCLOCK drivers.
4952c17a436SGiulio Benetti 	 */
4962c17a436SGiulio Benetti 	if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
4972c17a436SGiulio Benetti 		clk_set_phase(tcon->dclk, 240);
4982c17a436SGiulio Benetti 
4992c17a436SGiulio Benetti 	if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
5002c17a436SGiulio Benetti 		clk_set_phase(tcon->dclk, 0);
5012c17a436SGiulio Benetti 
5029026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
5039026e0d1SMaxime Ripard 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
5049026e0d1SMaxime Ripard 			   val);
5059026e0d1SMaxime Ripard 
5069026e0d1SMaxime Ripard 	/* Map output pins to channel 0 */
5079026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
5089026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
5099026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
5109026e0d1SMaxime Ripard 
5119026e0d1SMaxime Ripard 	/* Enable the output on the pins */
5129026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
5139026e0d1SMaxime Ripard }
5149026e0d1SMaxime Ripard 
5155b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
5165b8f0910SMaxime Ripard 				 const struct drm_display_mode *mode)
5179026e0d1SMaxime Ripard {
518b8317a3dSMaxime Ripard 	unsigned int bp, hsync, vsync, vtotal;
5199026e0d1SMaxime Ripard 	u8 clk_delay;
5209026e0d1SMaxime Ripard 	u32 val;
5219026e0d1SMaxime Ripard 
52291ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
5238e924047SMaxime Ripard 
52486cf6788SChen-Yu Tsai 	/* Configure the dot clock */
52586cf6788SChen-Yu Tsai 	clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
52686cf6788SChen-Yu Tsai 
5279026e0d1SMaxime Ripard 	/* Adjust clock delay */
5289026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
5299026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
5309026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
5319026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
5329026e0d1SMaxime Ripard 
5339026e0d1SMaxime Ripard 	/* Set interlaced mode */
5349026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5359026e0d1SMaxime Ripard 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
5369026e0d1SMaxime Ripard 	else
5379026e0d1SMaxime Ripard 		val = 0;
5389026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
5399026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
5409026e0d1SMaxime Ripard 			   val);
5419026e0d1SMaxime Ripard 
5429026e0d1SMaxime Ripard 	/* Set the input resolution */
5439026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
5449026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
5459026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
5469026e0d1SMaxime Ripard 
5479026e0d1SMaxime Ripard 	/* Set the upscaling resolution */
5489026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
5499026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
5509026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
5519026e0d1SMaxime Ripard 
5529026e0d1SMaxime Ripard 	/* Set the output resolution */
5539026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
5549026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
5559026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
5569026e0d1SMaxime Ripard 
5579026e0d1SMaxime Ripard 	/* Set horizontal display timings */
5583cb2f46bSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
5599026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
5609026e0d1SMaxime Ripard 			 mode->htotal, bp);
5619026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
5629026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
5639026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
5649026e0d1SMaxime Ripard 
5653cb2f46bSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
5669026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
567b8317a3dSMaxime Ripard 			 mode->crtc_vtotal, bp);
568b8317a3dSMaxime Ripard 
569b8317a3dSMaxime Ripard 	/*
570b8317a3dSMaxime Ripard 	 * The vertical resolution needs to be doubled in all
571b8317a3dSMaxime Ripard 	 * cases. We could use crtc_vtotal and always multiply by two,
572b8317a3dSMaxime Ripard 	 * but that leads to a rounding error in interlace when vtotal
573b8317a3dSMaxime Ripard 	 * is odd.
574b8317a3dSMaxime Ripard 	 *
575b8317a3dSMaxime Ripard 	 * This happens with TV's PAL for example, where vtotal will
576b8317a3dSMaxime Ripard 	 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
577b8317a3dSMaxime Ripard 	 * 624, which apparently confuses the hardware.
578b8317a3dSMaxime Ripard 	 *
579b8317a3dSMaxime Ripard 	 * To work around this, we will always use vtotal, and
580b8317a3dSMaxime Ripard 	 * multiply by two only if we're not in interlace.
581b8317a3dSMaxime Ripard 	 */
582b8317a3dSMaxime Ripard 	vtotal = mode->vtotal;
583b8317a3dSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
584b8317a3dSMaxime Ripard 		vtotal = vtotal * 2;
585b8317a3dSMaxime Ripard 
586b8317a3dSMaxime Ripard 	/* Set vertical display timings */
5879026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
588b8317a3dSMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
5899026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
5909026e0d1SMaxime Ripard 
5919026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
5929026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
5939026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
5949026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
5959026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
5969026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
5979026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
5989026e0d1SMaxime Ripard 
5999026e0d1SMaxime Ripard 	/* Map output pins to channel 1 */
6009026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
6019026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
6029026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
6039026e0d1SMaxime Ripard }
6045b8f0910SMaxime Ripard 
6055b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
6065b8f0910SMaxime Ripard 			 const struct drm_encoder *encoder,
6075b8f0910SMaxime Ripard 			 const struct drm_display_mode *mode)
6085b8f0910SMaxime Ripard {
609a08fc7c8SMaxime Ripard 	struct sun6i_dsi *dsi;
610a08fc7c8SMaxime Ripard 
6115b8f0910SMaxime Ripard 	switch (encoder->encoder_type) {
612a08fc7c8SMaxime Ripard 	case DRM_MODE_ENCODER_DSI:
613a08fc7c8SMaxime Ripard 		/*
614a08fc7c8SMaxime Ripard 		 * This is not really elegant, but it's the "cleaner"
615a08fc7c8SMaxime Ripard 		 * way I could think of...
616a08fc7c8SMaxime Ripard 		 */
617a08fc7c8SMaxime Ripard 		dsi = encoder_to_sun6i_dsi(encoder);
618a08fc7c8SMaxime Ripard 		sun4i_tcon0_mode_set_cpu(tcon, dsi->device, mode);
619a08fc7c8SMaxime Ripard 		break;
620a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
621a0c1214eSMaxime Ripard 		sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
622a0c1214eSMaxime Ripard 		break;
6235b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
624ba19c537SMaxime Ripard 		sun4i_tcon0_mode_set_rgb(tcon, mode);
6255b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 0, encoder);
6265b8f0910SMaxime Ripard 		break;
6275b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
6285b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
6295b8f0910SMaxime Ripard 		sun4i_tcon1_mode_set(tcon, mode);
6305b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 1, encoder);
6315b8f0910SMaxime Ripard 		break;
6325b8f0910SMaxime Ripard 	default:
6335b8f0910SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
6345b8f0910SMaxime Ripard 	}
6355b8f0910SMaxime Ripard }
6365b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set);
6379026e0d1SMaxime Ripard 
6389026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
6399026e0d1SMaxime Ripard 					struct sun4i_crtc *scrtc)
6409026e0d1SMaxime Ripard {
6419026e0d1SMaxime Ripard 	unsigned long flags;
6429026e0d1SMaxime Ripard 
6439026e0d1SMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
6449026e0d1SMaxime Ripard 	if (scrtc->event) {
6459026e0d1SMaxime Ripard 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
6469026e0d1SMaxime Ripard 		drm_crtc_vblank_put(&scrtc->crtc);
6479026e0d1SMaxime Ripard 		scrtc->event = NULL;
6489026e0d1SMaxime Ripard 	}
6499026e0d1SMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
6509026e0d1SMaxime Ripard }
6519026e0d1SMaxime Ripard 
6529026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private)
6539026e0d1SMaxime Ripard {
6549026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = private;
6559026e0d1SMaxime Ripard 	struct drm_device *drm = tcon->drm;
65646cce6daSChen-Yu Tsai 	struct sun4i_crtc *scrtc = tcon->crtc;
6573004f75fSMaxime Ripard 	struct sunxi_engine *engine = scrtc->engine;
6589026e0d1SMaxime Ripard 	unsigned int status;
6599026e0d1SMaxime Ripard 
6609026e0d1SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
6619026e0d1SMaxime Ripard 
6629026e0d1SMaxime Ripard 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
663a493ceaeSMaxime Ripard 			SUN4I_TCON_GINT0_VBLANK_INT(1) |
664a493ceaeSMaxime Ripard 			SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT)))
6659026e0d1SMaxime Ripard 		return IRQ_NONE;
6669026e0d1SMaxime Ripard 
6679026e0d1SMaxime Ripard 	drm_crtc_handle_vblank(&scrtc->crtc);
6689026e0d1SMaxime Ripard 	sun4i_tcon_finish_page_flip(drm, scrtc);
6699026e0d1SMaxime Ripard 
6709026e0d1SMaxime Ripard 	/* Acknowledge the interrupt */
6719026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
6729026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
673a493ceaeSMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(1) |
674a493ceaeSMaxime Ripard 			   SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT,
6759026e0d1SMaxime Ripard 			   0);
6769026e0d1SMaxime Ripard 
6773004f75fSMaxime Ripard 	if (engine->ops->vblank_quirk)
6783004f75fSMaxime Ripard 		engine->ops->vblank_quirk(engine);
6793004f75fSMaxime Ripard 
6809026e0d1SMaxime Ripard 	return IRQ_HANDLED;
6819026e0d1SMaxime Ripard }
6829026e0d1SMaxime Ripard 
6839026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev,
6849026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
6859026e0d1SMaxime Ripard {
6869026e0d1SMaxime Ripard 	tcon->clk = devm_clk_get(dev, "ahb");
6879026e0d1SMaxime Ripard 	if (IS_ERR(tcon->clk)) {
6889026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON bus clock\n");
6899026e0d1SMaxime Ripard 		return PTR_ERR(tcon->clk);
6909026e0d1SMaxime Ripard 	}
6919026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->clk);
6929026e0d1SMaxime Ripard 
69334d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
6949026e0d1SMaxime Ripard 		tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
6959026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk0)) {
6969026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
6979026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk0);
6989026e0d1SMaxime Ripard 		}
69934d698f6SJernej Skrabec 	}
7009026e0d1SMaxime Ripard 
70191ea2f29SChen-Yu Tsai 	if (tcon->quirks->has_channel_1) {
7029026e0d1SMaxime Ripard 		tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
7039026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk1)) {
7049026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
7059026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk1);
7069026e0d1SMaxime Ripard 		}
7078e924047SMaxime Ripard 	}
7089026e0d1SMaxime Ripard 
7094c7f16d1SChen-Yu Tsai 	return 0;
7109026e0d1SMaxime Ripard }
7119026e0d1SMaxime Ripard 
7129026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
7139026e0d1SMaxime Ripard {
7149026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->clk);
7159026e0d1SMaxime Ripard }
7169026e0d1SMaxime Ripard 
7179026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev,
7189026e0d1SMaxime Ripard 			       struct sun4i_tcon *tcon)
7199026e0d1SMaxime Ripard {
7209026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
7219026e0d1SMaxime Ripard 	int irq, ret;
7229026e0d1SMaxime Ripard 
7239026e0d1SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
7249026e0d1SMaxime Ripard 	if (irq < 0) {
7259026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
7269026e0d1SMaxime Ripard 		return irq;
7279026e0d1SMaxime Ripard 	}
7289026e0d1SMaxime Ripard 
7299026e0d1SMaxime Ripard 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
7309026e0d1SMaxime Ripard 			       dev_name(dev), tcon);
7319026e0d1SMaxime Ripard 	if (ret) {
7329026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't request the IRQ\n");
7339026e0d1SMaxime Ripard 		return ret;
7349026e0d1SMaxime Ripard 	}
7359026e0d1SMaxime Ripard 
7369026e0d1SMaxime Ripard 	return 0;
7379026e0d1SMaxime Ripard }
7389026e0d1SMaxime Ripard 
7399026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = {
7409026e0d1SMaxime Ripard 	.reg_bits	= 32,
7419026e0d1SMaxime Ripard 	.val_bits	= 32,
7429026e0d1SMaxime Ripard 	.reg_stride	= 4,
7439026e0d1SMaxime Ripard 	.max_register	= 0x800,
7449026e0d1SMaxime Ripard };
7459026e0d1SMaxime Ripard 
7469026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev,
7479026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
7489026e0d1SMaxime Ripard {
7499026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
7509026e0d1SMaxime Ripard 	struct resource *res;
7519026e0d1SMaxime Ripard 	void __iomem *regs;
7529026e0d1SMaxime Ripard 
7539026e0d1SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7549026e0d1SMaxime Ripard 	regs = devm_ioremap_resource(dev, res);
755af346f55SWei Yongjun 	if (IS_ERR(regs))
7569026e0d1SMaxime Ripard 		return PTR_ERR(regs);
7579026e0d1SMaxime Ripard 
7589026e0d1SMaxime Ripard 	tcon->regs = devm_regmap_init_mmio(dev, regs,
7599026e0d1SMaxime Ripard 					   &sun4i_tcon_regmap_config);
7609026e0d1SMaxime Ripard 	if (IS_ERR(tcon->regs)) {
7619026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't create the TCON regmap\n");
7629026e0d1SMaxime Ripard 		return PTR_ERR(tcon->regs);
7639026e0d1SMaxime Ripard 	}
7649026e0d1SMaxime Ripard 
7659026e0d1SMaxime Ripard 	/* Make sure the TCON is disabled and all IRQs are off */
7669026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
7679026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
7689026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
7699026e0d1SMaxime Ripard 
7709026e0d1SMaxime Ripard 	/* Disable IO lines and set them to tristate */
7719026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
7729026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
7739026e0d1SMaxime Ripard 
7749026e0d1SMaxime Ripard 	return 0;
7759026e0d1SMaxime Ripard }
7769026e0d1SMaxime Ripard 
777b317fa3bSChen-Yu Tsai /*
778b317fa3bSChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
779b317fa3bSChen-Yu Tsai  * the TCON is always tied to just one backend. Hence we can traverse
780b317fa3bSChen-Yu Tsai  * the of_graph upwards to find the backend our tcon is connected to,
781b317fa3bSChen-Yu Tsai  * and take its ID as our own.
782b317fa3bSChen-Yu Tsai  *
783b317fa3bSChen-Yu Tsai  * We can either identify backends from their compatible strings, which
784b317fa3bSChen-Yu Tsai  * means maintaining a large list of them. Or, since the backend is
785b317fa3bSChen-Yu Tsai  * registered and binded before the TCON, we can just go through the
786b317fa3bSChen-Yu Tsai  * list of registered backends and compare the device node.
78787969338SIcenowy Zheng  *
78887969338SIcenowy Zheng  * As the structures now store engines instead of backends, here this
78987969338SIcenowy Zheng  * function in fact searches the corresponding engine, and the ID is
79087969338SIcenowy Zheng  * requested via the get_id function of the engine.
791b317fa3bSChen-Yu Tsai  */
792e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *
793e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
79449836b11SJernej Skrabec 				struct device_node *node,
79549836b11SJernej Skrabec 				u32 port_id)
796b317fa3bSChen-Yu Tsai {
797b317fa3bSChen-Yu Tsai 	struct device_node *port, *ep, *remote;
798be3fe0f9SChen-Yu Tsai 	struct sunxi_engine *engine = ERR_PTR(-EINVAL);
79949836b11SJernej Skrabec 	u32 reg = 0;
800b317fa3bSChen-Yu Tsai 
80149836b11SJernej Skrabec 	port = of_graph_get_port_by_id(node, port_id);
802b317fa3bSChen-Yu Tsai 	if (!port)
803b317fa3bSChen-Yu Tsai 		return ERR_PTR(-EINVAL);
804b317fa3bSChen-Yu Tsai 
8051469619dSChen-Yu Tsai 	/*
8061469619dSChen-Yu Tsai 	 * This only works if there is only one path from the TCON
8071469619dSChen-Yu Tsai 	 * to any display engine. Otherwise the probe order of the
8081469619dSChen-Yu Tsai 	 * TCONs and display engines is not guaranteed. They may
8091469619dSChen-Yu Tsai 	 * either bind to the wrong one, or worse, bind to the same
8101469619dSChen-Yu Tsai 	 * one if additional checks are not done.
8111469619dSChen-Yu Tsai 	 *
8121469619dSChen-Yu Tsai 	 * Bail out if there are multiple input connections.
8131469619dSChen-Yu Tsai 	 */
814be3fe0f9SChen-Yu Tsai 	if (of_get_available_child_count(port) != 1)
815be3fe0f9SChen-Yu Tsai 		goto out_put_port;
8161469619dSChen-Yu Tsai 
817be3fe0f9SChen-Yu Tsai 	/* Get the first connection without specifying an ID */
818be3fe0f9SChen-Yu Tsai 	ep = of_get_next_available_child(port, NULL);
819be3fe0f9SChen-Yu Tsai 	if (!ep)
820be3fe0f9SChen-Yu Tsai 		goto out_put_port;
821be3fe0f9SChen-Yu Tsai 
822b317fa3bSChen-Yu Tsai 	remote = of_graph_get_remote_port_parent(ep);
823b317fa3bSChen-Yu Tsai 	if (!remote)
824be3fe0f9SChen-Yu Tsai 		goto out_put_ep;
825b317fa3bSChen-Yu Tsai 
82687969338SIcenowy Zheng 	/* does this node match any registered engines? */
827be3fe0f9SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
828be3fe0f9SChen-Yu Tsai 		if (remote == engine->node)
829be3fe0f9SChen-Yu Tsai 			goto out_put_remote;
830b317fa3bSChen-Yu Tsai 
83149836b11SJernej Skrabec 	/*
83249836b11SJernej Skrabec 	 * According to device tree binding input ports have even id
83349836b11SJernej Skrabec 	 * number and output ports have odd id. Since component with
83449836b11SJernej Skrabec 	 * more than one input and one output (TCON TOP) exits, correct
83549836b11SJernej Skrabec 	 * remote input id has to be calculated by subtracting 1 from
83649836b11SJernej Skrabec 	 * remote output id. If this for some reason can't be done, 0
83749836b11SJernej Skrabec 	 * is used as input port id.
83849836b11SJernej Skrabec 	 */
83949836b11SJernej Skrabec 	port = of_graph_get_remote_port(ep);
84049836b11SJernej Skrabec 	if (!of_property_read_u32(port, "reg", &reg) && reg > 0)
84149836b11SJernej Skrabec 		reg -= 1;
84249836b11SJernej Skrabec 
843b317fa3bSChen-Yu Tsai 	/* keep looking through upstream ports */
84449836b11SJernej Skrabec 	engine = sun4i_tcon_find_engine_traverse(drv, remote, reg);
845b317fa3bSChen-Yu Tsai 
846be3fe0f9SChen-Yu Tsai out_put_remote:
847be3fe0f9SChen-Yu Tsai 	of_node_put(remote);
848be3fe0f9SChen-Yu Tsai out_put_ep:
849be3fe0f9SChen-Yu Tsai 	of_node_put(ep);
850be3fe0f9SChen-Yu Tsai out_put_port:
851be3fe0f9SChen-Yu Tsai 	of_node_put(port);
852be3fe0f9SChen-Yu Tsai 
853be3fe0f9SChen-Yu Tsai 	return engine;
854b317fa3bSChen-Yu Tsai }
855b317fa3bSChen-Yu Tsai 
856e8d5bbf7SChen-Yu Tsai /*
857e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
858e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
859e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
860e8d5bbf7SChen-Yu Tsai  * component. Thus we can look at any one of the input connections of
861e8d5bbf7SChen-Yu Tsai  * the TCONs, and use that connection's remote endpoint ID as our own.
862e8d5bbf7SChen-Yu Tsai  *
863e8d5bbf7SChen-Yu Tsai  * Since the user of this function already finds the input port,
864e8d5bbf7SChen-Yu Tsai  * the port is passed in directly without further checks.
865e8d5bbf7SChen-Yu Tsai  */
866e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
867e8d5bbf7SChen-Yu Tsai {
868e8d5bbf7SChen-Yu Tsai 	struct device_node *ep;
869e8d5bbf7SChen-Yu Tsai 	int ret = -EINVAL;
870e8d5bbf7SChen-Yu Tsai 
871e8d5bbf7SChen-Yu Tsai 	/* try finding an upstream endpoint */
872e8d5bbf7SChen-Yu Tsai 	for_each_available_child_of_node(port, ep) {
873e8d5bbf7SChen-Yu Tsai 		struct device_node *remote;
874e8d5bbf7SChen-Yu Tsai 		u32 reg;
875e8d5bbf7SChen-Yu Tsai 
876e8d5bbf7SChen-Yu Tsai 		remote = of_graph_get_remote_endpoint(ep);
877e8d5bbf7SChen-Yu Tsai 		if (!remote)
878e8d5bbf7SChen-Yu Tsai 			continue;
879e8d5bbf7SChen-Yu Tsai 
880e8d5bbf7SChen-Yu Tsai 		ret = of_property_read_u32(remote, "reg", &reg);
881e8d5bbf7SChen-Yu Tsai 		if (ret)
882e8d5bbf7SChen-Yu Tsai 			continue;
883e8d5bbf7SChen-Yu Tsai 
884e8d5bbf7SChen-Yu Tsai 		ret = reg;
885e8d5bbf7SChen-Yu Tsai 	}
886e8d5bbf7SChen-Yu Tsai 
887e8d5bbf7SChen-Yu Tsai 	return ret;
888e8d5bbf7SChen-Yu Tsai }
889e8d5bbf7SChen-Yu Tsai 
890e8d5bbf7SChen-Yu Tsai /*
891e8d5bbf7SChen-Yu Tsai  * Once we know the TCON's id, we can look through the list of
892e8d5bbf7SChen-Yu Tsai  * engines to find a matching one. We assume all engines have
893e8d5bbf7SChen-Yu Tsai  * been probed and added to the list.
894e8d5bbf7SChen-Yu Tsai  */
895e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
896e8d5bbf7SChen-Yu Tsai 							int id)
897e8d5bbf7SChen-Yu Tsai {
898e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
899e8d5bbf7SChen-Yu Tsai 
900e8d5bbf7SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
901e8d5bbf7SChen-Yu Tsai 		if (engine->id == id)
902e8d5bbf7SChen-Yu Tsai 			return engine;
903e8d5bbf7SChen-Yu Tsai 
904e8d5bbf7SChen-Yu Tsai 	return ERR_PTR(-EINVAL);
905e8d5bbf7SChen-Yu Tsai }
906e8d5bbf7SChen-Yu Tsai 
907e8d5bbf7SChen-Yu Tsai /*
908e8d5bbf7SChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
909e8d5bbf7SChen-Yu Tsai  * we assumed the TCON was always tied to just one backend. However
910e8d5bbf7SChen-Yu Tsai  * this proved not to be the case. On the A31, the TCON can select
911e8d5bbf7SChen-Yu Tsai  * either backend as its source. On the A20 (and likely on the A10),
912e8d5bbf7SChen-Yu Tsai  * the backend can choose which TCON to output to.
913e8d5bbf7SChen-Yu Tsai  *
914e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
915e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
916e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
917e8d5bbf7SChen-Yu Tsai  * component. Thus we should be able to look at any one of the input
918e8d5bbf7SChen-Yu Tsai  * connections of the TCONs, and use that connection's remote endpoint
919e8d5bbf7SChen-Yu Tsai  * ID as our own.
920e8d5bbf7SChen-Yu Tsai  *
921e8d5bbf7SChen-Yu Tsai  * However  the connections between the backend and TCON were assumed
922e8d5bbf7SChen-Yu Tsai  * to be always singular, and their endpoit IDs were all incorrectly
923e8d5bbf7SChen-Yu Tsai  * set to 0. This means for these old device trees, we cannot just look
924e8d5bbf7SChen-Yu Tsai  * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
925e8d5bbf7SChen-Yu Tsai  * incorrectly identified as TCON0.
926e8d5bbf7SChen-Yu Tsai  *
927e8d5bbf7SChen-Yu Tsai  * This function first checks if the TCON node has 2 input endpoints.
928e8d5bbf7SChen-Yu Tsai  * If so, then the device tree is a corrected version, and it will use
929e8d5bbf7SChen-Yu Tsai  * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
930e8d5bbf7SChen-Yu Tsai  * to fetch the ID and engine directly. If not, then it is likely an
931e8d5bbf7SChen-Yu Tsai  * old device trees, where the endpoint IDs were incorrect, but did not
932e8d5bbf7SChen-Yu Tsai  * have endpoint connections between the backend and TCON across
933e8d5bbf7SChen-Yu Tsai  * different display pipelines. It will fall back to the old method of
934e8d5bbf7SChen-Yu Tsai  * traversing the  of_graph to try and find a matching engine by device
935e8d5bbf7SChen-Yu Tsai  * node.
936e8d5bbf7SChen-Yu Tsai  *
937e8d5bbf7SChen-Yu Tsai  * In the case of single display pipeline device trees, either method
938e8d5bbf7SChen-Yu Tsai  * works.
939e8d5bbf7SChen-Yu Tsai  */
940e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
941e8d5bbf7SChen-Yu Tsai 						   struct device_node *node)
942e8d5bbf7SChen-Yu Tsai {
943e8d5bbf7SChen-Yu Tsai 	struct device_node *port;
944e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
945e8d5bbf7SChen-Yu Tsai 
946e8d5bbf7SChen-Yu Tsai 	port = of_graph_get_port_by_id(node, 0);
947e8d5bbf7SChen-Yu Tsai 	if (!port)
948e8d5bbf7SChen-Yu Tsai 		return ERR_PTR(-EINVAL);
949e8d5bbf7SChen-Yu Tsai 
950e8d5bbf7SChen-Yu Tsai 	/*
951e8d5bbf7SChen-Yu Tsai 	 * Is this a corrected device tree with cross pipeline
952e8d5bbf7SChen-Yu Tsai 	 * connections between the backend and TCON?
953e8d5bbf7SChen-Yu Tsai 	 */
954e8d5bbf7SChen-Yu Tsai 	if (of_get_child_count(port) > 1) {
955e8d5bbf7SChen-Yu Tsai 		/* Get our ID directly from an upstream endpoint */
956e8d5bbf7SChen-Yu Tsai 		int id = sun4i_tcon_of_get_id_from_port(port);
957e8d5bbf7SChen-Yu Tsai 
958e8d5bbf7SChen-Yu Tsai 		/* Get our engine by matching our ID */
959e8d5bbf7SChen-Yu Tsai 		engine = sun4i_tcon_get_engine_by_id(drv, id);
960e8d5bbf7SChen-Yu Tsai 
961e8d5bbf7SChen-Yu Tsai 		of_node_put(port);
962e8d5bbf7SChen-Yu Tsai 		return engine;
963e8d5bbf7SChen-Yu Tsai 	}
964e8d5bbf7SChen-Yu Tsai 
965e8d5bbf7SChen-Yu Tsai 	/* Fallback to old method by traversing input endpoints */
966e8d5bbf7SChen-Yu Tsai 	of_node_put(port);
96749836b11SJernej Skrabec 	return sun4i_tcon_find_engine_traverse(drv, node, 0);
968e8d5bbf7SChen-Yu Tsai }
969e8d5bbf7SChen-Yu Tsai 
9709026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master,
9719026e0d1SMaxime Ripard 			   void *data)
9729026e0d1SMaxime Ripard {
9739026e0d1SMaxime Ripard 	struct drm_device *drm = data;
9749026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
97587969338SIcenowy Zheng 	struct sunxi_engine *engine;
976a0c1214eSMaxime Ripard 	struct device_node *remote;
9779026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon;
9786664e9dcSChen-Yu Tsai 	struct reset_control *edp_rstc;
979a0c1214eSMaxime Ripard 	bool has_lvds_rst, has_lvds_alt, can_lvds;
9809026e0d1SMaxime Ripard 	int ret;
9819026e0d1SMaxime Ripard 
98287969338SIcenowy Zheng 	engine = sun4i_tcon_find_engine(drv, dev->of_node);
98387969338SIcenowy Zheng 	if (IS_ERR(engine)) {
98487969338SIcenowy Zheng 		dev_err(dev, "Couldn't find matching engine\n");
98580a58240SChen-Yu Tsai 		return -EPROBE_DEFER;
986b317fa3bSChen-Yu Tsai 	}
98780a58240SChen-Yu Tsai 
9889026e0d1SMaxime Ripard 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
9899026e0d1SMaxime Ripard 	if (!tcon)
9909026e0d1SMaxime Ripard 		return -ENOMEM;
9919026e0d1SMaxime Ripard 	dev_set_drvdata(dev, tcon);
9929026e0d1SMaxime Ripard 	tcon->drm = drm;
993ae558110SMaxime Ripard 	tcon->dev = dev;
99487969338SIcenowy Zheng 	tcon->id = engine->id;
99591ea2f29SChen-Yu Tsai 	tcon->quirks = of_device_get_match_data(dev);
9969026e0d1SMaxime Ripard 
9979026e0d1SMaxime Ripard 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
9989026e0d1SMaxime Ripard 	if (IS_ERR(tcon->lcd_rst)) {
9999026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
10009026e0d1SMaxime Ripard 		return PTR_ERR(tcon->lcd_rst);
10019026e0d1SMaxime Ripard 	}
10029026e0d1SMaxime Ripard 
10036664e9dcSChen-Yu Tsai 	if (tcon->quirks->needs_edp_reset) {
10046664e9dcSChen-Yu Tsai 		edp_rstc = devm_reset_control_get_shared(dev, "edp");
10056664e9dcSChen-Yu Tsai 		if (IS_ERR(edp_rstc)) {
10066664e9dcSChen-Yu Tsai 			dev_err(dev, "Couldn't get edp reset line\n");
10076664e9dcSChen-Yu Tsai 			return PTR_ERR(edp_rstc);
10086664e9dcSChen-Yu Tsai 		}
10096664e9dcSChen-Yu Tsai 
10106664e9dcSChen-Yu Tsai 		ret = reset_control_deassert(edp_rstc);
10116664e9dcSChen-Yu Tsai 		if (ret) {
10126664e9dcSChen-Yu Tsai 			dev_err(dev, "Couldn't deassert edp reset line\n");
10136664e9dcSChen-Yu Tsai 			return ret;
10146664e9dcSChen-Yu Tsai 		}
10156664e9dcSChen-Yu Tsai 	}
10166664e9dcSChen-Yu Tsai 
10179026e0d1SMaxime Ripard 	/* Make sure our TCON is reset */
1018d57294c1SChen-Yu Tsai 	ret = reset_control_reset(tcon->lcd_rst);
10199026e0d1SMaxime Ripard 	if (ret) {
10209026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't deassert our reset line\n");
10219026e0d1SMaxime Ripard 		return ret;
10229026e0d1SMaxime Ripard 	}
10239026e0d1SMaxime Ripard 
1024e742a17cSMaxime Ripard 	if (tcon->quirks->supports_lvds) {
1025a0c1214eSMaxime Ripard 		/*
1026e742a17cSMaxime Ripard 		 * This can only be made optional since we've had DT
1027e742a17cSMaxime Ripard 		 * nodes without the LVDS reset properties.
1028a0c1214eSMaxime Ripard 		 *
1029e742a17cSMaxime Ripard 		 * If the property is missing, just disable LVDS, and
1030e742a17cSMaxime Ripard 		 * print a warning.
1031a0c1214eSMaxime Ripard 		 */
1032a0c1214eSMaxime Ripard 		tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
1033a0c1214eSMaxime Ripard 		if (IS_ERR(tcon->lvds_rst)) {
1034a0c1214eSMaxime Ripard 			dev_err(dev, "Couldn't get our reset line\n");
1035a0c1214eSMaxime Ripard 			return PTR_ERR(tcon->lvds_rst);
1036a0c1214eSMaxime Ripard 		} else if (tcon->lvds_rst) {
1037a0c1214eSMaxime Ripard 			has_lvds_rst = true;
1038a0c1214eSMaxime Ripard 			reset_control_reset(tcon->lvds_rst);
1039a0c1214eSMaxime Ripard 		} else {
1040a0c1214eSMaxime Ripard 			has_lvds_rst = false;
1041a0c1214eSMaxime Ripard 		}
1042a0c1214eSMaxime Ripard 
1043a0c1214eSMaxime Ripard 		/*
1044e742a17cSMaxime Ripard 		 * This can only be made optional since we've had DT
1045e742a17cSMaxime Ripard 		 * nodes without the LVDS reset properties.
1046a0c1214eSMaxime Ripard 		 *
1047e742a17cSMaxime Ripard 		 * If the property is missing, just disable LVDS, and
1048e742a17cSMaxime Ripard 		 * print a warning.
1049a0c1214eSMaxime Ripard 		 */
1050a0c1214eSMaxime Ripard 		if (tcon->quirks->has_lvds_alt) {
1051a0c1214eSMaxime Ripard 			tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
1052a0c1214eSMaxime Ripard 			if (IS_ERR(tcon->lvds_pll)) {
1053a0c1214eSMaxime Ripard 				if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
1054a0c1214eSMaxime Ripard 					has_lvds_alt = false;
1055a0c1214eSMaxime Ripard 				} else {
1056a0c1214eSMaxime Ripard 					dev_err(dev, "Couldn't get the LVDS PLL\n");
105786a3ae58SDan Carpenter 					return PTR_ERR(tcon->lvds_pll);
1058a0c1214eSMaxime Ripard 				}
1059a0c1214eSMaxime Ripard 			} else {
1060a0c1214eSMaxime Ripard 				has_lvds_alt = true;
1061a0c1214eSMaxime Ripard 			}
1062a0c1214eSMaxime Ripard 		}
1063a0c1214eSMaxime Ripard 
1064e742a17cSMaxime Ripard 		if (!has_lvds_rst ||
1065e742a17cSMaxime Ripard 		    (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
1066e742a17cSMaxime Ripard 			dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
1067a0c1214eSMaxime Ripard 			dev_warn(dev, "LVDS output disabled\n");
1068a0c1214eSMaxime Ripard 			can_lvds = false;
1069a0c1214eSMaxime Ripard 		} else {
1070a0c1214eSMaxime Ripard 			can_lvds = true;
1071a0c1214eSMaxime Ripard 		}
1072e742a17cSMaxime Ripard 	} else {
1073e742a17cSMaxime Ripard 		can_lvds = false;
1074e742a17cSMaxime Ripard 	}
1075a0c1214eSMaxime Ripard 
10769026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_clocks(dev, tcon);
10779026e0d1SMaxime Ripard 	if (ret) {
10789026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON clocks\n");
10799026e0d1SMaxime Ripard 		goto err_assert_reset;
10809026e0d1SMaxime Ripard 	}
10819026e0d1SMaxime Ripard 
10824c7f16d1SChen-Yu Tsai 	ret = sun4i_tcon_init_regmap(dev, tcon);
10839026e0d1SMaxime Ripard 	if (ret) {
10844c7f16d1SChen-Yu Tsai 		dev_err(dev, "Couldn't init our TCON regmap\n");
10859026e0d1SMaxime Ripard 		goto err_free_clocks;
10869026e0d1SMaxime Ripard 	}
10879026e0d1SMaxime Ripard 
108834d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
10894c7f16d1SChen-Yu Tsai 		ret = sun4i_dclk_create(dev, tcon);
10904c7f16d1SChen-Yu Tsai 		if (ret) {
10914c7f16d1SChen-Yu Tsai 			dev_err(dev, "Couldn't create our TCON dot clock\n");
10924c7f16d1SChen-Yu Tsai 			goto err_free_clocks;
10934c7f16d1SChen-Yu Tsai 		}
109434d698f6SJernej Skrabec 	}
10954c7f16d1SChen-Yu Tsai 
10969026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_irq(dev, tcon);
10979026e0d1SMaxime Ripard 	if (ret) {
10989026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON interrupts\n");
10994c7f16d1SChen-Yu Tsai 		goto err_free_dotclock;
11009026e0d1SMaxime Ripard 	}
11019026e0d1SMaxime Ripard 
110287969338SIcenowy Zheng 	tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
110346cce6daSChen-Yu Tsai 	if (IS_ERR(tcon->crtc)) {
110446cce6daSChen-Yu Tsai 		dev_err(dev, "Couldn't create our CRTC\n");
110546cce6daSChen-Yu Tsai 		ret = PTR_ERR(tcon->crtc);
110692411f6dSMaxime Ripard 		goto err_free_dotclock;
110746cce6daSChen-Yu Tsai 	}
110846cce6daSChen-Yu Tsai 
11092a72d0c5SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
1110a0c1214eSMaxime Ripard 		/*
1111a0c1214eSMaxime Ripard 		 * If we have an LVDS panel connected to the TCON, we should
1112a0c1214eSMaxime Ripard 		 * just probe the LVDS connector. Otherwise, just probe RGB as
1113a0c1214eSMaxime Ripard 		 * we used to.
1114a0c1214eSMaxime Ripard 		 */
1115a0c1214eSMaxime Ripard 		remote = of_graph_get_remote_node(dev->of_node, 1, 0);
1116a0c1214eSMaxime Ripard 		if (of_device_is_compatible(remote, "panel-lvds"))
1117a0c1214eSMaxime Ripard 			if (can_lvds)
1118a0c1214eSMaxime Ripard 				ret = sun4i_lvds_init(drm, tcon);
1119a0c1214eSMaxime Ripard 			else
1120a0c1214eSMaxime Ripard 				ret = -EINVAL;
1121a0c1214eSMaxime Ripard 		else
1122b9c8506cSChen-Yu Tsai 			ret = sun4i_rgb_init(drm, tcon);
1123a0c1214eSMaxime Ripard 		of_node_put(remote);
1124a0c1214eSMaxime Ripard 
112513fef095SChen-Yu Tsai 		if (ret < 0)
112692411f6dSMaxime Ripard 			goto err_free_dotclock;
11272a72d0c5SJernej Skrabec 	}
112813fef095SChen-Yu Tsai 
112927e18de7SChen-Yu Tsai 	if (tcon->quirks->needs_de_be_mux) {
113027e18de7SChen-Yu Tsai 		/*
113127e18de7SChen-Yu Tsai 		 * We assume there is no dynamic muxing of backends
113227e18de7SChen-Yu Tsai 		 * and TCONs, so we select the backend with same ID.
113327e18de7SChen-Yu Tsai 		 *
113427e18de7SChen-Yu Tsai 		 * While dynamic selection might be interesting, since
113527e18de7SChen-Yu Tsai 		 * the CRTC is tied to the TCON, while the layers are
113627e18de7SChen-Yu Tsai 		 * tied to the backends, this means, we will need to
113727e18de7SChen-Yu Tsai 		 * switch between groups of layers. There might not be
113827e18de7SChen-Yu Tsai 		 * a way to represent this constraint in DRM.
113927e18de7SChen-Yu Tsai 		 */
114027e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
114127e18de7SChen-Yu Tsai 				   SUN4I_TCON0_CTL_SRC_SEL_MASK,
114227e18de7SChen-Yu Tsai 				   tcon->id);
114327e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
114427e18de7SChen-Yu Tsai 				   SUN4I_TCON1_CTL_SRC_SEL_MASK,
114527e18de7SChen-Yu Tsai 				   tcon->id);
114627e18de7SChen-Yu Tsai 	}
114727e18de7SChen-Yu Tsai 
114880a58240SChen-Yu Tsai 	list_add_tail(&tcon->list, &drv->tcon_list);
114980a58240SChen-Yu Tsai 
115013fef095SChen-Yu Tsai 	return 0;
11519026e0d1SMaxime Ripard 
11524c7f16d1SChen-Yu Tsai err_free_dotclock:
115334d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
11544c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
11559026e0d1SMaxime Ripard err_free_clocks:
11569026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
11579026e0d1SMaxime Ripard err_assert_reset:
11589026e0d1SMaxime Ripard 	reset_control_assert(tcon->lcd_rst);
11599026e0d1SMaxime Ripard 	return ret;
11609026e0d1SMaxime Ripard }
11619026e0d1SMaxime Ripard 
11629026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master,
11639026e0d1SMaxime Ripard 			      void *data)
11649026e0d1SMaxime Ripard {
11659026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
11669026e0d1SMaxime Ripard 
116780a58240SChen-Yu Tsai 	list_del(&tcon->list);
116834d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
11694c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
11709026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
11719026e0d1SMaxime Ripard }
11729026e0d1SMaxime Ripard 
1173dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = {
11749026e0d1SMaxime Ripard 	.bind	= sun4i_tcon_bind,
11759026e0d1SMaxime Ripard 	.unbind	= sun4i_tcon_unbind,
11769026e0d1SMaxime Ripard };
11779026e0d1SMaxime Ripard 
11789026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev)
11799026e0d1SMaxime Ripard {
118029e57fabSMaxime Ripard 	struct device_node *node = pdev->dev.of_node;
1181*63d6310fSJernej Skrabec 	const struct sun4i_tcon_quirks *quirks;
1182894f5a9fSMaxime Ripard 	struct drm_bridge *bridge;
118329e57fabSMaxime Ripard 	struct drm_panel *panel;
1184ebc94461SRob Herring 	int ret;
118529e57fabSMaxime Ripard 
1186*63d6310fSJernej Skrabec 	quirks = of_device_get_match_data(&pdev->dev);
1187*63d6310fSJernej Skrabec 
1188*63d6310fSJernej Skrabec 	/* panels and bridges are present only on TCONs with channel 0 */
1189*63d6310fSJernej Skrabec 	if (quirks->has_channel_0) {
1190ebc94461SRob Herring 		ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1191ebc94461SRob Herring 		if (ret == -EPROBE_DEFER)
1192ebc94461SRob Herring 			return ret;
1193*63d6310fSJernej Skrabec 	}
119429e57fabSMaxime Ripard 
11959026e0d1SMaxime Ripard 	return component_add(&pdev->dev, &sun4i_tcon_ops);
11969026e0d1SMaxime Ripard }
11979026e0d1SMaxime Ripard 
11989026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev)
11999026e0d1SMaxime Ripard {
12009026e0d1SMaxime Ripard 	component_del(&pdev->dev, &sun4i_tcon_ops);
12019026e0d1SMaxime Ripard 
12029026e0d1SMaxime Ripard 	return 0;
12039026e0d1SMaxime Ripard }
12049026e0d1SMaxime Ripard 
1205ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */
12064bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
12074bb206bfSJonathan Liu 				  const struct drm_encoder *encoder)
12084bb206bfSJonathan Liu {
12094bb206bfSJonathan Liu 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
12104bb206bfSJonathan Liu 	u32 shift;
12114bb206bfSJonathan Liu 
12124bb206bfSJonathan Liu 	if (!tcon0)
12134bb206bfSJonathan Liu 		return -EINVAL;
12144bb206bfSJonathan Liu 
12154bb206bfSJonathan Liu 	switch (encoder->encoder_type) {
12164bb206bfSJonathan Liu 	case DRM_MODE_ENCODER_TMDS:
12174bb206bfSJonathan Liu 		/* HDMI */
12184bb206bfSJonathan Liu 		shift = 8;
12194bb206bfSJonathan Liu 		break;
12204bb206bfSJonathan Liu 	default:
12214bb206bfSJonathan Liu 		return -EINVAL;
12224bb206bfSJonathan Liu 	}
12234bb206bfSJonathan Liu 
12244bb206bfSJonathan Liu 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
12254bb206bfSJonathan Liu 			   0x3 << shift, tcon->id << shift);
12264bb206bfSJonathan Liu 
12274bb206bfSJonathan Liu 	return 0;
12284bb206bfSJonathan Liu }
12294bb206bfSJonathan Liu 
1230ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
1231abcb8766SMaxime Ripard 				  const struct drm_encoder *encoder)
1232ad537fb2SChen-Yu Tsai {
1233ad537fb2SChen-Yu Tsai 	u32 val;
1234ad537fb2SChen-Yu Tsai 
1235ad537fb2SChen-Yu Tsai 	if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1236ad537fb2SChen-Yu Tsai 		val = 1;
1237ad537fb2SChen-Yu Tsai 	else
1238ad537fb2SChen-Yu Tsai 		val = 0;
1239ad537fb2SChen-Yu Tsai 
1240ad537fb2SChen-Yu Tsai 	/*
1241ad537fb2SChen-Yu Tsai 	 * FIXME: Undocumented bits
1242ad537fb2SChen-Yu Tsai 	 */
1243ad537fb2SChen-Yu Tsai 	return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1244ad537fb2SChen-Yu Tsai }
1245ad537fb2SChen-Yu Tsai 
124667e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
1247abcb8766SMaxime Ripard 			      const struct drm_encoder *encoder)
124867e32645SChen-Yu Tsai {
124967e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
125067e32645SChen-Yu Tsai 	u32 shift;
125167e32645SChen-Yu Tsai 
125267e32645SChen-Yu Tsai 	if (!tcon0)
125367e32645SChen-Yu Tsai 		return -EINVAL;
125467e32645SChen-Yu Tsai 
125567e32645SChen-Yu Tsai 	switch (encoder->encoder_type) {
125667e32645SChen-Yu Tsai 	case DRM_MODE_ENCODER_TMDS:
125767e32645SChen-Yu Tsai 		/* HDMI */
125867e32645SChen-Yu Tsai 		shift = 8;
125967e32645SChen-Yu Tsai 		break;
126067e32645SChen-Yu Tsai 	default:
126167e32645SChen-Yu Tsai 		/* TODO A31 has MIPI DSI but A31s does not */
126267e32645SChen-Yu Tsai 		return -EINVAL;
126367e32645SChen-Yu Tsai 	}
126467e32645SChen-Yu Tsai 
126567e32645SChen-Yu Tsai 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
126667e32645SChen-Yu Tsai 			   0x3 << shift, tcon->id << shift);
126767e32645SChen-Yu Tsai 
126867e32645SChen-Yu Tsai 	return 0;
126967e32645SChen-Yu Tsai }
127067e32645SChen-Yu Tsai 
12714bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
127234d698f6SJernej Skrabec 	.has_channel_0		= true,
12734bb206bfSJonathan Liu 	.has_channel_1		= true,
12744bb206bfSJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
12754bb206bfSJonathan Liu };
12764bb206bfSJonathan Liu 
127791ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
127834d698f6SJernej Skrabec 	.has_channel_0		= true,
127991ea2f29SChen-Yu Tsai 	.has_channel_1		= true,
1280ad537fb2SChen-Yu Tsai 	.set_mux		= sun5i_a13_tcon_set_mux,
128191ea2f29SChen-Yu Tsai };
128291ea2f29SChen-Yu Tsai 
128393a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
128434d698f6SJernej Skrabec 	.has_channel_0		= true,
128593a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
1286a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
128727e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
128867e32645SChen-Yu Tsai 	.set_mux		= sun6i_tcon_set_mux,
128993a5ec14SChen-Yu Tsai };
129093a5ec14SChen-Yu Tsai 
129193a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
129234d698f6SJernej Skrabec 	.has_channel_0		= true,
129393a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
129427e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
129593a5ec14SChen-Yu Tsai };
129693a5ec14SChen-Yu Tsai 
1297aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
129834d698f6SJernej Skrabec 	.has_channel_0		= true,
1299aaddb6d2SJonathan Liu 	.has_channel_1		= true,
1300aaddb6d2SJonathan Liu 	/* Same display pipeline structure as A10 */
1301aaddb6d2SJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
1302aaddb6d2SJonathan Liu };
1303aaddb6d2SJonathan Liu 
130491ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
130534d698f6SJernej Skrabec 	.has_channel_0		= true,
1306a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
130791ea2f29SChen-Yu Tsai };
130891ea2f29SChen-Yu Tsai 
13092f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
1310e742a17cSMaxime Ripard 	.supports_lvds		= true,
131134d698f6SJernej Skrabec 	.has_channel_0		= true,
13122f0d7bb1SMaxime Ripard };
13132f0d7bb1SMaxime Ripard 
131405adc89bSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
131505adc89bSJernej Skrabec 	.has_channel_1		= true,
131605adc89bSJernej Skrabec };
131705adc89bSJernej Skrabec 
13181a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
131934d698f6SJernej Skrabec 	.has_channel_0		= true,
13201a0edb3fSIcenowy Zheng };
13211a0edb3fSIcenowy Zheng 
13226664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
13236664e9dcSChen-Yu Tsai 	.has_channel_0	= true,
13246664e9dcSChen-Yu Tsai 	.needs_edp_reset = true,
13256664e9dcSChen-Yu Tsai };
13266664e9dcSChen-Yu Tsai 
13276664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
13286664e9dcSChen-Yu Tsai 	.has_channel_1	= true,
13296664e9dcSChen-Yu Tsai 	.needs_edp_reset = true,
13306664e9dcSChen-Yu Tsai };
13316664e9dcSChen-Yu Tsai 
1332ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */
1333ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = {
13344bb206bfSJonathan Liu 	{ .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
133591ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
133693a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
133793a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
1338aaddb6d2SJonathan Liu 	{ .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
133991ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
13402f0d7bb1SMaxime Ripard 	{ .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
134105adc89bSJernej Skrabec 	{ .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
13421a0edb3fSIcenowy Zheng 	{ .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
13436664e9dcSChen-Yu Tsai 	{ .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
13446664e9dcSChen-Yu Tsai 	{ .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
13459026e0d1SMaxime Ripard 	{ }
13469026e0d1SMaxime Ripard };
13479026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
1348ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table);
13499026e0d1SMaxime Ripard 
13509026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = {
13519026e0d1SMaxime Ripard 	.probe		= sun4i_tcon_probe,
13529026e0d1SMaxime Ripard 	.remove		= sun4i_tcon_remove,
13539026e0d1SMaxime Ripard 	.driver		= {
13549026e0d1SMaxime Ripard 		.name		= "sun4i-tcon",
13559026e0d1SMaxime Ripard 		.of_match_table	= sun4i_tcon_of_table,
13569026e0d1SMaxime Ripard 	},
13579026e0d1SMaxime Ripard };
13589026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver);
13599026e0d1SMaxime Ripard 
13609026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
13619026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
13629026e0d1SMaxime Ripard MODULE_LICENSE("GPL");
1363