2 * Driver for the SH-Mobile MIPI CSI-2 unit
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
18 #include <linux/module.h>
20 #include <media/sh_mobile_ceu.h>
21 #include <media/sh_mobile_csi2.h>
22 #include <media/soc_camera.h>
23 #include <media/soc_mediabus.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-mediabus.h>
28 #include <media/v4l2-subdev.h>
30 #define SH_CSI2_TREF 0x00
31 #define SH_CSI2_SRST 0x04
32 #define SH_CSI2_PHYCNT 0x08
33 #define SH_CSI2_CHKSUM 0x0C
34 #define SH_CSI2_VCDT 0x10
37 struct v4l2_subdev subdev
;
38 struct list_head list
;
40 unsigned long mipi_flags
;
42 struct platform_device
*pdev
;
43 struct sh_csi2_client_config
*client
;
46 static int sh_csi2_try_fmt(struct v4l2_subdev
*sd
,
47 struct v4l2_mbus_framefmt
*mf
)
49 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
50 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
54 else if (mf
->width
& 1)
57 switch (pdata
->type
) {
60 case V4L2_MBUS_FMT_UYVY8_2X8
: /* YUV422 */
61 case V4L2_MBUS_FMT_YUYV8_1_5X8
: /* YUV420 */
62 case V4L2_MBUS_FMT_Y8_1X8
: /* RAW8 */
63 case V4L2_MBUS_FMT_SBGGR8_1X8
:
64 case V4L2_MBUS_FMT_SGRBG8_1X8
:
67 /* All MIPI CSI-2 devices must support one of primary formats */
68 mf
->code
= V4L2_MBUS_FMT_YUYV8_2X8
;
73 case V4L2_MBUS_FMT_Y8_1X8
: /* RAW8 */
74 case V4L2_MBUS_FMT_SBGGR8_1X8
:
75 case V4L2_MBUS_FMT_SGRBG8_1X8
:
76 case V4L2_MBUS_FMT_SBGGR10_1X10
: /* RAW10 */
77 case V4L2_MBUS_FMT_SBGGR12_1X12
: /* RAW12 */
80 /* All MIPI CSI-2 devices must support one of primary formats */
81 mf
->code
= V4L2_MBUS_FMT_SBGGR8_1X8
;
90 * We have done our best in try_fmt to try and tell the sensor, which formats
91 * we support. If now the configuration is unsuitable for us we can only
94 static int sh_csi2_s_fmt(struct v4l2_subdev
*sd
,
95 struct v4l2_mbus_framefmt
*mf
)
97 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
98 u32 tmp
= (priv
->client
->channel
& 3) << 8;
100 dev_dbg(sd
->v4l2_dev
->dev
, "%s(%u)\n", __func__
, mf
->code
);
101 if (mf
->width
> 8188 || mf
->width
& 1)
105 case V4L2_MBUS_FMT_UYVY8_2X8
:
106 tmp
|= 0x1e; /* YUV422 8 bit */
108 case V4L2_MBUS_FMT_YUYV8_1_5X8
:
109 tmp
|= 0x18; /* YUV420 8 bit */
111 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE
:
112 tmp
|= 0x21; /* RGB555 */
114 case V4L2_MBUS_FMT_RGB565_2X8_BE
:
115 tmp
|= 0x22; /* RGB565 */
117 case V4L2_MBUS_FMT_Y8_1X8
:
118 case V4L2_MBUS_FMT_SBGGR8_1X8
:
119 case V4L2_MBUS_FMT_SGRBG8_1X8
:
120 tmp
|= 0x2a; /* RAW8 */
126 iowrite32(tmp
, priv
->base
+ SH_CSI2_VCDT
);
131 static int sh_csi2_g_mbus_config(struct v4l2_subdev
*sd
,
132 struct v4l2_mbus_config
*cfg
)
134 cfg
->flags
= V4L2_MBUS_PCLK_SAMPLE_RISING
|
135 V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_HIGH
|
136 V4L2_MBUS_MASTER
| V4L2_MBUS_DATA_ACTIVE_HIGH
;
137 cfg
->type
= V4L2_MBUS_PARALLEL
;
142 static int sh_csi2_s_mbus_config(struct v4l2_subdev
*sd
,
143 const struct v4l2_mbus_config
*cfg
)
145 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
146 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(sd
);
147 struct v4l2_subdev
*client_sd
= soc_camera_to_subdev(icd
);
148 struct v4l2_mbus_config client_cfg
= {.type
= V4L2_MBUS_CSI2
,
149 .flags
= priv
->mipi_flags
};
151 return v4l2_subdev_call(client_sd
, video
, s_mbus_config
, &client_cfg
);
154 static struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops
= {
155 .s_mbus_fmt
= sh_csi2_s_fmt
,
156 .try_mbus_fmt
= sh_csi2_try_fmt
,
157 .g_mbus_config
= sh_csi2_g_mbus_config
,
158 .s_mbus_config
= sh_csi2_s_mbus_config
,
161 static void sh_csi2_hwinit(struct sh_csi2
*priv
)
163 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
164 __u32 tmp
= 0x10; /* Enable MIPI CSI clock lane */
166 /* Reflect registers immediately */
167 iowrite32(0x00000001, priv
->base
+ SH_CSI2_TREF
);
168 /* reset CSI2 harware */
169 iowrite32(0x00000001, priv
->base
+ SH_CSI2_SRST
);
171 iowrite32(0x00000000, priv
->base
+ SH_CSI2_SRST
);
173 switch (pdata
->type
) {
175 if (priv
->client
->lanes
== 1)
178 /* Default - both lanes */
182 if (!priv
->client
->lanes
|| priv
->client
->lanes
> 4)
183 /* Default - all 4 lanes */
186 tmp
|= (1 << priv
->client
->lanes
) - 1;
189 if (priv
->client
->phy
== SH_CSI2_PHY_MAIN
)
192 iowrite32(tmp
, priv
->base
+ SH_CSI2_PHYCNT
);
195 if (pdata
->flags
& SH_CSI2_ECC
)
197 if (pdata
->flags
& SH_CSI2_CRC
)
199 iowrite32(tmp
, priv
->base
+ SH_CSI2_CHKSUM
);
202 static int sh_csi2_client_connect(struct sh_csi2
*priv
)
204 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
205 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(&priv
->subdev
);
206 struct v4l2_subdev
*client_sd
= soc_camera_to_subdev(icd
);
207 struct device
*dev
= v4l2_get_subdevdata(&priv
->subdev
);
208 struct v4l2_mbus_config cfg
;
209 unsigned long common_flags
, csi2_flags
;
215 for (i
= 0; i
< pdata
->num_clients
; i
++)
216 if (&pdata
->clients
[i
].pdev
->dev
== icd
->pdev
)
219 dev_dbg(dev
, "%s(%p): found #%d\n", __func__
, dev
, i
);
221 if (i
== pdata
->num_clients
)
224 /* Check if we can support this camera */
225 csi2_flags
= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK
| V4L2_MBUS_CSI2_1_LANE
;
227 switch (pdata
->type
) {
229 if (pdata
->clients
[i
].lanes
!= 1)
230 csi2_flags
|= V4L2_MBUS_CSI2_2_LANE
;
233 switch (pdata
->clients
[i
].lanes
) {
235 csi2_flags
|= V4L2_MBUS_CSI2_4_LANE
;
237 csi2_flags
|= V4L2_MBUS_CSI2_3_LANE
;
239 csi2_flags
|= V4L2_MBUS_CSI2_2_LANE
;
243 cfg
.type
= V4L2_MBUS_CSI2
;
244 ret
= v4l2_subdev_call(client_sd
, video
, g_mbus_config
, &cfg
);
245 if (ret
== -ENOIOCTLCMD
)
246 common_flags
= csi2_flags
;
248 common_flags
= soc_mbus_config_compatible(&cfg
,
256 /* All good: camera MIPI configuration supported */
257 priv
->mipi_flags
= common_flags
;
258 priv
->client
= pdata
->clients
+ i
;
260 pm_runtime_get_sync(dev
);
262 sh_csi2_hwinit(priv
);
267 static void sh_csi2_client_disconnect(struct sh_csi2
*priv
)
274 pm_runtime_put(v4l2_get_subdevdata(&priv
->subdev
));
277 static int sh_csi2_s_power(struct v4l2_subdev
*sd
, int on
)
279 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
282 return sh_csi2_client_connect(priv
);
284 sh_csi2_client_disconnect(priv
);
288 static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops
= {
289 .s_power
= sh_csi2_s_power
,
292 static struct v4l2_subdev_ops sh_csi2_subdev_ops
= {
293 .core
= &sh_csi2_subdev_core_ops
,
294 .video
= &sh_csi2_subdev_video_ops
,
297 static __devinit
int sh_csi2_probe(struct platform_device
*pdev
)
299 struct resource
*res
;
302 struct sh_csi2
*priv
;
303 /* Platform data specify the PHY, lanes, ECC, CRC */
304 struct sh_csi2_pdata
*pdata
= pdev
->dev
.platform_data
;
306 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
307 /* Interrupt unused so far */
308 irq
= platform_get_irq(pdev
, 0);
310 if (!res
|| (int)irq
<= 0 || !pdata
) {
311 dev_err(&pdev
->dev
, "Not enough CSI2 platform resources.\n");
315 /* TODO: Add support for CSI2I. Careful: different register layout! */
316 if (pdata
->type
!= SH_CSI2C
) {
317 dev_err(&pdev
->dev
, "Only CSI2C supported ATM.\n");
321 priv
= kzalloc(sizeof(struct sh_csi2
), GFP_KERNEL
);
327 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
328 dev_err(&pdev
->dev
, "CSI2 register region already claimed\n");
333 priv
->base
= ioremap(res
->start
, resource_size(res
));
336 dev_err(&pdev
->dev
, "Unable to ioremap CSI2 registers.\n");
341 platform_set_drvdata(pdev
, priv
);
343 v4l2_subdev_init(&priv
->subdev
, &sh_csi2_subdev_ops
);
344 v4l2_set_subdevdata(&priv
->subdev
, &pdev
->dev
);
346 snprintf(priv
->subdev
.name
, V4L2_SUBDEV_NAME_SIZE
, "%s.mipi-csi",
347 dev_name(pdata
->v4l2_dev
->dev
));
348 ret
= v4l2_device_register_subdev(pdata
->v4l2_dev
, &priv
->subdev
);
349 dev_dbg(&pdev
->dev
, "%s(%p): ret(register_subdev) = %d\n", __func__
, priv
, ret
);
353 pm_runtime_enable(&pdev
->dev
);
355 dev_dbg(&pdev
->dev
, "CSI2 probed.\n");
362 release_mem_region(res
->start
, resource_size(res
));
369 static __devexit
int sh_csi2_remove(struct platform_device
*pdev
)
371 struct sh_csi2
*priv
= platform_get_drvdata(pdev
);
372 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
374 v4l2_device_unregister_subdev(&priv
->subdev
);
375 pm_runtime_disable(&pdev
->dev
);
377 release_mem_region(res
->start
, resource_size(res
));
378 platform_set_drvdata(pdev
, NULL
);
384 static struct platform_driver __refdata sh_csi2_pdrv
= {
385 .remove
= __devexit_p(sh_csi2_remove
),
386 .probe
= sh_csi2_probe
,
388 .name
= "sh-mobile-csi2",
389 .owner
= THIS_MODULE
,
393 module_platform_driver(sh_csi2_pdrv
);
395 MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
396 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
397 MODULE_LICENSE("GPL v2");
398 MODULE_ALIAS("platform:sh-mobile-csi2");