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