stm32-dcmi.c 43 KB

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