1 // SPDX-License-Identifier: GPL-2.0+
3 * vsp1_histo.c -- R-Car VSP1 Histogram API
5 * Copyright (C) 2016 Renesas Electronics Corporation
6 * Copyright (C) 2016 Laurent Pinchart
8 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
11 #include <linux/device.h>
12 #include <linux/gfp.h>
14 #include <media/v4l2-ioctl.h>
15 #include <media/v4l2-subdev.h>
16 #include <media/videobuf2-vmalloc.h>
19 #include "vsp1_histo.h"
20 #include "vsp1_pipe.h"
22 #define HISTO_MIN_SIZE 4U
23 #define HISTO_MAX_SIZE 8192U
25 /* -----------------------------------------------------------------------------
29 static inline struct vsp1_histogram_buffer *
30 to_vsp1_histogram_buffer(struct vb2_v4l2_buffer *vbuf)
32 return container_of(vbuf, struct vsp1_histogram_buffer, buf);
35 struct vsp1_histogram_buffer *
36 vsp1_histogram_buffer_get(struct vsp1_histogram *histo)
38 struct vsp1_histogram_buffer *buf = NULL;
41 spin_lock_irqsave(&histo->irqlock, flags);
43 if (list_empty(&histo->irqqueue))
46 buf = list_first_entry(&histo->irqqueue, struct vsp1_histogram_buffer,
48 list_del(&buf->queue);
49 histo->readout = true;
52 spin_unlock_irqrestore(&histo->irqlock, flags);
56 void vsp1_histogram_buffer_complete(struct vsp1_histogram *histo,
57 struct vsp1_histogram_buffer *buf,
60 struct vsp1_pipeline *pipe = histo->entity.pipe;
64 * The pipeline pointer is guaranteed to be valid as this function is
65 * called from the frame completion interrupt handler, which can only
66 * occur when video streaming is active.
68 buf->buf.sequence = pipe->sequence;
69 buf->buf.vb2_buf.timestamp = ktime_get_ns();
70 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, size);
71 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
73 spin_lock_irqsave(&histo->irqlock, flags);
74 histo->readout = false;
75 wake_up(&histo->wait_queue);
76 spin_unlock_irqrestore(&histo->irqlock, flags);
79 /* -----------------------------------------------------------------------------
80 * videobuf2 Queue Operations
83 static int histo_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
84 unsigned int *nplanes, unsigned int sizes[],
85 struct device *alloc_devs[])
87 struct vsp1_histogram *histo = vb2_get_drv_priv(vq);
93 if (sizes[0] < histo->data_size)
100 sizes[0] = histo->data_size;
105 static int histo_buffer_prepare(struct vb2_buffer *vb)
107 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
108 struct vsp1_histogram *histo = vb2_get_drv_priv(vb->vb2_queue);
109 struct vsp1_histogram_buffer *buf = to_vsp1_histogram_buffer(vbuf);
111 if (vb->num_planes != 1)
114 if (vb2_plane_size(vb, 0) < histo->data_size)
117 buf->addr = vb2_plane_vaddr(vb, 0);
122 static void histo_buffer_queue(struct vb2_buffer *vb)
124 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
125 struct vsp1_histogram *histo = vb2_get_drv_priv(vb->vb2_queue);
126 struct vsp1_histogram_buffer *buf = to_vsp1_histogram_buffer(vbuf);
129 spin_lock_irqsave(&histo->irqlock, flags);
130 list_add_tail(&buf->queue, &histo->irqqueue);
131 spin_unlock_irqrestore(&histo->irqlock, flags);
134 static int histo_start_streaming(struct vb2_queue *vq, unsigned int count)
139 static void histo_stop_streaming(struct vb2_queue *vq)
141 struct vsp1_histogram *histo = vb2_get_drv_priv(vq);
142 struct vsp1_histogram_buffer *buffer;
145 spin_lock_irqsave(&histo->irqlock, flags);
147 /* Remove all buffers from the IRQ queue. */
148 list_for_each_entry(buffer, &histo->irqqueue, queue)
149 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
150 INIT_LIST_HEAD(&histo->irqqueue);
152 /* Wait for the buffer being read out (if any) to complete. */
153 wait_event_lock_irq(histo->wait_queue, !histo->readout, histo->irqlock);
155 spin_unlock_irqrestore(&histo->irqlock, flags);
158 static const struct vb2_ops histo_video_queue_qops = {
159 .queue_setup = histo_queue_setup,
160 .buf_prepare = histo_buffer_prepare,
161 .buf_queue = histo_buffer_queue,
162 .wait_prepare = vb2_ops_wait_prepare,
163 .wait_finish = vb2_ops_wait_finish,
164 .start_streaming = histo_start_streaming,
165 .stop_streaming = histo_stop_streaming,
168 /* -----------------------------------------------------------------------------
169 * V4L2 Subdevice Operations
172 static int histo_enum_mbus_code(struct v4l2_subdev *subdev,
173 struct v4l2_subdev_state *sd_state,
174 struct v4l2_subdev_mbus_code_enum *code)
176 struct vsp1_histogram *histo = subdev_to_histo(subdev);
178 if (code->pad == HISTO_PAD_SOURCE) {
179 code->code = MEDIA_BUS_FMT_FIXED;
183 return vsp1_subdev_enum_mbus_code(subdev, sd_state, code,
188 static int histo_enum_frame_size(struct v4l2_subdev *subdev,
189 struct v4l2_subdev_state *sd_state,
190 struct v4l2_subdev_frame_size_enum *fse)
192 if (fse->pad != HISTO_PAD_SINK)
195 return vsp1_subdev_enum_frame_size(subdev, sd_state, fse,
197 HISTO_MIN_SIZE, HISTO_MAX_SIZE,
201 static int histo_get_selection(struct v4l2_subdev *subdev,
202 struct v4l2_subdev_state *sd_state,
203 struct v4l2_subdev_selection *sel)
205 struct vsp1_histogram *histo = subdev_to_histo(subdev);
206 struct v4l2_subdev_state *config;
207 struct v4l2_mbus_framefmt *format;
208 struct v4l2_rect *crop;
211 if (sel->pad != HISTO_PAD_SINK)
214 mutex_lock(&histo->entity.lock);
216 config = vsp1_entity_get_pad_config(&histo->entity, sd_state,
223 switch (sel->target) {
224 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
225 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
226 crop = vsp1_entity_get_pad_selection(&histo->entity, config,
231 sel->r.width = crop->width;
232 sel->r.height = crop->height;
235 case V4L2_SEL_TGT_CROP_BOUNDS:
236 case V4L2_SEL_TGT_CROP_DEFAULT:
237 format = vsp1_entity_get_pad_format(&histo->entity, config,
241 sel->r.width = format->width;
242 sel->r.height = format->height;
245 case V4L2_SEL_TGT_COMPOSE:
246 case V4L2_SEL_TGT_CROP:
247 sel->r = *vsp1_entity_get_pad_selection(&histo->entity, config,
248 sel->pad, sel->target);
257 mutex_unlock(&histo->entity.lock);
261 static int histo_set_crop(struct v4l2_subdev *subdev,
262 struct v4l2_subdev_state *sd_state,
263 struct v4l2_subdev_selection *sel)
265 struct vsp1_histogram *histo = subdev_to_histo(subdev);
266 struct v4l2_mbus_framefmt *format;
267 struct v4l2_rect *selection;
269 /* The crop rectangle must be inside the input frame. */
270 format = vsp1_entity_get_pad_format(&histo->entity, sd_state,
272 sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
273 sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
274 sel->r.width = clamp_t(unsigned int, sel->r.width, HISTO_MIN_SIZE,
275 format->width - sel->r.left);
276 sel->r.height = clamp_t(unsigned int, sel->r.height, HISTO_MIN_SIZE,
277 format->height - sel->r.top);
279 /* Set the crop rectangle and reset the compose rectangle. */
280 selection = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
281 sel->pad, V4L2_SEL_TGT_CROP);
284 selection = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
286 V4L2_SEL_TGT_COMPOSE);
292 static int histo_set_compose(struct v4l2_subdev *subdev,
293 struct v4l2_subdev_state *sd_state,
294 struct v4l2_subdev_selection *sel)
296 struct vsp1_histogram *histo = subdev_to_histo(subdev);
297 struct v4l2_rect *compose;
298 struct v4l2_rect *crop;
302 * The compose rectangle is used to configure downscaling, the top left
303 * corner is fixed to (0,0) and the size to 1/2 or 1/4 of the crop
309 crop = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
314 * Clamp the width and height to acceptable values first and then
315 * compute the closest rounded dividing ratio.
317 * Ratio Rounded ratio
318 * --------------------------
323 * The rounded ratio can be computed using
325 * 1 << (ceil(ratio * 2) / 3)
327 sel->r.width = clamp(sel->r.width, crop->width / 4, crop->width);
328 ratio = 1 << (crop->width * 2 / sel->r.width / 3);
329 sel->r.width = crop->width / ratio;
332 sel->r.height = clamp(sel->r.height, crop->height / 4, crop->height);
333 ratio = 1 << (crop->height * 2 / sel->r.height / 3);
334 sel->r.height = crop->height / ratio;
336 compose = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
338 V4L2_SEL_TGT_COMPOSE);
344 static int histo_set_selection(struct v4l2_subdev *subdev,
345 struct v4l2_subdev_state *sd_state,
346 struct v4l2_subdev_selection *sel)
348 struct vsp1_histogram *histo = subdev_to_histo(subdev);
349 struct v4l2_subdev_state *config;
352 if (sel->pad != HISTO_PAD_SINK)
355 mutex_lock(&histo->entity.lock);
357 config = vsp1_entity_get_pad_config(&histo->entity, sd_state,
364 if (sel->target == V4L2_SEL_TGT_CROP)
365 ret = histo_set_crop(subdev, config, sel);
366 else if (sel->target == V4L2_SEL_TGT_COMPOSE)
367 ret = histo_set_compose(subdev, config, sel);
372 mutex_unlock(&histo->entity.lock);
376 static int histo_get_format(struct v4l2_subdev *subdev,
377 struct v4l2_subdev_state *sd_state,
378 struct v4l2_subdev_format *fmt)
380 if (fmt->pad == HISTO_PAD_SOURCE) {
381 fmt->format.code = MEDIA_BUS_FMT_FIXED;
382 fmt->format.width = 0;
383 fmt->format.height = 0;
384 fmt->format.field = V4L2_FIELD_NONE;
385 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
389 return vsp1_subdev_get_pad_format(subdev, sd_state, fmt);
392 static int histo_set_format(struct v4l2_subdev *subdev,
393 struct v4l2_subdev_state *sd_state,
394 struct v4l2_subdev_format *fmt)
396 struct vsp1_histogram *histo = subdev_to_histo(subdev);
398 if (fmt->pad != HISTO_PAD_SINK)
399 return histo_get_format(subdev, sd_state, fmt);
401 return vsp1_subdev_set_pad_format(subdev, sd_state, fmt,
402 histo->formats, histo->num_formats,
403 HISTO_MIN_SIZE, HISTO_MIN_SIZE,
404 HISTO_MAX_SIZE, HISTO_MAX_SIZE);
407 static const struct v4l2_subdev_pad_ops histo_pad_ops = {
408 .enum_mbus_code = histo_enum_mbus_code,
409 .enum_frame_size = histo_enum_frame_size,
410 .get_fmt = histo_get_format,
411 .set_fmt = histo_set_format,
412 .get_selection = histo_get_selection,
413 .set_selection = histo_set_selection,
416 static const struct v4l2_subdev_ops histo_ops = {
417 .pad = &histo_pad_ops,
420 /* -----------------------------------------------------------------------------
424 static int histo_v4l2_querycap(struct file *file, void *fh,
425 struct v4l2_capability *cap)
427 struct v4l2_fh *vfh = file->private_data;
428 struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
430 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
431 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
432 | V4L2_CAP_VIDEO_OUTPUT_MPLANE
433 | V4L2_CAP_META_CAPTURE;
435 strscpy(cap->driver, "vsp1", sizeof(cap->driver));
436 strscpy(cap->card, histo->video.name, sizeof(cap->card));
437 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
438 dev_name(histo->entity.vsp1->dev));
443 static int histo_v4l2_enum_format(struct file *file, void *fh,
444 struct v4l2_fmtdesc *f)
446 struct v4l2_fh *vfh = file->private_data;
447 struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
449 if (f->index > 0 || f->type != histo->queue.type)
452 f->pixelformat = histo->meta_format;
457 static int histo_v4l2_get_format(struct file *file, void *fh,
458 struct v4l2_format *format)
460 struct v4l2_fh *vfh = file->private_data;
461 struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
462 struct v4l2_meta_format *meta = &format->fmt.meta;
464 if (format->type != histo->queue.type)
467 memset(meta, 0, sizeof(*meta));
469 meta->dataformat = histo->meta_format;
470 meta->buffersize = histo->data_size;
475 static const struct v4l2_ioctl_ops histo_v4l2_ioctl_ops = {
476 .vidioc_querycap = histo_v4l2_querycap,
477 .vidioc_enum_fmt_meta_cap = histo_v4l2_enum_format,
478 .vidioc_g_fmt_meta_cap = histo_v4l2_get_format,
479 .vidioc_s_fmt_meta_cap = histo_v4l2_get_format,
480 .vidioc_try_fmt_meta_cap = histo_v4l2_get_format,
481 .vidioc_reqbufs = vb2_ioctl_reqbufs,
482 .vidioc_querybuf = vb2_ioctl_querybuf,
483 .vidioc_qbuf = vb2_ioctl_qbuf,
484 .vidioc_dqbuf = vb2_ioctl_dqbuf,
485 .vidioc_create_bufs = vb2_ioctl_create_bufs,
486 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
487 .vidioc_streamon = vb2_ioctl_streamon,
488 .vidioc_streamoff = vb2_ioctl_streamoff,
491 /* -----------------------------------------------------------------------------
492 * V4L2 File Operations
495 static const struct v4l2_file_operations histo_v4l2_fops = {
496 .owner = THIS_MODULE,
497 .unlocked_ioctl = video_ioctl2,
498 .open = v4l2_fh_open,
499 .release = vb2_fop_release,
500 .poll = vb2_fop_poll,
501 .mmap = vb2_fop_mmap,
504 static void vsp1_histogram_cleanup(struct vsp1_histogram *histo)
506 if (video_is_registered(&histo->video))
507 video_unregister_device(&histo->video);
509 media_entity_cleanup(&histo->video.entity);
512 void vsp1_histogram_destroy(struct vsp1_entity *entity)
514 struct vsp1_histogram *histo = subdev_to_histo(&entity->subdev);
516 vsp1_histogram_cleanup(histo);
519 int vsp1_histogram_init(struct vsp1_device *vsp1, struct vsp1_histogram *histo,
520 enum vsp1_entity_type type, const char *name,
521 const struct vsp1_entity_operations *ops,
522 const unsigned int *formats, unsigned int num_formats,
523 size_t data_size, u32 meta_format)
527 histo->formats = formats;
528 histo->num_formats = num_formats;
529 histo->data_size = data_size;
530 histo->meta_format = meta_format;
532 histo->pad.flags = MEDIA_PAD_FL_SINK;
533 histo->video.vfl_dir = VFL_DIR_RX;
535 mutex_init(&histo->lock);
536 spin_lock_init(&histo->irqlock);
537 INIT_LIST_HEAD(&histo->irqqueue);
538 init_waitqueue_head(&histo->wait_queue);
540 /* Initialize the VSP entity... */
541 histo->entity.ops = ops;
542 histo->entity.type = type;
544 ret = vsp1_entity_init(vsp1, &histo->entity, name, 2, &histo_ops,
545 MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
549 /* ... and the media entity... */
550 ret = media_entity_pads_init(&histo->video.entity, 1, &histo->pad);
554 /* ... and the video node... */
555 histo->video.v4l2_dev = &vsp1->v4l2_dev;
556 histo->video.fops = &histo_v4l2_fops;
557 snprintf(histo->video.name, sizeof(histo->video.name),
558 "%s histo", histo->entity.subdev.name);
559 histo->video.vfl_type = VFL_TYPE_VIDEO;
560 histo->video.release = video_device_release_empty;
561 histo->video.ioctl_ops = &histo_v4l2_ioctl_ops;
562 histo->video.device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
564 video_set_drvdata(&histo->video, histo);
566 /* ... and the buffers queue... */
567 histo->queue.type = V4L2_BUF_TYPE_META_CAPTURE;
568 histo->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
569 histo->queue.lock = &histo->lock;
570 histo->queue.drv_priv = histo;
571 histo->queue.buf_struct_size = sizeof(struct vsp1_histogram_buffer);
572 histo->queue.ops = &histo_video_queue_qops;
573 histo->queue.mem_ops = &vb2_vmalloc_memops;
574 histo->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
575 histo->queue.dev = vsp1->dev;
576 ret = vb2_queue_init(&histo->queue);
578 dev_err(vsp1->dev, "failed to initialize vb2 queue\n");
582 /* ... and register the video device. */
583 histo->video.queue = &histo->queue;
584 ret = video_register_device(&histo->video, VFL_TYPE_VIDEO, -1);
586 dev_err(vsp1->dev, "failed to register video device\n");
593 vsp1_histogram_cleanup(histo);