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