cdns-csi2tx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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_get_try_format(subdev, sd_state,
  147. fmt->pad);
  148. return &csi2tx->pad_fmts[fmt->pad];
  149. }
  150. static int csi2tx_get_pad_format(struct v4l2_subdev *subdev,
  151. struct v4l2_subdev_state *sd_state,
  152. struct v4l2_subdev_format *fmt)
  153. {
  154. const struct v4l2_mbus_framefmt *format;
  155. /* Multiplexed pad? */
  156. if (fmt->pad == CSI2TX_PAD_SOURCE)
  157. return -EINVAL;
  158. format = __csi2tx_get_pad_format(subdev, sd_state, fmt);
  159. if (!format)
  160. return -EINVAL;
  161. fmt->format = *format;
  162. return 0;
  163. }
  164. static int csi2tx_set_pad_format(struct v4l2_subdev *subdev,
  165. struct v4l2_subdev_state *sd_state,
  166. struct v4l2_subdev_format *fmt)
  167. {
  168. const struct v4l2_mbus_framefmt *src_format = &fmt->format;
  169. struct v4l2_mbus_framefmt *dst_format;
  170. /* Multiplexed pad? */
  171. if (fmt->pad == CSI2TX_PAD_SOURCE)
  172. return -EINVAL;
  173. if (!csi2tx_get_fmt_from_mbus(fmt->format.code))
  174. src_format = &fmt_default;
  175. dst_format = __csi2tx_get_pad_format(subdev, sd_state, fmt);
  176. if (!dst_format)
  177. return -EINVAL;
  178. *dst_format = *src_format;
  179. return 0;
  180. }
  181. static const struct v4l2_subdev_pad_ops csi2tx_pad_ops = {
  182. .enum_mbus_code = csi2tx_enum_mbus_code,
  183. .get_fmt = csi2tx_get_pad_format,
  184. .set_fmt = csi2tx_set_pad_format,
  185. };
  186. /* Set Wake Up value in the D-PHY */
  187. static void csi2tx_dphy_set_wakeup(struct csi2tx_priv *csi2tx)
  188. {
  189. writel(CSI2TX_DPHY_CLK_WAKEUP_ULPS_CYCLES(32),
  190. csi2tx->base + CSI2TX_DPHY_CLK_WAKEUP_REG);
  191. }
  192. /*
  193. * Finishes the D-PHY initialization
  194. * reg dphy cfg value to be used
  195. */
  196. static void csi2tx_dphy_init_finish(struct csi2tx_priv *csi2tx, u32 reg)
  197. {
  198. unsigned int i;
  199. udelay(10);
  200. /* Enable our (clock and data) lanes */
  201. reg |= CSI2TX_DPHY_CFG_CLK_ENABLE;
  202. for (i = 0; i < csi2tx->num_lanes; i++)
  203. reg |= CSI2TX_DPHY_CFG_LANE_ENABLE(csi2tx->lanes[i] - 1);
  204. writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
  205. udelay(10);
  206. /* Switch to HS mode */
  207. reg &= ~CSI2TX_DPHY_CFG_MODE_MASK;
  208. writel(reg | CSI2TX_DPHY_CFG_MODE_HS,
  209. csi2tx->base + CSI2TX_DPHY_CFG_REG);
  210. }
  211. /* Configures D-PHY in CSIv1.3 */
  212. static void csi2tx_dphy_setup(struct csi2tx_priv *csi2tx)
  213. {
  214. u32 reg;
  215. unsigned int i;
  216. csi2tx_dphy_set_wakeup(csi2tx);
  217. /* Put our lanes (clock and data) out of reset */
  218. reg = CSI2TX_DPHY_CFG_CLK_RESET | CSI2TX_DPHY_CFG_MODE_LPDT;
  219. for (i = 0; i < csi2tx->num_lanes; i++)
  220. reg |= CSI2TX_DPHY_CFG_LANE_RESET(csi2tx->lanes[i] - 1);
  221. writel(reg, csi2tx->base + CSI2TX_DPHY_CFG_REG);
  222. csi2tx_dphy_init_finish(csi2tx, reg);
  223. }
  224. /* Configures D-PHY in CSIv2 */
  225. static void csi2tx_v2_dphy_setup(struct csi2tx_priv *csi2tx)
  226. {
  227. u32 reg;
  228. csi2tx_dphy_set_wakeup(csi2tx);
  229. /* Put our lanes (clock and data) out of reset */
  230. reg = CSI2TX_V2_DPHY_CFG_RESET | CSI2TX_V2_DPHY_CFG_MODE_LPDT;
  231. writel(reg, csi2tx->base + CSI2TX_V2_DPHY_CFG_REG);
  232. csi2tx_dphy_init_finish(csi2tx, reg);
  233. }
  234. static void csi2tx_reset(struct csi2tx_priv *csi2tx)
  235. {
  236. writel(CSI2TX_CONFIG_SRST_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
  237. udelay(10);
  238. }
  239. static int csi2tx_start(struct csi2tx_priv *csi2tx)
  240. {
  241. struct media_entity *entity = &csi2tx->subdev.entity;
  242. struct media_link *link;
  243. unsigned int i;
  244. csi2tx_reset(csi2tx);
  245. writel(CSI2TX_CONFIG_CFG_REQ, csi2tx->base + CSI2TX_CONFIG_REG);
  246. udelay(10);
  247. if (csi2tx->vops && csi2tx->vops->dphy_setup) {
  248. csi2tx->vops->dphy_setup(csi2tx);
  249. udelay(10);
  250. }
  251. /*
  252. * Create a static mapping between the CSI virtual channels
  253. * and the input streams.
  254. *
  255. * This should be enhanced, but v4l2 lacks the support for
  256. * changing that mapping dynamically at the moment.
  257. *
  258. * We're protected from the userspace setting up links at the
  259. * same time by the upper layer having called
  260. * media_pipeline_start().
  261. */
  262. list_for_each_entry(link, &entity->links, list) {
  263. struct v4l2_mbus_framefmt *mfmt;
  264. const struct csi2tx_fmt *fmt;
  265. unsigned int stream;
  266. int pad_idx = -1;
  267. /* Only consider our enabled input pads */
  268. for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++) {
  269. struct media_pad *pad = &csi2tx->pads[i];
  270. if ((pad == link->sink) &&
  271. (link->flags & MEDIA_LNK_FL_ENABLED)) {
  272. pad_idx = i;
  273. break;
  274. }
  275. }
  276. if (pad_idx < 0)
  277. continue;
  278. mfmt = &csi2tx->pad_fmts[pad_idx];
  279. fmt = csi2tx_get_fmt_from_mbus(mfmt->code);
  280. if (!fmt)
  281. continue;
  282. stream = pad_idx - CSI2TX_PAD_SINK_STREAM0;
  283. /*
  284. * We use the stream ID there, but it's wrong.
  285. *
  286. * A stream could very well send a data type that is
  287. * not equal to its stream ID. We need to find a
  288. * proper way to address it.
  289. */
  290. writel(CSI2TX_DT_CFG_DT(fmt->dt),
  291. csi2tx->base + CSI2TX_DT_CFG_REG(stream));
  292. writel(CSI2TX_DT_FORMAT_BYTES_PER_LINE(mfmt->width * fmt->bpp) |
  293. CSI2TX_DT_FORMAT_MAX_LINE_NUM(mfmt->height + 1),
  294. csi2tx->base + CSI2TX_DT_FORMAT_REG(stream));
  295. /*
  296. * TODO: This needs to be calculated based on the
  297. * output CSI2 clock rate.
  298. */
  299. writel(CSI2TX_STREAM_IF_CFG_FILL_LEVEL(4),
  300. csi2tx->base + CSI2TX_STREAM_IF_CFG_REG(stream));
  301. }
  302. /* Disable the configuration mode */
  303. writel(0, csi2tx->base + CSI2TX_CONFIG_REG);
  304. return 0;
  305. }
  306. static void csi2tx_stop(struct csi2tx_priv *csi2tx)
  307. {
  308. writel(CSI2TX_CONFIG_CFG_REQ | CSI2TX_CONFIG_SRST_REQ,
  309. csi2tx->base + CSI2TX_CONFIG_REG);
  310. }
  311. static int csi2tx_s_stream(struct v4l2_subdev *subdev, int enable)
  312. {
  313. struct csi2tx_priv *csi2tx = v4l2_subdev_to_csi2tx(subdev);
  314. int ret = 0;
  315. mutex_lock(&csi2tx->lock);
  316. if (enable) {
  317. /*
  318. * If we're not the first users, there's no need to
  319. * enable the whole controller.
  320. */
  321. if (!csi2tx->count) {
  322. ret = csi2tx_start(csi2tx);
  323. if (ret)
  324. goto out;
  325. }
  326. csi2tx->count++;
  327. } else {
  328. csi2tx->count--;
  329. /*
  330. * Let the last user turn off the lights.
  331. */
  332. if (!csi2tx->count)
  333. csi2tx_stop(csi2tx);
  334. }
  335. out:
  336. mutex_unlock(&csi2tx->lock);
  337. return ret;
  338. }
  339. static const struct v4l2_subdev_video_ops csi2tx_video_ops = {
  340. .s_stream = csi2tx_s_stream,
  341. };
  342. static const struct v4l2_subdev_ops csi2tx_subdev_ops = {
  343. .pad = &csi2tx_pad_ops,
  344. .video = &csi2tx_video_ops,
  345. };
  346. static int csi2tx_get_resources(struct csi2tx_priv *csi2tx,
  347. struct platform_device *pdev)
  348. {
  349. unsigned int i;
  350. u32 dev_cfg;
  351. int ret;
  352. csi2tx->base = devm_platform_ioremap_resource(pdev, 0);
  353. if (IS_ERR(csi2tx->base))
  354. return PTR_ERR(csi2tx->base);
  355. csi2tx->p_clk = devm_clk_get(&pdev->dev, "p_clk");
  356. if (IS_ERR(csi2tx->p_clk)) {
  357. dev_err(&pdev->dev, "Couldn't get p_clk\n");
  358. return PTR_ERR(csi2tx->p_clk);
  359. }
  360. csi2tx->esc_clk = devm_clk_get(&pdev->dev, "esc_clk");
  361. if (IS_ERR(csi2tx->esc_clk)) {
  362. dev_err(&pdev->dev, "Couldn't get the esc_clk\n");
  363. return PTR_ERR(csi2tx->esc_clk);
  364. }
  365. ret = clk_prepare_enable(csi2tx->p_clk);
  366. if (ret) {
  367. dev_err(&pdev->dev, "Couldn't prepare and enable p_clk\n");
  368. return ret;
  369. }
  370. dev_cfg = readl(csi2tx->base + CSI2TX_DEVICE_CONFIG_REG);
  371. clk_disable_unprepare(csi2tx->p_clk);
  372. csi2tx->max_lanes = dev_cfg & CSI2TX_DEVICE_CONFIG_LANES_MASK;
  373. if (csi2tx->max_lanes > CSI2TX_LANES_MAX) {
  374. dev_err(&pdev->dev, "Invalid number of lanes: %u\n",
  375. csi2tx->max_lanes);
  376. return -EINVAL;
  377. }
  378. csi2tx->max_streams = (dev_cfg & CSI2TX_DEVICE_CONFIG_STREAMS_MASK) >> 4;
  379. if (csi2tx->max_streams > CSI2TX_STREAMS_MAX) {
  380. dev_err(&pdev->dev, "Invalid number of streams: %u\n",
  381. csi2tx->max_streams);
  382. return -EINVAL;
  383. }
  384. csi2tx->has_internal_dphy = !!(dev_cfg & CSI2TX_DEVICE_CONFIG_HAS_DPHY);
  385. for (i = 0; i < csi2tx->max_streams; i++) {
  386. char clk_name[16];
  387. snprintf(clk_name, sizeof(clk_name), "pixel_if%u_clk", i);
  388. csi2tx->pixel_clk[i] = devm_clk_get(&pdev->dev, clk_name);
  389. if (IS_ERR(csi2tx->pixel_clk[i])) {
  390. dev_err(&pdev->dev, "Couldn't get clock %s\n",
  391. clk_name);
  392. return PTR_ERR(csi2tx->pixel_clk[i]);
  393. }
  394. }
  395. return 0;
  396. }
  397. static int csi2tx_check_lanes(struct csi2tx_priv *csi2tx)
  398. {
  399. struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
  400. struct device_node *ep;
  401. int ret, i;
  402. ep = of_graph_get_endpoint_by_regs(csi2tx->dev->of_node, 0, 0);
  403. if (!ep)
  404. return -EINVAL;
  405. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &v4l2_ep);
  406. if (ret) {
  407. dev_err(csi2tx->dev, "Could not parse v4l2 endpoint\n");
  408. goto out;
  409. }
  410. if (v4l2_ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
  411. dev_err(csi2tx->dev, "Unsupported media bus type: 0x%x\n",
  412. v4l2_ep.bus_type);
  413. ret = -EINVAL;
  414. goto out;
  415. }
  416. csi2tx->num_lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
  417. if (csi2tx->num_lanes > csi2tx->max_lanes) {
  418. dev_err(csi2tx->dev,
  419. "Current configuration uses more lanes than supported\n");
  420. ret = -EINVAL;
  421. goto out;
  422. }
  423. for (i = 0; i < csi2tx->num_lanes; i++) {
  424. if (v4l2_ep.bus.mipi_csi2.data_lanes[i] < 1) {
  425. dev_err(csi2tx->dev, "Invalid lane[%d] number: %u\n",
  426. i, v4l2_ep.bus.mipi_csi2.data_lanes[i]);
  427. ret = -EINVAL;
  428. goto out;
  429. }
  430. }
  431. memcpy(csi2tx->lanes, v4l2_ep.bus.mipi_csi2.data_lanes,
  432. sizeof(csi2tx->lanes));
  433. out:
  434. of_node_put(ep);
  435. return ret;
  436. }
  437. static const struct csi2tx_vops csi2tx_vops = {
  438. .dphy_setup = csi2tx_dphy_setup,
  439. };
  440. static const struct csi2tx_vops csi2tx_v2_vops = {
  441. .dphy_setup = csi2tx_v2_dphy_setup,
  442. };
  443. static const struct of_device_id csi2tx_of_table[] = {
  444. {
  445. .compatible = "cdns,csi2tx",
  446. .data = &csi2tx_vops
  447. },
  448. {
  449. .compatible = "cdns,csi2tx-1.3",
  450. .data = &csi2tx_vops
  451. },
  452. {
  453. .compatible = "cdns,csi2tx-2.1",
  454. .data = &csi2tx_v2_vops
  455. },
  456. { }
  457. };
  458. MODULE_DEVICE_TABLE(of, csi2tx_of_table);
  459. static int csi2tx_probe(struct platform_device *pdev)
  460. {
  461. struct csi2tx_priv *csi2tx;
  462. const struct of_device_id *of_id;
  463. unsigned int i;
  464. int ret;
  465. csi2tx = kzalloc(sizeof(*csi2tx), GFP_KERNEL);
  466. if (!csi2tx)
  467. return -ENOMEM;
  468. platform_set_drvdata(pdev, csi2tx);
  469. mutex_init(&csi2tx->lock);
  470. csi2tx->dev = &pdev->dev;
  471. ret = csi2tx_get_resources(csi2tx, pdev);
  472. if (ret)
  473. goto err_free_priv;
  474. of_id = of_match_node(csi2tx_of_table, pdev->dev.of_node);
  475. csi2tx->vops = (struct csi2tx_vops *)of_id->data;
  476. v4l2_subdev_init(&csi2tx->subdev, &csi2tx_subdev_ops);
  477. csi2tx->subdev.owner = THIS_MODULE;
  478. csi2tx->subdev.dev = &pdev->dev;
  479. csi2tx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  480. snprintf(csi2tx->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s.%s",
  481. KBUILD_MODNAME, dev_name(&pdev->dev));
  482. ret = csi2tx_check_lanes(csi2tx);
  483. if (ret)
  484. goto err_free_priv;
  485. /* Create our media pads */
  486. csi2tx->subdev.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  487. csi2tx->pads[CSI2TX_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  488. for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
  489. csi2tx->pads[i].flags = MEDIA_PAD_FL_SINK;
  490. /*
  491. * Only the input pads are considered to have a format at the
  492. * moment. The CSI link can multiplex various streams with
  493. * different formats, and we can't expose this in v4l2 right
  494. * now.
  495. */
  496. for (i = CSI2TX_PAD_SINK_STREAM0; i < CSI2TX_PAD_MAX; i++)
  497. csi2tx->pad_fmts[i] = fmt_default;
  498. ret = media_entity_pads_init(&csi2tx->subdev.entity, CSI2TX_PAD_MAX,
  499. csi2tx->pads);
  500. if (ret)
  501. goto err_free_priv;
  502. ret = v4l2_async_register_subdev(&csi2tx->subdev);
  503. if (ret < 0)
  504. goto err_free_priv;
  505. dev_info(&pdev->dev,
  506. "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n",
  507. csi2tx->num_lanes, csi2tx->max_lanes, csi2tx->max_streams,
  508. csi2tx->has_internal_dphy ? "internal" : "no");
  509. return 0;
  510. err_free_priv:
  511. kfree(csi2tx);
  512. return ret;
  513. }
  514. static int csi2tx_remove(struct platform_device *pdev)
  515. {
  516. struct csi2tx_priv *csi2tx = platform_get_drvdata(pdev);
  517. v4l2_async_unregister_subdev(&csi2tx->subdev);
  518. kfree(csi2tx);
  519. return 0;
  520. }
  521. static struct platform_driver csi2tx_driver = {
  522. .probe = csi2tx_probe,
  523. .remove = csi2tx_remove,
  524. .driver = {
  525. .name = "cdns-csi2tx",
  526. .of_match_table = csi2tx_of_table,
  527. },
  528. };
  529. module_platform_driver(csi2tx_driver);
  530. MODULE_AUTHOR("Maxime Ripard <[email protected]>");
  531. MODULE_DESCRIPTION("Cadence CSI2-TX controller");
  532. MODULE_LICENSE("GPL");