xref: /linux/drivers/media/platform/renesas/vsp1/vsp1_histo.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
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 = v4l2_subdev_state_get_crop(state, HISTO_PAD_SINK);
222 		sel->r.left = 0;
223 		sel->r.top = 0;
224 		sel->r.width = crop->width;
225 		sel->r.height = crop->height;
226 		break;
227 
228 	case V4L2_SEL_TGT_CROP_BOUNDS:
229 	case V4L2_SEL_TGT_CROP_DEFAULT:
230 		format = v4l2_subdev_state_get_format(state, HISTO_PAD_SINK);
231 		sel->r.left = 0;
232 		sel->r.top = 0;
233 		sel->r.width = format->width;
234 		sel->r.height = format->height;
235 		break;
236 
237 	case V4L2_SEL_TGT_COMPOSE:
238 		sel->r = *v4l2_subdev_state_get_compose(state, sel->pad);
239 		break;
240 
241 	case V4L2_SEL_TGT_CROP:
242 		sel->r = *v4l2_subdev_state_get_crop(state, sel->pad);
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 v4l2_mbus_framefmt *format;
260 
261 	/* The crop rectangle must be inside the input frame. */
262 	format = v4l2_subdev_state_get_format(sd_state, HISTO_PAD_SINK);
263 	sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
264 	sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
265 	sel->r.width = clamp_t(unsigned int, sel->r.width, HISTO_MIN_SIZE,
266 			       format->width - sel->r.left);
267 	sel->r.height = clamp_t(unsigned int, sel->r.height, HISTO_MIN_SIZE,
268 				format->height - sel->r.top);
269 
270 	/* Set the crop rectangle and reset the compose rectangle. */
271 	*v4l2_subdev_state_get_crop(sd_state, sel->pad) = sel->r;
272 	*v4l2_subdev_state_get_compose(sd_state, sel->pad) = sel->r;
273 
274 	return 0;
275 }
276 
277 static int histo_set_compose(struct v4l2_subdev *subdev,
278 			     struct v4l2_subdev_state *sd_state,
279 			     struct v4l2_subdev_selection *sel)
280 {
281 	struct v4l2_rect *compose;
282 	struct v4l2_rect *crop;
283 	unsigned int ratio;
284 
285 	/*
286 	 * The compose rectangle is used to configure downscaling, the top left
287 	 * corner is fixed to (0,0) and the size to 1/2 or 1/4 of the crop
288 	 * rectangle.
289 	 */
290 	sel->r.left = 0;
291 	sel->r.top = 0;
292 
293 	crop = v4l2_subdev_state_get_crop(sd_state, sel->pad);
294 
295 	/*
296 	 * Clamp the width and height to acceptable values first and then
297 	 * compute the closest rounded dividing ratio.
298 	 *
299 	 * Ratio	Rounded ratio
300 	 * --------------------------
301 	 * [1.0 1.5[	1
302 	 * [1.5 3.0[	2
303 	 * [3.0 4.0]	4
304 	 *
305 	 * The rounded ratio can be computed using
306 	 *
307 	 * 1 << (ceil(ratio * 2) / 3)
308 	 */
309 	sel->r.width = clamp(sel->r.width, crop->width / 4, crop->width);
310 	ratio = 1 << (crop->width * 2 / sel->r.width / 3);
311 	sel->r.width = crop->width / ratio;
312 
313 
314 	sel->r.height = clamp(sel->r.height, crop->height / 4, crop->height);
315 	ratio = 1 << (crop->height * 2 / sel->r.height / 3);
316 	sel->r.height = crop->height / ratio;
317 
318 	compose = v4l2_subdev_state_get_compose(sd_state, sel->pad);
319 	*compose = sel->r;
320 
321 	return 0;
322 }
323 
324 static int histo_set_selection(struct v4l2_subdev *subdev,
325 			       struct v4l2_subdev_state *sd_state,
326 			       struct v4l2_subdev_selection *sel)
327 {
328 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
329 	struct v4l2_subdev_state *state;
330 	int ret;
331 
332 	if (sel->pad != HISTO_PAD_SINK)
333 		return -EINVAL;
334 
335 	mutex_lock(&histo->entity.lock);
336 
337 	state = vsp1_entity_get_state(&histo->entity, sd_state, sel->which);
338 	if (!state) {
339 		ret = -EINVAL;
340 		goto done;
341 	}
342 
343 	if (sel->target == V4L2_SEL_TGT_CROP)
344 		ret = histo_set_crop(subdev, state, sel);
345 	else if (sel->target == V4L2_SEL_TGT_COMPOSE)
346 		ret = histo_set_compose(subdev, state, sel);
347 	else
348 		ret = -EINVAL;
349 
350 done:
351 	mutex_unlock(&histo->entity.lock);
352 	return ret;
353 }
354 
355 static int histo_set_format(struct v4l2_subdev *subdev,
356 			    struct v4l2_subdev_state *sd_state,
357 			    struct v4l2_subdev_format *fmt)
358 {
359 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
360 
361 	if (fmt->pad == HISTO_PAD_SOURCE) {
362 		fmt->format.code = MEDIA_BUS_FMT_FIXED;
363 		fmt->format.width = 0;
364 		fmt->format.height = 0;
365 		fmt->format.field = V4L2_FIELD_NONE;
366 		fmt->format.colorspace = V4L2_COLORSPACE_RAW;
367 
368 		return 0;
369 	}
370 
371 	return vsp1_subdev_set_pad_format(subdev, sd_state, fmt,
372 					  histo->formats, histo->num_formats,
373 					  HISTO_MIN_SIZE, HISTO_MIN_SIZE,
374 					  HISTO_MAX_SIZE, HISTO_MAX_SIZE);
375 }
376 
377 static const struct v4l2_subdev_pad_ops histo_pad_ops = {
378 	.enum_mbus_code = histo_enum_mbus_code,
379 	.enum_frame_size = histo_enum_frame_size,
380 	.get_fmt = vsp1_subdev_get_pad_format,
381 	.set_fmt = histo_set_format,
382 	.get_selection = histo_get_selection,
383 	.set_selection = histo_set_selection,
384 };
385 
386 static const struct v4l2_subdev_ops histo_ops = {
387 	.pad    = &histo_pad_ops,
388 };
389 
390 /* -----------------------------------------------------------------------------
391  * V4L2 ioctls
392  */
393 
394 static int histo_v4l2_querycap(struct file *file, void *fh,
395 			       struct v4l2_capability *cap)
396 {
397 	struct v4l2_fh *vfh = file->private_data;
398 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
399 
400 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
401 			  | V4L2_CAP_VIDEO_CAPTURE_MPLANE
402 			  | V4L2_CAP_VIDEO_OUTPUT_MPLANE
403 			  | V4L2_CAP_META_CAPTURE;
404 
405 	strscpy(cap->driver, "vsp1", sizeof(cap->driver));
406 	strscpy(cap->card, histo->video.name, sizeof(cap->card));
407 
408 	return 0;
409 }
410 
411 static int histo_v4l2_enum_format(struct file *file, void *fh,
412 				  struct v4l2_fmtdesc *f)
413 {
414 	struct v4l2_fh *vfh = file->private_data;
415 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
416 
417 	if (f->index > 0 || f->type != histo->queue.type)
418 		return -EINVAL;
419 
420 	f->pixelformat = histo->meta_format;
421 
422 	return 0;
423 }
424 
425 static int histo_v4l2_get_format(struct file *file, void *fh,
426 				 struct v4l2_format *format)
427 {
428 	struct v4l2_fh *vfh = file->private_data;
429 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
430 	struct v4l2_meta_format *meta = &format->fmt.meta;
431 
432 	if (format->type != histo->queue.type)
433 		return -EINVAL;
434 
435 	memset(meta, 0, sizeof(*meta));
436 
437 	meta->dataformat = histo->meta_format;
438 	meta->buffersize = histo->data_size;
439 
440 	return 0;
441 }
442 
443 static const struct v4l2_ioctl_ops histo_v4l2_ioctl_ops = {
444 	.vidioc_querycap		= histo_v4l2_querycap,
445 	.vidioc_enum_fmt_meta_cap	= histo_v4l2_enum_format,
446 	.vidioc_g_fmt_meta_cap		= histo_v4l2_get_format,
447 	.vidioc_s_fmt_meta_cap		= histo_v4l2_get_format,
448 	.vidioc_try_fmt_meta_cap	= histo_v4l2_get_format,
449 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
450 	.vidioc_querybuf		= vb2_ioctl_querybuf,
451 	.vidioc_qbuf			= vb2_ioctl_qbuf,
452 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
453 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
454 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
455 	.vidioc_streamon		= vb2_ioctl_streamon,
456 	.vidioc_streamoff		= vb2_ioctl_streamoff,
457 };
458 
459 /* -----------------------------------------------------------------------------
460  * V4L2 File Operations
461  */
462 
463 static const struct v4l2_file_operations histo_v4l2_fops = {
464 	.owner = THIS_MODULE,
465 	.unlocked_ioctl = video_ioctl2,
466 	.open = v4l2_fh_open,
467 	.release = vb2_fop_release,
468 	.poll = vb2_fop_poll,
469 	.mmap = vb2_fop_mmap,
470 };
471 
472 static void vsp1_histogram_cleanup(struct vsp1_histogram *histo)
473 {
474 	if (video_is_registered(&histo->video))
475 		video_unregister_device(&histo->video);
476 
477 	media_entity_cleanup(&histo->video.entity);
478 }
479 
480 void vsp1_histogram_destroy(struct vsp1_entity *entity)
481 {
482 	struct vsp1_histogram *histo = subdev_to_histo(&entity->subdev);
483 
484 	vsp1_histogram_cleanup(histo);
485 }
486 
487 int vsp1_histogram_init(struct vsp1_device *vsp1, struct vsp1_histogram *histo,
488 			enum vsp1_entity_type type, const char *name,
489 			const struct vsp1_entity_operations *ops,
490 			const unsigned int *formats, unsigned int num_formats,
491 			size_t data_size, u32 meta_format)
492 {
493 	int ret;
494 
495 	histo->formats = formats;
496 	histo->num_formats = num_formats;
497 	histo->data_size = data_size;
498 	histo->meta_format = meta_format;
499 
500 	histo->pad.flags = MEDIA_PAD_FL_SINK;
501 	histo->video.vfl_dir = VFL_DIR_RX;
502 
503 	mutex_init(&histo->lock);
504 	spin_lock_init(&histo->irqlock);
505 	INIT_LIST_HEAD(&histo->irqqueue);
506 	init_waitqueue_head(&histo->wait_queue);
507 
508 	/* Initialize the VSP entity... */
509 	histo->entity.ops = ops;
510 	histo->entity.type = type;
511 
512 	ret = vsp1_entity_init(vsp1, &histo->entity, name, 2, &histo_ops,
513 			       MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
514 	if (ret < 0)
515 		return ret;
516 
517 	/* ... and the media entity... */
518 	ret = media_entity_pads_init(&histo->video.entity, 1, &histo->pad);
519 	if (ret < 0)
520 		return ret;
521 
522 	/* ... and the video node... */
523 	histo->video.v4l2_dev = &vsp1->v4l2_dev;
524 	histo->video.fops = &histo_v4l2_fops;
525 	snprintf(histo->video.name, sizeof(histo->video.name),
526 		 "%s histo", histo->entity.subdev.name);
527 	histo->video.vfl_type = VFL_TYPE_VIDEO;
528 	histo->video.release = video_device_release_empty;
529 	histo->video.ioctl_ops = &histo_v4l2_ioctl_ops;
530 	histo->video.device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
531 
532 	video_set_drvdata(&histo->video, histo);
533 
534 	/* ... and the buffers queue... */
535 	histo->queue.type = V4L2_BUF_TYPE_META_CAPTURE;
536 	histo->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
537 	histo->queue.lock = &histo->lock;
538 	histo->queue.drv_priv = histo;
539 	histo->queue.buf_struct_size = sizeof(struct vsp1_histogram_buffer);
540 	histo->queue.ops = &histo_video_queue_qops;
541 	histo->queue.mem_ops = &vb2_vmalloc_memops;
542 	histo->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
543 	histo->queue.dev = vsp1->dev;
544 	ret = vb2_queue_init(&histo->queue);
545 	if (ret < 0) {
546 		dev_err(vsp1->dev, "failed to initialize vb2 queue\n");
547 		goto error;
548 	}
549 
550 	/* ... and register the video device. */
551 	histo->video.queue = &histo->queue;
552 	ret = video_register_device(&histo->video, VFL_TYPE_VIDEO, -1);
553 	if (ret < 0) {
554 		dev_err(vsp1->dev, "failed to register video device\n");
555 		goto error;
556 	}
557 
558 	return 0;
559 
560 error:
561 	vsp1_histogram_cleanup(histo);
562 	return ret;
563 }
564