1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for STM32 Digital Camera Memory Interface
4 *
5 * Copyright (C) STMicroelectronics SA 2017
6 * Authors: Yannick Fertre <yannick.fertre@st.com>
7 * Hugues Fruchet <hugues.fruchet@st.com>
8 * for STMicroelectronics.
9 *
10 * This driver is based on atmel_isi.c
11 *
12 */
13
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>
22 #include <linux/of.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>
30
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>
40
41 #define DRV_NAME "stm32-dcmi"
42
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 */
56
57 /* Bits definition for control register (DCMI_CR) */
58 #define CR_CAPTURE BIT(0)
59 #define CR_CM BIT(1)
60 #define CR_CROP BIT(2)
61 #define CR_JPEG BIT(3)
62 #define CR_ESS BIT(4)
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)
71
72 /* Bits definition for status register (DCMI_SR) */
73 #define SR_HSYNC BIT(0)
74 #define SR_VSYNC BIT(1)
75 #define SR_FNE BIT(2)
76
77 /*
78 * Bits definition for interrupt registers
79 * (DCMI_RIS, DCMI_IER, DCMI_MIS, DCMI_ICR)
80 */
81 #define IT_FRAME BIT(0)
82 #define IT_OVR BIT(1)
83 #define IT_ERR BIT(2)
84 #define IT_VSYNC BIT(3)
85 #define IT_LINE BIT(4)
86
87 enum state {
88 STOPPED = 0,
89 WAIT_FOR_BUFFER,
90 RUNNING,
91 };
92
93 #define MIN_WIDTH 16U
94 #define MAX_WIDTH 2592U
95 #define MIN_HEIGHT 16U
96 #define MAX_HEIGHT 2592U
97
98 #define TIMEOUT_MS 1000
99
100 #define OVERRUN_ERROR_THRESHOLD 3
101
102 struct dcmi_format {
103 u32 fourcc;
104 u32 mbus_code;
105 u8 bpp;
106 };
107
108 struct dcmi_framesize {
109 u32 width;
110 u32 height;
111 };
112
113 struct dcmi_buf {
114 struct vb2_v4l2_buffer vb;
115 bool prepared;
116 dma_addr_t paddr;
117 size_t size;
118 struct list_head list;
119 };
120
121 struct stm32_dcmi {
122 /* Protects the access of variables shared within the interrupt */
123 spinlock_t irqlock;
124 struct device *dev;
125 void __iomem *regs;
126 struct resource *res;
127 struct reset_control *rstc;
128 int sequence;
129 struct list_head buffers;
130 struct dcmi_buf *active;
131 int irq;
132
133 struct v4l2_device v4l2_dev;
134 struct video_device *vdev;
135 struct v4l2_async_notifier notifier;
136 struct v4l2_subdev *source;
137 struct v4l2_format fmt;
138 struct v4l2_rect crop;
139 bool do_crop;
140
141 const struct dcmi_format **sd_formats;
142 unsigned int num_of_sd_formats;
143 const struct dcmi_format *sd_format;
144 struct dcmi_framesize *sd_framesizes;
145 unsigned int num_of_sd_framesizes;
146 struct dcmi_framesize sd_framesize;
147 struct v4l2_rect sd_bounds;
148
149 /* Protect this data structure */
150 struct mutex lock;
151 struct vb2_queue queue;
152
153 struct v4l2_fwnode_bus_parallel bus;
154 enum v4l2_mbus_type bus_type;
155 struct completion complete;
156 struct clk *mclk;
157 enum state state;
158 struct dma_chan *dma_chan;
159 dma_cookie_t dma_cookie;
160 u32 misr;
161 int errors_count;
162 int overrun_count;
163 int buffers_count;
164
165 /* Ensure DMA operations atomicity */
166 struct mutex dma_lock;
167
168 struct media_device mdev;
169 struct media_pad vid_cap_pad;
170 struct media_pipeline pipeline;
171 };
172
notifier_to_dcmi(struct v4l2_async_notifier * n)173 static inline struct stm32_dcmi *notifier_to_dcmi(struct v4l2_async_notifier *n)
174 {
175 return container_of(n, struct stm32_dcmi, notifier);
176 }
177
reg_read(void __iomem * base,u32 reg)178 static inline u32 reg_read(void __iomem *base, u32 reg)
179 {
180 return readl_relaxed(base + reg);
181 }
182
reg_write(void __iomem * base,u32 reg,u32 val)183 static inline void reg_write(void __iomem *base, u32 reg, u32 val)
184 {
185 writel_relaxed(val, base + reg);
186 }
187
reg_set(void __iomem * base,u32 reg,u32 mask)188 static inline void reg_set(void __iomem *base, u32 reg, u32 mask)
189 {
190 reg_write(base, reg, reg_read(base, reg) | mask);
191 }
192
reg_clear(void __iomem * base,u32 reg,u32 mask)193 static inline void reg_clear(void __iomem *base, u32 reg, u32 mask)
194 {
195 reg_write(base, reg, reg_read(base, reg) & ~mask);
196 }
197
198 static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf);
199
dcmi_buffer_done(struct stm32_dcmi * dcmi,struct dcmi_buf * buf,size_t bytesused,int err)200 static void dcmi_buffer_done(struct stm32_dcmi *dcmi,
201 struct dcmi_buf *buf,
202 size_t bytesused,
203 int err)
204 {
205 struct vb2_v4l2_buffer *vbuf;
206
207 if (!buf)
208 return;
209
210 list_del_init(&buf->list);
211
212 vbuf = &buf->vb;
213
214 vbuf->sequence = dcmi->sequence++;
215 vbuf->field = V4L2_FIELD_NONE;
216 vbuf->vb2_buf.timestamp = ktime_get_ns();
217 vb2_set_plane_payload(&vbuf->vb2_buf, 0, bytesused);
218 vb2_buffer_done(&vbuf->vb2_buf,
219 err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
220 dev_dbg(dcmi->dev, "buffer[%d] done seq=%d, bytesused=%zu\n",
221 vbuf->vb2_buf.index, vbuf->sequence, bytesused);
222
223 dcmi->buffers_count++;
224 dcmi->active = NULL;
225 }
226
dcmi_restart_capture(struct stm32_dcmi * dcmi)227 static int dcmi_restart_capture(struct stm32_dcmi *dcmi)
228 {
229 struct dcmi_buf *buf;
230
231 spin_lock_irq(&dcmi->irqlock);
232
233 if (dcmi->state != RUNNING) {
234 spin_unlock_irq(&dcmi->irqlock);
235 return -EINVAL;
236 }
237
238 /* Restart a new DMA transfer with next buffer */
239 if (list_empty(&dcmi->buffers)) {
240 dev_dbg(dcmi->dev, "Capture restart is deferred to next buffer queueing\n");
241 dcmi->state = WAIT_FOR_BUFFER;
242 spin_unlock_irq(&dcmi->irqlock);
243 return 0;
244 }
245 buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
246 dcmi->active = buf;
247
248 spin_unlock_irq(&dcmi->irqlock);
249
250 return dcmi_start_capture(dcmi, buf);
251 }
252
dcmi_dma_callback(void * param)253 static void dcmi_dma_callback(void *param)
254 {
255 struct stm32_dcmi *dcmi = (struct stm32_dcmi *)param;
256 struct dma_tx_state state;
257 enum dma_status status;
258 struct dcmi_buf *buf = dcmi->active;
259
260 spin_lock_irq(&dcmi->irqlock);
261
262 /* Check DMA status */
263 status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state);
264
265 switch (status) {
266 case DMA_IN_PROGRESS:
267 dev_dbg(dcmi->dev, "%s: Received DMA_IN_PROGRESS\n", __func__);
268 break;
269 case DMA_PAUSED:
270 dev_err(dcmi->dev, "%s: Received DMA_PAUSED\n", __func__);
271 break;
272 case DMA_ERROR:
273 dev_err(dcmi->dev, "%s: Received DMA_ERROR\n", __func__);
274
275 /* Return buffer to V4L2 in error state */
276 dcmi_buffer_done(dcmi, buf, 0, -EIO);
277 break;
278 case DMA_COMPLETE:
279 dev_dbg(dcmi->dev, "%s: Received DMA_COMPLETE\n", __func__);
280
281 /* Return buffer to V4L2 */
282 dcmi_buffer_done(dcmi, buf, buf->size, 0);
283
284 spin_unlock_irq(&dcmi->irqlock);
285
286 /* Restart capture */
287 if (dcmi_restart_capture(dcmi))
288 dev_err(dcmi->dev, "%s: Cannot restart capture on DMA complete\n",
289 __func__);
290 return;
291 default:
292 dev_err(dcmi->dev, "%s: Received unknown status\n", __func__);
293 break;
294 }
295
296 spin_unlock_irq(&dcmi->irqlock);
297 }
298
dcmi_start_dma(struct stm32_dcmi * dcmi,struct dcmi_buf * buf)299 static int dcmi_start_dma(struct stm32_dcmi *dcmi,
300 struct dcmi_buf *buf)
301 {
302 struct dma_async_tx_descriptor *desc = NULL;
303 struct dma_slave_config config;
304 int ret;
305
306 memset(&config, 0, sizeof(config));
307
308 config.src_addr = (dma_addr_t)dcmi->res->start + DCMI_DR;
309 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
310 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
311 config.dst_maxburst = 4;
312
313 /* Configure DMA channel */
314 ret = dmaengine_slave_config(dcmi->dma_chan, &config);
315 if (ret < 0) {
316 dev_err(dcmi->dev, "%s: DMA channel config failed (%d)\n",
317 __func__, ret);
318 return ret;
319 }
320
321 /*
322 * Avoid call of dmaengine_terminate_sync() between
323 * dmaengine_prep_slave_single() and dmaengine_submit()
324 * by locking the whole DMA submission sequence
325 */
326 mutex_lock(&dcmi->dma_lock);
327
328 /* Prepare a DMA transaction */
329 desc = dmaengine_prep_slave_single(dcmi->dma_chan, buf->paddr,
330 buf->size,
331 DMA_DEV_TO_MEM,
332 DMA_PREP_INTERRUPT);
333 if (!desc) {
334 dev_err(dcmi->dev, "%s: DMA dmaengine_prep_slave_single failed for buffer phy=%pad size=%zu\n",
335 __func__, &buf->paddr, buf->size);
336 mutex_unlock(&dcmi->dma_lock);
337 return -EINVAL;
338 }
339
340 /* Set completion callback routine for notification */
341 desc->callback = dcmi_dma_callback;
342 desc->callback_param = dcmi;
343
344 /* Push current DMA transaction in the pending queue */
345 dcmi->dma_cookie = dmaengine_submit(desc);
346 if (dma_submit_error(dcmi->dma_cookie)) {
347 dev_err(dcmi->dev, "%s: DMA submission failed\n", __func__);
348 mutex_unlock(&dcmi->dma_lock);
349 return -ENXIO;
350 }
351
352 mutex_unlock(&dcmi->dma_lock);
353
354 dma_async_issue_pending(dcmi->dma_chan);
355
356 return 0;
357 }
358
dcmi_start_capture(struct stm32_dcmi * dcmi,struct dcmi_buf * buf)359 static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf)
360 {
361 int ret;
362
363 if (!buf)
364 return -EINVAL;
365
366 ret = dcmi_start_dma(dcmi, buf);
367 if (ret) {
368 dcmi->errors_count++;
369 return ret;
370 }
371
372 /* Enable capture */
373 reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE);
374
375 return 0;
376 }
377
dcmi_set_crop(struct stm32_dcmi * dcmi)378 static void dcmi_set_crop(struct stm32_dcmi *dcmi)
379 {
380 u32 size, start;
381
382 /* Crop resolution */
383 size = ((dcmi->crop.height - 1) << 16) |
384 ((dcmi->crop.width << 1) - 1);
385 reg_write(dcmi->regs, DCMI_CWSIZE, size);
386
387 /* Crop start point */
388 start = ((dcmi->crop.top) << 16) |
389 ((dcmi->crop.left << 1));
390 reg_write(dcmi->regs, DCMI_CWSTRT, start);
391
392 dev_dbg(dcmi->dev, "Cropping to %ux%u@%u:%u\n",
393 dcmi->crop.width, dcmi->crop.height,
394 dcmi->crop.left, dcmi->crop.top);
395
396 /* Enable crop */
397 reg_set(dcmi->regs, DCMI_CR, CR_CROP);
398 }
399
dcmi_process_jpeg(struct stm32_dcmi * dcmi)400 static void dcmi_process_jpeg(struct stm32_dcmi *dcmi)
401 {
402 struct dma_tx_state state;
403 enum dma_status status;
404 struct dcmi_buf *buf = dcmi->active;
405
406 if (!buf)
407 return;
408
409 /*
410 * Because of variable JPEG buffer size sent by sensor,
411 * DMA transfer never completes due to transfer size never reached.
412 * In order to ensure that all the JPEG data are transferred
413 * in active buffer memory, DMA is drained.
414 * Then DMA tx status gives the amount of data transferred
415 * to memory, which is then returned to V4L2 through the active
416 * buffer payload.
417 */
418
419 /* Drain DMA */
420 dmaengine_synchronize(dcmi->dma_chan);
421
422 /* Get DMA residue to get JPEG size */
423 status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state);
424 if (status != DMA_ERROR && state.residue < buf->size) {
425 /* Return JPEG buffer to V4L2 with received JPEG buffer size */
426 dcmi_buffer_done(dcmi, buf, buf->size - state.residue, 0);
427 } else {
428 dcmi->errors_count++;
429 dev_err(dcmi->dev, "%s: Cannot get JPEG size from DMA\n",
430 __func__);
431 /* Return JPEG buffer to V4L2 in ERROR state */
432 dcmi_buffer_done(dcmi, buf, 0, -EIO);
433 }
434
435 /* Abort DMA operation */
436 dmaengine_terminate_sync(dcmi->dma_chan);
437
438 /* Restart capture */
439 if (dcmi_restart_capture(dcmi))
440 dev_err(dcmi->dev, "%s: Cannot restart capture on JPEG received\n",
441 __func__);
442 }
443
dcmi_irq_thread(int irq,void * arg)444 static irqreturn_t dcmi_irq_thread(int irq, void *arg)
445 {
446 struct stm32_dcmi *dcmi = arg;
447
448 spin_lock_irq(&dcmi->irqlock);
449
450 if (dcmi->misr & IT_OVR) {
451 dcmi->overrun_count++;
452 if (dcmi->overrun_count > OVERRUN_ERROR_THRESHOLD)
453 dcmi->errors_count++;
454 }
455 if (dcmi->misr & IT_ERR)
456 dcmi->errors_count++;
457
458 if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG &&
459 dcmi->misr & IT_FRAME) {
460 /* JPEG received */
461 spin_unlock_irq(&dcmi->irqlock);
462 dcmi_process_jpeg(dcmi);
463 return IRQ_HANDLED;
464 }
465
466 spin_unlock_irq(&dcmi->irqlock);
467 return IRQ_HANDLED;
468 }
469
dcmi_irq_callback(int irq,void * arg)470 static irqreturn_t dcmi_irq_callback(int irq, void *arg)
471 {
472 struct stm32_dcmi *dcmi = arg;
473 unsigned long flags;
474
475 spin_lock_irqsave(&dcmi->irqlock, flags);
476
477 dcmi->misr = reg_read(dcmi->regs, DCMI_MIS);
478
479 /* Clear interrupt */
480 reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR);
481
482 spin_unlock_irqrestore(&dcmi->irqlock, flags);
483
484 return IRQ_WAKE_THREAD;
485 }
486
dcmi_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])487 static int dcmi_queue_setup(struct vb2_queue *vq,
488 unsigned int *nbuffers,
489 unsigned int *nplanes,
490 unsigned int sizes[],
491 struct device *alloc_devs[])
492 {
493 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
494 unsigned int size;
495
496 size = dcmi->fmt.fmt.pix.sizeimage;
497
498 /* Make sure the image size is large enough */
499 if (*nplanes)
500 return sizes[0] < size ? -EINVAL : 0;
501
502 *nplanes = 1;
503 sizes[0] = size;
504
505 dev_dbg(dcmi->dev, "Setup queue, count=%d, size=%d\n",
506 *nbuffers, size);
507
508 return 0;
509 }
510
dcmi_buf_init(struct vb2_buffer * vb)511 static int dcmi_buf_init(struct vb2_buffer *vb)
512 {
513 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
514 struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
515
516 INIT_LIST_HEAD(&buf->list);
517
518 return 0;
519 }
520
dcmi_buf_prepare(struct vb2_buffer * vb)521 static int dcmi_buf_prepare(struct vb2_buffer *vb)
522 {
523 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
524 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
525 struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
526 unsigned long size;
527
528 size = dcmi->fmt.fmt.pix.sizeimage;
529
530 if (vb2_plane_size(vb, 0) < size) {
531 dev_err(dcmi->dev, "%s data will not fit into plane (%lu < %lu)\n",
532 __func__, vb2_plane_size(vb, 0), size);
533 return -EINVAL;
534 }
535
536 vb2_set_plane_payload(vb, 0, size);
537
538 if (!buf->prepared) {
539 /* Get memory addresses */
540 buf->paddr =
541 vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
542 buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0);
543 buf->prepared = true;
544
545 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size);
546
547 dev_dbg(dcmi->dev, "buffer[%d] phy=%pad size=%zu\n",
548 vb->index, &buf->paddr, buf->size);
549 }
550
551 return 0;
552 }
553
dcmi_buf_queue(struct vb2_buffer * vb)554 static void dcmi_buf_queue(struct vb2_buffer *vb)
555 {
556 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
557 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
558 struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
559
560 spin_lock_irq(&dcmi->irqlock);
561
562 /* Enqueue to video buffers list */
563 list_add_tail(&buf->list, &dcmi->buffers);
564
565 if (dcmi->state == WAIT_FOR_BUFFER) {
566 dcmi->state = RUNNING;
567 dcmi->active = buf;
568
569 dev_dbg(dcmi->dev, "Starting capture on buffer[%d] queued\n",
570 buf->vb.vb2_buf.index);
571
572 spin_unlock_irq(&dcmi->irqlock);
573 if (dcmi_start_capture(dcmi, buf))
574 dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n",
575 __func__);
576 return;
577 }
578
579 spin_unlock_irq(&dcmi->irqlock);
580 }
581
dcmi_find_source(struct stm32_dcmi * dcmi)582 static struct media_entity *dcmi_find_source(struct stm32_dcmi *dcmi)
583 {
584 struct media_entity *entity = &dcmi->vdev->entity;
585 struct media_pad *pad;
586
587 /* Walk searching for entity having no sink */
588 while (1) {
589 pad = &entity->pads[0];
590 if (!(pad->flags & MEDIA_PAD_FL_SINK))
591 break;
592
593 pad = media_entity_remote_pad(pad);
594 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
595 break;
596
597 entity = pad->entity;
598 }
599
600 return entity;
601 }
602
dcmi_pipeline_s_fmt(struct stm32_dcmi * dcmi,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)603 static int dcmi_pipeline_s_fmt(struct stm32_dcmi *dcmi,
604 struct v4l2_subdev_state *sd_state,
605 struct v4l2_subdev_format *format)
606 {
607 struct media_entity *entity = &dcmi->source->entity;
608 struct v4l2_subdev *subdev;
609 struct media_pad *sink_pad = NULL;
610 struct media_pad *src_pad = NULL;
611 struct media_pad *pad = NULL;
612 struct v4l2_subdev_format fmt = *format;
613 bool found = false;
614 int ret;
615
616 /*
617 * Starting from sensor subdevice, walk within
618 * pipeline and set format on each subdevice
619 */
620 while (1) {
621 unsigned int i;
622
623 /* Search if current entity has a source pad */
624 for (i = 0; i < entity->num_pads; i++) {
625 pad = &entity->pads[i];
626 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
627 src_pad = pad;
628 found = true;
629 break;
630 }
631 }
632 if (!found)
633 break;
634
635 subdev = media_entity_to_v4l2_subdev(entity);
636
637 /* Propagate format on sink pad if any, otherwise source pad */
638 if (sink_pad)
639 pad = sink_pad;
640
641 dev_dbg(dcmi->dev, "\"%s\":%d pad format set to 0x%x %ux%u\n",
642 subdev->name, pad->index, format->format.code,
643 format->format.width, format->format.height);
644
645 fmt.pad = pad->index;
646 ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt);
647 if (ret < 0) {
648 dev_err(dcmi->dev, "%s: Failed to set format 0x%x %ux%u on \"%s\":%d pad (%d)\n",
649 __func__, format->format.code,
650 format->format.width, format->format.height,
651 subdev->name, pad->index, ret);
652 return ret;
653 }
654
655 if (fmt.format.code != format->format.code ||
656 fmt.format.width != format->format.width ||
657 fmt.format.height != format->format.height) {
658 dev_dbg(dcmi->dev, "\"%s\":%d pad format has been changed to 0x%x %ux%u\n",
659 subdev->name, pad->index, fmt.format.code,
660 fmt.format.width, fmt.format.height);
661 }
662
663 /* Walk to next entity */
664 sink_pad = media_entity_remote_pad(src_pad);
665 if (!sink_pad || !is_media_entity_v4l2_subdev(sink_pad->entity))
666 break;
667
668 entity = sink_pad->entity;
669 }
670 *format = fmt;
671
672 return 0;
673 }
674
dcmi_pipeline_s_stream(struct stm32_dcmi * dcmi,int state)675 static int dcmi_pipeline_s_stream(struct stm32_dcmi *dcmi, int state)
676 {
677 struct media_entity *entity = &dcmi->vdev->entity;
678 struct v4l2_subdev *subdev;
679 struct media_pad *pad;
680 int ret;
681
682 /* Start/stop all entities within pipeline */
683 while (1) {
684 pad = &entity->pads[0];
685 if (!(pad->flags & MEDIA_PAD_FL_SINK))
686 break;
687
688 pad = media_entity_remote_pad(pad);
689 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
690 break;
691
692 entity = pad->entity;
693 subdev = media_entity_to_v4l2_subdev(entity);
694
695 ret = v4l2_subdev_call(subdev, video, s_stream, state);
696 if (ret < 0 && ret != -ENOIOCTLCMD) {
697 dev_err(dcmi->dev, "%s: \"%s\" failed to %s streaming (%d)\n",
698 __func__, subdev->name,
699 state ? "start" : "stop", ret);
700 return ret;
701 }
702
703 dev_dbg(dcmi->dev, "\"%s\" is %s\n",
704 subdev->name, state ? "started" : "stopped");
705 }
706
707 return 0;
708 }
709
dcmi_pipeline_start(struct stm32_dcmi * dcmi)710 static int dcmi_pipeline_start(struct stm32_dcmi *dcmi)
711 {
712 return dcmi_pipeline_s_stream(dcmi, 1);
713 }
714
dcmi_pipeline_stop(struct stm32_dcmi * dcmi)715 static void dcmi_pipeline_stop(struct stm32_dcmi *dcmi)
716 {
717 dcmi_pipeline_s_stream(dcmi, 0);
718 }
719
dcmi_start_streaming(struct vb2_queue * vq,unsigned int count)720 static int dcmi_start_streaming(struct vb2_queue *vq, unsigned int count)
721 {
722 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
723 struct dcmi_buf *buf, *node;
724 u32 val = 0;
725 int ret;
726
727 ret = pm_runtime_resume_and_get(dcmi->dev);
728 if (ret < 0) {
729 dev_err(dcmi->dev, "%s: Failed to start streaming, cannot get sync (%d)\n",
730 __func__, ret);
731 goto err_unlocked;
732 }
733
734 ret = media_pipeline_start(&dcmi->vdev->entity, &dcmi->pipeline);
735 if (ret < 0) {
736 dev_err(dcmi->dev, "%s: Failed to start streaming, media pipeline start error (%d)\n",
737 __func__, ret);
738 goto err_pm_put;
739 }
740
741 ret = dcmi_pipeline_start(dcmi);
742 if (ret)
743 goto err_media_pipeline_stop;
744
745 spin_lock_irq(&dcmi->irqlock);
746
747 /* Set bus width */
748 switch (dcmi->bus.bus_width) {
749 case 14:
750 val |= CR_EDM_0 | CR_EDM_1;
751 break;
752 case 12:
753 val |= CR_EDM_1;
754 break;
755 case 10:
756 val |= CR_EDM_0;
757 break;
758 default:
759 /* Set bus width to 8 bits by default */
760 break;
761 }
762
763 /* Set vertical synchronization polarity */
764 if (dcmi->bus.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
765 val |= CR_VSPOL;
766
767 /* Set horizontal synchronization polarity */
768 if (dcmi->bus.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
769 val |= CR_HSPOL;
770
771 /* Set pixel clock polarity */
772 if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
773 val |= CR_PCKPOL;
774
775 /*
776 * BT656 embedded synchronisation bus mode.
777 *
778 * Default SAV/EAV mode is supported here with default codes
779 * SAV=0xff000080 & EAV=0xff00009d.
780 * With DCMI this means LSC=SAV=0x80 & LEC=EAV=0x9d.
781 */
782 if (dcmi->bus_type == V4L2_MBUS_BT656) {
783 val |= CR_ESS;
784
785 /* Unmask all codes */
786 reg_write(dcmi->regs, DCMI_ESUR, 0xffffffff);/* FEC:LEC:LSC:FSC */
787
788 /* Trig on LSC=0x80 & LEC=0x9d codes, ignore FSC and FEC */
789 reg_write(dcmi->regs, DCMI_ESCR, 0xff9d80ff);/* FEC:LEC:LSC:FSC */
790 }
791
792 reg_write(dcmi->regs, DCMI_CR, val);
793
794 /* Set crop */
795 if (dcmi->do_crop)
796 dcmi_set_crop(dcmi);
797
798 /* Enable jpeg capture */
799 if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG)
800 reg_set(dcmi->regs, DCMI_CR, CR_CM);/* Snapshot mode */
801
802 /* Enable dcmi */
803 reg_set(dcmi->regs, DCMI_CR, CR_ENABLE);
804
805 dcmi->sequence = 0;
806 dcmi->errors_count = 0;
807 dcmi->overrun_count = 0;
808 dcmi->buffers_count = 0;
809
810 /*
811 * Start transfer if at least one buffer has been queued,
812 * otherwise transfer is deferred at buffer queueing
813 */
814 if (list_empty(&dcmi->buffers)) {
815 dev_dbg(dcmi->dev, "Start streaming is deferred to next buffer queueing\n");
816 dcmi->state = WAIT_FOR_BUFFER;
817 spin_unlock_irq(&dcmi->irqlock);
818 return 0;
819 }
820
821 buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
822 dcmi->active = buf;
823
824 dcmi->state = RUNNING;
825
826 dev_dbg(dcmi->dev, "Start streaming, starting capture\n");
827
828 spin_unlock_irq(&dcmi->irqlock);
829 ret = dcmi_start_capture(dcmi, buf);
830 if (ret) {
831 dev_err(dcmi->dev, "%s: Start streaming failed, cannot start capture\n",
832 __func__);
833 goto err_pipeline_stop;
834 }
835
836 /* Enable interruptions */
837 if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG)
838 reg_set(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
839 else
840 reg_set(dcmi->regs, DCMI_IER, IT_OVR | IT_ERR);
841
842 return 0;
843
844 err_pipeline_stop:
845 dcmi_pipeline_stop(dcmi);
846
847 err_media_pipeline_stop:
848 media_pipeline_stop(&dcmi->vdev->entity);
849
850 err_pm_put:
851 pm_runtime_put(dcmi->dev);
852 err_unlocked:
853 spin_lock_irq(&dcmi->irqlock);
854 /*
855 * Return all buffers to vb2 in QUEUED state.
856 * This will give ownership back to userspace
857 */
858 list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
859 list_del_init(&buf->list);
860 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
861 }
862 dcmi->active = NULL;
863 spin_unlock_irq(&dcmi->irqlock);
864
865 return ret;
866 }
867
dcmi_stop_streaming(struct vb2_queue * vq)868 static void dcmi_stop_streaming(struct vb2_queue *vq)
869 {
870 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
871 struct dcmi_buf *buf, *node;
872
873 dcmi_pipeline_stop(dcmi);
874
875 media_pipeline_stop(&dcmi->vdev->entity);
876
877 spin_lock_irq(&dcmi->irqlock);
878
879 /* Disable interruptions */
880 reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
881
882 /* Disable DCMI */
883 reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE);
884
885 /* Return all queued buffers to vb2 in ERROR state */
886 list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
887 list_del_init(&buf->list);
888 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
889 }
890
891 dcmi->active = NULL;
892 dcmi->state = STOPPED;
893
894 spin_unlock_irq(&dcmi->irqlock);
895
896 /* Stop all pending DMA operations */
897 mutex_lock(&dcmi->dma_lock);
898 dmaengine_terminate_sync(dcmi->dma_chan);
899 mutex_unlock(&dcmi->dma_lock);
900
901 pm_runtime_put(dcmi->dev);
902
903 if (dcmi->errors_count)
904 dev_warn(dcmi->dev, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n",
905 dcmi->errors_count, dcmi->overrun_count,
906 dcmi->buffers_count);
907 dev_dbg(dcmi->dev, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n",
908 dcmi->errors_count, dcmi->overrun_count,
909 dcmi->buffers_count);
910 }
911
912 static const struct vb2_ops dcmi_video_qops = {
913 .queue_setup = dcmi_queue_setup,
914 .buf_init = dcmi_buf_init,
915 .buf_prepare = dcmi_buf_prepare,
916 .buf_queue = dcmi_buf_queue,
917 .start_streaming = dcmi_start_streaming,
918 .stop_streaming = dcmi_stop_streaming,
919 .wait_prepare = vb2_ops_wait_prepare,
920 .wait_finish = vb2_ops_wait_finish,
921 };
922
dcmi_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * fmt)923 static int dcmi_g_fmt_vid_cap(struct file *file, void *priv,
924 struct v4l2_format *fmt)
925 {
926 struct stm32_dcmi *dcmi = video_drvdata(file);
927
928 *fmt = dcmi->fmt;
929
930 return 0;
931 }
932
find_format_by_fourcc(struct stm32_dcmi * dcmi,unsigned int fourcc)933 static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi,
934 unsigned int fourcc)
935 {
936 unsigned int num_formats = dcmi->num_of_sd_formats;
937 const struct dcmi_format *fmt;
938 unsigned int i;
939
940 for (i = 0; i < num_formats; i++) {
941 fmt = dcmi->sd_formats[i];
942 if (fmt->fourcc == fourcc)
943 return fmt;
944 }
945
946 return NULL;
947 }
948
__find_outer_frame_size(struct stm32_dcmi * dcmi,struct v4l2_pix_format * pix,struct dcmi_framesize * framesize)949 static void __find_outer_frame_size(struct stm32_dcmi *dcmi,
950 struct v4l2_pix_format *pix,
951 struct dcmi_framesize *framesize)
952 {
953 struct dcmi_framesize *match = NULL;
954 unsigned int i;
955 unsigned int min_err = UINT_MAX;
956
957 for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
958 struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
959 int w_err = (fsize->width - pix->width);
960 int h_err = (fsize->height - pix->height);
961 int err = w_err + h_err;
962
963 if (w_err >= 0 && h_err >= 0 && err < min_err) {
964 min_err = err;
965 match = fsize;
966 }
967 }
968 if (!match)
969 match = &dcmi->sd_framesizes[0];
970
971 *framesize = *match;
972 }
973
dcmi_try_fmt(struct stm32_dcmi * dcmi,struct v4l2_format * f,const struct dcmi_format ** sd_format,struct dcmi_framesize * sd_framesize)974 static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f,
975 const struct dcmi_format **sd_format,
976 struct dcmi_framesize *sd_framesize)
977 {
978 const struct dcmi_format *sd_fmt;
979 struct dcmi_framesize sd_fsize;
980 struct v4l2_pix_format *pix = &f->fmt.pix;
981 struct v4l2_subdev_pad_config pad_cfg;
982 struct v4l2_subdev_state pad_state = {
983 .pads = &pad_cfg
984 };
985 struct v4l2_subdev_format format = {
986 .which = V4L2_SUBDEV_FORMAT_TRY,
987 };
988 bool do_crop;
989 int ret;
990
991 sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
992 if (!sd_fmt) {
993 if (!dcmi->num_of_sd_formats)
994 return -ENODATA;
995
996 sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
997 pix->pixelformat = sd_fmt->fourcc;
998 }
999
1000 /* Limit to hardware capabilities */
1001 pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH);
1002 pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT);
1003
1004 /* No crop if JPEG is requested */
1005 do_crop = dcmi->do_crop && (pix->pixelformat != V4L2_PIX_FMT_JPEG);
1006
1007 if (do_crop && dcmi->num_of_sd_framesizes) {
1008 struct dcmi_framesize outer_sd_fsize;
1009 /*
1010 * If crop is requested and sensor have discrete frame sizes,
1011 * select the frame size that is just larger than request
1012 */
1013 __find_outer_frame_size(dcmi, pix, &outer_sd_fsize);
1014 pix->width = outer_sd_fsize.width;
1015 pix->height = outer_sd_fsize.height;
1016 }
1017
1018 v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
1019 ret = v4l2_subdev_call(dcmi->source, pad, set_fmt,
1020 &pad_state, &format);
1021 if (ret < 0)
1022 return ret;
1023
1024 /* Update pix regarding to what sensor can do */
1025 v4l2_fill_pix_format(pix, &format.format);
1026
1027 /* Save resolution that sensor can actually do */
1028 sd_fsize.width = pix->width;
1029 sd_fsize.height = pix->height;
1030
1031 if (do_crop) {
1032 struct v4l2_rect c = dcmi->crop;
1033 struct v4l2_rect max_rect;
1034
1035 /*
1036 * Adjust crop by making the intersection between
1037 * format resolution request and crop request
1038 */
1039 max_rect.top = 0;
1040 max_rect.left = 0;
1041 max_rect.width = pix->width;
1042 max_rect.height = pix->height;
1043 v4l2_rect_map_inside(&c, &max_rect);
1044 c.top = clamp_t(s32, c.top, 0, pix->height - c.height);
1045 c.left = clamp_t(s32, c.left, 0, pix->width - c.width);
1046 dcmi->crop = c;
1047
1048 /* Adjust format resolution request to crop */
1049 pix->width = dcmi->crop.width;
1050 pix->height = dcmi->crop.height;
1051 }
1052
1053 pix->field = V4L2_FIELD_NONE;
1054 pix->bytesperline = pix->width * sd_fmt->bpp;
1055 pix->sizeimage = pix->bytesperline * pix->height;
1056
1057 if (sd_format)
1058 *sd_format = sd_fmt;
1059 if (sd_framesize)
1060 *sd_framesize = sd_fsize;
1061
1062 return 0;
1063 }
1064
dcmi_set_fmt(struct stm32_dcmi * dcmi,struct v4l2_format * f)1065 static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f)
1066 {
1067 struct v4l2_subdev_format format = {
1068 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1069 };
1070 const struct dcmi_format *sd_format;
1071 struct dcmi_framesize sd_framesize;
1072 struct v4l2_mbus_framefmt *mf = &format.format;
1073 struct v4l2_pix_format *pix = &f->fmt.pix;
1074 int ret;
1075
1076 /*
1077 * Try format, fmt.width/height could have been changed
1078 * to match sensor capability or crop request
1079 * sd_format & sd_framesize will contain what subdev
1080 * can do for this request.
1081 */
1082 ret = dcmi_try_fmt(dcmi, f, &sd_format, &sd_framesize);
1083 if (ret)
1084 return ret;
1085
1086 /* Disable crop if JPEG is requested or BT656 bus is selected */
1087 if (pix->pixelformat == V4L2_PIX_FMT_JPEG &&
1088 dcmi->bus_type != V4L2_MBUS_BT656)
1089 dcmi->do_crop = false;
1090
1091 /* pix to mbus format */
1092 v4l2_fill_mbus_format(mf, pix,
1093 sd_format->mbus_code);
1094 mf->width = sd_framesize.width;
1095 mf->height = sd_framesize.height;
1096
1097 ret = dcmi_pipeline_s_fmt(dcmi, NULL, &format);
1098 if (ret < 0)
1099 return ret;
1100
1101 dev_dbg(dcmi->dev, "Sensor format set to 0x%x %ux%u\n",
1102 mf->code, mf->width, mf->height);
1103 dev_dbg(dcmi->dev, "Buffer format set to %4.4s %ux%u\n",
1104 (char *)&pix->pixelformat,
1105 pix->width, pix->height);
1106
1107 dcmi->fmt = *f;
1108 dcmi->sd_format = sd_format;
1109 dcmi->sd_framesize = sd_framesize;
1110
1111 return 0;
1112 }
1113
dcmi_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1114 static int dcmi_s_fmt_vid_cap(struct file *file, void *priv,
1115 struct v4l2_format *f)
1116 {
1117 struct stm32_dcmi *dcmi = video_drvdata(file);
1118
1119 if (vb2_is_streaming(&dcmi->queue))
1120 return -EBUSY;
1121
1122 return dcmi_set_fmt(dcmi, f);
1123 }
1124
dcmi_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1125 static int dcmi_try_fmt_vid_cap(struct file *file, void *priv,
1126 struct v4l2_format *f)
1127 {
1128 struct stm32_dcmi *dcmi = video_drvdata(file);
1129
1130 return dcmi_try_fmt(dcmi, f, NULL, NULL);
1131 }
1132
dcmi_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)1133 static int dcmi_enum_fmt_vid_cap(struct file *file, void *priv,
1134 struct v4l2_fmtdesc *f)
1135 {
1136 struct stm32_dcmi *dcmi = video_drvdata(file);
1137
1138 if (f->index >= dcmi->num_of_sd_formats)
1139 return -EINVAL;
1140
1141 f->pixelformat = dcmi->sd_formats[f->index]->fourcc;
1142 return 0;
1143 }
1144
dcmi_get_sensor_format(struct stm32_dcmi * dcmi,struct v4l2_pix_format * pix)1145 static int dcmi_get_sensor_format(struct stm32_dcmi *dcmi,
1146 struct v4l2_pix_format *pix)
1147 {
1148 struct v4l2_subdev_format fmt = {
1149 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1150 };
1151 int ret;
1152
1153 ret = v4l2_subdev_call(dcmi->source, pad, get_fmt, NULL, &fmt);
1154 if (ret)
1155 return ret;
1156
1157 v4l2_fill_pix_format(pix, &fmt.format);
1158
1159 return 0;
1160 }
1161
dcmi_set_sensor_format(struct stm32_dcmi * dcmi,struct v4l2_pix_format * pix)1162 static int dcmi_set_sensor_format(struct stm32_dcmi *dcmi,
1163 struct v4l2_pix_format *pix)
1164 {
1165 const struct dcmi_format *sd_fmt;
1166 struct v4l2_subdev_format format = {
1167 .which = V4L2_SUBDEV_FORMAT_TRY,
1168 };
1169 struct v4l2_subdev_pad_config pad_cfg;
1170 struct v4l2_subdev_state pad_state = {
1171 .pads = &pad_cfg
1172 };
1173 int ret;
1174
1175 sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
1176 if (!sd_fmt) {
1177 if (!dcmi->num_of_sd_formats)
1178 return -ENODATA;
1179
1180 sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
1181 pix->pixelformat = sd_fmt->fourcc;
1182 }
1183
1184 v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
1185 ret = v4l2_subdev_call(dcmi->source, pad, set_fmt,
1186 &pad_state, &format);
1187 if (ret < 0)
1188 return ret;
1189
1190 return 0;
1191 }
1192
dcmi_get_sensor_bounds(struct stm32_dcmi * dcmi,struct v4l2_rect * r)1193 static int dcmi_get_sensor_bounds(struct stm32_dcmi *dcmi,
1194 struct v4l2_rect *r)
1195 {
1196 struct v4l2_subdev_selection bounds = {
1197 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1198 .target = V4L2_SEL_TGT_CROP_BOUNDS,
1199 };
1200 unsigned int max_width, max_height, max_pixsize;
1201 struct v4l2_pix_format pix;
1202 unsigned int i;
1203 int ret;
1204
1205 /*
1206 * Get sensor bounds first
1207 */
1208 ret = v4l2_subdev_call(dcmi->source, pad, get_selection,
1209 NULL, &bounds);
1210 if (!ret)
1211 *r = bounds.r;
1212 if (ret != -ENOIOCTLCMD)
1213 return ret;
1214
1215 /*
1216 * If selection is not implemented,
1217 * fallback by enumerating sensor frame sizes
1218 * and take the largest one
1219 */
1220 max_width = 0;
1221 max_height = 0;
1222 max_pixsize = 0;
1223 for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
1224 struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
1225 unsigned int pixsize = fsize->width * fsize->height;
1226
1227 if (pixsize > max_pixsize) {
1228 max_pixsize = pixsize;
1229 max_width = fsize->width;
1230 max_height = fsize->height;
1231 }
1232 }
1233 if (max_pixsize > 0) {
1234 r->top = 0;
1235 r->left = 0;
1236 r->width = max_width;
1237 r->height = max_height;
1238 return 0;
1239 }
1240
1241 /*
1242 * If frame sizes enumeration is not implemented,
1243 * fallback by getting current sensor frame size
1244 */
1245 ret = dcmi_get_sensor_format(dcmi, &pix);
1246 if (ret)
1247 return ret;
1248
1249 r->top = 0;
1250 r->left = 0;
1251 r->width = pix.width;
1252 r->height = pix.height;
1253
1254 return 0;
1255 }
1256
dcmi_g_selection(struct file * file,void * fh,struct v4l2_selection * s)1257 static int dcmi_g_selection(struct file *file, void *fh,
1258 struct v4l2_selection *s)
1259 {
1260 struct stm32_dcmi *dcmi = video_drvdata(file);
1261
1262 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1263 return -EINVAL;
1264
1265 switch (s->target) {
1266 case V4L2_SEL_TGT_CROP_DEFAULT:
1267 case V4L2_SEL_TGT_CROP_BOUNDS:
1268 s->r = dcmi->sd_bounds;
1269 return 0;
1270 case V4L2_SEL_TGT_CROP:
1271 if (dcmi->do_crop) {
1272 s->r = dcmi->crop;
1273 } else {
1274 s->r.top = 0;
1275 s->r.left = 0;
1276 s->r.width = dcmi->fmt.fmt.pix.width;
1277 s->r.height = dcmi->fmt.fmt.pix.height;
1278 }
1279 break;
1280 default:
1281 return -EINVAL;
1282 }
1283
1284 return 0;
1285 }
1286
dcmi_s_selection(struct file * file,void * priv,struct v4l2_selection * s)1287 static int dcmi_s_selection(struct file *file, void *priv,
1288 struct v4l2_selection *s)
1289 {
1290 struct stm32_dcmi *dcmi = video_drvdata(file);
1291 struct v4l2_rect r = s->r;
1292 struct v4l2_rect max_rect;
1293 struct v4l2_pix_format pix;
1294
1295 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1296 s->target != V4L2_SEL_TGT_CROP)
1297 return -EINVAL;
1298
1299 /* Reset sensor resolution to max resolution */
1300 pix.pixelformat = dcmi->fmt.fmt.pix.pixelformat;
1301 pix.width = dcmi->sd_bounds.width;
1302 pix.height = dcmi->sd_bounds.height;
1303 dcmi_set_sensor_format(dcmi, &pix);
1304
1305 /*
1306 * Make the intersection between
1307 * sensor resolution
1308 * and crop request
1309 */
1310 max_rect.top = 0;
1311 max_rect.left = 0;
1312 max_rect.width = pix.width;
1313 max_rect.height = pix.height;
1314 v4l2_rect_map_inside(&r, &max_rect);
1315 r.top = clamp_t(s32, r.top, 0, pix.height - r.height);
1316 r.left = clamp_t(s32, r.left, 0, pix.width - r.width);
1317
1318 if (!(r.top == dcmi->sd_bounds.top &&
1319 r.left == dcmi->sd_bounds.left &&
1320 r.width == dcmi->sd_bounds.width &&
1321 r.height == dcmi->sd_bounds.height)) {
1322 /* Crop if request is different than sensor resolution */
1323 dcmi->do_crop = true;
1324 dcmi->crop = r;
1325 dev_dbg(dcmi->dev, "s_selection: crop %ux%u@(%u,%u) from %ux%u\n",
1326 r.width, r.height, r.left, r.top,
1327 pix.width, pix.height);
1328 } else {
1329 /* Disable crop */
1330 dcmi->do_crop = false;
1331 dev_dbg(dcmi->dev, "s_selection: crop is disabled\n");
1332 }
1333
1334 s->r = r;
1335 return 0;
1336 }
1337
dcmi_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1338 static int dcmi_querycap(struct file *file, void *priv,
1339 struct v4l2_capability *cap)
1340 {
1341 strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
1342 strscpy(cap->card, "STM32 Camera Memory Interface",
1343 sizeof(cap->card));
1344 strscpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info));
1345 return 0;
1346 }
1347
dcmi_enum_input(struct file * file,void * priv,struct v4l2_input * i)1348 static int dcmi_enum_input(struct file *file, void *priv,
1349 struct v4l2_input *i)
1350 {
1351 if (i->index != 0)
1352 return -EINVAL;
1353
1354 i->type = V4L2_INPUT_TYPE_CAMERA;
1355 strscpy(i->name, "Camera", sizeof(i->name));
1356 return 0;
1357 }
1358
dcmi_g_input(struct file * file,void * priv,unsigned int * i)1359 static int dcmi_g_input(struct file *file, void *priv, unsigned int *i)
1360 {
1361 *i = 0;
1362 return 0;
1363 }
1364
dcmi_s_input(struct file * file,void * priv,unsigned int i)1365 static int dcmi_s_input(struct file *file, void *priv, unsigned int i)
1366 {
1367 if (i > 0)
1368 return -EINVAL;
1369 return 0;
1370 }
1371
dcmi_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)1372 static int dcmi_enum_framesizes(struct file *file, void *fh,
1373 struct v4l2_frmsizeenum *fsize)
1374 {
1375 struct stm32_dcmi *dcmi = video_drvdata(file);
1376 const struct dcmi_format *sd_fmt;
1377 struct v4l2_subdev_frame_size_enum fse = {
1378 .index = fsize->index,
1379 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1380 };
1381 int ret;
1382
1383 sd_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format);
1384 if (!sd_fmt)
1385 return -EINVAL;
1386
1387 fse.code = sd_fmt->mbus_code;
1388
1389 ret = v4l2_subdev_call(dcmi->source, pad, enum_frame_size,
1390 NULL, &fse);
1391 if (ret)
1392 return ret;
1393
1394 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1395 fsize->discrete.width = fse.max_width;
1396 fsize->discrete.height = fse.max_height;
1397
1398 return 0;
1399 }
1400
dcmi_g_parm(struct file * file,void * priv,struct v4l2_streamparm * p)1401 static int dcmi_g_parm(struct file *file, void *priv,
1402 struct v4l2_streamparm *p)
1403 {
1404 struct stm32_dcmi *dcmi = video_drvdata(file);
1405
1406 return v4l2_g_parm_cap(video_devdata(file), dcmi->source, p);
1407 }
1408
dcmi_s_parm(struct file * file,void * priv,struct v4l2_streamparm * p)1409 static int dcmi_s_parm(struct file *file, void *priv,
1410 struct v4l2_streamparm *p)
1411 {
1412 struct stm32_dcmi *dcmi = video_drvdata(file);
1413
1414 return v4l2_s_parm_cap(video_devdata(file), dcmi->source, p);
1415 }
1416
dcmi_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * fival)1417 static int dcmi_enum_frameintervals(struct file *file, void *fh,
1418 struct v4l2_frmivalenum *fival)
1419 {
1420 struct stm32_dcmi *dcmi = video_drvdata(file);
1421 const struct dcmi_format *sd_fmt;
1422 struct v4l2_subdev_frame_interval_enum fie = {
1423 .index = fival->index,
1424 .width = fival->width,
1425 .height = fival->height,
1426 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1427 };
1428 int ret;
1429
1430 sd_fmt = find_format_by_fourcc(dcmi, fival->pixel_format);
1431 if (!sd_fmt)
1432 return -EINVAL;
1433
1434 fie.code = sd_fmt->mbus_code;
1435
1436 ret = v4l2_subdev_call(dcmi->source, pad,
1437 enum_frame_interval, NULL, &fie);
1438 if (ret)
1439 return ret;
1440
1441 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1442 fival->discrete = fie.interval;
1443
1444 return 0;
1445 }
1446
1447 static const struct of_device_id stm32_dcmi_of_match[] = {
1448 { .compatible = "st,stm32-dcmi"},
1449 { /* end node */ },
1450 };
1451 MODULE_DEVICE_TABLE(of, stm32_dcmi_of_match);
1452
dcmi_open(struct file * file)1453 static int dcmi_open(struct file *file)
1454 {
1455 struct stm32_dcmi *dcmi = video_drvdata(file);
1456 struct v4l2_subdev *sd = dcmi->source;
1457 int ret;
1458
1459 if (mutex_lock_interruptible(&dcmi->lock))
1460 return -ERESTARTSYS;
1461
1462 ret = v4l2_fh_open(file);
1463 if (ret < 0)
1464 goto unlock;
1465
1466 if (!v4l2_fh_is_singular_file(file))
1467 goto fh_rel;
1468
1469 ret = v4l2_subdev_call(sd, core, s_power, 1);
1470 if (ret < 0 && ret != -ENOIOCTLCMD)
1471 goto fh_rel;
1472
1473 ret = dcmi_set_fmt(dcmi, &dcmi->fmt);
1474 if (ret)
1475 v4l2_subdev_call(sd, core, s_power, 0);
1476 fh_rel:
1477 if (ret)
1478 v4l2_fh_release(file);
1479 unlock:
1480 mutex_unlock(&dcmi->lock);
1481 return ret;
1482 }
1483
dcmi_release(struct file * file)1484 static int dcmi_release(struct file *file)
1485 {
1486 struct stm32_dcmi *dcmi = video_drvdata(file);
1487 struct v4l2_subdev *sd = dcmi->source;
1488 bool fh_singular;
1489 int ret;
1490
1491 mutex_lock(&dcmi->lock);
1492
1493 fh_singular = v4l2_fh_is_singular_file(file);
1494
1495 ret = _vb2_fop_release(file, NULL);
1496
1497 if (fh_singular)
1498 v4l2_subdev_call(sd, core, s_power, 0);
1499
1500 mutex_unlock(&dcmi->lock);
1501
1502 return ret;
1503 }
1504
1505 static const struct v4l2_ioctl_ops dcmi_ioctl_ops = {
1506 .vidioc_querycap = dcmi_querycap,
1507
1508 .vidioc_try_fmt_vid_cap = dcmi_try_fmt_vid_cap,
1509 .vidioc_g_fmt_vid_cap = dcmi_g_fmt_vid_cap,
1510 .vidioc_s_fmt_vid_cap = dcmi_s_fmt_vid_cap,
1511 .vidioc_enum_fmt_vid_cap = dcmi_enum_fmt_vid_cap,
1512 .vidioc_g_selection = dcmi_g_selection,
1513 .vidioc_s_selection = dcmi_s_selection,
1514
1515 .vidioc_enum_input = dcmi_enum_input,
1516 .vidioc_g_input = dcmi_g_input,
1517 .vidioc_s_input = dcmi_s_input,
1518
1519 .vidioc_g_parm = dcmi_g_parm,
1520 .vidioc_s_parm = dcmi_s_parm,
1521
1522 .vidioc_enum_framesizes = dcmi_enum_framesizes,
1523 .vidioc_enum_frameintervals = dcmi_enum_frameintervals,
1524
1525 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1526 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1527 .vidioc_querybuf = vb2_ioctl_querybuf,
1528 .vidioc_qbuf = vb2_ioctl_qbuf,
1529 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1530 .vidioc_expbuf = vb2_ioctl_expbuf,
1531 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1532 .vidioc_streamon = vb2_ioctl_streamon,
1533 .vidioc_streamoff = vb2_ioctl_streamoff,
1534
1535 .vidioc_log_status = v4l2_ctrl_log_status,
1536 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1537 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1538 };
1539
1540 static const struct v4l2_file_operations dcmi_fops = {
1541 .owner = THIS_MODULE,
1542 .unlocked_ioctl = video_ioctl2,
1543 .open = dcmi_open,
1544 .release = dcmi_release,
1545 .poll = vb2_fop_poll,
1546 .mmap = vb2_fop_mmap,
1547 #ifndef CONFIG_MMU
1548 .get_unmapped_area = vb2_fop_get_unmapped_area,
1549 #endif
1550 .read = vb2_fop_read,
1551 };
1552
dcmi_set_default_fmt(struct stm32_dcmi * dcmi)1553 static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi)
1554 {
1555 struct v4l2_format f = {
1556 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1557 .fmt.pix = {
1558 .width = CIF_WIDTH,
1559 .height = CIF_HEIGHT,
1560 .field = V4L2_FIELD_NONE,
1561 .pixelformat = dcmi->sd_formats[0]->fourcc,
1562 },
1563 };
1564 int ret;
1565
1566 ret = dcmi_try_fmt(dcmi, &f, NULL, NULL);
1567 if (ret)
1568 return ret;
1569 dcmi->sd_format = dcmi->sd_formats[0];
1570 dcmi->fmt = f;
1571 return 0;
1572 }
1573
1574 /*
1575 * FIXME: For the time being we only support subdevices
1576 * which expose RGB & YUV "parallel form" mbus code (_2X8).
1577 * Nevertheless, this allows to support serial source subdevices
1578 * and serial to parallel bridges which conform to this.
1579 */
1580 static const struct dcmi_format dcmi_formats[] = {
1581 {
1582 .fourcc = V4L2_PIX_FMT_RGB565,
1583 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
1584 .bpp = 2,
1585 }, {
1586 .fourcc = V4L2_PIX_FMT_YUYV,
1587 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
1588 .bpp = 2,
1589 }, {
1590 .fourcc = V4L2_PIX_FMT_UYVY,
1591 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
1592 .bpp = 2,
1593 }, {
1594 .fourcc = V4L2_PIX_FMT_JPEG,
1595 .mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
1596 .bpp = 1,
1597 }, {
1598 .fourcc = V4L2_PIX_FMT_SBGGR8,
1599 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
1600 .bpp = 1,
1601 }, {
1602 .fourcc = V4L2_PIX_FMT_SGBRG8,
1603 .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
1604 .bpp = 1,
1605 }, {
1606 .fourcc = V4L2_PIX_FMT_SGRBG8,
1607 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
1608 .bpp = 1,
1609 }, {
1610 .fourcc = V4L2_PIX_FMT_SRGGB8,
1611 .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
1612 .bpp = 1,
1613 },
1614 };
1615
dcmi_formats_init(struct stm32_dcmi * dcmi)1616 static int dcmi_formats_init(struct stm32_dcmi *dcmi)
1617 {
1618 const struct dcmi_format *sd_fmts[ARRAY_SIZE(dcmi_formats)];
1619 unsigned int num_fmts = 0, i, j;
1620 struct v4l2_subdev *subdev = dcmi->source;
1621 struct v4l2_subdev_mbus_code_enum mbus_code = {
1622 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1623 };
1624
1625 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
1626 NULL, &mbus_code)) {
1627 for (i = 0; i < ARRAY_SIZE(dcmi_formats); i++) {
1628 if (dcmi_formats[i].mbus_code != mbus_code.code)
1629 continue;
1630
1631 /* Exclude JPEG if BT656 bus is selected */
1632 if (dcmi_formats[i].fourcc == V4L2_PIX_FMT_JPEG &&
1633 dcmi->bus_type == V4L2_MBUS_BT656)
1634 continue;
1635
1636 /* Code supported, have we got this fourcc yet? */
1637 for (j = 0; j < num_fmts; j++)
1638 if (sd_fmts[j]->fourcc ==
1639 dcmi_formats[i].fourcc) {
1640 /* Already available */
1641 dev_dbg(dcmi->dev, "Skipping fourcc/code: %4.4s/0x%x\n",
1642 (char *)&sd_fmts[j]->fourcc,
1643 mbus_code.code);
1644 break;
1645 }
1646 if (j == num_fmts) {
1647 /* New */
1648 sd_fmts[num_fmts++] = dcmi_formats + i;
1649 dev_dbg(dcmi->dev, "Supported fourcc/code: %4.4s/0x%x\n",
1650 (char *)&sd_fmts[num_fmts - 1]->fourcc,
1651 sd_fmts[num_fmts - 1]->mbus_code);
1652 }
1653 }
1654 mbus_code.index++;
1655 }
1656
1657 if (!num_fmts)
1658 return -ENXIO;
1659
1660 dcmi->num_of_sd_formats = num_fmts;
1661 dcmi->sd_formats = devm_kcalloc(dcmi->dev,
1662 num_fmts, sizeof(struct dcmi_format *),
1663 GFP_KERNEL);
1664 if (!dcmi->sd_formats) {
1665 dev_err(dcmi->dev, "Could not allocate memory\n");
1666 return -ENOMEM;
1667 }
1668
1669 memcpy(dcmi->sd_formats, sd_fmts,
1670 num_fmts * sizeof(struct dcmi_format *));
1671 dcmi->sd_format = dcmi->sd_formats[0];
1672
1673 return 0;
1674 }
1675
dcmi_framesizes_init(struct stm32_dcmi * dcmi)1676 static int dcmi_framesizes_init(struct stm32_dcmi *dcmi)
1677 {
1678 unsigned int num_fsize = 0;
1679 struct v4l2_subdev *subdev = dcmi->source;
1680 struct v4l2_subdev_frame_size_enum fse = {
1681 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1682 .code = dcmi->sd_format->mbus_code,
1683 };
1684 unsigned int ret;
1685 unsigned int i;
1686
1687 /* Allocate discrete framesizes array */
1688 while (!v4l2_subdev_call(subdev, pad, enum_frame_size,
1689 NULL, &fse))
1690 fse.index++;
1691
1692 num_fsize = fse.index;
1693 if (!num_fsize)
1694 return 0;
1695
1696 dcmi->num_of_sd_framesizes = num_fsize;
1697 dcmi->sd_framesizes = devm_kcalloc(dcmi->dev, num_fsize,
1698 sizeof(struct dcmi_framesize),
1699 GFP_KERNEL);
1700 if (!dcmi->sd_framesizes) {
1701 dev_err(dcmi->dev, "Could not allocate memory\n");
1702 return -ENOMEM;
1703 }
1704
1705 /* Fill array with sensor supported framesizes */
1706 dev_dbg(dcmi->dev, "Sensor supports %u frame sizes:\n", num_fsize);
1707 for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
1708 fse.index = i;
1709 ret = v4l2_subdev_call(subdev, pad, enum_frame_size,
1710 NULL, &fse);
1711 if (ret)
1712 return ret;
1713 dcmi->sd_framesizes[fse.index].width = fse.max_width;
1714 dcmi->sd_framesizes[fse.index].height = fse.max_height;
1715 dev_dbg(dcmi->dev, "%ux%u\n", fse.max_width, fse.max_height);
1716 }
1717
1718 return 0;
1719 }
1720
dcmi_graph_notify_complete(struct v4l2_async_notifier * notifier)1721 static int dcmi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1722 {
1723 struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
1724 int ret;
1725
1726 /*
1727 * Now that the graph is complete,
1728 * we search for the source subdevice
1729 * in order to expose it through V4L2 interface
1730 */
1731 dcmi->source = media_entity_to_v4l2_subdev(dcmi_find_source(dcmi));
1732 if (!dcmi->source) {
1733 dev_err(dcmi->dev, "Source subdevice not found\n");
1734 return -ENODEV;
1735 }
1736
1737 dcmi->vdev->ctrl_handler = dcmi->source->ctrl_handler;
1738
1739 ret = dcmi_formats_init(dcmi);
1740 if (ret) {
1741 dev_err(dcmi->dev, "No supported mediabus format found\n");
1742 return ret;
1743 }
1744
1745 ret = dcmi_framesizes_init(dcmi);
1746 if (ret) {
1747 dev_err(dcmi->dev, "Could not initialize framesizes\n");
1748 return ret;
1749 }
1750
1751 ret = dcmi_get_sensor_bounds(dcmi, &dcmi->sd_bounds);
1752 if (ret) {
1753 dev_err(dcmi->dev, "Could not get sensor bounds\n");
1754 return ret;
1755 }
1756
1757 ret = dcmi_set_default_fmt(dcmi);
1758 if (ret) {
1759 dev_err(dcmi->dev, "Could not set default format\n");
1760 return ret;
1761 }
1762
1763 ret = devm_request_threaded_irq(dcmi->dev, dcmi->irq, dcmi_irq_callback,
1764 dcmi_irq_thread, IRQF_ONESHOT,
1765 dev_name(dcmi->dev), dcmi);
1766 if (ret) {
1767 dev_err(dcmi->dev, "Unable to request irq %d\n", dcmi->irq);
1768 return ret;
1769 }
1770
1771 return 0;
1772 }
1773
dcmi_graph_notify_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_subdev * asd)1774 static void dcmi_graph_notify_unbind(struct v4l2_async_notifier *notifier,
1775 struct v4l2_subdev *sd,
1776 struct v4l2_async_subdev *asd)
1777 {
1778 struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
1779
1780 dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev));
1781
1782 /* Checks internally if vdev has been init or not */
1783 video_unregister_device(dcmi->vdev);
1784 }
1785
dcmi_graph_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)1786 static int dcmi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1787 struct v4l2_subdev *subdev,
1788 struct v4l2_async_subdev *asd)
1789 {
1790 struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
1791 unsigned int ret;
1792 int src_pad;
1793
1794 dev_dbg(dcmi->dev, "Subdev \"%s\" bound\n", subdev->name);
1795
1796 /*
1797 * Link this sub-device to DCMI, it could be
1798 * a parallel camera sensor or a bridge
1799 */
1800 src_pad = media_entity_get_fwnode_pad(&subdev->entity,
1801 subdev->fwnode,
1802 MEDIA_PAD_FL_SOURCE);
1803
1804 ret = media_create_pad_link(&subdev->entity, src_pad,
1805 &dcmi->vdev->entity, 0,
1806 MEDIA_LNK_FL_IMMUTABLE |
1807 MEDIA_LNK_FL_ENABLED);
1808 if (ret)
1809 dev_err(dcmi->dev, "Failed to create media pad link with subdev \"%s\"\n",
1810 subdev->name);
1811 else
1812 dev_dbg(dcmi->dev, "DCMI is now linked to \"%s\"\n",
1813 subdev->name);
1814
1815 return ret;
1816 }
1817
1818 static const struct v4l2_async_notifier_operations dcmi_graph_notify_ops = {
1819 .bound = dcmi_graph_notify_bound,
1820 .unbind = dcmi_graph_notify_unbind,
1821 .complete = dcmi_graph_notify_complete,
1822 };
1823
dcmi_graph_init(struct stm32_dcmi * dcmi)1824 static int dcmi_graph_init(struct stm32_dcmi *dcmi)
1825 {
1826 struct v4l2_async_subdev *asd;
1827 struct device_node *ep;
1828 int ret;
1829
1830 ep = of_graph_get_next_endpoint(dcmi->dev->of_node, NULL);
1831 if (!ep) {
1832 dev_err(dcmi->dev, "Failed to get next endpoint\n");
1833 return -EINVAL;
1834 }
1835
1836 v4l2_async_nf_init(&dcmi->notifier);
1837
1838 asd = v4l2_async_nf_add_fwnode_remote(&dcmi->notifier,
1839 of_fwnode_handle(ep),
1840 struct v4l2_async_subdev);
1841
1842 of_node_put(ep);
1843
1844 if (IS_ERR(asd)) {
1845 dev_err(dcmi->dev, "Failed to add subdev notifier\n");
1846 return PTR_ERR(asd);
1847 }
1848
1849 dcmi->notifier.ops = &dcmi_graph_notify_ops;
1850
1851 ret = v4l2_async_nf_register(&dcmi->v4l2_dev, &dcmi->notifier);
1852 if (ret < 0) {
1853 dev_err(dcmi->dev, "Failed to register notifier\n");
1854 v4l2_async_nf_cleanup(&dcmi->notifier);
1855 return ret;
1856 }
1857
1858 return 0;
1859 }
1860
dcmi_probe(struct platform_device * pdev)1861 static int dcmi_probe(struct platform_device *pdev)
1862 {
1863 struct device_node *np = pdev->dev.of_node;
1864 const struct of_device_id *match = NULL;
1865 struct v4l2_fwnode_endpoint ep = { .bus_type = 0 };
1866 struct stm32_dcmi *dcmi;
1867 struct vb2_queue *q;
1868 struct dma_chan *chan;
1869 struct clk *mclk;
1870 int irq;
1871 int ret = 0;
1872
1873 match = of_match_device(of_match_ptr(stm32_dcmi_of_match), &pdev->dev);
1874 if (!match) {
1875 dev_err(&pdev->dev, "Could not find a match in devicetree\n");
1876 return -ENODEV;
1877 }
1878
1879 dcmi = devm_kzalloc(&pdev->dev, sizeof(struct stm32_dcmi), GFP_KERNEL);
1880 if (!dcmi)
1881 return -ENOMEM;
1882
1883 dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1884 if (IS_ERR(dcmi->rstc)) {
1885 if (PTR_ERR(dcmi->rstc) != -EPROBE_DEFER)
1886 dev_err(&pdev->dev, "Could not get reset control\n");
1887
1888 return PTR_ERR(dcmi->rstc);
1889 }
1890
1891 /* Get bus characteristics from devicetree */
1892 np = of_graph_get_next_endpoint(np, NULL);
1893 if (!np) {
1894 dev_err(&pdev->dev, "Could not find the endpoint\n");
1895 return -ENODEV;
1896 }
1897
1898 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
1899 of_node_put(np);
1900 if (ret) {
1901 dev_err(&pdev->dev, "Could not parse the endpoint\n");
1902 return ret;
1903 }
1904
1905 if (ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
1906 dev_err(&pdev->dev, "CSI bus not supported\n");
1907 return -ENODEV;
1908 }
1909
1910 if (ep.bus_type == V4L2_MBUS_BT656 &&
1911 ep.bus.parallel.bus_width != 8) {
1912 dev_err(&pdev->dev, "BT656 bus conflicts with %u bits bus width (8 bits required)\n",
1913 ep.bus.parallel.bus_width);
1914 return -ENODEV;
1915 }
1916
1917 dcmi->bus.flags = ep.bus.parallel.flags;
1918 dcmi->bus.bus_width = ep.bus.parallel.bus_width;
1919 dcmi->bus.data_shift = ep.bus.parallel.data_shift;
1920 dcmi->bus_type = ep.bus_type;
1921
1922 irq = platform_get_irq(pdev, 0);
1923 if (irq <= 0)
1924 return irq ? irq : -ENXIO;
1925
1926 dcmi->irq = irq;
1927
1928 dcmi->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1929 if (!dcmi->res) {
1930 dev_err(&pdev->dev, "Could not get resource\n");
1931 return -ENODEV;
1932 }
1933
1934 dcmi->regs = devm_ioremap_resource(&pdev->dev, dcmi->res);
1935 if (IS_ERR(dcmi->regs)) {
1936 dev_err(&pdev->dev, "Could not map registers\n");
1937 return PTR_ERR(dcmi->regs);
1938 }
1939
1940 mclk = devm_clk_get(&pdev->dev, "mclk");
1941 if (IS_ERR(mclk)) {
1942 if (PTR_ERR(mclk) != -EPROBE_DEFER)
1943 dev_err(&pdev->dev, "Unable to get mclk\n");
1944 return PTR_ERR(mclk);
1945 }
1946
1947 chan = dma_request_chan(&pdev->dev, "tx");
1948 if (IS_ERR(chan)) {
1949 ret = PTR_ERR(chan);
1950 if (ret != -EPROBE_DEFER)
1951 dev_err(&pdev->dev,
1952 "Failed to request DMA channel: %d\n", ret);
1953 return ret;
1954 }
1955
1956 spin_lock_init(&dcmi->irqlock);
1957 mutex_init(&dcmi->lock);
1958 mutex_init(&dcmi->dma_lock);
1959 init_completion(&dcmi->complete);
1960 INIT_LIST_HEAD(&dcmi->buffers);
1961
1962 dcmi->dev = &pdev->dev;
1963 dcmi->mclk = mclk;
1964 dcmi->state = STOPPED;
1965 dcmi->dma_chan = chan;
1966
1967 q = &dcmi->queue;
1968
1969 dcmi->v4l2_dev.mdev = &dcmi->mdev;
1970
1971 /* Initialize media device */
1972 strscpy(dcmi->mdev.model, DRV_NAME, sizeof(dcmi->mdev.model));
1973 snprintf(dcmi->mdev.bus_info, sizeof(dcmi->mdev.bus_info),
1974 "platform:%s", DRV_NAME);
1975 dcmi->mdev.dev = &pdev->dev;
1976 media_device_init(&dcmi->mdev);
1977
1978 /* Initialize the top-level structure */
1979 ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev);
1980 if (ret)
1981 goto err_media_device_cleanup;
1982
1983 dcmi->vdev = video_device_alloc();
1984 if (!dcmi->vdev) {
1985 ret = -ENOMEM;
1986 goto err_device_unregister;
1987 }
1988
1989 /* Video node */
1990 dcmi->vdev->fops = &dcmi_fops;
1991 dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev;
1992 dcmi->vdev->queue = &dcmi->queue;
1993 strscpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name));
1994 dcmi->vdev->release = video_device_release;
1995 dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops;
1996 dcmi->vdev->lock = &dcmi->lock;
1997 dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1998 V4L2_CAP_READWRITE;
1999 video_set_drvdata(dcmi->vdev, dcmi);
2000
2001 /* Media entity pads */
2002 dcmi->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
2003 ret = media_entity_pads_init(&dcmi->vdev->entity,
2004 1, &dcmi->vid_cap_pad);
2005 if (ret) {
2006 dev_err(dcmi->dev, "Failed to init media entity pad\n");
2007 goto err_device_release;
2008 }
2009 dcmi->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
2010
2011 ret = video_register_device(dcmi->vdev, VFL_TYPE_VIDEO, -1);
2012 if (ret) {
2013 dev_err(dcmi->dev, "Failed to register video device\n");
2014 goto err_media_entity_cleanup;
2015 }
2016
2017 dev_dbg(dcmi->dev, "Device registered as %s\n",
2018 video_device_node_name(dcmi->vdev));
2019
2020 /* Buffer queue */
2021 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2022 q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
2023 q->lock = &dcmi->lock;
2024 q->drv_priv = dcmi;
2025 q->buf_struct_size = sizeof(struct dcmi_buf);
2026 q->ops = &dcmi_video_qops;
2027 q->mem_ops = &vb2_dma_contig_memops;
2028 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2029 q->min_buffers_needed = 2;
2030 q->dev = &pdev->dev;
2031
2032 ret = vb2_queue_init(q);
2033 if (ret < 0) {
2034 dev_err(&pdev->dev, "Failed to initialize vb2 queue\n");
2035 goto err_media_entity_cleanup;
2036 }
2037
2038 ret = dcmi_graph_init(dcmi);
2039 if (ret < 0)
2040 goto err_media_entity_cleanup;
2041
2042 /* Reset device */
2043 ret = reset_control_assert(dcmi->rstc);
2044 if (ret) {
2045 dev_err(&pdev->dev, "Failed to assert the reset line\n");
2046 goto err_cleanup;
2047 }
2048
2049 usleep_range(3000, 5000);
2050
2051 ret = reset_control_deassert(dcmi->rstc);
2052 if (ret) {
2053 dev_err(&pdev->dev, "Failed to deassert the reset line\n");
2054 goto err_cleanup;
2055 }
2056
2057 dev_info(&pdev->dev, "Probe done\n");
2058
2059 platform_set_drvdata(pdev, dcmi);
2060
2061 pm_runtime_enable(&pdev->dev);
2062
2063 return 0;
2064
2065 err_cleanup:
2066 v4l2_async_nf_cleanup(&dcmi->notifier);
2067 err_media_entity_cleanup:
2068 media_entity_cleanup(&dcmi->vdev->entity);
2069 err_device_release:
2070 video_device_release(dcmi->vdev);
2071 err_device_unregister:
2072 v4l2_device_unregister(&dcmi->v4l2_dev);
2073 err_media_device_cleanup:
2074 media_device_cleanup(&dcmi->mdev);
2075 dma_release_channel(dcmi->dma_chan);
2076
2077 return ret;
2078 }
2079
dcmi_remove(struct platform_device * pdev)2080 static int dcmi_remove(struct platform_device *pdev)
2081 {
2082 struct stm32_dcmi *dcmi = platform_get_drvdata(pdev);
2083
2084 pm_runtime_disable(&pdev->dev);
2085
2086 v4l2_async_nf_unregister(&dcmi->notifier);
2087 v4l2_async_nf_cleanup(&dcmi->notifier);
2088 media_entity_cleanup(&dcmi->vdev->entity);
2089 v4l2_device_unregister(&dcmi->v4l2_dev);
2090 media_device_cleanup(&dcmi->mdev);
2091
2092 dma_release_channel(dcmi->dma_chan);
2093
2094 return 0;
2095 }
2096
dcmi_runtime_suspend(struct device * dev)2097 static __maybe_unused int dcmi_runtime_suspend(struct device *dev)
2098 {
2099 struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
2100
2101 clk_disable_unprepare(dcmi->mclk);
2102
2103 return 0;
2104 }
2105
dcmi_runtime_resume(struct device * dev)2106 static __maybe_unused int dcmi_runtime_resume(struct device *dev)
2107 {
2108 struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
2109 int ret;
2110
2111 ret = clk_prepare_enable(dcmi->mclk);
2112 if (ret)
2113 dev_err(dev, "%s: Failed to prepare_enable clock\n", __func__);
2114
2115 return ret;
2116 }
2117
dcmi_suspend(struct device * dev)2118 static __maybe_unused int dcmi_suspend(struct device *dev)
2119 {
2120 /* disable clock */
2121 pm_runtime_force_suspend(dev);
2122
2123 /* change pinctrl state */
2124 pinctrl_pm_select_sleep_state(dev);
2125
2126 return 0;
2127 }
2128
dcmi_resume(struct device * dev)2129 static __maybe_unused int dcmi_resume(struct device *dev)
2130 {
2131 /* restore pinctl default state */
2132 pinctrl_pm_select_default_state(dev);
2133
2134 /* clock enable */
2135 pm_runtime_force_resume(dev);
2136
2137 return 0;
2138 }
2139
2140 static const struct dev_pm_ops dcmi_pm_ops = {
2141 SET_SYSTEM_SLEEP_PM_OPS(dcmi_suspend, dcmi_resume)
2142 SET_RUNTIME_PM_OPS(dcmi_runtime_suspend,
2143 dcmi_runtime_resume, NULL)
2144 };
2145
2146 static struct platform_driver stm32_dcmi_driver = {
2147 .probe = dcmi_probe,
2148 .remove = dcmi_remove,
2149 .driver = {
2150 .name = DRV_NAME,
2151 .of_match_table = of_match_ptr(stm32_dcmi_of_match),
2152 .pm = &dcmi_pm_ops,
2153 },
2154 };
2155
2156 module_platform_driver(stm32_dcmi_driver);
2157
2158 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
2159 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
2160 MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");
2161 MODULE_LICENSE("GPL");
2162