blob: 5fbd3ce9d65140182fa98fb883c244d644979602 [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>
18
19#include <linux/component.h>
20#include <linux/ioport.h>
21#include <linux/of_address.h>
22#include <linux/of_irq.h>
23#include <linux/regmap.h>
24#include <linux/reset.h>
25
26#include "sun4i_crtc.h"
27#include "sun4i_dotclock.h"
28#include "sun4i_drv.h"
29#include "sun4i_tcon.h"
30
31void sun4i_tcon_disable(struct sun4i_tcon *tcon)
32{
33 DRM_DEBUG_DRIVER("Disabling TCON\n");
34
35 /* Disable the TCON */
36 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
37 SUN4I_TCON_GCTL_TCON_ENABLE, 0);
38}
39EXPORT_SYMBOL(sun4i_tcon_disable);
40
41void sun4i_tcon_enable(struct sun4i_tcon *tcon)
42{
43 DRM_DEBUG_DRIVER("Enabling TCON\n");
44
45 /* Enable the TCON */
46 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
47 SUN4I_TCON_GCTL_TCON_ENABLE,
48 SUN4I_TCON_GCTL_TCON_ENABLE);
49}
50EXPORT_SYMBOL(sun4i_tcon_enable);
51
52void sun4i_tcon_channel_disable(struct sun4i_tcon *tcon, int channel)
53{
54 /* Disable the TCON's channel */
55 if (channel == 0) {
56 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
57 SUN4I_TCON0_CTL_TCON_ENABLE, 0);
58 clk_disable_unprepare(tcon->dclk);
59 } else if (channel == 1) {
60 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
61 SUN4I_TCON1_CTL_TCON_ENABLE, 0);
62 clk_disable_unprepare(tcon->sclk1);
63 }
64}
65EXPORT_SYMBOL(sun4i_tcon_channel_disable);
66
67void sun4i_tcon_channel_enable(struct sun4i_tcon *tcon, int channel)
68{
69 /* Enable the TCON's channel */
70 if (channel == 0) {
71 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
72 SUN4I_TCON0_CTL_TCON_ENABLE,
73 SUN4I_TCON0_CTL_TCON_ENABLE);
74 clk_prepare_enable(tcon->dclk);
75 } else if (channel == 1) {
76 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
77 SUN4I_TCON1_CTL_TCON_ENABLE,
78 SUN4I_TCON1_CTL_TCON_ENABLE);
79 clk_prepare_enable(tcon->sclk1);
80 }
81}
82EXPORT_SYMBOL(sun4i_tcon_channel_enable);
83
84void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
85{
86 u32 mask, val = 0;
87
88 DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
89
90 mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
91 SUN4I_TCON_GINT0_VBLANK_ENABLE(1);
92
93 if (enable)
94 val = mask;
95
96 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
97}
98EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
99
100static int sun4i_tcon_get_clk_delay(struct drm_display_mode *mode,
101 int channel)
102{
103 int delay = mode->vtotal - mode->vdisplay;
104
105 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
106 delay /= 2;
107
108 if (channel == 1)
109 delay -= 2;
110
111 delay = min(delay, 30);
112
113 DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
114
115 return delay;
116}
117
118void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon,
119 struct drm_display_mode *mode)
120{
121 unsigned int bp, hsync, vsync;
122 u8 clk_delay;
123 u32 val = 0;
124
125 /* Adjust clock delay */
126 clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
127 regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
128 SUN4I_TCON0_CTL_CLK_DELAY_MASK,
129 SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
130
131 /* Set the resolution */
132 regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
133 SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
134 SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
135
136 /*
137 * This is called a backporch in the register documentation,
138 * but it really is the front porch + hsync
139 */
140 bp = mode->crtc_htotal - mode->crtc_hsync_start;
141 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
142 mode->crtc_htotal, bp);
143
144 /* Set horizontal display timings */
145 regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
146 SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
147 SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
148
149 /*
150 * This is called a backporch in the register documentation,
151 * but it really is the front porch + hsync
152 */
153 bp = mode->crtc_vtotal - mode->crtc_vsync_start;
154 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
155 mode->crtc_vtotal, bp);
156
157 /* Set vertical display timings */
158 regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
159 SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal) |
160 SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
161
162 /* Set Hsync and Vsync length */
163 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
164 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
165 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
166 regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
167 SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
168 SUN4I_TCON0_BASIC3_H_SYNC(hsync));
169
170 /* Setup the polarity of the various signals */
171 if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
172 val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
173
174 if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
175 val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
176
177 regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
178 SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
179 val);
180
181 /* Map output pins to channel 0 */
182 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
183 SUN4I_TCON_GCTL_IOMAP_MASK,
184 SUN4I_TCON_GCTL_IOMAP_TCON0);
185
186 /* Enable the output on the pins */
187 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
188}
189EXPORT_SYMBOL(sun4i_tcon0_mode_set);
190
191void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
192 struct drm_display_mode *mode)
193{
194 unsigned int bp, hsync, vsync;
195 u8 clk_delay;
196 u32 val;
197
198 /* Adjust clock delay */
199 clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
200 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
201 SUN4I_TCON1_CTL_CLK_DELAY_MASK,
202 SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
203
204 /* Set interlaced mode */
205 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
206 val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
207 else
208 val = 0;
209 regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
210 SUN4I_TCON1_CTL_INTERLACE_ENABLE,
211 val);
212
213 /* Set the input resolution */
214 regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
215 SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
216 SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
217
218 /* Set the upscaling resolution */
219 regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
220 SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
221 SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
222
223 /* Set the output resolution */
224 regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
225 SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
226 SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
227
228 /* Set horizontal display timings */
229 bp = mode->crtc_htotal - mode->crtc_hsync_end;
230 DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
231 mode->htotal, bp);
232 regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
233 SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
234 SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
235
236 /* Set vertical display timings */
237 bp = mode->crtc_vtotal - mode->crtc_vsync_end;
238 DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
239 mode->vtotal, bp);
240 regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
241 SUN4I_TCON1_BASIC4_V_TOTAL(mode->vtotal) |
242 SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
243
244 /* Set Hsync and Vsync length */
245 hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
246 vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
247 DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
248 regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
249 SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
250 SUN4I_TCON1_BASIC5_H_SYNC(hsync));
251
252 /* Map output pins to channel 1 */
253 regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
254 SUN4I_TCON_GCTL_IOMAP_MASK,
255 SUN4I_TCON_GCTL_IOMAP_TCON1);
256
257 /*
258 * FIXME: Undocumented bits
259 */
260 if (tcon->has_mux)
261 regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, 1);
262}
263EXPORT_SYMBOL(sun4i_tcon1_mode_set);
264
265static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
266 struct sun4i_crtc *scrtc)
267{
268 unsigned long flags;
269
270 spin_lock_irqsave(&dev->event_lock, flags);
271 if (scrtc->event) {
272 drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
273 drm_crtc_vblank_put(&scrtc->crtc);
274 scrtc->event = NULL;
275 }
276 spin_unlock_irqrestore(&dev->event_lock, flags);
277}
278
279static irqreturn_t sun4i_tcon_handler(int irq, void *private)
280{
281 struct sun4i_tcon *tcon = private;
282 struct drm_device *drm = tcon->drm;
283 struct sun4i_drv *drv = drm->dev_private;
284 struct sun4i_crtc *scrtc = drv->crtc;
285 unsigned int status;
286
287 regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
288
289 if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
290 SUN4I_TCON_GINT0_VBLANK_INT(1))))
291 return IRQ_NONE;
292
293 drm_crtc_handle_vblank(&scrtc->crtc);
294 sun4i_tcon_finish_page_flip(drm, scrtc);
295
296 /* Acknowledge the interrupt */
297 regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
298 SUN4I_TCON_GINT0_VBLANK_INT(0) |
299 SUN4I_TCON_GINT0_VBLANK_INT(1),
300 0);
301
302 return IRQ_HANDLED;
303}
304
305static int sun4i_tcon_init_clocks(struct device *dev,
306 struct sun4i_tcon *tcon)
307{
308 tcon->clk = devm_clk_get(dev, "ahb");
309 if (IS_ERR(tcon->clk)) {
310 dev_err(dev, "Couldn't get the TCON bus clock\n");
311 return PTR_ERR(tcon->clk);
312 }
313 clk_prepare_enable(tcon->clk);
314
315 tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
316 if (IS_ERR(tcon->sclk0)) {
317 dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
318 return PTR_ERR(tcon->sclk0);
319 }
320
321 tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
322 if (IS_ERR(tcon->sclk1)) {
323 dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
324 return PTR_ERR(tcon->sclk1);
325 }
326
327 return sun4i_dclk_create(dev, tcon);
328}
329
330static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
331{
332 sun4i_dclk_free(tcon);
333 clk_disable_unprepare(tcon->clk);
334}
335
336static int sun4i_tcon_init_irq(struct device *dev,
337 struct sun4i_tcon *tcon)
338{
339 struct platform_device *pdev = to_platform_device(dev);
340 int irq, ret;
341
342 irq = platform_get_irq(pdev, 0);
343 if (irq < 0) {
344 dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
345 return irq;
346 }
347
348 ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
349 dev_name(dev), tcon);
350 if (ret) {
351 dev_err(dev, "Couldn't request the IRQ\n");
352 return ret;
353 }
354
355 return 0;
356}
357
358static struct regmap_config sun4i_tcon_regmap_config = {
359 .reg_bits = 32,
360 .val_bits = 32,
361 .reg_stride = 4,
362 .max_register = 0x800,
363};
364
365static int sun4i_tcon_init_regmap(struct device *dev,
366 struct sun4i_tcon *tcon)
367{
368 struct platform_device *pdev = to_platform_device(dev);
369 struct resource *res;
370 void __iomem *regs;
371
372 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
373 regs = devm_ioremap_resource(dev, res);
374 if (IS_ERR(regs)) {
375 dev_err(dev, "Couldn't map the TCON registers\n");
376 return PTR_ERR(regs);
377 }
378
379 tcon->regs = devm_regmap_init_mmio(dev, regs,
380 &sun4i_tcon_regmap_config);
381 if (IS_ERR(tcon->regs)) {
382 dev_err(dev, "Couldn't create the TCON regmap\n");
383 return PTR_ERR(tcon->regs);
384 }
385
386 /* Make sure the TCON is disabled and all IRQs are off */
387 regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
388 regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
389 regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
390
391 /* Disable IO lines and set them to tristate */
392 regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
393 regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
394
395 return 0;
396}
397
398static int sun4i_tcon_bind(struct device *dev, struct device *master,
399 void *data)
400{
401 struct drm_device *drm = data;
402 struct sun4i_drv *drv = drm->dev_private;
403 struct sun4i_tcon *tcon;
404 int ret;
405
406 tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
407 if (!tcon)
408 return -ENOMEM;
409 dev_set_drvdata(dev, tcon);
410 drv->tcon = tcon;
411 tcon->drm = drm;
412
413 if (of_device_is_compatible(dev->of_node, "allwinner,sun5i-a13-tcon"))
414 tcon->has_mux = true;
415
416 tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
417 if (IS_ERR(tcon->lcd_rst)) {
418 dev_err(dev, "Couldn't get our reset line\n");
419 return PTR_ERR(tcon->lcd_rst);
420 }
421
422 /* Make sure our TCON is reset */
423 if (!reset_control_status(tcon->lcd_rst))
424 reset_control_assert(tcon->lcd_rst);
425
426 ret = reset_control_deassert(tcon->lcd_rst);
427 if (ret) {
428 dev_err(dev, "Couldn't deassert our reset line\n");
429 return ret;
430 }
431
432 ret = sun4i_tcon_init_regmap(dev, tcon);
433 if (ret) {
434 dev_err(dev, "Couldn't init our TCON regmap\n");
435 goto err_assert_reset;
436 }
437
438 ret = sun4i_tcon_init_clocks(dev, tcon);
439 if (ret) {
440 dev_err(dev, "Couldn't init our TCON clocks\n");
441 goto err_assert_reset;
442 }
443
444 ret = sun4i_tcon_init_irq(dev, tcon);
445 if (ret) {
446 dev_err(dev, "Couldn't init our TCON interrupts\n");
447 goto err_free_clocks;
448 }
449
450 return 0;
451
452err_free_clocks:
453 sun4i_tcon_free_clocks(tcon);
454err_assert_reset:
455 reset_control_assert(tcon->lcd_rst);
456 return ret;
457}
458
459static void sun4i_tcon_unbind(struct device *dev, struct device *master,
460 void *data)
461{
462 struct sun4i_tcon *tcon = dev_get_drvdata(dev);
463
464 sun4i_tcon_free_clocks(tcon);
465}
466
467static struct component_ops sun4i_tcon_ops = {
468 .bind = sun4i_tcon_bind,
469 .unbind = sun4i_tcon_unbind,
470};
471
472static int sun4i_tcon_probe(struct platform_device *pdev)
473{
474 return component_add(&pdev->dev, &sun4i_tcon_ops);
475}
476
477static int sun4i_tcon_remove(struct platform_device *pdev)
478{
479 component_del(&pdev->dev, &sun4i_tcon_ops);
480
481 return 0;
482}
483
484static const struct of_device_id sun4i_tcon_of_table[] = {
485 { .compatible = "allwinner,sun5i-a13-tcon" },
486 { }
487};
488MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
489
490static struct platform_driver sun4i_tcon_platform_driver = {
491 .probe = sun4i_tcon_probe,
492 .remove = sun4i_tcon_remove,
493 .driver = {
494 .name = "sun4i-tcon",
495 .of_match_table = sun4i_tcon_of_table,
496 },
497};
498module_platform_driver(sun4i_tcon_platform_driver);
499
500MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
501MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
502MODULE_LICENSE("GPL");