cdns-csi2rx.c 12 KB

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