FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / media / video / timblogiw.c
blob84d4c7c83435caa355971c1bd0536d27253e0bd3
1 /*
2 * timblogiw.c timberdale FPGA LogiWin Video In driver
3 * Copyright (c) 2009-2010 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* Supports:
20 * Timberdale FPGA LogiWin Video In
23 #include <linux/version.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/dmaengine.h>
27 #include <linux/mfd/core.h>
28 #include <linux/scatterlist.h>
29 #include <linux/interrupt.h>
30 #include <linux/list.h>
31 #include <linux/i2c.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-device.h>
34 #include <media/videobuf-dma-contig.h>
35 #include <media/timb_video.h>
37 #define DRIVER_NAME "timb-video"
39 #define TIMBLOGIWIN_NAME "Timberdale Video-In"
40 #define TIMBLOGIW_VERSION_CODE 0x04
42 #define TIMBLOGIW_LINES_PER_DESC 44
43 #define TIMBLOGIW_MAX_VIDEO_MEM 16
45 #define TIMBLOGIW_HAS_DECODER(lw) (lw->pdata.encoder.module_name)
48 struct timblogiw {
49 struct video_device video_dev;
50 struct v4l2_device v4l2_dev; /* mutual exclusion */
51 struct mutex lock;
52 struct device *dev;
53 struct timb_video_platform_data pdata;
54 struct v4l2_subdev *sd_enc; /* encoder */
55 bool opened;
58 struct timblogiw_tvnorm {
59 v4l2_std_id std;
60 u16 width;
61 u16 height;
62 u8 fps;
65 struct timblogiw_fh {
66 struct videobuf_queue vb_vidq;
67 struct timblogiw_tvnorm const *cur_norm;
68 struct list_head capture;
69 struct dma_chan *chan;
70 spinlock_t queue_lock; /* mutual exclusion */
71 unsigned int frame_count;
74 struct timblogiw_buffer {
75 /* common v4l buffer stuff -- must be first */
76 struct videobuf_buffer vb;
77 struct scatterlist sg[16];
78 dma_cookie_t cookie;
79 struct timblogiw_fh *fh;
82 const struct timblogiw_tvnorm timblogiw_tvnorms[] = {
84 .std = V4L2_STD_PAL,
85 .width = 720,
86 .height = 576,
87 .fps = 25
90 .std = V4L2_STD_NTSC,
91 .width = 720,
92 .height = 480,
93 .fps = 30
97 static int timblogiw_bytes_per_line(const struct timblogiw_tvnorm *norm)
99 return norm->width * 2;
103 static int timblogiw_frame_size(const struct timblogiw_tvnorm *norm)
105 return norm->height * timblogiw_bytes_per_line(norm);
108 static const struct timblogiw_tvnorm *timblogiw_get_norm(const v4l2_std_id std)
110 int i;
111 for (i = 0; i < ARRAY_SIZE(timblogiw_tvnorms); i++)
112 if (timblogiw_tvnorms[i].std & std)
113 return timblogiw_tvnorms + i;
115 /* default to first element */
116 return timblogiw_tvnorms;
119 static void timblogiw_dma_cb(void *data)
121 struct timblogiw_buffer *buf = data;
122 struct timblogiw_fh *fh = buf->fh;
123 struct videobuf_buffer *vb = &buf->vb;
125 spin_lock(&fh->queue_lock);
127 /* mark the transfer done */
128 buf->cookie = -1;
130 fh->frame_count++;
132 if (vb->state != VIDEOBUF_ERROR) {
133 list_del(&vb->queue);
134 do_gettimeofday(&vb->ts);
135 vb->field_count = fh->frame_count * 2;
136 vb->state = VIDEOBUF_DONE;
138 wake_up(&vb->done);
141 if (!list_empty(&fh->capture)) {
142 vb = list_entry(fh->capture.next, struct videobuf_buffer,
143 queue);
144 vb->state = VIDEOBUF_ACTIVE;
147 spin_unlock(&fh->queue_lock);
150 static bool timblogiw_dma_filter_fn(struct dma_chan *chan, void *filter_param)
152 return chan->chan_id == (uintptr_t)filter_param;
155 /* IOCTL functions */
157 static int timblogiw_g_fmt(struct file *file, void *priv,
158 struct v4l2_format *format)
160 struct video_device *vdev = video_devdata(file);
161 struct timblogiw *lw = video_get_drvdata(vdev);
162 struct timblogiw_fh *fh = priv;
164 dev_dbg(&vdev->dev, "%s entry\n", __func__);
166 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
167 return -EINVAL;
169 mutex_lock(&lw->lock);
171 format->fmt.pix.width = fh->cur_norm->width;
172 format->fmt.pix.height = fh->cur_norm->height;
173 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
174 format->fmt.pix.bytesperline = timblogiw_bytes_per_line(fh->cur_norm);
175 format->fmt.pix.sizeimage = timblogiw_frame_size(fh->cur_norm);
176 format->fmt.pix.field = V4L2_FIELD_NONE;
178 mutex_unlock(&lw->lock);
180 return 0;
183 static int timblogiw_try_fmt(struct file *file, void *priv,
184 struct v4l2_format *format)
186 struct video_device *vdev = video_devdata(file);
187 struct v4l2_pix_format *pix = &format->fmt.pix;
189 dev_dbg(&vdev->dev,
190 "%s - width=%d, height=%d, pixelformat=%d, field=%d\n"
191 "bytes per line %d, size image: %d, colorspace: %d\n",
192 __func__,
193 pix->width, pix->height, pix->pixelformat, pix->field,
194 pix->bytesperline, pix->sizeimage, pix->colorspace);
196 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
197 return -EINVAL;
199 if (pix->field != V4L2_FIELD_NONE)
200 return -EINVAL;
202 if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
203 return -EINVAL;
205 return 0;
208 static int timblogiw_s_fmt(struct file *file, void *priv,
209 struct v4l2_format *format)
211 struct video_device *vdev = video_devdata(file);
212 struct timblogiw *lw = video_get_drvdata(vdev);
213 struct timblogiw_fh *fh = priv;
214 struct v4l2_pix_format *pix = &format->fmt.pix;
215 int err;
217 mutex_lock(&lw->lock);
219 err = timblogiw_try_fmt(file, priv, format);
220 if (err)
221 goto out;
223 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
224 dev_err(&vdev->dev, "%s queue busy\n", __func__);
225 err = -EBUSY;
226 goto out;
229 pix->width = fh->cur_norm->width;
230 pix->height = fh->cur_norm->height;
232 out:
233 mutex_unlock(&lw->lock);
234 return err;
237 static int timblogiw_querycap(struct file *file, void *priv,
238 struct v4l2_capability *cap)
240 struct video_device *vdev = video_devdata(file);
242 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
243 memset(cap, 0, sizeof(*cap));
244 strncpy(cap->card, TIMBLOGIWIN_NAME, sizeof(cap->card)-1);
245 strncpy(cap->driver, DRIVER_NAME, sizeof(cap->driver) - 1);
246 strlcpy(cap->bus_info, vdev->name, sizeof(cap->bus_info));
247 cap->version = TIMBLOGIW_VERSION_CODE;
248 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
249 V4L2_CAP_READWRITE;
251 return 0;
254 static int timblogiw_enum_fmt(struct file *file, void *priv,
255 struct v4l2_fmtdesc *fmt)
257 struct video_device *vdev = video_devdata(file);
259 dev_dbg(&vdev->dev, "%s, index: %d\n", __func__, fmt->index);
261 if (fmt->index != 0)
262 return -EINVAL;
263 memset(fmt, 0, sizeof(*fmt));
264 fmt->index = 0;
265 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
266 strncpy(fmt->description, "4:2:2, packed, YUYV",
267 sizeof(fmt->description)-1);
268 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
270 return 0;
273 static int timblogiw_g_parm(struct file *file, void *priv,
274 struct v4l2_streamparm *sp)
276 struct timblogiw_fh *fh = priv;
277 struct v4l2_captureparm *cp = &sp->parm.capture;
279 cp->capability = V4L2_CAP_TIMEPERFRAME;
280 cp->timeperframe.numerator = 1;
281 cp->timeperframe.denominator = fh->cur_norm->fps;
283 return 0;
286 static int timblogiw_reqbufs(struct file *file, void *priv,
287 struct v4l2_requestbuffers *rb)
289 struct video_device *vdev = video_devdata(file);
290 struct timblogiw_fh *fh = priv;
292 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
294 return videobuf_reqbufs(&fh->vb_vidq, rb);
297 static int timblogiw_querybuf(struct file *file, void *priv,
298 struct v4l2_buffer *b)
300 struct video_device *vdev = video_devdata(file);
301 struct timblogiw_fh *fh = priv;
303 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
305 return videobuf_querybuf(&fh->vb_vidq, b);
308 static int timblogiw_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
310 struct video_device *vdev = video_devdata(file);
311 struct timblogiw_fh *fh = priv;
313 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
315 return videobuf_qbuf(&fh->vb_vidq, b);
318 static int timblogiw_dqbuf(struct file *file, void *priv,
319 struct v4l2_buffer *b)
321 struct video_device *vdev = video_devdata(file);
322 struct timblogiw_fh *fh = priv;
324 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
326 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
329 static int timblogiw_g_std(struct file *file, void *priv, v4l2_std_id *std)
331 struct video_device *vdev = video_devdata(file);
332 struct timblogiw_fh *fh = priv;
334 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
336 *std = fh->cur_norm->std;
337 return 0;
340 static int timblogiw_s_std(struct file *file, void *priv, v4l2_std_id *std)
342 struct video_device *vdev = video_devdata(file);
343 struct timblogiw *lw = video_get_drvdata(vdev);
344 struct timblogiw_fh *fh = priv;
345 int err = 0;
347 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
349 mutex_lock(&lw->lock);
351 if (TIMBLOGIW_HAS_DECODER(lw))
352 err = v4l2_subdev_call(lw->sd_enc, core, s_std, *std);
354 if (!err)
355 fh->cur_norm = timblogiw_get_norm(*std);
357 mutex_unlock(&lw->lock);
359 return err;
362 static int timblogiw_enuminput(struct file *file, void *priv,
363 struct v4l2_input *inp)
365 struct video_device *vdev = video_devdata(file);
366 int i;
368 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
370 if (inp->index != 0)
371 return -EINVAL;
373 inp->index = 0;
375 strncpy(inp->name, "Timb input 1", sizeof(inp->name) - 1);
376 inp->type = V4L2_INPUT_TYPE_CAMERA;
378 inp->std = 0;
379 for (i = 0; i < ARRAY_SIZE(timblogiw_tvnorms); i++)
380 inp->std |= timblogiw_tvnorms[i].std;
382 return 0;
385 static int timblogiw_g_input(struct file *file, void *priv,
386 unsigned int *input)
388 struct video_device *vdev = video_devdata(file);
390 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
392 *input = 0;
394 return 0;
397 static int timblogiw_s_input(struct file *file, void *priv, unsigned int input)
399 struct video_device *vdev = video_devdata(file);
401 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
403 if (input != 0)
404 return -EINVAL;
405 return 0;
408 static int timblogiw_streamon(struct file *file, void *priv, unsigned int type)
410 struct video_device *vdev = video_devdata(file);
411 struct timblogiw_fh *fh = priv;
413 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
415 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
416 dev_dbg(&vdev->dev, "%s - No capture device\n", __func__);
417 return -EINVAL;
420 fh->frame_count = 0;
421 return videobuf_streamon(&fh->vb_vidq);
424 static int timblogiw_streamoff(struct file *file, void *priv,
425 unsigned int type)
427 struct video_device *vdev = video_devdata(file);
428 struct timblogiw_fh *fh = priv;
430 dev_dbg(&vdev->dev, "%s entry\n", __func__);
432 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
433 return -EINVAL;
435 return videobuf_streamoff(&fh->vb_vidq);
438 static int timblogiw_querystd(struct file *file, void *priv, v4l2_std_id *std)
440 struct video_device *vdev = video_devdata(file);
441 struct timblogiw *lw = video_get_drvdata(vdev);
442 struct timblogiw_fh *fh = priv;
444 dev_dbg(&vdev->dev, "%s entry\n", __func__);
446 if (TIMBLOGIW_HAS_DECODER(lw))
447 return v4l2_subdev_call(lw->sd_enc, video, querystd, std);
448 else {
449 *std = fh->cur_norm->std;
450 return 0;
454 static int timblogiw_enum_framesizes(struct file *file, void *priv,
455 struct v4l2_frmsizeenum *fsize)
457 struct video_device *vdev = video_devdata(file);
458 struct timblogiw_fh *fh = priv;
460 dev_dbg(&vdev->dev, "%s - index: %d, format: %d\n", __func__,
461 fsize->index, fsize->pixel_format);
463 if ((fsize->index != 0) ||
464 (fsize->pixel_format != V4L2_PIX_FMT_UYVY))
465 return -EINVAL;
467 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
468 fsize->discrete.width = fh->cur_norm->width;
469 fsize->discrete.height = fh->cur_norm->height;
471 return 0;
474 /* Video buffer functions */
476 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
477 unsigned int *size)
479 struct timblogiw_fh *fh = vq->priv_data;
481 *size = timblogiw_frame_size(fh->cur_norm);
483 if (!*count)
484 *count = 32;
486 while (*size * *count > TIMBLOGIW_MAX_VIDEO_MEM * 1024 * 1024)
487 (*count)--;
489 return 0;
492 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
493 enum v4l2_field field)
495 struct timblogiw_fh *fh = vq->priv_data;
496 struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
497 vb);
498 unsigned int data_size = timblogiw_frame_size(fh->cur_norm);
499 int err = 0;
501 if (vb->baddr && vb->bsize < data_size)
502 /* User provided buffer, but it is too small */
503 return -ENOMEM;
505 vb->size = data_size;
506 vb->width = fh->cur_norm->width;
507 vb->height = fh->cur_norm->height;
508 vb->field = field;
510 if (vb->state == VIDEOBUF_NEEDS_INIT) {
511 int i;
512 unsigned int size;
513 unsigned int bytes_per_desc = TIMBLOGIW_LINES_PER_DESC *
514 timblogiw_bytes_per_line(fh->cur_norm);
515 dma_addr_t addr;
517 sg_init_table(buf->sg, ARRAY_SIZE(buf->sg));
519 err = videobuf_iolock(vq, vb, NULL);
520 if (err)
521 goto err;
523 addr = videobuf_to_dma_contig(vb);
524 for (i = 0, size = 0; size < data_size; i++) {
525 sg_dma_address(buf->sg + i) = addr + size;
526 size += bytes_per_desc;
527 sg_dma_len(buf->sg + i) = (size > data_size) ?
528 (bytes_per_desc - (size - data_size)) :
529 bytes_per_desc;
532 vb->state = VIDEOBUF_PREPARED;
533 buf->cookie = -1;
534 buf->fh = fh;
537 return 0;
539 err:
540 videobuf_dma_contig_free(vq, vb);
541 vb->state = VIDEOBUF_NEEDS_INIT;
542 return err;
545 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
547 struct timblogiw_fh *fh = vq->priv_data;
548 struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
549 vb);
550 struct dma_async_tx_descriptor *desc;
551 int sg_elems;
552 int bytes_per_desc = TIMBLOGIW_LINES_PER_DESC *
553 timblogiw_bytes_per_line(fh->cur_norm);
555 sg_elems = timblogiw_frame_size(fh->cur_norm) / bytes_per_desc;
556 sg_elems +=
557 (timblogiw_frame_size(fh->cur_norm) % bytes_per_desc) ? 1 : 0;
559 if (list_empty(&fh->capture))
560 vb->state = VIDEOBUF_ACTIVE;
561 else
562 vb->state = VIDEOBUF_QUEUED;
564 list_add_tail(&vb->queue, &fh->capture);
566 spin_unlock_irq(&fh->queue_lock);
568 desc = fh->chan->device->device_prep_slave_sg(fh->chan,
569 buf->sg, sg_elems, DMA_FROM_DEVICE,
570 DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP);
571 if (!desc) {
572 spin_lock_irq(&fh->queue_lock);
573 list_del_init(&vb->queue);
574 vb->state = VIDEOBUF_PREPARED;
575 return;
578 desc->callback_param = buf;
579 desc->callback = timblogiw_dma_cb;
581 buf->cookie = desc->tx_submit(desc);
583 spin_lock_irq(&fh->queue_lock);
586 static void buffer_release(struct videobuf_queue *vq,
587 struct videobuf_buffer *vb)
589 struct timblogiw_fh *fh = vq->priv_data;
590 struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
591 vb);
593 videobuf_waiton(vq, vb, 0, 0);
594 if (buf->cookie >= 0)
595 dma_sync_wait(fh->chan, buf->cookie);
597 videobuf_dma_contig_free(vq, vb);
598 vb->state = VIDEOBUF_NEEDS_INIT;
601 static struct videobuf_queue_ops timblogiw_video_qops = {
602 .buf_setup = buffer_setup,
603 .buf_prepare = buffer_prepare,
604 .buf_queue = buffer_queue,
605 .buf_release = buffer_release,
608 /* Device Operations functions */
610 static int timblogiw_open(struct file *file)
612 struct video_device *vdev = video_devdata(file);
613 struct timblogiw *lw = video_get_drvdata(vdev);
614 struct timblogiw_fh *fh;
615 v4l2_std_id std;
616 dma_cap_mask_t mask;
617 int err = 0;
619 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
621 mutex_lock(&lw->lock);
622 if (lw->opened) {
623 err = -EBUSY;
624 goto out;
627 if (TIMBLOGIW_HAS_DECODER(lw) && !lw->sd_enc) {
628 struct i2c_adapter *adapt;
630 /* find the video decoder */
631 adapt = i2c_get_adapter(lw->pdata.i2c_adapter);
632 if (!adapt) {
633 dev_err(&vdev->dev, "No I2C bus #%d\n",
634 lw->pdata.i2c_adapter);
635 err = -ENODEV;
636 goto out;
639 /* now find the encoder */
640 lw->sd_enc = v4l2_i2c_new_subdev_board(&lw->v4l2_dev, adapt,
641 lw->pdata.encoder.info, NULL);
643 i2c_put_adapter(adapt);
645 if (!lw->sd_enc) {
646 dev_err(&vdev->dev, "Failed to get encoder: %s\n",
647 lw->pdata.encoder.module_name);
648 err = -ENODEV;
649 goto out;
653 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
654 if (!fh) {
655 err = -ENOMEM;
656 goto out;
659 fh->cur_norm = timblogiw_tvnorms;
660 timblogiw_querystd(file, fh, &std);
661 fh->cur_norm = timblogiw_get_norm(std);
663 INIT_LIST_HEAD(&fh->capture);
664 spin_lock_init(&fh->queue_lock);
666 dma_cap_zero(mask);
667 dma_cap_set(DMA_SLAVE, mask);
668 dma_cap_set(DMA_PRIVATE, mask);
670 /* find the DMA channel */
671 fh->chan = dma_request_channel(mask, timblogiw_dma_filter_fn,
672 (void *)(uintptr_t)lw->pdata.dma_channel);
673 if (!fh->chan) {
674 dev_err(&vdev->dev, "Failed to get DMA channel\n");
675 kfree(fh);
676 err = -ENODEV;
677 goto out;
680 file->private_data = fh;
681 videobuf_queue_dma_contig_init(&fh->vb_vidq,
682 &timblogiw_video_qops, lw->dev, &fh->queue_lock,
683 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
684 sizeof(struct timblogiw_buffer), fh, NULL);
686 lw->opened = true;
687 out:
688 mutex_unlock(&lw->lock);
690 return err;
693 static int timblogiw_close(struct file *file)
695 struct video_device *vdev = video_devdata(file);
696 struct timblogiw *lw = video_get_drvdata(vdev);
697 struct timblogiw_fh *fh = file->private_data;
699 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
701 videobuf_stop(&fh->vb_vidq);
702 videobuf_mmap_free(&fh->vb_vidq);
704 dma_release_channel(fh->chan);
706 kfree(fh);
708 mutex_lock(&lw->lock);
709 lw->opened = false;
710 mutex_unlock(&lw->lock);
711 return 0;
714 static ssize_t timblogiw_read(struct file *file, char __user *data,
715 size_t count, loff_t *ppos)
717 struct video_device *vdev = video_devdata(file);
718 struct timblogiw_fh *fh = file->private_data;
720 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
722 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
723 file->f_flags & O_NONBLOCK);
726 static unsigned int timblogiw_poll(struct file *file,
727 struct poll_table_struct *wait)
729 struct video_device *vdev = video_devdata(file);
730 struct timblogiw_fh *fh = file->private_data;
732 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
734 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
737 static int timblogiw_mmap(struct file *file, struct vm_area_struct *vma)
739 struct video_device *vdev = video_devdata(file);
740 struct timblogiw_fh *fh = file->private_data;
742 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
744 return videobuf_mmap_mapper(&fh->vb_vidq, vma);
747 /* Platform device functions */
749 static __devinitconst struct v4l2_ioctl_ops timblogiw_ioctl_ops = {
750 .vidioc_querycap = timblogiw_querycap,
751 .vidioc_enum_fmt_vid_cap = timblogiw_enum_fmt,
752 .vidioc_g_fmt_vid_cap = timblogiw_g_fmt,
753 .vidioc_try_fmt_vid_cap = timblogiw_try_fmt,
754 .vidioc_s_fmt_vid_cap = timblogiw_s_fmt,
755 .vidioc_g_parm = timblogiw_g_parm,
756 .vidioc_reqbufs = timblogiw_reqbufs,
757 .vidioc_querybuf = timblogiw_querybuf,
758 .vidioc_qbuf = timblogiw_qbuf,
759 .vidioc_dqbuf = timblogiw_dqbuf,
760 .vidioc_g_std = timblogiw_g_std,
761 .vidioc_s_std = timblogiw_s_std,
762 .vidioc_enum_input = timblogiw_enuminput,
763 .vidioc_g_input = timblogiw_g_input,
764 .vidioc_s_input = timblogiw_s_input,
765 .vidioc_streamon = timblogiw_streamon,
766 .vidioc_streamoff = timblogiw_streamoff,
767 .vidioc_querystd = timblogiw_querystd,
768 .vidioc_enum_framesizes = timblogiw_enum_framesizes,
771 static __devinitconst struct v4l2_file_operations timblogiw_fops = {
772 .owner = THIS_MODULE,
773 .open = timblogiw_open,
774 .release = timblogiw_close,
775 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
776 .mmap = timblogiw_mmap,
777 .read = timblogiw_read,
778 .poll = timblogiw_poll,
781 static __devinitconst struct video_device timblogiw_template = {
782 .name = TIMBLOGIWIN_NAME,
783 .fops = &timblogiw_fops,
784 .ioctl_ops = &timblogiw_ioctl_ops,
785 .release = video_device_release_empty,
786 .minor = -1,
787 .tvnorms = V4L2_STD_PAL | V4L2_STD_NTSC
790 static int __devinit timblogiw_probe(struct platform_device *pdev)
792 int err;
793 struct timblogiw *lw = NULL;
794 struct timb_video_platform_data *pdata = mfd_get_data(pdev);
796 if (!pdata) {
797 dev_err(&pdev->dev, "No platform data\n");
798 err = -EINVAL;
799 goto err;
802 if (!pdata->encoder.module_name)
803 dev_info(&pdev->dev, "Running without decoder\n");
805 lw = kzalloc(sizeof(*lw), GFP_KERNEL);
806 if (!lw) {
807 err = -ENOMEM;
808 goto err;
811 if (pdev->dev.parent)
812 lw->dev = pdev->dev.parent;
813 else
814 lw->dev = &pdev->dev;
816 memcpy(&lw->pdata, pdata, sizeof(lw->pdata));
818 mutex_init(&lw->lock);
820 lw->video_dev = timblogiw_template;
822 strlcpy(lw->v4l2_dev.name, DRIVER_NAME, sizeof(lw->v4l2_dev.name));
823 err = v4l2_device_register(NULL, &lw->v4l2_dev);
824 if (err)
825 goto err_register;
827 lw->video_dev.v4l2_dev = &lw->v4l2_dev;
829 platform_set_drvdata(pdev, lw);
830 video_set_drvdata(&lw->video_dev, lw);
832 err = video_register_device(&lw->video_dev, VFL_TYPE_GRABBER, 0);
833 if (err) {
834 dev_err(&pdev->dev, "Error reg video: %d\n", err);
835 goto err_request;
839 return 0;
841 err_request:
842 platform_set_drvdata(pdev, NULL);
843 v4l2_device_unregister(&lw->v4l2_dev);
844 err_register:
845 kfree(lw);
846 err:
847 dev_err(&pdev->dev, "Failed to register: %d\n", err);
849 return err;
852 static int __devexit timblogiw_remove(struct platform_device *pdev)
854 struct timblogiw *lw = platform_get_drvdata(pdev);
856 video_unregister_device(&lw->video_dev);
858 v4l2_device_unregister(&lw->v4l2_dev);
860 kfree(lw);
862 platform_set_drvdata(pdev, NULL);
864 return 0;
867 static struct platform_driver timblogiw_platform_driver = {
868 .driver = {
869 .name = DRIVER_NAME,
870 .owner = THIS_MODULE,
872 .probe = timblogiw_probe,
873 .remove = __devexit_p(timblogiw_remove),
876 /* Module functions */
878 static int __init timblogiw_init(void)
880 return platform_driver_register(&timblogiw_platform_driver);
883 static void __exit timblogiw_exit(void)
885 platform_driver_unregister(&timblogiw_platform_driver);
888 module_init(timblogiw_init);
889 module_exit(timblogiw_exit);
891 MODULE_DESCRIPTION(TIMBLOGIWIN_NAME);
892 MODULE_AUTHOR("Pelagicore AB <info@pelagicore.com>");
893 MODULE_LICENSE("GPL v2");
894 MODULE_ALIAS("platform:"DRIVER_NAME);