stm32-dcmi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Driver for STM32 Digital Camera Memory Interface
  3. *
  4. * Copyright (C) STMicroelectronics SA 2017
  5. * Authors: Yannick Fertre <yannick.fertre@st.com>
  6. * Hugues Fruchet <hugues.fruchet@st.com>
  7. * for STMicroelectronics.
  8. * License terms: GNU General Public License (GPL), version 2
  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/videobuf2-dma-contig.h>
  35. #define DRV_NAME "stm32-dcmi"
  36. /* Registers offset for DCMI */
  37. #define DCMI_CR 0x00 /* Control Register */
  38. #define DCMI_SR 0x04 /* Status Register */
  39. #define DCMI_RIS 0x08 /* Raw Interrupt Status register */
  40. #define DCMI_IER 0x0C /* Interrupt Enable Register */
  41. #define DCMI_MIS 0x10 /* Masked Interrupt Status register */
  42. #define DCMI_ICR 0x14 /* Interrupt Clear Register */
  43. #define DCMI_ESCR 0x18 /* Embedded Synchronization Code Register */
  44. #define DCMI_ESUR 0x1C /* Embedded Synchronization Unmask Register */
  45. #define DCMI_CWSTRT 0x20 /* Crop Window STaRT */
  46. #define DCMI_CWSIZE 0x24 /* Crop Window SIZE */
  47. #define DCMI_DR 0x28 /* Data Register */
  48. #define DCMI_IDR 0x2C /* IDentifier Register */
  49. /* Bits definition for control register (DCMI_CR) */
  50. #define CR_CAPTURE BIT(0)
  51. #define CR_CM BIT(1)
  52. #define CR_CROP BIT(2)
  53. #define CR_JPEG BIT(3)
  54. #define CR_ESS BIT(4)
  55. #define CR_PCKPOL BIT(5)
  56. #define CR_HSPOL BIT(6)
  57. #define CR_VSPOL BIT(7)
  58. #define CR_FCRC_0 BIT(8)
  59. #define CR_FCRC_1 BIT(9)
  60. #define CR_EDM_0 BIT(10)
  61. #define CR_EDM_1 BIT(11)
  62. #define CR_ENABLE BIT(14)
  63. /* Bits definition for status register (DCMI_SR) */
  64. #define SR_HSYNC BIT(0)
  65. #define SR_VSYNC BIT(1)
  66. #define SR_FNE BIT(2)
  67. /*
  68. * Bits definition for interrupt registers
  69. * (DCMI_RIS, DCMI_IER, DCMI_MIS, DCMI_ICR)
  70. */
  71. #define IT_FRAME BIT(0)
  72. #define IT_OVR BIT(1)
  73. #define IT_ERR BIT(2)
  74. #define IT_VSYNC BIT(3)
  75. #define IT_LINE BIT(4)
  76. enum state {
  77. STOPPED = 0,
  78. RUNNING,
  79. STOPPING,
  80. };
  81. #define MIN_WIDTH 16U
  82. #define MAX_WIDTH 2048U
  83. #define MIN_HEIGHT 16U
  84. #define MAX_HEIGHT 2048U
  85. #define TIMEOUT_MS 1000
  86. struct dcmi_graph_entity {
  87. struct device_node *node;
  88. struct v4l2_async_subdev asd;
  89. struct v4l2_subdev *subdev;
  90. };
  91. struct dcmi_format {
  92. u32 fourcc;
  93. u32 mbus_code;
  94. u8 bpp;
  95. };
  96. struct dcmi_buf {
  97. struct vb2_v4l2_buffer vb;
  98. bool prepared;
  99. dma_addr_t paddr;
  100. size_t size;
  101. struct list_head list;
  102. };
  103. struct stm32_dcmi {
  104. /* Protects the access of variables shared within the interrupt */
  105. spinlock_t irqlock;
  106. struct device *dev;
  107. void __iomem *regs;
  108. struct resource *res;
  109. struct reset_control *rstc;
  110. int sequence;
  111. struct list_head buffers;
  112. struct dcmi_buf *active;
  113. struct v4l2_device v4l2_dev;
  114. struct video_device *vdev;
  115. struct v4l2_async_notifier notifier;
  116. struct dcmi_graph_entity entity;
  117. struct v4l2_format fmt;
  118. const struct dcmi_format **user_formats;
  119. unsigned int num_user_formats;
  120. const struct dcmi_format *current_fmt;
  121. /* Protect this data structure */
  122. struct mutex lock;
  123. struct vb2_queue queue;
  124. struct v4l2_fwnode_bus_parallel bus;
  125. struct completion complete;
  126. struct clk *mclk;
  127. enum state state;
  128. struct dma_chan *dma_chan;
  129. dma_cookie_t dma_cookie;
  130. u32 misr;
  131. int errors_count;
  132. int buffers_count;
  133. };
  134. static inline struct stm32_dcmi *notifier_to_dcmi(struct v4l2_async_notifier *n)
  135. {
  136. return container_of(n, struct stm32_dcmi, notifier);
  137. }
  138. static inline u32 reg_read(void __iomem *base, u32 reg)
  139. {
  140. return readl_relaxed(base + reg);
  141. }
  142. static inline void reg_write(void __iomem *base, u32 reg, u32 val)
  143. {
  144. writel_relaxed(val, base + reg);
  145. }
  146. static inline void reg_set(void __iomem *base, u32 reg, u32 mask)
  147. {
  148. reg_write(base, reg, reg_read(base, reg) | mask);
  149. }
  150. static inline void reg_clear(void __iomem *base, u32 reg, u32 mask)
  151. {
  152. reg_write(base, reg, reg_read(base, reg) & ~mask);
  153. }
  154. static int dcmi_start_capture(struct stm32_dcmi *dcmi);
  155. static void dcmi_dma_callback(void *param)
  156. {
  157. struct stm32_dcmi *dcmi = (struct stm32_dcmi *)param;
  158. struct dma_chan *chan = dcmi->dma_chan;
  159. struct dma_tx_state state;
  160. enum dma_status status;
  161. spin_lock(&dcmi->irqlock);
  162. /* Check DMA status */
  163. status = dmaengine_tx_status(chan, dcmi->dma_cookie, &state);
  164. switch (status) {
  165. case DMA_IN_PROGRESS:
  166. dev_dbg(dcmi->dev, "%s: Received DMA_IN_PROGRESS\n", __func__);
  167. break;
  168. case DMA_PAUSED:
  169. dev_err(dcmi->dev, "%s: Received DMA_PAUSED\n", __func__);
  170. break;
  171. case DMA_ERROR:
  172. dev_err(dcmi->dev, "%s: Received DMA_ERROR\n", __func__);
  173. break;
  174. case DMA_COMPLETE:
  175. dev_dbg(dcmi->dev, "%s: Received DMA_COMPLETE\n", __func__);
  176. if (dcmi->active) {
  177. struct dcmi_buf *buf = dcmi->active;
  178. struct vb2_v4l2_buffer *vbuf = &dcmi->active->vb;
  179. vbuf->sequence = dcmi->sequence++;
  180. vbuf->field = V4L2_FIELD_NONE;
  181. vbuf->vb2_buf.timestamp = ktime_get_ns();
  182. vb2_set_plane_payload(&vbuf->vb2_buf, 0, buf->size);
  183. vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
  184. dev_dbg(dcmi->dev, "buffer[%d] done seq=%d\n",
  185. vbuf->vb2_buf.index, vbuf->sequence);
  186. dcmi->buffers_count++;
  187. dcmi->active = NULL;
  188. }
  189. /* Restart a new DMA transfer with next buffer */
  190. if (dcmi->state == RUNNING) {
  191. if (list_empty(&dcmi->buffers)) {
  192. dev_err(dcmi->dev, "%s: No more buffer queued, cannot capture buffer",
  193. __func__);
  194. dcmi->errors_count++;
  195. dcmi->active = NULL;
  196. spin_unlock(&dcmi->irqlock);
  197. return;
  198. }
  199. dcmi->active = list_entry(dcmi->buffers.next,
  200. struct dcmi_buf, list);
  201. list_del_init(&dcmi->active->list);
  202. if (dcmi_start_capture(dcmi)) {
  203. dev_err(dcmi->dev, "%s: Cannot restart capture on DMA complete",
  204. __func__);
  205. spin_unlock(&dcmi->irqlock);
  206. return;
  207. }
  208. /* Enable capture */
  209. reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE);
  210. }
  211. break;
  212. default:
  213. dev_err(dcmi->dev, "%s: Received unknown status\n", __func__);
  214. break;
  215. }
  216. spin_unlock(&dcmi->irqlock);
  217. }
  218. static int dcmi_start_dma(struct stm32_dcmi *dcmi,
  219. struct dcmi_buf *buf)
  220. {
  221. struct dma_async_tx_descriptor *desc = NULL;
  222. struct dma_slave_config config;
  223. int ret;
  224. memset(&config, 0, sizeof(config));
  225. config.src_addr = (dma_addr_t)dcmi->res->start + DCMI_DR;
  226. config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  227. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  228. config.dst_maxburst = 4;
  229. /* Configure DMA channel */
  230. ret = dmaengine_slave_config(dcmi->dma_chan, &config);
  231. if (ret < 0) {
  232. dev_err(dcmi->dev, "%s: DMA channel config failed (%d)\n",
  233. __func__, ret);
  234. return ret;
  235. }
  236. /* Prepare a DMA transaction */
  237. desc = dmaengine_prep_slave_single(dcmi->dma_chan, buf->paddr,
  238. buf->size,
  239. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  240. if (!desc) {
  241. dev_err(dcmi->dev, "%s: DMA dmaengine_prep_slave_single failed for buffer size %zu\n",
  242. __func__, buf->size);
  243. return -EINVAL;
  244. }
  245. /* Set completion callback routine for notification */
  246. desc->callback = dcmi_dma_callback;
  247. desc->callback_param = dcmi;
  248. /* Push current DMA transaction in the pending queue */
  249. dcmi->dma_cookie = dmaengine_submit(desc);
  250. if (dma_submit_error(dcmi->dma_cookie)) {
  251. dev_err(dcmi->dev, "%s: DMA submission failed\n", __func__);
  252. return -ENXIO;
  253. }
  254. dma_async_issue_pending(dcmi->dma_chan);
  255. return 0;
  256. }
  257. static int dcmi_start_capture(struct stm32_dcmi *dcmi)
  258. {
  259. int ret;
  260. struct dcmi_buf *buf = dcmi->active;
  261. if (!buf)
  262. return -EINVAL;
  263. ret = dcmi_start_dma(dcmi, buf);
  264. if (ret) {
  265. dcmi->errors_count++;
  266. return ret;
  267. }
  268. /* Enable capture */
  269. reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE);
  270. return 0;
  271. }
  272. static irqreturn_t dcmi_irq_thread(int irq, void *arg)
  273. {
  274. struct stm32_dcmi *dcmi = arg;
  275. spin_lock(&dcmi->irqlock);
  276. /* Stop capture is required */
  277. if (dcmi->state == STOPPING) {
  278. reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
  279. dcmi->state = STOPPED;
  280. complete(&dcmi->complete);
  281. spin_unlock(&dcmi->irqlock);
  282. return IRQ_HANDLED;
  283. }
  284. if ((dcmi->misr & IT_OVR) || (dcmi->misr & IT_ERR)) {
  285. /*
  286. * An overflow or an error has been detected,
  287. * stop current DMA transfert & restart it
  288. */
  289. dev_warn(dcmi->dev, "%s: Overflow or error detected\n",
  290. __func__);
  291. dcmi->errors_count++;
  292. dmaengine_terminate_all(dcmi->dma_chan);
  293. reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR);
  294. dev_dbg(dcmi->dev, "Restarting capture after DCMI error\n");
  295. if (dcmi_start_capture(dcmi)) {
  296. dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n",
  297. __func__);
  298. spin_unlock(&dcmi->irqlock);
  299. return IRQ_HANDLED;
  300. }
  301. }
  302. spin_unlock(&dcmi->irqlock);
  303. return IRQ_HANDLED;
  304. }
  305. static irqreturn_t dcmi_irq_callback(int irq, void *arg)
  306. {
  307. struct stm32_dcmi *dcmi = arg;
  308. spin_lock(&dcmi->irqlock);
  309. dcmi->misr = reg_read(dcmi->regs, DCMI_MIS);
  310. /* Clear interrupt */
  311. reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR);
  312. spin_unlock(&dcmi->irqlock);
  313. return IRQ_WAKE_THREAD;
  314. }
  315. static int dcmi_queue_setup(struct vb2_queue *vq,
  316. unsigned int *nbuffers,
  317. unsigned int *nplanes,
  318. unsigned int sizes[],
  319. struct device *alloc_devs[])
  320. {
  321. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  322. unsigned int size;
  323. size = dcmi->fmt.fmt.pix.sizeimage;
  324. /* Make sure the image size is large enough */
  325. if (*nplanes)
  326. return sizes[0] < size ? -EINVAL : 0;
  327. *nplanes = 1;
  328. sizes[0] = size;
  329. dcmi->active = NULL;
  330. dev_dbg(dcmi->dev, "Setup queue, count=%d, size=%d\n",
  331. *nbuffers, size);
  332. return 0;
  333. }
  334. static int dcmi_buf_init(struct vb2_buffer *vb)
  335. {
  336. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  337. struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
  338. INIT_LIST_HEAD(&buf->list);
  339. return 0;
  340. }
  341. static int dcmi_buf_prepare(struct vb2_buffer *vb)
  342. {
  343. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
  344. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  345. struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
  346. unsigned long size;
  347. size = dcmi->fmt.fmt.pix.sizeimage;
  348. if (vb2_plane_size(vb, 0) < size) {
  349. dev_err(dcmi->dev, "%s data will not fit into plane (%lu < %lu)\n",
  350. __func__, vb2_plane_size(vb, 0), size);
  351. return -EINVAL;
  352. }
  353. vb2_set_plane_payload(vb, 0, size);
  354. if (!buf->prepared) {
  355. /* Get memory addresses */
  356. buf->paddr =
  357. vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  358. buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0);
  359. buf->prepared = true;
  360. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size);
  361. dev_dbg(dcmi->dev, "buffer[%d] phy=0x%pad size=%zu\n",
  362. vb->index, &buf->paddr, buf->size);
  363. }
  364. return 0;
  365. }
  366. static void dcmi_buf_queue(struct vb2_buffer *vb)
  367. {
  368. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
  369. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  370. struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
  371. unsigned long flags = 0;
  372. spin_lock_irqsave(&dcmi->irqlock, flags);
  373. if ((dcmi->state == RUNNING) && (!dcmi->active)) {
  374. dcmi->active = buf;
  375. dev_dbg(dcmi->dev, "Starting capture on buffer[%d] queued\n",
  376. buf->vb.vb2_buf.index);
  377. if (dcmi_start_capture(dcmi)) {
  378. dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n",
  379. __func__);
  380. spin_unlock_irqrestore(&dcmi->irqlock, flags);
  381. return;
  382. }
  383. } else {
  384. /* Enqueue to video buffers list */
  385. list_add_tail(&buf->list, &dcmi->buffers);
  386. }
  387. spin_unlock_irqrestore(&dcmi->irqlock, flags);
  388. }
  389. static int dcmi_start_streaming(struct vb2_queue *vq, unsigned int count)
  390. {
  391. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  392. struct dcmi_buf *buf, *node;
  393. u32 val = 0;
  394. int ret;
  395. ret = clk_enable(dcmi->mclk);
  396. if (ret) {
  397. dev_err(dcmi->dev, "%s: Failed to start streaming, cannot enable clock",
  398. __func__);
  399. goto err_release_buffers;
  400. }
  401. /* Enable stream on the sub device */
  402. ret = v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 1);
  403. if (ret && ret != -ENOIOCTLCMD) {
  404. dev_err(dcmi->dev, "%s: Failed to start streaming, subdev streamon error",
  405. __func__);
  406. goto err_disable_clock;
  407. }
  408. spin_lock_irq(&dcmi->irqlock);
  409. /* Set bus width */
  410. switch (dcmi->bus.bus_width) {
  411. case 14:
  412. val |= CR_EDM_0 | CR_EDM_1;
  413. break;
  414. case 12:
  415. val |= CR_EDM_1;
  416. break;
  417. case 10:
  418. val |= CR_EDM_0;
  419. break;
  420. default:
  421. /* Set bus width to 8 bits by default */
  422. break;
  423. }
  424. /* Set vertical synchronization polarity */
  425. if (dcmi->bus.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  426. val |= CR_VSPOL;
  427. /* Set horizontal synchronization polarity */
  428. if (dcmi->bus.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  429. val |= CR_HSPOL;
  430. /* Set pixel clock polarity */
  431. if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  432. val |= CR_PCKPOL;
  433. reg_write(dcmi->regs, DCMI_CR, val);
  434. /* Enable dcmi */
  435. reg_set(dcmi->regs, DCMI_CR, CR_ENABLE);
  436. dcmi->state = RUNNING;
  437. dcmi->sequence = 0;
  438. dcmi->errors_count = 0;
  439. dcmi->buffers_count = 0;
  440. dcmi->active = NULL;
  441. /*
  442. * Start transfer if at least one buffer has been queued,
  443. * otherwise transfer is deferred at buffer queueing
  444. */
  445. if (list_empty(&dcmi->buffers)) {
  446. dev_dbg(dcmi->dev, "Start streaming is deferred to next buffer queueing\n");
  447. spin_unlock_irq(&dcmi->irqlock);
  448. return 0;
  449. }
  450. dcmi->active = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
  451. list_del_init(&dcmi->active->list);
  452. dev_dbg(dcmi->dev, "Start streaming, starting capture\n");
  453. ret = dcmi_start_capture(dcmi);
  454. if (ret) {
  455. dev_err(dcmi->dev, "%s: Start streaming failed, cannot start capture",
  456. __func__);
  457. spin_unlock_irq(&dcmi->irqlock);
  458. goto err_subdev_streamoff;
  459. }
  460. /* Enable interruptions */
  461. reg_set(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
  462. spin_unlock_irq(&dcmi->irqlock);
  463. return 0;
  464. err_subdev_streamoff:
  465. v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 0);
  466. err_disable_clock:
  467. clk_disable(dcmi->mclk);
  468. err_release_buffers:
  469. spin_lock_irq(&dcmi->irqlock);
  470. /*
  471. * Return all buffers to vb2 in QUEUED state.
  472. * This will give ownership back to userspace
  473. */
  474. if (dcmi->active) {
  475. buf = dcmi->active;
  476. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  477. dcmi->active = NULL;
  478. }
  479. list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
  480. list_del_init(&buf->list);
  481. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  482. }
  483. spin_unlock_irq(&dcmi->irqlock);
  484. return ret;
  485. }
  486. static void dcmi_stop_streaming(struct vb2_queue *vq)
  487. {
  488. struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
  489. struct dcmi_buf *buf, *node;
  490. unsigned long time_ms = msecs_to_jiffies(TIMEOUT_MS);
  491. long timeout;
  492. int ret;
  493. /* Disable stream on the sub device */
  494. ret = v4l2_subdev_call(dcmi->entity.subdev, video, s_stream, 0);
  495. if (ret && ret != -ENOIOCTLCMD)
  496. dev_err(dcmi->dev, "stream off failed in subdev\n");
  497. dcmi->state = STOPPING;
  498. timeout = wait_for_completion_interruptible_timeout(&dcmi->complete,
  499. time_ms);
  500. spin_lock_irq(&dcmi->irqlock);
  501. /* Disable interruptions */
  502. reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
  503. /* Disable DCMI */
  504. reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE);
  505. if (!timeout) {
  506. dev_err(dcmi->dev, "Timeout during stop streaming\n");
  507. dcmi->state = STOPPED;
  508. }
  509. /* Return all queued buffers to vb2 in ERROR state */
  510. if (dcmi->active) {
  511. buf = dcmi->active;
  512. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  513. dcmi->active = NULL;
  514. }
  515. list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
  516. list_del_init(&buf->list);
  517. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  518. }
  519. spin_unlock_irq(&dcmi->irqlock);
  520. /* Stop all pending DMA operations */
  521. dmaengine_terminate_all(dcmi->dma_chan);
  522. clk_disable(dcmi->mclk);
  523. dev_dbg(dcmi->dev, "Stop streaming, errors=%d buffers=%d\n",
  524. dcmi->errors_count, dcmi->buffers_count);
  525. }
  526. static const struct vb2_ops dcmi_video_qops = {
  527. .queue_setup = dcmi_queue_setup,
  528. .buf_init = dcmi_buf_init,
  529. .buf_prepare = dcmi_buf_prepare,
  530. .buf_queue = dcmi_buf_queue,
  531. .start_streaming = dcmi_start_streaming,
  532. .stop_streaming = dcmi_stop_streaming,
  533. .wait_prepare = vb2_ops_wait_prepare,
  534. .wait_finish = vb2_ops_wait_finish,
  535. };
  536. static int dcmi_g_fmt_vid_cap(struct file *file, void *priv,
  537. struct v4l2_format *fmt)
  538. {
  539. struct stm32_dcmi *dcmi = video_drvdata(file);
  540. *fmt = dcmi->fmt;
  541. return 0;
  542. }
  543. static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi,
  544. unsigned int fourcc)
  545. {
  546. unsigned int num_formats = dcmi->num_user_formats;
  547. const struct dcmi_format *fmt;
  548. unsigned int i;
  549. for (i = 0; i < num_formats; i++) {
  550. fmt = dcmi->user_formats[i];
  551. if (fmt->fourcc == fourcc)
  552. return fmt;
  553. }
  554. return NULL;
  555. }
  556. static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f,
  557. const struct dcmi_format **current_fmt)
  558. {
  559. const struct dcmi_format *dcmi_fmt;
  560. struct v4l2_pix_format *pixfmt = &f->fmt.pix;
  561. struct v4l2_subdev_pad_config pad_cfg;
  562. struct v4l2_subdev_format format = {
  563. .which = V4L2_SUBDEV_FORMAT_TRY,
  564. };
  565. int ret;
  566. dcmi_fmt = find_format_by_fourcc(dcmi, pixfmt->pixelformat);
  567. if (!dcmi_fmt) {
  568. dcmi_fmt = dcmi->user_formats[dcmi->num_user_formats - 1];
  569. pixfmt->pixelformat = dcmi_fmt->fourcc;
  570. }
  571. /* Limit to hardware capabilities */
  572. pixfmt->width = clamp(pixfmt->width, MIN_WIDTH, MAX_WIDTH);
  573. pixfmt->height = clamp(pixfmt->height, MIN_HEIGHT, MAX_HEIGHT);
  574. v4l2_fill_mbus_format(&format.format, pixfmt, dcmi_fmt->mbus_code);
  575. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, set_fmt,
  576. &pad_cfg, &format);
  577. if (ret < 0)
  578. return ret;
  579. v4l2_fill_pix_format(pixfmt, &format.format);
  580. pixfmt->field = V4L2_FIELD_NONE;
  581. pixfmt->bytesperline = pixfmt->width * dcmi_fmt->bpp;
  582. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  583. if (current_fmt)
  584. *current_fmt = dcmi_fmt;
  585. return 0;
  586. }
  587. static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f)
  588. {
  589. struct v4l2_subdev_format format = {
  590. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  591. };
  592. const struct dcmi_format *current_fmt;
  593. int ret;
  594. ret = dcmi_try_fmt(dcmi, f, &current_fmt);
  595. if (ret)
  596. return ret;
  597. v4l2_fill_mbus_format(&format.format, &f->fmt.pix,
  598. current_fmt->mbus_code);
  599. ret = v4l2_subdev_call(dcmi->entity.subdev, pad,
  600. set_fmt, NULL, &format);
  601. if (ret < 0)
  602. return ret;
  603. dcmi->fmt = *f;
  604. dcmi->current_fmt = current_fmt;
  605. return 0;
  606. }
  607. static int dcmi_s_fmt_vid_cap(struct file *file, void *priv,
  608. struct v4l2_format *f)
  609. {
  610. struct stm32_dcmi *dcmi = video_drvdata(file);
  611. if (vb2_is_streaming(&dcmi->queue))
  612. return -EBUSY;
  613. return dcmi_set_fmt(dcmi, f);
  614. }
  615. static int dcmi_try_fmt_vid_cap(struct file *file, void *priv,
  616. struct v4l2_format *f)
  617. {
  618. struct stm32_dcmi *dcmi = video_drvdata(file);
  619. return dcmi_try_fmt(dcmi, f, NULL);
  620. }
  621. static int dcmi_enum_fmt_vid_cap(struct file *file, void *priv,
  622. struct v4l2_fmtdesc *f)
  623. {
  624. struct stm32_dcmi *dcmi = video_drvdata(file);
  625. if (f->index >= dcmi->num_user_formats)
  626. return -EINVAL;
  627. f->pixelformat = dcmi->user_formats[f->index]->fourcc;
  628. return 0;
  629. }
  630. static int dcmi_querycap(struct file *file, void *priv,
  631. struct v4l2_capability *cap)
  632. {
  633. strlcpy(cap->driver, DRV_NAME, sizeof(cap->driver));
  634. strlcpy(cap->card, "STM32 Camera Memory Interface",
  635. sizeof(cap->card));
  636. strlcpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info));
  637. return 0;
  638. }
  639. static int dcmi_enum_input(struct file *file, void *priv,
  640. struct v4l2_input *i)
  641. {
  642. if (i->index != 0)
  643. return -EINVAL;
  644. i->type = V4L2_INPUT_TYPE_CAMERA;
  645. strlcpy(i->name, "Camera", sizeof(i->name));
  646. return 0;
  647. }
  648. static int dcmi_g_input(struct file *file, void *priv, unsigned int *i)
  649. {
  650. *i = 0;
  651. return 0;
  652. }
  653. static int dcmi_s_input(struct file *file, void *priv, unsigned int i)
  654. {
  655. if (i > 0)
  656. return -EINVAL;
  657. return 0;
  658. }
  659. static int dcmi_enum_framesizes(struct file *file, void *fh,
  660. struct v4l2_frmsizeenum *fsize)
  661. {
  662. struct stm32_dcmi *dcmi = video_drvdata(file);
  663. const struct dcmi_format *dcmi_fmt;
  664. struct v4l2_subdev_frame_size_enum fse = {
  665. .index = fsize->index,
  666. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  667. };
  668. int ret;
  669. dcmi_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format);
  670. if (!dcmi_fmt)
  671. return -EINVAL;
  672. fse.code = dcmi_fmt->mbus_code;
  673. ret = v4l2_subdev_call(dcmi->entity.subdev, pad, enum_frame_size,
  674. NULL, &fse);
  675. if (ret)
  676. return ret;
  677. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  678. fsize->discrete.width = fse.max_width;
  679. fsize->discrete.height = fse.max_height;
  680. return 0;
  681. }
  682. static int dcmi_enum_frameintervals(struct file *file, void *fh,
  683. struct v4l2_frmivalenum *fival)
  684. {
  685. struct stm32_dcmi *dcmi = video_drvdata(file);
  686. const struct dcmi_format *dcmi_fmt;
  687. struct v4l2_subdev_frame_interval_enum fie = {
  688. .index = fival->index,
  689. .width = fival->width,
  690. .height = fival->height,
  691. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  692. };
  693. int ret;
  694. dcmi_fmt = find_format_by_fourcc(dcmi, fival->pixel_format);
  695. if (!dcmi_fmt)
  696. return -EINVAL;
  697. fie.code = dcmi_fmt->mbus_code;
  698. ret = v4l2_subdev_call(dcmi->entity.subdev, pad,
  699. enum_frame_interval, NULL, &fie);
  700. if (ret)
  701. return ret;
  702. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  703. fival->discrete = fie.interval;
  704. return 0;
  705. }
  706. static const struct of_device_id stm32_dcmi_of_match[] = {
  707. { .compatible = "st,stm32-dcmi"},
  708. { /* end node */ },
  709. };
  710. MODULE_DEVICE_TABLE(of, stm32_dcmi_of_match);
  711. static int dcmi_open(struct file *file)
  712. {
  713. struct stm32_dcmi *dcmi = video_drvdata(file);
  714. struct v4l2_subdev *sd = dcmi->entity.subdev;
  715. int ret;
  716. if (mutex_lock_interruptible(&dcmi->lock))
  717. return -ERESTARTSYS;
  718. ret = v4l2_fh_open(file);
  719. if (ret < 0)
  720. goto unlock;
  721. if (!v4l2_fh_is_singular_file(file))
  722. goto fh_rel;
  723. ret = v4l2_subdev_call(sd, core, s_power, 1);
  724. if (ret < 0 && ret != -ENOIOCTLCMD)
  725. goto fh_rel;
  726. ret = dcmi_set_fmt(dcmi, &dcmi->fmt);
  727. if (ret)
  728. v4l2_subdev_call(sd, core, s_power, 0);
  729. fh_rel:
  730. if (ret)
  731. v4l2_fh_release(file);
  732. unlock:
  733. mutex_unlock(&dcmi->lock);
  734. return ret;
  735. }
  736. static int dcmi_release(struct file *file)
  737. {
  738. struct stm32_dcmi *dcmi = video_drvdata(file);
  739. struct v4l2_subdev *sd = dcmi->entity.subdev;
  740. bool fh_singular;
  741. int ret;
  742. mutex_lock(&dcmi->lock);
  743. fh_singular = v4l2_fh_is_singular_file(file);
  744. ret = _vb2_fop_release(file, NULL);
  745. if (fh_singular)
  746. v4l2_subdev_call(sd, core, s_power, 0);
  747. mutex_unlock(&dcmi->lock);
  748. return ret;
  749. }
  750. static const struct v4l2_ioctl_ops dcmi_ioctl_ops = {
  751. .vidioc_querycap = dcmi_querycap,
  752. .vidioc_try_fmt_vid_cap = dcmi_try_fmt_vid_cap,
  753. .vidioc_g_fmt_vid_cap = dcmi_g_fmt_vid_cap,
  754. .vidioc_s_fmt_vid_cap = dcmi_s_fmt_vid_cap,
  755. .vidioc_enum_fmt_vid_cap = dcmi_enum_fmt_vid_cap,
  756. .vidioc_enum_input = dcmi_enum_input,
  757. .vidioc_g_input = dcmi_g_input,
  758. .vidioc_s_input = dcmi_s_input,
  759. .vidioc_enum_framesizes = dcmi_enum_framesizes,
  760. .vidioc_enum_frameintervals = dcmi_enum_frameintervals,
  761. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  762. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  763. .vidioc_querybuf = vb2_ioctl_querybuf,
  764. .vidioc_qbuf = vb2_ioctl_qbuf,
  765. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  766. .vidioc_expbuf = vb2_ioctl_expbuf,
  767. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  768. .vidioc_streamon = vb2_ioctl_streamon,
  769. .vidioc_streamoff = vb2_ioctl_streamoff,
  770. .vidioc_log_status = v4l2_ctrl_log_status,
  771. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  772. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  773. };
  774. static const struct v4l2_file_operations dcmi_fops = {
  775. .owner = THIS_MODULE,
  776. .unlocked_ioctl = video_ioctl2,
  777. .open = dcmi_open,
  778. .release = dcmi_release,
  779. .poll = vb2_fop_poll,
  780. .mmap = vb2_fop_mmap,
  781. #ifndef CONFIG_MMU
  782. .get_unmapped_area = vb2_fop_get_unmapped_area,
  783. #endif
  784. .read = vb2_fop_read,
  785. };
  786. static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi)
  787. {
  788. struct v4l2_format f = {
  789. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  790. .fmt.pix = {
  791. .width = CIF_WIDTH,
  792. .height = CIF_HEIGHT,
  793. .field = V4L2_FIELD_NONE,
  794. .pixelformat = dcmi->user_formats[0]->fourcc,
  795. },
  796. };
  797. int ret;
  798. ret = dcmi_try_fmt(dcmi, &f, NULL);
  799. if (ret)
  800. return ret;
  801. dcmi->current_fmt = dcmi->user_formats[0];
  802. dcmi->fmt = f;
  803. return 0;
  804. }
  805. static const struct dcmi_format dcmi_formats[] = {
  806. {
  807. .fourcc = V4L2_PIX_FMT_RGB565,
  808. .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  809. .bpp = 2,
  810. }, {
  811. .fourcc = V4L2_PIX_FMT_YUYV,
  812. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  813. .bpp = 2,
  814. }, {
  815. .fourcc = V4L2_PIX_FMT_UYVY,
  816. .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
  817. .bpp = 2,
  818. },
  819. };
  820. static int dcmi_formats_init(struct stm32_dcmi *dcmi)
  821. {
  822. const struct dcmi_format *dcmi_fmts[ARRAY_SIZE(dcmi_formats)];
  823. unsigned int num_fmts = 0, i, j;
  824. struct v4l2_subdev *subdev = dcmi->entity.subdev;
  825. struct v4l2_subdev_mbus_code_enum mbus_code = {
  826. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  827. };
  828. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
  829. NULL, &mbus_code)) {
  830. for (i = 0; i < ARRAY_SIZE(dcmi_formats); i++) {
  831. if (dcmi_formats[i].mbus_code != mbus_code.code)
  832. continue;
  833. /* Code supported, have we got this fourcc yet? */
  834. for (j = 0; j < num_fmts; j++)
  835. if (dcmi_fmts[j]->fourcc ==
  836. dcmi_formats[i].fourcc)
  837. /* Already available */
  838. break;
  839. if (j == num_fmts)
  840. /* New */
  841. dcmi_fmts[num_fmts++] = dcmi_formats + i;
  842. }
  843. mbus_code.index++;
  844. }
  845. if (!num_fmts)
  846. return -ENXIO;
  847. dcmi->num_user_formats = num_fmts;
  848. dcmi->user_formats = devm_kcalloc(dcmi->dev,
  849. num_fmts, sizeof(struct dcmi_format *),
  850. GFP_KERNEL);
  851. if (!dcmi->user_formats) {
  852. dev_err(dcmi->dev, "could not allocate memory\n");
  853. return -ENOMEM;
  854. }
  855. memcpy(dcmi->user_formats, dcmi_fmts,
  856. num_fmts * sizeof(struct dcmi_format *));
  857. dcmi->current_fmt = dcmi->user_formats[0];
  858. return 0;
  859. }
  860. static int dcmi_graph_notify_complete(struct v4l2_async_notifier *notifier)
  861. {
  862. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  863. int ret;
  864. dcmi->vdev->ctrl_handler = dcmi->entity.subdev->ctrl_handler;
  865. ret = dcmi_formats_init(dcmi);
  866. if (ret) {
  867. dev_err(dcmi->dev, "No supported mediabus format found\n");
  868. return ret;
  869. }
  870. ret = dcmi_set_default_fmt(dcmi);
  871. if (ret) {
  872. dev_err(dcmi->dev, "Could not set default format\n");
  873. return ret;
  874. }
  875. ret = video_register_device(dcmi->vdev, VFL_TYPE_GRABBER, -1);
  876. if (ret) {
  877. dev_err(dcmi->dev, "Failed to register video device\n");
  878. return ret;
  879. }
  880. dev_dbg(dcmi->dev, "Device registered as %s\n",
  881. video_device_node_name(dcmi->vdev));
  882. return 0;
  883. }
  884. static void dcmi_graph_notify_unbind(struct v4l2_async_notifier *notifier,
  885. struct v4l2_subdev *sd,
  886. struct v4l2_async_subdev *asd)
  887. {
  888. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  889. dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev));
  890. /* Checks internaly if vdev has been init or not */
  891. video_unregister_device(dcmi->vdev);
  892. }
  893. static int dcmi_graph_notify_bound(struct v4l2_async_notifier *notifier,
  894. struct v4l2_subdev *subdev,
  895. struct v4l2_async_subdev *asd)
  896. {
  897. struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
  898. dev_dbg(dcmi->dev, "Subdev %s bound\n", subdev->name);
  899. dcmi->entity.subdev = subdev;
  900. return 0;
  901. }
  902. static int dcmi_graph_parse(struct stm32_dcmi *dcmi, struct device_node *node)
  903. {
  904. struct device_node *ep = NULL;
  905. struct device_node *remote;
  906. while (1) {
  907. ep = of_graph_get_next_endpoint(node, ep);
  908. if (!ep)
  909. return -EINVAL;
  910. remote = of_graph_get_remote_port_parent(ep);
  911. if (!remote) {
  912. of_node_put(ep);
  913. return -EINVAL;
  914. }
  915. /* Remote node to connect */
  916. dcmi->entity.node = remote;
  917. dcmi->entity.asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
  918. dcmi->entity.asd.match.fwnode.fwnode = of_fwnode_handle(remote);
  919. return 0;
  920. }
  921. }
  922. static int dcmi_graph_init(struct stm32_dcmi *dcmi)
  923. {
  924. struct v4l2_async_subdev **subdevs = NULL;
  925. int ret;
  926. /* Parse the graph to extract a list of subdevice DT nodes. */
  927. ret = dcmi_graph_parse(dcmi, dcmi->dev->of_node);
  928. if (ret < 0) {
  929. dev_err(dcmi->dev, "Graph parsing failed\n");
  930. return ret;
  931. }
  932. /* Register the subdevices notifier. */
  933. subdevs = devm_kzalloc(dcmi->dev, sizeof(*subdevs), GFP_KERNEL);
  934. if (!subdevs) {
  935. of_node_put(dcmi->entity.node);
  936. return -ENOMEM;
  937. }
  938. subdevs[0] = &dcmi->entity.asd;
  939. dcmi->notifier.subdevs = subdevs;
  940. dcmi->notifier.num_subdevs = 1;
  941. dcmi->notifier.bound = dcmi_graph_notify_bound;
  942. dcmi->notifier.unbind = dcmi_graph_notify_unbind;
  943. dcmi->notifier.complete = dcmi_graph_notify_complete;
  944. ret = v4l2_async_notifier_register(&dcmi->v4l2_dev, &dcmi->notifier);
  945. if (ret < 0) {
  946. dev_err(dcmi->dev, "Notifier registration failed\n");
  947. of_node_put(dcmi->entity.node);
  948. return ret;
  949. }
  950. return 0;
  951. }
  952. static int dcmi_probe(struct platform_device *pdev)
  953. {
  954. struct device_node *np = pdev->dev.of_node;
  955. const struct of_device_id *match = NULL;
  956. struct v4l2_fwnode_endpoint ep;
  957. struct stm32_dcmi *dcmi;
  958. struct vb2_queue *q;
  959. struct dma_chan *chan;
  960. struct clk *mclk;
  961. int irq;
  962. int ret = 0;
  963. match = of_match_device(of_match_ptr(stm32_dcmi_of_match), &pdev->dev);
  964. if (!match) {
  965. dev_err(&pdev->dev, "Could not find a match in devicetree\n");
  966. return -ENODEV;
  967. }
  968. dcmi = devm_kzalloc(&pdev->dev, sizeof(struct stm32_dcmi), GFP_KERNEL);
  969. if (!dcmi)
  970. return -ENOMEM;
  971. dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  972. if (IS_ERR(dcmi->rstc)) {
  973. dev_err(&pdev->dev, "Could not get reset control\n");
  974. return -ENODEV;
  975. }
  976. /* Get bus characteristics from devicetree */
  977. np = of_graph_get_next_endpoint(np, NULL);
  978. if (!np) {
  979. dev_err(&pdev->dev, "Could not find the endpoint\n");
  980. of_node_put(np);
  981. return -ENODEV;
  982. }
  983. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
  984. if (ret) {
  985. dev_err(&pdev->dev, "Could not parse the endpoint\n");
  986. of_node_put(np);
  987. return -ENODEV;
  988. }
  989. if (ep.bus_type == V4L2_MBUS_CSI2) {
  990. dev_err(&pdev->dev, "CSI bus not supported\n");
  991. of_node_put(np);
  992. return -ENODEV;
  993. }
  994. dcmi->bus.flags = ep.bus.parallel.flags;
  995. dcmi->bus.bus_width = ep.bus.parallel.bus_width;
  996. dcmi->bus.data_shift = ep.bus.parallel.data_shift;
  997. of_node_put(np);
  998. irq = platform_get_irq(pdev, 0);
  999. if (irq <= 0) {
  1000. dev_err(&pdev->dev, "Could not get irq\n");
  1001. return -ENODEV;
  1002. }
  1003. dcmi->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1004. if (!dcmi->res) {
  1005. dev_err(&pdev->dev, "Could not get resource\n");
  1006. return -ENODEV;
  1007. }
  1008. dcmi->regs = devm_ioremap_resource(&pdev->dev, dcmi->res);
  1009. if (IS_ERR(dcmi->regs)) {
  1010. dev_err(&pdev->dev, "Could not map registers\n");
  1011. return PTR_ERR(dcmi->regs);
  1012. }
  1013. ret = devm_request_threaded_irq(&pdev->dev, irq, dcmi_irq_callback,
  1014. dcmi_irq_thread, IRQF_ONESHOT,
  1015. dev_name(&pdev->dev), dcmi);
  1016. if (ret) {
  1017. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  1018. return -ENODEV;
  1019. }
  1020. mclk = devm_clk_get(&pdev->dev, "mclk");
  1021. if (IS_ERR(mclk)) {
  1022. dev_err(&pdev->dev, "Unable to get mclk\n");
  1023. return PTR_ERR(mclk);
  1024. }
  1025. chan = dma_request_slave_channel(&pdev->dev, "tx");
  1026. if (!chan) {
  1027. dev_info(&pdev->dev, "Unable to request DMA channel, defer probing\n");
  1028. return -EPROBE_DEFER;
  1029. }
  1030. ret = clk_prepare(mclk);
  1031. if (ret) {
  1032. dev_err(&pdev->dev, "Unable to prepare mclk %p\n", mclk);
  1033. goto err_dma_release;
  1034. }
  1035. spin_lock_init(&dcmi->irqlock);
  1036. mutex_init(&dcmi->lock);
  1037. init_completion(&dcmi->complete);
  1038. INIT_LIST_HEAD(&dcmi->buffers);
  1039. dcmi->dev = &pdev->dev;
  1040. dcmi->mclk = mclk;
  1041. dcmi->state = STOPPED;
  1042. dcmi->dma_chan = chan;
  1043. q = &dcmi->queue;
  1044. /* Initialize the top-level structure */
  1045. ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev);
  1046. if (ret)
  1047. goto err_clk_unprepare;
  1048. dcmi->vdev = video_device_alloc();
  1049. if (!dcmi->vdev) {
  1050. ret = -ENOMEM;
  1051. goto err_device_unregister;
  1052. }
  1053. /* Video node */
  1054. dcmi->vdev->fops = &dcmi_fops;
  1055. dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev;
  1056. dcmi->vdev->queue = &dcmi->queue;
  1057. strlcpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name));
  1058. dcmi->vdev->release = video_device_release;
  1059. dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops;
  1060. dcmi->vdev->lock = &dcmi->lock;
  1061. dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  1062. V4L2_CAP_READWRITE;
  1063. video_set_drvdata(dcmi->vdev, dcmi);
  1064. /* Buffer queue */
  1065. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1066. q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
  1067. q->lock = &dcmi->lock;
  1068. q->drv_priv = dcmi;
  1069. q->buf_struct_size = sizeof(struct dcmi_buf);
  1070. q->ops = &dcmi_video_qops;
  1071. q->mem_ops = &vb2_dma_contig_memops;
  1072. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1073. q->min_buffers_needed = 2;
  1074. q->dev = &pdev->dev;
  1075. ret = vb2_queue_init(q);
  1076. if (ret < 0) {
  1077. dev_err(&pdev->dev, "Failed to initialize vb2 queue\n");
  1078. goto err_device_release;
  1079. }
  1080. ret = dcmi_graph_init(dcmi);
  1081. if (ret < 0)
  1082. goto err_device_release;
  1083. /* Reset device */
  1084. ret = reset_control_assert(dcmi->rstc);
  1085. if (ret) {
  1086. dev_err(&pdev->dev, "Failed to assert the reset line\n");
  1087. goto err_device_release;
  1088. }
  1089. usleep_range(3000, 5000);
  1090. ret = reset_control_deassert(dcmi->rstc);
  1091. if (ret) {
  1092. dev_err(&pdev->dev, "Failed to deassert the reset line\n");
  1093. goto err_device_release;
  1094. }
  1095. dev_info(&pdev->dev, "Probe done\n");
  1096. platform_set_drvdata(pdev, dcmi);
  1097. return 0;
  1098. err_device_release:
  1099. video_device_release(dcmi->vdev);
  1100. err_device_unregister:
  1101. v4l2_device_unregister(&dcmi->v4l2_dev);
  1102. err_clk_unprepare:
  1103. clk_unprepare(dcmi->mclk);
  1104. err_dma_release:
  1105. dma_release_channel(dcmi->dma_chan);
  1106. return ret;
  1107. }
  1108. static int dcmi_remove(struct platform_device *pdev)
  1109. {
  1110. struct stm32_dcmi *dcmi = platform_get_drvdata(pdev);
  1111. v4l2_async_notifier_unregister(&dcmi->notifier);
  1112. v4l2_device_unregister(&dcmi->v4l2_dev);
  1113. clk_unprepare(dcmi->mclk);
  1114. dma_release_channel(dcmi->dma_chan);
  1115. return 0;
  1116. }
  1117. static struct platform_driver stm32_dcmi_driver = {
  1118. .probe = dcmi_probe,
  1119. .remove = dcmi_remove,
  1120. .driver = {
  1121. .name = DRV_NAME,
  1122. .of_match_table = of_match_ptr(stm32_dcmi_of_match),
  1123. },
  1124. };
  1125. module_platform_driver(stm32_dcmi_driver);
  1126. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  1127. MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
  1128. MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");
  1129. MODULE_LICENSE("GPL");
  1130. MODULE_SUPPORTED_DEVICE("video");