sh_mobile_csi2.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/io.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/slab.h>
  16. #include <linux/videodev2.h>
  17. #include <media/sh_mobile_csi2.h>
  18. #include <media/soc_camera.h>
  19. #include <media/v4l2-common.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-mediabus.h>
  23. #include <media/v4l2-subdev.h>
  24. #define SH_CSI2_TREF 0x00
  25. #define SH_CSI2_SRST 0x04
  26. #define SH_CSI2_PHYCNT 0x08
  27. #define SH_CSI2_CHKSUM 0x0C
  28. #define SH_CSI2_VCDT 0x10
  29. struct sh_csi2 {
  30. struct v4l2_subdev subdev;
  31. struct list_head list;
  32. struct notifier_block notifier;
  33. unsigned int irq;
  34. void __iomem *base;
  35. struct platform_device *pdev;
  36. struct sh_csi2_client_config *client;
  37. };
  38. static int sh_csi2_try_fmt(struct v4l2_subdev *sd,
  39. struct v4l2_mbus_framefmt *mf)
  40. {
  41. struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
  42. struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
  43. if (mf->width > 8188)
  44. mf->width = 8188;
  45. else if (mf->width & 1)
  46. mf->width &= ~1;
  47. switch (pdata->type) {
  48. case SH_CSI2C:
  49. switch (mf->code) {
  50. case V4L2_MBUS_FMT_UYVY8_2X8: /* YUV422 */
  51. case V4L2_MBUS_FMT_YUYV8_1_5X8: /* YUV420 */
  52. case V4L2_MBUS_FMT_Y8_1X8: /* RAW8 */
  53. case V4L2_MBUS_FMT_SBGGR8_1X8:
  54. case V4L2_MBUS_FMT_SGRBG8_1X8:
  55. break;
  56. default:
  57. /* All MIPI CSI-2 devices must support one of primary formats */
  58. mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
  59. }
  60. break;
  61. case SH_CSI2I:
  62. switch (mf->code) {
  63. case V4L2_MBUS_FMT_Y8_1X8: /* RAW8 */
  64. case V4L2_MBUS_FMT_SBGGR8_1X8:
  65. case V4L2_MBUS_FMT_SGRBG8_1X8:
  66. case V4L2_MBUS_FMT_SBGGR10_1X10: /* RAW10 */
  67. case V4L2_MBUS_FMT_SBGGR12_1X12: /* RAW12 */
  68. break;
  69. default:
  70. /* All MIPI CSI-2 devices must support one of primary formats */
  71. mf->code = V4L2_MBUS_FMT_SBGGR8_1X8;
  72. }
  73. break;
  74. }
  75. return 0;
  76. }
  77. /*
  78. * We have done our best in try_fmt to try and tell the sensor, which formats
  79. * we support. If now the configuration is unsuitable for us we can only
  80. * error out.
  81. */
  82. static int sh_csi2_s_fmt(struct v4l2_subdev *sd,
  83. struct v4l2_mbus_framefmt *mf)
  84. {
  85. struct sh_csi2 *priv = container_of(sd, struct sh_csi2, subdev);
  86. u32 tmp = (priv->client->channel & 3) << 8;
  87. dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
  88. if (mf->width > 8188 || mf->width & 1)
  89. return -EINVAL;
  90. switch (mf->code) {
  91. case V4L2_MBUS_FMT_UYVY8_2X8:
  92. tmp |= 0x1e; /* YUV422 8 bit */
  93. break;
  94. case V4L2_MBUS_FMT_YUYV8_1_5X8:
  95. tmp |= 0x18; /* YUV420 8 bit */
  96. break;
  97. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE:
  98. tmp |= 0x21; /* RGB555 */
  99. break;
  100. case V4L2_MBUS_FMT_RGB565_2X8_BE:
  101. tmp |= 0x22; /* RGB565 */
  102. break;
  103. case V4L2_MBUS_FMT_Y8_1X8:
  104. case V4L2_MBUS_FMT_SBGGR8_1X8:
  105. case V4L2_MBUS_FMT_SGRBG8_1X8:
  106. tmp |= 0x2a; /* RAW8 */
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. iowrite32(tmp, priv->base + SH_CSI2_VCDT);
  112. return 0;
  113. }
  114. static struct v4l2_subdev_video_ops sh_csi2_subdev_video_ops = {
  115. .s_mbus_fmt = sh_csi2_s_fmt,
  116. .try_mbus_fmt = sh_csi2_try_fmt,
  117. };
  118. static struct v4l2_subdev_core_ops sh_csi2_subdev_core_ops;
  119. static struct v4l2_subdev_ops sh_csi2_subdev_ops = {
  120. .core = &sh_csi2_subdev_core_ops,
  121. .video = &sh_csi2_subdev_video_ops,
  122. };
  123. static void sh_csi2_hwinit(struct sh_csi2 *priv)
  124. {
  125. struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
  126. __u32 tmp = 0x10; /* Enable MIPI CSI clock lane */
  127. /* Reflect registers immediately */
  128. iowrite32(0x00000001, priv->base + SH_CSI2_TREF);
  129. /* reset CSI2 harware */
  130. iowrite32(0x00000001, priv->base + SH_CSI2_SRST);
  131. udelay(5);
  132. iowrite32(0x00000000, priv->base + SH_CSI2_SRST);
  133. if (priv->client->lanes & 3)
  134. tmp |= priv->client->lanes & 3;
  135. else
  136. /* Default - both lanes */
  137. tmp |= 3;
  138. if (priv->client->phy == SH_CSI2_PHY_MAIN)
  139. tmp |= 0x8000;
  140. iowrite32(tmp, priv->base + SH_CSI2_PHYCNT);
  141. tmp = 0;
  142. if (pdata->flags & SH_CSI2_ECC)
  143. tmp |= 2;
  144. if (pdata->flags & SH_CSI2_CRC)
  145. tmp |= 1;
  146. iowrite32(tmp, priv->base + SH_CSI2_CHKSUM);
  147. }
  148. static int sh_csi2_set_bus_param(struct soc_camera_device *icd,
  149. unsigned long flags)
  150. {
  151. return 0;
  152. }
  153. static unsigned long sh_csi2_query_bus_param(struct soc_camera_device *icd)
  154. {
  155. struct soc_camera_link *icl = to_soc_camera_link(icd);
  156. const unsigned long flags = SOCAM_PCLK_SAMPLE_RISING |
  157. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
  158. SOCAM_MASTER | SOCAM_DATAWIDTH_8 | SOCAM_DATA_ACTIVE_HIGH;
  159. return soc_camera_apply_sensor_flags(icl, flags);
  160. }
  161. static int sh_csi2_notify(struct notifier_block *nb,
  162. unsigned long action, void *data)
  163. {
  164. struct device *dev = data;
  165. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  166. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev->parent);
  167. struct sh_csi2 *priv =
  168. container_of(nb, struct sh_csi2, notifier);
  169. struct sh_csi2_pdata *pdata = priv->pdev->dev.platform_data;
  170. int ret, i;
  171. for (i = 0; i < pdata->num_clients; i++)
  172. if (&pdata->clients[i].pdev->dev == icd->pdev)
  173. break;
  174. dev_dbg(dev, "%s(%p): action = %lu, found #%d\n", __func__, dev, action, i);
  175. if (i == pdata->num_clients)
  176. return NOTIFY_DONE;
  177. switch (action) {
  178. case BUS_NOTIFY_BOUND_DRIVER:
  179. snprintf(priv->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s%s",
  180. dev_name(v4l2_dev->dev), ".mipi-csi");
  181. ret = v4l2_device_register_subdev(v4l2_dev, &priv->subdev);
  182. dev_dbg(dev, "%s(%p): ret(register_subdev) = %d\n", __func__, priv, ret);
  183. if (ret < 0)
  184. return NOTIFY_DONE;
  185. priv->client = pdata->clients + i;
  186. icd->ops->set_bus_param = sh_csi2_set_bus_param;
  187. icd->ops->query_bus_param = sh_csi2_query_bus_param;
  188. pm_runtime_get_sync(v4l2_get_subdevdata(&priv->subdev));
  189. sh_csi2_hwinit(priv);
  190. break;
  191. case BUS_NOTIFY_UNBIND_DRIVER:
  192. priv->client = NULL;
  193. /* Driver is about to be unbound */
  194. icd->ops->set_bus_param = NULL;
  195. icd->ops->query_bus_param = NULL;
  196. v4l2_device_unregister_subdev(&priv->subdev);
  197. pm_runtime_put(v4l2_get_subdevdata(&priv->subdev));
  198. break;
  199. }
  200. return NOTIFY_OK;
  201. }
  202. static __devinit int sh_csi2_probe(struct platform_device *pdev)
  203. {
  204. struct resource *res;
  205. unsigned int irq;
  206. int ret;
  207. struct sh_csi2 *priv;
  208. /* Platform data specify the PHY, lanes, ECC, CRC */
  209. struct sh_csi2_pdata *pdata = pdev->dev.platform_data;
  210. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  211. /* Interrupt unused so far */
  212. irq = platform_get_irq(pdev, 0);
  213. if (!res || (int)irq <= 0 || !pdata) {
  214. dev_err(&pdev->dev, "Not enough CSI2 platform resources.\n");
  215. return -ENODEV;
  216. }
  217. /* TODO: Add support for CSI2I. Careful: different register layout! */
  218. if (pdata->type != SH_CSI2C) {
  219. dev_err(&pdev->dev, "Only CSI2C supported ATM.\n");
  220. return -EINVAL;
  221. }
  222. priv = kzalloc(sizeof(struct sh_csi2), GFP_KERNEL);
  223. if (!priv)
  224. return -ENOMEM;
  225. priv->irq = irq;
  226. priv->notifier.notifier_call = sh_csi2_notify;
  227. /* We MUST attach after the MIPI sensor */
  228. ret = bus_register_notifier(&soc_camera_bus_type, &priv->notifier);
  229. if (ret < 0) {
  230. dev_err(&pdev->dev, "CSI2 cannot register notifier\n");
  231. goto ernotify;
  232. }
  233. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  234. dev_err(&pdev->dev, "CSI2 register region already claimed\n");
  235. ret = -EBUSY;
  236. goto ereqreg;
  237. }
  238. priv->base = ioremap(res->start, resource_size(res));
  239. if (!priv->base) {
  240. ret = -ENXIO;
  241. dev_err(&pdev->dev, "Unable to ioremap CSI2 registers.\n");
  242. goto eremap;
  243. }
  244. priv->pdev = pdev;
  245. v4l2_subdev_init(&priv->subdev, &sh_csi2_subdev_ops);
  246. v4l2_set_subdevdata(&priv->subdev, &pdev->dev);
  247. platform_set_drvdata(pdev, priv);
  248. pm_runtime_enable(&pdev->dev);
  249. dev_dbg(&pdev->dev, "CSI2 probed.\n");
  250. return 0;
  251. eremap:
  252. release_mem_region(res->start, resource_size(res));
  253. ereqreg:
  254. bus_unregister_notifier(&soc_camera_bus_type, &priv->notifier);
  255. ernotify:
  256. kfree(priv);
  257. return ret;
  258. }
  259. static __devexit int sh_csi2_remove(struct platform_device *pdev)
  260. {
  261. struct sh_csi2 *priv = platform_get_drvdata(pdev);
  262. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  263. bus_unregister_notifier(&soc_camera_bus_type, &priv->notifier);
  264. pm_runtime_disable(&pdev->dev);
  265. iounmap(priv->base);
  266. release_mem_region(res->start, resource_size(res));
  267. platform_set_drvdata(pdev, NULL);
  268. kfree(priv);
  269. return 0;
  270. }
  271. static struct platform_driver __refdata sh_csi2_pdrv = {
  272. .remove = __devexit_p(sh_csi2_remove),
  273. .driver = {
  274. .name = "sh-mobile-csi2",
  275. .owner = THIS_MODULE,
  276. },
  277. };
  278. static int __init sh_csi2_init(void)
  279. {
  280. return platform_driver_probe(&sh_csi2_pdrv, sh_csi2_probe);
  281. }
  282. static void __exit sh_csi2_exit(void)
  283. {
  284. platform_driver_unregister(&sh_csi2_pdrv);
  285. }
  286. module_init(sh_csi2_init);
  287. module_exit(sh_csi2_exit);
  288. MODULE_DESCRIPTION("SH-Mobile MIPI CSI-2 driver");
  289. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  290. MODULE_LICENSE("GPL v2");
  291. MODULE_ALIAS("platform:sh-mobile-csi2");