xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision 9c25a297664518b36933dae0887cbea3008fb840)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29026e0d1SMaxime Ripard /*
39026e0d1SMaxime Ripard  * Copyright (C) 2015 Free Electrons
49026e0d1SMaxime Ripard  * Copyright (C) 2015 NextThing Co
59026e0d1SMaxime Ripard  *
69026e0d1SMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
79026e0d1SMaxime Ripard  */
89026e0d1SMaxime Ripard 
9*9c25a297SSam Ravnborg #include <linux/component.h>
10*9c25a297SSam Ravnborg #include <linux/ioport.h>
11*9c25a297SSam Ravnborg #include <linux/module.h>
12*9c25a297SSam Ravnborg #include <linux/of_address.h>
13*9c25a297SSam Ravnborg #include <linux/of_device.h>
14*9c25a297SSam Ravnborg #include <linux/of_irq.h>
15*9c25a297SSam Ravnborg #include <linux/regmap.h>
16*9c25a297SSam Ravnborg #include <linux/reset.h>
17*9c25a297SSam Ravnborg 
189026e0d1SMaxime Ripard #include <drm/drm_atomic_helper.h>
19f11adcecSJonathan Liu #include <drm/drm_connector.h>
209026e0d1SMaxime Ripard #include <drm/drm_crtc.h>
21ad537fb2SChen-Yu Tsai #include <drm/drm_encoder.h>
229026e0d1SMaxime Ripard #include <drm/drm_modes.h>
23ebc94461SRob Herring #include <drm/drm_of.h>
24490cda5aSGiulio Benetti #include <drm/drm_panel.h>
25*9c25a297SSam Ravnborg #include <drm/drm_print.h>
26fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
27*9c25a297SSam Ravnborg #include <drm/drm_vblank.h>
289026e0d1SMaxime Ripard 
29ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h>
30ad537fb2SChen-Yu Tsai 
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"
38cf77d79bSJernej Skrabec #include "sun8i_tcon_top.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 
2371f2f0599SYueHaibing static 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 
279f11adcecSJonathan Liu static void sun4i_tcon0_mode_set_dithering(struct sun4i_tcon *tcon,
280f11adcecSJonathan Liu 					   const struct drm_connector *connector)
281f11adcecSJonathan Liu {
282f11adcecSJonathan Liu 	u32 bus_format = 0;
283f11adcecSJonathan Liu 	u32 val = 0;
284f11adcecSJonathan Liu 
285f11adcecSJonathan Liu 	/* XXX Would this ever happen? */
286f11adcecSJonathan Liu 	if (!connector)
287f11adcecSJonathan Liu 		return;
288f11adcecSJonathan Liu 
289f11adcecSJonathan Liu 	/*
290f11adcecSJonathan Liu 	 * FIXME: Undocumented bits
291f11adcecSJonathan Liu 	 *
292f11adcecSJonathan Liu 	 * The whole dithering process and these parameters are not
293f11adcecSJonathan Liu 	 * explained in the vendor documents or BSP kernel code.
294f11adcecSJonathan Liu 	 */
295f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PR_REG, 0x11111111);
296f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PG_REG, 0x11111111);
297f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PB_REG, 0x11111111);
298f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LR_REG, 0x11111111);
299f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LG_REG, 0x11111111);
300f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LB_REG, 0x11111111);
301f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL0_REG, 0x01010000);
302f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL1_REG, 0x15151111);
303f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL2_REG, 0x57575555);
304f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL3_REG, 0x7f7f7777);
305f11adcecSJonathan Liu 
306f11adcecSJonathan Liu 	/* Do dithering if panel only supports 6 bits per color */
307f11adcecSJonathan Liu 	if (connector->display_info.bpc == 6)
308f11adcecSJonathan Liu 		val |= SUN4I_TCON0_FRM_CTL_EN;
309f11adcecSJonathan Liu 
310f11adcecSJonathan Liu 	if (connector->display_info.num_bus_formats == 1)
311f11adcecSJonathan Liu 		bus_format = connector->display_info.bus_formats[0];
312f11adcecSJonathan Liu 
313f11adcecSJonathan Liu 	/* Check the connection format */
314f11adcecSJonathan Liu 	switch (bus_format) {
315f11adcecSJonathan Liu 	case MEDIA_BUS_FMT_RGB565_1X16:
316f11adcecSJonathan Liu 		/* R and B components are only 5 bits deep */
317f11adcecSJonathan Liu 		val |= SUN4I_TCON0_FRM_CTL_MODE_R;
318f11adcecSJonathan Liu 		val |= SUN4I_TCON0_FRM_CTL_MODE_B;
319f11adcecSJonathan Liu 	case MEDIA_BUS_FMT_RGB666_1X18:
320f11adcecSJonathan Liu 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
321f11adcecSJonathan Liu 		/* Fall through: enable dithering */
322f11adcecSJonathan Liu 		val |= SUN4I_TCON0_FRM_CTL_EN;
323f11adcecSJonathan Liu 		break;
324f11adcecSJonathan Liu 	}
325f11adcecSJonathan Liu 
326f11adcecSJonathan Liu 	/* Write dithering settings */
327f11adcecSJonathan Liu 	regmap_write(tcon->regs, SUN4I_TCON_FRM_CTL_REG, val);
328f11adcecSJonathan Liu }
329f11adcecSJonathan Liu 
330a08fc7c8SMaxime Ripard static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon,
33179891d56SChen-Yu Tsai 				     const struct drm_encoder *encoder,
332a08fc7c8SMaxime Ripard 				     const struct drm_display_mode *mode)
333a08fc7c8SMaxime Ripard {
33479891d56SChen-Yu Tsai 	/* TODO support normal CPU interface modes */
33579891d56SChen-Yu Tsai 	struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder);
33679891d56SChen-Yu Tsai 	struct mipi_dsi_device *device = dsi->device;
337a08fc7c8SMaxime Ripard 	u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format);
338a08fc7c8SMaxime Ripard 	u8 lanes = device->lanes;
339a08fc7c8SMaxime Ripard 	u32 block_space, start_delay;
340a08fc7c8SMaxime Ripard 	u32 tcon_div;
341a08fc7c8SMaxime Ripard 
34285fb3526SMaxime Ripard 	tcon->dclk_min_div = SUN6I_DSI_TCON_DIV;
34385fb3526SMaxime Ripard 	tcon->dclk_max_div = SUN6I_DSI_TCON_DIV;
344a08fc7c8SMaxime Ripard 
345a08fc7c8SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
346a08fc7c8SMaxime Ripard 
347f11adcecSJonathan Liu 	/* Set dithering if needed */
348f11adcecSJonathan Liu 	sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder));
349f11adcecSJonathan Liu 
350a08fc7c8SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
351a08fc7c8SMaxime Ripard 			   SUN4I_TCON0_CTL_IF_MASK,
352a08fc7c8SMaxime Ripard 			   SUN4I_TCON0_CTL_IF_8080);
353a08fc7c8SMaxime Ripard 
354a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG,
355a08fc7c8SMaxime Ripard 		     SUN4I_TCON_ECC_FIFO_EN);
356a08fc7c8SMaxime Ripard 
357a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG,
358a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_MODE_DSI |
359a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH |
360a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_EN |
361a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_IF_TRI_EN);
362a08fc7c8SMaxime Ripard 
363a08fc7c8SMaxime Ripard 	/*
364a08fc7c8SMaxime Ripard 	 * This looks suspicious, but it works...
365a08fc7c8SMaxime Ripard 	 *
366a08fc7c8SMaxime Ripard 	 * The datasheet says that this should be set higher than 20 *
367a08fc7c8SMaxime Ripard 	 * pixel cycle, but it's not clear what a pixel cycle is.
368a08fc7c8SMaxime Ripard 	 */
369a08fc7c8SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div);
370a08fc7c8SMaxime Ripard 	tcon_div &= GENMASK(6, 0);
371a08fc7c8SMaxime Ripard 	block_space = mode->htotal * bpp / (tcon_div * lanes);
372a08fc7c8SMaxime Ripard 	block_space -= mode->hdisplay + 40;
373a08fc7c8SMaxime Ripard 
374a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG,
375a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) |
376a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay));
377a08fc7c8SMaxime Ripard 
378a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG,
379a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay));
380a08fc7c8SMaxime Ripard 
381a08fc7c8SMaxime Ripard 	start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1);
382a08fc7c8SMaxime Ripard 	start_delay = start_delay * mode->crtc_htotal * 149;
383a08fc7c8SMaxime Ripard 	start_delay = start_delay / (mode->crtc_clock / 1000) / 8;
384a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG,
385a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) |
386a08fc7c8SMaxime Ripard 		     SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay));
387a08fc7c8SMaxime Ripard 
388a08fc7c8SMaxime Ripard 	/*
389a08fc7c8SMaxime Ripard 	 * The Allwinner BSP has a comment that the period should be
390a08fc7c8SMaxime Ripard 	 * the display clock * 15, but uses an hardcoded 3000...
391a08fc7c8SMaxime Ripard 	 */
392a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG,
393a08fc7c8SMaxime Ripard 		     SUN4I_TCON_SAFE_PERIOD_NUM(3000) |
394a08fc7c8SMaxime Ripard 		     SUN4I_TCON_SAFE_PERIOD_MODE(3));
395a08fc7c8SMaxime Ripard 
396a08fc7c8SMaxime Ripard 	/* Enable the output on the pins */
397a08fc7c8SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG,
398a08fc7c8SMaxime Ripard 		     0xe0000000);
399a08fc7c8SMaxime Ripard }
400a08fc7c8SMaxime Ripard 
401a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
402a0c1214eSMaxime Ripard 				      const struct drm_encoder *encoder,
403a0c1214eSMaxime Ripard 				      const struct drm_display_mode *mode)
404a0c1214eSMaxime Ripard {
405a0c1214eSMaxime Ripard 	unsigned int bp;
406a0c1214eSMaxime Ripard 	u8 clk_delay;
407a0c1214eSMaxime Ripard 	u32 reg, val = 0;
408a0c1214eSMaxime Ripard 
40934d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
41034d698f6SJernej Skrabec 
411a0c1214eSMaxime Ripard 	tcon->dclk_min_div = 7;
412a0c1214eSMaxime Ripard 	tcon->dclk_max_div = 7;
413a0c1214eSMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
414a0c1214eSMaxime Ripard 
415f11adcecSJonathan Liu 	/* Set dithering if needed */
416f11adcecSJonathan Liu 	sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder));
417f11adcecSJonathan Liu 
418a0c1214eSMaxime Ripard 	/* Adjust clock delay */
419a0c1214eSMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
420a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
421a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
422a0c1214eSMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
423a0c1214eSMaxime Ripard 
424a0c1214eSMaxime Ripard 	/*
425a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
426a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
427a0c1214eSMaxime Ripard 	 */
428a0c1214eSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
429a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
430a0c1214eSMaxime Ripard 			 mode->crtc_htotal, bp);
431a0c1214eSMaxime Ripard 
432a0c1214eSMaxime Ripard 	/* Set horizontal display timings */
433a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
434a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
435a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
436a0c1214eSMaxime Ripard 
437a0c1214eSMaxime Ripard 	/*
438a0c1214eSMaxime Ripard 	 * This is called a backporch in the register documentation,
439a0c1214eSMaxime Ripard 	 * but it really is the back porch + hsync
440a0c1214eSMaxime Ripard 	 */
441a0c1214eSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
442a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
443a0c1214eSMaxime Ripard 			 mode->crtc_vtotal, bp);
444a0c1214eSMaxime Ripard 
445a0c1214eSMaxime Ripard 	/* Set vertical display timings */
446a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
447a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
448a0c1214eSMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
449a0c1214eSMaxime Ripard 
450a0c1214eSMaxime Ripard 	reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
451a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
452a0c1214eSMaxime Ripard 		SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
453a0c1214eSMaxime Ripard 	if (sun4i_tcon_get_pixel_depth(encoder) == 24)
454a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
455a0c1214eSMaxime Ripard 	else
456a0c1214eSMaxime Ripard 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
457a0c1214eSMaxime Ripard 
458a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
459a0c1214eSMaxime Ripard 
460a0c1214eSMaxime Ripard 	/* Setup the polarity of the various signals */
461a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
462a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
463a0c1214eSMaxime Ripard 
464a0c1214eSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
465a0c1214eSMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
466a0c1214eSMaxime Ripard 
467a0c1214eSMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
468a0c1214eSMaxime Ripard 
469a0c1214eSMaxime Ripard 	/* Map output pins to channel 0 */
470a0c1214eSMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
471a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
472a0c1214eSMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
47380b79e31SOndrej Jirman 
47480b79e31SOndrej Jirman 	/* Enable the output on the pins */
47580b79e31SOndrej Jirman 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000);
476a0c1214eSMaxime Ripard }
477a0c1214eSMaxime Ripard 
478ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
479b842e2c9SPaul Kocialkowski 				     const struct drm_encoder *encoder,
4805b8f0910SMaxime Ripard 				     const struct drm_display_mode *mode)
4819026e0d1SMaxime Ripard {
4824843c9a2SPaul Kocialkowski 	struct drm_connector *connector = sun4i_tcon_get_connector(encoder);
4831e612a0fSVille Syrjälä 	const struct drm_display_info *info = &connector->display_info;
4849026e0d1SMaxime Ripard 	unsigned int bp, hsync, vsync;
4859026e0d1SMaxime Ripard 	u8 clk_delay;
4869026e0d1SMaxime Ripard 	u32 val = 0;
4879026e0d1SMaxime Ripard 
48834d698f6SJernej Skrabec 	WARN_ON(!tcon->quirks->has_channel_0);
48934d698f6SJernej Skrabec 
490ec08d596SMaxime Ripard 	tcon->dclk_min_div = 6;
491ec08d596SMaxime Ripard 	tcon->dclk_max_div = 127;
492ba19c537SMaxime Ripard 	sun4i_tcon0_mode_set_common(tcon, mode);
49386cf6788SChen-Yu Tsai 
494f11adcecSJonathan Liu 	/* Set dithering if needed */
4954843c9a2SPaul Kocialkowski 	sun4i_tcon0_mode_set_dithering(tcon, connector);
496f11adcecSJonathan Liu 
4979026e0d1SMaxime Ripard 	/* Adjust clock delay */
4989026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
4999026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
5009026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
5019026e0d1SMaxime Ripard 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
5029026e0d1SMaxime Ripard 
5039026e0d1SMaxime Ripard 	/*
5049026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
50523a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
5069026e0d1SMaxime Ripard 	 */
5079026e0d1SMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
5089026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
5099026e0d1SMaxime Ripard 			 mode->crtc_htotal, bp);
5109026e0d1SMaxime Ripard 
5119026e0d1SMaxime Ripard 	/* Set horizontal display timings */
5129026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
5139026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
5149026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
5159026e0d1SMaxime Ripard 
5169026e0d1SMaxime Ripard 	/*
5179026e0d1SMaxime Ripard 	 * This is called a backporch in the register documentation,
51823a1cb11SChen-Yu Tsai 	 * but it really is the back porch + hsync
5199026e0d1SMaxime Ripard 	 */
5209026e0d1SMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
5219026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
5229026e0d1SMaxime Ripard 			 mode->crtc_vtotal, bp);
5239026e0d1SMaxime Ripard 
5249026e0d1SMaxime Ripard 	/* Set vertical display timings */
5259026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
526a88cbbd4SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
5279026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
5289026e0d1SMaxime Ripard 
5299026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
5309026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
5319026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
5329026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
5339026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
5349026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
5359026e0d1SMaxime Ripard 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
5369026e0d1SMaxime Ripard 
5379026e0d1SMaxime Ripard 	/* Setup the polarity of the various signals */
538fa4127c5SGiulio Benetti 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
5399026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
5409026e0d1SMaxime Ripard 
541fa4127c5SGiulio Benetti 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
5429026e0d1SMaxime Ripard 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
5439026e0d1SMaxime Ripard 
5441e612a0fSVille Syrjälä 	if (info->bus_flags & DRM_BUS_FLAG_DE_LOW)
54565bf2d54SPaul Kocialkowski 		val |= SUN4I_TCON0_IO_POL_DE_NEGATIVE;
54665bf2d54SPaul Kocialkowski 
547490cda5aSGiulio Benetti 	/*
548490cda5aSGiulio Benetti 	 * On A20 and similar SoCs, the only way to achieve Positive Edge
549490cda5aSGiulio Benetti 	 * (Rising Edge), is setting dclk clock phase to 2/3(240°).
550490cda5aSGiulio Benetti 	 * By default TCON works in Negative Edge(Falling Edge),
551490cda5aSGiulio Benetti 	 * this is why phase is set to 0 in that case.
552490cda5aSGiulio Benetti 	 * Unfortunately there's no way to logically invert dclk through
553490cda5aSGiulio Benetti 	 * IO_POL register.
554490cda5aSGiulio Benetti 	 * The only acceptable way to work, triple checked with scope,
555490cda5aSGiulio Benetti 	 * is using clock phase set to 0° for Negative Edge and set to 240°
556490cda5aSGiulio Benetti 	 * for Positive Edge.
557490cda5aSGiulio Benetti 	 * On A33 and similar SoCs there would be a 90° phase option,
558490cda5aSGiulio Benetti 	 * but it divides also dclk by 2.
559490cda5aSGiulio Benetti 	 * Following code is a way to avoid quirks all around TCON
560490cda5aSGiulio Benetti 	 * and DOTCLOCK drivers.
561490cda5aSGiulio Benetti 	 */
5621e612a0fSVille Syrjälä 	if (info->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
563490cda5aSGiulio Benetti 		clk_set_phase(tcon->dclk, 240);
564490cda5aSGiulio Benetti 
5651e612a0fSVille Syrjälä 	if (info->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
566490cda5aSGiulio Benetti 		clk_set_phase(tcon->dclk, 0);
567490cda5aSGiulio Benetti 
5689026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
56965bf2d54SPaul Kocialkowski 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE |
57065bf2d54SPaul Kocialkowski 			   SUN4I_TCON0_IO_POL_VSYNC_POSITIVE |
57165bf2d54SPaul Kocialkowski 			   SUN4I_TCON0_IO_POL_DE_NEGATIVE,
5729026e0d1SMaxime Ripard 			   val);
5739026e0d1SMaxime Ripard 
5749026e0d1SMaxime Ripard 	/* Map output pins to channel 0 */
5759026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
5769026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
5779026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
5789026e0d1SMaxime Ripard 
5799026e0d1SMaxime Ripard 	/* Enable the output on the pins */
5809026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
5819026e0d1SMaxime Ripard }
5829026e0d1SMaxime Ripard 
5835b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
5845b8f0910SMaxime Ripard 				 const struct drm_display_mode *mode)
5859026e0d1SMaxime Ripard {
586b8317a3dSMaxime Ripard 	unsigned int bp, hsync, vsync, vtotal;
5879026e0d1SMaxime Ripard 	u8 clk_delay;
5889026e0d1SMaxime Ripard 	u32 val;
5899026e0d1SMaxime Ripard 
59091ea2f29SChen-Yu Tsai 	WARN_ON(!tcon->quirks->has_channel_1);
5918e924047SMaxime Ripard 
59286cf6788SChen-Yu Tsai 	/* Configure the dot clock */
59386cf6788SChen-Yu Tsai 	clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
59486cf6788SChen-Yu Tsai 
5959026e0d1SMaxime Ripard 	/* Adjust clock delay */
5969026e0d1SMaxime Ripard 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
5979026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
5989026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
5999026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
6009026e0d1SMaxime Ripard 
6019026e0d1SMaxime Ripard 	/* Set interlaced mode */
6029026e0d1SMaxime Ripard 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6039026e0d1SMaxime Ripard 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
6049026e0d1SMaxime Ripard 	else
6059026e0d1SMaxime Ripard 		val = 0;
6069026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
6079026e0d1SMaxime Ripard 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
6089026e0d1SMaxime Ripard 			   val);
6099026e0d1SMaxime Ripard 
6109026e0d1SMaxime Ripard 	/* Set the input resolution */
6119026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
6129026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
6139026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
6149026e0d1SMaxime Ripard 
6159026e0d1SMaxime Ripard 	/* Set the upscaling resolution */
6169026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
6179026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
6189026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
6199026e0d1SMaxime Ripard 
6209026e0d1SMaxime Ripard 	/* Set the output resolution */
6219026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
6229026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
6239026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
6249026e0d1SMaxime Ripard 
6259026e0d1SMaxime Ripard 	/* Set horizontal display timings */
6263cb2f46bSMaxime Ripard 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
6279026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
6289026e0d1SMaxime Ripard 			 mode->htotal, bp);
6299026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
6309026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
6319026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
6329026e0d1SMaxime Ripard 
6333cb2f46bSMaxime Ripard 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
6349026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
635b8317a3dSMaxime Ripard 			 mode->crtc_vtotal, bp);
636b8317a3dSMaxime Ripard 
637b8317a3dSMaxime Ripard 	/*
638b8317a3dSMaxime Ripard 	 * The vertical resolution needs to be doubled in all
639b8317a3dSMaxime Ripard 	 * cases. We could use crtc_vtotal and always multiply by two,
640b8317a3dSMaxime Ripard 	 * but that leads to a rounding error in interlace when vtotal
641b8317a3dSMaxime Ripard 	 * is odd.
642b8317a3dSMaxime Ripard 	 *
643b8317a3dSMaxime Ripard 	 * This happens with TV's PAL for example, where vtotal will
644b8317a3dSMaxime Ripard 	 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
645b8317a3dSMaxime Ripard 	 * 624, which apparently confuses the hardware.
646b8317a3dSMaxime Ripard 	 *
647b8317a3dSMaxime Ripard 	 * To work around this, we will always use vtotal, and
648b8317a3dSMaxime Ripard 	 * multiply by two only if we're not in interlace.
649b8317a3dSMaxime Ripard 	 */
650b8317a3dSMaxime Ripard 	vtotal = mode->vtotal;
651b8317a3dSMaxime Ripard 	if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
652b8317a3dSMaxime Ripard 		vtotal = vtotal * 2;
653b8317a3dSMaxime Ripard 
654b8317a3dSMaxime Ripard 	/* Set vertical display timings */
6559026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
656b8317a3dSMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
6579026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
6589026e0d1SMaxime Ripard 
6599026e0d1SMaxime Ripard 	/* Set Hsync and Vsync length */
6609026e0d1SMaxime Ripard 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
6619026e0d1SMaxime Ripard 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
6629026e0d1SMaxime Ripard 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
6639026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
6649026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
6659026e0d1SMaxime Ripard 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
6669026e0d1SMaxime Ripard 
6679026e0d1SMaxime Ripard 	/* Map output pins to channel 1 */
6689026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
6699026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_MASK,
6709026e0d1SMaxime Ripard 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
6719026e0d1SMaxime Ripard }
6725b8f0910SMaxime Ripard 
6735b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
6745b8f0910SMaxime Ripard 			 const struct drm_encoder *encoder,
6755b8f0910SMaxime Ripard 			 const struct drm_display_mode *mode)
6765b8f0910SMaxime Ripard {
6775b8f0910SMaxime Ripard 	switch (encoder->encoder_type) {
678a08fc7c8SMaxime Ripard 	case DRM_MODE_ENCODER_DSI:
67979891d56SChen-Yu Tsai 		/* DSI is tied to special case of CPU interface */
68079891d56SChen-Yu Tsai 		sun4i_tcon0_mode_set_cpu(tcon, encoder, mode);
681a08fc7c8SMaxime Ripard 		break;
682a0c1214eSMaxime Ripard 	case DRM_MODE_ENCODER_LVDS:
683a0c1214eSMaxime Ripard 		sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
684a0c1214eSMaxime Ripard 		break;
6855b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_NONE:
686b842e2c9SPaul Kocialkowski 		sun4i_tcon0_mode_set_rgb(tcon, encoder, mode);
6875b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 0, encoder);
6885b8f0910SMaxime Ripard 		break;
6895b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TVDAC:
6905b8f0910SMaxime Ripard 	case DRM_MODE_ENCODER_TMDS:
6915b8f0910SMaxime Ripard 		sun4i_tcon1_mode_set(tcon, mode);
6925b8f0910SMaxime Ripard 		sun4i_tcon_set_mux(tcon, 1, encoder);
6935b8f0910SMaxime Ripard 		break;
6945b8f0910SMaxime Ripard 	default:
6955b8f0910SMaxime Ripard 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
6965b8f0910SMaxime Ripard 	}
6975b8f0910SMaxime Ripard }
6985b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set);
6999026e0d1SMaxime Ripard 
7009026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
7019026e0d1SMaxime Ripard 					struct sun4i_crtc *scrtc)
7029026e0d1SMaxime Ripard {
7039026e0d1SMaxime Ripard 	unsigned long flags;
7049026e0d1SMaxime Ripard 
7059026e0d1SMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
7069026e0d1SMaxime Ripard 	if (scrtc->event) {
7079026e0d1SMaxime Ripard 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
7089026e0d1SMaxime Ripard 		drm_crtc_vblank_put(&scrtc->crtc);
7099026e0d1SMaxime Ripard 		scrtc->event = NULL;
7109026e0d1SMaxime Ripard 	}
7119026e0d1SMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
7129026e0d1SMaxime Ripard }
7139026e0d1SMaxime Ripard 
7149026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private)
7159026e0d1SMaxime Ripard {
7169026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = private;
7179026e0d1SMaxime Ripard 	struct drm_device *drm = tcon->drm;
71846cce6daSChen-Yu Tsai 	struct sun4i_crtc *scrtc = tcon->crtc;
7193004f75fSMaxime Ripard 	struct sunxi_engine *engine = scrtc->engine;
7209026e0d1SMaxime Ripard 	unsigned int status;
7219026e0d1SMaxime Ripard 
7229026e0d1SMaxime Ripard 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
7239026e0d1SMaxime Ripard 
7249026e0d1SMaxime Ripard 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
725a493ceaeSMaxime Ripard 			SUN4I_TCON_GINT0_VBLANK_INT(1) |
726a493ceaeSMaxime Ripard 			SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT)))
7279026e0d1SMaxime Ripard 		return IRQ_NONE;
7289026e0d1SMaxime Ripard 
7299026e0d1SMaxime Ripard 	drm_crtc_handle_vblank(&scrtc->crtc);
7309026e0d1SMaxime Ripard 	sun4i_tcon_finish_page_flip(drm, scrtc);
7319026e0d1SMaxime Ripard 
7329026e0d1SMaxime Ripard 	/* Acknowledge the interrupt */
7339026e0d1SMaxime Ripard 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
7349026e0d1SMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
735a493ceaeSMaxime Ripard 			   SUN4I_TCON_GINT0_VBLANK_INT(1) |
736a493ceaeSMaxime Ripard 			   SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT,
7379026e0d1SMaxime Ripard 			   0);
7389026e0d1SMaxime Ripard 
7393004f75fSMaxime Ripard 	if (engine->ops->vblank_quirk)
7403004f75fSMaxime Ripard 		engine->ops->vblank_quirk(engine);
7413004f75fSMaxime Ripard 
7429026e0d1SMaxime Ripard 	return IRQ_HANDLED;
7439026e0d1SMaxime Ripard }
7449026e0d1SMaxime Ripard 
7459026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev,
7469026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
7479026e0d1SMaxime Ripard {
7489026e0d1SMaxime Ripard 	tcon->clk = devm_clk_get(dev, "ahb");
7499026e0d1SMaxime Ripard 	if (IS_ERR(tcon->clk)) {
7509026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get the TCON bus clock\n");
7519026e0d1SMaxime Ripard 		return PTR_ERR(tcon->clk);
7529026e0d1SMaxime Ripard 	}
7539026e0d1SMaxime Ripard 	clk_prepare_enable(tcon->clk);
7549026e0d1SMaxime Ripard 
75534d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
7569026e0d1SMaxime Ripard 		tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
7579026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk0)) {
7589026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
7599026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk0);
7609026e0d1SMaxime Ripard 		}
76134d698f6SJernej Skrabec 	}
762b14e945bSPaul Kocialkowski 	clk_prepare_enable(tcon->sclk0);
7639026e0d1SMaxime Ripard 
76491ea2f29SChen-Yu Tsai 	if (tcon->quirks->has_channel_1) {
7659026e0d1SMaxime Ripard 		tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
7669026e0d1SMaxime Ripard 		if (IS_ERR(tcon->sclk1)) {
7679026e0d1SMaxime Ripard 			dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
7689026e0d1SMaxime Ripard 			return PTR_ERR(tcon->sclk1);
7699026e0d1SMaxime Ripard 		}
7708e924047SMaxime Ripard 	}
7719026e0d1SMaxime Ripard 
7724c7f16d1SChen-Yu Tsai 	return 0;
7739026e0d1SMaxime Ripard }
7749026e0d1SMaxime Ripard 
7759026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
7769026e0d1SMaxime Ripard {
777b14e945bSPaul Kocialkowski 	clk_disable_unprepare(tcon->sclk0);
7789026e0d1SMaxime Ripard 	clk_disable_unprepare(tcon->clk);
7799026e0d1SMaxime Ripard }
7809026e0d1SMaxime Ripard 
7819026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev,
7829026e0d1SMaxime Ripard 			       struct sun4i_tcon *tcon)
7839026e0d1SMaxime Ripard {
7849026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
7859026e0d1SMaxime Ripard 	int irq, ret;
7869026e0d1SMaxime Ripard 
7879026e0d1SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
7889026e0d1SMaxime Ripard 	if (irq < 0) {
7899026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
7909026e0d1SMaxime Ripard 		return irq;
7919026e0d1SMaxime Ripard 	}
7929026e0d1SMaxime Ripard 
7939026e0d1SMaxime Ripard 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
7949026e0d1SMaxime Ripard 			       dev_name(dev), tcon);
7959026e0d1SMaxime Ripard 	if (ret) {
7969026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't request the IRQ\n");
7979026e0d1SMaxime Ripard 		return ret;
7989026e0d1SMaxime Ripard 	}
7999026e0d1SMaxime Ripard 
8009026e0d1SMaxime Ripard 	return 0;
8019026e0d1SMaxime Ripard }
8029026e0d1SMaxime Ripard 
8039026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = {
8049026e0d1SMaxime Ripard 	.reg_bits	= 32,
8059026e0d1SMaxime Ripard 	.val_bits	= 32,
8069026e0d1SMaxime Ripard 	.reg_stride	= 4,
8079026e0d1SMaxime Ripard 	.max_register	= 0x800,
8089026e0d1SMaxime Ripard };
8099026e0d1SMaxime Ripard 
8109026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev,
8119026e0d1SMaxime Ripard 				  struct sun4i_tcon *tcon)
8129026e0d1SMaxime Ripard {
8139026e0d1SMaxime Ripard 	struct platform_device *pdev = to_platform_device(dev);
8149026e0d1SMaxime Ripard 	struct resource *res;
8159026e0d1SMaxime Ripard 	void __iomem *regs;
8169026e0d1SMaxime Ripard 
8179026e0d1SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
8189026e0d1SMaxime Ripard 	regs = devm_ioremap_resource(dev, res);
819af346f55SWei Yongjun 	if (IS_ERR(regs))
8209026e0d1SMaxime Ripard 		return PTR_ERR(regs);
8219026e0d1SMaxime Ripard 
8229026e0d1SMaxime Ripard 	tcon->regs = devm_regmap_init_mmio(dev, regs,
8239026e0d1SMaxime Ripard 					   &sun4i_tcon_regmap_config);
8249026e0d1SMaxime Ripard 	if (IS_ERR(tcon->regs)) {
8259026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't create the TCON regmap\n");
8269026e0d1SMaxime Ripard 		return PTR_ERR(tcon->regs);
8279026e0d1SMaxime Ripard 	}
8289026e0d1SMaxime Ripard 
8299026e0d1SMaxime Ripard 	/* Make sure the TCON is disabled and all IRQs are off */
8309026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
8319026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
8329026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
8339026e0d1SMaxime Ripard 
8349026e0d1SMaxime Ripard 	/* Disable IO lines and set them to tristate */
8359026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
8369026e0d1SMaxime Ripard 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
8379026e0d1SMaxime Ripard 
8389026e0d1SMaxime Ripard 	return 0;
8399026e0d1SMaxime Ripard }
8409026e0d1SMaxime Ripard 
841b317fa3bSChen-Yu Tsai /*
842b317fa3bSChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
843b317fa3bSChen-Yu Tsai  * the TCON is always tied to just one backend. Hence we can traverse
844b317fa3bSChen-Yu Tsai  * the of_graph upwards to find the backend our tcon is connected to,
845b317fa3bSChen-Yu Tsai  * and take its ID as our own.
846b317fa3bSChen-Yu Tsai  *
847b317fa3bSChen-Yu Tsai  * We can either identify backends from their compatible strings, which
848b317fa3bSChen-Yu Tsai  * means maintaining a large list of them. Or, since the backend is
849b317fa3bSChen-Yu Tsai  * registered and binded before the TCON, we can just go through the
850b317fa3bSChen-Yu Tsai  * list of registered backends and compare the device node.
85187969338SIcenowy Zheng  *
85287969338SIcenowy Zheng  * As the structures now store engines instead of backends, here this
85387969338SIcenowy Zheng  * function in fact searches the corresponding engine, and the ID is
85487969338SIcenowy Zheng  * requested via the get_id function of the engine.
855b317fa3bSChen-Yu Tsai  */
856e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *
857e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
85849836b11SJernej Skrabec 				struct device_node *node,
85949836b11SJernej Skrabec 				u32 port_id)
860b317fa3bSChen-Yu Tsai {
861b317fa3bSChen-Yu Tsai 	struct device_node *port, *ep, *remote;
862be3fe0f9SChen-Yu Tsai 	struct sunxi_engine *engine = ERR_PTR(-EINVAL);
86349836b11SJernej Skrabec 	u32 reg = 0;
864b317fa3bSChen-Yu Tsai 
86549836b11SJernej Skrabec 	port = of_graph_get_port_by_id(node, port_id);
866b317fa3bSChen-Yu Tsai 	if (!port)
867b317fa3bSChen-Yu Tsai 		return ERR_PTR(-EINVAL);
868b317fa3bSChen-Yu Tsai 
8691469619dSChen-Yu Tsai 	/*
8701469619dSChen-Yu Tsai 	 * This only works if there is only one path from the TCON
8711469619dSChen-Yu Tsai 	 * to any display engine. Otherwise the probe order of the
8721469619dSChen-Yu Tsai 	 * TCONs and display engines is not guaranteed. They may
8731469619dSChen-Yu Tsai 	 * either bind to the wrong one, or worse, bind to the same
8741469619dSChen-Yu Tsai 	 * one if additional checks are not done.
8751469619dSChen-Yu Tsai 	 *
8761469619dSChen-Yu Tsai 	 * Bail out if there are multiple input connections.
8771469619dSChen-Yu Tsai 	 */
878be3fe0f9SChen-Yu Tsai 	if (of_get_available_child_count(port) != 1)
879be3fe0f9SChen-Yu Tsai 		goto out_put_port;
8801469619dSChen-Yu Tsai 
881be3fe0f9SChen-Yu Tsai 	/* Get the first connection without specifying an ID */
882be3fe0f9SChen-Yu Tsai 	ep = of_get_next_available_child(port, NULL);
883be3fe0f9SChen-Yu Tsai 	if (!ep)
884be3fe0f9SChen-Yu Tsai 		goto out_put_port;
885be3fe0f9SChen-Yu Tsai 
886b317fa3bSChen-Yu Tsai 	remote = of_graph_get_remote_port_parent(ep);
887b317fa3bSChen-Yu Tsai 	if (!remote)
888be3fe0f9SChen-Yu Tsai 		goto out_put_ep;
889b317fa3bSChen-Yu Tsai 
89087969338SIcenowy Zheng 	/* does this node match any registered engines? */
891be3fe0f9SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
892be3fe0f9SChen-Yu Tsai 		if (remote == engine->node)
893be3fe0f9SChen-Yu Tsai 			goto out_put_remote;
894b317fa3bSChen-Yu Tsai 
89549836b11SJernej Skrabec 	/*
89649836b11SJernej Skrabec 	 * According to device tree binding input ports have even id
89749836b11SJernej Skrabec 	 * number and output ports have odd id. Since component with
89849836b11SJernej Skrabec 	 * more than one input and one output (TCON TOP) exits, correct
89949836b11SJernej Skrabec 	 * remote input id has to be calculated by subtracting 1 from
90049836b11SJernej Skrabec 	 * remote output id. If this for some reason can't be done, 0
90149836b11SJernej Skrabec 	 * is used as input port id.
90249836b11SJernej Skrabec 	 */
903da82107eSJernej Skrabec 	of_node_put(port);
90449836b11SJernej Skrabec 	port = of_graph_get_remote_port(ep);
90549836b11SJernej Skrabec 	if (!of_property_read_u32(port, "reg", &reg) && reg > 0)
90649836b11SJernej Skrabec 		reg -= 1;
90749836b11SJernej Skrabec 
908b317fa3bSChen-Yu Tsai 	/* keep looking through upstream ports */
90949836b11SJernej Skrabec 	engine = sun4i_tcon_find_engine_traverse(drv, remote, reg);
910b317fa3bSChen-Yu Tsai 
911be3fe0f9SChen-Yu Tsai out_put_remote:
912be3fe0f9SChen-Yu Tsai 	of_node_put(remote);
913be3fe0f9SChen-Yu Tsai out_put_ep:
914be3fe0f9SChen-Yu Tsai 	of_node_put(ep);
915be3fe0f9SChen-Yu Tsai out_put_port:
916be3fe0f9SChen-Yu Tsai 	of_node_put(port);
917be3fe0f9SChen-Yu Tsai 
918be3fe0f9SChen-Yu Tsai 	return engine;
919b317fa3bSChen-Yu Tsai }
920b317fa3bSChen-Yu Tsai 
921e8d5bbf7SChen-Yu Tsai /*
922e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
923e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
924e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
925e8d5bbf7SChen-Yu Tsai  * component. Thus we can look at any one of the input connections of
926e8d5bbf7SChen-Yu Tsai  * the TCONs, and use that connection's remote endpoint ID as our own.
927e8d5bbf7SChen-Yu Tsai  *
928e8d5bbf7SChen-Yu Tsai  * Since the user of this function already finds the input port,
929e8d5bbf7SChen-Yu Tsai  * the port is passed in directly without further checks.
930e8d5bbf7SChen-Yu Tsai  */
931e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
932e8d5bbf7SChen-Yu Tsai {
933e8d5bbf7SChen-Yu Tsai 	struct device_node *ep;
934e8d5bbf7SChen-Yu Tsai 	int ret = -EINVAL;
935e8d5bbf7SChen-Yu Tsai 
936e8d5bbf7SChen-Yu Tsai 	/* try finding an upstream endpoint */
937e8d5bbf7SChen-Yu Tsai 	for_each_available_child_of_node(port, ep) {
938e8d5bbf7SChen-Yu Tsai 		struct device_node *remote;
939e8d5bbf7SChen-Yu Tsai 		u32 reg;
940e8d5bbf7SChen-Yu Tsai 
941e8d5bbf7SChen-Yu Tsai 		remote = of_graph_get_remote_endpoint(ep);
942e8d5bbf7SChen-Yu Tsai 		if (!remote)
943e8d5bbf7SChen-Yu Tsai 			continue;
944e8d5bbf7SChen-Yu Tsai 
945e8d5bbf7SChen-Yu Tsai 		ret = of_property_read_u32(remote, "reg", &reg);
946e8d5bbf7SChen-Yu Tsai 		if (ret)
947e8d5bbf7SChen-Yu Tsai 			continue;
948e8d5bbf7SChen-Yu Tsai 
949e8d5bbf7SChen-Yu Tsai 		ret = reg;
950e8d5bbf7SChen-Yu Tsai 	}
951e8d5bbf7SChen-Yu Tsai 
952e8d5bbf7SChen-Yu Tsai 	return ret;
953e8d5bbf7SChen-Yu Tsai }
954e8d5bbf7SChen-Yu Tsai 
955e8d5bbf7SChen-Yu Tsai /*
956e8d5bbf7SChen-Yu Tsai  * Once we know the TCON's id, we can look through the list of
957e8d5bbf7SChen-Yu Tsai  * engines to find a matching one. We assume all engines have
958e8d5bbf7SChen-Yu Tsai  * been probed and added to the list.
959e8d5bbf7SChen-Yu Tsai  */
960e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
961e8d5bbf7SChen-Yu Tsai 							int id)
962e8d5bbf7SChen-Yu Tsai {
963e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
964e8d5bbf7SChen-Yu Tsai 
965e8d5bbf7SChen-Yu Tsai 	list_for_each_entry(engine, &drv->engine_list, list)
966e8d5bbf7SChen-Yu Tsai 		if (engine->id == id)
967e8d5bbf7SChen-Yu Tsai 			return engine;
968e8d5bbf7SChen-Yu Tsai 
969e8d5bbf7SChen-Yu Tsai 	return ERR_PTR(-EINVAL);
970e8d5bbf7SChen-Yu Tsai }
971e8d5bbf7SChen-Yu Tsai 
972cf77d79bSJernej Skrabec static bool sun4i_tcon_connected_to_tcon_top(struct device_node *node)
973cf77d79bSJernej Skrabec {
974cf77d79bSJernej Skrabec 	struct device_node *remote;
975cf77d79bSJernej Skrabec 	bool ret = false;
976cf77d79bSJernej Skrabec 
977cf77d79bSJernej Skrabec 	remote = of_graph_get_remote_node(node, 0, -1);
978cf77d79bSJernej Skrabec 	if (remote) {
979185e0bebSMaxime Ripard 		ret = !!(IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
980185e0bebSMaxime Ripard 			 of_match_node(sun8i_tcon_top_of_table, remote));
981cf77d79bSJernej Skrabec 		of_node_put(remote);
982cf77d79bSJernej Skrabec 	}
983cf77d79bSJernej Skrabec 
984cf77d79bSJernej Skrabec 	return ret;
985cf77d79bSJernej Skrabec }
986cf77d79bSJernej Skrabec 
987cf77d79bSJernej Skrabec static int sun4i_tcon_get_index(struct sun4i_drv *drv)
988cf77d79bSJernej Skrabec {
989cf77d79bSJernej Skrabec 	struct list_head *pos;
990cf77d79bSJernej Skrabec 	int size = 0;
991cf77d79bSJernej Skrabec 
992cf77d79bSJernej Skrabec 	/*
993cf77d79bSJernej Skrabec 	 * Because TCON is added to the list at the end of the probe
994cf77d79bSJernej Skrabec 	 * (after this function is called), index of the current TCON
995cf77d79bSJernej Skrabec 	 * will be same as current TCON list size.
996cf77d79bSJernej Skrabec 	 */
997cf77d79bSJernej Skrabec 	list_for_each(pos, &drv->tcon_list)
998cf77d79bSJernej Skrabec 		++size;
999cf77d79bSJernej Skrabec 
1000cf77d79bSJernej Skrabec 	return size;
1001cf77d79bSJernej Skrabec }
1002cf77d79bSJernej Skrabec 
1003e8d5bbf7SChen-Yu Tsai /*
1004e8d5bbf7SChen-Yu Tsai  * On SoCs with the old display pipeline design (Display Engine 1.0),
1005e8d5bbf7SChen-Yu Tsai  * we assumed the TCON was always tied to just one backend. However
1006e8d5bbf7SChen-Yu Tsai  * this proved not to be the case. On the A31, the TCON can select
1007e8d5bbf7SChen-Yu Tsai  * either backend as its source. On the A20 (and likely on the A10),
1008e8d5bbf7SChen-Yu Tsai  * the backend can choose which TCON to output to.
1009e8d5bbf7SChen-Yu Tsai  *
1010e8d5bbf7SChen-Yu Tsai  * The device tree binding says that the remote endpoint ID of any
1011e8d5bbf7SChen-Yu Tsai  * connection between components, up to and including the TCON, of
1012e8d5bbf7SChen-Yu Tsai  * the display pipeline should be equal to the actual ID of the local
1013e8d5bbf7SChen-Yu Tsai  * component. Thus we should be able to look at any one of the input
1014e8d5bbf7SChen-Yu Tsai  * connections of the TCONs, and use that connection's remote endpoint
1015e8d5bbf7SChen-Yu Tsai  * ID as our own.
1016e8d5bbf7SChen-Yu Tsai  *
1017e8d5bbf7SChen-Yu Tsai  * However  the connections between the backend and TCON were assumed
1018e8d5bbf7SChen-Yu Tsai  * to be always singular, and their endpoit IDs were all incorrectly
1019e8d5bbf7SChen-Yu Tsai  * set to 0. This means for these old device trees, we cannot just look
1020e8d5bbf7SChen-Yu Tsai  * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
1021e8d5bbf7SChen-Yu Tsai  * incorrectly identified as TCON0.
1022e8d5bbf7SChen-Yu Tsai  *
1023e8d5bbf7SChen-Yu Tsai  * This function first checks if the TCON node has 2 input endpoints.
1024e8d5bbf7SChen-Yu Tsai  * If so, then the device tree is a corrected version, and it will use
1025e8d5bbf7SChen-Yu Tsai  * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
1026e8d5bbf7SChen-Yu Tsai  * to fetch the ID and engine directly. If not, then it is likely an
1027e8d5bbf7SChen-Yu Tsai  * old device trees, where the endpoint IDs were incorrect, but did not
1028e8d5bbf7SChen-Yu Tsai  * have endpoint connections between the backend and TCON across
1029e8d5bbf7SChen-Yu Tsai  * different display pipelines. It will fall back to the old method of
1030e8d5bbf7SChen-Yu Tsai  * traversing the  of_graph to try and find a matching engine by device
1031e8d5bbf7SChen-Yu Tsai  * node.
1032e8d5bbf7SChen-Yu Tsai  *
1033e8d5bbf7SChen-Yu Tsai  * In the case of single display pipeline device trees, either method
1034e8d5bbf7SChen-Yu Tsai  * works.
1035e8d5bbf7SChen-Yu Tsai  */
1036e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
1037e8d5bbf7SChen-Yu Tsai 						   struct device_node *node)
1038e8d5bbf7SChen-Yu Tsai {
1039e8d5bbf7SChen-Yu Tsai 	struct device_node *port;
1040e8d5bbf7SChen-Yu Tsai 	struct sunxi_engine *engine;
1041e8d5bbf7SChen-Yu Tsai 
1042e8d5bbf7SChen-Yu Tsai 	port = of_graph_get_port_by_id(node, 0);
1043e8d5bbf7SChen-Yu Tsai 	if (!port)
1044e8d5bbf7SChen-Yu Tsai 		return ERR_PTR(-EINVAL);
1045e8d5bbf7SChen-Yu Tsai 
1046e8d5bbf7SChen-Yu Tsai 	/*
1047e8d5bbf7SChen-Yu Tsai 	 * Is this a corrected device tree with cross pipeline
1048e8d5bbf7SChen-Yu Tsai 	 * connections between the backend and TCON?
1049e8d5bbf7SChen-Yu Tsai 	 */
1050e8d5bbf7SChen-Yu Tsai 	if (of_get_child_count(port) > 1) {
1051cf77d79bSJernej Skrabec 		int id;
1052cf77d79bSJernej Skrabec 
1053cf77d79bSJernej Skrabec 		/*
1054cf77d79bSJernej Skrabec 		 * When pipeline has the same number of TCONs and engines which
1055cf77d79bSJernej Skrabec 		 * are represented by frontends/backends (DE1) or mixers (DE2),
1056cf77d79bSJernej Skrabec 		 * we match them by their respective IDs. However, if pipeline
1057cf77d79bSJernej Skrabec 		 * contains TCON TOP, chances are that there are either more
1058cf77d79bSJernej Skrabec 		 * TCONs than engines (R40) or TCONs with non-consecutive ids.
1059cf77d79bSJernej Skrabec 		 * (H6). In that case it's easier just use TCON index in list
1060cf77d79bSJernej Skrabec 		 * as an id. That means that on R40, any 2 TCONs can be enabled
1061cf77d79bSJernej Skrabec 		 * in DT out of 4 (there are 2 mixers). Due to the design of
1062cf77d79bSJernej Skrabec 		 * TCON TOP, remaining 2 TCONs can't be connected to anything
1063cf77d79bSJernej Skrabec 		 * anyway.
1064cf77d79bSJernej Skrabec 		 */
1065cf77d79bSJernej Skrabec 		if (sun4i_tcon_connected_to_tcon_top(node))
1066cf77d79bSJernej Skrabec 			id = sun4i_tcon_get_index(drv);
1067cf77d79bSJernej Skrabec 		else
1068cf77d79bSJernej Skrabec 			id = sun4i_tcon_of_get_id_from_port(port);
1069e8d5bbf7SChen-Yu Tsai 
1070e8d5bbf7SChen-Yu Tsai 		/* Get our engine by matching our ID */
1071e8d5bbf7SChen-Yu Tsai 		engine = sun4i_tcon_get_engine_by_id(drv, id);
1072e8d5bbf7SChen-Yu Tsai 
1073e8d5bbf7SChen-Yu Tsai 		of_node_put(port);
1074e8d5bbf7SChen-Yu Tsai 		return engine;
1075e8d5bbf7SChen-Yu Tsai 	}
1076e8d5bbf7SChen-Yu Tsai 
1077e8d5bbf7SChen-Yu Tsai 	/* Fallback to old method by traversing input endpoints */
1078e8d5bbf7SChen-Yu Tsai 	of_node_put(port);
107949836b11SJernej Skrabec 	return sun4i_tcon_find_engine_traverse(drv, node, 0);
1080e8d5bbf7SChen-Yu Tsai }
1081e8d5bbf7SChen-Yu Tsai 
10829026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master,
10839026e0d1SMaxime Ripard 			   void *data)
10849026e0d1SMaxime Ripard {
10859026e0d1SMaxime Ripard 	struct drm_device *drm = data;
10869026e0d1SMaxime Ripard 	struct sun4i_drv *drv = drm->dev_private;
108787969338SIcenowy Zheng 	struct sunxi_engine *engine;
1088a0c1214eSMaxime Ripard 	struct device_node *remote;
10899026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon;
10906664e9dcSChen-Yu Tsai 	struct reset_control *edp_rstc;
1091a0c1214eSMaxime Ripard 	bool has_lvds_rst, has_lvds_alt, can_lvds;
10929026e0d1SMaxime Ripard 	int ret;
10939026e0d1SMaxime Ripard 
109487969338SIcenowy Zheng 	engine = sun4i_tcon_find_engine(drv, dev->of_node);
109587969338SIcenowy Zheng 	if (IS_ERR(engine)) {
109687969338SIcenowy Zheng 		dev_err(dev, "Couldn't find matching engine\n");
109780a58240SChen-Yu Tsai 		return -EPROBE_DEFER;
1098b317fa3bSChen-Yu Tsai 	}
109980a58240SChen-Yu Tsai 
11009026e0d1SMaxime Ripard 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
11019026e0d1SMaxime Ripard 	if (!tcon)
11029026e0d1SMaxime Ripard 		return -ENOMEM;
11039026e0d1SMaxime Ripard 	dev_set_drvdata(dev, tcon);
11049026e0d1SMaxime Ripard 	tcon->drm = drm;
1105ae558110SMaxime Ripard 	tcon->dev = dev;
110687969338SIcenowy Zheng 	tcon->id = engine->id;
110791ea2f29SChen-Yu Tsai 	tcon->quirks = of_device_get_match_data(dev);
11089026e0d1SMaxime Ripard 
11099026e0d1SMaxime Ripard 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
11109026e0d1SMaxime Ripard 	if (IS_ERR(tcon->lcd_rst)) {
11119026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't get our reset line\n");
11129026e0d1SMaxime Ripard 		return PTR_ERR(tcon->lcd_rst);
11139026e0d1SMaxime Ripard 	}
11149026e0d1SMaxime Ripard 
11156664e9dcSChen-Yu Tsai 	if (tcon->quirks->needs_edp_reset) {
11166664e9dcSChen-Yu Tsai 		edp_rstc = devm_reset_control_get_shared(dev, "edp");
11176664e9dcSChen-Yu Tsai 		if (IS_ERR(edp_rstc)) {
11186664e9dcSChen-Yu Tsai 			dev_err(dev, "Couldn't get edp reset line\n");
11196664e9dcSChen-Yu Tsai 			return PTR_ERR(edp_rstc);
11206664e9dcSChen-Yu Tsai 		}
11216664e9dcSChen-Yu Tsai 
11226664e9dcSChen-Yu Tsai 		ret = reset_control_deassert(edp_rstc);
11236664e9dcSChen-Yu Tsai 		if (ret) {
11246664e9dcSChen-Yu Tsai 			dev_err(dev, "Couldn't deassert edp reset line\n");
11256664e9dcSChen-Yu Tsai 			return ret;
11266664e9dcSChen-Yu Tsai 		}
11276664e9dcSChen-Yu Tsai 	}
11286664e9dcSChen-Yu Tsai 
11299026e0d1SMaxime Ripard 	/* Make sure our TCON is reset */
1130d57294c1SChen-Yu Tsai 	ret = reset_control_reset(tcon->lcd_rst);
11319026e0d1SMaxime Ripard 	if (ret) {
11329026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't deassert our reset line\n");
11339026e0d1SMaxime Ripard 		return ret;
11349026e0d1SMaxime Ripard 	}
11359026e0d1SMaxime Ripard 
1136e742a17cSMaxime Ripard 	if (tcon->quirks->supports_lvds) {
1137a0c1214eSMaxime Ripard 		/*
1138e742a17cSMaxime Ripard 		 * This can only be made optional since we've had DT
1139e742a17cSMaxime Ripard 		 * nodes without the LVDS reset properties.
1140a0c1214eSMaxime Ripard 		 *
1141e742a17cSMaxime Ripard 		 * If the property is missing, just disable LVDS, and
1142e742a17cSMaxime Ripard 		 * print a warning.
1143a0c1214eSMaxime Ripard 		 */
1144a0c1214eSMaxime Ripard 		tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
1145a0c1214eSMaxime Ripard 		if (IS_ERR(tcon->lvds_rst)) {
1146a0c1214eSMaxime Ripard 			dev_err(dev, "Couldn't get our reset line\n");
1147a0c1214eSMaxime Ripard 			return PTR_ERR(tcon->lvds_rst);
1148a0c1214eSMaxime Ripard 		} else if (tcon->lvds_rst) {
1149a0c1214eSMaxime Ripard 			has_lvds_rst = true;
1150a0c1214eSMaxime Ripard 			reset_control_reset(tcon->lvds_rst);
1151a0c1214eSMaxime Ripard 		} else {
1152a0c1214eSMaxime Ripard 			has_lvds_rst = false;
1153a0c1214eSMaxime Ripard 		}
1154a0c1214eSMaxime Ripard 
1155a0c1214eSMaxime Ripard 		/*
1156e742a17cSMaxime Ripard 		 * This can only be made optional since we've had DT
1157e742a17cSMaxime Ripard 		 * nodes without the LVDS reset properties.
1158a0c1214eSMaxime Ripard 		 *
1159e742a17cSMaxime Ripard 		 * If the property is missing, just disable LVDS, and
1160e742a17cSMaxime Ripard 		 * print a warning.
1161a0c1214eSMaxime Ripard 		 */
1162a0c1214eSMaxime Ripard 		if (tcon->quirks->has_lvds_alt) {
1163a0c1214eSMaxime Ripard 			tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
1164a0c1214eSMaxime Ripard 			if (IS_ERR(tcon->lvds_pll)) {
1165a0c1214eSMaxime Ripard 				if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
1166a0c1214eSMaxime Ripard 					has_lvds_alt = false;
1167a0c1214eSMaxime Ripard 				} else {
1168a0c1214eSMaxime Ripard 					dev_err(dev, "Couldn't get the LVDS PLL\n");
116986a3ae58SDan Carpenter 					return PTR_ERR(tcon->lvds_pll);
1170a0c1214eSMaxime Ripard 				}
1171a0c1214eSMaxime Ripard 			} else {
1172a0c1214eSMaxime Ripard 				has_lvds_alt = true;
1173a0c1214eSMaxime Ripard 			}
1174a0c1214eSMaxime Ripard 		}
1175a0c1214eSMaxime Ripard 
1176e742a17cSMaxime Ripard 		if (!has_lvds_rst ||
1177e742a17cSMaxime Ripard 		    (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
1178e742a17cSMaxime Ripard 			dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
1179a0c1214eSMaxime Ripard 			dev_warn(dev, "LVDS output disabled\n");
1180a0c1214eSMaxime Ripard 			can_lvds = false;
1181a0c1214eSMaxime Ripard 		} else {
1182a0c1214eSMaxime Ripard 			can_lvds = true;
1183a0c1214eSMaxime Ripard 		}
1184e742a17cSMaxime Ripard 	} else {
1185e742a17cSMaxime Ripard 		can_lvds = false;
1186e742a17cSMaxime Ripard 	}
1187a0c1214eSMaxime Ripard 
11889026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_clocks(dev, tcon);
11899026e0d1SMaxime Ripard 	if (ret) {
11909026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON clocks\n");
11919026e0d1SMaxime Ripard 		goto err_assert_reset;
11929026e0d1SMaxime Ripard 	}
11939026e0d1SMaxime Ripard 
11944c7f16d1SChen-Yu Tsai 	ret = sun4i_tcon_init_regmap(dev, tcon);
11959026e0d1SMaxime Ripard 	if (ret) {
11964c7f16d1SChen-Yu Tsai 		dev_err(dev, "Couldn't init our TCON regmap\n");
11979026e0d1SMaxime Ripard 		goto err_free_clocks;
11989026e0d1SMaxime Ripard 	}
11999026e0d1SMaxime Ripard 
120034d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
12014c7f16d1SChen-Yu Tsai 		ret = sun4i_dclk_create(dev, tcon);
12024c7f16d1SChen-Yu Tsai 		if (ret) {
12034c7f16d1SChen-Yu Tsai 			dev_err(dev, "Couldn't create our TCON dot clock\n");
12044c7f16d1SChen-Yu Tsai 			goto err_free_clocks;
12054c7f16d1SChen-Yu Tsai 		}
120634d698f6SJernej Skrabec 	}
12074c7f16d1SChen-Yu Tsai 
12089026e0d1SMaxime Ripard 	ret = sun4i_tcon_init_irq(dev, tcon);
12099026e0d1SMaxime Ripard 	if (ret) {
12109026e0d1SMaxime Ripard 		dev_err(dev, "Couldn't init our TCON interrupts\n");
12114c7f16d1SChen-Yu Tsai 		goto err_free_dotclock;
12129026e0d1SMaxime Ripard 	}
12139026e0d1SMaxime Ripard 
121487969338SIcenowy Zheng 	tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
121546cce6daSChen-Yu Tsai 	if (IS_ERR(tcon->crtc)) {
121646cce6daSChen-Yu Tsai 		dev_err(dev, "Couldn't create our CRTC\n");
121746cce6daSChen-Yu Tsai 		ret = PTR_ERR(tcon->crtc);
121892411f6dSMaxime Ripard 		goto err_free_dotclock;
121946cce6daSChen-Yu Tsai 	}
122046cce6daSChen-Yu Tsai 
12212a72d0c5SJernej Skrabec 	if (tcon->quirks->has_channel_0) {
1222a0c1214eSMaxime Ripard 		/*
1223a0c1214eSMaxime Ripard 		 * If we have an LVDS panel connected to the TCON, we should
1224a0c1214eSMaxime Ripard 		 * just probe the LVDS connector. Otherwise, just probe RGB as
1225a0c1214eSMaxime Ripard 		 * we used to.
1226a0c1214eSMaxime Ripard 		 */
1227a0c1214eSMaxime Ripard 		remote = of_graph_get_remote_node(dev->of_node, 1, 0);
1228a0c1214eSMaxime Ripard 		if (of_device_is_compatible(remote, "panel-lvds"))
1229a0c1214eSMaxime Ripard 			if (can_lvds)
1230a0c1214eSMaxime Ripard 				ret = sun4i_lvds_init(drm, tcon);
1231a0c1214eSMaxime Ripard 			else
1232a0c1214eSMaxime Ripard 				ret = -EINVAL;
1233a0c1214eSMaxime Ripard 		else
1234b9c8506cSChen-Yu Tsai 			ret = sun4i_rgb_init(drm, tcon);
1235a0c1214eSMaxime Ripard 		of_node_put(remote);
1236a0c1214eSMaxime Ripard 
123713fef095SChen-Yu Tsai 		if (ret < 0)
123892411f6dSMaxime Ripard 			goto err_free_dotclock;
12392a72d0c5SJernej Skrabec 	}
124013fef095SChen-Yu Tsai 
124127e18de7SChen-Yu Tsai 	if (tcon->quirks->needs_de_be_mux) {
124227e18de7SChen-Yu Tsai 		/*
124327e18de7SChen-Yu Tsai 		 * We assume there is no dynamic muxing of backends
124427e18de7SChen-Yu Tsai 		 * and TCONs, so we select the backend with same ID.
124527e18de7SChen-Yu Tsai 		 *
124627e18de7SChen-Yu Tsai 		 * While dynamic selection might be interesting, since
124727e18de7SChen-Yu Tsai 		 * the CRTC is tied to the TCON, while the layers are
124827e18de7SChen-Yu Tsai 		 * tied to the backends, this means, we will need to
124927e18de7SChen-Yu Tsai 		 * switch between groups of layers. There might not be
125027e18de7SChen-Yu Tsai 		 * a way to represent this constraint in DRM.
125127e18de7SChen-Yu Tsai 		 */
125227e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
125327e18de7SChen-Yu Tsai 				   SUN4I_TCON0_CTL_SRC_SEL_MASK,
125427e18de7SChen-Yu Tsai 				   tcon->id);
125527e18de7SChen-Yu Tsai 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
125627e18de7SChen-Yu Tsai 				   SUN4I_TCON1_CTL_SRC_SEL_MASK,
125727e18de7SChen-Yu Tsai 				   tcon->id);
125827e18de7SChen-Yu Tsai 	}
125927e18de7SChen-Yu Tsai 
126080a58240SChen-Yu Tsai 	list_add_tail(&tcon->list, &drv->tcon_list);
126180a58240SChen-Yu Tsai 
126213fef095SChen-Yu Tsai 	return 0;
12639026e0d1SMaxime Ripard 
12644c7f16d1SChen-Yu Tsai err_free_dotclock:
126534d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
12664c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
12679026e0d1SMaxime Ripard err_free_clocks:
12689026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
12699026e0d1SMaxime Ripard err_assert_reset:
12709026e0d1SMaxime Ripard 	reset_control_assert(tcon->lcd_rst);
12719026e0d1SMaxime Ripard 	return ret;
12729026e0d1SMaxime Ripard }
12739026e0d1SMaxime Ripard 
12749026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master,
12759026e0d1SMaxime Ripard 			      void *data)
12769026e0d1SMaxime Ripard {
12779026e0d1SMaxime Ripard 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
12789026e0d1SMaxime Ripard 
127980a58240SChen-Yu Tsai 	list_del(&tcon->list);
128034d698f6SJernej Skrabec 	if (tcon->quirks->has_channel_0)
12814c7f16d1SChen-Yu Tsai 		sun4i_dclk_free(tcon);
12829026e0d1SMaxime Ripard 	sun4i_tcon_free_clocks(tcon);
12839026e0d1SMaxime Ripard }
12849026e0d1SMaxime Ripard 
1285dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = {
12869026e0d1SMaxime Ripard 	.bind	= sun4i_tcon_bind,
12879026e0d1SMaxime Ripard 	.unbind	= sun4i_tcon_unbind,
12889026e0d1SMaxime Ripard };
12899026e0d1SMaxime Ripard 
12909026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev)
12919026e0d1SMaxime Ripard {
129229e57fabSMaxime Ripard 	struct device_node *node = pdev->dev.of_node;
129363d6310fSJernej Skrabec 	const struct sun4i_tcon_quirks *quirks;
1294894f5a9fSMaxime Ripard 	struct drm_bridge *bridge;
129529e57fabSMaxime Ripard 	struct drm_panel *panel;
1296ebc94461SRob Herring 	int ret;
129729e57fabSMaxime Ripard 
129863d6310fSJernej Skrabec 	quirks = of_device_get_match_data(&pdev->dev);
129963d6310fSJernej Skrabec 
130063d6310fSJernej Skrabec 	/* panels and bridges are present only on TCONs with channel 0 */
130163d6310fSJernej Skrabec 	if (quirks->has_channel_0) {
1302ebc94461SRob Herring 		ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1303ebc94461SRob Herring 		if (ret == -EPROBE_DEFER)
1304ebc94461SRob Herring 			return ret;
130563d6310fSJernej Skrabec 	}
130629e57fabSMaxime Ripard 
13079026e0d1SMaxime Ripard 	return component_add(&pdev->dev, &sun4i_tcon_ops);
13089026e0d1SMaxime Ripard }
13099026e0d1SMaxime Ripard 
13109026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev)
13119026e0d1SMaxime Ripard {
13129026e0d1SMaxime Ripard 	component_del(&pdev->dev, &sun4i_tcon_ops);
13139026e0d1SMaxime Ripard 
13149026e0d1SMaxime Ripard 	return 0;
13159026e0d1SMaxime Ripard }
13169026e0d1SMaxime Ripard 
1317ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */
13184bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
13194bb206bfSJonathan Liu 				  const struct drm_encoder *encoder)
13204bb206bfSJonathan Liu {
13214bb206bfSJonathan Liu 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
13224bb206bfSJonathan Liu 	u32 shift;
13234bb206bfSJonathan Liu 
13244bb206bfSJonathan Liu 	if (!tcon0)
13254bb206bfSJonathan Liu 		return -EINVAL;
13264bb206bfSJonathan Liu 
13274bb206bfSJonathan Liu 	switch (encoder->encoder_type) {
13284bb206bfSJonathan Liu 	case DRM_MODE_ENCODER_TMDS:
13294bb206bfSJonathan Liu 		/* HDMI */
13304bb206bfSJonathan Liu 		shift = 8;
13314bb206bfSJonathan Liu 		break;
13324bb206bfSJonathan Liu 	default:
13334bb206bfSJonathan Liu 		return -EINVAL;
13344bb206bfSJonathan Liu 	}
13354bb206bfSJonathan Liu 
13364bb206bfSJonathan Liu 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
13374bb206bfSJonathan Liu 			   0x3 << shift, tcon->id << shift);
13384bb206bfSJonathan Liu 
13394bb206bfSJonathan Liu 	return 0;
13404bb206bfSJonathan Liu }
13414bb206bfSJonathan Liu 
1342ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
1343abcb8766SMaxime Ripard 				  const struct drm_encoder *encoder)
1344ad537fb2SChen-Yu Tsai {
1345ad537fb2SChen-Yu Tsai 	u32 val;
1346ad537fb2SChen-Yu Tsai 
1347ad537fb2SChen-Yu Tsai 	if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1348ad537fb2SChen-Yu Tsai 		val = 1;
1349ad537fb2SChen-Yu Tsai 	else
1350ad537fb2SChen-Yu Tsai 		val = 0;
1351ad537fb2SChen-Yu Tsai 
1352ad537fb2SChen-Yu Tsai 	/*
1353ad537fb2SChen-Yu Tsai 	 * FIXME: Undocumented bits
1354ad537fb2SChen-Yu Tsai 	 */
1355ad537fb2SChen-Yu Tsai 	return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1356ad537fb2SChen-Yu Tsai }
1357ad537fb2SChen-Yu Tsai 
135867e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
1359abcb8766SMaxime Ripard 			      const struct drm_encoder *encoder)
136067e32645SChen-Yu Tsai {
136167e32645SChen-Yu Tsai 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
136267e32645SChen-Yu Tsai 	u32 shift;
136367e32645SChen-Yu Tsai 
136467e32645SChen-Yu Tsai 	if (!tcon0)
136567e32645SChen-Yu Tsai 		return -EINVAL;
136667e32645SChen-Yu Tsai 
136767e32645SChen-Yu Tsai 	switch (encoder->encoder_type) {
136867e32645SChen-Yu Tsai 	case DRM_MODE_ENCODER_TMDS:
136967e32645SChen-Yu Tsai 		/* HDMI */
137067e32645SChen-Yu Tsai 		shift = 8;
137167e32645SChen-Yu Tsai 		break;
137267e32645SChen-Yu Tsai 	default:
137367e32645SChen-Yu Tsai 		/* TODO A31 has MIPI DSI but A31s does not */
137467e32645SChen-Yu Tsai 		return -EINVAL;
137567e32645SChen-Yu Tsai 	}
137667e32645SChen-Yu Tsai 
137767e32645SChen-Yu Tsai 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
137867e32645SChen-Yu Tsai 			   0x3 << shift, tcon->id << shift);
137967e32645SChen-Yu Tsai 
138067e32645SChen-Yu Tsai 	return 0;
138167e32645SChen-Yu Tsai }
138267e32645SChen-Yu Tsai 
13830305189aSJernej Skrabec static int sun8i_r40_tcon_tv_set_mux(struct sun4i_tcon *tcon,
13840305189aSJernej Skrabec 				     const struct drm_encoder *encoder)
13850305189aSJernej Skrabec {
13860305189aSJernej Skrabec 	struct device_node *port, *remote;
13870305189aSJernej Skrabec 	struct platform_device *pdev;
13880305189aSJernej Skrabec 	int id, ret;
13890305189aSJernej Skrabec 
13900305189aSJernej Skrabec 	/* find TCON TOP platform device and TCON id */
13910305189aSJernej Skrabec 
13920305189aSJernej Skrabec 	port = of_graph_get_port_by_id(tcon->dev->of_node, 0);
13930305189aSJernej Skrabec 	if (!port)
13940305189aSJernej Skrabec 		return -EINVAL;
13950305189aSJernej Skrabec 
13960305189aSJernej Skrabec 	id = sun4i_tcon_of_get_id_from_port(port);
13970305189aSJernej Skrabec 	of_node_put(port);
13980305189aSJernej Skrabec 
13990305189aSJernej Skrabec 	remote = of_graph_get_remote_node(tcon->dev->of_node, 0, -1);
14000305189aSJernej Skrabec 	if (!remote)
14010305189aSJernej Skrabec 		return -EINVAL;
14020305189aSJernej Skrabec 
14030305189aSJernej Skrabec 	pdev = of_find_device_by_node(remote);
14040305189aSJernej Skrabec 	of_node_put(remote);
14050305189aSJernej Skrabec 	if (!pdev)
14060305189aSJernej Skrabec 		return -EINVAL;
14070305189aSJernej Skrabec 
1408185e0bebSMaxime Ripard 	if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
1409185e0bebSMaxime Ripard 	    encoder->encoder_type == DRM_MODE_ENCODER_TMDS) {
14100305189aSJernej Skrabec 		ret = sun8i_tcon_top_set_hdmi_src(&pdev->dev, id);
14110305189aSJernej Skrabec 		if (ret)
14120305189aSJernej Skrabec 			return ret;
14130305189aSJernej Skrabec 	}
14140305189aSJernej Skrabec 
1415185e0bebSMaxime Ripard 	if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP)) {
1416185e0bebSMaxime Ripard 		ret = sun8i_tcon_top_de_config(&pdev->dev, tcon->id, id);
1417185e0bebSMaxime Ripard 		if (ret)
1418185e0bebSMaxime Ripard 			return ret;
1419185e0bebSMaxime Ripard 	}
1420185e0bebSMaxime Ripard 
1421185e0bebSMaxime Ripard 	return 0;
14220305189aSJernej Skrabec }
14230305189aSJernej Skrabec 
14244bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
142534d698f6SJernej Skrabec 	.has_channel_0		= true,
14264bb206bfSJonathan Liu 	.has_channel_1		= true,
14274bb206bfSJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
14284bb206bfSJonathan Liu };
14294bb206bfSJonathan Liu 
143091ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
143134d698f6SJernej Skrabec 	.has_channel_0		= true,
143291ea2f29SChen-Yu Tsai 	.has_channel_1		= true,
1433ad537fb2SChen-Yu Tsai 	.set_mux		= sun5i_a13_tcon_set_mux,
143491ea2f29SChen-Yu Tsai };
143591ea2f29SChen-Yu Tsai 
143693a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
143734d698f6SJernej Skrabec 	.has_channel_0		= true,
143893a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
1439a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
144027e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
144167e32645SChen-Yu Tsai 	.set_mux		= sun6i_tcon_set_mux,
144293a5ec14SChen-Yu Tsai };
144393a5ec14SChen-Yu Tsai 
144493a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
144534d698f6SJernej Skrabec 	.has_channel_0		= true,
144693a5ec14SChen-Yu Tsai 	.has_channel_1		= true,
144727e18de7SChen-Yu Tsai 	.needs_de_be_mux	= true,
144893a5ec14SChen-Yu Tsai };
144993a5ec14SChen-Yu Tsai 
1450aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
145134d698f6SJernej Skrabec 	.has_channel_0		= true,
1452aaddb6d2SJonathan Liu 	.has_channel_1		= true,
1453aaddb6d2SJonathan Liu 	/* Same display pipeline structure as A10 */
1454aaddb6d2SJonathan Liu 	.set_mux		= sun4i_a10_tcon_set_mux,
1455aaddb6d2SJonathan Liu };
1456aaddb6d2SJonathan Liu 
145791ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
145834d698f6SJernej Skrabec 	.has_channel_0		= true,
1459a0c1214eSMaxime Ripard 	.has_lvds_alt		= true,
146091ea2f29SChen-Yu Tsai };
146191ea2f29SChen-Yu Tsai 
14622f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
1463e742a17cSMaxime Ripard 	.supports_lvds		= true,
146434d698f6SJernej Skrabec 	.has_channel_0		= true,
14652f0d7bb1SMaxime Ripard };
14662f0d7bb1SMaxime Ripard 
146705adc89bSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
146805adc89bSJernej Skrabec 	.has_channel_1		= true,
146905adc89bSJernej Skrabec };
147005adc89bSJernej Skrabec 
14710305189aSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_r40_tv_quirks = {
14720305189aSJernej Skrabec 	.has_channel_1		= true,
14730305189aSJernej Skrabec 	.set_mux		= sun8i_r40_tcon_tv_set_mux,
14740305189aSJernej Skrabec };
14750305189aSJernej Skrabec 
14761a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
147734d698f6SJernej Skrabec 	.has_channel_0		= true,
14781a0edb3fSIcenowy Zheng };
14791a0edb3fSIcenowy Zheng 
14806664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
14816664e9dcSChen-Yu Tsai 	.has_channel_0	= true,
14826664e9dcSChen-Yu Tsai 	.needs_edp_reset = true,
14836664e9dcSChen-Yu Tsai };
14846664e9dcSChen-Yu Tsai 
14856664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
14866664e9dcSChen-Yu Tsai 	.has_channel_1	= true,
14876664e9dcSChen-Yu Tsai 	.needs_edp_reset = true,
14886664e9dcSChen-Yu Tsai };
14896664e9dcSChen-Yu Tsai 
1490ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */
1491ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = {
14924bb206bfSJonathan Liu 	{ .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
149391ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
149493a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
149593a5ec14SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
1496aaddb6d2SJonathan Liu 	{ .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
1497d0ec0a3eSChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a23-tcon", .data = &sun8i_a33_quirks },
149891ea2f29SChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
14992f0d7bb1SMaxime Ripard 	{ .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
150005adc89bSJernej Skrabec 	{ .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
15010305189aSJernej Skrabec 	{ .compatible = "allwinner,sun8i-r40-tcon-tv", .data = &sun8i_r40_tv_quirks },
15021a0edb3fSIcenowy Zheng 	{ .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
15036664e9dcSChen-Yu Tsai 	{ .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
15046664e9dcSChen-Yu Tsai 	{ .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
15059026e0d1SMaxime Ripard 	{ }
15069026e0d1SMaxime Ripard };
15079026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
1508ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table);
15099026e0d1SMaxime Ripard 
15109026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = {
15119026e0d1SMaxime Ripard 	.probe		= sun4i_tcon_probe,
15129026e0d1SMaxime Ripard 	.remove		= sun4i_tcon_remove,
15139026e0d1SMaxime Ripard 	.driver		= {
15149026e0d1SMaxime Ripard 		.name		= "sun4i-tcon",
15159026e0d1SMaxime Ripard 		.of_match_table	= sun4i_tcon_of_table,
15169026e0d1SMaxime Ripard 	},
15179026e0d1SMaxime Ripard };
15189026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver);
15199026e0d1SMaxime Ripard 
15209026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
15219026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
15229026e0d1SMaxime Ripard MODULE_LICENSE("GPL");
1523