Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

Linux/drivers/media/platform/blackfin/bfin_capture.c

  1 /*
  2  * Analog Devices video capture driver
  3  *
  4  * Copyright (c) 2011 Analog Devices Inc.
  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 version 2 as
  8  * published by the Free Software Foundation.
  9  *
 10  * This program is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU General Public License
 16  * along with this program; if not, write to the Free Software
 17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 18  */
 19 
 20 #include <linux/completion.h>
 21 #include <linux/delay.h>
 22 #include <linux/errno.h>
 23 #include <linux/fs.h>
 24 #include <linux/i2c.h>
 25 #include <linux/init.h>
 26 #include <linux/interrupt.h>
 27 #include <linux/io.h>
 28 #include <linux/mm.h>
 29 #include <linux/module.h>
 30 #include <linux/platform_device.h>
 31 #include <linux/slab.h>
 32 #include <linux/time.h>
 33 #include <linux/types.h>
 34 
 35 #include <media/v4l2-common.h>
 36 #include <media/v4l2-ctrls.h>
 37 #include <media/v4l2-device.h>
 38 #include <media/v4l2-ioctl.h>
 39 #include <media/videobuf2-dma-contig.h>
 40 
 41 #include <asm/dma.h>
 42 
 43 #include <media/blackfin/bfin_capture.h>
 44 #include <media/blackfin/ppi.h>
 45 
 46 #define CAPTURE_DRV_NAME        "bfin_capture"
 47 
 48 struct bcap_format {
 49         char *desc;
 50         u32 pixelformat;
 51         u32 mbus_code;
 52         int bpp; /* bits per pixel */
 53         int dlen; /* data length for ppi in bits */
 54 };
 55 
 56 struct bcap_buffer {
 57         struct vb2_buffer vb;
 58         struct list_head list;
 59 };
 60 
 61 struct bcap_device {
 62         /* capture device instance */
 63         struct v4l2_device v4l2_dev;
 64         /* v4l2 control handler */
 65         struct v4l2_ctrl_handler ctrl_handler;
 66         /* device node data */
 67         struct video_device video_dev;
 68         /* sub device instance */
 69         struct v4l2_subdev *sd;
 70         /* capture config */
 71         struct bfin_capture_config *cfg;
 72         /* ppi interface */
 73         struct ppi_if *ppi;
 74         /* current input */
 75         unsigned int cur_input;
 76         /* current selected standard */
 77         v4l2_std_id std;
 78         /* current selected dv_timings */
 79         struct v4l2_dv_timings dv_timings;
 80         /* used to store pixel format */
 81         struct v4l2_pix_format fmt;
 82         /* bits per pixel*/
 83         int bpp;
 84         /* data length for ppi in bits */
 85         int dlen;
 86         /* used to store sensor supported format */
 87         struct bcap_format *sensor_formats;
 88         /* number of sensor formats array */
 89         int num_sensor_formats;
 90         /* pointing to current video buffer */
 91         struct bcap_buffer *cur_frm;
 92         /* buffer queue used in videobuf2 */
 93         struct vb2_queue buffer_queue;
 94         /* allocator-specific contexts for each plane */
 95         struct vb2_alloc_ctx *alloc_ctx;
 96         /* queue of filled frames */
 97         struct list_head dma_queue;
 98         /* used in videobuf2 callback */
 99         spinlock_t lock;
100         /* used to access capture device */
101         struct mutex mutex;
102         /* used to wait ppi to complete one transfer */
103         struct completion comp;
104         /* prepare to stop */
105         bool stop;
106         /* vb2 buffer sequence counter */
107         unsigned sequence;
108 };
109 
110 static const struct bcap_format bcap_formats[] = {
111         {
112                 .desc        = "YCbCr 4:2:2 Interleaved UYVY",
113                 .pixelformat = V4L2_PIX_FMT_UYVY,
114                 .mbus_code   = MEDIA_BUS_FMT_UYVY8_2X8,
115                 .bpp         = 16,
116                 .dlen        = 8,
117         },
118         {
119                 .desc        = "YCbCr 4:2:2 Interleaved YUYV",
120                 .pixelformat = V4L2_PIX_FMT_YUYV,
121                 .mbus_code   = MEDIA_BUS_FMT_YUYV8_2X8,
122                 .bpp         = 16,
123                 .dlen        = 8,
124         },
125         {
126                 .desc        = "YCbCr 4:2:2 Interleaved UYVY",
127                 .pixelformat = V4L2_PIX_FMT_UYVY,
128                 .mbus_code   = MEDIA_BUS_FMT_UYVY8_1X16,
129                 .bpp         = 16,
130                 .dlen        = 16,
131         },
132         {
133                 .desc        = "RGB 565",
134                 .pixelformat = V4L2_PIX_FMT_RGB565,
135                 .mbus_code   = MEDIA_BUS_FMT_RGB565_2X8_LE,
136                 .bpp         = 16,
137                 .dlen        = 8,
138         },
139         {
140                 .desc        = "RGB 444",
141                 .pixelformat = V4L2_PIX_FMT_RGB444,
142                 .mbus_code   = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
143                 .bpp         = 16,
144                 .dlen        = 8,
145         },
146 
147 };
148 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
149 
150 static irqreturn_t bcap_isr(int irq, void *dev_id);
151 
152 static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
153 {
154         return container_of(vb, struct bcap_buffer, vb);
155 }
156 
157 static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
158 {
159         u32 code;
160         struct bcap_format *sf;
161         unsigned int num_formats = 0;
162         int i, j;
163 
164         while (!v4l2_subdev_call(bcap_dev->sd, video,
165                                 enum_mbus_fmt, num_formats, &code))
166                 num_formats++;
167         if (!num_formats)
168                 return -ENXIO;
169 
170         sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
171         if (!sf)
172                 return -ENOMEM;
173 
174         for (i = 0; i < num_formats; i++) {
175                 v4l2_subdev_call(bcap_dev->sd, video,
176                                 enum_mbus_fmt, i, &code);
177                 for (j = 0; j < BCAP_MAX_FMTS; j++)
178                         if (code == bcap_formats[j].mbus_code)
179                                 break;
180                 if (j == BCAP_MAX_FMTS) {
181                         /* we don't allow this sensor working with our bridge */
182                         kfree(sf);
183                         return -EINVAL;
184                 }
185                 sf[i] = bcap_formats[j];
186         }
187         bcap_dev->sensor_formats = sf;
188         bcap_dev->num_sensor_formats = num_formats;
189         return 0;
190 }
191 
192 static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
193 {
194         bcap_dev->num_sensor_formats = 0;
195         kfree(bcap_dev->sensor_formats);
196         bcap_dev->sensor_formats = NULL;
197 }
198 
199 static int bcap_queue_setup(struct vb2_queue *vq,
200                                 const struct v4l2_format *fmt,
201                                 unsigned int *nbuffers, unsigned int *nplanes,
202                                 unsigned int sizes[], void *alloc_ctxs[])
203 {
204         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
205 
206         if (fmt && fmt->fmt.pix.sizeimage < bcap_dev->fmt.sizeimage)
207                 return -EINVAL;
208 
209         if (vq->num_buffers + *nbuffers < 2)
210                 *nbuffers = 2;
211 
212         *nplanes = 1;
213         sizes[0] = fmt ? fmt->fmt.pix.sizeimage : bcap_dev->fmt.sizeimage;
214         alloc_ctxs[0] = bcap_dev->alloc_ctx;
215 
216         return 0;
217 }
218 
219 static int bcap_buffer_prepare(struct vb2_buffer *vb)
220 {
221         struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
222         unsigned long size = bcap_dev->fmt.sizeimage;
223 
224         if (vb2_plane_size(vb, 0) < size) {
225                 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
226                                 vb2_plane_size(vb, 0), size);
227                 return -EINVAL;
228         }
229         vb2_set_plane_payload(vb, 0, size);
230 
231         vb->v4l2_buf.field = bcap_dev->fmt.field;
232 
233         return 0;
234 }
235 
236 static void bcap_buffer_queue(struct vb2_buffer *vb)
237 {
238         struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
239         struct bcap_buffer *buf = to_bcap_vb(vb);
240         unsigned long flags;
241 
242         spin_lock_irqsave(&bcap_dev->lock, flags);
243         list_add_tail(&buf->list, &bcap_dev->dma_queue);
244         spin_unlock_irqrestore(&bcap_dev->lock, flags);
245 }
246 
247 static void bcap_buffer_cleanup(struct vb2_buffer *vb)
248 {
249         struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
250         struct bcap_buffer *buf = to_bcap_vb(vb);
251         unsigned long flags;
252 
253         spin_lock_irqsave(&bcap_dev->lock, flags);
254         list_del_init(&buf->list);
255         spin_unlock_irqrestore(&bcap_dev->lock, flags);
256 }
257 
258 static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
259 {
260         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
261         struct ppi_if *ppi = bcap_dev->ppi;
262         struct bcap_buffer *buf, *tmp;
263         struct ppi_params params;
264         dma_addr_t addr;
265         int ret;
266 
267         /* enable streamon on the sub device */
268         ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
269         if (ret && (ret != -ENOIOCTLCMD)) {
270                 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
271                 goto err;
272         }
273 
274         /* set ppi params */
275         params.width = bcap_dev->fmt.width;
276         params.height = bcap_dev->fmt.height;
277         params.bpp = bcap_dev->bpp;
278         params.dlen = bcap_dev->dlen;
279         params.ppi_control = bcap_dev->cfg->ppi_control;
280         params.int_mask = bcap_dev->cfg->int_mask;
281         if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
282                         & V4L2_IN_CAP_DV_TIMINGS) {
283                 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
284 
285                 params.hdelay = bt->hsync + bt->hbackporch;
286                 params.vdelay = bt->vsync + bt->vbackporch;
287                 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
288                 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
289         } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
290                         & V4L2_IN_CAP_STD) {
291                 params.hdelay = 0;
292                 params.vdelay = 0;
293                 if (bcap_dev->std & V4L2_STD_525_60) {
294                         params.line = 858;
295                         params.frame = 525;
296                 } else {
297                         params.line = 864;
298                         params.frame = 625;
299                 }
300         } else {
301                 params.hdelay = 0;
302                 params.vdelay = 0;
303                 params.line = params.width + bcap_dev->cfg->blank_pixels;
304                 params.frame = params.height;
305         }
306         ret = ppi->ops->set_params(ppi, &params);
307         if (ret < 0) {
308                 v4l2_err(&bcap_dev->v4l2_dev,
309                                 "Error in setting ppi params\n");
310                 goto err;
311         }
312 
313         /* attach ppi DMA irq handler */
314         ret = ppi->ops->attach_irq(ppi, bcap_isr);
315         if (ret < 0) {
316                 v4l2_err(&bcap_dev->v4l2_dev,
317                                 "Error in attaching interrupt handler\n");
318                 goto err;
319         }
320 
321         bcap_dev->sequence = 0;
322 
323         reinit_completion(&bcap_dev->comp);
324         bcap_dev->stop = false;
325 
326         /* get the next frame from the dma queue */
327         bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
328                                         struct bcap_buffer, list);
329         /* remove buffer from the dma queue */
330         list_del_init(&bcap_dev->cur_frm->list);
331         addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
332         /* update DMA address */
333         ppi->ops->update_addr(ppi, (unsigned long)addr);
334         /* enable ppi */
335         ppi->ops->start(ppi);
336 
337         return 0;
338 
339 err:
340         list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
341                 list_del(&buf->list);
342                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
343         }
344 
345         return ret;
346 }
347 
348 static void bcap_stop_streaming(struct vb2_queue *vq)
349 {
350         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
351         struct ppi_if *ppi = bcap_dev->ppi;
352         int ret;
353 
354         bcap_dev->stop = true;
355         wait_for_completion(&bcap_dev->comp);
356         ppi->ops->stop(ppi);
357         ppi->ops->detach_irq(ppi);
358         ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
359         if (ret && (ret != -ENOIOCTLCMD))
360                 v4l2_err(&bcap_dev->v4l2_dev,
361                                 "stream off failed in subdev\n");
362 
363         /* release all active buffers */
364         if (bcap_dev->cur_frm)
365                 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
366 
367         while (!list_empty(&bcap_dev->dma_queue)) {
368                 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
369                                                 struct bcap_buffer, list);
370                 list_del_init(&bcap_dev->cur_frm->list);
371                 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
372         }
373 }
374 
375 static struct vb2_ops bcap_video_qops = {
376         .queue_setup            = bcap_queue_setup,
377         .buf_prepare            = bcap_buffer_prepare,
378         .buf_cleanup            = bcap_buffer_cleanup,
379         .buf_queue              = bcap_buffer_queue,
380         .wait_prepare           = vb2_ops_wait_prepare,
381         .wait_finish            = vb2_ops_wait_finish,
382         .start_streaming        = bcap_start_streaming,
383         .stop_streaming         = bcap_stop_streaming,
384 };
385 
386 static irqreturn_t bcap_isr(int irq, void *dev_id)
387 {
388         struct ppi_if *ppi = dev_id;
389         struct bcap_device *bcap_dev = ppi->priv;
390         struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
391         dma_addr_t addr;
392 
393         spin_lock(&bcap_dev->lock);
394 
395         if (!list_empty(&bcap_dev->dma_queue)) {
396                 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
397                 if (ppi->err) {
398                         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
399                         ppi->err = false;
400                 } else {
401                         vb->v4l2_buf.sequence = bcap_dev->sequence++;
402                         vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
403                 }
404                 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
405                                 struct bcap_buffer, list);
406                 list_del_init(&bcap_dev->cur_frm->list);
407         } else {
408                 /* clear error flag, we will get a new frame */
409                 if (ppi->err)
410                         ppi->err = false;
411         }
412 
413         ppi->ops->stop(ppi);
414 
415         if (bcap_dev->stop) {
416                 complete(&bcap_dev->comp);
417         } else {
418                 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
419                 ppi->ops->update_addr(ppi, (unsigned long)addr);
420                 ppi->ops->start(ppi);
421         }
422 
423         spin_unlock(&bcap_dev->lock);
424 
425         return IRQ_HANDLED;
426 }
427 
428 static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
429 {
430         struct bcap_device *bcap_dev = video_drvdata(file);
431         struct v4l2_input input;
432 
433         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
434         if (!(input.capabilities & V4L2_IN_CAP_STD))
435                 return -ENODATA;
436 
437         return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
438 }
439 
440 static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
441 {
442         struct bcap_device *bcap_dev = video_drvdata(file);
443         struct v4l2_input input;
444 
445         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
446         if (!(input.capabilities & V4L2_IN_CAP_STD))
447                 return -ENODATA;
448 
449         *std = bcap_dev->std;
450         return 0;
451 }
452 
453 static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
454 {
455         struct bcap_device *bcap_dev = video_drvdata(file);
456         struct v4l2_input input;
457         int ret;
458 
459         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
460         if (!(input.capabilities & V4L2_IN_CAP_STD))
461                 return -ENODATA;
462 
463         if (vb2_is_busy(&bcap_dev->buffer_queue))
464                 return -EBUSY;
465 
466         ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
467         if (ret < 0)
468                 return ret;
469 
470         bcap_dev->std = std;
471         return 0;
472 }
473 
474 static int bcap_enum_dv_timings(struct file *file, void *priv,
475                                 struct v4l2_enum_dv_timings *timings)
476 {
477         struct bcap_device *bcap_dev = video_drvdata(file);
478         struct v4l2_input input;
479 
480         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
481         if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
482                 return -ENODATA;
483 
484         timings->pad = 0;
485 
486         return v4l2_subdev_call(bcap_dev->sd, pad,
487                         enum_dv_timings, timings);
488 }
489 
490 static int bcap_query_dv_timings(struct file *file, void *priv,
491                                 struct v4l2_dv_timings *timings)
492 {
493         struct bcap_device *bcap_dev = video_drvdata(file);
494         struct v4l2_input input;
495 
496         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
497         if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
498                 return -ENODATA;
499 
500         return v4l2_subdev_call(bcap_dev->sd, video,
501                                 query_dv_timings, timings);
502 }
503 
504 static int bcap_g_dv_timings(struct file *file, void *priv,
505                                 struct v4l2_dv_timings *timings)
506 {
507         struct bcap_device *bcap_dev = video_drvdata(file);
508         struct v4l2_input input;
509 
510         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
511         if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
512                 return -ENODATA;
513 
514         *timings = bcap_dev->dv_timings;
515         return 0;
516 }
517 
518 static int bcap_s_dv_timings(struct file *file, void *priv,
519                                 struct v4l2_dv_timings *timings)
520 {
521         struct bcap_device *bcap_dev = video_drvdata(file);
522         struct v4l2_input input;
523         int ret;
524 
525         input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
526         if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
527                 return -ENODATA;
528 
529         if (vb2_is_busy(&bcap_dev->buffer_queue))
530                 return -EBUSY;
531 
532         ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
533         if (ret < 0)
534                 return ret;
535 
536         bcap_dev->dv_timings = *timings;
537         return 0;
538 }
539 
540 static int bcap_enum_input(struct file *file, void *priv,
541                                 struct v4l2_input *input)
542 {
543         struct bcap_device *bcap_dev = video_drvdata(file);
544         struct bfin_capture_config *config = bcap_dev->cfg;
545         int ret;
546         u32 status;
547 
548         if (input->index >= config->num_inputs)
549                 return -EINVAL;
550 
551         *input = config->inputs[input->index];
552         /* get input status */
553         ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
554         if (!ret)
555                 input->status = status;
556         return 0;
557 }
558 
559 static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
560 {
561         struct bcap_device *bcap_dev = video_drvdata(file);
562 
563         *index = bcap_dev->cur_input;
564         return 0;
565 }
566 
567 static int bcap_s_input(struct file *file, void *priv, unsigned int index)
568 {
569         struct bcap_device *bcap_dev = video_drvdata(file);
570         struct bfin_capture_config *config = bcap_dev->cfg;
571         struct bcap_route *route;
572         int ret;
573 
574         if (vb2_is_busy(&bcap_dev->buffer_queue))
575                 return -EBUSY;
576 
577         if (index >= config->num_inputs)
578                 return -EINVAL;
579 
580         route = &config->routes[index];
581         ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
582                                 route->input, route->output, 0);
583         if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
584                 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
585                 return ret;
586         }
587         bcap_dev->cur_input = index;
588         /* if this route has specific config, update ppi control */
589         if (route->ppi_control)
590                 config->ppi_control = route->ppi_control;
591         return 0;
592 }
593 
594 static int bcap_try_format(struct bcap_device *bcap,
595                                 struct v4l2_pix_format *pixfmt,
596                                 struct bcap_format *bcap_fmt)
597 {
598         struct bcap_format *sf = bcap->sensor_formats;
599         struct bcap_format *fmt = NULL;
600         struct v4l2_mbus_framefmt mbus_fmt;
601         int ret, i;
602 
603         for (i = 0; i < bcap->num_sensor_formats; i++) {
604                 fmt = &sf[i];
605                 if (pixfmt->pixelformat == fmt->pixelformat)
606                         break;
607         }
608         if (i == bcap->num_sensor_formats)
609                 fmt = &sf[0];
610 
611         v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
612         ret = v4l2_subdev_call(bcap->sd, video,
613                                 try_mbus_fmt, &mbus_fmt);
614         if (ret < 0)
615                 return ret;
616         v4l2_fill_pix_format(pixfmt, &mbus_fmt);
617         if (bcap_fmt) {
618                 for (i = 0; i < bcap->num_sensor_formats; i++) {
619                         fmt = &sf[i];
620                         if (mbus_fmt.code == fmt->mbus_code)
621                                 break;
622                 }
623                 *bcap_fmt = *fmt;
624         }
625         pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
626         pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
627         return 0;
628 }
629 
630 static int bcap_enum_fmt_vid_cap(struct file *file, void  *priv,
631                                         struct v4l2_fmtdesc *fmt)
632 {
633         struct bcap_device *bcap_dev = video_drvdata(file);
634         struct bcap_format *sf = bcap_dev->sensor_formats;
635 
636         if (fmt->index >= bcap_dev->num_sensor_formats)
637                 return -EINVAL;
638 
639         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
640         strlcpy(fmt->description,
641                 sf[fmt->index].desc,
642                 sizeof(fmt->description));
643         fmt->pixelformat = sf[fmt->index].pixelformat;
644         return 0;
645 }
646 
647 static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
648                                         struct v4l2_format *fmt)
649 {
650         struct bcap_device *bcap_dev = video_drvdata(file);
651         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
652 
653         return bcap_try_format(bcap_dev, pixfmt, NULL);
654 }
655 
656 static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
657                                 struct v4l2_format *fmt)
658 {
659         struct bcap_device *bcap_dev = video_drvdata(file);
660 
661         fmt->fmt.pix = bcap_dev->fmt;
662         return 0;
663 }
664 
665 static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
666                                 struct v4l2_format *fmt)
667 {
668         struct bcap_device *bcap_dev = video_drvdata(file);
669         struct v4l2_mbus_framefmt mbus_fmt;
670         struct bcap_format bcap_fmt;
671         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
672         int ret;
673 
674         if (vb2_is_busy(&bcap_dev->buffer_queue))
675                 return -EBUSY;
676 
677         /* see if format works */
678         ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
679         if (ret < 0)
680                 return ret;
681 
682         v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
683         ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
684         if (ret < 0)
685                 return ret;
686         bcap_dev->fmt = *pixfmt;
687         bcap_dev->bpp = bcap_fmt.bpp;
688         bcap_dev->dlen = bcap_fmt.dlen;
689         return 0;
690 }
691 
692 static int bcap_querycap(struct file *file, void  *priv,
693                                 struct v4l2_capability *cap)
694 {
695         struct bcap_device *bcap_dev = video_drvdata(file);
696 
697         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
698         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
699         strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
700         strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
701         strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
702         return 0;
703 }
704 
705 static int bcap_g_parm(struct file *file, void *fh,
706                                 struct v4l2_streamparm *a)
707 {
708         struct bcap_device *bcap_dev = video_drvdata(file);
709 
710         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
711                 return -EINVAL;
712         return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
713 }
714 
715 static int bcap_s_parm(struct file *file, void *fh,
716                                 struct v4l2_streamparm *a)
717 {
718         struct bcap_device *bcap_dev = video_drvdata(file);
719 
720         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
721                 return -EINVAL;
722         return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
723 }
724 
725 static int bcap_log_status(struct file *file, void *priv)
726 {
727         struct bcap_device *bcap_dev = video_drvdata(file);
728         /* status for sub devices */
729         v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
730         return 0;
731 }
732 
733 static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
734         .vidioc_querycap         = bcap_querycap,
735         .vidioc_g_fmt_vid_cap    = bcap_g_fmt_vid_cap,
736         .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
737         .vidioc_s_fmt_vid_cap    = bcap_s_fmt_vid_cap,
738         .vidioc_try_fmt_vid_cap  = bcap_try_fmt_vid_cap,
739         .vidioc_enum_input       = bcap_enum_input,
740         .vidioc_g_input          = bcap_g_input,
741         .vidioc_s_input          = bcap_s_input,
742         .vidioc_querystd         = bcap_querystd,
743         .vidioc_s_std            = bcap_s_std,
744         .vidioc_g_std            = bcap_g_std,
745         .vidioc_s_dv_timings     = bcap_s_dv_timings,
746         .vidioc_g_dv_timings     = bcap_g_dv_timings,
747         .vidioc_query_dv_timings = bcap_query_dv_timings,
748         .vidioc_enum_dv_timings  = bcap_enum_dv_timings,
749         .vidioc_reqbufs          = vb2_ioctl_reqbufs,
750         .vidioc_create_bufs      = vb2_ioctl_create_bufs,
751         .vidioc_querybuf         = vb2_ioctl_querybuf,
752         .vidioc_qbuf             = vb2_ioctl_qbuf,
753         .vidioc_dqbuf            = vb2_ioctl_dqbuf,
754         .vidioc_expbuf           = vb2_ioctl_expbuf,
755         .vidioc_streamon         = vb2_ioctl_streamon,
756         .vidioc_streamoff        = vb2_ioctl_streamoff,
757         .vidioc_g_parm           = bcap_g_parm,
758         .vidioc_s_parm           = bcap_s_parm,
759         .vidioc_log_status       = bcap_log_status,
760 };
761 
762 static struct v4l2_file_operations bcap_fops = {
763         .owner = THIS_MODULE,
764         .open = v4l2_fh_open,
765         .release = vb2_fop_release,
766         .unlocked_ioctl = video_ioctl2,
767         .mmap = vb2_fop_mmap,
768 #ifndef CONFIG_MMU
769         .get_unmapped_area = vb2_fop_get_unmapped_area,
770 #endif
771         .poll = vb2_fop_poll
772 };
773 
774 static int bcap_probe(struct platform_device *pdev)
775 {
776         struct bcap_device *bcap_dev;
777         struct video_device *vfd;
778         struct i2c_adapter *i2c_adap;
779         struct bfin_capture_config *config;
780         struct vb2_queue *q;
781         struct bcap_route *route;
782         int ret;
783 
784         config = pdev->dev.platform_data;
785         if (!config || !config->num_inputs) {
786                 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
787                 return -ENODEV;
788         }
789 
790         bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
791         if (!bcap_dev) {
792                 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
793                 return -ENOMEM;
794         }
795 
796         bcap_dev->cfg = config;
797 
798         bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
799         if (!bcap_dev->ppi) {
800                 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
801                 ret = -ENODEV;
802                 goto err_free_dev;
803         }
804         bcap_dev->ppi->priv = bcap_dev;
805 
806         bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
807         if (IS_ERR(bcap_dev->alloc_ctx)) {
808                 ret = PTR_ERR(bcap_dev->alloc_ctx);
809                 goto err_free_ppi;
810         }
811 
812         vfd = &bcap_dev->video_dev;
813         /* initialize field of video device */
814         vfd->release            = video_device_release_empty;
815         vfd->fops               = &bcap_fops;
816         vfd->ioctl_ops          = &bcap_ioctl_ops;
817         vfd->tvnorms            = 0;
818         vfd->v4l2_dev           = &bcap_dev->v4l2_dev;
819         strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
820 
821         ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
822         if (ret) {
823                 v4l2_err(pdev->dev.driver,
824                                 "Unable to register v4l2 device\n");
825                 goto err_cleanup_ctx;
826         }
827         v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
828 
829         bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
830         ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
831         if (ret) {
832                 v4l2_err(&bcap_dev->v4l2_dev,
833                                 "Unable to init control handler\n");
834                 goto err_unreg_v4l2;
835         }
836 
837         spin_lock_init(&bcap_dev->lock);
838         /* initialize queue */
839         q = &bcap_dev->buffer_queue;
840         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
841         q->io_modes = VB2_MMAP | VB2_DMABUF;
842         q->drv_priv = bcap_dev;
843         q->buf_struct_size = sizeof(struct bcap_buffer);
844         q->ops = &bcap_video_qops;
845         q->mem_ops = &vb2_dma_contig_memops;
846         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
847         q->lock = &bcap_dev->mutex;
848         q->min_buffers_needed = 1;
849 
850         ret = vb2_queue_init(q);
851         if (ret)
852                 goto err_free_handler;
853 
854         mutex_init(&bcap_dev->mutex);
855         init_completion(&bcap_dev->comp);
856 
857         /* init video dma queues */
858         INIT_LIST_HEAD(&bcap_dev->dma_queue);
859 
860         vfd->lock = &bcap_dev->mutex;
861         vfd->queue = q;
862 
863         /* register video device */
864         ret = video_register_device(&bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
865         if (ret) {
866                 v4l2_err(&bcap_dev->v4l2_dev,
867                                 "Unable to register video device\n");
868                 goto err_free_handler;
869         }
870         video_set_drvdata(&bcap_dev->video_dev, bcap_dev);
871         v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
872                         video_device_node_name(vfd));
873 
874         /* load up the subdevice */
875         i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
876         if (!i2c_adap) {
877                 v4l2_err(&bcap_dev->v4l2_dev,
878                                 "Unable to find i2c adapter\n");
879                 ret = -ENODEV;
880                 goto err_unreg_vdev;
881 
882         }
883         bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
884                                                  i2c_adap,
885                                                  &config->board_info,
886                                                  NULL);
887         if (bcap_dev->sd) {
888                 int i;
889 
890                 /* update tvnorms from the sub devices */
891                 for (i = 0; i < config->num_inputs; i++)
892                         vfd->tvnorms |= config->inputs[i].std;
893         } else {
894                 v4l2_err(&bcap_dev->v4l2_dev,
895                                 "Unable to register sub device\n");
896                 ret = -ENODEV;
897                 goto err_unreg_vdev;
898         }
899 
900         v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
901 
902         /*
903          * explicitly set input, otherwise some boards
904          * may not work at the state as we expected
905          */
906         route = &config->routes[0];
907         ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
908                                 route->input, route->output, 0);
909         if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
910                 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
911                 goto err_unreg_vdev;
912         }
913         bcap_dev->cur_input = 0;
914         /* if this route has specific config, update ppi control */
915         if (route->ppi_control)
916                 config->ppi_control = route->ppi_control;
917 
918         /* now we can probe the default state */
919         if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
920                 v4l2_std_id std;
921                 ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
922                 if (ret) {
923                         v4l2_err(&bcap_dev->v4l2_dev,
924                                         "Unable to get std\n");
925                         goto err_unreg_vdev;
926                 }
927                 bcap_dev->std = std;
928         }
929         if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
930                 struct v4l2_dv_timings dv_timings;
931                 ret = v4l2_subdev_call(bcap_dev->sd, video,
932                                 g_dv_timings, &dv_timings);
933                 if (ret) {
934                         v4l2_err(&bcap_dev->v4l2_dev,
935                                         "Unable to get dv timings\n");
936                         goto err_unreg_vdev;
937                 }
938                 bcap_dev->dv_timings = dv_timings;
939         }
940         ret = bcap_init_sensor_formats(bcap_dev);
941         if (ret) {
942                 v4l2_err(&bcap_dev->v4l2_dev,
943                                 "Unable to create sensor formats table\n");
944                 goto err_unreg_vdev;
945         }
946         return 0;
947 err_unreg_vdev:
948         video_unregister_device(&bcap_dev->video_dev);
949 err_free_handler:
950         v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
951 err_unreg_v4l2:
952         v4l2_device_unregister(&bcap_dev->v4l2_dev);
953 err_cleanup_ctx:
954         vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
955 err_free_ppi:
956         ppi_delete_instance(bcap_dev->ppi);
957 err_free_dev:
958         kfree(bcap_dev);
959         return ret;
960 }
961 
962 static int bcap_remove(struct platform_device *pdev)
963 {
964         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
965         struct bcap_device *bcap_dev = container_of(v4l2_dev,
966                                                 struct bcap_device, v4l2_dev);
967 
968         bcap_free_sensor_formats(bcap_dev);
969         video_unregister_device(&bcap_dev->video_dev);
970         v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
971         v4l2_device_unregister(v4l2_dev);
972         vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
973         ppi_delete_instance(bcap_dev->ppi);
974         kfree(bcap_dev);
975         return 0;
976 }
977 
978 static struct platform_driver bcap_driver = {
979         .driver = {
980                 .name  = CAPTURE_DRV_NAME,
981         },
982         .probe = bcap_probe,
983         .remove = bcap_remove,
984 };
985 module_platform_driver(bcap_driver);
986 
987 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
988 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
989 MODULE_LICENSE("GPL v2");
990 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us