blob: e7d0fc2228a6f0c18bb482ad0e982eb2c8e760d7 [file] [log] [blame]
Helen Koikef2fe8902017-04-07 14:55:19 -03001/*
2 * vimc-capture.c Virtual Media Controller Driver
3 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Helen Fornazier4a29b702017-06-19 14:00:18 -030018#include <linux/component.h>
19#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070020#include <linux/mod_devicetable.h>
Helen Fornazier4a29b702017-06-19 14:00:18 -030021#include <linux/platform_device.h>
Helen Koikef2fe8902017-04-07 14:55:19 -030022#include <media/v4l2-ioctl.h>
23#include <media/videobuf2-core.h>
24#include <media/videobuf2-vmalloc.h>
25
Helen Fornazier4a29b702017-06-19 14:00:18 -030026#include "vimc-common.h"
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050027#include "vimc-streamer.h"
Helen Fornazier4a29b702017-06-19 14:00:18 -030028
29#define VIMC_CAP_DRV_NAME "vimc-capture"
Helen Koikef2fe8902017-04-07 14:55:19 -030030
Helen Fornazierb6c61a62019-03-13 14:29:37 -040031static const u32 vimc_cap_supported_pixfmt[] = {
32 V4L2_PIX_FMT_BGR24,
33 V4L2_PIX_FMT_RGB24,
34 V4L2_PIX_FMT_ARGB32,
35 V4L2_PIX_FMT_SBGGR8,
36 V4L2_PIX_FMT_SGBRG8,
37 V4L2_PIX_FMT_SGRBG8,
38 V4L2_PIX_FMT_SRGGB8,
39 V4L2_PIX_FMT_SBGGR10,
40 V4L2_PIX_FMT_SGBRG10,
41 V4L2_PIX_FMT_SGRBG10,
42 V4L2_PIX_FMT_SRGGB10,
43 V4L2_PIX_FMT_SBGGR10ALAW8,
44 V4L2_PIX_FMT_SGBRG10ALAW8,
45 V4L2_PIX_FMT_SGRBG10ALAW8,
46 V4L2_PIX_FMT_SRGGB10ALAW8,
47 V4L2_PIX_FMT_SBGGR10DPCM8,
48 V4L2_PIX_FMT_SGBRG10DPCM8,
49 V4L2_PIX_FMT_SGRBG10DPCM8,
50 V4L2_PIX_FMT_SRGGB10DPCM8,
51 V4L2_PIX_FMT_SBGGR12,
52 V4L2_PIX_FMT_SGBRG12,
53 V4L2_PIX_FMT_SGRBG12,
54 V4L2_PIX_FMT_SRGGB12,
55};
56
Helen Koikef2fe8902017-04-07 14:55:19 -030057struct vimc_cap_device {
58 struct vimc_ent_device ved;
59 struct video_device vdev;
Helen Fornazier4a29b702017-06-19 14:00:18 -030060 struct device *dev;
Helen Koikef2fe8902017-04-07 14:55:19 -030061 struct v4l2_pix_format format;
62 struct vb2_queue queue;
63 struct list_head buf_list;
64 /*
65 * NOTE: in a real driver, a spin lock must be used to access the
66 * queue because the frames are generated from a hardware interruption
67 * and the isr is not allowed to sleep.
68 * Even if it is not necessary a spinlock in the vimc driver, we
69 * use it here as a code reference
70 */
71 spinlock_t qlock;
72 struct mutex lock;
73 u32 sequence;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050074 struct vimc_stream stream;
Helen Koikef2fe8902017-04-07 14:55:19 -030075};
76
Helen Fornazier535d2962017-06-19 14:00:17 -030077static const struct v4l2_pix_format fmt_default = {
78 .width = 640,
79 .height = 480,
80 .pixelformat = V4L2_PIX_FMT_RGB24,
81 .field = V4L2_FIELD_NONE,
82 .colorspace = V4L2_COLORSPACE_DEFAULT,
83};
84
Helen Koikef2fe8902017-04-07 14:55:19 -030085struct vimc_cap_buffer {
86 /*
87 * struct vb2_v4l2_buffer must be the first element
88 * the videobuf2 framework will allocate this struct based on
89 * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of
90 * memory as a vb2_buffer
91 */
92 struct vb2_v4l2_buffer vb2;
93 struct list_head list;
94};
95
96static int vimc_cap_querycap(struct file *file, void *priv,
97 struct v4l2_capability *cap)
98{
Hans Verkuil04ee6d62019-01-14 10:27:45 -020099 strscpy(cap->driver, VIMC_PDEV_NAME, sizeof(cap->driver));
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400100 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
Helen Koikef2fe8902017-04-07 14:55:19 -0300101 snprintf(cap->bus_info, sizeof(cap->bus_info),
Hans Verkuil6fd369d2019-01-30 08:43:51 -0500102 "platform:%s", VIMC_PDEV_NAME);
Helen Koikef2fe8902017-04-07 14:55:19 -0300103
104 return 0;
105}
106
Helen Fornazier288a22d2017-06-19 14:00:14 -0300107static void vimc_cap_get_format(struct vimc_ent_device *ved,
108 struct v4l2_pix_format *fmt)
109{
110 struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
111 ved);
112
113 *fmt = vcap->format;
114}
115
Helen Fornazier535d2962017-06-19 14:00:17 -0300116static int vimc_cap_g_fmt_vid_cap(struct file *file, void *priv,
Helen Koikef2fe8902017-04-07 14:55:19 -0300117 struct v4l2_format *f)
118{
119 struct vimc_cap_device *vcap = video_drvdata(file);
120
121 f->fmt.pix = vcap->format;
122
123 return 0;
124}
125
Helen Fornazier535d2962017-06-19 14:00:17 -0300126static int vimc_cap_try_fmt_vid_cap(struct file *file, void *priv,
127 struct v4l2_format *f)
128{
129 struct v4l2_pix_format *format = &f->fmt.pix;
Helen Fornazier535d2962017-06-19 14:00:17 -0300130
131 format->width = clamp_t(u32, format->width, VIMC_FRAME_MIN_WIDTH,
132 VIMC_FRAME_MAX_WIDTH) & ~1;
133 format->height = clamp_t(u32, format->height, VIMC_FRAME_MIN_HEIGHT,
134 VIMC_FRAME_MAX_HEIGHT) & ~1;
135
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400136 vimc_colorimetry_clamp(format);
Helen Fornazier535d2962017-06-19 14:00:17 -0300137
138 if (format->field == V4L2_FIELD_ANY)
139 format->field = fmt_default.field;
140
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400141 /* TODO: Add support for custom bytesperline values */
Helen Fornazier535d2962017-06-19 14:00:17 -0300142
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400143 /* Don't accept a pixelformat that is not on the table */
144 if (!v4l2_format_info(format->pixelformat))
145 format->pixelformat = fmt_default.pixelformat;
146
147 return v4l2_fill_pixfmt(format, format->pixelformat,
148 format->width, format->height);
Helen Fornazier535d2962017-06-19 14:00:17 -0300149}
150
151static int vimc_cap_s_fmt_vid_cap(struct file *file, void *priv,
152 struct v4l2_format *f)
Helen Koikef2fe8902017-04-07 14:55:19 -0300153{
154 struct vimc_cap_device *vcap = video_drvdata(file);
155
Helen Fornazier535d2962017-06-19 14:00:17 -0300156 /* Do not change the format while stream is on */
157 if (vb2_is_busy(&vcap->queue))
158 return -EBUSY;
159
160 vimc_cap_try_fmt_vid_cap(file, priv, f);
161
Helen Fornazier4a29b702017-06-19 14:00:18 -0300162 dev_dbg(vcap->dev, "%s: format update: "
Helen Fornazier535d2962017-06-19 14:00:17 -0300163 "old:%dx%d (0x%x, %d, %d, %d, %d) "
164 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vcap->vdev.name,
165 /* old */
166 vcap->format.width, vcap->format.height,
167 vcap->format.pixelformat, vcap->format.colorspace,
168 vcap->format.quantization, vcap->format.xfer_func,
169 vcap->format.ycbcr_enc,
170 /* new */
171 f->fmt.pix.width, f->fmt.pix.height,
172 f->fmt.pix.pixelformat, f->fmt.pix.colorspace,
173 f->fmt.pix.quantization, f->fmt.pix.xfer_func,
174 f->fmt.pix.ycbcr_enc);
175
176 vcap->format = f->fmt.pix;
177
178 return 0;
179}
180
181static int vimc_cap_enum_fmt_vid_cap(struct file *file, void *priv,
182 struct v4l2_fmtdesc *f)
183{
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400184 if (f->index >= ARRAY_SIZE(vimc_cap_supported_pixfmt))
Helen Koikef2fe8902017-04-07 14:55:19 -0300185 return -EINVAL;
186
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400187 f->pixelformat = vimc_cap_supported_pixfmt[f->index];
Helen Fornazier535d2962017-06-19 14:00:17 -0300188
189 return 0;
190}
191
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400192static bool vimc_cap_is_pixfmt_supported(u32 pixelformat)
193{
194 unsigned int i;
195
196 for (i = 0; i < ARRAY_SIZE(vimc_cap_supported_pixfmt); i++)
197 if (vimc_cap_supported_pixfmt[i] == pixelformat)
198 return true;
199 return false;
200}
201
Helen Fornazier535d2962017-06-19 14:00:17 -0300202static int vimc_cap_enum_framesizes(struct file *file, void *fh,
203 struct v4l2_frmsizeenum *fsize)
204{
Helen Fornazier535d2962017-06-19 14:00:17 -0300205 if (fsize->index)
206 return -EINVAL;
207
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400208 if (!vimc_cap_is_pixfmt_supported(fsize->pixel_format))
Helen Fornazier535d2962017-06-19 14:00:17 -0300209 return -EINVAL;
210
211 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
212 fsize->stepwise.min_width = VIMC_FRAME_MIN_WIDTH;
213 fsize->stepwise.max_width = VIMC_FRAME_MAX_WIDTH;
214 fsize->stepwise.min_height = VIMC_FRAME_MIN_HEIGHT;
215 fsize->stepwise.max_height = VIMC_FRAME_MAX_HEIGHT;
Helen Fornazier5efbc652019-03-06 17:42:39 -0500216 fsize->stepwise.step_width = 1;
217 fsize->stepwise.step_height = 1;
Helen Koikef2fe8902017-04-07 14:55:19 -0300218
219 return 0;
220}
221
222static const struct v4l2_file_operations vimc_cap_fops = {
223 .owner = THIS_MODULE,
224 .open = v4l2_fh_open,
225 .release = vb2_fop_release,
226 .read = vb2_fop_read,
227 .poll = vb2_fop_poll,
228 .unlocked_ioctl = video_ioctl2,
229 .mmap = vb2_fop_mmap,
230};
231
232static const struct v4l2_ioctl_ops vimc_cap_ioctl_ops = {
233 .vidioc_querycap = vimc_cap_querycap,
234
Helen Fornazier535d2962017-06-19 14:00:17 -0300235 .vidioc_g_fmt_vid_cap = vimc_cap_g_fmt_vid_cap,
236 .vidioc_s_fmt_vid_cap = vimc_cap_s_fmt_vid_cap,
237 .vidioc_try_fmt_vid_cap = vimc_cap_try_fmt_vid_cap,
Helen Koikef2fe8902017-04-07 14:55:19 -0300238 .vidioc_enum_fmt_vid_cap = vimc_cap_enum_fmt_vid_cap,
Helen Fornazier535d2962017-06-19 14:00:17 -0300239 .vidioc_enum_framesizes = vimc_cap_enum_framesizes,
Helen Koikef2fe8902017-04-07 14:55:19 -0300240
241 .vidioc_reqbufs = vb2_ioctl_reqbufs,
242 .vidioc_create_bufs = vb2_ioctl_create_bufs,
243 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
244 .vidioc_querybuf = vb2_ioctl_querybuf,
245 .vidioc_qbuf = vb2_ioctl_qbuf,
246 .vidioc_dqbuf = vb2_ioctl_dqbuf,
247 .vidioc_expbuf = vb2_ioctl_expbuf,
248 .vidioc_streamon = vb2_ioctl_streamon,
249 .vidioc_streamoff = vb2_ioctl_streamoff,
250};
251
252static void vimc_cap_return_all_buffers(struct vimc_cap_device *vcap,
253 enum vb2_buffer_state state)
254{
255 struct vimc_cap_buffer *vbuf, *node;
256
257 spin_lock(&vcap->qlock);
258
259 list_for_each_entry_safe(vbuf, node, &vcap->buf_list, list) {
260 list_del(&vbuf->list);
261 vb2_buffer_done(&vbuf->vb2.vb2_buf, state);
262 }
263
264 spin_unlock(&vcap->qlock);
265}
266
Helen Koikef2fe8902017-04-07 14:55:19 -0300267static int vimc_cap_start_streaming(struct vb2_queue *vq, unsigned int count)
268{
269 struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
270 struct media_entity *entity = &vcap->vdev.entity;
271 int ret;
272
273 vcap->sequence = 0;
274
275 /* Start the media pipeline */
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500276 ret = media_pipeline_start(entity, &vcap->stream.pipe);
Helen Koikef2fe8902017-04-07 14:55:19 -0300277 if (ret) {
278 vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
279 return ret;
280 }
281
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400282 vcap->stream.producer_pixfmt = vcap->format.pixelformat;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500283 ret = vimc_streamer_s_stream(&vcap->stream, &vcap->ved, 1);
Helen Koikef2fe8902017-04-07 14:55:19 -0300284 if (ret) {
285 media_pipeline_stop(entity);
286 vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
287 return ret;
288 }
289
290 return 0;
291}
292
293/*
294 * Stop the stream engine. Any remaining buffers in the stream queue are
295 * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
296 */
297static void vimc_cap_stop_streaming(struct vb2_queue *vq)
298{
299 struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
300
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500301 vimc_streamer_s_stream(&vcap->stream, &vcap->ved, 0);
Helen Koikef2fe8902017-04-07 14:55:19 -0300302
303 /* Stop the media pipeline */
304 media_pipeline_stop(&vcap->vdev.entity);
305
306 /* Release all active buffers */
307 vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_ERROR);
308}
309
310static void vimc_cap_buf_queue(struct vb2_buffer *vb2_buf)
311{
312 struct vimc_cap_device *vcap = vb2_get_drv_priv(vb2_buf->vb2_queue);
313 struct vimc_cap_buffer *buf = container_of(vb2_buf,
314 struct vimc_cap_buffer,
315 vb2.vb2_buf);
316
317 spin_lock(&vcap->qlock);
318 list_add_tail(&buf->list, &vcap->buf_list);
319 spin_unlock(&vcap->qlock);
320}
321
322static int vimc_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
323 unsigned int *nplanes, unsigned int sizes[],
324 struct device *alloc_devs[])
325{
326 struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
327
328 if (*nplanes)
329 return sizes[0] < vcap->format.sizeimage ? -EINVAL : 0;
330 /* We don't support multiplanes for now */
331 *nplanes = 1;
332 sizes[0] = vcap->format.sizeimage;
333
334 return 0;
335}
336
337static int vimc_cap_buffer_prepare(struct vb2_buffer *vb)
338{
339 struct vimc_cap_device *vcap = vb2_get_drv_priv(vb->vb2_queue);
340 unsigned long size = vcap->format.sizeimage;
341
342 if (vb2_plane_size(vb, 0) < size) {
Helen Fornazier4a29b702017-06-19 14:00:18 -0300343 dev_err(vcap->dev, "%s: buffer too small (%lu < %lu)\n",
Helen Koikef2fe8902017-04-07 14:55:19 -0300344 vcap->vdev.name, vb2_plane_size(vb, 0), size);
345 return -EINVAL;
346 }
347 return 0;
348}
349
350static const struct vb2_ops vimc_cap_qops = {
351 .start_streaming = vimc_cap_start_streaming,
352 .stop_streaming = vimc_cap_stop_streaming,
353 .buf_queue = vimc_cap_buf_queue,
354 .queue_setup = vimc_cap_queue_setup,
355 .buf_prepare = vimc_cap_buffer_prepare,
356 /*
357 * Since q->lock is set we can use the standard
358 * vb2_ops_wait_prepare/finish helper functions.
359 */
360 .wait_prepare = vb2_ops_wait_prepare,
361 .wait_finish = vb2_ops_wait_finish,
362};
363
Helen Koikef2fe8902017-04-07 14:55:19 -0300364static const struct media_entity_operations vimc_cap_mops = {
Helen Fornazier288a22d2017-06-19 14:00:14 -0300365 .link_validate = vimc_link_validate,
Helen Koikef2fe8902017-04-07 14:55:19 -0300366};
367
Hans Verkuil3650a232019-02-21 08:47:28 -0500368static void vimc_cap_release(struct video_device *vdev)
369{
370 struct vimc_cap_device *vcap =
371 container_of(vdev, struct vimc_cap_device, vdev);
372
373 vimc_pads_cleanup(vcap->ved.pads);
374 kfree(vcap);
375}
376
Helen Fornazier4a29b702017-06-19 14:00:18 -0300377static void vimc_cap_comp_unbind(struct device *comp, struct device *master,
378 void *master_data)
Helen Koikef2fe8902017-04-07 14:55:19 -0300379{
Helen Fornazier4a29b702017-06-19 14:00:18 -0300380 struct vimc_ent_device *ved = dev_get_drvdata(comp);
Helen Koikef2fe8902017-04-07 14:55:19 -0300381 struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
382 ved);
383
384 vb2_queue_release(&vcap->queue);
385 media_entity_cleanup(ved->ent);
386 video_unregister_device(&vcap->vdev);
Helen Koikef2fe8902017-04-07 14:55:19 -0300387}
388
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500389static void *vimc_cap_process_frame(struct vimc_ent_device *ved,
390 const void *frame)
Helen Koikef2fe8902017-04-07 14:55:19 -0300391{
392 struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
393 ved);
394 struct vimc_cap_buffer *vimc_buf;
395 void *vbuf;
396
397 spin_lock(&vcap->qlock);
398
399 /* Get the first entry of the list */
400 vimc_buf = list_first_entry_or_null(&vcap->buf_list,
401 typeof(*vimc_buf), list);
402 if (!vimc_buf) {
403 spin_unlock(&vcap->qlock);
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500404 return ERR_PTR(-EAGAIN);
Helen Koikef2fe8902017-04-07 14:55:19 -0300405 }
406
407 /* Remove this entry from the list */
408 list_del(&vimc_buf->list);
409
410 spin_unlock(&vcap->qlock);
411
412 /* Fill the buffer */
413 vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
414 vimc_buf->vb2.sequence = vcap->sequence++;
415 vimc_buf->vb2.field = vcap->format.field;
416
417 vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
418
419 memcpy(vbuf, frame, vcap->format.sizeimage);
420
421 /* Set it as ready */
422 vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0,
423 vcap->format.sizeimage);
424 vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500425 return NULL;
Helen Koikef2fe8902017-04-07 14:55:19 -0300426}
427
Helen Fornazier4a29b702017-06-19 14:00:18 -0300428static int vimc_cap_comp_bind(struct device *comp, struct device *master,
429 void *master_data)
Helen Koikef2fe8902017-04-07 14:55:19 -0300430{
Helen Fornazier4a29b702017-06-19 14:00:18 -0300431 struct v4l2_device *v4l2_dev = master_data;
432 struct vimc_platform_data *pdata = comp->platform_data;
Helen Koikef2fe8902017-04-07 14:55:19 -0300433 struct vimc_cap_device *vcap;
434 struct video_device *vdev;
435 struct vb2_queue *q;
436 int ret;
437
Helen Koikef2fe8902017-04-07 14:55:19 -0300438 /* Allocate the vimc_cap_device struct */
439 vcap = kzalloc(sizeof(*vcap), GFP_KERNEL);
440 if (!vcap)
Helen Fornazier4a29b702017-06-19 14:00:18 -0300441 return -ENOMEM;
Helen Koikef2fe8902017-04-07 14:55:19 -0300442
443 /* Allocate the pads */
Helen Fornazier4a29b702017-06-19 14:00:18 -0300444 vcap->ved.pads =
445 vimc_pads_init(1, (const unsigned long[1]) {MEDIA_PAD_FL_SINK});
Helen Koikef2fe8902017-04-07 14:55:19 -0300446 if (IS_ERR(vcap->ved.pads)) {
447 ret = PTR_ERR(vcap->ved.pads);
448 goto err_free_vcap;
449 }
450
451 /* Initialize the media entity */
Helen Fornazier4a29b702017-06-19 14:00:18 -0300452 vcap->vdev.entity.name = pdata->entity_name;
Helen Koikef2fe8902017-04-07 14:55:19 -0300453 vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
454 ret = media_entity_pads_init(&vcap->vdev.entity,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300455 1, vcap->ved.pads);
Helen Koikef2fe8902017-04-07 14:55:19 -0300456 if (ret)
457 goto err_clean_pads;
458
459 /* Initialize the lock */
460 mutex_init(&vcap->lock);
461
462 /* Initialize the vb2 queue */
463 q = &vcap->queue;
464 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil09714562019-01-14 09:58:26 -0500465 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
Helen Koikef2fe8902017-04-07 14:55:19 -0300466 q->drv_priv = vcap;
467 q->buf_struct_size = sizeof(struct vimc_cap_buffer);
468 q->ops = &vimc_cap_qops;
469 q->mem_ops = &vb2_vmalloc_memops;
470 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
471 q->min_buffers_needed = 2;
472 q->lock = &vcap->lock;
473
474 ret = vb2_queue_init(q);
475 if (ret) {
Helen Fornazier4a29b702017-06-19 14:00:18 -0300476 dev_err(comp, "%s: vb2 queue init failed (err=%d)\n",
477 pdata->entity_name, ret);
Helen Koikef2fe8902017-04-07 14:55:19 -0300478 goto err_clean_m_ent;
479 }
480
481 /* Initialize buffer list and its lock */
482 INIT_LIST_HEAD(&vcap->buf_list);
483 spin_lock_init(&vcap->qlock);
484
Helen Fornazier535d2962017-06-19 14:00:17 -0300485 /* Set default frame format */
486 vcap->format = fmt_default;
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400487 v4l2_fill_pixfmt(&vcap->format, vcap->format.pixelformat,
488 vcap->format.width, vcap->format.height);
Helen Koikef2fe8902017-04-07 14:55:19 -0300489
490 /* Fill the vimc_ent_device struct */
Helen Koikef2fe8902017-04-07 14:55:19 -0300491 vcap->ved.ent = &vcap->vdev.entity;
492 vcap->ved.process_frame = vimc_cap_process_frame;
Helen Fornazier288a22d2017-06-19 14:00:14 -0300493 vcap->ved.vdev_get_format = vimc_cap_get_format;
Helen Fornazier4a29b702017-06-19 14:00:18 -0300494 dev_set_drvdata(comp, &vcap->ved);
495 vcap->dev = comp;
Helen Koikef2fe8902017-04-07 14:55:19 -0300496
497 /* Initialize the video_device struct */
498 vdev = &vcap->vdev;
499 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
500 vdev->entity.ops = &vimc_cap_mops;
Hans Verkuil3650a232019-02-21 08:47:28 -0500501 vdev->release = vimc_cap_release;
Helen Koikef2fe8902017-04-07 14:55:19 -0300502 vdev->fops = &vimc_cap_fops;
503 vdev->ioctl_ops = &vimc_cap_ioctl_ops;
504 vdev->lock = &vcap->lock;
505 vdev->queue = q;
506 vdev->v4l2_dev = v4l2_dev;
507 vdev->vfl_dir = VFL_DIR_RX;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400508 strscpy(vdev->name, pdata->entity_name, sizeof(vdev->name));
Helen Koikef2fe8902017-04-07 14:55:19 -0300509 video_set_drvdata(vdev, &vcap->ved);
510
511 /* Register the video_device with the v4l2 and the media framework */
512 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
513 if (ret) {
Helen Fornazier4a29b702017-06-19 14:00:18 -0300514 dev_err(comp, "%s: video register failed (err=%d)\n",
Helen Koikef2fe8902017-04-07 14:55:19 -0300515 vcap->vdev.name, ret);
516 goto err_release_queue;
517 }
518
Helen Fornazier4a29b702017-06-19 14:00:18 -0300519 return 0;
Helen Koikef2fe8902017-04-07 14:55:19 -0300520
521err_release_queue:
522 vb2_queue_release(q);
523err_clean_m_ent:
524 media_entity_cleanup(&vcap->vdev.entity);
525err_clean_pads:
526 vimc_pads_cleanup(vcap->ved.pads);
527err_free_vcap:
528 kfree(vcap);
529
Helen Fornazier4a29b702017-06-19 14:00:18 -0300530 return ret;
Helen Koikef2fe8902017-04-07 14:55:19 -0300531}
Helen Fornazier4a29b702017-06-19 14:00:18 -0300532
533static const struct component_ops vimc_cap_comp_ops = {
534 .bind = vimc_cap_comp_bind,
535 .unbind = vimc_cap_comp_unbind,
536};
537
538static int vimc_cap_probe(struct platform_device *pdev)
539{
540 return component_add(&pdev->dev, &vimc_cap_comp_ops);
541}
542
543static int vimc_cap_remove(struct platform_device *pdev)
544{
545 component_del(&pdev->dev, &vimc_cap_comp_ops);
546
547 return 0;
548}
549
Helen Fornazier4a29b702017-06-19 14:00:18 -0300550static const struct platform_device_id vimc_cap_driver_ids[] = {
551 {
552 .name = VIMC_CAP_DRV_NAME,
553 },
554 { }
555};
556
Javier Martinez Canillasbf183e02017-07-14 05:58:39 -0300557static struct platform_driver vimc_cap_pdrv = {
558 .probe = vimc_cap_probe,
559 .remove = vimc_cap_remove,
560 .id_table = vimc_cap_driver_ids,
561 .driver = {
562 .name = VIMC_CAP_DRV_NAME,
563 },
564};
565
Helen Fornazier4a29b702017-06-19 14:00:18 -0300566module_platform_driver(vimc_cap_pdrv);
567
568MODULE_DEVICE_TABLE(platform, vimc_cap_driver_ids);
569
570MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Capture");
571MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
572MODULE_LICENSE("GPL");