blob: 08747fc3ee713d6ba796b946103334b302a48758 [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"
Maxime Riparda08fc7c2018-04-04 11:57:12 +020038#include "sun6i_mipi_dsi.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080039#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010040
Maxime Riparda0c12142017-12-21 12:02:33 +010041static 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
Maxime Ripard45e88f92017-10-17 11:06:12 +020082static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
83 bool enabled)
Maxime Ripard9026e0d2015-10-29 09:36:23 +010084{
Maxime Ripard45e88f92017-10-17 11:06:12 +020085 struct clk *clk;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010086
Maxime Ripard45e88f92017-10-17 11:06:12 +020087 switch (channel) {
88 case 0:
Jernej Skrabec34d698f2018-02-14 21:09:01 +010089 WARN_ON(!tcon->quirks->has_channel_0);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010090 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
91 SUN4I_TCON0_CTL_TCON_ENABLE,
Maxime Ripard45e88f92017-10-17 11:06:12 +020092 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");
Maxime Ripard8e924042016-01-07 12:32:07 +0100104 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100105 }
Maxime Ripard8e924042016-01-07 12:32:07 +0100106
Jernej Skrabecf3e5fee2018-03-01 22:34:32 +0100107 if (enabled) {
Maxime Ripard45e88f92017-10-17 11:06:12 +0200108 clk_prepare_enable(clk);
Ondrej Jirman70350462018-03-10 12:05:11 +0100109 clk_rate_exclusive_get(clk);
Jernej Skrabecf3e5fee2018-03-01 22:34:32 +0100110 } else {
111 clk_rate_exclusive_put(clk);
Maxime Ripard45e88f92017-10-17 11:06:12 +0200112 clk_disable_unprepare(clk);
Jernej Skrabecf3e5fee2018-03-01 22:34:32 +0100113 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100114}
Maxime Ripard45e88f92017-10-17 11:06:12 +0200115
Maxime Riparda0c12142017-12-21 12:02:33 +0100116static 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
Maxime Ripard45e88f92017-10-17 11:06:12 +0200162void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
163 const struct drm_encoder *encoder,
164 bool enabled)
165{
Maxime Riparda0c12142017-12-21 12:02:33 +0100166 bool is_lvds = false;
Maxime Ripard45e88f92017-10-17 11:06:12 +0200167 int channel;
168
169 switch (encoder->encoder_type) {
Maxime Riparda0c12142017-12-21 12:02:33 +0100170 case DRM_MODE_ENCODER_LVDS:
171 is_lvds = true;
172 /* Fallthrough */
Maxime Riparda08fc7c2018-04-04 11:57:12 +0200173 case DRM_MODE_ENCODER_DSI:
Maxime Ripard45e88f92017-10-17 11:06:12 +0200174 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
Maxime Riparda0c12142017-12-21 12:02:33 +0100186 if (is_lvds && !enabled)
187 sun4i_tcon_lvds_set_status(tcon, encoder, false);
188
Maxime Ripard45e88f92017-10-17 11:06:12 +0200189 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
190 SUN4I_TCON_GCTL_TCON_ENABLE,
191 enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
192
Maxime Riparda0c12142017-12-21 12:02:33 +0100193 if (is_lvds && enabled)
194 sun4i_tcon_lvds_set_status(tcon, encoder, true);
195
Maxime Ripard45e88f92017-10-17 11:06:12 +0200196 sun4i_tcon_channel_set_status(tcon, channel, enabled);
197}
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100198
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) |
Maxime Riparda493cea2018-04-04 11:57:09 +0200206 SUN4I_TCON_GINT0_VBLANK_ENABLE(1) |
207 SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100208
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
Chen-Yu Tsai67e32642017-10-10 11:19:59 +0800216/*
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
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200237void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
Maxime Ripardabcb8762017-10-17 11:06:10 +0200238 const struct drm_encoder *encoder)
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200239{
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800240 int ret = -ENOTSUPP;
Maxime Ripardb7cb9b92017-05-27 18:09:28 +0200241
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800242 if (tcon->quirks->set_mux)
243 ret = tcon->quirks->set_mux(tcon, encoder);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200244
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800245 DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
246 encoder->name, encoder->crtc->name, ret);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200247}
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200248
Maxime Ripard961c6452017-10-17 11:06:11 +0200249static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100250 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
Maxime Ripardba19c532017-10-17 11:06:14 +0200267static 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
Maxime Riparda08fc7c2018-04-04 11:57:12 +0200279static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon,
280 struct mipi_dsi_device *device,
281 const struct drm_display_mode *mode)
282{
283 u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format);
284 u8 lanes = device->lanes;
285 u32 block_space, start_delay;
286 u32 tcon_div;
287
288 tcon->dclk_min_div = 4;
289 tcon->dclk_max_div = 127;
290
291 sun4i_tcon0_mode_set_common(tcon, mode);
292
293 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
294 SUN4I_TCON0_CTL_IF_MASK,
295 SUN4I_TCON0_CTL_IF_8080);
296
297 regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG,
298 SUN4I_TCON_ECC_FIFO_EN);
299
300 regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG,
301 SUN4I_TCON0_CPU_IF_MODE_DSI |
302 SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH |
303 SUN4I_TCON0_CPU_IF_TRI_FIFO_EN |
304 SUN4I_TCON0_CPU_IF_TRI_EN);
305
306 /*
307 * This looks suspicious, but it works...
308 *
309 * The datasheet says that this should be set higher than 20 *
310 * pixel cycle, but it's not clear what a pixel cycle is.
311 */
312 regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div);
313 tcon_div &= GENMASK(6, 0);
314 block_space = mode->htotal * bpp / (tcon_div * lanes);
315 block_space -= mode->hdisplay + 40;
316
317 regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG,
318 SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) |
319 SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay));
320
321 regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG,
322 SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay));
323
324 start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1);
325 start_delay = start_delay * mode->crtc_htotal * 149;
326 start_delay = start_delay / (mode->crtc_clock / 1000) / 8;
327 regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG,
328 SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) |
329 SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay));
330
331 /*
332 * The Allwinner BSP has a comment that the period should be
333 * the display clock * 15, but uses an hardcoded 3000...
334 */
335 regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG,
336 SUN4I_TCON_SAFE_PERIOD_NUM(3000) |
337 SUN4I_TCON_SAFE_PERIOD_MODE(3));
338
339 /* Enable the output on the pins */
340 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG,
341 0xe0000000);
342}
343
Maxime Riparda0c12142017-12-21 12:02:33 +0100344static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
345 const struct drm_encoder *encoder,
346 const struct drm_display_mode *mode)
347{
348 unsigned int bp;
349 u8 clk_delay;
350 u32 reg, val = 0;
351
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100352 WARN_ON(!tcon->quirks->has_channel_0);
353
Maxime Riparda0c12142017-12-21 12:02:33 +0100354 tcon->dclk_min_div = 7;
355 tcon->dclk_max_div = 7;
356 sun4i_tcon0_mode_set_common(tcon, mode);
357
358 /* Adjust clock delay */
359 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
360 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
361 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
362 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
363
364 /*
365 * This is called a backporch in the register documentation,
366 * but it really is the back porch + hsync
367 */
368 bp = mode->crtc_htotal - mode->crtc_hsync_start;
369 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
370 mode->crtc_htotal, bp);
371
372 /* Set horizontal display timings */
373 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
374 SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
375 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
376
377 /*
378 * This is called a backporch in the register documentation,
379 * but it really is the back porch + hsync
380 */
381 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
382 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
383 mode->crtc_vtotal, bp);
384
385 /* Set vertical display timings */
386 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
387 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
388 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
389
390 reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
391 SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
392 SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
393 if (sun4i_tcon_get_pixel_depth(encoder) == 24)
394 reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
395 else
396 reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
397
398 regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
399
400 /* Setup the polarity of the various signals */
401 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
402 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
403
404 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
405 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
406
407 regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
408
409 /* Map output pins to channel 0 */
410 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
411 SUN4I_TCON_GCTL_IOMAP_MASK,
412 SUN4I_TCON_GCTL_IOMAP_TCON0);
Ondrej Jirman80b79e32018-02-22 17:12:17 +0100413
414 /* Enable the output on the pins */
415 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000);
Maxime Riparda0c12142017-12-21 12:02:33 +0100416}
417
Maxime Ripardba19c532017-10-17 11:06:14 +0200418static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
419 const struct drm_display_mode *mode)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100420{
Giulio Benetti2c17a432018-03-13 18:54:37 +0100421 struct drm_panel *panel = tcon->panel;
422 struct drm_connector *connector = panel->connector;
423 struct drm_display_info display_info = connector->display_info;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100424 unsigned int bp, hsync, vsync;
425 u8 clk_delay;
426 u32 val = 0;
427
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100428 WARN_ON(!tcon->quirks->has_channel_0);
429
Maxime Ripardec08d592017-12-21 12:02:32 +0100430 tcon->dclk_min_div = 6;
431 tcon->dclk_max_div = 127;
Maxime Ripardba19c532017-10-17 11:06:14 +0200432 sun4i_tcon0_mode_set_common(tcon, mode);
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800433
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100434 /* Adjust clock delay */
435 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
436 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
437 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
438 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
439
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100440 /*
441 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800442 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100443 */
444 bp = mode->crtc_htotal - mode->crtc_hsync_start;
445 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
446 mode->crtc_htotal, bp);
447
448 /* Set horizontal display timings */
449 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
450 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
451 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
452
453 /*
454 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800455 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100456 */
457 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
458 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
459 mode->crtc_vtotal, bp);
460
461 /* Set vertical display timings */
462 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
Maxime Riparda88cbbd2017-05-27 18:09:30 +0200463 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100464 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
465
466 /* Set Hsync and Vsync length */
467 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
468 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
469 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
470 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
471 SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
472 SUN4I_TCON0_BASIC3_H_SYNC(hsync));
473
474 /* Setup the polarity of the various signals */
Giulio Benettifa4127c2018-02-15 18:54:48 +0100475 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100476 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
477
Giulio Benettifa4127c2018-02-15 18:54:48 +0100478 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100479 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
480
Giulio Benetti2c17a432018-03-13 18:54:37 +0100481 /*
482 * On A20 and similar SoCs, the only way to achieve Positive Edge
483 * (Rising Edge), is setting dclk clock phase to 2/3(240°).
484 * By default TCON works in Negative Edge(Falling Edge),
485 * this is why phase is set to 0 in that case.
486 * Unfortunately there's no way to logically invert dclk through
487 * IO_POL register.
488 * The only acceptable way to work, triple checked with scope,
489 * is using clock phase set to 0° for Negative Edge and set to 240°
490 * for Positive Edge.
491 * On A33 and similar SoCs there would be a 90° phase option,
492 * but it divides also dclk by 2.
493 * Following code is a way to avoid quirks all around TCON
494 * and DOTCLOCK drivers.
495 */
496 if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
497 clk_set_phase(tcon->dclk, 240);
498
499 if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
500 clk_set_phase(tcon->dclk, 0);
501
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100502 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
503 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
504 val);
505
506 /* Map output pins to channel 0 */
507 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
508 SUN4I_TCON_GCTL_IOMAP_MASK,
509 SUN4I_TCON_GCTL_IOMAP_TCON0);
510
511 /* Enable the output on the pins */
512 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
513}
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100514
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200515static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
516 const struct drm_display_mode *mode)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100517{
Maxime Ripardb8317a32017-05-27 18:09:31 +0200518 unsigned int bp, hsync, vsync, vtotal;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100519 u8 clk_delay;
520 u32 val;
521
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800522 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +0100523
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800524 /* Configure the dot clock */
525 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
526
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100527 /* Adjust clock delay */
528 clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
529 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
530 SUN4I_TCON1_CTL_CLK_DELAY_MASK,
531 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
532
533 /* Set interlaced mode */
534 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
535 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
536 else
537 val = 0;
538 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
539 SUN4I_TCON1_CTL_INTERLACE_ENABLE,
540 val);
541
542 /* Set the input resolution */
543 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
544 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
545 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
546
547 /* Set the upscaling resolution */
548 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
549 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
550 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
551
552 /* Set the output resolution */
553 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
554 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
555 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
556
557 /* Set horizontal display timings */
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200558 bp = mode->crtc_htotal - mode->crtc_hsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100559 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
560 mode->htotal, bp);
561 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
562 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
563 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
564
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200565 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100566 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
Maxime Ripardb8317a32017-05-27 18:09:31 +0200567 mode->crtc_vtotal, bp);
568
569 /*
570 * The vertical resolution needs to be doubled in all
571 * cases. We could use crtc_vtotal and always multiply by two,
572 * but that leads to a rounding error in interlace when vtotal
573 * is odd.
574 *
575 * This happens with TV's PAL for example, where vtotal will
576 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
577 * 624, which apparently confuses the hardware.
578 *
579 * To work around this, we will always use vtotal, and
580 * multiply by two only if we're not in interlace.
581 */
582 vtotal = mode->vtotal;
583 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
584 vtotal = vtotal * 2;
585
586 /* Set vertical display timings */
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100587 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
Maxime Ripardb8317a32017-05-27 18:09:31 +0200588 SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100589 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
590
591 /* Set Hsync and Vsync length */
592 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
593 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
594 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
595 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
596 SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
597 SUN4I_TCON1_BASIC5_H_SYNC(hsync));
598
599 /* Map output pins to channel 1 */
600 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
601 SUN4I_TCON_GCTL_IOMAP_MASK,
602 SUN4I_TCON_GCTL_IOMAP_TCON1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100603}
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200604
605void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
606 const struct drm_encoder *encoder,
607 const struct drm_display_mode *mode)
608{
Maxime Riparda08fc7c2018-04-04 11:57:12 +0200609 struct sun6i_dsi *dsi;
610
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200611 switch (encoder->encoder_type) {
Maxime Riparda08fc7c2018-04-04 11:57:12 +0200612 case DRM_MODE_ENCODER_DSI:
613 /*
614 * This is not really elegant, but it's the "cleaner"
615 * way I could think of...
616 */
617 dsi = encoder_to_sun6i_dsi(encoder);
618 sun4i_tcon0_mode_set_cpu(tcon, dsi->device, mode);
619 break;
Maxime Riparda0c12142017-12-21 12:02:33 +0100620 case DRM_MODE_ENCODER_LVDS:
621 sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
622 break;
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200623 case DRM_MODE_ENCODER_NONE:
Maxime Ripardba19c532017-10-17 11:06:14 +0200624 sun4i_tcon0_mode_set_rgb(tcon, mode);
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200625 sun4i_tcon_set_mux(tcon, 0, encoder);
626 break;
627 case DRM_MODE_ENCODER_TVDAC:
628 case DRM_MODE_ENCODER_TMDS:
629 sun4i_tcon1_mode_set(tcon, mode);
630 sun4i_tcon_set_mux(tcon, 1, encoder);
631 break;
632 default:
633 DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
634 }
635}
636EXPORT_SYMBOL(sun4i_tcon_mode_set);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100637
638static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
639 struct sun4i_crtc *scrtc)
640{
641 unsigned long flags;
642
643 spin_lock_irqsave(&dev->event_lock, flags);
644 if (scrtc->event) {
645 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
646 drm_crtc_vblank_put(&scrtc->crtc);
647 scrtc->event = NULL;
648 }
649 spin_unlock_irqrestore(&dev->event_lock, flags);
650}
651
652static irqreturn_t sun4i_tcon_handler(int irq, void *private)
653{
654 struct sun4i_tcon *tcon = private;
655 struct drm_device *drm = tcon->drm;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800656 struct sun4i_crtc *scrtc = tcon->crtc;
Maxime Ripard3004f752018-01-22 10:25:20 +0100657 struct sunxi_engine *engine = scrtc->engine;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100658 unsigned int status;
659
660 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
661
662 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
Maxime Riparda493cea2018-04-04 11:57:09 +0200663 SUN4I_TCON_GINT0_VBLANK_INT(1) |
664 SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT)))
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100665 return IRQ_NONE;
666
667 drm_crtc_handle_vblank(&scrtc->crtc);
668 sun4i_tcon_finish_page_flip(drm, scrtc);
669
670 /* Acknowledge the interrupt */
671 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
672 SUN4I_TCON_GINT0_VBLANK_INT(0) |
Maxime Riparda493cea2018-04-04 11:57:09 +0200673 SUN4I_TCON_GINT0_VBLANK_INT(1) |
674 SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100675 0);
676
Maxime Ripard3004f752018-01-22 10:25:20 +0100677 if (engine->ops->vblank_quirk)
678 engine->ops->vblank_quirk(engine);
679
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100680 return IRQ_HANDLED;
681}
682
683static int sun4i_tcon_init_clocks(struct device *dev,
684 struct sun4i_tcon *tcon)
685{
686 tcon->clk = devm_clk_get(dev, "ahb");
687 if (IS_ERR(tcon->clk)) {
688 dev_err(dev, "Couldn't get the TCON bus clock\n");
689 return PTR_ERR(tcon->clk);
690 }
691 clk_prepare_enable(tcon->clk);
692
Jernej Skrabec34d698f2018-02-14 21:09:01 +0100693 if (tcon->quirks->has_channel_0) {
694 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
695 if (IS_ERR(tcon->sclk0)) {
696 dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
697 return PTR_ERR(tcon->sclk0);
698 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100699 }
700
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800701 if (tcon->quirks->has_channel_1) {
Maxime Ripard8e924042016-01-07 12:32:07 +0100702 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
703 if (IS_ERR(tcon->sclk1)) {
704 dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
705 return PTR_ERR(tcon->sclk1);
706 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100707 }
708
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800709 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100710}
711
712static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
713{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100714 clk_disable_unprepare(tcon->clk);
715}
716
717static int sun4i_tcon_init_irq(struct device *dev,
718 struct sun4i_tcon *tcon)
719{
720 struct platform_device *pdev = to_platform_device(dev);
721 int irq, ret;
722
723 irq = platform_get_irq(pdev, 0);
724 if (irq < 0) {
725 dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
726 return irq;
727 }
728
729 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
730 dev_name(dev), tcon);
731 if (ret) {
732 dev_err(dev, "Couldn't request the IRQ\n");
733 return ret;
734 }
735
736 return 0;
737}
738
739static struct regmap_config sun4i_tcon_regmap_config = {
740 .reg_bits = 32,
741 .val_bits = 32,
742 .reg_stride = 4,
743 .max_register = 0x800,
744};
745
746static int sun4i_tcon_init_regmap(struct device *dev,
747 struct sun4i_tcon *tcon)
748{
749 struct platform_device *pdev = to_platform_device(dev);
750 struct resource *res;
751 void __iomem *regs;
752
753 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
754 regs = devm_ioremap_resource(dev, res);
Wei Yongjunaf346f52016-08-26 14:25:25 +0000755 if (IS_ERR(regs))
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100756 return PTR_ERR(regs);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100757
758 tcon->regs = devm_regmap_init_mmio(dev, regs,
759 &sun4i_tcon_regmap_config);
760 if (IS_ERR(tcon->regs)) {
761 dev_err(dev, "Couldn't create the TCON regmap\n");
762 return PTR_ERR(tcon->regs);
763 }
764
765 /* Make sure the TCON is disabled and all IRQs are off */
766 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
767 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
768 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
769
770 /* Disable IO lines and set them to tristate */
771 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
772 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
773
774 return 0;
775}
776
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800777/*
778 * On SoCs with the old display pipeline design (Display Engine 1.0),
779 * the TCON is always tied to just one backend. Hence we can traverse
780 * the of_graph upwards to find the backend our tcon is connected to,
781 * and take its ID as our own.
782 *
783 * We can either identify backends from their compatible strings, which
784 * means maintaining a large list of them. Or, since the backend is
785 * registered and binded before the TCON, we can just go through the
786 * list of registered backends and compare the device node.
Icenowy Zheng87969332017-05-17 22:47:17 +0800787 *
788 * As the structures now store engines instead of backends, here this
789 * function in fact searches the corresponding engine, and the ID is
790 * requested via the get_id function of the engine.
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800791 */
Chen-Yu Tsaie8d5bbf2017-09-08 15:50:12 +0800792static struct sunxi_engine *
793sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
794 struct device_node *node)
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800795{
796 struct device_node *port, *ep, *remote;
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800797 struct sunxi_engine *engine = ERR_PTR(-EINVAL);
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800798
799 port = of_graph_get_port_by_id(node, 0);
800 if (!port)
801 return ERR_PTR(-EINVAL);
802
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800803 /*
804 * This only works if there is only one path from the TCON
805 * to any display engine. Otherwise the probe order of the
806 * TCONs and display engines is not guaranteed. They may
807 * either bind to the wrong one, or worse, bind to the same
808 * one if additional checks are not done.
809 *
810 * Bail out if there are multiple input connections.
811 */
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800812 if (of_get_available_child_count(port) != 1)
813 goto out_put_port;
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800814
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800815 /* Get the first connection without specifying an ID */
816 ep = of_get_next_available_child(port, NULL);
817 if (!ep)
818 goto out_put_port;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800819
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800820 remote = of_graph_get_remote_port_parent(ep);
821 if (!remote)
822 goto out_put_ep;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800823
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800824 /* does this node match any registered engines? */
825 list_for_each_entry(engine, &drv->engine_list, list)
826 if (remote == engine->node)
827 goto out_put_remote;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800828
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800829 /* keep looking through upstream ports */
830 engine = sun4i_tcon_find_engine_traverse(drv, remote);
831
832out_put_remote:
833 of_node_put(remote);
834out_put_ep:
835 of_node_put(ep);
836out_put_port:
837 of_node_put(port);
838
839 return engine;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800840}
841
Chen-Yu Tsaie8d5bbf2017-09-08 15:50:12 +0800842/*
843 * The device tree binding says that the remote endpoint ID of any
844 * connection between components, up to and including the TCON, of
845 * the display pipeline should be equal to the actual ID of the local
846 * component. Thus we can look at any one of the input connections of
847 * the TCONs, and use that connection's remote endpoint ID as our own.
848 *
849 * Since the user of this function already finds the input port,
850 * the port is passed in directly without further checks.
851 */
852static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
853{
854 struct device_node *ep;
855 int ret = -EINVAL;
856
857 /* try finding an upstream endpoint */
858 for_each_available_child_of_node(port, ep) {
859 struct device_node *remote;
860 u32 reg;
861
862 remote = of_graph_get_remote_endpoint(ep);
863 if (!remote)
864 continue;
865
866 ret = of_property_read_u32(remote, "reg", &reg);
867 if (ret)
868 continue;
869
870 ret = reg;
871 }
872
873 return ret;
874}
875
876/*
877 * Once we know the TCON's id, we can look through the list of
878 * engines to find a matching one. We assume all engines have
879 * been probed and added to the list.
880 */
881static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
882 int id)
883{
884 struct sunxi_engine *engine;
885
886 list_for_each_entry(engine, &drv->engine_list, list)
887 if (engine->id == id)
888 return engine;
889
890 return ERR_PTR(-EINVAL);
891}
892
893/*
894 * On SoCs with the old display pipeline design (Display Engine 1.0),
895 * we assumed the TCON was always tied to just one backend. However
896 * this proved not to be the case. On the A31, the TCON can select
897 * either backend as its source. On the A20 (and likely on the A10),
898 * the backend can choose which TCON to output to.
899 *
900 * The device tree binding says that the remote endpoint ID of any
901 * connection between components, up to and including the TCON, of
902 * the display pipeline should be equal to the actual ID of the local
903 * component. Thus we should be able to look at any one of the input
904 * connections of the TCONs, and use that connection's remote endpoint
905 * ID as our own.
906 *
907 * However the connections between the backend and TCON were assumed
908 * to be always singular, and their endpoit IDs were all incorrectly
909 * set to 0. This means for these old device trees, we cannot just look
910 * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
911 * incorrectly identified as TCON0.
912 *
913 * This function first checks if the TCON node has 2 input endpoints.
914 * If so, then the device tree is a corrected version, and it will use
915 * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
916 * to fetch the ID and engine directly. If not, then it is likely an
917 * old device trees, where the endpoint IDs were incorrect, but did not
918 * have endpoint connections between the backend and TCON across
919 * different display pipelines. It will fall back to the old method of
920 * traversing the of_graph to try and find a matching engine by device
921 * node.
922 *
923 * In the case of single display pipeline device trees, either method
924 * works.
925 */
926static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
927 struct device_node *node)
928{
929 struct device_node *port;
930 struct sunxi_engine *engine;
931
932 port = of_graph_get_port_by_id(node, 0);
933 if (!port)
934 return ERR_PTR(-EINVAL);
935
936 /*
937 * Is this a corrected device tree with cross pipeline
938 * connections between the backend and TCON?
939 */
940 if (of_get_child_count(port) > 1) {
941 /* Get our ID directly from an upstream endpoint */
942 int id = sun4i_tcon_of_get_id_from_port(port);
943
944 /* Get our engine by matching our ID */
945 engine = sun4i_tcon_get_engine_by_id(drv, id);
946
947 of_node_put(port);
948 return engine;
949 }
950
951 /* Fallback to old method by traversing input endpoints */
952 of_node_put(port);
953 return sun4i_tcon_find_engine_traverse(drv, node);
954}
955
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100956static int sun4i_tcon_bind(struct device *dev, struct device *master,
957 void *data)
958{
959 struct drm_device *drm = data;
960 struct sun4i_drv *drv = drm->dev_private;
Icenowy Zheng87969332017-05-17 22:47:17 +0800961 struct sunxi_engine *engine;
Maxime Riparda0c12142017-12-21 12:02:33 +0100962 struct device_node *remote;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100963 struct sun4i_tcon *tcon;
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +0800964 struct reset_control *edp_rstc;
Maxime Riparda0c12142017-12-21 12:02:33 +0100965 bool has_lvds_rst, has_lvds_alt, can_lvds;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100966 int ret;
967
Icenowy Zheng87969332017-05-17 22:47:17 +0800968 engine = sun4i_tcon_find_engine(drv, dev->of_node);
969 if (IS_ERR(engine)) {
970 dev_err(dev, "Couldn't find matching engine\n");
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800971 return -EPROBE_DEFER;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800972 }
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800973
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100974 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
975 if (!tcon)
976 return -ENOMEM;
977 dev_set_drvdata(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100978 tcon->drm = drm;
Maxime Ripardae558112016-07-19 15:17:27 +0200979 tcon->dev = dev;
Icenowy Zheng87969332017-05-17 22:47:17 +0800980 tcon->id = engine->id;
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800981 tcon->quirks = of_device_get_match_data(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100982
983 tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
984 if (IS_ERR(tcon->lcd_rst)) {
985 dev_err(dev, "Couldn't get our reset line\n");
986 return PTR_ERR(tcon->lcd_rst);
987 }
988
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +0800989 if (tcon->quirks->needs_edp_reset) {
990 edp_rstc = devm_reset_control_get_shared(dev, "edp");
991 if (IS_ERR(edp_rstc)) {
992 dev_err(dev, "Couldn't get edp reset line\n");
993 return PTR_ERR(edp_rstc);
994 }
995
996 ret = reset_control_deassert(edp_rstc);
997 if (ret) {
998 dev_err(dev, "Couldn't deassert edp reset line\n");
999 return ret;
1000 }
1001 }
1002
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001003 /* Make sure our TCON is reset */
Chen-Yu Tsaid57294c2017-09-08 17:00:16 +08001004 ret = reset_control_reset(tcon->lcd_rst);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001005 if (ret) {
1006 dev_err(dev, "Couldn't deassert our reset line\n");
1007 return ret;
1008 }
1009
Maxime Riparde742a172018-02-21 13:57:01 +01001010 if (tcon->quirks->supports_lvds) {
1011 /*
1012 * This can only be made optional since we've had DT
1013 * nodes without the LVDS reset properties.
1014 *
1015 * If the property is missing, just disable LVDS, and
1016 * print a warning.
1017 */
1018 tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
1019 if (IS_ERR(tcon->lvds_rst)) {
1020 dev_err(dev, "Couldn't get our reset line\n");
1021 return PTR_ERR(tcon->lvds_rst);
1022 } else if (tcon->lvds_rst) {
1023 has_lvds_rst = true;
1024 reset_control_reset(tcon->lvds_rst);
Maxime Riparda0c12142017-12-21 12:02:33 +01001025 } else {
Maxime Riparde742a172018-02-21 13:57:01 +01001026 has_lvds_rst = false;
Maxime Riparda0c12142017-12-21 12:02:33 +01001027 }
Maxime Riparda0c12142017-12-21 12:02:33 +01001028
Maxime Riparde742a172018-02-21 13:57:01 +01001029 /*
1030 * This can only be made optional since we've had DT
1031 * nodes without the LVDS reset properties.
1032 *
1033 * If the property is missing, just disable LVDS, and
1034 * print a warning.
1035 */
1036 if (tcon->quirks->has_lvds_alt) {
1037 tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
1038 if (IS_ERR(tcon->lvds_pll)) {
1039 if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
1040 has_lvds_alt = false;
1041 } else {
1042 dev_err(dev, "Couldn't get the LVDS PLL\n");
1043 return PTR_ERR(tcon->lvds_pll);
1044 }
1045 } else {
1046 has_lvds_alt = true;
1047 }
1048 }
1049
1050 if (!has_lvds_rst ||
1051 (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
1052 dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
1053 dev_warn(dev, "LVDS output disabled\n");
1054 can_lvds = false;
1055 } else {
1056 can_lvds = true;
1057 }
Maxime Riparda0c12142017-12-21 12:02:33 +01001058 } else {
Maxime Riparde742a172018-02-21 13:57:01 +01001059 can_lvds = false;
Maxime Riparda0c12142017-12-21 12:02:33 +01001060 }
1061
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001062 ret = sun4i_tcon_init_clocks(dev, tcon);
1063 if (ret) {
1064 dev_err(dev, "Couldn't init our TCON clocks\n");
1065 goto err_assert_reset;
1066 }
1067
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +08001068 ret = sun4i_tcon_init_regmap(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001069 if (ret) {
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +08001070 dev_err(dev, "Couldn't init our TCON regmap\n");
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001071 goto err_free_clocks;
1072 }
1073
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001074 if (tcon->quirks->has_channel_0) {
1075 ret = sun4i_dclk_create(dev, tcon);
1076 if (ret) {
1077 dev_err(dev, "Couldn't create our TCON dot clock\n");
1078 goto err_free_clocks;
1079 }
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +08001080 }
1081
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001082 ret = sun4i_tcon_init_irq(dev, tcon);
1083 if (ret) {
1084 dev_err(dev, "Couldn't init our TCON interrupts\n");
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +08001085 goto err_free_dotclock;
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001086 }
1087
Icenowy Zheng87969332017-05-17 22:47:17 +08001088 tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +08001089 if (IS_ERR(tcon->crtc)) {
1090 dev_err(dev, "Couldn't create our CRTC\n");
1091 ret = PTR_ERR(tcon->crtc);
Maxime Ripard92411f62017-12-07 16:58:50 +01001092 goto err_free_dotclock;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +08001093 }
1094
Maxime Riparda0c12142017-12-21 12:02:33 +01001095 /*
1096 * If we have an LVDS panel connected to the TCON, we should
1097 * just probe the LVDS connector. Otherwise, just probe RGB as
1098 * we used to.
1099 */
1100 remote = of_graph_get_remote_node(dev->of_node, 1, 0);
1101 if (of_device_is_compatible(remote, "panel-lvds"))
1102 if (can_lvds)
1103 ret = sun4i_lvds_init(drm, tcon);
1104 else
1105 ret = -EINVAL;
1106 else
1107 ret = sun4i_rgb_init(drm, tcon);
1108 of_node_put(remote);
1109
Chen-Yu Tsai13fef092016-05-17 23:56:06 +08001110 if (ret < 0)
Maxime Ripard92411f62017-12-07 16:58:50 +01001111 goto err_free_dotclock;
Chen-Yu Tsai13fef092016-05-17 23:56:06 +08001112
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001113 if (tcon->quirks->needs_de_be_mux) {
1114 /*
1115 * We assume there is no dynamic muxing of backends
1116 * and TCONs, so we select the backend with same ID.
1117 *
1118 * While dynamic selection might be interesting, since
1119 * the CRTC is tied to the TCON, while the layers are
1120 * tied to the backends, this means, we will need to
1121 * switch between groups of layers. There might not be
1122 * a way to represent this constraint in DRM.
1123 */
1124 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
1125 SUN4I_TCON0_CTL_SRC_SEL_MASK,
1126 tcon->id);
1127 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
1128 SUN4I_TCON1_CTL_SRC_SEL_MASK,
1129 tcon->id);
1130 }
1131
Chen-Yu Tsai80a58242017-04-21 16:38:50 +08001132 list_add_tail(&tcon->list, &drv->tcon_list);
1133
Chen-Yu Tsai13fef092016-05-17 23:56:06 +08001134 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001135
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +08001136err_free_dotclock:
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001137 if (tcon->quirks->has_channel_0)
1138 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001139err_free_clocks:
1140 sun4i_tcon_free_clocks(tcon);
1141err_assert_reset:
1142 reset_control_assert(tcon->lcd_rst);
1143 return ret;
1144}
1145
1146static void sun4i_tcon_unbind(struct device *dev, struct device *master,
1147 void *data)
1148{
1149 struct sun4i_tcon *tcon = dev_get_drvdata(dev);
1150
Chen-Yu Tsai80a58242017-04-21 16:38:50 +08001151 list_del(&tcon->list);
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001152 if (tcon->quirks->has_channel_0)
1153 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001154 sun4i_tcon_free_clocks(tcon);
1155}
1156
Julia Lawalldfeb6932016-11-12 18:19:58 +01001157static const struct component_ops sun4i_tcon_ops = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001158 .bind = sun4i_tcon_bind,
1159 .unbind = sun4i_tcon_unbind,
1160};
1161
1162static int sun4i_tcon_probe(struct platform_device *pdev)
1163{
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001164 struct device_node *node = pdev->dev.of_node;
Maxime Ripard894f5a92016-04-11 12:16:33 +02001165 struct drm_bridge *bridge;
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001166 struct drm_panel *panel;
Rob Herringebc94462017-03-29 13:55:46 -05001167 int ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001168
Rob Herringebc94462017-03-29 13:55:46 -05001169 ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
1170 if (ret == -EPROBE_DEFER)
1171 return ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +01001172
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001173 return component_add(&pdev->dev, &sun4i_tcon_ops);
1174}
1175
1176static int sun4i_tcon_remove(struct platform_device *pdev)
1177{
1178 component_del(&pdev->dev, &sun4i_tcon_ops);
1179
1180 return 0;
1181}
1182
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001183/* platform specific TCON muxing callbacks */
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001184static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
1185 const struct drm_encoder *encoder)
1186{
1187 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
1188 u32 shift;
1189
1190 if (!tcon0)
1191 return -EINVAL;
1192
1193 switch (encoder->encoder_type) {
1194 case DRM_MODE_ENCODER_TMDS:
1195 /* HDMI */
1196 shift = 8;
1197 break;
1198 default:
1199 return -EINVAL;
1200 }
1201
1202 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
1203 0x3 << shift, tcon->id << shift);
1204
1205 return 0;
1206}
1207
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001208static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
Maxime Ripardabcb8762017-10-17 11:06:10 +02001209 const struct drm_encoder *encoder)
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001210{
1211 u32 val;
1212
1213 if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
1214 val = 1;
1215 else
1216 val = 0;
1217
1218 /*
1219 * FIXME: Undocumented bits
1220 */
1221 return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
1222}
1223
Chen-Yu Tsai67e32642017-10-10 11:19:59 +08001224static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
Maxime Ripardabcb8762017-10-17 11:06:10 +02001225 const struct drm_encoder *encoder)
Chen-Yu Tsai67e32642017-10-10 11:19:59 +08001226{
1227 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
1228 u32 shift;
1229
1230 if (!tcon0)
1231 return -EINVAL;
1232
1233 switch (encoder->encoder_type) {
1234 case DRM_MODE_ENCODER_TMDS:
1235 /* HDMI */
1236 shift = 8;
1237 break;
1238 default:
1239 /* TODO A31 has MIPI DSI but A31s does not */
1240 return -EINVAL;
1241 }
1242
1243 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
1244 0x3 << shift, tcon->id << shift);
1245
1246 return 0;
1247}
1248
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001249static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001250 .has_channel_0 = true,
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001251 .has_channel_1 = true,
1252 .set_mux = sun4i_a10_tcon_set_mux,
1253};
1254
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001255static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001256 .has_channel_0 = true,
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +08001257 .has_channel_1 = true,
1258 .set_mux = sun5i_a13_tcon_set_mux,
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001259};
1260
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001261static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001262 .has_channel_0 = true,
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001263 .has_channel_1 = true,
Maxime Riparda0c12142017-12-21 12:02:33 +01001264 .has_lvds_alt = true,
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001265 .needs_de_be_mux = true,
Chen-Yu Tsai67e32642017-10-10 11:19:59 +08001266 .set_mux = sun6i_tcon_set_mux,
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001267};
1268
1269static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001270 .has_channel_0 = true,
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +08001271 .has_channel_1 = true,
1272 .needs_de_be_mux = true,
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001273};
1274
Jonathan Liuaaddb6d2017-10-17 20:18:02 +08001275static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001276 .has_channel_0 = true,
Jonathan Liuaaddb6d2017-10-17 20:18:02 +08001277 .has_channel_1 = true,
1278 /* Same display pipeline structure as A10 */
1279 .set_mux = sun4i_a10_tcon_set_mux,
1280};
1281
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001282static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001283 .has_channel_0 = true,
Maxime Riparda0c12142017-12-21 12:02:33 +01001284 .has_lvds_alt = true,
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001285};
1286
Maxime Ripard2f0d7bb2017-12-21 12:02:34 +01001287static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
Maxime Riparde742a172018-02-21 13:57:01 +01001288 .supports_lvds = true,
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001289 .has_channel_0 = true,
Maxime Ripard2f0d7bb2017-12-21 12:02:34 +01001290};
1291
Jernej Skrabec05adc892018-02-14 21:09:02 +01001292static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
1293 .has_channel_1 = true,
1294};
1295
Icenowy Zheng1a0edb32017-05-17 22:47:22 +08001296static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
Jernej Skrabec34d698f2018-02-14 21:09:01 +01001297 .has_channel_0 = true,
Icenowy Zheng1a0edb32017-05-17 22:47:22 +08001298};
1299
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +08001300static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
1301 .has_channel_0 = true,
1302 .needs_edp_reset = true,
1303};
1304
1305static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
1306 .has_channel_1 = true,
1307 .needs_edp_reset = true,
1308};
1309
Chen-Yu Tsaiff71c2c2017-11-27 16:46:32 +08001310/* sun4i_drv uses this list to check if a device node is a TCON */
1311const struct of_device_id sun4i_tcon_of_table[] = {
Jonathan Liu4bb206b2017-10-17 20:17:59 +08001312 { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001313 { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +08001314 { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
1315 { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
Jonathan Liuaaddb6d2017-10-17 20:18:02 +08001316 { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +08001317 { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
Maxime Ripard2f0d7bb2017-12-21 12:02:34 +01001318 { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
Jernej Skrabec05adc892018-02-14 21:09:02 +01001319 { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
Icenowy Zheng1a0edb32017-05-17 22:47:22 +08001320 { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
Chen-Yu Tsai6664e9d2018-03-15 19:41:31 +08001321 { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
1322 { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001323 { }
1324};
1325MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
Chen-Yu Tsaiff71c2c2017-11-27 16:46:32 +08001326EXPORT_SYMBOL(sun4i_tcon_of_table);
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001327
1328static struct platform_driver sun4i_tcon_platform_driver = {
1329 .probe = sun4i_tcon_probe,
1330 .remove = sun4i_tcon_remove,
1331 .driver = {
1332 .name = "sun4i-tcon",
1333 .of_match_table = sun4i_tcon_of_table,
1334 },
1335};
1336module_platform_driver(sun4i_tcon_platform_driver);
1337
1338MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1339MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
1340MODULE_LICENSE("GPL");