cdns-csi2rx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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/iopoll.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reset.h>
  17. #include <linux/slab.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-fwnode.h>
  21. #include <media/v4l2-subdev.h>
  22. #define CSI2RX_DEVICE_CFG_REG 0x000
  23. #define CSI2RX_SOFT_RESET_REG 0x004
  24. #define CSI2RX_SOFT_RESET_PROTOCOL BIT(1)
  25. #define CSI2RX_SOFT_RESET_FRONT BIT(0)
  26. #define CSI2RX_STATIC_CFG_REG 0x008
  27. #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane) ((plane) << (16 + (llane) * 4))
  28. #define CSI2RX_STATIC_CFG_LANES_MASK GENMASK(11, 8)
  29. #define CSI2RX_DPHY_LANE_CTRL_REG 0x40
  30. #define CSI2RX_DPHY_CL_RST BIT(16)
  31. #define CSI2RX_DPHY_DL_RST(i) BIT((i) + 12)
  32. #define CSI2RX_DPHY_CL_EN BIT(4)
  33. #define CSI2RX_DPHY_DL_EN(i) BIT(i)
  34. #define CSI2RX_STREAM_BASE(n) (((n) + 1) * 0x100)
  35. #define CSI2RX_STREAM_CTRL_REG(n) (CSI2RX_STREAM_BASE(n) + 0x000)
  36. #define CSI2RX_STREAM_CTRL_SOFT_RST BIT(4)
  37. #define CSI2RX_STREAM_CTRL_STOP BIT(1)
  38. #define CSI2RX_STREAM_CTRL_START BIT(0)
  39. #define CSI2RX_STREAM_STATUS_REG(n) (CSI2RX_STREAM_BASE(n) + 0x004)
  40. #define CSI2RX_STREAM_STATUS_RDY BIT(31)
  41. #define CSI2RX_STREAM_DATA_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x008)
  42. #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n) BIT((n) + 16)
  43. #define CSI2RX_STREAM_CFG_REG(n) (CSI2RX_STREAM_BASE(n) + 0x00c)
  44. #define CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF (1 << 8)
  45. #define CSI2RX_LANES_MAX 4
  46. #define CSI2RX_STREAMS_MAX 4
  47. enum csi2rx_pads {
  48. CSI2RX_PAD_SINK,
  49. CSI2RX_PAD_SOURCE_STREAM0,
  50. CSI2RX_PAD_SOURCE_STREAM1,
  51. CSI2RX_PAD_SOURCE_STREAM2,
  52. CSI2RX_PAD_SOURCE_STREAM3,
  53. CSI2RX_PAD_MAX,
  54. };
  55. struct csi2rx_fmt {
  56. u32 code;
  57. u8 bpp;
  58. };
  59. struct csi2rx_priv {
  60. struct device *dev;
  61. unsigned int count;
  62. /*
  63. * Used to prevent race conditions between multiple,
  64. * concurrent calls to start and stop.
  65. */
  66. struct mutex lock;
  67. void __iomem *base;
  68. struct clk *sys_clk;
  69. struct clk *p_clk;
  70. struct clk *pixel_clk[CSI2RX_STREAMS_MAX];
  71. struct reset_control *sys_rst;
  72. struct reset_control *p_rst;
  73. struct reset_control *pixel_rst[CSI2RX_STREAMS_MAX];
  74. struct phy *dphy;
  75. u8 lanes[CSI2RX_LANES_MAX];
  76. u8 num_lanes;
  77. u8 max_lanes;
  78. u8 max_streams;
  79. bool has_internal_dphy;
  80. struct v4l2_subdev subdev;
  81. struct v4l2_async_notifier notifier;
  82. struct media_pad pads[CSI2RX_PAD_MAX];
  83. /* Remote source */
  84. struct v4l2_subdev *source_subdev;
  85. int source_pad;
  86. };
  87. static const struct csi2rx_fmt formats[] = {
  88. { .code = MEDIA_BUS_FMT_YUYV8_1X16, .bpp = 16, },
  89. { .code = MEDIA_BUS_FMT_UYVY8_1X16, .bpp = 16, },
  90. { .code = MEDIA_BUS_FMT_YVYU8_1X16, .bpp = 16, },
  91. { .code = MEDIA_BUS_FMT_VYUY8_1X16, .bpp = 16, },
  92. { .code = MEDIA_BUS_FMT_SBGGR8_1X8, .bpp = 8, },
  93. { .code = MEDIA_BUS_FMT_SGBRG8_1X8, .bpp = 8, },
  94. { .code = MEDIA_BUS_FMT_SGRBG8_1X8, .bpp = 8, },
  95. { .code = MEDIA_BUS_FMT_SRGGB8_1X8, .bpp = 8, },
  96. { .code = MEDIA_BUS_FMT_Y8_1X8, .bpp = 8, },
  97. { .code = MEDIA_BUS_FMT_SBGGR10_1X10, .bpp = 10, },
  98. { .code = MEDIA_BUS_FMT_SGBRG10_1X10, .bpp = 10, },
  99. { .code = MEDIA_BUS_FMT_SGRBG10_1X10, .bpp = 10, },
  100. { .code = MEDIA_BUS_FMT_SRGGB10_1X10, .bpp = 10, },
  101. { .code = MEDIA_BUS_FMT_RGB565_1X16, .bpp = 16, },
  102. { .code = MEDIA_BUS_FMT_RGB888_1X24, .bpp = 24, },
  103. { .code = MEDIA_BUS_FMT_BGR888_1X24, .bpp = 24, },
  104. };
  105. static const struct csi2rx_fmt *csi2rx_get_fmt_by_code(u32 code)
  106. {
  107. unsigned int i;
  108. for (i = 0; i < ARRAY_SIZE(formats); i++)
  109. if (formats[i].code == code)
  110. return &formats[i];
  111. return NULL;
  112. }
  113. static inline
  114. struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev)
  115. {
  116. return container_of(subdev, struct csi2rx_priv, subdev);
  117. }
  118. static void csi2rx_reset(struct csi2rx_priv *csi2rx)
  119. {
  120. unsigned int i;
  121. /* Reset module */
  122. writel(CSI2RX_SOFT_RESET_PROTOCOL | CSI2RX_SOFT_RESET_FRONT,
  123. csi2rx->base + CSI2RX_SOFT_RESET_REG);
  124. /* Reset individual streams. */
  125. for (i = 0; i < csi2rx->max_streams; i++) {
  126. writel(CSI2RX_STREAM_CTRL_SOFT_RST,
  127. csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
  128. }
  129. usleep_range(10, 20);
  130. /* Clear resets */
  131. writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG);
  132. for (i = 0; i < csi2rx->max_streams; i++)
  133. writel(0, csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
  134. }
  135. static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
  136. {
  137. union phy_configure_opts opts = { };
  138. struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
  139. struct v4l2_subdev_format sd_fmt = {
  140. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  141. .pad = CSI2RX_PAD_SINK,
  142. };
  143. const struct csi2rx_fmt *fmt;
  144. s64 link_freq;
  145. int ret;
  146. ret = v4l2_subdev_call_state_active(&csi2rx->subdev, pad, get_fmt,
  147. &sd_fmt);
  148. if (ret < 0)
  149. return ret;
  150. fmt = csi2rx_get_fmt_by_code(sd_fmt.format.code);
  151. link_freq = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
  152. fmt->bpp, 2 * csi2rx->num_lanes);
  153. if (link_freq < 0)
  154. return link_freq;
  155. ret = phy_mipi_dphy_get_default_config_for_hsclk(link_freq,
  156. csi2rx->num_lanes, cfg);
  157. if (ret)
  158. return ret;
  159. ret = phy_power_on(csi2rx->dphy);
  160. if (ret)
  161. return ret;
  162. ret = phy_configure(csi2rx->dphy, &opts);
  163. if (ret) {
  164. phy_power_off(csi2rx->dphy);
  165. return ret;
  166. }
  167. return 0;
  168. }
  169. static int csi2rx_start(struct csi2rx_priv *csi2rx)
  170. {
  171. unsigned int i;
  172. unsigned long lanes_used = 0;
  173. u32 reg;
  174. int ret;
  175. ret = clk_prepare_enable(csi2rx->p_clk);
  176. if (ret)
  177. return ret;
  178. reset_control_deassert(csi2rx->p_rst);
  179. csi2rx_reset(csi2rx);
  180. reg = csi2rx->num_lanes << 8;
  181. for (i = 0; i < csi2rx->num_lanes; i++) {
  182. reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, csi2rx->lanes[i]);
  183. set_bit(csi2rx->lanes[i], &lanes_used);
  184. }
  185. /*
  186. * Even the unused lanes need to be mapped. In order to avoid
  187. * to map twice to the same physical lane, keep the lanes used
  188. * in the previous loop, and only map unused physical lanes to
  189. * the rest of our logical lanes.
  190. */
  191. for (i = csi2rx->num_lanes; i < csi2rx->max_lanes; i++) {
  192. unsigned int idx = find_first_zero_bit(&lanes_used,
  193. csi2rx->max_lanes);
  194. set_bit(idx, &lanes_used);
  195. reg |= CSI2RX_STATIC_CFG_DLANE_MAP(i, i + 1);
  196. }
  197. writel(reg, csi2rx->base + CSI2RX_STATIC_CFG_REG);
  198. /* Enable DPHY clk and data lanes. */
  199. if (csi2rx->dphy) {
  200. reg = CSI2RX_DPHY_CL_EN | CSI2RX_DPHY_CL_RST;
  201. for (i = 0; i < csi2rx->num_lanes; i++) {
  202. reg |= CSI2RX_DPHY_DL_EN(csi2rx->lanes[i] - 1);
  203. reg |= CSI2RX_DPHY_DL_RST(csi2rx->lanes[i] - 1);
  204. }
  205. writel(reg, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
  206. ret = csi2rx_configure_ext_dphy(csi2rx);
  207. if (ret) {
  208. dev_err(csi2rx->dev,
  209. "Failed to configure external DPHY: %d\n", ret);
  210. goto err_disable_pclk;
  211. }
  212. }
  213. /*
  214. * Create a static mapping between the CSI virtual channels
  215. * and the output stream.
  216. *
  217. * This should be enhanced, but v4l2 lacks the support for
  218. * changing that mapping dynamically.
  219. *
  220. * We also cannot enable and disable independent streams here,
  221. * hence the reference counting.
  222. */
  223. for (i = 0; i < csi2rx->max_streams; i++) {
  224. ret = clk_prepare_enable(csi2rx->pixel_clk[i]);
  225. if (ret)
  226. goto err_disable_pixclk;
  227. reset_control_deassert(csi2rx->pixel_rst[i]);
  228. writel(CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF,
  229. csi2rx->base + CSI2RX_STREAM_CFG_REG(i));
  230. /*
  231. * Enable one virtual channel. When multiple virtual channels
  232. * are supported this will have to be changed.
  233. */
  234. writel(CSI2RX_STREAM_DATA_CFG_VC_SELECT(0),
  235. csi2rx->base + CSI2RX_STREAM_DATA_CFG_REG(i));
  236. writel(CSI2RX_STREAM_CTRL_START,
  237. csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
  238. }
  239. ret = clk_prepare_enable(csi2rx->sys_clk);
  240. if (ret)
  241. goto err_disable_pixclk;
  242. reset_control_deassert(csi2rx->sys_rst);
  243. ret = v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, true);
  244. if (ret)
  245. goto err_disable_sysclk;
  246. clk_disable_unprepare(csi2rx->p_clk);
  247. return 0;
  248. err_disable_sysclk:
  249. clk_disable_unprepare(csi2rx->sys_clk);
  250. err_disable_pixclk:
  251. for (; i > 0; i--) {
  252. reset_control_assert(csi2rx->pixel_rst[i - 1]);
  253. clk_disable_unprepare(csi2rx->pixel_clk[i - 1]);
  254. }
  255. if (csi2rx->dphy) {
  256. writel(0, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
  257. phy_power_off(csi2rx->dphy);
  258. }
  259. err_disable_pclk:
  260. clk_disable_unprepare(csi2rx->p_clk);
  261. return ret;
  262. }
  263. static void csi2rx_stop(struct csi2rx_priv *csi2rx)
  264. {
  265. unsigned int i;
  266. u32 val;
  267. int ret;
  268. clk_prepare_enable(csi2rx->p_clk);
  269. reset_control_assert(csi2rx->sys_rst);
  270. clk_disable_unprepare(csi2rx->sys_clk);
  271. for (i = 0; i < csi2rx->max_streams; i++) {
  272. writel(CSI2RX_STREAM_CTRL_STOP,
  273. csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
  274. ret = readl_relaxed_poll_timeout(csi2rx->base +
  275. CSI2RX_STREAM_STATUS_REG(i),
  276. val,
  277. !(val & CSI2RX_STREAM_STATUS_RDY),
  278. 10, 10000);
  279. if (ret)
  280. dev_warn(csi2rx->dev,
  281. "Failed to stop streaming on pad%u\n", i);
  282. reset_control_assert(csi2rx->pixel_rst[i]);
  283. clk_disable_unprepare(csi2rx->pixel_clk[i]);
  284. }
  285. reset_control_assert(csi2rx->p_rst);
  286. clk_disable_unprepare(csi2rx->p_clk);
  287. if (v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, false))
  288. dev_warn(csi2rx->dev, "Couldn't disable our subdev\n");
  289. if (csi2rx->dphy) {
  290. writel(0, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
  291. if (phy_power_off(csi2rx->dphy))
  292. dev_warn(csi2rx->dev, "Couldn't power off DPHY\n");
  293. }
  294. }
  295. static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable)
  296. {
  297. struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
  298. int ret = 0;
  299. mutex_lock(&csi2rx->lock);
  300. if (enable) {
  301. /*
  302. * If we're not the first users, there's no need to
  303. * enable the whole controller.
  304. */
  305. if (!csi2rx->count) {
  306. ret = csi2rx_start(csi2rx);
  307. if (ret)
  308. goto out;
  309. }
  310. csi2rx->count++;
  311. } else {
  312. csi2rx->count--;
  313. /*
  314. * Let the last user turn off the lights.
  315. */
  316. if (!csi2rx->count)
  317. csi2rx_stop(csi2rx);
  318. }
  319. out:
  320. mutex_unlock(&csi2rx->lock);
  321. return ret;
  322. }
  323. static int csi2rx_enum_mbus_code(struct v4l2_subdev *subdev,
  324. struct v4l2_subdev_state *state,
  325. struct v4l2_subdev_mbus_code_enum *code_enum)
  326. {
  327. if (code_enum->index >= ARRAY_SIZE(formats))
  328. return -EINVAL;
  329. code_enum->code = formats[code_enum->index].code;
  330. return 0;
  331. }
  332. static int csi2rx_set_fmt(struct v4l2_subdev *subdev,
  333. struct v4l2_subdev_state *state,
  334. struct v4l2_subdev_format *format)
  335. {
  336. struct v4l2_mbus_framefmt *fmt;
  337. unsigned int i;
  338. /* No transcoding, source and sink formats must match. */
  339. if (format->pad != CSI2RX_PAD_SINK)
  340. return v4l2_subdev_get_fmt(subdev, state, format);
  341. if (!csi2rx_get_fmt_by_code(format->format.code))
  342. format->format.code = formats[0].code;
  343. format->format.field = V4L2_FIELD_NONE;
  344. /* Set sink format */
  345. fmt = v4l2_subdev_state_get_format(state, format->pad);
  346. *fmt = format->format;
  347. /* Propagate to source formats */
  348. for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++) {
  349. fmt = v4l2_subdev_state_get_format(state, i);
  350. *fmt = format->format;
  351. }
  352. return 0;
  353. }
  354. static int csi2rx_init_state(struct v4l2_subdev *subdev,
  355. struct v4l2_subdev_state *state)
  356. {
  357. struct v4l2_subdev_format format = {
  358. .pad = CSI2RX_PAD_SINK,
  359. .format = {
  360. .width = 640,
  361. .height = 480,
  362. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  363. .field = V4L2_FIELD_NONE,
  364. .colorspace = V4L2_COLORSPACE_SRGB,
  365. .ycbcr_enc = V4L2_YCBCR_ENC_601,
  366. .quantization = V4L2_QUANTIZATION_LIM_RANGE,
  367. .xfer_func = V4L2_XFER_FUNC_SRGB,
  368. },
  369. };
  370. return csi2rx_set_fmt(subdev, state, &format);
  371. }
  372. static const struct v4l2_subdev_pad_ops csi2rx_pad_ops = {
  373. .enum_mbus_code = csi2rx_enum_mbus_code,
  374. .get_fmt = v4l2_subdev_get_fmt,
  375. .set_fmt = csi2rx_set_fmt,
  376. };
  377. static const struct v4l2_subdev_video_ops csi2rx_video_ops = {
  378. .s_stream = csi2rx_s_stream,
  379. };
  380. static const struct v4l2_subdev_ops csi2rx_subdev_ops = {
  381. .video = &csi2rx_video_ops,
  382. .pad = &csi2rx_pad_ops,
  383. };
  384. static const struct v4l2_subdev_internal_ops csi2rx_internal_ops = {
  385. .init_state = csi2rx_init_state,
  386. };
  387. static const struct media_entity_operations csi2rx_media_ops = {
  388. .link_validate = v4l2_subdev_link_validate,
  389. };
  390. static int csi2rx_async_bound(struct v4l2_async_notifier *notifier,
  391. struct v4l2_subdev *s_subdev,
  392. struct v4l2_async_connection *asd)
  393. {
  394. struct v4l2_subdev *subdev = notifier->sd;
  395. struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
  396. csi2rx->source_pad = media_entity_get_fwnode_pad(&s_subdev->entity,
  397. asd->match.fwnode,
  398. MEDIA_PAD_FL_SOURCE);
  399. if (csi2rx->source_pad < 0) {
  400. dev_err(csi2rx->dev, "Couldn't find output pad for subdev %s\n",
  401. s_subdev->name);
  402. return csi2rx->source_pad;
  403. }
  404. csi2rx->source_subdev = s_subdev;
  405. dev_dbg(csi2rx->dev, "Bound %s pad: %d\n", s_subdev->name,
  406. csi2rx->source_pad);
  407. return media_create_pad_link(&csi2rx->source_subdev->entity,
  408. csi2rx->source_pad,
  409. &csi2rx->subdev.entity, 0,
  410. MEDIA_LNK_FL_ENABLED |
  411. MEDIA_LNK_FL_IMMUTABLE);
  412. }
  413. static const struct v4l2_async_notifier_operations csi2rx_notifier_ops = {
  414. .bound = csi2rx_async_bound,
  415. };
  416. static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
  417. struct platform_device *pdev)
  418. {
  419. unsigned char i;
  420. u32 dev_cfg;
  421. int ret;
  422. csi2rx->base = devm_platform_ioremap_resource(pdev, 0);
  423. if (IS_ERR(csi2rx->base))
  424. return PTR_ERR(csi2rx->base);
  425. csi2rx->sys_clk = devm_clk_get(&pdev->dev, "sys_clk");
  426. if (IS_ERR(csi2rx->sys_clk)) {
  427. dev_err(&pdev->dev, "Couldn't get sys clock\n");
  428. return PTR_ERR(csi2rx->sys_clk);
  429. }
  430. csi2rx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
  431. if (IS_ERR(csi2rx->p_clk)) {
  432. dev_err(&pdev->dev, "Couldn't get P clock\n");
  433. return PTR_ERR(csi2rx->p_clk);
  434. }
  435. csi2rx->sys_rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
  436. "sys");
  437. if (IS_ERR(csi2rx->sys_rst))
  438. return PTR_ERR(csi2rx->sys_rst);
  439. csi2rx->p_rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
  440. "reg_bank");
  441. if (IS_ERR(csi2rx->p_rst))
  442. return PTR_ERR(csi2rx->p_rst);
  443. csi2rx->dphy = devm_phy_optional_get(&pdev->dev, "dphy");
  444. if (IS_ERR(csi2rx->dphy)) {
  445. dev_err(&pdev->dev, "Couldn't get external D-PHY\n");
  446. return PTR_ERR(csi2rx->dphy);
  447. }
  448. ret = clk_prepare_enable(csi2rx->p_clk);
  449. if (ret) {
  450. dev_err(&pdev->dev, "Couldn't prepare and enable P clock\n");
  451. return ret;
  452. }
  453. dev_cfg = readl(csi2rx->base + CSI2RX_DEVICE_CFG_REG);
  454. clk_disable_unprepare(csi2rx->p_clk);
  455. csi2rx->max_lanes = dev_cfg & 7;
  456. if (csi2rx->max_lanes > CSI2RX_LANES_MAX) {
  457. dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
  458. csi2rx->max_lanes);
  459. return -EINVAL;
  460. }
  461. csi2rx->max_streams = (dev_cfg >> 4) & 7;
  462. if (csi2rx->max_streams > CSI2RX_STREAMS_MAX) {
  463. dev_err(&pdev->dev, "Invalid number of streams: %u\n",
  464. csi2rx->max_streams);
  465. return -EINVAL;
  466. }
  467. csi2rx->has_internal_dphy = dev_cfg & BIT(3) ? true : false;
  468. /*
  469. * FIXME: Once we'll have internal D-PHY support, the check
  470. * will need to be removed.
  471. */
  472. if (!csi2rx->dphy && csi2rx->has_internal_dphy) {
  473. dev_err(&pdev->dev, "Internal D-PHY not supported yet\n");
  474. return -EINVAL;
  475. }
  476. for (i = 0; i < csi2rx->max_streams; i++) {
  477. char name[16];
  478. snprintf(name, sizeof(name), "pixel_if%u_clk", i);
  479. csi2rx->pixel_clk[i] = devm_clk_get(&pdev->dev, name);
  480. if (IS_ERR(csi2rx->pixel_clk[i])) {
  481. dev_err(&pdev->dev, "Couldn't get clock %s\n", name);
  482. return PTR_ERR(csi2rx->pixel_clk[i]);
  483. }
  484. snprintf(name, sizeof(name), "pixel_if%u", i);
  485. csi2rx->pixel_rst[i] =
  486. devm_reset_control_get_optional_exclusive(&pdev->dev,
  487. name);
  488. if (IS_ERR(csi2rx->pixel_rst[i]))
  489. return PTR_ERR(csi2rx->pixel_rst[i]);
  490. }
  491. return 0;
  492. }
  493. static int csi2rx_parse_dt(struct csi2rx_priv *csi2rx)
  494. {
  495. struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
  496. struct v4l2_async_connection *asd;
  497. struct fwnode_handle *fwh;
  498. struct device_node *ep;
  499. int ret;
  500. ep = of_graph_get_endpoint_by_regs(csi2rx->dev->of_node, 0, 0);
  501. if (!ep)
  502. return -EINVAL;
  503. fwh = of_fwnode_handle(ep);
  504. ret = v4l2_fwnode_endpoint_parse(fwh, &v4l2_ep);
  505. if (ret) {
  506. dev_err(csi2rx->dev, "Could not parse v4l2 endpoint\n");
  507. of_node_put(ep);
  508. return ret;
  509. }
  510. if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
  511. dev_err(csi2rx->dev, "Unsupported media bus type: 0x%x\n",
  512. v4l2_ep.bus_type);
  513. of_node_put(ep);
  514. return -EINVAL;
  515. }
  516. memcpy(csi2rx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
  517. sizeof(csi2rx->lanes));
  518. csi2rx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
  519. if (csi2rx->num_lanes > csi2rx->max_lanes) {
  520. dev_err(csi2rx->dev, "Unsupported number of data-lanes: %d\n",
  521. csi2rx->num_lanes);
  522. of_node_put(ep);
  523. return -EINVAL;
  524. }
  525. v4l2_async_subdev_nf_init(&csi2rx->notifier, &csi2rx->subdev);
  526. asd = v4l2_async_nf_add_fwnode_remote(&csi2rx->notifier, fwh,
  527. struct v4l2_async_connection);
  528. of_node_put(ep);
  529. if (IS_ERR(asd)) {
  530. v4l2_async_nf_cleanup(&csi2rx->notifier);
  531. return PTR_ERR(asd);
  532. }
  533. csi2rx->notifier.ops = &csi2rx_notifier_ops;
  534. ret = v4l2_async_nf_register(&csi2rx->notifier);
  535. if (ret)
  536. v4l2_async_nf_cleanup(&csi2rx->notifier);
  537. return ret;
  538. }
  539. static int csi2rx_probe(struct platform_device *pdev)
  540. {
  541. struct csi2rx_priv *csi2rx;
  542. unsigned int i;
  543. int ret;
  544. csi2rx = kzalloc(sizeof(*csi2rx), GFP_KERNEL);
  545. if (!csi2rx)
  546. return -ENOMEM;
  547. platform_set_drvdata(pdev, csi2rx);
  548. csi2rx->dev = &pdev->dev;
  549. mutex_init(&csi2rx->lock);
  550. ret = csi2rx_get_resources(csi2rx, pdev);
  551. if (ret)
  552. goto err_free_priv;
  553. ret = csi2rx_parse_dt(csi2rx);
  554. if (ret)
  555. goto err_free_priv;
  556. csi2rx->subdev.owner = THIS_MODULE;
  557. csi2rx->subdev.dev = &pdev->dev;
  558. v4l2_subdev_init(&csi2rx->subdev, &csi2rx_subdev_ops);
  559. csi2rx->subdev.internal_ops = &csi2rx_internal_ops;
  560. v4l2_set_subdevdata(&csi2rx->subdev, &pdev->dev);
  561. snprintf(csi2rx->subdev.name, sizeof(csi2rx->subdev.name),
  562. "%s.%s", KBUILD_MODNAME, dev_name(&pdev->dev));
  563. /* Create our media pads */
  564. csi2rx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  565. csi2rx->pads[CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  566. for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++)
  567. csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE;
  568. csi2rx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  569. csi2rx->subdev.entity.ops = &csi2rx_media_ops;
  570. ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX,
  571. csi2rx->pads);
  572. if (ret)
  573. goto err_cleanup;
  574. ret = v4l2_subdev_init_finalize(&csi2rx->subdev);
  575. if (ret)
  576. goto err_cleanup;
  577. ret = v4l2_async_register_subdev(&csi2rx->subdev);
  578. if (ret < 0)
  579. goto err_free_state;
  580. dev_info(&pdev->dev,
  581. "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
  582. csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams,
  583. csi2rx->dphy ? "external" :
  584. csi2rx->has_internal_dphy ? "internal" : "no");
  585. return 0;
  586. err_free_state:
  587. v4l2_subdev_cleanup(&csi2rx->subdev);
  588. err_cleanup:
  589. v4l2_async_nf_unregister(&csi2rx->notifier);
  590. v4l2_async_nf_cleanup(&csi2rx->notifier);
  591. media_entity_cleanup(&csi2rx->subdev.entity);
  592. err_free_priv:
  593. kfree(csi2rx);
  594. return ret;
  595. }
  596. static void csi2rx_remove(struct platform_device *pdev)
  597. {
  598. struct csi2rx_priv *csi2rx = platform_get_drvdata(pdev);
  599. v4l2_async_nf_unregister(&csi2rx->notifier);
  600. v4l2_async_nf_cleanup(&csi2rx->notifier);
  601. v4l2_async_unregister_subdev(&csi2rx->subdev);
  602. v4l2_subdev_cleanup(&csi2rx->subdev);
  603. media_entity_cleanup(&csi2rx->subdev.entity);
  604. kfree(csi2rx);
  605. }
  606. static const struct of_device_id csi2rx_of_table[] = {
  607. { .compatible = "starfive,jh7110-csi2rx" },
  608. { .compatible = "cdns,csi2rx" },
  609. { },
  610. };
  611. MODULE_DEVICE_TABLE(of, csi2rx_of_table);
  612. static struct platform_driver csi2rx_driver = {
  613. .probe = csi2rx_probe,
  614. .remove_new = csi2rx_remove,
  615. .driver = {
  616. .name = "cdns-csi2rx",
  617. .of_match_table = csi2rx_of_table,
  618. },
  619. };
  620. module_platform_driver(csi2rx_driver);
  621. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
  622. MODULE_DESCRIPTION("Cadence CSI2-RX controller");
  623. MODULE_LICENSE("GPL");