xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision da82107ecf328d4914d316872866ceeb683c01eb)
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"
37a08fc7c8SMaxime Ripard #include "sun6i_mipi_dsi.h"
3887969338SIcenowy Zheng #include "sunxi_engine.h"
399026e0d1SMaxime Ripard 
40a0c1214eSMaxime Ripard static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder)
41a0c1214eSMaxime Ripard {
42a0c1214eSMaxime Ripard 	struct drm_connector *connector;
43a0c1214eSMaxime Ripard 	struct drm_connector_list_iter iter;
44a0c1214eSMaxime Ripard 
45a0c1214eSMaxime Ripard 	drm_connector_list_iter_begin(encoder->dev, &iter);
46a0c1214eSMaxime Ripard 	drm_for_each_connector_iter(connector, &iter)
47a0c1214eSMaxime Ripard 		if (connector->encoder == encoder) {
48a0c1214eSMaxime Ripard 			drm_connector_list_iter_end(&iter);
49a0c1214eSMaxime Ripard 			return connector;
50a0c1214eSMaxime Ripard 		}
51a0c1214eSMaxime Ripard 	drm_connector_list_iter_end(&iter);
52a0c1214eSMaxime Ripard 
53a0c1214eSMaxime Ripard 	return NULL;
54a0c1214eSMaxime Ripard }
55a0c1214eSMaxime Ripard 
56a0c1214eSMaxime Ripard static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder)
57a0c1214eSMaxime Ripard {
58a0c1214eSMaxime Ripard 	struct drm_connector *connector;
59a0c1214eSMaxime Ripard 	struct drm_display_info *info;
60a0c1214eSMaxime Ripard 
61a0c1214eSMaxime Ripard 	connector = sun4i_tcon_get_connector(encoder);
62a0c1214eSMaxime Ripard 	if (!connector)
63a0c1214eSMaxime Ripard 		return -EINVAL;
64a0c1214eSMaxime Ripard 
65a0c1214eSMaxime Ripard 	info = &connector->display_info;
66a0c1214eSMaxime Ripard 	if (info->num_bus_formats != 1)
67a0c1214eSMaxime Ripard 		return -EINVAL;
68a0c1214eSMaxime Ripard 
69a0c1214eSMaxime Ripard 	switch (info->bus_formats[0]) {
70a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
71a0c1214eSMaxime Ripard 		return 18;
72a0c1214eSMaxime Ripard 
73a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
74a0c1214eSMaxime Ripard 	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
75a0c1214eSMaxime Ripard 		return 24;
76a0c1214eSMaxime Ripard 	}
77a0c1214eSMaxime Ripard 
78a0c1214eSMaxime Ripard 	return -EINVAL;
79a0c1214eSMaxime Ripard }
80a0c1214eSMaxime Ripard 
8145e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
8245e88f99SMaxime Ripard 					  bool enabled)
839026e0d1SMaxime Ripard {
8445e88f99SMaxime Ripard 	struct clk *clk;
859026e0d1SMaxime Ripard 
8645e88f99SMaxime Ripard 	switch (channel) {
8745e88f99SMaxime Ripard 	case 0:
8834d698f6SJernej Skrabec 		WARN_ON(!tcon->quirks->has_channel_0);
899026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
909026e0d1SMaxime Ripard 				   SUN4I_TCON0_CTL_TCON_ENABLE,
9145e88f99SMaxime Ripard 				   enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
9245e88f99SMaxime Ripard 		clk = tcon->dclk;
9345e88f99SMaxime Ripard 		break;
9445e88f99SMaxime Ripard 	case 1:
9591ea2f29SChen-Yu Tsai 		WARN_ON(!tcon->quirks->has_channel_1);
969026e0d1SMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
979026e0d1SMaxime Ripard 				   SUN4I_TCON1_CTL_TCON_ENABLE,
9845e88f99SMaxime Ripard 				   enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
9945e88f99SMaxime Ripard 		clk = tcon->sclk1;
10045e88f99SMaxime Ripard 		break;
10145e88f99SMaxime Ripard 	default:
10245e88f99SMaxime Ripard 		DRM_WARN("Unknown channel... doing nothing\n");
10345e88f99SMaxime Ripard 		return;
1049026e0d1SMaxime Ripard 	}
10545e88f99SMaxime Ripard 
106f3e5feebSJernej Skrabec 	if (enabled) {
10745e88f99SMaxime Ripard 		clk_prepare_enable(clk);
1087035046dSOndrej Jirman 		clk_rate_exclusive_get(clk);
109f3e5feebSJernej Skrabec 	} else {
110f3e5feebSJernej Skrabec 		clk_rate_exclusive_put(clk);
11145e88f99SMaxime Ripard 		clk_disable_unprepare(clk);
11245e88f99SMaxime Ripard 	}
113f3e5feebSJernej Skrabec }
11445e88f99SMaxime Ripard 
115a0c1214eSMaxime Ripard static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon,
116a0c1214eSMaxime Ripard 				       const struct drm_encoder *encoder,
117a0c1214eSMaxime Ripard 				       bool enabled)
118a0c1214eSMaxime Ripard {
119a0c1214eSMaxime Ripard 	if (enabled) {
120a0c1214eSMaxime Ripard 		u8 val;
121a0c1214eSMaxime Ripard 
122a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
123a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN,
124a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN);
125a0c1214eSMaxime Ripard 
126a0c1214eSMaxime Ripard 		/*
127a0c1214eSMaxime Ripard 		 * As their name suggest, these values only apply to the A31
128a0c1214eSMaxime Ripard 		 * and later SoCs. We'll have to rework this when merging
129a0c1214eSMaxime Ripard 		 * support for the older SoCs.
130a0c1214eSMaxime Ripard 		 */
131a0c1214eSMaxime Ripard 		regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
132a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_C(2) |
133a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_V(3) |
134a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_PD(2) |
135a0c1214eSMaxime Ripard 			     SUN6I_TCON0_LVDS_ANA0_EN_LDO);
136a0c1214eSMaxime Ripard 		udelay(2);
137a0c1214eSMaxime Ripard 
138a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
139a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_MB,
140a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_MB);
141a0c1214eSMaxime Ripard 		udelay(2);
142a0c1214eSMaxime Ripard 
143a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
144a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC,
145a0c1214eSMaxime Ripard 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC);
146a0c1214eSMaxime Ripard 
147a0c1214eSMaxime Ripard 		if (sun4i_tcon_get_pixel_depth(encoder) == 18)
148a0c1214eSMaxime Ripard 			val = 7;
149a0c1214eSMaxime Ripard 		else
150a0c1214eSMaxime Ripard 			val = 0xf;
151a0c1214eSMaxime Ripard 
152a0c1214eSMaxime Ripard 		regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
153a0c1214eSMaxime Ripard 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf),
154a0c1214eSMaxime Ripard 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val));
155a0c1214eSMaxime Ripard 	} else {
156a0c1214eSMaxime Ripard 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
157a0c1214eSMaxime Ripard 				   SUN4I_TCON0_LVDS_IF_EN, 0);
158a0c1214eSMaxime Ripard 	}
159a0c1214eSMaxime Ripard }
160a0c1214eSMaxime Ripard 
16145e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
16245e88f99SMaxime Ripard 			   const struct drm_encoder *encoder,
16345e88f99SMaxime Ripard 			   bool enabled)
16445e88f99SMaxime Ripard {
165a0c1214eSMaxime Ripard 	bool is_lvds = false;
16645e88f99SMaxime Ripard 	int channel;
16745e88f99SMaxime Ripard 
16845e88f99SMaxime Ripard 	switch (encoder->encoder_type) {
169a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
170a0c1214eSMaxime Ripard 		is_lvds = true;
171a0c1214eSMaxime Ripard 		/* Fallthrough */
172a08fc7c8SMaxime Ripard 	case DRM_MODE_ENCODER_DSI:
17345e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
17445e88f99SMaxime Ripard 		channel = 0;
17545e88f99SMaxime Ripard 		break;
17645e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
17745e88f99SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
17845e88f99SMaxime Ripard 		channel = 1;
17945e88f99SMaxime Ripard 		break;
18045e88f99SMaxime Ripard 	default:
18145e88f99SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
18245e88f99SMaxime Ripard 		return;
18345e88f99SMaxime Ripard 	}
18445e88f99SMaxime Ripard 
185a0c1214eSMaxime Ripard 	if (is_lvds && !enabled)
186a0c1214eSMaxime Ripard 		sun4i_tcon_lvds_set_status(tcon, encoder, false);
187a0c1214eSMaxime Ripard 
18845e88f99SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
18945e88f99SMaxime Ripard 			   SUN4I_TCON_GCTL_TCON_ENABLE,
19045e88f99SMaxime Ripard 			   enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
19145e88f99SMaxime Ripard 
192a0c1214eSMaxime Ripard 	if (is_lvds && enabled)
193a0c1214eSMaxime Ripard 		sun4i_tcon_lvds_set_status(tcon, encoder, true);
194a0c1214eSMaxime Ripard 
19545e88f99SMaxime Ripard 	sun4i_tcon_channel_set_status(tcon, channel, enabled);
19645e88f99SMaxime Ripard }
1979026e0d1SMaxime Ripard 
1989026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
1999026e0d1SMaxime Ripard {
2009026e0d1SMaxime Ripard 	u32 mask, val = 0;
2019026e0d1SMaxime Ripard 
2029026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
2039026e0d1SMaxime Ripard 
2049026e0d1SMaxime Ripard 	mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
205a493ceaeSMaxime Ripard 		SUN4I_TCON_GINT0_VBLANK_ENABLE(1) |
206a493ceaeSMaxime Ripard 		SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE;
2079026e0d1SMaxime Ripard 
2089026e0d1SMaxime Ripard 	if (enable)
2099026e0d1SMaxime Ripard 		val = mask;
2109026e0d1SMaxime Ripard 
2119026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
2129026e0d1SMaxime Ripard }
2139026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
2149026e0d1SMaxime Ripard 
21567e32645SChen-Yu Tsai /*
21667e32645SChen-Yu Tsai  * This function is a helper for TCON output muxing. The TCON output
21767e32645SChen-Yu Tsai  * muxing control register in earlier SoCs (without the TCON TOP block)
21867e32645SChen-Yu Tsai  * are located in TCON0. This helper returns a pointer to TCON0's
21967e32645SChen-Yu Tsai  * sun4i_tcon structure, or NULL if not found.
22067e32645SChen-Yu Tsai  */
22167e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
22267e32645SChen-Yu Tsai {
22367e32645SChen-Yu Tsai 	struct sun4i_drv *drv = drm->dev_private;
22467e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon;
22567e32645SChen-Yu Tsai 
22667e32645SChen-Yu Tsai 	list_for_each_entry(tcon, &drv->tcon_list, list)
22767e32645SChen-Yu Tsai 		if (tcon->id == 0)
22867e32645SChen-Yu Tsai 			return tcon;
22967e32645SChen-Yu Tsai 
23067e32645SChen-Yu Tsai 	dev_warn(drm->dev,
23167e32645SChen-Yu Tsai 		 "TCON0 not found, display output muxing may not work\n");
23267e32645SChen-Yu Tsai 
23367e32645SChen-Yu Tsai 	return NULL;
23467e32645SChen-Yu Tsai }
23567e32645SChen-Yu Tsai 
236f8c73f4fSMaxime Ripard void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
237abcb8766SMaxime Ripard 			const struct drm_encoder *encoder)
238f8c73f4fSMaxime Ripard {
239ad537fb2SChen-Yu Tsai 	int ret = -ENOTSUPP;
240b7cb9b91SMaxime Ripard 
241ad537fb2SChen-Yu Tsai 	if (tcon->quirks->set_mux)
242ad537fb2SChen-Yu Tsai 		ret = tcon->quirks->set_mux(tcon, encoder);
243f8c73f4fSMaxime Ripard 
244ad537fb2SChen-Yu Tsai 	DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
245ad537fb2SChen-Yu Tsai 			 encoder->name, encoder->crtc->name, ret);
246f8c73f4fSMaxime Ripard }
247f8c73f4fSMaxime Ripard 
248961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
2499026e0d1SMaxime Ripard 				    int channel)
2509026e0d1SMaxime Ripard {
2519026e0d1SMaxime Ripard 	int delay = mode->vtotal - mode->vdisplay;
2529026e0d1SMaxime Ripard 
2539026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2549026e0d1SMaxime Ripard 		delay /= 2;
2559026e0d1SMaxime Ripard 
2569026e0d1SMaxime Ripard 	if (channel == 1)
2579026e0d1SMaxime Ripard 		delay -= 2;
2589026e0d1SMaxime Ripard 
2599026e0d1SMaxime Ripard 	delay = min(delay, 30);
2609026e0d1SMaxime Ripard 
2619026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
2629026e0d1SMaxime Ripard 
2639026e0d1SMaxime Ripard 	return delay;
2649026e0d1SMaxime Ripard }
2659026e0d1SMaxime Ripard 
266ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon,
267ba19c537SMaxime Ripard 					const struct drm_display_mode *mode)
268ba19c537SMaxime Ripard {
269ba19c537SMaxime Ripard 	/* Configure the dot clock */
270ba19c537SMaxime Ripard 	clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
271ba19c537SMaxime Ripard 
272ba19c537SMaxime Ripard 	/* Set the resolution */
273ba19c537SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
274ba19c537SMaxime Ripard 		     SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
275ba19c537SMaxime Ripard 		     SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
276ba19c537SMaxime Ripard }
277ba19c537SMaxime Ripard 
278a08fc7c8SMaxime Ripard static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon,
279a08fc7c8SMaxime Ripard 				     struct mipi_dsi_device *device,
280a08fc7c8SMaxime Ripard 				     const struct drm_display_mode *mode)
281a08fc7c8SMaxime Ripard {
282a08fc7c8SMaxime Ripard 	u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format);
283a08fc7c8SMaxime Ripard 	u8 lanes = device->lanes;
284a08fc7c8SMaxime Ripard 	u32 block_space, start_delay;
285a08fc7c8SMaxime Ripard 	u32 tcon_div;
286a08fc7c8SMaxime Ripard 
287a08fc7c8SMaxime Ripard 	tcon->dclk_min_div = 4;
288a08fc7c8SMaxime Ripard 	tcon->dclk_max_div = 127;
289a08fc7c8SMaxime Ripard 
290a08fc7c8SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
291a08fc7c8SMaxime Ripard 
292a08fc7c8SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
293a08fc7c8SMaxime Ripard 			   SUN4I_TCON0_CTL_IF_MASK,
294a08fc7c8SMaxime Ripard 			   SUN4I_TCON0_CTL_IF_8080);
295a08fc7c8SMaxime Ripard 
296a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG,
297a08fc7c8SMaxime Ripard 		     SUN4I_TCON_ECC_FIFO_EN);
298a08fc7c8SMaxime Ripard 
299a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG,
300a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_MODE_DSI |
301a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH |
302a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_EN |
303a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_EN);
304a08fc7c8SMaxime Ripard 
305a08fc7c8SMaxime Ripard 	/*
306a08fc7c8SMaxime Ripard 	 * This looks suspicious, but it works...
307a08fc7c8SMaxime Ripard 	 *
308a08fc7c8SMaxime Ripard 	 * The datasheet says that this should be set higher than 20 *
309a08fc7c8SMaxime Ripard 	 * pixel cycle, but it's not clear what a pixel cycle is.
310a08fc7c8SMaxime Ripard 	 */
311a08fc7c8SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div);
312a08fc7c8SMaxime Ripard 	tcon_div &= GENMASK(6, 0);
313a08fc7c8SMaxime Ripard 	block_space = mode->htotal * bpp / (tcon_div * lanes);
314a08fc7c8SMaxime Ripard 	block_space -= mode->hdisplay + 40;
315a08fc7c8SMaxime Ripard 
316a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG,
317a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) |
318a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay));
319a08fc7c8SMaxime Ripard 
320a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG,
321a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay));
322a08fc7c8SMaxime Ripard 
323a08fc7c8SMaxime Ripard 	start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1);
324a08fc7c8SMaxime Ripard 	start_delay = start_delay * mode->crtc_htotal * 149;
325a08fc7c8SMaxime Ripard 	start_delay = start_delay / (mode->crtc_clock / 1000) / 8;
326a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG,
327a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) |
328a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay));
329a08fc7c8SMaxime Ripard 
330a08fc7c8SMaxime Ripard 	/*
331a08fc7c8SMaxime Ripard 	 * The Allwinner BSP has a comment that the period should be
332a08fc7c8SMaxime Ripard 	 * the display clock * 15, but uses an hardcoded 3000...
333a08fc7c8SMaxime Ripard 	 */
334a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG,
335a08fc7c8SMaxime Ripard 		     SUN4I_TCON_SAFE_PERIOD_NUM(3000) |
336a08fc7c8SMaxime Ripard 		     SUN4I_TCON_SAFE_PERIOD_MODE(3));
337a08fc7c8SMaxime Ripard 
338a08fc7c8SMaxime Ripard 	/* Enable the output on the pins */
339a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG,
340a08fc7c8SMaxime Ripard 		     0xe0000000);
341a08fc7c8SMaxime Ripard }
342a08fc7c8SMaxime Ripard 
343a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
344a0c1214eSMaxime Ripard 				      const struct drm_encoder *encoder,
345a0c1214eSMaxime Ripard 				      const struct drm_display_mode *mode)
346a0c1214eSMaxime Ripard {
347a0c1214eSMaxime Ripard 	unsigned int bp;
348a0c1214eSMaxime Ripard 	u8 clk_delay;
349a0c1214eSMaxime Ripard 	u32 reg, val = 0;
350a0c1214eSMaxime Ripard 
35134d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
35234d698f6SJernej Skrabec 
353a0c1214eSMaxime Ripard 	tcon->dclk_min_div = 7;
354a0c1214eSMaxime Ripard 	tcon->dclk_max_div = 7;
355a0c1214eSMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
356a0c1214eSMaxime Ripard 
357a0c1214eSMaxime Ripard 	/* Adjust clock delay */
358a0c1214eSMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
359a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
360a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
361a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
362a0c1214eSMaxime Ripard 
363a0c1214eSMaxime Ripard 	/*
364a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
365a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
366a0c1214eSMaxime Ripard 	 */
367a0c1214eSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
368a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
369a0c1214eSMaxime Ripard 			 mode->crtc_htotal, bp);
370a0c1214eSMaxime Ripard 
371a0c1214eSMaxime Ripard 	/* Set horizontal display timings */
372a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
373a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
374a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
375a0c1214eSMaxime Ripard 
376a0c1214eSMaxime Ripard 	/*
377a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
378a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
379a0c1214eSMaxime Ripard 	 */
380a0c1214eSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
381a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
382a0c1214eSMaxime Ripard 			 mode->crtc_vtotal, bp);
383a0c1214eSMaxime Ripard 
384a0c1214eSMaxime Ripard 	/* Set vertical display timings */
385a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
386a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
387a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
388a0c1214eSMaxime Ripard 
389a0c1214eSMaxime Ripard 	reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
390a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
391a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
392a0c1214eSMaxime Ripard 	if (sun4i_tcon_get_pixel_depth(encoder) == 24)
393a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
394a0c1214eSMaxime Ripard 	else
395a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
396a0c1214eSMaxime Ripard 
397a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
398a0c1214eSMaxime Ripard 
399a0c1214eSMaxime Ripard 	/* Setup the polarity of the various signals */
400a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
401a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
402a0c1214eSMaxime Ripard 
403a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
404a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
405a0c1214eSMaxime Ripard 
406a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
407a0c1214eSMaxime Ripard 
408a0c1214eSMaxime Ripard 	/* Map output pins to channel 0 */
409a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
410a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
411a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
41280b79e31SOndrej Jirman 
41380b79e31SOndrej Jirman 	/* Enable the output on the pins */
41480b79e31SOndrej Jirman 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000);
415a0c1214eSMaxime Ripard }
416a0c1214eSMaxime Ripard 
417ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
4185b8f0910SMaxime Ripard 				     const struct drm_display_mode *mode)
4199026e0d1SMaxime Ripard {
4209026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
4219026e0d1SMaxime Ripard 	u8 clk_delay;
4229026e0d1SMaxime Ripard 	u32 val = 0;
4239026e0d1SMaxime Ripard 
42434d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
42534d698f6SJernej Skrabec 
426ec08d596SMaxime Ripard 	tcon->dclk_min_div = 6;
427ec08d596SMaxime Ripard 	tcon->dclk_max_div = 127;
428ba19c537SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
42986cf6788SChen-Yu Tsai 
4309026e0d1SMaxime Ripard 	/* Adjust clock delay */
4319026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
4329026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
4339026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
4349026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
4359026e0d1SMaxime Ripard 
4369026e0d1SMaxime Ripard 	/*
4379026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
43823a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
4399026e0d1SMaxime Ripard 	 */
4409026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
4419026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
4429026e0d1SMaxime Ripard 			 mode->crtc_htotal, bp);
4439026e0d1SMaxime Ripard 
4449026e0d1SMaxime Ripard 	/* Set horizontal display timings */
4459026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
4469026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
4479026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
4489026e0d1SMaxime Ripard 
4499026e0d1SMaxime Ripard 	/*
4509026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
45123a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
4529026e0d1SMaxime Ripard 	 */
4539026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
4549026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
4559026e0d1SMaxime Ripard 			 mode->crtc_vtotal, bp);
4569026e0d1SMaxime Ripard 
4579026e0d1SMaxime Ripard 	/* Set vertical display timings */
4589026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
459a88cbbd4SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
4609026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
4619026e0d1SMaxime Ripard 
4629026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
4639026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
4649026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
4659026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
4669026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
4679026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
4689026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
4699026e0d1SMaxime Ripard 
4709026e0d1SMaxime Ripard 	/* Setup the polarity of the various signals */
471fa4127c5SGiulio Benetti 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
4729026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
4739026e0d1SMaxime Ripard 
474fa4127c5SGiulio Benetti 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
4759026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
4769026e0d1SMaxime Ripard 
4779026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
4789026e0d1SMaxime Ripard 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
4799026e0d1SMaxime Ripard 			   val);
4809026e0d1SMaxime Ripard 
4819026e0d1SMaxime Ripard 	/* Map output pins to channel 0 */
4829026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
4839026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
4849026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
4859026e0d1SMaxime Ripard 
4869026e0d1SMaxime Ripard 	/* Enable the output on the pins */
4879026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
4889026e0d1SMaxime Ripard }
4899026e0d1SMaxime Ripard 
4905b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
4915b8f0910SMaxime Ripard 				 const struct drm_display_mode *mode)
4929026e0d1SMaxime Ripard {
493b8317a3dSMaxime Ripard 	unsigned int bp, hsync, vsync, vtotal;
4949026e0d1SMaxime Ripard 	u8 clk_delay;
4959026e0d1SMaxime Ripard 	u32 val;
4969026e0d1SMaxime Ripard 
49791ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
4988e924047SMaxime Ripard 
49986cf6788SChen-Yu Tsai 	/* Configure the dot clock */
50086cf6788SChen-Yu Tsai 	clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
50186cf6788SChen-Yu Tsai 
5029026e0d1SMaxime Ripard 	/* Adjust clock delay */
5039026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
5049026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
5059026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
5069026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
5079026e0d1SMaxime Ripard 
5089026e0d1SMaxime Ripard 	/* Set interlaced mode */
5099026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5109026e0d1SMaxime Ripard 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
5119026e0d1SMaxime Ripard 	else
5129026e0d1SMaxime Ripard 		val = 0;
5139026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
5149026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
5159026e0d1SMaxime Ripard 			   val);
5169026e0d1SMaxime Ripard 
5179026e0d1SMaxime Ripard 	/* Set the input resolution */
5189026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
5199026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
5209026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
5219026e0d1SMaxime Ripard 
5229026e0d1SMaxime Ripard 	/* Set the upscaling resolution */
5239026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
5249026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
5259026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
5269026e0d1SMaxime Ripard 
5279026e0d1SMaxime Ripard 	/* Set the output resolution */
5289026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
5299026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
5309026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
5319026e0d1SMaxime Ripard 
5329026e0d1SMaxime Ripard 	/* Set horizontal display timings */
5333cb2f46bSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
5349026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
5359026e0d1SMaxime Ripard 			 mode->htotal, bp);
5369026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
5379026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
5389026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
5399026e0d1SMaxime Ripard 
5403cb2f46bSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
5419026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
542b8317a3dSMaxime Ripard 			 mode->crtc_vtotal, bp);
543b8317a3dSMaxime Ripard 
544b8317a3dSMaxime Ripard 	/*
545b8317a3dSMaxime Ripard 	 * The vertical resolution needs to be doubled in all
546b8317a3dSMaxime Ripard 	 * cases. We could use crtc_vtotal and always multiply by two,
547b8317a3dSMaxime Ripard 	 * but that leads to a rounding error in interlace when vtotal
548b8317a3dSMaxime Ripard 	 * is odd.
549b8317a3dSMaxime Ripard 	 *
550b8317a3dSMaxime Ripard 	 * This happens with TV's PAL for example, where vtotal will
551b8317a3dSMaxime Ripard 	 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
552b8317a3dSMaxime Ripard 	 * 624, which apparently confuses the hardware.
553b8317a3dSMaxime Ripard 	 *
554b8317a3dSMaxime Ripard 	 * To work around this, we will always use vtotal, and
555b8317a3dSMaxime Ripard 	 * multiply by two only if we're not in interlace.
556b8317a3dSMaxime Ripard 	 */
557b8317a3dSMaxime Ripard 	vtotal = mode->vtotal;
558b8317a3dSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
559b8317a3dSMaxime Ripard 		vtotal = vtotal * 2;
560b8317a3dSMaxime Ripard 
561b8317a3dSMaxime Ripard 	/* Set vertical display timings */
5629026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
563b8317a3dSMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
5649026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
5659026e0d1SMaxime Ripard 
5669026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
5679026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
5689026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
5699026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
5709026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
5719026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
5729026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
5739026e0d1SMaxime Ripard 
5749026e0d1SMaxime Ripard 	/* Map output pins to channel 1 */
5759026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
5769026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
5779026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
5789026e0d1SMaxime Ripard }
5795b8f0910SMaxime Ripard 
5805b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
5815b8f0910SMaxime Ripard 			 const struct drm_encoder *encoder,
5825b8f0910SMaxime Ripard 			 const struct drm_display_mode *mode)
5835b8f0910SMaxime Ripard {
584a08fc7c8SMaxime Ripard 	struct sun6i_dsi *dsi;
585a08fc7c8SMaxime Ripard 
5865b8f0910SMaxime Ripard 	switch (encoder->encoder_type) {
587a08fc7c8SMaxime Ripard 	case DRM_MODE_ENCODER_DSI:
588a08fc7c8SMaxime Ripard 		/*
589a08fc7c8SMaxime Ripard 		 * This is not really elegant, but it's the "cleaner"
590a08fc7c8SMaxime Ripard 		 * way I could think of...
591a08fc7c8SMaxime Ripard 		 */
592a08fc7c8SMaxime Ripard 		dsi = encoder_to_sun6i_dsi(encoder);
593a08fc7c8SMaxime Ripard 		sun4i_tcon0_mode_set_cpu(tcon, dsi->device, mode);
594a08fc7c8SMaxime Ripard 		break;
595a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
596a0c1214eSMaxime Ripard 		sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
597a0c1214eSMaxime Ripard 		break;
5985b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
599ba19c537SMaxime Ripard 		sun4i_tcon0_mode_set_rgb(tcon, mode);
6005b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 0, encoder);
6015b8f0910SMaxime Ripard 		break;
6025b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
6035b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
6045b8f0910SMaxime Ripard 		sun4i_tcon1_mode_set(tcon, mode);
6055b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 1, encoder);
6065b8f0910SMaxime Ripard 		break;
6075b8f0910SMaxime Ripard 	default:
6085b8f0910SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
6095b8f0910SMaxime Ripard 	}
6105b8f0910SMaxime Ripard }
6115b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set);
6129026e0d1SMaxime Ripard 
6139026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
6149026e0d1SMaxime Ripard 					struct sun4i_crtc *scrtc)
6159026e0d1SMaxime Ripard {
6169026e0d1SMaxime Ripard 	unsigned long flags;
6179026e0d1SMaxime Ripard 
6189026e0d1SMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
6199026e0d1SMaxime Ripard 	if (scrtc->event) {
6209026e0d1SMaxime Ripard 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
6219026e0d1SMaxime Ripard 		drm_crtc_vblank_put(&scrtc->crtc);
6229026e0d1SMaxime Ripard 		scrtc->event = NULL;
6239026e0d1SMaxime Ripard 	}
6249026e0d1SMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
6259026e0d1SMaxime Ripard }
6269026e0d1SMaxime Ripard 
6279026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private)
6289026e0d1SMaxime Ripard {
6299026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = private;
6309026e0d1SMaxime Ripard 	struct drm_device *drm = tcon->drm;
63146cce6daSChen-Yu Tsai 	struct sun4i_crtc *scrtc = tcon->crtc;
6323004f75fSMaxime Ripard 	struct sunxi_engine *engine = scrtc->engine;
6339026e0d1SMaxime Ripard 	unsigned int status;
6349026e0d1SMaxime Ripard 
6359026e0d1SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
6369026e0d1SMaxime Ripard 
6379026e0d1SMaxime Ripard 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
638a493ceaeSMaxime Ripard 			SUN4I_TCON_GINT0_VBLANK_INT(1) |
639a493ceaeSMaxime Ripard 			SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT)))
6409026e0d1SMaxime Ripard 		return IRQ_NONE;
6419026e0d1SMaxime Ripard 
6429026e0d1SMaxime Ripard 	drm_crtc_handle_vblank(&scrtc->crtc);
6439026e0d1SMaxime Ripard 	sun4i_tcon_finish_page_flip(drm, scrtc);
6449026e0d1SMaxime Ripard 
6459026e0d1SMaxime Ripard 	/* Acknowledge the interrupt */
6469026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
6479026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
648a493ceaeSMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(1) |
649a493ceaeSMaxime Ripard 			   SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT,
6509026e0d1SMaxime Ripard 			   0);
6519026e0d1SMaxime Ripard 
6523004f75fSMaxime Ripard 	if (engine->ops->vblank_quirk)
6533004f75fSMaxime Ripard 		engine->ops->vblank_quirk(engine);
6543004f75fSMaxime Ripard 
6559026e0d1SMaxime Ripard 	return IRQ_HANDLED;
6569026e0d1SMaxime Ripard }
6579026e0d1SMaxime Ripard 
6589026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev,
6599026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
6609026e0d1SMaxime Ripard {
6619026e0d1SMaxime Ripard 	tcon->clk = devm_clk_get(dev, "ahb");
6629026e0d1SMaxime Ripard 	if (IS_ERR(tcon->clk)) {
6639026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON bus clock\n");
6649026e0d1SMaxime Ripard 		return PTR_ERR(tcon->clk);
6659026e0d1SMaxime Ripard 	}
6669026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->clk);
6679026e0d1SMaxime Ripard 
66834d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
6699026e0d1SMaxime Ripard 		tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
6709026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk0)) {
6719026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
6729026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk0);
6739026e0d1SMaxime Ripard 		}
67434d698f6SJernej Skrabec 	}
6759026e0d1SMaxime Ripard 
67691ea2f29SChen-Yu Tsai 	if (tcon->quirks->has_channel_1) {
6779026e0d1SMaxime Ripard 		tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
6789026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk1)) {
6799026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
6809026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk1);
6819026e0d1SMaxime Ripard 		}
6828e924047SMaxime Ripard 	}
6839026e0d1SMaxime Ripard 
6844c7f16d1SChen-Yu Tsai 	return 0;
6859026e0d1SMaxime Ripard }
6869026e0d1SMaxime Ripard 
6879026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
6889026e0d1SMaxime Ripard {
6899026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->clk);
6909026e0d1SMaxime Ripard }
6919026e0d1SMaxime Ripard 
6929026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev,
6939026e0d1SMaxime Ripard 			       struct sun4i_tcon *tcon)
6949026e0d1SMaxime Ripard {
6959026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
6969026e0d1SMaxime Ripard 	int irq, ret;
6979026e0d1SMaxime Ripard 
6989026e0d1SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
6999026e0d1SMaxime Ripard 	if (irq < 0) {
7009026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
7019026e0d1SMaxime Ripard 		return irq;
7029026e0d1SMaxime Ripard 	}
7039026e0d1SMaxime Ripard 
7049026e0d1SMaxime Ripard 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
7059026e0d1SMaxime Ripard 			       dev_name(dev), tcon);
7069026e0d1SMaxime Ripard 	if (ret) {
7079026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't request the IRQ\n");
7089026e0d1SMaxime Ripard 		return ret;
7099026e0d1SMaxime Ripard 	}
7109026e0d1SMaxime Ripard 
7119026e0d1SMaxime Ripard 	return 0;
7129026e0d1SMaxime Ripard }
7139026e0d1SMaxime Ripard 
7149026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = {
7159026e0d1SMaxime Ripard 	.reg_bits	= 32,
7169026e0d1SMaxime Ripard 	.val_bits	= 32,
7179026e0d1SMaxime Ripard 	.reg_stride	= 4,
7189026e0d1SMaxime Ripard 	.max_register	= 0x800,
7199026e0d1SMaxime Ripard };
7209026e0d1SMaxime Ripard 
7219026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev,
7229026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
7239026e0d1SMaxime Ripard {
7249026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
7259026e0d1SMaxime Ripard 	struct resource *res;
7269026e0d1SMaxime Ripard 	void __iomem *regs;
7279026e0d1SMaxime Ripard 
7289026e0d1SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7299026e0d1SMaxime Ripard 	regs = devm_ioremap_resource(dev, res);
730af346f55SWei Yongjun 	if (IS_ERR(regs))
7319026e0d1SMaxime Ripard 		return PTR_ERR(regs);
7329026e0d1SMaxime Ripard 
7339026e0d1SMaxime Ripard 	tcon->regs = devm_regmap_init_mmio(dev, regs,
7349026e0d1SMaxime Ripard 					   &sun4i_tcon_regmap_config);
7359026e0d1SMaxime Ripard 	if (IS_ERR(tcon->regs)) {
7369026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't create the TCON regmap\n");
7379026e0d1SMaxime Ripard 		return PTR_ERR(tcon->regs);
7389026e0d1SMaxime Ripard 	}
7399026e0d1SMaxime Ripard 
7409026e0d1SMaxime Ripard 	/* Make sure the TCON is disabled and all IRQs are off */
7419026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
7429026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
7439026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
7449026e0d1SMaxime Ripard 
7459026e0d1SMaxime Ripard 	/* Disable IO lines and set them to tristate */
7469026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
7479026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
7489026e0d1SMaxime Ripard 
7499026e0d1SMaxime Ripard 	return 0;
7509026e0d1SMaxime Ripard }
7519026e0d1SMaxime Ripard 
752b317fa3bSChen-Yu Tsai /*
753b317fa3bSChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
754b317fa3bSChen-Yu Tsai  * the TCON is always tied to just one backend. Hence we can traverse
755b317fa3bSChen-Yu Tsai  * the of_graph upwards to find the backend our tcon is connected to,
756b317fa3bSChen-Yu Tsai  * and take its ID as our own.
757b317fa3bSChen-Yu Tsai  *
758b317fa3bSChen-Yu Tsai  * We can either identify backends from their compatible strings, which
759b317fa3bSChen-Yu Tsai  * means maintaining a large list of them. Or, since the backend is
760b317fa3bSChen-Yu Tsai  * registered and binded before the TCON, we can just go through the
761b317fa3bSChen-Yu Tsai  * list of registered backends and compare the device node.
76287969338SIcenowy Zheng  *
76387969338SIcenowy Zheng  * As the structures now store engines instead of backends, here this
76487969338SIcenowy Zheng  * function in fact searches the corresponding engine, and the ID is
76587969338SIcenowy Zheng  * requested via the get_id function of the engine.
766b317fa3bSChen-Yu Tsai  */
767e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *
768e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
76949836b11SJernej Skrabec 				struct device_node *node,
77049836b11SJernej Skrabec 				u32 port_id)
771b317fa3bSChen-Yu Tsai {
772b317fa3bSChen-Yu Tsai 	struct device_node *port, *ep, *remote;
773be3fe0f9SChen-Yu Tsai 	struct sunxi_engine *engine = ERR_PTR(-EINVAL);
77449836b11SJernej Skrabec 	u32 reg = 0;
775b317fa3bSChen-Yu Tsai 
77649836b11SJernej Skrabec 	port = of_graph_get_port_by_id(node, port_id);
777b317fa3bSChen-Yu Tsai 	if (!port)
778b317fa3bSChen-Yu Tsai 		return ERR_PTR(-EINVAL);
779b317fa3bSChen-Yu Tsai 
7801469619dSChen-Yu Tsai 	/*
7811469619dSChen-Yu Tsai 	 * This only works if there is only one path from the TCON
7821469619dSChen-Yu Tsai 	 * to any display engine. Otherwise the probe order of the
7831469619dSChen-Yu Tsai 	 * TCONs and display engines is not guaranteed. They may
7841469619dSChen-Yu Tsai 	 * either bind to the wrong one, or worse, bind to the same
7851469619dSChen-Yu Tsai 	 * one if additional checks are not done.
7861469619dSChen-Yu Tsai 	 *
7871469619dSChen-Yu Tsai 	 * Bail out if there are multiple input connections.
7881469619dSChen-Yu Tsai 	 */
789be3fe0f9SChen-Yu Tsai 	if (of_get_available_child_count(port) != 1)
790be3fe0f9SChen-Yu Tsai 		goto out_put_port;
7911469619dSChen-Yu Tsai 
792be3fe0f9SChen-Yu Tsai 	/* Get the first connection without specifying an ID */
793be3fe0f9SChen-Yu Tsai 	ep = of_get_next_available_child(port, NULL);
794be3fe0f9SChen-Yu Tsai 	if (!ep)
795be3fe0f9SChen-Yu Tsai 		goto out_put_port;
796be3fe0f9SChen-Yu Tsai 
797b317fa3bSChen-Yu Tsai 	remote = of_graph_get_remote_port_parent(ep);
798b317fa3bSChen-Yu Tsai 	if (!remote)
799be3fe0f9SChen-Yu Tsai 		goto out_put_ep;
800b317fa3bSChen-Yu Tsai 
80187969338SIcenowy Zheng 	/* does this node match any registered engines? */
802be3fe0f9SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
803be3fe0f9SChen-Yu Tsai 		if (remote == engine->node)
804be3fe0f9SChen-Yu Tsai 			goto out_put_remote;
805b317fa3bSChen-Yu Tsai 
80649836b11SJernej Skrabec 	/*
80749836b11SJernej Skrabec 	 * According to device tree binding input ports have even id
80849836b11SJernej Skrabec 	 * number and output ports have odd id. Since component with
80949836b11SJernej Skrabec 	 * more than one input and one output (TCON TOP) exits, correct
81049836b11SJernej Skrabec 	 * remote input id has to be calculated by subtracting 1 from
81149836b11SJernej Skrabec 	 * remote output id. If this for some reason can't be done, 0
81249836b11SJernej Skrabec 	 * is used as input port id.
81349836b11SJernej Skrabec 	 */
814*da82107eSJernej Skrabec 	of_node_put(port);
81549836b11SJernej Skrabec 	port = of_graph_get_remote_port(ep);
81649836b11SJernej Skrabec 	if (!of_property_read_u32(port, "reg", &reg) && reg > 0)
81749836b11SJernej Skrabec 		reg -= 1;
81849836b11SJernej Skrabec 
819b317fa3bSChen-Yu Tsai 	/* keep looking through upstream ports */
82049836b11SJernej Skrabec 	engine = sun4i_tcon_find_engine_traverse(drv, remote, reg);
821b317fa3bSChen-Yu Tsai 
822be3fe0f9SChen-Yu Tsai out_put_remote:
823be3fe0f9SChen-Yu Tsai 	of_node_put(remote);
824be3fe0f9SChen-Yu Tsai out_put_ep:
825be3fe0f9SChen-Yu Tsai 	of_node_put(ep);
826be3fe0f9SChen-Yu Tsai out_put_port:
827be3fe0f9SChen-Yu Tsai 	of_node_put(port);
828be3fe0f9SChen-Yu Tsai 
829be3fe0f9SChen-Yu Tsai 	return engine;
830b317fa3bSChen-Yu Tsai }
831b317fa3bSChen-Yu Tsai 
832e8d5bbf7SChen-Yu Tsai /*
833e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
834e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
835e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
836e8d5bbf7SChen-Yu Tsai  * component. Thus we can look at any one of the input connections of
837e8d5bbf7SChen-Yu Tsai  * the TCONs, and use that connection's remote endpoint ID as our own.
838e8d5bbf7SChen-Yu Tsai  *
839e8d5bbf7SChen-Yu Tsai  * Since the user of this function already finds the input port,
840e8d5bbf7SChen-Yu Tsai  * the port is passed in directly without further checks.
841e8d5bbf7SChen-Yu Tsai  */
842e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
843e8d5bbf7SChen-Yu Tsai {
844e8d5bbf7SChen-Yu Tsai 	struct device_node *ep;
845e8d5bbf7SChen-Yu Tsai 	int ret = -EINVAL;
846e8d5bbf7SChen-Yu Tsai 
847e8d5bbf7SChen-Yu Tsai 	/* try finding an upstream endpoint */
848e8d5bbf7SChen-Yu Tsai 	for_each_available_child_of_node(port, ep) {
849e8d5bbf7SChen-Yu Tsai 		struct device_node *remote;
850e8d5bbf7SChen-Yu Tsai 		u32 reg;
851e8d5bbf7SChen-Yu Tsai 
852e8d5bbf7SChen-Yu Tsai 		remote = of_graph_get_remote_endpoint(ep);
853e8d5bbf7SChen-Yu Tsai 		if (!remote)
854e8d5bbf7SChen-Yu Tsai 			continue;
855e8d5bbf7SChen-Yu Tsai 
856e8d5bbf7SChen-Yu Tsai 		ret = of_property_read_u32(remote, "reg", &reg);
857e8d5bbf7SChen-Yu Tsai 		if (ret)
858e8d5bbf7SChen-Yu Tsai 			continue;
859e8d5bbf7SChen-Yu Tsai 
860e8d5bbf7SChen-Yu Tsai 		ret = reg;
861e8d5bbf7SChen-Yu Tsai 	}
862e8d5bbf7SChen-Yu Tsai 
863e8d5bbf7SChen-Yu Tsai 	return ret;
864e8d5bbf7SChen-Yu Tsai }
865e8d5bbf7SChen-Yu Tsai 
866e8d5bbf7SChen-Yu Tsai /*
867e8d5bbf7SChen-Yu Tsai  * Once we know the TCON's id, we can look through the list of
868e8d5bbf7SChen-Yu Tsai  * engines to find a matching one. We assume all engines have
869e8d5bbf7SChen-Yu Tsai  * been probed and added to the list.
870e8d5bbf7SChen-Yu Tsai  */
871e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
872e8d5bbf7SChen-Yu Tsai 							int id)
873e8d5bbf7SChen-Yu Tsai {
874e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
875e8d5bbf7SChen-Yu Tsai 
876e8d5bbf7SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
877e8d5bbf7SChen-Yu Tsai 		if (engine->id == id)
878e8d5bbf7SChen-Yu Tsai 			return engine;
879e8d5bbf7SChen-Yu Tsai 
880e8d5bbf7SChen-Yu Tsai 	return ERR_PTR(-EINVAL);
881e8d5bbf7SChen-Yu Tsai }
882e8d5bbf7SChen-Yu Tsai 
883e8d5bbf7SChen-Yu Tsai /*
884e8d5bbf7SChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
885e8d5bbf7SChen-Yu Tsai  * we assumed the TCON was always tied to just one backend. However
886e8d5bbf7SChen-Yu Tsai  * this proved not to be the case. On the A31, the TCON can select
887e8d5bbf7SChen-Yu Tsai  * either backend as its source. On the A20 (and likely on the A10),
888e8d5bbf7SChen-Yu Tsai  * the backend can choose which TCON to output to.
889e8d5bbf7SChen-Yu Tsai  *
890e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
891e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
892e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
893e8d5bbf7SChen-Yu Tsai  * component. Thus we should be able to look at any one of the input
894e8d5bbf7SChen-Yu Tsai  * connections of the TCONs, and use that connection's remote endpoint
895e8d5bbf7SChen-Yu Tsai  * ID as our own.
896e8d5bbf7SChen-Yu Tsai  *
897e8d5bbf7SChen-Yu Tsai  * However  the connections between the backend and TCON were assumed
898e8d5bbf7SChen-Yu Tsai  * to be always singular, and their endpoit IDs were all incorrectly
899e8d5bbf7SChen-Yu Tsai  * set to 0. This means for these old device trees, we cannot just look
900e8d5bbf7SChen-Yu Tsai  * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
901e8d5bbf7SChen-Yu Tsai  * incorrectly identified as TCON0.
902e8d5bbf7SChen-Yu Tsai  *
903e8d5bbf7SChen-Yu Tsai  * This function first checks if the TCON node has 2 input endpoints.
904e8d5bbf7SChen-Yu Tsai  * If so, then the device tree is a corrected version, and it will use
905e8d5bbf7SChen-Yu Tsai  * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
906e8d5bbf7SChen-Yu Tsai  * to fetch the ID and engine directly. If not, then it is likely an
907e8d5bbf7SChen-Yu Tsai  * old device trees, where the endpoint IDs were incorrect, but did not
908e8d5bbf7SChen-Yu Tsai  * have endpoint connections between the backend and TCON across
909e8d5bbf7SChen-Yu Tsai  * different display pipelines. It will fall back to the old method of
910e8d5bbf7SChen-Yu Tsai  * traversing the  of_graph to try and find a matching engine by device
911e8d5bbf7SChen-Yu Tsai  * node.
912e8d5bbf7SChen-Yu Tsai  *
913e8d5bbf7SChen-Yu Tsai  * In the case of single display pipeline device trees, either method
914e8d5bbf7SChen-Yu Tsai  * works.
915e8d5bbf7SChen-Yu Tsai  */
916e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
917e8d5bbf7SChen-Yu Tsai 						   struct device_node *node)
918e8d5bbf7SChen-Yu Tsai {
919e8d5bbf7SChen-Yu Tsai 	struct device_node *port;
920e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
921e8d5bbf7SChen-Yu Tsai 
922e8d5bbf7SChen-Yu Tsai 	port = of_graph_get_port_by_id(node, 0);
923e8d5bbf7SChen-Yu Tsai 	if (!port)
924e8d5bbf7SChen-Yu Tsai 		return ERR_PTR(-EINVAL);
925e8d5bbf7SChen-Yu Tsai 
926e8d5bbf7SChen-Yu Tsai 	/*
927e8d5bbf7SChen-Yu Tsai 	 * Is this a corrected device tree with cross pipeline
928e8d5bbf7SChen-Yu Tsai 	 * connections between the backend and TCON?
929e8d5bbf7SChen-Yu Tsai 	 */
930e8d5bbf7SChen-Yu Tsai 	if (of_get_child_count(port) > 1) {
931e8d5bbf7SChen-Yu Tsai 		/* Get our ID directly from an upstream endpoint */
932e8d5bbf7SChen-Yu Tsai 		int id = sun4i_tcon_of_get_id_from_port(port);
933e8d5bbf7SChen-Yu Tsai 
934e8d5bbf7SChen-Yu Tsai 		/* Get our engine by matching our ID */
935e8d5bbf7SChen-Yu Tsai 		engine = sun4i_tcon_get_engine_by_id(drv, id);
936e8d5bbf7SChen-Yu Tsai 
937e8d5bbf7SChen-Yu Tsai 		of_node_put(port);
938e8d5bbf7SChen-Yu Tsai 		return engine;
939e8d5bbf7SChen-Yu Tsai 	}
940e8d5bbf7SChen-Yu Tsai 
941e8d5bbf7SChen-Yu Tsai 	/* Fallback to old method by traversing input endpoints */
942e8d5bbf7SChen-Yu Tsai 	of_node_put(port);
94349836b11SJernej Skrabec 	return sun4i_tcon_find_engine_traverse(drv, node, 0);
944e8d5bbf7SChen-Yu Tsai }
945e8d5bbf7SChen-Yu Tsai 
9469026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master,
9479026e0d1SMaxime Ripard 			   void *data)
9489026e0d1SMaxime Ripard {
9499026e0d1SMaxime Ripard 	struct drm_device *drm = data;
9509026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
95187969338SIcenowy Zheng 	struct sunxi_engine *engine;
952a0c1214eSMaxime Ripard 	struct device_node *remote;
9539026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon;
9546664e9dcSChen-Yu Tsai 	struct reset_control *edp_rstc;
955a0c1214eSMaxime Ripard 	bool has_lvds_rst, has_lvds_alt, can_lvds;
9569026e0d1SMaxime Ripard 	int ret;
9579026e0d1SMaxime Ripard 
95887969338SIcenowy Zheng 	engine = sun4i_tcon_find_engine(drv, dev->of_node);
95987969338SIcenowy Zheng 	if (IS_ERR(engine)) {
96087969338SIcenowy Zheng 		dev_err(dev, "Couldn't find matching engine\n");
96180a58240SChen-Yu Tsai 		return -EPROBE_DEFER;
962b317fa3bSChen-Yu Tsai 	}
96380a58240SChen-Yu Tsai 
9649026e0d1SMaxime Ripard 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
9659026e0d1SMaxime Ripard 	if (!tcon)
9669026e0d1SMaxime Ripard 		return -ENOMEM;
9679026e0d1SMaxime Ripard 	dev_set_drvdata(dev, tcon);
9689026e0d1SMaxime Ripard 	tcon->drm = drm;
969ae558110SMaxime Ripard 	tcon->dev = dev;
97087969338SIcenowy Zheng 	tcon->id = engine->id;
97191ea2f29SChen-Yu Tsai 	tcon->quirks = of_device_get_match_data(dev);
9729026e0d1SMaxime Ripard 
9739026e0d1SMaxime Ripard 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
9749026e0d1SMaxime Ripard 	if (IS_ERR(tcon->lcd_rst)) {
9759026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
9769026e0d1SMaxime Ripard 		return PTR_ERR(tcon->lcd_rst);
9779026e0d1SMaxime Ripard 	}
9789026e0d1SMaxime Ripard 
9796664e9dcSChen-Yu Tsai 	if (tcon->quirks->needs_edp_reset) {
9806664e9dcSChen-Yu Tsai 		edp_rstc = devm_reset_control_get_shared(dev, "edp");
9816664e9dcSChen-Yu Tsai 		if (IS_ERR(edp_rstc)) {
9826664e9dcSChen-Yu Tsai 			dev_err(dev, "Couldn't get edp reset line\n");
9836664e9dcSChen-Yu Tsai 			return PTR_ERR(edp_rstc);
9846664e9dcSChen-Yu Tsai 		}
9856664e9dcSChen-Yu Tsai 
9866664e9dcSChen-Yu Tsai 		ret = reset_control_deassert(edp_rstc);
9876664e9dcSChen-Yu Tsai 		if (ret) {
9886664e9dcSChen-Yu Tsai 			dev_err(dev, "Couldn't deassert edp reset line\n");
9896664e9dcSChen-Yu Tsai 			return ret;
9906664e9dcSChen-Yu Tsai 		}
9916664e9dcSChen-Yu Tsai 	}
9926664e9dcSChen-Yu Tsai 
9939026e0d1SMaxime Ripard 	/* Make sure our TCON is reset */
994d57294c1SChen-Yu Tsai 	ret = reset_control_reset(tcon->lcd_rst);
9959026e0d1SMaxime Ripard 	if (ret) {
9969026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't deassert our reset line\n");
9979026e0d1SMaxime Ripard 		return ret;
9989026e0d1SMaxime Ripard 	}
9999026e0d1SMaxime Ripard 
1000e742a17cSMaxime Ripard 	if (tcon->quirks->supports_lvds) {
1001a0c1214eSMaxime Ripard 		/*
1002e742a17cSMaxime Ripard 		 * This can only be made optional since we've had DT
1003e742a17cSMaxime Ripard 		 * nodes without the LVDS reset properties.
1004a0c1214eSMaxime Ripard 		 *
1005e742a17cSMaxime Ripard 		 * If the property is missing, just disable LVDS, and
1006e742a17cSMaxime Ripard 		 * print a warning.
1007a0c1214eSMaxime Ripard 		 */
1008a0c1214eSMaxime Ripard 		tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
1009a0c1214eSMaxime Ripard 		if (IS_ERR(tcon->lvds_rst)) {
1010a0c1214eSMaxime Ripard 			dev_err(dev, "Couldn't get our reset line\n");
1011a0c1214eSMaxime Ripard 			return PTR_ERR(tcon->lvds_rst);
1012a0c1214eSMaxime Ripard 		} else if (tcon->lvds_rst) {
1013a0c1214eSMaxime Ripard 			has_lvds_rst = true;
1014a0c1214eSMaxime Ripard 			reset_control_reset(tcon->lvds_rst);
1015a0c1214eSMaxime Ripard 		} else {
1016a0c1214eSMaxime Ripard 			has_lvds_rst = false;
1017a0c1214eSMaxime Ripard 		}
1018a0c1214eSMaxime Ripard 
1019a0c1214eSMaxime Ripard 		/*
1020e742a17cSMaxime Ripard 		 * This can only be made optional since we've had DT
1021e742a17cSMaxime Ripard 		 * nodes without the LVDS reset properties.
1022a0c1214eSMaxime Ripard 		 *
1023e742a17cSMaxime Ripard 		 * If the property is missing, just disable LVDS, and
1024e742a17cSMaxime Ripard 		 * print a warning.
1025a0c1214eSMaxime Ripard 		 */
1026a0c1214eSMaxime Ripard 		if (tcon->quirks->has_lvds_alt) {
1027a0c1214eSMaxime Ripard 			tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
1028a0c1214eSMaxime Ripard 			if (IS_ERR(tcon->lvds_pll)) {
1029a0c1214eSMaxime Ripard 				if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
1030a0c1214eSMaxime Ripard 					has_lvds_alt = false;
1031a0c1214eSMaxime Ripard 				} else {
1032a0c1214eSMaxime Ripard 					dev_err(dev, "Couldn't get the LVDS PLL\n");
103386a3ae58SDan Carpenter 					return PTR_ERR(tcon->lvds_pll);
1034a0c1214eSMaxime Ripard 				}
1035a0c1214eSMaxime Ripard 			} else {
1036a0c1214eSMaxime Ripard 				has_lvds_alt = true;
1037a0c1214eSMaxime Ripard 			}
1038a0c1214eSMaxime Ripard 		}
1039a0c1214eSMaxime Ripard 
1040e742a17cSMaxime Ripard 		if (!has_lvds_rst ||
1041e742a17cSMaxime Ripard 		    (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
1042e742a17cSMaxime Ripard 			dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
1043a0c1214eSMaxime Ripard 			dev_warn(dev, "LVDS output disabled\n");
1044a0c1214eSMaxime Ripard 			can_lvds = false;
1045a0c1214eSMaxime Ripard 		} else {
1046a0c1214eSMaxime Ripard 			can_lvds = true;
1047a0c1214eSMaxime Ripard 		}
1048e742a17cSMaxime Ripard 	} else {
1049e742a17cSMaxime Ripard 		can_lvds = false;
1050e742a17cSMaxime Ripard 	}
1051a0c1214eSMaxime Ripard 
10529026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_clocks(dev, tcon);
10539026e0d1SMaxime Ripard 	if (ret) {
10549026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON clocks\n");
10559026e0d1SMaxime Ripard 		goto err_assert_reset;
10569026e0d1SMaxime Ripard 	}
10579026e0d1SMaxime Ripard 
10584c7f16d1SChen-Yu Tsai 	ret = sun4i_tcon_init_regmap(dev, tcon);
10599026e0d1SMaxime Ripard 	if (ret) {
10604c7f16d1SChen-Yu Tsai 		dev_err(dev, "Couldn't init our TCON regmap\n");
10619026e0d1SMaxime Ripard 		goto err_free_clocks;
10629026e0d1SMaxime Ripard 	}
10639026e0d1SMaxime Ripard 
106434d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
10654c7f16d1SChen-Yu Tsai 		ret = sun4i_dclk_create(dev, tcon);
10664c7f16d1SChen-Yu Tsai 		if (ret) {
10674c7f16d1SChen-Yu Tsai 			dev_err(dev, "Couldn't create our TCON dot clock\n");
10684c7f16d1SChen-Yu Tsai 			goto err_free_clocks;
10694c7f16d1SChen-Yu Tsai 		}
107034d698f6SJernej Skrabec 	}
10714c7f16d1SChen-Yu Tsai 
10729026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_irq(dev, tcon);
10739026e0d1SMaxime Ripard 	if (ret) {
10749026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON interrupts\n");
10754c7f16d1SChen-Yu Tsai 		goto err_free_dotclock;
10769026e0d1SMaxime Ripard 	}
10779026e0d1SMaxime Ripard 
107887969338SIcenowy Zheng 	tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
107946cce6daSChen-Yu Tsai 	if (IS_ERR(tcon->crtc)) {
108046cce6daSChen-Yu Tsai 		dev_err(dev, "Couldn't create our CRTC\n");
108146cce6daSChen-Yu Tsai 		ret = PTR_ERR(tcon->crtc);
108292411f6dSMaxime Ripard 		goto err_free_dotclock;
108346cce6daSChen-Yu Tsai 	}
108446cce6daSChen-Yu Tsai 
10852a72d0c5SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
1086a0c1214eSMaxime Ripard 		/*
1087a0c1214eSMaxime Ripard 		 * If we have an LVDS panel connected to the TCON, we should
1088a0c1214eSMaxime Ripard 		 * just probe the LVDS connector. Otherwise, just probe RGB as
1089a0c1214eSMaxime Ripard 		 * we used to.
1090a0c1214eSMaxime Ripard 		 */
1091a0c1214eSMaxime Ripard 		remote = of_graph_get_remote_node(dev->of_node, 1, 0);
1092a0c1214eSMaxime Ripard 		if (of_device_is_compatible(remote, "panel-lvds"))
1093a0c1214eSMaxime Ripard 			if (can_lvds)
1094a0c1214eSMaxime Ripard 				ret = sun4i_lvds_init(drm, tcon);
1095a0c1214eSMaxime Ripard 			else
1096a0c1214eSMaxime Ripard 				ret = -EINVAL;
1097a0c1214eSMaxime Ripard 		else
1098b9c8506cSChen-Yu Tsai 			ret = sun4i_rgb_init(drm, tcon);
1099a0c1214eSMaxime Ripard 		of_node_put(remote);
1100a0c1214eSMaxime Ripard 
110113fef095SChen-Yu Tsai 		if (ret < 0)
110292411f6dSMaxime Ripard 			goto err_free_dotclock;
11032a72d0c5SJernej Skrabec 	}
110413fef095SChen-Yu Tsai 
110527e18de7SChen-Yu Tsai 	if (tcon->quirks->needs_de_be_mux) {
110627e18de7SChen-Yu Tsai 		/*
110727e18de7SChen-Yu Tsai 		 * We assume there is no dynamic muxing of backends
110827e18de7SChen-Yu Tsai 		 * and TCONs, so we select the backend with same ID.
110927e18de7SChen-Yu Tsai 		 *
111027e18de7SChen-Yu Tsai 		 * While dynamic selection might be interesting, since
111127e18de7SChen-Yu Tsai 		 * the CRTC is tied to the TCON, while the layers are
111227e18de7SChen-Yu Tsai 		 * tied to the backends, this means, we will need to
111327e18de7SChen-Yu Tsai 		 * switch between groups of layers. There might not be
111427e18de7SChen-Yu Tsai 		 * a way to represent this constraint in DRM.
111527e18de7SChen-Yu Tsai 		 */
111627e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
111727e18de7SChen-Yu Tsai 				   SUN4I_TCON0_CTL_SRC_SEL_MASK,
111827e18de7SChen-Yu Tsai 				   tcon->id);
111927e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
112027e18de7SChen-Yu Tsai 				   SUN4I_TCON1_CTL_SRC_SEL_MASK,
112127e18de7SChen-Yu Tsai 				   tcon->id);
112227e18de7SChen-Yu Tsai 	}
112327e18de7SChen-Yu Tsai 
112480a58240SChen-Yu Tsai 	list_add_tail(&tcon->list, &drv->tcon_list);
112580a58240SChen-Yu Tsai 
112613fef095SChen-Yu Tsai 	return 0;
11279026e0d1SMaxime Ripard 
11284c7f16d1SChen-Yu Tsai err_free_dotclock:
112934d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
11304c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
11319026e0d1SMaxime Ripard err_free_clocks:
11329026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
11339026e0d1SMaxime Ripard err_assert_reset:
11349026e0d1SMaxime Ripard 	reset_control_assert(tcon->lcd_rst);
11359026e0d1SMaxime Ripard 	return ret;
11369026e0d1SMaxime Ripard }
11379026e0d1SMaxime Ripard 
11389026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master,
11399026e0d1SMaxime Ripard 			      void *data)
11409026e0d1SMaxime Ripard {
11419026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
11429026e0d1SMaxime Ripard 
114380a58240SChen-Yu Tsai 	list_del(&tcon->list);
114434d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
11454c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
11469026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
11479026e0d1SMaxime Ripard }
11489026e0d1SMaxime Ripard 
1149dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = {
11509026e0d1SMaxime Ripard 	.bind	= sun4i_tcon_bind,
11519026e0d1SMaxime Ripard 	.unbind	= sun4i_tcon_unbind,
11529026e0d1SMaxime Ripard };
11539026e0d1SMaxime Ripard 
11549026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev)
11559026e0d1SMaxime Ripard {
115629e57fabSMaxime Ripard 	struct device_node *node = pdev->dev.of_node;
115763d6310fSJernej Skrabec 	const struct sun4i_tcon_quirks *quirks;
1158894f5a9fSMaxime Ripard 	struct drm_bridge *bridge;
115929e57fabSMaxime Ripard 	struct drm_panel *panel;
1160ebc94461SRob Herring 	int ret;
116129e57fabSMaxime Ripard 
116263d6310fSJernej Skrabec 	quirks = of_device_get_match_data(&pdev->dev);
116363d6310fSJernej Skrabec 
116463d6310fSJernej Skrabec 	/* panels and bridges are present only on TCONs with channel 0 */
116563d6310fSJernej Skrabec 	if (quirks->has_channel_0) {
1166ebc94461SRob Herring 		ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1167ebc94461SRob Herring 		if (ret == -EPROBE_DEFER)
1168ebc94461SRob Herring 			return ret;
116963d6310fSJernej Skrabec 	}
117029e57fabSMaxime Ripard 
11719026e0d1SMaxime Ripard 	return component_add(&pdev->dev, &sun4i_tcon_ops);
11729026e0d1SMaxime Ripard }
11739026e0d1SMaxime Ripard 
11749026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev)
11759026e0d1SMaxime Ripard {
11769026e0d1SMaxime Ripard 	component_del(&pdev->dev, &sun4i_tcon_ops);
11779026e0d1SMaxime Ripard 
11789026e0d1SMaxime Ripard 	return 0;
11799026e0d1SMaxime Ripard }
11809026e0d1SMaxime Ripard 
1181ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */
11824bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
11834bb206bfSJonathan Liu 				  const struct drm_encoder *encoder)
11844bb206bfSJonathan Liu {
11854bb206bfSJonathan Liu 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
11864bb206bfSJonathan Liu 	u32 shift;
11874bb206bfSJonathan Liu 
11884bb206bfSJonathan Liu 	if (!tcon0)
11894bb206bfSJonathan Liu 		return -EINVAL;
11904bb206bfSJonathan Liu 
11914bb206bfSJonathan Liu 	switch (encoder->encoder_type) {
11924bb206bfSJonathan Liu 	case DRM_MODE_ENCODER_TMDS:
11934bb206bfSJonathan Liu 		/* HDMI */
11944bb206bfSJonathan Liu 		shift = 8;
11954bb206bfSJonathan Liu 		break;
11964bb206bfSJonathan Liu 	default:
11974bb206bfSJonathan Liu 		return -EINVAL;
11984bb206bfSJonathan Liu 	}
11994bb206bfSJonathan Liu 
12004bb206bfSJonathan Liu 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
12014bb206bfSJonathan Liu 			   0x3 << shift, tcon->id << shift);
12024bb206bfSJonathan Liu 
12034bb206bfSJonathan Liu 	return 0;
12044bb206bfSJonathan Liu }
12054bb206bfSJonathan Liu 
1206ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
1207abcb8766SMaxime Ripard 				  const struct drm_encoder *encoder)
1208ad537fb2SChen-Yu Tsai {
1209ad537fb2SChen-Yu Tsai 	u32 val;
1210ad537fb2SChen-Yu Tsai 
1211ad537fb2SChen-Yu Tsai 	if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1212ad537fb2SChen-Yu Tsai 		val = 1;
1213ad537fb2SChen-Yu Tsai 	else
1214ad537fb2SChen-Yu Tsai 		val = 0;
1215ad537fb2SChen-Yu Tsai 
1216ad537fb2SChen-Yu Tsai 	/*
1217ad537fb2SChen-Yu Tsai 	 * FIXME: Undocumented bits
1218ad537fb2SChen-Yu Tsai 	 */
1219ad537fb2SChen-Yu Tsai 	return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1220ad537fb2SChen-Yu Tsai }
1221ad537fb2SChen-Yu Tsai 
122267e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
1223abcb8766SMaxime Ripard 			      const struct drm_encoder *encoder)
122467e32645SChen-Yu Tsai {
122567e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
122667e32645SChen-Yu Tsai 	u32 shift;
122767e32645SChen-Yu Tsai 
122867e32645SChen-Yu Tsai 	if (!tcon0)
122967e32645SChen-Yu Tsai 		return -EINVAL;
123067e32645SChen-Yu Tsai 
123167e32645SChen-Yu Tsai 	switch (encoder->encoder_type) {
123267e32645SChen-Yu Tsai 	case DRM_MODE_ENCODER_TMDS:
123367e32645SChen-Yu Tsai 		/* HDMI */
123467e32645SChen-Yu Tsai 		shift = 8;
123567e32645SChen-Yu Tsai 		break;
123667e32645SChen-Yu Tsai 	default:
123767e32645SChen-Yu Tsai 		/* TODO A31 has MIPI DSI but A31s does not */
123867e32645SChen-Yu Tsai 		return -EINVAL;
123967e32645SChen-Yu Tsai 	}
124067e32645SChen-Yu Tsai 
124167e32645SChen-Yu Tsai 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
124267e32645SChen-Yu Tsai 			   0x3 << shift, tcon->id << shift);
124367e32645SChen-Yu Tsai 
124467e32645SChen-Yu Tsai 	return 0;
124567e32645SChen-Yu Tsai }
124667e32645SChen-Yu Tsai 
12474bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
124834d698f6SJernej Skrabec 	.has_channel_0		= true,
12494bb206bfSJonathan Liu 	.has_channel_1		= true,
12504bb206bfSJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
12514bb206bfSJonathan Liu };
12524bb206bfSJonathan Liu 
125391ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
125434d698f6SJernej Skrabec 	.has_channel_0		= true,
125591ea2f29SChen-Yu Tsai 	.has_channel_1		= true,
1256ad537fb2SChen-Yu Tsai 	.set_mux		= sun5i_a13_tcon_set_mux,
125791ea2f29SChen-Yu Tsai };
125891ea2f29SChen-Yu Tsai 
125993a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
126034d698f6SJernej Skrabec 	.has_channel_0		= true,
126193a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
1262a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
126327e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
126467e32645SChen-Yu Tsai 	.set_mux		= sun6i_tcon_set_mux,
126593a5ec14SChen-Yu Tsai };
126693a5ec14SChen-Yu Tsai 
126793a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
126834d698f6SJernej Skrabec 	.has_channel_0		= true,
126993a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
127027e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
127193a5ec14SChen-Yu Tsai };
127293a5ec14SChen-Yu Tsai 
1273aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
127434d698f6SJernej Skrabec 	.has_channel_0		= true,
1275aaddb6d2SJonathan Liu 	.has_channel_1		= true,
1276aaddb6d2SJonathan Liu 	/* Same display pipeline structure as A10 */
1277aaddb6d2SJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
1278aaddb6d2SJonathan Liu };
1279aaddb6d2SJonathan Liu 
128091ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
128134d698f6SJernej Skrabec 	.has_channel_0		= true,
1282a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
128391ea2f29SChen-Yu Tsai };
128491ea2f29SChen-Yu Tsai 
12852f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
1286e742a17cSMaxime Ripard 	.supports_lvds		= true,
128734d698f6SJernej Skrabec 	.has_channel_0		= true,
12882f0d7bb1SMaxime Ripard };
12892f0d7bb1SMaxime Ripard 
129005adc89bSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
129105adc89bSJernej Skrabec 	.has_channel_1		= true,
129205adc89bSJernej Skrabec };
129305adc89bSJernej Skrabec 
12941a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
129534d698f6SJernej Skrabec 	.has_channel_0		= true,
12961a0edb3fSIcenowy Zheng };
12971a0edb3fSIcenowy Zheng 
12986664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
12996664e9dcSChen-Yu Tsai 	.has_channel_0	= true,
13006664e9dcSChen-Yu Tsai 	.needs_edp_reset = true,
13016664e9dcSChen-Yu Tsai };
13026664e9dcSChen-Yu Tsai 
13036664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
13046664e9dcSChen-Yu Tsai 	.has_channel_1	= true,
13056664e9dcSChen-Yu Tsai 	.needs_edp_reset = true,
13066664e9dcSChen-Yu Tsai };
13076664e9dcSChen-Yu Tsai 
1308ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */
1309ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = {
13104bb206bfSJonathan Liu 	{ .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
131191ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
131293a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
131393a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
1314aaddb6d2SJonathan Liu 	{ .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
131591ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
13162f0d7bb1SMaxime Ripard 	{ .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
131705adc89bSJernej Skrabec 	{ .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
13181a0edb3fSIcenowy Zheng 	{ .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
13196664e9dcSChen-Yu Tsai 	{ .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
13206664e9dcSChen-Yu Tsai 	{ .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
13219026e0d1SMaxime Ripard 	{ }
13229026e0d1SMaxime Ripard };
13239026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
1324ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table);
13259026e0d1SMaxime Ripard 
13269026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = {
13279026e0d1SMaxime Ripard 	.probe		= sun4i_tcon_probe,
13289026e0d1SMaxime Ripard 	.remove		= sun4i_tcon_remove,
13299026e0d1SMaxime Ripard 	.driver		= {
13309026e0d1SMaxime Ripard 		.name		= "sun4i-tcon",
13319026e0d1SMaxime Ripard 		.of_match_table	= sun4i_tcon_of_table,
13329026e0d1SMaxime Ripard 	},
13339026e0d1SMaxime Ripard };
13349026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver);
13359026e0d1SMaxime Ripard 
13369026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
13379026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
13389026e0d1SMaxime Ripard MODULE_LICENSE("GPL");
1339