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/clk.h>
12 #include <linux/delay.h>
13 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
21 #include <video/mipi_display.h>
22 #include <video/sh_mipi_dsi.h>
23 #include <video/sh_mobile_lcdc.h>
25 #define SYSCTRL 0x0000
26 #define SYSCONF 0x0004
28 #define RESREQSET0 0x0018
29 #define RESREQSET1 0x001c
30 #define HSTTOVSET 0x0020
31 #define LPRTOVSET 0x0024
32 #define TATOVSET 0x0028
33 #define PRTOVSET 0x002c
34 #define DSICTRL 0x0030
35 #define DSIINTE 0x0060
36 #define PHYCTRL 0x0070
38 /* relative to linkbase */
43 #define CMTSRTREQ 0x0070
44 #define CMTSRTCTR 0x00d0
46 /* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */
47 #define MAX_SH_MIPI_DSI 2
51 void __iomem *linkbase;
56 void *next_board_data;
57 void (*next_display_on)(void *board_data, struct fb_info *info);
58 void (*next_display_off)(void *board_data);
61 static struct sh_mipi *mipi_dsi[MAX_SH_MIPI_DSI];
63 /* Protect the above array */
64 static DEFINE_MUTEX(array_lock);
66 static struct sh_mipi *sh_mipi_by_handle(int handle)
68 if (handle >= ARRAY_SIZE(mipi_dsi) || handle < 0)
71 return mipi_dsi[handle];
74 static int sh_mipi_send_short(struct sh_mipi *mipi, u8 dsi_cmd,
77 u32 data = (dsi_cmd << 24) | (cmd << 16) | (param << 8);
80 /* transmit a short packet to LCD panel */
81 iowrite32(1 | data, mipi->linkbase + CMTSRTCTR);
82 iowrite32(1, mipi->linkbase + CMTSRTREQ);
84 while ((ioread32(mipi->linkbase + CMTSRTREQ) & 1) && --cnt)
87 return cnt ? 0 : -ETIMEDOUT;
90 #define LCD_CHAN2MIPI(c) ((c) < LCDC_CHAN_MAINLCD || (c) > LCDC_CHAN_SUBLCD ? \
93 static int sh_mipi_dcs(int handle, u8 cmd)
95 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
98 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE, cmd, 0);
101 static int sh_mipi_dcs_param(int handle, u8 cmd, u8 param)
103 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
106 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE_PARAM, cmd,
110 static void sh_mipi_dsi_enable(struct sh_mipi *mipi, bool enable)
113 * enable LCDC data tx, transition to LPS after completion of each HS
116 iowrite32(0x00000002 | enable, mipi->linkbase + DTCTR);
119 static void sh_mipi_shutdown(struct platform_device *pdev)
121 struct sh_mipi *mipi = platform_get_drvdata(pdev);
123 sh_mipi_dsi_enable(mipi, false);
126 static void mipi_display_on(void *arg, struct fb_info *info)
128 struct sh_mipi *mipi = arg;
130 pm_runtime_get_sync(mipi->dev);
131 sh_mipi_dsi_enable(mipi, true);
133 if (mipi->next_display_on)
134 mipi->next_display_on(mipi->next_board_data, info);
137 static void mipi_display_off(void *arg)
139 struct sh_mipi *mipi = arg;
141 if (mipi->next_display_off)
142 mipi->next_display_off(mipi->next_board_data);
144 sh_mipi_dsi_enable(mipi, false);
145 pm_runtime_put(mipi->dev);
148 static int __init sh_mipi_setup(struct sh_mipi *mipi,
149 struct sh_mipi_dsi_info *pdata)
151 void __iomem *base = mipi->base;
152 struct sh_mobile_lcdc_chan_cfg *ch = pdata->lcd_chan;
153 u32 pctype, datatype, pixfmt, linelength, vmctr2 = 0x00e00000;
157 * Select data format. MIPI DSI is not hot-pluggable, so, we just use
158 * the default videomode. If this ever becomes a problem, We'll have to
159 * move this to mipi_display_on() above and use info->var.xres
161 switch (pdata->data_format) {
164 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
165 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
166 linelength = ch->lcd_cfg[0].xres * 3;
171 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
172 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
173 linelength = ch->lcd_cfg[0].xres * 2;
178 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
179 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
180 linelength = ch->lcd_cfg[0].xres * 3;
185 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
186 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
187 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
192 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
193 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
194 linelength = ch->lcd_cfg[0].xres * 3;
199 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
200 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
201 linelength = ch->lcd_cfg[0].xres * 2;
206 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
207 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
208 linelength = ch->lcd_cfg[0].xres * 3;
213 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
214 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
215 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
220 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
221 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
222 linelength = ch->lcd_cfg[0].xres * 2;
227 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
228 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
229 linelength = ch->lcd_cfg[0].xres * 2;
234 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
235 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
236 linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8;
241 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
242 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
243 /* Length of U/V line */
244 linelength = (ch->lcd_cfg[0].xres + 1) / 2;
251 if ((yuv && ch->interface_type != YUV422) ||
252 (!yuv && ch->interface_type != RGB24))
256 iowrite32(0x00000001, base + SYSCTRL);
257 /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
259 iowrite32(0x00000000, base + SYSCTRL);
264 * Default = ULPS enable |
265 * Contention detection enabled |
266 * EoT packet transmission enable |
269 * additionally enable first two lanes
271 iowrite32(0x00003703, base + SYSCONF);
279 iowrite32(0x70003332, base + TIMSET);
280 /* no responses requested */
281 iowrite32(0x00000000, base + RESREQSET0);
282 /* request response to packets of type 0x28 */
283 iowrite32(0x00000100, base + RESREQSET1);
284 /* High-speed transmission timeout, default 0xffffffff */
285 iowrite32(0x0fffffff, base + HSTTOVSET);
286 /* LP reception timeout, default 0xffffffff */
287 iowrite32(0x0fffffff, base + LPRTOVSET);
288 /* Turn-around timeout, default 0xffffffff */
289 iowrite32(0x0fffffff, base + TATOVSET);
290 /* Peripheral reset timeout, default 0xffffffff */
291 iowrite32(0x0fffffff, base + PRTOVSET);
292 /* Enable timeout counters */
293 iowrite32(0x00000f00, base + DSICTRL);
294 /* Interrupts not used, disable all */
295 iowrite32(0, base + DSIINTE);
297 iowrite32(0x00000001, base + PHYCTRL);
299 /* Deassert resets, power on, set multiplier */
300 iowrite32(0x03070b01, base + PHYCTRL);
305 * Enable transmission of all packets,
306 * transmit LPS after each HS packet completion
308 iowrite32(0x00000006, mipi->linkbase + DTCTR);
309 /* VSYNC width = 2 (<< 17) */
310 iowrite32((ch->lcd_cfg[0].vsync_len << pdata->vsynw_offset) |
311 (pdata->clksrc << 16) | (pctype << 12) | datatype,
312 mipi->linkbase + VMCTR1);
315 * Non-burst mode with sync pulses: VSE and HSE are output,
316 * HSA period allowed, no commands in LP
318 if (pdata->flags & SH_MIPI_DSI_HSABM)
320 if (pdata->flags & SH_MIPI_DSI_HSPBM)
322 iowrite32(vmctr2, mipi->linkbase + VMCTR2);
325 * 0x660 = 1632 bytes per line (RGB24, 544 pixels: see
326 * sh_mobile_lcdc_info.ch[0].lcd_cfg[0].xres), HSALEN = 1 - default
327 * (unused if VMCTR2[HSABM] = 0)
329 iowrite32(1 | (linelength << 16), mipi->linkbase + VMLEN1);
333 /* setup LCD panel */
335 /* cf. drivers/video/omap/lcd_mipid.c */
336 sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE);
339 * [7] - Page Address Mode
340 * [6] - Column Address Mode
341 * [5] - Page / Column Address Mode
342 * [4] - Display Device Line Refresh Order
343 * [3] - RGB/BGR Order
344 * [2] - Display Data Latch Data Order
345 * [1] - Flip Horizontal
346 * [0] - Flip Vertical
348 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
349 /* cf. set_data_lines() */
350 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_PIXEL_FORMAT,
352 sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON);
357 static int __init sh_mipi_probe(struct platform_device *pdev)
359 struct sh_mipi *mipi;
360 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
361 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
362 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
363 unsigned long rate, f_current;
364 int idx = pdev->id, ret;
365 char dsip_clk[] = "dsi.p_clk";
367 if (!res || !res2 || idx >= ARRAY_SIZE(mipi_dsi) || !pdata)
370 mutex_lock(&array_lock);
372 for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++)
375 if (idx == ARRAY_SIZE(mipi_dsi)) {
380 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
386 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
387 dev_err(&pdev->dev, "MIPI register region already claimed\n");
392 mipi->base = ioremap(res->start, resource_size(res));
398 if (!request_mem_region(res2->start, resource_size(res2), pdev->name)) {
399 dev_err(&pdev->dev, "MIPI register region 2 already claimed\n");
404 mipi->linkbase = ioremap(res2->start, resource_size(res2));
405 if (!mipi->linkbase) {
410 mipi->dev = &pdev->dev;
412 mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk");
413 if (IS_ERR(mipi->dsit_clk)) {
414 ret = PTR_ERR(mipi->dsit_clk);
418 f_current = clk_get_rate(mipi->dsit_clk);
419 /* 80MHz required by the datasheet */
420 rate = clk_round_rate(mipi->dsit_clk, 80000000);
421 if (rate > 0 && rate != f_current)
422 ret = clk_set_rate(mipi->dsit_clk, rate);
428 dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate);
430 sprintf(dsip_clk, "dsi%1.1dp_clk", idx);
431 mipi->dsip_clk = clk_get(&pdev->dev, dsip_clk);
432 if (IS_ERR(mipi->dsip_clk)) {
433 ret = PTR_ERR(mipi->dsip_clk);
437 f_current = clk_get_rate(mipi->dsip_clk);
438 /* Between 10 and 50MHz */
439 rate = clk_round_rate(mipi->dsip_clk, 24000000);
440 if (rate > 0 && rate != f_current)
441 ret = clk_set_rate(mipi->dsip_clk, rate);
447 dev_dbg(&pdev->dev, "DSI-P clk %lu -> %lu\n", f_current, rate);
451 ret = clk_enable(mipi->dsit_clk);
455 ret = clk_enable(mipi->dsip_clk);
459 mipi_dsi[idx] = mipi;
461 pm_runtime_enable(&pdev->dev);
462 pm_runtime_resume(&pdev->dev);
464 ret = sh_mipi_setup(mipi, pdata);
468 mutex_unlock(&array_lock);
469 platform_set_drvdata(pdev, mipi);
471 /* Save original LCDC callbacks */
472 mipi->next_board_data = pdata->lcd_chan->board_cfg.board_data;
473 mipi->next_display_on = pdata->lcd_chan->board_cfg.display_on;
474 mipi->next_display_off = pdata->lcd_chan->board_cfg.display_off;
476 /* Set up LCDC callbacks */
477 pdata->lcd_chan->board_cfg.board_data = mipi;
478 pdata->lcd_chan->board_cfg.display_on = mipi_display_on;
479 pdata->lcd_chan->board_cfg.display_off = mipi_display_off;
480 pdata->lcd_chan->board_cfg.owner = THIS_MODULE;
485 mipi_dsi[idx] = NULL;
486 pm_runtime_disable(&pdev->dev);
487 clk_disable(mipi->dsip_clk);
489 clk_disable(mipi->dsit_clk);
492 clk_put(mipi->dsip_clk);
495 clk_put(mipi->dsit_clk);
497 iounmap(mipi->linkbase);
499 release_mem_region(res2->start, resource_size(res2));
503 release_mem_region(res->start, resource_size(res));
508 mutex_unlock(&array_lock);
513 static int __exit sh_mipi_remove(struct platform_device *pdev)
515 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
516 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
517 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
518 struct sh_mipi *mipi = platform_get_drvdata(pdev);
521 mutex_lock(&array_lock);
523 for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++)
526 if (i == ARRAY_SIZE(mipi_dsi)) {
533 mutex_unlock(&array_lock);
538 pdata->lcd_chan->board_cfg.owner = NULL;
539 pdata->lcd_chan->board_cfg.display_on = NULL;
540 pdata->lcd_chan->board_cfg.display_off = NULL;
541 pdata->lcd_chan->board_cfg.board_data = NULL;
543 pm_runtime_disable(&pdev->dev);
544 clk_disable(mipi->dsip_clk);
545 clk_disable(mipi->dsit_clk);
546 clk_put(mipi->dsit_clk);
547 clk_put(mipi->dsip_clk);
548 iounmap(mipi->linkbase);
550 release_mem_region(res2->start, resource_size(res2));
553 release_mem_region(res->start, resource_size(res));
554 platform_set_drvdata(pdev, NULL);
560 static struct platform_driver sh_mipi_driver = {
561 .remove = __exit_p(sh_mipi_remove),
562 .shutdown = sh_mipi_shutdown,
564 .name = "sh-mipi-dsi",
568 static int __init sh_mipi_init(void)
570 return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe);
572 module_init(sh_mipi_init);
574 static void __exit sh_mipi_exit(void)
576 platform_driver_unregister(&sh_mipi_driver);
578 module_exit(sh_mipi_exit);
580 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
581 MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
582 MODULE_LICENSE("GPL v2");