xref: /linux/drivers/media/platform/renesas/vsp1/vsp1_histo.c (revision 0aaf7db087267734da8b0353533ff67b1e4080df)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_histo.c  --  R-Car VSP1 Histogram API
4  *
5  * Copyright (C) 2016 Renesas Electronics Corporation
6  * Copyright (C) 2016 Laurent Pinchart
7  *
8  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9  */
10 
11 #include <linux/device.h>
12 #include <linux/gfp.h>
13 
14 #include <media/v4l2-ioctl.h>
15 #include <media/v4l2-subdev.h>
16 #include <media/videobuf2-vmalloc.h>
17 
18 #include "vsp1.h"
19 #include "vsp1_histo.h"
20 #include "vsp1_pipe.h"
21 
22 #define HISTO_MIN_SIZE				4U
23 #define HISTO_MAX_SIZE				8192U
24 
25 /* -----------------------------------------------------------------------------
26  * Buffer Operations
27  */
28 
29 static inline struct vsp1_histogram_buffer *
30 to_vsp1_histogram_buffer(struct vb2_v4l2_buffer *vbuf)
31 {
32 	return container_of(vbuf, struct vsp1_histogram_buffer, buf);
33 }
34 
35 struct vsp1_histogram_buffer *
36 vsp1_histogram_buffer_get(struct vsp1_histogram *histo)
37 {
38 	struct vsp1_histogram_buffer *buf = NULL;
39 
40 	spin_lock(&histo->irqlock);
41 
42 	if (list_empty(&histo->irqqueue))
43 		goto done;
44 
45 	buf = list_first_entry(&histo->irqqueue, struct vsp1_histogram_buffer,
46 			       queue);
47 	list_del(&buf->queue);
48 	histo->readout = true;
49 
50 done:
51 	spin_unlock(&histo->irqlock);
52 	return buf;
53 }
54 
55 void vsp1_histogram_buffer_complete(struct vsp1_histogram *histo,
56 				    struct vsp1_histogram_buffer *buf,
57 				    size_t size)
58 {
59 	struct vsp1_pipeline *pipe = histo->entity.pipe;
60 
61 	/*
62 	 * The pipeline pointer is guaranteed to be valid as this function is
63 	 * called from the frame completion interrupt handler, which can only
64 	 * occur when video streaming is active.
65 	 */
66 	buf->buf.sequence = pipe->sequence;
67 	buf->buf.vb2_buf.timestamp = ktime_get_ns();
68 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, size);
69 	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
70 
71 	spin_lock(&histo->irqlock);
72 	histo->readout = false;
73 	wake_up(&histo->wait_queue);
74 	spin_unlock(&histo->irqlock);
75 }
76 
77 /* -----------------------------------------------------------------------------
78  * videobuf2 Queue Operations
79  */
80 
81 static int histo_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
82 			     unsigned int *nplanes, unsigned int sizes[],
83 			     struct device *alloc_devs[])
84 {
85 	struct vsp1_histogram *histo = vb2_get_drv_priv(vq);
86 
87 	if (*nplanes) {
88 		if (*nplanes != 1)
89 			return -EINVAL;
90 
91 		if (sizes[0] < histo->data_size)
92 			return -EINVAL;
93 
94 		return 0;
95 	}
96 
97 	*nplanes = 1;
98 	sizes[0] = histo->data_size;
99 
100 	return 0;
101 }
102 
103 static int histo_buffer_prepare(struct vb2_buffer *vb)
104 {
105 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
106 	struct vsp1_histogram *histo = vb2_get_drv_priv(vb->vb2_queue);
107 	struct vsp1_histogram_buffer *buf = to_vsp1_histogram_buffer(vbuf);
108 
109 	if (vb->num_planes != 1)
110 		return -EINVAL;
111 
112 	if (vb2_plane_size(vb, 0) < histo->data_size)
113 		return -EINVAL;
114 
115 	buf->addr = vb2_plane_vaddr(vb, 0);
116 
117 	return 0;
118 }
119 
120 static void histo_buffer_queue(struct vb2_buffer *vb)
121 {
122 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
123 	struct vsp1_histogram *histo = vb2_get_drv_priv(vb->vb2_queue);
124 	struct vsp1_histogram_buffer *buf = to_vsp1_histogram_buffer(vbuf);
125 
126 	spin_lock_irq(&histo->irqlock);
127 	list_add_tail(&buf->queue, &histo->irqqueue);
128 	spin_unlock_irq(&histo->irqlock);
129 }
130 
131 static int histo_start_streaming(struct vb2_queue *vq, unsigned int count)
132 {
133 	return 0;
134 }
135 
136 static void histo_stop_streaming(struct vb2_queue *vq)
137 {
138 	struct vsp1_histogram *histo = vb2_get_drv_priv(vq);
139 	struct vsp1_histogram_buffer *buffer;
140 
141 	spin_lock_irq(&histo->irqlock);
142 
143 	/* Remove all buffers from the IRQ queue. */
144 	list_for_each_entry(buffer, &histo->irqqueue, queue)
145 		vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
146 	INIT_LIST_HEAD(&histo->irqqueue);
147 
148 	/* Wait for the buffer being read out (if any) to complete. */
149 	wait_event_lock_irq(histo->wait_queue, !histo->readout, histo->irqlock);
150 
151 	spin_unlock_irq(&histo->irqlock);
152 }
153 
154 static const struct vb2_ops histo_video_queue_qops = {
155 	.queue_setup = histo_queue_setup,
156 	.buf_prepare = histo_buffer_prepare,
157 	.buf_queue = histo_buffer_queue,
158 	.wait_prepare = vb2_ops_wait_prepare,
159 	.wait_finish = vb2_ops_wait_finish,
160 	.start_streaming = histo_start_streaming,
161 	.stop_streaming = histo_stop_streaming,
162 };
163 
164 /* -----------------------------------------------------------------------------
165  * V4L2 Subdevice Operations
166  */
167 
168 static int histo_enum_mbus_code(struct v4l2_subdev *subdev,
169 				struct v4l2_subdev_state *sd_state,
170 				struct v4l2_subdev_mbus_code_enum *code)
171 {
172 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
173 
174 	if (code->pad == HISTO_PAD_SOURCE) {
175 		code->code = MEDIA_BUS_FMT_FIXED;
176 		return 0;
177 	}
178 
179 	return vsp1_subdev_enum_mbus_code(subdev, sd_state, code,
180 					  histo->formats,
181 					  histo->num_formats);
182 }
183 
184 static int histo_enum_frame_size(struct v4l2_subdev *subdev,
185 				 struct v4l2_subdev_state *sd_state,
186 				 struct v4l2_subdev_frame_size_enum *fse)
187 {
188 	if (fse->pad != HISTO_PAD_SINK)
189 		return -EINVAL;
190 
191 	return vsp1_subdev_enum_frame_size(subdev, sd_state, fse,
192 					   HISTO_MIN_SIZE,
193 					   HISTO_MIN_SIZE, HISTO_MAX_SIZE,
194 					   HISTO_MAX_SIZE);
195 }
196 
197 static int histo_get_selection(struct v4l2_subdev *subdev,
198 			       struct v4l2_subdev_state *sd_state,
199 			       struct v4l2_subdev_selection *sel)
200 {
201 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
202 	struct v4l2_subdev_state *state;
203 	struct v4l2_mbus_framefmt *format;
204 	struct v4l2_rect *crop;
205 	int ret = 0;
206 
207 	if (sel->pad != HISTO_PAD_SINK)
208 		return -EINVAL;
209 
210 	mutex_lock(&histo->entity.lock);
211 
212 	state = vsp1_entity_get_state(&histo->entity, sd_state, sel->which);
213 	if (!state) {
214 		ret = -EINVAL;
215 		goto done;
216 	}
217 
218 	switch (sel->target) {
219 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
220 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
221 		crop = vsp1_entity_get_pad_selection(&histo->entity, state,
222 						     HISTO_PAD_SINK,
223 						     V4L2_SEL_TGT_CROP);
224 		sel->r.left = 0;
225 		sel->r.top = 0;
226 		sel->r.width = crop->width;
227 		sel->r.height = crop->height;
228 		break;
229 
230 	case V4L2_SEL_TGT_CROP_BOUNDS:
231 	case V4L2_SEL_TGT_CROP_DEFAULT:
232 		format = v4l2_subdev_state_get_format(state, HISTO_PAD_SINK);
233 		sel->r.left = 0;
234 		sel->r.top = 0;
235 		sel->r.width = format->width;
236 		sel->r.height = format->height;
237 		break;
238 
239 	case V4L2_SEL_TGT_COMPOSE:
240 	case V4L2_SEL_TGT_CROP:
241 		sel->r = *vsp1_entity_get_pad_selection(&histo->entity, state,
242 							sel->pad, sel->target);
243 		break;
244 
245 	default:
246 		ret = -EINVAL;
247 		break;
248 	}
249 
250 done:
251 	mutex_unlock(&histo->entity.lock);
252 	return ret;
253 }
254 
255 static int histo_set_crop(struct v4l2_subdev *subdev,
256 			  struct v4l2_subdev_state *sd_state,
257 			  struct v4l2_subdev_selection *sel)
258 {
259 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
260 	struct v4l2_mbus_framefmt *format;
261 	struct v4l2_rect *selection;
262 
263 	/* The crop rectangle must be inside the input frame. */
264 	format = v4l2_subdev_state_get_format(sd_state, HISTO_PAD_SINK);
265 	sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
266 	sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
267 	sel->r.width = clamp_t(unsigned int, sel->r.width, HISTO_MIN_SIZE,
268 			       format->width - sel->r.left);
269 	sel->r.height = clamp_t(unsigned int, sel->r.height, HISTO_MIN_SIZE,
270 				format->height - sel->r.top);
271 
272 	/* Set the crop rectangle and reset the compose rectangle. */
273 	selection = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
274 						  sel->pad, V4L2_SEL_TGT_CROP);
275 	*selection = sel->r;
276 
277 	selection = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
278 						  sel->pad,
279 						  V4L2_SEL_TGT_COMPOSE);
280 	*selection = sel->r;
281 
282 	return 0;
283 }
284 
285 static int histo_set_compose(struct v4l2_subdev *subdev,
286 			     struct v4l2_subdev_state *sd_state,
287 			     struct v4l2_subdev_selection *sel)
288 {
289 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
290 	struct v4l2_rect *compose;
291 	struct v4l2_rect *crop;
292 	unsigned int ratio;
293 
294 	/*
295 	 * The compose rectangle is used to configure downscaling, the top left
296 	 * corner is fixed to (0,0) and the size to 1/2 or 1/4 of the crop
297 	 * rectangle.
298 	 */
299 	sel->r.left = 0;
300 	sel->r.top = 0;
301 
302 	crop = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
303 					     sel->pad,
304 					     V4L2_SEL_TGT_CROP);
305 
306 	/*
307 	 * Clamp the width and height to acceptable values first and then
308 	 * compute the closest rounded dividing ratio.
309 	 *
310 	 * Ratio	Rounded ratio
311 	 * --------------------------
312 	 * [1.0 1.5[	1
313 	 * [1.5 3.0[	2
314 	 * [3.0 4.0]	4
315 	 *
316 	 * The rounded ratio can be computed using
317 	 *
318 	 * 1 << (ceil(ratio * 2) / 3)
319 	 */
320 	sel->r.width = clamp(sel->r.width, crop->width / 4, crop->width);
321 	ratio = 1 << (crop->width * 2 / sel->r.width / 3);
322 	sel->r.width = crop->width / ratio;
323 
324 
325 	sel->r.height = clamp(sel->r.height, crop->height / 4, crop->height);
326 	ratio = 1 << (crop->height * 2 / sel->r.height / 3);
327 	sel->r.height = crop->height / ratio;
328 
329 	compose = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
330 						sel->pad,
331 						V4L2_SEL_TGT_COMPOSE);
332 	*compose = sel->r;
333 
334 	return 0;
335 }
336 
337 static int histo_set_selection(struct v4l2_subdev *subdev,
338 			       struct v4l2_subdev_state *sd_state,
339 			       struct v4l2_subdev_selection *sel)
340 {
341 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
342 	struct v4l2_subdev_state *state;
343 	int ret;
344 
345 	if (sel->pad != HISTO_PAD_SINK)
346 		return -EINVAL;
347 
348 	mutex_lock(&histo->entity.lock);
349 
350 	state = vsp1_entity_get_state(&histo->entity, sd_state, sel->which);
351 	if (!state) {
352 		ret = -EINVAL;
353 		goto done;
354 	}
355 
356 	if (sel->target == V4L2_SEL_TGT_CROP)
357 		ret = histo_set_crop(subdev, state, sel);
358 	else if (sel->target == V4L2_SEL_TGT_COMPOSE)
359 		ret = histo_set_compose(subdev, state, sel);
360 	else
361 		ret = -EINVAL;
362 
363 done:
364 	mutex_unlock(&histo->entity.lock);
365 	return ret;
366 }
367 
368 static int histo_get_format(struct v4l2_subdev *subdev,
369 			    struct v4l2_subdev_state *sd_state,
370 			    struct v4l2_subdev_format *fmt)
371 {
372 	if (fmt->pad == HISTO_PAD_SOURCE) {
373 		fmt->format.code = MEDIA_BUS_FMT_FIXED;
374 		fmt->format.width = 0;
375 		fmt->format.height = 0;
376 		fmt->format.field = V4L2_FIELD_NONE;
377 		fmt->format.colorspace = V4L2_COLORSPACE_RAW;
378 		return 0;
379 	}
380 
381 	return vsp1_subdev_get_pad_format(subdev, sd_state, fmt);
382 }
383 
384 static int histo_set_format(struct v4l2_subdev *subdev,
385 			    struct v4l2_subdev_state *sd_state,
386 			    struct v4l2_subdev_format *fmt)
387 {
388 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
389 
390 	if (fmt->pad != HISTO_PAD_SINK)
391 		return histo_get_format(subdev, sd_state, fmt);
392 
393 	return vsp1_subdev_set_pad_format(subdev, sd_state, fmt,
394 					  histo->formats, histo->num_formats,
395 					  HISTO_MIN_SIZE, HISTO_MIN_SIZE,
396 					  HISTO_MAX_SIZE, HISTO_MAX_SIZE);
397 }
398 
399 static const struct v4l2_subdev_pad_ops histo_pad_ops = {
400 	.enum_mbus_code = histo_enum_mbus_code,
401 	.enum_frame_size = histo_enum_frame_size,
402 	.get_fmt = histo_get_format,
403 	.set_fmt = histo_set_format,
404 	.get_selection = histo_get_selection,
405 	.set_selection = histo_set_selection,
406 };
407 
408 static const struct v4l2_subdev_ops histo_ops = {
409 	.pad    = &histo_pad_ops,
410 };
411 
412 /* -----------------------------------------------------------------------------
413  * V4L2 ioctls
414  */
415 
416 static int histo_v4l2_querycap(struct file *file, void *fh,
417 			       struct v4l2_capability *cap)
418 {
419 	struct v4l2_fh *vfh = file->private_data;
420 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
421 
422 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
423 			  | V4L2_CAP_VIDEO_CAPTURE_MPLANE
424 			  | V4L2_CAP_VIDEO_OUTPUT_MPLANE
425 			  | V4L2_CAP_META_CAPTURE;
426 
427 	strscpy(cap->driver, "vsp1", sizeof(cap->driver));
428 	strscpy(cap->card, histo->video.name, sizeof(cap->card));
429 
430 	return 0;
431 }
432 
433 static int histo_v4l2_enum_format(struct file *file, void *fh,
434 				  struct v4l2_fmtdesc *f)
435 {
436 	struct v4l2_fh *vfh = file->private_data;
437 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
438 
439 	if (f->index > 0 || f->type != histo->queue.type)
440 		return -EINVAL;
441 
442 	f->pixelformat = histo->meta_format;
443 
444 	return 0;
445 }
446 
447 static int histo_v4l2_get_format(struct file *file, void *fh,
448 				 struct v4l2_format *format)
449 {
450 	struct v4l2_fh *vfh = file->private_data;
451 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
452 	struct v4l2_meta_format *meta = &format->fmt.meta;
453 
454 	if (format->type != histo->queue.type)
455 		return -EINVAL;
456 
457 	memset(meta, 0, sizeof(*meta));
458 
459 	meta->dataformat = histo->meta_format;
460 	meta->buffersize = histo->data_size;
461 
462 	return 0;
463 }
464 
465 static const struct v4l2_ioctl_ops histo_v4l2_ioctl_ops = {
466 	.vidioc_querycap		= histo_v4l2_querycap,
467 	.vidioc_enum_fmt_meta_cap	= histo_v4l2_enum_format,
468 	.vidioc_g_fmt_meta_cap		= histo_v4l2_get_format,
469 	.vidioc_s_fmt_meta_cap		= histo_v4l2_get_format,
470 	.vidioc_try_fmt_meta_cap	= histo_v4l2_get_format,
471 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
472 	.vidioc_querybuf		= vb2_ioctl_querybuf,
473 	.vidioc_qbuf			= vb2_ioctl_qbuf,
474 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
475 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
476 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
477 	.vidioc_streamon		= vb2_ioctl_streamon,
478 	.vidioc_streamoff		= vb2_ioctl_streamoff,
479 };
480 
481 /* -----------------------------------------------------------------------------
482  * V4L2 File Operations
483  */
484 
485 static const struct v4l2_file_operations histo_v4l2_fops = {
486 	.owner = THIS_MODULE,
487 	.unlocked_ioctl = video_ioctl2,
488 	.open = v4l2_fh_open,
489 	.release = vb2_fop_release,
490 	.poll = vb2_fop_poll,
491 	.mmap = vb2_fop_mmap,
492 };
493 
494 static void vsp1_histogram_cleanup(struct vsp1_histogram *histo)
495 {
496 	if (video_is_registered(&histo->video))
497 		video_unregister_device(&histo->video);
498 
499 	media_entity_cleanup(&histo->video.entity);
500 }
501 
502 void vsp1_histogram_destroy(struct vsp1_entity *entity)
503 {
504 	struct vsp1_histogram *histo = subdev_to_histo(&entity->subdev);
505 
506 	vsp1_histogram_cleanup(histo);
507 }
508 
509 int vsp1_histogram_init(struct vsp1_device *vsp1, struct vsp1_histogram *histo,
510 			enum vsp1_entity_type type, const char *name,
511 			const struct vsp1_entity_operations *ops,
512 			const unsigned int *formats, unsigned int num_formats,
513 			size_t data_size, u32 meta_format)
514 {
515 	int ret;
516 
517 	histo->formats = formats;
518 	histo->num_formats = num_formats;
519 	histo->data_size = data_size;
520 	histo->meta_format = meta_format;
521 
522 	histo->pad.flags = MEDIA_PAD_FL_SINK;
523 	histo->video.vfl_dir = VFL_DIR_RX;
524 
525 	mutex_init(&histo->lock);
526 	spin_lock_init(&histo->irqlock);
527 	INIT_LIST_HEAD(&histo->irqqueue);
528 	init_waitqueue_head(&histo->wait_queue);
529 
530 	/* Initialize the VSP entity... */
531 	histo->entity.ops = ops;
532 	histo->entity.type = type;
533 
534 	ret = vsp1_entity_init(vsp1, &histo->entity, name, 2, &histo_ops,
535 			       MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
536 	if (ret < 0)
537 		return ret;
538 
539 	/* ... and the media entity... */
540 	ret = media_entity_pads_init(&histo->video.entity, 1, &histo->pad);
541 	if (ret < 0)
542 		return ret;
543 
544 	/* ... and the video node... */
545 	histo->video.v4l2_dev = &vsp1->v4l2_dev;
546 	histo->video.fops = &histo_v4l2_fops;
547 	snprintf(histo->video.name, sizeof(histo->video.name),
548 		 "%s histo", histo->entity.subdev.name);
549 	histo->video.vfl_type = VFL_TYPE_VIDEO;
550 	histo->video.release = video_device_release_empty;
551 	histo->video.ioctl_ops = &histo_v4l2_ioctl_ops;
552 	histo->video.device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
553 
554 	video_set_drvdata(&histo->video, histo);
555 
556 	/* ... and the buffers queue... */
557 	histo->queue.type = V4L2_BUF_TYPE_META_CAPTURE;
558 	histo->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
559 	histo->queue.lock = &histo->lock;
560 	histo->queue.drv_priv = histo;
561 	histo->queue.buf_struct_size = sizeof(struct vsp1_histogram_buffer);
562 	histo->queue.ops = &histo_video_queue_qops;
563 	histo->queue.mem_ops = &vb2_vmalloc_memops;
564 	histo->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
565 	histo->queue.dev = vsp1->dev;
566 	ret = vb2_queue_init(&histo->queue);
567 	if (ret < 0) {
568 		dev_err(vsp1->dev, "failed to initialize vb2 queue\n");
569 		goto error;
570 	}
571 
572 	/* ... and register the video device. */
573 	histo->video.queue = &histo->queue;
574 	ret = video_register_device(&histo->video, VFL_TYPE_VIDEO, -1);
575 	if (ret < 0) {
576 		dev_err(vsp1->dev, "failed to register video device\n");
577 		goto error;
578 	}
579 
580 	return 0;
581 
582 error:
583 	vsp1_histogram_cleanup(histo);
584 	return ret;
585 }
586