xilinx-csi2rxss.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Xilinx MIPI CSI-2 Rx Subsystem
  4. *
  5. * Copyright (C) 2016 - 2020 Xilinx, Inc.
  6. *
  7. * Contacts: Vishal Sagar <vishal.sagar@xilinx.com>
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/v4l2-subdev.h>
  20. #include <media/media-entity.h>
  21. #include <media/mipi-csi2.h>
  22. #include <media/v4l2-common.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-fwnode.h>
  25. #include <media/v4l2-subdev.h>
  26. #include "xilinx-vip.h"
  27. /* Register register map */
  28. #define XCSI_CCR_OFFSET 0x00
  29. #define XCSI_CCR_SOFTRESET BIT(1)
  30. #define XCSI_CCR_ENABLE BIT(0)
  31. #define XCSI_PCR_OFFSET 0x04
  32. #define XCSI_PCR_MAXLANES_MASK GENMASK(4, 3)
  33. #define XCSI_PCR_ACTLANES_MASK GENMASK(1, 0)
  34. #define XCSI_CSR_OFFSET 0x10
  35. #define XCSI_CSR_PKTCNT GENMASK(31, 16)
  36. #define XCSI_CSR_SPFIFOFULL BIT(3)
  37. #define XCSI_CSR_SPFIFONE BIT(2)
  38. #define XCSI_CSR_SLBF BIT(1)
  39. #define XCSI_CSR_RIPCD BIT(0)
  40. #define XCSI_GIER_OFFSET 0x20
  41. #define XCSI_GIER_GIE BIT(0)
  42. #define XCSI_ISR_OFFSET 0x24
  43. #define XCSI_IER_OFFSET 0x28
  44. #define XCSI_ISR_FR BIT(31)
  45. #define XCSI_ISR_VCXFE BIT(30)
  46. #define XCSI_ISR_WCC BIT(22)
  47. #define XCSI_ISR_ILC BIT(21)
  48. #define XCSI_ISR_SPFIFOF BIT(20)
  49. #define XCSI_ISR_SPFIFONE BIT(19)
  50. #define XCSI_ISR_SLBF BIT(18)
  51. #define XCSI_ISR_STOP BIT(17)
  52. #define XCSI_ISR_SOTERR BIT(13)
  53. #define XCSI_ISR_SOTSYNCERR BIT(12)
  54. #define XCSI_ISR_ECC2BERR BIT(11)
  55. #define XCSI_ISR_ECC1BERR BIT(10)
  56. #define XCSI_ISR_CRCERR BIT(9)
  57. #define XCSI_ISR_DATAIDERR BIT(8)
  58. #define XCSI_ISR_VC3FSYNCERR BIT(7)
  59. #define XCSI_ISR_VC3FLVLERR BIT(6)
  60. #define XCSI_ISR_VC2FSYNCERR BIT(5)
  61. #define XCSI_ISR_VC2FLVLERR BIT(4)
  62. #define XCSI_ISR_VC1FSYNCERR BIT(3)
  63. #define XCSI_ISR_VC1FLVLERR BIT(2)
  64. #define XCSI_ISR_VC0FSYNCERR BIT(1)
  65. #define XCSI_ISR_VC0FLVLERR BIT(0)
  66. #define XCSI_ISR_ALLINTR_MASK (0xc07e3fff)
  67. /*
  68. * Removed VCXFE mask as it doesn't exist in IER
  69. * Removed STOP state irq as this will keep driver in irq handler only
  70. */
  71. #define XCSI_IER_INTR_MASK (XCSI_ISR_ALLINTR_MASK &\
  72. ~(XCSI_ISR_STOP | XCSI_ISR_VCXFE))
  73. #define XCSI_SPKTR_OFFSET 0x30
  74. #define XCSI_SPKTR_DATA GENMASK(23, 8)
  75. #define XCSI_SPKTR_VC GENMASK(7, 6)
  76. #define XCSI_SPKTR_DT GENMASK(5, 0)
  77. #define XCSI_SPKT_FIFO_DEPTH 31
  78. #define XCSI_VCXR_OFFSET 0x34
  79. #define XCSI_VCXR_VCERR GENMASK(23, 0)
  80. #define XCSI_VCXR_FSYNCERR BIT(1)
  81. #define XCSI_VCXR_FLVLERR BIT(0)
  82. #define XCSI_CLKINFR_OFFSET 0x3C
  83. #define XCSI_CLKINFR_STOP BIT(1)
  84. #define XCSI_DLXINFR_OFFSET 0x40
  85. #define XCSI_DLXINFR_STOP BIT(5)
  86. #define XCSI_DLXINFR_SOTERR BIT(1)
  87. #define XCSI_DLXINFR_SOTSYNCERR BIT(0)
  88. #define XCSI_MAXDL_COUNT 0x4
  89. #define XCSI_VCXINF1R_OFFSET 0x60
  90. #define XCSI_VCXINF1R_LINECOUNT GENMASK(31, 16)
  91. #define XCSI_VCXINF1R_LINECOUNT_SHIFT 16
  92. #define XCSI_VCXINF1R_BYTECOUNT GENMASK(15, 0)
  93. #define XCSI_VCXINF2R_OFFSET 0x64
  94. #define XCSI_VCXINF2R_DT GENMASK(5, 0)
  95. #define XCSI_MAXVCX_COUNT 16
  96. /*
  97. * Sink pad connected to sensor source pad.
  98. * Source pad connected to next module like demosaic.
  99. */
  100. #define XCSI_MEDIA_PADS 2
  101. #define XCSI_DEFAULT_WIDTH 1920
  102. #define XCSI_DEFAULT_HEIGHT 1080
  103. #define XCSI_VCX_START 4
  104. #define XCSI_MAX_VC 4
  105. #define XCSI_MAX_VCX 16
  106. #define XCSI_NEXTREG_OFFSET 4
  107. /* There are 2 events frame sync and frame level error per VC */
  108. #define XCSI_VCX_NUM_EVENTS ((XCSI_MAX_VCX - XCSI_MAX_VC) * 2)
  109. /**
  110. * struct xcsi2rxss_event - Event log structure
  111. * @mask: Event mask
  112. * @name: Name of the event
  113. */
  114. struct xcsi2rxss_event {
  115. u32 mask;
  116. const char *name;
  117. };
  118. static const struct xcsi2rxss_event xcsi2rxss_events[] = {
  119. { XCSI_ISR_FR, "Frame Received" },
  120. { XCSI_ISR_VCXFE, "VCX Frame Errors" },
  121. { XCSI_ISR_WCC, "Word Count Errors" },
  122. { XCSI_ISR_ILC, "Invalid Lane Count Error" },
  123. { XCSI_ISR_SPFIFOF, "Short Packet FIFO OverFlow Error" },
  124. { XCSI_ISR_SPFIFONE, "Short Packet FIFO Not Empty" },
  125. { XCSI_ISR_SLBF, "Streamline Buffer Full Error" },
  126. { XCSI_ISR_STOP, "Lane Stop State" },
  127. { XCSI_ISR_SOTERR, "SOT Error" },
  128. { XCSI_ISR_SOTSYNCERR, "SOT Sync Error" },
  129. { XCSI_ISR_ECC2BERR, "2 Bit ECC Unrecoverable Error" },
  130. { XCSI_ISR_ECC1BERR, "1 Bit ECC Recoverable Error" },
  131. { XCSI_ISR_CRCERR, "CRC Error" },
  132. { XCSI_ISR_DATAIDERR, "Data Id Error" },
  133. { XCSI_ISR_VC3FSYNCERR, "Virtual Channel 3 Frame Sync Error" },
  134. { XCSI_ISR_VC3FLVLERR, "Virtual Channel 3 Frame Level Error" },
  135. { XCSI_ISR_VC2FSYNCERR, "Virtual Channel 2 Frame Sync Error" },
  136. { XCSI_ISR_VC2FLVLERR, "Virtual Channel 2 Frame Level Error" },
  137. { XCSI_ISR_VC1FSYNCERR, "Virtual Channel 1 Frame Sync Error" },
  138. { XCSI_ISR_VC1FLVLERR, "Virtual Channel 1 Frame Level Error" },
  139. { XCSI_ISR_VC0FSYNCERR, "Virtual Channel 0 Frame Sync Error" },
  140. { XCSI_ISR_VC0FLVLERR, "Virtual Channel 0 Frame Level Error" }
  141. };
  142. #define XCSI_NUM_EVENTS ARRAY_SIZE(xcsi2rxss_events)
  143. /*
  144. * This table provides a mapping between CSI-2 Data type
  145. * and media bus formats
  146. */
  147. static const u32 xcsi2dt_mbus_lut[][2] = {
  148. { MIPI_CSI2_DT_YUV422_8B, MEDIA_BUS_FMT_UYVY8_1X16 },
  149. { MIPI_CSI2_DT_YUV422_10B, MEDIA_BUS_FMT_UYVY10_1X20 },
  150. { MIPI_CSI2_DT_RGB444, 0 },
  151. { MIPI_CSI2_DT_RGB555, 0 },
  152. { MIPI_CSI2_DT_RGB565, 0 },
  153. { MIPI_CSI2_DT_RGB666, 0 },
  154. { MIPI_CSI2_DT_RGB888, MEDIA_BUS_FMT_RBG888_1X24 },
  155. { MIPI_CSI2_DT_RAW6, 0 },
  156. { MIPI_CSI2_DT_RAW7, 0 },
  157. { MIPI_CSI2_DT_RAW8, MEDIA_BUS_FMT_SRGGB8_1X8 },
  158. { MIPI_CSI2_DT_RAW8, MEDIA_BUS_FMT_SBGGR8_1X8 },
  159. { MIPI_CSI2_DT_RAW8, MEDIA_BUS_FMT_SGBRG8_1X8 },
  160. { MIPI_CSI2_DT_RAW8, MEDIA_BUS_FMT_SGRBG8_1X8 },
  161. { MIPI_CSI2_DT_RAW10, MEDIA_BUS_FMT_SRGGB10_1X10 },
  162. { MIPI_CSI2_DT_RAW10, MEDIA_BUS_FMT_SBGGR10_1X10 },
  163. { MIPI_CSI2_DT_RAW10, MEDIA_BUS_FMT_SGBRG10_1X10 },
  164. { MIPI_CSI2_DT_RAW10, MEDIA_BUS_FMT_SGRBG10_1X10 },
  165. { MIPI_CSI2_DT_RAW12, MEDIA_BUS_FMT_SRGGB12_1X12 },
  166. { MIPI_CSI2_DT_RAW12, MEDIA_BUS_FMT_SBGGR12_1X12 },
  167. { MIPI_CSI2_DT_RAW12, MEDIA_BUS_FMT_SGBRG12_1X12 },
  168. { MIPI_CSI2_DT_RAW12, MEDIA_BUS_FMT_SGRBG12_1X12 },
  169. { MIPI_CSI2_DT_RAW12, MEDIA_BUS_FMT_Y12_1X12 },
  170. { MIPI_CSI2_DT_RAW16, MEDIA_BUS_FMT_SRGGB16_1X16 },
  171. { MIPI_CSI2_DT_RAW16, MEDIA_BUS_FMT_SBGGR16_1X16 },
  172. { MIPI_CSI2_DT_RAW16, MEDIA_BUS_FMT_SGBRG16_1X16 },
  173. { MIPI_CSI2_DT_RAW16, MEDIA_BUS_FMT_SGRBG16_1X16 },
  174. { MIPI_CSI2_DT_RAW20, 0 },
  175. };
  176. /**
  177. * struct xcsi2rxss_state - CSI-2 Rx Subsystem device structure
  178. * @subdev: The v4l2 subdev structure
  179. * @format: Active V4L2 formats on each pad
  180. * @default_format: Default V4L2 format
  181. * @events: counter for events
  182. * @vcx_events: counter for vcx_events
  183. * @dev: Platform structure
  184. * @rsubdev: Remote subdev connected to sink pad
  185. * @rst_gpio: reset to video_aresetn
  186. * @clks: array of clocks
  187. * @iomem: Base address of subsystem
  188. * @max_num_lanes: Maximum number of lanes present
  189. * @datatype: Data type filter
  190. * @lock: mutex for accessing this structure
  191. * @pads: media pads
  192. * @streaming: Flag for storing streaming state
  193. * @enable_active_lanes: If number of active lanes can be modified
  194. * @en_vcx: If more than 4 VC are enabled
  195. *
  196. * This structure contains the device driver related parameters
  197. */
  198. struct xcsi2rxss_state {
  199. struct v4l2_subdev subdev;
  200. struct v4l2_mbus_framefmt format;
  201. struct v4l2_mbus_framefmt default_format;
  202. u32 events[XCSI_NUM_EVENTS];
  203. u32 vcx_events[XCSI_VCX_NUM_EVENTS];
  204. struct device *dev;
  205. struct v4l2_subdev *rsubdev;
  206. struct gpio_desc *rst_gpio;
  207. struct clk_bulk_data *clks;
  208. void __iomem *iomem;
  209. u32 max_num_lanes;
  210. u32 datatype;
  211. /* used to protect access to this struct */
  212. struct mutex lock;
  213. struct media_pad pads[XCSI_MEDIA_PADS];
  214. bool streaming;
  215. bool enable_active_lanes;
  216. bool en_vcx;
  217. };
  218. static const struct clk_bulk_data xcsi2rxss_clks[] = {
  219. { .id = "lite_aclk" },
  220. { .id = "video_aclk" },
  221. };
  222. static inline struct xcsi2rxss_state *
  223. to_xcsi2rxssstate(struct v4l2_subdev *subdev)
  224. {
  225. return container_of(subdev, struct xcsi2rxss_state, subdev);
  226. }
  227. /*
  228. * Register related operations
  229. */
  230. static inline u32 xcsi2rxss_read(struct xcsi2rxss_state *xcsi2rxss, u32 addr)
  231. {
  232. return ioread32(xcsi2rxss->iomem + addr);
  233. }
  234. static inline void xcsi2rxss_write(struct xcsi2rxss_state *xcsi2rxss, u32 addr,
  235. u32 value)
  236. {
  237. iowrite32(value, xcsi2rxss->iomem + addr);
  238. }
  239. static inline void xcsi2rxss_clr(struct xcsi2rxss_state *xcsi2rxss, u32 addr,
  240. u32 clr)
  241. {
  242. xcsi2rxss_write(xcsi2rxss, addr,
  243. xcsi2rxss_read(xcsi2rxss, addr) & ~clr);
  244. }
  245. static inline void xcsi2rxss_set(struct xcsi2rxss_state *xcsi2rxss, u32 addr,
  246. u32 set)
  247. {
  248. xcsi2rxss_write(xcsi2rxss, addr, xcsi2rxss_read(xcsi2rxss, addr) | set);
  249. }
  250. /*
  251. * This function returns the nth mbus for a data type.
  252. * In case of error, mbus code returned is 0.
  253. */
  254. static u32 xcsi2rxss_get_nth_mbus(u32 dt, u32 n)
  255. {
  256. unsigned int i;
  257. for (i = 0; i < ARRAY_SIZE(xcsi2dt_mbus_lut); i++) {
  258. if (xcsi2dt_mbus_lut[i][0] == dt) {
  259. if (n-- == 0)
  260. return xcsi2dt_mbus_lut[i][1];
  261. }
  262. }
  263. return 0;
  264. }
  265. /* This returns the data type for a media bus format else 0 */
  266. static u32 xcsi2rxss_get_dt(u32 mbus)
  267. {
  268. unsigned int i;
  269. for (i = 0; i < ARRAY_SIZE(xcsi2dt_mbus_lut); i++) {
  270. if (xcsi2dt_mbus_lut[i][1] == mbus)
  271. return xcsi2dt_mbus_lut[i][0];
  272. }
  273. return 0;
  274. }
  275. /**
  276. * xcsi2rxss_soft_reset - Does a soft reset of the MIPI CSI-2 Rx Subsystem
  277. * @state: Xilinx CSI-2 Rx Subsystem structure pointer
  278. *
  279. * Core takes less than 100 video clock cycles to reset.
  280. * So a larger timeout value is chosen for margin.
  281. *
  282. * Return: 0 - on success OR -ETIME if reset times out
  283. */
  284. static int xcsi2rxss_soft_reset(struct xcsi2rxss_state *state)
  285. {
  286. u32 timeout = 1000; /* us */
  287. xcsi2rxss_set(state, XCSI_CCR_OFFSET, XCSI_CCR_SOFTRESET);
  288. while (xcsi2rxss_read(state, XCSI_CSR_OFFSET) & XCSI_CSR_RIPCD) {
  289. if (timeout == 0) {
  290. dev_err(state->dev, "soft reset timed out!\n");
  291. return -ETIME;
  292. }
  293. timeout--;
  294. udelay(1);
  295. }
  296. xcsi2rxss_clr(state, XCSI_CCR_OFFSET, XCSI_CCR_SOFTRESET);
  297. return 0;
  298. }
  299. static void xcsi2rxss_hard_reset(struct xcsi2rxss_state *state)
  300. {
  301. if (!state->rst_gpio)
  302. return;
  303. /* minimum of 40 dphy_clk_200M cycles */
  304. gpiod_set_value_cansleep(state->rst_gpio, 1);
  305. usleep_range(1, 2);
  306. gpiod_set_value_cansleep(state->rst_gpio, 0);
  307. }
  308. static void xcsi2rxss_reset_event_counters(struct xcsi2rxss_state *state)
  309. {
  310. unsigned int i;
  311. for (i = 0; i < XCSI_NUM_EVENTS; i++)
  312. state->events[i] = 0;
  313. for (i = 0; i < XCSI_VCX_NUM_EVENTS; i++)
  314. state->vcx_events[i] = 0;
  315. }
  316. /* Print event counters */
  317. static void xcsi2rxss_log_counters(struct xcsi2rxss_state *state)
  318. {
  319. struct device *dev = state->dev;
  320. unsigned int i;
  321. for (i = 0; i < XCSI_NUM_EVENTS; i++) {
  322. if (state->events[i] > 0) {
  323. dev_info(dev, "%s events: %d\n",
  324. xcsi2rxss_events[i].name,
  325. state->events[i]);
  326. }
  327. }
  328. if (state->en_vcx) {
  329. for (i = 0; i < XCSI_VCX_NUM_EVENTS; i++) {
  330. if (state->vcx_events[i] > 0) {
  331. dev_info(dev,
  332. "VC %d Frame %s err vcx events: %d\n",
  333. (i / 2) + XCSI_VCX_START,
  334. i & 1 ? "Sync" : "Level",
  335. state->vcx_events[i]);
  336. }
  337. }
  338. }
  339. }
  340. static int xcsi2rxss_log_status(struct v4l2_subdev *sd)
  341. {
  342. struct xcsi2rxss_state *xcsi2rxss = to_xcsi2rxssstate(sd);
  343. struct device *dev = xcsi2rxss->dev;
  344. u32 reg, data;
  345. unsigned int i, max_vc;
  346. mutex_lock(&xcsi2rxss->lock);
  347. xcsi2rxss_log_counters(xcsi2rxss);
  348. dev_info(dev, "***** Core Status *****\n");
  349. data = xcsi2rxss_read(xcsi2rxss, XCSI_CSR_OFFSET);
  350. dev_info(dev, "Short Packet FIFO Full = %s\n",
  351. data & XCSI_CSR_SPFIFOFULL ? "true" : "false");
  352. dev_info(dev, "Short Packet FIFO Not Empty = %s\n",
  353. data & XCSI_CSR_SPFIFONE ? "true" : "false");
  354. dev_info(dev, "Stream line buffer full = %s\n",
  355. data & XCSI_CSR_SLBF ? "true" : "false");
  356. dev_info(dev, "Soft reset/Core disable in progress = %s\n",
  357. data & XCSI_CSR_RIPCD ? "true" : "false");
  358. /* Clk & Lane Info */
  359. dev_info(dev, "******** Clock Lane Info *********\n");
  360. data = xcsi2rxss_read(xcsi2rxss, XCSI_CLKINFR_OFFSET);
  361. dev_info(dev, "Clock Lane in Stop State = %s\n",
  362. data & XCSI_CLKINFR_STOP ? "true" : "false");
  363. dev_info(dev, "******** Data Lane Info *********\n");
  364. dev_info(dev, "Lane\tSoT Error\tSoT Sync Error\tStop State\n");
  365. reg = XCSI_DLXINFR_OFFSET;
  366. for (i = 0; i < XCSI_MAXDL_COUNT; i++) {
  367. data = xcsi2rxss_read(xcsi2rxss, reg);
  368. dev_info(dev, "%d\t%s\t\t%s\t\t%s\n", i,
  369. data & XCSI_DLXINFR_SOTERR ? "true" : "false",
  370. data & XCSI_DLXINFR_SOTSYNCERR ? "true" : "false",
  371. data & XCSI_DLXINFR_STOP ? "true" : "false");
  372. reg += XCSI_NEXTREG_OFFSET;
  373. }
  374. /* Virtual Channel Image Information */
  375. dev_info(dev, "********** Virtual Channel Info ************\n");
  376. dev_info(dev, "VC\tLine Count\tByte Count\tData Type\n");
  377. if (xcsi2rxss->en_vcx)
  378. max_vc = XCSI_MAX_VCX;
  379. else
  380. max_vc = XCSI_MAX_VC;
  381. reg = XCSI_VCXINF1R_OFFSET;
  382. for (i = 0; i < max_vc; i++) {
  383. u32 line_count, byte_count, data_type;
  384. /* Get line and byte count from VCXINFR1 Register */
  385. data = xcsi2rxss_read(xcsi2rxss, reg);
  386. byte_count = data & XCSI_VCXINF1R_BYTECOUNT;
  387. line_count = data & XCSI_VCXINF1R_LINECOUNT;
  388. line_count >>= XCSI_VCXINF1R_LINECOUNT_SHIFT;
  389. /* Get data type from VCXINFR2 Register */
  390. reg += XCSI_NEXTREG_OFFSET;
  391. data = xcsi2rxss_read(xcsi2rxss, reg);
  392. data_type = data & XCSI_VCXINF2R_DT;
  393. dev_info(dev, "%d\t%d\t\t%d\t\t0x%x\n", i, line_count,
  394. byte_count, data_type);
  395. /* Move to next pair of VC Info registers */
  396. reg += XCSI_NEXTREG_OFFSET;
  397. }
  398. mutex_unlock(&xcsi2rxss->lock);
  399. return 0;
  400. }
  401. static struct v4l2_subdev *xcsi2rxss_get_remote_subdev(struct media_pad *local)
  402. {
  403. struct media_pad *remote;
  404. remote = media_pad_remote_pad_first(local);
  405. if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
  406. return NULL;
  407. return media_entity_to_v4l2_subdev(remote->entity);
  408. }
  409. static int xcsi2rxss_start_stream(struct xcsi2rxss_state *state)
  410. {
  411. int ret = 0;
  412. /* enable core */
  413. xcsi2rxss_set(state, XCSI_CCR_OFFSET, XCSI_CCR_ENABLE);
  414. ret = xcsi2rxss_soft_reset(state);
  415. if (ret) {
  416. state->streaming = false;
  417. return ret;
  418. }
  419. /* enable interrupts */
  420. xcsi2rxss_clr(state, XCSI_GIER_OFFSET, XCSI_GIER_GIE);
  421. xcsi2rxss_write(state, XCSI_IER_OFFSET, XCSI_IER_INTR_MASK);
  422. xcsi2rxss_set(state, XCSI_GIER_OFFSET, XCSI_GIER_GIE);
  423. state->streaming = true;
  424. state->rsubdev =
  425. xcsi2rxss_get_remote_subdev(&state->pads[XVIP_PAD_SINK]);
  426. ret = v4l2_subdev_call(state->rsubdev, video, s_stream, 1);
  427. if (ret) {
  428. /* disable interrupts */
  429. xcsi2rxss_clr(state, XCSI_IER_OFFSET, XCSI_IER_INTR_MASK);
  430. xcsi2rxss_clr(state, XCSI_GIER_OFFSET, XCSI_GIER_GIE);
  431. /* disable core */
  432. xcsi2rxss_clr(state, XCSI_CCR_OFFSET, XCSI_CCR_ENABLE);
  433. state->streaming = false;
  434. }
  435. return ret;
  436. }
  437. static void xcsi2rxss_stop_stream(struct xcsi2rxss_state *state)
  438. {
  439. v4l2_subdev_call(state->rsubdev, video, s_stream, 0);
  440. /* disable interrupts */
  441. xcsi2rxss_clr(state, XCSI_IER_OFFSET, XCSI_IER_INTR_MASK);
  442. xcsi2rxss_clr(state, XCSI_GIER_OFFSET, XCSI_GIER_GIE);
  443. /* disable core */
  444. xcsi2rxss_clr(state, XCSI_CCR_OFFSET, XCSI_CCR_ENABLE);
  445. state->streaming = false;
  446. }
  447. /**
  448. * xcsi2rxss_irq_handler - Interrupt handler for CSI-2
  449. * @irq: IRQ number
  450. * @data: Pointer to device state
  451. *
  452. * In the interrupt handler, a list of event counters are updated for
  453. * corresponding interrupts. This is useful to get status / debug.
  454. *
  455. * Return: IRQ_HANDLED after handling interrupts
  456. */
  457. static irqreturn_t xcsi2rxss_irq_handler(int irq, void *data)
  458. {
  459. struct xcsi2rxss_state *state = (struct xcsi2rxss_state *)data;
  460. struct device *dev = state->dev;
  461. u32 status;
  462. status = xcsi2rxss_read(state, XCSI_ISR_OFFSET) & XCSI_ISR_ALLINTR_MASK;
  463. xcsi2rxss_write(state, XCSI_ISR_OFFSET, status);
  464. /* Received a short packet */
  465. if (status & XCSI_ISR_SPFIFONE) {
  466. u32 count = 0;
  467. /*
  468. * Drain generic short packet FIFO by reading max 31
  469. * (fifo depth) short packets from fifo or till fifo is empty.
  470. */
  471. for (count = 0; count < XCSI_SPKT_FIFO_DEPTH; ++count) {
  472. u32 spfifostat, spkt;
  473. spkt = xcsi2rxss_read(state, XCSI_SPKTR_OFFSET);
  474. dev_dbg(dev, "Short packet = 0x%08x\n", spkt);
  475. spfifostat = xcsi2rxss_read(state, XCSI_ISR_OFFSET);
  476. spfifostat &= XCSI_ISR_SPFIFONE;
  477. if (!spfifostat)
  478. break;
  479. xcsi2rxss_write(state, XCSI_ISR_OFFSET, spfifostat);
  480. }
  481. }
  482. /* Short packet FIFO overflow */
  483. if (status & XCSI_ISR_SPFIFOF)
  484. dev_dbg_ratelimited(dev, "Short packet FIFO overflowed\n");
  485. /*
  486. * Stream line buffer full
  487. * This means there is a backpressure from downstream IP
  488. */
  489. if (status & XCSI_ISR_SLBF) {
  490. dev_alert_ratelimited(dev, "Stream Line Buffer Full!\n");
  491. /* disable interrupts */
  492. xcsi2rxss_clr(state, XCSI_IER_OFFSET, XCSI_IER_INTR_MASK);
  493. xcsi2rxss_clr(state, XCSI_GIER_OFFSET, XCSI_GIER_GIE);
  494. /* disable core */
  495. xcsi2rxss_clr(state, XCSI_CCR_OFFSET, XCSI_CCR_ENABLE);
  496. /*
  497. * The IP needs to be hard reset before it can be used now.
  498. * This will be done in streamoff.
  499. */
  500. /*
  501. * TODO: Notify the whole pipeline with v4l2_subdev_notify() to
  502. * inform userspace.
  503. */
  504. }
  505. /* Increment event counters */
  506. if (status & XCSI_ISR_ALLINTR_MASK) {
  507. unsigned int i;
  508. for (i = 0; i < XCSI_NUM_EVENTS; i++) {
  509. if (!(status & xcsi2rxss_events[i].mask))
  510. continue;
  511. state->events[i]++;
  512. dev_dbg_ratelimited(dev, "%s: %u\n",
  513. xcsi2rxss_events[i].name,
  514. state->events[i]);
  515. }
  516. if (status & XCSI_ISR_VCXFE && state->en_vcx) {
  517. u32 vcxstatus;
  518. vcxstatus = xcsi2rxss_read(state, XCSI_VCXR_OFFSET);
  519. vcxstatus &= XCSI_VCXR_VCERR;
  520. for (i = 0; i < XCSI_VCX_NUM_EVENTS; i++) {
  521. if (!(vcxstatus & BIT(i)))
  522. continue;
  523. state->vcx_events[i]++;
  524. }
  525. xcsi2rxss_write(state, XCSI_VCXR_OFFSET, vcxstatus);
  526. }
  527. }
  528. return IRQ_HANDLED;
  529. }
  530. static int xcsi2rxss_s_stream(struct v4l2_subdev *sd, int enable)
  531. {
  532. struct xcsi2rxss_state *xcsi2rxss = to_xcsi2rxssstate(sd);
  533. int ret = 0;
  534. mutex_lock(&xcsi2rxss->lock);
  535. if (enable == xcsi2rxss->streaming)
  536. goto stream_done;
  537. if (enable) {
  538. xcsi2rxss_reset_event_counters(xcsi2rxss);
  539. ret = xcsi2rxss_start_stream(xcsi2rxss);
  540. } else {
  541. xcsi2rxss_stop_stream(xcsi2rxss);
  542. xcsi2rxss_hard_reset(xcsi2rxss);
  543. }
  544. stream_done:
  545. mutex_unlock(&xcsi2rxss->lock);
  546. return ret;
  547. }
  548. static struct v4l2_mbus_framefmt *
  549. __xcsi2rxss_get_pad_format(struct xcsi2rxss_state *xcsi2rxss,
  550. struct v4l2_subdev_state *sd_state,
  551. unsigned int pad, u32 which)
  552. {
  553. switch (which) {
  554. case V4L2_SUBDEV_FORMAT_TRY:
  555. return v4l2_subdev_state_get_format(sd_state, pad);
  556. case V4L2_SUBDEV_FORMAT_ACTIVE:
  557. return &xcsi2rxss->format;
  558. default:
  559. return NULL;
  560. }
  561. }
  562. static int xcsi2rxss_init_state(struct v4l2_subdev *sd,
  563. struct v4l2_subdev_state *sd_state)
  564. {
  565. struct xcsi2rxss_state *xcsi2rxss = to_xcsi2rxssstate(sd);
  566. struct v4l2_mbus_framefmt *format;
  567. unsigned int i;
  568. mutex_lock(&xcsi2rxss->lock);
  569. for (i = 0; i < XCSI_MEDIA_PADS; i++) {
  570. format = v4l2_subdev_state_get_format(sd_state, i);
  571. *format = xcsi2rxss->default_format;
  572. }
  573. mutex_unlock(&xcsi2rxss->lock);
  574. return 0;
  575. }
  576. static int xcsi2rxss_get_format(struct v4l2_subdev *sd,
  577. struct v4l2_subdev_state *sd_state,
  578. struct v4l2_subdev_format *fmt)
  579. {
  580. struct xcsi2rxss_state *xcsi2rxss = to_xcsi2rxssstate(sd);
  581. mutex_lock(&xcsi2rxss->lock);
  582. fmt->format = *__xcsi2rxss_get_pad_format(xcsi2rxss, sd_state,
  583. fmt->pad,
  584. fmt->which);
  585. mutex_unlock(&xcsi2rxss->lock);
  586. return 0;
  587. }
  588. static int xcsi2rxss_set_format(struct v4l2_subdev *sd,
  589. struct v4l2_subdev_state *sd_state,
  590. struct v4l2_subdev_format *fmt)
  591. {
  592. struct xcsi2rxss_state *xcsi2rxss = to_xcsi2rxssstate(sd);
  593. struct v4l2_mbus_framefmt *__format;
  594. u32 dt;
  595. mutex_lock(&xcsi2rxss->lock);
  596. /*
  597. * Only the format->code parameter matters for CSI as the
  598. * CSI format cannot be changed at runtime.
  599. * Ensure that format to set is copied to over to CSI pad format
  600. */
  601. __format = __xcsi2rxss_get_pad_format(xcsi2rxss, sd_state,
  602. fmt->pad, fmt->which);
  603. /* only sink pad format can be updated */
  604. if (fmt->pad == XVIP_PAD_SOURCE) {
  605. fmt->format = *__format;
  606. mutex_unlock(&xcsi2rxss->lock);
  607. return 0;
  608. }
  609. /*
  610. * RAW8 is supported in all datatypes. So if requested media bus format
  611. * is of RAW8 type, then allow to be set. In case core is configured to
  612. * other RAW, YUV422 8/10 or RGB888, set appropriate media bus format.
  613. */
  614. dt = xcsi2rxss_get_dt(fmt->format.code);
  615. if (dt != xcsi2rxss->datatype && dt != MIPI_CSI2_DT_RAW8) {
  616. dev_dbg(xcsi2rxss->dev, "Unsupported media bus format");
  617. /* set the default format for the data type */
  618. fmt->format.code = xcsi2rxss_get_nth_mbus(xcsi2rxss->datatype,
  619. 0);
  620. }
  621. *__format = fmt->format;
  622. mutex_unlock(&xcsi2rxss->lock);
  623. return 0;
  624. }
  625. static int xcsi2rxss_enum_mbus_code(struct v4l2_subdev *sd,
  626. struct v4l2_subdev_state *sd_state,
  627. struct v4l2_subdev_mbus_code_enum *code)
  628. {
  629. struct xcsi2rxss_state *state = to_xcsi2rxssstate(sd);
  630. u32 dt, n;
  631. int ret = 0;
  632. /* RAW8 dt packets are available in all DT configurations */
  633. if (code->index < 4) {
  634. n = code->index;
  635. dt = MIPI_CSI2_DT_RAW8;
  636. } else if (state->datatype != MIPI_CSI2_DT_RAW8) {
  637. n = code->index - 4;
  638. dt = state->datatype;
  639. } else {
  640. return -EINVAL;
  641. }
  642. code->code = xcsi2rxss_get_nth_mbus(dt, n);
  643. if (!code->code)
  644. ret = -EINVAL;
  645. return ret;
  646. }
  647. /* -----------------------------------------------------------------------------
  648. * Media Operations
  649. */
  650. static const struct media_entity_operations xcsi2rxss_media_ops = {
  651. .link_validate = v4l2_subdev_link_validate
  652. };
  653. static const struct v4l2_subdev_core_ops xcsi2rxss_core_ops = {
  654. .log_status = xcsi2rxss_log_status,
  655. };
  656. static const struct v4l2_subdev_video_ops xcsi2rxss_video_ops = {
  657. .s_stream = xcsi2rxss_s_stream
  658. };
  659. static const struct v4l2_subdev_pad_ops xcsi2rxss_pad_ops = {
  660. .get_fmt = xcsi2rxss_get_format,
  661. .set_fmt = xcsi2rxss_set_format,
  662. .enum_mbus_code = xcsi2rxss_enum_mbus_code,
  663. .link_validate = v4l2_subdev_link_validate_default,
  664. };
  665. static const struct v4l2_subdev_ops xcsi2rxss_ops = {
  666. .core = &xcsi2rxss_core_ops,
  667. .video = &xcsi2rxss_video_ops,
  668. .pad = &xcsi2rxss_pad_ops
  669. };
  670. static const struct v4l2_subdev_internal_ops xcsi2rxss_internal_ops = {
  671. .init_state = xcsi2rxss_init_state,
  672. };
  673. static int xcsi2rxss_parse_of(struct xcsi2rxss_state *xcsi2rxss)
  674. {
  675. struct device *dev = xcsi2rxss->dev;
  676. struct device_node *node = dev->of_node;
  677. struct fwnode_handle *ep;
  678. struct v4l2_fwnode_endpoint vep = {
  679. .bus_type = V4L2_MBUS_CSI2_DPHY
  680. };
  681. bool en_csi_v20, vfb;
  682. int ret;
  683. en_csi_v20 = of_property_read_bool(node, "xlnx,en-csi-v2-0");
  684. if (en_csi_v20)
  685. xcsi2rxss->en_vcx = of_property_read_bool(node, "xlnx,en-vcx");
  686. xcsi2rxss->enable_active_lanes =
  687. of_property_read_bool(node, "xlnx,en-active-lanes");
  688. ret = of_property_read_u32(node, "xlnx,csi-pxl-format",
  689. &xcsi2rxss->datatype);
  690. if (ret < 0) {
  691. dev_err(dev, "missing xlnx,csi-pxl-format property\n");
  692. return ret;
  693. }
  694. switch (xcsi2rxss->datatype) {
  695. case MIPI_CSI2_DT_YUV422_8B:
  696. case MIPI_CSI2_DT_RGB444:
  697. case MIPI_CSI2_DT_RGB555:
  698. case MIPI_CSI2_DT_RGB565:
  699. case MIPI_CSI2_DT_RGB666:
  700. case MIPI_CSI2_DT_RGB888:
  701. case MIPI_CSI2_DT_RAW6:
  702. case MIPI_CSI2_DT_RAW7:
  703. case MIPI_CSI2_DT_RAW8:
  704. case MIPI_CSI2_DT_RAW10:
  705. case MIPI_CSI2_DT_RAW12:
  706. case MIPI_CSI2_DT_RAW14:
  707. break;
  708. case MIPI_CSI2_DT_YUV422_10B:
  709. case MIPI_CSI2_DT_RAW16:
  710. case MIPI_CSI2_DT_RAW20:
  711. if (!en_csi_v20) {
  712. ret = -EINVAL;
  713. dev_dbg(dev, "enable csi v2 for this pixel format");
  714. }
  715. break;
  716. default:
  717. ret = -EINVAL;
  718. }
  719. if (ret < 0) {
  720. dev_err(dev, "invalid csi-pxl-format property!\n");
  721. return ret;
  722. }
  723. vfb = of_property_read_bool(node, "xlnx,vfb");
  724. if (!vfb) {
  725. dev_err(dev, "operation without VFB is not supported\n");
  726. return -EINVAL;
  727. }
  728. ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
  729. XVIP_PAD_SINK, 0,
  730. FWNODE_GRAPH_ENDPOINT_NEXT);
  731. if (!ep) {
  732. dev_err(dev, "no sink port found");
  733. return -EINVAL;
  734. }
  735. ret = v4l2_fwnode_endpoint_parse(ep, &vep);
  736. fwnode_handle_put(ep);
  737. if (ret) {
  738. dev_err(dev, "error parsing sink port");
  739. return ret;
  740. }
  741. dev_dbg(dev, "mipi number lanes = %d\n",
  742. vep.bus.mipi_csi2.num_data_lanes);
  743. xcsi2rxss->max_num_lanes = vep.bus.mipi_csi2.num_data_lanes;
  744. ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
  745. XVIP_PAD_SOURCE, 0,
  746. FWNODE_GRAPH_ENDPOINT_NEXT);
  747. if (!ep) {
  748. dev_err(dev, "no source port found");
  749. return -EINVAL;
  750. }
  751. fwnode_handle_put(ep);
  752. dev_dbg(dev, "vcx %s, %u data lanes (%s), data type 0x%02x\n",
  753. xcsi2rxss->en_vcx ? "enabled" : "disabled",
  754. xcsi2rxss->max_num_lanes,
  755. xcsi2rxss->enable_active_lanes ? "dynamic" : "static",
  756. xcsi2rxss->datatype);
  757. return 0;
  758. }
  759. static int xcsi2rxss_probe(struct platform_device *pdev)
  760. {
  761. struct v4l2_subdev *subdev;
  762. struct xcsi2rxss_state *xcsi2rxss;
  763. int num_clks = ARRAY_SIZE(xcsi2rxss_clks);
  764. struct device *dev = &pdev->dev;
  765. int irq, ret;
  766. xcsi2rxss = devm_kzalloc(dev, sizeof(*xcsi2rxss), GFP_KERNEL);
  767. if (!xcsi2rxss)
  768. return -ENOMEM;
  769. xcsi2rxss->dev = dev;
  770. xcsi2rxss->clks = devm_kmemdup(dev, xcsi2rxss_clks,
  771. sizeof(xcsi2rxss_clks), GFP_KERNEL);
  772. if (!xcsi2rxss->clks)
  773. return -ENOMEM;
  774. /* Reset GPIO */
  775. xcsi2rxss->rst_gpio = devm_gpiod_get_optional(dev, "video-reset",
  776. GPIOD_OUT_HIGH);
  777. if (IS_ERR(xcsi2rxss->rst_gpio))
  778. return dev_err_probe(dev, PTR_ERR(xcsi2rxss->rst_gpio),
  779. "Video Reset GPIO not setup in DT\n");
  780. ret = xcsi2rxss_parse_of(xcsi2rxss);
  781. if (ret < 0)
  782. return ret;
  783. xcsi2rxss->iomem = devm_platform_ioremap_resource(pdev, 0);
  784. if (IS_ERR(xcsi2rxss->iomem))
  785. return PTR_ERR(xcsi2rxss->iomem);
  786. irq = platform_get_irq(pdev, 0);
  787. if (irq < 0)
  788. return irq;
  789. ret = devm_request_threaded_irq(dev, irq, NULL,
  790. xcsi2rxss_irq_handler, IRQF_ONESHOT,
  791. dev_name(dev), xcsi2rxss);
  792. if (ret) {
  793. dev_err(dev, "Err = %d Interrupt handler reg failed!\n", ret);
  794. return ret;
  795. }
  796. ret = clk_bulk_get(dev, num_clks, xcsi2rxss->clks);
  797. if (ret)
  798. return ret;
  799. /* TODO: Enable/disable clocks at stream on/off time. */
  800. ret = clk_bulk_prepare_enable(num_clks, xcsi2rxss->clks);
  801. if (ret)
  802. goto err_clk_put;
  803. mutex_init(&xcsi2rxss->lock);
  804. xcsi2rxss_hard_reset(xcsi2rxss);
  805. xcsi2rxss_soft_reset(xcsi2rxss);
  806. /* Initialize V4L2 subdevice and media entity */
  807. xcsi2rxss->pads[XVIP_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  808. xcsi2rxss->pads[XVIP_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  809. /* Initialize the default format */
  810. xcsi2rxss->default_format.code =
  811. xcsi2rxss_get_nth_mbus(xcsi2rxss->datatype, 0);
  812. xcsi2rxss->default_format.field = V4L2_FIELD_NONE;
  813. xcsi2rxss->default_format.colorspace = V4L2_COLORSPACE_SRGB;
  814. xcsi2rxss->default_format.width = XCSI_DEFAULT_WIDTH;
  815. xcsi2rxss->default_format.height = XCSI_DEFAULT_HEIGHT;
  816. xcsi2rxss->format = xcsi2rxss->default_format;
  817. /* Initialize V4L2 subdevice and media entity */
  818. subdev = &xcsi2rxss->subdev;
  819. v4l2_subdev_init(subdev, &xcsi2rxss_ops);
  820. subdev->internal_ops = &xcsi2rxss_internal_ops;
  821. subdev->dev = dev;
  822. strscpy(subdev->name, dev_name(dev), sizeof(subdev->name));
  823. subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
  824. subdev->entity.ops = &xcsi2rxss_media_ops;
  825. v4l2_set_subdevdata(subdev, xcsi2rxss);
  826. ret = media_entity_pads_init(&subdev->entity, XCSI_MEDIA_PADS,
  827. xcsi2rxss->pads);
  828. if (ret < 0)
  829. goto error;
  830. platform_set_drvdata(pdev, xcsi2rxss);
  831. ret = v4l2_async_register_subdev(subdev);
  832. if (ret < 0) {
  833. dev_err(dev, "failed to register subdev\n");
  834. goto error;
  835. }
  836. return 0;
  837. error:
  838. media_entity_cleanup(&subdev->entity);
  839. mutex_destroy(&xcsi2rxss->lock);
  840. clk_bulk_disable_unprepare(num_clks, xcsi2rxss->clks);
  841. err_clk_put:
  842. clk_bulk_put(num_clks, xcsi2rxss->clks);
  843. return ret;
  844. }
  845. static void xcsi2rxss_remove(struct platform_device *pdev)
  846. {
  847. struct xcsi2rxss_state *xcsi2rxss = platform_get_drvdata(pdev);
  848. struct v4l2_subdev *subdev = &xcsi2rxss->subdev;
  849. int num_clks = ARRAY_SIZE(xcsi2rxss_clks);
  850. v4l2_async_unregister_subdev(subdev);
  851. media_entity_cleanup(&subdev->entity);
  852. mutex_destroy(&xcsi2rxss->lock);
  853. clk_bulk_disable_unprepare(num_clks, xcsi2rxss->clks);
  854. clk_bulk_put(num_clks, xcsi2rxss->clks);
  855. }
  856. static const struct of_device_id xcsi2rxss_of_id_table[] = {
  857. { .compatible = "xlnx,mipi-csi2-rx-subsystem-5.0", },
  858. { }
  859. };
  860. MODULE_DEVICE_TABLE(of, xcsi2rxss_of_id_table);
  861. static struct platform_driver xcsi2rxss_driver = {
  862. .driver = {
  863. .name = "xilinx-csi2rxss",
  864. .of_match_table = xcsi2rxss_of_id_table,
  865. },
  866. .probe = xcsi2rxss_probe,
  867. .remove_new = xcsi2rxss_remove,
  868. };
  869. module_platform_driver(xcsi2rxss_driver);
  870. MODULE_AUTHOR("Vishal Sagar <vsagar@xilinx.com>");
  871. MODULE_DESCRIPTION("Xilinx MIPI CSI-2 Rx Subsystem Driver");
  872. MODULE_LICENSE("GPL v2");