sh_mobile_csi2.c 9.9 KB

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