stm32-dcmi.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for STM32 Digital Camera Memory Interface
  4. *
  5. * Copyright (C) STMicroelectronics SA 2017
  6. * Authors: Yannick Fertre <yannick.fertre@st.com>
  7. * Hugues Fruchet <hugues.fruchet@st.com>
  8. * for STMicroelectronics.
  9. *
  10. * This driver is based on atmel_isi.c
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/dmaengine.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_graph.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/reset.h>
  28. #include <linux/videodev2.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-dev.h>
  31. #include <media/v4l2-device.h>
  32. #include <media/v4l2-event.h>
  33. #include <media/v4l2-fwnode.h>
  34. #include <media/v4l2-image-sizes.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-rect.h>
  37. #include <media/videobuf2-dma-contig.h>
  38. #define DRV_NAME "stm32-dcmi"
  39. /* Registers offset for DCMI */
  40. #define DCMI_CR 0x00 /* Control Register */
  41. #define DCMI_SR 0x04 /* Status Register */
  42. #define DCMI_RIS 0x08 /* Raw Interrupt Status register */
  43. #define DCMI_IER 0x0C /* Interrupt Enable Register */
  44. #define DCMI_MIS 0x10 /* Masked Interrupt Status register */
  45. #define DCMI_ICR 0x14 /* Interrupt Clear Register */
  46. #define DCMI_ESCR 0x18 /* Embedded Synchronization Code Register */
  47. #define DCMI_ESUR 0x1C /* Embedded Synchronization Unmask Register */
  48. #define DCMI_CWSTRT 0x20 /* Crop Window STaRT */
  49. #define DCMI_CWSIZE 0x24 /* Crop Window SIZE */
  50. #define DCMI_DR 0x28 /* Data Register */
  51. #define DCMI_IDR 0x2C /* IDentifier Register */
  52. /* Bits definition for control register (DCMI_CR) */
  53. #define CR_CAPTURE BIT(0)
  54. #define CR_CM BIT(1)
  55. #define CR_CROP BIT(2)
  56. #define CR_JPEG BIT(3)
  57. #define CR_ESS BIT(4)
  58. #define CR_PCKPOL BIT(5)
  59. #define CR_HSPOL BIT(6)
  60. #define CR_VSPOL BIT(7)
  61. #define CR_FCRC_0 BIT(8)
  62. #define CR_FCRC_1 BIT(9)
  63. #define CR_EDM_0 BIT(10)
  64. #define CR_EDM_1 BIT(11)
  65. #define CR_ENABLE BIT(14)
  66. /* Bits definition for status register (DCMI_SR) */
  67. #define SR_HSYNC BIT(0)
  68. #define SR_VSYNC BIT(1)
  69. #define SR_FNE BIT(2)
  70. /*
  71. * Bits definition for interrupt registers
  72. * (DCMI_RIS, DCMI_IER, DCMI_MIS, DCMI_ICR)
  73. */
  74. #define IT_FRAME BIT(0)
  75. #define IT_OVR BIT(1)
  76. #define IT_ERR BIT(2)
  77. #define IT_VSYNC BIT(3)
  78. #define IT_LINE BIT(4)
  79. enum state {
  80. STOPPED = 0,
  81. WAIT_FOR_BUFFER,
  82. RUNNING,
  83. };
  84. #define MIN_WIDTH 16U
  85. #define MAX_WIDTH 2592U
  86. #define MIN_HEIGHT 16U
  87. #define MAX_HEIGHT 2592U
  88. #define TIMEOUT_MS 1000
  89. struct dcmi_graph_entity {
  90. struct device_node *node;
  91. struct v4l2_async_subdev asd;
  92. struct v4l2_subdev *subdev;
  93. };
  94. struct dcmi_format {
  95. u32 fourcc;
  96. u32 mbus_code;
  97. u8 bpp;
  98. };
  99. struct dcmi_framesize {
  100. u32 width;
  101. u32 height;
  102. };
  103. struct dcmi_buf {
  104. struct vb2_v4l2_buffer vb;
  105. bool prepared;
  106. dma_addr_t paddr;
  107. size_t size;
  108. struct list_head list;
  109. };
  110. struct stm32_dcmi {
  111. /* Protects the access of variables shared within the interrupt */
  112. spinlock_t irqlock;
  113. struct device *dev;
  114. void __iomem *regs;
  115. struct resource *res;
  116. struct reset_control *rstc;
  117. int sequence;
  118. struct list_head buffers;
  119. struct dcmi_buf *active;
  120. struct v4l2_device v4l2_dev;
  121. struct video_device *vdev;
  122. struct v4l2_async_notifier notifier;
  123. struct dcmi_graph_entity entity;
  124. struct v4l2_format fmt;
  125. struct v4l2_rect crop;
  126. bool do_crop;
  127. const struct dcmi_format **sd_formats;
  128. unsigned int num_of_sd_formats;
  129. const struct dcmi_format *sd_format;
  130. struct dcmi_framesize *sd_framesizes;
  131. unsigned int num_of_sd_framesizes;
  132. struct dcmi_framesize sd_framesize;
  133. struct v4l2_rect sd_bounds;
  134. /* Protect this data structure */
  135. struct mutex lock;
  136. struct vb2_queue queue;
  137. struct v4l2_fwnode_bus_parallel bus;
  138. struct completion complete;
  139. struct clk *mclk;
  140. enum state state;
  141. struct dma_chan *dma_chan;
  142. dma_cookie_t dma_cookie;
  143. u32 misr;
  144. int errors_count;
  145. int overrun_count;
  146. int buffers_count;
  147. };
  148. static inline struct stm32_dcmi *notifier_to_dcmi(struct v4l2_async_notifier *n)
  149. {
  150. return container_of(n, struct stm32_dcmi, notifier);
  151. }
  152. static inline u32 reg_read(void __iomem *base, u32 reg)
  153. {
  154. return readl_relaxed(base + reg);
  155. }
  156. static inline void reg_write(void __iomem *base, u32 reg, u32 val)
  157. {
  158. writel_relaxed(val, base + reg);
  159. }
  160. static inline void reg_set(void __iomem *base, u32 reg, u32 mask)
  161. {
  162. reg_write(base, reg, reg_read(base, reg) | mask);
  163. }
  164. static inline void reg_clear(void __iomem *base, u32 reg, u32 mask)
  165. {
  166. reg_write(base, reg, reg_read(base, reg) & ~mask);
  167. }
  168. static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf);
  169. static void dcmi_buffer_done(struct stm32_dcmi *dcmi,
  170. struct dcmi_buf *buf,
  171. size_t bytesused,
  172. int err)
  173. {
  174. struct vb2_v4l2_buffer *vbuf;
  175. if (!buf)
  176. return;
  177. list_del_init(&buf->list);
  178. vbuf = &buf->vb;
  179. vbuf->sequence = dcmi->sequence++;
  180. vbuf->field = V4L2_FIELD_NONE;
  181. vbuf->vb2_buf.timestamp = ktime_get_ns();
  182. vb2_set_plane_payload(&vbuf->vb2_buf, 0, bytesused);
  183. vb2_buffer_done(&vbuf->vb2_buf,
  184. err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  185. dev_dbg(dcmi->dev, "buffer[%d] done seq=%d, bytesused=%zu\n",
  186. vbuf->vb2_buf.index, vbuf->sequence, bytesused);
  187. dcmi->buffers_count++;
  188. dcmi->active = NULL;
  189. }
  190. static int dcmi_restart_capture(struct stm32_dcmi *dcmi)
  191. {
  192. struct dcmi_buf *buf;
  193. spin_lock_irq(&dcmi->irqlock);
  194. if (dcmi->state != RUNNING) {
  195. spin_unlock_irq(&dcmi->irqlock);
  196. return -EINVAL;
  197. }
  198. /* Restart a new DMA transfer with next buffer */
  199. if (list_empty(&dcmi->buffers)) {
  200. dev_dbg(dcmi->dev, "Capture restart is deferred to next buffer queueing\n");
  201. dcmi->state = WAIT_FOR_BUFFER;
  202. spin_unlock_irq(&dcmi->irqlock);
  203. return 0;
  204. }
  205. buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
  206. dcmi->active = buf;
  207. spin_unlock_irq(&dcmi->irqlock);
  208. return dcmi_start_capture(dcmi, buf);
  209. }
  210. static void dcmi_dma_callback(void *param)
  211. {
  212. struct stm32_dcmi *dcmi = (struct stm32_dcmi *)param;
  213. struct dma_tx_state state;
  214. enum dma_status status;
  215. struct dcmi_buf *buf = dcmi->active;
  216. spin_lock_irq(&dcmi->irqlock);
  217. /* Check DMA status */
  218. status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state);
  219. switch (status) {
  220. case DMA_IN_PROGRESS:
  221. dev_dbg(dcmi->dev, "%s: Received DMA_IN_PROGRESS\n", __func__);
  222. break;
  223. case DMA_PAUSED:
  224. dev_err(dcmi->dev, "%s: Received DMA_PAUSED\n", __func__);
  225. break;
  226. case DMA_ERROR:
  227. dev_err(dcmi->dev, "%s: Received DMA_ERROR\n", __func__);
  228. /* Return buffer to V4L2 in error state */
  229. dcmi_buffer_done(dcmi, buf, 0, -EIO);
  230. break;
  231. case DMA_COMPLETE:
  232. dev_dbg(dcmi->dev, "%s: Received DMA_COMPLETE\n", __func__);
  233. /* Return buffer to V4L2 */
  234. dcmi_buffer_done(dcmi, buf, buf->size, 0);
  235. spin_unlock_irq(&dcmi->irqlock);
  236. /* Restart capture */
  237. if (dcmi_restart_capture(dcmi))
  238. dev_err(dcmi->dev, "%s: Cannot restart capture on DMA complete\n",
  239. __func__);
  240. return;
  241. default:
  242. dev_err(dcmi->dev, "%s: Received unknown status\n", __func__);
  243. break;
  244. }
  245. spin_unlock_irq(&dcmi->irqlock);
  246. }
  247. static int dcmi_start_dma(struct stm32_dcmi *dcmi,
  248. struct dcmi_buf *buf)
  249. {
  250. struct dma_async_tx_descriptor *desc = NULL;
  251. struct dma_slave_config config;
  252. int ret;
  253. memset(&config, 0, sizeof(config));
  254. config.src_addr = (dma_addr_t)dcmi->res->start + DCMI_DR;
  255. config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  256. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  257. config.dst_maxburst = 4;
  258. /* Configure DMA channel */
  259. ret = dmaengine_slave_config(dcmi->dma_chan, &config);
  260. if (ret < 0) {
  261. dev_err(dcmi->dev, "%s: DMA channel config failed (%d)\n",
  262. __func__, ret);
  263. return ret;
  264. }
  265. /* Prepare a DMA transaction */
  266. desc = dmaengine_prep_slave_single(dcmi->dma_chan, buf->paddr,
  267. buf->size,
  268. DMA_DEV_TO_MEM,
  269. DMA_PREP_INTERRUPT);
  270. if (!desc) {
  271. dev_err(dcmi->dev, "%s: DMA dmaengine_prep_slave_single failed for buffer phy=%pad size=%zu\n",
  272. __func__, &buf->paddr, buf->size);
  273. return -EINVAL;
  274. }
  275. /* Set completion callback routine for notification */
  276. desc->callback = dcmi_dma_callback;
  277. desc->callback_param = dcmi;
  278. /* Push current DMA transaction in the pending queue */
  279. dcmi->dma_cookie = dmaengine_submit(desc);
  280. if (dma_submit_error(dcmi->dma_cookie)) {
  281. dev_err(dcmi->dev, "%s: DMA submission failed\n", __func__);
  282. return -ENXIO;
  283. }
  284. dma_async_issue_pending(dcmi->dma_chan);
  285. return 0;
  286. }
  287. static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf)
  288. {
  289. int ret;
  290. if (!buf)
  291. return -EINVAL;
  292. ret = dcmi_start_dma(dcmi, buf);
  293. if (ret) {
  294. dcmi->errors_count++;
  295. return ret;
  296. }
  297. /* Enable capture */
  298. reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE);
  299. return 0;
  300. }
  301. static void dcmi_set_crop(struct stm32_dcmi *dcmi)
  302. {
  303. u32 size, start;
  304. /* Crop resolution */
  305. size = ((dcmi->crop.height - 1) << 16) |
  306. ((dcmi->crop.width << 1) - 1);
  307. reg_write(dcmi->regs, DCMI_CWSIZE, size);
  308. /* Crop start point */
  309. start = ((dcmi->crop.top) << 16) |
  310. ((dcmi->crop.left << 1));
  311. reg_write(dcmi->regs, DCMI_CWSTRT, start);
  312. dev_dbg(dcmi->dev, "Cropping to %ux%u@%u:%u\n",
  313. dcmi->crop.width, dcmi->crop.height,
  314. dcmi->crop.left, dcmi->crop.top);
  315. /* Enable crop */
  316. reg_set(dcmi->regs, DCMI_CR, CR_CROP);
  317. }
  318. static void dcmi_process_jpeg(struct stm32_dcmi *dcmi)
  319. {
  320. struct dma_tx_state state;
  321. enum dma_status status;
  322. struct dcmi_buf *buf = dcmi->active;
  323. if (!buf)
  324. return;
  325. /*
  326. * Because of variable JPEG buffer size sent by sensor,
  327. * DMA transfer never completes due to transfer size never reached.
  328. * In order to ensure that all the JPEG data are transferred
  329. * in active buffer memory, DMA is drained.
  330. * Then DMA tx status gives the amount of data transferred
  331. * to memory, which is then returned to V4L2 through the active
  332. * buffer payload.
  333. */
  334. /* Drain DMA */
  335. dmaengine_synchronize(dcmi->dma_chan);
  336. /* Get DMA residue to get JPEG size */
  337. status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state);
  338. if (status != DMA_ERROR && state.residue < buf->size) {
  339. /* Return JPEG buffer to V4L2 with received JPEG buffer size */
  340. dcmi_buffer_done(dcmi, buf, buf->size - state.residue, 0);
  341. } else {
  342. dcmi->errors_count++;
  343. dev_err(dcmi->dev, "%s: Cannot get JPEG size from DMA\n",
  344. __func__);
  345. /* Return JPEG buffer to V4L2 in ERROR state */
  346. dcmi_buffer_done(dcmi, buf, 0, -EIO);
  347. }
  348. /* Abort DMA operation */
  349. dmaengine_terminate_all(dcmi->dma_chan);
  350. /* Restart capture */
  351. if (dcmi_restart_capture(dcmi))
  352. dev_err(dcmi->dev, "%s: Cannot restart capture on JPEG received\n",
  353. __func__);
  354. }
  355. static irqreturn_t dcmi_irq_thread(int irq, void *arg)
  356. {
  357. struct stm32_dcmi *dcmi = arg;
  358. spin_lock_irq(&dcmi->irqlock);
  359. if ((dcmi->misr & IT_OVR) || (dcmi->misr & IT_ERR)) {
  360. dcmi->errors_count++;
  361. if (dcmi->misr & IT_OVR)
  362. dcmi->overrun_count++;
  363. }
  364. if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG &&
  365. dcmi->misr & IT_FRAME) {
  366. /* JPEG received */
  367. spin_unlock_irq(&dcmi->irqlock);
  368. dcmi_process_jpeg(dcmi);
  369. return IRQ_HANDLED;
  370. }
  371. spin_unlock_irq(&dcmi->irqlock);
  372. return IRQ_HANDLED;
  373. }
  374. static irqreturn_t dcmi_irq_callback(int irq, void *arg)
  375. {
  376. struct stm32_dcmi *dcmi = arg;
  377. unsigned long flags;
  378. spin_lock_irqsave(&dcmi->irqlock, flags);
  379. dcmi->misr = reg_read(dcmi->regs, DCMI_MIS);
  380. /* Clear interrupt */
  381. reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR);
  382. spin_unlock_irqrestore(&dcmi->irqlock, flags);
  383. return IRQ_WAKE_THREAD;
  384. }
  385. static int dcmi_queue_setup(struct vb2_queue *vq,
  386. unsigned int *nbuffers,
  387. unsigned int *nplanes,
  388. unsigned int sizes[],
  389. struct device *alloc_devs[])
  390. {
  391. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  392. unsigned int size;
  393. size = dcmi->fmt.fmt.pix.sizeimage;
  394. /* Make sure the image size is large enough */
  395. if (*nplanes)
  396. return sizes[0] < size ? -EINVAL : 0;
  397. *nplanes = 1;
  398. sizes[0] = size;
  399. dev_dbg(dcmi->dev, "Setup queue, count=%d, size=%d\n",
  400. *nbuffers, size);
  401. return 0;
  402. }
  403. static int dcmi_buf_init(struct vb2_buffer *vb)
  404. {
  405. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  406. struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
  407. INIT_LIST_HEAD(&buf->list);
  408. return 0;
  409. }
  410. static int dcmi_buf_prepare(struct vb2_buffer *vb)
  411. {
  412. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
  413. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  414. struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
  415. unsigned long size;
  416. size = dcmi->fmt.fmt.pix.sizeimage;
  417. if (vb2_plane_size(vb, 0) < size) {
  418. dev_err(dcmi->dev, "%s data will not fit into plane (%lu < %lu)\n",
  419. __func__, vb2_plane_size(vb, 0), size);
  420. return -EINVAL;
  421. }
  422. vb2_set_plane_payload(vb, 0, size);
  423. if (!buf->prepared) {
  424. /* Get memory addresses */
  425. buf->paddr =
  426. vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  427. buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0);
  428. buf->prepared = true;
  429. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size);
  430. dev_dbg(dcmi->dev, "buffer[%d] phy=%pad size=%zu\n",
  431. vb->index, &buf->paddr, buf->size);
  432. }
  433. return 0;
  434. }
  435. static void dcmi_buf_queue(struct vb2_buffer *vb)
  436. {
  437. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
  438. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  439. struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
  440. spin_lock_irq(&dcmi->irqlock);
  441. /* Enqueue to video buffers list */
  442. list_add_tail(&buf->list, &dcmi->buffers);
  443. if (dcmi->state == WAIT_FOR_BUFFER) {
  444. dcmi->state = RUNNING;
  445. dcmi->active = buf;
  446. dev_dbg(dcmi->dev, "Starting capture on buffer[%d] queued\n",
  447. buf->vb.vb2_buf.index);
  448. spin_unlock_irq(&dcmi->irqlock);
  449. if (dcmi_start_capture(dcmi, buf))
  450. dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n",
  451. __func__);
  452. return;
  453. }
  454. spin_unlock_irq(&dcmi->irqlock);
  455. }
  456. static int dcmi_start_streaming(struct vb2_queue *vq, unsigned int count)
  457. {
  458. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  459. struct dcmi_buf *buf, *node;
  460. u32 val = 0;
  461. int ret;
  462. ret = pm_runtime_get_sync(dcmi->dev);
  463. if (ret) {
  464. dev_err(dcmi->dev, "%s: Failed to start streaming, cannot get sync\n",
  465. __func__);
  466. goto err_release_buffers;
  467. }
  468. /* Enable stream on the sub device */
  469. ret = v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 1);
  470. if (ret && ret != -ENOIOCTLCMD) {
  471. dev_err(dcmi->dev, "%s: Failed to start streaming, subdev streamon error",
  472. __func__);
  473. goto err_pm_put;
  474. }
  475. spin_lock_irq(&dcmi->irqlock);
  476. /* Set bus width */
  477. switch (dcmi->bus.bus_width) {
  478. case 14:
  479. val |= CR_EDM_0 | CR_EDM_1;
  480. break;
  481. case 12:
  482. val |= CR_EDM_1;
  483. break;
  484. case 10:
  485. val |= CR_EDM_0;
  486. break;
  487. default:
  488. /* Set bus width to 8 bits by default */
  489. break;
  490. }
  491. /* Set vertical synchronization polarity */
  492. if (dcmi->bus.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  493. val |= CR_VSPOL;
  494. /* Set horizontal synchronization polarity */
  495. if (dcmi->bus.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  496. val |= CR_HSPOL;
  497. /* Set pixel clock polarity */
  498. if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  499. val |= CR_PCKPOL;
  500. reg_write(dcmi->regs, DCMI_CR, val);
  501. /* Set crop */
  502. if (dcmi->do_crop)
  503. dcmi_set_crop(dcmi);
  504. /* Enable jpeg capture */
  505. if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG)
  506. reg_set(dcmi->regs, DCMI_CR, CR_CM);/* Snapshot mode */
  507. /* Enable dcmi */
  508. reg_set(dcmi->regs, DCMI_CR, CR_ENABLE);
  509. dcmi->sequence = 0;
  510. dcmi->errors_count = 0;
  511. dcmi->overrun_count = 0;
  512. dcmi->buffers_count = 0;
  513. /*
  514. * Start transfer if at least one buffer has been queued,
  515. * otherwise transfer is deferred at buffer queueing
  516. */
  517. if (list_empty(&dcmi->buffers)) {
  518. dev_dbg(dcmi->dev, "Start streaming is deferred to next buffer queueing\n");
  519. dcmi->state = WAIT_FOR_BUFFER;
  520. spin_unlock_irq(&dcmi->irqlock);
  521. return 0;
  522. }
  523. buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
  524. dcmi->active = buf;
  525. dcmi->state = RUNNING;
  526. dev_dbg(dcmi->dev, "Start streaming, starting capture\n");
  527. spin_unlock_irq(&dcmi->irqlock);
  528. ret = dcmi_start_capture(dcmi, buf);
  529. if (ret) {
  530. dev_err(dcmi->dev, "%s: Start streaming failed, cannot start capture\n",
  531. __func__);
  532. goto err_subdev_streamoff;
  533. }
  534. /* Enable interruptions */
  535. if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG)
  536. reg_set(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
  537. else
  538. reg_set(dcmi->regs, DCMI_IER, IT_OVR | IT_ERR);
  539. return 0;
  540. err_subdev_streamoff:
  541. v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 0);
  542. err_pm_put:
  543. pm_runtime_put(dcmi->dev);
  544. err_release_buffers:
  545. spin_lock_irq(&dcmi->irqlock);
  546. /*
  547. * Return all buffers to vb2 in QUEUED state.
  548. * This will give ownership back to userspace
  549. */
  550. list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
  551. list_del_init(&buf->list);
  552. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  553. }
  554. dcmi->active = NULL;
  555. spin_unlock_irq(&dcmi->irqlock);
  556. return ret;
  557. }
  558. static void dcmi_stop_streaming(struct vb2_queue *vq)
  559. {
  560. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  561. struct dcmi_buf *buf, *node;
  562. int ret;
  563. /* Disable stream on the sub device */
  564. ret = v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 0);
  565. if (ret && ret != -ENOIOCTLCMD)
  566. dev_err(dcmi->dev, "%s: Failed to stop streaming, subdev streamoff error (%d)\n",
  567. __func__, ret);
  568. spin_lock_irq(&dcmi->irqlock);
  569. /* Disable interruptions */
  570. reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
  571. /* Disable DCMI */
  572. reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE);
  573. /* Return all queued buffers to vb2 in ERROR state */
  574. list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
  575. list_del_init(&buf->list);
  576. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  577. }
  578. dcmi->active = NULL;
  579. dcmi->state = STOPPED;
  580. spin_unlock_irq(&dcmi->irqlock);
  581. /* Stop all pending DMA operations */
  582. dmaengine_terminate_all(dcmi->dma_chan);
  583. pm_runtime_put(dcmi->dev);
  584. if (dcmi->errors_count)
  585. dev_warn(dcmi->dev, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n",
  586. dcmi->errors_count, dcmi->overrun_count,
  587. dcmi->buffers_count);
  588. dev_dbg(dcmi->dev, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n",
  589. dcmi->errors_count, dcmi->overrun_count,
  590. dcmi->buffers_count);
  591. }
  592. static const struct vb2_ops dcmi_video_qops = {
  593. .queue_setup = dcmi_queue_setup,
  594. .buf_init = dcmi_buf_init,
  595. .buf_prepare = dcmi_buf_prepare,
  596. .buf_queue = dcmi_buf_queue,
  597. .start_streaming = dcmi_start_streaming,
  598. .stop_streaming = dcmi_stop_streaming,
  599. .wait_prepare = vb2_ops_wait_prepare,
  600. .wait_finish = vb2_ops_wait_finish,
  601. };
  602. static int dcmi_g_fmt_vid_cap(struct file *file, void *priv,
  603. struct v4l2_format *fmt)
  604. {
  605. struct stm32_dcmi *dcmi = video_drvdata(file);
  606. *fmt = dcmi->fmt;
  607. return 0;
  608. }
  609. static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi,
  610. unsigned int fourcc)
  611. {
  612. unsigned int num_formats = dcmi->num_of_sd_formats;
  613. const struct dcmi_format *fmt;
  614. unsigned int i;
  615. for (i = 0; i < num_formats; i++) {
  616. fmt = dcmi->sd_formats[i];
  617. if (fmt->fourcc == fourcc)
  618. return fmt;
  619. }
  620. return NULL;
  621. }
  622. static void __find_outer_frame_size(struct stm32_dcmi *dcmi,
  623. struct v4l2_pix_format *pix,
  624. struct dcmi_framesize *framesize)
  625. {
  626. struct dcmi_framesize *match = NULL;
  627. unsigned int i;
  628. unsigned int min_err = UINT_MAX;
  629. for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
  630. struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
  631. int w_err = (fsize->width - pix->width);
  632. int h_err = (fsize->height - pix->height);
  633. int err = w_err + h_err;
  634. if (w_err >= 0 && h_err >= 0 && err < min_err) {
  635. min_err = err;
  636. match = fsize;
  637. }
  638. }
  639. if (!match)
  640. match = &dcmi->sd_framesizes[0];
  641. *framesize = *match;
  642. }
  643. static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f,
  644. const struct dcmi_format **sd_format,
  645. struct dcmi_framesize *sd_framesize)
  646. {
  647. const struct dcmi_format *sd_fmt;
  648. struct dcmi_framesize sd_fsize;
  649. struct v4l2_pix_format *pix = &f->fmt.pix;
  650. struct v4l2_subdev_pad_config pad_cfg;
  651. struct v4l2_subdev_format format = {
  652. .which = V4L2_SUBDEV_FORMAT_TRY,
  653. };
  654. bool do_crop;
  655. int ret;
  656. sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
  657. if (!sd_fmt) {
  658. sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
  659. pix->pixelformat = sd_fmt->fourcc;
  660. }
  661. /* Limit to hardware capabilities */
  662. pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH);
  663. pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT);
  664. /* No crop if JPEG is requested */
  665. do_crop = dcmi->do_crop && (pix->pixelformat != V4L2_PIX_FMT_JPEG);
  666. if (do_crop && dcmi->num_of_sd_framesizes) {
  667. struct dcmi_framesize outer_sd_fsize;
  668. /*
  669. * If crop is requested and sensor have discrete frame sizes,
  670. * select the frame size that is just larger than request
  671. */
  672. __find_outer_frame_size(dcmi, pix, &outer_sd_fsize);
  673. pix->width = outer_sd_fsize.width;
  674. pix->height = outer_sd_fsize.height;
  675. }
  676. v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
  677. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, set_fmt,
  678. &pad_cfg, &format);
  679. if (ret < 0)
  680. return ret;
  681. /* Update pix regarding to what sensor can do */
  682. v4l2_fill_pix_format(pix, &format.format);
  683. /* Save resolution that sensor can actually do */
  684. sd_fsize.width = pix->width;
  685. sd_fsize.height = pix->height;
  686. if (do_crop) {
  687. struct v4l2_rect c = dcmi->crop;
  688. struct v4l2_rect max_rect;
  689. /*
  690. * Adjust crop by making the intersection between
  691. * format resolution request and crop request
  692. */
  693. max_rect.top = 0;
  694. max_rect.left = 0;
  695. max_rect.width = pix->width;
  696. max_rect.height = pix->height;
  697. v4l2_rect_map_inside(&c, &max_rect);
  698. c.top = clamp_t(s32, c.top, 0, pix->height - c.height);
  699. c.left = clamp_t(s32, c.left, 0, pix->width - c.width);
  700. dcmi->crop = c;
  701. /* Adjust format resolution request to crop */
  702. pix->width = dcmi->crop.width;
  703. pix->height = dcmi->crop.height;
  704. }
  705. pix->field = V4L2_FIELD_NONE;
  706. pix->bytesperline = pix->width * sd_fmt->bpp;
  707. pix->sizeimage = pix->bytesperline * pix->height;
  708. if (sd_format)
  709. *sd_format = sd_fmt;
  710. if (sd_framesize)
  711. *sd_framesize = sd_fsize;
  712. return 0;
  713. }
  714. static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f)
  715. {
  716. struct v4l2_subdev_format format = {
  717. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  718. };
  719. const struct dcmi_format *sd_format;
  720. struct dcmi_framesize sd_framesize;
  721. struct v4l2_mbus_framefmt *mf = &format.format;
  722. struct v4l2_pix_format *pix = &f->fmt.pix;
  723. int ret;
  724. /*
  725. * Try format, fmt.width/height could have been changed
  726. * to match sensor capability or crop request
  727. * sd_format & sd_framesize will contain what subdev
  728. * can do for this request.
  729. */
  730. ret = dcmi_try_fmt(dcmi, f, &sd_format, &sd_framesize);
  731. if (ret)
  732. return ret;
  733. /* Disable crop if JPEG is requested */
  734. if (pix->pixelformat == V4L2_PIX_FMT_JPEG)
  735. dcmi->do_crop = false;
  736. /* pix to mbus format */
  737. v4l2_fill_mbus_format(mf, pix,
  738. sd_format->mbus_code);
  739. mf->width = sd_framesize.width;
  740. mf->height = sd_framesize.height;
  741. ret = v4l2_subdev_call(dcmi->entity.subdev, pad,
  742. set_fmt, NULL, &format);
  743. if (ret < 0)
  744. return ret;
  745. dev_dbg(dcmi->dev, "Sensor format set to 0x%x %ux%u\n",
  746. mf->code, mf->width, mf->height);
  747. dev_dbg(dcmi->dev, "Buffer format set to %4.4s %ux%u\n",
  748. (char *)&pix->pixelformat,
  749. pix->width, pix->height);
  750. dcmi->fmt = *f;
  751. dcmi->sd_format = sd_format;
  752. dcmi->sd_framesize = sd_framesize;
  753. return 0;
  754. }
  755. static int dcmi_s_fmt_vid_cap(struct file *file, void *priv,
  756. struct v4l2_format *f)
  757. {
  758. struct stm32_dcmi *dcmi = video_drvdata(file);
  759. if (vb2_is_streaming(&dcmi->queue))
  760. return -EBUSY;
  761. return dcmi_set_fmt(dcmi, f);
  762. }
  763. static int dcmi_try_fmt_vid_cap(struct file *file, void *priv,
  764. struct v4l2_format *f)
  765. {
  766. struct stm32_dcmi *dcmi = video_drvdata(file);
  767. return dcmi_try_fmt(dcmi, f, NULL, NULL);
  768. }
  769. static int dcmi_enum_fmt_vid_cap(struct file *file, void *priv,
  770. struct v4l2_fmtdesc *f)
  771. {
  772. struct stm32_dcmi *dcmi = video_drvdata(file);
  773. if (f->index >= dcmi->num_of_sd_formats)
  774. return -EINVAL;
  775. f->pixelformat = dcmi->sd_formats[f->index]->fourcc;
  776. return 0;
  777. }
  778. static int dcmi_get_sensor_format(struct stm32_dcmi *dcmi,
  779. struct v4l2_pix_format *pix)
  780. {
  781. struct v4l2_subdev_format fmt = {
  782. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  783. };
  784. int ret;
  785. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, get_fmt, NULL, &fmt);
  786. if (ret)
  787. return ret;
  788. v4l2_fill_pix_format(pix, &fmt.format);
  789. return 0;
  790. }
  791. static int dcmi_set_sensor_format(struct stm32_dcmi *dcmi,
  792. struct v4l2_pix_format *pix)
  793. {
  794. const struct dcmi_format *sd_fmt;
  795. struct v4l2_subdev_format format = {
  796. .which = V4L2_SUBDEV_FORMAT_TRY,
  797. };
  798. struct v4l2_subdev_pad_config pad_cfg;
  799. int ret;
  800. sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
  801. if (!sd_fmt) {
  802. sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
  803. pix->pixelformat = sd_fmt->fourcc;
  804. }
  805. v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
  806. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, set_fmt,
  807. &pad_cfg, &format);
  808. if (ret < 0)
  809. return ret;
  810. return 0;
  811. }
  812. static int dcmi_get_sensor_bounds(struct stm32_dcmi *dcmi,
  813. struct v4l2_rect *r)
  814. {
  815. struct v4l2_subdev_selection bounds = {
  816. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  817. .target = V4L2_SEL_TGT_CROP_BOUNDS,
  818. };
  819. unsigned int max_width, max_height, max_pixsize;
  820. struct v4l2_pix_format pix;
  821. unsigned int i;
  822. int ret;
  823. /*
  824. * Get sensor bounds first
  825. */
  826. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, get_selection,
  827. NULL, &bounds);
  828. if (!ret)
  829. *r = bounds.r;
  830. if (ret != -ENOIOCTLCMD)
  831. return ret;
  832. /*
  833. * If selection is not implemented,
  834. * fallback by enumerating sensor frame sizes
  835. * and take the largest one
  836. */
  837. max_width = 0;
  838. max_height = 0;
  839. max_pixsize = 0;
  840. for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
  841. struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
  842. unsigned int pixsize = fsize->width * fsize->height;
  843. if (pixsize > max_pixsize) {
  844. max_pixsize = pixsize;
  845. max_width = fsize->width;
  846. max_height = fsize->height;
  847. }
  848. }
  849. if (max_pixsize > 0) {
  850. r->top = 0;
  851. r->left = 0;
  852. r->width = max_width;
  853. r->height = max_height;
  854. return 0;
  855. }
  856. /*
  857. * If frame sizes enumeration is not implemented,
  858. * fallback by getting current sensor frame size
  859. */
  860. ret = dcmi_get_sensor_format(dcmi, &pix);
  861. if (ret)
  862. return ret;
  863. r->top = 0;
  864. r->left = 0;
  865. r->width = pix.width;
  866. r->height = pix.height;
  867. return 0;
  868. }
  869. static int dcmi_g_selection(struct file *file, void *fh,
  870. struct v4l2_selection *s)
  871. {
  872. struct stm32_dcmi *dcmi = video_drvdata(file);
  873. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  874. return -EINVAL;
  875. switch (s->target) {
  876. case V4L2_SEL_TGT_CROP_DEFAULT:
  877. case V4L2_SEL_TGT_CROP_BOUNDS:
  878. s->r = dcmi->sd_bounds;
  879. return 0;
  880. case V4L2_SEL_TGT_CROP:
  881. if (dcmi->do_crop) {
  882. s->r = dcmi->crop;
  883. } else {
  884. s->r.top = 0;
  885. s->r.left = 0;
  886. s->r.width = dcmi->fmt.fmt.pix.width;
  887. s->r.height = dcmi->fmt.fmt.pix.height;
  888. }
  889. break;
  890. default:
  891. return -EINVAL;
  892. }
  893. return 0;
  894. }
  895. static int dcmi_s_selection(struct file *file, void *priv,
  896. struct v4l2_selection *s)
  897. {
  898. struct stm32_dcmi *dcmi = video_drvdata(file);
  899. struct v4l2_rect r = s->r;
  900. struct v4l2_rect max_rect;
  901. struct v4l2_pix_format pix;
  902. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  903. s->target != V4L2_SEL_TGT_CROP)
  904. return -EINVAL;
  905. /* Reset sensor resolution to max resolution */
  906. pix.pixelformat = dcmi->fmt.fmt.pix.pixelformat;
  907. pix.width = dcmi->sd_bounds.width;
  908. pix.height = dcmi->sd_bounds.height;
  909. dcmi_set_sensor_format(dcmi, &pix);
  910. /*
  911. * Make the intersection between
  912. * sensor resolution
  913. * and crop request
  914. */
  915. max_rect.top = 0;
  916. max_rect.left = 0;
  917. max_rect.width = pix.width;
  918. max_rect.height = pix.height;
  919. v4l2_rect_map_inside(&r, &max_rect);
  920. r.top = clamp_t(s32, r.top, 0, pix.height - r.height);
  921. r.left = clamp_t(s32, r.left, 0, pix.width - r.width);
  922. if (!(r.top == dcmi->sd_bounds.top &&
  923. r.left == dcmi->sd_bounds.left &&
  924. r.width == dcmi->sd_bounds.width &&
  925. r.height == dcmi->sd_bounds.height)) {
  926. /* Crop if request is different than sensor resolution */
  927. dcmi->do_crop = true;
  928. dcmi->crop = r;
  929. dev_dbg(dcmi->dev, "s_selection: crop %ux%u@(%u,%u) from %ux%u\n",
  930. r.width, r.height, r.left, r.top,
  931. pix.width, pix.height);
  932. } else {
  933. /* Disable crop */
  934. dcmi->do_crop = false;
  935. dev_dbg(dcmi->dev, "s_selection: crop is disabled\n");
  936. }
  937. s->r = r;
  938. return 0;
  939. }
  940. static int dcmi_querycap(struct file *file, void *priv,
  941. struct v4l2_capability *cap)
  942. {
  943. strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
  944. strscpy(cap->card, "STM32 Camera Memory Interface",
  945. sizeof(cap->card));
  946. strscpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info));
  947. return 0;
  948. }
  949. static int dcmi_enum_input(struct file *file, void *priv,
  950. struct v4l2_input *i)
  951. {
  952. if (i->index != 0)
  953. return -EINVAL;
  954. i->type = V4L2_INPUT_TYPE_CAMERA;
  955. strscpy(i->name, "Camera", sizeof(i->name));
  956. return 0;
  957. }
  958. static int dcmi_g_input(struct file *file, void *priv, unsigned int *i)
  959. {
  960. *i = 0;
  961. return 0;
  962. }
  963. static int dcmi_s_input(struct file *file, void *priv, unsigned int i)
  964. {
  965. if (i > 0)
  966. return -EINVAL;
  967. return 0;
  968. }
  969. static int dcmi_enum_framesizes(struct file *file, void *fh,
  970. struct v4l2_frmsizeenum *fsize)
  971. {
  972. struct stm32_dcmi *dcmi = video_drvdata(file);
  973. const struct dcmi_format *sd_fmt;
  974. struct v4l2_subdev_frame_size_enum fse = {
  975. .index = fsize->index,
  976. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  977. };
  978. int ret;
  979. sd_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format);
  980. if (!sd_fmt)
  981. return -EINVAL;
  982. fse.code = sd_fmt->mbus_code;
  983. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, enum_frame_size,
  984. NULL, &fse);
  985. if (ret)
  986. return ret;
  987. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  988. fsize->discrete.width = fse.max_width;
  989. fsize->discrete.height = fse.max_height;
  990. return 0;
  991. }
  992. static int dcmi_g_parm(struct file *file, void *priv,
  993. struct v4l2_streamparm *p)
  994. {
  995. struct stm32_dcmi *dcmi = video_drvdata(file);
  996. return v4l2_g_parm_cap(video_devdata(file), dcmi->entity.subdev, p);
  997. }
  998. static int dcmi_s_parm(struct file *file, void *priv,
  999. struct v4l2_streamparm *p)
  1000. {
  1001. struct stm32_dcmi *dcmi = video_drvdata(file);
  1002. return v4l2_s_parm_cap(video_devdata(file), dcmi->entity.subdev, p);
  1003. }
  1004. static int dcmi_enum_frameintervals(struct file *file, void *fh,
  1005. struct v4l2_frmivalenum *fival)
  1006. {
  1007. struct stm32_dcmi *dcmi = video_drvdata(file);
  1008. const struct dcmi_format *sd_fmt;
  1009. struct v4l2_subdev_frame_interval_enum fie = {
  1010. .index = fival->index,
  1011. .width = fival->width,
  1012. .height = fival->height,
  1013. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1014. };
  1015. int ret;
  1016. sd_fmt = find_format_by_fourcc(dcmi, fival->pixel_format);
  1017. if (!sd_fmt)
  1018. return -EINVAL;
  1019. fie.code = sd_fmt->mbus_code;
  1020. ret = v4l2_subdev_call(dcmi->entity.subdev, pad,
  1021. enum_frame_interval, NULL, &fie);
  1022. if (ret)
  1023. return ret;
  1024. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1025. fival->discrete = fie.interval;
  1026. return 0;
  1027. }
  1028. static const struct of_device_id stm32_dcmi_of_match[] = {
  1029. { .compatible = "st,stm32-dcmi"},
  1030. { /* end node */ },
  1031. };
  1032. MODULE_DEVICE_TABLE(of, stm32_dcmi_of_match);
  1033. static int dcmi_open(struct file *file)
  1034. {
  1035. struct stm32_dcmi *dcmi = video_drvdata(file);
  1036. struct v4l2_subdev *sd = dcmi->entity.subdev;
  1037. int ret;
  1038. if (mutex_lock_interruptible(&dcmi->lock))
  1039. return -ERESTARTSYS;
  1040. ret = v4l2_fh_open(file);
  1041. if (ret < 0)
  1042. goto unlock;
  1043. if (!v4l2_fh_is_singular_file(file))
  1044. goto fh_rel;
  1045. ret = v4l2_subdev_call(sd, core, s_power, 1);
  1046. if (ret < 0 && ret != -ENOIOCTLCMD)
  1047. goto fh_rel;
  1048. ret = dcmi_set_fmt(dcmi, &dcmi->fmt);
  1049. if (ret)
  1050. v4l2_subdev_call(sd, core, s_power, 0);
  1051. fh_rel:
  1052. if (ret)
  1053. v4l2_fh_release(file);
  1054. unlock:
  1055. mutex_unlock(&dcmi->lock);
  1056. return ret;
  1057. }
  1058. static int dcmi_release(struct file *file)
  1059. {
  1060. struct stm32_dcmi *dcmi = video_drvdata(file);
  1061. struct v4l2_subdev *sd = dcmi->entity.subdev;
  1062. bool fh_singular;
  1063. int ret;
  1064. mutex_lock(&dcmi->lock);
  1065. fh_singular = v4l2_fh_is_singular_file(file);
  1066. ret = _vb2_fop_release(file, NULL);
  1067. if (fh_singular)
  1068. v4l2_subdev_call(sd, core, s_power, 0);
  1069. mutex_unlock(&dcmi->lock);
  1070. return ret;
  1071. }
  1072. static const struct v4l2_ioctl_ops dcmi_ioctl_ops = {
  1073. .vidioc_querycap = dcmi_querycap,
  1074. .vidioc_try_fmt_vid_cap = dcmi_try_fmt_vid_cap,
  1075. .vidioc_g_fmt_vid_cap = dcmi_g_fmt_vid_cap,
  1076. .vidioc_s_fmt_vid_cap = dcmi_s_fmt_vid_cap,
  1077. .vidioc_enum_fmt_vid_cap = dcmi_enum_fmt_vid_cap,
  1078. .vidioc_g_selection = dcmi_g_selection,
  1079. .vidioc_s_selection = dcmi_s_selection,
  1080. .vidioc_enum_input = dcmi_enum_input,
  1081. .vidioc_g_input = dcmi_g_input,
  1082. .vidioc_s_input = dcmi_s_input,
  1083. .vidioc_g_parm = dcmi_g_parm,
  1084. .vidioc_s_parm = dcmi_s_parm,
  1085. .vidioc_enum_framesizes = dcmi_enum_framesizes,
  1086. .vidioc_enum_frameintervals = dcmi_enum_frameintervals,
  1087. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1088. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1089. .vidioc_querybuf = vb2_ioctl_querybuf,
  1090. .vidioc_qbuf = vb2_ioctl_qbuf,
  1091. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1092. .vidioc_expbuf = vb2_ioctl_expbuf,
  1093. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1094. .vidioc_streamon = vb2_ioctl_streamon,
  1095. .vidioc_streamoff = vb2_ioctl_streamoff,
  1096. .vidioc_log_status = v4l2_ctrl_log_status,
  1097. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1098. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1099. };
  1100. static const struct v4l2_file_operations dcmi_fops = {
  1101. .owner = THIS_MODULE,
  1102. .unlocked_ioctl = video_ioctl2,
  1103. .open = dcmi_open,
  1104. .release = dcmi_release,
  1105. .poll = vb2_fop_poll,
  1106. .mmap = vb2_fop_mmap,
  1107. #ifndef CONFIG_MMU
  1108. .get_unmapped_area = vb2_fop_get_unmapped_area,
  1109. #endif
  1110. .read = vb2_fop_read,
  1111. };
  1112. static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi)
  1113. {
  1114. struct v4l2_format f = {
  1115. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1116. .fmt.pix = {
  1117. .width = CIF_WIDTH,
  1118. .height = CIF_HEIGHT,
  1119. .field = V4L2_FIELD_NONE,
  1120. .pixelformat = dcmi->sd_formats[0]->fourcc,
  1121. },
  1122. };
  1123. int ret;
  1124. ret = dcmi_try_fmt(dcmi, &f, NULL, NULL);
  1125. if (ret)
  1126. return ret;
  1127. dcmi->sd_format = dcmi->sd_formats[0];
  1128. dcmi->fmt = f;
  1129. return 0;
  1130. }
  1131. static const struct dcmi_format dcmi_formats[] = {
  1132. {
  1133. .fourcc = V4L2_PIX_FMT_RGB565,
  1134. .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  1135. .bpp = 2,
  1136. }, {
  1137. .fourcc = V4L2_PIX_FMT_YUYV,
  1138. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  1139. .bpp = 2,
  1140. }, {
  1141. .fourcc = V4L2_PIX_FMT_UYVY,
  1142. .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
  1143. .bpp = 2,
  1144. }, {
  1145. .fourcc = V4L2_PIX_FMT_JPEG,
  1146. .mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
  1147. .bpp = 1,
  1148. },
  1149. };
  1150. static int dcmi_formats_init(struct stm32_dcmi *dcmi)
  1151. {
  1152. const struct dcmi_format *sd_fmts[ARRAY_SIZE(dcmi_formats)];
  1153. unsigned int num_fmts = 0, i, j;
  1154. struct v4l2_subdev *subdev = dcmi->entity.subdev;
  1155. struct v4l2_subdev_mbus_code_enum mbus_code = {
  1156. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1157. };
  1158. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
  1159. NULL, &mbus_code)) {
  1160. for (i = 0; i < ARRAY_SIZE(dcmi_formats); i++) {
  1161. if (dcmi_formats[i].mbus_code != mbus_code.code)
  1162. continue;
  1163. /* Code supported, have we got this fourcc yet? */
  1164. for (j = 0; j < num_fmts; j++)
  1165. if (sd_fmts[j]->fourcc ==
  1166. dcmi_formats[i].fourcc)
  1167. /* Already available */
  1168. break;
  1169. if (j == num_fmts)
  1170. /* New */
  1171. sd_fmts[num_fmts++] = dcmi_formats + i;
  1172. }
  1173. mbus_code.index++;
  1174. }
  1175. if (!num_fmts)
  1176. return -ENXIO;
  1177. dcmi->num_of_sd_formats = num_fmts;
  1178. dcmi->sd_formats = devm_kcalloc(dcmi->dev,
  1179. num_fmts, sizeof(struct dcmi_format *),
  1180. GFP_KERNEL);
  1181. if (!dcmi->sd_formats) {
  1182. dev_err(dcmi->dev, "Could not allocate memory\n");
  1183. return -ENOMEM;
  1184. }
  1185. memcpy(dcmi->sd_formats, sd_fmts,
  1186. num_fmts * sizeof(struct dcmi_format *));
  1187. dcmi->sd_format = dcmi->sd_formats[0];
  1188. return 0;
  1189. }
  1190. static int dcmi_framesizes_init(struct stm32_dcmi *dcmi)
  1191. {
  1192. unsigned int num_fsize = 0;
  1193. struct v4l2_subdev *subdev = dcmi->entity.subdev;
  1194. struct v4l2_subdev_frame_size_enum fse = {
  1195. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1196. .code = dcmi->sd_format->mbus_code,
  1197. };
  1198. unsigned int ret;
  1199. unsigned int i;
  1200. /* Allocate discrete framesizes array */
  1201. while (!v4l2_subdev_call(subdev, pad, enum_frame_size,
  1202. NULL, &fse))
  1203. fse.index++;
  1204. num_fsize = fse.index;
  1205. if (!num_fsize)
  1206. return 0;
  1207. dcmi->num_of_sd_framesizes = num_fsize;
  1208. dcmi->sd_framesizes = devm_kcalloc(dcmi->dev, num_fsize,
  1209. sizeof(struct dcmi_framesize),
  1210. GFP_KERNEL);
  1211. if (!dcmi->sd_framesizes) {
  1212. dev_err(dcmi->dev, "Could not allocate memory\n");
  1213. return -ENOMEM;
  1214. }
  1215. /* Fill array with sensor supported framesizes */
  1216. dev_dbg(dcmi->dev, "Sensor supports %u frame sizes:\n", num_fsize);
  1217. for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
  1218. fse.index = i;
  1219. ret = v4l2_subdev_call(subdev, pad, enum_frame_size,
  1220. NULL, &fse);
  1221. if (ret)
  1222. return ret;
  1223. dcmi->sd_framesizes[fse.index].width = fse.max_width;
  1224. dcmi->sd_framesizes[fse.index].height = fse.max_height;
  1225. dev_dbg(dcmi->dev, "%ux%u\n", fse.max_width, fse.max_height);
  1226. }
  1227. return 0;
  1228. }
  1229. static int dcmi_graph_notify_complete(struct v4l2_async_notifier *notifier)
  1230. {
  1231. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  1232. int ret;
  1233. dcmi->vdev->ctrl_handler = dcmi->entity.subdev->ctrl_handler;
  1234. ret = dcmi_formats_init(dcmi);
  1235. if (ret) {
  1236. dev_err(dcmi->dev, "No supported mediabus format found\n");
  1237. return ret;
  1238. }
  1239. ret = dcmi_framesizes_init(dcmi);
  1240. if (ret) {
  1241. dev_err(dcmi->dev, "Could not initialize framesizes\n");
  1242. return ret;
  1243. }
  1244. ret = dcmi_get_sensor_bounds(dcmi, &dcmi->sd_bounds);
  1245. if (ret) {
  1246. dev_err(dcmi->dev, "Could not get sensor bounds\n");
  1247. return ret;
  1248. }
  1249. ret = dcmi_set_default_fmt(dcmi);
  1250. if (ret) {
  1251. dev_err(dcmi->dev, "Could not set default format\n");
  1252. return ret;
  1253. }
  1254. ret = video_register_device(dcmi->vdev, VFL_TYPE_GRABBER, -1);
  1255. if (ret) {
  1256. dev_err(dcmi->dev, "Failed to register video device\n");
  1257. return ret;
  1258. }
  1259. dev_dbg(dcmi->dev, "Device registered as %s\n",
  1260. video_device_node_name(dcmi->vdev));
  1261. return 0;
  1262. }
  1263. static void dcmi_graph_notify_unbind(struct v4l2_async_notifier *notifier,
  1264. struct v4l2_subdev *sd,
  1265. struct v4l2_async_subdev *asd)
  1266. {
  1267. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  1268. dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev));
  1269. /* Checks internaly if vdev has been init or not */
  1270. video_unregister_device(dcmi->vdev);
  1271. }
  1272. static int dcmi_graph_notify_bound(struct v4l2_async_notifier *notifier,
  1273. struct v4l2_subdev *subdev,
  1274. struct v4l2_async_subdev *asd)
  1275. {
  1276. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  1277. dev_dbg(dcmi->dev, "Subdev %s bound\n", subdev->name);
  1278. dcmi->entity.subdev = subdev;
  1279. return 0;
  1280. }
  1281. static const struct v4l2_async_notifier_operations dcmi_graph_notify_ops = {
  1282. .bound = dcmi_graph_notify_bound,
  1283. .unbind = dcmi_graph_notify_unbind,
  1284. .complete = dcmi_graph_notify_complete,
  1285. };
  1286. static int dcmi_graph_parse(struct stm32_dcmi *dcmi, struct device_node *node)
  1287. {
  1288. struct device_node *ep = NULL;
  1289. struct device_node *remote;
  1290. ep = of_graph_get_next_endpoint(node, ep);
  1291. if (!ep)
  1292. return -EINVAL;
  1293. remote = of_graph_get_remote_port_parent(ep);
  1294. of_node_put(ep);
  1295. if (!remote)
  1296. return -EINVAL;
  1297. /* Remote node to connect */
  1298. dcmi->entity.node = remote;
  1299. dcmi->entity.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
  1300. dcmi->entity.asd.match.fwnode = of_fwnode_handle(remote);
  1301. return 0;
  1302. }
  1303. static int dcmi_graph_init(struct stm32_dcmi *dcmi)
  1304. {
  1305. int ret;
  1306. /* Parse the graph to extract a list of subdevice DT nodes. */
  1307. ret = dcmi_graph_parse(dcmi, dcmi->dev->of_node);
  1308. if (ret < 0) {
  1309. dev_err(dcmi->dev, "Graph parsing failed\n");
  1310. return ret;
  1311. }
  1312. v4l2_async_notifier_init(&dcmi->notifier);
  1313. ret = v4l2_async_notifier_add_subdev(&dcmi->notifier,
  1314. &dcmi->entity.asd);
  1315. if (ret) {
  1316. of_node_put(dcmi->entity.node);
  1317. return ret;
  1318. }
  1319. dcmi->notifier.ops = &dcmi_graph_notify_ops;
  1320. ret = v4l2_async_notifier_register(&dcmi->v4l2_dev, &dcmi->notifier);
  1321. if (ret < 0) {
  1322. dev_err(dcmi->dev, "Notifier registration failed\n");
  1323. v4l2_async_notifier_cleanup(&dcmi->notifier);
  1324. return ret;
  1325. }
  1326. return 0;
  1327. }
  1328. static int dcmi_probe(struct platform_device *pdev)
  1329. {
  1330. struct device_node *np = pdev->dev.of_node;
  1331. const struct of_device_id *match = NULL;
  1332. struct v4l2_fwnode_endpoint ep;
  1333. struct stm32_dcmi *dcmi;
  1334. struct vb2_queue *q;
  1335. struct dma_chan *chan;
  1336. struct clk *mclk;
  1337. int irq;
  1338. int ret = 0;
  1339. match = of_match_device(of_match_ptr(stm32_dcmi_of_match), &pdev->dev);
  1340. if (!match) {
  1341. dev_err(&pdev->dev, "Could not find a match in devicetree\n");
  1342. return -ENODEV;
  1343. }
  1344. dcmi = devm_kzalloc(&pdev->dev, sizeof(struct stm32_dcmi), GFP_KERNEL);
  1345. if (!dcmi)
  1346. return -ENOMEM;
  1347. dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  1348. if (IS_ERR(dcmi->rstc)) {
  1349. dev_err(&pdev->dev, "Could not get reset control\n");
  1350. return -ENODEV;
  1351. }
  1352. /* Get bus characteristics from devicetree */
  1353. np = of_graph_get_next_endpoint(np, NULL);
  1354. if (!np) {
  1355. dev_err(&pdev->dev, "Could not find the endpoint\n");
  1356. of_node_put(np);
  1357. return -ENODEV;
  1358. }
  1359. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
  1360. of_node_put(np);
  1361. if (ret) {
  1362. dev_err(&pdev->dev, "Could not parse the endpoint\n");
  1363. return -ENODEV;
  1364. }
  1365. if (ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
  1366. dev_err(&pdev->dev, "CSI bus not supported\n");
  1367. return -ENODEV;
  1368. }
  1369. dcmi->bus.flags = ep.bus.parallel.flags;
  1370. dcmi->bus.bus_width = ep.bus.parallel.bus_width;
  1371. dcmi->bus.data_shift = ep.bus.parallel.data_shift;
  1372. irq = platform_get_irq(pdev, 0);
  1373. if (irq <= 0) {
  1374. dev_err(&pdev->dev, "Could not get irq\n");
  1375. return -ENODEV;
  1376. }
  1377. dcmi->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1378. if (!dcmi->res) {
  1379. dev_err(&pdev->dev, "Could not get resource\n");
  1380. return -ENODEV;
  1381. }
  1382. dcmi->regs = devm_ioremap_resource(&pdev->dev, dcmi->res);
  1383. if (IS_ERR(dcmi->regs)) {
  1384. dev_err(&pdev->dev, "Could not map registers\n");
  1385. return PTR_ERR(dcmi->regs);
  1386. }
  1387. ret = devm_request_threaded_irq(&pdev->dev, irq, dcmi_irq_callback,
  1388. dcmi_irq_thread, IRQF_ONESHOT,
  1389. dev_name(&pdev->dev), dcmi);
  1390. if (ret) {
  1391. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  1392. return -ENODEV;
  1393. }
  1394. mclk = devm_clk_get(&pdev->dev, "mclk");
  1395. if (IS_ERR(mclk)) {
  1396. dev_err(&pdev->dev, "Unable to get mclk\n");
  1397. return PTR_ERR(mclk);
  1398. }
  1399. chan = dma_request_slave_channel(&pdev->dev, "tx");
  1400. if (!chan) {
  1401. dev_info(&pdev->dev, "Unable to request DMA channel, defer probing\n");
  1402. return -EPROBE_DEFER;
  1403. }
  1404. spin_lock_init(&dcmi->irqlock);
  1405. mutex_init(&dcmi->lock);
  1406. init_completion(&dcmi->complete);
  1407. INIT_LIST_HEAD(&dcmi->buffers);
  1408. dcmi->dev = &pdev->dev;
  1409. dcmi->mclk = mclk;
  1410. dcmi->state = STOPPED;
  1411. dcmi->dma_chan = chan;
  1412. q = &dcmi->queue;
  1413. /* Initialize the top-level structure */
  1414. ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev);
  1415. if (ret)
  1416. goto err_dma_release;
  1417. dcmi->vdev = video_device_alloc();
  1418. if (!dcmi->vdev) {
  1419. ret = -ENOMEM;
  1420. goto err_device_unregister;
  1421. }
  1422. /* Video node */
  1423. dcmi->vdev->fops = &dcmi_fops;
  1424. dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev;
  1425. dcmi->vdev->queue = &dcmi->queue;
  1426. strscpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name));
  1427. dcmi->vdev->release = video_device_release;
  1428. dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops;
  1429. dcmi->vdev->lock = &dcmi->lock;
  1430. dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  1431. V4L2_CAP_READWRITE;
  1432. video_set_drvdata(dcmi->vdev, dcmi);
  1433. /* Buffer queue */
  1434. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1435. q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
  1436. q->lock = &dcmi->lock;
  1437. q->drv_priv = dcmi;
  1438. q->buf_struct_size = sizeof(struct dcmi_buf);
  1439. q->ops = &dcmi_video_qops;
  1440. q->mem_ops = &vb2_dma_contig_memops;
  1441. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1442. q->min_buffers_needed = 2;
  1443. q->dev = &pdev->dev;
  1444. ret = vb2_queue_init(q);
  1445. if (ret < 0) {
  1446. dev_err(&pdev->dev, "Failed to initialize vb2 queue\n");
  1447. goto err_device_release;
  1448. }
  1449. ret = dcmi_graph_init(dcmi);
  1450. if (ret < 0)
  1451. goto err_device_release;
  1452. /* Reset device */
  1453. ret = reset_control_assert(dcmi->rstc);
  1454. if (ret) {
  1455. dev_err(&pdev->dev, "Failed to assert the reset line\n");
  1456. goto err_cleanup;
  1457. }
  1458. usleep_range(3000, 5000);
  1459. ret = reset_control_deassert(dcmi->rstc);
  1460. if (ret) {
  1461. dev_err(&pdev->dev, "Failed to deassert the reset line\n");
  1462. goto err_cleanup;
  1463. }
  1464. dev_info(&pdev->dev, "Probe done\n");
  1465. platform_set_drvdata(pdev, dcmi);
  1466. pm_runtime_enable(&pdev->dev);
  1467. return 0;
  1468. err_cleanup:
  1469. v4l2_async_notifier_cleanup(&dcmi->notifier);
  1470. err_device_release:
  1471. video_device_release(dcmi->vdev);
  1472. err_device_unregister:
  1473. v4l2_device_unregister(&dcmi->v4l2_dev);
  1474. err_dma_release:
  1475. dma_release_channel(dcmi->dma_chan);
  1476. return ret;
  1477. }
  1478. static int dcmi_remove(struct platform_device *pdev)
  1479. {
  1480. struct stm32_dcmi *dcmi = platform_get_drvdata(pdev);
  1481. pm_runtime_disable(&pdev->dev);
  1482. v4l2_async_notifier_unregister(&dcmi->notifier);
  1483. v4l2_async_notifier_cleanup(&dcmi->notifier);
  1484. v4l2_device_unregister(&dcmi->v4l2_dev);
  1485. dma_release_channel(dcmi->dma_chan);
  1486. return 0;
  1487. }
  1488. static __maybe_unused int dcmi_runtime_suspend(struct device *dev)
  1489. {
  1490. struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
  1491. clk_disable_unprepare(dcmi->mclk);
  1492. return 0;
  1493. }
  1494. static __maybe_unused int dcmi_runtime_resume(struct device *dev)
  1495. {
  1496. struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
  1497. int ret;
  1498. ret = clk_prepare_enable(dcmi->mclk);
  1499. if (ret)
  1500. dev_err(dev, "%s: Failed to prepare_enable clock\n", __func__);
  1501. return ret;
  1502. }
  1503. static __maybe_unused int dcmi_suspend(struct device *dev)
  1504. {
  1505. /* disable clock */
  1506. pm_runtime_force_suspend(dev);
  1507. /* change pinctrl state */
  1508. pinctrl_pm_select_sleep_state(dev);
  1509. return 0;
  1510. }
  1511. static __maybe_unused int dcmi_resume(struct device *dev)
  1512. {
  1513. /* restore pinctl default state */
  1514. pinctrl_pm_select_default_state(dev);
  1515. /* clock enable */
  1516. pm_runtime_force_resume(dev);
  1517. return 0;
  1518. }
  1519. static const struct dev_pm_ops dcmi_pm_ops = {
  1520. SET_SYSTEM_SLEEP_PM_OPS(dcmi_suspend, dcmi_resume)
  1521. SET_RUNTIME_PM_OPS(dcmi_runtime_suspend,
  1522. dcmi_runtime_resume, NULL)
  1523. };
  1524. static struct platform_driver stm32_dcmi_driver = {
  1525. .probe = dcmi_probe,
  1526. .remove = dcmi_remove,
  1527. .driver = {
  1528. .name = DRV_NAME,
  1529. .of_match_table = of_match_ptr(stm32_dcmi_of_match),
  1530. .pm = &dcmi_pm_ops,
  1531. },
  1532. };
  1533. module_platform_driver(stm32_dcmi_driver);
  1534. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  1535. MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
  1536. MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");
  1537. MODULE_LICENSE("GPL");
  1538. MODULE_SUPPORTED_DEVICE("video");