1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for STM32 Digital Camera Memory Interface
5 * Copyright (C) STMicroelectronics SA 2017
6 * Authors: Yannick Fertre <yannick.fertre@st.com>
7 * Hugues Fruchet <hugues.fruchet@st.com>
8 * for STMicroelectronics.
10 * This driver is based on atmel_isi.c
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/dmaengine.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/of_graph.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/reset.h>
29 #include <linux/videodev2.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-dev.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-event.h>
35 #include <media/v4l2-fwnode.h>
36 #include <media/v4l2-image-sizes.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-rect.h>
39 #include <media/videobuf2-dma-contig.h>
41 #define DRV_NAME "stm32-dcmi"
43 /* Registers offset for DCMI */
44 #define DCMI_CR 0x00 /* Control Register */
45 #define DCMI_SR 0x04 /* Status Register */
46 #define DCMI_RIS 0x08 /* Raw Interrupt Status register */
47 #define DCMI_IER 0x0C /* Interrupt Enable Register */
48 #define DCMI_MIS 0x10 /* Masked Interrupt Status register */
49 #define DCMI_ICR 0x14 /* Interrupt Clear Register */
50 #define DCMI_ESCR 0x18 /* Embedded Synchronization Code Register */
51 #define DCMI_ESUR 0x1C /* Embedded Synchronization Unmask Register */
52 #define DCMI_CWSTRT 0x20 /* Crop Window STaRT */
53 #define DCMI_CWSIZE 0x24 /* Crop Window SIZE */
54 #define DCMI_DR 0x28 /* Data Register */
55 #define DCMI_IDR 0x2C /* IDentifier Register */
57 /* Bits definition for control register (DCMI_CR) */
58 #define CR_CAPTURE BIT(0)
60 #define CR_CROP BIT(2)
61 #define CR_JPEG BIT(3)
63 #define CR_PCKPOL BIT(5)
64 #define CR_HSPOL BIT(6)
65 #define CR_VSPOL BIT(7)
66 #define CR_FCRC_0 BIT(8)
67 #define CR_FCRC_1 BIT(9)
68 #define CR_EDM_0 BIT(10)
69 #define CR_EDM_1 BIT(11)
70 #define CR_ENABLE BIT(14)
72 /* Bits definition for status register (DCMI_SR) */
73 #define SR_HSYNC BIT(0)
74 #define SR_VSYNC BIT(1)
78 * Bits definition for interrupt registers
79 * (DCMI_RIS, DCMI_IER, DCMI_MIS, DCMI_ICR)
81 #define IT_FRAME BIT(0)
84 #define IT_VSYNC BIT(3)
85 #define IT_LINE BIT(4)
94 #define MAX_WIDTH 2592U
95 #define MIN_HEIGHT 16U
96 #define MAX_HEIGHT 2592U
98 #define TIMEOUT_MS 1000
100 #define OVERRUN_ERROR_THRESHOLD 3
102 struct dcmi_graph_entity
{
103 struct v4l2_async_subdev asd
;
105 struct device_node
*remote_node
;
106 struct v4l2_subdev
*source
;
115 struct dcmi_framesize
{
121 struct vb2_v4l2_buffer vb
;
125 struct list_head list
;
129 /* Protects the access of variables shared within the interrupt */
133 struct resource
*res
;
134 struct reset_control
*rstc
;
136 struct list_head buffers
;
137 struct dcmi_buf
*active
;
139 struct v4l2_device v4l2_dev
;
140 struct video_device
*vdev
;
141 struct v4l2_async_notifier notifier
;
142 struct dcmi_graph_entity entity
;
143 struct v4l2_format fmt
;
144 struct v4l2_rect crop
;
147 const struct dcmi_format
**sd_formats
;
148 unsigned int num_of_sd_formats
;
149 const struct dcmi_format
*sd_format
;
150 struct dcmi_framesize
*sd_framesizes
;
151 unsigned int num_of_sd_framesizes
;
152 struct dcmi_framesize sd_framesize
;
153 struct v4l2_rect sd_bounds
;
155 /* Protect this data structure */
157 struct vb2_queue queue
;
159 struct v4l2_fwnode_bus_parallel bus
;
160 enum v4l2_mbus_type bus_type
;
161 struct completion complete
;
164 struct dma_chan
*dma_chan
;
165 dma_cookie_t dma_cookie
;
171 /* Ensure DMA operations atomicity */
172 struct mutex dma_lock
;
174 struct media_device mdev
;
175 struct media_pad vid_cap_pad
;
176 struct media_pipeline pipeline
;
179 static inline struct stm32_dcmi
*notifier_to_dcmi(struct v4l2_async_notifier
*n
)
181 return container_of(n
, struct stm32_dcmi
, notifier
);
184 static inline u32
reg_read(void __iomem
*base
, u32 reg
)
186 return readl_relaxed(base
+ reg
);
189 static inline void reg_write(void __iomem
*base
, u32 reg
, u32 val
)
191 writel_relaxed(val
, base
+ reg
);
194 static inline void reg_set(void __iomem
*base
, u32 reg
, u32 mask
)
196 reg_write(base
, reg
, reg_read(base
, reg
) | mask
);
199 static inline void reg_clear(void __iomem
*base
, u32 reg
, u32 mask
)
201 reg_write(base
, reg
, reg_read(base
, reg
) & ~mask
);
204 static int dcmi_start_capture(struct stm32_dcmi
*dcmi
, struct dcmi_buf
*buf
);
206 static void dcmi_buffer_done(struct stm32_dcmi
*dcmi
,
207 struct dcmi_buf
*buf
,
211 struct vb2_v4l2_buffer
*vbuf
;
216 list_del_init(&buf
->list
);
220 vbuf
->sequence
= dcmi
->sequence
++;
221 vbuf
->field
= V4L2_FIELD_NONE
;
222 vbuf
->vb2_buf
.timestamp
= ktime_get_ns();
223 vb2_set_plane_payload(&vbuf
->vb2_buf
, 0, bytesused
);
224 vb2_buffer_done(&vbuf
->vb2_buf
,
225 err
? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
226 dev_dbg(dcmi
->dev
, "buffer[%d] done seq=%d, bytesused=%zu\n",
227 vbuf
->vb2_buf
.index
, vbuf
->sequence
, bytesused
);
229 dcmi
->buffers_count
++;
233 static int dcmi_restart_capture(struct stm32_dcmi
*dcmi
)
235 struct dcmi_buf
*buf
;
237 spin_lock_irq(&dcmi
->irqlock
);
239 if (dcmi
->state
!= RUNNING
) {
240 spin_unlock_irq(&dcmi
->irqlock
);
244 /* Restart a new DMA transfer with next buffer */
245 if (list_empty(&dcmi
->buffers
)) {
246 dev_dbg(dcmi
->dev
, "Capture restart is deferred to next buffer queueing\n");
247 dcmi
->state
= WAIT_FOR_BUFFER
;
248 spin_unlock_irq(&dcmi
->irqlock
);
251 buf
= list_entry(dcmi
->buffers
.next
, struct dcmi_buf
, list
);
254 spin_unlock_irq(&dcmi
->irqlock
);
256 return dcmi_start_capture(dcmi
, buf
);
259 static void dcmi_dma_callback(void *param
)
261 struct stm32_dcmi
*dcmi
= (struct stm32_dcmi
*)param
;
262 struct dma_tx_state state
;
263 enum dma_status status
;
264 struct dcmi_buf
*buf
= dcmi
->active
;
266 spin_lock_irq(&dcmi
->irqlock
);
268 /* Check DMA status */
269 status
= dmaengine_tx_status(dcmi
->dma_chan
, dcmi
->dma_cookie
, &state
);
272 case DMA_IN_PROGRESS
:
273 dev_dbg(dcmi
->dev
, "%s: Received DMA_IN_PROGRESS\n", __func__
);
276 dev_err(dcmi
->dev
, "%s: Received DMA_PAUSED\n", __func__
);
279 dev_err(dcmi
->dev
, "%s: Received DMA_ERROR\n", __func__
);
281 /* Return buffer to V4L2 in error state */
282 dcmi_buffer_done(dcmi
, buf
, 0, -EIO
);
285 dev_dbg(dcmi
->dev
, "%s: Received DMA_COMPLETE\n", __func__
);
287 /* Return buffer to V4L2 */
288 dcmi_buffer_done(dcmi
, buf
, buf
->size
, 0);
290 spin_unlock_irq(&dcmi
->irqlock
);
292 /* Restart capture */
293 if (dcmi_restart_capture(dcmi
))
294 dev_err(dcmi
->dev
, "%s: Cannot restart capture on DMA complete\n",
298 dev_err(dcmi
->dev
, "%s: Received unknown status\n", __func__
);
302 spin_unlock_irq(&dcmi
->irqlock
);
305 static int dcmi_start_dma(struct stm32_dcmi
*dcmi
,
306 struct dcmi_buf
*buf
)
308 struct dma_async_tx_descriptor
*desc
= NULL
;
309 struct dma_slave_config config
;
312 memset(&config
, 0, sizeof(config
));
314 config
.src_addr
= (dma_addr_t
)dcmi
->res
->start
+ DCMI_DR
;
315 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
316 config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
317 config
.dst_maxburst
= 4;
319 /* Configure DMA channel */
320 ret
= dmaengine_slave_config(dcmi
->dma_chan
, &config
);
322 dev_err(dcmi
->dev
, "%s: DMA channel config failed (%d)\n",
328 * Avoid call of dmaengine_terminate_sync() between
329 * dmaengine_prep_slave_single() and dmaengine_submit()
330 * by locking the whole DMA submission sequence
332 mutex_lock(&dcmi
->dma_lock
);
334 /* Prepare a DMA transaction */
335 desc
= dmaengine_prep_slave_single(dcmi
->dma_chan
, buf
->paddr
,
340 dev_err(dcmi
->dev
, "%s: DMA dmaengine_prep_slave_single failed for buffer phy=%pad size=%zu\n",
341 __func__
, &buf
->paddr
, buf
->size
);
342 mutex_unlock(&dcmi
->dma_lock
);
346 /* Set completion callback routine for notification */
347 desc
->callback
= dcmi_dma_callback
;
348 desc
->callback_param
= dcmi
;
350 /* Push current DMA transaction in the pending queue */
351 dcmi
->dma_cookie
= dmaengine_submit(desc
);
352 if (dma_submit_error(dcmi
->dma_cookie
)) {
353 dev_err(dcmi
->dev
, "%s: DMA submission failed\n", __func__
);
354 mutex_unlock(&dcmi
->dma_lock
);
358 mutex_unlock(&dcmi
->dma_lock
);
360 dma_async_issue_pending(dcmi
->dma_chan
);
365 static int dcmi_start_capture(struct stm32_dcmi
*dcmi
, struct dcmi_buf
*buf
)
372 ret
= dcmi_start_dma(dcmi
, buf
);
374 dcmi
->errors_count
++;
379 reg_set(dcmi
->regs
, DCMI_CR
, CR_CAPTURE
);
384 static void dcmi_set_crop(struct stm32_dcmi
*dcmi
)
388 /* Crop resolution */
389 size
= ((dcmi
->crop
.height
- 1) << 16) |
390 ((dcmi
->crop
.width
<< 1) - 1);
391 reg_write(dcmi
->regs
, DCMI_CWSIZE
, size
);
393 /* Crop start point */
394 start
= ((dcmi
->crop
.top
) << 16) |
395 ((dcmi
->crop
.left
<< 1));
396 reg_write(dcmi
->regs
, DCMI_CWSTRT
, start
);
398 dev_dbg(dcmi
->dev
, "Cropping to %ux%u@%u:%u\n",
399 dcmi
->crop
.width
, dcmi
->crop
.height
,
400 dcmi
->crop
.left
, dcmi
->crop
.top
);
403 reg_set(dcmi
->regs
, DCMI_CR
, CR_CROP
);
406 static void dcmi_process_jpeg(struct stm32_dcmi
*dcmi
)
408 struct dma_tx_state state
;
409 enum dma_status status
;
410 struct dcmi_buf
*buf
= dcmi
->active
;
416 * Because of variable JPEG buffer size sent by sensor,
417 * DMA transfer never completes due to transfer size never reached.
418 * In order to ensure that all the JPEG data are transferred
419 * in active buffer memory, DMA is drained.
420 * Then DMA tx status gives the amount of data transferred
421 * to memory, which is then returned to V4L2 through the active
426 dmaengine_synchronize(dcmi
->dma_chan
);
428 /* Get DMA residue to get JPEG size */
429 status
= dmaengine_tx_status(dcmi
->dma_chan
, dcmi
->dma_cookie
, &state
);
430 if (status
!= DMA_ERROR
&& state
.residue
< buf
->size
) {
431 /* Return JPEG buffer to V4L2 with received JPEG buffer size */
432 dcmi_buffer_done(dcmi
, buf
, buf
->size
- state
.residue
, 0);
434 dcmi
->errors_count
++;
435 dev_err(dcmi
->dev
, "%s: Cannot get JPEG size from DMA\n",
437 /* Return JPEG buffer to V4L2 in ERROR state */
438 dcmi_buffer_done(dcmi
, buf
, 0, -EIO
);
441 /* Abort DMA operation */
442 dmaengine_terminate_sync(dcmi
->dma_chan
);
444 /* Restart capture */
445 if (dcmi_restart_capture(dcmi
))
446 dev_err(dcmi
->dev
, "%s: Cannot restart capture on JPEG received\n",
450 static irqreturn_t
dcmi_irq_thread(int irq
, void *arg
)
452 struct stm32_dcmi
*dcmi
= arg
;
454 spin_lock_irq(&dcmi
->irqlock
);
456 if (dcmi
->misr
& IT_OVR
) {
457 dcmi
->overrun_count
++;
458 if (dcmi
->overrun_count
> OVERRUN_ERROR_THRESHOLD
)
459 dcmi
->errors_count
++;
461 if (dcmi
->misr
& IT_ERR
)
462 dcmi
->errors_count
++;
464 if (dcmi
->sd_format
->fourcc
== V4L2_PIX_FMT_JPEG
&&
465 dcmi
->misr
& IT_FRAME
) {
467 spin_unlock_irq(&dcmi
->irqlock
);
468 dcmi_process_jpeg(dcmi
);
472 spin_unlock_irq(&dcmi
->irqlock
);
476 static irqreturn_t
dcmi_irq_callback(int irq
, void *arg
)
478 struct stm32_dcmi
*dcmi
= arg
;
481 spin_lock_irqsave(&dcmi
->irqlock
, flags
);
483 dcmi
->misr
= reg_read(dcmi
->regs
, DCMI_MIS
);
485 /* Clear interrupt */
486 reg_set(dcmi
->regs
, DCMI_ICR
, IT_FRAME
| IT_OVR
| IT_ERR
);
488 spin_unlock_irqrestore(&dcmi
->irqlock
, flags
);
490 return IRQ_WAKE_THREAD
;
493 static int dcmi_queue_setup(struct vb2_queue
*vq
,
494 unsigned int *nbuffers
,
495 unsigned int *nplanes
,
496 unsigned int sizes
[],
497 struct device
*alloc_devs
[])
499 struct stm32_dcmi
*dcmi
= vb2_get_drv_priv(vq
);
502 size
= dcmi
->fmt
.fmt
.pix
.sizeimage
;
504 /* Make sure the image size is large enough */
506 return sizes
[0] < size
? -EINVAL
: 0;
511 dev_dbg(dcmi
->dev
, "Setup queue, count=%d, size=%d\n",
517 static int dcmi_buf_init(struct vb2_buffer
*vb
)
519 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
520 struct dcmi_buf
*buf
= container_of(vbuf
, struct dcmi_buf
, vb
);
522 INIT_LIST_HEAD(&buf
->list
);
527 static int dcmi_buf_prepare(struct vb2_buffer
*vb
)
529 struct stm32_dcmi
*dcmi
= vb2_get_drv_priv(vb
->vb2_queue
);
530 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
531 struct dcmi_buf
*buf
= container_of(vbuf
, struct dcmi_buf
, vb
);
534 size
= dcmi
->fmt
.fmt
.pix
.sizeimage
;
536 if (vb2_plane_size(vb
, 0) < size
) {
537 dev_err(dcmi
->dev
, "%s data will not fit into plane (%lu < %lu)\n",
538 __func__
, vb2_plane_size(vb
, 0), size
);
542 vb2_set_plane_payload(vb
, 0, size
);
544 if (!buf
->prepared
) {
545 /* Get memory addresses */
547 vb2_dma_contig_plane_dma_addr(&buf
->vb
.vb2_buf
, 0);
548 buf
->size
= vb2_plane_size(&buf
->vb
.vb2_buf
, 0);
549 buf
->prepared
= true;
551 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, buf
->size
);
553 dev_dbg(dcmi
->dev
, "buffer[%d] phy=%pad size=%zu\n",
554 vb
->index
, &buf
->paddr
, buf
->size
);
560 static void dcmi_buf_queue(struct vb2_buffer
*vb
)
562 struct stm32_dcmi
*dcmi
= vb2_get_drv_priv(vb
->vb2_queue
);
563 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
564 struct dcmi_buf
*buf
= container_of(vbuf
, struct dcmi_buf
, vb
);
566 spin_lock_irq(&dcmi
->irqlock
);
568 /* Enqueue to video buffers list */
569 list_add_tail(&buf
->list
, &dcmi
->buffers
);
571 if (dcmi
->state
== WAIT_FOR_BUFFER
) {
572 dcmi
->state
= RUNNING
;
575 dev_dbg(dcmi
->dev
, "Starting capture on buffer[%d] queued\n",
576 buf
->vb
.vb2_buf
.index
);
578 spin_unlock_irq(&dcmi
->irqlock
);
579 if (dcmi_start_capture(dcmi
, buf
))
580 dev_err(dcmi
->dev
, "%s: Cannot restart capture on overflow or error\n",
585 spin_unlock_irq(&dcmi
->irqlock
);
588 static struct media_entity
*dcmi_find_source(struct stm32_dcmi
*dcmi
)
590 struct media_entity
*entity
= &dcmi
->vdev
->entity
;
591 struct media_pad
*pad
;
593 /* Walk searching for entity having no sink */
595 pad
= &entity
->pads
[0];
596 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
599 pad
= media_entity_remote_pad(pad
);
600 if (!pad
|| !is_media_entity_v4l2_subdev(pad
->entity
))
603 entity
= pad
->entity
;
609 static int dcmi_pipeline_s_fmt(struct stm32_dcmi
*dcmi
,
610 struct v4l2_subdev_pad_config
*pad_cfg
,
611 struct v4l2_subdev_format
*format
)
613 struct media_entity
*entity
= &dcmi
->entity
.source
->entity
;
614 struct v4l2_subdev
*subdev
;
615 struct media_pad
*sink_pad
= NULL
;
616 struct media_pad
*src_pad
= NULL
;
617 struct media_pad
*pad
= NULL
;
618 struct v4l2_subdev_format fmt
= *format
;
623 * Starting from sensor subdevice, walk within
624 * pipeline and set format on each subdevice
629 /* Search if current entity has a source pad */
630 for (i
= 0; i
< entity
->num_pads
; i
++) {
631 pad
= &entity
->pads
[i
];
632 if (pad
->flags
& MEDIA_PAD_FL_SOURCE
) {
641 subdev
= media_entity_to_v4l2_subdev(entity
);
643 /* Propagate format on sink pad if any, otherwise source pad */
647 dev_dbg(dcmi
->dev
, "\"%s\":%d pad format set to 0x%x %ux%u\n",
648 subdev
->name
, pad
->index
, format
->format
.code
,
649 format
->format
.width
, format
->format
.height
);
651 fmt
.pad
= pad
->index
;
652 ret
= v4l2_subdev_call(subdev
, pad
, set_fmt
, pad_cfg
, &fmt
);
654 dev_err(dcmi
->dev
, "%s: Failed to set format 0x%x %ux%u on \"%s\":%d pad (%d)\n",
655 __func__
, format
->format
.code
,
656 format
->format
.width
, format
->format
.height
,
657 subdev
->name
, pad
->index
, ret
);
661 if (fmt
.format
.code
!= format
->format
.code
||
662 fmt
.format
.width
!= format
->format
.width
||
663 fmt
.format
.height
!= format
->format
.height
) {
664 dev_dbg(dcmi
->dev
, "\"%s\":%d pad format has been changed to 0x%x %ux%u\n",
665 subdev
->name
, pad
->index
, fmt
.format
.code
,
666 fmt
.format
.width
, fmt
.format
.height
);
669 /* Walk to next entity */
670 sink_pad
= media_entity_remote_pad(src_pad
);
671 if (!sink_pad
|| !is_media_entity_v4l2_subdev(sink_pad
->entity
))
674 entity
= sink_pad
->entity
;
681 static int dcmi_pipeline_s_stream(struct stm32_dcmi
*dcmi
, int state
)
683 struct media_entity
*entity
= &dcmi
->vdev
->entity
;
684 struct v4l2_subdev
*subdev
;
685 struct media_pad
*pad
;
688 /* Start/stop all entities within pipeline */
690 pad
= &entity
->pads
[0];
691 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
694 pad
= media_entity_remote_pad(pad
);
695 if (!pad
|| !is_media_entity_v4l2_subdev(pad
->entity
))
698 entity
= pad
->entity
;
699 subdev
= media_entity_to_v4l2_subdev(entity
);
701 ret
= v4l2_subdev_call(subdev
, video
, s_stream
, state
);
702 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
703 dev_err(dcmi
->dev
, "%s: \"%s\" failed to %s streaming (%d)\n",
704 __func__
, subdev
->name
,
705 state
? "start" : "stop", ret
);
709 dev_dbg(dcmi
->dev
, "\"%s\" is %s\n",
710 subdev
->name
, state
? "started" : "stopped");
716 static int dcmi_pipeline_start(struct stm32_dcmi
*dcmi
)
718 return dcmi_pipeline_s_stream(dcmi
, 1);
721 static void dcmi_pipeline_stop(struct stm32_dcmi
*dcmi
)
723 dcmi_pipeline_s_stream(dcmi
, 0);
726 static int dcmi_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
728 struct stm32_dcmi
*dcmi
= vb2_get_drv_priv(vq
);
729 struct dcmi_buf
*buf
, *node
;
733 ret
= pm_runtime_get_sync(dcmi
->dev
);
735 dev_err(dcmi
->dev
, "%s: Failed to start streaming, cannot get sync (%d)\n",
740 ret
= media_pipeline_start(&dcmi
->vdev
->entity
, &dcmi
->pipeline
);
742 dev_err(dcmi
->dev
, "%s: Failed to start streaming, media pipeline start error (%d)\n",
747 ret
= dcmi_pipeline_start(dcmi
);
749 goto err_media_pipeline_stop
;
751 spin_lock_irq(&dcmi
->irqlock
);
754 switch (dcmi
->bus
.bus_width
) {
756 val
|= CR_EDM_0
| CR_EDM_1
;
765 /* Set bus width to 8 bits by default */
769 /* Set vertical synchronization polarity */
770 if (dcmi
->bus
.flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
)
773 /* Set horizontal synchronization polarity */
774 if (dcmi
->bus
.flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
)
777 /* Set pixel clock polarity */
778 if (dcmi
->bus
.flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
)
782 * BT656 embedded synchronisation bus mode.
784 * Default SAV/EAV mode is supported here with default codes
785 * SAV=0xff000080 & EAV=0xff00009d.
786 * With DCMI this means LSC=SAV=0x80 & LEC=EAV=0x9d.
788 if (dcmi
->bus_type
== V4L2_MBUS_BT656
) {
791 /* Unmask all codes */
792 reg_write(dcmi
->regs
, DCMI_ESUR
, 0xffffffff);/* FEC:LEC:LSC:FSC */
794 /* Trig on LSC=0x80 & LEC=0x9d codes, ignore FSC and FEC */
795 reg_write(dcmi
->regs
, DCMI_ESCR
, 0xff9d80ff);/* FEC:LEC:LSC:FSC */
798 reg_write(dcmi
->regs
, DCMI_CR
, val
);
804 /* Enable jpeg capture */
805 if (dcmi
->sd_format
->fourcc
== V4L2_PIX_FMT_JPEG
)
806 reg_set(dcmi
->regs
, DCMI_CR
, CR_CM
);/* Snapshot mode */
809 reg_set(dcmi
->regs
, DCMI_CR
, CR_ENABLE
);
812 dcmi
->errors_count
= 0;
813 dcmi
->overrun_count
= 0;
814 dcmi
->buffers_count
= 0;
817 * Start transfer if at least one buffer has been queued,
818 * otherwise transfer is deferred at buffer queueing
820 if (list_empty(&dcmi
->buffers
)) {
821 dev_dbg(dcmi
->dev
, "Start streaming is deferred to next buffer queueing\n");
822 dcmi
->state
= WAIT_FOR_BUFFER
;
823 spin_unlock_irq(&dcmi
->irqlock
);
827 buf
= list_entry(dcmi
->buffers
.next
, struct dcmi_buf
, list
);
830 dcmi
->state
= RUNNING
;
832 dev_dbg(dcmi
->dev
, "Start streaming, starting capture\n");
834 spin_unlock_irq(&dcmi
->irqlock
);
835 ret
= dcmi_start_capture(dcmi
, buf
);
837 dev_err(dcmi
->dev
, "%s: Start streaming failed, cannot start capture\n",
839 goto err_pipeline_stop
;
842 /* Enable interruptions */
843 if (dcmi
->sd_format
->fourcc
== V4L2_PIX_FMT_JPEG
)
844 reg_set(dcmi
->regs
, DCMI_IER
, IT_FRAME
| IT_OVR
| IT_ERR
);
846 reg_set(dcmi
->regs
, DCMI_IER
, IT_OVR
| IT_ERR
);
851 dcmi_pipeline_stop(dcmi
);
853 err_media_pipeline_stop
:
854 media_pipeline_stop(&dcmi
->vdev
->entity
);
857 pm_runtime_put(dcmi
->dev
);
858 spin_lock_irq(&dcmi
->irqlock
);
860 * Return all buffers to vb2 in QUEUED state.
861 * This will give ownership back to userspace
863 list_for_each_entry_safe(buf
, node
, &dcmi
->buffers
, list
) {
864 list_del_init(&buf
->list
);
865 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_QUEUED
);
868 spin_unlock_irq(&dcmi
->irqlock
);
873 static void dcmi_stop_streaming(struct vb2_queue
*vq
)
875 struct stm32_dcmi
*dcmi
= vb2_get_drv_priv(vq
);
876 struct dcmi_buf
*buf
, *node
;
878 dcmi_pipeline_stop(dcmi
);
880 media_pipeline_stop(&dcmi
->vdev
->entity
);
882 spin_lock_irq(&dcmi
->irqlock
);
884 /* Disable interruptions */
885 reg_clear(dcmi
->regs
, DCMI_IER
, IT_FRAME
| IT_OVR
| IT_ERR
);
888 reg_clear(dcmi
->regs
, DCMI_CR
, CR_ENABLE
);
890 /* Return all queued buffers to vb2 in ERROR state */
891 list_for_each_entry_safe(buf
, node
, &dcmi
->buffers
, list
) {
892 list_del_init(&buf
->list
);
893 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
897 dcmi
->state
= STOPPED
;
899 spin_unlock_irq(&dcmi
->irqlock
);
901 /* Stop all pending DMA operations */
902 mutex_lock(&dcmi
->dma_lock
);
903 dmaengine_terminate_sync(dcmi
->dma_chan
);
904 mutex_unlock(&dcmi
->dma_lock
);
906 pm_runtime_put(dcmi
->dev
);
908 if (dcmi
->errors_count
)
909 dev_warn(dcmi
->dev
, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n",
910 dcmi
->errors_count
, dcmi
->overrun_count
,
911 dcmi
->buffers_count
);
912 dev_dbg(dcmi
->dev
, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n",
913 dcmi
->errors_count
, dcmi
->overrun_count
,
914 dcmi
->buffers_count
);
917 static const struct vb2_ops dcmi_video_qops
= {
918 .queue_setup
= dcmi_queue_setup
,
919 .buf_init
= dcmi_buf_init
,
920 .buf_prepare
= dcmi_buf_prepare
,
921 .buf_queue
= dcmi_buf_queue
,
922 .start_streaming
= dcmi_start_streaming
,
923 .stop_streaming
= dcmi_stop_streaming
,
924 .wait_prepare
= vb2_ops_wait_prepare
,
925 .wait_finish
= vb2_ops_wait_finish
,
928 static int dcmi_g_fmt_vid_cap(struct file
*file
, void *priv
,
929 struct v4l2_format
*fmt
)
931 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
938 static const struct dcmi_format
*find_format_by_fourcc(struct stm32_dcmi
*dcmi
,
941 unsigned int num_formats
= dcmi
->num_of_sd_formats
;
942 const struct dcmi_format
*fmt
;
945 for (i
= 0; i
< num_formats
; i
++) {
946 fmt
= dcmi
->sd_formats
[i
];
947 if (fmt
->fourcc
== fourcc
)
954 static void __find_outer_frame_size(struct stm32_dcmi
*dcmi
,
955 struct v4l2_pix_format
*pix
,
956 struct dcmi_framesize
*framesize
)
958 struct dcmi_framesize
*match
= NULL
;
960 unsigned int min_err
= UINT_MAX
;
962 for (i
= 0; i
< dcmi
->num_of_sd_framesizes
; i
++) {
963 struct dcmi_framesize
*fsize
= &dcmi
->sd_framesizes
[i
];
964 int w_err
= (fsize
->width
- pix
->width
);
965 int h_err
= (fsize
->height
- pix
->height
);
966 int err
= w_err
+ h_err
;
968 if (w_err
>= 0 && h_err
>= 0 && err
< min_err
) {
974 match
= &dcmi
->sd_framesizes
[0];
979 static int dcmi_try_fmt(struct stm32_dcmi
*dcmi
, struct v4l2_format
*f
,
980 const struct dcmi_format
**sd_format
,
981 struct dcmi_framesize
*sd_framesize
)
983 const struct dcmi_format
*sd_fmt
;
984 struct dcmi_framesize sd_fsize
;
985 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
986 struct v4l2_subdev_pad_config pad_cfg
;
987 struct v4l2_subdev_format format
= {
988 .which
= V4L2_SUBDEV_FORMAT_TRY
,
993 sd_fmt
= find_format_by_fourcc(dcmi
, pix
->pixelformat
);
995 if (!dcmi
->num_of_sd_formats
)
998 sd_fmt
= dcmi
->sd_formats
[dcmi
->num_of_sd_formats
- 1];
999 pix
->pixelformat
= sd_fmt
->fourcc
;
1002 /* Limit to hardware capabilities */
1003 pix
->width
= clamp(pix
->width
, MIN_WIDTH
, MAX_WIDTH
);
1004 pix
->height
= clamp(pix
->height
, MIN_HEIGHT
, MAX_HEIGHT
);
1006 /* No crop if JPEG is requested */
1007 do_crop
= dcmi
->do_crop
&& (pix
->pixelformat
!= V4L2_PIX_FMT_JPEG
);
1009 if (do_crop
&& dcmi
->num_of_sd_framesizes
) {
1010 struct dcmi_framesize outer_sd_fsize
;
1012 * If crop is requested and sensor have discrete frame sizes,
1013 * select the frame size that is just larger than request
1015 __find_outer_frame_size(dcmi
, pix
, &outer_sd_fsize
);
1016 pix
->width
= outer_sd_fsize
.width
;
1017 pix
->height
= outer_sd_fsize
.height
;
1020 v4l2_fill_mbus_format(&format
.format
, pix
, sd_fmt
->mbus_code
);
1021 ret
= v4l2_subdev_call(dcmi
->entity
.source
, pad
, set_fmt
,
1026 /* Update pix regarding to what sensor can do */
1027 v4l2_fill_pix_format(pix
, &format
.format
);
1029 /* Save resolution that sensor can actually do */
1030 sd_fsize
.width
= pix
->width
;
1031 sd_fsize
.height
= pix
->height
;
1034 struct v4l2_rect c
= dcmi
->crop
;
1035 struct v4l2_rect max_rect
;
1038 * Adjust crop by making the intersection between
1039 * format resolution request and crop request
1043 max_rect
.width
= pix
->width
;
1044 max_rect
.height
= pix
->height
;
1045 v4l2_rect_map_inside(&c
, &max_rect
);
1046 c
.top
= clamp_t(s32
, c
.top
, 0, pix
->height
- c
.height
);
1047 c
.left
= clamp_t(s32
, c
.left
, 0, pix
->width
- c
.width
);
1050 /* Adjust format resolution request to crop */
1051 pix
->width
= dcmi
->crop
.width
;
1052 pix
->height
= dcmi
->crop
.height
;
1055 pix
->field
= V4L2_FIELD_NONE
;
1056 pix
->bytesperline
= pix
->width
* sd_fmt
->bpp
;
1057 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
1060 *sd_format
= sd_fmt
;
1062 *sd_framesize
= sd_fsize
;
1067 static int dcmi_set_fmt(struct stm32_dcmi
*dcmi
, struct v4l2_format
*f
)
1069 struct v4l2_subdev_format format
= {
1070 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1072 const struct dcmi_format
*sd_format
;
1073 struct dcmi_framesize sd_framesize
;
1074 struct v4l2_mbus_framefmt
*mf
= &format
.format
;
1075 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1079 * Try format, fmt.width/height could have been changed
1080 * to match sensor capability or crop request
1081 * sd_format & sd_framesize will contain what subdev
1082 * can do for this request.
1084 ret
= dcmi_try_fmt(dcmi
, f
, &sd_format
, &sd_framesize
);
1088 /* Disable crop if JPEG is requested or BT656 bus is selected */
1089 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
&&
1090 dcmi
->bus_type
!= V4L2_MBUS_BT656
)
1091 dcmi
->do_crop
= false;
1093 /* pix to mbus format */
1094 v4l2_fill_mbus_format(mf
, pix
,
1095 sd_format
->mbus_code
);
1096 mf
->width
= sd_framesize
.width
;
1097 mf
->height
= sd_framesize
.height
;
1099 ret
= dcmi_pipeline_s_fmt(dcmi
, NULL
, &format
);
1103 dev_dbg(dcmi
->dev
, "Sensor format set to 0x%x %ux%u\n",
1104 mf
->code
, mf
->width
, mf
->height
);
1105 dev_dbg(dcmi
->dev
, "Buffer format set to %4.4s %ux%u\n",
1106 (char *)&pix
->pixelformat
,
1107 pix
->width
, pix
->height
);
1110 dcmi
->sd_format
= sd_format
;
1111 dcmi
->sd_framesize
= sd_framesize
;
1116 static int dcmi_s_fmt_vid_cap(struct file
*file
, void *priv
,
1117 struct v4l2_format
*f
)
1119 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1121 if (vb2_is_streaming(&dcmi
->queue
))
1124 return dcmi_set_fmt(dcmi
, f
);
1127 static int dcmi_try_fmt_vid_cap(struct file
*file
, void *priv
,
1128 struct v4l2_format
*f
)
1130 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1132 return dcmi_try_fmt(dcmi
, f
, NULL
, NULL
);
1135 static int dcmi_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1136 struct v4l2_fmtdesc
*f
)
1138 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1140 if (f
->index
>= dcmi
->num_of_sd_formats
)
1143 f
->pixelformat
= dcmi
->sd_formats
[f
->index
]->fourcc
;
1147 static int dcmi_get_sensor_format(struct stm32_dcmi
*dcmi
,
1148 struct v4l2_pix_format
*pix
)
1150 struct v4l2_subdev_format fmt
= {
1151 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1155 ret
= v4l2_subdev_call(dcmi
->entity
.source
, pad
, get_fmt
, NULL
, &fmt
);
1159 v4l2_fill_pix_format(pix
, &fmt
.format
);
1164 static int dcmi_set_sensor_format(struct stm32_dcmi
*dcmi
,
1165 struct v4l2_pix_format
*pix
)
1167 const struct dcmi_format
*sd_fmt
;
1168 struct v4l2_subdev_format format
= {
1169 .which
= V4L2_SUBDEV_FORMAT_TRY
,
1171 struct v4l2_subdev_pad_config pad_cfg
;
1174 sd_fmt
= find_format_by_fourcc(dcmi
, pix
->pixelformat
);
1176 if (!dcmi
->num_of_sd_formats
)
1179 sd_fmt
= dcmi
->sd_formats
[dcmi
->num_of_sd_formats
- 1];
1180 pix
->pixelformat
= sd_fmt
->fourcc
;
1183 v4l2_fill_mbus_format(&format
.format
, pix
, sd_fmt
->mbus_code
);
1184 ret
= v4l2_subdev_call(dcmi
->entity
.source
, pad
, set_fmt
,
1192 static int dcmi_get_sensor_bounds(struct stm32_dcmi
*dcmi
,
1193 struct v4l2_rect
*r
)
1195 struct v4l2_subdev_selection bounds
= {
1196 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1197 .target
= V4L2_SEL_TGT_CROP_BOUNDS
,
1199 unsigned int max_width
, max_height
, max_pixsize
;
1200 struct v4l2_pix_format pix
;
1205 * Get sensor bounds first
1207 ret
= v4l2_subdev_call(dcmi
->entity
.source
, pad
, get_selection
,
1211 if (ret
!= -ENOIOCTLCMD
)
1215 * If selection is not implemented,
1216 * fallback by enumerating sensor frame sizes
1217 * and take the largest one
1222 for (i
= 0; i
< dcmi
->num_of_sd_framesizes
; i
++) {
1223 struct dcmi_framesize
*fsize
= &dcmi
->sd_framesizes
[i
];
1224 unsigned int pixsize
= fsize
->width
* fsize
->height
;
1226 if (pixsize
> max_pixsize
) {
1227 max_pixsize
= pixsize
;
1228 max_width
= fsize
->width
;
1229 max_height
= fsize
->height
;
1232 if (max_pixsize
> 0) {
1235 r
->width
= max_width
;
1236 r
->height
= max_height
;
1241 * If frame sizes enumeration is not implemented,
1242 * fallback by getting current sensor frame size
1244 ret
= dcmi_get_sensor_format(dcmi
, &pix
);
1250 r
->width
= pix
.width
;
1251 r
->height
= pix
.height
;
1256 static int dcmi_g_selection(struct file
*file
, void *fh
,
1257 struct v4l2_selection
*s
)
1259 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1261 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1264 switch (s
->target
) {
1265 case V4L2_SEL_TGT_CROP_DEFAULT
:
1266 case V4L2_SEL_TGT_CROP_BOUNDS
:
1267 s
->r
= dcmi
->sd_bounds
;
1269 case V4L2_SEL_TGT_CROP
:
1270 if (dcmi
->do_crop
) {
1275 s
->r
.width
= dcmi
->fmt
.fmt
.pix
.width
;
1276 s
->r
.height
= dcmi
->fmt
.fmt
.pix
.height
;
1286 static int dcmi_s_selection(struct file
*file
, void *priv
,
1287 struct v4l2_selection
*s
)
1289 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1290 struct v4l2_rect r
= s
->r
;
1291 struct v4l2_rect max_rect
;
1292 struct v4l2_pix_format pix
;
1294 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1295 s
->target
!= V4L2_SEL_TGT_CROP
)
1298 /* Reset sensor resolution to max resolution */
1299 pix
.pixelformat
= dcmi
->fmt
.fmt
.pix
.pixelformat
;
1300 pix
.width
= dcmi
->sd_bounds
.width
;
1301 pix
.height
= dcmi
->sd_bounds
.height
;
1302 dcmi_set_sensor_format(dcmi
, &pix
);
1305 * Make the intersection between
1311 max_rect
.width
= pix
.width
;
1312 max_rect
.height
= pix
.height
;
1313 v4l2_rect_map_inside(&r
, &max_rect
);
1314 r
.top
= clamp_t(s32
, r
.top
, 0, pix
.height
- r
.height
);
1315 r
.left
= clamp_t(s32
, r
.left
, 0, pix
.width
- r
.width
);
1317 if (!(r
.top
== dcmi
->sd_bounds
.top
&&
1318 r
.left
== dcmi
->sd_bounds
.left
&&
1319 r
.width
== dcmi
->sd_bounds
.width
&&
1320 r
.height
== dcmi
->sd_bounds
.height
)) {
1321 /* Crop if request is different than sensor resolution */
1322 dcmi
->do_crop
= true;
1324 dev_dbg(dcmi
->dev
, "s_selection: crop %ux%u@(%u,%u) from %ux%u\n",
1325 r
.width
, r
.height
, r
.left
, r
.top
,
1326 pix
.width
, pix
.height
);
1329 dcmi
->do_crop
= false;
1330 dev_dbg(dcmi
->dev
, "s_selection: crop is disabled\n");
1337 static int dcmi_querycap(struct file
*file
, void *priv
,
1338 struct v4l2_capability
*cap
)
1340 strscpy(cap
->driver
, DRV_NAME
, sizeof(cap
->driver
));
1341 strscpy(cap
->card
, "STM32 Camera Memory Interface",
1343 strscpy(cap
->bus_info
, "platform:dcmi", sizeof(cap
->bus_info
));
1347 static int dcmi_enum_input(struct file
*file
, void *priv
,
1348 struct v4l2_input
*i
)
1353 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1354 strscpy(i
->name
, "Camera", sizeof(i
->name
));
1358 static int dcmi_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1364 static int dcmi_s_input(struct file
*file
, void *priv
, unsigned int i
)
1371 static int dcmi_enum_framesizes(struct file
*file
, void *fh
,
1372 struct v4l2_frmsizeenum
*fsize
)
1374 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1375 const struct dcmi_format
*sd_fmt
;
1376 struct v4l2_subdev_frame_size_enum fse
= {
1377 .index
= fsize
->index
,
1378 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1382 sd_fmt
= find_format_by_fourcc(dcmi
, fsize
->pixel_format
);
1386 fse
.code
= sd_fmt
->mbus_code
;
1388 ret
= v4l2_subdev_call(dcmi
->entity
.source
, pad
, enum_frame_size
,
1393 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1394 fsize
->discrete
.width
= fse
.max_width
;
1395 fsize
->discrete
.height
= fse
.max_height
;
1400 static int dcmi_g_parm(struct file
*file
, void *priv
,
1401 struct v4l2_streamparm
*p
)
1403 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1405 return v4l2_g_parm_cap(video_devdata(file
), dcmi
->entity
.source
, p
);
1408 static int dcmi_s_parm(struct file
*file
, void *priv
,
1409 struct v4l2_streamparm
*p
)
1411 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1413 return v4l2_s_parm_cap(video_devdata(file
), dcmi
->entity
.source
, p
);
1416 static int dcmi_enum_frameintervals(struct file
*file
, void *fh
,
1417 struct v4l2_frmivalenum
*fival
)
1419 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1420 const struct dcmi_format
*sd_fmt
;
1421 struct v4l2_subdev_frame_interval_enum fie
= {
1422 .index
= fival
->index
,
1423 .width
= fival
->width
,
1424 .height
= fival
->height
,
1425 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1429 sd_fmt
= find_format_by_fourcc(dcmi
, fival
->pixel_format
);
1433 fie
.code
= sd_fmt
->mbus_code
;
1435 ret
= v4l2_subdev_call(dcmi
->entity
.source
, pad
,
1436 enum_frame_interval
, NULL
, &fie
);
1440 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1441 fival
->discrete
= fie
.interval
;
1446 static const struct of_device_id stm32_dcmi_of_match
[] = {
1447 { .compatible
= "st,stm32-dcmi"},
1450 MODULE_DEVICE_TABLE(of
, stm32_dcmi_of_match
);
1452 static int dcmi_open(struct file
*file
)
1454 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1455 struct v4l2_subdev
*sd
= dcmi
->entity
.source
;
1458 if (mutex_lock_interruptible(&dcmi
->lock
))
1459 return -ERESTARTSYS
;
1461 ret
= v4l2_fh_open(file
);
1465 if (!v4l2_fh_is_singular_file(file
))
1468 ret
= v4l2_subdev_call(sd
, core
, s_power
, 1);
1469 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1472 ret
= dcmi_set_fmt(dcmi
, &dcmi
->fmt
);
1474 v4l2_subdev_call(sd
, core
, s_power
, 0);
1477 v4l2_fh_release(file
);
1479 mutex_unlock(&dcmi
->lock
);
1483 static int dcmi_release(struct file
*file
)
1485 struct stm32_dcmi
*dcmi
= video_drvdata(file
);
1486 struct v4l2_subdev
*sd
= dcmi
->entity
.source
;
1490 mutex_lock(&dcmi
->lock
);
1492 fh_singular
= v4l2_fh_is_singular_file(file
);
1494 ret
= _vb2_fop_release(file
, NULL
);
1497 v4l2_subdev_call(sd
, core
, s_power
, 0);
1499 mutex_unlock(&dcmi
->lock
);
1504 static const struct v4l2_ioctl_ops dcmi_ioctl_ops
= {
1505 .vidioc_querycap
= dcmi_querycap
,
1507 .vidioc_try_fmt_vid_cap
= dcmi_try_fmt_vid_cap
,
1508 .vidioc_g_fmt_vid_cap
= dcmi_g_fmt_vid_cap
,
1509 .vidioc_s_fmt_vid_cap
= dcmi_s_fmt_vid_cap
,
1510 .vidioc_enum_fmt_vid_cap
= dcmi_enum_fmt_vid_cap
,
1511 .vidioc_g_selection
= dcmi_g_selection
,
1512 .vidioc_s_selection
= dcmi_s_selection
,
1514 .vidioc_enum_input
= dcmi_enum_input
,
1515 .vidioc_g_input
= dcmi_g_input
,
1516 .vidioc_s_input
= dcmi_s_input
,
1518 .vidioc_g_parm
= dcmi_g_parm
,
1519 .vidioc_s_parm
= dcmi_s_parm
,
1521 .vidioc_enum_framesizes
= dcmi_enum_framesizes
,
1522 .vidioc_enum_frameintervals
= dcmi_enum_frameintervals
,
1524 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1525 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1526 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1527 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1528 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1529 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1530 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1531 .vidioc_streamon
= vb2_ioctl_streamon
,
1532 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1534 .vidioc_log_status
= v4l2_ctrl_log_status
,
1535 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1536 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1539 static const struct v4l2_file_operations dcmi_fops
= {
1540 .owner
= THIS_MODULE
,
1541 .unlocked_ioctl
= video_ioctl2
,
1543 .release
= dcmi_release
,
1544 .poll
= vb2_fop_poll
,
1545 .mmap
= vb2_fop_mmap
,
1547 .get_unmapped_area
= vb2_fop_get_unmapped_area
,
1549 .read
= vb2_fop_read
,
1552 static int dcmi_set_default_fmt(struct stm32_dcmi
*dcmi
)
1554 struct v4l2_format f
= {
1555 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1558 .height
= CIF_HEIGHT
,
1559 .field
= V4L2_FIELD_NONE
,
1560 .pixelformat
= dcmi
->sd_formats
[0]->fourcc
,
1565 ret
= dcmi_try_fmt(dcmi
, &f
, NULL
, NULL
);
1568 dcmi
->sd_format
= dcmi
->sd_formats
[0];
1574 * FIXME: For the time being we only support subdevices
1575 * which expose RGB & YUV "parallel form" mbus code (_2X8).
1576 * Nevertheless, this allows to support serial source subdevices
1577 * and serial to parallel bridges which conform to this.
1579 static const struct dcmi_format dcmi_formats
[] = {
1581 .fourcc
= V4L2_PIX_FMT_RGB565
,
1582 .mbus_code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
1585 .fourcc
= V4L2_PIX_FMT_YUYV
,
1586 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
1589 .fourcc
= V4L2_PIX_FMT_UYVY
,
1590 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
1593 .fourcc
= V4L2_PIX_FMT_JPEG
,
1594 .mbus_code
= MEDIA_BUS_FMT_JPEG_1X8
,
1597 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
1598 .mbus_code
= MEDIA_BUS_FMT_SBGGR8_1X8
,
1601 .fourcc
= V4L2_PIX_FMT_SGBRG8
,
1602 .mbus_code
= MEDIA_BUS_FMT_SGBRG8_1X8
,
1605 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
1606 .mbus_code
= MEDIA_BUS_FMT_SGRBG8_1X8
,
1609 .fourcc
= V4L2_PIX_FMT_SRGGB8
,
1610 .mbus_code
= MEDIA_BUS_FMT_SRGGB8_1X8
,
1615 static int dcmi_formats_init(struct stm32_dcmi
*dcmi
)
1617 const struct dcmi_format
*sd_fmts
[ARRAY_SIZE(dcmi_formats
)];
1618 unsigned int num_fmts
= 0, i
, j
;
1619 struct v4l2_subdev
*subdev
= dcmi
->entity
.source
;
1620 struct v4l2_subdev_mbus_code_enum mbus_code
= {
1621 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1624 while (!v4l2_subdev_call(subdev
, pad
, enum_mbus_code
,
1625 NULL
, &mbus_code
)) {
1626 for (i
= 0; i
< ARRAY_SIZE(dcmi_formats
); i
++) {
1627 if (dcmi_formats
[i
].mbus_code
!= mbus_code
.code
)
1630 /* Exclude JPEG if BT656 bus is selected */
1631 if (dcmi_formats
[i
].fourcc
== V4L2_PIX_FMT_JPEG
&&
1632 dcmi
->bus_type
== V4L2_MBUS_BT656
)
1635 /* Code supported, have we got this fourcc yet? */
1636 for (j
= 0; j
< num_fmts
; j
++)
1637 if (sd_fmts
[j
]->fourcc
==
1638 dcmi_formats
[i
].fourcc
) {
1639 /* Already available */
1640 dev_dbg(dcmi
->dev
, "Skipping fourcc/code: %4.4s/0x%x\n",
1641 (char *)&sd_fmts
[j
]->fourcc
,
1645 if (j
== num_fmts
) {
1647 sd_fmts
[num_fmts
++] = dcmi_formats
+ i
;
1648 dev_dbg(dcmi
->dev
, "Supported fourcc/code: %4.4s/0x%x\n",
1649 (char *)&sd_fmts
[num_fmts
- 1]->fourcc
,
1650 sd_fmts
[num_fmts
- 1]->mbus_code
);
1659 dcmi
->num_of_sd_formats
= num_fmts
;
1660 dcmi
->sd_formats
= devm_kcalloc(dcmi
->dev
,
1661 num_fmts
, sizeof(struct dcmi_format
*),
1663 if (!dcmi
->sd_formats
) {
1664 dev_err(dcmi
->dev
, "Could not allocate memory\n");
1668 memcpy(dcmi
->sd_formats
, sd_fmts
,
1669 num_fmts
* sizeof(struct dcmi_format
*));
1670 dcmi
->sd_format
= dcmi
->sd_formats
[0];
1675 static int dcmi_framesizes_init(struct stm32_dcmi
*dcmi
)
1677 unsigned int num_fsize
= 0;
1678 struct v4l2_subdev
*subdev
= dcmi
->entity
.source
;
1679 struct v4l2_subdev_frame_size_enum fse
= {
1680 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1681 .code
= dcmi
->sd_format
->mbus_code
,
1686 /* Allocate discrete framesizes array */
1687 while (!v4l2_subdev_call(subdev
, pad
, enum_frame_size
,
1691 num_fsize
= fse
.index
;
1695 dcmi
->num_of_sd_framesizes
= num_fsize
;
1696 dcmi
->sd_framesizes
= devm_kcalloc(dcmi
->dev
, num_fsize
,
1697 sizeof(struct dcmi_framesize
),
1699 if (!dcmi
->sd_framesizes
) {
1700 dev_err(dcmi
->dev
, "Could not allocate memory\n");
1704 /* Fill array with sensor supported framesizes */
1705 dev_dbg(dcmi
->dev
, "Sensor supports %u frame sizes:\n", num_fsize
);
1706 for (i
= 0; i
< dcmi
->num_of_sd_framesizes
; i
++) {
1708 ret
= v4l2_subdev_call(subdev
, pad
, enum_frame_size
,
1712 dcmi
->sd_framesizes
[fse
.index
].width
= fse
.max_width
;
1713 dcmi
->sd_framesizes
[fse
.index
].height
= fse
.max_height
;
1714 dev_dbg(dcmi
->dev
, "%ux%u\n", fse
.max_width
, fse
.max_height
);
1720 static int dcmi_graph_notify_complete(struct v4l2_async_notifier
*notifier
)
1722 struct stm32_dcmi
*dcmi
= notifier_to_dcmi(notifier
);
1726 * Now that the graph is complete,
1727 * we search for the source subdevice
1728 * in order to expose it through V4L2 interface
1730 dcmi
->entity
.source
=
1731 media_entity_to_v4l2_subdev(dcmi_find_source(dcmi
));
1732 if (!dcmi
->entity
.source
) {
1733 dev_err(dcmi
->dev
, "Source subdevice not found\n");
1737 dcmi
->vdev
->ctrl_handler
= dcmi
->entity
.source
->ctrl_handler
;
1739 ret
= dcmi_formats_init(dcmi
);
1741 dev_err(dcmi
->dev
, "No supported mediabus format found\n");
1745 ret
= dcmi_framesizes_init(dcmi
);
1747 dev_err(dcmi
->dev
, "Could not initialize framesizes\n");
1751 ret
= dcmi_get_sensor_bounds(dcmi
, &dcmi
->sd_bounds
);
1753 dev_err(dcmi
->dev
, "Could not get sensor bounds\n");
1757 ret
= dcmi_set_default_fmt(dcmi
);
1759 dev_err(dcmi
->dev
, "Could not set default format\n");
1766 static void dcmi_graph_notify_unbind(struct v4l2_async_notifier
*notifier
,
1767 struct v4l2_subdev
*sd
,
1768 struct v4l2_async_subdev
*asd
)
1770 struct stm32_dcmi
*dcmi
= notifier_to_dcmi(notifier
);
1772 dev_dbg(dcmi
->dev
, "Removing %s\n", video_device_node_name(dcmi
->vdev
));
1774 /* Checks internally if vdev has been init or not */
1775 video_unregister_device(dcmi
->vdev
);
1778 static int dcmi_graph_notify_bound(struct v4l2_async_notifier
*notifier
,
1779 struct v4l2_subdev
*subdev
,
1780 struct v4l2_async_subdev
*asd
)
1782 struct stm32_dcmi
*dcmi
= notifier_to_dcmi(notifier
);
1786 dev_dbg(dcmi
->dev
, "Subdev \"%s\" bound\n", subdev
->name
);
1789 * Link this sub-device to DCMI, it could be
1790 * a parallel camera sensor or a bridge
1792 src_pad
= media_entity_get_fwnode_pad(&subdev
->entity
,
1794 MEDIA_PAD_FL_SOURCE
);
1796 ret
= media_create_pad_link(&subdev
->entity
, src_pad
,
1797 &dcmi
->vdev
->entity
, 0,
1798 MEDIA_LNK_FL_IMMUTABLE
|
1799 MEDIA_LNK_FL_ENABLED
);
1801 dev_err(dcmi
->dev
, "Failed to create media pad link with subdev \"%s\"\n",
1804 dev_dbg(dcmi
->dev
, "DCMI is now linked to \"%s\"\n",
1810 static const struct v4l2_async_notifier_operations dcmi_graph_notify_ops
= {
1811 .bound
= dcmi_graph_notify_bound
,
1812 .unbind
= dcmi_graph_notify_unbind
,
1813 .complete
= dcmi_graph_notify_complete
,
1816 static int dcmi_graph_parse(struct stm32_dcmi
*dcmi
, struct device_node
*node
)
1818 struct device_node
*ep
= NULL
;
1819 struct device_node
*remote
;
1821 ep
= of_graph_get_next_endpoint(node
, ep
);
1825 remote
= of_graph_get_remote_port_parent(ep
);
1830 /* Remote node to connect */
1831 dcmi
->entity
.remote_node
= remote
;
1832 dcmi
->entity
.asd
.match_type
= V4L2_ASYNC_MATCH_FWNODE
;
1833 dcmi
->entity
.asd
.match
.fwnode
= of_fwnode_handle(remote
);
1837 static int dcmi_graph_init(struct stm32_dcmi
*dcmi
)
1841 /* Parse the graph to extract a list of subdevice DT nodes. */
1842 ret
= dcmi_graph_parse(dcmi
, dcmi
->dev
->of_node
);
1844 dev_err(dcmi
->dev
, "Failed to parse graph\n");
1848 v4l2_async_notifier_init(&dcmi
->notifier
);
1850 ret
= v4l2_async_notifier_add_subdev(&dcmi
->notifier
,
1853 dev_err(dcmi
->dev
, "Failed to add subdev notifier\n");
1854 of_node_put(dcmi
->entity
.remote_node
);
1858 dcmi
->notifier
.ops
= &dcmi_graph_notify_ops
;
1860 ret
= v4l2_async_notifier_register(&dcmi
->v4l2_dev
, &dcmi
->notifier
);
1862 dev_err(dcmi
->dev
, "Failed to register notifier\n");
1863 v4l2_async_notifier_cleanup(&dcmi
->notifier
);
1870 static int dcmi_probe(struct platform_device
*pdev
)
1872 struct device_node
*np
= pdev
->dev
.of_node
;
1873 const struct of_device_id
*match
= NULL
;
1874 struct v4l2_fwnode_endpoint ep
= { .bus_type
= 0 };
1875 struct stm32_dcmi
*dcmi
;
1876 struct vb2_queue
*q
;
1877 struct dma_chan
*chan
;
1882 match
= of_match_device(of_match_ptr(stm32_dcmi_of_match
), &pdev
->dev
);
1884 dev_err(&pdev
->dev
, "Could not find a match in devicetree\n");
1888 dcmi
= devm_kzalloc(&pdev
->dev
, sizeof(struct stm32_dcmi
), GFP_KERNEL
);
1892 dcmi
->rstc
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
1893 if (IS_ERR(dcmi
->rstc
)) {
1894 if (PTR_ERR(dcmi
->rstc
) != -EPROBE_DEFER
)
1895 dev_err(&pdev
->dev
, "Could not get reset control\n");
1897 return PTR_ERR(dcmi
->rstc
);
1900 /* Get bus characteristics from devicetree */
1901 np
= of_graph_get_next_endpoint(np
, NULL
);
1903 dev_err(&pdev
->dev
, "Could not find the endpoint\n");
1907 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(np
), &ep
);
1910 dev_err(&pdev
->dev
, "Could not parse the endpoint\n");
1914 if (ep
.bus_type
== V4L2_MBUS_CSI2_DPHY
) {
1915 dev_err(&pdev
->dev
, "CSI bus not supported\n");
1919 if (ep
.bus_type
== V4L2_MBUS_BT656
&&
1920 ep
.bus
.parallel
.bus_width
!= 8) {
1921 dev_err(&pdev
->dev
, "BT656 bus conflicts with %u bits bus width (8 bits required)\n",
1922 ep
.bus
.parallel
.bus_width
);
1926 dcmi
->bus
.flags
= ep
.bus
.parallel
.flags
;
1927 dcmi
->bus
.bus_width
= ep
.bus
.parallel
.bus_width
;
1928 dcmi
->bus
.data_shift
= ep
.bus
.parallel
.data_shift
;
1929 dcmi
->bus_type
= ep
.bus_type
;
1931 irq
= platform_get_irq(pdev
, 0);
1933 return irq
? irq
: -ENXIO
;
1935 dcmi
->res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1937 dev_err(&pdev
->dev
, "Could not get resource\n");
1941 dcmi
->regs
= devm_ioremap_resource(&pdev
->dev
, dcmi
->res
);
1942 if (IS_ERR(dcmi
->regs
)) {
1943 dev_err(&pdev
->dev
, "Could not map registers\n");
1944 return PTR_ERR(dcmi
->regs
);
1947 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, dcmi_irq_callback
,
1948 dcmi_irq_thread
, IRQF_ONESHOT
,
1949 dev_name(&pdev
->dev
), dcmi
);
1951 dev_err(&pdev
->dev
, "Unable to request irq %d\n", irq
);
1955 mclk
= devm_clk_get(&pdev
->dev
, "mclk");
1957 if (PTR_ERR(mclk
) != -EPROBE_DEFER
)
1958 dev_err(&pdev
->dev
, "Unable to get mclk\n");
1959 return PTR_ERR(mclk
);
1962 chan
= dma_request_chan(&pdev
->dev
, "tx");
1964 ret
= PTR_ERR(chan
);
1965 if (ret
!= -EPROBE_DEFER
)
1967 "Failed to request DMA channel: %d\n", ret
);
1971 spin_lock_init(&dcmi
->irqlock
);
1972 mutex_init(&dcmi
->lock
);
1973 mutex_init(&dcmi
->dma_lock
);
1974 init_completion(&dcmi
->complete
);
1975 INIT_LIST_HEAD(&dcmi
->buffers
);
1977 dcmi
->dev
= &pdev
->dev
;
1979 dcmi
->state
= STOPPED
;
1980 dcmi
->dma_chan
= chan
;
1984 dcmi
->v4l2_dev
.mdev
= &dcmi
->mdev
;
1986 /* Initialize media device */
1987 strscpy(dcmi
->mdev
.model
, DRV_NAME
, sizeof(dcmi
->mdev
.model
));
1988 snprintf(dcmi
->mdev
.bus_info
, sizeof(dcmi
->mdev
.bus_info
),
1989 "platform:%s", DRV_NAME
);
1990 dcmi
->mdev
.dev
= &pdev
->dev
;
1991 media_device_init(&dcmi
->mdev
);
1993 /* Initialize the top-level structure */
1994 ret
= v4l2_device_register(&pdev
->dev
, &dcmi
->v4l2_dev
);
1996 goto err_media_device_cleanup
;
1998 dcmi
->vdev
= video_device_alloc();
2001 goto err_device_unregister
;
2005 dcmi
->vdev
->fops
= &dcmi_fops
;
2006 dcmi
->vdev
->v4l2_dev
= &dcmi
->v4l2_dev
;
2007 dcmi
->vdev
->queue
= &dcmi
->queue
;
2008 strscpy(dcmi
->vdev
->name
, KBUILD_MODNAME
, sizeof(dcmi
->vdev
->name
));
2009 dcmi
->vdev
->release
= video_device_release
;
2010 dcmi
->vdev
->ioctl_ops
= &dcmi_ioctl_ops
;
2011 dcmi
->vdev
->lock
= &dcmi
->lock
;
2012 dcmi
->vdev
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
2014 video_set_drvdata(dcmi
->vdev
, dcmi
);
2016 /* Media entity pads */
2017 dcmi
->vid_cap_pad
.flags
= MEDIA_PAD_FL_SINK
;
2018 ret
= media_entity_pads_init(&dcmi
->vdev
->entity
,
2019 1, &dcmi
->vid_cap_pad
);
2021 dev_err(dcmi
->dev
, "Failed to init media entity pad\n");
2022 goto err_device_release
;
2024 dcmi
->vdev
->entity
.flags
|= MEDIA_ENT_FL_DEFAULT
;
2026 ret
= video_register_device(dcmi
->vdev
, VFL_TYPE_VIDEO
, -1);
2028 dev_err(dcmi
->dev
, "Failed to register video device\n");
2029 goto err_media_entity_cleanup
;
2032 dev_dbg(dcmi
->dev
, "Device registered as %s\n",
2033 video_device_node_name(dcmi
->vdev
));
2036 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2037 q
->io_modes
= VB2_MMAP
| VB2_READ
| VB2_DMABUF
;
2038 q
->lock
= &dcmi
->lock
;
2040 q
->buf_struct_size
= sizeof(struct dcmi_buf
);
2041 q
->ops
= &dcmi_video_qops
;
2042 q
->mem_ops
= &vb2_dma_contig_memops
;
2043 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
2044 q
->min_buffers_needed
= 2;
2045 q
->dev
= &pdev
->dev
;
2047 ret
= vb2_queue_init(q
);
2049 dev_err(&pdev
->dev
, "Failed to initialize vb2 queue\n");
2050 goto err_media_entity_cleanup
;
2053 ret
= dcmi_graph_init(dcmi
);
2055 goto err_media_entity_cleanup
;
2058 ret
= reset_control_assert(dcmi
->rstc
);
2060 dev_err(&pdev
->dev
, "Failed to assert the reset line\n");
2064 usleep_range(3000, 5000);
2066 ret
= reset_control_deassert(dcmi
->rstc
);
2068 dev_err(&pdev
->dev
, "Failed to deassert the reset line\n");
2072 dev_info(&pdev
->dev
, "Probe done\n");
2074 platform_set_drvdata(pdev
, dcmi
);
2076 pm_runtime_enable(&pdev
->dev
);
2081 v4l2_async_notifier_cleanup(&dcmi
->notifier
);
2082 err_media_entity_cleanup
:
2083 media_entity_cleanup(&dcmi
->vdev
->entity
);
2085 video_device_release(dcmi
->vdev
);
2086 err_device_unregister
:
2087 v4l2_device_unregister(&dcmi
->v4l2_dev
);
2088 err_media_device_cleanup
:
2089 media_device_cleanup(&dcmi
->mdev
);
2090 dma_release_channel(dcmi
->dma_chan
);
2095 static int dcmi_remove(struct platform_device
*pdev
)
2097 struct stm32_dcmi
*dcmi
= platform_get_drvdata(pdev
);
2099 pm_runtime_disable(&pdev
->dev
);
2101 v4l2_async_notifier_unregister(&dcmi
->notifier
);
2102 v4l2_async_notifier_cleanup(&dcmi
->notifier
);
2103 media_entity_cleanup(&dcmi
->vdev
->entity
);
2104 v4l2_device_unregister(&dcmi
->v4l2_dev
);
2105 media_device_cleanup(&dcmi
->mdev
);
2107 dma_release_channel(dcmi
->dma_chan
);
2112 static __maybe_unused
int dcmi_runtime_suspend(struct device
*dev
)
2114 struct stm32_dcmi
*dcmi
= dev_get_drvdata(dev
);
2116 clk_disable_unprepare(dcmi
->mclk
);
2121 static __maybe_unused
int dcmi_runtime_resume(struct device
*dev
)
2123 struct stm32_dcmi
*dcmi
= dev_get_drvdata(dev
);
2126 ret
= clk_prepare_enable(dcmi
->mclk
);
2128 dev_err(dev
, "%s: Failed to prepare_enable clock\n", __func__
);
2133 static __maybe_unused
int dcmi_suspend(struct device
*dev
)
2136 pm_runtime_force_suspend(dev
);
2138 /* change pinctrl state */
2139 pinctrl_pm_select_sleep_state(dev
);
2144 static __maybe_unused
int dcmi_resume(struct device
*dev
)
2146 /* restore pinctl default state */
2147 pinctrl_pm_select_default_state(dev
);
2150 pm_runtime_force_resume(dev
);
2155 static const struct dev_pm_ops dcmi_pm_ops
= {
2156 SET_SYSTEM_SLEEP_PM_OPS(dcmi_suspend
, dcmi_resume
)
2157 SET_RUNTIME_PM_OPS(dcmi_runtime_suspend
,
2158 dcmi_runtime_resume
, NULL
)
2161 static struct platform_driver stm32_dcmi_driver
= {
2162 .probe
= dcmi_probe
,
2163 .remove
= dcmi_remove
,
2166 .of_match_table
= of_match_ptr(stm32_dcmi_of_match
),
2171 module_platform_driver(stm32_dcmi_driver
);
2173 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
2174 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
2175 MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");
2176 MODULE_LICENSE("GPL");
2177 MODULE_SUPPORTED_DEVICE("video");