2 * Renesas SH-mobile MIPI DSI support
4 * Copyright (C) 2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
11 #include <linux/bitmap.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 #include <linux/module.h>
23 #include <video/mipi_display.h>
24 #include <video/sh_mipi_dsi.h>
25 #include <video/sh_mobile_lcdc.h>
27 #define SYSCTRL 0x0000
28 #define SYSCONF 0x0004
30 #define RESREQSET0 0x0018
31 #define RESREQSET1 0x001c
32 #define HSTTOVSET 0x0020
33 #define LPRTOVSET 0x0024
34 #define TATOVSET 0x0028
35 #define PRTOVSET 0x002c
36 #define DSICTRL 0x0030
37 #define DSIINTE 0x0060
38 #define PHYCTRL 0x0070
40 /* relative to linkbase */
46 #define CMTSRTREQ 0x0070
47 #define CMTSRTCTR 0x00d0
49 /* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */
50 #define MAX_SH_MIPI_DSI 2
54 void __iomem *linkbase;
56 struct platform_device *pdev;
58 void *next_board_data;
59 void (*next_display_on)(void *board_data, struct fb_info *info);
60 void (*next_display_off)(void *board_data);
63 static struct sh_mipi *mipi_dsi[MAX_SH_MIPI_DSI];
65 /* Protect the above array */
66 static DEFINE_MUTEX(array_lock);
68 static struct sh_mipi *sh_mipi_by_handle(int handle)
70 if (handle >= ARRAY_SIZE(mipi_dsi) || handle < 0)
73 return mipi_dsi[handle];
76 static int sh_mipi_send_short(struct sh_mipi *mipi, u8 dsi_cmd,
79 u32 data = (dsi_cmd << 24) | (cmd << 16) | (param << 8);
82 /* transmit a short packet to LCD panel */
83 iowrite32(1 | data, mipi->linkbase + CMTSRTCTR);
84 iowrite32(1, mipi->linkbase + CMTSRTREQ);
86 while ((ioread32(mipi->linkbase + CMTSRTREQ) & 1) && --cnt)
89 return cnt ? 0 : -ETIMEDOUT;
92 #define LCD_CHAN2MIPI(c) ((c) < LCDC_CHAN_MAINLCD || (c) > LCDC_CHAN_SUBLCD ? \
95 static int sh_mipi_dcs(int handle, u8 cmd)
97 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
100 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE, cmd, 0);
103 static int sh_mipi_dcs_param(int handle, u8 cmd, u8 param)
105 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
108 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE_PARAM, cmd,
112 static void sh_mipi_dsi_enable(struct sh_mipi *mipi, bool enable)
115 * enable LCDC data tx, transition to LPS after completion of each HS
118 iowrite32(0x00000002 | enable, mipi->linkbase + DTCTR);
121 static void sh_mipi_shutdown(struct platform_device *pdev)
123 struct sh_mipi *mipi = platform_get_drvdata(pdev);
125 sh_mipi_dsi_enable(mipi, false);
128 static int __init sh_mipi_setup(struct sh_mipi *mipi,
129 struct sh_mipi_dsi_info *pdata)
131 void __iomem *base = mipi->base;
132 struct sh_mobile_lcdc_chan_cfg *ch = pdata->lcd_chan;
133 u32 pctype, datatype, pixfmt, linelength, vmctr2;
134 u32 tmp, top, bottom, delay, div;
139 * Select data format. MIPI DSI is not hot-pluggable, so, we just use
140 * the default videomode. If this ever becomes a problem, We'll have to
141 * move this to mipi_display_on() above and use info->var.xres
143 switch (pdata->data_format) {
146 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
147 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
148 linelength = ch->lcd_cfg[0].xres * 3;
153 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
154 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
155 linelength = ch->lcd_cfg[0].xres * 2;
160 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
161 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
162 linelength = ch->lcd_cfg[0].xres * 3;
167 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
168 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
169 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
174 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
175 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
176 linelength = ch->lcd_cfg[0].xres * 3;
181 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
182 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
183 linelength = ch->lcd_cfg[0].xres * 2;
188 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
189 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
190 linelength = ch->lcd_cfg[0].xres * 3;
195 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
196 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
197 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
202 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
203 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
204 linelength = ch->lcd_cfg[0].xres * 2;
209 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
210 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
211 linelength = ch->lcd_cfg[0].xres * 2;
216 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
217 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
218 linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8;
223 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
224 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
225 /* Length of U/V line */
226 linelength = (ch->lcd_cfg[0].xres + 1) / 2;
233 if ((yuv && ch->interface_type != YUV422) ||
234 (!yuv && ch->interface_type != RGB24))
241 iowrite32(0x00000001, base + SYSCTRL);
242 /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
244 iowrite32(0x00000000, base + SYSCTRL);
255 iowrite32(0x70003332, base + TIMSET);
256 /* no responses requested */
257 iowrite32(0x00000000, base + RESREQSET0);
258 /* request response to packets of type 0x28 */
259 iowrite32(0x00000100, base + RESREQSET1);
260 /* High-speed transmission timeout, default 0xffffffff */
261 iowrite32(0x0fffffff, base + HSTTOVSET);
262 /* LP reception timeout, default 0xffffffff */
263 iowrite32(0x0fffffff, base + LPRTOVSET);
264 /* Turn-around timeout, default 0xffffffff */
265 iowrite32(0x0fffffff, base + TATOVSET);
266 /* Peripheral reset timeout, default 0xffffffff */
267 iowrite32(0x0fffffff, base + PRTOVSET);
268 /* Interrupts not used, disable all */
269 iowrite32(0, base + DSIINTE);
271 iowrite32(0x00000001, base + PHYCTRL);
273 /* Deassert resets, power on */
274 iowrite32(0x03070001, base + PHYCTRL);
277 * Default = ULPS enable |
278 * Contention detection enabled |
279 * EoT packet transmission enable |
283 bitmap_fill((unsigned long *)&tmp, pdata->lane);
285 iowrite32(tmp, base + SYSCONF);
290 * Enable transmission of all packets,
291 * transmit LPS after each HS packet completion
293 iowrite32(0x00000006, mipi->linkbase + DTCTR);
294 /* VSYNC width = 2 (<< 17) */
295 iowrite32((ch->lcd_cfg[0].vsync_len << pdata->vsynw_offset) |
296 (pdata->clksrc << 16) | (pctype << 12) | datatype,
297 mipi->linkbase + VMCTR1);
300 * Non-burst mode with sync pulses: VSE and HSE are output,
301 * HSA period allowed, no commands in LP
304 if (pdata->flags & SH_MIPI_DSI_VSEE)
306 if (pdata->flags & SH_MIPI_DSI_HSEE)
308 if (pdata->flags & SH_MIPI_DSI_HSAE)
310 if (pdata->flags & SH_MIPI_DSI_BL2E)
312 if (pdata->flags & SH_MIPI_DSI_HSABM)
314 if (pdata->flags & SH_MIPI_DSI_HBPBM)
316 if (pdata->flags & SH_MIPI_DSI_HFPBM)
318 iowrite32(vmctr2, mipi->linkbase + VMCTR2);
321 * VMLEN1 = RGBLEN | HSALEN
324 * Video mode - Blanking Packet setting
326 top = linelength << 16; /* RGBLEN */
328 if (pdata->flags & SH_MIPI_DSI_HSABM) /* HSALEN */
329 bottom = (pdata->lane * ch->lcd_cfg[0].hsync_len) - 10;
330 iowrite32(top | bottom , mipi->linkbase + VMLEN1);
333 * VMLEN2 = HBPLEN | HFPLEN
336 * Video mode - Blanking Packet setting
342 div = 1; /* HSbyteCLK is calculation base
343 * HS4divCLK = HSbyteCLK/2
344 * HS6divCLK is not supported for now */
345 if (pdata->flags & SH_MIPI_DSI_HS4divCLK)
348 if (pdata->flags & SH_MIPI_DSI_HFPBM) { /* HBPLEN */
349 top = ch->lcd_cfg[0].hsync_len + ch->lcd_cfg[0].left_margin;
350 top = ((pdata->lane * top / div) - 10) << 16;
352 if (pdata->flags & SH_MIPI_DSI_HBPBM) { /* HFPLEN */
353 bottom = ch->lcd_cfg[0].right_margin;
354 bottom = (pdata->lane * bottom / div) - 12;
357 bpp = linelength / ch->lcd_cfg[0].xres; /* byte / pixel */
358 if ((pdata->lane / div) > bpp) {
359 tmp = ch->lcd_cfg[0].xres / bpp; /* output cycle */
360 tmp = ch->lcd_cfg[0].xres - tmp; /* (input - output) cycle */
361 delay = (pdata->lane * tmp);
364 iowrite32(top | (bottom + delay) , mipi->linkbase + VMLEN2);
368 /* setup LCD panel */
370 /* cf. drivers/video/omap/lcd_mipid.c */
371 sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE);
374 * [7] - Page Address Mode
375 * [6] - Column Address Mode
376 * [5] - Page / Column Address Mode
377 * [4] - Display Device Line Refresh Order
378 * [3] - RGB/BGR Order
379 * [2] - Display Data Latch Data Order
380 * [1] - Flip Horizontal
381 * [0] - Flip Vertical
383 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
384 /* cf. set_data_lines() */
385 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_PIXEL_FORMAT,
387 sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON);
389 /* Enable timeout counters */
390 iowrite32(0x00000f00, base + DSICTRL);
395 static void mipi_display_on(void *arg, struct fb_info *info)
397 struct sh_mipi *mipi = arg;
398 struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data;
401 pm_runtime_get_sync(&mipi->pdev->dev);
403 ret = pdata->set_dot_clock(mipi->pdev, mipi->base, 1);
405 goto mipi_display_on_fail1;
407 ret = sh_mipi_setup(mipi, pdata);
409 goto mipi_display_on_fail2;
411 sh_mipi_dsi_enable(mipi, true);
413 if (mipi->next_display_on)
414 mipi->next_display_on(mipi->next_board_data, info);
418 mipi_display_on_fail1:
419 pm_runtime_put_sync(&mipi->pdev->dev);
420 mipi_display_on_fail2:
421 pdata->set_dot_clock(mipi->pdev, mipi->base, 0);
424 static void mipi_display_off(void *arg)
426 struct sh_mipi *mipi = arg;
427 struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data;
429 if (mipi->next_display_off)
430 mipi->next_display_off(mipi->next_board_data);
432 sh_mipi_dsi_enable(mipi, false);
434 pdata->set_dot_clock(mipi->pdev, mipi->base, 0);
436 pm_runtime_put_sync(&mipi->pdev->dev);
439 static int __init sh_mipi_probe(struct platform_device *pdev)
441 struct sh_mipi *mipi;
442 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
443 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
444 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
445 unsigned long rate, f_current;
446 int idx = pdev->id, ret;
448 if (!res || !res2 || idx >= ARRAY_SIZE(mipi_dsi) || !pdata)
451 if (!pdata->set_dot_clock)
454 mutex_lock(&array_lock);
456 for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++)
459 if (idx == ARRAY_SIZE(mipi_dsi)) {
464 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
470 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
471 dev_err(&pdev->dev, "MIPI register region already claimed\n");
476 mipi->base = ioremap(res->start, resource_size(res));
482 if (!request_mem_region(res2->start, resource_size(res2), pdev->name)) {
483 dev_err(&pdev->dev, "MIPI register region 2 already claimed\n");
488 mipi->linkbase = ioremap(res2->start, resource_size(res2));
489 if (!mipi->linkbase) {
496 mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk");
497 if (IS_ERR(mipi->dsit_clk)) {
498 ret = PTR_ERR(mipi->dsit_clk);
502 f_current = clk_get_rate(mipi->dsit_clk);
503 /* 80MHz required by the datasheet */
504 rate = clk_round_rate(mipi->dsit_clk, 80000000);
505 if (rate > 0 && rate != f_current)
506 ret = clk_set_rate(mipi->dsit_clk, rate);
512 dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate);
514 ret = clk_enable(mipi->dsit_clk);
518 mipi_dsi[idx] = mipi;
520 pm_runtime_enable(&pdev->dev);
521 pm_runtime_resume(&pdev->dev);
523 mutex_unlock(&array_lock);
524 platform_set_drvdata(pdev, mipi);
526 /* Save original LCDC callbacks */
527 mipi->next_board_data = pdata->lcd_chan->board_cfg.board_data;
528 mipi->next_display_on = pdata->lcd_chan->board_cfg.display_on;
529 mipi->next_display_off = pdata->lcd_chan->board_cfg.display_off;
531 /* Set up LCDC callbacks */
532 pdata->lcd_chan->board_cfg.board_data = mipi;
533 pdata->lcd_chan->board_cfg.display_on = mipi_display_on;
534 pdata->lcd_chan->board_cfg.display_off = mipi_display_off;
535 pdata->lcd_chan->board_cfg.owner = THIS_MODULE;
541 clk_put(mipi->dsit_clk);
543 iounmap(mipi->linkbase);
545 release_mem_region(res2->start, resource_size(res2));
549 release_mem_region(res->start, resource_size(res));
554 mutex_unlock(&array_lock);
559 static int __exit sh_mipi_remove(struct platform_device *pdev)
561 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
562 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
563 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
564 struct sh_mipi *mipi = platform_get_drvdata(pdev);
567 mutex_lock(&array_lock);
569 for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++)
572 if (i == ARRAY_SIZE(mipi_dsi)) {
579 mutex_unlock(&array_lock);
584 pdata->lcd_chan->board_cfg.owner = NULL;
585 pdata->lcd_chan->board_cfg.display_on = NULL;
586 pdata->lcd_chan->board_cfg.display_off = NULL;
587 pdata->lcd_chan->board_cfg.board_data = NULL;
589 pm_runtime_disable(&pdev->dev);
590 clk_disable(mipi->dsit_clk);
591 clk_put(mipi->dsit_clk);
593 iounmap(mipi->linkbase);
595 release_mem_region(res2->start, resource_size(res2));
598 release_mem_region(res->start, resource_size(res));
599 platform_set_drvdata(pdev, NULL);
605 static struct platform_driver sh_mipi_driver = {
606 .remove = __exit_p(sh_mipi_remove),
607 .shutdown = sh_mipi_shutdown,
609 .name = "sh-mipi-dsi",
613 static int __init sh_mipi_init(void)
615 return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe);
617 module_init(sh_mipi_init);
619 static void __exit sh_mipi_exit(void)
621 platform_driver_unregister(&sh_mipi_driver);
623 module_exit(sh_mipi_exit);
625 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
626 MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
627 MODULE_LICENSE("GPL v2");