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/v4l2-common.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-mediabus.h>
27 #include <media/v4l2-subdev.h>
29 #define SH_CSI2_TREF 0x00
30 #define SH_CSI2_SRST 0x04
31 #define SH_CSI2_PHYCNT 0x08
32 #define SH_CSI2_CHKSUM 0x0C
33 #define SH_CSI2_VCDT 0x10
36 struct v4l2_subdev subdev
;
37 struct list_head list
;
40 struct platform_device
*pdev
;
41 struct sh_csi2_client_config
*client
;
42 unsigned long (*query_bus_param
)(struct soc_camera_device
*);
43 int (*set_bus_param
)(struct soc_camera_device
*, unsigned long);
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 struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops
= {
132 .s_mbus_fmt
= sh_csi2_s_fmt
,
133 .try_mbus_fmt
= sh_csi2_try_fmt
,
136 static void sh_csi2_hwinit(struct sh_csi2
*priv
)
138 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
139 __u32 tmp
= 0x10; /* Enable MIPI CSI clock lane */
141 /* Reflect registers immediately */
142 iowrite32(0x00000001, priv
->base
+ SH_CSI2_TREF
);
143 /* reset CSI2 harware */
144 iowrite32(0x00000001, priv
->base
+ SH_CSI2_SRST
);
146 iowrite32(0x00000000, priv
->base
+ SH_CSI2_SRST
);
148 if (priv
->client
->lanes
& 3)
149 tmp
|= priv
->client
->lanes
& 3;
151 /* Default - both lanes */
154 if (priv
->client
->phy
== SH_CSI2_PHY_MAIN
)
157 iowrite32(tmp
, priv
->base
+ SH_CSI2_PHYCNT
);
160 if (pdata
->flags
& SH_CSI2_ECC
)
162 if (pdata
->flags
& SH_CSI2_CRC
)
164 iowrite32(tmp
, priv
->base
+ SH_CSI2_CHKSUM
);
167 static int sh_csi2_set_bus_param(struct soc_camera_device
*icd
,
173 static unsigned long sh_csi2_query_bus_param(struct soc_camera_device
*icd
)
175 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
176 const unsigned long flags
= SOCAM_PCLK_SAMPLE_RISING
|
177 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_HIGH
|
178 SOCAM_MASTER
| SOCAM_DATAWIDTH_8
| SOCAM_DATA_ACTIVE_HIGH
;
180 return soc_camera_apply_sensor_flags(icl
, flags
);
183 static int sh_csi2_client_connect(struct sh_csi2
*priv
)
185 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
186 struct v4l2_subdev
*sd
, *csi2_sd
= &priv
->subdev
;
187 struct soc_camera_device
*icd
= NULL
;
188 struct device
*dev
= v4l2_get_subdevdata(&priv
->subdev
);
191 v4l2_device_for_each_subdev(sd
, csi2_sd
->v4l2_dev
)
193 icd
= (struct soc_camera_device
*)sd
->grp_id
;
200 for (i
= 0; i
< pdata
->num_clients
; i
++)
201 if (&pdata
->clients
[i
].pdev
->dev
== icd
->pdev
)
204 dev_dbg(dev
, "%s(%p): found #%d\n", __func__
, dev
, i
);
206 if (i
== pdata
->num_clients
)
209 priv
->client
= pdata
->clients
+ i
;
211 priv
->set_bus_param
= icd
->ops
->set_bus_param
;
212 priv
->query_bus_param
= icd
->ops
->query_bus_param
;
213 icd
->ops
->set_bus_param
= sh_csi2_set_bus_param
;
214 icd
->ops
->query_bus_param
= sh_csi2_query_bus_param
;
216 csi2_sd
->grp_id
= (long)icd
;
218 pm_runtime_get_sync(dev
);
220 sh_csi2_hwinit(priv
);
225 static void sh_csi2_client_disconnect(struct sh_csi2
*priv
)
227 struct soc_camera_device
*icd
= (struct soc_camera_device
*)priv
->subdev
.grp_id
;
230 priv
->subdev
.grp_id
= 0;
232 /* Driver is about to be unbound */
233 icd
->ops
->set_bus_param
= priv
->set_bus_param
;
234 icd
->ops
->query_bus_param
= priv
->query_bus_param
;
235 priv
->set_bus_param
= NULL
;
236 priv
->query_bus_param
= NULL
;
238 pm_runtime_put(v4l2_get_subdevdata(&priv
->subdev
));
241 static int sh_csi2_s_power(struct v4l2_subdev
*sd
, int on
)
243 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
246 return sh_csi2_client_connect(priv
);
248 sh_csi2_client_disconnect(priv
);
252 static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops
= {
253 .s_power
= sh_csi2_s_power
,
256 static struct v4l2_subdev_ops sh_csi2_subdev_ops
= {
257 .core
= &sh_csi2_subdev_core_ops
,
258 .video
= &sh_csi2_subdev_video_ops
,
261 static __devinit
int sh_csi2_probe(struct platform_device
*pdev
)
263 struct resource
*res
;
266 struct sh_csi2
*priv
;
267 /* Platform data specify the PHY, lanes, ECC, CRC */
268 struct sh_csi2_pdata
*pdata
= pdev
->dev
.platform_data
;
270 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
271 /* Interrupt unused so far */
272 irq
= platform_get_irq(pdev
, 0);
274 if (!res
|| (int)irq
<= 0 || !pdata
) {
275 dev_err(&pdev
->dev
, "Not enough CSI2 platform resources.\n");
279 /* TODO: Add support for CSI2I. Careful: different register layout! */
280 if (pdata
->type
!= SH_CSI2C
) {
281 dev_err(&pdev
->dev
, "Only CSI2C supported ATM.\n");
285 priv
= kzalloc(sizeof(struct sh_csi2
), GFP_KERNEL
);
291 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
292 dev_err(&pdev
->dev
, "CSI2 register region already claimed\n");
297 priv
->base
= ioremap(res
->start
, resource_size(res
));
300 dev_err(&pdev
->dev
, "Unable to ioremap CSI2 registers.\n");
305 platform_set_drvdata(pdev
, priv
);
307 v4l2_subdev_init(&priv
->subdev
, &sh_csi2_subdev_ops
);
308 v4l2_set_subdevdata(&priv
->subdev
, &pdev
->dev
);
310 snprintf(priv
->subdev
.name
, V4L2_SUBDEV_NAME_SIZE
, "%s.mipi-csi",
311 dev_name(pdata
->v4l2_dev
->dev
));
312 ret
= v4l2_device_register_subdev(pdata
->v4l2_dev
, &priv
->subdev
);
313 dev_dbg(&pdev
->dev
, "%s(%p): ret(register_subdev) = %d\n", __func__
, priv
, ret
);
317 pm_runtime_enable(&pdev
->dev
);
319 dev_dbg(&pdev
->dev
, "CSI2 probed.\n");
326 release_mem_region(res
->start
, resource_size(res
));
333 static __devexit
int sh_csi2_remove(struct platform_device
*pdev
)
335 struct sh_csi2
*priv
= platform_get_drvdata(pdev
);
336 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
338 v4l2_device_unregister_subdev(&priv
->subdev
);
339 pm_runtime_disable(&pdev
->dev
);
341 release_mem_region(res
->start
, resource_size(res
));
342 platform_set_drvdata(pdev
, NULL
);
348 static struct platform_driver __refdata sh_csi2_pdrv
= {
349 .remove
= __devexit_p(sh_csi2_remove
),
350 .probe
= sh_csi2_probe
,
352 .name
= "sh-mobile-csi2",
353 .owner
= THIS_MODULE
,
357 static int __init
sh_csi2_init(void)
359 return platform_driver_register(&sh_csi2_pdrv
);
362 static void __exit
sh_csi2_exit(void)
364 platform_driver_unregister(&sh_csi2_pdrv
);
367 module_init(sh_csi2_init
);
368 module_exit(sh_csi2_exit
);
370 MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
371 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
372 MODULE_LICENSE("GPL v2");
373 MODULE_ALIAS("platform:sh-mobile-csi2");