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 #include "sh_mobile_lcdcfb.h"
29 #define SYSCTRL 0x0000
30 #define SYSCONF 0x0004
32 #define RESREQSET0 0x0018
33 #define RESREQSET1 0x001c
34 #define HSTTOVSET 0x0020
35 #define LPRTOVSET 0x0024
36 #define TATOVSET 0x0028
37 #define PRTOVSET 0x002c
38 #define DSICTRL 0x0030
39 #define DSIINTE 0x0060
40 #define PHYCTRL 0x0070
42 /* relative to linkbase */
48 #define CMTSRTREQ 0x0070
49 #define CMTSRTCTR 0x00d0
51 /* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */
52 #define MAX_SH_MIPI_DSI 2
55 struct sh_mobile_lcdc_entity entity
;
58 void __iomem
*linkbase
;
60 struct platform_device
*pdev
;
63 #define to_sh_mipi(e) container_of(e, struct sh_mipi, entity)
65 static struct sh_mipi
*mipi_dsi
[MAX_SH_MIPI_DSI
];
67 /* Protect the above array */
68 static DEFINE_MUTEX(array_lock
);
70 static struct sh_mipi
*sh_mipi_by_handle(int handle
)
72 if (handle
>= ARRAY_SIZE(mipi_dsi
) || handle
< 0)
75 return mipi_dsi
[handle
];
78 static int sh_mipi_send_short(struct sh_mipi
*mipi
, u8 dsi_cmd
,
81 u32 data
= (dsi_cmd
<< 24) | (cmd
<< 16) | (param
<< 8);
84 /* transmit a short packet to LCD panel */
85 iowrite32(1 | data
, mipi
->linkbase
+ CMTSRTCTR
);
86 iowrite32(1, mipi
->linkbase
+ CMTSRTREQ
);
88 while ((ioread32(mipi
->linkbase
+ CMTSRTREQ
) & 1) && --cnt
)
91 return cnt
? 0 : -ETIMEDOUT
;
94 #define LCD_CHAN2MIPI(c) ((c) < LCDC_CHAN_MAINLCD || (c) > LCDC_CHAN_SUBLCD ? \
97 static int sh_mipi_dcs(int handle
, u8 cmd
)
99 struct sh_mipi
*mipi
= sh_mipi_by_handle(LCD_CHAN2MIPI(handle
));
102 return sh_mipi_send_short(mipi
, MIPI_DSI_DCS_SHORT_WRITE
, cmd
, 0);
105 static int sh_mipi_dcs_param(int handle
, u8 cmd
, u8 param
)
107 struct sh_mipi
*mipi
= sh_mipi_by_handle(LCD_CHAN2MIPI(handle
));
110 return sh_mipi_send_short(mipi
, MIPI_DSI_DCS_SHORT_WRITE_PARAM
, cmd
,
114 static void sh_mipi_dsi_enable(struct sh_mipi
*mipi
, bool enable
)
117 * enable LCDC data tx, transition to LPS after completion of each HS
120 iowrite32(0x00000002 | enable
, mipi
->linkbase
+ DTCTR
);
123 static void sh_mipi_shutdown(struct platform_device
*pdev
)
125 struct sh_mipi
*mipi
= to_sh_mipi(platform_get_drvdata(pdev
));
127 sh_mipi_dsi_enable(mipi
, false);
130 static int sh_mipi_setup(struct sh_mipi
*mipi
, const struct fb_videomode
*mode
)
132 void __iomem
*base
= mipi
->base
;
133 struct sh_mipi_dsi_info
*pdata
= mipi
->pdev
->dev
.platform_data
;
134 u32 pctype
, datatype
, pixfmt
, linelength
, vmctr2
;
135 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
= mode
->xres
* 3;
152 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_16
;
153 pixfmt
= MIPI_DCS_PIXEL_FMT_16BIT
;
154 linelength
= mode
->xres
* 2;
158 datatype
= MIPI_DSI_PIXEL_STREAM_3BYTE_18
;
159 pixfmt
= MIPI_DCS_PIXEL_FMT_24BIT
;
160 linelength
= mode
->xres
* 3;
164 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_18
;
165 pixfmt
= MIPI_DCS_PIXEL_FMT_18BIT
;
166 linelength
= (mode
->xres
* 18 + 7) / 8;
170 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_24
;
171 pixfmt
= MIPI_DCS_PIXEL_FMT_24BIT
;
172 linelength
= mode
->xres
* 3;
176 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_16
;
177 pixfmt
= MIPI_DCS_PIXEL_FMT_16BIT
;
178 linelength
= mode
->xres
* 2;
182 datatype
= MIPI_DSI_PIXEL_STREAM_3BYTE_18
;
183 pixfmt
= MIPI_DCS_PIXEL_FMT_24BIT
;
184 linelength
= mode
->xres
* 3;
188 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_18
;
189 pixfmt
= MIPI_DCS_PIXEL_FMT_18BIT
;
190 linelength
= (mode
->xres
* 18 + 7) / 8;
194 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16
;
195 pixfmt
= MIPI_DCS_PIXEL_FMT_16BIT
;
196 linelength
= mode
->xres
* 2;
200 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16
;
201 pixfmt
= MIPI_DCS_PIXEL_FMT_16BIT
;
202 linelength
= mode
->xres
* 2;
206 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12
;
207 pixfmt
= MIPI_DCS_PIXEL_FMT_12BIT
;
208 linelength
= (mode
->xres
* 12 + 7) / 8;
212 datatype
= MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12
;
213 pixfmt
= MIPI_DCS_PIXEL_FMT_12BIT
;
214 /* Length of U/V line */
215 linelength
= (mode
->xres
+ 1) / 2;
225 iowrite32(0x00000001, base
+ SYSCTRL
);
226 /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
228 iowrite32(0x00000000, base
+ SYSCTRL
);
239 iowrite32(0x70003332, base
+ TIMSET
);
240 /* no responses requested */
241 iowrite32(0x00000000, base
+ RESREQSET0
);
242 /* request response to packets of type 0x28 */
243 iowrite32(0x00000100, base
+ RESREQSET1
);
244 /* High-speed transmission timeout, default 0xffffffff */
245 iowrite32(0x0fffffff, base
+ HSTTOVSET
);
246 /* LP reception timeout, default 0xffffffff */
247 iowrite32(0x0fffffff, base
+ LPRTOVSET
);
248 /* Turn-around timeout, default 0xffffffff */
249 iowrite32(0x0fffffff, base
+ TATOVSET
);
250 /* Peripheral reset timeout, default 0xffffffff */
251 iowrite32(0x0fffffff, base
+ PRTOVSET
);
252 /* Interrupts not used, disable all */
253 iowrite32(0, base
+ DSIINTE
);
255 iowrite32(0x00000001, base
+ PHYCTRL
);
257 /* Deassert resets, power on */
258 iowrite32(0x03070001 | pdata
->phyctrl
, base
+ PHYCTRL
);
261 * Default = ULPS enable |
262 * Contention detection enabled |
263 * EoT packet transmission enable |
267 bitmap_fill((unsigned long *)&tmp
, pdata
->lane
);
269 iowrite32(tmp
, base
+ SYSCONF
);
274 * Enable transmission of all packets,
275 * transmit LPS after each HS packet completion
277 iowrite32(0x00000006, mipi
->linkbase
+ DTCTR
);
278 /* VSYNC width = 2 (<< 17) */
279 iowrite32((mode
->vsync_len
<< pdata
->vsynw_offset
) |
280 (pdata
->clksrc
<< 16) | (pctype
<< 12) | datatype
,
281 mipi
->linkbase
+ VMCTR1
);
284 * Non-burst mode with sync pulses: VSE and HSE are output,
285 * HSA period allowed, no commands in LP
288 if (pdata
->flags
& SH_MIPI_DSI_VSEE
)
290 if (pdata
->flags
& SH_MIPI_DSI_HSEE
)
292 if (pdata
->flags
& SH_MIPI_DSI_HSAE
)
294 if (pdata
->flags
& SH_MIPI_DSI_BL2E
)
296 if (pdata
->flags
& SH_MIPI_DSI_HSABM
)
298 if (pdata
->flags
& SH_MIPI_DSI_HBPBM
)
300 if (pdata
->flags
& SH_MIPI_DSI_HFPBM
)
302 iowrite32(vmctr2
, mipi
->linkbase
+ VMCTR2
);
305 * VMLEN1 = RGBLEN | HSALEN
308 * Video mode - Blanking Packet setting
310 top
= linelength
<< 16; /* RGBLEN */
312 if (pdata
->flags
& SH_MIPI_DSI_HSABM
) /* HSALEN */
313 bottom
= (pdata
->lane
* mode
->hsync_len
) - 10;
314 iowrite32(top
| bottom
, mipi
->linkbase
+ VMLEN1
);
317 * VMLEN2 = HBPLEN | HFPLEN
320 * Video mode - Blanking Packet setting
326 div
= 1; /* HSbyteCLK is calculation base
327 * HS4divCLK = HSbyteCLK/2
328 * HS6divCLK is not supported for now */
329 if (pdata
->flags
& SH_MIPI_DSI_HS4divCLK
)
332 if (pdata
->flags
& SH_MIPI_DSI_HFPBM
) { /* HBPLEN */
333 top
= mode
->hsync_len
+ mode
->left_margin
;
334 top
= ((pdata
->lane
* top
/ div
) - 10) << 16;
336 if (pdata
->flags
& SH_MIPI_DSI_HBPBM
) { /* HFPLEN */
337 bottom
= mode
->right_margin
;
338 bottom
= (pdata
->lane
* bottom
/ div
) - 12;
341 bpp
= linelength
/ mode
->xres
; /* byte / pixel */
342 if ((pdata
->lane
/ div
) > bpp
) {
343 tmp
= mode
->xres
/ bpp
; /* output cycle */
344 tmp
= mode
->xres
- tmp
; /* (input - output) cycle */
345 delay
= (pdata
->lane
* tmp
);
348 iowrite32(top
| (bottom
+ delay
) , mipi
->linkbase
+ VMLEN2
);
352 /* setup LCD panel */
354 /* cf. drivers/video/omap/lcd_mipid.c */
355 sh_mipi_dcs(pdata
->channel
, MIPI_DCS_EXIT_SLEEP_MODE
);
358 * [7] - Page Address Mode
359 * [6] - Column Address Mode
360 * [5] - Page / Column Address Mode
361 * [4] - Display Device Line Refresh Order
362 * [3] - RGB/BGR Order
363 * [2] - Display Data Latch Data Order
364 * [1] - Flip Horizontal
365 * [0] - Flip Vertical
367 sh_mipi_dcs_param(pdata
->channel
, MIPI_DCS_SET_ADDRESS_MODE
, 0x00);
368 /* cf. set_data_lines() */
369 sh_mipi_dcs_param(pdata
->channel
, MIPI_DCS_SET_PIXEL_FORMAT
,
371 sh_mipi_dcs(pdata
->channel
, MIPI_DCS_SET_DISPLAY_ON
);
373 /* Enable timeout counters */
374 iowrite32(0x00000f00, base
+ DSICTRL
);
379 static int mipi_display_on(struct sh_mobile_lcdc_entity
*entity
)
381 struct sh_mipi
*mipi
= to_sh_mipi(entity
);
382 struct sh_mipi_dsi_info
*pdata
= mipi
->pdev
->dev
.platform_data
;
385 pm_runtime_get_sync(&mipi
->pdev
->dev
);
387 ret
= pdata
->set_dot_clock(mipi
->pdev
, mipi
->base
, 1);
389 goto mipi_display_on_fail1
;
391 ret
= sh_mipi_setup(mipi
, &entity
->def_mode
);
393 goto mipi_display_on_fail2
;
395 sh_mipi_dsi_enable(mipi
, true);
397 return SH_MOBILE_LCDC_DISPLAY_CONNECTED
;
399 mipi_display_on_fail1
:
400 pm_runtime_put_sync(&mipi
->pdev
->dev
);
401 mipi_display_on_fail2
:
402 pdata
->set_dot_clock(mipi
->pdev
, mipi
->base
, 0);
407 static void mipi_display_off(struct sh_mobile_lcdc_entity
*entity
)
409 struct sh_mipi
*mipi
= to_sh_mipi(entity
);
410 struct sh_mipi_dsi_info
*pdata
= mipi
->pdev
->dev
.platform_data
;
412 sh_mipi_dsi_enable(mipi
, false);
414 pdata
->set_dot_clock(mipi
->pdev
, mipi
->base
, 0);
416 pm_runtime_put_sync(&mipi
->pdev
->dev
);
419 static const struct sh_mobile_lcdc_entity_ops mipi_ops
= {
420 .display_on
= mipi_display_on
,
421 .display_off
= mipi_display_off
,
424 static int __init
sh_mipi_probe(struct platform_device
*pdev
)
426 struct sh_mipi
*mipi
;
427 struct sh_mipi_dsi_info
*pdata
= pdev
->dev
.platform_data
;
428 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
429 struct resource
*res2
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
430 unsigned long rate
, f_current
;
431 int idx
= pdev
->id
, ret
;
433 if (!res
|| !res2
|| idx
>= ARRAY_SIZE(mipi_dsi
) || !pdata
)
436 if (!pdata
->set_dot_clock
)
439 mutex_lock(&array_lock
);
441 for (idx
= 0; idx
< ARRAY_SIZE(mipi_dsi
) && mipi_dsi
[idx
]; idx
++)
444 if (idx
== ARRAY_SIZE(mipi_dsi
)) {
449 mipi
= kzalloc(sizeof(*mipi
), GFP_KERNEL
);
455 mipi
->entity
.owner
= THIS_MODULE
;
456 mipi
->entity
.ops
= &mipi_ops
;
458 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
459 dev_err(&pdev
->dev
, "MIPI register region already claimed\n");
464 mipi
->base
= ioremap(res
->start
, resource_size(res
));
470 if (!request_mem_region(res2
->start
, resource_size(res2
), pdev
->name
)) {
471 dev_err(&pdev
->dev
, "MIPI register region 2 already claimed\n");
476 mipi
->linkbase
= ioremap(res2
->start
, resource_size(res2
));
477 if (!mipi
->linkbase
) {
484 mipi
->dsit_clk
= clk_get(&pdev
->dev
, "dsit_clk");
485 if (IS_ERR(mipi
->dsit_clk
)) {
486 ret
= PTR_ERR(mipi
->dsit_clk
);
490 f_current
= clk_get_rate(mipi
->dsit_clk
);
491 /* 80MHz required by the datasheet */
492 rate
= clk_round_rate(mipi
->dsit_clk
, 80000000);
493 if (rate
> 0 && rate
!= f_current
)
494 ret
= clk_set_rate(mipi
->dsit_clk
, rate
);
500 dev_dbg(&pdev
->dev
, "DSI-T clk %lu -> %lu\n", f_current
, rate
);
502 ret
= clk_enable(mipi
->dsit_clk
);
506 mipi_dsi
[idx
] = mipi
;
508 pm_runtime_enable(&pdev
->dev
);
509 pm_runtime_resume(&pdev
->dev
);
511 mutex_unlock(&array_lock
);
512 platform_set_drvdata(pdev
, &mipi
->entity
);
518 clk_put(mipi
->dsit_clk
);
520 iounmap(mipi
->linkbase
);
522 release_mem_region(res2
->start
, resource_size(res2
));
526 release_mem_region(res
->start
, resource_size(res
));
531 mutex_unlock(&array_lock
);
536 static int __devexit
sh_mipi_remove(struct platform_device
*pdev
)
538 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
539 struct resource
*res2
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
540 struct sh_mipi
*mipi
= to_sh_mipi(platform_get_drvdata(pdev
));
543 mutex_lock(&array_lock
);
545 for (i
= 0; i
< ARRAY_SIZE(mipi_dsi
) && mipi_dsi
[i
] != mipi
; i
++)
548 if (i
== ARRAY_SIZE(mipi_dsi
)) {
555 mutex_unlock(&array_lock
);
560 pm_runtime_disable(&pdev
->dev
);
561 clk_disable(mipi
->dsit_clk
);
562 clk_put(mipi
->dsit_clk
);
564 iounmap(mipi
->linkbase
);
566 release_mem_region(res2
->start
, resource_size(res2
));
569 release_mem_region(res
->start
, resource_size(res
));
570 platform_set_drvdata(pdev
, NULL
);
576 static struct platform_driver sh_mipi_driver
= {
577 .remove
= __devexit_p(sh_mipi_remove
),
578 .shutdown
= sh_mipi_shutdown
,
580 .name
= "sh-mipi-dsi",
584 static int __init
sh_mipi_init(void)
586 return platform_driver_probe(&sh_mipi_driver
, sh_mipi_probe
);
588 module_init(sh_mipi_init
);
590 static void __exit
sh_mipi_exit(void)
592 platform_driver_unregister(&sh_mipi_driver
);
594 module_exit(sh_mipi_exit
);
596 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
597 MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
598 MODULE_LICENSE("GPL v2");