stm32-dcmi.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  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_pm_put;
  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. spin_lock_irq(&dcmi->irqlock);
  553. /*
  554. * Return all buffers to vb2 in QUEUED state.
  555. * This will give ownership back to userspace
  556. */
  557. list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
  558. list_del_init(&buf->list);
  559. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  560. }
  561. dcmi->active = NULL;
  562. spin_unlock_irq(&dcmi->irqlock);
  563. return ret;
  564. }
  565. static void dcmi_stop_streaming(struct vb2_queue *vq)
  566. {
  567. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  568. struct dcmi_buf *buf, *node;
  569. int ret;
  570. /* Disable stream on the sub device */
  571. ret = v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 0);
  572. if (ret && ret != -ENOIOCTLCMD)
  573. dev_err(dcmi->dev, "%s: Failed to stop streaming, subdev streamoff error (%d)\n",
  574. __func__, ret);
  575. spin_lock_irq(&dcmi->irqlock);
  576. /* Disable interruptions */
  577. reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
  578. /* Disable DCMI */
  579. reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE);
  580. /* Return all queued buffers to vb2 in ERROR state */
  581. list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
  582. list_del_init(&buf->list);
  583. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  584. }
  585. dcmi->active = NULL;
  586. dcmi->state = STOPPED;
  587. spin_unlock_irq(&dcmi->irqlock);
  588. /* Stop all pending DMA operations */
  589. mutex_lock(&dcmi->dma_lock);
  590. dmaengine_terminate_all(dcmi->dma_chan);
  591. mutex_unlock(&dcmi->dma_lock);
  592. pm_runtime_put(dcmi->dev);
  593. if (dcmi->errors_count)
  594. dev_warn(dcmi->dev, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n",
  595. dcmi->errors_count, dcmi->overrun_count,
  596. dcmi->buffers_count);
  597. dev_dbg(dcmi->dev, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n",
  598. dcmi->errors_count, dcmi->overrun_count,
  599. dcmi->buffers_count);
  600. }
  601. static const struct vb2_ops dcmi_video_qops = {
  602. .queue_setup = dcmi_queue_setup,
  603. .buf_init = dcmi_buf_init,
  604. .buf_prepare = dcmi_buf_prepare,
  605. .buf_queue = dcmi_buf_queue,
  606. .start_streaming = dcmi_start_streaming,
  607. .stop_streaming = dcmi_stop_streaming,
  608. .wait_prepare = vb2_ops_wait_prepare,
  609. .wait_finish = vb2_ops_wait_finish,
  610. };
  611. static int dcmi_g_fmt_vid_cap(struct file *file, void *priv,
  612. struct v4l2_format *fmt)
  613. {
  614. struct stm32_dcmi *dcmi = video_drvdata(file);
  615. *fmt = dcmi->fmt;
  616. return 0;
  617. }
  618. static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi,
  619. unsigned int fourcc)
  620. {
  621. unsigned int num_formats = dcmi->num_of_sd_formats;
  622. const struct dcmi_format *fmt;
  623. unsigned int i;
  624. for (i = 0; i < num_formats; i++) {
  625. fmt = dcmi->sd_formats[i];
  626. if (fmt->fourcc == fourcc)
  627. return fmt;
  628. }
  629. return NULL;
  630. }
  631. static void __find_outer_frame_size(struct stm32_dcmi *dcmi,
  632. struct v4l2_pix_format *pix,
  633. struct dcmi_framesize *framesize)
  634. {
  635. struct dcmi_framesize *match = NULL;
  636. unsigned int i;
  637. unsigned int min_err = UINT_MAX;
  638. for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
  639. struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
  640. int w_err = (fsize->width - pix->width);
  641. int h_err = (fsize->height - pix->height);
  642. int err = w_err + h_err;
  643. if (w_err >= 0 && h_err >= 0 && err < min_err) {
  644. min_err = err;
  645. match = fsize;
  646. }
  647. }
  648. if (!match)
  649. match = &dcmi->sd_framesizes[0];
  650. *framesize = *match;
  651. }
  652. static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f,
  653. const struct dcmi_format **sd_format,
  654. struct dcmi_framesize *sd_framesize)
  655. {
  656. const struct dcmi_format *sd_fmt;
  657. struct dcmi_framesize sd_fsize;
  658. struct v4l2_pix_format *pix = &f->fmt.pix;
  659. struct v4l2_subdev_pad_config pad_cfg;
  660. struct v4l2_subdev_format format = {
  661. .which = V4L2_SUBDEV_FORMAT_TRY,
  662. };
  663. bool do_crop;
  664. int ret;
  665. sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
  666. if (!sd_fmt) {
  667. if (!dcmi->num_of_sd_formats)
  668. return -ENODATA;
  669. sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
  670. pix->pixelformat = sd_fmt->fourcc;
  671. }
  672. /* Limit to hardware capabilities */
  673. pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH);
  674. pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT);
  675. /* No crop if JPEG is requested */
  676. do_crop = dcmi->do_crop && (pix->pixelformat != V4L2_PIX_FMT_JPEG);
  677. if (do_crop && dcmi->num_of_sd_framesizes) {
  678. struct dcmi_framesize outer_sd_fsize;
  679. /*
  680. * If crop is requested and sensor have discrete frame sizes,
  681. * select the frame size that is just larger than request
  682. */
  683. __find_outer_frame_size(dcmi, pix, &outer_sd_fsize);
  684. pix->width = outer_sd_fsize.width;
  685. pix->height = outer_sd_fsize.height;
  686. }
  687. v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
  688. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, set_fmt,
  689. &pad_cfg, &format);
  690. if (ret < 0)
  691. return ret;
  692. /* Update pix regarding to what sensor can do */
  693. v4l2_fill_pix_format(pix, &format.format);
  694. /* Save resolution that sensor can actually do */
  695. sd_fsize.width = pix->width;
  696. sd_fsize.height = pix->height;
  697. if (do_crop) {
  698. struct v4l2_rect c = dcmi->crop;
  699. struct v4l2_rect max_rect;
  700. /*
  701. * Adjust crop by making the intersection between
  702. * format resolution request and crop request
  703. */
  704. max_rect.top = 0;
  705. max_rect.left = 0;
  706. max_rect.width = pix->width;
  707. max_rect.height = pix->height;
  708. v4l2_rect_map_inside(&c, &max_rect);
  709. c.top = clamp_t(s32, c.top, 0, pix->height - c.height);
  710. c.left = clamp_t(s32, c.left, 0, pix->width - c.width);
  711. dcmi->crop = c;
  712. /* Adjust format resolution request to crop */
  713. pix->width = dcmi->crop.width;
  714. pix->height = dcmi->crop.height;
  715. }
  716. pix->field = V4L2_FIELD_NONE;
  717. pix->bytesperline = pix->width * sd_fmt->bpp;
  718. pix->sizeimage = pix->bytesperline * pix->height;
  719. if (sd_format)
  720. *sd_format = sd_fmt;
  721. if (sd_framesize)
  722. *sd_framesize = sd_fsize;
  723. return 0;
  724. }
  725. static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f)
  726. {
  727. struct v4l2_subdev_format format = {
  728. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  729. };
  730. const struct dcmi_format *sd_format;
  731. struct dcmi_framesize sd_framesize;
  732. struct v4l2_mbus_framefmt *mf = &format.format;
  733. struct v4l2_pix_format *pix = &f->fmt.pix;
  734. int ret;
  735. /*
  736. * Try format, fmt.width/height could have been changed
  737. * to match sensor capability or crop request
  738. * sd_format & sd_framesize will contain what subdev
  739. * can do for this request.
  740. */
  741. ret = dcmi_try_fmt(dcmi, f, &sd_format, &sd_framesize);
  742. if (ret)
  743. return ret;
  744. /* Disable crop if JPEG is requested */
  745. if (pix->pixelformat == V4L2_PIX_FMT_JPEG)
  746. dcmi->do_crop = false;
  747. /* pix to mbus format */
  748. v4l2_fill_mbus_format(mf, pix,
  749. sd_format->mbus_code);
  750. mf->width = sd_framesize.width;
  751. mf->height = sd_framesize.height;
  752. ret = v4l2_subdev_call(dcmi->entity.subdev, pad,
  753. set_fmt, NULL, &format);
  754. if (ret < 0)
  755. return ret;
  756. dev_dbg(dcmi->dev, "Sensor format set to 0x%x %ux%u\n",
  757. mf->code, mf->width, mf->height);
  758. dev_dbg(dcmi->dev, "Buffer format set to %4.4s %ux%u\n",
  759. (char *)&pix->pixelformat,
  760. pix->width, pix->height);
  761. dcmi->fmt = *f;
  762. dcmi->sd_format = sd_format;
  763. dcmi->sd_framesize = sd_framesize;
  764. return 0;
  765. }
  766. static int dcmi_s_fmt_vid_cap(struct file *file, void *priv,
  767. struct v4l2_format *f)
  768. {
  769. struct stm32_dcmi *dcmi = video_drvdata(file);
  770. if (vb2_is_streaming(&dcmi->queue))
  771. return -EBUSY;
  772. return dcmi_set_fmt(dcmi, f);
  773. }
  774. static int dcmi_try_fmt_vid_cap(struct file *file, void *priv,
  775. struct v4l2_format *f)
  776. {
  777. struct stm32_dcmi *dcmi = video_drvdata(file);
  778. return dcmi_try_fmt(dcmi, f, NULL, NULL);
  779. }
  780. static int dcmi_enum_fmt_vid_cap(struct file *file, void *priv,
  781. struct v4l2_fmtdesc *f)
  782. {
  783. struct stm32_dcmi *dcmi = video_drvdata(file);
  784. if (f->index >= dcmi->num_of_sd_formats)
  785. return -EINVAL;
  786. f->pixelformat = dcmi->sd_formats[f->index]->fourcc;
  787. return 0;
  788. }
  789. static int dcmi_get_sensor_format(struct stm32_dcmi *dcmi,
  790. struct v4l2_pix_format *pix)
  791. {
  792. struct v4l2_subdev_format fmt = {
  793. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  794. };
  795. int ret;
  796. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, get_fmt, NULL, &fmt);
  797. if (ret)
  798. return ret;
  799. v4l2_fill_pix_format(pix, &fmt.format);
  800. return 0;
  801. }
  802. static int dcmi_set_sensor_format(struct stm32_dcmi *dcmi,
  803. struct v4l2_pix_format *pix)
  804. {
  805. const struct dcmi_format *sd_fmt;
  806. struct v4l2_subdev_format format = {
  807. .which = V4L2_SUBDEV_FORMAT_TRY,
  808. };
  809. struct v4l2_subdev_pad_config pad_cfg;
  810. int ret;
  811. sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
  812. if (!sd_fmt) {
  813. if (!dcmi->num_of_sd_formats)
  814. return -ENODATA;
  815. sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
  816. pix->pixelformat = sd_fmt->fourcc;
  817. }
  818. v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
  819. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, set_fmt,
  820. &pad_cfg, &format);
  821. if (ret < 0)
  822. return ret;
  823. return 0;
  824. }
  825. static int dcmi_get_sensor_bounds(struct stm32_dcmi *dcmi,
  826. struct v4l2_rect *r)
  827. {
  828. struct v4l2_subdev_selection bounds = {
  829. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  830. .target = V4L2_SEL_TGT_CROP_BOUNDS,
  831. };
  832. unsigned int max_width, max_height, max_pixsize;
  833. struct v4l2_pix_format pix;
  834. unsigned int i;
  835. int ret;
  836. /*
  837. * Get sensor bounds first
  838. */
  839. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, get_selection,
  840. NULL, &bounds);
  841. if (!ret)
  842. *r = bounds.r;
  843. if (ret != -ENOIOCTLCMD)
  844. return ret;
  845. /*
  846. * If selection is not implemented,
  847. * fallback by enumerating sensor frame sizes
  848. * and take the largest one
  849. */
  850. max_width = 0;
  851. max_height = 0;
  852. max_pixsize = 0;
  853. for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
  854. struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
  855. unsigned int pixsize = fsize->width * fsize->height;
  856. if (pixsize > max_pixsize) {
  857. max_pixsize = pixsize;
  858. max_width = fsize->width;
  859. max_height = fsize->height;
  860. }
  861. }
  862. if (max_pixsize > 0) {
  863. r->top = 0;
  864. r->left = 0;
  865. r->width = max_width;
  866. r->height = max_height;
  867. return 0;
  868. }
  869. /*
  870. * If frame sizes enumeration is not implemented,
  871. * fallback by getting current sensor frame size
  872. */
  873. ret = dcmi_get_sensor_format(dcmi, &pix);
  874. if (ret)
  875. return ret;
  876. r->top = 0;
  877. r->left = 0;
  878. r->width = pix.width;
  879. r->height = pix.height;
  880. return 0;
  881. }
  882. static int dcmi_g_selection(struct file *file, void *fh,
  883. struct v4l2_selection *s)
  884. {
  885. struct stm32_dcmi *dcmi = video_drvdata(file);
  886. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  887. return -EINVAL;
  888. switch (s->target) {
  889. case V4L2_SEL_TGT_CROP_DEFAULT:
  890. case V4L2_SEL_TGT_CROP_BOUNDS:
  891. s->r = dcmi->sd_bounds;
  892. return 0;
  893. case V4L2_SEL_TGT_CROP:
  894. if (dcmi->do_crop) {
  895. s->r = dcmi->crop;
  896. } else {
  897. s->r.top = 0;
  898. s->r.left = 0;
  899. s->r.width = dcmi->fmt.fmt.pix.width;
  900. s->r.height = dcmi->fmt.fmt.pix.height;
  901. }
  902. break;
  903. default:
  904. return -EINVAL;
  905. }
  906. return 0;
  907. }
  908. static int dcmi_s_selection(struct file *file, void *priv,
  909. struct v4l2_selection *s)
  910. {
  911. struct stm32_dcmi *dcmi = video_drvdata(file);
  912. struct v4l2_rect r = s->r;
  913. struct v4l2_rect max_rect;
  914. struct v4l2_pix_format pix;
  915. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  916. s->target != V4L2_SEL_TGT_CROP)
  917. return -EINVAL;
  918. /* Reset sensor resolution to max resolution */
  919. pix.pixelformat = dcmi->fmt.fmt.pix.pixelformat;
  920. pix.width = dcmi->sd_bounds.width;
  921. pix.height = dcmi->sd_bounds.height;
  922. dcmi_set_sensor_format(dcmi, &pix);
  923. /*
  924. * Make the intersection between
  925. * sensor resolution
  926. * and crop request
  927. */
  928. max_rect.top = 0;
  929. max_rect.left = 0;
  930. max_rect.width = pix.width;
  931. max_rect.height = pix.height;
  932. v4l2_rect_map_inside(&r, &max_rect);
  933. r.top = clamp_t(s32, r.top, 0, pix.height - r.height);
  934. r.left = clamp_t(s32, r.left, 0, pix.width - r.width);
  935. if (!(r.top == dcmi->sd_bounds.top &&
  936. r.left == dcmi->sd_bounds.left &&
  937. r.width == dcmi->sd_bounds.width &&
  938. r.height == dcmi->sd_bounds.height)) {
  939. /* Crop if request is different than sensor resolution */
  940. dcmi->do_crop = true;
  941. dcmi->crop = r;
  942. dev_dbg(dcmi->dev, "s_selection: crop %ux%u@(%u,%u) from %ux%u\n",
  943. r.width, r.height, r.left, r.top,
  944. pix.width, pix.height);
  945. } else {
  946. /* Disable crop */
  947. dcmi->do_crop = false;
  948. dev_dbg(dcmi->dev, "s_selection: crop is disabled\n");
  949. }
  950. s->r = r;
  951. return 0;
  952. }
  953. static int dcmi_querycap(struct file *file, void *priv,
  954. struct v4l2_capability *cap)
  955. {
  956. strlcpy(cap->driver, DRV_NAME, sizeof(cap->driver));
  957. strlcpy(cap->card, "STM32 Camera Memory Interface",
  958. sizeof(cap->card));
  959. strlcpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info));
  960. return 0;
  961. }
  962. static int dcmi_enum_input(struct file *file, void *priv,
  963. struct v4l2_input *i)
  964. {
  965. if (i->index != 0)
  966. return -EINVAL;
  967. i->type = V4L2_INPUT_TYPE_CAMERA;
  968. strlcpy(i->name, "Camera", sizeof(i->name));
  969. return 0;
  970. }
  971. static int dcmi_g_input(struct file *file, void *priv, unsigned int *i)
  972. {
  973. *i = 0;
  974. return 0;
  975. }
  976. static int dcmi_s_input(struct file *file, void *priv, unsigned int i)
  977. {
  978. if (i > 0)
  979. return -EINVAL;
  980. return 0;
  981. }
  982. static int dcmi_enum_framesizes(struct file *file, void *fh,
  983. struct v4l2_frmsizeenum *fsize)
  984. {
  985. struct stm32_dcmi *dcmi = video_drvdata(file);
  986. const struct dcmi_format *sd_fmt;
  987. struct v4l2_subdev_frame_size_enum fse = {
  988. .index = fsize->index,
  989. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  990. };
  991. int ret;
  992. sd_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format);
  993. if (!sd_fmt)
  994. return -EINVAL;
  995. fse.code = sd_fmt->mbus_code;
  996. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, enum_frame_size,
  997. NULL, &fse);
  998. if (ret)
  999. return ret;
  1000. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1001. fsize->discrete.width = fse.max_width;
  1002. fsize->discrete.height = fse.max_height;
  1003. return 0;
  1004. }
  1005. static int dcmi_g_parm(struct file *file, void *priv,
  1006. struct v4l2_streamparm *p)
  1007. {
  1008. struct stm32_dcmi *dcmi = video_drvdata(file);
  1009. return v4l2_g_parm_cap(video_devdata(file), dcmi->entity.subdev, p);
  1010. }
  1011. static int dcmi_s_parm(struct file *file, void *priv,
  1012. struct v4l2_streamparm *p)
  1013. {
  1014. struct stm32_dcmi *dcmi = video_drvdata(file);
  1015. return v4l2_s_parm_cap(video_devdata(file), dcmi->entity.subdev, p);
  1016. }
  1017. static int dcmi_enum_frameintervals(struct file *file, void *fh,
  1018. struct v4l2_frmivalenum *fival)
  1019. {
  1020. struct stm32_dcmi *dcmi = video_drvdata(file);
  1021. const struct dcmi_format *sd_fmt;
  1022. struct v4l2_subdev_frame_interval_enum fie = {
  1023. .index = fival->index,
  1024. .width = fival->width,
  1025. .height = fival->height,
  1026. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1027. };
  1028. int ret;
  1029. sd_fmt = find_format_by_fourcc(dcmi, fival->pixel_format);
  1030. if (!sd_fmt)
  1031. return -EINVAL;
  1032. fie.code = sd_fmt->mbus_code;
  1033. ret = v4l2_subdev_call(dcmi->entity.subdev, pad,
  1034. enum_frame_interval, NULL, &fie);
  1035. if (ret)
  1036. return ret;
  1037. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  1038. fival->discrete = fie.interval;
  1039. return 0;
  1040. }
  1041. static const struct of_device_id stm32_dcmi_of_match[] = {
  1042. { .compatible = "st,stm32-dcmi"},
  1043. { /* end node */ },
  1044. };
  1045. MODULE_DEVICE_TABLE(of, stm32_dcmi_of_match);
  1046. static int dcmi_open(struct file *file)
  1047. {
  1048. struct stm32_dcmi *dcmi = video_drvdata(file);
  1049. struct v4l2_subdev *sd = dcmi->entity.subdev;
  1050. int ret;
  1051. if (mutex_lock_interruptible(&dcmi->lock))
  1052. return -ERESTARTSYS;
  1053. ret = v4l2_fh_open(file);
  1054. if (ret < 0)
  1055. goto unlock;
  1056. if (!v4l2_fh_is_singular_file(file))
  1057. goto fh_rel;
  1058. ret = v4l2_subdev_call(sd, core, s_power, 1);
  1059. if (ret < 0 && ret != -ENOIOCTLCMD)
  1060. goto fh_rel;
  1061. ret = dcmi_set_fmt(dcmi, &dcmi->fmt);
  1062. if (ret)
  1063. v4l2_subdev_call(sd, core, s_power, 0);
  1064. fh_rel:
  1065. if (ret)
  1066. v4l2_fh_release(file);
  1067. unlock:
  1068. mutex_unlock(&dcmi->lock);
  1069. return ret;
  1070. }
  1071. static int dcmi_release(struct file *file)
  1072. {
  1073. struct stm32_dcmi *dcmi = video_drvdata(file);
  1074. struct v4l2_subdev *sd = dcmi->entity.subdev;
  1075. bool fh_singular;
  1076. int ret;
  1077. mutex_lock(&dcmi->lock);
  1078. fh_singular = v4l2_fh_is_singular_file(file);
  1079. ret = _vb2_fop_release(file, NULL);
  1080. if (fh_singular)
  1081. v4l2_subdev_call(sd, core, s_power, 0);
  1082. mutex_unlock(&dcmi->lock);
  1083. return ret;
  1084. }
  1085. static const struct v4l2_ioctl_ops dcmi_ioctl_ops = {
  1086. .vidioc_querycap = dcmi_querycap,
  1087. .vidioc_try_fmt_vid_cap = dcmi_try_fmt_vid_cap,
  1088. .vidioc_g_fmt_vid_cap = dcmi_g_fmt_vid_cap,
  1089. .vidioc_s_fmt_vid_cap = dcmi_s_fmt_vid_cap,
  1090. .vidioc_enum_fmt_vid_cap = dcmi_enum_fmt_vid_cap,
  1091. .vidioc_g_selection = dcmi_g_selection,
  1092. .vidioc_s_selection = dcmi_s_selection,
  1093. .vidioc_enum_input = dcmi_enum_input,
  1094. .vidioc_g_input = dcmi_g_input,
  1095. .vidioc_s_input = dcmi_s_input,
  1096. .vidioc_g_parm = dcmi_g_parm,
  1097. .vidioc_s_parm = dcmi_s_parm,
  1098. .vidioc_enum_framesizes = dcmi_enum_framesizes,
  1099. .vidioc_enum_frameintervals = dcmi_enum_frameintervals,
  1100. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1101. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1102. .vidioc_querybuf = vb2_ioctl_querybuf,
  1103. .vidioc_qbuf = vb2_ioctl_qbuf,
  1104. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1105. .vidioc_expbuf = vb2_ioctl_expbuf,
  1106. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1107. .vidioc_streamon = vb2_ioctl_streamon,
  1108. .vidioc_streamoff = vb2_ioctl_streamoff,
  1109. .vidioc_log_status = v4l2_ctrl_log_status,
  1110. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1111. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1112. };
  1113. static const struct v4l2_file_operations dcmi_fops = {
  1114. .owner = THIS_MODULE,
  1115. .unlocked_ioctl = video_ioctl2,
  1116. .open = dcmi_open,
  1117. .release = dcmi_release,
  1118. .poll = vb2_fop_poll,
  1119. .mmap = vb2_fop_mmap,
  1120. #ifndef CONFIG_MMU
  1121. .get_unmapped_area = vb2_fop_get_unmapped_area,
  1122. #endif
  1123. .read = vb2_fop_read,
  1124. };
  1125. static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi)
  1126. {
  1127. struct v4l2_format f = {
  1128. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1129. .fmt.pix = {
  1130. .width = CIF_WIDTH,
  1131. .height = CIF_HEIGHT,
  1132. .field = V4L2_FIELD_NONE,
  1133. .pixelformat = dcmi->sd_formats[0]->fourcc,
  1134. },
  1135. };
  1136. int ret;
  1137. ret = dcmi_try_fmt(dcmi, &f, NULL, NULL);
  1138. if (ret)
  1139. return ret;
  1140. dcmi->sd_format = dcmi->sd_formats[0];
  1141. dcmi->fmt = f;
  1142. return 0;
  1143. }
  1144. static const struct dcmi_format dcmi_formats[] = {
  1145. {
  1146. .fourcc = V4L2_PIX_FMT_RGB565,
  1147. .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  1148. .bpp = 2,
  1149. }, {
  1150. .fourcc = V4L2_PIX_FMT_YUYV,
  1151. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  1152. .bpp = 2,
  1153. }, {
  1154. .fourcc = V4L2_PIX_FMT_UYVY,
  1155. .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
  1156. .bpp = 2,
  1157. }, {
  1158. .fourcc = V4L2_PIX_FMT_JPEG,
  1159. .mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
  1160. .bpp = 1,
  1161. },
  1162. };
  1163. static int dcmi_formats_init(struct stm32_dcmi *dcmi)
  1164. {
  1165. const struct dcmi_format *sd_fmts[ARRAY_SIZE(dcmi_formats)];
  1166. unsigned int num_fmts = 0, i, j;
  1167. struct v4l2_subdev *subdev = dcmi->entity.subdev;
  1168. struct v4l2_subdev_mbus_code_enum mbus_code = {
  1169. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1170. };
  1171. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
  1172. NULL, &mbus_code)) {
  1173. for (i = 0; i < ARRAY_SIZE(dcmi_formats); i++) {
  1174. if (dcmi_formats[i].mbus_code != mbus_code.code)
  1175. continue;
  1176. /* Code supported, have we got this fourcc yet? */
  1177. for (j = 0; j < num_fmts; j++)
  1178. if (sd_fmts[j]->fourcc ==
  1179. dcmi_formats[i].fourcc)
  1180. /* Already available */
  1181. break;
  1182. if (j == num_fmts)
  1183. /* New */
  1184. sd_fmts[num_fmts++] = dcmi_formats + i;
  1185. }
  1186. mbus_code.index++;
  1187. }
  1188. if (!num_fmts)
  1189. return -ENXIO;
  1190. dcmi->num_of_sd_formats = num_fmts;
  1191. dcmi->sd_formats = devm_kcalloc(dcmi->dev,
  1192. num_fmts, sizeof(struct dcmi_format *),
  1193. GFP_KERNEL);
  1194. if (!dcmi->sd_formats) {
  1195. dev_err(dcmi->dev, "Could not allocate memory\n");
  1196. return -ENOMEM;
  1197. }
  1198. memcpy(dcmi->sd_formats, sd_fmts,
  1199. num_fmts * sizeof(struct dcmi_format *));
  1200. dcmi->sd_format = dcmi->sd_formats[0];
  1201. return 0;
  1202. }
  1203. static int dcmi_framesizes_init(struct stm32_dcmi *dcmi)
  1204. {
  1205. unsigned int num_fsize = 0;
  1206. struct v4l2_subdev *subdev = dcmi->entity.subdev;
  1207. struct v4l2_subdev_frame_size_enum fse = {
  1208. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1209. .code = dcmi->sd_format->mbus_code,
  1210. };
  1211. unsigned int ret;
  1212. unsigned int i;
  1213. /* Allocate discrete framesizes array */
  1214. while (!v4l2_subdev_call(subdev, pad, enum_frame_size,
  1215. NULL, &fse))
  1216. fse.index++;
  1217. num_fsize = fse.index;
  1218. if (!num_fsize)
  1219. return 0;
  1220. dcmi->num_of_sd_framesizes = num_fsize;
  1221. dcmi->sd_framesizes = devm_kcalloc(dcmi->dev, num_fsize,
  1222. sizeof(struct dcmi_framesize),
  1223. GFP_KERNEL);
  1224. if (!dcmi->sd_framesizes) {
  1225. dev_err(dcmi->dev, "Could not allocate memory\n");
  1226. return -ENOMEM;
  1227. }
  1228. /* Fill array with sensor supported framesizes */
  1229. dev_dbg(dcmi->dev, "Sensor supports %u frame sizes:\n", num_fsize);
  1230. for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
  1231. fse.index = i;
  1232. ret = v4l2_subdev_call(subdev, pad, enum_frame_size,
  1233. NULL, &fse);
  1234. if (ret)
  1235. return ret;
  1236. dcmi->sd_framesizes[fse.index].width = fse.max_width;
  1237. dcmi->sd_framesizes[fse.index].height = fse.max_height;
  1238. dev_dbg(dcmi->dev, "%ux%u\n", fse.max_width, fse.max_height);
  1239. }
  1240. return 0;
  1241. }
  1242. static int dcmi_graph_notify_complete(struct v4l2_async_notifier *notifier)
  1243. {
  1244. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  1245. int ret;
  1246. dcmi->vdev->ctrl_handler = dcmi->entity.subdev->ctrl_handler;
  1247. ret = dcmi_formats_init(dcmi);
  1248. if (ret) {
  1249. dev_err(dcmi->dev, "No supported mediabus format found\n");
  1250. return ret;
  1251. }
  1252. ret = dcmi_framesizes_init(dcmi);
  1253. if (ret) {
  1254. dev_err(dcmi->dev, "Could not initialize framesizes\n");
  1255. return ret;
  1256. }
  1257. ret = dcmi_get_sensor_bounds(dcmi, &dcmi->sd_bounds);
  1258. if (ret) {
  1259. dev_err(dcmi->dev, "Could not get sensor bounds\n");
  1260. return ret;
  1261. }
  1262. ret = dcmi_set_default_fmt(dcmi);
  1263. if (ret) {
  1264. dev_err(dcmi->dev, "Could not set default format\n");
  1265. return ret;
  1266. }
  1267. ret = video_register_device(dcmi->vdev, VFL_TYPE_GRABBER, -1);
  1268. if (ret) {
  1269. dev_err(dcmi->dev, "Failed to register video device\n");
  1270. return ret;
  1271. }
  1272. dev_dbg(dcmi->dev, "Device registered as %s\n",
  1273. video_device_node_name(dcmi->vdev));
  1274. return 0;
  1275. }
  1276. static void dcmi_graph_notify_unbind(struct v4l2_async_notifier *notifier,
  1277. struct v4l2_subdev *sd,
  1278. struct v4l2_async_subdev *asd)
  1279. {
  1280. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  1281. dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev));
  1282. /* Checks internaly if vdev has been init or not */
  1283. video_unregister_device(dcmi->vdev);
  1284. }
  1285. static int dcmi_graph_notify_bound(struct v4l2_async_notifier *notifier,
  1286. struct v4l2_subdev *subdev,
  1287. struct v4l2_async_subdev *asd)
  1288. {
  1289. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  1290. dev_dbg(dcmi->dev, "Subdev %s bound\n", subdev->name);
  1291. dcmi->entity.subdev = subdev;
  1292. return 0;
  1293. }
  1294. static const struct v4l2_async_notifier_operations dcmi_graph_notify_ops = {
  1295. .bound = dcmi_graph_notify_bound,
  1296. .unbind = dcmi_graph_notify_unbind,
  1297. .complete = dcmi_graph_notify_complete,
  1298. };
  1299. static int dcmi_graph_parse(struct stm32_dcmi *dcmi, struct device_node *node)
  1300. {
  1301. struct device_node *ep = NULL;
  1302. struct device_node *remote;
  1303. ep = of_graph_get_next_endpoint(node, ep);
  1304. if (!ep)
  1305. return -EINVAL;
  1306. remote = of_graph_get_remote_port_parent(ep);
  1307. of_node_put(ep);
  1308. if (!remote)
  1309. return -EINVAL;
  1310. /* Remote node to connect */
  1311. dcmi->entity.node = remote;
  1312. dcmi->entity.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
  1313. dcmi->entity.asd.match.fwnode = of_fwnode_handle(remote);
  1314. return 0;
  1315. }
  1316. static int dcmi_graph_init(struct stm32_dcmi *dcmi)
  1317. {
  1318. struct v4l2_async_subdev **subdevs = NULL;
  1319. int ret;
  1320. /* Parse the graph to extract a list of subdevice DT nodes. */
  1321. ret = dcmi_graph_parse(dcmi, dcmi->dev->of_node);
  1322. if (ret < 0) {
  1323. dev_err(dcmi->dev, "Graph parsing failed\n");
  1324. return ret;
  1325. }
  1326. /* Register the subdevices notifier. */
  1327. subdevs = devm_kzalloc(dcmi->dev, sizeof(*subdevs), GFP_KERNEL);
  1328. if (!subdevs) {
  1329. of_node_put(dcmi->entity.node);
  1330. return -ENOMEM;
  1331. }
  1332. subdevs[0] = &dcmi->entity.asd;
  1333. dcmi->notifier.subdevs = subdevs;
  1334. dcmi->notifier.num_subdevs = 1;
  1335. dcmi->notifier.ops = &dcmi_graph_notify_ops;
  1336. ret = v4l2_async_notifier_register(&dcmi->v4l2_dev, &dcmi->notifier);
  1337. if (ret < 0) {
  1338. dev_err(dcmi->dev, "Notifier registration failed\n");
  1339. of_node_put(dcmi->entity.node);
  1340. return ret;
  1341. }
  1342. return 0;
  1343. }
  1344. static int dcmi_probe(struct platform_device *pdev)
  1345. {
  1346. struct device_node *np = pdev->dev.of_node;
  1347. const struct of_device_id *match = NULL;
  1348. struct v4l2_fwnode_endpoint ep;
  1349. struct stm32_dcmi *dcmi;
  1350. struct vb2_queue *q;
  1351. struct dma_chan *chan;
  1352. struct clk *mclk;
  1353. int irq;
  1354. int ret = 0;
  1355. match = of_match_device(of_match_ptr(stm32_dcmi_of_match), &pdev->dev);
  1356. if (!match) {
  1357. dev_err(&pdev->dev, "Could not find a match in devicetree\n");
  1358. return -ENODEV;
  1359. }
  1360. dcmi = devm_kzalloc(&pdev->dev, sizeof(struct stm32_dcmi), GFP_KERNEL);
  1361. if (!dcmi)
  1362. return -ENOMEM;
  1363. dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  1364. if (IS_ERR(dcmi->rstc)) {
  1365. dev_err(&pdev->dev, "Could not get reset control\n");
  1366. return PTR_ERR(dcmi->rstc);
  1367. }
  1368. /* Get bus characteristics from devicetree */
  1369. np = of_graph_get_next_endpoint(np, NULL);
  1370. if (!np) {
  1371. dev_err(&pdev->dev, "Could not find the endpoint\n");
  1372. of_node_put(np);
  1373. return -ENODEV;
  1374. }
  1375. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
  1376. of_node_put(np);
  1377. if (ret) {
  1378. dev_err(&pdev->dev, "Could not parse the endpoint\n");
  1379. return ret;
  1380. }
  1381. if (ep.bus_type == V4L2_MBUS_CSI2) {
  1382. dev_err(&pdev->dev, "CSI bus not supported\n");
  1383. return -ENODEV;
  1384. }
  1385. dcmi->bus.flags = ep.bus.parallel.flags;
  1386. dcmi->bus.bus_width = ep.bus.parallel.bus_width;
  1387. dcmi->bus.data_shift = ep.bus.parallel.data_shift;
  1388. irq = platform_get_irq(pdev, 0);
  1389. if (irq <= 0) {
  1390. if (irq != -EPROBE_DEFER)
  1391. dev_err(&pdev->dev, "Could not get irq\n");
  1392. return irq ? irq : -ENXIO;
  1393. }
  1394. dcmi->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1395. if (!dcmi->res) {
  1396. dev_err(&pdev->dev, "Could not get resource\n");
  1397. return -ENODEV;
  1398. }
  1399. dcmi->regs = devm_ioremap_resource(&pdev->dev, dcmi->res);
  1400. if (IS_ERR(dcmi->regs)) {
  1401. dev_err(&pdev->dev, "Could not map registers\n");
  1402. return PTR_ERR(dcmi->regs);
  1403. }
  1404. ret = devm_request_threaded_irq(&pdev->dev, irq, dcmi_irq_callback,
  1405. dcmi_irq_thread, IRQF_ONESHOT,
  1406. dev_name(&pdev->dev), dcmi);
  1407. if (ret) {
  1408. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  1409. return ret;
  1410. }
  1411. mclk = devm_clk_get(&pdev->dev, "mclk");
  1412. if (IS_ERR(mclk)) {
  1413. if (PTR_ERR(mclk) != -EPROBE_DEFER)
  1414. dev_err(&pdev->dev, "Unable to get mclk\n");
  1415. return PTR_ERR(mclk);
  1416. }
  1417. chan = dma_request_slave_channel(&pdev->dev, "tx");
  1418. if (!chan) {
  1419. dev_info(&pdev->dev, "Unable to request DMA channel, defer probing\n");
  1420. return -EPROBE_DEFER;
  1421. }
  1422. spin_lock_init(&dcmi->irqlock);
  1423. mutex_init(&dcmi->lock);
  1424. mutex_init(&dcmi->dma_lock);
  1425. init_completion(&dcmi->complete);
  1426. INIT_LIST_HEAD(&dcmi->buffers);
  1427. dcmi->dev = &pdev->dev;
  1428. dcmi->mclk = mclk;
  1429. dcmi->state = STOPPED;
  1430. dcmi->dma_chan = chan;
  1431. q = &dcmi->queue;
  1432. /* Initialize the top-level structure */
  1433. ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev);
  1434. if (ret)
  1435. goto err_dma_release;
  1436. dcmi->vdev = video_device_alloc();
  1437. if (!dcmi->vdev) {
  1438. ret = -ENOMEM;
  1439. goto err_device_unregister;
  1440. }
  1441. /* Video node */
  1442. dcmi->vdev->fops = &dcmi_fops;
  1443. dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev;
  1444. dcmi->vdev->queue = &dcmi->queue;
  1445. strlcpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name));
  1446. dcmi->vdev->release = video_device_release;
  1447. dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops;
  1448. dcmi->vdev->lock = &dcmi->lock;
  1449. dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  1450. V4L2_CAP_READWRITE;
  1451. video_set_drvdata(dcmi->vdev, dcmi);
  1452. /* Buffer queue */
  1453. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1454. q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
  1455. q->lock = &dcmi->lock;
  1456. q->drv_priv = dcmi;
  1457. q->buf_struct_size = sizeof(struct dcmi_buf);
  1458. q->ops = &dcmi_video_qops;
  1459. q->mem_ops = &vb2_dma_contig_memops;
  1460. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1461. q->min_buffers_needed = 2;
  1462. q->dev = &pdev->dev;
  1463. ret = vb2_queue_init(q);
  1464. if (ret < 0) {
  1465. dev_err(&pdev->dev, "Failed to initialize vb2 queue\n");
  1466. goto err_device_release;
  1467. }
  1468. ret = dcmi_graph_init(dcmi);
  1469. if (ret < 0)
  1470. goto err_device_release;
  1471. /* Reset device */
  1472. ret = reset_control_assert(dcmi->rstc);
  1473. if (ret) {
  1474. dev_err(&pdev->dev, "Failed to assert the reset line\n");
  1475. goto err_device_release;
  1476. }
  1477. usleep_range(3000, 5000);
  1478. ret = reset_control_deassert(dcmi->rstc);
  1479. if (ret) {
  1480. dev_err(&pdev->dev, "Failed to deassert the reset line\n");
  1481. goto err_device_release;
  1482. }
  1483. dev_info(&pdev->dev, "Probe done\n");
  1484. platform_set_drvdata(pdev, dcmi);
  1485. pm_runtime_enable(&pdev->dev);
  1486. return 0;
  1487. err_device_release:
  1488. video_device_release(dcmi->vdev);
  1489. err_device_unregister:
  1490. v4l2_device_unregister(&dcmi->v4l2_dev);
  1491. err_dma_release:
  1492. dma_release_channel(dcmi->dma_chan);
  1493. return ret;
  1494. }
  1495. static int dcmi_remove(struct platform_device *pdev)
  1496. {
  1497. struct stm32_dcmi *dcmi = platform_get_drvdata(pdev);
  1498. pm_runtime_disable(&pdev->dev);
  1499. v4l2_async_notifier_unregister(&dcmi->notifier);
  1500. v4l2_device_unregister(&dcmi->v4l2_dev);
  1501. dma_release_channel(dcmi->dma_chan);
  1502. return 0;
  1503. }
  1504. static __maybe_unused int dcmi_runtime_suspend(struct device *dev)
  1505. {
  1506. struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
  1507. clk_disable_unprepare(dcmi->mclk);
  1508. return 0;
  1509. }
  1510. static __maybe_unused int dcmi_runtime_resume(struct device *dev)
  1511. {
  1512. struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
  1513. int ret;
  1514. ret = clk_prepare_enable(dcmi->mclk);
  1515. if (ret)
  1516. dev_err(dev, "%s: Failed to prepare_enable clock\n", __func__);
  1517. return ret;
  1518. }
  1519. static __maybe_unused int dcmi_suspend(struct device *dev)
  1520. {
  1521. /* disable clock */
  1522. pm_runtime_force_suspend(dev);
  1523. /* change pinctrl state */
  1524. pinctrl_pm_select_sleep_state(dev);
  1525. return 0;
  1526. }
  1527. static __maybe_unused int dcmi_resume(struct device *dev)
  1528. {
  1529. /* restore pinctl default state */
  1530. pinctrl_pm_select_default_state(dev);
  1531. /* clock enable */
  1532. pm_runtime_force_resume(dev);
  1533. return 0;
  1534. }
  1535. static const struct dev_pm_ops dcmi_pm_ops = {
  1536. SET_SYSTEM_SLEEP_PM_OPS(dcmi_suspend, dcmi_resume)
  1537. SET_RUNTIME_PM_OPS(dcmi_runtime_suspend,
  1538. dcmi_runtime_resume, NULL)
  1539. };
  1540. static struct platform_driver stm32_dcmi_driver = {
  1541. .probe = dcmi_probe,
  1542. .remove = dcmi_remove,
  1543. .driver = {
  1544. .name = DRV_NAME,
  1545. .of_match_table = of_match_ptr(stm32_dcmi_of_match),
  1546. .pm = &dcmi_pm_ops,
  1547. },
  1548. };
  1549. module_platform_driver(stm32_dcmi_driver);
  1550. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  1551. MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
  1552. MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");
  1553. MODULE_LICENSE("GPL");
  1554. MODULE_SUPPORTED_DEVICE("video");