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/sh_mobile_ceu.h>
22 #include <media/sh_mobile_csi2.h>
23 #include <media/soc_camera.h>
24 #include <media/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_try_fmt(struct v4l2_subdev *sd,
49 struct v4l2_mbus_framefmt *mf)
51 struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
52 struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
56 else if (mf->width & 1)
59 switch (pdata->type) {
62 case MEDIA_BUS_FMT_UYVY8_2X8: /* YUV422 */
63 case MEDIA_BUS_FMT_YUYV8_1_5X8: /* YUV420 */
64 case MEDIA_BUS_FMT_Y8_1X8: /* RAW8 */
65 case MEDIA_BUS_FMT_SBGGR8_1X8:
66 case MEDIA_BUS_FMT_SGRBG8_1X8:
69 /* All MIPI CSI-2 devices must support one of primary formats */
70 mf->code = MEDIA_BUS_FMT_YUYV8_2X8;
75 case MEDIA_BUS_FMT_Y8_1X8: /* RAW8 */
76 case MEDIA_BUS_FMT_SBGGR8_1X8:
77 case MEDIA_BUS_FMT_SGRBG8_1X8:
78 case MEDIA_BUS_FMT_SBGGR10_1X10: /* RAW10 */
79 case MEDIA_BUS_FMT_SBGGR12_1X12: /* RAW12 */
82 /* All MIPI CSI-2 devices must support one of primary formats */
83 mf->code = MEDIA_BUS_FMT_SBGGR8_1X8;
92 * We have done our best in try_fmt to try and tell the sensor, which formats
93 * we support. If now the configuration is unsuitable for us we can only
96 static int sh_csi2_s_fmt(struct v4l2_subdev *sd,
97 struct v4l2_mbus_framefmt *mf)
99 struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
100 u32 tmp = (priv->client->channel & 3) << 8;
102 dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
103 if (mf->width > 8188 || mf->width & 1)
107 case MEDIA_BUS_FMT_UYVY8_2X8:
108 tmp |= 0x1e; /* YUV422 8 bit */
110 case MEDIA_BUS_FMT_YUYV8_1_5X8:
111 tmp |= 0x18; /* YUV420 8 bit */
113 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE:
114 tmp |= 0x21; /* RGB555 */
116 case MEDIA_BUS_FMT_RGB565_2X8_BE:
117 tmp |= 0x22; /* RGB565 */
119 case MEDIA_BUS_FMT_Y8_1X8:
120 case MEDIA_BUS_FMT_SBGGR8_1X8:
121 case MEDIA_BUS_FMT_SGRBG8_1X8:
122 tmp |= 0x2a; /* RAW8 */
128 iowrite32(tmp, priv->base + SH_CSI2_VCDT);
133 static int sh_csi2_g_mbus_config(struct v4l2_subdev *sd,
134 struct v4l2_mbus_config *cfg)
136 struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
138 if (!priv->mipi_flags) {
139 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
140 struct v4l2_subdev *client_sd = soc_camera_to_subdev(icd);
141 struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
142 unsigned long common_flags, csi2_flags;
143 struct v4l2_mbus_config client_cfg = {.type = V4L2_MBUS_CSI2,};
146 /* Check if we can support this camera */
147 csi2_flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK |
148 V4L2_MBUS_CSI2_1_LANE;
150 switch (pdata->type) {
152 if (priv->client->lanes != 1)
153 csi2_flags |= V4L2_MBUS_CSI2_2_LANE;
156 switch (priv->client->lanes) {
158 csi2_flags |= V4L2_MBUS_CSI2_4_LANE;
160 csi2_flags |= V4L2_MBUS_CSI2_3_LANE;
162 csi2_flags |= V4L2_MBUS_CSI2_2_LANE;
166 ret = v4l2_subdev_call(client_sd, video, g_mbus_config, &client_cfg);
167 if (ret == -ENOIOCTLCMD)
168 common_flags = csi2_flags;
170 common_flags = soc_mbus_config_compatible(&client_cfg,
178 /* All good: camera MIPI configuration supported */
179 priv->mipi_flags = common_flags;
183 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING |
184 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
185 V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH;
186 cfg->type = V4L2_MBUS_PARALLEL;
192 static int sh_csi2_s_mbus_config(struct v4l2_subdev *sd,
193 const struct v4l2_mbus_config *cfg)
195 struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
196 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
197 struct v4l2_subdev *client_sd = soc_camera_to_subdev(icd);
198 struct v4l2_mbus_config client_cfg = {.type = V4L2_MBUS_CSI2,};
199 int ret = sh_csi2_g_mbus_config(sd, NULL);
204 pm_runtime_get_sync(&priv->pdev->dev);
206 sh_csi2_hwinit(priv);
208 client_cfg.flags = priv->mipi_flags;
210 return v4l2_subdev_call(client_sd, video, s_mbus_config, &client_cfg);
213 static struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops = {
214 .s_mbus_fmt = sh_csi2_s_fmt,
215 .try_mbus_fmt = sh_csi2_try_fmt,
216 .g_mbus_config = sh_csi2_g_mbus_config,
217 .s_mbus_config = sh_csi2_s_mbus_config,
220 static void sh_csi2_hwinit(struct sh_csi2 *priv)
222 struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
223 __u32 tmp = 0x10; /* Enable MIPI CSI clock lane */
225 /* Reflect registers immediately */
226 iowrite32(0x00000001, priv->base + SH_CSI2_TREF);
227 /* reset CSI2 harware */
228 iowrite32(0x00000001, priv->base + SH_CSI2_SRST);
230 iowrite32(0x00000000, priv->base + SH_CSI2_SRST);
232 switch (pdata->type) {
234 if (priv->client->lanes == 1)
237 /* Default - both lanes */
241 if (!priv->client->lanes || priv->client->lanes > 4)
242 /* Default - all 4 lanes */
245 tmp |= (1 << priv->client->lanes) - 1;
248 if (priv->client->phy == SH_CSI2_PHY_MAIN)
251 iowrite32(tmp, priv->base + SH_CSI2_PHYCNT);
254 if (pdata->flags & SH_CSI2_ECC)
256 if (pdata->flags & SH_CSI2_CRC)
258 iowrite32(tmp, priv->base + SH_CSI2_CHKSUM);
261 static int sh_csi2_client_connect(struct sh_csi2 *priv)
263 struct device *dev = v4l2_get_subdevdata(&priv->subdev);
264 struct sh_csi2_pdata *pdata = dev->platform_data;
265 struct soc_camera_device *icd = v4l2_get_subdev_hostdata(&priv->subdev);
271 for (i = 0; i < pdata->num_clients; i++)
272 if ((pdata->clients[i].pdev &&
273 &pdata->clients[i].pdev->dev == icd->pdev) ||
275 strcmp(pdata->clients[i].name, dev_name(icd->control))))
278 dev_dbg(dev, "%s(%p): found #%d\n", __func__, dev, i);
280 if (i == pdata->num_clients)
283 priv->client = pdata->clients + i;
288 static void sh_csi2_client_disconnect(struct sh_csi2 *priv)
295 pm_runtime_put(v4l2_get_subdevdata(&priv->subdev));
298 static int sh_csi2_s_power(struct v4l2_subdev *sd, int on)
300 struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
303 return sh_csi2_client_connect(priv);
305 sh_csi2_client_disconnect(priv);
309 static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops = {
310 .s_power = sh_csi2_s_power,
313 static struct v4l2_subdev_ops sh_csi2_subdev_ops = {
314 .core = &sh_csi2_subdev_core_ops,
315 .video = &sh_csi2_subdev_video_ops,
318 static int sh_csi2_probe(struct platform_device *pdev)
320 struct resource *res;
323 struct sh_csi2 *priv;
324 /* Platform data specify the PHY, lanes, ECC, CRC */
325 struct sh_csi2_pdata *pdata = pdev->dev.platform_data;
330 priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_csi2), GFP_KERNEL);
334 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
335 /* Interrupt unused so far */
336 irq = platform_get_irq(pdev, 0);
338 if (!res || (int)irq <= 0) {
339 dev_err(&pdev->dev, "Not enough CSI2 platform resources.\n");
343 /* TODO: Add support for CSI2I. Careful: different register layout! */
344 if (pdata->type != SH_CSI2C) {
345 dev_err(&pdev->dev, "Only CSI2C supported ATM.\n");
351 priv->base = devm_ioremap_resource(&pdev->dev, res);
352 if (IS_ERR(priv->base))
353 return PTR_ERR(priv->base);
356 priv->subdev.owner = THIS_MODULE;
357 priv->subdev.dev = &pdev->dev;
358 platform_set_drvdata(pdev, &priv->subdev);
360 v4l2_subdev_init(&priv->subdev, &sh_csi2_subdev_ops);
361 v4l2_set_subdevdata(&priv->subdev, &pdev->dev);
363 snprintf(priv->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.mipi-csi",
364 dev_name(&pdev->dev));
366 ret = v4l2_async_register_subdev(&priv->subdev);
370 pm_runtime_enable(&pdev->dev);
372 dev_dbg(&pdev->dev, "CSI2 probed.\n");
377 static int sh_csi2_remove(struct platform_device *pdev)
379 struct v4l2_subdev *subdev = platform_get_drvdata(pdev);
380 struct sh_csi2 *priv = container_of(subdev, struct sh_csi2, subdev);
382 v4l2_async_unregister_subdev(&priv->subdev);
383 v4l2_device_unregister_subdev(subdev);
384 pm_runtime_disable(&pdev->dev);
389 static struct platform_driver __refdata sh_csi2_pdrv = {
390 .remove = sh_csi2_remove,
391 .probe = sh_csi2_probe,
393 .name = "sh-mobile-csi2",
397 module_platform_driver(sh_csi2_pdrv);
399 MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
400 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
401 MODULE_LICENSE("GPL v2");
402 MODULE_ALIAS("platform:sh-mobile-csi2");