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>
19 #include <media/sh_mobile_csi2.h>
20 #include <media/soc_camera.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-mediabus.h>
25 #include <media/v4l2-subdev.h>
27 #define SH_CSI2_TREF 0x00
28 #define SH_CSI2_SRST 0x04
29 #define SH_CSI2_PHYCNT 0x08
30 #define SH_CSI2_CHKSUM 0x0C
31 #define SH_CSI2_VCDT 0x10
34 struct v4l2_subdev subdev
;
35 struct list_head list
;
36 struct notifier_block notifier
;
39 struct platform_device
*pdev
;
40 struct sh_csi2_client_config
*client
;
43 static int sh_csi2_try_fmt(struct v4l2_subdev
*sd
,
44 struct v4l2_mbus_framefmt
*mf
)
46 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
47 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
51 else if (mf
->width
& 1)
54 switch (pdata
->type
) {
57 case V4L2_MBUS_FMT_UYVY8_2X8
: /* YUV422 */
58 case V4L2_MBUS_FMT_YUYV8_1_5X8
: /* YUV420 */
59 case V4L2_MBUS_FMT_Y8_1X8
: /* RAW8 */
60 case V4L2_MBUS_FMT_SBGGR8_1X8
:
61 case V4L2_MBUS_FMT_SGRBG8_1X8
:
64 /* All MIPI CSI-2 devices must support one of primary formats */
65 mf
->code
= V4L2_MBUS_FMT_YUYV8_2X8
;
70 case V4L2_MBUS_FMT_Y8_1X8
: /* RAW8 */
71 case V4L2_MBUS_FMT_SBGGR8_1X8
:
72 case V4L2_MBUS_FMT_SGRBG8_1X8
:
73 case V4L2_MBUS_FMT_SBGGR10_1X10
: /* RAW10 */
74 case V4L2_MBUS_FMT_SBGGR12_1X12
: /* RAW12 */
77 /* All MIPI CSI-2 devices must support one of primary formats */
78 mf
->code
= V4L2_MBUS_FMT_SBGGR8_1X8
;
87 * We have done our best in try_fmt to try and tell the sensor, which formats
88 * we support. If now the configuration is unsuitable for us we can only
91 static int sh_csi2_s_fmt(struct v4l2_subdev
*sd
,
92 struct v4l2_mbus_framefmt
*mf
)
94 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
95 u32 tmp
= (priv
->client
->channel
& 3) << 8;
97 dev_dbg(sd
->v4l2_dev
->dev
, "%s(%u)\n", __func__
, mf
->code
);
98 if (mf
->width
> 8188 || mf
->width
& 1)
102 case V4L2_MBUS_FMT_UYVY8_2X8
:
103 tmp
|= 0x1e; /* YUV422 8 bit */
105 case V4L2_MBUS_FMT_YUYV8_1_5X8
:
106 tmp
|= 0x18; /* YUV420 8 bit */
108 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE
:
109 tmp
|= 0x21; /* RGB555 */
111 case V4L2_MBUS_FMT_RGB565_2X8_BE
:
112 tmp
|= 0x22; /* RGB565 */
114 case V4L2_MBUS_FMT_Y8_1X8
:
115 case V4L2_MBUS_FMT_SBGGR8_1X8
:
116 case V4L2_MBUS_FMT_SGRBG8_1X8
:
117 tmp
|= 0x2a; /* RAW8 */
123 iowrite32(tmp
, priv
->base
+ SH_CSI2_VCDT
);
128 static struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops
= {
129 .s_mbus_fmt
= sh_csi2_s_fmt
,
130 .try_mbus_fmt
= sh_csi2_try_fmt
,
133 static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops
;
135 static struct v4l2_subdev_ops sh_csi2_subdev_ops
= {
136 .core
= &sh_csi2_subdev_core_ops
,
137 .video
= &sh_csi2_subdev_video_ops
,
140 static void sh_csi2_hwinit(struct sh_csi2
*priv
)
142 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
143 __u32 tmp
= 0x10; /* Enable MIPI CSI clock lane */
145 /* Reflect registers immediately */
146 iowrite32(0x00000001, priv
->base
+ SH_CSI2_TREF
);
147 /* reset CSI2 harware */
148 iowrite32(0x00000001, priv
->base
+ SH_CSI2_SRST
);
150 iowrite32(0x00000000, priv
->base
+ SH_CSI2_SRST
);
152 if (priv
->client
->lanes
& 3)
153 tmp
|= priv
->client
->lanes
& 3;
155 /* Default - both lanes */
158 if (priv
->client
->phy
== SH_CSI2_PHY_MAIN
)
161 iowrite32(tmp
, priv
->base
+ SH_CSI2_PHYCNT
);
164 if (pdata
->flags
& SH_CSI2_ECC
)
166 if (pdata
->flags
& SH_CSI2_CRC
)
168 iowrite32(tmp
, priv
->base
+ SH_CSI2_CHKSUM
);
171 static int sh_csi2_set_bus_param(struct soc_camera_device
*icd
,
177 static unsigned long sh_csi2_query_bus_param(struct soc_camera_device
*icd
)
179 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
180 const unsigned long flags
= SOCAM_PCLK_SAMPLE_RISING
|
181 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_HIGH
|
182 SOCAM_MASTER
| SOCAM_DATAWIDTH_8
| SOCAM_DATA_ACTIVE_HIGH
;
184 return soc_camera_apply_sensor_flags(icl
, flags
);
187 static int sh_csi2_notify(struct notifier_block
*nb
,
188 unsigned long action
, void *data
)
190 struct device
*dev
= data
;
191 struct soc_camera_device
*icd
= to_soc_camera_dev(dev
);
192 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
->parent
);
193 struct sh_csi2
*priv
=
194 container_of(nb
, struct sh_csi2
, notifier
);
195 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
198 for (i
= 0; i
< pdata
->num_clients
; i
++)
199 if (&pdata
->clients
[i
].pdev
->dev
== icd
->pdev
)
202 dev_dbg(dev
, "%s(%p): action = %lu, found #%d\n", __func__
, dev
, action
, i
);
204 if (i
== pdata
->num_clients
)
208 case BUS_NOTIFY_BOUND_DRIVER
:
209 snprintf(priv
->subdev
.name
, V4L2_SUBDEV_NAME_SIZE
, "%s%s",
210 dev_name(v4l2_dev
->dev
), ".mipi-csi");
211 ret
= v4l2_device_register_subdev(v4l2_dev
, &priv
->subdev
);
212 dev_dbg(dev
, "%s(%p): ret(register_subdev) = %d\n", __func__
, priv
, ret
);
216 priv
->client
= pdata
->clients
+ i
;
218 icd
->ops
->set_bus_param
= sh_csi2_set_bus_param
;
219 icd
->ops
->query_bus_param
= sh_csi2_query_bus_param
;
221 pm_runtime_get_sync(v4l2_get_subdevdata(&priv
->subdev
));
223 sh_csi2_hwinit(priv
);
225 case BUS_NOTIFY_UNBIND_DRIVER
:
228 /* Driver is about to be unbound */
229 icd
->ops
->set_bus_param
= NULL
;
230 icd
->ops
->query_bus_param
= NULL
;
232 v4l2_device_unregister_subdev(&priv
->subdev
);
234 pm_runtime_put(v4l2_get_subdevdata(&priv
->subdev
));
241 static __devinit
int sh_csi2_probe(struct platform_device
*pdev
)
243 struct resource
*res
;
246 struct sh_csi2
*priv
;
247 /* Platform data specify the PHY, lanes, ECC, CRC */
248 struct sh_csi2_pdata
*pdata
= pdev
->dev
.platform_data
;
250 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
251 /* Interrupt unused so far */
252 irq
= platform_get_irq(pdev
, 0);
254 if (!res
|| (int)irq
<= 0 || !pdata
) {
255 dev_err(&pdev
->dev
, "Not enough CSI2 platform resources.\n");
259 /* TODO: Add support for CSI2I. Careful: different register layout! */
260 if (pdata
->type
!= SH_CSI2C
) {
261 dev_err(&pdev
->dev
, "Only CSI2C supported ATM.\n");
265 priv
= kzalloc(sizeof(struct sh_csi2
), GFP_KERNEL
);
270 priv
->notifier
.notifier_call
= sh_csi2_notify
;
272 /* We MUST attach after the MIPI sensor */
273 ret
= bus_register_notifier(&soc_camera_bus_type
, &priv
->notifier
);
275 dev_err(&pdev
->dev
, "CSI2 cannot register notifier\n");
279 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
280 dev_err(&pdev
->dev
, "CSI2 register region already claimed\n");
285 priv
->base
= ioremap(res
->start
, resource_size(res
));
288 dev_err(&pdev
->dev
, "Unable to ioremap CSI2 registers.\n");
294 v4l2_subdev_init(&priv
->subdev
, &sh_csi2_subdev_ops
);
295 v4l2_set_subdevdata(&priv
->subdev
, &pdev
->dev
);
297 platform_set_drvdata(pdev
, priv
);
299 pm_runtime_enable(&pdev
->dev
);
301 dev_dbg(&pdev
->dev
, "CSI2 probed.\n");
306 release_mem_region(res
->start
, resource_size(res
));
308 bus_unregister_notifier(&soc_camera_bus_type
, &priv
->notifier
);
315 static __devexit
int sh_csi2_remove(struct platform_device
*pdev
)
317 struct sh_csi2
*priv
= platform_get_drvdata(pdev
);
318 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
320 bus_unregister_notifier(&soc_camera_bus_type
, &priv
->notifier
);
321 pm_runtime_disable(&pdev
->dev
);
323 release_mem_region(res
->start
, resource_size(res
));
324 platform_set_drvdata(pdev
, NULL
);
330 static struct platform_driver __refdata sh_csi2_pdrv
= {
331 .remove
= __devexit_p(sh_csi2_remove
),
333 .name
= "sh-mobile-csi2",
334 .owner
= THIS_MODULE
,
338 static int __init
sh_csi2_init(void)
340 return platform_driver_probe(&sh_csi2_pdrv
, sh_csi2_probe
);
343 static void __exit
sh_csi2_exit(void)
345 platform_driver_unregister(&sh_csi2_pdrv
);
348 module_init(sh_csi2_init
);
349 module_exit(sh_csi2_exit
);
351 MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
352 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
353 MODULE_LICENSE("GPL v2");
354 MODULE_ALIAS("platform:sh-mobile-csi2");