stm32-dcmi.c 42 KB

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