blob: 58744fee230e326fc3465bca4a0f2990a6105073 [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
Laurent Pinchart732bbcc2011-09-11 18:27:50 +020027#include "sh_mobile_lcdcfb.h"
28
Magnus Damm71b146c2010-11-17 06:44:25 +000029#define SYSCTRL 0x0000
30#define SYSCONF 0x0004
31#define TIMSET 0x0008
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
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000039#define DSIINTE 0x0060
Magnus Damm71b146c2010-11-17 06:44:25 +000040#define PHYCTRL 0x0070
41
Magnus Dammdeaba192010-11-17 09:53:25 +000042/* relative to linkbase */
43#define DTCTR 0x0000
44#define VMCTR1 0x0020
45#define VMCTR2 0x0024
46#define VMLEN1 0x0028
Kuninori Morimoto08750612011-11-08 20:35:05 -080047#define VMLEN2 0x002c
Magnus Dammdeaba192010-11-17 09:53:25 +000048#define CMTSRTREQ 0x0070
49#define CMTSRTCTR 0x00d0
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000050
51/* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */
52#define MAX_SH_MIPI_DSI 2
53
54struct sh_mipi {
Laurent Pinchart732bbcc2011-09-11 18:27:50 +020055 struct sh_mobile_lcdc_entity entity;
56
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000057 void __iomem *base;
Magnus Dammdeaba192010-11-17 09:53:25 +000058 void __iomem *linkbase;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000059 struct clk *dsit_clk;
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -080060 struct platform_device *pdev;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +000061
62 void *next_board_data;
63 void (*next_display_on)(void *board_data, struct fb_info *info);
64 void (*next_display_off)(void *board_data);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000065};
66
Laurent Pinchart732bbcc2011-09-11 18:27:50 +020067#define to_sh_mipi(e) container_of(e, struct sh_mipi, entity)
68
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000069static struct sh_mipi *mipi_dsi[MAX_SH_MIPI_DSI];
70
71/* Protect the above array */
72static DEFINE_MUTEX(array_lock);
73
74static struct sh_mipi *sh_mipi_by_handle(int handle)
75{
76 if (handle >= ARRAY_SIZE(mipi_dsi) || handle < 0)
77 return NULL;
78
79 return mipi_dsi[handle];
80}
81
82static int sh_mipi_send_short(struct sh_mipi *mipi, u8 dsi_cmd,
83 u8 cmd, u8 param)
84{
85 u32 data = (dsi_cmd << 24) | (cmd << 16) | (param << 8);
86 int cnt = 100;
87
88 /* transmit a short packet to LCD panel */
Magnus Dammdeaba192010-11-17 09:53:25 +000089 iowrite32(1 | data, mipi->linkbase + CMTSRTCTR);
90 iowrite32(1, mipi->linkbase + CMTSRTREQ);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000091
Magnus Dammdeaba192010-11-17 09:53:25 +000092 while ((ioread32(mipi->linkbase + CMTSRTREQ) & 1) && --cnt)
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +000093 udelay(1);
94
95 return cnt ? 0 : -ETIMEDOUT;
96}
97
98#define LCD_CHAN2MIPI(c) ((c) < LCDC_CHAN_MAINLCD || (c) > LCDC_CHAN_SUBLCD ? \
99 -EINVAL : (c) - 1)
100
101static int sh_mipi_dcs(int handle, u8 cmd)
102{
103 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
104 if (!mipi)
105 return -ENODEV;
106 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE, cmd, 0);
107}
108
109static int sh_mipi_dcs_param(int handle, u8 cmd, u8 param)
110{
111 struct sh_mipi *mipi = sh_mipi_by_handle(LCD_CHAN2MIPI(handle));
112 if (!mipi)
113 return -ENODEV;
114 return sh_mipi_send_short(mipi, MIPI_DSI_DCS_SHORT_WRITE_PARAM, cmd,
115 param);
116}
117
118static void sh_mipi_dsi_enable(struct sh_mipi *mipi, bool enable)
119{
120 /*
121 * enable LCDC data tx, transition to LPS after completion of each HS
122 * packet
123 */
Magnus Dammdeaba192010-11-17 09:53:25 +0000124 iowrite32(0x00000002 | enable, mipi->linkbase + DTCTR);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000125}
126
127static void sh_mipi_shutdown(struct platform_device *pdev)
128{
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200129 struct sh_mipi *mipi = to_sh_mipi(platform_get_drvdata(pdev));
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000130
131 sh_mipi_dsi_enable(mipi, false);
132}
133
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000134static int __init sh_mipi_setup(struct sh_mipi *mipi,
135 struct sh_mipi_dsi_info *pdata)
136{
137 void __iomem *base = mipi->base;
138 struct sh_mobile_lcdc_chan_cfg *ch = pdata->lcd_chan;
Kuninori Morimotof832906a2011-11-08 20:34:55 -0800139 u32 pctype, datatype, pixfmt, linelength, vmctr2;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800140 u32 tmp, top, bottom, delay, div;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000141 bool yuv;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800142 int bpp;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000143
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000144 /*
145 * Select data format. MIPI DSI is not hot-pluggable, so, we just use
146 * the default videomode. If this ever becomes a problem, We'll have to
147 * move this to mipi_display_on() above and use info->var.xres
148 */
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000149 switch (pdata->data_format) {
150 case MIPI_RGB888:
151 pctype = 0;
152 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
153 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000154 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000155 yuv = false;
156 break;
157 case MIPI_RGB565:
158 pctype = 1;
159 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
160 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000161 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000162 yuv = false;
163 break;
164 case MIPI_RGB666_LP:
165 pctype = 2;
166 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
167 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000168 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000169 yuv = false;
170 break;
171 case MIPI_RGB666:
172 pctype = 3;
173 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
174 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000175 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000176 yuv = false;
177 break;
178 case MIPI_BGR888:
179 pctype = 8;
180 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_24;
181 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000182 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000183 yuv = false;
184 break;
185 case MIPI_BGR565:
186 pctype = 9;
187 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_16;
188 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000189 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000190 yuv = false;
191 break;
192 case MIPI_BGR666_LP:
193 pctype = 0xa;
194 datatype = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
195 pixfmt = MIPI_DCS_PIXEL_FMT_24BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000196 linelength = ch->lcd_cfg[0].xres * 3;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000197 yuv = false;
198 break;
199 case MIPI_BGR666:
200 pctype = 0xb;
201 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_18;
202 pixfmt = MIPI_DCS_PIXEL_FMT_18BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000203 linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000204 yuv = false;
205 break;
206 case MIPI_YUYV:
207 pctype = 4;
208 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
209 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000210 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000211 yuv = true;
212 break;
213 case MIPI_UYVY:
214 pctype = 5;
215 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR16;
216 pixfmt = MIPI_DCS_PIXEL_FMT_16BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000217 linelength = ch->lcd_cfg[0].xres * 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000218 yuv = true;
219 break;
220 case MIPI_YUV420_L:
221 pctype = 6;
222 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
223 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000224 linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000225 yuv = true;
226 break;
227 case MIPI_YUV420:
228 pctype = 7;
229 datatype = MIPI_DSI_PACKED_PIXEL_STREAM_YCBCR12;
230 pixfmt = MIPI_DCS_PIXEL_FMT_12BIT;
231 /* Length of U/V line */
Guennadi Liakhovetski44432402010-09-03 07:20:04 +0000232 linelength = (ch->lcd_cfg[0].xres + 1) / 2;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000233 yuv = true;
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 if ((yuv && ch->interface_type != YUV422) ||
240 (!yuv && ch->interface_type != RGB24))
241 return -EINVAL;
242
Kuninori Morimoto26c3d7a2011-11-08 20:34:43 -0800243 if (!pdata->lane)
244 return -EINVAL;
245
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000246 /* reset DSI link */
Magnus Damm71b146c2010-11-17 06:44:25 +0000247 iowrite32(0x00000001, base + SYSCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000248 /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */
249 udelay(50);
Magnus Damm71b146c2010-11-17 06:44:25 +0000250 iowrite32(0x00000000, base + SYSCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000251
252 /* setup DSI link */
253
254 /*
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000255 * T_wakeup = 0x7000
256 * T_hs-trail = 3
257 * T_hs-prepare = 3
258 * T_clk-trail = 3
259 * T_clk-prepare = 2
260 */
Magnus Damm71b146c2010-11-17 06:44:25 +0000261 iowrite32(0x70003332, base + TIMSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000262 /* no responses requested */
Magnus Damm71b146c2010-11-17 06:44:25 +0000263 iowrite32(0x00000000, base + RESREQSET0);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000264 /* request response to packets of type 0x28 */
Magnus Damm71b146c2010-11-17 06:44:25 +0000265 iowrite32(0x00000100, base + RESREQSET1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000266 /* High-speed transmission timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000267 iowrite32(0x0fffffff, base + HSTTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000268 /* LP reception timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000269 iowrite32(0x0fffffff, base + LPRTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000270 /* Turn-around timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000271 iowrite32(0x0fffffff, base + TATOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000272 /* Peripheral reset timeout, default 0xffffffff */
Magnus Damm71b146c2010-11-17 06:44:25 +0000273 iowrite32(0x0fffffff, base + PRTOVSET);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000274 /* Interrupts not used, disable all */
275 iowrite32(0, base + DSIINTE);
276 /* DSI-Tx bias on */
Magnus Damm71b146c2010-11-17 06:44:25 +0000277 iowrite32(0x00000001, base + PHYCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000278 udelay(200);
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800279 /* Deassert resets, power on */
280 iowrite32(0x03070001, base + PHYCTRL);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000281
Kuninori Morimotoa2065a32011-11-08 20:35:56 -0800282 /*
283 * Default = ULPS enable |
284 * Contention detection enabled |
285 * EoT packet transmission enable |
286 * CRC check enable |
287 * ECC check enable
288 */
289 bitmap_fill((unsigned long *)&tmp, pdata->lane);
290 tmp |= 0x00003700;
291 iowrite32(tmp, base + SYSCONF);
292
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000293 /* setup l-bridge */
294
295 /*
296 * Enable transmission of all packets,
297 * transmit LPS after each HS packet completion
298 */
Magnus Dammdeaba192010-11-17 09:53:25 +0000299 iowrite32(0x00000006, mipi->linkbase + DTCTR);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000300 /* VSYNC width = 2 (<< 17) */
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000301 iowrite32((ch->lcd_cfg[0].vsync_len << pdata->vsynw_offset) |
302 (pdata->clksrc << 16) | (pctype << 12) | datatype,
Magnus Dammdeaba192010-11-17 09:53:25 +0000303 mipi->linkbase + VMCTR1);
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000304
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000305 /*
306 * Non-burst mode with sync pulses: VSE and HSE are output,
307 * HSA period allowed, no commands in LP
308 */
Kuninori Morimotof832906a2011-11-08 20:34:55 -0800309 vmctr2 = 0;
310 if (pdata->flags & SH_MIPI_DSI_VSEE)
311 vmctr2 |= 1 << 23;
312 if (pdata->flags & SH_MIPI_DSI_HSEE)
313 vmctr2 |= 1 << 22;
314 if (pdata->flags & SH_MIPI_DSI_HSAE)
315 vmctr2 |= 1 << 21;
Kuninori Morimotod07a9d22011-11-08 20:34:33 -0800316 if (pdata->flags & SH_MIPI_DSI_BL2E)
317 vmctr2 |= 1 << 17;
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000318 if (pdata->flags & SH_MIPI_DSI_HSABM)
Kuninori Morimoto3c2a6592011-11-08 20:34:12 -0800319 vmctr2 |= 1 << 5;
Kuninori Morimoto32ba95c2011-11-08 20:34:01 -0800320 if (pdata->flags & SH_MIPI_DSI_HBPBM)
Kuninori Morimoto3c2a6592011-11-08 20:34:12 -0800321 vmctr2 |= 1 << 4;
Kuninori Morimotof7b0af62011-11-08 20:34:24 -0800322 if (pdata->flags & SH_MIPI_DSI_HFPBM)
323 vmctr2 |= 1 << 3;
Guennadi Liakhovetski14bbb7c2010-12-29 08:12:29 +0000324 iowrite32(vmctr2, mipi->linkbase + VMCTR2);
325
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000326 /*
Kuninori Morimoto08750612011-11-08 20:35:05 -0800327 * VMLEN1 = RGBLEN | HSALEN
328 *
329 * see
330 * Video mode - Blanking Packet setting
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000331 */
Kuninori Morimoto08750612011-11-08 20:35:05 -0800332 top = linelength << 16; /* RGBLEN */
333 bottom = 0x00000001;
334 if (pdata->flags & SH_MIPI_DSI_HSABM) /* HSALEN */
335 bottom = (pdata->lane * ch->lcd_cfg[0].hsync_len) - 10;
336 iowrite32(top | bottom , mipi->linkbase + VMLEN1);
337
338 /*
339 * VMLEN2 = HBPLEN | HFPLEN
340 *
341 * see
342 * Video mode - Blanking Packet setting
343 */
344 top = 0x00010000;
345 bottom = 0x00000001;
346 delay = 0;
347
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800348 div = 1; /* HSbyteCLK is calculation base
349 * HS4divCLK = HSbyteCLK/2
350 * HS6divCLK is not supported for now */
351 if (pdata->flags & SH_MIPI_DSI_HS4divCLK)
352 div = 2;
353
Kuninori Morimoto08750612011-11-08 20:35:05 -0800354 if (pdata->flags & SH_MIPI_DSI_HFPBM) { /* HBPLEN */
355 top = ch->lcd_cfg[0].hsync_len + ch->lcd_cfg[0].left_margin;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800356 top = ((pdata->lane * top / div) - 10) << 16;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800357 }
358 if (pdata->flags & SH_MIPI_DSI_HBPBM) { /* HFPLEN */
359 bottom = ch->lcd_cfg[0].right_margin;
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800360 bottom = (pdata->lane * bottom / div) - 12;
Kuninori Morimoto08750612011-11-08 20:35:05 -0800361 }
362
363 bpp = linelength / ch->lcd_cfg[0].xres; /* byte / pixel */
Kuninori Morimotoa2e62972011-11-08 20:35:27 -0800364 if ((pdata->lane / div) > bpp) {
Kuninori Morimoto08750612011-11-08 20:35:05 -0800365 tmp = ch->lcd_cfg[0].xres / bpp; /* output cycle */
366 tmp = ch->lcd_cfg[0].xres - tmp; /* (input - output) cycle */
367 delay = (pdata->lane * tmp);
368 }
369
370 iowrite32(top | (bottom + delay) , mipi->linkbase + VMLEN2);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000371
372 msleep(5);
373
374 /* setup LCD panel */
375
376 /* cf. drivers/video/omap/lcd_mipid.c */
377 sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE);
378 msleep(120);
379 /*
380 * [7] - Page Address Mode
381 * [6] - Column Address Mode
382 * [5] - Page / Column Address Mode
383 * [4] - Display Device Line Refresh Order
384 * [3] - RGB/BGR Order
385 * [2] - Display Data Latch Data Order
386 * [1] - Flip Horizontal
387 * [0] - Flip Vertical
388 */
389 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
390 /* cf. set_data_lines() */
391 sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_PIXEL_FORMAT,
392 pixfmt << 4);
393 sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON);
394
Kuninori Morimoto97cab452011-11-08 20:36:07 -0800395 /* Enable timeout counters */
396 iowrite32(0x00000f00, base + DSICTRL);
397
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000398 return 0;
399}
400
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200401static int __mipi_display_on(struct sh_mobile_lcdc_entity *entity,
402 struct fb_info *info)
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800403{
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200404 struct sh_mipi *mipi = to_sh_mipi(entity);
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800405 struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data;
406 int ret;
407
408 pm_runtime_get_sync(&mipi->pdev->dev);
409
410 ret = pdata->set_dot_clock(mipi->pdev, mipi->base, 1);
411 if (ret < 0)
412 goto mipi_display_on_fail1;
413
414 ret = sh_mipi_setup(mipi, pdata);
415 if (ret < 0)
416 goto mipi_display_on_fail2;
417
418 sh_mipi_dsi_enable(mipi, true);
419
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200420 return 0;
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800421
422mipi_display_on_fail1:
423 pm_runtime_put_sync(&mipi->pdev->dev);
424mipi_display_on_fail2:
425 pdata->set_dot_clock(mipi->pdev, mipi->base, 0);
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200426
427 return ret;
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800428}
429
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200430static void mipi_display_on(void *arg, struct fb_info *info)
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800431{
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200432 struct sh_mobile_lcdc_entity *entity = arg;
433 struct sh_mipi *mipi = to_sh_mipi(entity);
434 int ret;
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800435
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200436 ret = __mipi_display_on(entity, info);
437 if (ret < 0)
438 return;
439
440 if (mipi->next_display_on)
441 mipi->next_display_on(mipi->next_board_data, info);
442}
443
444static void __mipi_display_off(struct sh_mobile_lcdc_entity *entity)
445{
446 struct sh_mipi *mipi = to_sh_mipi(entity);
447 struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data;
Kuninori Morimotoc2658b72011-11-08 20:35:45 -0800448
449 sh_mipi_dsi_enable(mipi, false);
450
451 pdata->set_dot_clock(mipi->pdev, mipi->base, 0);
452
453 pm_runtime_put_sync(&mipi->pdev->dev);
454}
455
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200456static void mipi_display_off(void *arg)
457{
458 struct sh_mobile_lcdc_entity *entity = arg;
459 struct sh_mipi *mipi = to_sh_mipi(entity);
460
461 if (mipi->next_display_off)
462 mipi->next_display_off(mipi->next_board_data);
463
464 __mipi_display_off(entity);
465}
466
467static const struct sh_mobile_lcdc_entity_ops mipi_ops = {
468 .display_on = __mipi_display_on,
469 .display_off = __mipi_display_off,
470};
471
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000472static int __init sh_mipi_probe(struct platform_device *pdev)
473{
474 struct sh_mipi *mipi;
475 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
476 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Dammdeaba192010-11-17 09:53:25 +0000477 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000478 unsigned long rate, f_current;
479 int idx = pdev->id, ret;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000480
Magnus Dammdeaba192010-11-17 09:53:25 +0000481 if (!res || !res2 || idx >= ARRAY_SIZE(mipi_dsi) || !pdata)
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000482 return -ENODEV;
483
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800484 if (!pdata->set_dot_clock)
485 return -EINVAL;
486
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000487 mutex_lock(&array_lock);
488 if (idx < 0)
489 for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++)
490 ;
491
492 if (idx == ARRAY_SIZE(mipi_dsi)) {
493 ret = -EBUSY;
494 goto efindslot;
495 }
496
497 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
498 if (!mipi) {
499 ret = -ENOMEM;
500 goto ealloc;
501 }
502
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200503 mipi->entity.owner = THIS_MODULE;
504 mipi->entity.ops = &mipi_ops;
505
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000506 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
507 dev_err(&pdev->dev, "MIPI register region already claimed\n");
508 ret = -EBUSY;
509 goto ereqreg;
510 }
511
512 mipi->base = ioremap(res->start, resource_size(res));
513 if (!mipi->base) {
514 ret = -ENOMEM;
515 goto emap;
516 }
517
Magnus Dammdeaba192010-11-17 09:53:25 +0000518 if (!request_mem_region(res2->start, resource_size(res2), pdev->name)) {
519 dev_err(&pdev->dev, "MIPI register region 2 already claimed\n");
520 ret = -EBUSY;
521 goto ereqreg2;
522 }
523
524 mipi->linkbase = ioremap(res2->start, resource_size(res2));
525 if (!mipi->linkbase) {
526 ret = -ENOMEM;
527 goto emap2;
528 }
529
Kuninori Morimoto7d9f88b2011-11-08 20:35:36 -0800530 mipi->pdev = pdev;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000531
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000532 mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk");
533 if (IS_ERR(mipi->dsit_clk)) {
534 ret = PTR_ERR(mipi->dsit_clk);
535 goto eclktget;
536 }
537
538 f_current = clk_get_rate(mipi->dsit_clk);
539 /* 80MHz required by the datasheet */
540 rate = clk_round_rate(mipi->dsit_clk, 80000000);
541 if (rate > 0 && rate != f_current)
542 ret = clk_set_rate(mipi->dsit_clk, rate);
543 else
544 ret = rate;
545 if (ret < 0)
546 goto esettrate;
547
548 dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate);
549
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000550 ret = clk_enable(mipi->dsit_clk);
551 if (ret < 0)
552 goto eclkton;
553
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000554 mipi_dsi[idx] = mipi;
555
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000556 pm_runtime_enable(&pdev->dev);
557 pm_runtime_resume(&pdev->dev);
558
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000559 mutex_unlock(&array_lock);
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200560 platform_set_drvdata(pdev, &mipi->entity);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000561
Magnus Damm6722a402010-11-17 06:44:54 +0000562 /* Save original LCDC callbacks */
563 mipi->next_board_data = pdata->lcd_chan->board_cfg.board_data;
564 mipi->next_display_on = pdata->lcd_chan->board_cfg.display_on;
565 mipi->next_display_off = pdata->lcd_chan->board_cfg.display_off;
566
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000567 /* Set up LCDC callbacks */
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200568 pdata->lcd_chan->board_cfg.board_data = &mipi->entity;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000569 pdata->lcd_chan->board_cfg.display_on = mipi_display_on;
570 pdata->lcd_chan->board_cfg.display_off = mipi_display_off;
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000571 pdata->lcd_chan->board_cfg.owner = THIS_MODULE;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000572
573 return 0;
574
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000575eclkton:
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000576esettrate:
577 clk_put(mipi->dsit_clk);
578eclktget:
Magnus Dammdeaba192010-11-17 09:53:25 +0000579 iounmap(mipi->linkbase);
580emap2:
581 release_mem_region(res2->start, resource_size(res2));
582ereqreg2:
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000583 iounmap(mipi->base);
584emap:
585 release_mem_region(res->start, resource_size(res));
586ereqreg:
587 kfree(mipi);
588ealloc:
589efindslot:
590 mutex_unlock(&array_lock);
591
592 return ret;
593}
594
595static int __exit sh_mipi_remove(struct platform_device *pdev)
596{
597 struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data;
598 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Dammdeaba192010-11-17 09:53:25 +0000599 struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Laurent Pinchart732bbcc2011-09-11 18:27:50 +0200600 struct sh_mipi *mipi = to_sh_mipi(platform_get_drvdata(pdev));
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000601 int i, ret;
602
603 mutex_lock(&array_lock);
604
605 for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++)
606 ;
607
608 if (i == ARRAY_SIZE(mipi_dsi)) {
609 ret = -EINVAL;
610 } else {
611 ret = 0;
612 mipi_dsi[i] = NULL;
613 }
614
615 mutex_unlock(&array_lock);
616
617 if (ret < 0)
618 return ret;
619
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000620 pdata->lcd_chan->board_cfg.owner = NULL;
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000621 pdata->lcd_chan->board_cfg.display_on = NULL;
622 pdata->lcd_chan->board_cfg.display_off = NULL;
623 pdata->lcd_chan->board_cfg.board_data = NULL;
624
Guennadi Liakhovetski236782a2010-12-27 10:23:05 +0000625 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000626 clk_disable(mipi->dsit_clk);
627 clk_put(mipi->dsit_clk);
Kuninori Morimoto5e474312011-11-08 20:35:14 -0800628
Magnus Dammdeaba192010-11-17 09:53:25 +0000629 iounmap(mipi->linkbase);
630 if (res2)
631 release_mem_region(res2->start, resource_size(res2));
Guennadi Liakhovetski9fd04fe2010-05-23 14:00:43 +0000632 iounmap(mipi->base);
633 if (res)
634 release_mem_region(res->start, resource_size(res));
635 platform_set_drvdata(pdev, NULL);
636 kfree(mipi);
637
638 return 0;
639}
640
641static struct platform_driver sh_mipi_driver = {
642 .remove = __exit_p(sh_mipi_remove),
643 .shutdown = sh_mipi_shutdown,
644 .driver = {
645 .name = "sh-mipi-dsi",
646 },
647};
648
649static int __init sh_mipi_init(void)
650{
651 return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe);
652}
653module_init(sh_mipi_init);
654
655static void __exit sh_mipi_exit(void)
656{
657 platform_driver_unregister(&sh_mipi_driver);
658}
659module_exit(sh_mipi_exit);
660
661MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
662MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver");
663MODULE_LICENSE("GPL v2");