xref: /linux/drivers/gpu/drm/sun4i/sun4i_tcon.c (revision b45e0c30bc58fb6fcaa42f1d1d813cefb8ab4117)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Free Electrons
4  * Copyright (C) 2015 NextThing Co
5  *
6  * Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8 
9 #include <drm/drmP.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_connector.h>
12 #include <drm/drm_crtc.h>
13 #include <drm/drm_encoder.h>
14 #include <drm/drm_modes.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_panel.h>
17 #include <drm/drm_probe_helper.h>
18 
19 #include <uapi/drm/drm_mode.h>
20 
21 #include <linux/component.h>
22 #include <linux/ioport.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/of_irq.h>
26 #include <linux/regmap.h>
27 #include <linux/reset.h>
28 
29 #include "sun4i_crtc.h"
30 #include "sun4i_dotclock.h"
31 #include "sun4i_drv.h"
32 #include "sun4i_lvds.h"
33 #include "sun4i_rgb.h"
34 #include "sun4i_tcon.h"
35 #include "sun6i_mipi_dsi.h"
36 #include "sun8i_tcon_top.h"
37 #include "sunxi_engine.h"
38 
39 static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder)
40 {
41 	struct drm_connector *connector;
42 	struct drm_connector_list_iter iter;
43 
44 	drm_connector_list_iter_begin(encoder->dev, &iter);
45 	drm_for_each_connector_iter(connector, &iter)
46 		if (connector->encoder == encoder) {
47 			drm_connector_list_iter_end(&iter);
48 			return connector;
49 		}
50 	drm_connector_list_iter_end(&iter);
51 
52 	return NULL;
53 }
54 
55 static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder)
56 {
57 	struct drm_connector *connector;
58 	struct drm_display_info *info;
59 
60 	connector = sun4i_tcon_get_connector(encoder);
61 	if (!connector)
62 		return -EINVAL;
63 
64 	info = &connector->display_info;
65 	if (info->num_bus_formats != 1)
66 		return -EINVAL;
67 
68 	switch (info->bus_formats[0]) {
69 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
70 		return 18;
71 
72 	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
73 	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
74 		return 24;
75 	}
76 
77 	return -EINVAL;
78 }
79 
80 static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
81 					  bool enabled)
82 {
83 	struct clk *clk;
84 
85 	switch (channel) {
86 	case 0:
87 		WARN_ON(!tcon->quirks->has_channel_0);
88 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
89 				   SUN4I_TCON0_CTL_TCON_ENABLE,
90 				   enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
91 		clk = tcon->dclk;
92 		break;
93 	case 1:
94 		WARN_ON(!tcon->quirks->has_channel_1);
95 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
96 				   SUN4I_TCON1_CTL_TCON_ENABLE,
97 				   enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
98 		clk = tcon->sclk1;
99 		break;
100 	default:
101 		DRM_WARN("Unknown channel... doing nothing\n");
102 		return;
103 	}
104 
105 	if (enabled) {
106 		clk_prepare_enable(clk);
107 		clk_rate_exclusive_get(clk);
108 	} else {
109 		clk_rate_exclusive_put(clk);
110 		clk_disable_unprepare(clk);
111 	}
112 }
113 
114 static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon,
115 				       const struct drm_encoder *encoder,
116 				       bool enabled)
117 {
118 	if (enabled) {
119 		u8 val;
120 
121 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
122 				   SUN4I_TCON0_LVDS_IF_EN,
123 				   SUN4I_TCON0_LVDS_IF_EN);
124 
125 		/*
126 		 * As their name suggest, these values only apply to the A31
127 		 * and later SoCs. We'll have to rework this when merging
128 		 * support for the older SoCs.
129 		 */
130 		regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
131 			     SUN6I_TCON0_LVDS_ANA0_C(2) |
132 			     SUN6I_TCON0_LVDS_ANA0_V(3) |
133 			     SUN6I_TCON0_LVDS_ANA0_PD(2) |
134 			     SUN6I_TCON0_LVDS_ANA0_EN_LDO);
135 		udelay(2);
136 
137 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
138 				   SUN6I_TCON0_LVDS_ANA0_EN_MB,
139 				   SUN6I_TCON0_LVDS_ANA0_EN_MB);
140 		udelay(2);
141 
142 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
143 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC,
144 				   SUN6I_TCON0_LVDS_ANA0_EN_DRVC);
145 
146 		if (sun4i_tcon_get_pixel_depth(encoder) == 18)
147 			val = 7;
148 		else
149 			val = 0xf;
150 
151 		regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
152 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf),
153 				  SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val));
154 	} else {
155 		regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
156 				   SUN4I_TCON0_LVDS_IF_EN, 0);
157 	}
158 }
159 
160 void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
161 			   const struct drm_encoder *encoder,
162 			   bool enabled)
163 {
164 	bool is_lvds = false;
165 	int channel;
166 
167 	switch (encoder->encoder_type) {
168 	case DRM_MODE_ENCODER_LVDS:
169 		is_lvds = true;
170 		/* Fallthrough */
171 	case DRM_MODE_ENCODER_DSI:
172 	case DRM_MODE_ENCODER_NONE:
173 		channel = 0;
174 		break;
175 	case DRM_MODE_ENCODER_TMDS:
176 	case DRM_MODE_ENCODER_TVDAC:
177 		channel = 1;
178 		break;
179 	default:
180 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
181 		return;
182 	}
183 
184 	if (is_lvds && !enabled)
185 		sun4i_tcon_lvds_set_status(tcon, encoder, false);
186 
187 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
188 			   SUN4I_TCON_GCTL_TCON_ENABLE,
189 			   enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
190 
191 	if (is_lvds && enabled)
192 		sun4i_tcon_lvds_set_status(tcon, encoder, true);
193 
194 	sun4i_tcon_channel_set_status(tcon, channel, enabled);
195 }
196 
197 void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
198 {
199 	u32 mask, val = 0;
200 
201 	DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
202 
203 	mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
204 		SUN4I_TCON_GINT0_VBLANK_ENABLE(1) |
205 		SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE;
206 
207 	if (enable)
208 		val = mask;
209 
210 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
211 }
212 EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
213 
214 /*
215  * This function is a helper for TCON output muxing. The TCON output
216  * muxing control register in earlier SoCs (without the TCON TOP block)
217  * are located in TCON0. This helper returns a pointer to TCON0's
218  * sun4i_tcon structure, or NULL if not found.
219  */
220 static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
221 {
222 	struct sun4i_drv *drv = drm->dev_private;
223 	struct sun4i_tcon *tcon;
224 
225 	list_for_each_entry(tcon, &drv->tcon_list, list)
226 		if (tcon->id == 0)
227 			return tcon;
228 
229 	dev_warn(drm->dev,
230 		 "TCON0 not found, display output muxing may not work\n");
231 
232 	return NULL;
233 }
234 
235 static void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
236 			       const struct drm_encoder *encoder)
237 {
238 	int ret = -ENOTSUPP;
239 
240 	if (tcon->quirks->set_mux)
241 		ret = tcon->quirks->set_mux(tcon, encoder);
242 
243 	DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
244 			 encoder->name, encoder->crtc->name, ret);
245 }
246 
247 static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
248 				    int channel)
249 {
250 	int delay = mode->vtotal - mode->vdisplay;
251 
252 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
253 		delay /= 2;
254 
255 	if (channel == 1)
256 		delay -= 2;
257 
258 	delay = min(delay, 30);
259 
260 	DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
261 
262 	return delay;
263 }
264 
265 static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon,
266 					const struct drm_display_mode *mode)
267 {
268 	/* Configure the dot clock */
269 	clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
270 
271 	/* Set the resolution */
272 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
273 		     SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
274 		     SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
275 }
276 
277 static void sun4i_tcon0_mode_set_dithering(struct sun4i_tcon *tcon,
278 					   const struct drm_connector *connector)
279 {
280 	u32 bus_format = 0;
281 	u32 val = 0;
282 
283 	/* XXX Would this ever happen? */
284 	if (!connector)
285 		return;
286 
287 	/*
288 	 * FIXME: Undocumented bits
289 	 *
290 	 * The whole dithering process and these parameters are not
291 	 * explained in the vendor documents or BSP kernel code.
292 	 */
293 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PR_REG, 0x11111111);
294 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PG_REG, 0x11111111);
295 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PB_REG, 0x11111111);
296 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LR_REG, 0x11111111);
297 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LG_REG, 0x11111111);
298 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LB_REG, 0x11111111);
299 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL0_REG, 0x01010000);
300 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL1_REG, 0x15151111);
301 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL2_REG, 0x57575555);
302 	regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL3_REG, 0x7f7f7777);
303 
304 	/* Do dithering if panel only supports 6 bits per color */
305 	if (connector->display_info.bpc == 6)
306 		val |= SUN4I_TCON0_FRM_CTL_EN;
307 
308 	if (connector->display_info.num_bus_formats == 1)
309 		bus_format = connector->display_info.bus_formats[0];
310 
311 	/* Check the connection format */
312 	switch (bus_format) {
313 	case MEDIA_BUS_FMT_RGB565_1X16:
314 		/* R and B components are only 5 bits deep */
315 		val |= SUN4I_TCON0_FRM_CTL_MODE_R;
316 		val |= SUN4I_TCON0_FRM_CTL_MODE_B;
317 		/* Fall through */
318 	case MEDIA_BUS_FMT_RGB666_1X18:
319 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
320 		/* Fall through: enable dithering */
321 		val |= SUN4I_TCON0_FRM_CTL_EN;
322 		break;
323 	}
324 
325 	/* Write dithering settings */
326 	regmap_write(tcon->regs, SUN4I_TCON_FRM_CTL_REG, val);
327 }
328 
329 static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon,
330 				     const struct drm_encoder *encoder,
331 				     const struct drm_display_mode *mode)
332 {
333 	/* TODO support normal CPU interface modes */
334 	struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder);
335 	struct mipi_dsi_device *device = dsi->device;
336 	u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format);
337 	u8 lanes = device->lanes;
338 	u32 block_space, start_delay;
339 	u32 tcon_div;
340 
341 	tcon->dclk_min_div = SUN6I_DSI_TCON_DIV;
342 	tcon->dclk_max_div = SUN6I_DSI_TCON_DIV;
343 
344 	sun4i_tcon0_mode_set_common(tcon, mode);
345 
346 	/* Set dithering if needed */
347 	sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder));
348 
349 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
350 			   SUN4I_TCON0_CTL_IF_MASK,
351 			   SUN4I_TCON0_CTL_IF_8080);
352 
353 	regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG,
354 		     SUN4I_TCON_ECC_FIFO_EN);
355 
356 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG,
357 		     SUN4I_TCON0_CPU_IF_MODE_DSI |
358 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH |
359 		     SUN4I_TCON0_CPU_IF_TRI_FIFO_EN |
360 		     SUN4I_TCON0_CPU_IF_TRI_EN);
361 
362 	/*
363 	 * This looks suspicious, but it works...
364 	 *
365 	 * The datasheet says that this should be set higher than 20 *
366 	 * pixel cycle, but it's not clear what a pixel cycle is.
367 	 */
368 	regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div);
369 	tcon_div &= GENMASK(6, 0);
370 	block_space = mode->htotal * bpp / (tcon_div * lanes);
371 	block_space -= mode->hdisplay + 40;
372 
373 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG,
374 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) |
375 		     SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay));
376 
377 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG,
378 		     SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay));
379 
380 	start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1);
381 	start_delay = start_delay * mode->crtc_htotal * 149;
382 	start_delay = start_delay / (mode->crtc_clock / 1000) / 8;
383 	regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG,
384 		     SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) |
385 		     SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay));
386 
387 	/*
388 	 * The Allwinner BSP has a comment that the period should be
389 	 * the display clock * 15, but uses an hardcoded 3000...
390 	 */
391 	regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG,
392 		     SUN4I_TCON_SAFE_PERIOD_NUM(3000) |
393 		     SUN4I_TCON_SAFE_PERIOD_MODE(3));
394 
395 	/* Enable the output on the pins */
396 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG,
397 		     0xe0000000);
398 }
399 
400 static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
401 				      const struct drm_encoder *encoder,
402 				      const struct drm_display_mode *mode)
403 {
404 	unsigned int bp;
405 	u8 clk_delay;
406 	u32 reg, val = 0;
407 
408 	WARN_ON(!tcon->quirks->has_channel_0);
409 
410 	tcon->dclk_min_div = 7;
411 	tcon->dclk_max_div = 7;
412 	sun4i_tcon0_mode_set_common(tcon, mode);
413 
414 	/* Set dithering if needed */
415 	sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder));
416 
417 	/* Adjust clock delay */
418 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
419 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
420 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
421 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
422 
423 	/*
424 	 * This is called a backporch in the register documentation,
425 	 * but it really is the back porch + hsync
426 	 */
427 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
428 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
429 			 mode->crtc_htotal, bp);
430 
431 	/* Set horizontal display timings */
432 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
433 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
434 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
435 
436 	/*
437 	 * This is called a backporch in the register documentation,
438 	 * but it really is the back porch + hsync
439 	 */
440 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
441 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
442 			 mode->crtc_vtotal, bp);
443 
444 	/* Set vertical display timings */
445 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
446 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
447 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
448 
449 	reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
450 		SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
451 		SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
452 	if (sun4i_tcon_get_pixel_depth(encoder) == 24)
453 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
454 	else
455 		reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
456 
457 	regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
458 
459 	/* Setup the polarity of the various signals */
460 	if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
461 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
462 
463 	if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
464 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
465 
466 	regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
467 
468 	/* Map output pins to channel 0 */
469 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
470 			   SUN4I_TCON_GCTL_IOMAP_MASK,
471 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
472 
473 	/* Enable the output on the pins */
474 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000);
475 }
476 
477 static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
478 				     const struct drm_encoder *encoder,
479 				     const struct drm_display_mode *mode)
480 {
481 	struct drm_connector *connector = sun4i_tcon_get_connector(encoder);
482 	struct drm_display_info display_info = connector->display_info;
483 	unsigned int bp, hsync, vsync;
484 	u8 clk_delay;
485 	u32 val = 0;
486 
487 	WARN_ON(!tcon->quirks->has_channel_0);
488 
489 	tcon->dclk_min_div = 6;
490 	tcon->dclk_max_div = 127;
491 	sun4i_tcon0_mode_set_common(tcon, mode);
492 
493 	/* Set dithering if needed */
494 	sun4i_tcon0_mode_set_dithering(tcon, connector);
495 
496 	/* Adjust clock delay */
497 	clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
498 	regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
499 			   SUN4I_TCON0_CTL_CLK_DELAY_MASK,
500 			   SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
501 
502 	/*
503 	 * This is called a backporch in the register documentation,
504 	 * but it really is the back porch + hsync
505 	 */
506 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
507 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
508 			 mode->crtc_htotal, bp);
509 
510 	/* Set horizontal display timings */
511 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
512 		     SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
513 		     SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
514 
515 	/*
516 	 * This is called a backporch in the register documentation,
517 	 * but it really is the back porch + hsync
518 	 */
519 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
520 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
521 			 mode->crtc_vtotal, bp);
522 
523 	/* Set vertical display timings */
524 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
525 		     SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
526 		     SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
527 
528 	/* Set Hsync and Vsync length */
529 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
530 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
531 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
532 	regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
533 		     SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
534 		     SUN4I_TCON0_BASIC3_H_SYNC(hsync));
535 
536 	/* Setup the polarity of the various signals */
537 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
538 		val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
539 
540 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
541 		val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
542 
543 	if (display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
544 		val |= SUN4I_TCON0_IO_POL_DE_NEGATIVE;
545 
546 	/*
547 	 * On A20 and similar SoCs, the only way to achieve Positive Edge
548 	 * (Rising Edge), is setting dclk clock phase to 2/3(240°).
549 	 * By default TCON works in Negative Edge(Falling Edge),
550 	 * this is why phase is set to 0 in that case.
551 	 * Unfortunately there's no way to logically invert dclk through
552 	 * IO_POL register.
553 	 * The only acceptable way to work, triple checked with scope,
554 	 * is using clock phase set to 0° for Negative Edge and set to 240°
555 	 * for Positive Edge.
556 	 * On A33 and similar SoCs there would be a 90° phase option,
557 	 * but it divides also dclk by 2.
558 	 * Following code is a way to avoid quirks all around TCON
559 	 * and DOTCLOCK drivers.
560 	 */
561 	if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
562 		clk_set_phase(tcon->dclk, 240);
563 
564 	if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
565 		clk_set_phase(tcon->dclk, 0);
566 
567 	regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
568 			   SUN4I_TCON0_IO_POL_HSYNC_POSITIVE |
569 			   SUN4I_TCON0_IO_POL_VSYNC_POSITIVE |
570 			   SUN4I_TCON0_IO_POL_DE_NEGATIVE,
571 			   val);
572 
573 	/* Map output pins to channel 0 */
574 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
575 			   SUN4I_TCON_GCTL_IOMAP_MASK,
576 			   SUN4I_TCON_GCTL_IOMAP_TCON0);
577 
578 	/* Enable the output on the pins */
579 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
580 }
581 
582 static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
583 				 const struct drm_display_mode *mode)
584 {
585 	unsigned int bp, hsync, vsync, vtotal;
586 	u8 clk_delay;
587 	u32 val;
588 
589 	WARN_ON(!tcon->quirks->has_channel_1);
590 
591 	/* Configure the dot clock */
592 	clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
593 
594 	/* Adjust clock delay */
595 	clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
596 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
597 			   SUN4I_TCON1_CTL_CLK_DELAY_MASK,
598 			   SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
599 
600 	/* Set interlaced mode */
601 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
602 		val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
603 	else
604 		val = 0;
605 	regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
606 			   SUN4I_TCON1_CTL_INTERLACE_ENABLE,
607 			   val);
608 
609 	/* Set the input resolution */
610 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
611 		     SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
612 		     SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
613 
614 	/* Set the upscaling resolution */
615 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
616 		     SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
617 		     SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
618 
619 	/* Set the output resolution */
620 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
621 		     SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
622 		     SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
623 
624 	/* Set horizontal display timings */
625 	bp = mode->crtc_htotal - mode->crtc_hsync_start;
626 	DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
627 			 mode->htotal, bp);
628 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
629 		     SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
630 		     SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
631 
632 	bp = mode->crtc_vtotal - mode->crtc_vsync_start;
633 	DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
634 			 mode->crtc_vtotal, bp);
635 
636 	/*
637 	 * The vertical resolution needs to be doubled in all
638 	 * cases. We could use crtc_vtotal and always multiply by two,
639 	 * but that leads to a rounding error in interlace when vtotal
640 	 * is odd.
641 	 *
642 	 * This happens with TV's PAL for example, where vtotal will
643 	 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
644 	 * 624, which apparently confuses the hardware.
645 	 *
646 	 * To work around this, we will always use vtotal, and
647 	 * multiply by two only if we're not in interlace.
648 	 */
649 	vtotal = mode->vtotal;
650 	if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
651 		vtotal = vtotal * 2;
652 
653 	/* Set vertical display timings */
654 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
655 		     SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
656 		     SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
657 
658 	/* Set Hsync and Vsync length */
659 	hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
660 	vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
661 	DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
662 	regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
663 		     SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
664 		     SUN4I_TCON1_BASIC5_H_SYNC(hsync));
665 
666 	/* Map output pins to channel 1 */
667 	regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
668 			   SUN4I_TCON_GCTL_IOMAP_MASK,
669 			   SUN4I_TCON_GCTL_IOMAP_TCON1);
670 }
671 
672 void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
673 			 const struct drm_encoder *encoder,
674 			 const struct drm_display_mode *mode)
675 {
676 	switch (encoder->encoder_type) {
677 	case DRM_MODE_ENCODER_DSI:
678 		/* DSI is tied to special case of CPU interface */
679 		sun4i_tcon0_mode_set_cpu(tcon, encoder, mode);
680 		break;
681 	case DRM_MODE_ENCODER_LVDS:
682 		sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
683 		break;
684 	case DRM_MODE_ENCODER_NONE:
685 		sun4i_tcon0_mode_set_rgb(tcon, encoder, mode);
686 		sun4i_tcon_set_mux(tcon, 0, encoder);
687 		break;
688 	case DRM_MODE_ENCODER_TVDAC:
689 	case DRM_MODE_ENCODER_TMDS:
690 		sun4i_tcon1_mode_set(tcon, mode);
691 		sun4i_tcon_set_mux(tcon, 1, encoder);
692 		break;
693 	default:
694 		DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
695 	}
696 }
697 EXPORT_SYMBOL(sun4i_tcon_mode_set);
698 
699 static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
700 					struct sun4i_crtc *scrtc)
701 {
702 	unsigned long flags;
703 
704 	spin_lock_irqsave(&dev->event_lock, flags);
705 	if (scrtc->event) {
706 		drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
707 		drm_crtc_vblank_put(&scrtc->crtc);
708 		scrtc->event = NULL;
709 	}
710 	spin_unlock_irqrestore(&dev->event_lock, flags);
711 }
712 
713 static irqreturn_t sun4i_tcon_handler(int irq, void *private)
714 {
715 	struct sun4i_tcon *tcon = private;
716 	struct drm_device *drm = tcon->drm;
717 	struct sun4i_crtc *scrtc = tcon->crtc;
718 	struct sunxi_engine *engine = scrtc->engine;
719 	unsigned int status;
720 
721 	regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
722 
723 	if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
724 			SUN4I_TCON_GINT0_VBLANK_INT(1) |
725 			SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT)))
726 		return IRQ_NONE;
727 
728 	drm_crtc_handle_vblank(&scrtc->crtc);
729 	sun4i_tcon_finish_page_flip(drm, scrtc);
730 
731 	/* Acknowledge the interrupt */
732 	regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
733 			   SUN4I_TCON_GINT0_VBLANK_INT(0) |
734 			   SUN4I_TCON_GINT0_VBLANK_INT(1) |
735 			   SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT,
736 			   0);
737 
738 	if (engine->ops->vblank_quirk)
739 		engine->ops->vblank_quirk(engine);
740 
741 	return IRQ_HANDLED;
742 }
743 
744 static int sun4i_tcon_init_clocks(struct device *dev,
745 				  struct sun4i_tcon *tcon)
746 {
747 	tcon->clk = devm_clk_get(dev, "ahb");
748 	if (IS_ERR(tcon->clk)) {
749 		dev_err(dev, "Couldn't get the TCON bus clock\n");
750 		return PTR_ERR(tcon->clk);
751 	}
752 	clk_prepare_enable(tcon->clk);
753 
754 	if (tcon->quirks->has_channel_0) {
755 		tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
756 		if (IS_ERR(tcon->sclk0)) {
757 			dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
758 			return PTR_ERR(tcon->sclk0);
759 		}
760 	}
761 	clk_prepare_enable(tcon->sclk0);
762 
763 	if (tcon->quirks->has_channel_1) {
764 		tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
765 		if (IS_ERR(tcon->sclk1)) {
766 			dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
767 			return PTR_ERR(tcon->sclk1);
768 		}
769 	}
770 
771 	return 0;
772 }
773 
774 static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
775 {
776 	clk_disable_unprepare(tcon->sclk0);
777 	clk_disable_unprepare(tcon->clk);
778 }
779 
780 static int sun4i_tcon_init_irq(struct device *dev,
781 			       struct sun4i_tcon *tcon)
782 {
783 	struct platform_device *pdev = to_platform_device(dev);
784 	int irq, ret;
785 
786 	irq = platform_get_irq(pdev, 0);
787 	if (irq < 0) {
788 		dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
789 		return irq;
790 	}
791 
792 	ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
793 			       dev_name(dev), tcon);
794 	if (ret) {
795 		dev_err(dev, "Couldn't request the IRQ\n");
796 		return ret;
797 	}
798 
799 	return 0;
800 }
801 
802 static struct regmap_config sun4i_tcon_regmap_config = {
803 	.reg_bits	= 32,
804 	.val_bits	= 32,
805 	.reg_stride	= 4,
806 	.max_register	= 0x800,
807 };
808 
809 static int sun4i_tcon_init_regmap(struct device *dev,
810 				  struct sun4i_tcon *tcon)
811 {
812 	struct platform_device *pdev = to_platform_device(dev);
813 	struct resource *res;
814 	void __iomem *regs;
815 
816 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
817 	regs = devm_ioremap_resource(dev, res);
818 	if (IS_ERR(regs))
819 		return PTR_ERR(regs);
820 
821 	tcon->regs = devm_regmap_init_mmio(dev, regs,
822 					   &sun4i_tcon_regmap_config);
823 	if (IS_ERR(tcon->regs)) {
824 		dev_err(dev, "Couldn't create the TCON regmap\n");
825 		return PTR_ERR(tcon->regs);
826 	}
827 
828 	/* Make sure the TCON is disabled and all IRQs are off */
829 	regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
830 	regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
831 	regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
832 
833 	/* Disable IO lines and set them to tristate */
834 	regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
835 	regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
836 
837 	return 0;
838 }
839 
840 /*
841  * On SoCs with the old display pipeline design (Display Engine 1.0),
842  * the TCON is always tied to just one backend. Hence we can traverse
843  * the of_graph upwards to find the backend our tcon is connected to,
844  * and take its ID as our own.
845  *
846  * We can either identify backends from their compatible strings, which
847  * means maintaining a large list of them. Or, since the backend is
848  * registered and binded before the TCON, we can just go through the
849  * list of registered backends and compare the device node.
850  *
851  * As the structures now store engines instead of backends, here this
852  * function in fact searches the corresponding engine, and the ID is
853  * requested via the get_id function of the engine.
854  */
855 static struct sunxi_engine *
856 sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
857 				struct device_node *node,
858 				u32 port_id)
859 {
860 	struct device_node *port, *ep, *remote;
861 	struct sunxi_engine *engine = ERR_PTR(-EINVAL);
862 	u32 reg = 0;
863 
864 	port = of_graph_get_port_by_id(node, port_id);
865 	if (!port)
866 		return ERR_PTR(-EINVAL);
867 
868 	/*
869 	 * This only works if there is only one path from the TCON
870 	 * to any display engine. Otherwise the probe order of the
871 	 * TCONs and display engines is not guaranteed. They may
872 	 * either bind to the wrong one, or worse, bind to the same
873 	 * one if additional checks are not done.
874 	 *
875 	 * Bail out if there are multiple input connections.
876 	 */
877 	if (of_get_available_child_count(port) != 1)
878 		goto out_put_port;
879 
880 	/* Get the first connection without specifying an ID */
881 	ep = of_get_next_available_child(port, NULL);
882 	if (!ep)
883 		goto out_put_port;
884 
885 	remote = of_graph_get_remote_port_parent(ep);
886 	if (!remote)
887 		goto out_put_ep;
888 
889 	/* does this node match any registered engines? */
890 	list_for_each_entry(engine, &drv->engine_list, list)
891 		if (remote == engine->node)
892 			goto out_put_remote;
893 
894 	/*
895 	 * According to device tree binding input ports have even id
896 	 * number and output ports have odd id. Since component with
897 	 * more than one input and one output (TCON TOP) exits, correct
898 	 * remote input id has to be calculated by subtracting 1 from
899 	 * remote output id. If this for some reason can't be done, 0
900 	 * is used as input port id.
901 	 */
902 	of_node_put(port);
903 	port = of_graph_get_remote_port(ep);
904 	if (!of_property_read_u32(port, "reg", &reg) && reg > 0)
905 		reg -= 1;
906 
907 	/* keep looking through upstream ports */
908 	engine = sun4i_tcon_find_engine_traverse(drv, remote, reg);
909 
910 out_put_remote:
911 	of_node_put(remote);
912 out_put_ep:
913 	of_node_put(ep);
914 out_put_port:
915 	of_node_put(port);
916 
917 	return engine;
918 }
919 
920 /*
921  * The device tree binding says that the remote endpoint ID of any
922  * connection between components, up to and including the TCON, of
923  * the display pipeline should be equal to the actual ID of the local
924  * component. Thus we can look at any one of the input connections of
925  * the TCONs, and use that connection's remote endpoint ID as our own.
926  *
927  * Since the user of this function already finds the input port,
928  * the port is passed in directly without further checks.
929  */
930 static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
931 {
932 	struct device_node *ep;
933 	int ret = -EINVAL;
934 
935 	/* try finding an upstream endpoint */
936 	for_each_available_child_of_node(port, ep) {
937 		struct device_node *remote;
938 		u32 reg;
939 
940 		remote = of_graph_get_remote_endpoint(ep);
941 		if (!remote)
942 			continue;
943 
944 		ret = of_property_read_u32(remote, "reg", &reg);
945 		if (ret)
946 			continue;
947 
948 		ret = reg;
949 	}
950 
951 	return ret;
952 }
953 
954 /*
955  * Once we know the TCON's id, we can look through the list of
956  * engines to find a matching one. We assume all engines have
957  * been probed and added to the list.
958  */
959 static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
960 							int id)
961 {
962 	struct sunxi_engine *engine;
963 
964 	list_for_each_entry(engine, &drv->engine_list, list)
965 		if (engine->id == id)
966 			return engine;
967 
968 	return ERR_PTR(-EINVAL);
969 }
970 
971 static bool sun4i_tcon_connected_to_tcon_top(struct device_node *node)
972 {
973 	struct device_node *remote;
974 	bool ret = false;
975 
976 	remote = of_graph_get_remote_node(node, 0, -1);
977 	if (remote) {
978 		ret = !!(IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
979 			 of_match_node(sun8i_tcon_top_of_table, remote));
980 		of_node_put(remote);
981 	}
982 
983 	return ret;
984 }
985 
986 static int sun4i_tcon_get_index(struct sun4i_drv *drv)
987 {
988 	struct list_head *pos;
989 	int size = 0;
990 
991 	/*
992 	 * Because TCON is added to the list at the end of the probe
993 	 * (after this function is called), index of the current TCON
994 	 * will be same as current TCON list size.
995 	 */
996 	list_for_each(pos, &drv->tcon_list)
997 		++size;
998 
999 	return size;
1000 }
1001 
1002 /*
1003  * On SoCs with the old display pipeline design (Display Engine 1.0),
1004  * we assumed the TCON was always tied to just one backend. However
1005  * this proved not to be the case. On the A31, the TCON can select
1006  * either backend as its source. On the A20 (and likely on the A10),
1007  * the backend can choose which TCON to output to.
1008  *
1009  * The device tree binding says that the remote endpoint ID of any
1010  * connection between components, up to and including the TCON, of
1011  * the display pipeline should be equal to the actual ID of the local
1012  * component. Thus we should be able to look at any one of the input
1013  * connections of the TCONs, and use that connection's remote endpoint
1014  * ID as our own.
1015  *
1016  * However  the connections between the backend and TCON were assumed
1017  * to be always singular, and their endpoit IDs were all incorrectly
1018  * set to 0. This means for these old device trees, we cannot just look
1019  * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
1020  * incorrectly identified as TCON0.
1021  *
1022  * This function first checks if the TCON node has 2 input endpoints.
1023  * If so, then the device tree is a corrected version, and it will use
1024  * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
1025  * to fetch the ID and engine directly. If not, then it is likely an
1026  * old device trees, where the endpoint IDs were incorrect, but did not
1027  * have endpoint connections between the backend and TCON across
1028  * different display pipelines. It will fall back to the old method of
1029  * traversing the  of_graph to try and find a matching engine by device
1030  * node.
1031  *
1032  * In the case of single display pipeline device trees, either method
1033  * works.
1034  */
1035 static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
1036 						   struct device_node *node)
1037 {
1038 	struct device_node *port;
1039 	struct sunxi_engine *engine;
1040 
1041 	port = of_graph_get_port_by_id(node, 0);
1042 	if (!port)
1043 		return ERR_PTR(-EINVAL);
1044 
1045 	/*
1046 	 * Is this a corrected device tree with cross pipeline
1047 	 * connections between the backend and TCON?
1048 	 */
1049 	if (of_get_child_count(port) > 1) {
1050 		int id;
1051 
1052 		/*
1053 		 * When pipeline has the same number of TCONs and engines which
1054 		 * are represented by frontends/backends (DE1) or mixers (DE2),
1055 		 * we match them by their respective IDs. However, if pipeline
1056 		 * contains TCON TOP, chances are that there are either more
1057 		 * TCONs than engines (R40) or TCONs with non-consecutive ids.
1058 		 * (H6). In that case it's easier just use TCON index in list
1059 		 * as an id. That means that on R40, any 2 TCONs can be enabled
1060 		 * in DT out of 4 (there are 2 mixers). Due to the design of
1061 		 * TCON TOP, remaining 2 TCONs can't be connected to anything
1062 		 * anyway.
1063 		 */
1064 		if (sun4i_tcon_connected_to_tcon_top(node))
1065 			id = sun4i_tcon_get_index(drv);
1066 		else
1067 			id = sun4i_tcon_of_get_id_from_port(port);
1068 
1069 		/* Get our engine by matching our ID */
1070 		engine = sun4i_tcon_get_engine_by_id(drv, id);
1071 
1072 		of_node_put(port);
1073 		return engine;
1074 	}
1075 
1076 	/* Fallback to old method by traversing input endpoints */
1077 	of_node_put(port);
1078 	return sun4i_tcon_find_engine_traverse(drv, node, 0);
1079 }
1080 
1081 static int sun4i_tcon_bind(struct device *dev, struct device *master,
1082 			   void *data)
1083 {
1084 	struct drm_device *drm = data;
1085 	struct sun4i_drv *drv = drm->dev_private;
1086 	struct sunxi_engine *engine;
1087 	struct device_node *remote;
1088 	struct sun4i_tcon *tcon;
1089 	struct reset_control *edp_rstc;
1090 	bool has_lvds_rst, has_lvds_alt, can_lvds;
1091 	int ret;
1092 
1093 	engine = sun4i_tcon_find_engine(drv, dev->of_node);
1094 	if (IS_ERR(engine)) {
1095 		dev_err(dev, "Couldn't find matching engine\n");
1096 		return -EPROBE_DEFER;
1097 	}
1098 
1099 	tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
1100 	if (!tcon)
1101 		return -ENOMEM;
1102 	dev_set_drvdata(dev, tcon);
1103 	tcon->drm = drm;
1104 	tcon->dev = dev;
1105 	tcon->id = engine->id;
1106 	tcon->quirks = of_device_get_match_data(dev);
1107 
1108 	tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
1109 	if (IS_ERR(tcon->lcd_rst)) {
1110 		dev_err(dev, "Couldn't get our reset line\n");
1111 		return PTR_ERR(tcon->lcd_rst);
1112 	}
1113 
1114 	if (tcon->quirks->needs_edp_reset) {
1115 		edp_rstc = devm_reset_control_get_shared(dev, "edp");
1116 		if (IS_ERR(edp_rstc)) {
1117 			dev_err(dev, "Couldn't get edp reset line\n");
1118 			return PTR_ERR(edp_rstc);
1119 		}
1120 
1121 		ret = reset_control_deassert(edp_rstc);
1122 		if (ret) {
1123 			dev_err(dev, "Couldn't deassert edp reset line\n");
1124 			return ret;
1125 		}
1126 	}
1127 
1128 	/* Make sure our TCON is reset */
1129 	ret = reset_control_reset(tcon->lcd_rst);
1130 	if (ret) {
1131 		dev_err(dev, "Couldn't deassert our reset line\n");
1132 		return ret;
1133 	}
1134 
1135 	if (tcon->quirks->supports_lvds) {
1136 		/*
1137 		 * This can only be made optional since we've had DT
1138 		 * nodes without the LVDS reset properties.
1139 		 *
1140 		 * If the property is missing, just disable LVDS, and
1141 		 * print a warning.
1142 		 */
1143 		tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
1144 		if (IS_ERR(tcon->lvds_rst)) {
1145 			dev_err(dev, "Couldn't get our reset line\n");
1146 			return PTR_ERR(tcon->lvds_rst);
1147 		} else if (tcon->lvds_rst) {
1148 			has_lvds_rst = true;
1149 			reset_control_reset(tcon->lvds_rst);
1150 		} else {
1151 			has_lvds_rst = false;
1152 		}
1153 
1154 		/*
1155 		 * This can only be made optional since we've had DT
1156 		 * nodes without the LVDS reset properties.
1157 		 *
1158 		 * If the property is missing, just disable LVDS, and
1159 		 * print a warning.
1160 		 */
1161 		if (tcon->quirks->has_lvds_alt) {
1162 			tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
1163 			if (IS_ERR(tcon->lvds_pll)) {
1164 				if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
1165 					has_lvds_alt = false;
1166 				} else {
1167 					dev_err(dev, "Couldn't get the LVDS PLL\n");
1168 					return PTR_ERR(tcon->lvds_pll);
1169 				}
1170 			} else {
1171 				has_lvds_alt = true;
1172 			}
1173 		}
1174 
1175 		if (!has_lvds_rst ||
1176 		    (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
1177 			dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
1178 			dev_warn(dev, "LVDS output disabled\n");
1179 			can_lvds = false;
1180 		} else {
1181 			can_lvds = true;
1182 		}
1183 	} else {
1184 		can_lvds = false;
1185 	}
1186 
1187 	ret = sun4i_tcon_init_clocks(dev, tcon);
1188 	if (ret) {
1189 		dev_err(dev, "Couldn't init our TCON clocks\n");
1190 		goto err_assert_reset;
1191 	}
1192 
1193 	ret = sun4i_tcon_init_regmap(dev, tcon);
1194 	if (ret) {
1195 		dev_err(dev, "Couldn't init our TCON regmap\n");
1196 		goto err_free_clocks;
1197 	}
1198 
1199 	if (tcon->quirks->has_channel_0) {
1200 		ret = sun4i_dclk_create(dev, tcon);
1201 		if (ret) {
1202 			dev_err(dev, "Couldn't create our TCON dot clock\n");
1203 			goto err_free_clocks;
1204 		}
1205 	}
1206 
1207 	ret = sun4i_tcon_init_irq(dev, tcon);
1208 	if (ret) {
1209 		dev_err(dev, "Couldn't init our TCON interrupts\n");
1210 		goto err_free_dotclock;
1211 	}
1212 
1213 	tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
1214 	if (IS_ERR(tcon->crtc)) {
1215 		dev_err(dev, "Couldn't create our CRTC\n");
1216 		ret = PTR_ERR(tcon->crtc);
1217 		goto err_free_dotclock;
1218 	}
1219 
1220 	if (tcon->quirks->has_channel_0) {
1221 		/*
1222 		 * If we have an LVDS panel connected to the TCON, we should
1223 		 * just probe the LVDS connector. Otherwise, just probe RGB as
1224 		 * we used to.
1225 		 */
1226 		remote = of_graph_get_remote_node(dev->of_node, 1, 0);
1227 		if (of_device_is_compatible(remote, "panel-lvds"))
1228 			if (can_lvds)
1229 				ret = sun4i_lvds_init(drm, tcon);
1230 			else
1231 				ret = -EINVAL;
1232 		else
1233 			ret = sun4i_rgb_init(drm, tcon);
1234 		of_node_put(remote);
1235 
1236 		if (ret < 0)
1237 			goto err_free_dotclock;
1238 	}
1239 
1240 	if (tcon->quirks->needs_de_be_mux) {
1241 		/*
1242 		 * We assume there is no dynamic muxing of backends
1243 		 * and TCONs, so we select the backend with same ID.
1244 		 *
1245 		 * While dynamic selection might be interesting, since
1246 		 * the CRTC is tied to the TCON, while the layers are
1247 		 * tied to the backends, this means, we will need to
1248 		 * switch between groups of layers. There might not be
1249 		 * a way to represent this constraint in DRM.
1250 		 */
1251 		regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
1252 				   SUN4I_TCON0_CTL_SRC_SEL_MASK,
1253 				   tcon->id);
1254 		regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
1255 				   SUN4I_TCON1_CTL_SRC_SEL_MASK,
1256 				   tcon->id);
1257 	}
1258 
1259 	list_add_tail(&tcon->list, &drv->tcon_list);
1260 
1261 	return 0;
1262 
1263 err_free_dotclock:
1264 	if (tcon->quirks->has_channel_0)
1265 		sun4i_dclk_free(tcon);
1266 err_free_clocks:
1267 	sun4i_tcon_free_clocks(tcon);
1268 err_assert_reset:
1269 	reset_control_assert(tcon->lcd_rst);
1270 	return ret;
1271 }
1272 
1273 static void sun4i_tcon_unbind(struct device *dev, struct device *master,
1274 			      void *data)
1275 {
1276 	struct sun4i_tcon *tcon = dev_get_drvdata(dev);
1277 
1278 	list_del(&tcon->list);
1279 	if (tcon->quirks->has_channel_0)
1280 		sun4i_dclk_free(tcon);
1281 	sun4i_tcon_free_clocks(tcon);
1282 }
1283 
1284 static const struct component_ops sun4i_tcon_ops = {
1285 	.bind	= sun4i_tcon_bind,
1286 	.unbind	= sun4i_tcon_unbind,
1287 };
1288 
1289 static int sun4i_tcon_probe(struct platform_device *pdev)
1290 {
1291 	struct device_node *node = pdev->dev.of_node;
1292 	const struct sun4i_tcon_quirks *quirks;
1293 	struct drm_bridge *bridge;
1294 	struct drm_panel *panel;
1295 	int ret;
1296 
1297 	quirks = of_device_get_match_data(&pdev->dev);
1298 
1299 	/* panels and bridges are present only on TCONs with channel 0 */
1300 	if (quirks->has_channel_0) {
1301 		ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1302 		if (ret == -EPROBE_DEFER)
1303 			return ret;
1304 	}
1305 
1306 	return component_add(&pdev->dev, &sun4i_tcon_ops);
1307 }
1308 
1309 static int sun4i_tcon_remove(struct platform_device *pdev)
1310 {
1311 	component_del(&pdev->dev, &sun4i_tcon_ops);
1312 
1313 	return 0;
1314 }
1315 
1316 /* platform specific TCON muxing callbacks */
1317 static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
1318 				  const struct drm_encoder *encoder)
1319 {
1320 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
1321 	u32 shift;
1322 
1323 	if (!tcon0)
1324 		return -EINVAL;
1325 
1326 	switch (encoder->encoder_type) {
1327 	case DRM_MODE_ENCODER_TMDS:
1328 		/* HDMI */
1329 		shift = 8;
1330 		break;
1331 	default:
1332 		return -EINVAL;
1333 	}
1334 
1335 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
1336 			   0x3 << shift, tcon->id << shift);
1337 
1338 	return 0;
1339 }
1340 
1341 static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
1342 				  const struct drm_encoder *encoder)
1343 {
1344 	u32 val;
1345 
1346 	if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1347 		val = 1;
1348 	else
1349 		val = 0;
1350 
1351 	/*
1352 	 * FIXME: Undocumented bits
1353 	 */
1354 	return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1355 }
1356 
1357 static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
1358 			      const struct drm_encoder *encoder)
1359 {
1360 	struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
1361 	u32 shift;
1362 
1363 	if (!tcon0)
1364 		return -EINVAL;
1365 
1366 	switch (encoder->encoder_type) {
1367 	case DRM_MODE_ENCODER_TMDS:
1368 		/* HDMI */
1369 		shift = 8;
1370 		break;
1371 	default:
1372 		/* TODO A31 has MIPI DSI but A31s does not */
1373 		return -EINVAL;
1374 	}
1375 
1376 	regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
1377 			   0x3 << shift, tcon->id << shift);
1378 
1379 	return 0;
1380 }
1381 
1382 static int sun8i_r40_tcon_tv_set_mux(struct sun4i_tcon *tcon,
1383 				     const struct drm_encoder *encoder)
1384 {
1385 	struct device_node *port, *remote;
1386 	struct platform_device *pdev;
1387 	int id, ret;
1388 
1389 	/* find TCON TOP platform device and TCON id */
1390 
1391 	port = of_graph_get_port_by_id(tcon->dev->of_node, 0);
1392 	if (!port)
1393 		return -EINVAL;
1394 
1395 	id = sun4i_tcon_of_get_id_from_port(port);
1396 	of_node_put(port);
1397 
1398 	remote = of_graph_get_remote_node(tcon->dev->of_node, 0, -1);
1399 	if (!remote)
1400 		return -EINVAL;
1401 
1402 	pdev = of_find_device_by_node(remote);
1403 	of_node_put(remote);
1404 	if (!pdev)
1405 		return -EINVAL;
1406 
1407 	if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
1408 	    encoder->encoder_type == DRM_MODE_ENCODER_TMDS) {
1409 		ret = sun8i_tcon_top_set_hdmi_src(&pdev->dev, id);
1410 		if (ret)
1411 			return ret;
1412 	}
1413 
1414 	if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP)) {
1415 		ret = sun8i_tcon_top_de_config(&pdev->dev, tcon->id, id);
1416 		if (ret)
1417 			return ret;
1418 	}
1419 
1420 	return 0;
1421 }
1422 
1423 static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
1424 	.has_channel_0		= true,
1425 	.has_channel_1		= true,
1426 	.set_mux		= sun4i_a10_tcon_set_mux,
1427 };
1428 
1429 static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
1430 	.has_channel_0		= true,
1431 	.has_channel_1		= true,
1432 	.set_mux		= sun5i_a13_tcon_set_mux,
1433 };
1434 
1435 static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
1436 	.has_channel_0		= true,
1437 	.has_channel_1		= true,
1438 	.has_lvds_alt		= true,
1439 	.needs_de_be_mux	= true,
1440 	.set_mux		= sun6i_tcon_set_mux,
1441 };
1442 
1443 static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
1444 	.has_channel_0		= true,
1445 	.has_channel_1		= true,
1446 	.needs_de_be_mux	= true,
1447 };
1448 
1449 static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
1450 	.has_channel_0		= true,
1451 	.has_channel_1		= true,
1452 	/* Same display pipeline structure as A10 */
1453 	.set_mux		= sun4i_a10_tcon_set_mux,
1454 };
1455 
1456 static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
1457 	.has_channel_0		= true,
1458 	.has_lvds_alt		= true,
1459 };
1460 
1461 static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
1462 	.supports_lvds		= true,
1463 	.has_channel_0		= true,
1464 };
1465 
1466 static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
1467 	.has_channel_1		= true,
1468 };
1469 
1470 static const struct sun4i_tcon_quirks sun8i_r40_tv_quirks = {
1471 	.has_channel_1		= true,
1472 	.set_mux		= sun8i_r40_tcon_tv_set_mux,
1473 };
1474 
1475 static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
1476 	.has_channel_0		= true,
1477 };
1478 
1479 static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
1480 	.has_channel_0	= true,
1481 	.needs_edp_reset = true,
1482 };
1483 
1484 static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
1485 	.has_channel_1	= true,
1486 	.needs_edp_reset = true,
1487 };
1488 
1489 /* sun4i_drv uses this list to check if a device node is a TCON */
1490 const struct of_device_id sun4i_tcon_of_table[] = {
1491 	{ .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
1492 	{ .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
1493 	{ .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
1494 	{ .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
1495 	{ .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
1496 	{ .compatible = "allwinner,sun8i-a23-tcon", .data = &sun8i_a33_quirks },
1497 	{ .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
1498 	{ .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
1499 	{ .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
1500 	{ .compatible = "allwinner,sun8i-r40-tcon-tv", .data = &sun8i_r40_tv_quirks },
1501 	{ .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
1502 	{ .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
1503 	{ .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
1504 	{ }
1505 };
1506 MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
1507 EXPORT_SYMBOL(sun4i_tcon_of_table);
1508 
1509 static struct platform_driver sun4i_tcon_platform_driver = {
1510 	.probe		= sun4i_tcon_probe,
1511 	.remove		= sun4i_tcon_remove,
1512 	.driver		= {
1513 		.name		= "sun4i-tcon",
1514 		.of_match_table	= sun4i_tcon_of_table,
1515 	},
1516 };
1517 module_platform_driver(sun4i_tcon_platform_driver);
1518 
1519 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1520 MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
1521 MODULE_LICENSE("GPL");
1522