blob: 1a114e380f13ee8e13d970d18d57c8f4f930e093 [file] [log] [blame]
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001/*
2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
4 *
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
13#include <drm/drmP.h>
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_crtc.h>
16#include <drm/drm_crtc_helper.h>
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +080017#include <drm/drm_encoder.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010018#include <drm/drm_modes.h>
Rob Herringebc94462017-03-29 13:55:46 -050019#include <drm/drm_of.h>
Giulio Benetti2c17a432018-03-13 18:54:37 +010020#include <drm/drm_panel.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010021
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +080022#include <uapi/drm/drm_mode.h>
23
Maxime Ripard9026e0d2015-10-29 09:36:23 +010024#include <linux/component.h>
25#include <linux/ioport.h>
26#include <linux/of_address.h>
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080027#include <linux/of_device.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010028#include <linux/of_irq.h>
29#include <linux/regmap.h>
30#include <linux/reset.h>
31
32#include "sun4i_crtc.h"
33#include "sun4i_dotclock.h"
34#include "sun4i_drv.h"
Maxime Riparda0c12142017-12-21 12:02:33 +010035#include "sun4i_lvds.h"
Maxime Ripard29e57fa2015-10-29 09:37:32 +010036#include "sun4i_rgb.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010037#include "sun4i_tcon.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080038#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010039
Maxime Riparda0c12142017-12-21 12:02:33 +010040static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder)
41{
42 struct drm_connector *connector;
43 struct drm_connector_list_iter iter;
44
45 drm_connector_list_iter_begin(encoder->dev, &iter);
46 drm_for_each_connector_iter(connector, &iter)
47 if (connector->encoder == encoder) {
48 drm_connector_list_iter_end(&iter);
49 return connector;
50 }
51 drm_connector_list_iter_end(&iter);
52
53 return NULL;
54}
55
56static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder)
57{
58 struct drm_connector *connector;
59 struct drm_display_info *info;
60
61 connector = sun4i_tcon_get_connector(encoder);
62 if (!connector)
63 return -EINVAL;
64
65 info = &connector->display_info;
66 if (info->num_bus_formats != 1)
67 return -EINVAL;
68
69 switch (info->bus_formats[0]) {
70 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
71 return 18;
72
73 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
74 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
75 return 24;
76 }
77
78 return -EINVAL;
79}
80
Maxime Ripard45e88f92017-10-17 11:06:12 +020081static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
82 bool enabled)
Maxime Ripard9026e0d2015-10-29 09:36:23 +010083{
Maxime Ripard45e88f92017-10-17 11:06:12 +020084 struct clk *clk;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010085
Maxime Ripard45e88f92017-10-17 11:06:12 +020086 switch (channel) {
87 case 0:
Jernej Skrabec34d698f2018-02-14 21:09:01 +010088 WARN_ON(!tcon->quirks->has_channel_0);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010089 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
90 SUN4I_TCON0_CTL_TCON_ENABLE,
Maxime Ripard45e88f92017-10-17 11:06:12 +020091 enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
92 clk = tcon->dclk;
93 break;
94 case 1:
95 WARN_ON(!tcon->quirks->has_channel_1);
96 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
97 SUN4I_TCON1_CTL_TCON_ENABLE,
98 enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
99 clk = tcon->sclk1;
100 break;
101 default:
102 DRM_WARN("Unknown channel... doing nothing\n");
Maxime Ripard8e924042016-01-07 12:32:07 +0100103 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100104 }
Maxime Ripard8e924042016-01-07 12:32:07 +0100105
Maxime Ripard45e88f92017-10-17 11:06:12 +0200106 if (enabled)
107 clk_prepare_enable(clk);
108 else
109 clk_disable_unprepare(clk);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100110}
Maxime Ripard45e88f92017-10-17 11:06:12 +0200111
Maxime Riparda0c12142017-12-21 12:02:33 +0100112static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon,
113 const struct drm_encoder *encoder,
114 bool enabled)
115{
116 if (enabled) {
117 u8 val;
118
119 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
120 SUN4I_TCON0_LVDS_IF_EN,
121 SUN4I_TCON0_LVDS_IF_EN);
122
123 /*
124 * As their name suggest, these values only apply to the A31
125 * and later SoCs. We'll have to rework this when merging
126 * support for the older SoCs.
127 */
128 regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
129 SUN6I_TCON0_LVDS_ANA0_C(2) |
130 SUN6I_TCON0_LVDS_ANA0_V(3) |
131 SUN6I_TCON0_LVDS_ANA0_PD(2) |
132 SUN6I_TCON0_LVDS_ANA0_EN_LDO);
133 udelay(2);
134
135 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
136 SUN6I_TCON0_LVDS_ANA0_EN_MB,
137 SUN6I_TCON0_LVDS_ANA0_EN_MB);
138 udelay(2);
139
140 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
141 SUN6I_TCON0_LVDS_ANA0_EN_DRVC,
142 SUN6I_TCON0_LVDS_ANA0_EN_DRVC);
143
144 if (sun4i_tcon_get_pixel_depth(encoder) == 18)
145 val = 7;
146 else
147 val = 0xf;
148
149 regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
150 SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf),
151 SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val));
152 } else {
153 regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
154 SUN4I_TCON0_LVDS_IF_EN, 0);
155 }
156}
157
Maxime Ripard45e88f92017-10-17 11:06:12 +0200158void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
159 const struct drm_encoder *encoder,
160 bool enabled)
161{
Maxime Riparda0c12142017-12-21 12:02:33 +0100162 bool is_lvds = false;
Maxime Ripard45e88f92017-10-17 11:06:12 +0200163 int channel;
164
165 switch (encoder->encoder_type) {
Maxime Riparda0c12142017-12-21 12:02:33 +0100166 case DRM_MODE_ENCODER_LVDS:
167 is_lvds = true;
168 /* Fallthrough */
Maxime Ripard45e88f92017-10-17 11:06:12 +0200169 case DRM_MODE_ENCODER_NONE:
170 channel = 0;
171 break;
172 case DRM_MODE_ENCODER_TMDS:
173 case DRM_MODE_ENCODER_TVDAC:
174 channel = 1;
175 break;
176 default:
177 DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
178 return;
179 }
180
Maxime Riparda0c12142017-12-21 12:02:33 +0100181 if (is_lvds && !enabled)
182 sun4i_tcon_lvds_set_status(tcon, encoder, false);
183
Maxime Ripard45e88f92017-10-17 11:06:12 +0200184 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
185 SUN4I_TCON_GCTL_TCON_ENABLE,
186 enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
187
Maxime Riparda0c12142017-12-21 12:02:33 +0100188 if (is_lvds && enabled)
189 sun4i_tcon_lvds_set_status(tcon, encoder, true);
190
Maxime Ripard45e88f92017-10-17 11:06:12 +0200191 sun4i_tcon_channel_set_status(tcon, channel, enabled);
192}
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100193
194void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
195{
196 u32 mask, val = 0;
197
198 DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
199
200 mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
201 SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
202
203 if (enable)
204 val = mask;
205
206 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
207}
208EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
209
Chen-Yu Tsai67e32642017-10-10 11:19:59 +0800210/*
211 * This function is a helper for TCON output muxing. The TCON output
212 * muxing control register in earlier SoCs (without the TCON TOP block)
213 * are located in TCON0. This helper returns a pointer to TCON0's
214 * sun4i_tcon structure, or NULL if not found.
215 */
216static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
217{
218 struct sun4i_drv *drv = drm->dev_private;
219 struct sun4i_tcon *tcon;
220
221 list_for_each_entry(tcon, &drv->tcon_list, list)
222 if (tcon->id == 0)
223 return tcon;
224
225 dev_warn(drm->dev,
226 "TCON0 not found, display output muxing may not work\n");
227
228 return NULL;
229}
230
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200231void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
Maxime Ripardabcb8762017-10-17 11:06:10 +0200232 const struct drm_encoder *encoder)
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200233{
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800234 int ret = -ENOTSUPP;
Maxime Ripardb7cb9b92017-05-27 18:09:28 +0200235
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800236 if (tcon->quirks->set_mux)
237 ret = tcon->quirks->set_mux(tcon, encoder);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200238
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800239 DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
240 encoder->name, encoder->crtc->name, ret);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200241}
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200242
Maxime Ripard961c6452017-10-17 11:06:11 +0200243static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100244 int channel)
245{
246 int delay = mode->vtotal - mode->vdisplay;
247
248 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
249 delay /= 2;
250
251 if (channel == 1)
252 delay -= 2;
253
254 delay = min(delay, 30);
255
256 DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
257
258 return delay;
259}
260
Maxime Ripardba19c532017-10-17 11:06:14 +0200261static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon,
262 const struct drm_display_mode *mode)
263{
264 /* Configure the dot clock */
265 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
266
267 /* Set the resolution */
268 regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
269 SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
270 SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
271}
272
Maxime Riparda0c12142017-12-21 12:02:33 +0100273static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
274 const struct drm_encoder *encoder,
275 const struct drm_display_mode *mode)
276{
277 unsigned int bp;
278 u8 clk_delay;
279 u32 reg, val = 0;
280
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100281 WARN_ON(!tcon->quirks->has_channel_0);
282
Maxime Riparda0c12142017-12-21 12:02:33 +0100283 tcon->dclk_min_div = 7;
284 tcon->dclk_max_div = 7;
285 sun4i_tcon0_mode_set_common(tcon, mode);
286
287 /* Adjust clock delay */
288 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
289 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
290 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
291 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
292
293 /*
294 * This is called a backporch in the register documentation,
295 * but it really is the back porch + hsync
296 */
297 bp = mode->crtc_htotal - mode->crtc_hsync_start;
298 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
299 mode->crtc_htotal, bp);
300
301 /* Set horizontal display timings */
302 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
303 SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
304 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
305
306 /*
307 * This is called a backporch in the register documentation,
308 * but it really is the back porch + hsync
309 */
310 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
311 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
312 mode->crtc_vtotal, bp);
313
314 /* Set vertical display timings */
315 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
316 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
317 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
318
319 reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
320 SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
321 SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
322 if (sun4i_tcon_get_pixel_depth(encoder) == 24)
323 reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
324 else
325 reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
326
327 regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
328
329 /* Setup the polarity of the various signals */
330 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
331 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
332
333 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
334 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
335
336 regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
337
338 /* Map output pins to channel 0 */
339 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
340 SUN4I_TCON_GCTL_IOMAP_MASK,
341 SUN4I_TCON_GCTL_IOMAP_TCON0);
342}
343
Maxime Ripardba19c532017-10-17 11:06:14 +0200344static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
345 const struct drm_display_mode *mode)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100346{
Giulio Benetti2c17a432018-03-13 18:54:37 +0100347 struct drm_panel *panel = tcon->panel;
348 struct drm_connector *connector = panel->connector;
349 struct drm_display_info display_info = connector->display_info;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100350 unsigned int bp, hsync, vsync;
351 u8 clk_delay;
352 u32 val = 0;
353
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100354 WARN_ON(!tcon->quirks->has_channel_0);
355
Maxime Ripardec08d592017-12-21 12:02:32 +0100356 tcon->dclk_min_div = 6;
357 tcon->dclk_max_div = 127;
Maxime Ripardba19c532017-10-17 11:06:14 +0200358 sun4i_tcon0_mode_set_common(tcon, mode);
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800359
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100360 /* Adjust clock delay */
361 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
362 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
363 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
364 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
365
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100366 /*
367 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800368 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100369 */
370 bp = mode->crtc_htotal - mode->crtc_hsync_start;
371 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
372 mode->crtc_htotal, bp);
373
374 /* Set horizontal display timings */
375 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
376 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
377 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
378
379 /*
380 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800381 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100382 */
383 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
384 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
385 mode->crtc_vtotal, bp);
386
387 /* Set vertical display timings */
388 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
Maxime Riparda88cbbd2017-05-27 18:09:30 +0200389 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100390 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
391
392 /* Set Hsync and Vsync length */
393 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
394 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
395 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
396 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
397 SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
398 SUN4I_TCON0_BASIC3_H_SYNC(hsync));
399
400 /* Setup the polarity of the various signals */
Giulio Benettifa4127c2018-02-15 18:54:48 +0100401 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100402 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
403
Giulio Benettifa4127c2018-02-15 18:54:48 +0100404 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100405 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
406
Giulio Benetti2c17a432018-03-13 18:54:37 +0100407 /*
408 * On A20 and similar SoCs, the only way to achieve Positive Edge
409 * (Rising Edge), is setting dclk clock phase to 2/3(240°).
410 * By default TCON works in Negative Edge(Falling Edge),
411 * this is why phase is set to 0 in that case.
412 * Unfortunately there's no way to logically invert dclk through
413 * IO_POL register.
414 * The only acceptable way to work, triple checked with scope,
415 * is using clock phase set to 0° for Negative Edge and set to 240°
416 * for Positive Edge.
417 * On A33 and similar SoCs there would be a 90° phase option,
418 * but it divides also dclk by 2.
419 * Following code is a way to avoid quirks all around TCON
420 * and DOTCLOCK drivers.
421 */
422 if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
423 clk_set_phase(tcon->dclk, 240);
424
425 if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
426 clk_set_phase(tcon->dclk, 0);
427
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100428 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
429 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
430 val);
431
432 /* Map output pins to channel 0 */
433 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
434 SUN4I_TCON_GCTL_IOMAP_MASK,
435 SUN4I_TCON_GCTL_IOMAP_TCON0);
436
437 /* Enable the output on the pins */
438 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
439}
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100440
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200441static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
442 const struct drm_display_mode *mode)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100443{
Maxime Ripardb8317a32017-05-27 18:09:31 +0200444 unsigned int bp, hsync, vsync, vtotal;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100445 u8 clk_delay;
446 u32 val;
447
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800448 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +0100449
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800450 /* Configure the dot clock */
451 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
452
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100453 /* Adjust clock delay */
454 clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
455 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
456 SUN4I_TCON1_CTL_CLK_DELAY_MASK,
457 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
458
459 /* Set interlaced mode */
460 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
461 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
462 else
463 val = 0;
464 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
465 SUN4I_TCON1_CTL_INTERLACE_ENABLE,
466 val);
467
468 /* Set the input resolution */
469 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
470 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
471 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
472
473 /* Set the upscaling resolution */
474 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
475 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
476 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
477
478 /* Set the output resolution */
479 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
480 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
481 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
482
483 /* Set horizontal display timings */
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200484 bp = mode->crtc_htotal - mode->crtc_hsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100485 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
486 mode->htotal, bp);
487 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
488 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
489 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
490
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200491 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100492 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
Maxime Ripardb8317a32017-05-27 18:09:31 +0200493 mode->crtc_vtotal, bp);
494
495 /*
496 * The vertical resolution needs to be doubled in all
497 * cases. We could use crtc_vtotal and always multiply by two,
498 * but that leads to a rounding error in interlace when vtotal
499 * is odd.
500 *
501 * This happens with TV's PAL for example, where vtotal will
502 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
503 * 624, which apparently confuses the hardware.
504 *
505 * To work around this, we will always use vtotal, and
506 * multiply by two only if we're not in interlace.
507 */
508 vtotal = mode->vtotal;
509 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
510 vtotal = vtotal * 2;
511
512 /* Set vertical display timings */
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100513 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
Maxime Ripardb8317a32017-05-27 18:09:31 +0200514 SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100515 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
516
517 /* Set Hsync and Vsync length */
518 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
519 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
520 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
521 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
522 SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
523 SUN4I_TCON1_BASIC5_H_SYNC(hsync));
524
525 /* Map output pins to channel 1 */
526 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
527 SUN4I_TCON_GCTL_IOMAP_MASK,
528 SUN4I_TCON_GCTL_IOMAP_TCON1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100529}
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200530
531void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
532 const struct drm_encoder *encoder,
533 const struct drm_display_mode *mode)
534{
535 switch (encoder->encoder_type) {
Maxime Riparda0c12142017-12-21 12:02:33 +0100536 case DRM_MODE_ENCODER_LVDS:
537 sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
538 break;
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200539 case DRM_MODE_ENCODER_NONE:
Maxime Ripardba19c532017-10-17 11:06:14 +0200540 sun4i_tcon0_mode_set_rgb(tcon, mode);
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200541 sun4i_tcon_set_mux(tcon, 0, encoder);
542 break;
543 case DRM_MODE_ENCODER_TVDAC:
544 case DRM_MODE_ENCODER_TMDS:
545 sun4i_tcon1_mode_set(tcon, mode);
546 sun4i_tcon_set_mux(tcon, 1, encoder);
547 break;
548 default:
549 DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
550 }
551}
552EXPORT_SYMBOL(sun4i_tcon_mode_set);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100553
554static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
555 struct sun4i_crtc *scrtc)
556{
557 unsigned long flags;
558
559 spin_lock_irqsave(&dev->event_lock, flags);
560 if (scrtc->event) {
561 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
562 drm_crtc_vblank_put(&scrtc->crtc);
563 scrtc->event = NULL;
564 }
565 spin_unlock_irqrestore(&dev->event_lock, flags);
566}
567
568static irqreturn_t sun4i_tcon_handler(int irq, void *private)
569{
570 struct sun4i_tcon *tcon = private;
571 struct drm_device *drm = tcon->drm;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800572 struct sun4i_crtc *scrtc = tcon->crtc;
Maxime Ripard3004f752018-01-22 10:25:20 +0100573 struct sunxi_engine *engine = scrtc->engine;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100574 unsigned int status;
575
576 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
577
578 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
579 SUN4I_TCON_GINT0_VBLANK_INT(1))))
580 return IRQ_NONE;
581
582 drm_crtc_handle_vblank(&scrtc->crtc);
583 sun4i_tcon_finish_page_flip(drm, scrtc);
584
585 /* Acknowledge the interrupt */
586 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
587 SUN4I_TCON_GINT0_VBLANK_INT(0) |
588 SUN4I_TCON_GINT0_VBLANK_INT(1),
589 0);
590
Maxime Ripard3004f752018-01-22 10:25:20 +0100591 if (engine->ops->vblank_quirk)
592 engine->ops->vblank_quirk(engine);
593
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100594 return IRQ_HANDLED;
595}
596
597static int sun4i_tcon_init_clocks(struct device *dev,
598 struct sun4i_tcon *tcon)
599{
600 tcon->clk = devm_clk_get(dev, "ahb");
601 if (IS_ERR(tcon->clk)) {
602 dev_err(dev, "Couldn't get the TCON bus clock\n");
603 return PTR_ERR(tcon->clk);
604 }
605 clk_prepare_enable(tcon->clk);
606
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100607 if (tcon->quirks->has_channel_0) {
608 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
609 if (IS_ERR(tcon->sclk0)) {
610 dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
611 return PTR_ERR(tcon->sclk0);
612 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100613 }
614
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800615 if (tcon->quirks->has_channel_1) {
Maxime Ripard8e924042016-01-07 12:32:07 +0100616 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
617 if (IS_ERR(tcon->sclk1)) {
618 dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
619 return PTR_ERR(tcon->sclk1);
620 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100621 }
622
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800623 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100624}
625
626static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
627{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100628 clk_disable_unprepare(tcon->clk);
629}
630
631static int sun4i_tcon_init_irq(struct device *dev,
632 struct sun4i_tcon *tcon)
633{
634 struct platform_device *pdev = to_platform_device(dev);
635 int irq, ret;
636
637 irq = platform_get_irq(pdev, 0);
638 if (irq < 0) {
639 dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
640 return irq;
641 }
642
643 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
644 dev_name(dev), tcon);
645 if (ret) {
646 dev_err(dev, "Couldn't request the IRQ\n");
647 return ret;
648 }
649
650 return 0;
651}
652
653static struct regmap_config sun4i_tcon_regmap_config = {
654 .reg_bits = 32,
655 .val_bits = 32,
656 .reg_stride = 4,
657 .max_register = 0x800,
658};
659
660static int sun4i_tcon_init_regmap(struct device *dev,
661 struct sun4i_tcon *tcon)
662{
663 struct platform_device *pdev = to_platform_device(dev);
664 struct resource *res;
665 void __iomem *regs;
666
667 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
668 regs = devm_ioremap_resource(dev, res);
Wei Yongjunaf346f52016-08-26 14:25:25 +0000669 if (IS_ERR(regs))
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100670 return PTR_ERR(regs);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100671
672 tcon->regs = devm_regmap_init_mmio(dev, regs,
673 &sun4i_tcon_regmap_config);
674 if (IS_ERR(tcon->regs)) {
675 dev_err(dev, "Couldn't create the TCON regmap\n");
676 return PTR_ERR(tcon->regs);
677 }
678
679 /* Make sure the TCON is disabled and all IRQs are off */
680 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
681 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
682 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
683
684 /* Disable IO lines and set them to tristate */
685 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
686 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
687
688 return 0;
689}
690
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800691/*
692 * On SoCs with the old display pipeline design (Display Engine 1.0),
693 * the TCON is always tied to just one backend. Hence we can traverse
694 * the of_graph upwards to find the backend our tcon is connected to,
695 * and take its ID as our own.
696 *
697 * We can either identify backends from their compatible strings, which
698 * means maintaining a large list of them. Or, since the backend is
699 * registered and binded before the TCON, we can just go through the
700 * list of registered backends and compare the device node.
Icenowy Zheng87969332017-05-17 22:47:17 +0800701 *
702 * As the structures now store engines instead of backends, here this
703 * function in fact searches the corresponding engine, and the ID is
704 * requested via the get_id function of the engine.
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800705 */
Chen-Yu Tsaie8d5bbf2017-09-08 15:50:12 +0800706static struct sunxi_engine *
707sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
708 struct device_node *node)
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800709{
710 struct device_node *port, *ep, *remote;
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800711 struct sunxi_engine *engine = ERR_PTR(-EINVAL);
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800712
713 port = of_graph_get_port_by_id(node, 0);
714 if (!port)
715 return ERR_PTR(-EINVAL);
716
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800717 /*
718 * This only works if there is only one path from the TCON
719 * to any display engine. Otherwise the probe order of the
720 * TCONs and display engines is not guaranteed. They may
721 * either bind to the wrong one, or worse, bind to the same
722 * one if additional checks are not done.
723 *
724 * Bail out if there are multiple input connections.
725 */
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800726 if (of_get_available_child_count(port) != 1)
727 goto out_put_port;
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800728
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800729 /* Get the first connection without specifying an ID */
730 ep = of_get_next_available_child(port, NULL);
731 if (!ep)
732 goto out_put_port;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800733
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800734 remote = of_graph_get_remote_port_parent(ep);
735 if (!remote)
736 goto out_put_ep;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800737
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800738 /* does this node match any registered engines? */
739 list_for_each_entry(engine, &drv->engine_list, list)
740 if (remote == engine->node)
741 goto out_put_remote;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800742
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800743 /* keep looking through upstream ports */
744 engine = sun4i_tcon_find_engine_traverse(drv, remote);
745
746out_put_remote:
747 of_node_put(remote);
748out_put_ep:
749 of_node_put(ep);
750out_put_port:
751 of_node_put(port);
752
753 return engine;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800754}
755
Chen-Yu Tsaie8d5bbf2017-09-08 15:50:12 +0800756/*
757 * The device tree binding says that the remote endpoint ID of any
758 * connection between components, up to and including the TCON, of
759 * the display pipeline should be equal to the actual ID of the local
760 * component. Thus we can look at any one of the input connections of
761 * the TCONs, and use that connection's remote endpoint ID as our own.
762 *
763 * Since the user of this function already finds the input port,
764 * the port is passed in directly without further checks.
765 */
766static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
767{
768 struct device_node *ep;
769 int ret = -EINVAL;
770
771 /* try finding an upstream endpoint */
772 for_each_available_child_of_node(port, ep) {
773 struct device_node *remote;
774 u32 reg;
775
776 remote = of_graph_get_remote_endpoint(ep);
777 if (!remote)
778 continue;
779
780 ret = of_property_read_u32(remote, "reg", &reg);
781 if (ret)
782 continue;
783
784 ret = reg;
785 }
786
787 return ret;
788}
789
790/*
791 * Once we know the TCON's id, we can look through the list of
792 * engines to find a matching one. We assume all engines have
793 * been probed and added to the list.
794 */
795static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
796 int id)
797{
798 struct sunxi_engine *engine;
799
800 list_for_each_entry(engine, &drv->engine_list, list)
801 if (engine->id == id)
802 return engine;
803
804 return ERR_PTR(-EINVAL);
805}
806
807/*
808 * On SoCs with the old display pipeline design (Display Engine 1.0),
809 * we assumed the TCON was always tied to just one backend. However
810 * this proved not to be the case. On the A31, the TCON can select
811 * either backend as its source. On the A20 (and likely on the A10),
812 * the backend can choose which TCON to output to.
813 *
814 * The device tree binding says that the remote endpoint ID of any
815 * connection between components, up to and including the TCON, of
816 * the display pipeline should be equal to the actual ID of the local
817 * component. Thus we should be able to look at any one of the input
818 * connections of the TCONs, and use that connection's remote endpoint
819 * ID as our own.
820 *
821 * However the connections between the backend and TCON were assumed
822 * to be always singular, and their endpoit IDs were all incorrectly
823 * set to 0. This means for these old device trees, we cannot just look
824 * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
825 * incorrectly identified as TCON0.
826 *
827 * This function first checks if the TCON node has 2 input endpoints.
828 * If so, then the device tree is a corrected version, and it will use
829 * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
830 * to fetch the ID and engine directly. If not, then it is likely an
831 * old device trees, where the endpoint IDs were incorrect, but did not
832 * have endpoint connections between the backend and TCON across
833 * different display pipelines. It will fall back to the old method of
834 * traversing the of_graph to try and find a matching engine by device
835 * node.
836 *
837 * In the case of single display pipeline device trees, either method
838 * works.
839 */
840static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
841 struct device_node *node)
842{
843 struct device_node *port;
844 struct sunxi_engine *engine;
845
846 port = of_graph_get_port_by_id(node, 0);
847 if (!port)
848 return ERR_PTR(-EINVAL);
849
850 /*
851 * Is this a corrected device tree with cross pipeline
852 * connections between the backend and TCON?
853 */
854 if (of_get_child_count(port) > 1) {
855 /* Get our ID directly from an upstream endpoint */
856 int id = sun4i_tcon_of_get_id_from_port(port);
857
858 /* Get our engine by matching our ID */
859 engine = sun4i_tcon_get_engine_by_id(drv, id);
860
861 of_node_put(port);
862 return engine;
863 }
864
865 /* Fallback to old method by traversing input endpoints */
866 of_node_put(port);
867 return sun4i_tcon_find_engine_traverse(drv, node);
868}
869
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100870static int sun4i_tcon_bind(struct device *dev, struct device *master,
871 void *data)
872{
873 struct drm_device *drm = data;
874 struct sun4i_drv *drv = drm->dev_private;
Icenowy Zheng87969332017-05-17 22:47:17 +0800875 struct sunxi_engine *engine;
Maxime Riparda0c12142017-12-21 12:02:33 +0100876 struct device_node *remote;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100877 struct sun4i_tcon *tcon;
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +0800878 struct reset_control *edp_rstc;
Maxime Riparda0c12142017-12-21 12:02:33 +0100879 bool has_lvds_rst, has_lvds_alt, can_lvds;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100880 int ret;
881
Icenowy Zheng87969332017-05-17 22:47:17 +0800882 engine = sun4i_tcon_find_engine(drv, dev->of_node);
883 if (IS_ERR(engine)) {
884 dev_err(dev, "Couldn't find matching engine\n");
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800885 return -EPROBE_DEFER;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800886 }
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800887
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100888 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
889 if (!tcon)
890 return -ENOMEM;
891 dev_set_drvdata(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100892 tcon->drm = drm;
Maxime Ripardae558112016-07-19 15:17:27 +0200893 tcon->dev = dev;
Icenowy Zheng87969332017-05-17 22:47:17 +0800894 tcon->id = engine->id;
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800895 tcon->quirks = of_device_get_match_data(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100896
897 tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
898 if (IS_ERR(tcon->lcd_rst)) {
899 dev_err(dev, "Couldn't get our reset line\n");
900 return PTR_ERR(tcon->lcd_rst);
901 }
902
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +0800903 if (tcon->quirks->needs_edp_reset) {
904 edp_rstc = devm_reset_control_get_shared(dev, "edp");
905 if (IS_ERR(edp_rstc)) {
906 dev_err(dev, "Couldn't get edp reset line\n");
907 return PTR_ERR(edp_rstc);
908 }
909
910 ret = reset_control_deassert(edp_rstc);
911 if (ret) {
912 dev_err(dev, "Couldn't deassert edp reset line\n");
913 return ret;
914 }
915 }
916
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100917 /* Make sure our TCON is reset */
Chen-Yu Tsaid57294c2017-09-08 17:00:16 +0800918 ret = reset_control_reset(tcon->lcd_rst);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100919 if (ret) {
920 dev_err(dev, "Couldn't deassert our reset line\n");
921 return ret;
922 }
923
Maxime Riparda0c12142017-12-21 12:02:33 +0100924 /*
925 * This can only be made optional since we've had DT nodes
926 * without the LVDS reset properties.
927 *
928 * If the property is missing, just disable LVDS, and print a
929 * warning.
930 */
931 tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
932 if (IS_ERR(tcon->lvds_rst)) {
933 dev_err(dev, "Couldn't get our reset line\n");
934 return PTR_ERR(tcon->lvds_rst);
935 } else if (tcon->lvds_rst) {
936 has_lvds_rst = true;
937 reset_control_reset(tcon->lvds_rst);
938 } else {
939 has_lvds_rst = false;
940 }
941
942 /*
943 * This can only be made optional since we've had DT nodes
944 * without the LVDS reset properties.
945 *
946 * If the property is missing, just disable LVDS, and print a
947 * warning.
948 */
949 if (tcon->quirks->has_lvds_alt) {
950 tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
951 if (IS_ERR(tcon->lvds_pll)) {
952 if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
953 has_lvds_alt = false;
954 } else {
955 dev_err(dev, "Couldn't get the LVDS PLL\n");
Dan Carpenter86a3ae52018-01-15 11:11:13 +0300956 return PTR_ERR(tcon->lvds_pll);
Maxime Riparda0c12142017-12-21 12:02:33 +0100957 }
958 } else {
959 has_lvds_alt = true;
960 }
961 }
962
963 if (!has_lvds_rst || (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
964 dev_warn(dev,
965 "Missing LVDS properties, Please upgrade your DT\n");
966 dev_warn(dev, "LVDS output disabled\n");
967 can_lvds = false;
968 } else {
969 can_lvds = true;
970 }
971
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100972 ret = sun4i_tcon_init_clocks(dev, tcon);
973 if (ret) {
974 dev_err(dev, "Couldn't init our TCON clocks\n");
975 goto err_assert_reset;
976 }
977
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800978 ret = sun4i_tcon_init_regmap(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100979 if (ret) {
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800980 dev_err(dev, "Couldn't init our TCON regmap\n");
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100981 goto err_free_clocks;
982 }
983
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100984 if (tcon->quirks->has_channel_0) {
985 ret = sun4i_dclk_create(dev, tcon);
986 if (ret) {
987 dev_err(dev, "Couldn't create our TCON dot clock\n");
988 goto err_free_clocks;
989 }
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800990 }
991
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100992 ret = sun4i_tcon_init_irq(dev, tcon);
993 if (ret) {
994 dev_err(dev, "Couldn't init our TCON interrupts\n");
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800995 goto err_free_dotclock;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100996 }
997
Icenowy Zheng87969332017-05-17 22:47:17 +0800998 tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800999 if (IS_ERR(tcon->crtc)) {
1000 dev_err(dev, "Couldn't create our CRTC\n");
1001 ret = PTR_ERR(tcon->crtc);
Maxime Ripard92411f62017-12-07 16:58:50 +01001002 goto err_free_dotclock;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +08001003 }
1004
Maxime Riparda0c12142017-12-21 12:02:33 +01001005 /*
1006 * If we have an LVDS panel connected to the TCON, we should
1007 * just probe the LVDS connector. Otherwise, just probe RGB as
1008 * we used to.
1009 */
1010 remote = of_graph_get_remote_node(dev->of_node, 1, 0);
1011 if (of_device_is_compatible(remote, "panel-lvds"))
1012 if (can_lvds)
1013 ret = sun4i_lvds_init(drm, tcon);
1014 else
1015 ret = -EINVAL;
1016 else
1017 ret = sun4i_rgb_init(drm, tcon);
1018 of_node_put(remote);
1019
Chen-Yu Tsai13fef092016-05-17 23:56:06 +08001020 if (ret < 0)
Maxime Ripard92411f62017-12-07 16:58:50 +01001021 goto err_free_dotclock;
Chen-Yu Tsai13fef092016-05-17 23:56:06 +08001022
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001023 if (tcon->quirks->needs_de_be_mux) {
1024 /*
1025 * We assume there is no dynamic muxing of backends
1026 * and TCONs, so we select the backend with same ID.
1027 *
1028 * While dynamic selection might be interesting, since
1029 * the CRTC is tied to the TCON, while the layers are
1030 * tied to the backends, this means, we will need to
1031 * switch between groups of layers. There might not be
1032 * a way to represent this constraint in DRM.
1033 */
1034 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
1035 SUN4I_TCON0_CTL_SRC_SEL_MASK,
1036 tcon->id);
1037 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
1038 SUN4I_TCON1_CTL_SRC_SEL_MASK,
1039 tcon->id);
1040 }
1041
Chen-Yu Tsai80a58242017-04-21 16:38:50 +08001042 list_add_tail(&tcon->list, &drv->tcon_list);
1043
Chen-Yu Tsai13fef092016-05-17 23:56:06 +08001044 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001045
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +08001046err_free_dotclock:
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001047 if (tcon->quirks->has_channel_0)
1048 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001049err_free_clocks:
1050 sun4i_tcon_free_clocks(tcon);
1051err_assert_reset:
1052 reset_control_assert(tcon->lcd_rst);
1053 return ret;
1054}
1055
1056static void sun4i_tcon_unbind(struct device *dev, struct device *master,
1057 void *data)
1058{
1059 struct sun4i_tcon *tcon = dev_get_drvdata(dev);
1060
Chen-Yu Tsai80a58242017-04-21 16:38:50 +08001061 list_del(&tcon->list);
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001062 if (tcon->quirks->has_channel_0)
1063 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001064 sun4i_tcon_free_clocks(tcon);
1065}
1066
Julia Lawalldfeb6932016-11-12 18:19:58 +01001067static const struct component_ops sun4i_tcon_ops = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001068 .bind = sun4i_tcon_bind,
1069 .unbind = sun4i_tcon_unbind,
1070};
1071
1072static int sun4i_tcon_probe(struct platform_device *pdev)
1073{
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001074 struct device_node *node = pdev->dev.of_node;
Maxime Ripard894f5a92016-04-11 12:16:33 +02001075 struct drm_bridge *bridge;
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001076 struct drm_panel *panel;
Rob Herringebc94462017-03-29 13:55:46 -05001077 int ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001078
Rob Herringebc94462017-03-29 13:55:46 -05001079 ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1080 if (ret == -EPROBE_DEFER)
1081 return ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001082
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001083 return component_add(&pdev->dev, &sun4i_tcon_ops);
1084}
1085
1086static int sun4i_tcon_remove(struct platform_device *pdev)
1087{
1088 component_del(&pdev->dev, &sun4i_tcon_ops);
1089
1090 return 0;
1091}
1092
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001093/* platform specific TCON muxing callbacks */
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001094static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
1095 const struct drm_encoder *encoder)
1096{
1097 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
1098 u32 shift;
1099
1100 if (!tcon0)
1101 return -EINVAL;
1102
1103 switch (encoder->encoder_type) {
1104 case DRM_MODE_ENCODER_TMDS:
1105 /* HDMI */
1106 shift = 8;
1107 break;
1108 default:
1109 return -EINVAL;
1110 }
1111
1112 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
1113 0x3 << shift, tcon->id << shift);
1114
1115 return 0;
1116}
1117
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001118static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
Maxime Ripardabcb8762017-10-17 11:06:10 +02001119 const struct drm_encoder *encoder)
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001120{
1121 u32 val;
1122
1123 if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1124 val = 1;
1125 else
1126 val = 0;
1127
1128 /*
1129 * FIXME: Undocumented bits
1130 */
1131 return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1132}
1133
Chen-Yu Tsai67e32642017-10-10 11:19:59 +08001134static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
Maxime Ripardabcb8762017-10-17 11:06:10 +02001135 const struct drm_encoder *encoder)
Chen-Yu Tsai67e32642017-10-10 11:19:59 +08001136{
1137 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
1138 u32 shift;
1139
1140 if (!tcon0)
1141 return -EINVAL;
1142
1143 switch (encoder->encoder_type) {
1144 case DRM_MODE_ENCODER_TMDS:
1145 /* HDMI */
1146 shift = 8;
1147 break;
1148 default:
1149 /* TODO A31 has MIPI DSI but A31s does not */
1150 return -EINVAL;
1151 }
1152
1153 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
1154 0x3 << shift, tcon->id << shift);
1155
1156 return 0;
1157}
1158
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001159static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001160 .has_channel_0 = true,
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001161 .has_channel_1 = true,
1162 .set_mux = sun4i_a10_tcon_set_mux,
1163};
1164
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001165static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001166 .has_channel_0 = true,
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001167 .has_channel_1 = true,
1168 .set_mux = sun5i_a13_tcon_set_mux,
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001169};
1170
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001171static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001172 .has_channel_0 = true,
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001173 .has_channel_1 = true,
Maxime Riparda0c12142017-12-21 12:02:33 +01001174 .has_lvds_alt = true,
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001175 .needs_de_be_mux = true,
Chen-Yu Tsai67e32642017-10-10 11:19:59 +08001176 .set_mux = sun6i_tcon_set_mux,
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001177};
1178
1179static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001180 .has_channel_0 = true,
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001181 .has_channel_1 = true,
1182 .needs_de_be_mux = true,
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001183};
1184
Jonathan Liuaaddb6d2017-10-17 20:18:02 +08001185static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001186 .has_channel_0 = true,
Jonathan Liuaaddb6d2017-10-17 20:18:02 +08001187 .has_channel_1 = true,
1188 /* Same display pipeline structure as A10 */
1189 .set_mux = sun4i_a10_tcon_set_mux,
1190};
1191
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001192static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001193 .has_channel_0 = true,
Maxime Riparda0c12142017-12-21 12:02:33 +01001194 .has_lvds_alt = true,
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001195};
1196
Maxime Ripard2f0d7bb2017-12-21 12:02:34 +01001197static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001198 .has_channel_0 = true,
Maxime Ripard2f0d7bb2017-12-21 12:02:34 +01001199};
1200
Jernej Skrabec05adc892018-02-14 21:09:02 +01001201static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
1202 .has_channel_1 = true,
1203};
1204
Icenowy Zheng1a0edb32017-05-17 22:47:22 +08001205static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001206 .has_channel_0 = true,
Icenowy Zheng1a0edb32017-05-17 22:47:22 +08001207};
1208
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +08001209static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
1210 .has_channel_0 = true,
1211 .needs_edp_reset = true,
1212};
1213
1214static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
1215 .has_channel_1 = true,
1216 .needs_edp_reset = true,
1217};
1218
Chen-Yu Tsaiff71c2c2017-11-27 16:46:32 +08001219/* sun4i_drv uses this list to check if a device node is a TCON */
1220const struct of_device_id sun4i_tcon_of_table[] = {
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001221 { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001222 { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001223 { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
1224 { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
Jonathan Liuaaddb6d2017-10-17 20:18:02 +08001225 { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001226 { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
Maxime Ripard2f0d7bb2017-12-21 12:02:34 +01001227 { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
Jernej Skrabec05adc892018-02-14 21:09:02 +01001228 { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
Icenowy Zheng1a0edb32017-05-17 22:47:22 +08001229 { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +08001230 { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
1231 { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001232 { }
1233};
1234MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
Chen-Yu Tsaiff71c2c2017-11-27 16:46:32 +08001235EXPORT_SYMBOL(sun4i_tcon_of_table);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001236
1237static struct platform_driver sun4i_tcon_platform_driver = {
1238 .probe = sun4i_tcon_probe,
1239 .remove = sun4i_tcon_remove,
1240 .driver = {
1241 .name = "sun4i-tcon",
1242 .of_match_table = sun4i_tcon_of_table,
1243 },
1244};
1245module_platform_driver(sun4i_tcon_platform_driver);
1246
1247MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1248MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
1249MODULE_LICENSE("GPL");