blob: 7e4650530c9ade98f15412828fcee65c9dd8b9e8 [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 Liakhovetskic2439392010-07-21 10:13:17 +0000128static void mipi_display_on(void *arg, struct fb_info *info)
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000129{
130 struct sh_mipi *mipi = arg;
131
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -0800132 pm_runtime_get_sync(&mipi->pdev->dev);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000133 sh_mipi_dsi_enable(mipi, true);
Magnus Damm6722a402010-11-17 06:44:54 +0000134
135 if (mipi->next_display_on)
136 mipi->next_display_on(mipi->next_board_data, info);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000137}
138
139static void mipi_display_off(void *arg)
140{
141 struct sh_mipi *mipi = arg;
142
Magnus Damm6722a402010-11-17 06:44:54 +0000143 if (mipi->next_display_off)
144 mipi->next_display_off(mipi->next_board_data);
145
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000146 sh_mipi_dsi_enable(mipi, false);
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -0800147 pm_runtime_put(&mipi->pdev->dev);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000148}
149
150static int __init sh_mipi_setup(struct sh_mipi *mipi,
151 struct sh_mipi_dsi_info *pdata)
152{
153 void __iomem *base = mipi->base;
154 struct sh_mobile_lcdc_chan_cfg *ch = pdata->lcd_chan;
Kuninori Morimotof832906a2011-11-08 20:34:55 -0800155 u32 pctype, datatype, pixfmt, linelength, vmctr2;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800156 u32 tmp, top, bottom, delay, div;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000157 bool yuv;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800158 int bpp;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000159
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000160 /*
161 * Select data format. MIPI DSI is not hot-pluggable, so, we just use
162 * the default videomode. If this ever becomes a problem, We'll have to
163 * move this to mipi_display_on() above and use info->var.xres
164 */
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000165 switch (pdata->data_format) {
166 case MIPI_RGB888:
167 pctype = 0;
168 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
169 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000170 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000171 yuv = false;
172 break;
173 case MIPI_RGB565:
174 pctype = 1;
175 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
176 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000177 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000178 yuv = false;
179 break;
180 case MIPI_RGB666_LP:
181 pctype = 2;
182 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
183 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000184 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000185 yuv = false;
186 break;
187 case MIPI_RGB666:
188 pctype = 3;
189 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
190 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000191 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000192 yuv = false;
193 break;
194 case MIPI_BGR888:
195 pctype = 8;
196 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
197 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000198 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000199 yuv = false;
200 break;
201 case MIPI_BGR565:
202 pctype = 9;
203 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
204 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000205 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000206 yuv = false;
207 break;
208 case MIPI_BGR666_LP:
209 pctype = 0xa;
210 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
211 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000212 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000213 yuv = false;
214 break;
215 case MIPI_BGR666:
216 pctype = 0xb;
217 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
218 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000219 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000220 yuv = false;
221 break;
222 case MIPI_YUYV:
223 pctype = 4;
224 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
225 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000226 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000227 yuv = true;
228 break;
229 case MIPI_UYVY:
230 pctype = 5;
231 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
232 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000233 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000234 yuv = true;
235 break;
236 case MIPI_YUV420_L:
237 pctype = 6;
238 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
239 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000240 linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000241 yuv = true;
242 break;
243 case MIPI_YUV420:
244 pctype = 7;
245 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
246 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
247 /* Length of U/V line */
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000248 linelength = (ch->lcd_cfg[0].xres + 1) / 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000249 yuv = true;
250 break;
251 default:
252 return -EINVAL;
253 }
254
255 if ((yuv && ch->interface_type != YUV422) ||
256 (!yuv && ch->interface_type != RGB24))
257 return -EINVAL;
258
Kuninori Morimoto26c3d7a2011-11-08 20:34:43 -0800259 if (!pdata->lane)
260 return -EINVAL;
261
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000262 /* reset DSI link */
Magnus Damm71b146c2010-11-17 06:44:25 +0000263 iowrite32(0x00000001, base + SYSCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000264 /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
265 udelay(50);
Magnus Damm71b146c2010-11-17 06:44:25 +0000266 iowrite32(0x00000000, base + SYSCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000267
268 /* setup DSI link */
269
270 /*
271 * Default = ULPS enable |
272 * Contention detection enabled |
273 * EoT packet transmission enable |
274 * CRC check enable |
275 * ECC check enable
276 * additionally enable first two lanes
277 */
Kuninori Morimoto26c3d7a2011-11-08 20:34:43 -0800278 bitmap_fill((unsigned long *)&tmp, pdata->lane);
279 tmp |= 0x00003700;
280 iowrite32(tmp, base + SYSCONF);
281
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000282 /*
283 * T_wakeup = 0x7000
284 * T_hs-trail = 3
285 * T_hs-prepare = 3
286 * T_clk-trail = 3
287 * T_clk-prepare = 2
288 */
Magnus Damm71b146c2010-11-17 06:44:25 +0000289 iowrite32(0x70003332, base + TIMSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000290 /* no responses requested */
Magnus Damm71b146c2010-11-17 06:44:25 +0000291 iowrite32(0x00000000, base + RESREQSET0);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000292 /* request response to packets of type 0x28 */
Magnus Damm71b146c2010-11-17 06:44:25 +0000293 iowrite32(0x00000100, base + RESREQSET1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000294 /* High-speed transmission timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000295 iowrite32(0x0fffffff, base + HSTTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000296 /* LP reception timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000297 iowrite32(0x0fffffff, base + LPRTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000298 /* Turn-around timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000299 iowrite32(0x0fffffff, base + TATOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000300 /* Peripheral reset timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000301 iowrite32(0x0fffffff, base + PRTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000302 /* Enable timeout counters */
Magnus Damm71b146c2010-11-17 06:44:25 +0000303 iowrite32(0x00000f00, base + DSICTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000304 /* Interrupts not used, disable all */
305 iowrite32(0, base + DSIINTE);
306 /* DSI-Tx bias on */
Magnus Damm71b146c2010-11-17 06:44:25 +0000307 iowrite32(0x00000001, base + PHYCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000308 udelay(200);
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800309 /* Deassert resets, power on */
310 iowrite32(0x03070001, base + PHYCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000311
312 /* setup l-bridge */
313
314 /*
315 * Enable transmission of all packets,
316 * transmit LPS after each HS packet completion
317 */
Magnus Dammdeaba192010-11-17 09:53:25 +0000318 iowrite32(0x00000006, mipi->linkbase + DTCTR);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000319 /* VSYNC width = 2 (<< 17) */
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000320 iowrite32((ch->lcd_cfg[0].vsync_len << pdata->vsynw_offset) |
321 (pdata->clksrc << 16) | (pctype << 12) | datatype,
Magnus Dammdeaba192010-11-17 09:53:25 +0000322 mipi->linkbase + VMCTR1);
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000323
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000324 /*
325 * Non-burst mode with sync pulses: VSE and HSE are output,
326 * HSA period allowed, no commands in LP
327 */
Kuninori Morimotof832906a2011-11-08 20:34:55 -0800328 vmctr2 = 0;
329 if (pdata->flags & SH_MIPI_DSI_VSEE)
330 vmctr2 |= 1 << 23;
331 if (pdata->flags & SH_MIPI_DSI_HSEE)
332 vmctr2 |= 1 << 22;
333 if (pdata->flags & SH_MIPI_DSI_HSAE)
334 vmctr2 |= 1 << 21;
Kuninori Morimotod07a9d22011-11-08 20:34:33 -0800335 if (pdata->flags & SH_MIPI_DSI_BL2E)
336 vmctr2 |= 1 << 17;
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000337 if (pdata->flags & SH_MIPI_DSI_HSABM)
Kuninori Morimoto3c2a6592011-11-08 20:34:12 -0800338 vmctr2 |= 1 << 5;
Kuninori Morimoto32ba95c2011-11-08 20:34:01 -0800339 if (pdata->flags & SH_MIPI_DSI_HBPBM)
Kuninori Morimoto3c2a6592011-11-08 20:34:12 -0800340 vmctr2 |= 1 << 4;
Kuninori Morimotof7b0af62011-11-08 20:34:24 -0800341 if (pdata->flags & SH_MIPI_DSI_HFPBM)
342 vmctr2 |= 1 << 3;
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000343 iowrite32(vmctr2, mipi->linkbase + VMCTR2);
344
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000345 /*
Kuninori Morimoto08750612011-11-08 20:35:05 -0800346 * VMLEN1 = RGBLEN | HSALEN
347 *
348 * see
349 * Video mode - Blanking Packet setting
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000350 */
Kuninori Morimoto08750612011-11-08 20:35:05 -0800351 top = linelength << 16; /* RGBLEN */
352 bottom = 0x00000001;
353 if (pdata->flags & SH_MIPI_DSI_HSABM) /* HSALEN */
354 bottom = (pdata->lane * ch->lcd_cfg[0].hsync_len) - 10;
355 iowrite32(top | bottom , mipi->linkbase + VMLEN1);
356
357 /*
358 * VMLEN2 = HBPLEN | HFPLEN
359 *
360 * see
361 * Video mode - Blanking Packet setting
362 */
363 top = 0x00010000;
364 bottom = 0x00000001;
365 delay = 0;
366
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800367 div = 1; /* HSbyteCLK is calculation base
368 * HS4divCLK = HSbyteCLK/2
369 * HS6divCLK is not supported for now */
370 if (pdata->flags & SH_MIPI_DSI_HS4divCLK)
371 div = 2;
372
Kuninori Morimoto08750612011-11-08 20:35:05 -0800373 if (pdata->flags & SH_MIPI_DSI_HFPBM) { /* HBPLEN */
374 top = ch->lcd_cfg[0].hsync_len + ch->lcd_cfg[0].left_margin;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800375 top = ((pdata->lane * top / div) - 10) << 16;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800376 }
377 if (pdata->flags & SH_MIPI_DSI_HBPBM) { /* HFPLEN */
378 bottom = ch->lcd_cfg[0].right_margin;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800379 bottom = (pdata->lane * bottom / div) - 12;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800380 }
381
382 bpp = linelength / ch->lcd_cfg[0].xres; /* byte / pixel */
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800383 if ((pdata->lane / div) > bpp) {
Kuninori Morimoto08750612011-11-08 20:35:05 -0800384 tmp = ch->lcd_cfg[0].xres / bpp; /* output cycle */
385 tmp = ch->lcd_cfg[0].xres - tmp; /* (input - output) cycle */
386 delay = (pdata->lane * tmp);
387 }
388
389 iowrite32(top | (bottom + delay) , mipi->linkbase + VMLEN2);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000390
391 msleep(5);
392
393 /* setup LCD panel */
394
395 /* cf. drivers/video/omap/lcd_mipid.c */
396 sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE);
397 msleep(120);
398 /*
399 * [7] - Page Address Mode
400 * [6] - Column Address Mode
401 * [5] - Page / Column Address Mode
402 * [4] - Display Device Line Refresh Order
403 * [3] - RGB/BGR Order
404 * [2] - Display Data Latch Data Order
405 * [1] - Flip Horizontal
406 * [0] - Flip Vertical
407 */
408 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
409 /* cf. set_data_lines() */
410 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_PIXEL_FORMAT,
411 pixfmt << 4);
412 sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON);
413
414 return 0;
415}
416
417static int __init sh_mipi_probe(struct platform_device *pdev)
418{
419 struct sh_mipi *mipi;
420 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
421 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Dammdeaba192010-11-17 09:53:25 +0000422 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000423 unsigned long rate, f_current;
424 int idx = pdev->id, ret;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000425
Magnus Dammdeaba192010-11-17 09:53:25 +0000426 if (!res || !res2 || idx >= ARRAY_SIZE(mipi_dsi) || !pdata)
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000427 return -ENODEV;
428
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800429 if (!pdata->set_dot_clock)
430 return -EINVAL;
431
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000432 mutex_lock(&array_lock);
433 if (idx < 0)
434 for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++)
435 ;
436
437 if (idx == ARRAY_SIZE(mipi_dsi)) {
438 ret = -EBUSY;
439 goto efindslot;
440 }
441
442 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
443 if (!mipi) {
444 ret = -ENOMEM;
445 goto ealloc;
446 }
447
448 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
449 dev_err(&pdev->dev, "MIPI register region already claimed\n");
450 ret = -EBUSY;
451 goto ereqreg;
452 }
453
454 mipi->base = ioremap(res->start, resource_size(res));
455 if (!mipi->base) {
456 ret = -ENOMEM;
457 goto emap;
458 }
459
Magnus Dammdeaba192010-11-17 09:53:25 +0000460 if (!request_mem_region(res2->start, resource_size(res2), pdev->name)) {
461 dev_err(&pdev->dev, "MIPI register region 2 already claimed\n");
462 ret = -EBUSY;
463 goto ereqreg2;
464 }
465
466 mipi->linkbase = ioremap(res2->start, resource_size(res2));
467 if (!mipi->linkbase) {
468 ret = -ENOMEM;
469 goto emap2;
470 }
471
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -0800472 mipi->pdev = pdev;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000473
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000474 mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk");
475 if (IS_ERR(mipi->dsit_clk)) {
476 ret = PTR_ERR(mipi->dsit_clk);
477 goto eclktget;
478 }
479
480 f_current = clk_get_rate(mipi->dsit_clk);
481 /* 80MHz required by the datasheet */
482 rate = clk_round_rate(mipi->dsit_clk, 80000000);
483 if (rate > 0 && rate != f_current)
484 ret = clk_set_rate(mipi->dsit_clk, rate);
485 else
486 ret = rate;
487 if (ret < 0)
488 goto esettrate;
489
490 dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate);
491
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000492 ret = clk_enable(mipi->dsit_clk);
493 if (ret < 0)
494 goto eclkton;
495
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000496 mipi_dsi[idx] = mipi;
497
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000498 pm_runtime_enable(&pdev->dev);
499 pm_runtime_resume(&pdev->dev);
500
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000501 ret = sh_mipi_setup(mipi, pdata);
502 if (ret < 0)
503 goto emipisetup;
504
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800505 ret = pdata->set_dot_clock(pdev, mipi->base, 1);
506 if (ret < 0)
507 goto emipisetup;
508
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000509 mutex_unlock(&array_lock);
510 platform_set_drvdata(pdev, mipi);
511
Magnus Damm6722a402010-11-17 06:44:54 +0000512 /* Save original LCDC callbacks */
513 mipi->next_board_data = pdata->lcd_chan->board_cfg.board_data;
514 mipi->next_display_on = pdata->lcd_chan->board_cfg.display_on;
515 mipi->next_display_off = pdata->lcd_chan->board_cfg.display_off;
516
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000517 /* Set up LCDC callbacks */
518 pdata->lcd_chan->board_cfg.board_data = mipi;
519 pdata->lcd_chan->board_cfg.display_on = mipi_display_on;
520 pdata->lcd_chan->board_cfg.display_off = mipi_display_off;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000521 pdata->lcd_chan->board_cfg.owner = THIS_MODULE;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000522
523 return 0;
524
525emipisetup:
526 mipi_dsi[idx] = NULL;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000527 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000528 clk_disable(mipi->dsit_clk);
529eclkton:
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000530esettrate:
531 clk_put(mipi->dsit_clk);
532eclktget:
Magnus Dammdeaba192010-11-17 09:53:25 +0000533 iounmap(mipi->linkbase);
534emap2:
535 release_mem_region(res2->start, resource_size(res2));
536ereqreg2:
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000537 iounmap(mipi->base);
538emap:
539 release_mem_region(res->start, resource_size(res));
540ereqreg:
541 kfree(mipi);
542ealloc:
543efindslot:
544 mutex_unlock(&array_lock);
545
546 return ret;
547}
548
549static int __exit sh_mipi_remove(struct platform_device *pdev)
550{
551 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
552 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Dammdeaba192010-11-17 09:53:25 +0000553 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000554 struct sh_mipi *mipi = platform_get_drvdata(pdev);
555 int i, ret;
556
557 mutex_lock(&array_lock);
558
559 for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++)
560 ;
561
562 if (i == ARRAY_SIZE(mipi_dsi)) {
563 ret = -EINVAL;
564 } else {
565 ret = 0;
566 mipi_dsi[i] = NULL;
567 }
568
569 mutex_unlock(&array_lock);
570
571 if (ret < 0)
572 return ret;
573
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000574 pdata->lcd_chan->board_cfg.owner = NULL;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000575 pdata->lcd_chan->board_cfg.display_on = NULL;
576 pdata->lcd_chan->board_cfg.display_off = NULL;
577 pdata->lcd_chan->board_cfg.board_data = NULL;
578
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000579 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000580 clk_disable(mipi->dsit_clk);
581 clk_put(mipi->dsit_clk);
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800582 pdata->set_dot_clock(pdev, mipi->base, 0);
583
Magnus Dammdeaba192010-11-17 09:53:25 +0000584 iounmap(mipi->linkbase);
585 if (res2)
586 release_mem_region(res2->start, resource_size(res2));
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000587 iounmap(mipi->base);
588 if (res)
589 release_mem_region(res->start, resource_size(res));
590 platform_set_drvdata(pdev, NULL);
591 kfree(mipi);
592
593 return 0;
594}
595
596static struct platform_driver sh_mipi_driver = {
597 .remove = __exit_p(sh_mipi_remove),
598 .shutdown = sh_mipi_shutdown,
599 .driver = {
600 .name = "sh-mipi-dsi",
601 },
602};
603
604static int __init sh_mipi_init(void)
605{
606 return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe);
607}
608module_init(sh_mipi_init);
609
610static void __exit sh_mipi_exit(void)
611{
612 platform_driver_unregister(&sh_mipi_driver);
613}
614module_exit(sh_mipi_exit);
615
616MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
617MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
618MODULE_LICENSE("GPL v2");