cdns-csi2rx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Cadence MIPI-CSI2 RX Controller v1.3
  4. *
  5. * Copyright (C) 2017 Cadence Design Systems Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <media/v4l2-ctrls.h>
  17. #include <media/v4l2-device.h>
  18. #include <media/v4l2-fwnode.h>
  19. #include <media/v4l2-subdev.h>
  20. #define CSI2RX_DEVICE_CFG_REG 0x000
  21. #define CSI2RX_SOFT_RESET_REG 0x004
  22. #define CSI2RX_SOFT_RESET_PROTOCOL BIT(1)
  23. #define CSI2RX_SOFT_RESET_FRONT BIT(0)
  24. #define CSI2RX_STATIC_CFG_REG 0x008
  25. #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane) ((plane) << (16 + (llane) * 4))
  26. #define CSI2RX_STATIC_CFG_LANES_MASK GENMASK(11, 8)
  27. #define CSI2RX_STREAM_BASE(n) (((n) + 1) * 0x100)
  28. #define CSI2RX_STREAM_CTRL_REG(n) (CSI2RX_STREAM_BASE(n) + 0x000)
  29. #define CSI2RX_STREAM_CTRL_START BIT(0)
  30. #define CSI2RX_STREAM_DATA_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x008)
  31. #define CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT BIT(31)
  32. #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n) BIT((n) + 16)
  33. #define CSI2RX_STREAM_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x00c)
  34. #define CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF (1 << 8)
  35. #define CSI2RX_LANES_MAX 4
  36. #define CSI2RX_STREAMS_MAX 4
  37. enum csi2rx_pads {
  38. CSI2RX_PAD_SINK,
  39. CSI2RX_PAD_SOURCE_STREAM0,
  40. CSI2RX_PAD_SOURCE_STREAM1,
  41. CSI2RX_PAD_SOURCE_STREAM2,
  42. CSI2RX_PAD_SOURCE_STREAM3,
  43. CSI2RX_PAD_MAX,
  44. };
  45. struct csi2rx_priv {
  46. struct device *dev;
  47. unsigned int count;
  48. /*
  49. * Used to prevent race conditions between multiple,
  50. * concurrent calls to start and stop.
  51. */
  52. struct mutex lock;
  53. void __iomem *base;
  54. struct clk *sys_clk;
  55. struct clk *p_clk;
  56. struct clk *pixel_clk[CSI2RX_STREAMS_MAX];
  57. struct phy *dphy;
  58. u8 lanes[CSI2RX_LANES_MAX];
  59. u8 num_lanes;
  60. u8 max_lanes;
  61. u8 max_streams;
  62. bool has_internal_dphy;
  63. struct v4l2_subdev subdev;
  64. struct v4l2_async_notifier notifier;
  65. struct media_pad pads[CSI2RX_PAD_MAX];
  66. /* Remote source */
  67. struct v4l2_subdev *source_subdev;
  68. int source_pad;
  69. };
  70. static inline
  71. struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev)
  72. {
  73. return container_of(subdev, struct csi2rx_priv, subdev);
  74. }
  75. static void csi2rx_reset(struct csi2rx_priv *csi2rx)
  76. {
  77. writel(CSI2RX_SOFT_RESET_PROTOCOL | CSI2RX_SOFT_RESET_FRONT,
  78. csi2rx->base + CSI2RX_SOFT_RESET_REG);
  79. udelay(10);
  80. writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG);
  81. }
  82. static int csi2rx_start(struct csi2rx_priv *csi2rx)
  83. {
  84. unsigned int i;
  85. unsigned long lanes_used = 0;
  86. u32 reg;
  87. int ret;
  88. ret = clk_prepare_enable(csi2rx->p_clk);
  89. if (ret)
  90. return ret;
  91. csi2rx_reset(csi2rx);
  92. reg = csi2rx->num_lanes << 8;
  93. for (i = 0; i < csi2rx->num_lanes; i++) {
  94. reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, csi2rx->lanes[i]);
  95. set_bit(csi2rx->lanes[i], &lanes_used);
  96. }
  97. /*
  98. * Even the unused lanes need to be mapped. In order to avoid
  99. * to map twice to the same physical lane, keep the lanes used
  100. * in the previous loop, and only map unused physical lanes to
  101. * the rest of our logical lanes.
  102. */
  103. for (i = csi2rx->num_lanes; i < csi2rx->max_lanes; i++) {
  104. unsigned int idx = find_first_zero_bit(&lanes_used,
  105. csi2rx->max_lanes);
  106. set_bit(idx, &lanes_used);
  107. reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, i + 1);
  108. }
  109. writel(reg, csi2rx->base + CSI2RX_STATIC_CFG_REG);
  110. ret = v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, true);
  111. if (ret)
  112. goto err_disable_pclk;
  113. /*
  114. * Create a static mapping between the CSI virtual channels
  115. * and the output stream.
  116. *
  117. * This should be enhanced, but v4l2 lacks the support for
  118. * changing that mapping dynamically.
  119. *
  120. * We also cannot enable and disable independent streams here,
  121. * hence the reference counting.
  122. */
  123. for (i = 0; i < csi2rx->max_streams; i++) {
  124. ret = clk_prepare_enable(csi2rx->pixel_clk[i]);
  125. if (ret)
  126. goto err_disable_pixclk;
  127. writel(CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF,
  128. csi2rx->base + CSI2RX_STREAM_CFG_REG(i));
  129. writel(CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT |
  130. CSI2RX_STREAM_DATA_CFG_VC_SELECT(i),
  131. csi2rx->base + CSI2RX_STREAM_DATA_CFG_REG(i));
  132. writel(CSI2RX_STREAM_CTRL_START,
  133. csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
  134. }
  135. ret = clk_prepare_enable(csi2rx->sys_clk);
  136. if (ret)
  137. goto err_disable_pixclk;
  138. clk_disable_unprepare(csi2rx->p_clk);
  139. return 0;
  140. err_disable_pixclk:
  141. for (; i > 0; i--)
  142. clk_disable_unprepare(csi2rx->pixel_clk[i - 1]);
  143. err_disable_pclk:
  144. clk_disable_unprepare(csi2rx->p_clk);
  145. return ret;
  146. }
  147. static void csi2rx_stop(struct csi2rx_priv *csi2rx)
  148. {
  149. unsigned int i;
  150. clk_prepare_enable(csi2rx->p_clk);
  151. clk_disable_unprepare(csi2rx->sys_clk);
  152. for (i = 0; i < csi2rx->max_streams; i++) {
  153. writel(0, csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
  154. clk_disable_unprepare(csi2rx->pixel_clk[i]);
  155. }
  156. clk_disable_unprepare(csi2rx->p_clk);
  157. if (v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, false))
  158. dev_warn(csi2rx->dev, "Couldn't disable our subdev\n");
  159. }
  160. static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable)
  161. {
  162. struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
  163. int ret = 0;
  164. mutex_lock(&csi2rx->lock);
  165. if (enable) {
  166. /*
  167. * If we're not the first users, there's no need to
  168. * enable the whole controller.
  169. */
  170. if (!csi2rx->count) {
  171. ret = csi2rx_start(csi2rx);
  172. if (ret)
  173. goto out;
  174. }
  175. csi2rx->count++;
  176. } else {
  177. csi2rx->count--;
  178. /*
  179. * Let the last user turn off the lights.
  180. */
  181. if (!csi2rx->count)
  182. csi2rx_stop(csi2rx);
  183. }
  184. out:
  185. mutex_unlock(&csi2rx->lock);
  186. return ret;
  187. }
  188. static const struct v4l2_subdev_video_ops csi2rx_video_ops = {
  189. .s_stream = csi2rx_s_stream,
  190. };
  191. static const struct v4l2_subdev_ops csi2rx_subdev_ops = {
  192. .video = &csi2rx_video_ops,
  193. };
  194. static int csi2rx_async_bound(struct v4l2_async_notifier *notifier,
  195. struct v4l2_subdev *s_subdev,
  196. struct v4l2_async_subdev *asd)
  197. {
  198. struct v4l2_subdev *subdev = notifier->sd;
  199. struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
  200. csi2rx->source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
  201. s_subdev->fwnode,
  202. MEDIA_PAD_FL_SOURCE);
  203. if (csi2rx->source_pad < 0) {
  204. dev_err(csi2rx->dev, "Couldn't find output pad for subdev %s\n",
  205. s_subdev->name);
  206. return csi2rx->source_pad;
  207. }
  208. csi2rx->source_subdev = s_subdev;
  209. dev_dbg(csi2rx->dev, "Bound %s pad: %d\n", s_subdev->name,
  210. csi2rx->source_pad);
  211. return media_create_pad_link(&csi2rx->source_subdev->entity,
  212. csi2rx->source_pad,
  213. &csi2rx->subdev.entity, 0,
  214. MEDIA_LNK_FL_ENABLED |
  215. MEDIA_LNK_FL_IMMUTABLE);
  216. }
  217. static const struct v4l2_async_notifier_operations csi2rx_notifier_ops = {
  218. .bound = csi2rx_async_bound,
  219. };
  220. static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
  221. struct platform_device *pdev)
  222. {
  223. unsigned char i;
  224. u32 dev_cfg;
  225. int ret;
  226. csi2rx->base = devm_platform_ioremap_resource(pdev, 0);
  227. if (IS_ERR(csi2rx->base))
  228. return PTR_ERR(csi2rx->base);
  229. csi2rx->sys_clk = devm_clk_get(&pdev->dev, "sys_clk");
  230. if (IS_ERR(csi2rx->sys_clk)) {
  231. dev_err(&pdev->dev, "Couldn't get sys clock\n");
  232. return PTR_ERR(csi2rx->sys_clk);
  233. }
  234. csi2rx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
  235. if (IS_ERR(csi2rx->p_clk)) {
  236. dev_err(&pdev->dev, "Couldn't get P clock\n");
  237. return PTR_ERR(csi2rx->p_clk);
  238. }
  239. csi2rx->dphy = devm_phy_optional_get(&pdev->dev, "dphy");
  240. if (IS_ERR(csi2rx->dphy)) {
  241. dev_err(&pdev->dev, "Couldn't get external D-PHY\n");
  242. return PTR_ERR(csi2rx->dphy);
  243. }
  244. /*
  245. * FIXME: Once we'll have external D-PHY support, the check
  246. * will need to be removed.
  247. */
  248. if (csi2rx->dphy) {
  249. dev_err(&pdev->dev, "External D-PHY not supported yet\n");
  250. return -EINVAL;
  251. }
  252. ret = clk_prepare_enable(csi2rx->p_clk);
  253. if (ret) {
  254. dev_err(&pdev->dev, "Couldn't prepare and enable P clock\n");
  255. return ret;
  256. }
  257. dev_cfg = readl(csi2rx->base + CSI2RX_DEVICE_CFG_REG);
  258. clk_disable_unprepare(csi2rx->p_clk);
  259. csi2rx->max_lanes = dev_cfg & 7;
  260. if (csi2rx->max_lanes > CSI2RX_LANES_MAX) {
  261. dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
  262. csi2rx->max_lanes);
  263. return -EINVAL;
  264. }
  265. csi2rx->max_streams = (dev_cfg >> 4) & 7;
  266. if (csi2rx->max_streams > CSI2RX_STREAMS_MAX) {
  267. dev_err(&pdev->dev, "Invalid number of streams: %u\n",
  268. csi2rx->max_streams);
  269. return -EINVAL;
  270. }
  271. csi2rx->has_internal_dphy = dev_cfg & BIT(3) ? true : false;
  272. /*
  273. * FIXME: Once we'll have internal D-PHY support, the check
  274. * will need to be removed.
  275. */
  276. if (csi2rx->has_internal_dphy) {
  277. dev_err(&pdev->dev, "Internal D-PHY not supported yet\n");
  278. return -EINVAL;
  279. }
  280. for (i = 0; i < csi2rx->max_streams; i++) {
  281. char clk_name[16];
  282. snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
  283. csi2rx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
  284. if (IS_ERR(csi2rx->pixel_clk[i])) {
  285. dev_err(&pdev->dev, "Couldn't get clock %s\n", clk_name);
  286. return PTR_ERR(csi2rx->pixel_clk[i]);
  287. }
  288. }
  289. return 0;
  290. }
  291. static int csi2rx_parse_dt(struct csi2rx_priv *csi2rx)
  292. {
  293. struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
  294. struct v4l2_async_subdev *asd;
  295. struct fwnode_handle *fwh;
  296. struct device_node *ep;
  297. int ret;
  298. ep = of_graph_get_endpoint_by_regs(csi2rx->dev->of_node, 0, 0);
  299. if (!ep)
  300. return -EINVAL;
  301. fwh = of_fwnode_handle(ep);
  302. ret = v4l2_fwnode_endpoint_parse(fwh, &v4l2_ep);
  303. if (ret) {
  304. dev_err(csi2rx->dev, "Could not parse v4l2 endpoint\n");
  305. of_node_put(ep);
  306. return ret;
  307. }
  308. if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
  309. dev_err(csi2rx->dev, "Unsupported media bus type: 0x%x\n",
  310. v4l2_ep.bus_type);
  311. of_node_put(ep);
  312. return -EINVAL;
  313. }
  314. memcpy(csi2rx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
  315. sizeof(csi2rx->lanes));
  316. csi2rx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
  317. if (csi2rx->num_lanes > csi2rx->max_lanes) {
  318. dev_err(csi2rx->dev, "Unsupported number of data-lanes: %d\n",
  319. csi2rx->num_lanes);
  320. of_node_put(ep);
  321. return -EINVAL;
  322. }
  323. v4l2_async_nf_init(&csi2rx->notifier);
  324. asd = v4l2_async_nf_add_fwnode_remote(&csi2rx->notifier, fwh,
  325. struct v4l2_async_subdev);
  326. of_node_put(ep);
  327. if (IS_ERR(asd)) {
  328. v4l2_async_nf_cleanup(&csi2rx->notifier);
  329. return PTR_ERR(asd);
  330. }
  331. csi2rx->notifier.ops = &csi2rx_notifier_ops;
  332. ret = v4l2_async_subdev_nf_register(&csi2rx->subdev, &csi2rx->notifier);
  333. if (ret)
  334. v4l2_async_nf_cleanup(&csi2rx->notifier);
  335. return ret;
  336. }
  337. static int csi2rx_probe(struct platform_device *pdev)
  338. {
  339. struct csi2rx_priv *csi2rx;
  340. unsigned int i;
  341. int ret;
  342. csi2rx = kzalloc(sizeof(*csi2rx), GFP_KERNEL);
  343. if (!csi2rx)
  344. return -ENOMEM;
  345. platform_set_drvdata(pdev, csi2rx);
  346. csi2rx->dev = &pdev->dev;
  347. mutex_init(&csi2rx->lock);
  348. ret = csi2rx_get_resources(csi2rx, pdev);
  349. if (ret)
  350. goto err_free_priv;
  351. ret = csi2rx_parse_dt(csi2rx);
  352. if (ret)
  353. goto err_free_priv;
  354. csi2rx->subdev.owner = THIS_MODULE;
  355. csi2rx->subdev.dev = &pdev->dev;
  356. v4l2_subdev_init(&csi2rx->subdev, &csi2rx_subdev_ops);
  357. v4l2_set_subdevdata(&csi2rx->subdev, &pdev->dev);
  358. snprintf(csi2rx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
  359. KBUILD_MODNAME, dev_name(&pdev->dev));
  360. /* Create our media pads */
  361. csi2rx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  362. csi2rx->pads[CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  363. for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++)
  364. csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE;
  365. ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX,
  366. csi2rx->pads);
  367. if (ret)
  368. goto err_cleanup;
  369. ret = v4l2_async_register_subdev(&csi2rx->subdev);
  370. if (ret < 0)
  371. goto err_cleanup;
  372. dev_info(&pdev->dev,
  373. "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
  374. csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams,
  375. csi2rx->has_internal_dphy ? "internal" : "no");
  376. return 0;
  377. err_cleanup:
  378. v4l2_async_nf_unregister(&csi2rx->notifier);
  379. v4l2_async_nf_cleanup(&csi2rx->notifier);
  380. err_free_priv:
  381. kfree(csi2rx);
  382. return ret;
  383. }
  384. static int csi2rx_remove(struct platform_device *pdev)
  385. {
  386. struct csi2rx_priv *csi2rx = platform_get_drvdata(pdev);
  387. v4l2_async_nf_unregister(&csi2rx->notifier);
  388. v4l2_async_nf_cleanup(&csi2rx->notifier);
  389. v4l2_async_unregister_subdev(&csi2rx->subdev);
  390. kfree(csi2rx);
  391. return 0;
  392. }
  393. static const struct of_device_id csi2rx_of_table[] = {
  394. { .compatible = "cdns,csi2rx" },
  395. { },
  396. };
  397. MODULE_DEVICE_TABLE(of, csi2rx_of_table);
  398. static struct platform_driver csi2rx_driver = {
  399. .probe = csi2rx_probe,
  400. .remove = csi2rx_remove,
  401. .driver = {
  402. .name = "cdns-csi2rx",
  403. .of_match_table = csi2rx_of_table,
  404. },
  405. };
  406. module_platform_driver(csi2rx_driver);
  407. MODULE_AUTHOR("Maxime Ripard <[email protected]>");
  408. MODULE_DESCRIPTION("Cadence CSI2-RX controller");
  409. MODULE_LICENSE("GPL");