imx7-media-csi.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * V4L2 Capture CSI Subdev for Freescale i.MX6UL/L / i.MX7 SOC
  4. *
  5. * Copyright (c) 2019 Linaro Ltd
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/completion.h>
  9. #include <linux/container_of.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/math.h>
  20. #include <linux/minmax.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/property.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/string.h>
  29. #include <linux/timekeeping.h>
  30. #include <linux/types.h>
  31. #include <media/media-device.h>
  32. #include <media/media-entity.h>
  33. #include <media/v4l2-async.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-dev.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-fh.h>
  38. #include <media/v4l2-ioctl.h>
  39. #include <media/v4l2-mc.h>
  40. #include <media/v4l2-subdev.h>
  41. #include <media/videobuf2-core.h>
  42. #include <media/videobuf2-dma-contig.h>
  43. #include <media/videobuf2-v4l2.h>
  44. #define IMX7_CSI_PAD_SINK 0
  45. #define IMX7_CSI_PAD_SRC 1
  46. #define IMX7_CSI_PADS_NUM 2
  47. /* csi control reg 1 */
  48. #define BIT_SWAP16_EN BIT(31)
  49. #define BIT_EXT_VSYNC BIT(30)
  50. #define BIT_EOF_INT_EN BIT(29)
  51. #define BIT_PRP_IF_EN BIT(28)
  52. #define BIT_CCIR_MODE BIT(27)
  53. #define BIT_COF_INT_EN BIT(26)
  54. #define BIT_SF_OR_INTEN BIT(25)
  55. #define BIT_RF_OR_INTEN BIT(24)
  56. #define BIT_SFF_DMA_DONE_INTEN BIT(22)
  57. #define BIT_STATFF_INTEN BIT(21)
  58. #define BIT_FB2_DMA_DONE_INTEN BIT(20)
  59. #define BIT_FB1_DMA_DONE_INTEN BIT(19)
  60. #define BIT_RXFF_INTEN BIT(18)
  61. #define BIT_SOF_POL BIT(17)
  62. #define BIT_SOF_INTEN BIT(16)
  63. #define BIT_MCLKDIV(n) ((n) << 12)
  64. #define BIT_MCLKDIV_MASK (0xf << 12)
  65. #define BIT_HSYNC_POL BIT(11)
  66. #define BIT_CCIR_EN BIT(10)
  67. #define BIT_MCLKEN BIT(9)
  68. #define BIT_FCC BIT(8)
  69. #define BIT_PACK_DIR BIT(7)
  70. #define BIT_CLR_STATFIFO BIT(6)
  71. #define BIT_CLR_RXFIFO BIT(5)
  72. #define BIT_GCLK_MODE BIT(4)
  73. #define BIT_INV_DATA BIT(3)
  74. #define BIT_INV_PCLK BIT(2)
  75. #define BIT_REDGE BIT(1)
  76. #define BIT_PIXEL_BIT BIT(0)
  77. /* control reg 2 */
  78. #define BIT_DMA_BURST_TYPE_RFF_INCR4 (1 << 30)
  79. #define BIT_DMA_BURST_TYPE_RFF_INCR8 (2 << 30)
  80. #define BIT_DMA_BURST_TYPE_RFF_INCR16 (3 << 30)
  81. #define BIT_DMA_BURST_TYPE_RFF_MASK (3 << 30)
  82. /* control reg 3 */
  83. #define BIT_FRMCNT(n) ((n) << 16)
  84. #define BIT_FRMCNT_MASK (0xffff << 16)
  85. #define BIT_FRMCNT_RST BIT(15)
  86. #define BIT_DMA_REFLASH_RFF BIT(14)
  87. #define BIT_DMA_REFLASH_SFF BIT(13)
  88. #define BIT_DMA_REQ_EN_RFF BIT(12)
  89. #define BIT_DMA_REQ_EN_SFF BIT(11)
  90. #define BIT_STATFF_LEVEL(n) ((n) << 8)
  91. #define BIT_STATFF_LEVEL_MASK (0x7 << 8)
  92. #define BIT_HRESP_ERR_EN BIT(7)
  93. #define BIT_RXFF_LEVEL(n) ((n) << 4)
  94. #define BIT_RXFF_LEVEL_MASK (0x7 << 4)
  95. #define BIT_TWO_8BIT_SENSOR BIT(3)
  96. #define BIT_ZERO_PACK_EN BIT(2)
  97. #define BIT_ECC_INT_EN BIT(1)
  98. #define BIT_ECC_AUTO_EN BIT(0)
  99. /* csi status reg */
  100. #define BIT_ADDR_CH_ERR_INT BIT(28)
  101. #define BIT_FIELD0_INT BIT(27)
  102. #define BIT_FIELD1_INT BIT(26)
  103. #define BIT_SFF_OR_INT BIT(25)
  104. #define BIT_RFF_OR_INT BIT(24)
  105. #define BIT_DMA_TSF_DONE_SFF BIT(22)
  106. #define BIT_STATFF_INT BIT(21)
  107. #define BIT_DMA_TSF_DONE_FB2 BIT(20)
  108. #define BIT_DMA_TSF_DONE_FB1 BIT(19)
  109. #define BIT_RXFF_INT BIT(18)
  110. #define BIT_EOF_INT BIT(17)
  111. #define BIT_SOF_INT BIT(16)
  112. #define BIT_F2_INT BIT(15)
  113. #define BIT_F1_INT BIT(14)
  114. #define BIT_COF_INT BIT(13)
  115. #define BIT_HRESP_ERR_INT BIT(7)
  116. #define BIT_ECC_INT BIT(1)
  117. #define BIT_DRDY BIT(0)
  118. /* csi image parameter reg */
  119. #define BIT_IMAGE_WIDTH(n) ((n) << 16)
  120. #define BIT_IMAGE_HEIGHT(n) (n)
  121. /* csi control reg 18 */
  122. #define BIT_CSI_HW_ENABLE BIT(31)
  123. #define BIT_MIPI_DATA_FORMAT_RAW8 (0x2a << 25)
  124. #define BIT_MIPI_DATA_FORMAT_RAW10 (0x2b << 25)
  125. #define BIT_MIPI_DATA_FORMAT_RAW12 (0x2c << 25)
  126. #define BIT_MIPI_DATA_FORMAT_RAW14 (0x2d << 25)
  127. #define BIT_MIPI_DATA_FORMAT_YUV422_8B (0x1e << 25)
  128. #define BIT_MIPI_DATA_FORMAT_MASK (0x3f << 25)
  129. #define BIT_DATA_FROM_MIPI BIT(22)
  130. #define BIT_MIPI_YU_SWAP BIT(21)
  131. #define BIT_MIPI_DOUBLE_CMPNT BIT(20)
  132. #define BIT_MASK_OPTION_FIRST_FRAME (0 << 18)
  133. #define BIT_MASK_OPTION_CSI_EN (1 << 18)
  134. #define BIT_MASK_OPTION_SECOND_FRAME (2 << 18)
  135. #define BIT_MASK_OPTION_ON_DATA (3 << 18)
  136. #define BIT_BASEADDR_CHG_ERR_EN BIT(9)
  137. #define BIT_BASEADDR_SWITCH_SEL BIT(5)
  138. #define BIT_BASEADDR_SWITCH_EN BIT(4)
  139. #define BIT_PARALLEL24_EN BIT(3)
  140. #define BIT_DEINTERLACE_EN BIT(2)
  141. #define BIT_TVDECODER_IN_EN BIT(1)
  142. #define BIT_NTSC_EN BIT(0)
  143. #define CSI_MCLK_VF 1
  144. #define CSI_MCLK_ENC 2
  145. #define CSI_MCLK_RAW 4
  146. #define CSI_MCLK_I2C 8
  147. #define CSI_CSICR1 0x00
  148. #define CSI_CSICR2 0x04
  149. #define CSI_CSICR3 0x08
  150. #define CSI_STATFIFO 0x0c
  151. #define CSI_CSIRXFIFO 0x10
  152. #define CSI_CSIRXCNT 0x14
  153. #define CSI_CSISR 0x18
  154. #define CSI_CSIDBG 0x1c
  155. #define CSI_CSIDMASA_STATFIFO 0x20
  156. #define CSI_CSIDMATS_STATFIFO 0x24
  157. #define CSI_CSIDMASA_FB1 0x28
  158. #define CSI_CSIDMASA_FB2 0x2c
  159. #define CSI_CSIFBUF_PARA 0x30
  160. #define CSI_CSIIMAG_PARA 0x34
  161. #define CSI_CSICR18 0x48
  162. #define CSI_CSICR19 0x4c
  163. #define IMX7_CSI_VIDEO_NAME "imx-capture"
  164. /* In bytes, per queue */
  165. #define IMX7_CSI_VIDEO_MEM_LIMIT SZ_512M
  166. #define IMX7_CSI_VIDEO_EOF_TIMEOUT 2000
  167. #define IMX7_CSI_DEF_MBUS_CODE MEDIA_BUS_FMT_UYVY8_2X8
  168. #define IMX7_CSI_DEF_PIX_FORMAT V4L2_PIX_FMT_UYVY
  169. #define IMX7_CSI_DEF_PIX_WIDTH 640
  170. #define IMX7_CSI_DEF_PIX_HEIGHT 480
  171. enum imx_csi_model {
  172. IMX7_CSI_IMX7 = 0,
  173. IMX7_CSI_IMX8MQ,
  174. };
  175. struct imx7_csi_pixfmt {
  176. /* the in-memory FourCC pixel format */
  177. u32 fourcc;
  178. /*
  179. * the set of equivalent media bus codes for the fourcc.
  180. * NOTE! codes pointer is NULL for in-memory-only formats.
  181. */
  182. const u32 *codes;
  183. int bpp; /* total bpp */
  184. bool yuv;
  185. };
  186. struct imx7_csi_vb2_buffer {
  187. struct vb2_v4l2_buffer vbuf;
  188. struct list_head list;
  189. };
  190. static inline struct imx7_csi_vb2_buffer *
  191. to_imx7_csi_vb2_buffer(struct vb2_buffer *vb)
  192. {
  193. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  194. return container_of(vbuf, struct imx7_csi_vb2_buffer, vbuf);
  195. }
  196. struct imx7_csi_dma_buf {
  197. void *virt;
  198. dma_addr_t dma_addr;
  199. unsigned long len;
  200. };
  201. struct imx7_csi {
  202. struct device *dev;
  203. /* Resources and locks */
  204. void __iomem *regbase;
  205. int irq;
  206. struct clk *mclk;
  207. spinlock_t irqlock; /* Protects last_eof */
  208. /* Media and V4L2 device */
  209. struct media_device mdev;
  210. struct v4l2_device v4l2_dev;
  211. struct v4l2_async_notifier notifier;
  212. struct media_pipeline pipe;
  213. struct v4l2_subdev *src_sd;
  214. bool is_csi2;
  215. /* V4L2 subdev */
  216. struct v4l2_subdev sd;
  217. struct media_pad pad[IMX7_CSI_PADS_NUM];
  218. /* Video device */
  219. struct video_device *vdev; /* Video device */
  220. struct media_pad vdev_pad; /* Video device pad */
  221. struct v4l2_pix_format vdev_fmt; /* The user format */
  222. const struct imx7_csi_pixfmt *vdev_cc;
  223. struct v4l2_rect vdev_compose; /* The compose rectangle */
  224. struct mutex vdev_mutex; /* Protect vdev operations */
  225. struct vb2_queue q; /* The videobuf2 queue */
  226. struct list_head ready_q; /* List of queued buffers */
  227. spinlock_t q_lock; /* Protect ready_q */
  228. /* Buffers and streaming state */
  229. struct imx7_csi_vb2_buffer *active_vb2_buf[2];
  230. struct imx7_csi_dma_buf underrun_buf;
  231. bool is_streaming;
  232. int buf_num;
  233. u32 frame_sequence;
  234. bool last_eof;
  235. struct completion last_eof_completion;
  236. enum imx_csi_model model;
  237. };
  238. static struct imx7_csi *
  239. imx7_csi_notifier_to_dev(struct v4l2_async_notifier *n)
  240. {
  241. return container_of(n, struct imx7_csi, notifier);
  242. }
  243. /* -----------------------------------------------------------------------------
  244. * Hardware Configuration
  245. */
  246. static u32 imx7_csi_reg_read(struct imx7_csi *csi, unsigned int offset)
  247. {
  248. return readl(csi->regbase + offset);
  249. }
  250. static void imx7_csi_reg_write(struct imx7_csi *csi, unsigned int value,
  251. unsigned int offset)
  252. {
  253. writel(value, csi->regbase + offset);
  254. }
  255. static u32 imx7_csi_irq_clear(struct imx7_csi *csi)
  256. {
  257. u32 isr;
  258. isr = imx7_csi_reg_read(csi, CSI_CSISR);
  259. imx7_csi_reg_write(csi, isr, CSI_CSISR);
  260. return isr;
  261. }
  262. static void imx7_csi_init_default(struct imx7_csi *csi)
  263. {
  264. imx7_csi_reg_write(csi, BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE |
  265. BIT_HSYNC_POL | BIT_FCC | BIT_MCLKDIV(1) |
  266. BIT_MCLKEN, CSI_CSICR1);
  267. imx7_csi_reg_write(csi, 0, CSI_CSICR2);
  268. imx7_csi_reg_write(csi, BIT_FRMCNT_RST, CSI_CSICR3);
  269. imx7_csi_reg_write(csi, BIT_IMAGE_WIDTH(IMX7_CSI_DEF_PIX_WIDTH) |
  270. BIT_IMAGE_HEIGHT(IMX7_CSI_DEF_PIX_HEIGHT),
  271. CSI_CSIIMAG_PARA);
  272. imx7_csi_reg_write(csi, BIT_DMA_REFLASH_RFF, CSI_CSICR3);
  273. }
  274. static void imx7_csi_hw_enable_irq(struct imx7_csi *csi)
  275. {
  276. u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1);
  277. cr1 |= BIT_RFF_OR_INT;
  278. cr1 |= BIT_FB1_DMA_DONE_INTEN;
  279. cr1 |= BIT_FB2_DMA_DONE_INTEN;
  280. imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
  281. }
  282. static void imx7_csi_hw_disable_irq(struct imx7_csi *csi)
  283. {
  284. u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1);
  285. cr1 &= ~BIT_RFF_OR_INT;
  286. cr1 &= ~BIT_FB1_DMA_DONE_INTEN;
  287. cr1 &= ~BIT_FB2_DMA_DONE_INTEN;
  288. imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
  289. }
  290. static void imx7_csi_hw_enable(struct imx7_csi *csi)
  291. {
  292. u32 cr = imx7_csi_reg_read(csi, CSI_CSICR18);
  293. cr |= BIT_CSI_HW_ENABLE;
  294. imx7_csi_reg_write(csi, cr, CSI_CSICR18);
  295. }
  296. static void imx7_csi_hw_disable(struct imx7_csi *csi)
  297. {
  298. u32 cr = imx7_csi_reg_read(csi, CSI_CSICR18);
  299. cr &= ~BIT_CSI_HW_ENABLE;
  300. imx7_csi_reg_write(csi, cr, CSI_CSICR18);
  301. }
  302. static void imx7_csi_dma_reflash(struct imx7_csi *csi)
  303. {
  304. u32 cr3;
  305. cr3 = imx7_csi_reg_read(csi, CSI_CSICR3);
  306. cr3 |= BIT_DMA_REFLASH_RFF;
  307. imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
  308. }
  309. static void imx7_csi_rx_fifo_clear(struct imx7_csi *csi)
  310. {
  311. u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1) & ~BIT_FCC;
  312. imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
  313. imx7_csi_reg_write(csi, cr1 | BIT_CLR_RXFIFO, CSI_CSICR1);
  314. imx7_csi_reg_write(csi, cr1 | BIT_FCC, CSI_CSICR1);
  315. }
  316. static void imx7_csi_dmareq_rff_enable(struct imx7_csi *csi)
  317. {
  318. u32 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3);
  319. cr3 |= BIT_DMA_REQ_EN_RFF;
  320. cr3 |= BIT_HRESP_ERR_EN;
  321. cr3 &= ~BIT_RXFF_LEVEL_MASK;
  322. cr3 |= BIT_RXFF_LEVEL(2);
  323. imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
  324. }
  325. static void imx7_csi_dmareq_rff_disable(struct imx7_csi *csi)
  326. {
  327. u32 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3);
  328. cr3 &= ~BIT_DMA_REQ_EN_RFF;
  329. cr3 &= ~BIT_HRESP_ERR_EN;
  330. imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
  331. }
  332. static void imx7_csi_update_buf(struct imx7_csi *csi, dma_addr_t dma_addr,
  333. int buf_num)
  334. {
  335. if (buf_num == 1)
  336. imx7_csi_reg_write(csi, dma_addr, CSI_CSIDMASA_FB2);
  337. else
  338. imx7_csi_reg_write(csi, dma_addr, CSI_CSIDMASA_FB1);
  339. }
  340. static struct imx7_csi_vb2_buffer *imx7_csi_video_next_buf(struct imx7_csi *csi);
  341. static void imx7_csi_setup_vb2_buf(struct imx7_csi *csi)
  342. {
  343. struct imx7_csi_vb2_buffer *buf;
  344. struct vb2_buffer *vb2_buf;
  345. int i;
  346. for (i = 0; i < 2; i++) {
  347. dma_addr_t dma_addr;
  348. buf = imx7_csi_video_next_buf(csi);
  349. if (buf) {
  350. csi->active_vb2_buf[i] = buf;
  351. vb2_buf = &buf->vbuf.vb2_buf;
  352. dma_addr = vb2_dma_contig_plane_dma_addr(vb2_buf, 0);
  353. } else {
  354. csi->active_vb2_buf[i] = NULL;
  355. dma_addr = csi->underrun_buf.dma_addr;
  356. }
  357. imx7_csi_update_buf(csi, dma_addr, i);
  358. }
  359. }
  360. static void imx7_csi_dma_unsetup_vb2_buf(struct imx7_csi *csi,
  361. enum vb2_buffer_state return_status)
  362. {
  363. struct imx7_csi_vb2_buffer *buf;
  364. int i;
  365. /* return any remaining active frames with return_status */
  366. for (i = 0; i < 2; i++) {
  367. buf = csi->active_vb2_buf[i];
  368. if (buf) {
  369. struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
  370. vb->timestamp = ktime_get_ns();
  371. vb2_buffer_done(vb, return_status);
  372. csi->active_vb2_buf[i] = NULL;
  373. }
  374. }
  375. }
  376. static void imx7_csi_free_dma_buf(struct imx7_csi *csi,
  377. struct imx7_csi_dma_buf *buf)
  378. {
  379. if (buf->virt)
  380. dma_free_coherent(csi->dev, buf->len, buf->virt, buf->dma_addr);
  381. buf->virt = NULL;
  382. buf->dma_addr = 0;
  383. }
  384. static int imx7_csi_alloc_dma_buf(struct imx7_csi *csi,
  385. struct imx7_csi_dma_buf *buf, int size)
  386. {
  387. imx7_csi_free_dma_buf(csi, buf);
  388. buf->len = PAGE_ALIGN(size);
  389. buf->virt = dma_alloc_coherent(csi->dev, buf->len, &buf->dma_addr,
  390. GFP_DMA | GFP_KERNEL);
  391. if (!buf->virt)
  392. return -ENOMEM;
  393. return 0;
  394. }
  395. static int imx7_csi_dma_setup(struct imx7_csi *csi)
  396. {
  397. int ret;
  398. ret = imx7_csi_alloc_dma_buf(csi, &csi->underrun_buf,
  399. csi->vdev_fmt.sizeimage);
  400. if (ret < 0) {
  401. v4l2_warn(&csi->sd, "consider increasing the CMA area\n");
  402. return ret;
  403. }
  404. csi->frame_sequence = 0;
  405. csi->last_eof = false;
  406. init_completion(&csi->last_eof_completion);
  407. imx7_csi_setup_vb2_buf(csi);
  408. return 0;
  409. }
  410. static void imx7_csi_dma_cleanup(struct imx7_csi *csi,
  411. enum vb2_buffer_state return_status)
  412. {
  413. imx7_csi_dma_unsetup_vb2_buf(csi, return_status);
  414. imx7_csi_free_dma_buf(csi, &csi->underrun_buf);
  415. }
  416. static void imx7_csi_dma_stop(struct imx7_csi *csi)
  417. {
  418. unsigned long timeout_jiffies;
  419. unsigned long flags;
  420. int ret;
  421. /* mark next EOF interrupt as the last before stream off */
  422. spin_lock_irqsave(&csi->irqlock, flags);
  423. csi->last_eof = true;
  424. spin_unlock_irqrestore(&csi->irqlock, flags);
  425. /*
  426. * and then wait for interrupt handler to mark completion.
  427. */
  428. timeout_jiffies = msecs_to_jiffies(IMX7_CSI_VIDEO_EOF_TIMEOUT);
  429. ret = wait_for_completion_timeout(&csi->last_eof_completion,
  430. timeout_jiffies);
  431. if (ret == 0)
  432. v4l2_warn(&csi->sd, "wait last EOF timeout\n");
  433. imx7_csi_hw_disable_irq(csi);
  434. }
  435. static void imx7_csi_configure(struct imx7_csi *csi,
  436. struct v4l2_subdev_state *sd_state)
  437. {
  438. struct v4l2_pix_format *out_pix = &csi->vdev_fmt;
  439. int width = out_pix->width;
  440. u32 stride = 0;
  441. u32 cr3 = BIT_FRMCNT_RST;
  442. u32 cr1, cr18;
  443. cr18 = imx7_csi_reg_read(csi, CSI_CSICR18);
  444. cr18 &= ~(BIT_CSI_HW_ENABLE | BIT_MIPI_DATA_FORMAT_MASK |
  445. BIT_DATA_FROM_MIPI | BIT_MIPI_DOUBLE_CMPNT |
  446. BIT_BASEADDR_CHG_ERR_EN | BIT_BASEADDR_SWITCH_SEL |
  447. BIT_BASEADDR_SWITCH_EN | BIT_DEINTERLACE_EN);
  448. if (out_pix->field == V4L2_FIELD_INTERLACED) {
  449. cr18 |= BIT_DEINTERLACE_EN;
  450. stride = out_pix->width;
  451. }
  452. if (!csi->is_csi2) {
  453. cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE | BIT_HSYNC_POL
  454. | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN;
  455. cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
  456. BIT_BASEADDR_CHG_ERR_EN;
  457. if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY ||
  458. out_pix->pixelformat == V4L2_PIX_FMT_YUYV)
  459. width *= 2;
  460. } else {
  461. const struct v4l2_mbus_framefmt *sink_fmt;
  462. sink_fmt = v4l2_subdev_state_get_format(sd_state,
  463. IMX7_CSI_PAD_SINK);
  464. cr1 = BIT_SOF_POL | BIT_REDGE | BIT_HSYNC_POL | BIT_FCC
  465. | BIT_MCLKDIV(1) | BIT_MCLKEN;
  466. cr18 |= BIT_DATA_FROM_MIPI;
  467. switch (sink_fmt->code) {
  468. case MEDIA_BUS_FMT_Y8_1X8:
  469. case MEDIA_BUS_FMT_SBGGR8_1X8:
  470. case MEDIA_BUS_FMT_SGBRG8_1X8:
  471. case MEDIA_BUS_FMT_SGRBG8_1X8:
  472. case MEDIA_BUS_FMT_SRGGB8_1X8:
  473. cr18 |= BIT_MIPI_DATA_FORMAT_RAW8;
  474. break;
  475. case MEDIA_BUS_FMT_Y10_1X10:
  476. case MEDIA_BUS_FMT_SBGGR10_1X10:
  477. case MEDIA_BUS_FMT_SGBRG10_1X10:
  478. case MEDIA_BUS_FMT_SGRBG10_1X10:
  479. case MEDIA_BUS_FMT_SRGGB10_1X10:
  480. cr3 |= BIT_TWO_8BIT_SENSOR;
  481. cr18 |= BIT_MIPI_DATA_FORMAT_RAW10;
  482. break;
  483. case MEDIA_BUS_FMT_Y12_1X12:
  484. case MEDIA_BUS_FMT_SBGGR12_1X12:
  485. case MEDIA_BUS_FMT_SGBRG12_1X12:
  486. case MEDIA_BUS_FMT_SGRBG12_1X12:
  487. case MEDIA_BUS_FMT_SRGGB12_1X12:
  488. cr3 |= BIT_TWO_8BIT_SENSOR;
  489. cr18 |= BIT_MIPI_DATA_FORMAT_RAW12;
  490. break;
  491. case MEDIA_BUS_FMT_Y14_1X14:
  492. case MEDIA_BUS_FMT_SBGGR14_1X14:
  493. case MEDIA_BUS_FMT_SGBRG14_1X14:
  494. case MEDIA_BUS_FMT_SGRBG14_1X14:
  495. case MEDIA_BUS_FMT_SRGGB14_1X14:
  496. cr3 |= BIT_TWO_8BIT_SENSOR;
  497. cr18 |= BIT_MIPI_DATA_FORMAT_RAW14;
  498. break;
  499. /*
  500. * The CSI bridge has a 16-bit input bus. Depending on the
  501. * connected source, data may be transmitted with 8 or 10 bits
  502. * per clock sample (in bits [9:2] or [9:0] respectively) or
  503. * with 16 bits per clock sample (in bits [15:0]). The data is
  504. * then packed into a 32-bit FIFO (as shown in figure 13-11 of
  505. * the i.MX8MM reference manual rev. 3).
  506. *
  507. * The data packing in a 32-bit FIFO input word is controlled by
  508. * the CR3 TWO_8BIT_SENSOR field (also known as SENSOR_16BITS in
  509. * the i.MX8MM reference manual). When set to 0, data packing
  510. * groups four 8-bit input samples (bits [9:2]). When set to 1,
  511. * data packing groups two 16-bit input samples (bits [15:0]).
  512. *
  513. * The register field CR18 MIPI_DOUBLE_CMPNT also needs to be
  514. * configured according to the input format for YUV 4:2:2 data.
  515. * The field controls the gasket between the CSI-2 receiver and
  516. * the CSI bridge. On i.MX7 and i.MX8MM, the field must be set
  517. * to 1 when the CSIS outputs 16-bit samples. On i.MX8MQ, the
  518. * gasket ignores the MIPI_DOUBLE_CMPNT bit and YUV 4:2:2 always
  519. * uses 16-bit samples. Setting MIPI_DOUBLE_CMPNT in that case
  520. * has no effect, but doesn't cause any issue.
  521. */
  522. case MEDIA_BUS_FMT_UYVY8_2X8:
  523. case MEDIA_BUS_FMT_YUYV8_2X8:
  524. cr18 |= BIT_MIPI_DATA_FORMAT_YUV422_8B;
  525. break;
  526. case MEDIA_BUS_FMT_UYVY8_1X16:
  527. case MEDIA_BUS_FMT_YUYV8_1X16:
  528. cr3 |= BIT_TWO_8BIT_SENSOR;
  529. cr18 |= BIT_MIPI_DATA_FORMAT_YUV422_8B |
  530. BIT_MIPI_DOUBLE_CMPNT;
  531. break;
  532. }
  533. }
  534. imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
  535. imx7_csi_reg_write(csi, BIT_DMA_BURST_TYPE_RFF_INCR16, CSI_CSICR2);
  536. imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
  537. imx7_csi_reg_write(csi, cr18, CSI_CSICR18);
  538. imx7_csi_reg_write(csi, (width * out_pix->height) >> 2, CSI_CSIRXCNT);
  539. imx7_csi_reg_write(csi, BIT_IMAGE_WIDTH(width) |
  540. BIT_IMAGE_HEIGHT(out_pix->height),
  541. CSI_CSIIMAG_PARA);
  542. imx7_csi_reg_write(csi, stride, CSI_CSIFBUF_PARA);
  543. }
  544. static int imx7_csi_init(struct imx7_csi *csi,
  545. struct v4l2_subdev_state *sd_state)
  546. {
  547. int ret;
  548. ret = clk_prepare_enable(csi->mclk);
  549. if (ret < 0)
  550. return ret;
  551. imx7_csi_configure(csi, sd_state);
  552. ret = imx7_csi_dma_setup(csi);
  553. if (ret < 0) {
  554. clk_disable_unprepare(csi->mclk);
  555. return ret;
  556. }
  557. return 0;
  558. }
  559. static void imx7_csi_deinit(struct imx7_csi *csi,
  560. enum vb2_buffer_state return_status)
  561. {
  562. imx7_csi_dma_cleanup(csi, return_status);
  563. imx7_csi_init_default(csi);
  564. imx7_csi_dmareq_rff_disable(csi);
  565. clk_disable_unprepare(csi->mclk);
  566. }
  567. static void imx7_csi_baseaddr_switch_on_second_frame(struct imx7_csi *csi)
  568. {
  569. u32 cr18 = imx7_csi_reg_read(csi, CSI_CSICR18);
  570. cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
  571. BIT_BASEADDR_CHG_ERR_EN;
  572. cr18 |= BIT_MASK_OPTION_SECOND_FRAME;
  573. imx7_csi_reg_write(csi, cr18, CSI_CSICR18);
  574. }
  575. static void imx7_csi_enable(struct imx7_csi *csi)
  576. {
  577. /* Clear the Rx FIFO and reflash the DMA controller. */
  578. imx7_csi_rx_fifo_clear(csi);
  579. imx7_csi_dma_reflash(csi);
  580. usleep_range(2000, 3000);
  581. /* Clear and enable the interrupts. */
  582. imx7_csi_irq_clear(csi);
  583. imx7_csi_hw_enable_irq(csi);
  584. /* Enable the RxFIFO DMA and the CSI. */
  585. imx7_csi_dmareq_rff_enable(csi);
  586. imx7_csi_hw_enable(csi);
  587. if (csi->model == IMX7_CSI_IMX8MQ)
  588. imx7_csi_baseaddr_switch_on_second_frame(csi);
  589. }
  590. static void imx7_csi_disable(struct imx7_csi *csi)
  591. {
  592. imx7_csi_dma_stop(csi);
  593. imx7_csi_dmareq_rff_disable(csi);
  594. imx7_csi_hw_disable_irq(csi);
  595. imx7_csi_hw_disable(csi);
  596. }
  597. /* -----------------------------------------------------------------------------
  598. * Interrupt Handling
  599. */
  600. static void imx7_csi_error_recovery(struct imx7_csi *csi)
  601. {
  602. imx7_csi_hw_disable(csi);
  603. imx7_csi_rx_fifo_clear(csi);
  604. imx7_csi_dma_reflash(csi);
  605. imx7_csi_hw_enable(csi);
  606. }
  607. static void imx7_csi_vb2_buf_done(struct imx7_csi *csi)
  608. {
  609. struct imx7_csi_vb2_buffer *done, *next;
  610. struct vb2_buffer *vb;
  611. dma_addr_t dma_addr;
  612. done = csi->active_vb2_buf[csi->buf_num];
  613. if (done) {
  614. done->vbuf.field = csi->vdev_fmt.field;
  615. done->vbuf.sequence = csi->frame_sequence;
  616. vb = &done->vbuf.vb2_buf;
  617. vb->timestamp = ktime_get_ns();
  618. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  619. }
  620. csi->frame_sequence++;
  621. /* get next queued buffer */
  622. next = imx7_csi_video_next_buf(csi);
  623. if (next) {
  624. dma_addr = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
  625. csi->active_vb2_buf[csi->buf_num] = next;
  626. } else {
  627. dma_addr = csi->underrun_buf.dma_addr;
  628. csi->active_vb2_buf[csi->buf_num] = NULL;
  629. }
  630. imx7_csi_update_buf(csi, dma_addr, csi->buf_num);
  631. }
  632. static irqreturn_t imx7_csi_irq_handler(int irq, void *data)
  633. {
  634. struct imx7_csi *csi = data;
  635. u32 status;
  636. spin_lock(&csi->irqlock);
  637. status = imx7_csi_irq_clear(csi);
  638. if (status & BIT_RFF_OR_INT) {
  639. dev_warn(csi->dev, "Rx fifo overflow\n");
  640. imx7_csi_error_recovery(csi);
  641. }
  642. if (status & BIT_HRESP_ERR_INT) {
  643. dev_warn(csi->dev, "Hresponse error detected\n");
  644. imx7_csi_error_recovery(csi);
  645. }
  646. if (status & BIT_ADDR_CH_ERR_INT) {
  647. imx7_csi_hw_disable(csi);
  648. imx7_csi_dma_reflash(csi);
  649. imx7_csi_hw_enable(csi);
  650. }
  651. if ((status & BIT_DMA_TSF_DONE_FB1) &&
  652. (status & BIT_DMA_TSF_DONE_FB2)) {
  653. /*
  654. * For both FB1 and FB2 interrupter bits set case,
  655. * CSI DMA is work in one of FB1 and FB2 buffer,
  656. * but software can not know the state.
  657. * Skip it to avoid base address updated
  658. * when csi work in field0 and field1 will write to
  659. * new base address.
  660. */
  661. } else if (status & BIT_DMA_TSF_DONE_FB1) {
  662. csi->buf_num = 0;
  663. } else if (status & BIT_DMA_TSF_DONE_FB2) {
  664. csi->buf_num = 1;
  665. }
  666. if ((status & BIT_DMA_TSF_DONE_FB1) ||
  667. (status & BIT_DMA_TSF_DONE_FB2)) {
  668. imx7_csi_vb2_buf_done(csi);
  669. if (csi->last_eof) {
  670. complete(&csi->last_eof_completion);
  671. csi->last_eof = false;
  672. }
  673. }
  674. spin_unlock(&csi->irqlock);
  675. return IRQ_HANDLED;
  676. }
  677. /* -----------------------------------------------------------------------------
  678. * Format Helpers
  679. */
  680. #define IMX_BUS_FMTS(fmt...) (const u32[]) {fmt, 0}
  681. /*
  682. * List of supported pixel formats for the subdevs. Keep V4L2_PIX_FMT_UYVY and
  683. * MEDIA_BUS_FMT_UYVY8_2X8 first to match IMX7_CSI_DEF_PIX_FORMAT and
  684. * IMX7_CSI_DEF_MBUS_CODE.
  685. *
  686. * TODO: Restrict the supported formats list based on the SoC integration.
  687. *
  688. * The CSI bridge can be configured to sample pixel components from the Rx queue
  689. * in single (8bpp) or double (16bpp) component modes. Image format variants
  690. * with different sample sizes (ie YUYV_2X8 vs YUYV_1X16) determine the pixel
  691. * components sampling size per each clock cycle and their packing mode (see
  692. * imx7_csi_configure() for details).
  693. *
  694. * As the CSI bridge can be interfaced with different IP blocks depending on the
  695. * SoC model it is integrated on, the Rx queue sampling size should match the
  696. * size of the samples transferred by the transmitting IP block. To avoid
  697. * misconfigurations of the capture pipeline, the enumeration of the supported
  698. * formats should be restricted to match the pixel source transmitting mode.
  699. *
  700. * Example: i.MX8MM SoC integrates the CSI bridge with the Samsung CSIS CSI-2
  701. * receiver which operates in dual pixel sampling mode. The CSI bridge should
  702. * only expose the 1X16 formats variant which instructs it to operate in dual
  703. * pixel sampling mode. When the CSI bridge is instead integrated on an i.MX7,
  704. * which supports both serial and parallel input, it should expose both
  705. * variants.
  706. *
  707. * This currently only applies to YUYV formats, but other formats might need to
  708. * be handled in the same way.
  709. */
  710. static const struct imx7_csi_pixfmt pixel_formats[] = {
  711. /*** YUV formats start here ***/
  712. {
  713. .fourcc = V4L2_PIX_FMT_UYVY,
  714. .codes = IMX_BUS_FMTS(
  715. MEDIA_BUS_FMT_UYVY8_2X8,
  716. MEDIA_BUS_FMT_UYVY8_1X16
  717. ),
  718. .yuv = true,
  719. .bpp = 16,
  720. }, {
  721. .fourcc = V4L2_PIX_FMT_YUYV,
  722. .codes = IMX_BUS_FMTS(
  723. MEDIA_BUS_FMT_YUYV8_2X8,
  724. MEDIA_BUS_FMT_YUYV8_1X16
  725. ),
  726. .yuv = true,
  727. .bpp = 16,
  728. },
  729. /*** raw bayer and grayscale formats start here ***/
  730. {
  731. .fourcc = V4L2_PIX_FMT_SBGGR8,
  732. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR8_1X8),
  733. .bpp = 8,
  734. }, {
  735. .fourcc = V4L2_PIX_FMT_SGBRG8,
  736. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG8_1X8),
  737. .bpp = 8,
  738. }, {
  739. .fourcc = V4L2_PIX_FMT_SGRBG8,
  740. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG8_1X8),
  741. .bpp = 8,
  742. }, {
  743. .fourcc = V4L2_PIX_FMT_SRGGB8,
  744. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB8_1X8),
  745. .bpp = 8,
  746. }, {
  747. .fourcc = V4L2_PIX_FMT_SBGGR10,
  748. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR10_1X10),
  749. .bpp = 16,
  750. }, {
  751. .fourcc = V4L2_PIX_FMT_SGBRG10,
  752. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG10_1X10),
  753. .bpp = 16,
  754. }, {
  755. .fourcc = V4L2_PIX_FMT_SGRBG10,
  756. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG10_1X10),
  757. .bpp = 16,
  758. }, {
  759. .fourcc = V4L2_PIX_FMT_SRGGB10,
  760. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB10_1X10),
  761. .bpp = 16,
  762. }, {
  763. .fourcc = V4L2_PIX_FMT_SBGGR12,
  764. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR12_1X12),
  765. .bpp = 16,
  766. }, {
  767. .fourcc = V4L2_PIX_FMT_SGBRG12,
  768. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG12_1X12),
  769. .bpp = 16,
  770. }, {
  771. .fourcc = V4L2_PIX_FMT_SGRBG12,
  772. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG12_1X12),
  773. .bpp = 16,
  774. }, {
  775. .fourcc = V4L2_PIX_FMT_SRGGB12,
  776. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB12_1X12),
  777. .bpp = 16,
  778. }, {
  779. .fourcc = V4L2_PIX_FMT_SBGGR14,
  780. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR14_1X14),
  781. .bpp = 16,
  782. }, {
  783. .fourcc = V4L2_PIX_FMT_SGBRG14,
  784. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG14_1X14),
  785. .bpp = 16,
  786. }, {
  787. .fourcc = V4L2_PIX_FMT_SGRBG14,
  788. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG14_1X14),
  789. .bpp = 16,
  790. }, {
  791. .fourcc = V4L2_PIX_FMT_SRGGB14,
  792. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB14_1X14),
  793. .bpp = 16,
  794. }, {
  795. .fourcc = V4L2_PIX_FMT_GREY,
  796. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y8_1X8),
  797. .bpp = 8,
  798. }, {
  799. .fourcc = V4L2_PIX_FMT_Y10,
  800. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y10_1X10),
  801. .bpp = 16,
  802. }, {
  803. .fourcc = V4L2_PIX_FMT_Y12,
  804. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y12_1X12),
  805. .bpp = 16,
  806. }, {
  807. .fourcc = V4L2_PIX_FMT_Y14,
  808. .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y14_1X14),
  809. .bpp = 16,
  810. },
  811. };
  812. /*
  813. * Search in the pixel_formats[] array for an entry with the given fourcc
  814. * return it.
  815. */
  816. static const struct imx7_csi_pixfmt *imx7_csi_find_pixel_format(u32 fourcc)
  817. {
  818. unsigned int i;
  819. for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
  820. const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
  821. if (fmt->fourcc == fourcc)
  822. return fmt;
  823. }
  824. return NULL;
  825. }
  826. /*
  827. * Search in the pixel_formats[] array for an entry with the given media
  828. * bus code and return it.
  829. */
  830. static const struct imx7_csi_pixfmt *imx7_csi_find_mbus_format(u32 code)
  831. {
  832. unsigned int i;
  833. for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
  834. const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
  835. unsigned int j;
  836. if (!fmt->codes)
  837. continue;
  838. for (j = 0; fmt->codes[j]; j++) {
  839. if (code == fmt->codes[j])
  840. return fmt;
  841. }
  842. }
  843. return NULL;
  844. }
  845. /*
  846. * Enumerate entries in the pixel_formats[] array that match the
  847. * requested search criteria. Return the media-bus code that matches
  848. * the search criteria at the requested match index.
  849. *
  850. * @code: The returned media-bus code that matches the search criteria at
  851. * the requested match index.
  852. * @index: The requested match index.
  853. */
  854. static int imx7_csi_enum_mbus_formats(u32 *code, u32 index)
  855. {
  856. unsigned int i;
  857. for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
  858. const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
  859. unsigned int j;
  860. if (!fmt->codes)
  861. continue;
  862. for (j = 0; fmt->codes[j]; j++) {
  863. if (index == 0) {
  864. *code = fmt->codes[j];
  865. return 0;
  866. }
  867. index--;
  868. }
  869. }
  870. return -EINVAL;
  871. }
  872. /* -----------------------------------------------------------------------------
  873. * Video Capture Device - IOCTLs
  874. */
  875. static int imx7_csi_video_querycap(struct file *file, void *fh,
  876. struct v4l2_capability *cap)
  877. {
  878. struct imx7_csi *csi = video_drvdata(file);
  879. strscpy(cap->driver, IMX7_CSI_VIDEO_NAME, sizeof(cap->driver));
  880. strscpy(cap->card, IMX7_CSI_VIDEO_NAME, sizeof(cap->card));
  881. snprintf(cap->bus_info, sizeof(cap->bus_info),
  882. "platform:%s", dev_name(csi->dev));
  883. return 0;
  884. }
  885. static int imx7_csi_video_enum_fmt_vid_cap(struct file *file, void *fh,
  886. struct v4l2_fmtdesc *f)
  887. {
  888. unsigned int index = f->index;
  889. unsigned int i;
  890. for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
  891. const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
  892. /*
  893. * If a media bus code is specified, only consider formats that
  894. * match it.
  895. */
  896. if (f->mbus_code) {
  897. unsigned int j;
  898. if (!fmt->codes)
  899. continue;
  900. for (j = 0; fmt->codes[j]; j++) {
  901. if (f->mbus_code == fmt->codes[j])
  902. break;
  903. }
  904. if (!fmt->codes[j])
  905. continue;
  906. }
  907. if (index == 0) {
  908. f->pixelformat = fmt->fourcc;
  909. return 0;
  910. }
  911. index--;
  912. }
  913. return -EINVAL;
  914. }
  915. static int imx7_csi_video_enum_framesizes(struct file *file, void *fh,
  916. struct v4l2_frmsizeenum *fsize)
  917. {
  918. const struct imx7_csi_pixfmt *cc;
  919. u32 walign;
  920. if (fsize->index > 0)
  921. return -EINVAL;
  922. cc = imx7_csi_find_pixel_format(fsize->pixel_format);
  923. if (!cc)
  924. return -EINVAL;
  925. /*
  926. * The width alignment is 8 bytes as indicated by the
  927. * CSI_IMAG_PARA.IMAGE_WIDTH documentation. Convert it to pixels.
  928. */
  929. walign = 8 * 8 / cc->bpp;
  930. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  931. fsize->stepwise.min_width = walign;
  932. fsize->stepwise.max_width = round_down(65535U, walign);
  933. fsize->stepwise.min_height = 1;
  934. fsize->stepwise.max_height = 65535;
  935. fsize->stepwise.step_width = walign;
  936. fsize->stepwise.step_height = 1;
  937. return 0;
  938. }
  939. static int imx7_csi_video_g_fmt_vid_cap(struct file *file, void *fh,
  940. struct v4l2_format *f)
  941. {
  942. struct imx7_csi *csi = video_drvdata(file);
  943. f->fmt.pix = csi->vdev_fmt;
  944. return 0;
  945. }
  946. static const struct imx7_csi_pixfmt *
  947. __imx7_csi_video_try_fmt(struct v4l2_pix_format *pixfmt,
  948. struct v4l2_rect *compose)
  949. {
  950. const struct imx7_csi_pixfmt *cc;
  951. u32 walign;
  952. if (compose) {
  953. compose->width = pixfmt->width;
  954. compose->height = pixfmt->height;
  955. }
  956. /*
  957. * Find the pixel format, default to the first supported format if not
  958. * found.
  959. */
  960. cc = imx7_csi_find_pixel_format(pixfmt->pixelformat);
  961. if (!cc) {
  962. pixfmt->pixelformat = IMX7_CSI_DEF_PIX_FORMAT;
  963. cc = imx7_csi_find_pixel_format(pixfmt->pixelformat);
  964. }
  965. /*
  966. * The width alignment is 8 bytes as indicated by the
  967. * CSI_IMAG_PARA.IMAGE_WIDTH documentation. Convert it to pixels.
  968. *
  969. * TODO: Implement configurable stride support.
  970. */
  971. walign = 8 * 8 / cc->bpp;
  972. pixfmt->width = clamp(round_up(pixfmt->width, walign), walign,
  973. round_down(65535U, walign));
  974. pixfmt->height = clamp(pixfmt->height, 1U, 65535U);
  975. pixfmt->bytesperline = pixfmt->width * cc->bpp / 8;
  976. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  977. pixfmt->field = V4L2_FIELD_NONE;
  978. return cc;
  979. }
  980. static int imx7_csi_video_try_fmt_vid_cap(struct file *file, void *fh,
  981. struct v4l2_format *f)
  982. {
  983. __imx7_csi_video_try_fmt(&f->fmt.pix, NULL);
  984. return 0;
  985. }
  986. static int imx7_csi_video_s_fmt_vid_cap(struct file *file, void *fh,
  987. struct v4l2_format *f)
  988. {
  989. struct imx7_csi *csi = video_drvdata(file);
  990. const struct imx7_csi_pixfmt *cc;
  991. if (vb2_is_busy(&csi->q)) {
  992. dev_err(csi->dev, "%s queue busy\n", __func__);
  993. return -EBUSY;
  994. }
  995. cc = __imx7_csi_video_try_fmt(&f->fmt.pix, &csi->vdev_compose);
  996. csi->vdev_cc = cc;
  997. csi->vdev_fmt = f->fmt.pix;
  998. return 0;
  999. }
  1000. static int imx7_csi_video_g_selection(struct file *file, void *fh,
  1001. struct v4l2_selection *s)
  1002. {
  1003. struct imx7_csi *csi = video_drvdata(file);
  1004. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1005. return -EINVAL;
  1006. switch (s->target) {
  1007. case V4L2_SEL_TGT_COMPOSE:
  1008. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1009. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1010. /* The compose rectangle is fixed to the source format. */
  1011. s->r = csi->vdev_compose;
  1012. break;
  1013. case V4L2_SEL_TGT_COMPOSE_PADDED:
  1014. /*
  1015. * The hardware writes with a configurable but fixed DMA burst
  1016. * size. If the source format width is not burst size aligned,
  1017. * the written frame contains padding to the right.
  1018. */
  1019. s->r.left = 0;
  1020. s->r.top = 0;
  1021. s->r.width = csi->vdev_fmt.width;
  1022. s->r.height = csi->vdev_fmt.height;
  1023. break;
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. return 0;
  1028. }
  1029. static const struct v4l2_ioctl_ops imx7_csi_video_ioctl_ops = {
  1030. .vidioc_querycap = imx7_csi_video_querycap,
  1031. .vidioc_enum_fmt_vid_cap = imx7_csi_video_enum_fmt_vid_cap,
  1032. .vidioc_enum_framesizes = imx7_csi_video_enum_framesizes,
  1033. .vidioc_g_fmt_vid_cap = imx7_csi_video_g_fmt_vid_cap,
  1034. .vidioc_try_fmt_vid_cap = imx7_csi_video_try_fmt_vid_cap,
  1035. .vidioc_s_fmt_vid_cap = imx7_csi_video_s_fmt_vid_cap,
  1036. .vidioc_g_selection = imx7_csi_video_g_selection,
  1037. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1038. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1039. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1040. .vidioc_querybuf = vb2_ioctl_querybuf,
  1041. .vidioc_qbuf = vb2_ioctl_qbuf,
  1042. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1043. .vidioc_expbuf = vb2_ioctl_expbuf,
  1044. .vidioc_streamon = vb2_ioctl_streamon,
  1045. .vidioc_streamoff = vb2_ioctl_streamoff,
  1046. };
  1047. /* -----------------------------------------------------------------------------
  1048. * Video Capture Device - Queue Operations
  1049. */
  1050. static int imx7_csi_video_queue_setup(struct vb2_queue *vq,
  1051. unsigned int *nbuffers,
  1052. unsigned int *nplanes,
  1053. unsigned int sizes[],
  1054. struct device *alloc_devs[])
  1055. {
  1056. struct imx7_csi *csi = vb2_get_drv_priv(vq);
  1057. unsigned int q_num_bufs = vb2_get_num_buffers(vq);
  1058. struct v4l2_pix_format *pix = &csi->vdev_fmt;
  1059. unsigned int count = *nbuffers;
  1060. if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1061. return -EINVAL;
  1062. if (*nplanes) {
  1063. if (*nplanes != 1 || sizes[0] < pix->sizeimage)
  1064. return -EINVAL;
  1065. count += q_num_bufs;
  1066. }
  1067. count = min_t(__u32, IMX7_CSI_VIDEO_MEM_LIMIT / pix->sizeimage, count);
  1068. if (*nplanes)
  1069. *nbuffers = (count < q_num_bufs) ? 0 :
  1070. count - q_num_bufs;
  1071. else
  1072. *nbuffers = count;
  1073. *nplanes = 1;
  1074. sizes[0] = pix->sizeimage;
  1075. return 0;
  1076. }
  1077. static int imx7_csi_video_buf_init(struct vb2_buffer *vb)
  1078. {
  1079. struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb);
  1080. INIT_LIST_HEAD(&buf->list);
  1081. return 0;
  1082. }
  1083. static int imx7_csi_video_buf_prepare(struct vb2_buffer *vb)
  1084. {
  1085. struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
  1086. struct v4l2_pix_format *pix = &csi->vdev_fmt;
  1087. if (vb2_plane_size(vb, 0) < pix->sizeimage) {
  1088. dev_err(csi->dev,
  1089. "data will not fit into plane (%lu < %lu)\n",
  1090. vb2_plane_size(vb, 0), (long)pix->sizeimage);
  1091. return -EINVAL;
  1092. }
  1093. vb2_set_plane_payload(vb, 0, pix->sizeimage);
  1094. return 0;
  1095. }
  1096. static bool imx7_csi_fast_track_buffer(struct imx7_csi *csi,
  1097. struct imx7_csi_vb2_buffer *buf)
  1098. {
  1099. unsigned long flags;
  1100. dma_addr_t dma_addr;
  1101. int buf_num;
  1102. u32 isr;
  1103. if (!csi->is_streaming)
  1104. return false;
  1105. dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vbuf.vb2_buf, 0);
  1106. /*
  1107. * buf_num holds the framebuffer ID of the most recently (*not* the
  1108. * next anticipated) triggered interrupt. Without loss of generality,
  1109. * if buf_num is 0, the hardware is capturing to FB2. If FB1 has been
  1110. * programmed with a dummy buffer (as indicated by active_vb2_buf[0]
  1111. * being NULL), then we can fast-track the new buffer by programming
  1112. * its address in FB1 before the hardware completes FB2, instead of
  1113. * adding it to the buffer queue and incurring a delay of one
  1114. * additional frame.
  1115. *
  1116. * The irqlock prevents races with the interrupt handler that updates
  1117. * buf_num when it programs the next buffer, but we can still race with
  1118. * the hardware if we program the buffer in FB1 just after the hardware
  1119. * completes FB2 and switches to FB1 and before buf_num can be updated
  1120. * by the interrupt handler for FB2. The fast-tracked buffer would
  1121. * then be ignored by the hardware while the driver would think it has
  1122. * successfully been processed.
  1123. *
  1124. * To avoid this problem, if we can't avoid the race, we can detect
  1125. * that we have lost it by checking, after programming the buffer in
  1126. * FB1, if the interrupt flag indicating completion of FB2 has been
  1127. * raised. If that is not the case, fast-tracking succeeded, and we can
  1128. * update active_vb2_buf[0]. Otherwise, we may or may not have lost the
  1129. * race (as the interrupt flag may have been raised just after
  1130. * programming FB1 and before we read the interrupt status register),
  1131. * and we need to assume the worst case of a race loss and queue the
  1132. * buffer through the slow path.
  1133. */
  1134. spin_lock_irqsave(&csi->irqlock, flags);
  1135. buf_num = csi->buf_num;
  1136. if (csi->active_vb2_buf[buf_num]) {
  1137. spin_unlock_irqrestore(&csi->irqlock, flags);
  1138. return false;
  1139. }
  1140. imx7_csi_update_buf(csi, dma_addr, buf_num);
  1141. isr = imx7_csi_reg_read(csi, CSI_CSISR);
  1142. if (isr & (buf_num ? BIT_DMA_TSF_DONE_FB1 : BIT_DMA_TSF_DONE_FB2)) {
  1143. /*
  1144. * The interrupt for the /other/ FB just came (the isr hasn't
  1145. * run yet though, because we have the lock here); we can't be
  1146. * sure we've programmed buf_num FB in time, so queue the buffer
  1147. * to the buffer queue normally. No need to undo writing the FB
  1148. * register, since we won't return it as active_vb2_buf is NULL,
  1149. * so it's okay to potentially write it to both FB1 and FB2;
  1150. * only the one where it was queued normally will be returned.
  1151. */
  1152. spin_unlock_irqrestore(&csi->irqlock, flags);
  1153. return false;
  1154. }
  1155. csi->active_vb2_buf[buf_num] = buf;
  1156. spin_unlock_irqrestore(&csi->irqlock, flags);
  1157. return true;
  1158. }
  1159. static void imx7_csi_video_buf_queue(struct vb2_buffer *vb)
  1160. {
  1161. struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
  1162. struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb);
  1163. unsigned long flags;
  1164. if (imx7_csi_fast_track_buffer(csi, buf))
  1165. return;
  1166. spin_lock_irqsave(&csi->q_lock, flags);
  1167. list_add_tail(&buf->list, &csi->ready_q);
  1168. spin_unlock_irqrestore(&csi->q_lock, flags);
  1169. }
  1170. static int imx7_csi_video_validate_fmt(struct imx7_csi *csi)
  1171. {
  1172. struct v4l2_subdev_format fmt_src = {
  1173. .pad = IMX7_CSI_PAD_SRC,
  1174. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1175. };
  1176. const struct imx7_csi_pixfmt *cc;
  1177. int ret;
  1178. /* Retrieve the media bus format on the source subdev. */
  1179. ret = v4l2_subdev_call_state_active(&csi->sd, pad, get_fmt, &fmt_src);
  1180. if (ret)
  1181. return ret;
  1182. /*
  1183. * Verify that the media bus size matches the size set on the video
  1184. * node. It is sufficient to check the compose rectangle size without
  1185. * checking the rounded size from pix_fmt, as the rounded size is
  1186. * derived directly from the compose rectangle size, and will thus
  1187. * always match if the compose rectangle matches.
  1188. */
  1189. if (csi->vdev_compose.width != fmt_src.format.width ||
  1190. csi->vdev_compose.height != fmt_src.format.height)
  1191. return -EPIPE;
  1192. /*
  1193. * Verify that the media bus code is compatible with the pixel format
  1194. * set on the video node.
  1195. */
  1196. cc = imx7_csi_find_mbus_format(fmt_src.format.code);
  1197. if (!cc || csi->vdev_cc->yuv != cc->yuv)
  1198. return -EPIPE;
  1199. return 0;
  1200. }
  1201. static int imx7_csi_video_start_streaming(struct vb2_queue *vq,
  1202. unsigned int count)
  1203. {
  1204. struct imx7_csi *csi = vb2_get_drv_priv(vq);
  1205. struct imx7_csi_vb2_buffer *buf, *tmp;
  1206. unsigned long flags;
  1207. int ret;
  1208. ret = imx7_csi_video_validate_fmt(csi);
  1209. if (ret) {
  1210. dev_err(csi->dev, "capture format not valid\n");
  1211. goto err_buffers;
  1212. }
  1213. mutex_lock(&csi->mdev.graph_mutex);
  1214. ret = __video_device_pipeline_start(csi->vdev, &csi->pipe);
  1215. if (ret)
  1216. goto err_unlock;
  1217. ret = v4l2_subdev_call(&csi->sd, video, s_stream, 1);
  1218. if (ret)
  1219. goto err_stop;
  1220. mutex_unlock(&csi->mdev.graph_mutex);
  1221. return 0;
  1222. err_stop:
  1223. __video_device_pipeline_stop(csi->vdev);
  1224. err_unlock:
  1225. mutex_unlock(&csi->mdev.graph_mutex);
  1226. dev_err(csi->dev, "pipeline start failed with %d\n", ret);
  1227. err_buffers:
  1228. spin_lock_irqsave(&csi->q_lock, flags);
  1229. list_for_each_entry_safe(buf, tmp, &csi->ready_q, list) {
  1230. list_del(&buf->list);
  1231. vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
  1232. }
  1233. spin_unlock_irqrestore(&csi->q_lock, flags);
  1234. return ret;
  1235. }
  1236. static void imx7_csi_video_stop_streaming(struct vb2_queue *vq)
  1237. {
  1238. struct imx7_csi *csi = vb2_get_drv_priv(vq);
  1239. struct imx7_csi_vb2_buffer *frame;
  1240. struct imx7_csi_vb2_buffer *tmp;
  1241. unsigned long flags;
  1242. mutex_lock(&csi->mdev.graph_mutex);
  1243. v4l2_subdev_call(&csi->sd, video, s_stream, 0);
  1244. __video_device_pipeline_stop(csi->vdev);
  1245. mutex_unlock(&csi->mdev.graph_mutex);
  1246. /* release all active buffers */
  1247. spin_lock_irqsave(&csi->q_lock, flags);
  1248. list_for_each_entry_safe(frame, tmp, &csi->ready_q, list) {
  1249. list_del(&frame->list);
  1250. vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
  1251. }
  1252. spin_unlock_irqrestore(&csi->q_lock, flags);
  1253. }
  1254. static const struct vb2_ops imx7_csi_video_qops = {
  1255. .queue_setup = imx7_csi_video_queue_setup,
  1256. .buf_init = imx7_csi_video_buf_init,
  1257. .buf_prepare = imx7_csi_video_buf_prepare,
  1258. .buf_queue = imx7_csi_video_buf_queue,
  1259. .wait_prepare = vb2_ops_wait_prepare,
  1260. .wait_finish = vb2_ops_wait_finish,
  1261. .start_streaming = imx7_csi_video_start_streaming,
  1262. .stop_streaming = imx7_csi_video_stop_streaming,
  1263. };
  1264. /* -----------------------------------------------------------------------------
  1265. * Video Capture Device - File Operations
  1266. */
  1267. static int imx7_csi_video_open(struct file *file)
  1268. {
  1269. struct imx7_csi *csi = video_drvdata(file);
  1270. int ret;
  1271. if (mutex_lock_interruptible(&csi->vdev_mutex))
  1272. return -ERESTARTSYS;
  1273. ret = v4l2_fh_open(file);
  1274. if (ret) {
  1275. dev_err(csi->dev, "v4l2_fh_open failed\n");
  1276. goto out;
  1277. }
  1278. ret = v4l2_pipeline_pm_get(&csi->vdev->entity);
  1279. if (ret)
  1280. v4l2_fh_release(file);
  1281. out:
  1282. mutex_unlock(&csi->vdev_mutex);
  1283. return ret;
  1284. }
  1285. static int imx7_csi_video_release(struct file *file)
  1286. {
  1287. struct imx7_csi *csi = video_drvdata(file);
  1288. struct vb2_queue *vq = &csi->q;
  1289. mutex_lock(&csi->vdev_mutex);
  1290. if (file->private_data == vq->owner) {
  1291. vb2_queue_release(vq);
  1292. vq->owner = NULL;
  1293. }
  1294. v4l2_pipeline_pm_put(&csi->vdev->entity);
  1295. v4l2_fh_release(file);
  1296. mutex_unlock(&csi->vdev_mutex);
  1297. return 0;
  1298. }
  1299. static const struct v4l2_file_operations imx7_csi_video_fops = {
  1300. .owner = THIS_MODULE,
  1301. .open = imx7_csi_video_open,
  1302. .release = imx7_csi_video_release,
  1303. .poll = vb2_fop_poll,
  1304. .unlocked_ioctl = video_ioctl2,
  1305. .mmap = vb2_fop_mmap,
  1306. };
  1307. /* -----------------------------------------------------------------------------
  1308. * Video Capture Device - Init & Cleanup
  1309. */
  1310. static struct imx7_csi_vb2_buffer *imx7_csi_video_next_buf(struct imx7_csi *csi)
  1311. {
  1312. struct imx7_csi_vb2_buffer *buf = NULL;
  1313. unsigned long flags;
  1314. spin_lock_irqsave(&csi->q_lock, flags);
  1315. /* get next queued buffer */
  1316. if (!list_empty(&csi->ready_q)) {
  1317. buf = list_entry(csi->ready_q.next, struct imx7_csi_vb2_buffer,
  1318. list);
  1319. list_del(&buf->list);
  1320. }
  1321. spin_unlock_irqrestore(&csi->q_lock, flags);
  1322. return buf;
  1323. }
  1324. static void imx7_csi_video_init_format(struct imx7_csi *csi)
  1325. {
  1326. struct v4l2_pix_format *pixfmt = &csi->vdev_fmt;
  1327. pixfmt->width = IMX7_CSI_DEF_PIX_WIDTH;
  1328. pixfmt->height = IMX7_CSI_DEF_PIX_HEIGHT;
  1329. csi->vdev_cc = __imx7_csi_video_try_fmt(pixfmt, &csi->vdev_compose);
  1330. }
  1331. static int imx7_csi_video_register(struct imx7_csi *csi)
  1332. {
  1333. struct v4l2_subdev *sd = &csi->sd;
  1334. struct v4l2_device *v4l2_dev = sd->v4l2_dev;
  1335. struct video_device *vdev = csi->vdev;
  1336. int ret;
  1337. vdev->v4l2_dev = v4l2_dev;
  1338. /* Initialize the default format and compose rectangle. */
  1339. imx7_csi_video_init_format(csi);
  1340. /* Register the video device. */
  1341. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  1342. if (ret) {
  1343. dev_err(csi->dev, "Failed to register video device\n");
  1344. return ret;
  1345. }
  1346. dev_info(csi->dev, "Registered %s as /dev/%s\n", vdev->name,
  1347. video_device_node_name(vdev));
  1348. /* Create the link from the CSI subdev to the video device. */
  1349. ret = media_create_pad_link(&sd->entity, IMX7_CSI_PAD_SRC,
  1350. &vdev->entity, 0, MEDIA_LNK_FL_IMMUTABLE |
  1351. MEDIA_LNK_FL_ENABLED);
  1352. if (ret) {
  1353. dev_err(csi->dev, "failed to create link to device node\n");
  1354. video_unregister_device(vdev);
  1355. return ret;
  1356. }
  1357. return 0;
  1358. }
  1359. static void imx7_csi_video_unregister(struct imx7_csi *csi)
  1360. {
  1361. media_entity_cleanup(&csi->vdev->entity);
  1362. video_unregister_device(csi->vdev);
  1363. }
  1364. static int imx7_csi_video_init(struct imx7_csi *csi)
  1365. {
  1366. struct video_device *vdev;
  1367. struct vb2_queue *vq;
  1368. int ret;
  1369. mutex_init(&csi->vdev_mutex);
  1370. INIT_LIST_HEAD(&csi->ready_q);
  1371. spin_lock_init(&csi->q_lock);
  1372. /* Allocate and initialize the video device. */
  1373. vdev = video_device_alloc();
  1374. if (!vdev)
  1375. return -ENOMEM;
  1376. vdev->fops = &imx7_csi_video_fops;
  1377. vdev->ioctl_ops = &imx7_csi_video_ioctl_ops;
  1378. vdev->minor = -1;
  1379. vdev->release = video_device_release;
  1380. vdev->vfl_dir = VFL_DIR_RX;
  1381. vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
  1382. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
  1383. | V4L2_CAP_IO_MC;
  1384. vdev->lock = &csi->vdev_mutex;
  1385. vdev->queue = &csi->q;
  1386. snprintf(vdev->name, sizeof(vdev->name), "%s capture", csi->sd.name);
  1387. video_set_drvdata(vdev, csi);
  1388. csi->vdev = vdev;
  1389. /* Initialize the video device pad. */
  1390. csi->vdev_pad.flags = MEDIA_PAD_FL_SINK;
  1391. ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
  1392. if (ret) {
  1393. video_device_release(vdev);
  1394. return ret;
  1395. }
  1396. /* Initialize the vb2 queue. */
  1397. vq = &csi->q;
  1398. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1399. vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1400. vq->drv_priv = csi;
  1401. vq->buf_struct_size = sizeof(struct imx7_csi_vb2_buffer);
  1402. vq->ops = &imx7_csi_video_qops;
  1403. vq->mem_ops = &vb2_dma_contig_memops;
  1404. vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1405. vq->lock = &csi->vdev_mutex;
  1406. vq->min_queued_buffers = 2;
  1407. vq->dev = csi->dev;
  1408. ret = vb2_queue_init(vq);
  1409. if (ret) {
  1410. dev_err(csi->dev, "vb2_queue_init failed\n");
  1411. video_device_release(vdev);
  1412. return ret;
  1413. }
  1414. return 0;
  1415. }
  1416. /* -----------------------------------------------------------------------------
  1417. * V4L2 Subdev Operations
  1418. */
  1419. static int imx7_csi_s_stream(struct v4l2_subdev *sd, int enable)
  1420. {
  1421. struct imx7_csi *csi = v4l2_get_subdevdata(sd);
  1422. struct v4l2_subdev_state *sd_state;
  1423. int ret = 0;
  1424. sd_state = v4l2_subdev_lock_and_get_active_state(sd);
  1425. if (enable) {
  1426. ret = imx7_csi_init(csi, sd_state);
  1427. if (ret < 0)
  1428. goto out_unlock;
  1429. ret = v4l2_subdev_call(csi->src_sd, video, s_stream, 1);
  1430. if (ret < 0) {
  1431. imx7_csi_deinit(csi, VB2_BUF_STATE_QUEUED);
  1432. goto out_unlock;
  1433. }
  1434. imx7_csi_enable(csi);
  1435. } else {
  1436. imx7_csi_disable(csi);
  1437. v4l2_subdev_call(csi->src_sd, video, s_stream, 0);
  1438. imx7_csi_deinit(csi, VB2_BUF_STATE_ERROR);
  1439. }
  1440. csi->is_streaming = !!enable;
  1441. out_unlock:
  1442. v4l2_subdev_unlock_state(sd_state);
  1443. return ret;
  1444. }
  1445. static int imx7_csi_init_state(struct v4l2_subdev *sd,
  1446. struct v4l2_subdev_state *sd_state)
  1447. {
  1448. const struct imx7_csi_pixfmt *cc;
  1449. int i;
  1450. cc = imx7_csi_find_mbus_format(IMX7_CSI_DEF_MBUS_CODE);
  1451. for (i = 0; i < IMX7_CSI_PADS_NUM; i++) {
  1452. struct v4l2_mbus_framefmt *mf =
  1453. v4l2_subdev_state_get_format(sd_state, i);
  1454. mf->code = IMX7_CSI_DEF_MBUS_CODE;
  1455. mf->width = IMX7_CSI_DEF_PIX_WIDTH;
  1456. mf->height = IMX7_CSI_DEF_PIX_HEIGHT;
  1457. mf->field = V4L2_FIELD_NONE;
  1458. mf->colorspace = V4L2_COLORSPACE_SRGB;
  1459. mf->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(mf->colorspace);
  1460. mf->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(mf->colorspace);
  1461. mf->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(!cc->yuv,
  1462. mf->colorspace, mf->ycbcr_enc);
  1463. }
  1464. return 0;
  1465. }
  1466. static int imx7_csi_enum_mbus_code(struct v4l2_subdev *sd,
  1467. struct v4l2_subdev_state *sd_state,
  1468. struct v4l2_subdev_mbus_code_enum *code)
  1469. {
  1470. struct v4l2_mbus_framefmt *in_fmt;
  1471. int ret = 0;
  1472. in_fmt = v4l2_subdev_state_get_format(sd_state, IMX7_CSI_PAD_SINK);
  1473. switch (code->pad) {
  1474. case IMX7_CSI_PAD_SINK:
  1475. ret = imx7_csi_enum_mbus_formats(&code->code, code->index);
  1476. break;
  1477. case IMX7_CSI_PAD_SRC:
  1478. if (code->index != 0) {
  1479. ret = -EINVAL;
  1480. break;
  1481. }
  1482. code->code = in_fmt->code;
  1483. break;
  1484. default:
  1485. ret = -EINVAL;
  1486. break;
  1487. }
  1488. return ret;
  1489. }
  1490. /*
  1491. * Default the colorspace in tryfmt to SRGB if set to an unsupported
  1492. * colorspace or not initialized. Then set the remaining colorimetry
  1493. * parameters based on the colorspace if they are uninitialized.
  1494. *
  1495. * tryfmt->code must be set on entry.
  1496. */
  1497. static void imx7_csi_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt)
  1498. {
  1499. const struct imx7_csi_pixfmt *cc;
  1500. bool is_rgb = false;
  1501. cc = imx7_csi_find_mbus_format(tryfmt->code);
  1502. if (cc && !cc->yuv)
  1503. is_rgb = true;
  1504. switch (tryfmt->colorspace) {
  1505. case V4L2_COLORSPACE_SMPTE170M:
  1506. case V4L2_COLORSPACE_REC709:
  1507. case V4L2_COLORSPACE_JPEG:
  1508. case V4L2_COLORSPACE_SRGB:
  1509. case V4L2_COLORSPACE_BT2020:
  1510. case V4L2_COLORSPACE_OPRGB:
  1511. case V4L2_COLORSPACE_DCI_P3:
  1512. case V4L2_COLORSPACE_RAW:
  1513. break;
  1514. default:
  1515. tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
  1516. break;
  1517. }
  1518. if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
  1519. tryfmt->xfer_func =
  1520. V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
  1521. if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
  1522. tryfmt->ycbcr_enc =
  1523. V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
  1524. if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
  1525. tryfmt->quantization =
  1526. V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
  1527. tryfmt->colorspace,
  1528. tryfmt->ycbcr_enc);
  1529. }
  1530. static void imx7_csi_try_fmt(struct v4l2_subdev *sd,
  1531. struct v4l2_subdev_state *sd_state,
  1532. struct v4l2_subdev_format *sdformat,
  1533. const struct imx7_csi_pixfmt **cc)
  1534. {
  1535. const struct imx7_csi_pixfmt *in_cc;
  1536. struct v4l2_mbus_framefmt *in_fmt;
  1537. u32 code;
  1538. in_fmt = v4l2_subdev_state_get_format(sd_state, IMX7_CSI_PAD_SINK);
  1539. switch (sdformat->pad) {
  1540. case IMX7_CSI_PAD_SRC:
  1541. in_cc = imx7_csi_find_mbus_format(in_fmt->code);
  1542. sdformat->format.width = in_fmt->width;
  1543. sdformat->format.height = in_fmt->height;
  1544. sdformat->format.code = in_fmt->code;
  1545. sdformat->format.field = in_fmt->field;
  1546. *cc = in_cc;
  1547. sdformat->format.colorspace = in_fmt->colorspace;
  1548. sdformat->format.xfer_func = in_fmt->xfer_func;
  1549. sdformat->format.quantization = in_fmt->quantization;
  1550. sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
  1551. break;
  1552. case IMX7_CSI_PAD_SINK:
  1553. *cc = imx7_csi_find_mbus_format(sdformat->format.code);
  1554. if (!*cc) {
  1555. code = IMX7_CSI_DEF_MBUS_CODE;
  1556. *cc = imx7_csi_find_mbus_format(code);
  1557. sdformat->format.code = code;
  1558. }
  1559. if (sdformat->format.field != V4L2_FIELD_INTERLACED)
  1560. sdformat->format.field = V4L2_FIELD_NONE;
  1561. break;
  1562. }
  1563. imx7_csi_try_colorimetry(&sdformat->format);
  1564. }
  1565. static int imx7_csi_set_fmt(struct v4l2_subdev *sd,
  1566. struct v4l2_subdev_state *sd_state,
  1567. struct v4l2_subdev_format *sdformat)
  1568. {
  1569. struct imx7_csi *csi = v4l2_get_subdevdata(sd);
  1570. const struct imx7_csi_pixfmt *outcc;
  1571. struct v4l2_mbus_framefmt *outfmt;
  1572. const struct imx7_csi_pixfmt *cc;
  1573. struct v4l2_mbus_framefmt *fmt;
  1574. struct v4l2_subdev_format format;
  1575. if (csi->is_streaming)
  1576. return -EBUSY;
  1577. imx7_csi_try_fmt(sd, sd_state, sdformat, &cc);
  1578. fmt = v4l2_subdev_state_get_format(sd_state, sdformat->pad);
  1579. *fmt = sdformat->format;
  1580. if (sdformat->pad == IMX7_CSI_PAD_SINK) {
  1581. /* propagate format to source pads */
  1582. format.pad = IMX7_CSI_PAD_SRC;
  1583. format.which = sdformat->which;
  1584. format.format = sdformat->format;
  1585. imx7_csi_try_fmt(sd, sd_state, &format, &outcc);
  1586. outfmt = v4l2_subdev_state_get_format(sd_state,
  1587. IMX7_CSI_PAD_SRC);
  1588. *outfmt = format.format;
  1589. }
  1590. return 0;
  1591. }
  1592. static int imx7_csi_pad_link_validate(struct v4l2_subdev *sd,
  1593. struct media_link *link,
  1594. struct v4l2_subdev_format *source_fmt,
  1595. struct v4l2_subdev_format *sink_fmt)
  1596. {
  1597. struct imx7_csi *csi = v4l2_get_subdevdata(sd);
  1598. struct media_pad *pad = NULL;
  1599. unsigned int i;
  1600. int ret;
  1601. /*
  1602. * Validate the source link, and record whether the source uses the
  1603. * parallel input or the CSI-2 receiver.
  1604. */
  1605. ret = v4l2_subdev_link_validate_default(sd, link, source_fmt, sink_fmt);
  1606. if (ret)
  1607. return ret;
  1608. switch (csi->src_sd->entity.function) {
  1609. case MEDIA_ENT_F_VID_IF_BRIDGE:
  1610. /* The input is the CSI-2 receiver. */
  1611. csi->is_csi2 = true;
  1612. break;
  1613. case MEDIA_ENT_F_VID_MUX:
  1614. /* The input is the mux, check its input. */
  1615. for (i = 0; i < csi->src_sd->entity.num_pads; i++) {
  1616. struct media_pad *spad = &csi->src_sd->entity.pads[i];
  1617. if (!(spad->flags & MEDIA_PAD_FL_SINK))
  1618. continue;
  1619. pad = media_pad_remote_pad_first(spad);
  1620. if (pad)
  1621. break;
  1622. }
  1623. if (!pad)
  1624. return -ENODEV;
  1625. csi->is_csi2 = pad->entity->function == MEDIA_ENT_F_VID_IF_BRIDGE;
  1626. break;
  1627. default:
  1628. /*
  1629. * The input is an external entity, it must use the parallel
  1630. * bus.
  1631. */
  1632. csi->is_csi2 = false;
  1633. break;
  1634. }
  1635. return 0;
  1636. }
  1637. static int imx7_csi_registered(struct v4l2_subdev *sd)
  1638. {
  1639. struct imx7_csi *csi = v4l2_get_subdevdata(sd);
  1640. int ret;
  1641. ret = imx7_csi_video_init(csi);
  1642. if (ret)
  1643. return ret;
  1644. ret = imx7_csi_video_register(csi);
  1645. if (ret)
  1646. return ret;
  1647. ret = v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
  1648. if (ret)
  1649. goto err_unreg;
  1650. ret = media_device_register(&csi->mdev);
  1651. if (ret)
  1652. goto err_unreg;
  1653. return 0;
  1654. err_unreg:
  1655. imx7_csi_video_unregister(csi);
  1656. return ret;
  1657. }
  1658. static void imx7_csi_unregistered(struct v4l2_subdev *sd)
  1659. {
  1660. struct imx7_csi *csi = v4l2_get_subdevdata(sd);
  1661. imx7_csi_video_unregister(csi);
  1662. }
  1663. static const struct v4l2_subdev_video_ops imx7_csi_video_ops = {
  1664. .s_stream = imx7_csi_s_stream,
  1665. };
  1666. static const struct v4l2_subdev_pad_ops imx7_csi_pad_ops = {
  1667. .enum_mbus_code = imx7_csi_enum_mbus_code,
  1668. .get_fmt = v4l2_subdev_get_fmt,
  1669. .set_fmt = imx7_csi_set_fmt,
  1670. .link_validate = imx7_csi_pad_link_validate,
  1671. };
  1672. static const struct v4l2_subdev_ops imx7_csi_subdev_ops = {
  1673. .video = &imx7_csi_video_ops,
  1674. .pad = &imx7_csi_pad_ops,
  1675. };
  1676. static const struct v4l2_subdev_internal_ops imx7_csi_internal_ops = {
  1677. .init_state = imx7_csi_init_state,
  1678. .registered = imx7_csi_registered,
  1679. .unregistered = imx7_csi_unregistered,
  1680. };
  1681. /* -----------------------------------------------------------------------------
  1682. * Media Entity Operations
  1683. */
  1684. static const struct media_entity_operations imx7_csi_entity_ops = {
  1685. .link_validate = v4l2_subdev_link_validate,
  1686. .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
  1687. };
  1688. /* -----------------------------------------------------------------------------
  1689. * Probe & Remove
  1690. */
  1691. static int imx7_csi_notify_bound(struct v4l2_async_notifier *notifier,
  1692. struct v4l2_subdev *sd,
  1693. struct v4l2_async_connection *asd)
  1694. {
  1695. struct imx7_csi *csi = imx7_csi_notifier_to_dev(notifier);
  1696. struct media_pad *sink = &csi->sd.entity.pads[IMX7_CSI_PAD_SINK];
  1697. csi->src_sd = sd;
  1698. return v4l2_create_fwnode_links_to_pad(sd, sink, MEDIA_LNK_FL_ENABLED |
  1699. MEDIA_LNK_FL_IMMUTABLE);
  1700. }
  1701. static int imx7_csi_notify_complete(struct v4l2_async_notifier *notifier)
  1702. {
  1703. struct imx7_csi *csi = imx7_csi_notifier_to_dev(notifier);
  1704. return v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
  1705. }
  1706. static const struct v4l2_async_notifier_operations imx7_csi_notify_ops = {
  1707. .bound = imx7_csi_notify_bound,
  1708. .complete = imx7_csi_notify_complete,
  1709. };
  1710. static int imx7_csi_async_register(struct imx7_csi *csi)
  1711. {
  1712. struct v4l2_async_connection *asd;
  1713. struct fwnode_handle *ep;
  1714. int ret;
  1715. v4l2_async_nf_init(&csi->notifier, &csi->v4l2_dev);
  1716. ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0,
  1717. FWNODE_GRAPH_ENDPOINT_NEXT);
  1718. if (!ep) {
  1719. ret = dev_err_probe(csi->dev, -ENOTCONN,
  1720. "Failed to get remote endpoint\n");
  1721. goto error;
  1722. }
  1723. asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, ep,
  1724. struct v4l2_async_connection);
  1725. fwnode_handle_put(ep);
  1726. if (IS_ERR(asd)) {
  1727. ret = dev_err_probe(csi->dev, PTR_ERR(asd),
  1728. "Failed to add remote subdev to notifier\n");
  1729. goto error;
  1730. }
  1731. csi->notifier.ops = &imx7_csi_notify_ops;
  1732. ret = v4l2_async_nf_register(&csi->notifier);
  1733. if (ret)
  1734. goto error;
  1735. return 0;
  1736. error:
  1737. v4l2_async_nf_cleanup(&csi->notifier);
  1738. return ret;
  1739. }
  1740. static void imx7_csi_media_cleanup(struct imx7_csi *csi)
  1741. {
  1742. v4l2_device_unregister(&csi->v4l2_dev);
  1743. media_device_unregister(&csi->mdev);
  1744. v4l2_subdev_cleanup(&csi->sd);
  1745. media_device_cleanup(&csi->mdev);
  1746. }
  1747. static const struct media_device_ops imx7_csi_media_ops = {
  1748. .link_notify = v4l2_pipeline_link_notify,
  1749. };
  1750. static int imx7_csi_media_dev_init(struct imx7_csi *csi)
  1751. {
  1752. int ret;
  1753. strscpy(csi->mdev.model, "imx-media", sizeof(csi->mdev.model));
  1754. csi->mdev.ops = &imx7_csi_media_ops;
  1755. csi->mdev.dev = csi->dev;
  1756. csi->v4l2_dev.mdev = &csi->mdev;
  1757. strscpy(csi->v4l2_dev.name, "imx-media",
  1758. sizeof(csi->v4l2_dev.name));
  1759. snprintf(csi->mdev.bus_info, sizeof(csi->mdev.bus_info),
  1760. "platform:%s", dev_name(csi->mdev.dev));
  1761. media_device_init(&csi->mdev);
  1762. ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
  1763. if (ret < 0) {
  1764. v4l2_err(&csi->v4l2_dev,
  1765. "Failed to register v4l2_device: %d\n", ret);
  1766. goto cleanup;
  1767. }
  1768. return 0;
  1769. cleanup:
  1770. media_device_cleanup(&csi->mdev);
  1771. return ret;
  1772. }
  1773. static int imx7_csi_media_init(struct imx7_csi *csi)
  1774. {
  1775. unsigned int i;
  1776. int ret;
  1777. /* add media device */
  1778. ret = imx7_csi_media_dev_init(csi);
  1779. if (ret)
  1780. return ret;
  1781. v4l2_subdev_init(&csi->sd, &imx7_csi_subdev_ops);
  1782. v4l2_set_subdevdata(&csi->sd, csi);
  1783. csi->sd.internal_ops = &imx7_csi_internal_ops;
  1784. csi->sd.entity.ops = &imx7_csi_entity_ops;
  1785. csi->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1786. csi->sd.dev = csi->dev;
  1787. csi->sd.owner = THIS_MODULE;
  1788. csi->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1789. snprintf(csi->sd.name, sizeof(csi->sd.name), "csi");
  1790. for (i = 0; i < IMX7_CSI_PADS_NUM; i++)
  1791. csi->pad[i].flags = (i == IMX7_CSI_PAD_SINK) ?
  1792. MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
  1793. ret = media_entity_pads_init(&csi->sd.entity, IMX7_CSI_PADS_NUM,
  1794. csi->pad);
  1795. if (ret)
  1796. goto error;
  1797. ret = v4l2_subdev_init_finalize(&csi->sd);
  1798. if (ret)
  1799. goto error;
  1800. ret = v4l2_device_register_subdev(&csi->v4l2_dev, &csi->sd);
  1801. if (ret)
  1802. goto error;
  1803. return 0;
  1804. error:
  1805. imx7_csi_media_cleanup(csi);
  1806. return ret;
  1807. }
  1808. static int imx7_csi_probe(struct platform_device *pdev)
  1809. {
  1810. struct device *dev = &pdev->dev;
  1811. struct imx7_csi *csi;
  1812. int ret;
  1813. csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
  1814. if (!csi)
  1815. return -ENOMEM;
  1816. csi->dev = dev;
  1817. platform_set_drvdata(pdev, csi);
  1818. spin_lock_init(&csi->irqlock);
  1819. /* Acquire resources and install interrupt handler. */
  1820. csi->mclk = devm_clk_get(&pdev->dev, "mclk");
  1821. if (IS_ERR(csi->mclk)) {
  1822. ret = PTR_ERR(csi->mclk);
  1823. dev_err(dev, "Failed to get mclk: %d", ret);
  1824. return ret;
  1825. }
  1826. csi->irq = platform_get_irq(pdev, 0);
  1827. if (csi->irq < 0)
  1828. return csi->irq;
  1829. csi->regbase = devm_platform_ioremap_resource(pdev, 0);
  1830. if (IS_ERR(csi->regbase))
  1831. return PTR_ERR(csi->regbase);
  1832. csi->model = (enum imx_csi_model)(uintptr_t)of_device_get_match_data(&pdev->dev);
  1833. ret = devm_request_irq(dev, csi->irq, imx7_csi_irq_handler, 0, "csi",
  1834. (void *)csi);
  1835. if (ret < 0) {
  1836. dev_err(dev, "Request CSI IRQ failed.\n");
  1837. return ret;
  1838. }
  1839. /* Initialize all the media device infrastructure. */
  1840. ret = imx7_csi_media_init(csi);
  1841. if (ret)
  1842. return ret;
  1843. ret = imx7_csi_async_register(csi);
  1844. if (ret)
  1845. goto err_media_cleanup;
  1846. return 0;
  1847. err_media_cleanup:
  1848. imx7_csi_media_cleanup(csi);
  1849. return ret;
  1850. }
  1851. static void imx7_csi_remove(struct platform_device *pdev)
  1852. {
  1853. struct imx7_csi *csi = platform_get_drvdata(pdev);
  1854. imx7_csi_media_cleanup(csi);
  1855. v4l2_async_nf_unregister(&csi->notifier);
  1856. v4l2_async_nf_cleanup(&csi->notifier);
  1857. v4l2_async_unregister_subdev(&csi->sd);
  1858. }
  1859. static const struct of_device_id imx7_csi_of_match[] = {
  1860. { .compatible = "fsl,imx8mq-csi", .data = (void *)IMX7_CSI_IMX8MQ },
  1861. { .compatible = "fsl,imx7-csi", .data = (void *)IMX7_CSI_IMX7 },
  1862. { .compatible = "fsl,imx6ul-csi", .data = (void *)IMX7_CSI_IMX7 },
  1863. { },
  1864. };
  1865. MODULE_DEVICE_TABLE(of, imx7_csi_of_match);
  1866. static struct platform_driver imx7_csi_driver = {
  1867. .probe = imx7_csi_probe,
  1868. .remove_new = imx7_csi_remove,
  1869. .driver = {
  1870. .of_match_table = imx7_csi_of_match,
  1871. .name = "imx7-csi",
  1872. },
  1873. };
  1874. module_platform_driver(imx7_csi_driver);
  1875. MODULE_DESCRIPTION("i.MX7 CSI subdev driver");
  1876. MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
  1877. MODULE_LICENSE("GPL v2");
  1878. MODULE_ALIAS("platform:imx7-csi");