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