mmp-driver.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support for the camera device found on Marvell MMP processors; known
  4. * to work with the Armada 610 as used in the OLPC 1.75 system.
  5. *
  6. * Copyright 2011 Jonathan Corbet <[email protected]>
  7. * Copyright 2018 Lubomir Rintel <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/slab.h>
  15. #include <linux/videodev2.h>
  16. #include <media/v4l2-device.h>
  17. #include <linux/platform_data/media/mmp-camera.h>
  18. #include <linux/device.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/io.h>
  24. #include <linux/list.h>
  25. #include <linux/pm.h>
  26. #include <linux/clk.h>
  27. #include "mcam-core.h"
  28. MODULE_ALIAS("platform:mmp-camera");
  29. MODULE_AUTHOR("Jonathan Corbet <[email protected]>");
  30. MODULE_LICENSE("GPL");
  31. static char *mcam_clks[] = {"axi", "func", "phy"};
  32. struct mmp_camera {
  33. struct platform_device *pdev;
  34. struct mcam_camera mcam;
  35. struct list_head devlist;
  36. struct clk *mipi_clk;
  37. int irq;
  38. };
  39. static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
  40. {
  41. return container_of(mcam, struct mmp_camera, mcam);
  42. }
  43. /*
  44. * calc the dphy register values
  45. * There are three dphy registers being used.
  46. * dphy[0] - CSI2_DPHY3
  47. * dphy[1] - CSI2_DPHY5
  48. * dphy[2] - CSI2_DPHY6
  49. * CSI2_DPHY3 and CSI2_DPHY6 can be set with a default value
  50. * or be calculated dynamically
  51. */
  52. static void mmpcam_calc_dphy(struct mcam_camera *mcam)
  53. {
  54. struct mmp_camera *cam = mcam_to_cam(mcam);
  55. struct mmp_camera_platform_data *pdata = cam->pdev->dev.platform_data;
  56. struct device *dev = &cam->pdev->dev;
  57. unsigned long tx_clk_esc;
  58. /*
  59. * If CSI2_DPHY3 is calculated dynamically,
  60. * pdata->lane_clk should be already set
  61. * either in the board driver statically
  62. * or in the sensor driver dynamically.
  63. */
  64. /*
  65. * dphy[0] - CSI2_DPHY3:
  66. * bit 0 ~ bit 7: HS Term Enable.
  67. * defines the time that the DPHY
  68. * wait before enabling the data
  69. * lane termination after detecting
  70. * that the sensor has driven the data
  71. * lanes to the LP00 bridge state.
  72. * The value is calculated by:
  73. * (Max T(D_TERM_EN)/Period(DDR)) - 1
  74. * bit 8 ~ bit 15: HS_SETTLE
  75. * Time interval during which the HS
  76. * receiver shall ignore any Data Lane
  77. * HS transitions.
  78. * The value has been calibrated on
  79. * different boards. It seems to work well.
  80. *
  81. * More detail please refer
  82. * MIPI Alliance Spectification for D-PHY
  83. * document for explanation of HS-SETTLE
  84. * and D-TERM-EN.
  85. */
  86. switch (pdata->dphy3_algo) {
  87. case DPHY3_ALGO_PXA910:
  88. /*
  89. * Calculate CSI2_DPHY3 algo for PXA910
  90. */
  91. pdata->dphy[0] =
  92. (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
  93. | (1 + pdata->lane_clk * 35 / 1000);
  94. break;
  95. case DPHY3_ALGO_PXA2128:
  96. /*
  97. * Calculate CSI2_DPHY3 algo for PXA2128
  98. */
  99. pdata->dphy[0] =
  100. (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
  101. | (1 + pdata->lane_clk * 35 / 1000);
  102. break;
  103. default:
  104. /*
  105. * Use default CSI2_DPHY3 value for PXA688/PXA988
  106. */
  107. dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
  108. }
  109. /*
  110. * mipi_clk will never be changed, it is a fixed value on MMP
  111. */
  112. if (IS_ERR(cam->mipi_clk))
  113. return;
  114. /* get the escape clk, this is hard coded */
  115. clk_prepare_enable(cam->mipi_clk);
  116. tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
  117. clk_disable_unprepare(cam->mipi_clk);
  118. /*
  119. * dphy[2] - CSI2_DPHY6:
  120. * bit 0 ~ bit 7: CK Term Enable
  121. * Time for the Clock Lane receiver to enable the HS line
  122. * termination. The value is calculated similarly with
  123. * HS Term Enable
  124. * bit 8 ~ bit 15: CK Settle
  125. * Time interval during which the HS receiver shall ignore
  126. * any Clock Lane HS transitions.
  127. * The value is calibrated on the boards.
  128. */
  129. pdata->dphy[2] =
  130. ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
  131. | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
  132. dev_dbg(dev, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
  133. pdata->dphy[0], pdata->dphy[1], pdata->dphy[2]);
  134. }
  135. static irqreturn_t mmpcam_irq(int irq, void *data)
  136. {
  137. struct mcam_camera *mcam = data;
  138. unsigned int irqs, handled;
  139. spin_lock(&mcam->dev_lock);
  140. irqs = mcam_reg_read(mcam, REG_IRQSTAT);
  141. handled = mccic_irq(mcam, irqs);
  142. spin_unlock(&mcam->dev_lock);
  143. return IRQ_RETVAL(handled);
  144. }
  145. static void mcam_init_clk(struct mcam_camera *mcam)
  146. {
  147. unsigned int i;
  148. for (i = 0; i < NR_MCAM_CLK; i++) {
  149. if (mcam_clks[i] != NULL) {
  150. /* Some clks are not necessary on some boards
  151. * We still try to run even it fails getting clk
  152. */
  153. mcam->clk[i] = devm_clk_get(mcam->dev, mcam_clks[i]);
  154. if (IS_ERR(mcam->clk[i]))
  155. dev_warn(mcam->dev, "Could not get clk: %s\n",
  156. mcam_clks[i]);
  157. }
  158. }
  159. }
  160. static int mmpcam_probe(struct platform_device *pdev)
  161. {
  162. struct mmp_camera *cam;
  163. struct mcam_camera *mcam;
  164. struct resource *res;
  165. struct fwnode_handle *ep;
  166. struct mmp_camera_platform_data *pdata;
  167. struct v4l2_async_subdev *asd;
  168. int ret;
  169. cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL);
  170. if (cam == NULL)
  171. return -ENOMEM;
  172. platform_set_drvdata(pdev, cam);
  173. cam->pdev = pdev;
  174. INIT_LIST_HEAD(&cam->devlist);
  175. mcam = &cam->mcam;
  176. mcam->calc_dphy = mmpcam_calc_dphy;
  177. mcam->dev = &pdev->dev;
  178. pdata = pdev->dev.platform_data;
  179. if (pdata) {
  180. mcam->mclk_src = pdata->mclk_src;
  181. mcam->mclk_div = pdata->mclk_div;
  182. mcam->bus_type = pdata->bus_type;
  183. mcam->dphy = pdata->dphy;
  184. mcam->lane = pdata->lane;
  185. } else {
  186. /*
  187. * These are values that used to be hardcoded in mcam-core and
  188. * work well on a OLPC XO 1.75 with a parallel bus sensor.
  189. * If it turns out other setups make sense, the values should
  190. * be obtained from the device tree.
  191. */
  192. mcam->mclk_src = 3;
  193. mcam->mclk_div = 2;
  194. }
  195. if (mcam->bus_type == V4L2_MBUS_CSI2_DPHY) {
  196. cam->mipi_clk = devm_clk_get(mcam->dev, "mipi");
  197. if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0))
  198. return PTR_ERR(cam->mipi_clk);
  199. }
  200. mcam->mipi_enabled = false;
  201. mcam->chip_id = MCAM_ARMADA610;
  202. mcam->buffer_mode = B_DMA_sg;
  203. strscpy(mcam->bus_info, "platform:mmp-camera", sizeof(mcam->bus_info));
  204. spin_lock_init(&mcam->dev_lock);
  205. /*
  206. * Get our I/O memory.
  207. */
  208. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  209. mcam->regs = devm_ioremap_resource(&pdev->dev, res);
  210. if (IS_ERR(mcam->regs))
  211. return PTR_ERR(mcam->regs);
  212. mcam->regs_size = resource_size(res);
  213. mcam_init_clk(mcam);
  214. /*
  215. * Create a match of the sensor against its OF node.
  216. */
  217. ep = fwnode_graph_get_next_endpoint(of_fwnode_handle(pdev->dev.of_node),
  218. NULL);
  219. if (!ep)
  220. return -ENODEV;
  221. v4l2_async_nf_init(&mcam->notifier);
  222. asd = v4l2_async_nf_add_fwnode_remote(&mcam->notifier, ep,
  223. struct v4l2_async_subdev);
  224. fwnode_handle_put(ep);
  225. if (IS_ERR(asd)) {
  226. ret = PTR_ERR(asd);
  227. goto out;
  228. }
  229. /*
  230. * Register the device with the core.
  231. */
  232. ret = mccic_register(mcam);
  233. if (ret)
  234. return ret;
  235. /*
  236. * Add OF clock provider.
  237. */
  238. ret = of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get,
  239. mcam->mclk);
  240. if (ret) {
  241. dev_err(&pdev->dev, "can't add DT clock provider\n");
  242. goto out;
  243. }
  244. /*
  245. * Finally, set up our IRQ now that the core is ready to
  246. * deal with it.
  247. */
  248. ret = platform_get_irq(pdev, 0);
  249. if (ret < 0)
  250. goto out;
  251. cam->irq = ret;
  252. ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED,
  253. "mmp-camera", mcam);
  254. if (ret)
  255. goto out;
  256. pm_runtime_enable(&pdev->dev);
  257. return 0;
  258. out:
  259. mccic_shutdown(mcam);
  260. return ret;
  261. }
  262. static int mmpcam_remove(struct mmp_camera *cam)
  263. {
  264. struct mcam_camera *mcam = &cam->mcam;
  265. mccic_shutdown(mcam);
  266. pm_runtime_force_suspend(mcam->dev);
  267. return 0;
  268. }
  269. static int mmpcam_platform_remove(struct platform_device *pdev)
  270. {
  271. struct mmp_camera *cam = platform_get_drvdata(pdev);
  272. if (cam == NULL)
  273. return -ENODEV;
  274. return mmpcam_remove(cam);
  275. }
  276. /*
  277. * Suspend/resume support.
  278. */
  279. static int __maybe_unused mmpcam_runtime_resume(struct device *dev)
  280. {
  281. struct mmp_camera *cam = dev_get_drvdata(dev);
  282. struct mcam_camera *mcam = &cam->mcam;
  283. unsigned int i;
  284. for (i = 0; i < NR_MCAM_CLK; i++) {
  285. if (!IS_ERR(mcam->clk[i]))
  286. clk_prepare_enable(mcam->clk[i]);
  287. }
  288. return 0;
  289. }
  290. static int __maybe_unused mmpcam_runtime_suspend(struct device *dev)
  291. {
  292. struct mmp_camera *cam = dev_get_drvdata(dev);
  293. struct mcam_camera *mcam = &cam->mcam;
  294. int i;
  295. for (i = NR_MCAM_CLK - 1; i >= 0; i--) {
  296. if (!IS_ERR(mcam->clk[i]))
  297. clk_disable_unprepare(mcam->clk[i]);
  298. }
  299. return 0;
  300. }
  301. static int __maybe_unused mmpcam_suspend(struct device *dev)
  302. {
  303. struct mmp_camera *cam = dev_get_drvdata(dev);
  304. if (!pm_runtime_suspended(dev))
  305. mccic_suspend(&cam->mcam);
  306. return 0;
  307. }
  308. static int __maybe_unused mmpcam_resume(struct device *dev)
  309. {
  310. struct mmp_camera *cam = dev_get_drvdata(dev);
  311. if (!pm_runtime_suspended(dev))
  312. return mccic_resume(&cam->mcam);
  313. return 0;
  314. }
  315. static const struct dev_pm_ops mmpcam_pm_ops = {
  316. SET_RUNTIME_PM_OPS(mmpcam_runtime_suspend, mmpcam_runtime_resume, NULL)
  317. SET_SYSTEM_SLEEP_PM_OPS(mmpcam_suspend, mmpcam_resume)
  318. };
  319. static const struct of_device_id mmpcam_of_match[] = {
  320. { .compatible = "marvell,mmp2-ccic", },
  321. {},
  322. };
  323. MODULE_DEVICE_TABLE(of, mmpcam_of_match);
  324. static struct platform_driver mmpcam_driver = {
  325. .probe = mmpcam_probe,
  326. .remove = mmpcam_platform_remove,
  327. .driver = {
  328. .name = "mmp-camera",
  329. .of_match_table = of_match_ptr(mmpcam_of_match),
  330. .pm = &mmpcam_pm_ops,
  331. }
  332. };
  333. module_platform_driver(mmpcam_driver);