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