cdns-csi2tx.c 16 KB

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