Version:  2.0.40 2.2.26 2.4.37 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 4.2

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

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