Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 1 | /* |
| 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 Morimoto | 26c3d7a | 2011-11-08 20:34:43 -0800 | [diff] [blame] | 11 | #include <linux/bitmap.h> |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 12 | #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 Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/types.h> |
Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 22 | |
| 23 | #include <video/mipi_display.h> |
| 24 | #include <video/sh_mipi_dsi.h> |
| 25 | #include <video/sh_mobile_lcdc.h> |
| 26 | |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 27 | #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 Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 37 | #define DSIINTE 0x0060 |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 38 | #define PHYCTRL 0x0070 |
| 39 | |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 40 | /* relative to linkbase */ |
| 41 | #define DTCTR 0x0000 |
| 42 | #define VMCTR1 0x0020 |
| 43 | #define VMCTR2 0x0024 |
| 44 | #define VMLEN1 0x0028 |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 45 | #define VMLEN2 0x002c |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 46 | #define CMTSRTREQ 0x0070 |
| 47 | #define CMTSRTCTR 0x00d0 |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 48 | |
| 49 | /* E.g., sh7372 has 2 MIPI-DSIs - one for each LCDC */ |
| 50 | #define MAX_SH_MIPI_DSI 2 |
| 51 | |
| 52 | struct sh_mipi { |
| 53 | void __iomem *base; |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 54 | void __iomem *linkbase; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 55 | struct clk *dsit_clk; |
Kuninori Morimoto | 7d9f88b | 2011-11-08 20:35:36 -0800 | [diff] [blame] | 56 | struct platform_device *pdev; |
Guennadi Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 57 | |
| 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 Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static struct sh_mipi *mipi_dsi[MAX_SH_MIPI_DSI]; |
| 64 | |
| 65 | /* Protect the above array */ |
| 66 | static DEFINE_MUTEX(array_lock); |
| 67 | |
| 68 | static 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 | |
| 76 | static 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 Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 83 | iowrite32(1 | data, mipi->linkbase + CMTSRTCTR); |
| 84 | iowrite32(1, mipi->linkbase + CMTSRTREQ); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 85 | |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 86 | while ((ioread32(mipi->linkbase + CMTSRTREQ) & 1) && --cnt) |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 87 | 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 | |
| 95 | static 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 | |
| 103 | static 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 | |
| 112 | static 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 Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 118 | iowrite32(0x00000002 | enable, mipi->linkbase + DTCTR); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static 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 Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 128 | static 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 Morimoto | f832906a | 2011-11-08 20:34:55 -0800 | [diff] [blame] | 133 | u32 pctype, datatype, pixfmt, linelength, vmctr2; |
Kuninori Morimoto | a2e6297 | 2011-11-08 20:35:27 -0800 | [diff] [blame] | 134 | u32 tmp, top, bottom, delay, div; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 135 | bool yuv; |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 136 | int bpp; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 137 | |
Guennadi Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 138 | /* |
| 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 Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 143 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 148 | linelength = ch->lcd_cfg[0].xres * 3; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 149 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 155 | linelength = ch->lcd_cfg[0].xres * 2; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 156 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 162 | linelength = ch->lcd_cfg[0].xres * 3; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 163 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 169 | linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 170 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 176 | linelength = ch->lcd_cfg[0].xres * 3; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 177 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 183 | linelength = ch->lcd_cfg[0].xres * 2; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 184 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 190 | linelength = ch->lcd_cfg[0].xres * 3; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 191 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 197 | linelength = (ch->lcd_cfg[0].xres * 18 + 7) / 8; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 198 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 204 | linelength = ch->lcd_cfg[0].xres * 2; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 205 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 211 | linelength = ch->lcd_cfg[0].xres * 2; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 212 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 218 | linelength = (ch->lcd_cfg[0].xres * 12 + 7) / 8; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 219 | 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 Liakhovetski | 4443240 | 2010-09-03 07:20:04 +0000 | [diff] [blame] | 226 | linelength = (ch->lcd_cfg[0].xres + 1) / 2; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 227 | 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 Morimoto | 26c3d7a | 2011-11-08 20:34:43 -0800 | [diff] [blame] | 237 | if (!pdata->lane) |
| 238 | return -EINVAL; |
| 239 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 240 | /* reset DSI link */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 241 | iowrite32(0x00000001, base + SYSCTRL); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 242 | /* Hold reset for 100 cycles of the slowest of bus, HS byte and LP clock */ |
| 243 | udelay(50); |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 244 | iowrite32(0x00000000, base + SYSCTRL); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 245 | |
| 246 | /* setup DSI link */ |
| 247 | |
| 248 | /* |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 249 | * 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 Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 255 | iowrite32(0x70003332, base + TIMSET); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 256 | /* no responses requested */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 257 | iowrite32(0x00000000, base + RESREQSET0); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 258 | /* request response to packets of type 0x28 */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 259 | iowrite32(0x00000100, base + RESREQSET1); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 260 | /* High-speed transmission timeout, default 0xffffffff */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 261 | iowrite32(0x0fffffff, base + HSTTOVSET); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 262 | /* LP reception timeout, default 0xffffffff */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 263 | iowrite32(0x0fffffff, base + LPRTOVSET); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 264 | /* Turn-around timeout, default 0xffffffff */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 265 | iowrite32(0x0fffffff, base + TATOVSET); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 266 | /* Peripheral reset timeout, default 0xffffffff */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 267 | iowrite32(0x0fffffff, base + PRTOVSET); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 268 | /* Enable timeout counters */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 269 | iowrite32(0x00000f00, base + DSICTRL); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 270 | /* Interrupts not used, disable all */ |
| 271 | iowrite32(0, base + DSIINTE); |
| 272 | /* DSI-Tx bias on */ |
Magnus Damm | 71b146c | 2010-11-17 06:44:25 +0000 | [diff] [blame] | 273 | iowrite32(0x00000001, base + PHYCTRL); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 274 | udelay(200); |
Kuninori Morimoto | 5e47431 | 2011-11-08 20:35:14 -0800 | [diff] [blame] | 275 | /* Deassert resets, power on */ |
| 276 | iowrite32(0x03070001, base + PHYCTRL); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 277 | |
Kuninori Morimoto | a2065a3 | 2011-11-08 20:35:56 -0800 | [diff] [blame^] | 278 | /* |
| 279 | * Default = ULPS enable | |
| 280 | * Contention detection enabled | |
| 281 | * EoT packet transmission enable | |
| 282 | * CRC check enable | |
| 283 | * ECC check enable |
| 284 | */ |
| 285 | bitmap_fill((unsigned long *)&tmp, pdata->lane); |
| 286 | tmp |= 0x00003700; |
| 287 | iowrite32(tmp, base + SYSCONF); |
| 288 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 289 | /* setup l-bridge */ |
| 290 | |
| 291 | /* |
| 292 | * Enable transmission of all packets, |
| 293 | * transmit LPS after each HS packet completion |
| 294 | */ |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 295 | iowrite32(0x00000006, mipi->linkbase + DTCTR); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 296 | /* VSYNC width = 2 (<< 17) */ |
Guennadi Liakhovetski | 14bbb7c | 2010-12-29 08:12:29 +0000 | [diff] [blame] | 297 | iowrite32((ch->lcd_cfg[0].vsync_len << pdata->vsynw_offset) | |
| 298 | (pdata->clksrc << 16) | (pctype << 12) | datatype, |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 299 | mipi->linkbase + VMCTR1); |
Guennadi Liakhovetski | 14bbb7c | 2010-12-29 08:12:29 +0000 | [diff] [blame] | 300 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 301 | /* |
| 302 | * Non-burst mode with sync pulses: VSE and HSE are output, |
| 303 | * HSA period allowed, no commands in LP |
| 304 | */ |
Kuninori Morimoto | f832906a | 2011-11-08 20:34:55 -0800 | [diff] [blame] | 305 | vmctr2 = 0; |
| 306 | if (pdata->flags & SH_MIPI_DSI_VSEE) |
| 307 | vmctr2 |= 1 << 23; |
| 308 | if (pdata->flags & SH_MIPI_DSI_HSEE) |
| 309 | vmctr2 |= 1 << 22; |
| 310 | if (pdata->flags & SH_MIPI_DSI_HSAE) |
| 311 | vmctr2 |= 1 << 21; |
Kuninori Morimoto | d07a9d2 | 2011-11-08 20:34:33 -0800 | [diff] [blame] | 312 | if (pdata->flags & SH_MIPI_DSI_BL2E) |
| 313 | vmctr2 |= 1 << 17; |
Guennadi Liakhovetski | 14bbb7c | 2010-12-29 08:12:29 +0000 | [diff] [blame] | 314 | if (pdata->flags & SH_MIPI_DSI_HSABM) |
Kuninori Morimoto | 3c2a659 | 2011-11-08 20:34:12 -0800 | [diff] [blame] | 315 | vmctr2 |= 1 << 5; |
Kuninori Morimoto | 32ba95c | 2011-11-08 20:34:01 -0800 | [diff] [blame] | 316 | if (pdata->flags & SH_MIPI_DSI_HBPBM) |
Kuninori Morimoto | 3c2a659 | 2011-11-08 20:34:12 -0800 | [diff] [blame] | 317 | vmctr2 |= 1 << 4; |
Kuninori Morimoto | f7b0af6 | 2011-11-08 20:34:24 -0800 | [diff] [blame] | 318 | if (pdata->flags & SH_MIPI_DSI_HFPBM) |
| 319 | vmctr2 |= 1 << 3; |
Guennadi Liakhovetski | 14bbb7c | 2010-12-29 08:12:29 +0000 | [diff] [blame] | 320 | iowrite32(vmctr2, mipi->linkbase + VMCTR2); |
| 321 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 322 | /* |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 323 | * VMLEN1 = RGBLEN | HSALEN |
| 324 | * |
| 325 | * see |
| 326 | * Video mode - Blanking Packet setting |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 327 | */ |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 328 | top = linelength << 16; /* RGBLEN */ |
| 329 | bottom = 0x00000001; |
| 330 | if (pdata->flags & SH_MIPI_DSI_HSABM) /* HSALEN */ |
| 331 | bottom = (pdata->lane * ch->lcd_cfg[0].hsync_len) - 10; |
| 332 | iowrite32(top | bottom , mipi->linkbase + VMLEN1); |
| 333 | |
| 334 | /* |
| 335 | * VMLEN2 = HBPLEN | HFPLEN |
| 336 | * |
| 337 | * see |
| 338 | * Video mode - Blanking Packet setting |
| 339 | */ |
| 340 | top = 0x00010000; |
| 341 | bottom = 0x00000001; |
| 342 | delay = 0; |
| 343 | |
Kuninori Morimoto | a2e6297 | 2011-11-08 20:35:27 -0800 | [diff] [blame] | 344 | div = 1; /* HSbyteCLK is calculation base |
| 345 | * HS4divCLK = HSbyteCLK/2 |
| 346 | * HS6divCLK is not supported for now */ |
| 347 | if (pdata->flags & SH_MIPI_DSI_HS4divCLK) |
| 348 | div = 2; |
| 349 | |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 350 | if (pdata->flags & SH_MIPI_DSI_HFPBM) { /* HBPLEN */ |
| 351 | top = ch->lcd_cfg[0].hsync_len + ch->lcd_cfg[0].left_margin; |
Kuninori Morimoto | a2e6297 | 2011-11-08 20:35:27 -0800 | [diff] [blame] | 352 | top = ((pdata->lane * top / div) - 10) << 16; |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 353 | } |
| 354 | if (pdata->flags & SH_MIPI_DSI_HBPBM) { /* HFPLEN */ |
| 355 | bottom = ch->lcd_cfg[0].right_margin; |
Kuninori Morimoto | a2e6297 | 2011-11-08 20:35:27 -0800 | [diff] [blame] | 356 | bottom = (pdata->lane * bottom / div) - 12; |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | bpp = linelength / ch->lcd_cfg[0].xres; /* byte / pixel */ |
Kuninori Morimoto | a2e6297 | 2011-11-08 20:35:27 -0800 | [diff] [blame] | 360 | if ((pdata->lane / div) > bpp) { |
Kuninori Morimoto | 0875061 | 2011-11-08 20:35:05 -0800 | [diff] [blame] | 361 | tmp = ch->lcd_cfg[0].xres / bpp; /* output cycle */ |
| 362 | tmp = ch->lcd_cfg[0].xres - tmp; /* (input - output) cycle */ |
| 363 | delay = (pdata->lane * tmp); |
| 364 | } |
| 365 | |
| 366 | iowrite32(top | (bottom + delay) , mipi->linkbase + VMLEN2); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 367 | |
| 368 | msleep(5); |
| 369 | |
| 370 | /* setup LCD panel */ |
| 371 | |
| 372 | /* cf. drivers/video/omap/lcd_mipid.c */ |
| 373 | sh_mipi_dcs(ch->chan, MIPI_DCS_EXIT_SLEEP_MODE); |
| 374 | msleep(120); |
| 375 | /* |
| 376 | * [7] - Page Address Mode |
| 377 | * [6] - Column Address Mode |
| 378 | * [5] - Page / Column Address Mode |
| 379 | * [4] - Display Device Line Refresh Order |
| 380 | * [3] - RGB/BGR Order |
| 381 | * [2] - Display Data Latch Data Order |
| 382 | * [1] - Flip Horizontal |
| 383 | * [0] - Flip Vertical |
| 384 | */ |
| 385 | sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_ADDRESS_MODE, 0x00); |
| 386 | /* cf. set_data_lines() */ |
| 387 | sh_mipi_dcs_param(ch->chan, MIPI_DCS_SET_PIXEL_FORMAT, |
| 388 | pixfmt << 4); |
| 389 | sh_mipi_dcs(ch->chan, MIPI_DCS_SET_DISPLAY_ON); |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Kuninori Morimoto | c2658b7 | 2011-11-08 20:35:45 -0800 | [diff] [blame] | 394 | static void mipi_display_on(void *arg, struct fb_info *info) |
| 395 | { |
| 396 | struct sh_mipi *mipi = arg; |
| 397 | struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data; |
| 398 | int ret; |
| 399 | |
| 400 | pm_runtime_get_sync(&mipi->pdev->dev); |
| 401 | |
| 402 | ret = pdata->set_dot_clock(mipi->pdev, mipi->base, 1); |
| 403 | if (ret < 0) |
| 404 | goto mipi_display_on_fail1; |
| 405 | |
| 406 | ret = sh_mipi_setup(mipi, pdata); |
| 407 | if (ret < 0) |
| 408 | goto mipi_display_on_fail2; |
| 409 | |
| 410 | sh_mipi_dsi_enable(mipi, true); |
| 411 | |
| 412 | if (mipi->next_display_on) |
| 413 | mipi->next_display_on(mipi->next_board_data, info); |
| 414 | |
| 415 | return; |
| 416 | |
| 417 | mipi_display_on_fail1: |
| 418 | pm_runtime_put_sync(&mipi->pdev->dev); |
| 419 | mipi_display_on_fail2: |
| 420 | pdata->set_dot_clock(mipi->pdev, mipi->base, 0); |
| 421 | } |
| 422 | |
| 423 | static void mipi_display_off(void *arg) |
| 424 | { |
| 425 | struct sh_mipi *mipi = arg; |
| 426 | struct sh_mipi_dsi_info *pdata = mipi->pdev->dev.platform_data; |
| 427 | |
| 428 | if (mipi->next_display_off) |
| 429 | mipi->next_display_off(mipi->next_board_data); |
| 430 | |
| 431 | sh_mipi_dsi_enable(mipi, false); |
| 432 | |
| 433 | pdata->set_dot_clock(mipi->pdev, mipi->base, 0); |
| 434 | |
| 435 | pm_runtime_put_sync(&mipi->pdev->dev); |
| 436 | } |
| 437 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 438 | static int __init sh_mipi_probe(struct platform_device *pdev) |
| 439 | { |
| 440 | struct sh_mipi *mipi; |
| 441 | struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data; |
| 442 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 443 | struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 444 | unsigned long rate, f_current; |
| 445 | int idx = pdev->id, ret; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 446 | |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 447 | if (!res || !res2 || idx >= ARRAY_SIZE(mipi_dsi) || !pdata) |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 448 | return -ENODEV; |
| 449 | |
Kuninori Morimoto | 5e47431 | 2011-11-08 20:35:14 -0800 | [diff] [blame] | 450 | if (!pdata->set_dot_clock) |
| 451 | return -EINVAL; |
| 452 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 453 | mutex_lock(&array_lock); |
| 454 | if (idx < 0) |
| 455 | for (idx = 0; idx < ARRAY_SIZE(mipi_dsi) && mipi_dsi[idx]; idx++) |
| 456 | ; |
| 457 | |
| 458 | if (idx == ARRAY_SIZE(mipi_dsi)) { |
| 459 | ret = -EBUSY; |
| 460 | goto efindslot; |
| 461 | } |
| 462 | |
| 463 | mipi = kzalloc(sizeof(*mipi), GFP_KERNEL); |
| 464 | if (!mipi) { |
| 465 | ret = -ENOMEM; |
| 466 | goto ealloc; |
| 467 | } |
| 468 | |
| 469 | if (!request_mem_region(res->start, resource_size(res), pdev->name)) { |
| 470 | dev_err(&pdev->dev, "MIPI register region already claimed\n"); |
| 471 | ret = -EBUSY; |
| 472 | goto ereqreg; |
| 473 | } |
| 474 | |
| 475 | mipi->base = ioremap(res->start, resource_size(res)); |
| 476 | if (!mipi->base) { |
| 477 | ret = -ENOMEM; |
| 478 | goto emap; |
| 479 | } |
| 480 | |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 481 | if (!request_mem_region(res2->start, resource_size(res2), pdev->name)) { |
| 482 | dev_err(&pdev->dev, "MIPI register region 2 already claimed\n"); |
| 483 | ret = -EBUSY; |
| 484 | goto ereqreg2; |
| 485 | } |
| 486 | |
| 487 | mipi->linkbase = ioremap(res2->start, resource_size(res2)); |
| 488 | if (!mipi->linkbase) { |
| 489 | ret = -ENOMEM; |
| 490 | goto emap2; |
| 491 | } |
| 492 | |
Kuninori Morimoto | 7d9f88b | 2011-11-08 20:35:36 -0800 | [diff] [blame] | 493 | mipi->pdev = pdev; |
Guennadi Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 494 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 495 | mipi->dsit_clk = clk_get(&pdev->dev, "dsit_clk"); |
| 496 | if (IS_ERR(mipi->dsit_clk)) { |
| 497 | ret = PTR_ERR(mipi->dsit_clk); |
| 498 | goto eclktget; |
| 499 | } |
| 500 | |
| 501 | f_current = clk_get_rate(mipi->dsit_clk); |
| 502 | /* 80MHz required by the datasheet */ |
| 503 | rate = clk_round_rate(mipi->dsit_clk, 80000000); |
| 504 | if (rate > 0 && rate != f_current) |
| 505 | ret = clk_set_rate(mipi->dsit_clk, rate); |
| 506 | else |
| 507 | ret = rate; |
| 508 | if (ret < 0) |
| 509 | goto esettrate; |
| 510 | |
| 511 | dev_dbg(&pdev->dev, "DSI-T clk %lu -> %lu\n", f_current, rate); |
| 512 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 513 | ret = clk_enable(mipi->dsit_clk); |
| 514 | if (ret < 0) |
| 515 | goto eclkton; |
| 516 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 517 | mipi_dsi[idx] = mipi; |
| 518 | |
Guennadi Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 519 | pm_runtime_enable(&pdev->dev); |
| 520 | pm_runtime_resume(&pdev->dev); |
| 521 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 522 | mutex_unlock(&array_lock); |
| 523 | platform_set_drvdata(pdev, mipi); |
| 524 | |
Magnus Damm | 6722a40 | 2010-11-17 06:44:54 +0000 | [diff] [blame] | 525 | /* Save original LCDC callbacks */ |
| 526 | mipi->next_board_data = pdata->lcd_chan->board_cfg.board_data; |
| 527 | mipi->next_display_on = pdata->lcd_chan->board_cfg.display_on; |
| 528 | mipi->next_display_off = pdata->lcd_chan->board_cfg.display_off; |
| 529 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 530 | /* Set up LCDC callbacks */ |
| 531 | pdata->lcd_chan->board_cfg.board_data = mipi; |
| 532 | pdata->lcd_chan->board_cfg.display_on = mipi_display_on; |
| 533 | pdata->lcd_chan->board_cfg.display_off = mipi_display_off; |
Guennadi Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 534 | pdata->lcd_chan->board_cfg.owner = THIS_MODULE; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 535 | |
| 536 | return 0; |
| 537 | |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 538 | eclkton: |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 539 | esettrate: |
| 540 | clk_put(mipi->dsit_clk); |
| 541 | eclktget: |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 542 | iounmap(mipi->linkbase); |
| 543 | emap2: |
| 544 | release_mem_region(res2->start, resource_size(res2)); |
| 545 | ereqreg2: |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 546 | iounmap(mipi->base); |
| 547 | emap: |
| 548 | release_mem_region(res->start, resource_size(res)); |
| 549 | ereqreg: |
| 550 | kfree(mipi); |
| 551 | ealloc: |
| 552 | efindslot: |
| 553 | mutex_unlock(&array_lock); |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static int __exit sh_mipi_remove(struct platform_device *pdev) |
| 559 | { |
| 560 | struct sh_mipi_dsi_info *pdata = pdev->dev.platform_data; |
| 561 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 562 | struct resource *res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 563 | struct sh_mipi *mipi = platform_get_drvdata(pdev); |
| 564 | int i, ret; |
| 565 | |
| 566 | mutex_lock(&array_lock); |
| 567 | |
| 568 | for (i = 0; i < ARRAY_SIZE(mipi_dsi) && mipi_dsi[i] != mipi; i++) |
| 569 | ; |
| 570 | |
| 571 | if (i == ARRAY_SIZE(mipi_dsi)) { |
| 572 | ret = -EINVAL; |
| 573 | } else { |
| 574 | ret = 0; |
| 575 | mipi_dsi[i] = NULL; |
| 576 | } |
| 577 | |
| 578 | mutex_unlock(&array_lock); |
| 579 | |
| 580 | if (ret < 0) |
| 581 | return ret; |
| 582 | |
Guennadi Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 583 | pdata->lcd_chan->board_cfg.owner = NULL; |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 584 | pdata->lcd_chan->board_cfg.display_on = NULL; |
| 585 | pdata->lcd_chan->board_cfg.display_off = NULL; |
| 586 | pdata->lcd_chan->board_cfg.board_data = NULL; |
| 587 | |
Guennadi Liakhovetski | 236782a | 2010-12-27 10:23:05 +0000 | [diff] [blame] | 588 | pm_runtime_disable(&pdev->dev); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 589 | clk_disable(mipi->dsit_clk); |
| 590 | clk_put(mipi->dsit_clk); |
Kuninori Morimoto | 5e47431 | 2011-11-08 20:35:14 -0800 | [diff] [blame] | 591 | |
Magnus Damm | deaba19 | 2010-11-17 09:53:25 +0000 | [diff] [blame] | 592 | iounmap(mipi->linkbase); |
| 593 | if (res2) |
| 594 | release_mem_region(res2->start, resource_size(res2)); |
Guennadi Liakhovetski | 9fd04fe | 2010-05-23 14:00:43 +0000 | [diff] [blame] | 595 | iounmap(mipi->base); |
| 596 | if (res) |
| 597 | release_mem_region(res->start, resource_size(res)); |
| 598 | platform_set_drvdata(pdev, NULL); |
| 599 | kfree(mipi); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static struct platform_driver sh_mipi_driver = { |
| 605 | .remove = __exit_p(sh_mipi_remove), |
| 606 | .shutdown = sh_mipi_shutdown, |
| 607 | .driver = { |
| 608 | .name = "sh-mipi-dsi", |
| 609 | }, |
| 610 | }; |
| 611 | |
| 612 | static int __init sh_mipi_init(void) |
| 613 | { |
| 614 | return platform_driver_probe(&sh_mipi_driver, sh_mipi_probe); |
| 615 | } |
| 616 | module_init(sh_mipi_init); |
| 617 | |
| 618 | static void __exit sh_mipi_exit(void) |
| 619 | { |
| 620 | platform_driver_unregister(&sh_mipi_driver); |
| 621 | } |
| 622 | module_exit(sh_mipi_exit); |
| 623 | |
| 624 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 625 | MODULE_DESCRIPTION("SuperH / ARM-shmobile MIPI DSI driver"); |
| 626 | MODULE_LICENSE("GPL v2"); |