renesas-ceu.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * V4L2 Driver for Renesas Capture Engine Unit (CEU) interface
  4. * Copyright (C) 2017-2018 Jacopo Mondi <[email protected]>
  5. *
  6. * Based on soc-camera driver "soc_camera/sh_mobile_ceu_camera.c"
  7. * Copyright (C) 2008 Magnus Damm
  8. *
  9. * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
  10. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  11. * Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_graph.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/slab.h>
  29. #include <linux/time.h>
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-async.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/v4l2-dev.h>
  35. #include <media/v4l2-device.h>
  36. #include <media/v4l2-event.h>
  37. #include <media/v4l2-fwnode.h>
  38. #include <media/v4l2-image-sizes.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-mediabus.h>
  41. #include <media/videobuf2-dma-contig.h>
  42. #include <media/drv-intf/renesas-ceu.h>
  43. #define DRIVER_NAME "renesas-ceu"
  44. /* CEU registers offsets and masks. */
  45. #define CEU_CAPSR 0x00 /* Capture start register */
  46. #define CEU_CAPCR 0x04 /* Capture control register */
  47. #define CEU_CAMCR 0x08 /* Capture interface control register */
  48. #define CEU_CAMOR 0x10 /* Capture interface offset register */
  49. #define CEU_CAPWR 0x14 /* Capture interface width register */
  50. #define CEU_CAIFR 0x18 /* Capture interface input format register */
  51. #define CEU_CRCNTR 0x28 /* CEU register control register */
  52. #define CEU_CRCMPR 0x2c /* CEU register forcible control register */
  53. #define CEU_CFLCR 0x30 /* Capture filter control register */
  54. #define CEU_CFSZR 0x34 /* Capture filter size clip register */
  55. #define CEU_CDWDR 0x38 /* Capture destination width register */
  56. #define CEU_CDAYR 0x3c /* Capture data address Y register */
  57. #define CEU_CDACR 0x40 /* Capture data address C register */
  58. #define CEU_CFWCR 0x5c /* Firewall operation control register */
  59. #define CEU_CDOCR 0x64 /* Capture data output control register */
  60. #define CEU_CEIER 0x70 /* Capture event interrupt enable register */
  61. #define CEU_CETCR 0x74 /* Capture event flag clear register */
  62. #define CEU_CSTSR 0x7c /* Capture status register */
  63. #define CEU_CSRTR 0x80 /* Capture software reset register */
  64. /* Data synchronous fetch mode. */
  65. #define CEU_CAMCR_JPEG BIT(4)
  66. /* Input components ordering: CEU_CAMCR.DTARY field. */
  67. #define CEU_CAMCR_DTARY_8_UYVY (0x00 << 8)
  68. #define CEU_CAMCR_DTARY_8_VYUY (0x01 << 8)
  69. #define CEU_CAMCR_DTARY_8_YUYV (0x02 << 8)
  70. #define CEU_CAMCR_DTARY_8_YVYU (0x03 << 8)
  71. /* TODO: input components ordering for 16 bits input. */
  72. /* Bus transfer MTU. */
  73. #define CEU_CAPCR_BUS_WIDTH256 (0x3 << 20)
  74. /* Bus width configuration. */
  75. #define CEU_CAMCR_DTIF_16BITS BIT(12)
  76. /* No downsampling to planar YUV420 in image fetch mode. */
  77. #define CEU_CDOCR_NO_DOWSAMPLE BIT(4)
  78. /* Swap all input data in 8-bit, 16-bits and 32-bits units (Figure 46.45). */
  79. #define CEU_CDOCR_SWAP_ENDIANNESS (7)
  80. /* Capture reset and enable bits. */
  81. #define CEU_CAPSR_CPKIL BIT(16)
  82. #define CEU_CAPSR_CE BIT(0)
  83. /* CEU operating flag bit. */
  84. #define CEU_CAPCR_CTNCP BIT(16)
  85. #define CEU_CSTRST_CPTON BIT(0)
  86. /* Platform specific IRQ source flags. */
  87. #define CEU_CETCR_ALL_IRQS_RZ 0x397f313
  88. #define CEU_CETCR_ALL_IRQS_SH4 0x3d7f313
  89. /* Prohibited register access interrupt bit. */
  90. #define CEU_CETCR_IGRW BIT(4)
  91. /* One-frame capture end interrupt. */
  92. #define CEU_CEIER_CPE BIT(0)
  93. /* VBP error. */
  94. #define CEU_CEIER_VBP BIT(20)
  95. #define CEU_CEIER_MASK (CEU_CEIER_CPE | CEU_CEIER_VBP)
  96. #define CEU_MAX_WIDTH 2560
  97. #define CEU_MAX_HEIGHT 1920
  98. #define CEU_MAX_BPL 8188
  99. #define CEU_W_MAX(w) ((w) < CEU_MAX_WIDTH ? (w) : CEU_MAX_WIDTH)
  100. #define CEU_H_MAX(h) ((h) < CEU_MAX_HEIGHT ? (h) : CEU_MAX_HEIGHT)
  101. /*
  102. * ceu_bus_fmt - describe a 8-bits yuyv format the sensor can produce
  103. *
  104. * @mbus_code: bus format code
  105. * @fmt_order: CEU_CAMCR.DTARY ordering of input components (Y, Cb, Cr)
  106. * @fmt_order_swap: swapped CEU_CAMCR.DTARY ordering of input components
  107. * (Y, Cr, Cb)
  108. * @swapped: does Cr appear before Cb?
  109. * @bps: number of bits sent over bus for each sample
  110. * @bpp: number of bits per pixels unit
  111. */
  112. struct ceu_mbus_fmt {
  113. u32 mbus_code;
  114. u32 fmt_order;
  115. u32 fmt_order_swap;
  116. bool swapped;
  117. u8 bps;
  118. u8 bpp;
  119. };
  120. /*
  121. * ceu_buffer - Link vb2 buffer to the list of available buffers.
  122. */
  123. struct ceu_buffer {
  124. struct vb2_v4l2_buffer vb;
  125. struct list_head queue;
  126. };
  127. static inline struct ceu_buffer *vb2_to_ceu(struct vb2_v4l2_buffer *vbuf)
  128. {
  129. return container_of(vbuf, struct ceu_buffer, vb);
  130. }
  131. /*
  132. * ceu_subdev - Wraps v4l2 sub-device and provides async subdevice.
  133. */
  134. struct ceu_subdev {
  135. struct v4l2_async_subdev asd;
  136. struct v4l2_subdev *v4l2_sd;
  137. /* per-subdevice mbus configuration options */
  138. unsigned int mbus_flags;
  139. struct ceu_mbus_fmt mbus_fmt;
  140. };
  141. static struct ceu_subdev *to_ceu_subdev(struct v4l2_async_subdev *asd)
  142. {
  143. return container_of(asd, struct ceu_subdev, asd);
  144. }
  145. /*
  146. * ceu_device - CEU device instance
  147. */
  148. struct ceu_device {
  149. struct device *dev;
  150. struct video_device vdev;
  151. struct v4l2_device v4l2_dev;
  152. /* subdevices descriptors */
  153. struct ceu_subdev **subdevs;
  154. /* the subdevice currently in use */
  155. struct ceu_subdev *sd;
  156. unsigned int sd_index;
  157. unsigned int num_sd;
  158. /* platform specific mask with all IRQ sources flagged */
  159. u32 irq_mask;
  160. /* currently configured field and pixel format */
  161. enum v4l2_field field;
  162. struct v4l2_pix_format_mplane v4l2_pix;
  163. /* async subdev notification helpers */
  164. struct v4l2_async_notifier notifier;
  165. /* vb2 queue, capture buffer list and active buffer pointer */
  166. struct vb2_queue vb2_vq;
  167. struct list_head capture;
  168. struct vb2_v4l2_buffer *active;
  169. unsigned int sequence;
  170. /* mlock - lock access to interface reset and vb2 queue */
  171. struct mutex mlock;
  172. /* lock - lock access to capture buffer queue and active buffer */
  173. spinlock_t lock;
  174. /* base - CEU memory base address */
  175. void __iomem *base;
  176. };
  177. static inline struct ceu_device *v4l2_to_ceu(struct v4l2_device *v4l2_dev)
  178. {
  179. return container_of(v4l2_dev, struct ceu_device, v4l2_dev);
  180. }
  181. /* --- CEU memory output formats --- */
  182. /*
  183. * ceu_fmt - describe a memory output format supported by CEU interface.
  184. *
  185. * @fourcc: memory layout fourcc format code
  186. * @bpp: number of bits for each pixel stored in memory
  187. */
  188. struct ceu_fmt {
  189. u32 fourcc;
  190. u32 bpp;
  191. };
  192. /*
  193. * ceu_format_list - List of supported memory output formats
  194. *
  195. * If sensor provides any YUYV bus format, all the following planar memory
  196. * formats are available thanks to CEU re-ordering and sub-sampling
  197. * capabilities.
  198. */
  199. static const struct ceu_fmt ceu_fmt_list[] = {
  200. {
  201. .fourcc = V4L2_PIX_FMT_NV16,
  202. .bpp = 16,
  203. },
  204. {
  205. .fourcc = V4L2_PIX_FMT_NV61,
  206. .bpp = 16,
  207. },
  208. {
  209. .fourcc = V4L2_PIX_FMT_NV12,
  210. .bpp = 12,
  211. },
  212. {
  213. .fourcc = V4L2_PIX_FMT_NV21,
  214. .bpp = 12,
  215. },
  216. {
  217. .fourcc = V4L2_PIX_FMT_YUYV,
  218. .bpp = 16,
  219. },
  220. {
  221. .fourcc = V4L2_PIX_FMT_UYVY,
  222. .bpp = 16,
  223. },
  224. {
  225. .fourcc = V4L2_PIX_FMT_YVYU,
  226. .bpp = 16,
  227. },
  228. {
  229. .fourcc = V4L2_PIX_FMT_VYUY,
  230. .bpp = 16,
  231. },
  232. };
  233. static const struct ceu_fmt *get_ceu_fmt_from_fourcc(unsigned int fourcc)
  234. {
  235. const struct ceu_fmt *fmt = &ceu_fmt_list[0];
  236. unsigned int i;
  237. for (i = 0; i < ARRAY_SIZE(ceu_fmt_list); i++, fmt++)
  238. if (fmt->fourcc == fourcc)
  239. return fmt;
  240. return NULL;
  241. }
  242. static bool ceu_fmt_mplane(struct v4l2_pix_format_mplane *pix)
  243. {
  244. switch (pix->pixelformat) {
  245. case V4L2_PIX_FMT_YUYV:
  246. case V4L2_PIX_FMT_UYVY:
  247. case V4L2_PIX_FMT_YVYU:
  248. case V4L2_PIX_FMT_VYUY:
  249. return false;
  250. case V4L2_PIX_FMT_NV16:
  251. case V4L2_PIX_FMT_NV61:
  252. case V4L2_PIX_FMT_NV12:
  253. case V4L2_PIX_FMT_NV21:
  254. return true;
  255. default:
  256. return false;
  257. }
  258. }
  259. /* --- CEU HW operations --- */
  260. static void ceu_write(struct ceu_device *priv, unsigned int reg_offs, u32 data)
  261. {
  262. iowrite32(data, priv->base + reg_offs);
  263. }
  264. static u32 ceu_read(struct ceu_device *priv, unsigned int reg_offs)
  265. {
  266. return ioread32(priv->base + reg_offs);
  267. }
  268. /*
  269. * ceu_soft_reset() - Software reset the CEU interface.
  270. * @ceu_device: CEU device.
  271. *
  272. * Returns 0 for success, -EIO for error.
  273. */
  274. static int ceu_soft_reset(struct ceu_device *ceudev)
  275. {
  276. unsigned int i;
  277. ceu_write(ceudev, CEU_CAPSR, CEU_CAPSR_CPKIL);
  278. for (i = 0; i < 100; i++) {
  279. if (!(ceu_read(ceudev, CEU_CSTSR) & CEU_CSTRST_CPTON))
  280. break;
  281. udelay(1);
  282. }
  283. if (i == 100) {
  284. dev_err(ceudev->dev, "soft reset time out\n");
  285. return -EIO;
  286. }
  287. for (i = 0; i < 100; i++) {
  288. if (!(ceu_read(ceudev, CEU_CAPSR) & CEU_CAPSR_CPKIL))
  289. return 0;
  290. udelay(1);
  291. }
  292. /* If we get here, CEU has not reset properly. */
  293. return -EIO;
  294. }
  295. /* --- CEU Capture Operations --- */
  296. /*
  297. * ceu_hw_config() - Configure CEU interface registers.
  298. */
  299. static int ceu_hw_config(struct ceu_device *ceudev)
  300. {
  301. u32 camcr, cdocr, cfzsr, cdwdr, capwr;
  302. struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
  303. struct ceu_subdev *ceu_sd = ceudev->sd;
  304. struct ceu_mbus_fmt *mbus_fmt = &ceu_sd->mbus_fmt;
  305. unsigned int mbus_flags = ceu_sd->mbus_flags;
  306. /* Start configuring CEU registers */
  307. ceu_write(ceudev, CEU_CAIFR, 0);
  308. ceu_write(ceudev, CEU_CFWCR, 0);
  309. ceu_write(ceudev, CEU_CRCNTR, 0);
  310. ceu_write(ceudev, CEU_CRCMPR, 0);
  311. /* Set the frame capture period for both image capture and data sync. */
  312. capwr = (pix->height << 16) | pix->width * mbus_fmt->bpp / 8;
  313. /*
  314. * Swap input data endianness by default.
  315. * In data fetch mode bytes are received in chunks of 8 bytes.
  316. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  317. * The data is however by default written to memory in reverse order:
  318. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  319. *
  320. * Use CEU_CDOCR[2:0] to swap data ordering.
  321. */
  322. cdocr = CEU_CDOCR_SWAP_ENDIANNESS;
  323. /*
  324. * Configure CAMCR and CDOCR:
  325. * match input components ordering with memory output format and
  326. * handle downsampling to YUV420.
  327. *
  328. * If the memory output planar format is 'swapped' (Cr before Cb) and
  329. * input format is not, use the swapped version of CAMCR.DTARY.
  330. *
  331. * If the memory output planar format is not 'swapped' (Cb before Cr)
  332. * and input format is, use the swapped version of CAMCR.DTARY.
  333. *
  334. * CEU by default downsample to planar YUV420 (CDCOR[4] = 0).
  335. * If output is planar YUV422 set CDOCR[4] = 1
  336. *
  337. * No downsample for data fetch sync mode.
  338. */
  339. switch (pix->pixelformat) {
  340. /* Data fetch sync mode */
  341. case V4L2_PIX_FMT_YUYV:
  342. case V4L2_PIX_FMT_YVYU:
  343. case V4L2_PIX_FMT_UYVY:
  344. case V4L2_PIX_FMT_VYUY:
  345. camcr = CEU_CAMCR_JPEG;
  346. cdocr |= CEU_CDOCR_NO_DOWSAMPLE;
  347. cfzsr = (pix->height << 16) | pix->width;
  348. cdwdr = pix->plane_fmt[0].bytesperline;
  349. break;
  350. /* Non-swapped planar image capture mode. */
  351. case V4L2_PIX_FMT_NV16:
  352. cdocr |= CEU_CDOCR_NO_DOWSAMPLE;
  353. fallthrough;
  354. case V4L2_PIX_FMT_NV12:
  355. if (mbus_fmt->swapped)
  356. camcr = mbus_fmt->fmt_order_swap;
  357. else
  358. camcr = mbus_fmt->fmt_order;
  359. cfzsr = (pix->height << 16) | pix->width;
  360. cdwdr = pix->width;
  361. break;
  362. /* Swapped planar image capture mode. */
  363. case V4L2_PIX_FMT_NV61:
  364. cdocr |= CEU_CDOCR_NO_DOWSAMPLE;
  365. fallthrough;
  366. case V4L2_PIX_FMT_NV21:
  367. if (mbus_fmt->swapped)
  368. camcr = mbus_fmt->fmt_order;
  369. else
  370. camcr = mbus_fmt->fmt_order_swap;
  371. cfzsr = (pix->height << 16) | pix->width;
  372. cdwdr = pix->width;
  373. break;
  374. default:
  375. return -EINVAL;
  376. }
  377. camcr |= mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  378. camcr |= mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  379. /* TODO: handle 16 bit bus width with DTIF bit in CAMCR */
  380. ceu_write(ceudev, CEU_CAMCR, camcr);
  381. ceu_write(ceudev, CEU_CDOCR, cdocr);
  382. ceu_write(ceudev, CEU_CAPCR, CEU_CAPCR_BUS_WIDTH256);
  383. /*
  384. * TODO: make CAMOR offsets configurable.
  385. * CAMOR wants to know the number of blanks between a VS/HS signal
  386. * and valid data. This value should actually come from the sensor...
  387. */
  388. ceu_write(ceudev, CEU_CAMOR, 0);
  389. /* TODO: 16 bit bus width require re-calculation of cdwdr and cfzsr */
  390. ceu_write(ceudev, CEU_CAPWR, capwr);
  391. ceu_write(ceudev, CEU_CFSZR, cfzsr);
  392. ceu_write(ceudev, CEU_CDWDR, cdwdr);
  393. return 0;
  394. }
  395. /*
  396. * ceu_capture() - Trigger start of a capture sequence.
  397. *
  398. * Program the CEU DMA registers with addresses where to transfer image data.
  399. */
  400. static int ceu_capture(struct ceu_device *ceudev)
  401. {
  402. struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
  403. dma_addr_t phys_addr_top;
  404. phys_addr_top =
  405. vb2_dma_contig_plane_dma_addr(&ceudev->active->vb2_buf, 0);
  406. ceu_write(ceudev, CEU_CDAYR, phys_addr_top);
  407. /* Ignore CbCr plane for non multi-planar image formats. */
  408. if (ceu_fmt_mplane(pix)) {
  409. phys_addr_top =
  410. vb2_dma_contig_plane_dma_addr(&ceudev->active->vb2_buf,
  411. 1);
  412. ceu_write(ceudev, CEU_CDACR, phys_addr_top);
  413. }
  414. /*
  415. * Trigger new capture start: once for each frame, as we work in
  416. * one-frame capture mode.
  417. */
  418. ceu_write(ceudev, CEU_CAPSR, CEU_CAPSR_CE);
  419. return 0;
  420. }
  421. static irqreturn_t ceu_irq(int irq, void *data)
  422. {
  423. struct ceu_device *ceudev = data;
  424. struct vb2_v4l2_buffer *vbuf;
  425. struct ceu_buffer *buf;
  426. u32 status;
  427. /* Clean interrupt status. */
  428. status = ceu_read(ceudev, CEU_CETCR);
  429. ceu_write(ceudev, CEU_CETCR, ~ceudev->irq_mask);
  430. /* Unexpected interrupt. */
  431. if (!(status & CEU_CEIER_MASK))
  432. return IRQ_NONE;
  433. spin_lock(&ceudev->lock);
  434. /* Stale interrupt from a released buffer, ignore it. */
  435. vbuf = ceudev->active;
  436. if (!vbuf) {
  437. spin_unlock(&ceudev->lock);
  438. return IRQ_HANDLED;
  439. }
  440. /*
  441. * When a VBP interrupt occurs, no capture end interrupt will occur
  442. * and the image of that frame is not captured correctly.
  443. */
  444. if (status & CEU_CEIER_VBP) {
  445. dev_err(ceudev->dev, "VBP interrupt: abort capture\n");
  446. goto error_irq_out;
  447. }
  448. /* Prepare to return the 'previous' buffer. */
  449. vbuf->vb2_buf.timestamp = ktime_get_ns();
  450. vbuf->sequence = ceudev->sequence++;
  451. vbuf->field = ceudev->field;
  452. /* Prepare a new 'active' buffer and trigger a new capture. */
  453. if (!list_empty(&ceudev->capture)) {
  454. buf = list_first_entry(&ceudev->capture, struct ceu_buffer,
  455. queue);
  456. list_del(&buf->queue);
  457. ceudev->active = &buf->vb;
  458. ceu_capture(ceudev);
  459. }
  460. /* Return the 'previous' buffer. */
  461. vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
  462. spin_unlock(&ceudev->lock);
  463. return IRQ_HANDLED;
  464. error_irq_out:
  465. /* Return the 'previous' buffer and all queued ones. */
  466. vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_ERROR);
  467. list_for_each_entry(buf, &ceudev->capture, queue)
  468. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  469. spin_unlock(&ceudev->lock);
  470. return IRQ_HANDLED;
  471. }
  472. /* --- CEU Videobuf2 operations --- */
  473. static void ceu_update_plane_sizes(struct v4l2_plane_pix_format *plane,
  474. unsigned int bpl, unsigned int szimage)
  475. {
  476. memset(plane, 0, sizeof(*plane));
  477. plane->sizeimage = szimage;
  478. if (plane->bytesperline < bpl || plane->bytesperline > CEU_MAX_BPL)
  479. plane->bytesperline = bpl;
  480. }
  481. /*
  482. * ceu_calc_plane_sizes() - Fill per-plane 'struct v4l2_plane_pix_format'
  483. * information according to the currently configured
  484. * pixel format.
  485. * @ceu_device: CEU device.
  486. * @ceu_fmt: Active image format.
  487. * @pix: Pixel format information (store line width and image sizes)
  488. */
  489. static void ceu_calc_plane_sizes(struct ceu_device *ceudev,
  490. const struct ceu_fmt *ceu_fmt,
  491. struct v4l2_pix_format_mplane *pix)
  492. {
  493. unsigned int bpl, szimage;
  494. switch (pix->pixelformat) {
  495. case V4L2_PIX_FMT_YUYV:
  496. case V4L2_PIX_FMT_UYVY:
  497. case V4L2_PIX_FMT_YVYU:
  498. case V4L2_PIX_FMT_VYUY:
  499. pix->num_planes = 1;
  500. bpl = pix->width * ceu_fmt->bpp / 8;
  501. szimage = pix->height * bpl;
  502. ceu_update_plane_sizes(&pix->plane_fmt[0], bpl, szimage);
  503. break;
  504. case V4L2_PIX_FMT_NV12:
  505. case V4L2_PIX_FMT_NV21:
  506. pix->num_planes = 2;
  507. bpl = pix->width;
  508. szimage = pix->height * pix->width;
  509. ceu_update_plane_sizes(&pix->plane_fmt[0], bpl, szimage);
  510. ceu_update_plane_sizes(&pix->plane_fmt[1], bpl, szimage / 2);
  511. break;
  512. case V4L2_PIX_FMT_NV16:
  513. case V4L2_PIX_FMT_NV61:
  514. default:
  515. pix->num_planes = 2;
  516. bpl = pix->width;
  517. szimage = pix->height * pix->width;
  518. ceu_update_plane_sizes(&pix->plane_fmt[0], bpl, szimage);
  519. ceu_update_plane_sizes(&pix->plane_fmt[1], bpl, szimage);
  520. break;
  521. }
  522. }
  523. /*
  524. * ceu_vb2_setup() - is called to check whether the driver can accept the
  525. * requested number of buffers and to fill in plane sizes
  526. * for the current frame format, if required.
  527. */
  528. static int ceu_vb2_setup(struct vb2_queue *vq, unsigned int *count,
  529. unsigned int *num_planes, unsigned int sizes[],
  530. struct device *alloc_devs[])
  531. {
  532. struct ceu_device *ceudev = vb2_get_drv_priv(vq);
  533. struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
  534. unsigned int i;
  535. /* num_planes is set: just check plane sizes. */
  536. if (*num_planes) {
  537. for (i = 0; i < pix->num_planes; i++)
  538. if (sizes[i] < pix->plane_fmt[i].sizeimage)
  539. return -EINVAL;
  540. return 0;
  541. }
  542. /* num_planes not set: called from REQBUFS, just set plane sizes. */
  543. *num_planes = pix->num_planes;
  544. for (i = 0; i < pix->num_planes; i++)
  545. sizes[i] = pix->plane_fmt[i].sizeimage;
  546. return 0;
  547. }
  548. static void ceu_vb2_queue(struct vb2_buffer *vb)
  549. {
  550. struct ceu_device *ceudev = vb2_get_drv_priv(vb->vb2_queue);
  551. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  552. struct ceu_buffer *buf = vb2_to_ceu(vbuf);
  553. unsigned long irqflags;
  554. spin_lock_irqsave(&ceudev->lock, irqflags);
  555. list_add_tail(&buf->queue, &ceudev->capture);
  556. spin_unlock_irqrestore(&ceudev->lock, irqflags);
  557. }
  558. static int ceu_vb2_prepare(struct vb2_buffer *vb)
  559. {
  560. struct ceu_device *ceudev = vb2_get_drv_priv(vb->vb2_queue);
  561. struct v4l2_pix_format_mplane *pix = &ceudev->v4l2_pix;
  562. unsigned int i;
  563. for (i = 0; i < pix->num_planes; i++) {
  564. if (vb2_plane_size(vb, i) < pix->plane_fmt[i].sizeimage) {
  565. dev_err(ceudev->dev,
  566. "Plane size too small (%lu < %u)\n",
  567. vb2_plane_size(vb, i),
  568. pix->plane_fmt[i].sizeimage);
  569. return -EINVAL;
  570. }
  571. vb2_set_plane_payload(vb, i, pix->plane_fmt[i].sizeimage);
  572. }
  573. return 0;
  574. }
  575. static int ceu_start_streaming(struct vb2_queue *vq, unsigned int count)
  576. {
  577. struct ceu_device *ceudev = vb2_get_drv_priv(vq);
  578. struct v4l2_subdev *v4l2_sd = ceudev->sd->v4l2_sd;
  579. struct ceu_buffer *buf;
  580. unsigned long irqflags;
  581. int ret;
  582. /* Program the CEU interface according to the CEU image format. */
  583. ret = ceu_hw_config(ceudev);
  584. if (ret)
  585. goto error_return_bufs;
  586. ret = v4l2_subdev_call(v4l2_sd, video, s_stream, 1);
  587. if (ret && ret != -ENOIOCTLCMD) {
  588. dev_dbg(ceudev->dev,
  589. "Subdevice failed to start streaming: %d\n", ret);
  590. goto error_return_bufs;
  591. }
  592. spin_lock_irqsave(&ceudev->lock, irqflags);
  593. ceudev->sequence = 0;
  594. /* Grab the first available buffer and trigger the first capture. */
  595. buf = list_first_entry(&ceudev->capture, struct ceu_buffer,
  596. queue);
  597. if (!buf) {
  598. spin_unlock_irqrestore(&ceudev->lock, irqflags);
  599. dev_dbg(ceudev->dev,
  600. "No buffer available for capture.\n");
  601. goto error_stop_sensor;
  602. }
  603. list_del(&buf->queue);
  604. ceudev->active = &buf->vb;
  605. /* Clean and program interrupts for first capture. */
  606. ceu_write(ceudev, CEU_CETCR, ~ceudev->irq_mask);
  607. ceu_write(ceudev, CEU_CEIER, CEU_CEIER_MASK);
  608. ceu_capture(ceudev);
  609. spin_unlock_irqrestore(&ceudev->lock, irqflags);
  610. return 0;
  611. error_stop_sensor:
  612. v4l2_subdev_call(v4l2_sd, video, s_stream, 0);
  613. error_return_bufs:
  614. spin_lock_irqsave(&ceudev->lock, irqflags);
  615. list_for_each_entry(buf, &ceudev->capture, queue)
  616. vb2_buffer_done(&ceudev->active->vb2_buf,
  617. VB2_BUF_STATE_QUEUED);
  618. ceudev->active = NULL;
  619. spin_unlock_irqrestore(&ceudev->lock, irqflags);
  620. return ret;
  621. }
  622. static void ceu_stop_streaming(struct vb2_queue *vq)
  623. {
  624. struct ceu_device *ceudev = vb2_get_drv_priv(vq);
  625. struct v4l2_subdev *v4l2_sd = ceudev->sd->v4l2_sd;
  626. struct ceu_buffer *buf;
  627. unsigned long irqflags;
  628. /* Clean and disable interrupt sources. */
  629. ceu_write(ceudev, CEU_CETCR,
  630. ceu_read(ceudev, CEU_CETCR) & ceudev->irq_mask);
  631. ceu_write(ceudev, CEU_CEIER, CEU_CEIER_MASK);
  632. v4l2_subdev_call(v4l2_sd, video, s_stream, 0);
  633. spin_lock_irqsave(&ceudev->lock, irqflags);
  634. if (ceudev->active) {
  635. vb2_buffer_done(&ceudev->active->vb2_buf,
  636. VB2_BUF_STATE_ERROR);
  637. ceudev->active = NULL;
  638. }
  639. /* Release all queued buffers. */
  640. list_for_each_entry(buf, &ceudev->capture, queue)
  641. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  642. INIT_LIST_HEAD(&ceudev->capture);
  643. spin_unlock_irqrestore(&ceudev->lock, irqflags);
  644. ceu_soft_reset(ceudev);
  645. }
  646. static const struct vb2_ops ceu_vb2_ops = {
  647. .queue_setup = ceu_vb2_setup,
  648. .buf_queue = ceu_vb2_queue,
  649. .buf_prepare = ceu_vb2_prepare,
  650. .wait_prepare = vb2_ops_wait_prepare,
  651. .wait_finish = vb2_ops_wait_finish,
  652. .start_streaming = ceu_start_streaming,
  653. .stop_streaming = ceu_stop_streaming,
  654. };
  655. /* --- CEU image formats handling --- */
  656. /*
  657. * __ceu_try_fmt() - test format on CEU and sensor
  658. * @ceudev: The CEU device.
  659. * @v4l2_fmt: format to test.
  660. * @sd_mbus_code: the media bus code accepted by the subdevice; output param.
  661. *
  662. * Returns 0 for success, < 0 for errors.
  663. */
  664. static int __ceu_try_fmt(struct ceu_device *ceudev, struct v4l2_format *v4l2_fmt,
  665. u32 *sd_mbus_code)
  666. {
  667. struct ceu_subdev *ceu_sd = ceudev->sd;
  668. struct v4l2_pix_format_mplane *pix = &v4l2_fmt->fmt.pix_mp;
  669. struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
  670. struct v4l2_subdev_pad_config pad_cfg;
  671. struct v4l2_subdev_state pad_state = {
  672. .pads = &pad_cfg
  673. };
  674. const struct ceu_fmt *ceu_fmt;
  675. u32 mbus_code_old;
  676. u32 mbus_code;
  677. int ret;
  678. /*
  679. * Set format on sensor sub device: bus format used to produce memory
  680. * format is selected depending on YUV component ordering or
  681. * at initialization time.
  682. */
  683. struct v4l2_subdev_format sd_format = {
  684. .which = V4L2_SUBDEV_FORMAT_TRY,
  685. };
  686. mbus_code_old = ceu_sd->mbus_fmt.mbus_code;
  687. switch (pix->pixelformat) {
  688. case V4L2_PIX_FMT_YUYV:
  689. mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
  690. break;
  691. case V4L2_PIX_FMT_UYVY:
  692. mbus_code = MEDIA_BUS_FMT_UYVY8_2X8;
  693. break;
  694. case V4L2_PIX_FMT_YVYU:
  695. mbus_code = MEDIA_BUS_FMT_YVYU8_2X8;
  696. break;
  697. case V4L2_PIX_FMT_VYUY:
  698. mbus_code = MEDIA_BUS_FMT_VYUY8_2X8;
  699. break;
  700. case V4L2_PIX_FMT_NV16:
  701. case V4L2_PIX_FMT_NV61:
  702. case V4L2_PIX_FMT_NV12:
  703. case V4L2_PIX_FMT_NV21:
  704. mbus_code = ceu_sd->mbus_fmt.mbus_code;
  705. break;
  706. default:
  707. pix->pixelformat = V4L2_PIX_FMT_NV16;
  708. mbus_code = ceu_sd->mbus_fmt.mbus_code;
  709. break;
  710. }
  711. ceu_fmt = get_ceu_fmt_from_fourcc(pix->pixelformat);
  712. /* CFSZR requires height and width to be 4-pixel aligned. */
  713. v4l_bound_align_image(&pix->width, 2, CEU_MAX_WIDTH, 4,
  714. &pix->height, 4, CEU_MAX_HEIGHT, 4, 0);
  715. v4l2_fill_mbus_format_mplane(&sd_format.format, pix);
  716. /*
  717. * Try with the mbus_code matching YUYV components ordering first,
  718. * if that one fails, fallback to default selected at initialization
  719. * time.
  720. */
  721. sd_format.format.code = mbus_code;
  722. ret = v4l2_subdev_call(v4l2_sd, pad, set_fmt, &pad_state, &sd_format);
  723. if (ret) {
  724. if (ret == -EINVAL) {
  725. /* fallback */
  726. sd_format.format.code = mbus_code_old;
  727. ret = v4l2_subdev_call(v4l2_sd, pad, set_fmt,
  728. &pad_state, &sd_format);
  729. }
  730. if (ret)
  731. return ret;
  732. }
  733. /* Apply size returned by sensor as the CEU can't scale. */
  734. v4l2_fill_pix_format_mplane(pix, &sd_format.format);
  735. /* Calculate per-plane sizes based on image format. */
  736. ceu_calc_plane_sizes(ceudev, ceu_fmt, pix);
  737. /* Report to caller the configured mbus format. */
  738. *sd_mbus_code = sd_format.format.code;
  739. return 0;
  740. }
  741. /*
  742. * ceu_try_fmt() - Wrapper for __ceu_try_fmt; discard configured mbus_fmt
  743. */
  744. static int ceu_try_fmt(struct ceu_device *ceudev, struct v4l2_format *v4l2_fmt)
  745. {
  746. u32 mbus_code;
  747. return __ceu_try_fmt(ceudev, v4l2_fmt, &mbus_code);
  748. }
  749. /*
  750. * ceu_set_fmt() - Apply the supplied format to both sensor and CEU
  751. */
  752. static int ceu_set_fmt(struct ceu_device *ceudev, struct v4l2_format *v4l2_fmt)
  753. {
  754. struct ceu_subdev *ceu_sd = ceudev->sd;
  755. struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
  756. u32 mbus_code;
  757. int ret;
  758. /*
  759. * Set format on sensor sub device: bus format used to produce memory
  760. * format is selected at initialization time.
  761. */
  762. struct v4l2_subdev_format format = {
  763. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  764. };
  765. ret = __ceu_try_fmt(ceudev, v4l2_fmt, &mbus_code);
  766. if (ret)
  767. return ret;
  768. format.format.code = mbus_code;
  769. v4l2_fill_mbus_format_mplane(&format.format, &v4l2_fmt->fmt.pix_mp);
  770. ret = v4l2_subdev_call(v4l2_sd, pad, set_fmt, NULL, &format);
  771. if (ret)
  772. return ret;
  773. ceudev->v4l2_pix = v4l2_fmt->fmt.pix_mp;
  774. ceudev->field = V4L2_FIELD_NONE;
  775. return 0;
  776. }
  777. /*
  778. * ceu_set_default_fmt() - Apply default NV16 memory output format with VGA
  779. * sizes.
  780. */
  781. static int ceu_set_default_fmt(struct ceu_device *ceudev)
  782. {
  783. int ret;
  784. struct v4l2_format v4l2_fmt = {
  785. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  786. .fmt.pix_mp = {
  787. .width = VGA_WIDTH,
  788. .height = VGA_HEIGHT,
  789. .field = V4L2_FIELD_NONE,
  790. .pixelformat = V4L2_PIX_FMT_NV16,
  791. .num_planes = 2,
  792. .plane_fmt = {
  793. [0] = {
  794. .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
  795. .bytesperline = VGA_WIDTH * 2,
  796. },
  797. [1] = {
  798. .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
  799. .bytesperline = VGA_WIDTH * 2,
  800. },
  801. },
  802. },
  803. };
  804. ret = ceu_try_fmt(ceudev, &v4l2_fmt);
  805. if (ret)
  806. return ret;
  807. ceudev->v4l2_pix = v4l2_fmt.fmt.pix_mp;
  808. ceudev->field = V4L2_FIELD_NONE;
  809. return 0;
  810. }
  811. /*
  812. * ceu_init_mbus_fmt() - Query sensor for supported formats and initialize
  813. * CEU media bus format used to produce memory formats.
  814. *
  815. * Find out if sensor can produce a permutation of 8-bits YUYV bus format.
  816. * From a single 8-bits YUYV bus format the CEU can produce several memory
  817. * output formats:
  818. * - NV[12|21|16|61] through image fetch mode;
  819. * - YUYV422 if sensor provides YUYV422
  820. *
  821. * TODO: Other YUYV422 permutations through data fetch sync mode and DTARY
  822. * TODO: Binary data (eg. JPEG) and raw formats through data fetch sync mode
  823. */
  824. static int ceu_init_mbus_fmt(struct ceu_device *ceudev)
  825. {
  826. struct ceu_subdev *ceu_sd = ceudev->sd;
  827. struct ceu_mbus_fmt *mbus_fmt = &ceu_sd->mbus_fmt;
  828. struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
  829. bool yuyv_bus_fmt = false;
  830. struct v4l2_subdev_mbus_code_enum sd_mbus_fmt = {
  831. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  832. .index = 0,
  833. };
  834. /* Find out if sensor can produce any permutation of 8-bits YUYV422. */
  835. while (!yuyv_bus_fmt &&
  836. !v4l2_subdev_call(v4l2_sd, pad, enum_mbus_code,
  837. NULL, &sd_mbus_fmt)) {
  838. switch (sd_mbus_fmt.code) {
  839. case MEDIA_BUS_FMT_YUYV8_2X8:
  840. case MEDIA_BUS_FMT_YVYU8_2X8:
  841. case MEDIA_BUS_FMT_UYVY8_2X8:
  842. case MEDIA_BUS_FMT_VYUY8_2X8:
  843. yuyv_bus_fmt = true;
  844. break;
  845. default:
  846. /*
  847. * Only support 8-bits YUYV bus formats at the moment;
  848. *
  849. * TODO: add support for binary formats (data sync
  850. * fetch mode).
  851. */
  852. break;
  853. }
  854. sd_mbus_fmt.index++;
  855. }
  856. if (!yuyv_bus_fmt)
  857. return -ENXIO;
  858. /*
  859. * Save the first encountered YUYV format as "mbus_fmt" and use it
  860. * to output all planar YUV422 and YUV420 (NV*) formats to memory as
  861. * well as for data synch fetch mode (YUYV - YVYU etc. ).
  862. */
  863. mbus_fmt->mbus_code = sd_mbus_fmt.code;
  864. mbus_fmt->bps = 8;
  865. /* Annotate the selected bus format components ordering. */
  866. switch (sd_mbus_fmt.code) {
  867. case MEDIA_BUS_FMT_YUYV8_2X8:
  868. mbus_fmt->fmt_order = CEU_CAMCR_DTARY_8_YUYV;
  869. mbus_fmt->fmt_order_swap = CEU_CAMCR_DTARY_8_YVYU;
  870. mbus_fmt->swapped = false;
  871. mbus_fmt->bpp = 16;
  872. break;
  873. case MEDIA_BUS_FMT_YVYU8_2X8:
  874. mbus_fmt->fmt_order = CEU_CAMCR_DTARY_8_YVYU;
  875. mbus_fmt->fmt_order_swap = CEU_CAMCR_DTARY_8_YUYV;
  876. mbus_fmt->swapped = true;
  877. mbus_fmt->bpp = 16;
  878. break;
  879. case MEDIA_BUS_FMT_UYVY8_2X8:
  880. mbus_fmt->fmt_order = CEU_CAMCR_DTARY_8_UYVY;
  881. mbus_fmt->fmt_order_swap = CEU_CAMCR_DTARY_8_VYUY;
  882. mbus_fmt->swapped = false;
  883. mbus_fmt->bpp = 16;
  884. break;
  885. case MEDIA_BUS_FMT_VYUY8_2X8:
  886. mbus_fmt->fmt_order = CEU_CAMCR_DTARY_8_VYUY;
  887. mbus_fmt->fmt_order_swap = CEU_CAMCR_DTARY_8_UYVY;
  888. mbus_fmt->swapped = true;
  889. mbus_fmt->bpp = 16;
  890. break;
  891. }
  892. return 0;
  893. }
  894. /* --- Runtime PM Handlers --- */
  895. /*
  896. * ceu_runtime_resume() - soft-reset the interface and turn sensor power on.
  897. */
  898. static int __maybe_unused ceu_runtime_resume(struct device *dev)
  899. {
  900. struct ceu_device *ceudev = dev_get_drvdata(dev);
  901. struct v4l2_subdev *v4l2_sd = ceudev->sd->v4l2_sd;
  902. v4l2_subdev_call(v4l2_sd, core, s_power, 1);
  903. ceu_soft_reset(ceudev);
  904. return 0;
  905. }
  906. /*
  907. * ceu_runtime_suspend() - disable capture and interrupts and soft-reset.
  908. * Turn sensor power off.
  909. */
  910. static int __maybe_unused ceu_runtime_suspend(struct device *dev)
  911. {
  912. struct ceu_device *ceudev = dev_get_drvdata(dev);
  913. struct v4l2_subdev *v4l2_sd = ceudev->sd->v4l2_sd;
  914. v4l2_subdev_call(v4l2_sd, core, s_power, 0);
  915. ceu_write(ceudev, CEU_CEIER, 0);
  916. ceu_soft_reset(ceudev);
  917. return 0;
  918. }
  919. /* --- File Operations --- */
  920. static int ceu_open(struct file *file)
  921. {
  922. struct ceu_device *ceudev = video_drvdata(file);
  923. int ret;
  924. ret = v4l2_fh_open(file);
  925. if (ret)
  926. return ret;
  927. mutex_lock(&ceudev->mlock);
  928. /* Causes soft-reset and sensor power on on first open */
  929. ret = pm_runtime_resume_and_get(ceudev->dev);
  930. mutex_unlock(&ceudev->mlock);
  931. return ret;
  932. }
  933. static int ceu_release(struct file *file)
  934. {
  935. struct ceu_device *ceudev = video_drvdata(file);
  936. vb2_fop_release(file);
  937. mutex_lock(&ceudev->mlock);
  938. /* Causes soft-reset and sensor power down on last close */
  939. pm_runtime_put(ceudev->dev);
  940. mutex_unlock(&ceudev->mlock);
  941. return 0;
  942. }
  943. static const struct v4l2_file_operations ceu_fops = {
  944. .owner = THIS_MODULE,
  945. .open = ceu_open,
  946. .release = ceu_release,
  947. .unlocked_ioctl = video_ioctl2,
  948. .mmap = vb2_fop_mmap,
  949. .poll = vb2_fop_poll,
  950. };
  951. /* --- Video Device IOCTLs --- */
  952. static int ceu_querycap(struct file *file, void *priv,
  953. struct v4l2_capability *cap)
  954. {
  955. struct ceu_device *ceudev = video_drvdata(file);
  956. strscpy(cap->card, "Renesas CEU", sizeof(cap->card));
  957. strscpy(cap->driver, DRIVER_NAME, sizeof(cap->driver));
  958. snprintf(cap->bus_info, sizeof(cap->bus_info),
  959. "platform:renesas-ceu-%s", dev_name(ceudev->dev));
  960. return 0;
  961. }
  962. static int ceu_enum_fmt_vid_cap(struct file *file, void *priv,
  963. struct v4l2_fmtdesc *f)
  964. {
  965. const struct ceu_fmt *fmt;
  966. if (f->index >= ARRAY_SIZE(ceu_fmt_list))
  967. return -EINVAL;
  968. fmt = &ceu_fmt_list[f->index];
  969. f->pixelformat = fmt->fourcc;
  970. return 0;
  971. }
  972. static int ceu_try_fmt_vid_cap(struct file *file, void *priv,
  973. struct v4l2_format *f)
  974. {
  975. struct ceu_device *ceudev = video_drvdata(file);
  976. return ceu_try_fmt(ceudev, f);
  977. }
  978. static int ceu_s_fmt_vid_cap(struct file *file, void *priv,
  979. struct v4l2_format *f)
  980. {
  981. struct ceu_device *ceudev = video_drvdata(file);
  982. if (vb2_is_streaming(&ceudev->vb2_vq))
  983. return -EBUSY;
  984. return ceu_set_fmt(ceudev, f);
  985. }
  986. static int ceu_g_fmt_vid_cap(struct file *file, void *priv,
  987. struct v4l2_format *f)
  988. {
  989. struct ceu_device *ceudev = video_drvdata(file);
  990. f->fmt.pix_mp = ceudev->v4l2_pix;
  991. return 0;
  992. }
  993. static int ceu_enum_input(struct file *file, void *priv,
  994. struct v4l2_input *inp)
  995. {
  996. struct ceu_device *ceudev = video_drvdata(file);
  997. struct ceu_subdev *ceusd;
  998. if (inp->index >= ceudev->num_sd)
  999. return -EINVAL;
  1000. ceusd = ceudev->subdevs[inp->index];
  1001. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1002. inp->std = 0;
  1003. snprintf(inp->name, sizeof(inp->name), "Camera%u: %s",
  1004. inp->index, ceusd->v4l2_sd->name);
  1005. return 0;
  1006. }
  1007. static int ceu_g_input(struct file *file, void *priv, unsigned int *i)
  1008. {
  1009. struct ceu_device *ceudev = video_drvdata(file);
  1010. *i = ceudev->sd_index;
  1011. return 0;
  1012. }
  1013. static int ceu_s_input(struct file *file, void *priv, unsigned int i)
  1014. {
  1015. struct ceu_device *ceudev = video_drvdata(file);
  1016. struct ceu_subdev *ceu_sd_old;
  1017. int ret;
  1018. if (i >= ceudev->num_sd)
  1019. return -EINVAL;
  1020. if (vb2_is_streaming(&ceudev->vb2_vq))
  1021. return -EBUSY;
  1022. if (i == ceudev->sd_index)
  1023. return 0;
  1024. ceu_sd_old = ceudev->sd;
  1025. ceudev->sd = ceudev->subdevs[i];
  1026. /*
  1027. * Make sure we can generate output image formats and apply
  1028. * default one.
  1029. */
  1030. ret = ceu_init_mbus_fmt(ceudev);
  1031. if (ret) {
  1032. ceudev->sd = ceu_sd_old;
  1033. return -EINVAL;
  1034. }
  1035. ret = ceu_set_default_fmt(ceudev);
  1036. if (ret) {
  1037. ceudev->sd = ceu_sd_old;
  1038. return -EINVAL;
  1039. }
  1040. /* Now that we're sure we can use the sensor, power off the old one. */
  1041. v4l2_subdev_call(ceu_sd_old->v4l2_sd, core, s_power, 0);
  1042. v4l2_subdev_call(ceudev->sd->v4l2_sd, core, s_power, 1);
  1043. ceudev->sd_index = i;
  1044. return 0;
  1045. }
  1046. static int ceu_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1047. {
  1048. struct ceu_device *ceudev = video_drvdata(file);
  1049. return v4l2_g_parm_cap(video_devdata(file), ceudev->sd->v4l2_sd, a);
  1050. }
  1051. static int ceu_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  1052. {
  1053. struct ceu_device *ceudev = video_drvdata(file);
  1054. return v4l2_s_parm_cap(video_devdata(file), ceudev->sd->v4l2_sd, a);
  1055. }
  1056. static int ceu_enum_framesizes(struct file *file, void *fh,
  1057. struct v4l2_frmsizeenum *fsize)
  1058. {
  1059. struct ceu_device *ceudev = video_drvdata(file);
  1060. struct ceu_subdev *ceu_sd = ceudev->sd;
  1061. const struct ceu_fmt *ceu_fmt;
  1062. struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
  1063. int ret;
  1064. struct v4l2_subdev_frame_size_enum fse = {
  1065. .code = ceu_sd->mbus_fmt.mbus_code,
  1066. .index = fsize->index,
  1067. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1068. };
  1069. /* Just check if user supplied pixel format is supported. */
  1070. ceu_fmt = get_ceu_fmt_from_fourcc(fsize->pixel_format);
  1071. if (!ceu_fmt)
  1072. return -EINVAL;
  1073. ret = v4l2_subdev_call(v4l2_sd, pad, enum_frame_size,
  1074. NULL, &fse);
  1075. if (ret)
  1076. return ret;
  1077. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1078. fsize->discrete.width = CEU_W_MAX(fse.max_width);
  1079. fsize->discrete.height = CEU_H_MAX(fse.max_height);
  1080. return 0;
  1081. }
  1082. static int ceu_enum_frameintervals(struct file *file, void *fh,
  1083. struct v4l2_frmivalenum *fival)
  1084. {
  1085. struct ceu_device *ceudev = video_drvdata(file);
  1086. struct ceu_subdev *ceu_sd = ceudev->sd;
  1087. const struct ceu_fmt *ceu_fmt;
  1088. struct v4l2_subdev *v4l2_sd = ceu_sd->v4l2_sd;
  1089. int ret;
  1090. struct v4l2_subdev_frame_interval_enum fie = {
  1091. .code = ceu_sd->mbus_fmt.mbus_code,
  1092. .index = fival->index,
  1093. .width = fival->width,
  1094. .height = fival->height,
  1095. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1096. };
  1097. /* Just check if user supplied pixel format is supported. */
  1098. ceu_fmt = get_ceu_fmt_from_fourcc(fival->pixel_format);
  1099. if (!ceu_fmt)
  1100. return -EINVAL;
  1101. ret = v4l2_subdev_call(v4l2_sd, pad, enum_frame_interval, NULL,
  1102. &fie);
  1103. if (ret)
  1104. return ret;
  1105. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1106. fival->discrete = fie.interval;
  1107. return 0;
  1108. }
  1109. static const struct v4l2_ioctl_ops ceu_ioctl_ops = {
  1110. .vidioc_querycap = ceu_querycap,
  1111. .vidioc_enum_fmt_vid_cap = ceu_enum_fmt_vid_cap,
  1112. .vidioc_try_fmt_vid_cap_mplane = ceu_try_fmt_vid_cap,
  1113. .vidioc_s_fmt_vid_cap_mplane = ceu_s_fmt_vid_cap,
  1114. .vidioc_g_fmt_vid_cap_mplane = ceu_g_fmt_vid_cap,
  1115. .vidioc_enum_input = ceu_enum_input,
  1116. .vidioc_g_input = ceu_g_input,
  1117. .vidioc_s_input = ceu_s_input,
  1118. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1119. .vidioc_querybuf = vb2_ioctl_querybuf,
  1120. .vidioc_qbuf = vb2_ioctl_qbuf,
  1121. .vidioc_expbuf = vb2_ioctl_expbuf,
  1122. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1123. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1124. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1125. .vidioc_streamon = vb2_ioctl_streamon,
  1126. .vidioc_streamoff = vb2_ioctl_streamoff,
  1127. .vidioc_g_parm = ceu_g_parm,
  1128. .vidioc_s_parm = ceu_s_parm,
  1129. .vidioc_enum_framesizes = ceu_enum_framesizes,
  1130. .vidioc_enum_frameintervals = ceu_enum_frameintervals,
  1131. .vidioc_log_status = v4l2_ctrl_log_status,
  1132. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1133. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1134. };
  1135. /*
  1136. * ceu_vdev_release() - release CEU video device memory when last reference
  1137. * to this driver is closed
  1138. */
  1139. static void ceu_vdev_release(struct video_device *vdev)
  1140. {
  1141. struct ceu_device *ceudev = video_get_drvdata(vdev);
  1142. kfree(ceudev);
  1143. }
  1144. static int ceu_notify_bound(struct v4l2_async_notifier *notifier,
  1145. struct v4l2_subdev *v4l2_sd,
  1146. struct v4l2_async_subdev *asd)
  1147. {
  1148. struct v4l2_device *v4l2_dev = notifier->v4l2_dev;
  1149. struct ceu_device *ceudev = v4l2_to_ceu(v4l2_dev);
  1150. struct ceu_subdev *ceu_sd = to_ceu_subdev(asd);
  1151. ceu_sd->v4l2_sd = v4l2_sd;
  1152. ceudev->num_sd++;
  1153. return 0;
  1154. }
  1155. static int ceu_notify_complete(struct v4l2_async_notifier *notifier)
  1156. {
  1157. struct v4l2_device *v4l2_dev = notifier->v4l2_dev;
  1158. struct ceu_device *ceudev = v4l2_to_ceu(v4l2_dev);
  1159. struct video_device *vdev = &ceudev->vdev;
  1160. struct vb2_queue *q = &ceudev->vb2_vq;
  1161. struct v4l2_subdev *v4l2_sd;
  1162. int ret;
  1163. /* Initialize vb2 queue. */
  1164. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1165. q->io_modes = VB2_MMAP | VB2_DMABUF;
  1166. q->drv_priv = ceudev;
  1167. q->ops = &ceu_vb2_ops;
  1168. q->mem_ops = &vb2_dma_contig_memops;
  1169. q->buf_struct_size = sizeof(struct ceu_buffer);
  1170. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1171. q->min_buffers_needed = 2;
  1172. q->lock = &ceudev->mlock;
  1173. q->dev = ceudev->v4l2_dev.dev;
  1174. ret = vb2_queue_init(q);
  1175. if (ret)
  1176. return ret;
  1177. /*
  1178. * Make sure at least one sensor is primary and use it to initialize
  1179. * ceu formats.
  1180. */
  1181. if (!ceudev->sd) {
  1182. ceudev->sd = ceudev->subdevs[0];
  1183. ceudev->sd_index = 0;
  1184. }
  1185. v4l2_sd = ceudev->sd->v4l2_sd;
  1186. ret = ceu_init_mbus_fmt(ceudev);
  1187. if (ret)
  1188. return ret;
  1189. ret = ceu_set_default_fmt(ceudev);
  1190. if (ret)
  1191. return ret;
  1192. /* Register the video device. */
  1193. strscpy(vdev->name, DRIVER_NAME, sizeof(vdev->name));
  1194. vdev->v4l2_dev = v4l2_dev;
  1195. vdev->lock = &ceudev->mlock;
  1196. vdev->queue = &ceudev->vb2_vq;
  1197. vdev->ctrl_handler = v4l2_sd->ctrl_handler;
  1198. vdev->fops = &ceu_fops;
  1199. vdev->ioctl_ops = &ceu_ioctl_ops;
  1200. vdev->release = ceu_vdev_release;
  1201. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
  1202. V4L2_CAP_STREAMING;
  1203. video_set_drvdata(vdev, ceudev);
  1204. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  1205. if (ret < 0) {
  1206. v4l2_err(vdev->v4l2_dev,
  1207. "video_register_device failed: %d\n", ret);
  1208. return ret;
  1209. }
  1210. return 0;
  1211. }
  1212. static const struct v4l2_async_notifier_operations ceu_notify_ops = {
  1213. .bound = ceu_notify_bound,
  1214. .complete = ceu_notify_complete,
  1215. };
  1216. /*
  1217. * ceu_init_async_subdevs() - Initialize CEU subdevices and async_subdevs in
  1218. * ceu device. Both DT and platform data parsing use
  1219. * this routine.
  1220. *
  1221. * Returns 0 for success, -ENOMEM for failure.
  1222. */
  1223. static int ceu_init_async_subdevs(struct ceu_device *ceudev, unsigned int n_sd)
  1224. {
  1225. /* Reserve memory for 'n_sd' ceu_subdev descriptors. */
  1226. ceudev->subdevs = devm_kcalloc(ceudev->dev, n_sd,
  1227. sizeof(*ceudev->subdevs), GFP_KERNEL);
  1228. if (!ceudev->subdevs)
  1229. return -ENOMEM;
  1230. ceudev->sd = NULL;
  1231. ceudev->sd_index = 0;
  1232. ceudev->num_sd = 0;
  1233. return 0;
  1234. }
  1235. /*
  1236. * ceu_parse_platform_data() - Initialize async_subdevices using platform
  1237. * device provided data.
  1238. */
  1239. static int ceu_parse_platform_data(struct ceu_device *ceudev,
  1240. const struct ceu_platform_data *pdata)
  1241. {
  1242. const struct ceu_async_subdev *async_sd;
  1243. struct ceu_subdev *ceu_sd;
  1244. unsigned int i;
  1245. int ret;
  1246. if (pdata->num_subdevs == 0)
  1247. return -ENODEV;
  1248. ret = ceu_init_async_subdevs(ceudev, pdata->num_subdevs);
  1249. if (ret)
  1250. return ret;
  1251. for (i = 0; i < pdata->num_subdevs; i++) {
  1252. /* Setup the ceu subdevice and the async subdevice. */
  1253. async_sd = &pdata->subdevs[i];
  1254. ceu_sd = v4l2_async_nf_add_i2c(&ceudev->notifier,
  1255. async_sd->i2c_adapter_id,
  1256. async_sd->i2c_address,
  1257. struct ceu_subdev);
  1258. if (IS_ERR(ceu_sd)) {
  1259. v4l2_async_nf_cleanup(&ceudev->notifier);
  1260. return PTR_ERR(ceu_sd);
  1261. }
  1262. ceu_sd->mbus_flags = async_sd->flags;
  1263. ceudev->subdevs[i] = ceu_sd;
  1264. }
  1265. return pdata->num_subdevs;
  1266. }
  1267. /*
  1268. * ceu_parse_dt() - Initialize async_subdevs parsing device tree graph.
  1269. */
  1270. static int ceu_parse_dt(struct ceu_device *ceudev)
  1271. {
  1272. struct device_node *of = ceudev->dev->of_node;
  1273. struct device_node *ep;
  1274. struct ceu_subdev *ceu_sd;
  1275. unsigned int i;
  1276. int num_ep;
  1277. int ret;
  1278. num_ep = of_graph_get_endpoint_count(of);
  1279. if (!num_ep)
  1280. return -ENODEV;
  1281. ret = ceu_init_async_subdevs(ceudev, num_ep);
  1282. if (ret)
  1283. return ret;
  1284. for (i = 0; i < num_ep; i++) {
  1285. struct v4l2_fwnode_endpoint fw_ep = {
  1286. .bus_type = V4L2_MBUS_PARALLEL,
  1287. .bus = {
  1288. .parallel = {
  1289. .flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  1290. V4L2_MBUS_VSYNC_ACTIVE_HIGH,
  1291. .bus_width = 8,
  1292. },
  1293. },
  1294. };
  1295. ep = of_graph_get_endpoint_by_regs(of, 0, i);
  1296. if (!ep) {
  1297. dev_err(ceudev->dev,
  1298. "No subdevice connected on endpoint %u.\n", i);
  1299. ret = -ENODEV;
  1300. goto error_cleanup;
  1301. }
  1302. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &fw_ep);
  1303. if (ret) {
  1304. dev_err(ceudev->dev,
  1305. "Unable to parse endpoint #%u: %d.\n", i, ret);
  1306. goto error_cleanup;
  1307. }
  1308. /* Setup the ceu subdevice and the async subdevice. */
  1309. ceu_sd = v4l2_async_nf_add_fwnode_remote(&ceudev->notifier,
  1310. of_fwnode_handle(ep),
  1311. struct ceu_subdev);
  1312. if (IS_ERR(ceu_sd)) {
  1313. ret = PTR_ERR(ceu_sd);
  1314. goto error_cleanup;
  1315. }
  1316. ceu_sd->mbus_flags = fw_ep.bus.parallel.flags;
  1317. ceudev->subdevs[i] = ceu_sd;
  1318. of_node_put(ep);
  1319. }
  1320. return num_ep;
  1321. error_cleanup:
  1322. v4l2_async_nf_cleanup(&ceudev->notifier);
  1323. of_node_put(ep);
  1324. return ret;
  1325. }
  1326. /*
  1327. * struct ceu_data - Platform specific CEU data
  1328. * @irq_mask: CETCR mask with all interrupt sources enabled. The mask differs
  1329. * between SH4 and RZ platforms.
  1330. */
  1331. struct ceu_data {
  1332. u32 irq_mask;
  1333. };
  1334. static const struct ceu_data ceu_data_sh4 = {
  1335. .irq_mask = CEU_CETCR_ALL_IRQS_SH4,
  1336. };
  1337. #if IS_ENABLED(CONFIG_OF)
  1338. static const struct ceu_data ceu_data_rz = {
  1339. .irq_mask = CEU_CETCR_ALL_IRQS_RZ,
  1340. };
  1341. static const struct of_device_id ceu_of_match[] = {
  1342. { .compatible = "renesas,r7s72100-ceu", .data = &ceu_data_rz },
  1343. { .compatible = "renesas,r8a7740-ceu", .data = &ceu_data_rz },
  1344. { }
  1345. };
  1346. MODULE_DEVICE_TABLE(of, ceu_of_match);
  1347. #endif
  1348. static int ceu_probe(struct platform_device *pdev)
  1349. {
  1350. struct device *dev = &pdev->dev;
  1351. const struct ceu_data *ceu_data;
  1352. struct ceu_device *ceudev;
  1353. unsigned int irq;
  1354. int num_subdevs;
  1355. int ret;
  1356. ceudev = kzalloc(sizeof(*ceudev), GFP_KERNEL);
  1357. if (!ceudev)
  1358. return -ENOMEM;
  1359. platform_set_drvdata(pdev, ceudev);
  1360. ceudev->dev = dev;
  1361. INIT_LIST_HEAD(&ceudev->capture);
  1362. spin_lock_init(&ceudev->lock);
  1363. mutex_init(&ceudev->mlock);
  1364. ceudev->base = devm_platform_ioremap_resource(pdev, 0);
  1365. if (IS_ERR(ceudev->base)) {
  1366. ret = PTR_ERR(ceudev->base);
  1367. goto error_free_ceudev;
  1368. }
  1369. ret = platform_get_irq(pdev, 0);
  1370. if (ret < 0)
  1371. goto error_free_ceudev;
  1372. irq = ret;
  1373. ret = devm_request_irq(dev, irq, ceu_irq,
  1374. 0, dev_name(dev), ceudev);
  1375. if (ret) {
  1376. dev_err(&pdev->dev, "Unable to request CEU interrupt.\n");
  1377. goto error_free_ceudev;
  1378. }
  1379. pm_runtime_enable(dev);
  1380. ret = v4l2_device_register(dev, &ceudev->v4l2_dev);
  1381. if (ret)
  1382. goto error_pm_disable;
  1383. v4l2_async_nf_init(&ceudev->notifier);
  1384. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  1385. ceu_data = of_device_get_match_data(dev);
  1386. num_subdevs = ceu_parse_dt(ceudev);
  1387. } else if (dev->platform_data) {
  1388. /* Assume SH4 if booting with platform data. */
  1389. ceu_data = &ceu_data_sh4;
  1390. num_subdevs = ceu_parse_platform_data(ceudev,
  1391. dev->platform_data);
  1392. } else {
  1393. num_subdevs = -EINVAL;
  1394. }
  1395. if (num_subdevs < 0) {
  1396. ret = num_subdevs;
  1397. goto error_v4l2_unregister;
  1398. }
  1399. ceudev->irq_mask = ceu_data->irq_mask;
  1400. ceudev->notifier.v4l2_dev = &ceudev->v4l2_dev;
  1401. ceudev->notifier.ops = &ceu_notify_ops;
  1402. ret = v4l2_async_nf_register(&ceudev->v4l2_dev, &ceudev->notifier);
  1403. if (ret)
  1404. goto error_cleanup;
  1405. dev_info(dev, "Renesas Capture Engine Unit %s\n", dev_name(dev));
  1406. return 0;
  1407. error_cleanup:
  1408. v4l2_async_nf_cleanup(&ceudev->notifier);
  1409. error_v4l2_unregister:
  1410. v4l2_device_unregister(&ceudev->v4l2_dev);
  1411. error_pm_disable:
  1412. pm_runtime_disable(dev);
  1413. error_free_ceudev:
  1414. kfree(ceudev);
  1415. return ret;
  1416. }
  1417. static int ceu_remove(struct platform_device *pdev)
  1418. {
  1419. struct ceu_device *ceudev = platform_get_drvdata(pdev);
  1420. pm_runtime_disable(ceudev->dev);
  1421. v4l2_async_nf_unregister(&ceudev->notifier);
  1422. v4l2_async_nf_cleanup(&ceudev->notifier);
  1423. v4l2_device_unregister(&ceudev->v4l2_dev);
  1424. video_unregister_device(&ceudev->vdev);
  1425. return 0;
  1426. }
  1427. static const struct dev_pm_ops ceu_pm_ops = {
  1428. SET_RUNTIME_PM_OPS(ceu_runtime_suspend,
  1429. ceu_runtime_resume,
  1430. NULL)
  1431. };
  1432. static struct platform_driver ceu_driver = {
  1433. .driver = {
  1434. .name = DRIVER_NAME,
  1435. .pm = &ceu_pm_ops,
  1436. .of_match_table = of_match_ptr(ceu_of_match),
  1437. },
  1438. .probe = ceu_probe,
  1439. .remove = ceu_remove,
  1440. };
  1441. module_platform_driver(ceu_driver);
  1442. MODULE_DESCRIPTION("Renesas CEU camera driver");
  1443. MODULE_AUTHOR("Jacopo Mondi <[email protected]>");
  1444. MODULE_LICENSE("GPL v2");