Commit | Line | Data |
---|---|---|
63b1a90d SJ |
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-chip-ident.h> | |
36 | #include <media/v4l2-common.h> | |
37 | #include <media/v4l2-ctrls.h> | |
38 | #include <media/v4l2-device.h> | |
39 | #include <media/v4l2-ioctl.h> | |
40 | #include <media/videobuf2-dma-contig.h> | |
41 | ||
42 | #include <asm/dma.h> | |
43 | ||
44 | #include <media/blackfin/bfin_capture.h> | |
45 | #include <media/blackfin/ppi.h> | |
46 | ||
47 | #define CAPTURE_DRV_NAME "bfin_capture" | |
48 | #define BCAP_MIN_NUM_BUF 2 | |
49 | ||
50 | struct bcap_format { | |
51 | char *desc; | |
52 | u32 pixelformat; | |
53 | enum v4l2_mbus_pixelcode mbus_code; | |
54 | int bpp; /* bits per pixel */ | |
45b82596 | 55 | int dlen; /* data length for ppi in bits */ |
63b1a90d SJ |
56 | }; |
57 | ||
58 | struct bcap_buffer { | |
59 | struct vb2_buffer vb; | |
60 | struct list_head list; | |
61 | }; | |
62 | ||
63 | struct bcap_device { | |
64 | /* capture device instance */ | |
65 | struct v4l2_device v4l2_dev; | |
66 | /* v4l2 control handler */ | |
67 | struct v4l2_ctrl_handler ctrl_handler; | |
68 | /* device node data */ | |
69 | struct video_device *video_dev; | |
70 | /* sub device instance */ | |
71 | struct v4l2_subdev *sd; | |
72 | /* capture config */ | |
73 | struct bfin_capture_config *cfg; | |
74 | /* ppi interface */ | |
75 | struct ppi_if *ppi; | |
76 | /* current input */ | |
77 | unsigned int cur_input; | |
78 | /* current selected standard */ | |
79 | v4l2_std_id std; | |
45b82596 SJ |
80 | /* current selected dv_timings */ |
81 | struct v4l2_dv_timings dv_timings; | |
63b1a90d SJ |
82 | /* used to store pixel format */ |
83 | struct v4l2_pix_format fmt; | |
84 | /* bits per pixel*/ | |
85 | int bpp; | |
45b82596 SJ |
86 | /* data length for ppi in bits */ |
87 | int dlen; | |
63b1a90d SJ |
88 | /* used to store sensor supported format */ |
89 | struct bcap_format *sensor_formats; | |
90 | /* number of sensor formats array */ | |
91 | int num_sensor_formats; | |
92 | /* pointing to current video buffer */ | |
93 | struct bcap_buffer *cur_frm; | |
63b1a90d SJ |
94 | /* buffer queue used in videobuf2 */ |
95 | struct vb2_queue buffer_queue; | |
96 | /* allocator-specific contexts for each plane */ | |
97 | struct vb2_alloc_ctx *alloc_ctx; | |
98 | /* queue of filled frames */ | |
99 | struct list_head dma_queue; | |
100 | /* used in videobuf2 callback */ | |
101 | spinlock_t lock; | |
102 | /* used to access capture device */ | |
103 | struct mutex mutex; | |
104 | /* used to wait ppi to complete one transfer */ | |
105 | struct completion comp; | |
106 | /* prepare to stop */ | |
107 | bool stop; | |
108 | }; | |
109 | ||
110 | struct bcap_fh { | |
111 | struct v4l2_fh fh; | |
112 | /* indicates whether this file handle is doing IO */ | |
113 | bool io_allowed; | |
114 | }; | |
115 | ||
116 | static const struct bcap_format bcap_formats[] = { | |
117 | { | |
118 | .desc = "YCbCr 4:2:2 Interleaved UYVY", | |
119 | .pixelformat = V4L2_PIX_FMT_UYVY, | |
120 | .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8, | |
121 | .bpp = 16, | |
45b82596 | 122 | .dlen = 8, |
63b1a90d SJ |
123 | }, |
124 | { | |
125 | .desc = "YCbCr 4:2:2 Interleaved YUYV", | |
126 | .pixelformat = V4L2_PIX_FMT_YUYV, | |
127 | .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8, | |
128 | .bpp = 16, | |
45b82596 SJ |
129 | .dlen = 8, |
130 | }, | |
131 | { | |
132 | .desc = "YCbCr 4:2:2 Interleaved UYVY", | |
133 | .pixelformat = V4L2_PIX_FMT_UYVY, | |
134 | .mbus_code = V4L2_MBUS_FMT_UYVY8_1X16, | |
135 | .bpp = 16, | |
136 | .dlen = 16, | |
63b1a90d SJ |
137 | }, |
138 | { | |
139 | .desc = "RGB 565", | |
140 | .pixelformat = V4L2_PIX_FMT_RGB565, | |
141 | .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE, | |
142 | .bpp = 16, | |
45b82596 | 143 | .dlen = 8, |
63b1a90d SJ |
144 | }, |
145 | { | |
146 | .desc = "RGB 444", | |
147 | .pixelformat = V4L2_PIX_FMT_RGB444, | |
148 | .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE, | |
149 | .bpp = 16, | |
45b82596 | 150 | .dlen = 8, |
63b1a90d SJ |
151 | }, |
152 | ||
153 | }; | |
154 | #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats) | |
155 | ||
156 | static irqreturn_t bcap_isr(int irq, void *dev_id); | |
157 | ||
158 | static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb) | |
159 | { | |
160 | return container_of(vb, struct bcap_buffer, vb); | |
161 | } | |
162 | ||
163 | static int bcap_init_sensor_formats(struct bcap_device *bcap_dev) | |
164 | { | |
165 | enum v4l2_mbus_pixelcode code; | |
166 | struct bcap_format *sf; | |
167 | unsigned int num_formats = 0; | |
168 | int i, j; | |
169 | ||
170 | while (!v4l2_subdev_call(bcap_dev->sd, video, | |
171 | enum_mbus_fmt, num_formats, &code)) | |
172 | num_formats++; | |
173 | if (!num_formats) | |
174 | return -ENXIO; | |
175 | ||
176 | sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL); | |
177 | if (!sf) | |
178 | return -ENOMEM; | |
179 | ||
180 | for (i = 0; i < num_formats; i++) { | |
181 | v4l2_subdev_call(bcap_dev->sd, video, | |
182 | enum_mbus_fmt, i, &code); | |
183 | for (j = 0; j < BCAP_MAX_FMTS; j++) | |
184 | if (code == bcap_formats[j].mbus_code) | |
185 | break; | |
186 | if (j == BCAP_MAX_FMTS) { | |
187 | /* we don't allow this sensor working with our bridge */ | |
188 | kfree(sf); | |
189 | return -EINVAL; | |
190 | } | |
191 | sf[i] = bcap_formats[j]; | |
192 | } | |
193 | bcap_dev->sensor_formats = sf; | |
194 | bcap_dev->num_sensor_formats = num_formats; | |
195 | return 0; | |
196 | } | |
197 | ||
198 | static void bcap_free_sensor_formats(struct bcap_device *bcap_dev) | |
199 | { | |
200 | bcap_dev->num_sensor_formats = 0; | |
201 | kfree(bcap_dev->sensor_formats); | |
202 | bcap_dev->sensor_formats = NULL; | |
203 | } | |
204 | ||
205 | static int bcap_open(struct file *file) | |
206 | { | |
207 | struct bcap_device *bcap_dev = video_drvdata(file); | |
208 | struct video_device *vfd = bcap_dev->video_dev; | |
209 | struct bcap_fh *bcap_fh; | |
210 | ||
211 | if (!bcap_dev->sd) { | |
212 | v4l2_err(&bcap_dev->v4l2_dev, "No sub device registered\n"); | |
213 | return -ENODEV; | |
214 | } | |
215 | ||
216 | bcap_fh = kzalloc(sizeof(*bcap_fh), GFP_KERNEL); | |
217 | if (!bcap_fh) { | |
218 | v4l2_err(&bcap_dev->v4l2_dev, | |
219 | "unable to allocate memory for file handle object\n"); | |
220 | return -ENOMEM; | |
221 | } | |
222 | ||
223 | v4l2_fh_init(&bcap_fh->fh, vfd); | |
224 | ||
225 | /* store pointer to v4l2_fh in private_data member of file */ | |
226 | file->private_data = &bcap_fh->fh; | |
227 | v4l2_fh_add(&bcap_fh->fh); | |
228 | bcap_fh->io_allowed = false; | |
229 | return 0; | |
230 | } | |
231 | ||
232 | static int bcap_release(struct file *file) | |
233 | { | |
234 | struct bcap_device *bcap_dev = video_drvdata(file); | |
235 | struct v4l2_fh *fh = file->private_data; | |
236 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); | |
237 | ||
238 | /* if this instance is doing IO */ | |
239 | if (bcap_fh->io_allowed) | |
240 | vb2_queue_release(&bcap_dev->buffer_queue); | |
241 | ||
242 | file->private_data = NULL; | |
243 | v4l2_fh_del(&bcap_fh->fh); | |
244 | v4l2_fh_exit(&bcap_fh->fh); | |
245 | kfree(bcap_fh); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | static int bcap_mmap(struct file *file, struct vm_area_struct *vma) | |
250 | { | |
251 | struct bcap_device *bcap_dev = video_drvdata(file); | |
28c3682a | 252 | int ret; |
63b1a90d | 253 | |
28c3682a HV |
254 | if (mutex_lock_interruptible(&bcap_dev->mutex)) |
255 | return -ERESTARTSYS; | |
256 | ret = vb2_mmap(&bcap_dev->buffer_queue, vma); | |
257 | mutex_unlock(&bcap_dev->mutex); | |
258 | return ret; | |
63b1a90d SJ |
259 | } |
260 | ||
261 | #ifndef CONFIG_MMU | |
262 | static unsigned long bcap_get_unmapped_area(struct file *file, | |
263 | unsigned long addr, | |
264 | unsigned long len, | |
265 | unsigned long pgoff, | |
266 | unsigned long flags) | |
267 | { | |
268 | struct bcap_device *bcap_dev = video_drvdata(file); | |
269 | ||
270 | return vb2_get_unmapped_area(&bcap_dev->buffer_queue, | |
271 | addr, | |
272 | len, | |
273 | pgoff, | |
274 | flags); | |
275 | } | |
276 | #endif | |
277 | ||
278 | static unsigned int bcap_poll(struct file *file, poll_table *wait) | |
279 | { | |
280 | struct bcap_device *bcap_dev = video_drvdata(file); | |
28c3682a | 281 | unsigned int res; |
63b1a90d | 282 | |
28c3682a HV |
283 | mutex_lock(&bcap_dev->mutex); |
284 | res = vb2_poll(&bcap_dev->buffer_queue, file, wait); | |
285 | mutex_unlock(&bcap_dev->mutex); | |
286 | return res; | |
63b1a90d SJ |
287 | } |
288 | ||
289 | static int bcap_queue_setup(struct vb2_queue *vq, | |
290 | const struct v4l2_format *fmt, | |
291 | unsigned int *nbuffers, unsigned int *nplanes, | |
292 | unsigned int sizes[], void *alloc_ctxs[]) | |
293 | { | |
294 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); | |
295 | ||
296 | if (*nbuffers < BCAP_MIN_NUM_BUF) | |
297 | *nbuffers = BCAP_MIN_NUM_BUF; | |
298 | ||
299 | *nplanes = 1; | |
300 | sizes[0] = bcap_dev->fmt.sizeimage; | |
301 | alloc_ctxs[0] = bcap_dev->alloc_ctx; | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
306 | static int bcap_buffer_init(struct vb2_buffer *vb) | |
307 | { | |
308 | struct bcap_buffer *buf = to_bcap_vb(vb); | |
309 | ||
310 | INIT_LIST_HEAD(&buf->list); | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int bcap_buffer_prepare(struct vb2_buffer *vb) | |
315 | { | |
316 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue); | |
317 | struct bcap_buffer *buf = to_bcap_vb(vb); | |
318 | unsigned long size; | |
319 | ||
320 | size = bcap_dev->fmt.sizeimage; | |
321 | if (vb2_plane_size(vb, 0) < size) { | |
322 | v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n", | |
323 | vb2_plane_size(vb, 0), size); | |
324 | return -EINVAL; | |
325 | } | |
326 | vb2_set_plane_payload(&buf->vb, 0, size); | |
327 | ||
328 | return 0; | |
329 | } | |
330 | ||
331 | static void bcap_buffer_queue(struct vb2_buffer *vb) | |
332 | { | |
333 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue); | |
334 | struct bcap_buffer *buf = to_bcap_vb(vb); | |
335 | unsigned long flags; | |
336 | ||
337 | spin_lock_irqsave(&bcap_dev->lock, flags); | |
338 | list_add_tail(&buf->list, &bcap_dev->dma_queue); | |
339 | spin_unlock_irqrestore(&bcap_dev->lock, flags); | |
340 | } | |
341 | ||
342 | static void bcap_buffer_cleanup(struct vb2_buffer *vb) | |
343 | { | |
344 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue); | |
345 | struct bcap_buffer *buf = to_bcap_vb(vb); | |
346 | unsigned long flags; | |
347 | ||
348 | spin_lock_irqsave(&bcap_dev->lock, flags); | |
349 | list_del_init(&buf->list); | |
350 | spin_unlock_irqrestore(&bcap_dev->lock, flags); | |
351 | } | |
352 | ||
353 | static void bcap_lock(struct vb2_queue *vq) | |
354 | { | |
355 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); | |
356 | mutex_lock(&bcap_dev->mutex); | |
357 | } | |
358 | ||
359 | static void bcap_unlock(struct vb2_queue *vq) | |
360 | { | |
361 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); | |
362 | mutex_unlock(&bcap_dev->mutex); | |
363 | } | |
364 | ||
365 | static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count) | |
366 | { | |
367 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); | |
368 | struct ppi_if *ppi = bcap_dev->ppi; | |
369 | struct ppi_params params; | |
370 | int ret; | |
371 | ||
372 | /* enable streamon on the sub device */ | |
373 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1); | |
374 | if (ret && (ret != -ENOIOCTLCMD)) { | |
375 | v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n"); | |
376 | return ret; | |
377 | } | |
378 | ||
379 | /* set ppi params */ | |
380 | params.width = bcap_dev->fmt.width; | |
381 | params.height = bcap_dev->fmt.height; | |
382 | params.bpp = bcap_dev->bpp; | |
45b82596 | 383 | params.dlen = bcap_dev->dlen; |
63b1a90d SJ |
384 | params.ppi_control = bcap_dev->cfg->ppi_control; |
385 | params.int_mask = bcap_dev->cfg->int_mask; | |
45b82596 | 386 | if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities |
a8451ed2 | 387 | & V4L2_IN_CAP_DV_TIMINGS) { |
45b82596 SJ |
388 | struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt; |
389 | ||
390 | params.hdelay = bt->hsync + bt->hbackporch; | |
391 | params.vdelay = bt->vsync + bt->vbackporch; | |
392 | params.line = bt->hfrontporch + bt->hsync | |
393 | + bt->hbackporch + bt->width; | |
394 | params.frame = bt->vfrontporch + bt->vsync | |
395 | + bt->vbackporch + bt->height; | |
396 | if (bt->interlaced) | |
397 | params.frame += bt->il_vfrontporch + bt->il_vsync | |
398 | + bt->il_vbackporch; | |
399 | } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities | |
400 | & V4L2_IN_CAP_STD) { | |
401 | params.hdelay = 0; | |
402 | params.vdelay = 0; | |
403 | if (bcap_dev->std & V4L2_STD_525_60) { | |
404 | params.line = 858; | |
405 | params.frame = 525; | |
406 | } else { | |
407 | params.line = 864; | |
408 | params.frame = 625; | |
409 | } | |
410 | } else { | |
411 | params.hdelay = 0; | |
412 | params.vdelay = 0; | |
413 | params.line = params.width + bcap_dev->cfg->blank_pixels; | |
414 | params.frame = params.height; | |
415 | } | |
63b1a90d SJ |
416 | ret = ppi->ops->set_params(ppi, ¶ms); |
417 | if (ret < 0) { | |
418 | v4l2_err(&bcap_dev->v4l2_dev, | |
419 | "Error in setting ppi params\n"); | |
420 | return ret; | |
421 | } | |
422 | ||
423 | /* attach ppi DMA irq handler */ | |
424 | ret = ppi->ops->attach_irq(ppi, bcap_isr); | |
425 | if (ret < 0) { | |
426 | v4l2_err(&bcap_dev->v4l2_dev, | |
427 | "Error in attaching interrupt handler\n"); | |
428 | return ret; | |
429 | } | |
430 | ||
431 | INIT_COMPLETION(bcap_dev->comp); | |
432 | bcap_dev->stop = false; | |
433 | return 0; | |
434 | } | |
435 | ||
436 | static int bcap_stop_streaming(struct vb2_queue *vq) | |
437 | { | |
438 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); | |
439 | struct ppi_if *ppi = bcap_dev->ppi; | |
440 | int ret; | |
441 | ||
442 | if (!vb2_is_streaming(vq)) | |
443 | return 0; | |
444 | ||
445 | bcap_dev->stop = true; | |
446 | wait_for_completion(&bcap_dev->comp); | |
447 | ppi->ops->stop(ppi); | |
448 | ppi->ops->detach_irq(ppi); | |
449 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0); | |
450 | if (ret && (ret != -ENOIOCTLCMD)) | |
451 | v4l2_err(&bcap_dev->v4l2_dev, | |
452 | "stream off failed in subdev\n"); | |
453 | ||
454 | /* release all active buffers */ | |
455 | while (!list_empty(&bcap_dev->dma_queue)) { | |
d78a4882 | 456 | bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next, |
63b1a90d | 457 | struct bcap_buffer, list); |
d78a4882 SJ |
458 | list_del(&bcap_dev->cur_frm->list); |
459 | vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR); | |
63b1a90d SJ |
460 | } |
461 | return 0; | |
462 | } | |
463 | ||
464 | static struct vb2_ops bcap_video_qops = { | |
465 | .queue_setup = bcap_queue_setup, | |
466 | .buf_init = bcap_buffer_init, | |
467 | .buf_prepare = bcap_buffer_prepare, | |
468 | .buf_cleanup = bcap_buffer_cleanup, | |
469 | .buf_queue = bcap_buffer_queue, | |
470 | .wait_prepare = bcap_unlock, | |
471 | .wait_finish = bcap_lock, | |
472 | .start_streaming = bcap_start_streaming, | |
473 | .stop_streaming = bcap_stop_streaming, | |
474 | }; | |
475 | ||
476 | static int bcap_reqbufs(struct file *file, void *priv, | |
477 | struct v4l2_requestbuffers *req_buf) | |
478 | { | |
479 | struct bcap_device *bcap_dev = video_drvdata(file); | |
480 | struct vb2_queue *vq = &bcap_dev->buffer_queue; | |
481 | struct v4l2_fh *fh = file->private_data; | |
482 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); | |
483 | ||
484 | if (vb2_is_busy(vq)) | |
485 | return -EBUSY; | |
486 | ||
487 | bcap_fh->io_allowed = true; | |
488 | ||
489 | return vb2_reqbufs(vq, req_buf); | |
490 | } | |
491 | ||
492 | static int bcap_querybuf(struct file *file, void *priv, | |
493 | struct v4l2_buffer *buf) | |
494 | { | |
495 | struct bcap_device *bcap_dev = video_drvdata(file); | |
496 | ||
497 | return vb2_querybuf(&bcap_dev->buffer_queue, buf); | |
498 | } | |
499 | ||
500 | static int bcap_qbuf(struct file *file, void *priv, | |
501 | struct v4l2_buffer *buf) | |
502 | { | |
503 | struct bcap_device *bcap_dev = video_drvdata(file); | |
504 | struct v4l2_fh *fh = file->private_data; | |
505 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); | |
506 | ||
507 | if (!bcap_fh->io_allowed) | |
508 | return -EBUSY; | |
509 | ||
510 | return vb2_qbuf(&bcap_dev->buffer_queue, buf); | |
511 | } | |
512 | ||
513 | static int bcap_dqbuf(struct file *file, void *priv, | |
514 | struct v4l2_buffer *buf) | |
515 | { | |
516 | struct bcap_device *bcap_dev = video_drvdata(file); | |
517 | struct v4l2_fh *fh = file->private_data; | |
518 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); | |
519 | ||
520 | if (!bcap_fh->io_allowed) | |
521 | return -EBUSY; | |
522 | ||
523 | return vb2_dqbuf(&bcap_dev->buffer_queue, | |
524 | buf, file->f_flags & O_NONBLOCK); | |
525 | } | |
526 | ||
527 | static irqreturn_t bcap_isr(int irq, void *dev_id) | |
528 | { | |
529 | struct ppi_if *ppi = dev_id; | |
530 | struct bcap_device *bcap_dev = ppi->priv; | |
63b1a90d SJ |
531 | struct vb2_buffer *vb = &bcap_dev->cur_frm->vb; |
532 | dma_addr_t addr; | |
533 | ||
534 | spin_lock(&bcap_dev->lock); | |
535 | ||
d78a4882 | 536 | if (!list_empty(&bcap_dev->dma_queue)) { |
8e6057b5 | 537 | v4l2_get_timestamp(&vb->v4l2_buf.timestamp); |
d78a4882 SJ |
538 | if (ppi->err) { |
539 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
540 | ppi->err = false; | |
541 | } else { | |
542 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); | |
543 | } | |
544 | bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next, | |
545 | struct bcap_buffer, list); | |
546 | list_del(&bcap_dev->cur_frm->list); | |
547 | } else { | |
548 | /* clear error flag, we will get a new frame */ | |
549 | if (ppi->err) | |
550 | ppi->err = false; | |
63b1a90d SJ |
551 | } |
552 | ||
553 | ppi->ops->stop(ppi); | |
554 | ||
555 | if (bcap_dev->stop) { | |
556 | complete(&bcap_dev->comp); | |
557 | } else { | |
d78a4882 SJ |
558 | addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0); |
559 | ppi->ops->update_addr(ppi, (unsigned long)addr); | |
63b1a90d SJ |
560 | ppi->ops->start(ppi); |
561 | } | |
562 | ||
563 | spin_unlock(&bcap_dev->lock); | |
564 | ||
565 | return IRQ_HANDLED; | |
566 | } | |
567 | ||
568 | static int bcap_streamon(struct file *file, void *priv, | |
569 | enum v4l2_buf_type buf_type) | |
570 | { | |
571 | struct bcap_device *bcap_dev = video_drvdata(file); | |
572 | struct bcap_fh *fh = file->private_data; | |
573 | struct ppi_if *ppi = bcap_dev->ppi; | |
574 | dma_addr_t addr; | |
575 | int ret; | |
576 | ||
577 | if (!fh->io_allowed) | |
578 | return -EBUSY; | |
579 | ||
580 | /* call streamon to start streaming in videobuf */ | |
581 | ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type); | |
582 | if (ret) | |
583 | return ret; | |
584 | ||
585 | /* if dma queue is empty, return error */ | |
586 | if (list_empty(&bcap_dev->dma_queue)) { | |
587 | v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n"); | |
588 | ret = -EINVAL; | |
589 | goto err; | |
590 | } | |
591 | ||
592 | /* get the next frame from the dma queue */ | |
d78a4882 | 593 | bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next, |
63b1a90d | 594 | struct bcap_buffer, list); |
63b1a90d SJ |
595 | /* remove buffer from the dma queue */ |
596 | list_del(&bcap_dev->cur_frm->list); | |
597 | addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0); | |
598 | /* update DMA address */ | |
599 | ppi->ops->update_addr(ppi, (unsigned long)addr); | |
600 | /* enable ppi */ | |
601 | ppi->ops->start(ppi); | |
602 | ||
603 | return 0; | |
604 | err: | |
605 | vb2_streamoff(&bcap_dev->buffer_queue, buf_type); | |
606 | return ret; | |
607 | } | |
608 | ||
609 | static int bcap_streamoff(struct file *file, void *priv, | |
610 | enum v4l2_buf_type buf_type) | |
611 | { | |
612 | struct bcap_device *bcap_dev = video_drvdata(file); | |
613 | struct bcap_fh *fh = file->private_data; | |
614 | ||
615 | if (!fh->io_allowed) | |
616 | return -EBUSY; | |
617 | ||
618 | return vb2_streamoff(&bcap_dev->buffer_queue, buf_type); | |
619 | } | |
620 | ||
621 | static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std) | |
622 | { | |
623 | struct bcap_device *bcap_dev = video_drvdata(file); | |
624 | ||
625 | return v4l2_subdev_call(bcap_dev->sd, video, querystd, std); | |
626 | } | |
627 | ||
628 | static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std) | |
629 | { | |
630 | struct bcap_device *bcap_dev = video_drvdata(file); | |
631 | ||
632 | *std = bcap_dev->std; | |
633 | return 0; | |
634 | } | |
635 | ||
314527ac | 636 | static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std) |
63b1a90d SJ |
637 | { |
638 | struct bcap_device *bcap_dev = video_drvdata(file); | |
639 | int ret; | |
640 | ||
641 | if (vb2_is_busy(&bcap_dev->buffer_queue)) | |
642 | return -EBUSY; | |
643 | ||
314527ac | 644 | ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, std); |
63b1a90d SJ |
645 | if (ret < 0) |
646 | return ret; | |
647 | ||
314527ac | 648 | bcap_dev->std = std; |
63b1a90d SJ |
649 | return 0; |
650 | } | |
651 | ||
45b82596 SJ |
652 | static int bcap_g_dv_timings(struct file *file, void *priv, |
653 | struct v4l2_dv_timings *timings) | |
654 | { | |
655 | struct bcap_device *bcap_dev = video_drvdata(file); | |
656 | int ret; | |
657 | ||
658 | ret = v4l2_subdev_call(bcap_dev->sd, video, | |
659 | g_dv_timings, timings); | |
660 | if (ret < 0) | |
661 | return ret; | |
662 | ||
663 | bcap_dev->dv_timings = *timings; | |
664 | return 0; | |
665 | } | |
666 | ||
667 | static int bcap_s_dv_timings(struct file *file, void *priv, | |
668 | struct v4l2_dv_timings *timings) | |
669 | { | |
670 | struct bcap_device *bcap_dev = video_drvdata(file); | |
671 | int ret; | |
672 | if (vb2_is_busy(&bcap_dev->buffer_queue)) | |
673 | return -EBUSY; | |
674 | ||
675 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings); | |
676 | if (ret < 0) | |
677 | return ret; | |
678 | ||
679 | bcap_dev->dv_timings = *timings; | |
680 | return 0; | |
681 | } | |
682 | ||
63b1a90d SJ |
683 | static int bcap_enum_input(struct file *file, void *priv, |
684 | struct v4l2_input *input) | |
685 | { | |
686 | struct bcap_device *bcap_dev = video_drvdata(file); | |
687 | struct bfin_capture_config *config = bcap_dev->cfg; | |
688 | int ret; | |
689 | u32 status; | |
690 | ||
691 | if (input->index >= config->num_inputs) | |
692 | return -EINVAL; | |
693 | ||
694 | *input = config->inputs[input->index]; | |
695 | /* get input status */ | |
696 | ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status); | |
697 | if (!ret) | |
698 | input->status = status; | |
699 | return 0; | |
700 | } | |
701 | ||
702 | static int bcap_g_input(struct file *file, void *priv, unsigned int *index) | |
703 | { | |
704 | struct bcap_device *bcap_dev = video_drvdata(file); | |
705 | ||
706 | *index = bcap_dev->cur_input; | |
707 | return 0; | |
708 | } | |
709 | ||
710 | static int bcap_s_input(struct file *file, void *priv, unsigned int index) | |
711 | { | |
712 | struct bcap_device *bcap_dev = video_drvdata(file); | |
713 | struct bfin_capture_config *config = bcap_dev->cfg; | |
714 | struct bcap_route *route; | |
715 | int ret; | |
716 | ||
717 | if (vb2_is_busy(&bcap_dev->buffer_queue)) | |
718 | return -EBUSY; | |
719 | ||
720 | if (index >= config->num_inputs) | |
721 | return -EINVAL; | |
722 | ||
723 | route = &config->routes[index]; | |
724 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing, | |
725 | route->input, route->output, 0); | |
726 | if ((ret < 0) && (ret != -ENOIOCTLCMD)) { | |
727 | v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n"); | |
728 | return ret; | |
729 | } | |
730 | bcap_dev->cur_input = index; | |
45b82596 SJ |
731 | /* if this route has specific config, update ppi control */ |
732 | if (route->ppi_control) | |
733 | config->ppi_control = route->ppi_control; | |
63b1a90d SJ |
734 | return 0; |
735 | } | |
736 | ||
737 | static int bcap_try_format(struct bcap_device *bcap, | |
738 | struct v4l2_pix_format *pixfmt, | |
45b82596 | 739 | struct bcap_format *bcap_fmt) |
63b1a90d SJ |
740 | { |
741 | struct bcap_format *sf = bcap->sensor_formats; | |
742 | struct bcap_format *fmt = NULL; | |
743 | struct v4l2_mbus_framefmt mbus_fmt; | |
744 | int ret, i; | |
745 | ||
746 | for (i = 0; i < bcap->num_sensor_formats; i++) { | |
747 | fmt = &sf[i]; | |
748 | if (pixfmt->pixelformat == fmt->pixelformat) | |
749 | break; | |
750 | } | |
751 | if (i == bcap->num_sensor_formats) | |
752 | fmt = &sf[0]; | |
753 | ||
63b1a90d SJ |
754 | v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code); |
755 | ret = v4l2_subdev_call(bcap->sd, video, | |
756 | try_mbus_fmt, &mbus_fmt); | |
757 | if (ret < 0) | |
758 | return ret; | |
759 | v4l2_fill_pix_format(pixfmt, &mbus_fmt); | |
45b82596 SJ |
760 | if (bcap_fmt) { |
761 | for (i = 0; i < bcap->num_sensor_formats; i++) { | |
762 | fmt = &sf[i]; | |
763 | if (mbus_fmt.code == fmt->mbus_code) | |
764 | break; | |
765 | } | |
766 | *bcap_fmt = *fmt; | |
767 | } | |
63b1a90d SJ |
768 | pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8; |
769 | pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height; | |
770 | return 0; | |
771 | } | |
772 | ||
773 | static int bcap_enum_fmt_vid_cap(struct file *file, void *priv, | |
774 | struct v4l2_fmtdesc *fmt) | |
775 | { | |
776 | struct bcap_device *bcap_dev = video_drvdata(file); | |
777 | struct bcap_format *sf = bcap_dev->sensor_formats; | |
778 | ||
779 | if (fmt->index >= bcap_dev->num_sensor_formats) | |
780 | return -EINVAL; | |
781 | ||
782 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
783 | strlcpy(fmt->description, | |
784 | sf[fmt->index].desc, | |
785 | sizeof(fmt->description)); | |
786 | fmt->pixelformat = sf[fmt->index].pixelformat; | |
787 | return 0; | |
788 | } | |
789 | ||
790 | static int bcap_try_fmt_vid_cap(struct file *file, void *priv, | |
791 | struct v4l2_format *fmt) | |
792 | { | |
793 | struct bcap_device *bcap_dev = video_drvdata(file); | |
794 | struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; | |
795 | ||
45b82596 | 796 | return bcap_try_format(bcap_dev, pixfmt, NULL); |
63b1a90d SJ |
797 | } |
798 | ||
799 | static int bcap_g_fmt_vid_cap(struct file *file, void *priv, | |
800 | struct v4l2_format *fmt) | |
801 | { | |
802 | struct bcap_device *bcap_dev = video_drvdata(file); | |
803 | ||
804 | fmt->fmt.pix = bcap_dev->fmt; | |
805 | return 0; | |
806 | } | |
807 | ||
808 | static int bcap_s_fmt_vid_cap(struct file *file, void *priv, | |
809 | struct v4l2_format *fmt) | |
810 | { | |
811 | struct bcap_device *bcap_dev = video_drvdata(file); | |
812 | struct v4l2_mbus_framefmt mbus_fmt; | |
45b82596 | 813 | struct bcap_format bcap_fmt; |
63b1a90d | 814 | struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; |
45b82596 | 815 | int ret; |
63b1a90d SJ |
816 | |
817 | if (vb2_is_busy(&bcap_dev->buffer_queue)) | |
818 | return -EBUSY; | |
819 | ||
820 | /* see if format works */ | |
45b82596 | 821 | ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt); |
63b1a90d SJ |
822 | if (ret < 0) |
823 | return ret; | |
824 | ||
45b82596 | 825 | v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code); |
63b1a90d SJ |
826 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt); |
827 | if (ret < 0) | |
828 | return ret; | |
829 | bcap_dev->fmt = *pixfmt; | |
45b82596 SJ |
830 | bcap_dev->bpp = bcap_fmt.bpp; |
831 | bcap_dev->dlen = bcap_fmt.dlen; | |
63b1a90d SJ |
832 | return 0; |
833 | } | |
834 | ||
835 | static int bcap_querycap(struct file *file, void *priv, | |
836 | struct v4l2_capability *cap) | |
837 | { | |
838 | struct bcap_device *bcap_dev = video_drvdata(file); | |
839 | ||
840 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; | |
841 | strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver)); | |
842 | strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info)); | |
843 | strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card)); | |
844 | return 0; | |
845 | } | |
846 | ||
847 | static int bcap_g_parm(struct file *file, void *fh, | |
848 | struct v4l2_streamparm *a) | |
849 | { | |
850 | struct bcap_device *bcap_dev = video_drvdata(file); | |
851 | ||
852 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
853 | return -EINVAL; | |
854 | return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a); | |
855 | } | |
856 | ||
857 | static int bcap_s_parm(struct file *file, void *fh, | |
858 | struct v4l2_streamparm *a) | |
859 | { | |
860 | struct bcap_device *bcap_dev = video_drvdata(file); | |
861 | ||
862 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
863 | return -EINVAL; | |
864 | return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a); | |
865 | } | |
866 | ||
867 | static int bcap_g_chip_ident(struct file *file, void *priv, | |
868 | struct v4l2_dbg_chip_ident *chip) | |
869 | { | |
870 | struct bcap_device *bcap_dev = video_drvdata(file); | |
871 | ||
872 | chip->ident = V4L2_IDENT_NONE; | |
873 | chip->revision = 0; | |
874 | if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER && | |
875 | chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) | |
876 | return -EINVAL; | |
877 | ||
878 | return v4l2_subdev_call(bcap_dev->sd, core, | |
879 | g_chip_ident, chip); | |
880 | } | |
881 | ||
882 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
883 | static int bcap_dbg_g_register(struct file *file, void *priv, | |
884 | struct v4l2_dbg_register *reg) | |
885 | { | |
886 | struct bcap_device *bcap_dev = video_drvdata(file); | |
887 | ||
888 | return v4l2_subdev_call(bcap_dev->sd, core, | |
889 | g_register, reg); | |
890 | } | |
891 | ||
892 | static int bcap_dbg_s_register(struct file *file, void *priv, | |
977ba3b1 | 893 | const struct v4l2_dbg_register *reg) |
63b1a90d SJ |
894 | { |
895 | struct bcap_device *bcap_dev = video_drvdata(file); | |
896 | ||
897 | return v4l2_subdev_call(bcap_dev->sd, core, | |
898 | s_register, reg); | |
899 | } | |
900 | #endif | |
901 | ||
902 | static int bcap_log_status(struct file *file, void *priv) | |
903 | { | |
904 | struct bcap_device *bcap_dev = video_drvdata(file); | |
905 | /* status for sub devices */ | |
906 | v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status); | |
907 | return 0; | |
908 | } | |
909 | ||
910 | static const struct v4l2_ioctl_ops bcap_ioctl_ops = { | |
911 | .vidioc_querycap = bcap_querycap, | |
912 | .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap, | |
913 | .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap, | |
914 | .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap, | |
915 | .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap, | |
916 | .vidioc_enum_input = bcap_enum_input, | |
917 | .vidioc_g_input = bcap_g_input, | |
918 | .vidioc_s_input = bcap_s_input, | |
919 | .vidioc_querystd = bcap_querystd, | |
920 | .vidioc_s_std = bcap_s_std, | |
921 | .vidioc_g_std = bcap_g_std, | |
45b82596 SJ |
922 | .vidioc_s_dv_timings = bcap_s_dv_timings, |
923 | .vidioc_g_dv_timings = bcap_g_dv_timings, | |
63b1a90d SJ |
924 | .vidioc_reqbufs = bcap_reqbufs, |
925 | .vidioc_querybuf = bcap_querybuf, | |
926 | .vidioc_qbuf = bcap_qbuf, | |
927 | .vidioc_dqbuf = bcap_dqbuf, | |
928 | .vidioc_streamon = bcap_streamon, | |
929 | .vidioc_streamoff = bcap_streamoff, | |
930 | .vidioc_g_parm = bcap_g_parm, | |
931 | .vidioc_s_parm = bcap_s_parm, | |
932 | .vidioc_g_chip_ident = bcap_g_chip_ident, | |
933 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
934 | .vidioc_g_register = bcap_dbg_g_register, | |
935 | .vidioc_s_register = bcap_dbg_s_register, | |
936 | #endif | |
937 | .vidioc_log_status = bcap_log_status, | |
938 | }; | |
939 | ||
940 | static struct v4l2_file_operations bcap_fops = { | |
941 | .owner = THIS_MODULE, | |
942 | .open = bcap_open, | |
943 | .release = bcap_release, | |
944 | .unlocked_ioctl = video_ioctl2, | |
945 | .mmap = bcap_mmap, | |
946 | #ifndef CONFIG_MMU | |
947 | .get_unmapped_area = bcap_get_unmapped_area, | |
948 | #endif | |
949 | .poll = bcap_poll | |
950 | }; | |
951 | ||
4c62e976 | 952 | static int bcap_probe(struct platform_device *pdev) |
63b1a90d SJ |
953 | { |
954 | struct bcap_device *bcap_dev; | |
955 | struct video_device *vfd; | |
956 | struct i2c_adapter *i2c_adap; | |
957 | struct bfin_capture_config *config; | |
958 | struct vb2_queue *q; | |
45b82596 | 959 | struct bcap_route *route; |
63b1a90d SJ |
960 | int ret; |
961 | ||
962 | config = pdev->dev.platform_data; | |
963 | if (!config) { | |
964 | v4l2_err(pdev->dev.driver, "Unable to get board config\n"); | |
965 | return -ENODEV; | |
966 | } | |
967 | ||
968 | bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL); | |
969 | if (!bcap_dev) { | |
970 | v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n"); | |
971 | return -ENOMEM; | |
972 | } | |
973 | ||
974 | bcap_dev->cfg = config; | |
975 | ||
976 | bcap_dev->ppi = ppi_create_instance(config->ppi_info); | |
977 | if (!bcap_dev->ppi) { | |
978 | v4l2_err(pdev->dev.driver, "Unable to create ppi\n"); | |
979 | ret = -ENODEV; | |
980 | goto err_free_dev; | |
981 | } | |
982 | bcap_dev->ppi->priv = bcap_dev; | |
983 | ||
984 | bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); | |
985 | if (IS_ERR(bcap_dev->alloc_ctx)) { | |
986 | ret = PTR_ERR(bcap_dev->alloc_ctx); | |
987 | goto err_free_ppi; | |
988 | } | |
989 | ||
990 | vfd = video_device_alloc(); | |
991 | if (!vfd) { | |
992 | ret = -ENOMEM; | |
993 | v4l2_err(pdev->dev.driver, "Unable to alloc video device\n"); | |
994 | goto err_cleanup_ctx; | |
995 | } | |
996 | ||
997 | /* initialize field of video device */ | |
998 | vfd->release = video_device_release; | |
999 | vfd->fops = &bcap_fops; | |
1000 | vfd->ioctl_ops = &bcap_ioctl_ops; | |
1001 | vfd->tvnorms = 0; | |
1002 | vfd->v4l2_dev = &bcap_dev->v4l2_dev; | |
1003 | set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags); | |
1004 | strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name)); | |
1005 | bcap_dev->video_dev = vfd; | |
1006 | ||
1007 | ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev); | |
1008 | if (ret) { | |
1009 | v4l2_err(pdev->dev.driver, | |
1010 | "Unable to register v4l2 device\n"); | |
1011 | goto err_release_vdev; | |
1012 | } | |
1013 | v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n"); | |
1014 | ||
1015 | bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler; | |
1016 | ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0); | |
1017 | if (ret) { | |
1018 | v4l2_err(&bcap_dev->v4l2_dev, | |
1019 | "Unable to init control handler\n"); | |
1020 | goto err_unreg_v4l2; | |
1021 | } | |
1022 | ||
1023 | spin_lock_init(&bcap_dev->lock); | |
1024 | /* initialize queue */ | |
1025 | q = &bcap_dev->buffer_queue; | |
1026 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1027 | q->io_modes = VB2_MMAP; | |
1028 | q->drv_priv = bcap_dev; | |
1029 | q->buf_struct_size = sizeof(struct bcap_buffer); | |
1030 | q->ops = &bcap_video_qops; | |
1031 | q->mem_ops = &vb2_dma_contig_memops; | |
6aa69f99 | 1032 | q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
63b1a90d SJ |
1033 | |
1034 | vb2_queue_init(q); | |
1035 | ||
1036 | mutex_init(&bcap_dev->mutex); | |
1037 | init_completion(&bcap_dev->comp); | |
1038 | ||
1039 | /* init video dma queues */ | |
1040 | INIT_LIST_HEAD(&bcap_dev->dma_queue); | |
1041 | ||
1042 | vfd->lock = &bcap_dev->mutex; | |
1043 | ||
1044 | /* register video device */ | |
1045 | ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1); | |
1046 | if (ret) { | |
1047 | v4l2_err(&bcap_dev->v4l2_dev, | |
1048 | "Unable to register video device\n"); | |
1049 | goto err_free_handler; | |
1050 | } | |
1051 | video_set_drvdata(bcap_dev->video_dev, bcap_dev); | |
1052 | v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n", | |
1053 | video_device_node_name(vfd)); | |
1054 | ||
1055 | /* load up the subdevice */ | |
1056 | i2c_adap = i2c_get_adapter(config->i2c_adapter_id); | |
1057 | if (!i2c_adap) { | |
1058 | v4l2_err(&bcap_dev->v4l2_dev, | |
1059 | "Unable to find i2c adapter\n"); | |
150a1363 | 1060 | ret = -ENODEV; |
63b1a90d SJ |
1061 | goto err_unreg_vdev; |
1062 | ||
1063 | } | |
1064 | bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev, | |
1065 | i2c_adap, | |
1066 | &config->board_info, | |
1067 | NULL); | |
1068 | if (bcap_dev->sd) { | |
1069 | int i; | |
45b82596 SJ |
1070 | if (!config->num_inputs) { |
1071 | v4l2_err(&bcap_dev->v4l2_dev, | |
1072 | "Unable to work without input\n"); | |
1073 | goto err_unreg_vdev; | |
1074 | } | |
1075 | ||
63b1a90d SJ |
1076 | /* update tvnorms from the sub devices */ |
1077 | for (i = 0; i < config->num_inputs; i++) | |
1078 | vfd->tvnorms |= config->inputs[i].std; | |
1079 | } else { | |
1080 | v4l2_err(&bcap_dev->v4l2_dev, | |
1081 | "Unable to register sub device\n"); | |
1082 | goto err_unreg_vdev; | |
1083 | } | |
1084 | ||
1085 | v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n"); | |
1086 | ||
45b82596 SJ |
1087 | /* |
1088 | * explicitly set input, otherwise some boards | |
1089 | * may not work at the state as we expected | |
1090 | */ | |
1091 | route = &config->routes[0]; | |
1092 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing, | |
1093 | route->input, route->output, 0); | |
1094 | if ((ret < 0) && (ret != -ENOIOCTLCMD)) { | |
1095 | v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n"); | |
1096 | goto err_unreg_vdev; | |
1097 | } | |
1098 | bcap_dev->cur_input = 0; | |
1099 | /* if this route has specific config, update ppi control */ | |
1100 | if (route->ppi_control) | |
1101 | config->ppi_control = route->ppi_control; | |
1102 | ||
63b1a90d | 1103 | /* now we can probe the default state */ |
45b82596 | 1104 | if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) { |
63b1a90d SJ |
1105 | v4l2_std_id std; |
1106 | ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std); | |
1107 | if (ret) { | |
1108 | v4l2_err(&bcap_dev->v4l2_dev, | |
1109 | "Unable to get std\n"); | |
1110 | goto err_unreg_vdev; | |
1111 | } | |
1112 | bcap_dev->std = std; | |
1113 | } | |
a8451ed2 | 1114 | if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) { |
45b82596 SJ |
1115 | struct v4l2_dv_timings dv_timings; |
1116 | ret = v4l2_subdev_call(bcap_dev->sd, video, | |
1117 | g_dv_timings, &dv_timings); | |
1118 | if (ret) { | |
1119 | v4l2_err(&bcap_dev->v4l2_dev, | |
1120 | "Unable to get dv timings\n"); | |
1121 | goto err_unreg_vdev; | |
1122 | } | |
1123 | bcap_dev->dv_timings = dv_timings; | |
1124 | } | |
63b1a90d SJ |
1125 | ret = bcap_init_sensor_formats(bcap_dev); |
1126 | if (ret) { | |
1127 | v4l2_err(&bcap_dev->v4l2_dev, | |
1128 | "Unable to create sensor formats table\n"); | |
1129 | goto err_unreg_vdev; | |
1130 | } | |
1131 | return 0; | |
1132 | err_unreg_vdev: | |
1133 | video_unregister_device(bcap_dev->video_dev); | |
1134 | bcap_dev->video_dev = NULL; | |
1135 | err_free_handler: | |
1136 | v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler); | |
1137 | err_unreg_v4l2: | |
1138 | v4l2_device_unregister(&bcap_dev->v4l2_dev); | |
1139 | err_release_vdev: | |
1140 | if (bcap_dev->video_dev) | |
1141 | video_device_release(bcap_dev->video_dev); | |
1142 | err_cleanup_ctx: | |
1143 | vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx); | |
1144 | err_free_ppi: | |
1145 | ppi_delete_instance(bcap_dev->ppi); | |
1146 | err_free_dev: | |
1147 | kfree(bcap_dev); | |
1148 | return ret; | |
1149 | } | |
1150 | ||
4c62e976 | 1151 | static int bcap_remove(struct platform_device *pdev) |
63b1a90d SJ |
1152 | { |
1153 | struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev); | |
1154 | struct bcap_device *bcap_dev = container_of(v4l2_dev, | |
1155 | struct bcap_device, v4l2_dev); | |
1156 | ||
1157 | bcap_free_sensor_formats(bcap_dev); | |
1158 | video_unregister_device(bcap_dev->video_dev); | |
1159 | v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler); | |
1160 | v4l2_device_unregister(v4l2_dev); | |
1161 | vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx); | |
1162 | ppi_delete_instance(bcap_dev->ppi); | |
1163 | kfree(bcap_dev); | |
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | static struct platform_driver bcap_driver = { | |
1168 | .driver = { | |
1169 | .name = CAPTURE_DRV_NAME, | |
1170 | .owner = THIS_MODULE, | |
1171 | }, | |
1172 | .probe = bcap_probe, | |
4c62e976 | 1173 | .remove = bcap_remove, |
63b1a90d | 1174 | }; |
7f2b3a7d | 1175 | module_platform_driver(bcap_driver); |
63b1a90d SJ |
1176 | |
1177 | MODULE_DESCRIPTION("Analog Devices blackfin video capture driver"); | |
1178 | MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>"); | |
1179 | MODULE_LICENSE("GPL v2"); |