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 	unsigned long flags;
40 
41 	spin_lock_irqsave(&histo->irqlock, flags);
42 
43 	if (list_empty(&histo->irqqueue))
44 		goto done;
45 
46 	buf = list_first_entry(&histo->irqqueue, struct vsp1_histogram_buffer,
47 			       queue);
48 	list_del(&buf->queue);
49 	histo->readout = true;
50 
51 done:
52 	spin_unlock_irqrestore(&histo->irqlock, flags);
53 	return buf;
54 }
55 
56 void vsp1_histogram_buffer_complete(struct vsp1_histogram *histo,
57 				    struct vsp1_histogram_buffer *buf,
58 				    size_t size)
59 {
60 	struct vsp1_pipeline *pipe = histo->entity.pipe;
61 	unsigned long flags;
62 
63 	/*
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.
67 	 */
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);
72 
73 	spin_lock_irqsave(&histo->irqlock, flags);
74 	histo->readout = false;
75 	wake_up(&histo->wait_queue);
76 	spin_unlock_irqrestore(&histo->irqlock, flags);
77 }
78 
79 /* -----------------------------------------------------------------------------
80  * videobuf2 Queue Operations
81  */
82 
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[])
86 {
87 	struct vsp1_histogram *histo = vb2_get_drv_priv(vq);
88 
89 	if (*nplanes) {
90 		if (*nplanes != 1)
91 			return -EINVAL;
92 
93 		if (sizes[0] < histo->data_size)
94 			return -EINVAL;
95 
96 		return 0;
97 	}
98 
99 	*nplanes = 1;
100 	sizes[0] = histo->data_size;
101 
102 	return 0;
103 }
104 
105 static int histo_buffer_prepare(struct vb2_buffer *vb)
106 {
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);
110 
111 	if (vb->num_planes != 1)
112 		return -EINVAL;
113 
114 	if (vb2_plane_size(vb, 0) < histo->data_size)
115 		return -EINVAL;
116 
117 	buf->addr = vb2_plane_vaddr(vb, 0);
118 
119 	return 0;
120 }
121 
122 static void histo_buffer_queue(struct vb2_buffer *vb)
123 {
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);
127 	unsigned long flags;
128 
129 	spin_lock_irqsave(&histo->irqlock, flags);
130 	list_add_tail(&buf->queue, &histo->irqqueue);
131 	spin_unlock_irqrestore(&histo->irqlock, flags);
132 }
133 
134 static int histo_start_streaming(struct vb2_queue *vq, unsigned int count)
135 {
136 	return 0;
137 }
138 
139 static void histo_stop_streaming(struct vb2_queue *vq)
140 {
141 	struct vsp1_histogram *histo = vb2_get_drv_priv(vq);
142 	struct vsp1_histogram_buffer *buffer;
143 	unsigned long flags;
144 
145 	spin_lock_irqsave(&histo->irqlock, flags);
146 
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);
151 
152 	/* Wait for the buffer being read out (if any) to complete. */
153 	wait_event_lock_irq(histo->wait_queue, !histo->readout, histo->irqlock);
154 
155 	spin_unlock_irqrestore(&histo->irqlock, flags);
156 }
157 
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,
166 };
167 
168 /* -----------------------------------------------------------------------------
169  * V4L2 Subdevice Operations
170  */
171 
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)
175 {
176 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
177 
178 	if (code->pad == HISTO_PAD_SOURCE) {
179 		code->code = MEDIA_BUS_FMT_FIXED;
180 		return 0;
181 	}
182 
183 	return vsp1_subdev_enum_mbus_code(subdev, sd_state, code,
184 					  histo->formats,
185 					  histo->num_formats);
186 }
187 
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)
191 {
192 	if (fse->pad != HISTO_PAD_SINK)
193 		return -EINVAL;
194 
195 	return vsp1_subdev_enum_frame_size(subdev, sd_state, fse,
196 					   HISTO_MIN_SIZE,
197 					   HISTO_MIN_SIZE, HISTO_MAX_SIZE,
198 					   HISTO_MAX_SIZE);
199 }
200 
201 static int histo_get_selection(struct v4l2_subdev *subdev,
202 			       struct v4l2_subdev_state *sd_state,
203 			       struct v4l2_subdev_selection *sel)
204 {
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;
209 	int ret = 0;
210 
211 	if (sel->pad != HISTO_PAD_SINK)
212 		return -EINVAL;
213 
214 	mutex_lock(&histo->entity.lock);
215 
216 	config = vsp1_entity_get_pad_config(&histo->entity, sd_state,
217 					    sel->which);
218 	if (!config) {
219 		ret = -EINVAL;
220 		goto done;
221 	}
222 
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,
227 						     HISTO_PAD_SINK,
228 						     V4L2_SEL_TGT_CROP);
229 		sel->r.left = 0;
230 		sel->r.top = 0;
231 		sel->r.width = crop->width;
232 		sel->r.height = crop->height;
233 		break;
234 
235 	case V4L2_SEL_TGT_CROP_BOUNDS:
236 	case V4L2_SEL_TGT_CROP_DEFAULT:
237 		format = vsp1_entity_get_pad_format(&histo->entity, config,
238 						    HISTO_PAD_SINK);
239 		sel->r.left = 0;
240 		sel->r.top = 0;
241 		sel->r.width = format->width;
242 		sel->r.height = format->height;
243 		break;
244 
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);
249 		break;
250 
251 	default:
252 		ret = -EINVAL;
253 		break;
254 	}
255 
256 done:
257 	mutex_unlock(&histo->entity.lock);
258 	return ret;
259 }
260 
261 static int histo_set_crop(struct v4l2_subdev *subdev,
262 			  struct v4l2_subdev_state *sd_state,
263 			  struct v4l2_subdev_selection *sel)
264 {
265 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
266 	struct v4l2_mbus_framefmt *format;
267 	struct v4l2_rect *selection;
268 
269 	/* The crop rectangle must be inside the input frame. */
270 	format = vsp1_entity_get_pad_format(&histo->entity, sd_state,
271 					    HISTO_PAD_SINK);
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);
278 
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);
282 	*selection = sel->r;
283 
284 	selection = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
285 						  sel->pad,
286 						  V4L2_SEL_TGT_COMPOSE);
287 	*selection = sel->r;
288 
289 	return 0;
290 }
291 
292 static int histo_set_compose(struct v4l2_subdev *subdev,
293 			     struct v4l2_subdev_state *sd_state,
294 			     struct v4l2_subdev_selection *sel)
295 {
296 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
297 	struct v4l2_rect *compose;
298 	struct v4l2_rect *crop;
299 	unsigned int ratio;
300 
301 	/*
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
304 	 * rectangle.
305 	 */
306 	sel->r.left = 0;
307 	sel->r.top = 0;
308 
309 	crop = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
310 					     sel->pad,
311 					     V4L2_SEL_TGT_CROP);
312 
313 	/*
314 	 * Clamp the width and height to acceptable values first and then
315 	 * compute the closest rounded dividing ratio.
316 	 *
317 	 * Ratio	Rounded ratio
318 	 * --------------------------
319 	 * [1.0 1.5[	1
320 	 * [1.5 3.0[	2
321 	 * [3.0 4.0]	4
322 	 *
323 	 * The rounded ratio can be computed using
324 	 *
325 	 * 1 << (ceil(ratio * 2) / 3)
326 	 */
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;
330 
331 
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;
335 
336 	compose = vsp1_entity_get_pad_selection(&histo->entity, sd_state,
337 						sel->pad,
338 						V4L2_SEL_TGT_COMPOSE);
339 	*compose = sel->r;
340 
341 	return 0;
342 }
343 
344 static int histo_set_selection(struct v4l2_subdev *subdev,
345 			       struct v4l2_subdev_state *sd_state,
346 			       struct v4l2_subdev_selection *sel)
347 {
348 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
349 	struct v4l2_subdev_state *config;
350 	int ret;
351 
352 	if (sel->pad != HISTO_PAD_SINK)
353 		return -EINVAL;
354 
355 	mutex_lock(&histo->entity.lock);
356 
357 	config = vsp1_entity_get_pad_config(&histo->entity, sd_state,
358 					    sel->which);
359 	if (!config) {
360 		ret = -EINVAL;
361 		goto done;
362 	}
363 
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);
368 	else
369 		ret = -EINVAL;
370 
371 done:
372 	mutex_unlock(&histo->entity.lock);
373 	return ret;
374 }
375 
376 static int histo_get_format(struct v4l2_subdev *subdev,
377 			    struct v4l2_subdev_state *sd_state,
378 			    struct v4l2_subdev_format *fmt)
379 {
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;
386 		return 0;
387 	}
388 
389 	return vsp1_subdev_get_pad_format(subdev, sd_state, fmt);
390 }
391 
392 static int histo_set_format(struct v4l2_subdev *subdev,
393 			    struct v4l2_subdev_state *sd_state,
394 			    struct v4l2_subdev_format *fmt)
395 {
396 	struct vsp1_histogram *histo = subdev_to_histo(subdev);
397 
398 	if (fmt->pad != HISTO_PAD_SINK)
399 		return histo_get_format(subdev, sd_state, fmt);
400 
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);
405 }
406 
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,
414 };
415 
416 static const struct v4l2_subdev_ops histo_ops = {
417 	.pad    = &histo_pad_ops,
418 };
419 
420 /* -----------------------------------------------------------------------------
421  * V4L2 ioctls
422  */
423 
424 static int histo_v4l2_querycap(struct file *file, void *fh,
425 			       struct v4l2_capability *cap)
426 {
427 	struct v4l2_fh *vfh = file->private_data;
428 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
429 
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;
434 
435 	strscpy(cap->driver, "vsp1", sizeof(cap->driver));
436 	strscpy(cap->card, histo->video.name, sizeof(cap->card));
437 
438 	return 0;
439 }
440 
441 static int histo_v4l2_enum_format(struct file *file, void *fh,
442 				  struct v4l2_fmtdesc *f)
443 {
444 	struct v4l2_fh *vfh = file->private_data;
445 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
446 
447 	if (f->index > 0 || f->type != histo->queue.type)
448 		return -EINVAL;
449 
450 	f->pixelformat = histo->meta_format;
451 
452 	return 0;
453 }
454 
455 static int histo_v4l2_get_format(struct file *file, void *fh,
456 				 struct v4l2_format *format)
457 {
458 	struct v4l2_fh *vfh = file->private_data;
459 	struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev);
460 	struct v4l2_meta_format *meta = &format->fmt.meta;
461 
462 	if (format->type != histo->queue.type)
463 		return -EINVAL;
464 
465 	memset(meta, 0, sizeof(*meta));
466 
467 	meta->dataformat = histo->meta_format;
468 	meta->buffersize = histo->data_size;
469 
470 	return 0;
471 }
472 
473 static const struct v4l2_ioctl_ops histo_v4l2_ioctl_ops = {
474 	.vidioc_querycap		= histo_v4l2_querycap,
475 	.vidioc_enum_fmt_meta_cap	= histo_v4l2_enum_format,
476 	.vidioc_g_fmt_meta_cap		= histo_v4l2_get_format,
477 	.vidioc_s_fmt_meta_cap		= histo_v4l2_get_format,
478 	.vidioc_try_fmt_meta_cap	= histo_v4l2_get_format,
479 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
480 	.vidioc_querybuf		= vb2_ioctl_querybuf,
481 	.vidioc_qbuf			= vb2_ioctl_qbuf,
482 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
483 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
484 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
485 	.vidioc_streamon		= vb2_ioctl_streamon,
486 	.vidioc_streamoff		= vb2_ioctl_streamoff,
487 };
488 
489 /* -----------------------------------------------------------------------------
490  * V4L2 File Operations
491  */
492 
493 static const struct v4l2_file_operations histo_v4l2_fops = {
494 	.owner = THIS_MODULE,
495 	.unlocked_ioctl = video_ioctl2,
496 	.open = v4l2_fh_open,
497 	.release = vb2_fop_release,
498 	.poll = vb2_fop_poll,
499 	.mmap = vb2_fop_mmap,
500 };
501 
502 static void vsp1_histogram_cleanup(struct vsp1_histogram *histo)
503 {
504 	if (video_is_registered(&histo->video))
505 		video_unregister_device(&histo->video);
506 
507 	media_entity_cleanup(&histo->video.entity);
508 }
509 
510 void vsp1_histogram_destroy(struct vsp1_entity *entity)
511 {
512 	struct vsp1_histogram *histo = subdev_to_histo(&entity->subdev);
513 
514 	vsp1_histogram_cleanup(histo);
515 }
516 
517 int vsp1_histogram_init(struct vsp1_device *vsp1, struct vsp1_histogram *histo,
518 			enum vsp1_entity_type type, const char *name,
519 			const struct vsp1_entity_operations *ops,
520 			const unsigned int *formats, unsigned int num_formats,
521 			size_t data_size, u32 meta_format)
522 {
523 	int ret;
524 
525 	histo->formats = formats;
526 	histo->num_formats = num_formats;
527 	histo->data_size = data_size;
528 	histo->meta_format = meta_format;
529 
530 	histo->pad.flags = MEDIA_PAD_FL_SINK;
531 	histo->video.vfl_dir = VFL_DIR_RX;
532 
533 	mutex_init(&histo->lock);
534 	spin_lock_init(&histo->irqlock);
535 	INIT_LIST_HEAD(&histo->irqqueue);
536 	init_waitqueue_head(&histo->wait_queue);
537 
538 	/* Initialize the VSP entity... */
539 	histo->entity.ops = ops;
540 	histo->entity.type = type;
541 
542 	ret = vsp1_entity_init(vsp1, &histo->entity, name, 2, &histo_ops,
543 			       MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
544 	if (ret < 0)
545 		return ret;
546 
547 	/* ... and the media entity... */
548 	ret = media_entity_pads_init(&histo->video.entity, 1, &histo->pad);
549 	if (ret < 0)
550 		return ret;
551 
552 	/* ... and the video node... */
553 	histo->video.v4l2_dev = &vsp1->v4l2_dev;
554 	histo->video.fops = &histo_v4l2_fops;
555 	snprintf(histo->video.name, sizeof(histo->video.name),
556 		 "%s histo", histo->entity.subdev.name);
557 	histo->video.vfl_type = VFL_TYPE_VIDEO;
558 	histo->video.release = video_device_release_empty;
559 	histo->video.ioctl_ops = &histo_v4l2_ioctl_ops;
560 	histo->video.device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
561 
562 	video_set_drvdata(&histo->video, histo);
563 
564 	/* ... and the buffers queue... */
565 	histo->queue.type = V4L2_BUF_TYPE_META_CAPTURE;
566 	histo->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
567 	histo->queue.lock = &histo->lock;
568 	histo->queue.drv_priv = histo;
569 	histo->queue.buf_struct_size = sizeof(struct vsp1_histogram_buffer);
570 	histo->queue.ops = &histo_video_queue_qops;
571 	histo->queue.mem_ops = &vb2_vmalloc_memops;
572 	histo->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
573 	histo->queue.dev = vsp1->dev;
574 	ret = vb2_queue_init(&histo->queue);
575 	if (ret < 0) {
576 		dev_err(vsp1->dev, "failed to initialize vb2 queue\n");
577 		goto error;
578 	}
579 
580 	/* ... and register the video device. */
581 	histo->video.queue = &histo->queue;
582 	ret = video_register_device(&histo->video, VFL_TYPE_VIDEO, -1);
583 	if (ret < 0) {
584 		dev_err(vsp1->dev, "failed to register video device\n");
585 		goto error;
586 	}
587 
588 	return 0;
589 
590 error:
591 	vsp1_histogram_cleanup(histo);
592 	return ret;
593 }
594