sh_mobile_csi2.c 9.8 KB

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