cdns-csi2tx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Cadence MIPI-CSI2 TX Controller
  4. *
  5. * Copyright (C) 2017-2018 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/mutex.h>
  12. #include <linux/of.h>
  13. #include <linux/of_graph.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 CSI2TX_DEVICE_CONFIG_REG 0x00
  21. #define CSI2TX_DEVICE_CONFIG_STREAMS_MASK GENMASK(6, 4)
  22. #define CSI2TX_DEVICE_CONFIG_HAS_DPHY BIT(3)
  23. #define CSI2TX_DEVICE_CONFIG_LANES_MASK GENMASK(2, 0)
  24. #define CSI2TX_CONFIG_REG 0x20
  25. #define CSI2TX_CONFIG_CFG_REQ BIT(2)
  26. #define CSI2TX_CONFIG_SRST_REQ BIT(1)
  27. #define CSI2TX_DPHY_CFG_REG 0x28
  28. #define CSI2TX_DPHY_CFG_CLK_RESET BIT(16)
  29. #define CSI2TX_DPHY_CFG_LANE_RESET(n) BIT((n) + 12)
  30. #define CSI2TX_DPHY_CFG_MODE_MASK GENMASK(9, 8)
  31. #define CSI2TX_DPHY_CFG_MODE_LPDT (2 << 8)
  32. #define CSI2TX_DPHY_CFG_MODE_HS (1 << 8)
  33. #define CSI2TX_DPHY_CFG_MODE_ULPS (0 << 8)
  34. #define CSI2TX_DPHY_CFG_CLK_ENABLE BIT(4)
  35. #define CSI2TX_DPHY_CFG_LANE_ENABLE(n) BIT(n)
  36. #define CSI2TX_DPHY_CLK_WAKEUP_REG 0x2c
  37. #define CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(n) ((n) & 0xffff)
  38. #define CSI2TX_DT_CFG_REG(n) (0x80 + (n) * 8)
  39. #define CSI2TX_DT_CFG_DT(n) (((n) & 0x3f) << 2)
  40. #define CSI2TX_DT_FORMAT_REG(n) (0x84 + (n) * 8)
  41. #define CSI2TX_DT_FORMAT_BYTES_PER_LINE(n) (((n) & 0xffff) << 16)
  42. #define CSI2TX_DT_FORMAT_MAX_LINE_NUM(n) ((n) & 0xffff)
  43. #define CSI2TX_STREAM_IF_CFG_REG(n) (0x100 + (n) * 4)
  44. #define CSI2TX_STREAM_IF_CFG_FILL_LEVEL(n) ((n) & 0x1f)
  45. #define CSI2TX_LANES_MAX 4
  46. #define CSI2TX_STREAMS_MAX 4
  47. enum csi2tx_pads {
  48. CSI2TX_PAD_SOURCE,
  49. CSI2TX_PAD_SINK_STREAM0,
  50. CSI2TX_PAD_SINK_STREAM1,
  51. CSI2TX_PAD_SINK_STREAM2,
  52. CSI2TX_PAD_SINK_STREAM3,
  53. CSI2TX_PAD_MAX,
  54. };
  55. struct csi2tx_fmt {
  56. u32 mbus;
  57. u32 dt;
  58. u32 bpp;
  59. };
  60. struct csi2tx_priv {
  61. struct device *dev;
  62. unsigned int count;
  63. /*
  64. * Used to prevent race conditions between multiple,
  65. * concurrent calls to start and stop.
  66. */
  67. struct mutex lock;
  68. void __iomem *base;
  69. struct clk *esc_clk;
  70. struct clk *p_clk;
  71. struct clk *pixel_clk[CSI2TX_STREAMS_MAX];
  72. struct v4l2_subdev subdev;
  73. struct media_pad pads[CSI2TX_PAD_MAX];
  74. struct v4l2_mbus_framefmt pad_fmts[CSI2TX_PAD_MAX];
  75. bool has_internal_dphy;
  76. u8 lanes[CSI2TX_LANES_MAX];
  77. unsigned int num_lanes;
  78. unsigned int max_lanes;
  79. unsigned int max_streams;
  80. };
  81. static const struct csi2tx_fmt csi2tx_formats[] = {
  82. {
  83. .mbus = MEDIA_BUS_FMT_UYVY8_1X16,
  84. .bpp = 2,
  85. .dt = 0x1e,
  86. },
  87. {
  88. .mbus = MEDIA_BUS_FMT_RGB888_1X24,
  89. .bpp = 3,
  90. .dt = 0x24,
  91. },
  92. };
  93. static const struct v4l2_mbus_framefmt fmt_default = {
  94. .width = 1280,
  95. .height = 720,
  96. .code = MEDIA_BUS_FMT_RGB888_1X24,
  97. .field = V4L2_FIELD_NONE,
  98. .colorspace = V4L2_COLORSPACE_DEFAULT,
  99. };
  100. static inline
  101. struct csi2tx_priv *v4l2_subdev_to_csi2tx(struct v4l2_subdev *subdev)
  102. {
  103. return container_of(subdev, struct csi2tx_priv, subdev);
  104. }
  105. static const struct csi2tx_fmt *csi2tx_get_fmt_from_mbus(u32 mbus)
  106. {
  107. unsigned int i;
  108. for (i = 0; i < ARRAY_SIZE(csi2tx_formats); i++)
  109. if (csi2tx_formats[i].mbus == mbus)
  110. return &csi2tx_formats[i];
  111. return NULL;
  112. }
  113. static int csi2tx_enum_mbus_code(struct v4l2_subdev *subdev,
  114. struct v4l2_subdev_pad_config *cfg,
  115. struct v4l2_subdev_mbus_code_enum *code)
  116. {
  117. if (code->pad || code->index >= ARRAY_SIZE(csi2tx_formats))
  118. return -EINVAL;
  119. code->code = csi2tx_formats[code->index].mbus;
  120. return 0;
  121. }
  122. static struct v4l2_mbus_framefmt *
  123. __csi2tx_get_pad_format(struct v4l2_subdev *subdev,
  124. struct v4l2_subdev_pad_config *cfg,
  125. struct v4l2_subdev_format *fmt)
  126. {
  127. struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
  128. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  129. return v4l2_subdev_get_try_format(subdev, cfg,
  130. fmt->pad);
  131. return &csi2tx->pad_fmts[fmt->pad];
  132. }
  133. static int csi2tx_get_pad_format(struct v4l2_subdev *subdev,
  134. struct v4l2_subdev_pad_config *cfg,
  135. struct v4l2_subdev_format *fmt)
  136. {
  137. const struct v4l2_mbus_framefmt *format;
  138. /* Multiplexed pad? */
  139. if (fmt->pad == CSI2TX_PAD_SOURCE)
  140. return -EINVAL;
  141. format = __csi2tx_get_pad_format(subdev, cfg, fmt);
  142. if (!format)
  143. return -EINVAL;
  144. fmt->format = *format;
  145. return 0;
  146. }
  147. static int csi2tx_set_pad_format(struct v4l2_subdev *subdev,
  148. struct v4l2_subdev_pad_config *cfg,
  149. struct v4l2_subdev_format *fmt)
  150. {
  151. const struct v4l2_mbus_framefmt *src_format = &fmt->format;
  152. struct v4l2_mbus_framefmt *dst_format;
  153. /* Multiplexed pad? */
  154. if (fmt->pad == CSI2TX_PAD_SOURCE)
  155. return -EINVAL;
  156. if (!csi2tx_get_fmt_from_mbus(fmt->format.code))
  157. src_format = &fmt_default;
  158. dst_format = __csi2tx_get_pad_format(subdev, cfg, fmt);
  159. if (!dst_format)
  160. return -EINVAL;
  161. *dst_format = *src_format;
  162. return 0;
  163. }
  164. static const struct v4l2_subdev_pad_ops csi2tx_pad_ops = {
  165. .enum_mbus_code = csi2tx_enum_mbus_code,
  166. .get_fmt = csi2tx_get_pad_format,
  167. .set_fmt = csi2tx_set_pad_format,
  168. };
  169. static void csi2tx_reset(struct csi2tx_priv *csi2tx)
  170. {
  171. writel(CSI2TX_CONFIG_SRST_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
  172. udelay(10);
  173. }
  174. static int csi2tx_start(struct csi2tx_priv *csi2tx)
  175. {
  176. struct media_entity *entity = &csi2tx->subdev.entity;
  177. struct media_link *link;
  178. unsigned int i;
  179. u32 reg;
  180. csi2tx_reset(csi2tx);
  181. writel(CSI2TX_CONFIG_CFG_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
  182. udelay(10);
  183. /* Configure our PPI interface with the D-PHY */
  184. writel(CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(32),
  185. csi2tx->base + CSI2TX_DPHY_CLK_WAKEUP_REG);
  186. /* Put our lanes (clock and data) out of reset */
  187. reg = CSI2TX_DPHY_CFG_CLK_RESET | CSI2TX_DPHY_CFG_MODE_LPDT;
  188. for (i = 0; i < csi2tx->num_lanes; i++)
  189. reg |= CSI2TX_DPHY_CFG_LANE_RESET(csi2tx->lanes[i]);
  190. writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
  191. udelay(10);
  192. /* Enable our (clock and data) lanes */
  193. reg |= CSI2TX_DPHY_CFG_CLK_ENABLE;
  194. for (i = 0; i < csi2tx->num_lanes; i++)
  195. reg |= CSI2TX_DPHY_CFG_LANE_ENABLE(csi2tx->lanes[i]);
  196. writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
  197. udelay(10);
  198. /* Switch to HS mode */
  199. reg &= ~CSI2TX_DPHY_CFG_MODE_MASK;
  200. writel(reg | CSI2TX_DPHY_CFG_MODE_HS,
  201. csi2tx->base + CSI2TX_DPHY_CFG_REG);
  202. udelay(10);
  203. /*
  204. * Create a static mapping between the CSI virtual channels
  205. * and the input streams.
  206. *
  207. * This should be enhanced, but v4l2 lacks the support for
  208. * changing that mapping dynamically at the moment.
  209. *
  210. * We're protected from the userspace setting up links at the
  211. * same time by the upper layer having called
  212. * media_pipeline_start().
  213. */
  214. list_for_each_entry(link, &entity->links, list) {
  215. struct v4l2_mbus_framefmt *mfmt;
  216. const struct csi2tx_fmt *fmt;
  217. unsigned int stream;
  218. int pad_idx = -1;
  219. /* Only consider our enabled input pads */
  220. for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++) {
  221. struct media_pad *pad = &csi2tx->pads[i];
  222. if ((pad == link->sink) &&
  223. (link->flags & MEDIA_LNK_FL_ENABLED)) {
  224. pad_idx = i;
  225. break;
  226. }
  227. }
  228. if (pad_idx < 0)
  229. continue;
  230. mfmt = &csi2tx->pad_fmts[pad_idx];
  231. fmt = csi2tx_get_fmt_from_mbus(mfmt->code);
  232. if (!fmt)
  233. continue;
  234. stream = pad_idx - CSI2TX_PAD_SINK_STREAM0;
  235. /*
  236. * We use the stream ID there, but it's wrong.
  237. *
  238. * A stream could very well send a data type that is
  239. * not equal to its stream ID. We need to find a
  240. * proper way to address it.
  241. */
  242. writel(CSI2TX_DT_CFG_DT(fmt->dt),
  243. csi2tx->base + CSI2TX_DT_CFG_REG(stream));
  244. writel(CSI2TX_DT_FORMAT_BYTES_PER_LINE(mfmt->width * fmt->bpp) |
  245. CSI2TX_DT_FORMAT_MAX_LINE_NUM(mfmt->height + 1),
  246. csi2tx->base + CSI2TX_DT_FORMAT_REG(stream));
  247. /*
  248. * TODO: This needs to be calculated based on the
  249. * output CSI2 clock rate.
  250. */
  251. writel(CSI2TX_STREAM_IF_CFG_FILL_LEVEL(4),
  252. csi2tx->base + CSI2TX_STREAM_IF_CFG_REG(stream));
  253. }
  254. /* Disable the configuration mode */
  255. writel(0, csi2tx->base + CSI2TX_CONFIG_REG);
  256. return 0;
  257. }
  258. static void csi2tx_stop(struct csi2tx_priv *csi2tx)
  259. {
  260. writel(CSI2TX_CONFIG_CFG_REQ | CSI2TX_CONFIG_SRST_REQ,
  261. csi2tx->base + CSI2TX_CONFIG_REG);
  262. }
  263. static int csi2tx_s_stream(struct v4l2_subdev *subdev, int enable)
  264. {
  265. struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
  266. int ret = 0;
  267. mutex_lock(&csi2tx->lock);
  268. if (enable) {
  269. /*
  270. * If we're not the first users, there's no need to
  271. * enable the whole controller.
  272. */
  273. if (!csi2tx->count) {
  274. ret = csi2tx_start(csi2tx);
  275. if (ret)
  276. goto out;
  277. }
  278. csi2tx->count++;
  279. } else {
  280. csi2tx->count--;
  281. /*
  282. * Let the last user turn off the lights.
  283. */
  284. if (!csi2tx->count)
  285. csi2tx_stop(csi2tx);
  286. }
  287. out:
  288. mutex_unlock(&csi2tx->lock);
  289. return ret;
  290. }
  291. static const struct v4l2_subdev_video_ops csi2tx_video_ops = {
  292. .s_stream = csi2tx_s_stream,
  293. };
  294. static const struct v4l2_subdev_ops csi2tx_subdev_ops = {
  295. .pad = &csi2tx_pad_ops,
  296. .video = &csi2tx_video_ops,
  297. };
  298. static int csi2tx_get_resources(struct csi2tx_priv *csi2tx,
  299. struct platform_device *pdev)
  300. {
  301. struct resource *res;
  302. unsigned int i;
  303. u32 dev_cfg;
  304. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  305. csi2tx->base = devm_ioremap_resource(&pdev->dev, res);
  306. if (IS_ERR(csi2tx->base))
  307. return PTR_ERR(csi2tx->base);
  308. csi2tx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
  309. if (IS_ERR(csi2tx->p_clk)) {
  310. dev_err(&pdev->dev, "Couldn't get p_clk\n");
  311. return PTR_ERR(csi2tx->p_clk);
  312. }
  313. csi2tx->esc_clk = devm_clk_get(&pdev->dev, "esc_clk");
  314. if (IS_ERR(csi2tx->esc_clk)) {
  315. dev_err(&pdev->dev, "Couldn't get the esc_clk\n");
  316. return PTR_ERR(csi2tx->esc_clk);
  317. }
  318. clk_prepare_enable(csi2tx->p_clk);
  319. dev_cfg = readl(csi2tx->base + CSI2TX_DEVICE_CONFIG_REG);
  320. clk_disable_unprepare(csi2tx->p_clk);
  321. csi2tx->max_lanes = dev_cfg & CSI2TX_DEVICE_CONFIG_LANES_MASK;
  322. if (csi2tx->max_lanes > CSI2TX_LANES_MAX) {
  323. dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
  324. csi2tx->max_lanes);
  325. return -EINVAL;
  326. }
  327. csi2tx->max_streams = (dev_cfg & CSI2TX_DEVICE_CONFIG_STREAMS_MASK) >> 4;
  328. if (csi2tx->max_streams > CSI2TX_STREAMS_MAX) {
  329. dev_err(&pdev->dev, "Invalid number of streams: %u\n",
  330. csi2tx->max_streams);
  331. return -EINVAL;
  332. }
  333. csi2tx->has_internal_dphy = !!(dev_cfg & CSI2TX_DEVICE_CONFIG_HAS_DPHY);
  334. for (i = 0; i < csi2tx->max_streams; i++) {
  335. char clk_name[16];
  336. snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
  337. csi2tx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
  338. if (IS_ERR(csi2tx->pixel_clk[i])) {
  339. dev_err(&pdev->dev, "Couldn't get clock %s\n",
  340. clk_name);
  341. return PTR_ERR(csi2tx->pixel_clk[i]);
  342. }
  343. }
  344. return 0;
  345. }
  346. static int csi2tx_check_lanes(struct csi2tx_priv *csi2tx)
  347. {
  348. struct v4l2_fwnode_endpoint v4l2_ep;
  349. struct device_node *ep;
  350. int ret;
  351. ep = of_graph_get_endpoint_by_regs(csi2tx->dev->of_node, 0, 0);
  352. if (!ep)
  353. return -EINVAL;
  354. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &v4l2_ep);
  355. if (ret) {
  356. dev_err(csi2tx->dev, "Could not parse v4l2 endpoint\n");
  357. goto out;
  358. }
  359. if (v4l2_ep.bus_type != V4L2_MBUS_CSI2) {
  360. dev_err(csi2tx->dev, "Unsupported media bus type: 0x%x\n",
  361. v4l2_ep.bus_type);
  362. ret = -EINVAL;
  363. goto out;
  364. }
  365. csi2tx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
  366. if (csi2tx->num_lanes > csi2tx->max_lanes) {
  367. dev_err(csi2tx->dev,
  368. "Current configuration uses more lanes than supported\n");
  369. ret = -EINVAL;
  370. goto out;
  371. }
  372. memcpy(csi2tx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
  373. sizeof(csi2tx->lanes));
  374. out:
  375. of_node_put(ep);
  376. return ret;
  377. }
  378. static int csi2tx_probe(struct platform_device *pdev)
  379. {
  380. struct csi2tx_priv *csi2tx;
  381. unsigned int i;
  382. int ret;
  383. csi2tx = kzalloc(sizeof(*csi2tx), GFP_KERNEL);
  384. if (!csi2tx)
  385. return -ENOMEM;
  386. platform_set_drvdata(pdev, csi2tx);
  387. mutex_init(&csi2tx->lock);
  388. csi2tx->dev = &pdev->dev;
  389. ret = csi2tx_get_resources(csi2tx, pdev);
  390. if (ret)
  391. goto err_free_priv;
  392. v4l2_subdev_init(&csi2tx->subdev, &csi2tx_subdev_ops);
  393. csi2tx->subdev.owner = THIS_MODULE;
  394. csi2tx->subdev.dev = &pdev->dev;
  395. csi2tx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  396. snprintf(csi2tx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
  397. KBUILD_MODNAME, dev_name(&pdev->dev));
  398. ret = csi2tx_check_lanes(csi2tx);
  399. if (ret)
  400. goto err_free_priv;
  401. /* Create our media pads */
  402. csi2tx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  403. csi2tx->pads[CSI2TX_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  404. for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
  405. csi2tx->pads[i].flags = MEDIA_PAD_FL_SINK;
  406. /*
  407. * Only the input pads are considered to have a format at the
  408. * moment. The CSI link can multiplex various streams with
  409. * different formats, and we can't expose this in v4l2 right
  410. * now.
  411. */
  412. for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
  413. csi2tx->pad_fmts[i] = fmt_default;
  414. ret = media_entity_pads_init(&csi2tx->subdev.entity, CSI2TX_PAD_MAX,
  415. csi2tx->pads);
  416. if (ret)
  417. goto err_free_priv;
  418. ret = v4l2_async_register_subdev(&csi2tx->subdev);
  419. if (ret < 0)
  420. goto err_free_priv;
  421. dev_info(&pdev->dev,
  422. "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n",
  423. csi2tx->num_lanes, csi2tx->max_lanes, csi2tx->max_streams,
  424. csi2tx->has_internal_dphy ? "internal" : "no");
  425. return 0;
  426. err_free_priv:
  427. kfree(csi2tx);
  428. return ret;
  429. }
  430. static int csi2tx_remove(struct platform_device *pdev)
  431. {
  432. struct csi2tx_priv *csi2tx = platform_get_drvdata(pdev);
  433. v4l2_async_unregister_subdev(&csi2tx->subdev);
  434. kfree(csi2tx);
  435. return 0;
  436. }
  437. static const struct of_device_id csi2tx_of_table[] = {
  438. { .compatible = "cdns,csi2tx" },
  439. { },
  440. };
  441. MODULE_DEVICE_TABLE(of, csi2tx_of_table);
  442. static struct platform_driver csi2tx_driver = {
  443. .probe = csi2tx_probe,
  444. .remove = csi2tx_remove,
  445. .driver = {
  446. .name = "cdns-csi2tx",
  447. .of_match_table = csi2tx_of_table,
  448. },
  449. };
  450. module_platform_driver(csi2tx_driver);
  451. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
  452. MODULE_DESCRIPTION("Cadence CSI2-TX controller");
  453. MODULE_LICENSE("GPL");