blob: c52c482c8fd0d93c399e21fc8433da7b4ea58a73 [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>
Maxime Ripard29e57fa2015-10-29 09:37:32 +010018#include <drm/drm_panel.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 Ripard29e57fa2015-10-29 09:37:32 +010024#include <linux/of_graph.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010025#include <linux/of_irq.h>
26#include <linux/regmap.h>
27#include <linux/reset.h>
28
29#include "sun4i_crtc.h"
30#include "sun4i_dotclock.h"
31#include "sun4i_drv.h"
Maxime Ripard29e57fa2015-10-29 09:37:32 +010032#include "sun4i_rgb.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010033#include "sun4i_tcon.h"
34
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{
58 /* Disable the TCON's channel */
59 if (channel == 0) {
60 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
61 SUN4I_TCON0_CTL_TCON_ENABLE, 0);
62 clk_disable_unprepare(tcon->dclk);
Maxime Ripard8e924042016-01-07 12:32:07 +010063 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010064 }
Maxime Ripard8e924042016-01-07 12:32:07 +010065
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080066 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +010067 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
68 SUN4I_TCON1_CTL_TCON_ENABLE, 0);
69 clk_disable_unprepare(tcon->sclk1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010070}
71EXPORT_SYMBOL(sun4i_tcon_channel_disable);
72
73void sun4i_tcon_channel_enable(struct sun4i_tcon *tcon, int channel)
74{
75 /* Enable the TCON's channel */
76 if (channel == 0) {
77 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
78 SUN4I_TCON0_CTL_TCON_ENABLE,
79 SUN4I_TCON0_CTL_TCON_ENABLE);
80 clk_prepare_enable(tcon->dclk);
Maxime Ripard8e924042016-01-07 12:32:07 +010081 return;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010082 }
Maxime Ripard8e924042016-01-07 12:32:07 +010083
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +080084 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +010085 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
86 SUN4I_TCON1_CTL_TCON_ENABLE,
87 SUN4I_TCON1_CTL_TCON_ENABLE);
88 clk_prepare_enable(tcon->sclk1);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010089}
90EXPORT_SYMBOL(sun4i_tcon_channel_enable);
91
92void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
93{
94 u32 mask, val = 0;
95
96 DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
97
98 mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
99 SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
100
101 if (enable)
102 val = mask;
103
104 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
105}
106EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
107
108static int sun4i_tcon_get_clk_delay(struct drm_display_mode *mode,
109 int channel)
110{
111 int delay = mode->vtotal - mode->vdisplay;
112
113 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
114 delay /= 2;
115
116 if (channel == 1)
117 delay -= 2;
118
119 delay = min(delay, 30);
120
121 DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
122
123 return delay;
124}
125
126void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon,
127 struct drm_display_mode *mode)
128{
129 unsigned int bp, hsync, vsync;
130 u8 clk_delay;
131 u32 val = 0;
132
133 /* Adjust clock delay */
134 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
135 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
136 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
137 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
138
139 /* Set the resolution */
140 regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
141 SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
142 SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
143
144 /*
145 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800146 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100147 */
148 bp = mode->crtc_htotal - mode->crtc_hsync_start;
149 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
150 mode->crtc_htotal, bp);
151
152 /* Set horizontal display timings */
153 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
154 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
155 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
156
157 /*
158 * This is called a backporch in the register documentation,
Chen-Yu Tsai23a1cb12017-03-09 18:05:25 +0800159 * but it really is the back porch + hsync
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100160 */
161 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
162 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
163 mode->crtc_vtotal, bp);
164
165 /* Set vertical display timings */
166 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
167 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal) |
168 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
169
170 /* Set Hsync and Vsync length */
171 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
172 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
173 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
174 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
175 SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
176 SUN4I_TCON0_BASIC3_H_SYNC(hsync));
177
178 /* Setup the polarity of the various signals */
179 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
180 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
181
182 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
183 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
184
185 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
186 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
187 val);
188
189 /* Map output pins to channel 0 */
190 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
191 SUN4I_TCON_GCTL_IOMAP_MASK,
192 SUN4I_TCON_GCTL_IOMAP_TCON0);
193
194 /* Enable the output on the pins */
195 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
196}
197EXPORT_SYMBOL(sun4i_tcon0_mode_set);
198
199void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
200 struct drm_display_mode *mode)
201{
202 unsigned int bp, hsync, vsync;
203 u8 clk_delay;
204 u32 val;
205
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800206 WARN_ON(!tcon->quirks->has_channel_1);
Maxime Ripard8e924042016-01-07 12:32:07 +0100207
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100208 /* Adjust clock delay */
209 clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
210 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
211 SUN4I_TCON1_CTL_CLK_DELAY_MASK,
212 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
213
214 /* Set interlaced mode */
215 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
216 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
217 else
218 val = 0;
219 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
220 SUN4I_TCON1_CTL_INTERLACE_ENABLE,
221 val);
222
223 /* Set the input resolution */
224 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
225 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
226 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
227
228 /* Set the upscaling resolution */
229 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
230 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
231 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
232
233 /* Set the output resolution */
234 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
235 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
236 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
237
238 /* Set horizontal display timings */
239 bp = mode->crtc_htotal - mode->crtc_hsync_end;
240 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
241 mode->htotal, bp);
242 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
243 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
244 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
245
246 /* Set vertical display timings */
247 bp = mode->crtc_vtotal - mode->crtc_vsync_end;
248 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
249 mode->vtotal, bp);
250 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
251 SUN4I_TCON1_BASIC4_V_TOTAL(mode->vtotal) |
252 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
253
254 /* Set Hsync and Vsync length */
255 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
256 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
257 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
258 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
259 SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
260 SUN4I_TCON1_BASIC5_H_SYNC(hsync));
261
262 /* Map output pins to channel 1 */
263 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
264 SUN4I_TCON_GCTL_IOMAP_MASK,
265 SUN4I_TCON_GCTL_IOMAP_TCON1);
266
267 /*
268 * FIXME: Undocumented bits
269 */
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800270 if (tcon->quirks->has_unknown_mux)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100271 regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, 1);
272}
273EXPORT_SYMBOL(sun4i_tcon1_mode_set);
274
275static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
276 struct sun4i_crtc *scrtc)
277{
278 unsigned long flags;
279
280 spin_lock_irqsave(&dev->event_lock, flags);
281 if (scrtc->event) {
282 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
283 drm_crtc_vblank_put(&scrtc->crtc);
284 scrtc->event = NULL;
285 }
286 spin_unlock_irqrestore(&dev->event_lock, flags);
287}
288
289static irqreturn_t sun4i_tcon_handler(int irq, void *private)
290{
291 struct sun4i_tcon *tcon = private;
292 struct drm_device *drm = tcon->drm;
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800293 struct sun4i_crtc *scrtc = tcon->crtc;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100294 unsigned int status;
295
296 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
297
298 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
299 SUN4I_TCON_GINT0_VBLANK_INT(1))))
300 return IRQ_NONE;
301
302 drm_crtc_handle_vblank(&scrtc->crtc);
303 sun4i_tcon_finish_page_flip(drm, scrtc);
304
305 /* Acknowledge the interrupt */
306 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
307 SUN4I_TCON_GINT0_VBLANK_INT(0) |
308 SUN4I_TCON_GINT0_VBLANK_INT(1),
309 0);
310
311 return IRQ_HANDLED;
312}
313
314static int sun4i_tcon_init_clocks(struct device *dev,
315 struct sun4i_tcon *tcon)
316{
317 tcon->clk = devm_clk_get(dev, "ahb");
318 if (IS_ERR(tcon->clk)) {
319 dev_err(dev, "Couldn't get the TCON bus clock\n");
320 return PTR_ERR(tcon->clk);
321 }
322 clk_prepare_enable(tcon->clk);
323
324 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
325 if (IS_ERR(tcon->sclk0)) {
326 dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
327 return PTR_ERR(tcon->sclk0);
328 }
329
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800330 if (tcon->quirks->has_channel_1) {
Maxime Ripard8e924042016-01-07 12:32:07 +0100331 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
332 if (IS_ERR(tcon->sclk1)) {
333 dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
334 return PTR_ERR(tcon->sclk1);
335 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100336 }
337
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800338 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100339}
340
341static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
342{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100343 clk_disable_unprepare(tcon->clk);
344}
345
346static int sun4i_tcon_init_irq(struct device *dev,
347 struct sun4i_tcon *tcon)
348{
349 struct platform_device *pdev = to_platform_device(dev);
350 int irq, ret;
351
352 irq = platform_get_irq(pdev, 0);
353 if (irq < 0) {
354 dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
355 return irq;
356 }
357
358 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
359 dev_name(dev), tcon);
360 if (ret) {
361 dev_err(dev, "Couldn't request the IRQ\n");
362 return ret;
363 }
364
365 return 0;
366}
367
368static struct regmap_config sun4i_tcon_regmap_config = {
369 .reg_bits = 32,
370 .val_bits = 32,
371 .reg_stride = 4,
372 .max_register = 0x800,
373};
374
375static int sun4i_tcon_init_regmap(struct device *dev,
376 struct sun4i_tcon *tcon)
377{
378 struct platform_device *pdev = to_platform_device(dev);
379 struct resource *res;
380 void __iomem *regs;
381
382 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383 regs = devm_ioremap_resource(dev, res);
Wei Yongjunaf346f52016-08-26 14:25:25 +0000384 if (IS_ERR(regs))
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100385 return PTR_ERR(regs);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100386
387 tcon->regs = devm_regmap_init_mmio(dev, regs,
388 &sun4i_tcon_regmap_config);
389 if (IS_ERR(tcon->regs)) {
390 dev_err(dev, "Couldn't create the TCON regmap\n");
391 return PTR_ERR(tcon->regs);
392 }
393
394 /* Make sure the TCON is disabled and all IRQs are off */
395 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
396 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
397 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
398
399 /* Disable IO lines and set them to tristate */
400 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
401 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
402
403 return 0;
404}
405
Maxime Riparda8444c72016-07-20 10:35:06 +0200406struct drm_panel *sun4i_tcon_find_panel(struct device_node *node)
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100407{
408 struct device_node *port, *remote, *child;
409 struct device_node *end_node = NULL;
410
411 /* Inputs are listed first, then outputs */
412 port = of_graph_get_port_by_id(node, 1);
413
414 /*
415 * Our first output is the RGB interface where the panel will
416 * be connected.
417 */
418 for_each_child_of_node(port, child) {
419 u32 reg;
420
421 of_property_read_u32(child, "reg", &reg);
422 if (reg == 0)
423 end_node = child;
424 }
425
426 if (!end_node) {
427 DRM_DEBUG_DRIVER("Missing panel endpoint\n");
428 return ERR_PTR(-ENODEV);
429 }
430
431 remote = of_graph_get_remote_port_parent(end_node);
432 if (!remote) {
Maxime Ripard0bbbb002016-05-04 17:38:32 +0200433 DRM_DEBUG_DRIVER("Unable to parse remote node\n");
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100434 return ERR_PTR(-EINVAL);
435 }
436
Maxime Ripard0bbbb002016-05-04 17:38:32 +0200437 return of_drm_find_panel(remote) ?: ERR_PTR(-EPROBE_DEFER);
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100438}
439
Maxime Ripard894f5a92016-04-11 12:16:33 +0200440struct drm_bridge *sun4i_tcon_find_bridge(struct device_node *node)
441{
442 struct device_node *port, *remote, *child;
443 struct device_node *end_node = NULL;
444
445 /* Inputs are listed first, then outputs */
446 port = of_graph_get_port_by_id(node, 1);
447
448 /*
449 * Our first output is the RGB interface where the panel will
450 * be connected.
451 */
452 for_each_child_of_node(port, child) {
453 u32 reg;
454
455 of_property_read_u32(child, "reg", &reg);
456 if (reg == 0)
457 end_node = child;
458 }
459
460 if (!end_node) {
461 DRM_DEBUG_DRIVER("Missing bridge endpoint\n");
462 return ERR_PTR(-ENODEV);
463 }
464
465 remote = of_graph_get_remote_port_parent(end_node);
466 if (!remote) {
467 DRM_DEBUG_DRIVER("Enable to parse remote node\n");
468 return ERR_PTR(-EINVAL);
469 }
470
471 return of_drm_find_bridge(remote) ?: ERR_PTR(-EPROBE_DEFER);
472}
473
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100474static int sun4i_tcon_bind(struct device *dev, struct device *master,
475 void *data)
476{
477 struct drm_device *drm = data;
478 struct sun4i_drv *drv = drm->dev_private;
479 struct sun4i_tcon *tcon;
480 int ret;
481
482 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
483 if (!tcon)
484 return -ENOMEM;
485 dev_set_drvdata(dev, tcon);
486 drv->tcon = tcon;
487 tcon->drm = drm;
Maxime Ripardae558112016-07-19 15:17:27 +0200488 tcon->dev = dev;
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800489 tcon->quirks = of_device_get_match_data(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100490
491 tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
492 if (IS_ERR(tcon->lcd_rst)) {
493 dev_err(dev, "Couldn't get our reset line\n");
494 return PTR_ERR(tcon->lcd_rst);
495 }
496
497 /* Make sure our TCON is reset */
498 if (!reset_control_status(tcon->lcd_rst))
499 reset_control_assert(tcon->lcd_rst);
500
501 ret = reset_control_deassert(tcon->lcd_rst);
502 if (ret) {
503 dev_err(dev, "Couldn't deassert our reset line\n");
504 return ret;
505 }
506
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100507 ret = sun4i_tcon_init_clocks(dev, tcon);
508 if (ret) {
509 dev_err(dev, "Couldn't init our TCON clocks\n");
510 goto err_assert_reset;
511 }
512
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800513 ret = sun4i_tcon_init_regmap(dev, tcon);
514 if (ret) {
515 dev_err(dev, "Couldn't init our TCON regmap\n");
516 goto err_free_clocks;
517 }
518
519 ret = sun4i_dclk_create(dev, tcon);
520 if (ret) {
521 dev_err(dev, "Couldn't create our TCON dot clock\n");
522 goto err_free_clocks;
523 }
524
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100525 ret = sun4i_tcon_init_irq(dev, tcon);
526 if (ret) {
527 dev_err(dev, "Couldn't init our TCON interrupts\n");
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800528 goto err_free_dotclock;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100529 }
530
Chen-Yu Tsai46cce6d2017-02-23 16:05:37 +0800531 tcon->crtc = sun4i_crtc_init(drm);
532 if (IS_ERR(tcon->crtc)) {
533 dev_err(dev, "Couldn't create our CRTC\n");
534 ret = PTR_ERR(tcon->crtc);
535 goto err_free_clocks;
536 }
537
Chen-Yu Tsaib9c85062017-02-23 16:05:41 +0800538 ret = sun4i_rgb_init(drm, tcon);
Chen-Yu Tsai13fef092016-05-17 23:56:06 +0800539 if (ret < 0)
540 goto err_free_clocks;
541
542 return 0;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100543
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800544err_free_dotclock:
545 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100546err_free_clocks:
547 sun4i_tcon_free_clocks(tcon);
548err_assert_reset:
549 reset_control_assert(tcon->lcd_rst);
550 return ret;
551}
552
553static void sun4i_tcon_unbind(struct device *dev, struct device *master,
554 void *data)
555{
556 struct sun4i_tcon *tcon = dev_get_drvdata(dev);
557
Chen-Yu Tsai4c7f16d2017-03-09 18:05:24 +0800558 sun4i_dclk_free(tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100559 sun4i_tcon_free_clocks(tcon);
560}
561
Julia Lawalldfeb6932016-11-12 18:19:58 +0100562static const struct component_ops sun4i_tcon_ops = {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100563 .bind = sun4i_tcon_bind,
564 .unbind = sun4i_tcon_unbind,
565};
566
567static int sun4i_tcon_probe(struct platform_device *pdev)
568{
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100569 struct device_node *node = pdev->dev.of_node;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200570 struct drm_bridge *bridge;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100571 struct drm_panel *panel;
572
573 /*
Maxime Ripard894f5a92016-04-11 12:16:33 +0200574 * Neither the bridge or the panel is ready.
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100575 * Defer the probe.
576 */
577 panel = sun4i_tcon_find_panel(node);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200578 bridge = sun4i_tcon_find_bridge(node);
Maxime Ripard0bbbb002016-05-04 17:38:32 +0200579
580 /*
581 * If we don't have a panel endpoint, just go on
582 */
Maxime Ripard894f5a92016-04-11 12:16:33 +0200583 if ((PTR_ERR(panel) == -EPROBE_DEFER) &&
584 (PTR_ERR(bridge) == -EPROBE_DEFER)) {
585 DRM_DEBUG_DRIVER("Still waiting for our panel/bridge. Deferring...\n");
Maxime Ripard0bbbb002016-05-04 17:38:32 +0200586 return -EPROBE_DEFER;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100587 }
588
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100589 return component_add(&pdev->dev, &sun4i_tcon_ops);
590}
591
592static int sun4i_tcon_remove(struct platform_device *pdev)
593{
594 component_del(&pdev->dev, &sun4i_tcon_ops);
595
596 return 0;
597}
598
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800599static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
600 .has_unknown_mux = true,
601 .has_channel_1 = true,
602};
603
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800604static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
605 .has_channel_1 = true,
606};
607
608static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
609 .has_channel_1 = true,
610};
611
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800612static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
613 /* nothing is supported */
614};
615
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100616static const struct of_device_id sun4i_tcon_of_table[] = {
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800617 { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800618 { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
619 { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
Chen-Yu Tsai91ea2f22016-10-20 11:43:39 +0800620 { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100621 { }
622};
623MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
624
625static struct platform_driver sun4i_tcon_platform_driver = {
626 .probe = sun4i_tcon_probe,
627 .remove = sun4i_tcon_remove,
628 .driver = {
629 .name = "sun4i-tcon",
630 .of_match_table = sun4i_tcon_of_table,
631 },
632};
633module_platform_driver(sun4i_tcon_platform_driver);
634
635MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
636MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
637MODULE_LICENSE("GPL");