blob: 05151b82f40f9867bd36ea5579610a5e6ae82af4 [file] [log] [blame]
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +00001/*
2 * Renesas SH-mobile MIPI DSI support
3 *
4 * Copyright (C) 2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
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.
9 */
10
Kuninori Morimoto26c3d7a2011-11-08 20:34:43 -080011#include <linux/bitmap.h>
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000012#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +000017#include <linux/pm_runtime.h>
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000018#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/types.h>
Paul Gortmaker355b2002011-07-03 16:17:28 -040021#include <linux/module.h>
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000022
23#include <video/mipi_display.h>
24#include <video/sh_mipi_dsi.h>
25#include <video/sh_mobile_lcdc.h>
26
Magnus Damm71b146c2010-11-17 06:44:25 +000027#define SYSCTRL 0x0000
28#define SYSCONF 0x0004
29#define TIMSET 0x0008
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
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000037#define DSIINTE 0x0060
Magnus Damm71b146c2010-11-17 06:44:25 +000038#define PHYCTRL 0x0070
39
Magnus Dammdeaba192010-11-17 09:53:25 +000040/* relative to linkbase */
41#define DTCTR 0x0000
42#define VMCTR1 0x0020
43#define VMCTR2 0x0024
44#define VMLEN1 0x0028
Kuninori Morimoto08750612011-11-08 20:35:05 -080045#define VMLEN2 0x002c
Magnus Dammdeaba192010-11-17 09:53:25 +000046#define CMTSRTREQ 0x0070
47#define CMTSRTCTR 0x00d0
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000048
49/* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */
50#define MAX_SH_MIPI_DSI 2
51
52struct sh_mipi {
53 void __iomem *base;
Magnus Dammdeaba192010-11-17 09:53:25 +000054 void __iomem *linkbase;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000055 struct clk *dsit_clk;
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -080056 struct platform_device *pdev;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +000057
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);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000061};
62
63static struct sh_mipi *mipi_dsi[MAX_SH_MIPI_DSI];
64
65/* Protect the above array */
66static DEFINE_MUTEX(array_lock);
67
68static struct sh_mipi *sh_mipi_by_handle(int handle)
69{
70 if (handle >= ARRAY_SIZE(mipi_dsi) || handle < 0)
71 return NULL;
72
73 return mipi_dsi[handle];
74}
75
76static int sh_mipi_send_short(struct sh_mipi *mipi, u8 dsi_cmd,
77 u8 cmd, u8 param)
78{
79 u32 data = (dsi_cmd << 24) | (cmd << 16) | (param << 8);
80 int cnt = 100;
81
82 /* transmit a short packet to LCD panel */
Magnus Dammdeaba192010-11-17 09:53:25 +000083 iowrite32(1 | data, mipi->linkbase + CMTSRTCTR);
84 iowrite32(1, mipi->linkbase + CMTSRTREQ);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000085
Magnus Dammdeaba192010-11-17 09:53:25 +000086 while ((ioread32(mipi->linkbase + CMTSRTREQ) & 1) && --cnt)
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000087 udelay(1);
88
89 return cnt ? 0 : -ETIMEDOUT;
90}
91
92#define LCD_CHAN2MIPI(c) ((c) < LCDC_CHAN_MAINLCD || (c) > LCDC_CHAN_SUBLCD ? \
93 -EINVAL : (c) - 1)
94
95static int sh_mipi_dcs(int handle, u8 cmd)
96{
97 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
98 if (!mipi)
99 return -ENODEV;
100 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE, cmd, 0);
101}
102
103static int sh_mipi_dcs_param(int handle, u8 cmd, u8 param)
104{
105 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
106 if (!mipi)
107 return -ENODEV;
108 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE_PARAM, cmd,
109 param);
110}
111
112static void sh_mipi_dsi_enable(struct sh_mipi *mipi, bool enable)
113{
114 /*
115 * enable LCDC data tx, transition to LPS after completion of each HS
116 * packet
117 */
Magnus Dammdeaba192010-11-17 09:53:25 +0000118 iowrite32(0x00000002 | enable, mipi->linkbase + DTCTR);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000119}
120
121static void sh_mipi_shutdown(struct platform_device *pdev)
122{
123 struct sh_mipi *mipi = platform_get_drvdata(pdev);
124
125 sh_mipi_dsi_enable(mipi, false);
126}
127
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000128static int __init sh_mipi_setup(struct sh_mipi *mipi,
129 struct sh_mipi_dsi_info *pdata)
130{
131 void __iomem *base = mipi->base;
132 struct sh_mobile_lcdc_chan_cfg *ch = pdata->lcd_chan;
Kuninori Morimotof8329062011-11-08 20:34:55 -0800133 u32 pctype, datatype, pixfmt, linelength, vmctr2;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800134 u32 tmp, top, bottom, delay, div;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000135 bool yuv;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800136 int bpp;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000137
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000138 /*
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
142 */
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000143 switch (pdata->data_format) {
144 case MIPI_RGB888:
145 pctype = 0;
146 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
147 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000148 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000149 yuv = false;
150 break;
151 case MIPI_RGB565:
152 pctype = 1;
153 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
154 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000155 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000156 yuv = false;
157 break;
158 case MIPI_RGB666_LP:
159 pctype = 2;
160 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
161 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000162 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000163 yuv = false;
164 break;
165 case MIPI_RGB666:
166 pctype = 3;
167 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
168 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000169 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000170 yuv = false;
171 break;
172 case MIPI_BGR888:
173 pctype = 8;
174 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
175 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000176 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000177 yuv = false;
178 break;
179 case MIPI_BGR565:
180 pctype = 9;
181 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
182 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000183 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000184 yuv = false;
185 break;
186 case MIPI_BGR666_LP:
187 pctype = 0xa;
188 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
189 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000190 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000191 yuv = false;
192 break;
193 case MIPI_BGR666:
194 pctype = 0xb;
195 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
196 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000197 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000198 yuv = false;
199 break;
200 case MIPI_YUYV:
201 pctype = 4;
202 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
203 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000204 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000205 yuv = true;
206 break;
207 case MIPI_UYVY:
208 pctype = 5;
209 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
210 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000211 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000212 yuv = true;
213 break;
214 case MIPI_YUV420_L:
215 pctype = 6;
216 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
217 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000218 linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000219 yuv = true;
220 break;
221 case MIPI_YUV420:
222 pctype = 7;
223 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
224 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
225 /* Length of U/V line */
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000226 linelength = (ch->lcd_cfg[0].xres + 1) / 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000227 yuv = true;
228 break;
229 default:
230 return -EINVAL;
231 }
232
233 if ((yuv && ch->interface_type != YUV422) ||
234 (!yuv && ch->interface_type != RGB24))
235 return -EINVAL;
236
Kuninori Morimoto26c3d7a2011-11-08 20:34:43 -0800237 if (!pdata->lane)
238 return -EINVAL;
239
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000240 /* reset DSI link */
Magnus Damm71b146c2010-11-17 06:44:25 +0000241 iowrite32(0x00000001, base + SYSCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000242 /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
243 udelay(50);
Magnus Damm71b146c2010-11-17 06:44:25 +0000244 iowrite32(0x00000000, base + SYSCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000245
246 /* setup DSI link */
247
248 /*
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000249 * T_wakeup = 0x7000
250 * T_hs-trail = 3
251 * T_hs-prepare = 3
252 * T_clk-trail = 3
253 * T_clk-prepare = 2
254 */
Magnus Damm71b146c2010-11-17 06:44:25 +0000255 iowrite32(0x70003332, base + TIMSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000256 /* no responses requested */
Magnus Damm71b146c2010-11-17 06:44:25 +0000257 iowrite32(0x00000000, base + RESREQSET0);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000258 /* request response to packets of type 0x28 */
Magnus Damm71b146c2010-11-17 06:44:25 +0000259 iowrite32(0x00000100, base + RESREQSET1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000260 /* High-speed transmission timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000261 iowrite32(0x0fffffff, base + HSTTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000262 /* LP reception timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000263 iowrite32(0x0fffffff, base + LPRTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000264 /* Turn-around timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000265 iowrite32(0x0fffffff, base + TATOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000266 /* Peripheral reset timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000267 iowrite32(0x0fffffff, base + PRTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000268 /* Interrupts not used, disable all */
269 iowrite32(0, base + DSIINTE);
270 /* DSI-Tx bias on */
Magnus Damm71b146c2010-11-17 06:44:25 +0000271 iowrite32(0x00000001, base + PHYCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000272 udelay(200);
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800273 /* Deassert resets, power on */
274 iowrite32(0x03070001, base + PHYCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000275
Kuninori Morimotoa2065a32011-11-08 20:35:56 -0800276 /*
277 * Default = ULPS enable |
278 * Contention detection enabled |
279 * EoT packet transmission enable |
280 * CRC check enable |
281 * ECC check enable
282 */
283 bitmap_fill((unsigned long *)&tmp, pdata->lane);
284 tmp |= 0x00003700;
285 iowrite32(tmp, base + SYSCONF);
286
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000287 /* setup l-bridge */
288
289 /*
290 * Enable transmission of all packets,
291 * transmit LPS after each HS packet completion
292 */
Magnus Dammdeaba192010-11-17 09:53:25 +0000293 iowrite32(0x00000006, mipi->linkbase + DTCTR);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000294 /* VSYNC width = 2 (<< 17) */
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000295 iowrite32((ch->lcd_cfg[0].vsync_len << pdata->vsynw_offset) |
296 (pdata->clksrc << 16) | (pctype << 12) | datatype,
Magnus Dammdeaba192010-11-17 09:53:25 +0000297 mipi->linkbase + VMCTR1);
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000298
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000299 /*
300 * Non-burst mode with sync pulses: VSE and HSE are output,
301 * HSA period allowed, no commands in LP
302 */
Kuninori Morimotof8329062011-11-08 20:34:55 -0800303 vmctr2 = 0;
304 if (pdata->flags & SH_MIPI_DSI_VSEE)
305 vmctr2 |= 1 << 23;
306 if (pdata->flags & SH_MIPI_DSI_HSEE)
307 vmctr2 |= 1 << 22;
308 if (pdata->flags & SH_MIPI_DSI_HSAE)
309 vmctr2 |= 1 << 21;
Kuninori Morimotod07a9d22011-11-08 20:34:33 -0800310 if (pdata->flags & SH_MIPI_DSI_BL2E)
311 vmctr2 |= 1 << 17;
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000312 if (pdata->flags & SH_MIPI_DSI_HSABM)
Kuninori Morimoto3c2a6592011-11-08 20:34:12 -0800313 vmctr2 |= 1 << 5;
Kuninori Morimoto32ba95c2011-11-08 20:34:01 -0800314 if (pdata->flags & SH_MIPI_DSI_HBPBM)
Kuninori Morimoto3c2a6592011-11-08 20:34:12 -0800315 vmctr2 |= 1 << 4;
Kuninori Morimotof7b0af62011-11-08 20:34:24 -0800316 if (pdata->flags & SH_MIPI_DSI_HFPBM)
317 vmctr2 |= 1 << 3;
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000318 iowrite32(vmctr2, mipi->linkbase + VMCTR2);
319
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000320 /*
Kuninori Morimoto08750612011-11-08 20:35:05 -0800321 * VMLEN1 = RGBLEN | HSALEN
322 *
323 * see
324 * Video mode - Blanking Packet setting
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000325 */
Kuninori Morimoto08750612011-11-08 20:35:05 -0800326 top = linelength << 16; /* RGBLEN */
327 bottom = 0x00000001;
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);
331
332 /*
333 * VMLEN2 = HBPLEN | HFPLEN
334 *
335 * see
336 * Video mode - Blanking Packet setting
337 */
338 top = 0x00010000;
339 bottom = 0x00000001;
340 delay = 0;
341
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800342 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)
346 div = 2;
347
Kuninori Morimoto08750612011-11-08 20:35:05 -0800348 if (pdata->flags & SH_MIPI_DSI_HFPBM) { /* HBPLEN */
349 top = ch->lcd_cfg[0].hsync_len + ch->lcd_cfg[0].left_margin;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800350 top = ((pdata->lane * top / div) - 10) << 16;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800351 }
352 if (pdata->flags & SH_MIPI_DSI_HBPBM) { /* HFPLEN */
353 bottom = ch->lcd_cfg[0].right_margin;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800354 bottom = (pdata->lane * bottom / div) - 12;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800355 }
356
357 bpp = linelength / ch->lcd_cfg[0].xres; /* byte / pixel */
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800358 if ((pdata->lane / div) > bpp) {
Kuninori Morimoto08750612011-11-08 20:35:05 -0800359 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);
362 }
363
364 iowrite32(top | (bottom + delay) , mipi->linkbase + VMLEN2);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000365
366 msleep(5);
367
368 /* setup LCD panel */
369
370 /* cf. drivers/video/omap/lcd_mipid.c */
371 sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE);
372 msleep(120);
373 /*
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
382 */
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,
386 pixfmt << 4);
387 sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON);
388
Kuninori Morimoto97cab452011-11-08 20:36:07 -0800389 /* Enable timeout counters */
390 iowrite32(0x00000f00, base + DSICTRL);
391
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000392 return 0;
393}
394
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800395static void mipi_display_on(void *arg, struct fb_info *info)
396{
397 struct sh_mipi *mipi = arg;
398 struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data;
399 int ret;
400
401 pm_runtime_get_sync(&mipi->pdev->dev);
402
403 ret = pdata->set_dot_clock(mipi->pdev, mipi->base, 1);
404 if (ret < 0)
405 goto mipi_display_on_fail1;
406
407 ret = sh_mipi_setup(mipi, pdata);
408 if (ret < 0)
409 goto mipi_display_on_fail2;
410
411 sh_mipi_dsi_enable(mipi, true);
412
413 if (mipi->next_display_on)
414 mipi->next_display_on(mipi->next_board_data, info);
415
416 return;
417
418mipi_display_on_fail1:
419 pm_runtime_put_sync(&mipi->pdev->dev);
420mipi_display_on_fail2:
421 pdata->set_dot_clock(mipi->pdev, mipi->base, 0);
422}
423
424static void mipi_display_off(void *arg)
425{
426 struct sh_mipi *mipi = arg;
427 struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data;
428
429 if (mipi->next_display_off)
430 mipi->next_display_off(mipi->next_board_data);
431
432 sh_mipi_dsi_enable(mipi, false);
433
434 pdata->set_dot_clock(mipi->pdev, mipi->base, 0);
435
436 pm_runtime_put_sync(&mipi->pdev->dev);
437}
438
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000439static int __init sh_mipi_probe(struct platform_device *pdev)
440{
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);
Magnus Dammdeaba192010-11-17 09:53:25 +0000444 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000445 unsigned long rate, f_current;
446 int idx = pdev->id, ret;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000447
Magnus Dammdeaba192010-11-17 09:53:25 +0000448 if (!res || !res2 || idx >= ARRAY_SIZE(mipi_dsi) || !pdata)
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000449 return -ENODEV;
450
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800451 if (!pdata->set_dot_clock)
452 return -EINVAL;
453
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000454 mutex_lock(&array_lock);
455 if (idx < 0)
456 for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++)
457 ;
458
459 if (idx == ARRAY_SIZE(mipi_dsi)) {
460 ret = -EBUSY;
461 goto efindslot;
462 }
463
464 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
465 if (!mipi) {
466 ret = -ENOMEM;
467 goto ealloc;
468 }
469
470 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
471 dev_err(&pdev->dev, "MIPI register region already claimed\n");
472 ret = -EBUSY;
473 goto ereqreg;
474 }
475
476 mipi->base = ioremap(res->start, resource_size(res));
477 if (!mipi->base) {
478 ret = -ENOMEM;
479 goto emap;
480 }
481
Magnus Dammdeaba192010-11-17 09:53:25 +0000482 if (!request_mem_region(res2->start, resource_size(res2), pdev->name)) {
483 dev_err(&pdev->dev, "MIPI register region 2 already claimed\n");
484 ret = -EBUSY;
485 goto ereqreg2;
486 }
487
488 mipi->linkbase = ioremap(res2->start, resource_size(res2));
489 if (!mipi->linkbase) {
490 ret = -ENOMEM;
491 goto emap2;
492 }
493
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -0800494 mipi->pdev = pdev;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000495
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000496 mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk");
497 if (IS_ERR(mipi->dsit_clk)) {
498 ret = PTR_ERR(mipi->dsit_clk);
499 goto eclktget;
500 }
501
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);
507 else
508 ret = rate;
509 if (ret < 0)
510 goto esettrate;
511
512 dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate);
513
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000514 ret = clk_enable(mipi->dsit_clk);
515 if (ret < 0)
516 goto eclkton;
517
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000518 mipi_dsi[idx] = mipi;
519
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000520 pm_runtime_enable(&pdev->dev);
521 pm_runtime_resume(&pdev->dev);
522
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000523 mutex_unlock(&array_lock);
524 platform_set_drvdata(pdev, mipi);
525
Magnus Damm6722a402010-11-17 06:44:54 +0000526 /* 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;
530
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000531 /* 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;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000535 pdata->lcd_chan->board_cfg.owner = THIS_MODULE;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000536
537 return 0;
538
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000539eclkton:
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000540esettrate:
541 clk_put(mipi->dsit_clk);
542eclktget:
Magnus Dammdeaba192010-11-17 09:53:25 +0000543 iounmap(mipi->linkbase);
544emap2:
545 release_mem_region(res2->start, resource_size(res2));
546ereqreg2:
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000547 iounmap(mipi->base);
548emap:
549 release_mem_region(res->start, resource_size(res));
550ereqreg:
551 kfree(mipi);
552ealloc:
553efindslot:
554 mutex_unlock(&array_lock);
555
556 return ret;
557}
558
559static int __exit sh_mipi_remove(struct platform_device *pdev)
560{
561 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
562 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Dammdeaba192010-11-17 09:53:25 +0000563 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000564 struct sh_mipi *mipi = platform_get_drvdata(pdev);
565 int i, ret;
566
567 mutex_lock(&array_lock);
568
569 for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++)
570 ;
571
572 if (i == ARRAY_SIZE(mipi_dsi)) {
573 ret = -EINVAL;
574 } else {
575 ret = 0;
576 mipi_dsi[i] = NULL;
577 }
578
579 mutex_unlock(&array_lock);
580
581 if (ret < 0)
582 return ret;
583
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000584 pdata->lcd_chan->board_cfg.owner = NULL;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000585 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;
588
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000589 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000590 clk_disable(mipi->dsit_clk);
591 clk_put(mipi->dsit_clk);
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800592
Magnus Dammdeaba192010-11-17 09:53:25 +0000593 iounmap(mipi->linkbase);
594 if (res2)
595 release_mem_region(res2->start, resource_size(res2));
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000596 iounmap(mipi->base);
597 if (res)
598 release_mem_region(res->start, resource_size(res));
599 platform_set_drvdata(pdev, NULL);
600 kfree(mipi);
601
602 return 0;
603}
604
605static struct platform_driver sh_mipi_driver = {
606 .remove = __exit_p(sh_mipi_remove),
607 .shutdown = sh_mipi_shutdown,
608 .driver = {
609 .name = "sh-mipi-dsi",
610 },
611};
612
613static int __init sh_mipi_init(void)
614{
615 return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe);
616}
617module_init(sh_mipi_init);
618
619static void __exit sh_mipi_exit(void)
620{
621 platform_driver_unregister(&sh_mipi_driver);
622}
623module_exit(sh_mipi_exit);
624
625MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
626MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
627MODULE_LICENSE("GPL v2");