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