stm32-dcmi.c 43 KB

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