[media] ov9640: convert to the control framework
[pandora-kernel.git] / drivers / media / video / sh_mobile_csi2.c
1 /*
2  * Driver for the SH-Mobile MIPI CSI-2 unit
3  *
4  * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5  *
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  */
10
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
19 #include <media/sh_mobile_ceu.h>
20 #include <media/sh_mobile_csi2.h>
21 #include <media/soc_camera.h>
22 #include <media/soc_mediabus.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>
28
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
34
35 struct sh_csi2 {
36         struct v4l2_subdev              subdev;
37         struct list_head                list;
38         unsigned int                    irq;
39         unsigned long                   mipi_flags;
40         void __iomem                    *base;
41         struct platform_device          *pdev;
42         struct sh_csi2_client_config    *client;
43 };
44
45 static int sh_csi2_try_fmt(struct v4l2_subdev *sd,
46                            struct v4l2_mbus_framefmt *mf)
47 {
48         struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
49         struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
50
51         if (mf->width > 8188)
52                 mf->width = 8188;
53         else if (mf->width & 1)
54                 mf->width &= ~1;
55
56         switch (pdata->type) {
57         case SH_CSI2C:
58                 switch (mf->code) {
59                 case V4L2_MBUS_FMT_UYVY8_2X8:           /* YUV422 */
60                 case V4L2_MBUS_FMT_YUYV8_1_5X8:         /* YUV420 */
61                 case V4L2_MBUS_FMT_Y8_1X8:              /* RAW8 */
62                 case V4L2_MBUS_FMT_SBGGR8_1X8:
63                 case V4L2_MBUS_FMT_SGRBG8_1X8:
64                         break;
65                 default:
66                         /* All MIPI CSI-2 devices must support one of primary formats */
67                         mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
68                 }
69                 break;
70         case SH_CSI2I:
71                 switch (mf->code) {
72                 case V4L2_MBUS_FMT_Y8_1X8:              /* RAW8 */
73                 case V4L2_MBUS_FMT_SBGGR8_1X8:
74                 case V4L2_MBUS_FMT_SGRBG8_1X8:
75                 case V4L2_MBUS_FMT_SBGGR10_1X10:        /* RAW10 */
76                 case V4L2_MBUS_FMT_SBGGR12_1X12:        /* RAW12 */
77                         break;
78                 default:
79                         /* All MIPI CSI-2 devices must support one of primary formats */
80                         mf->code = V4L2_MBUS_FMT_SBGGR8_1X8;
81                 }
82                 break;
83         }
84
85         return 0;
86 }
87
88 /*
89  * We have done our best in try_fmt to try and tell the sensor, which formats
90  * we support. If now the configuration is unsuitable for us we can only
91  * error out.
92  */
93 static int sh_csi2_s_fmt(struct v4l2_subdev *sd,
94                          struct v4l2_mbus_framefmt *mf)
95 {
96         struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
97         u32 tmp = (priv->client->channel & 3) << 8;
98
99         dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
100         if (mf->width > 8188 || mf->width & 1)
101                 return -EINVAL;
102
103         switch (mf->code) {
104         case V4L2_MBUS_FMT_UYVY8_2X8:
105                 tmp |= 0x1e;    /* YUV422 8 bit */
106                 break;
107         case V4L2_MBUS_FMT_YUYV8_1_5X8:
108                 tmp |= 0x18;    /* YUV420 8 bit */
109                 break;
110         case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE:
111                 tmp |= 0x21;    /* RGB555 */
112                 break;
113         case V4L2_MBUS_FMT_RGB565_2X8_BE:
114                 tmp |= 0x22;    /* RGB565 */
115                 break;
116         case V4L2_MBUS_FMT_Y8_1X8:
117         case V4L2_MBUS_FMT_SBGGR8_1X8:
118         case V4L2_MBUS_FMT_SGRBG8_1X8:
119                 tmp |= 0x2a;    /* RAW8 */
120                 break;
121         default:
122                 return -EINVAL;
123         }
124
125         iowrite32(tmp, priv->base + SH_CSI2_VCDT);
126
127         return 0;
128 }
129
130 static int sh_csi2_g_mbus_config(struct v4l2_subdev *sd,
131                                  struct v4l2_mbus_config *cfg)
132 {
133         cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING |
134                 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
135                 V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH;
136         cfg->type = V4L2_MBUS_PARALLEL;
137
138         return 0;
139 }
140
141 static int sh_csi2_s_mbus_config(struct v4l2_subdev *sd,
142                                  const struct v4l2_mbus_config *cfg)
143 {
144         struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
145         struct soc_camera_device *icd = (struct soc_camera_device *)sd->grp_id;
146         struct v4l2_subdev *client_sd = soc_camera_to_subdev(icd);
147         struct v4l2_mbus_config client_cfg = {.type = V4L2_MBUS_CSI2,
148                                               .flags = priv->mipi_flags};
149
150         return v4l2_subdev_call(client_sd, video, s_mbus_config, &client_cfg);
151 }
152
153 static struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops = {
154         .s_mbus_fmt     = sh_csi2_s_fmt,
155         .try_mbus_fmt   = sh_csi2_try_fmt,
156         .g_mbus_config  = sh_csi2_g_mbus_config,
157         .s_mbus_config  = sh_csi2_s_mbus_config,
158 };
159
160 static void sh_csi2_hwinit(struct sh_csi2 *priv)
161 {
162         struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
163         __u32 tmp = 0x10; /* Enable MIPI CSI clock lane */
164
165         /* Reflect registers immediately */
166         iowrite32(0x00000001, priv->base + SH_CSI2_TREF);
167         /* reset CSI2 harware */
168         iowrite32(0x00000001, priv->base + SH_CSI2_SRST);
169         udelay(5);
170         iowrite32(0x00000000, priv->base + SH_CSI2_SRST);
171
172         switch (pdata->type) {
173         case SH_CSI2C:
174                 if (priv->client->lanes == 1)
175                         tmp |= 1;
176                 else
177                         /* Default - both lanes */
178                         tmp |= 3;
179                 break;
180         case SH_CSI2I:
181                 if (!priv->client->lanes || priv->client->lanes > 4)
182                         /* Default - all 4 lanes */
183                         tmp |= 0xf;
184                 else
185                         tmp |= (1 << priv->client->lanes) - 1;
186         }
187
188         if (priv->client->phy == SH_CSI2_PHY_MAIN)
189                 tmp |= 0x8000;
190
191         iowrite32(tmp, priv->base + SH_CSI2_PHYCNT);
192
193         tmp = 0;
194         if (pdata->flags & SH_CSI2_ECC)
195                 tmp |= 2;
196         if (pdata->flags & SH_CSI2_CRC)
197                 tmp |= 1;
198         iowrite32(tmp, priv->base + SH_CSI2_CHKSUM);
199 }
200
201 static int sh_csi2_client_connect(struct sh_csi2 *priv)
202 {
203         struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
204         struct v4l2_subdev *sd, *csi2_sd = &priv->subdev;
205         struct soc_camera_device *icd = NULL;
206         struct device *dev = v4l2_get_subdevdata(&priv->subdev);
207         struct v4l2_mbus_config cfg;
208         unsigned long common_flags, csi2_flags;
209         int i, ret;
210
211         v4l2_device_for_each_subdev(sd, csi2_sd->v4l2_dev)
212                 if (sd->grp_id) {
213                         icd = (struct soc_camera_device *)sd->grp_id;
214                         break;
215                 }
216
217         if (!icd)
218                 return -EINVAL;
219
220         for (i = 0; i < pdata->num_clients; i++)
221                 if (&pdata->clients[i].pdev->dev == icd->pdev)
222                         break;
223
224         dev_dbg(dev, "%s(%p): found #%d\n", __func__, dev, i);
225
226         if (i == pdata->num_clients)
227                 return -ENODEV;
228
229         /* Check if we can support this camera */
230         csi2_flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK | V4L2_MBUS_CSI2_1_LANE;
231
232         switch (pdata->type) {
233         case SH_CSI2C:
234                 if (pdata->clients[i].lanes != 1)
235                         csi2_flags |= V4L2_MBUS_CSI2_2_LANE;
236                 break;
237         case SH_CSI2I:
238                 switch (pdata->clients[i].lanes) {
239                 default:
240                         csi2_flags |= V4L2_MBUS_CSI2_4_LANE;
241                 case 3:
242                         csi2_flags |= V4L2_MBUS_CSI2_3_LANE;
243                 case 2:
244                         csi2_flags |= V4L2_MBUS_CSI2_2_LANE;
245                 }
246         }
247
248         cfg.type = V4L2_MBUS_CSI2;
249         ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
250         if (ret == -ENOIOCTLCMD)
251                 common_flags = csi2_flags;
252         else if (!ret)
253                 common_flags = soc_mbus_config_compatible(&cfg,
254                                                           csi2_flags);
255         else
256                 common_flags = 0;
257
258         if (!common_flags)
259                 return -EINVAL;
260
261         /* All good: camera MIPI configuration supported */
262         priv->mipi_flags = common_flags;
263         priv->client = pdata->clients + i;
264
265         csi2_sd->grp_id = (long)icd;
266
267         pm_runtime_get_sync(dev);
268
269         sh_csi2_hwinit(priv);
270
271         return 0;
272 }
273
274 static void sh_csi2_client_disconnect(struct sh_csi2 *priv)
275 {
276         priv->client = NULL;
277         priv->subdev.grp_id = 0;
278
279         pm_runtime_put(v4l2_get_subdevdata(&priv->subdev));
280 }
281
282 static int sh_csi2_s_power(struct v4l2_subdev *sd, int on)
283 {
284         struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
285
286         if (on)
287                 return sh_csi2_client_connect(priv);
288
289         sh_csi2_client_disconnect(priv);
290         return 0;
291 }
292
293 static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops = {
294         .s_power        = sh_csi2_s_power,
295 };
296
297 static struct v4l2_subdev_ops sh_csi2_subdev_ops = {
298         .core   = &sh_csi2_subdev_core_ops,
299         .video  = &sh_csi2_subdev_video_ops,
300 };
301
302 static __devinit int sh_csi2_probe(struct platform_device *pdev)
303 {
304         struct resource *res;
305         unsigned int irq;
306         int ret;
307         struct sh_csi2 *priv;
308         /* Platform data specify the PHY, lanes, ECC, CRC */
309         struct sh_csi2_pdata *pdata = pdev->dev.platform_data;
310
311         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
312         /* Interrupt unused so far */
313         irq = platform_get_irq(pdev, 0);
314
315         if (!res || (int)irq <= 0 || !pdata) {
316                 dev_err(&pdev->dev, "Not enough CSI2 platform resources.\n");
317                 return -ENODEV;
318         }
319
320         /* TODO: Add support for CSI2I. Careful: different register layout! */
321         if (pdata->type != SH_CSI2C) {
322                 dev_err(&pdev->dev, "Only CSI2C supported ATM.\n");
323                 return -EINVAL;
324         }
325
326         priv = kzalloc(sizeof(struct sh_csi2), GFP_KERNEL);
327         if (!priv)
328                 return -ENOMEM;
329
330         priv->irq = irq;
331
332         if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
333                 dev_err(&pdev->dev, "CSI2 register region already claimed\n");
334                 ret = -EBUSY;
335                 goto ereqreg;
336         }
337
338         priv->base = ioremap(res->start, resource_size(res));
339         if (!priv->base) {
340                 ret = -ENXIO;
341                 dev_err(&pdev->dev, "Unable to ioremap CSI2 registers.\n");
342                 goto eremap;
343         }
344
345         priv->pdev = pdev;
346         platform_set_drvdata(pdev, priv);
347
348         v4l2_subdev_init(&priv->subdev, &sh_csi2_subdev_ops);
349         v4l2_set_subdevdata(&priv->subdev, &pdev->dev);
350
351         snprintf(priv->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.mipi-csi",
352                  dev_name(pdata->v4l2_dev->dev));
353         ret = v4l2_device_register_subdev(pdata->v4l2_dev, &priv->subdev);
354         dev_dbg(&pdev->dev, "%s(%p): ret(register_subdev) = %d\n", __func__, priv, ret);
355         if (ret < 0)
356                 goto esdreg;
357
358         pm_runtime_enable(&pdev->dev);
359
360         dev_dbg(&pdev->dev, "CSI2 probed.\n");
361
362         return 0;
363
364 esdreg:
365         iounmap(priv->base);
366 eremap:
367         release_mem_region(res->start, resource_size(res));
368 ereqreg:
369         kfree(priv);
370
371         return ret;
372 }
373
374 static __devexit int sh_csi2_remove(struct platform_device *pdev)
375 {
376         struct sh_csi2 *priv = platform_get_drvdata(pdev);
377         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
378
379         v4l2_device_unregister_subdev(&priv->subdev);
380         pm_runtime_disable(&pdev->dev);
381         iounmap(priv->base);
382         release_mem_region(res->start, resource_size(res));
383         platform_set_drvdata(pdev, NULL);
384         kfree(priv);
385
386         return 0;
387 }
388
389 static struct platform_driver __refdata sh_csi2_pdrv = {
390         .remove = __devexit_p(sh_csi2_remove),
391         .probe  = sh_csi2_probe,
392         .driver = {
393                 .name   = "sh-mobile-csi2",
394                 .owner  = THIS_MODULE,
395         },
396 };
397
398 static int __init sh_csi2_init(void)
399 {
400         return platform_driver_register(&sh_csi2_pdrv);
401 }
402
403 static void __exit sh_csi2_exit(void)
404 {
405         platform_driver_unregister(&sh_csi2_pdrv);
406 }
407
408 module_init(sh_csi2_init);
409 module_exit(sh_csi2_exit);
410
411 MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
412 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
413 MODULE_LICENSE("GPL v2");
414 MODULE_ALIAS("platform:sh-mobile-csi2");