blob: 79d8a0319e7108ca0c587a11acee88f83df91301 [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>
17#include <drm/drm_modes.h>
Rob Herringebc94462017-03-29 13:55:46 -050018#include <drm/drm_of.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010019
20#include <linux/component.h>
21#include <linux/ioport.h>
22#include <linux/of_address.h>
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080023#include <linux/of_device.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010024#include <linux/of_irq.h>
25#include <linux/regmap.h>
26#include <linux/reset.h>
27
28#include "sun4i_crtc.h"
29#include "sun4i_dotclock.h"
30#include "sun4i_drv.h"
Maxime Ripard29e57fa2015-10-29 09:37:32 +010031#include "sun4i_rgb.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010032#include "sun4i_tcon.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080033#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010034
35void sun4i_tcon_disable(struct sun4i_tcon *tcon)
36{
37 DRM_DEBUG_DRIVER("Disabling TCON\n");
38
39 /* Disable the TCON */
40 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
41 SUN4I_TCON_GCTL_TCON_ENABLE, 0);
42}
43EXPORT_SYMBOL(sun4i_tcon_disable);
44
45void sun4i_tcon_enable(struct sun4i_tcon *tcon)
46{
47 DRM_DEBUG_DRIVER("Enabling TCON\n");
48
49 /* Enable the TCON */
50 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
51 SUN4I_TCON_GCTL_TCON_ENABLE,
52 SUN4I_TCON_GCTL_TCON_ENABLE);
53}
54EXPORT_SYMBOL(sun4i_tcon_enable);
55
56void sun4i_tcon_channel_disable(struct sun4i_tcon *tcon, int channel)
57{
Maxime Ripard1a075422017-05-27 18:09:26 +020058 DRM_DEBUG_DRIVER("Disabling TCON channel %d\n", channel);
59
Maxime Ripard9026e0d2015-10-29 09:36:23 +010060 /* Disable the TCON's channel */
61 if (channel == 0) {
62 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
63 SUN4I_TCON0_CTL_TCON_ENABLE, 0);
64 clk_disable_unprepare(tcon->dclk);
Maxime Ripard8e924042016-01-07 12:32:07 +010065 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010066 }
Maxime Ripard8e924042016-01-07 12:32:07 +010067
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080068 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +010069 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
70 SUN4I_TCON1_CTL_TCON_ENABLE, 0);
71 clk_disable_unprepare(tcon->sclk1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010072}
73EXPORT_SYMBOL(sun4i_tcon_channel_disable);
74
75void sun4i_tcon_channel_enable(struct sun4i_tcon *tcon, int channel)
76{
Maxime Ripard1a075422017-05-27 18:09:26 +020077 DRM_DEBUG_DRIVER("Enabling TCON channel %d\n", channel);
78
Maxime Ripard9026e0d2015-10-29 09:36:23 +010079 /* Enable the TCON's channel */
80 if (channel == 0) {
81 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
82 SUN4I_TCON0_CTL_TCON_ENABLE,
83 SUN4I_TCON0_CTL_TCON_ENABLE);
84 clk_prepare_enable(tcon->dclk);
Maxime Ripard8e924042016-01-07 12:32:07 +010085 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010086 }
Maxime Ripard8e924042016-01-07 12:32:07 +010087
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080088 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +010089 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
90 SUN4I_TCON1_CTL_TCON_ENABLE,
91 SUN4I_TCON1_CTL_TCON_ENABLE);
92 clk_prepare_enable(tcon->sclk1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010093}
94EXPORT_SYMBOL(sun4i_tcon_channel_enable);
95
96void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
97{
98 u32 mask, val = 0;
99
100 DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
101
102 mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
103 SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
104
105 if (enable)
106 val = mask;
107
108 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
109}
110EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
111
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200112void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
113 struct drm_encoder *encoder)
114{
Maxime Ripardb7cb9b92017-05-27 18:09:28 +0200115 u32 val;
116
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200117 if (!tcon->quirks->has_unknown_mux)
118 return;
119
120 if (channel != 1)
121 return;
122
Maxime Ripardb7cb9b92017-05-27 18:09:28 +0200123 if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
124 val = 1;
125 else
126 val = 0;
127
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200128 /*
129 * FIXME: Undocumented bits
130 */
Maxime Ripardb7cb9b92017-05-27 18:09:28 +0200131 regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
Maxime Ripardf8c73f42017-05-27 18:09:27 +0200132}
133EXPORT_SYMBOL(sun4i_tcon_set_mux);
134
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100135static int sun4i_tcon_get_clk_delay(struct drm_display_mode *mode,
136 int channel)
137{
138 int delay = mode->vtotal - mode->vdisplay;
139
140 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
141 delay /= 2;
142
143 if (channel == 1)
144 delay -= 2;
145
146 delay = min(delay, 30);
147
148 DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
149
150 return delay;
151}
152
153void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon,
154 struct drm_display_mode *mode)
155{
156 unsigned int bp, hsync, vsync;
157 u8 clk_delay;
158 u32 val = 0;
159
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800160 /* Configure the dot clock */
161 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
162
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100163 /* Adjust clock delay */
164 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
165 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
166 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
167 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
168
169 /* Set the resolution */
170 regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
171 SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
172 SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
173
174 /*
175 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800176 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100177 */
178 bp = mode->crtc_htotal - mode->crtc_hsync_start;
179 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
180 mode->crtc_htotal, bp);
181
182 /* Set horizontal display timings */
183 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
184 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
185 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
186
187 /*
188 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800189 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100190 */
191 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
192 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
193 mode->crtc_vtotal, bp);
194
195 /* Set vertical display timings */
196 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
Maxime Riparda88cbbd2017-05-27 18:09:30 +0200197 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100198 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
199
200 /* Set Hsync and Vsync length */
201 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
202 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
203 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
204 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
205 SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
206 SUN4I_TCON0_BASIC3_H_SYNC(hsync));
207
208 /* Setup the polarity of the various signals */
209 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
210 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
211
212 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
213 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
214
215 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
216 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
217 val);
218
219 /* Map output pins to channel 0 */
220 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
221 SUN4I_TCON_GCTL_IOMAP_MASK,
222 SUN4I_TCON_GCTL_IOMAP_TCON0);
223
224 /* Enable the output on the pins */
225 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
226}
227EXPORT_SYMBOL(sun4i_tcon0_mode_set);
228
229void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
230 struct drm_display_mode *mode)
231{
Maxime Ripardb8317a32017-05-27 18:09:31 +0200232 unsigned int bp, hsync, vsync, vtotal;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100233 u8 clk_delay;
234 u32 val;
235
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800236 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +0100237
Chen-Yu Tsai86cf6782017-04-25 23:25:04 +0800238 /* Configure the dot clock */
239 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
240
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100241 /* Adjust clock delay */
242 clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
243 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
244 SUN4I_TCON1_CTL_CLK_DELAY_MASK,
245 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
246
247 /* Set interlaced mode */
248 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
249 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
250 else
251 val = 0;
252 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
253 SUN4I_TCON1_CTL_INTERLACE_ENABLE,
254 val);
255
256 /* Set the input resolution */
257 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
258 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
259 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
260
261 /* Set the upscaling resolution */
262 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
263 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
264 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
265
266 /* Set the output resolution */
267 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
268 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
269 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
270
271 /* Set horizontal display timings */
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200272 bp = mode->crtc_htotal - mode->crtc_hsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100273 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
274 mode->htotal, bp);
275 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
276 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
277 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
278
Maxime Ripard3cb2f462017-05-27 18:09:29 +0200279 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100280 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
Maxime Ripardb8317a32017-05-27 18:09:31 +0200281 mode->crtc_vtotal, bp);
282
283 /*
284 * The vertical resolution needs to be doubled in all
285 * cases. We could use crtc_vtotal and always multiply by two,
286 * but that leads to a rounding error in interlace when vtotal
287 * is odd.
288 *
289 * This happens with TV's PAL for example, where vtotal will
290 * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
291 * 624, which apparently confuses the hardware.
292 *
293 * To work around this, we will always use vtotal, and
294 * multiply by two only if we're not in interlace.
295 */
296 vtotal = mode->vtotal;
297 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
298 vtotal = vtotal * 2;
299
300 /* Set vertical display timings */
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100301 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
Maxime Ripardb8317a32017-05-27 18:09:31 +0200302 SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100303 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
304
305 /* Set Hsync and Vsync length */
306 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
307 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
308 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
309 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
310 SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
311 SUN4I_TCON1_BASIC5_H_SYNC(hsync));
312
313 /* Map output pins to channel 1 */
314 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
315 SUN4I_TCON_GCTL_IOMAP_MASK,
316 SUN4I_TCON_GCTL_IOMAP_TCON1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100317}
318EXPORT_SYMBOL(sun4i_tcon1_mode_set);
319
320static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
321 struct sun4i_crtc *scrtc)
322{
323 unsigned long flags;
324
325 spin_lock_irqsave(&dev->event_lock, flags);
326 if (scrtc->event) {
327 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
328 drm_crtc_vblank_put(&scrtc->crtc);
329 scrtc->event = NULL;
330 }
331 spin_unlock_irqrestore(&dev->event_lock, flags);
332}
333
334static irqreturn_t sun4i_tcon_handler(int irq, void *private)
335{
336 struct sun4i_tcon *tcon = private;
337 struct drm_device *drm = tcon->drm;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800338 struct sun4i_crtc *scrtc = tcon->crtc;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100339 unsigned int status;
340
341 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
342
343 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
344 SUN4I_TCON_GINT0_VBLANK_INT(1))))
345 return IRQ_NONE;
346
347 drm_crtc_handle_vblank(&scrtc->crtc);
348 sun4i_tcon_finish_page_flip(drm, scrtc);
349
350 /* Acknowledge the interrupt */
351 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
352 SUN4I_TCON_GINT0_VBLANK_INT(0) |
353 SUN4I_TCON_GINT0_VBLANK_INT(1),
354 0);
355
356 return IRQ_HANDLED;
357}
358
359static int sun4i_tcon_init_clocks(struct device *dev,
360 struct sun4i_tcon *tcon)
361{
362 tcon->clk = devm_clk_get(dev, "ahb");
363 if (IS_ERR(tcon->clk)) {
364 dev_err(dev, "Couldn't get the TCON bus clock\n");
365 return PTR_ERR(tcon->clk);
366 }
367 clk_prepare_enable(tcon->clk);
368
369 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
370 if (IS_ERR(tcon->sclk0)) {
371 dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
372 return PTR_ERR(tcon->sclk0);
373 }
374
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800375 if (tcon->quirks->has_channel_1) {
Maxime Ripard8e924042016-01-07 12:32:07 +0100376 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
377 if (IS_ERR(tcon->sclk1)) {
378 dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
379 return PTR_ERR(tcon->sclk1);
380 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100381 }
382
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800383 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100384}
385
386static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
387{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100388 clk_disable_unprepare(tcon->clk);
389}
390
391static int sun4i_tcon_init_irq(struct device *dev,
392 struct sun4i_tcon *tcon)
393{
394 struct platform_device *pdev = to_platform_device(dev);
395 int irq, ret;
396
397 irq = platform_get_irq(pdev, 0);
398 if (irq < 0) {
399 dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
400 return irq;
401 }
402
403 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
404 dev_name(dev), tcon);
405 if (ret) {
406 dev_err(dev, "Couldn't request the IRQ\n");
407 return ret;
408 }
409
410 return 0;
411}
412
413static struct regmap_config sun4i_tcon_regmap_config = {
414 .reg_bits = 32,
415 .val_bits = 32,
416 .reg_stride = 4,
417 .max_register = 0x800,
418};
419
420static int sun4i_tcon_init_regmap(struct device *dev,
421 struct sun4i_tcon *tcon)
422{
423 struct platform_device *pdev = to_platform_device(dev);
424 struct resource *res;
425 void __iomem *regs;
426
427 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428 regs = devm_ioremap_resource(dev, res);
Wei Yongjunaf346f52016-08-26 14:25:25 +0000429 if (IS_ERR(regs))
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100430 return PTR_ERR(regs);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100431
432 tcon->regs = devm_regmap_init_mmio(dev, regs,
433 &sun4i_tcon_regmap_config);
434 if (IS_ERR(tcon->regs)) {
435 dev_err(dev, "Couldn't create the TCON regmap\n");
436 return PTR_ERR(tcon->regs);
437 }
438
439 /* Make sure the TCON is disabled and all IRQs are off */
440 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
441 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
442 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
443
444 /* Disable IO lines and set them to tristate */
445 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
446 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
447
448 return 0;
449}
450
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800451/*
452 * On SoCs with the old display pipeline design (Display Engine 1.0),
453 * the TCON is always tied to just one backend. Hence we can traverse
454 * the of_graph upwards to find the backend our tcon is connected to,
455 * and take its ID as our own.
456 *
457 * We can either identify backends from their compatible strings, which
458 * means maintaining a large list of them. Or, since the backend is
459 * registered and binded before the TCON, we can just go through the
460 * list of registered backends and compare the device node.
Icenowy Zheng87969332017-05-17 22:47:17 +0800461 *
462 * As the structures now store engines instead of backends, here this
463 * function in fact searches the corresponding engine, and the ID is
464 * requested via the get_id function of the engine.
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800465 */
Icenowy Zheng87969332017-05-17 22:47:17 +0800466static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
467 struct device_node *node)
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800468{
469 struct device_node *port, *ep, *remote;
Icenowy Zheng87969332017-05-17 22:47:17 +0800470 struct sunxi_engine *engine;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800471
472 port = of_graph_get_port_by_id(node, 0);
473 if (!port)
474 return ERR_PTR(-EINVAL);
475
Chen-Yu Tsai14696192017-09-08 15:50:11 +0800476 /*
477 * This only works if there is only one path from the TCON
478 * to any display engine. Otherwise the probe order of the
479 * TCONs and display engines is not guaranteed. They may
480 * either bind to the wrong one, or worse, bind to the same
481 * one if additional checks are not done.
482 *
483 * Bail out if there are multiple input connections.
484 */
485 if (of_get_available_child_count(port) != 1) {
486 of_node_put(port);
487 return ERR_PTR(-EINVAL);
488 }
489
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800490 for_each_available_child_of_node(port, ep) {
491 remote = of_graph_get_remote_port_parent(ep);
492 if (!remote)
493 continue;
494
Icenowy Zheng87969332017-05-17 22:47:17 +0800495 /* does this node match any registered engines? */
496 list_for_each_entry(engine, &drv->engine_list, list) {
497 if (remote == engine->node) {
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800498 of_node_put(remote);
499 of_node_put(port);
Icenowy Zheng87969332017-05-17 22:47:17 +0800500 return engine;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800501 }
502 }
503
504 /* keep looking through upstream ports */
Icenowy Zheng87969332017-05-17 22:47:17 +0800505 engine = sun4i_tcon_find_engine(drv, remote);
506 if (!IS_ERR(engine)) {
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800507 of_node_put(remote);
508 of_node_put(port);
Icenowy Zheng87969332017-05-17 22:47:17 +0800509 return engine;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800510 }
511 }
512
513 return ERR_PTR(-EINVAL);
514}
515
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100516static int sun4i_tcon_bind(struct device *dev, struct device *master,
517 void *data)
518{
519 struct drm_device *drm = data;
520 struct sun4i_drv *drv = drm->dev_private;
Icenowy Zheng87969332017-05-17 22:47:17 +0800521 struct sunxi_engine *engine;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100522 struct sun4i_tcon *tcon;
523 int ret;
524
Icenowy Zheng87969332017-05-17 22:47:17 +0800525 engine = sun4i_tcon_find_engine(drv, dev->of_node);
526 if (IS_ERR(engine)) {
527 dev_err(dev, "Couldn't find matching engine\n");
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800528 return -EPROBE_DEFER;
Chen-Yu Tsaib317fa32017-04-21 16:38:54 +0800529 }
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800530
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100531 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
532 if (!tcon)
533 return -ENOMEM;
534 dev_set_drvdata(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100535 tcon->drm = drm;
Maxime Ripardae558112016-07-19 15:17:27 +0200536 tcon->dev = dev;
Icenowy Zheng87969332017-05-17 22:47:17 +0800537 tcon->id = engine->id;
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800538 tcon->quirks = of_device_get_match_data(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100539
540 tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
541 if (IS_ERR(tcon->lcd_rst)) {
542 dev_err(dev, "Couldn't get our reset line\n");
543 return PTR_ERR(tcon->lcd_rst);
544 }
545
546 /* Make sure our TCON is reset */
Chen-Yu Tsaid57294c2017-09-08 17:00:16 +0800547 ret = reset_control_reset(tcon->lcd_rst);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100548 if (ret) {
549 dev_err(dev, "Couldn't deassert our reset line\n");
550 return ret;
551 }
552
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100553 ret = sun4i_tcon_init_clocks(dev, tcon);
554 if (ret) {
555 dev_err(dev, "Couldn't init our TCON clocks\n");
556 goto err_assert_reset;
557 }
558
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800559 ret = sun4i_tcon_init_regmap(dev, tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100560 if (ret) {
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800561 dev_err(dev, "Couldn't init our TCON regmap\n");
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100562 goto err_free_clocks;
563 }
564
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800565 ret = sun4i_dclk_create(dev, tcon);
566 if (ret) {
567 dev_err(dev, "Couldn't create our TCON dot clock\n");
568 goto err_free_clocks;
569 }
570
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100571 ret = sun4i_tcon_init_irq(dev, tcon);
572 if (ret) {
573 dev_err(dev, "Couldn't init our TCON interrupts\n");
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800574 goto err_free_dotclock;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100575 }
576
Icenowy Zheng87969332017-05-17 22:47:17 +0800577 tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800578 if (IS_ERR(tcon->crtc)) {
579 dev_err(dev, "Couldn't create our CRTC\n");
580 ret = PTR_ERR(tcon->crtc);
581 goto err_free_clocks;
582 }
583
Chen-Yu Tsaib9c85062017-02-23 16:05:41 +0800584 ret = sun4i_rgb_init(drm, tcon);
Chen-Yu Tsai13fef092016-05-17 23:56:06 +0800585 if (ret < 0)
586 goto err_free_clocks;
587
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800588 list_add_tail(&tcon->list, &drv->tcon_list);
589
Chen-Yu Tsai13fef092016-05-17 23:56:06 +0800590 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100591
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800592err_free_dotclock:
593 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100594err_free_clocks:
595 sun4i_tcon_free_clocks(tcon);
596err_assert_reset:
597 reset_control_assert(tcon->lcd_rst);
598 return ret;
599}
600
601static void sun4i_tcon_unbind(struct device *dev, struct device *master,
602 void *data)
603{
604 struct sun4i_tcon *tcon = dev_get_drvdata(dev);
605
Chen-Yu Tsai80a58242017-04-21 16:38:50 +0800606 list_del(&tcon->list);
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800607 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100608 sun4i_tcon_free_clocks(tcon);
609}
610
Julia Lawalldfeb6932016-11-12 18:19:58 +0100611static const struct component_ops sun4i_tcon_ops = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100612 .bind = sun4i_tcon_bind,
613 .unbind = sun4i_tcon_unbind,
614};
615
616static int sun4i_tcon_probe(struct platform_device *pdev)
617{
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100618 struct device_node *node = pdev->dev.of_node;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200619 struct drm_bridge *bridge;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100620 struct drm_panel *panel;
Rob Herringebc94462017-03-29 13:55:46 -0500621 int ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100622
Rob Herringebc94462017-03-29 13:55:46 -0500623 ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
624 if (ret == -EPROBE_DEFER)
625 return ret;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100626
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100627 return component_add(&pdev->dev, &sun4i_tcon_ops);
628}
629
630static int sun4i_tcon_remove(struct platform_device *pdev)
631{
632 component_del(&pdev->dev, &sun4i_tcon_ops);
633
634 return 0;
635}
636
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800637static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
638 .has_unknown_mux = true,
639 .has_channel_1 = true,
640};
641
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800642static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
643 .has_channel_1 = true,
644};
645
646static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
647 .has_channel_1 = true,
648};
649
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800650static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
651 /* nothing is supported */
652};
653
Icenowy Zheng1a0edb32017-05-17 22:47:22 +0800654static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
655 /* nothing is supported */
656};
657
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100658static const struct of_device_id sun4i_tcon_of_table[] = {
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800659 { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800660 { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
661 { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800662 { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
Icenowy Zheng1a0edb32017-05-17 22:47:22 +0800663 { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100664 { }
665};
666MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
667
668static struct platform_driver sun4i_tcon_platform_driver = {
669 .probe = sun4i_tcon_probe,
670 .remove = sun4i_tcon_remove,
671 .driver = {
672 .name = "sun4i-tcon",
673 .of_match_table = sun4i_tcon_of_table,
674 },
675};
676module_platform_driver(sun4i_tcon_platform_driver);
677
678MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
679MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
680MODULE_LICENSE("GPL");