sh_mobile_csi2.c 10 KB

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