Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / sh_mobile_csi2.c
blob426c8adef28a7a64c1839cf031da98711a237c42
1 /*
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.
9 */
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/io.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
35 struct sh_csi2 {
36 struct v4l2_subdev subdev;
37 struct list_head list;
38 unsigned int irq;
39 void __iomem *base;
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;
52 if (mf->width > 8188)
53 mf->width = 8188;
54 else if (mf->width & 1)
55 mf->width &= ~1;
57 switch (pdata->type) {
58 case SH_CSI2C:
59 switch (mf->code) {
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:
65 break;
66 default:
67 /* All MIPI CSI-2 devices must support one of primary formats */
68 mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
70 break;
71 case SH_CSI2I:
72 switch (mf->code) {
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 */
78 break;
79 default:
80 /* All MIPI CSI-2 devices must support one of primary formats */
81 mf->code = V4L2_MBUS_FMT_SBGGR8_1X8;
83 break;
86 return 0;
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
92 * error out.
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)
102 return -EINVAL;
104 switch (mf->code) {
105 case V4L2_MBUS_FMT_UYVY8_2X8:
106 tmp |= 0x1e; /* YUV422 8 bit */
107 break;
108 case V4L2_MBUS_FMT_YUYV8_1_5X8:
109 tmp |= 0x18; /* YUV420 8 bit */
110 break;
111 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE:
112 tmp |= 0x21; /* RGB555 */
113 break;
114 case V4L2_MBUS_FMT_RGB565_2X8_BE:
115 tmp |= 0x22; /* RGB565 */
116 break;
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 */
121 break;
122 default:
123 return -EINVAL;
126 iowrite32(tmp, priv->base + SH_CSI2_VCDT);
128 return 0;
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);
145 udelay(5);
146 iowrite32(0x00000000, priv->base + SH_CSI2_SRST);
148 if (priv->client->lanes & 3)
149 tmp |= priv->client->lanes & 3;
150 else
151 /* Default - both lanes */
152 tmp |= 3;
154 if (priv->client->phy == SH_CSI2_PHY_MAIN)
155 tmp |= 0x8000;
157 iowrite32(tmp, priv->base + SH_CSI2_PHYCNT);
159 tmp = 0;
160 if (pdata->flags & SH_CSI2_ECC)
161 tmp |= 2;
162 if (pdata->flags & SH_CSI2_CRC)
163 tmp |= 1;
164 iowrite32(tmp, priv->base + SH_CSI2_CHKSUM);
167 static int sh_csi2_set_bus_param(struct soc_camera_device *icd,
168 unsigned long flags)
170 return 0;
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);
189 int i;
191 v4l2_device_for_each_subdev(sd, csi2_sd->v4l2_dev)
192 if (sd->grp_id) {
193 icd = (struct soc_camera_device *)sd->grp_id;
194 break;
197 if (!icd)
198 return -EINVAL;
200 for (i = 0; i < pdata->num_clients; i++)
201 if (&pdata->clients[i].pdev->dev == icd->pdev)
202 break;
204 dev_dbg(dev, "%s(%p): found #%d\n", __func__, dev, i);
206 if (i == pdata->num_clients)
207 return -ENODEV;
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);
222 return 0;
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;
229 priv->client = NULL;
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);
245 if (on)
246 return sh_csi2_client_connect(priv);
248 sh_csi2_client_disconnect(priv);
249 return 0;
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;
264 unsigned int irq;
265 int ret;
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");
276 return -ENODEV;
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");
282 return -EINVAL;
285 priv = kzalloc(sizeof(struct sh_csi2), GFP_KERNEL);
286 if (!priv)
287 return -ENOMEM;
289 priv->irq = irq;
291 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
292 dev_err(&pdev->dev, "CSI2 register region already claimed\n");
293 ret = -EBUSY;
294 goto ereqreg;
297 priv->base = ioremap(res->start, resource_size(res));
298 if (!priv->base) {
299 ret = -ENXIO;
300 dev_err(&pdev->dev, "Unable to ioremap CSI2 registers.\n");
301 goto eremap;
304 priv->pdev = pdev;
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);
314 if (ret < 0)
315 goto esdreg;
317 pm_runtime_enable(&pdev->dev);
319 dev_dbg(&pdev->dev, "CSI2 probed.\n");
321 return 0;
323 esdreg:
324 iounmap(priv->base);
325 eremap:
326 release_mem_region(res->start, resource_size(res));
327 ereqreg:
328 kfree(priv);
330 return ret;
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);
340 iounmap(priv->base);
341 release_mem_region(res->start, resource_size(res));
342 platform_set_drvdata(pdev, NULL);
343 kfree(priv);
345 return 0;
348 static struct platform_driver __refdata sh_csi2_pdrv = {
349 .remove = __devexit_p(sh_csi2_remove),
350 .probe = sh_csi2_probe,
351 .driver = {
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");