Debian Codegraph @

drivers/media/platform/renesas/rzg2l-cru/rzg2l-video.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Driver for Renesas RZ/G2L CRU
4 *
5 * Copyright (C) 2022 Renesas Electronics Corp.
6 *
7 * Based on Renesas R-Car VIN
8 * Copyright (C) 2016 Renesas Electronics Corp.
9 * Copyright (C) 2011-2013 Renesas Solutions Corp.
10 * Copyright (C) 2013 Cogent Embedded, Inc., <[email protected]>
11 * Copyright (C) 2008 Magnus Damm
12 */
13
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/pm_runtime.h>
17
18 #include <media/mipi-csi2.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/videobuf2-dma-contig.h>
21
22 #include "rzg2l-cru.h"
23 #include "rzg2l-cru-regs.h"
24
25 #define RZG2L_TIMEOUT_MS 100
26 #define RZG2L_RETRIES 10
27
28 #define RZG2L_CRU_DEFAULT_FORMAT V4L2_PIX_FMT_UYVY
29 #define RZG2L_CRU_DEFAULT_WIDTH RZG2L_CRU_MIN_INPUT_WIDTH
30 #define RZG2L_CRU_DEFAULT_HEIGHT RZG2L_CRU_MIN_INPUT_HEIGHT
31 #define RZG2L_CRU_DEFAULT_FIELD V4L2_FIELD_NONE
32 #define RZG2L_CRU_DEFAULT_COLORSPACE V4L2_COLORSPACE_SRGB
33
34 #define RZG2L_CRU_STRIDE_MAX 32640
35 #define RZG2L_CRU_STRIDE_ALIGN 128
36
37 struct rzg2l_cru_buffer {
38 struct vb2_v4l2_buffer vb;
39 struct list_head list;
40 };
41
42 #define to_buf_list(vb2_buffer) \
43 (&container_of(vb2_buffer, struct rzg2l_cru_buffer, vb)->list)
44
45 /* -----------------------------------------------------------------------------
46 * DMA operations
47 */
48 static void __rzg2l_cru_write(struct rzg2l_cru_dev *cru, u32 offset, u32 value)
49 {
50 const u16 *regs = cru->info->regs;
51
52 /*
53 * CRUnCTRL is a first register on all CRU supported SoCs so validate
54 * rest of the registers have valid offset being set in cru->info->regs.
55 */
56 if (WARN_ON(offset >= RZG2L_CRU_MAX_REG) ||
57 WARN_ON(offset != CRUnCTRL && regs[offset] == 0))
58 return;
59
60 iowrite32(value, cru->base + regs[offset]);
61 }
62
63 static u32 __rzg2l_cru_read(struct rzg2l_cru_dev *cru, u32 offset)
64 {
65 const u16 *regs = cru->info->regs;
66
67 /*
68 * CRUnCTRL is a first register on all CRU supported SoCs so validate
69 * rest of the registers have valid offset being set in cru->info->regs.
70 */
71 if (WARN_ON(offset >= RZG2L_CRU_MAX_REG) ||
72 WARN_ON(offset != CRUnCTRL && regs[offset] == 0))
73 return 0;
74
75 return ioread32(cru->base + regs[offset]);
76 }
77
78 static __always_inline void
79 __rzg2l_cru_write_constant(struct rzg2l_cru_dev *cru, u32 offset, u32 value)
80 {
81 const u16 *regs = cru->info->regs;
82
83 BUILD_BUG_ON(offset >= RZG2L_CRU_MAX_REG);
84
85 iowrite32(value, cru->base + regs[offset]);
86 }
87
88 static __always_inline u32
89 __rzg2l_cru_read_constant(struct rzg2l_cru_dev *cru, u32 offset)
90 {
91 const u16 *regs = cru->info->regs;
92
93 BUILD_BUG_ON(offset >= RZG2L_CRU_MAX_REG);
94
95 return ioread32(cru->base + regs[offset]);
96 }
97
98 #define rzg2l_cru_write(cru, offset, value) \
99 (__builtin_constant_p(offset) ? \
100 __rzg2l_cru_write_constant(cru, offset, value) : \
101 __rzg2l_cru_write(cru, offset, value))
102
103 #define rzg2l_cru_read(cru, offset) \
104 (__builtin_constant_p(offset) ? \
105 __rzg2l_cru_read_constant(cru, offset) : \
106 __rzg2l_cru_read(cru, offset))
107
108 /* Need to hold qlock before calling */
109 static void return_unused_buffers(struct rzg2l_cru_dev *cru,
110 enum vb2_buffer_state state)
111 {
112 struct rzg2l_cru_buffer *buf, *node;
113 unsigned long flags;
114 unsigned int i;
115
116 spin_lock_irqsave(&cru->qlock, flags);
117 for (i = 0; i < cru->num_buf; i++) {
118 if (cru->queue_buf[i]) {
119 vb2_buffer_done(&cru->queue_buf[i]->vb2_buf,
120 state);
121 cru->queue_buf[i] = NULL;
122 }
123 }
124
125 list_for_each_entry_safe(buf, node, &cru->buf_list, list) {
126 vb2_buffer_done(&buf->vb.vb2_buf, state);
127 list_del(&buf->list);
128 }
129 spin_unlock_irqrestore(&cru->qlock, flags);
130 }
131
132 static int rzg2l_cru_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
133 unsigned int *nplanes, unsigned int sizes[],
134 struct device *alloc_devs[])
135 {
136 struct rzg2l_cru_dev *cru = vb2_get_drv_priv(vq);
137
138 /* Make sure the image size is large enough. */
139 if (*nplanes)
140 return sizes[0] < cru->format.sizeimage ? -EINVAL : 0;
141
142 *nplanes = 1;
143 sizes[0] = cru->format.sizeimage;
144
145 return 0;
146 };
147
148 static int rzg2l_cru_buffer_prepare(struct vb2_buffer *vb)
149 {
150 struct rzg2l_cru_dev *cru = vb2_get_drv_priv(vb->vb2_queue);
151 unsigned long size = cru->format.sizeimage;
152
153 if (vb2_plane_size(vb, 0) < size) {
154 dev_err(cru->dev, "buffer too small (%lu < %lu)\n",
155 vb2_plane_size(vb, 0), size);
156 return -EINVAL;
157 }
158
159 vb2_set_plane_payload(vb, 0, size);
160
161 return 0;
162 }
163
164 static void rzg2l_cru_buffer_queue(struct vb2_buffer *vb)
165 {
166 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
167 struct rzg2l_cru_dev *cru = vb2_get_drv_priv(vb->vb2_queue);
168 unsigned long flags;
169
170 spin_lock_irqsave(&cru->qlock, flags);
171
172 list_add_tail(to_buf_list(vbuf), &cru->buf_list);
173
174 spin_unlock_irqrestore(&cru->qlock, flags);
175 }
176
177 static void rzg2l_cru_set_slot_addr(struct rzg2l_cru_dev *cru,
178 int slot, dma_addr_t addr)
179 {
180 /*
181 * The address needs to be 512 bytes aligned. Driver should never accept
182 * settings that do not satisfy this in the first place...
183 */
184 if (WARN_ON((addr) & RZG2L_CRU_HW_BUFFER_MASK))
185 return;
186
187 /* Currently, we just use the buffer in 32 bits address */
188 rzg2l_cru_write(cru, AMnMBxADDRL(slot), addr);
189 rzg2l_cru_write(cru, AMnMBxADDRH(slot), 0);
190
191 cru->buf_addr[slot] = addr;
192 }
193
194 /*
195 * Moves a buffer from the queue to the HW slot. If no buffer is
196 * available use the scratch buffer. The scratch buffer is never
197 * returned to userspace, its only function is to enable the capture
198 * loop to keep running.
199 */
200 static void rzg2l_cru_fill_hw_slot(struct rzg2l_cru_dev *cru, int slot)
201 {
202 struct vb2_v4l2_buffer *vbuf;
203 struct rzg2l_cru_buffer *buf;
204 dma_addr_t phys_addr;
205
206 /* A already populated slot shall never be overwritten. */
207 if (WARN_ON(cru->queue_buf[slot]))
208 return;
209
210 dev_dbg(cru->dev, "Filling HW slot: %d\n", slot);
211
212 if (list_empty(&cru->buf_list)) {
213 cru->queue_buf[slot] = NULL;
214 phys_addr = cru->scratch_phys;
215 } else {
216 /* Keep track of buffer we give to HW */
217 buf = list_entry(cru->buf_list.next,
218 struct rzg2l_cru_buffer, list);
219 vbuf = &buf->vb;
220 list_del_init(to_buf_list(vbuf));
221 cru->queue_buf[slot] = vbuf;
222
223 /* Setup DMA */
224 phys_addr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
225 }
226
227 rzg2l_cru_set_slot_addr(cru, slot, phys_addr);
228 }
229
230 static void rzg2l_cru_initialize_axi(struct rzg2l_cru_dev *cru)
231 {
232 const struct rzg2l_cru_info *info = cru->info;
233 unsigned int slot;
234 u32 amnaxiattr;
235
236 /*
237 * Set image data memory banks.
238 * Currently, we will use maximum address.
239 */
240 rzg2l_cru_write(cru, AMnMBVALID, AMnMBVALID_MBVALID(cru->num_buf - 1));
241
242 for (slot = 0; slot < cru->num_buf; slot++)
243 rzg2l_cru_fill_hw_slot(cru, slot);
244
245 if (info->has_stride) {
246 u32 stride = cru->format.bytesperline;
247 u32 amnis;
248
249 stride /= RZG2L_CRU_STRIDE_ALIGN;
250 amnis = rzg2l_cru_read(cru, AMnIS) & ~AMnIS_IS_MASK;
251 rzg2l_cru_write(cru, AMnIS, amnis | AMnIS_IS(stride));
252 }
253
254 /* Set AXI burst max length to recommended setting */
255 amnaxiattr = rzg2l_cru_read(cru, AMnAXIATTR) & ~AMnAXIATTR_AXILEN_MASK;
256 amnaxiattr |= AMnAXIATTR_AXILEN;
257 rzg2l_cru_write(cru, AMnAXIATTR, amnaxiattr);
258 }
259
260 static void rzg2l_cru_csi2_setup(struct rzg2l_cru_dev *cru,
261 const struct rzg2l_cru_ip_format *ip_fmt,
262 u8 csi_vc)
263 {
264 const struct rzg2l_cru_info *info = cru->info;
265 u32 icnmc = rzg2l_cru_read(cru, info->image_conv) & ~(ICnMC_INF_MASK |
266 ICnMC_VCSEL_MASK);
267 icnmc |= ICnMC_INF(ip_fmt->datatype);
268
269 /*
270 * VC filtering goes through SVC register on G3E/V2H.
271 *
272 * FIXME: virtual channel filtering is likely broken and only VC=0
273 * works.
274 */
275 if (cru->info->regs[ICnSVC]) {
276 rzg2l_cru_write(cru, ICnSVCNUM, csi_vc);
277 rzg2l_cru_write(cru, ICnSVC, ICnSVC_SVC0(0) | ICnSVC_SVC1(1) |
278 ICnSVC_SVC2(2) | ICnSVC_SVC3(3));
279 } else {
280 icnmc |= ICnMC_VCSEL(csi_vc);
281 }
282
283 rzg2l_cru_write(cru, info->image_conv, icnmc);
284 }
285
286 static int rzg2l_cru_initialize_image_conv(struct rzg2l_cru_dev *cru,
287 struct v4l2_mbus_framefmt *ip_sd_fmt,
288 u8 csi_vc)
289 {
290 const struct rzg2l_cru_info *info = cru->info;
291 const struct rzg2l_cru_ip_format *cru_video_fmt;
292 const struct rzg2l_cru_ip_format *cru_ip_fmt;
293
294 cru_ip_fmt = rzg2l_cru_ip_code_to_fmt(ip_sd_fmt->code);
295 rzg2l_cru_csi2_setup(cru, cru_ip_fmt, csi_vc);
296
297 /* Output format */
298 cru_video_fmt = rzg2l_cru_ip_format_to_fmt(cru->format.pixelformat);
299 if (!cru_video_fmt) {
300 dev_err(cru->dev, "Invalid pixelformat (0x%x)\n",
301 cru->format.pixelformat);
302 return -EINVAL;
303 }
304
305 /* If input and output use same colorspace, do bypass mode */
306 if (cru_ip_fmt->yuv == cru_video_fmt->yuv)
307 rzg2l_cru_write(cru, info->image_conv,
308 rzg2l_cru_read(cru, info->image_conv) | ICnMC_CSCTHR);
309 else
310 rzg2l_cru_write(cru, info->image_conv,
311 rzg2l_cru_read(cru, info->image_conv) & ~ICnMC_CSCTHR);
312
313 /* Set output data format */
314 rzg2l_cru_write(cru, ICnDMR, cru_video_fmt->icndmr);
315
316 return 0;
317 }
318
319 bool rzg3e_fifo_empty(struct rzg2l_cru_dev *cru)
320 {
321 u32 amnfifopntr = rzg2l_cru_read(cru, AMnFIFOPNTR);
322
323 if ((((amnfifopntr & AMnFIFOPNTR_FIFORPNTR_B1) >> 24) ==
324 ((amnfifopntr & AMnFIFOPNTR_FIFOWPNTR_B1) >> 8)) &&
325 (((amnfifopntr & AMnFIFOPNTR_FIFORPNTR_B0) >> 16) ==
326 (amnfifopntr & AMnFIFOPNTR_FIFOWPNTR_B0)))
327 return true;
328
329 return false;
330 }
331
332 bool rzg2l_fifo_empty(struct rzg2l_cru_dev *cru)
333 {
334 u32 amnfifopntr, amnfifopntr_w, amnfifopntr_r_y;
335
336 amnfifopntr = rzg2l_cru_read(cru, AMnFIFOPNTR);
337
338 amnfifopntr_w = amnfifopntr & AMnFIFOPNTR_FIFOWPNTR;
339 amnfifopntr_r_y =
340 (amnfifopntr & AMnFIFOPNTR_FIFORPNTR_Y) >> 16;
341
342 return amnfifopntr_w == amnfifopntr_r_y;
343 }
344
345 void rzg2l_cru_stop_image_processing(struct rzg2l_cru_dev *cru)
346 {
347 unsigned int retries = 0;
348 unsigned long flags;
349 u32 icnms;
350
351 spin_lock_irqsave(&cru->qlock, flags);
352
353 /* Disable and clear the interrupt */
354 cru->info->disable_interrupts(cru);
355
356 /* Stop the operation of image conversion */
357 rzg2l_cru_write(cru, ICnEN, 0);
358
359 /* Wait for streaming to stop */
360 while ((rzg2l_cru_read(cru, ICnMS) & ICnMS_IA) && retries++ < RZG2L_RETRIES) {
361 spin_unlock_irqrestore(&cru->qlock, flags);
362 msleep(RZG2L_TIMEOUT_MS);
363 spin_lock_irqsave(&cru->qlock, flags);
364 }
365
366 icnms = rzg2l_cru_read(cru, ICnMS) & ICnMS_IA;
367 if (icnms)
368 dev_err(cru->dev, "Failed stop HW, something is seriously broken\n");
369
370 cru->state = RZG2L_CRU_DMA_STOPPED;
371
372 /* Wait until the FIFO becomes empty */
373 for (retries = 5; retries > 0; retries--) {
374 if (cru->info->fifo_empty(cru))
375 break;
376
377 usleep_range(10, 20);
378 }
379
380 /* Notify that FIFO is not empty here */
381 if (!retries)
382 dev_err(cru->dev, "Failed to empty FIFO\n");
383
384 /* Stop AXI bus */
385 rzg2l_cru_write(cru, AMnAXISTP, AMnAXISTP_AXI_STOP);
386
387 /* Wait until the AXI bus stop */
388 for (retries = 5; retries > 0; retries--) {
389 if (rzg2l_cru_read(cru, AMnAXISTPACK) &
390 AMnAXISTPACK_AXI_STOP_ACK)
391 break;
392
393 usleep_range(10, 20);
394 }
395
396 /* Notify that AXI bus can not stop here */
397 if (!retries)
398 dev_err(cru->dev, "Failed to stop AXI bus\n");
399
400 /* Cancel the AXI bus stop request */
401 rzg2l_cru_write(cru, AMnAXISTP, 0);
402
403 /* Reset the CRU (AXI-master) */
404 reset_control_assert(cru->aresetn);
405
406 /* Resets the image processing module */
407 rzg2l_cru_write(cru, CRUnRST, 0);
408
409 spin_unlock_irqrestore(&cru->qlock, flags);
410 }
411
412 static int rzg2l_cru_get_virtual_channel(struct rzg2l_cru_dev *cru)
413 {
414 struct v4l2_mbus_frame_desc fd = { };
415 struct media_pad *remote_pad;
416 int ret;
417
418 remote_pad = media_pad_remote_pad_unique(&cru->ip.pads[RZG2L_CRU_IP_SINK]);
419 ret = v4l2_subdev_call(cru->ip.remote, pad, get_frame_desc, remote_pad->index, &fd);
420 if (ret < 0 && ret != -ENOIOCTLCMD) {
421 dev_err(cru->dev, "get_frame_desc failed on IP remote subdev\n");
422 return ret;
423 }
424 /* If remote subdev does not implement .get_frame_desc default to VC0. */
425 if (ret == -ENOIOCTLCMD)
426 return 0;
427
428 if (fd.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
429 dev_err(cru->dev, "get_frame_desc returned invalid bus type %d\n", fd.type);
430 return -EINVAL;
431 }
432
433 if (!fd.num_entries) {
434 dev_err(cru->dev, "get_frame_desc returned zero entries\n");
435 return -EINVAL;
436 }
437
438 return fd.entry[0].bus.csi2.vc;
439 }
440
441 void rzg3e_cru_enable_interrupts(struct rzg2l_cru_dev *cru)
442 {
443 rzg2l_cru_write(cru, CRUnIE2, CRUnIE2_FSxE(cru->svc_channel));
444 rzg2l_cru_write(cru, CRUnIE2, CRUnIE2_FExE(cru->svc_channel));
445 }
446
447 void rzg3e_cru_disable_interrupts(struct rzg2l_cru_dev *cru)
448 {
449 rzg2l_cru_write(cru, CRUnIE, 0);
450 rzg2l_cru_write(cru, CRUnIE2, 0);
451 rzg2l_cru_write(cru, CRUnINTS, rzg2l_cru_read(cru, CRUnINTS));
452 rzg2l_cru_write(cru, CRUnINTS2, rzg2l_cru_read(cru, CRUnINTS2));
453 }
454
455 void rzg2l_cru_enable_interrupts(struct rzg2l_cru_dev *cru)
456 {
457 rzg2l_cru_write(cru, CRUnIE, CRUnIE_EFE);
458 }
459
460 void rzg2l_cru_disable_interrupts(struct rzg2l_cru_dev *cru)
461 {
462 rzg2l_cru_write(cru, CRUnIE, 0);
463 rzg2l_cru_write(cru, CRUnINTS, 0x001f000f);
464 }
465
466 int rzg2l_cru_start_image_processing(struct rzg2l_cru_dev *cru)
467 {
468 struct v4l2_mbus_framefmt *fmt = rzg2l_cru_ip_get_src_fmt(cru);
469 unsigned long flags;
470 u8 csi_vc;
471 int ret;
472
473 ret = rzg2l_cru_get_virtual_channel(cru);
474 if (ret < 0)
475 return ret;
476 csi_vc = ret;
477 cru->svc_channel = csi_vc;
478
479 spin_lock_irqsave(&cru->qlock, flags);
480
481 /* Select a video input */
482 rzg2l_cru_write(cru, CRUnCTRL, CRUnCTRL_VINSEL(0));
483
484 /* Cancel the software reset for image processing block */
485 rzg2l_cru_write(cru, CRUnRST, CRUnRST_VRESETN);
486
487 /* Disable and clear the interrupt before using */
488 cru->info->disable_interrupts(cru);
489
490 /* Initialize the AXI master */
491 rzg2l_cru_initialize_axi(cru);
492
493 /* Initialize image convert */
494 ret = rzg2l_cru_initialize_image_conv(cru, fmt, csi_vc);
495 if (ret) {
496 spin_unlock_irqrestore(&cru->qlock, flags);
497 return ret;
498 }
499
500 /* Enable interrupt */
501 cru->info->enable_interrupts(cru);
502
503 /* Enable image processing reception */
504 rzg2l_cru_write(cru, ICnEN, ICnEN_ICEN);
505
506 spin_unlock_irqrestore(&cru->qlock, flags);
507
508 return 0;
509 }
510
511 static int rzg2l_cru_set_stream(struct rzg2l_cru_dev *cru, int on)
512 {
513 struct media_pipeline *pipe;
514 struct v4l2_subdev *sd;
515 struct media_pad *pad;
516 int ret;
517
518 pad = media_pad_remote_pad_first(&cru->pad);
519 if (!pad)
520 return -EPIPE;
521
522 sd = media_entity_to_v4l2_subdev(pad->entity);
523
524 if (!on) {
525 int stream_off_ret = 0;
526
527 ret = v4l2_subdev_call(sd, video, s_stream, 0);
528 if (ret)
529 stream_off_ret = ret;
530
531 ret = v4l2_subdev_call(sd, video, post_streamoff);
532 if (ret == -ENOIOCTLCMD)
533 ret = 0;
534 if (ret && !stream_off_ret)
535 stream_off_ret = ret;
536
537 video_device_pipeline_stop(&cru->vdev);
538
539 return stream_off_ret;
540 }
541
542 pipe = media_entity_pipeline(&sd->entity) ? : &cru->vdev.pipe;
543 ret = video_device_pipeline_start(&cru->vdev, pipe);
544 if (ret)
545 return ret;
546
547 ret = v4l2_subdev_call(sd, video, pre_streamon, 0);
548 if (ret && ret != -ENOIOCTLCMD)
549 goto pipe_line_stop;
550
551 ret = v4l2_subdev_call(sd, video, s_stream, 1);
552 if (ret && ret != -ENOIOCTLCMD)
553 goto err_s_stream;
554
555 return 0;
556
557 err_s_stream:
558 v4l2_subdev_call(sd, video, post_streamoff);
559
560 pipe_line_stop:
561 video_device_pipeline_stop(&cru->vdev);
562
563 return ret;
564 }
565
566 static void rzg2l_cru_stop_streaming(struct rzg2l_cru_dev *cru)
567 {
568 cru->state = RZG2L_CRU_DMA_STOPPING;
569
570 rzg2l_cru_set_stream(cru, 0);
571 }
572
573 irqreturn_t rzg2l_cru_irq(int irq, void *data)
574 {
575 struct rzg2l_cru_dev *cru = data;
576 unsigned int handled = 0;
577 unsigned long flags;
578 u32 irq_status;
579 u32 amnmbs;
580 int slot;
581
582 spin_lock_irqsave(&cru->qlock, flags);
583
584 irq_status = rzg2l_cru_read(cru, CRUnINTS);
585 if (!irq_status)
586 goto done;
587
588 handled = 1;
589
590 rzg2l_cru_write(cru, CRUnINTS, rzg2l_cru_read(cru, CRUnINTS));
591
592 /* Nothing to do if capture status is 'RZG2L_CRU_DMA_STOPPED' */
593 if (cru->state == RZG2L_CRU_DMA_STOPPED) {
594 dev_dbg(cru->dev, "IRQ while state stopped\n");
595 goto done;
596 }
597
598 /* Increase stop retries if capture status is 'RZG2L_CRU_DMA_STOPPING' */
599 if (cru->state == RZG2L_CRU_DMA_STOPPING) {
600 if (irq_status & CRUnINTS_SFS)
601 dev_dbg(cru->dev, "IRQ while state stopping\n");
602 goto done;
603 }
604
605 /* Prepare for capture and update state */
606 amnmbs = rzg2l_cru_read(cru, AMnMBS);
607 slot = amnmbs & AMnMBS_MBSTS;
608
609 /*
610 * AMnMBS.MBSTS indicates the destination of Memory Bank (MB).
611 * Recalculate to get the current transfer complete MB.
612 */
613 if (slot == 0)
614 slot = cru->num_buf - 1;
615 else
616 slot--;
617
618 /*
619 * To hand buffers back in a known order to userspace start
620 * to capture first from slot 0.
621 */
622 if (cru->state == RZG2L_CRU_DMA_STARTING) {
623 if (slot != 0) {
624 dev_dbg(cru->dev, "Starting sync slot: %d\n", slot);
625 goto done;
626 }
627
628 dev_dbg(cru->dev, "Capture start synced!\n");
629 cru->state = RZG2L_CRU_DMA_RUNNING;
630 }
631
632 /* Capture frame */
633 if (cru->queue_buf[slot]) {
634 cru->queue_buf[slot]->field = cru->format.field;
635 cru->queue_buf[slot]->sequence = cru->sequence;
636 cru->queue_buf[slot]->vb2_buf.timestamp = ktime_get_ns();
637 vb2_buffer_done(&cru->queue_buf[slot]->vb2_buf,
638 VB2_BUF_STATE_DONE);
639 cru->queue_buf[slot] = NULL;
640 } else {
641 /* Scratch buffer was used, dropping frame. */
642 dev_dbg(cru->dev, "Dropping frame %u\n", cru->sequence);
643 }
644
645 cru->sequence++;
646
647 /* Prepare for next frame */
648 rzg2l_cru_fill_hw_slot(cru, slot);
649
650 done:
651 spin_unlock_irqrestore(&cru->qlock, flags);
652
653 return IRQ_RETVAL(handled);
654 }
655
656 static int rzg3e_cru_get_current_slot(struct rzg2l_cru_dev *cru)
657 {
658 u64 amnmadrs;
659 int slot;
660
661 /*
662 * When AMnMADRSL is read, AMnMADRSH of the higher-order
663 * address also latches the address.
664 *
665 * AMnMADRSH must be read after AMnMADRSL has been read.
666 */
667 amnmadrs = rzg2l_cru_read(cru, AMnMADRSL);
668 amnmadrs |= (u64)rzg2l_cru_read(cru, AMnMADRSH) << 32;
669
670 /* Ensure amnmadrs is within this buffer range */
671 for (slot = 0; slot < cru->num_buf; slot++) {
672 if (amnmadrs >= cru->buf_addr[slot] &&
673 amnmadrs < cru->buf_addr[slot] + cru->format.sizeimage)
674 return slot;
675 }
676
677 dev_err(cru->dev, "Invalid MB address 0x%llx (out of range)\n", amnmadrs);
678 return -EINVAL;
679 }
680
681 irqreturn_t rzg3e_cru_irq(int irq, void *data)
682 {
683 struct rzg2l_cru_dev *cru = data;
684 u32 irq_status;
685 int slot;
686
687 scoped_guard(spinlock, &cru->qlock) {
688 irq_status = rzg2l_cru_read(cru, CRUnINTS2);
689 if (!irq_status)
690 return IRQ_NONE;
691
692 dev_dbg(cru->dev, "CRUnINTS2 0x%x\n", irq_status);
693
694 rzg2l_cru_write(cru, CRUnINTS2, rzg2l_cru_read(cru, CRUnINTS2));
695
696 /* Nothing to do if capture status is 'RZG2L_CRU_DMA_STOPPED' */
697 if (cru->state == RZG2L_CRU_DMA_STOPPED) {
698 dev_dbg(cru->dev, "IRQ while state stopped\n");
699 return IRQ_HANDLED;
700 }
701
702 if (cru->state == RZG2L_CRU_DMA_STOPPING) {
703 if (irq_status & CRUnINTS2_FSxS(0) ||
704 irq_status & CRUnINTS2_FSxS(1) ||
705 irq_status & CRUnINTS2_FSxS(2) ||
706 irq_status & CRUnINTS2_FSxS(3))
707 dev_dbg(cru->dev, "IRQ while state stopping\n");
708 return IRQ_HANDLED;
709 }
710
711 slot = rzg3e_cru_get_current_slot(cru);
712 if (slot < 0)
713 return IRQ_HANDLED;
714
715 dev_dbg(cru->dev, "Current written slot: %d\n", slot);
716 cru->buf_addr[slot] = 0;
717
718 /*
719 * To hand buffers back in a known order to userspace start
720 * to capture first from slot 0.
721 */
722 if (cru->state == RZG2L_CRU_DMA_STARTING) {
723 if (slot != 0) {
724 dev_dbg(cru->dev, "Starting sync slot: %d\n", slot);
725 return IRQ_HANDLED;
726 }
727 dev_dbg(cru->dev, "Capture start synced!\n");
728 cru->state = RZG2L_CRU_DMA_RUNNING;
729 }
730
731 /* Capture frame */
732 if (cru->queue_buf[slot]) {
733 struct vb2_v4l2_buffer *buf = cru->queue_buf[slot];
734
735 buf->field = cru->format.field;
736 buf->sequence = cru->sequence;
737 buf->vb2_buf.timestamp = ktime_get_ns();
738 vb2_buffer_done(&buf->vb2_buf, VB2_BUF_STATE_DONE);
739 cru->queue_buf[slot] = NULL;
740 } else {
741 /* Scratch buffer was used, dropping frame. */
742 dev_dbg(cru->dev, "Dropping frame %u\n", cru->sequence);
743 }
744
745 cru->sequence++;
746
747 /* Prepare for next frame */
748 rzg2l_cru_fill_hw_slot(cru, slot);
749 }
750
751 return IRQ_HANDLED;
752 }
753
754 static int rzg2l_cru_start_streaming_vq(struct vb2_queue *vq, unsigned int count)
755 {
756 struct rzg2l_cru_dev *cru = vb2_get_drv_priv(vq);
757 int ret;
758
759 ret = pm_runtime_resume_and_get(cru->dev);
760 if (ret)
761 return ret;
762
763 ret = clk_prepare_enable(cru->vclk);
764 if (ret)
765 goto err_pm_put;
766
767 /* Release reset state */
768 ret = reset_control_deassert(cru->aresetn);
769 if (ret) {
770 dev_err(cru->dev, "failed to deassert aresetn\n");
771 goto err_vclk_disable;
772 }
773
774 ret = reset_control_deassert(cru->presetn);
775 if (ret) {
776 reset_control_assert(cru->aresetn);
777 dev_err(cru->dev, "failed to deassert presetn\n");
778 goto assert_aresetn;
779 }
780
781 /* Allocate scratch buffer */
782 cru->scratch = dma_alloc_coherent(cru->dev, cru->format.sizeimage,
783 &cru->scratch_phys, GFP_KERNEL);
784 if (!cru->scratch) {
785 return_unused_buffers(cru, VB2_BUF_STATE_QUEUED);
786 dev_err(cru->dev, "Failed to allocate scratch buffer\n");
787 ret = -ENOMEM;
788 goto assert_presetn;
789 }
790
791 cru->sequence = 0;
792
793 ret = rzg2l_cru_set_stream(cru, 1);
794 if (ret) {
795 return_unused_buffers(cru, VB2_BUF_STATE_QUEUED);
796 goto out;
797 }
798
799 cru->state = RZG2L_CRU_DMA_STARTING;
800 dev_dbg(cru->dev, "Starting to capture\n");
801 return 0;
802
803 out:
804 if (ret)
805 dma_free_coherent(cru->dev, cru->format.sizeimage, cru->scratch,
806 cru->scratch_phys);
807 assert_presetn:
808 reset_control_assert(cru->presetn);
809
810 assert_aresetn:
811 reset_control_assert(cru->aresetn);
812
813 err_vclk_disable:
814 clk_disable_unprepare(cru->vclk);
815
816 err_pm_put:
817 pm_runtime_put_sync(cru->dev);
818
819 return ret;
820 }
821
822 static void rzg2l_cru_stop_streaming_vq(struct vb2_queue *vq)
823 {
824 struct rzg2l_cru_dev *cru = vb2_get_drv_priv(vq);
825
826 rzg2l_cru_stop_streaming(cru);
827
828 /* Free scratch buffer */
829 dma_free_coherent(cru->dev, cru->format.sizeimage,
830 cru->scratch, cru->scratch_phys);
831
832 return_unused_buffers(cru, VB2_BUF_STATE_ERROR);
833
834 reset_control_assert(cru->presetn);
835 clk_disable_unprepare(cru->vclk);
836 pm_runtime_put_sync(cru->dev);
837 }
838
839 static const struct vb2_ops rzg2l_cru_qops = {
840 .queue_setup = rzg2l_cru_queue_setup,
841 .buf_prepare = rzg2l_cru_buffer_prepare,
842 .buf_queue = rzg2l_cru_buffer_queue,
843 .start_streaming = rzg2l_cru_start_streaming_vq,
844 .stop_streaming = rzg2l_cru_stop_streaming_vq,
845 };
846
847 void rzg2l_cru_dma_unregister(struct rzg2l_cru_dev *cru)
848 {
849 mutex_destroy(&cru->lock);
850
851 v4l2_device_unregister(&cru->v4l2_dev);
852 vb2_queue_release(&cru->queue);
853 }
854
855 int rzg2l_cru_dma_register(struct rzg2l_cru_dev *cru)
856 {
857 struct vb2_queue *q = &cru->queue;
858 unsigned int i;
859 int ret;
860
861 /* Initialize the top-level structure */
862 ret = v4l2_device_register(cru->dev, &cru->v4l2_dev);
863 if (ret)
864 return ret;
865
866 mutex_init(&cru->lock);
867 INIT_LIST_HEAD(&cru->buf_list);
868
869 spin_lock_init(&cru->qlock);
870
871 cru->state = RZG2L_CRU_DMA_STOPPED;
872
873 for (i = 0; i < RZG2L_CRU_HW_BUFFER_MAX; i++)
874 cru->queue_buf[i] = NULL;
875
876 /* buffer queue */
877 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
878 q->io_modes = VB2_MMAP | VB2_DMABUF;
879 q->lock = &cru->lock;
880 q->drv_priv = cru;
881 q->buf_struct_size = sizeof(struct rzg2l_cru_buffer);
882 q->ops = &rzg2l_cru_qops;
883 q->mem_ops = &vb2_dma_contig_memops;
884 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
885 q->min_queued_buffers = 4;
886 q->dev = cru->dev;
887
888 ret = vb2_queue_init(q);
889 if (ret < 0) {
890 dev_err(cru->dev, "failed to initialize VB2 queue\n");
891 goto error;
892 }
893
894 return 0;
895
896 error:
897 mutex_destroy(&cru->lock);
898 v4l2_device_unregister(&cru->v4l2_dev);
899 return ret;
900 }
901
902 /* -----------------------------------------------------------------------------
903 * V4L2 stuff
904 */
905
906 static void rzg2l_cru_format_align(struct rzg2l_cru_dev *cru,
907 struct v4l2_pix_format *pix)
908 {
909 const struct rzg2l_cru_info *info = cru->info;
910 const struct rzg2l_cru_ip_format *fmt;
911
912 fmt = rzg2l_cru_ip_format_to_fmt(pix->pixelformat);
913 if (!fmt) {
914 pix->pixelformat = RZG2L_CRU_DEFAULT_FORMAT;
915 fmt = rzg2l_cru_ip_format_to_fmt(pix->pixelformat);
916 }
917
918 switch (pix->field) {
919 case V4L2_FIELD_TOP:
920 case V4L2_FIELD_BOTTOM:
921 case V4L2_FIELD_NONE:
922 case V4L2_FIELD_INTERLACED_TB:
923 case V4L2_FIELD_INTERLACED_BT:
924 case V4L2_FIELD_INTERLACED:
925 break;
926 default:
927 pix->field = RZG2L_CRU_DEFAULT_FIELD;
928 break;
929 }
930
931 /* Limit to CRU capabilities */
932 v4l_bound_align_image(&pix->width, 320, info->max_width, 1,
933 &pix->height, 240, info->max_height, 2, 0);
934
935 v4l2_fill_pixfmt(pix, pix->pixelformat, pix->width, pix->height);
936
937 dev_dbg(cru->dev, "Format %ux%u bpl: %u size: %u\n",
938 pix->width, pix->height, pix->bytesperline, pix->sizeimage);
939 }
940
941 static void rzg2l_cru_try_format(struct rzg2l_cru_dev *cru,
942 struct v4l2_pix_format *pix)
943 {
944 /*
945 * The V4L2 specification clearly documents the colorspace fields
946 * as being set by drivers for capture devices. Using the values
947 * supplied by userspace thus wouldn't comply with the API. Until
948 * the API is updated force fixed values.
949 */
950 pix->colorspace = RZG2L_CRU_DEFAULT_COLORSPACE;
951 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
952 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
953 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
954 pix->ycbcr_enc);
955
956 rzg2l_cru_format_align(cru, pix);
957 }
958
959 static int rzg2l_cru_querycap(struct file *file, void *priv,
960 struct v4l2_capability *cap)
961 {
962 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
963 strscpy(cap->card, "RZG2L_CRU", sizeof(cap->card));
964
965 return 0;
966 }
967
968 static int rzg2l_cru_try_fmt_vid_cap(struct file *file, void *priv,
969 struct v4l2_format *f)
970 {
971 struct rzg2l_cru_dev *cru = video_drvdata(file);
972
973 rzg2l_cru_try_format(cru, &f->fmt.pix);
974
975 return 0;
976 }
977
978 static int rzg2l_cru_s_fmt_vid_cap(struct file *file, void *priv,
979 struct v4l2_format *f)
980 {
981 struct rzg2l_cru_dev *cru = video_drvdata(file);
982
983 if (vb2_is_busy(&cru->queue))
984 return -EBUSY;
985
986 rzg2l_cru_try_format(cru, &f->fmt.pix);
987
988 cru->format = f->fmt.pix;
989
990 return 0;
991 }
992
993 static int rzg2l_cru_g_fmt_vid_cap(struct file *file, void *priv,
994 struct v4l2_format *f)
995 {
996 struct rzg2l_cru_dev *cru = video_drvdata(file);
997
998 f->fmt.pix = cru->format;
999
1000 return 0;
1001 }
1002
1003 static int rzg2l_cru_enum_fmt_vid_cap(struct file *file, void *priv,
1004 struct v4l2_fmtdesc *f)
1005 {
1006 const struct rzg2l_cru_ip_format *fmt;
1007
1008 fmt = rzg2l_cru_ip_index_to_fmt(f->index);
1009 if (!fmt)
1010 return -EINVAL;
1011
1012 f->pixelformat = fmt->format;
1013
1014 return 0;
1015 }
1016
1017 static int rzg2l_cru_enum_framesizes(struct file *file, void *fh,
1018 struct v4l2_frmsizeenum *fsize)
1019 {
1020 struct rzg2l_cru_dev *cru = video_drvdata(file);
1021 const struct rzg2l_cru_info *info = cru->info;
1022 const struct rzg2l_cru_ip_format *fmt;
1023
1024 if (fsize->index)
1025 return -EINVAL;
1026
1027 fmt = rzg2l_cru_ip_format_to_fmt(fsize->pixel_format);
1028 if (!fmt)
1029 return -EINVAL;
1030
1031 fsize->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1032 fsize->stepwise.min_width = RZG2L_CRU_MIN_INPUT_WIDTH;
1033 fsize->stepwise.max_width = info->max_width;
1034 fsize->stepwise.step_width = 1;
1035 fsize->stepwise.min_height = RZG2L_CRU_MIN_INPUT_HEIGHT;
1036 fsize->stepwise.max_height = info->max_height;
1037 fsize->stepwise.step_height = 1;
1038
1039 return 0;
1040 }
1041
1042 static const struct v4l2_ioctl_ops rzg2l_cru_ioctl_ops = {
1043 .vidioc_querycap = rzg2l_cru_querycap,
1044 .vidioc_try_fmt_vid_cap = rzg2l_cru_try_fmt_vid_cap,
1045 .vidioc_g_fmt_vid_cap = rzg2l_cru_g_fmt_vid_cap,
1046 .vidioc_s_fmt_vid_cap = rzg2l_cru_s_fmt_vid_cap,
1047 .vidioc_enum_fmt_vid_cap = rzg2l_cru_enum_fmt_vid_cap,
1048
1049 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1050 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1051 .vidioc_querybuf = vb2_ioctl_querybuf,
1052 .vidioc_qbuf = vb2_ioctl_qbuf,
1053 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1054 .vidioc_expbuf = vb2_ioctl_expbuf,
1055 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1056 .vidioc_streamon = vb2_ioctl_streamon,
1057 .vidioc_streamoff = vb2_ioctl_streamoff,
1058 .vidioc_enum_framesizes = rzg2l_cru_enum_framesizes,
1059 };
1060
1061 /* -----------------------------------------------------------------------------
1062 * Media controller file operations
1063 */
1064
1065 static int rzg2l_cru_open(struct file *file)
1066 {
1067 struct rzg2l_cru_dev *cru = video_drvdata(file);
1068 int ret;
1069
1070 ret = mutex_lock_interruptible(&cru->lock);
1071 if (ret)
1072 return ret;
1073
1074 ret = v4l2_fh_open(file);
1075 if (ret)
1076 goto err_unlock;
1077
1078 mutex_unlock(&cru->lock);
1079
1080 return 0;
1081
1082 err_unlock:
1083 mutex_unlock(&cru->lock);
1084
1085 return ret;
1086 }
1087
1088 static int rzg2l_cru_release(struct file *file)
1089 {
1090 struct rzg2l_cru_dev *cru = video_drvdata(file);
1091 int ret;
1092
1093 mutex_lock(&cru->lock);
1094
1095 /* the release helper will cleanup any on-going streaming. */
1096 ret = _vb2_fop_release(file, NULL);
1097
1098 mutex_unlock(&cru->lock);
1099
1100 return ret;
1101 }
1102
1103 static const struct v4l2_file_operations rzg2l_cru_fops = {
1104 .owner = THIS_MODULE,
1105 .unlocked_ioctl = video_ioctl2,
1106 .open = rzg2l_cru_open,
1107 .release = rzg2l_cru_release,
1108 .poll = vb2_fop_poll,
1109 .mmap = vb2_fop_mmap,
1110 .read = vb2_fop_read,
1111 };
1112
1113 /* -----------------------------------------------------------------------------
1114 * Media entity operations
1115 */
1116
1117 static int rzg2l_cru_video_link_validate(struct media_link *link)
1118 {
1119 struct v4l2_subdev_format fmt = {
1120 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1121 };
1122 const struct rzg2l_cru_ip_format *video_fmt;
1123 struct v4l2_subdev *subdev;
1124 struct rzg2l_cru_dev *cru;
1125 int ret;
1126
1127 subdev = media_entity_to_v4l2_subdev(link->source->entity);
1128 fmt.pad = link->source->index;
1129 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
1130 if (ret < 0)
1131 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
1132
1133 cru = container_of(media_entity_to_video_device(link->sink->entity),
1134 struct rzg2l_cru_dev, vdev);
1135 video_fmt = rzg2l_cru_ip_format_to_fmt(cru->format.pixelformat);
1136
1137 if (fmt.format.width != cru->format.width ||
1138 fmt.format.height != cru->format.height ||
1139 fmt.format.field != cru->format.field ||
1140 !rzg2l_cru_ip_fmt_supports_mbus_code(video_fmt, fmt.format.code))
1141 return -EPIPE;
1142
1143 return 0;
1144 }
1145
1146 static const struct media_entity_operations rzg2l_cru_video_media_ops = {
1147 .link_validate = rzg2l_cru_video_link_validate,
1148 };
1149
1150 static void rzg2l_cru_v4l2_init(struct rzg2l_cru_dev *cru)
1151 {
1152 struct video_device *vdev = &cru->vdev;
1153
1154 vdev->v4l2_dev = &cru->v4l2_dev;
1155 vdev->queue = &cru->queue;
1156 snprintf(vdev->name, sizeof(vdev->name), "CRU output");
1157 vdev->release = video_device_release_empty;
1158 vdev->lock = &cru->lock;
1159 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1160 vdev->device_caps |= V4L2_CAP_IO_MC;
1161 vdev->entity.ops = &rzg2l_cru_video_media_ops;
1162 vdev->fops = &rzg2l_cru_fops;
1163 vdev->ioctl_ops = &rzg2l_cru_ioctl_ops;
1164
1165 /* Set a default format */
1166 cru->format.pixelformat = RZG2L_CRU_DEFAULT_FORMAT;
1167 cru->format.width = RZG2L_CRU_DEFAULT_WIDTH;
1168 cru->format.height = RZG2L_CRU_DEFAULT_HEIGHT;
1169 cru->format.field = RZG2L_CRU_DEFAULT_FIELD;
1170 cru->format.colorspace = RZG2L_CRU_DEFAULT_COLORSPACE;
1171 rzg2l_cru_format_align(cru, &cru->format);
1172 }
1173
1174 void rzg2l_cru_video_unregister(struct rzg2l_cru_dev *cru)
1175 {
1176 media_device_unregister(&cru->mdev);
1177 video_unregister_device(&cru->vdev);
1178 }
1179
1180 int rzg2l_cru_video_register(struct rzg2l_cru_dev *cru)
1181 {
1182 struct video_device *vdev = &cru->vdev;
1183 int ret;
1184
1185 if (video_is_registered(&cru->vdev)) {
1186 struct media_entity *entity;
1187
1188 entity = &cru->vdev.entity;
1189 if (!entity->graph_obj.mdev)
1190 entity->graph_obj.mdev = &cru->mdev;
1191 return 0;
1192 }
1193
1194 rzg2l_cru_v4l2_init(cru);
1195 video_set_drvdata(vdev, cru);
1196 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1197 if (ret) {
1198 dev_err(cru->dev, "Failed to register video device\n");
1199 return ret;
1200 }
1201
1202 ret = media_device_register(&cru->mdev);
1203 if (ret) {
1204 video_unregister_device(&cru->vdev);
1205 return ret;
1206 }
1207
1208 return 0;
1209 }