stm32-dcmi.c 43 KB

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