blob: 964cf22a1cedf34c8fa3fb28dbfc5960f8cb5b1b [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>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010020
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +080021#include <uapi/drm/drm_mode.h>
22
Maxime Ripard9026e0d2015-10-29 09:36:23 +010023#include <linux/component.h>
24#include <linux/ioport.h>
25#include <linux/of_address.h>
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080026#include <linux/of_device.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010027#include <linux/of_irq.h>
28#include <linux/regmap.h>
29#include <linux/reset.h>
30
31#include "sun4i_crtc.h"
32#include "sun4i_dotclock.h"
33#include "sun4i_drv.h"
Maxime Ripard29e57fa2015-10-29 09:37:32 +010034#include "sun4i_rgb.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010035#include "sun4i_tcon.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080036#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010037
Maxime Ripard45e88f92017-10-17 11:06:12 +020038static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
39 bool enabled)
Maxime Ripard9026e0d2015-10-29 09:36:23 +010040{
Maxime Ripard45e88f92017-10-17 11:06:12 +020041 struct clk *clk;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010042
Maxime Ripard45e88f92017-10-17 11:06:12 +020043 switch (channel) {
44 case 0:
Maxime Ripard9026e0d2015-10-29 09:36:23 +010045 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
46 SUN4I_TCON0_CTL_TCON_ENABLE,
Maxime Ripard45e88f92017-10-17 11:06:12 +020047 enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
48 clk = tcon->dclk;
49 break;
50 case 1:
51 WARN_ON(!tcon->quirks->has_channel_1);
52 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
53 SUN4I_TCON1_CTL_TCON_ENABLE,
54 enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
55 clk = tcon->sclk1;
56 break;
57 default:
58 DRM_WARN("Unknown channel... doing nothing\n");
Maxime Ripard8e924042016-01-07 12:32:07 +010059 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010060 }
Maxime Ripard8e924042016-01-07 12:32:07 +010061
Maxime Ripard45e88f92017-10-17 11:06:12 +020062 if (enabled)
63 clk_prepare_enable(clk);
64 else
65 clk_disable_unprepare(clk);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010066}
Maxime Ripard45e88f92017-10-17 11:06:12 +020067
68void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
69 const struct drm_encoder *encoder,
70 bool enabled)
71{
72 int channel;
73
74 switch (encoder->encoder_type) {
75 case DRM_MODE_ENCODER_NONE:
76 channel = 0;
77 break;
78 case DRM_MODE_ENCODER_TMDS:
79 case DRM_MODE_ENCODER_TVDAC:
80 channel = 1;
81 break;
82 default:
83 DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
84 return;
85 }
86
87 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
88 SUN4I_TCON_GCTL_TCON_ENABLE,
89 enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
90
91 sun4i_tcon_channel_set_status(tcon, channel, enabled);
92}
Maxime Ripard9026e0d2015-10-29 09:36:23 +010093
94void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
95{
96 u32 mask, val = 0;
97
98 DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
99
100 mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
101 SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
102
103 if (enable)
104 val = mask;
105
106 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
107}
108EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
109
Chen-Yu Tsai67e32642017-10-10 11:19:59 +0800110/*
111 * This function is a helper for TCON output muxing. The TCON output
112 * muxing control register in earlier SoCs (without the TCON TOP block)
113 * are located in TCON0. This helper returns a pointer to TCON0's
114 * sun4i_tcon structure, or NULL if not found.
115 */
116static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
117{
118 struct sun4i_drv *drv = drm->dev_private;
119 struct sun4i_tcon *tcon;
120
121 list_for_each_entry(tcon, &drv->tcon_list, list)
122 if (tcon->id == 0)
123 return tcon;
124
125 dev_warn(drm->dev,
126 "TCON0 not found, display output muxing may not work\n");
127
128 return NULL;
129}
130
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200131void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
Maxime Ripardabcb8762017-10-17 11:06:10 +0200132 const struct drm_encoder *encoder)
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200133{
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800134 int ret = -ENOTSUPP;
Maxime Ripardb7cb9b92017-05-27 18:09:28 +0200135
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800136 if (tcon->quirks->set_mux)
137 ret = tcon->quirks->set_mux(tcon, encoder);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200138
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800139 DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
140 encoder->name, encoder->crtc->name, ret);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200141}
142EXPORT_SYMBOL(sun4i_tcon_set_mux);
143
Maxime Ripard961c6452017-10-17 11:06:11 +0200144static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100145 int channel)
146{
147 int delay = mode->vtotal - mode->vdisplay;
148
149 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
150 delay /= 2;
151
152 if (channel == 1)
153 delay -= 2;
154
155 delay = min(delay, 30);
156
157 DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
158
159 return delay;
160}
161
162void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon,
163 struct drm_display_mode *mode)
164{
165 unsigned int bp, hsync, vsync;
166 u8 clk_delay;
167 u32 val = 0;
168
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800169 /* Configure the dot clock */
170 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
171
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100172 /* Adjust clock delay */
173 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
174 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
175 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
176 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
177
178 /* Set the resolution */
179 regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
180 SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
181 SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
182
183 /*
184 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800185 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100186 */
187 bp = mode->crtc_htotal - mode->crtc_hsync_start;
188 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
189 mode->crtc_htotal, bp);
190
191 /* Set horizontal display timings */
192 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
193 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
194 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
195
196 /*
197 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800198 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100199 */
200 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
201 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
202 mode->crtc_vtotal, bp);
203
204 /* Set vertical display timings */
205 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
Maxime Riparda88cbbd2017-05-27 18:09:30 +0200206 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100207 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
208
209 /* Set Hsync and Vsync length */
210 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
211 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
212 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
213 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
214 SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
215 SUN4I_TCON0_BASIC3_H_SYNC(hsync));
216
217 /* Setup the polarity of the various signals */
218 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
219 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
220
221 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
222 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
223
224 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
225 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
226 val);
227
228 /* Map output pins to channel 0 */
229 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
230 SUN4I_TCON_GCTL_IOMAP_MASK,
231 SUN4I_TCON_GCTL_IOMAP_TCON0);
232
233 /* Enable the output on the pins */
234 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
235}
236EXPORT_SYMBOL(sun4i_tcon0_mode_set);
237
238void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
239 struct drm_display_mode *mode)
240{
Maxime Ripardb8317a32017-05-27 18:09:31 +0200241 unsigned int bp, hsync, vsync, vtotal;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100242 u8 clk_delay;
243 u32 val;
244
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800245 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +0100246
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800247 /* Configure the dot clock */
248 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
249
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100250 /* Adjust clock delay */
251 clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
252 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
253 SUN4I_TCON1_CTL_CLK_DELAY_MASK,
254 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
255
256 /* Set interlaced mode */
257 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
258 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
259 else
260 val = 0;
261 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
262 SUN4I_TCON1_CTL_INTERLACE_ENABLE,
263 val);
264
265 /* Set the input resolution */
266 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
267 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
268 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
269
270 /* Set the upscaling resolution */
271 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
272 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
273 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
274
275 /* Set the output resolution */
276 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
277 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
278 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
279
280 /* Set horizontal display timings */
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200281 bp = mode->crtc_htotal - mode->crtc_hsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100282 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
283 mode->htotal, bp);
284 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
285 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
286 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
287
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200288 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100289 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
Maxime Ripardb8317a32017-05-27 18:09:31 +0200290 mode->crtc_vtotal, bp);
291
292 /*
293 * The vertical resolution needs to be doubled in all
294 * cases. We could use crtc_vtotal and always multiply by two,
295 * but that leads to a rounding error in interlace when vtotal
296 * is odd.
297 *
298 * This happens with TV's PAL for example, where vtotal will
299 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
300 * 624, which apparently confuses the hardware.
301 *
302 * To work around this, we will always use vtotal, and
303 * multiply by two only if we're not in interlace.
304 */
305 vtotal = mode->vtotal;
306 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
307 vtotal = vtotal * 2;
308
309 /* Set vertical display timings */
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100310 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
Maxime Ripardb8317a32017-05-27 18:09:31 +0200311 SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100312 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
313
314 /* Set Hsync and Vsync length */
315 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
316 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
317 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
318 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
319 SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
320 SUN4I_TCON1_BASIC5_H_SYNC(hsync));
321
322 /* Map output pins to channel 1 */
323 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
324 SUN4I_TCON_GCTL_IOMAP_MASK,
325 SUN4I_TCON_GCTL_IOMAP_TCON1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100326}
327EXPORT_SYMBOL(sun4i_tcon1_mode_set);
328
329static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
330 struct sun4i_crtc *scrtc)
331{
332 unsigned long flags;
333
334 spin_lock_irqsave(&dev->event_lock, flags);
335 if (scrtc->event) {
336 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
337 drm_crtc_vblank_put(&scrtc->crtc);
338 scrtc->event = NULL;
339 }
340 spin_unlock_irqrestore(&dev->event_lock, flags);
341}
342
343static irqreturn_t sun4i_tcon_handler(int irq, void *private)
344{
345 struct sun4i_tcon *tcon = private;
346 struct drm_device *drm = tcon->drm;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800347 struct sun4i_crtc *scrtc = tcon->crtc;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100348 unsigned int status;
349
350 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
351
352 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
353 SUN4I_TCON_GINT0_VBLANK_INT(1))))
354 return IRQ_NONE;
355
356 drm_crtc_handle_vblank(&scrtc->crtc);
357 sun4i_tcon_finish_page_flip(drm, scrtc);
358
359 /* Acknowledge the interrupt */
360 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
361 SUN4I_TCON_GINT0_VBLANK_INT(0) |
362 SUN4I_TCON_GINT0_VBLANK_INT(1),
363 0);
364
365 return IRQ_HANDLED;
366}
367
368static int sun4i_tcon_init_clocks(struct device *dev,
369 struct sun4i_tcon *tcon)
370{
371 tcon->clk = devm_clk_get(dev, "ahb");
372 if (IS_ERR(tcon->clk)) {
373 dev_err(dev, "Couldn't get the TCON bus clock\n");
374 return PTR_ERR(tcon->clk);
375 }
376 clk_prepare_enable(tcon->clk);
377
378 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
379 if (IS_ERR(tcon->sclk0)) {
380 dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
381 return PTR_ERR(tcon->sclk0);
382 }
383
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800384 if (tcon->quirks->has_channel_1) {
Maxime Ripard8e924042016-01-07 12:32:07 +0100385 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
386 if (IS_ERR(tcon->sclk1)) {
387 dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
388 return PTR_ERR(tcon->sclk1);
389 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100390 }
391
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800392 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100393}
394
395static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
396{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100397 clk_disable_unprepare(tcon->clk);
398}
399
400static int sun4i_tcon_init_irq(struct device *dev,
401 struct sun4i_tcon *tcon)
402{
403 struct platform_device *pdev = to_platform_device(dev);
404 int irq, ret;
405
406 irq = platform_get_irq(pdev, 0);
407 if (irq < 0) {
408 dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
409 return irq;
410 }
411
412 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
413 dev_name(dev), tcon);
414 if (ret) {
415 dev_err(dev, "Couldn't request the IRQ\n");
416 return ret;
417 }
418
419 return 0;
420}
421
422static struct regmap_config sun4i_tcon_regmap_config = {
423 .reg_bits = 32,
424 .val_bits = 32,
425 .reg_stride = 4,
426 .max_register = 0x800,
427};
428
429static int sun4i_tcon_init_regmap(struct device *dev,
430 struct sun4i_tcon *tcon)
431{
432 struct platform_device *pdev = to_platform_device(dev);
433 struct resource *res;
434 void __iomem *regs;
435
436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
437 regs = devm_ioremap_resource(dev, res);
Wei Yongjunaf346f52016-08-26 14:25:25 +0000438 if (IS_ERR(regs))
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100439 return PTR_ERR(regs);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100440
441 tcon->regs = devm_regmap_init_mmio(dev, regs,
442 &sun4i_tcon_regmap_config);
443 if (IS_ERR(tcon->regs)) {
444 dev_err(dev, "Couldn't create the TCON regmap\n");
445 return PTR_ERR(tcon->regs);
446 }
447
448 /* Make sure the TCON is disabled and all IRQs are off */
449 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
450 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
451 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
452
453 /* Disable IO lines and set them to tristate */
454 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
455 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
456
457 return 0;
458}
459
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800460/*
461 * On SoCs with the old display pipeline design (Display Engine 1.0),
462 * the TCON is always tied to just one backend. Hence we can traverse
463 * the of_graph upwards to find the backend our tcon is connected to,
464 * and take its ID as our own.
465 *
466 * We can either identify backends from their compatible strings, which
467 * means maintaining a large list of them. Or, since the backend is
468 * registered and binded before the TCON, we can just go through the
469 * list of registered backends and compare the device node.
Icenowy Zheng87969332017-05-17 22:47:17 +0800470 *
471 * As the structures now store engines instead of backends, here this
472 * function in fact searches the corresponding engine, and the ID is
473 * requested via the get_id function of the engine.
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800474 */
Chen-Yu Tsaie8d5bbf2017-09-08 15:50:12 +0800475static struct sunxi_engine *
476sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
477 struct device_node *node)
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800478{
479 struct device_node *port, *ep, *remote;
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800480 struct sunxi_engine *engine = ERR_PTR(-EINVAL);
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800481
482 port = of_graph_get_port_by_id(node, 0);
483 if (!port)
484 return ERR_PTR(-EINVAL);
485
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800486 /*
487 * This only works if there is only one path from the TCON
488 * to any display engine. Otherwise the probe order of the
489 * TCONs and display engines is not guaranteed. They may
490 * either bind to the wrong one, or worse, bind to the same
491 * one if additional checks are not done.
492 *
493 * Bail out if there are multiple input connections.
494 */
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800495 if (of_get_available_child_count(port) != 1)
496 goto out_put_port;
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800497
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800498 /* Get the first connection without specifying an ID */
499 ep = of_get_next_available_child(port, NULL);
500 if (!ep)
501 goto out_put_port;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800502
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800503 remote = of_graph_get_remote_port_parent(ep);
504 if (!remote)
505 goto out_put_ep;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800506
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800507 /* does this node match any registered engines? */
508 list_for_each_entry(engine, &drv->engine_list, list)
509 if (remote == engine->node)
510 goto out_put_remote;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800511
Chen-Yu Tsaibe3fe0f2017-09-08 15:50:13 +0800512 /* keep looking through upstream ports */
513 engine = sun4i_tcon_find_engine_traverse(drv, remote);
514
515out_put_remote:
516 of_node_put(remote);
517out_put_ep:
518 of_node_put(ep);
519out_put_port:
520 of_node_put(port);
521
522 return engine;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800523}
524
Chen-Yu Tsaie8d5bbf2017-09-08 15:50:12 +0800525/*
526 * The device tree binding says that the remote endpoint ID of any
527 * connection between components, up to and including the TCON, of
528 * the display pipeline should be equal to the actual ID of the local
529 * component. Thus we can look at any one of the input connections of
530 * the TCONs, and use that connection's remote endpoint ID as our own.
531 *
532 * Since the user of this function already finds the input port,
533 * the port is passed in directly without further checks.
534 */
535static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
536{
537 struct device_node *ep;
538 int ret = -EINVAL;
539
540 /* try finding an upstream endpoint */
541 for_each_available_child_of_node(port, ep) {
542 struct device_node *remote;
543 u32 reg;
544
545 remote = of_graph_get_remote_endpoint(ep);
546 if (!remote)
547 continue;
548
549 ret = of_property_read_u32(remote, "reg", &reg);
550 if (ret)
551 continue;
552
553 ret = reg;
554 }
555
556 return ret;
557}
558
559/*
560 * Once we know the TCON's id, we can look through the list of
561 * engines to find a matching one. We assume all engines have
562 * been probed and added to the list.
563 */
564static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
565 int id)
566{
567 struct sunxi_engine *engine;
568
569 list_for_each_entry(engine, &drv->engine_list, list)
570 if (engine->id == id)
571 return engine;
572
573 return ERR_PTR(-EINVAL);
574}
575
576/*
577 * On SoCs with the old display pipeline design (Display Engine 1.0),
578 * we assumed the TCON was always tied to just one backend. However
579 * this proved not to be the case. On the A31, the TCON can select
580 * either backend as its source. On the A20 (and likely on the A10),
581 * the backend can choose which TCON to output to.
582 *
583 * The device tree binding says that the remote endpoint ID of any
584 * connection between components, up to and including the TCON, of
585 * the display pipeline should be equal to the actual ID of the local
586 * component. Thus we should be able to look at any one of the input
587 * connections of the TCONs, and use that connection's remote endpoint
588 * ID as our own.
589 *
590 * However the connections between the backend and TCON were assumed
591 * to be always singular, and their endpoit IDs were all incorrectly
592 * set to 0. This means for these old device trees, we cannot just look
593 * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
594 * incorrectly identified as TCON0.
595 *
596 * This function first checks if the TCON node has 2 input endpoints.
597 * If so, then the device tree is a corrected version, and it will use
598 * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
599 * to fetch the ID and engine directly. If not, then it is likely an
600 * old device trees, where the endpoint IDs were incorrect, but did not
601 * have endpoint connections between the backend and TCON across
602 * different display pipelines. It will fall back to the old method of
603 * traversing the of_graph to try and find a matching engine by device
604 * node.
605 *
606 * In the case of single display pipeline device trees, either method
607 * works.
608 */
609static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
610 struct device_node *node)
611{
612 struct device_node *port;
613 struct sunxi_engine *engine;
614
615 port = of_graph_get_port_by_id(node, 0);
616 if (!port)
617 return ERR_PTR(-EINVAL);
618
619 /*
620 * Is this a corrected device tree with cross pipeline
621 * connections between the backend and TCON?
622 */
623 if (of_get_child_count(port) > 1) {
624 /* Get our ID directly from an upstream endpoint */
625 int id = sun4i_tcon_of_get_id_from_port(port);
626
627 /* Get our engine by matching our ID */
628 engine = sun4i_tcon_get_engine_by_id(drv, id);
629
630 of_node_put(port);
631 return engine;
632 }
633
634 /* Fallback to old method by traversing input endpoints */
635 of_node_put(port);
636 return sun4i_tcon_find_engine_traverse(drv, node);
637}
638
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100639static int sun4i_tcon_bind(struct device *dev, struct device *master,
640 void *data)
641{
642 struct drm_device *drm = data;
643 struct sun4i_drv *drv = drm->dev_private;
Icenowy Zheng87969332017-05-17 22:47:17 +0800644 struct sunxi_engine *engine;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100645 struct sun4i_tcon *tcon;
646 int ret;
647
Icenowy Zheng87969332017-05-17 22:47:17 +0800648 engine = sun4i_tcon_find_engine(drv, dev->of_node);
649 if (IS_ERR(engine)) {
650 dev_err(dev, "Couldn't find matching engine\n");
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800651 return -EPROBE_DEFER;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800652 }
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800653
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100654 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
655 if (!tcon)
656 return -ENOMEM;
657 dev_set_drvdata(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100658 tcon->drm = drm;
Maxime Ripardae558112016-07-19 15:17:27 +0200659 tcon->dev = dev;
Icenowy Zheng87969332017-05-17 22:47:17 +0800660 tcon->id = engine->id;
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800661 tcon->quirks = of_device_get_match_data(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100662
663 tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
664 if (IS_ERR(tcon->lcd_rst)) {
665 dev_err(dev, "Couldn't get our reset line\n");
666 return PTR_ERR(tcon->lcd_rst);
667 }
668
669 /* Make sure our TCON is reset */
Chen-Yu Tsaid57294c2017-09-08 17:00:16 +0800670 ret = reset_control_reset(tcon->lcd_rst);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100671 if (ret) {
672 dev_err(dev, "Couldn't deassert our reset line\n");
673 return ret;
674 }
675
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100676 ret = sun4i_tcon_init_clocks(dev, tcon);
677 if (ret) {
678 dev_err(dev, "Couldn't init our TCON clocks\n");
679 goto err_assert_reset;
680 }
681
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800682 ret = sun4i_tcon_init_regmap(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100683 if (ret) {
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800684 dev_err(dev, "Couldn't init our TCON regmap\n");
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100685 goto err_free_clocks;
686 }
687
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800688 ret = sun4i_dclk_create(dev, tcon);
689 if (ret) {
690 dev_err(dev, "Couldn't create our TCON dot clock\n");
691 goto err_free_clocks;
692 }
693
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100694 ret = sun4i_tcon_init_irq(dev, tcon);
695 if (ret) {
696 dev_err(dev, "Couldn't init our TCON interrupts\n");
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800697 goto err_free_dotclock;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100698 }
699
Icenowy Zheng87969332017-05-17 22:47:17 +0800700 tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800701 if (IS_ERR(tcon->crtc)) {
702 dev_err(dev, "Couldn't create our CRTC\n");
703 ret = PTR_ERR(tcon->crtc);
704 goto err_free_clocks;
705 }
706
Chen-Yu Tsaib9c85062017-02-23 16:05:41 +0800707 ret = sun4i_rgb_init(drm, tcon);
Chen-Yu Tsai13fef092016-05-17 23:56:06 +0800708 if (ret < 0)
709 goto err_free_clocks;
710
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +0800711 if (tcon->quirks->needs_de_be_mux) {
712 /*
713 * We assume there is no dynamic muxing of backends
714 * and TCONs, so we select the backend with same ID.
715 *
716 * While dynamic selection might be interesting, since
717 * the CRTC is tied to the TCON, while the layers are
718 * tied to the backends, this means, we will need to
719 * switch between groups of layers. There might not be
720 * a way to represent this constraint in DRM.
721 */
722 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
723 SUN4I_TCON0_CTL_SRC_SEL_MASK,
724 tcon->id);
725 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
726 SUN4I_TCON1_CTL_SRC_SEL_MASK,
727 tcon->id);
728 }
729
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800730 list_add_tail(&tcon->list, &drv->tcon_list);
731
Chen-Yu Tsai13fef092016-05-17 23:56:06 +0800732 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100733
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800734err_free_dotclock:
735 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100736err_free_clocks:
737 sun4i_tcon_free_clocks(tcon);
738err_assert_reset:
739 reset_control_assert(tcon->lcd_rst);
740 return ret;
741}
742
743static void sun4i_tcon_unbind(struct device *dev, struct device *master,
744 void *data)
745{
746 struct sun4i_tcon *tcon = dev_get_drvdata(dev);
747
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800748 list_del(&tcon->list);
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800749 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100750 sun4i_tcon_free_clocks(tcon);
751}
752
Julia Lawalldfeb6932016-11-12 18:19:58 +0100753static const struct component_ops sun4i_tcon_ops = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100754 .bind = sun4i_tcon_bind,
755 .unbind = sun4i_tcon_unbind,
756};
757
758static int sun4i_tcon_probe(struct platform_device *pdev)
759{
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100760 struct device_node *node = pdev->dev.of_node;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200761 struct drm_bridge *bridge;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100762 struct drm_panel *panel;
Rob Herringebc94462017-03-29 13:55:46 -0500763 int ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100764
Rob Herringebc94462017-03-29 13:55:46 -0500765 ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
766 if (ret == -EPROBE_DEFER)
767 return ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100768
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100769 return component_add(&pdev->dev, &sun4i_tcon_ops);
770}
771
772static int sun4i_tcon_remove(struct platform_device *pdev)
773{
774 component_del(&pdev->dev, &sun4i_tcon_ops);
775
776 return 0;
777}
778
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800779/* platform specific TCON muxing callbacks */
780static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
Maxime Ripardabcb8762017-10-17 11:06:10 +0200781 const struct drm_encoder *encoder)
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800782{
783 u32 val;
784
785 if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
786 val = 1;
787 else
788 val = 0;
789
790 /*
791 * FIXME: Undocumented bits
792 */
793 return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
794}
795
Chen-Yu Tsai67e32642017-10-10 11:19:59 +0800796static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
Maxime Ripardabcb8762017-10-17 11:06:10 +0200797 const struct drm_encoder *encoder)
Chen-Yu Tsai67e32642017-10-10 11:19:59 +0800798{
799 struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
800 u32 shift;
801
802 if (!tcon0)
803 return -EINVAL;
804
805 switch (encoder->encoder_type) {
806 case DRM_MODE_ENCODER_TMDS:
807 /* HDMI */
808 shift = 8;
809 break;
810 default:
811 /* TODO A31 has MIPI DSI but A31s does not */
812 return -EINVAL;
813 }
814
815 regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
816 0x3 << shift, tcon->id << shift);
817
818 return 0;
819}
820
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800821static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
Chen-Yu Tsaiad537fb2017-10-10 11:19:58 +0800822 .has_channel_1 = true,
823 .set_mux = sun5i_a13_tcon_set_mux,
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800824};
825
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800826static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +0800827 .has_channel_1 = true,
828 .needs_de_be_mux = true,
Chen-Yu Tsai67e32642017-10-10 11:19:59 +0800829 .set_mux = sun6i_tcon_set_mux,
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800830};
831
832static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
Chen-Yu Tsai27e18de2017-09-08 15:50:14 +0800833 .has_channel_1 = true,
834 .needs_de_be_mux = true,
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800835};
836
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800837static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
838 /* nothing is supported */
839};
840
Icenowy Zheng1a0edb32017-05-17 22:47:22 +0800841static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
842 /* nothing is supported */
843};
844
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100845static const struct of_device_id sun4i_tcon_of_table[] = {
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800846 { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800847 { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
848 { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800849 { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
Icenowy Zheng1a0edb32017-05-17 22:47:22 +0800850 { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100851 { }
852};
853MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
854
855static struct platform_driver sun4i_tcon_platform_driver = {
856 .probe = sun4i_tcon_probe,
857 .remove = sun4i_tcon_remove,
858 .driver = {
859 .name = "sun4i-tcon",
860 .of_match_table = sun4i_tcon_of_table,
861 },
862};
863module_platform_driver(sun4i_tcon_platform_driver);
864
865MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
866MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
867MODULE_LICENSE("GPL");