stm32-dcmi.c 45 KB

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