stm32-dcmi.c 46 KB

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