microchip-csi2dc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Microchip CSI2 Demux Controller (CSI2DC) driver
  4. *
  5. * Copyright (C) 2018 Microchip Technology, Inc.
  6. *
  7. * Author: Eugen Hristev <eugen.hristev@microchip.com>
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/videodev2.h>
  17. #include <media/v4l2-fwnode.h>
  18. #include <media/v4l2-subdev.h>
  19. /* Global configuration register */
  20. #define CSI2DC_GCFG 0x0
  21. /* MIPI sensor pixel clock is free running */
  22. #define CSI2DC_GCFG_MIPIFRN BIT(0)
  23. /* GPIO parallel interface selection */
  24. #define CSI2DC_GCFG_GPIOSEL BIT(1)
  25. /* Output waveform inter-line minimum delay */
  26. #define CSI2DC_GCFG_HLC(v) ((v) << 4)
  27. #define CSI2DC_GCFG_HLC_MASK GENMASK(7, 4)
  28. /* SAMA7G5 requires a HLC delay of 15 */
  29. #define SAMA7G5_HLC (15)
  30. /* Global control register */
  31. #define CSI2DC_GCTLR 0x04
  32. #define CSI2DC_GCTLR_SWRST BIT(0)
  33. /* Global status register */
  34. #define CSI2DC_GS 0x08
  35. /* SSP interrupt status register */
  36. #define CSI2DC_SSPIS 0x28
  37. /* Pipe update register */
  38. #define CSI2DC_PU 0xc0
  39. /* Video pipe attributes update */
  40. #define CSI2DC_PU_VP BIT(0)
  41. /* Pipe update status register */
  42. #define CSI2DC_PUS 0xc4
  43. /* Video pipeline Interrupt Status Register */
  44. #define CSI2DC_VPISR 0xf4
  45. /* Video pipeline enable register */
  46. #define CSI2DC_VPE 0xf8
  47. #define CSI2DC_VPE_ENABLE BIT(0)
  48. /* Video pipeline configuration register */
  49. #define CSI2DC_VPCFG 0xfc
  50. /* Data type */
  51. #define CSI2DC_VPCFG_DT(v) ((v) << 0)
  52. #define CSI2DC_VPCFG_DT_MASK GENMASK(5, 0)
  53. /* Virtual channel identifier */
  54. #define CSI2DC_VPCFG_VC(v) ((v) << 6)
  55. #define CSI2DC_VPCFG_VC_MASK GENMASK(7, 6)
  56. /* Decompression enable */
  57. #define CSI2DC_VPCFG_DE BIT(8)
  58. /* Decoder mode */
  59. #define CSI2DC_VPCFG_DM(v) ((v) << 9)
  60. #define CSI2DC_VPCFG_DM_DECODER8TO12 0
  61. /* Decoder predictor 2 selection */
  62. #define CSI2DC_VPCFG_DP2 BIT(12)
  63. /* Recommended memory storage */
  64. #define CSI2DC_VPCFG_RMS BIT(13)
  65. /* Post adjustment */
  66. #define CSI2DC_VPCFG_PA BIT(14)
  67. /* Video pipeline column register */
  68. #define CSI2DC_VPCOL 0x100
  69. /* Column number */
  70. #define CSI2DC_VPCOL_COL(v) ((v) << 0)
  71. #define CSI2DC_VPCOL_COL_MASK GENMASK(15, 0)
  72. /* Video pipeline row register */
  73. #define CSI2DC_VPROW 0x104
  74. /* Row number */
  75. #define CSI2DC_VPROW_ROW(v) ((v) << 0)
  76. #define CSI2DC_VPROW_ROW_MASK GENMASK(15, 0)
  77. /* Version register */
  78. #define CSI2DC_VERSION 0x1fc
  79. /* register read/write helpers */
  80. #define csi2dc_readl(st, reg) readl_relaxed((st)->base + (reg))
  81. #define csi2dc_writel(st, reg, val) writel_relaxed((val), \
  82. (st)->base + (reg))
  83. /* supported RAW data types */
  84. #define CSI2DC_DT_RAW6 0x28
  85. #define CSI2DC_DT_RAW7 0x29
  86. #define CSI2DC_DT_RAW8 0x2a
  87. #define CSI2DC_DT_RAW10 0x2b
  88. #define CSI2DC_DT_RAW12 0x2c
  89. #define CSI2DC_DT_RAW14 0x2d
  90. /* YUV data types */
  91. #define CSI2DC_DT_YUV422_8B 0x1e
  92. /*
  93. * struct csi2dc_format - CSI2DC format type struct
  94. * @mbus_code: Media bus code for the format
  95. * @dt: Data type constant for this format
  96. */
  97. struct csi2dc_format {
  98. u32 mbus_code;
  99. u32 dt;
  100. };
  101. static const struct csi2dc_format csi2dc_formats[] = {
  102. {
  103. .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
  104. .dt = CSI2DC_DT_RAW8,
  105. }, {
  106. .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
  107. .dt = CSI2DC_DT_RAW8,
  108. }, {
  109. .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
  110. .dt = CSI2DC_DT_RAW8,
  111. }, {
  112. .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
  113. .dt = CSI2DC_DT_RAW8,
  114. }, {
  115. .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
  116. .dt = CSI2DC_DT_RAW10,
  117. }, {
  118. .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
  119. .dt = CSI2DC_DT_RAW10,
  120. }, {
  121. .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
  122. .dt = CSI2DC_DT_RAW10,
  123. }, {
  124. .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
  125. .dt = CSI2DC_DT_RAW10,
  126. }, {
  127. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  128. .dt = CSI2DC_DT_YUV422_8B,
  129. },
  130. };
  131. enum mipi_csi_pads {
  132. CSI2DC_PAD_SINK = 0,
  133. CSI2DC_PAD_SOURCE = 1,
  134. CSI2DC_PADS_NUM = 2,
  135. };
  136. /*
  137. * struct csi2dc_device - CSI2DC device driver data/config struct
  138. * @base: Register map base address
  139. * @csi2dc_sd: v4l2 subdevice for the csi2dc device
  140. * This is the subdevice that the csi2dc device itself
  141. * registers in v4l2 subsystem
  142. * @dev: struct device for this csi2dc device
  143. * @pclk: Peripheral clock reference
  144. * Input clock that clocks the hardware block internal
  145. * logic
  146. * @scck: Sensor Controller clock reference
  147. * Input clock that is used to generate the pixel clock
  148. * @format: Current saved format used in g/s fmt
  149. * @cur_fmt: Current state format
  150. * @try_fmt: Try format that is being tried
  151. * @pads: Media entity pads for the csi2dc subdevice
  152. * @clk_gated: Whether the clock is gated or free running
  153. * @video_pipe: Whether video pipeline is configured
  154. * @parallel_mode: The underlying subdevice is connected on a parallel bus
  155. * @vc: Current set virtual channel
  156. * @notifier: Async notifier that is used to bound the underlying
  157. * subdevice to the csi2dc subdevice
  158. * @input_sd: Reference to the underlying subdevice bound to the
  159. * csi2dc subdevice
  160. * @remote_pad: Pad number of the underlying subdevice that is linked
  161. * to the csi2dc subdevice sink pad.
  162. */
  163. struct csi2dc_device {
  164. void __iomem *base;
  165. struct v4l2_subdev csi2dc_sd;
  166. struct device *dev;
  167. struct clk *pclk;
  168. struct clk *scck;
  169. struct v4l2_mbus_framefmt format;
  170. const struct csi2dc_format *cur_fmt;
  171. const struct csi2dc_format *try_fmt;
  172. struct media_pad pads[CSI2DC_PADS_NUM];
  173. bool clk_gated;
  174. bool video_pipe;
  175. bool parallel_mode;
  176. u32 vc;
  177. struct v4l2_async_notifier notifier;
  178. struct v4l2_subdev *input_sd;
  179. u32 remote_pad;
  180. };
  181. static inline struct csi2dc_device *
  182. csi2dc_sd_to_csi2dc_device(struct v4l2_subdev *csi2dc_sd)
  183. {
  184. return container_of(csi2dc_sd, struct csi2dc_device, csi2dc_sd);
  185. }
  186. static int csi2dc_enum_mbus_code(struct v4l2_subdev *csi2dc_sd,
  187. struct v4l2_subdev_state *sd_state,
  188. struct v4l2_subdev_mbus_code_enum *code)
  189. {
  190. if (code->index >= ARRAY_SIZE(csi2dc_formats))
  191. return -EINVAL;
  192. code->code = csi2dc_formats[code->index].mbus_code;
  193. return 0;
  194. }
  195. static int csi2dc_get_fmt(struct v4l2_subdev *csi2dc_sd,
  196. struct v4l2_subdev_state *sd_state,
  197. struct v4l2_subdev_format *format)
  198. {
  199. struct csi2dc_device *csi2dc = csi2dc_sd_to_csi2dc_device(csi2dc_sd);
  200. struct v4l2_mbus_framefmt *v4l2_try_fmt;
  201. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  202. v4l2_try_fmt = v4l2_subdev_state_get_format(sd_state,
  203. format->pad);
  204. format->format = *v4l2_try_fmt;
  205. return 0;
  206. }
  207. format->format = csi2dc->format;
  208. return 0;
  209. }
  210. static int csi2dc_set_fmt(struct v4l2_subdev *csi2dc_sd,
  211. struct v4l2_subdev_state *sd_state,
  212. struct v4l2_subdev_format *req_fmt)
  213. {
  214. struct csi2dc_device *csi2dc = csi2dc_sd_to_csi2dc_device(csi2dc_sd);
  215. const struct csi2dc_format *fmt, *try_fmt = NULL;
  216. struct v4l2_mbus_framefmt *v4l2_try_fmt;
  217. unsigned int i;
  218. /*
  219. * Setting the source pad is disabled.
  220. * The same format is being propagated from the sink to source.
  221. */
  222. if (req_fmt->pad == CSI2DC_PAD_SOURCE)
  223. return -EINVAL;
  224. for (i = 0; i < ARRAY_SIZE(csi2dc_formats); i++) {
  225. fmt = &csi2dc_formats[i];
  226. if (req_fmt->format.code == fmt->mbus_code)
  227. try_fmt = fmt;
  228. fmt++;
  229. }
  230. /* in case we could not find the desired format, default to something */
  231. if (!try_fmt) {
  232. try_fmt = &csi2dc_formats[0];
  233. dev_dbg(csi2dc->dev,
  234. "CSI2DC unsupported format 0x%x, defaulting to 0x%x\n",
  235. req_fmt->format.code, csi2dc_formats[0].mbus_code);
  236. }
  237. req_fmt->format.code = try_fmt->mbus_code;
  238. req_fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
  239. req_fmt->format.field = V4L2_FIELD_NONE;
  240. if (req_fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  241. v4l2_try_fmt = v4l2_subdev_state_get_format(sd_state,
  242. req_fmt->pad);
  243. *v4l2_try_fmt = req_fmt->format;
  244. /* Trying on the sink pad makes the source pad change too */
  245. v4l2_try_fmt = v4l2_subdev_state_get_format(sd_state,
  246. CSI2DC_PAD_SOURCE);
  247. *v4l2_try_fmt = req_fmt->format;
  248. /* if we are just trying, we are done */
  249. return 0;
  250. }
  251. /* save the format for later requests */
  252. csi2dc->format = req_fmt->format;
  253. /* update config */
  254. csi2dc->cur_fmt = try_fmt;
  255. dev_dbg(csi2dc->dev, "new format set: 0x%x @%dx%d\n",
  256. csi2dc->format.code, csi2dc->format.width,
  257. csi2dc->format.height);
  258. return 0;
  259. }
  260. static int csi2dc_power(struct csi2dc_device *csi2dc, int on)
  261. {
  262. int ret = 0;
  263. if (on) {
  264. ret = clk_prepare_enable(csi2dc->pclk);
  265. if (ret) {
  266. dev_err(csi2dc->dev, "failed to enable pclk:%d\n", ret);
  267. return ret;
  268. }
  269. ret = clk_prepare_enable(csi2dc->scck);
  270. if (ret) {
  271. dev_err(csi2dc->dev, "failed to enable scck:%d\n", ret);
  272. clk_disable_unprepare(csi2dc->pclk);
  273. return ret;
  274. }
  275. /* if powering up, deassert reset line */
  276. csi2dc_writel(csi2dc, CSI2DC_GCTLR, CSI2DC_GCTLR_SWRST);
  277. } else {
  278. /* if powering down, assert reset line */
  279. csi2dc_writel(csi2dc, CSI2DC_GCTLR, 0);
  280. clk_disable_unprepare(csi2dc->scck);
  281. clk_disable_unprepare(csi2dc->pclk);
  282. }
  283. return ret;
  284. }
  285. static int csi2dc_get_mbus_config(struct csi2dc_device *csi2dc)
  286. {
  287. struct v4l2_mbus_config mbus_config = { 0 };
  288. int ret;
  289. ret = v4l2_subdev_call(csi2dc->input_sd, pad, get_mbus_config,
  290. csi2dc->remote_pad, &mbus_config);
  291. if (ret == -ENOIOCTLCMD) {
  292. dev_dbg(csi2dc->dev,
  293. "no remote mbus configuration available\n");
  294. return 0;
  295. }
  296. if (ret) {
  297. dev_err(csi2dc->dev,
  298. "failed to get remote mbus configuration\n");
  299. return 0;
  300. }
  301. dev_dbg(csi2dc->dev, "subdev sending on channel %d\n", csi2dc->vc);
  302. csi2dc->clk_gated = mbus_config.bus.parallel.flags &
  303. V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  304. dev_dbg(csi2dc->dev, "mbus_config: %s clock\n",
  305. csi2dc->clk_gated ? "gated" : "free running");
  306. return 0;
  307. }
  308. static void csi2dc_vp_update(struct csi2dc_device *csi2dc)
  309. {
  310. u32 vp, gcfg;
  311. if (!csi2dc->video_pipe) {
  312. dev_err(csi2dc->dev, "video pipeline unavailable\n");
  313. return;
  314. }
  315. if (csi2dc->parallel_mode) {
  316. /* In parallel mode, GPIO parallel interface must be selected */
  317. gcfg = csi2dc_readl(csi2dc, CSI2DC_GCFG);
  318. gcfg |= CSI2DC_GCFG_GPIOSEL;
  319. csi2dc_writel(csi2dc, CSI2DC_GCFG, gcfg);
  320. return;
  321. }
  322. /* serial video pipeline */
  323. csi2dc_writel(csi2dc, CSI2DC_GCFG,
  324. (SAMA7G5_HLC & CSI2DC_GCFG_HLC_MASK) |
  325. (csi2dc->clk_gated ? 0 : CSI2DC_GCFG_MIPIFRN));
  326. vp = CSI2DC_VPCFG_DT(csi2dc->cur_fmt->dt) & CSI2DC_VPCFG_DT_MASK;
  327. vp |= CSI2DC_VPCFG_VC(csi2dc->vc) & CSI2DC_VPCFG_VC_MASK;
  328. vp &= ~CSI2DC_VPCFG_DE;
  329. vp |= CSI2DC_VPCFG_DM(CSI2DC_VPCFG_DM_DECODER8TO12);
  330. vp &= ~CSI2DC_VPCFG_DP2;
  331. vp &= ~CSI2DC_VPCFG_RMS;
  332. vp |= CSI2DC_VPCFG_PA;
  333. csi2dc_writel(csi2dc, CSI2DC_VPCFG, vp);
  334. csi2dc_writel(csi2dc, CSI2DC_VPE, CSI2DC_VPE_ENABLE);
  335. csi2dc_writel(csi2dc, CSI2DC_PU, CSI2DC_PU_VP);
  336. }
  337. static int csi2dc_s_stream(struct v4l2_subdev *csi2dc_sd, int enable)
  338. {
  339. struct csi2dc_device *csi2dc = csi2dc_sd_to_csi2dc_device(csi2dc_sd);
  340. int ret;
  341. if (enable) {
  342. ret = pm_runtime_resume_and_get(csi2dc->dev);
  343. if (ret < 0)
  344. return ret;
  345. csi2dc_get_mbus_config(csi2dc);
  346. csi2dc_vp_update(csi2dc);
  347. return v4l2_subdev_call(csi2dc->input_sd, video, s_stream,
  348. true);
  349. }
  350. dev_dbg(csi2dc->dev,
  351. "Last frame received: VPCOLR = %u, VPROWR= %u, VPISR = %x\n",
  352. csi2dc_readl(csi2dc, CSI2DC_VPCOL),
  353. csi2dc_readl(csi2dc, CSI2DC_VPROW),
  354. csi2dc_readl(csi2dc, CSI2DC_VPISR));
  355. /* stop streaming scenario */
  356. ret = v4l2_subdev_call(csi2dc->input_sd, video, s_stream, false);
  357. pm_runtime_put_sync(csi2dc->dev);
  358. return ret;
  359. }
  360. static int csi2dc_init_state(struct v4l2_subdev *csi2dc_sd,
  361. struct v4l2_subdev_state *sd_state)
  362. {
  363. struct v4l2_mbus_framefmt *v4l2_try_fmt =
  364. v4l2_subdev_state_get_format(sd_state, 0);
  365. v4l2_try_fmt->height = 480;
  366. v4l2_try_fmt->width = 640;
  367. v4l2_try_fmt->code = csi2dc_formats[0].mbus_code;
  368. v4l2_try_fmt->colorspace = V4L2_COLORSPACE_SRGB;
  369. v4l2_try_fmt->field = V4L2_FIELD_NONE;
  370. v4l2_try_fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  371. v4l2_try_fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
  372. v4l2_try_fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  373. return 0;
  374. }
  375. static const struct media_entity_operations csi2dc_entity_ops = {
  376. .link_validate = v4l2_subdev_link_validate,
  377. };
  378. static const struct v4l2_subdev_pad_ops csi2dc_pad_ops = {
  379. .enum_mbus_code = csi2dc_enum_mbus_code,
  380. .set_fmt = csi2dc_set_fmt,
  381. .get_fmt = csi2dc_get_fmt,
  382. };
  383. static const struct v4l2_subdev_video_ops csi2dc_video_ops = {
  384. .s_stream = csi2dc_s_stream,
  385. };
  386. static const struct v4l2_subdev_ops csi2dc_subdev_ops = {
  387. .pad = &csi2dc_pad_ops,
  388. .video = &csi2dc_video_ops,
  389. };
  390. static const struct v4l2_subdev_internal_ops csi2dc_internal_ops = {
  391. .init_state = csi2dc_init_state,
  392. };
  393. static int csi2dc_async_bound(struct v4l2_async_notifier *notifier,
  394. struct v4l2_subdev *subdev,
  395. struct v4l2_async_connection *asd)
  396. {
  397. struct csi2dc_device *csi2dc = container_of(notifier,
  398. struct csi2dc_device, notifier);
  399. int pad;
  400. int ret;
  401. csi2dc->input_sd = subdev;
  402. pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode,
  403. MEDIA_PAD_FL_SOURCE);
  404. if (pad < 0) {
  405. dev_err(csi2dc->dev, "Failed to find pad for %s\n",
  406. subdev->name);
  407. return pad;
  408. }
  409. csi2dc->remote_pad = pad;
  410. ret = media_create_pad_link(&csi2dc->input_sd->entity,
  411. csi2dc->remote_pad,
  412. &csi2dc->csi2dc_sd.entity, 0,
  413. MEDIA_LNK_FL_ENABLED);
  414. if (ret) {
  415. dev_err(csi2dc->dev,
  416. "Failed to create pad link: %s to %s\n",
  417. csi2dc->input_sd->entity.name,
  418. csi2dc->csi2dc_sd.entity.name);
  419. return ret;
  420. }
  421. dev_dbg(csi2dc->dev, "link with %s pad: %d\n",
  422. csi2dc->input_sd->name, csi2dc->remote_pad);
  423. return ret;
  424. }
  425. static const struct v4l2_async_notifier_operations csi2dc_async_ops = {
  426. .bound = csi2dc_async_bound,
  427. };
  428. static int csi2dc_prepare_notifier(struct csi2dc_device *csi2dc,
  429. struct fwnode_handle *input_fwnode)
  430. {
  431. struct v4l2_async_connection *asd;
  432. int ret = 0;
  433. v4l2_async_subdev_nf_init(&csi2dc->notifier, &csi2dc->csi2dc_sd);
  434. asd = v4l2_async_nf_add_fwnode_remote(&csi2dc->notifier,
  435. input_fwnode,
  436. struct v4l2_async_connection);
  437. fwnode_handle_put(input_fwnode);
  438. if (IS_ERR(asd)) {
  439. ret = PTR_ERR(asd);
  440. dev_err(csi2dc->dev,
  441. "failed to add async notifier for node %pOF: %d\n",
  442. to_of_node(input_fwnode), ret);
  443. v4l2_async_nf_cleanup(&csi2dc->notifier);
  444. return ret;
  445. }
  446. csi2dc->notifier.ops = &csi2dc_async_ops;
  447. ret = v4l2_async_nf_register(&csi2dc->notifier);
  448. if (ret) {
  449. dev_err(csi2dc->dev, "fail to register async notifier: %d\n",
  450. ret);
  451. v4l2_async_nf_cleanup(&csi2dc->notifier);
  452. }
  453. return ret;
  454. }
  455. static int csi2dc_of_parse(struct csi2dc_device *csi2dc,
  456. struct device_node *of_node)
  457. {
  458. struct fwnode_handle *input_fwnode, *output_fwnode;
  459. struct v4l2_fwnode_endpoint input_endpoint = { 0 },
  460. output_endpoint = { 0 };
  461. int ret;
  462. input_fwnode = fwnode_graph_get_next_endpoint(of_fwnode_handle(of_node),
  463. NULL);
  464. if (!input_fwnode) {
  465. dev_err(csi2dc->dev,
  466. "missing port node at %pOF, input node is mandatory.\n",
  467. of_node);
  468. return -EINVAL;
  469. }
  470. ret = v4l2_fwnode_endpoint_parse(input_fwnode, &input_endpoint);
  471. if (ret) {
  472. dev_err(csi2dc->dev, "endpoint not defined at %pOF\n", of_node);
  473. goto csi2dc_of_parse_err;
  474. }
  475. if (input_endpoint.bus_type == V4L2_MBUS_PARALLEL ||
  476. input_endpoint.bus_type == V4L2_MBUS_BT656) {
  477. csi2dc->parallel_mode = true;
  478. dev_dbg(csi2dc->dev,
  479. "subdevice connected on parallel interface\n");
  480. }
  481. if (input_endpoint.bus_type == V4L2_MBUS_CSI2_DPHY) {
  482. csi2dc->clk_gated = input_endpoint.bus.mipi_csi2.flags &
  483. V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  484. dev_dbg(csi2dc->dev,
  485. "subdevice connected on serial interface\n");
  486. dev_dbg(csi2dc->dev, "DT: %s clock\n",
  487. csi2dc->clk_gated ? "gated" : "free running");
  488. }
  489. output_fwnode = fwnode_graph_get_next_endpoint
  490. (of_fwnode_handle(of_node), input_fwnode);
  491. if (output_fwnode)
  492. ret = v4l2_fwnode_endpoint_parse(output_fwnode,
  493. &output_endpoint);
  494. fwnode_handle_put(output_fwnode);
  495. if (!output_fwnode || ret) {
  496. dev_info(csi2dc->dev,
  497. "missing output node at %pOF, data pipe available only.\n",
  498. of_node);
  499. } else {
  500. if (output_endpoint.bus_type != V4L2_MBUS_PARALLEL &&
  501. output_endpoint.bus_type != V4L2_MBUS_BT656) {
  502. dev_err(csi2dc->dev,
  503. "output port must be parallel/bt656.\n");
  504. ret = -EINVAL;
  505. goto csi2dc_of_parse_err;
  506. }
  507. csi2dc->video_pipe = true;
  508. dev_dbg(csi2dc->dev,
  509. "block %pOF [%d.%d]->[%d.%d] video pipeline\n",
  510. of_node, input_endpoint.base.port,
  511. input_endpoint.base.id, output_endpoint.base.port,
  512. output_endpoint.base.id);
  513. }
  514. /* prepare async notifier for subdevice completion */
  515. return csi2dc_prepare_notifier(csi2dc, input_fwnode);
  516. csi2dc_of_parse_err:
  517. fwnode_handle_put(input_fwnode);
  518. return ret;
  519. }
  520. static void csi2dc_default_format(struct csi2dc_device *csi2dc)
  521. {
  522. csi2dc->cur_fmt = &csi2dc_formats[0];
  523. csi2dc->format.height = 480;
  524. csi2dc->format.width = 640;
  525. csi2dc->format.code = csi2dc_formats[0].mbus_code;
  526. csi2dc->format.colorspace = V4L2_COLORSPACE_SRGB;
  527. csi2dc->format.field = V4L2_FIELD_NONE;
  528. csi2dc->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  529. csi2dc->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  530. csi2dc->format.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  531. }
  532. static int csi2dc_probe(struct platform_device *pdev)
  533. {
  534. struct device *dev = &pdev->dev;
  535. struct csi2dc_device *csi2dc;
  536. int ret = 0;
  537. u32 ver;
  538. csi2dc = devm_kzalloc(dev, sizeof(*csi2dc), GFP_KERNEL);
  539. if (!csi2dc)
  540. return -ENOMEM;
  541. csi2dc->dev = dev;
  542. csi2dc->base = devm_platform_ioremap_resource(pdev, 0);
  543. if (IS_ERR(csi2dc->base)) {
  544. dev_err(dev, "base address not set\n");
  545. return PTR_ERR(csi2dc->base);
  546. }
  547. csi2dc->pclk = devm_clk_get(dev, "pclk");
  548. if (IS_ERR(csi2dc->pclk)) {
  549. ret = PTR_ERR(csi2dc->pclk);
  550. dev_err(dev, "failed to get pclk: %d\n", ret);
  551. return ret;
  552. }
  553. csi2dc->scck = devm_clk_get(dev, "scck");
  554. if (IS_ERR(csi2dc->scck)) {
  555. ret = PTR_ERR(csi2dc->scck);
  556. dev_err(dev, "failed to get scck: %d\n", ret);
  557. return ret;
  558. }
  559. v4l2_subdev_init(&csi2dc->csi2dc_sd, &csi2dc_subdev_ops);
  560. csi2dc->csi2dc_sd.internal_ops = &csi2dc_internal_ops;
  561. csi2dc->csi2dc_sd.owner = THIS_MODULE;
  562. csi2dc->csi2dc_sd.dev = dev;
  563. snprintf(csi2dc->csi2dc_sd.name, sizeof(csi2dc->csi2dc_sd.name),
  564. "csi2dc");
  565. csi2dc->csi2dc_sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  566. csi2dc->csi2dc_sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  567. csi2dc->csi2dc_sd.entity.ops = &csi2dc_entity_ops;
  568. platform_set_drvdata(pdev, csi2dc);
  569. ret = csi2dc_of_parse(csi2dc, dev->of_node);
  570. if (ret)
  571. goto csi2dc_probe_cleanup_entity;
  572. csi2dc->pads[CSI2DC_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  573. if (csi2dc->video_pipe)
  574. csi2dc->pads[CSI2DC_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  575. ret = media_entity_pads_init(&csi2dc->csi2dc_sd.entity,
  576. csi2dc->video_pipe ? CSI2DC_PADS_NUM : 1,
  577. csi2dc->pads);
  578. if (ret < 0) {
  579. dev_err(dev, "media entity init failed\n");
  580. goto csi2dc_probe_cleanup_notifier;
  581. }
  582. csi2dc_default_format(csi2dc);
  583. /* turn power on to validate capabilities */
  584. ret = csi2dc_power(csi2dc, true);
  585. if (ret < 0)
  586. goto csi2dc_probe_cleanup_notifier;
  587. pm_runtime_set_active(dev);
  588. pm_runtime_enable(dev);
  589. ver = csi2dc_readl(csi2dc, CSI2DC_VERSION);
  590. /*
  591. * we must register the subdev after PM runtime has been requested,
  592. * otherwise we might bound immediately and request pm_runtime_resume
  593. * before runtime_enable.
  594. */
  595. ret = v4l2_async_register_subdev(&csi2dc->csi2dc_sd);
  596. if (ret) {
  597. dev_err(csi2dc->dev, "failed to register the subdevice\n");
  598. goto csi2dc_probe_cleanup_notifier;
  599. }
  600. dev_info(dev, "Microchip CSI2DC version %x\n", ver);
  601. return 0;
  602. csi2dc_probe_cleanup_notifier:
  603. v4l2_async_nf_cleanup(&csi2dc->notifier);
  604. csi2dc_probe_cleanup_entity:
  605. media_entity_cleanup(&csi2dc->csi2dc_sd.entity);
  606. return ret;
  607. }
  608. static void csi2dc_remove(struct platform_device *pdev)
  609. {
  610. struct csi2dc_device *csi2dc = platform_get_drvdata(pdev);
  611. pm_runtime_disable(&pdev->dev);
  612. v4l2_async_unregister_subdev(&csi2dc->csi2dc_sd);
  613. v4l2_async_nf_unregister(&csi2dc->notifier);
  614. v4l2_async_nf_cleanup(&csi2dc->notifier);
  615. media_entity_cleanup(&csi2dc->csi2dc_sd.entity);
  616. }
  617. static int __maybe_unused csi2dc_runtime_suspend(struct device *dev)
  618. {
  619. struct csi2dc_device *csi2dc = dev_get_drvdata(dev);
  620. return csi2dc_power(csi2dc, false);
  621. }
  622. static int __maybe_unused csi2dc_runtime_resume(struct device *dev)
  623. {
  624. struct csi2dc_device *csi2dc = dev_get_drvdata(dev);
  625. return csi2dc_power(csi2dc, true);
  626. }
  627. static const struct dev_pm_ops csi2dc_dev_pm_ops = {
  628. SET_RUNTIME_PM_OPS(csi2dc_runtime_suspend, csi2dc_runtime_resume, NULL)
  629. };
  630. static const struct of_device_id csi2dc_of_match[] = {
  631. { .compatible = "microchip,sama7g5-csi2dc" },
  632. { }
  633. };
  634. MODULE_DEVICE_TABLE(of, csi2dc_of_match);
  635. static struct platform_driver csi2dc_driver = {
  636. .probe = csi2dc_probe,
  637. .remove_new = csi2dc_remove,
  638. .driver = {
  639. .name = "microchip-csi2dc",
  640. .pm = &csi2dc_dev_pm_ops,
  641. .of_match_table = of_match_ptr(csi2dc_of_match),
  642. },
  643. };
  644. module_platform_driver(csi2dc_driver);
  645. MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com>");
  646. MODULE_DESCRIPTION("Microchip CSI2 Demux Controller driver");
  647. MODULE_LICENSE("GPL v2");