stm32-dcmi.c 44 KB

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