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/err.h>
13 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/videodev2.h>
19 #include <linux/module.h>
21 #include <media/drv-intf/sh_mobile_ceu.h>
22 #include <media/drv-intf/sh_mobile_csi2.h>
23 #include <media/soc_camera.h>
24 #include <media/drv-intf/soc_mediabus.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-mediabus.h>
29 #include <media/v4l2-subdev.h>
31 #define SH_CSI2_TREF 0x00
32 #define SH_CSI2_SRST 0x04
33 #define SH_CSI2_PHYCNT 0x08
34 #define SH_CSI2_CHKSUM 0x0C
35 #define SH_CSI2_VCDT 0x10
38 struct v4l2_subdev subdev
;
40 unsigned long mipi_flags
;
42 struct platform_device
*pdev
;
43 struct sh_csi2_client_config
*client
;
46 static void sh_csi2_hwinit(struct sh_csi2
*priv
);
48 static int sh_csi2_set_fmt(struct v4l2_subdev
*sd
,
49 struct v4l2_subdev_pad_config
*cfg
,
50 struct v4l2_subdev_format
*format
)
52 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
53 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
54 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
55 u32 tmp
= (priv
->client
->channel
& 3) << 8;
62 else if (mf
->width
& 1)
65 switch (pdata
->type
) {
68 case MEDIA_BUS_FMT_UYVY8_2X8
: /* YUV422 */
69 case MEDIA_BUS_FMT_YUYV8_1_5X8
: /* YUV420 */
70 case MEDIA_BUS_FMT_Y8_1X8
: /* RAW8 */
71 case MEDIA_BUS_FMT_SBGGR8_1X8
:
72 case MEDIA_BUS_FMT_SGRBG8_1X8
:
75 /* All MIPI CSI-2 devices must support one of primary formats */
76 mf
->code
= MEDIA_BUS_FMT_YUYV8_2X8
;
81 case MEDIA_BUS_FMT_Y8_1X8
: /* RAW8 */
82 case MEDIA_BUS_FMT_SBGGR8_1X8
:
83 case MEDIA_BUS_FMT_SGRBG8_1X8
:
84 case MEDIA_BUS_FMT_SBGGR10_1X10
: /* RAW10 */
85 case MEDIA_BUS_FMT_SBGGR12_1X12
: /* RAW12 */
88 /* All MIPI CSI-2 devices must support one of primary formats */
89 mf
->code
= MEDIA_BUS_FMT_SBGGR8_1X8
;
94 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
99 if (mf
->width
> 8188 || mf
->width
& 1)
103 case MEDIA_BUS_FMT_UYVY8_2X8
:
104 tmp
|= 0x1e; /* YUV422 8 bit */
106 case MEDIA_BUS_FMT_YUYV8_1_5X8
:
107 tmp
|= 0x18; /* YUV420 8 bit */
109 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE
:
110 tmp
|= 0x21; /* RGB555 */
112 case MEDIA_BUS_FMT_RGB565_2X8_BE
:
113 tmp
|= 0x22; /* RGB565 */
115 case MEDIA_BUS_FMT_Y8_1X8
:
116 case MEDIA_BUS_FMT_SBGGR8_1X8
:
117 case MEDIA_BUS_FMT_SGRBG8_1X8
:
118 tmp
|= 0x2a; /* RAW8 */
124 iowrite32(tmp
, priv
->base
+ SH_CSI2_VCDT
);
129 static int sh_csi2_g_mbus_config(struct v4l2_subdev
*sd
,
130 struct v4l2_mbus_config
*cfg
)
132 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
134 if (!priv
->mipi_flags
) {
135 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(sd
);
136 struct v4l2_subdev
*client_sd
= soc_camera_to_subdev(icd
);
137 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
138 unsigned long common_flags
, csi2_flags
;
139 struct v4l2_mbus_config client_cfg
= {.type
= V4L2_MBUS_CSI2
,};
142 /* Check if we can support this camera */
143 csi2_flags
= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK
|
144 V4L2_MBUS_CSI2_1_LANE
;
146 switch (pdata
->type
) {
148 if (priv
->client
->lanes
!= 1)
149 csi2_flags
|= V4L2_MBUS_CSI2_2_LANE
;
152 switch (priv
->client
->lanes
) {
154 csi2_flags
|= V4L2_MBUS_CSI2_4_LANE
;
156 csi2_flags
|= V4L2_MBUS_CSI2_3_LANE
;
158 csi2_flags
|= V4L2_MBUS_CSI2_2_LANE
;
162 ret
= v4l2_subdev_call(client_sd
, video
, g_mbus_config
, &client_cfg
);
163 if (ret
== -ENOIOCTLCMD
)
164 common_flags
= csi2_flags
;
166 common_flags
= soc_mbus_config_compatible(&client_cfg
,
174 /* All good: camera MIPI configuration supported */
175 priv
->mipi_flags
= common_flags
;
179 cfg
->flags
= V4L2_MBUS_PCLK_SAMPLE_RISING
|
180 V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_HIGH
|
181 V4L2_MBUS_MASTER
| V4L2_MBUS_DATA_ACTIVE_HIGH
;
182 cfg
->type
= V4L2_MBUS_PARALLEL
;
188 static int sh_csi2_s_mbus_config(struct v4l2_subdev
*sd
,
189 const struct v4l2_mbus_config
*cfg
)
191 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
192 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(sd
);
193 struct v4l2_subdev
*client_sd
= soc_camera_to_subdev(icd
);
194 struct v4l2_mbus_config client_cfg
= {.type
= V4L2_MBUS_CSI2
,};
195 int ret
= sh_csi2_g_mbus_config(sd
, NULL
);
200 pm_runtime_get_sync(&priv
->pdev
->dev
);
202 sh_csi2_hwinit(priv
);
204 client_cfg
.flags
= priv
->mipi_flags
;
206 return v4l2_subdev_call(client_sd
, video
, s_mbus_config
, &client_cfg
);
209 static struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops
= {
210 .g_mbus_config
= sh_csi2_g_mbus_config
,
211 .s_mbus_config
= sh_csi2_s_mbus_config
,
214 static struct v4l2_subdev_pad_ops sh_csi2_subdev_pad_ops
= {
215 .set_fmt
= sh_csi2_set_fmt
,
218 static void sh_csi2_hwinit(struct sh_csi2
*priv
)
220 struct sh_csi2_pdata
*pdata
= priv
->pdev
->dev
.platform_data
;
221 __u32 tmp
= 0x10; /* Enable MIPI CSI clock lane */
223 /* Reflect registers immediately */
224 iowrite32(0x00000001, priv
->base
+ SH_CSI2_TREF
);
225 /* reset CSI2 harware */
226 iowrite32(0x00000001, priv
->base
+ SH_CSI2_SRST
);
228 iowrite32(0x00000000, priv
->base
+ SH_CSI2_SRST
);
230 switch (pdata
->type
) {
232 if (priv
->client
->lanes
== 1)
235 /* Default - both lanes */
239 if (!priv
->client
->lanes
|| priv
->client
->lanes
> 4)
240 /* Default - all 4 lanes */
243 tmp
|= (1 << priv
->client
->lanes
) - 1;
246 if (priv
->client
->phy
== SH_CSI2_PHY_MAIN
)
249 iowrite32(tmp
, priv
->base
+ SH_CSI2_PHYCNT
);
252 if (pdata
->flags
& SH_CSI2_ECC
)
254 if (pdata
->flags
& SH_CSI2_CRC
)
256 iowrite32(tmp
, priv
->base
+ SH_CSI2_CHKSUM
);
259 static int sh_csi2_client_connect(struct sh_csi2
*priv
)
261 struct device
*dev
= v4l2_get_subdevdata(&priv
->subdev
);
262 struct sh_csi2_pdata
*pdata
= dev
->platform_data
;
263 struct soc_camera_device
*icd
= v4l2_get_subdev_hostdata(&priv
->subdev
);
269 for (i
= 0; i
< pdata
->num_clients
; i
++)
270 if ((pdata
->clients
[i
].pdev
&&
271 &pdata
->clients
[i
].pdev
->dev
== icd
->pdev
) ||
273 strcmp(pdata
->clients
[i
].name
, dev_name(icd
->control
))))
276 dev_dbg(dev
, "%s(%p): found #%d\n", __func__
, dev
, i
);
278 if (i
== pdata
->num_clients
)
281 priv
->client
= pdata
->clients
+ i
;
286 static void sh_csi2_client_disconnect(struct sh_csi2
*priv
)
293 pm_runtime_put(v4l2_get_subdevdata(&priv
->subdev
));
296 static int sh_csi2_s_power(struct v4l2_subdev
*sd
, int on
)
298 struct sh_csi2
*priv
= container_of(sd
, struct sh_csi2
, subdev
);
301 return sh_csi2_client_connect(priv
);
303 sh_csi2_client_disconnect(priv
);
307 static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops
= {
308 .s_power
= sh_csi2_s_power
,
311 static struct v4l2_subdev_ops sh_csi2_subdev_ops
= {
312 .core
= &sh_csi2_subdev_core_ops
,
313 .video
= &sh_csi2_subdev_video_ops
,
314 .pad
= &sh_csi2_subdev_pad_ops
,
317 static int sh_csi2_probe(struct platform_device
*pdev
)
319 struct resource
*res
;
322 struct sh_csi2
*priv
;
323 /* Platform data specify the PHY, lanes, ECC, CRC */
324 struct sh_csi2_pdata
*pdata
= pdev
->dev
.platform_data
;
329 priv
= devm_kzalloc(&pdev
->dev
, sizeof(struct sh_csi2
), GFP_KERNEL
);
333 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
334 /* Interrupt unused so far */
335 irq
= platform_get_irq(pdev
, 0);
337 if (!res
|| (int)irq
<= 0) {
338 dev_err(&pdev
->dev
, "Not enough CSI2 platform resources.\n");
342 /* TODO: Add support for CSI2I. Careful: different register layout! */
343 if (pdata
->type
!= SH_CSI2C
) {
344 dev_err(&pdev
->dev
, "Only CSI2C supported ATM.\n");
350 priv
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
351 if (IS_ERR(priv
->base
))
352 return PTR_ERR(priv
->base
);
355 priv
->subdev
.owner
= THIS_MODULE
;
356 priv
->subdev
.dev
= &pdev
->dev
;
357 platform_set_drvdata(pdev
, &priv
->subdev
);
359 v4l2_subdev_init(&priv
->subdev
, &sh_csi2_subdev_ops
);
360 v4l2_set_subdevdata(&priv
->subdev
, &pdev
->dev
);
362 snprintf(priv
->subdev
.name
, V4L2_SUBDEV_NAME_SIZE
, "%s.mipi-csi",
363 dev_name(&pdev
->dev
));
365 ret
= v4l2_async_register_subdev(&priv
->subdev
);
369 pm_runtime_enable(&pdev
->dev
);
371 dev_dbg(&pdev
->dev
, "CSI2 probed.\n");
376 static int sh_csi2_remove(struct platform_device
*pdev
)
378 struct v4l2_subdev
*subdev
= platform_get_drvdata(pdev
);
379 struct sh_csi2
*priv
= container_of(subdev
, struct sh_csi2
, subdev
);
381 v4l2_async_unregister_subdev(&priv
->subdev
);
382 pm_runtime_disable(&pdev
->dev
);
387 static struct platform_driver __refdata sh_csi2_pdrv
= {
388 .remove
= sh_csi2_remove
,
389 .probe
= sh_csi2_probe
,
391 .name
= "sh-mobile-csi2",
395 module_platform_driver(sh_csi2_pdrv
);
397 MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
398 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
399 MODULE_LICENSE("GPL v2");
400 MODULE_ALIAS("platform:sh-mobile-csi2");