Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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 | |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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; |
| 52 | enum v4l2_mbus_pixelcode mbus_code; |
| 53 | int bpp; /* bits per pixel */ |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 54 | int dlen; /* data length for ppi in bits */ |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 79 | /* current selected dv_timings */ |
| 80 | struct v4l2_dv_timings dv_timings; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 81 | /* used to store pixel format */ |
| 82 | struct v4l2_pix_format fmt; |
| 83 | /* bits per pixel*/ |
| 84 | int bpp; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 85 | /* data length for ppi in bits */ |
| 86 | int dlen; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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, |
| 119 | .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8, |
| 120 | .bpp = 16, |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 121 | .dlen = 8, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 122 | }, |
| 123 | { |
| 124 | .desc = "YCbCr 4:2:2 Interleaved YUYV", |
| 125 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 126 | .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8, |
| 127 | .bpp = 16, |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 128 | .dlen = 8, |
| 129 | }, |
| 130 | { |
| 131 | .desc = "YCbCr 4:2:2 Interleaved UYVY", |
| 132 | .pixelformat = V4L2_PIX_FMT_UYVY, |
| 133 | .mbus_code = V4L2_MBUS_FMT_UYVY8_1X16, |
| 134 | .bpp = 16, |
| 135 | .dlen = 16, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 136 | }, |
| 137 | { |
| 138 | .desc = "RGB 565", |
| 139 | .pixelformat = V4L2_PIX_FMT_RGB565, |
| 140 | .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE, |
| 141 | .bpp = 16, |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 142 | .dlen = 8, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 143 | }, |
| 144 | { |
| 145 | .desc = "RGB 444", |
| 146 | .pixelformat = V4L2_PIX_FMT_RGB444, |
| 147 | .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE, |
| 148 | .bpp = 16, |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 149 | .dlen = 8, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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 | { |
| 164 | enum v4l2_mbus_pixelcode code; |
| 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); |
Hans Verkuil | 28c3682 | 2012-06-24 06:36:49 -0300 | [diff] [blame] | 251 | int ret; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 252 | |
Hans Verkuil | 28c3682 | 2012-06-24 06:36:49 -0300 | [diff] [blame] | 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; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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); |
Hans Verkuil | 28c3682 | 2012-06-24 06:36:49 -0300 | [diff] [blame] | 280 | unsigned int res; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 281 | |
Hans Verkuil | 28c3682 | 2012-06-24 06:36:49 -0300 | [diff] [blame] | 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; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 382 | params.dlen = bcap_dev->dlen; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 383 | params.ppi_control = bcap_dev->cfg->ppi_control; |
| 384 | params.int_mask = bcap_dev->cfg->int_mask; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 385 | if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities |
Hans Verkuil | a8451ed | 2013-02-15 15:12:01 -0300 | [diff] [blame] | 386 | & V4L2_IN_CAP_DV_TIMINGS) { |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 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; |
Hans Verkuil | eacf8f9 | 2013-07-29 08:40:59 -0300 | [diff] [blame] | 391 | params.line = V4L2_DV_BT_FRAME_WIDTH(bt); |
| 392 | params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt); |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 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 | } |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 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 | |
| 425 | INIT_COMPLETION(bcap_dev->comp); |
| 426 | bcap_dev->stop = false; |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int bcap_stop_streaming(struct vb2_queue *vq) |
| 431 | { |
| 432 | struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); |
| 433 | struct ppi_if *ppi = bcap_dev->ppi; |
| 434 | int ret; |
| 435 | |
| 436 | if (!vb2_is_streaming(vq)) |
| 437 | return 0; |
| 438 | |
| 439 | bcap_dev->stop = true; |
| 440 | wait_for_completion(&bcap_dev->comp); |
| 441 | ppi->ops->stop(ppi); |
| 442 | ppi->ops->detach_irq(ppi); |
| 443 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0); |
| 444 | if (ret && (ret != -ENOIOCTLCMD)) |
| 445 | v4l2_err(&bcap_dev->v4l2_dev, |
| 446 | "stream off failed in subdev\n"); |
| 447 | |
| 448 | /* release all active buffers */ |
| 449 | while (!list_empty(&bcap_dev->dma_queue)) { |
Scott Jiang | d78a488 | 2013-01-18 17:09:48 -0300 | [diff] [blame] | 450 | bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 451 | struct bcap_buffer, list); |
Scott Jiang | d78a488 | 2013-01-18 17:09:48 -0300 | [diff] [blame] | 452 | list_del(&bcap_dev->cur_frm->list); |
| 453 | vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static struct vb2_ops bcap_video_qops = { |
| 459 | .queue_setup = bcap_queue_setup, |
| 460 | .buf_init = bcap_buffer_init, |
| 461 | .buf_prepare = bcap_buffer_prepare, |
| 462 | .buf_cleanup = bcap_buffer_cleanup, |
| 463 | .buf_queue = bcap_buffer_queue, |
| 464 | .wait_prepare = bcap_unlock, |
| 465 | .wait_finish = bcap_lock, |
| 466 | .start_streaming = bcap_start_streaming, |
| 467 | .stop_streaming = bcap_stop_streaming, |
| 468 | }; |
| 469 | |
| 470 | static int bcap_reqbufs(struct file *file, void *priv, |
| 471 | struct v4l2_requestbuffers *req_buf) |
| 472 | { |
| 473 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 474 | struct vb2_queue *vq = &bcap_dev->buffer_queue; |
| 475 | struct v4l2_fh *fh = file->private_data; |
| 476 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); |
| 477 | |
| 478 | if (vb2_is_busy(vq)) |
| 479 | return -EBUSY; |
| 480 | |
| 481 | bcap_fh->io_allowed = true; |
| 482 | |
| 483 | return vb2_reqbufs(vq, req_buf); |
| 484 | } |
| 485 | |
| 486 | static int bcap_querybuf(struct file *file, void *priv, |
| 487 | struct v4l2_buffer *buf) |
| 488 | { |
| 489 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 490 | |
| 491 | return vb2_querybuf(&bcap_dev->buffer_queue, buf); |
| 492 | } |
| 493 | |
| 494 | static int bcap_qbuf(struct file *file, void *priv, |
| 495 | struct v4l2_buffer *buf) |
| 496 | { |
| 497 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 498 | struct v4l2_fh *fh = file->private_data; |
| 499 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); |
| 500 | |
| 501 | if (!bcap_fh->io_allowed) |
| 502 | return -EBUSY; |
| 503 | |
| 504 | return vb2_qbuf(&bcap_dev->buffer_queue, buf); |
| 505 | } |
| 506 | |
| 507 | static int bcap_dqbuf(struct file *file, void *priv, |
| 508 | struct v4l2_buffer *buf) |
| 509 | { |
| 510 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 511 | struct v4l2_fh *fh = file->private_data; |
| 512 | struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh); |
| 513 | |
| 514 | if (!bcap_fh->io_allowed) |
| 515 | return -EBUSY; |
| 516 | |
| 517 | return vb2_dqbuf(&bcap_dev->buffer_queue, |
| 518 | buf, file->f_flags & O_NONBLOCK); |
| 519 | } |
| 520 | |
| 521 | static irqreturn_t bcap_isr(int irq, void *dev_id) |
| 522 | { |
| 523 | struct ppi_if *ppi = dev_id; |
| 524 | struct bcap_device *bcap_dev = ppi->priv; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 525 | struct vb2_buffer *vb = &bcap_dev->cur_frm->vb; |
| 526 | dma_addr_t addr; |
| 527 | |
| 528 | spin_lock(&bcap_dev->lock); |
| 529 | |
Scott Jiang | d78a488 | 2013-01-18 17:09:48 -0300 | [diff] [blame] | 530 | if (!list_empty(&bcap_dev->dma_queue)) { |
Sakari Ailus | 8e6057b | 2012-09-15 15:14:42 -0300 | [diff] [blame] | 531 | v4l2_get_timestamp(&vb->v4l2_buf.timestamp); |
Scott Jiang | d78a488 | 2013-01-18 17:09:48 -0300 | [diff] [blame] | 532 | if (ppi->err) { |
| 533 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); |
| 534 | ppi->err = false; |
| 535 | } else { |
| 536 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
| 537 | } |
| 538 | bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next, |
| 539 | struct bcap_buffer, list); |
| 540 | list_del(&bcap_dev->cur_frm->list); |
| 541 | } else { |
| 542 | /* clear error flag, we will get a new frame */ |
| 543 | if (ppi->err) |
| 544 | ppi->err = false; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | ppi->ops->stop(ppi); |
| 548 | |
| 549 | if (bcap_dev->stop) { |
| 550 | complete(&bcap_dev->comp); |
| 551 | } else { |
Scott Jiang | d78a488 | 2013-01-18 17:09:48 -0300 | [diff] [blame] | 552 | addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0); |
| 553 | ppi->ops->update_addr(ppi, (unsigned long)addr); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 554 | ppi->ops->start(ppi); |
| 555 | } |
| 556 | |
| 557 | spin_unlock(&bcap_dev->lock); |
| 558 | |
| 559 | return IRQ_HANDLED; |
| 560 | } |
| 561 | |
| 562 | static int bcap_streamon(struct file *file, void *priv, |
| 563 | enum v4l2_buf_type buf_type) |
| 564 | { |
| 565 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 566 | struct bcap_fh *fh = file->private_data; |
| 567 | struct ppi_if *ppi = bcap_dev->ppi; |
| 568 | dma_addr_t addr; |
| 569 | int ret; |
| 570 | |
| 571 | if (!fh->io_allowed) |
| 572 | return -EBUSY; |
| 573 | |
| 574 | /* call streamon to start streaming in videobuf */ |
| 575 | ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type); |
| 576 | if (ret) |
| 577 | return ret; |
| 578 | |
| 579 | /* if dma queue is empty, return error */ |
| 580 | if (list_empty(&bcap_dev->dma_queue)) { |
| 581 | v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n"); |
| 582 | ret = -EINVAL; |
| 583 | goto err; |
| 584 | } |
| 585 | |
| 586 | /* get the next frame from the dma queue */ |
Scott Jiang | d78a488 | 2013-01-18 17:09:48 -0300 | [diff] [blame] | 587 | bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 588 | struct bcap_buffer, list); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 589 | /* remove buffer from the dma queue */ |
| 590 | list_del(&bcap_dev->cur_frm->list); |
| 591 | addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0); |
| 592 | /* update DMA address */ |
| 593 | ppi->ops->update_addr(ppi, (unsigned long)addr); |
| 594 | /* enable ppi */ |
| 595 | ppi->ops->start(ppi); |
| 596 | |
| 597 | return 0; |
| 598 | err: |
| 599 | vb2_streamoff(&bcap_dev->buffer_queue, buf_type); |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | static int bcap_streamoff(struct file *file, void *priv, |
| 604 | enum v4l2_buf_type buf_type) |
| 605 | { |
| 606 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 607 | struct bcap_fh *fh = file->private_data; |
| 608 | |
| 609 | if (!fh->io_allowed) |
| 610 | return -EBUSY; |
| 611 | |
| 612 | return vb2_streamoff(&bcap_dev->buffer_queue, buf_type); |
| 613 | } |
| 614 | |
| 615 | static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std) |
| 616 | { |
| 617 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 618 | |
| 619 | return v4l2_subdev_call(bcap_dev->sd, video, querystd, std); |
| 620 | } |
| 621 | |
| 622 | static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std) |
| 623 | { |
| 624 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 625 | |
| 626 | *std = bcap_dev->std; |
| 627 | return 0; |
| 628 | } |
| 629 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 630 | static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std) |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 631 | { |
| 632 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 633 | int ret; |
| 634 | |
| 635 | if (vb2_is_busy(&bcap_dev->buffer_queue)) |
| 636 | return -EBUSY; |
| 637 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 638 | ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, std); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 639 | if (ret < 0) |
| 640 | return ret; |
| 641 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 642 | bcap_dev->std = std; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 643 | return 0; |
| 644 | } |
| 645 | |
Scott Jiang | ead156c | 2013-04-12 19:52:59 -0300 | [diff] [blame] | 646 | static int bcap_enum_dv_timings(struct file *file, void *priv, |
| 647 | struct v4l2_enum_dv_timings *timings) |
| 648 | { |
| 649 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 650 | |
| 651 | return v4l2_subdev_call(bcap_dev->sd, video, |
| 652 | enum_dv_timings, timings); |
| 653 | } |
| 654 | |
| 655 | static int bcap_query_dv_timings(struct file *file, void *priv, |
| 656 | struct v4l2_dv_timings *timings) |
| 657 | { |
| 658 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 659 | |
| 660 | return v4l2_subdev_call(bcap_dev->sd, video, |
| 661 | query_dv_timings, timings); |
| 662 | } |
| 663 | |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 664 | static int bcap_g_dv_timings(struct file *file, void *priv, |
| 665 | struct v4l2_dv_timings *timings) |
| 666 | { |
| 667 | struct bcap_device *bcap_dev = video_drvdata(file); |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 668 | |
Scott Jiang | ead156c | 2013-04-12 19:52:59 -0300 | [diff] [blame] | 669 | *timings = bcap_dev->dv_timings; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int bcap_s_dv_timings(struct file *file, void *priv, |
| 674 | struct v4l2_dv_timings *timings) |
| 675 | { |
| 676 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 677 | int ret; |
| 678 | if (vb2_is_busy(&bcap_dev->buffer_queue)) |
| 679 | return -EBUSY; |
| 680 | |
| 681 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings); |
| 682 | if (ret < 0) |
| 683 | return ret; |
| 684 | |
| 685 | bcap_dev->dv_timings = *timings; |
| 686 | return 0; |
| 687 | } |
| 688 | |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 689 | static int bcap_enum_input(struct file *file, void *priv, |
| 690 | struct v4l2_input *input) |
| 691 | { |
| 692 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 693 | struct bfin_capture_config *config = bcap_dev->cfg; |
| 694 | int ret; |
| 695 | u32 status; |
| 696 | |
| 697 | if (input->index >= config->num_inputs) |
| 698 | return -EINVAL; |
| 699 | |
| 700 | *input = config->inputs[input->index]; |
| 701 | /* get input status */ |
| 702 | ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status); |
| 703 | if (!ret) |
| 704 | input->status = status; |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static int bcap_g_input(struct file *file, void *priv, unsigned int *index) |
| 709 | { |
| 710 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 711 | |
| 712 | *index = bcap_dev->cur_input; |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int bcap_s_input(struct file *file, void *priv, unsigned int index) |
| 717 | { |
| 718 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 719 | struct bfin_capture_config *config = bcap_dev->cfg; |
| 720 | struct bcap_route *route; |
| 721 | int ret; |
| 722 | |
| 723 | if (vb2_is_busy(&bcap_dev->buffer_queue)) |
| 724 | return -EBUSY; |
| 725 | |
| 726 | if (index >= config->num_inputs) |
| 727 | return -EINVAL; |
| 728 | |
| 729 | route = &config->routes[index]; |
| 730 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing, |
| 731 | route->input, route->output, 0); |
| 732 | if ((ret < 0) && (ret != -ENOIOCTLCMD)) { |
| 733 | v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n"); |
| 734 | return ret; |
| 735 | } |
| 736 | bcap_dev->cur_input = index; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 737 | /* if this route has specific config, update ppi control */ |
| 738 | if (route->ppi_control) |
| 739 | config->ppi_control = route->ppi_control; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int bcap_try_format(struct bcap_device *bcap, |
| 744 | struct v4l2_pix_format *pixfmt, |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 745 | struct bcap_format *bcap_fmt) |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 746 | { |
| 747 | struct bcap_format *sf = bcap->sensor_formats; |
| 748 | struct bcap_format *fmt = NULL; |
| 749 | struct v4l2_mbus_framefmt mbus_fmt; |
| 750 | int ret, i; |
| 751 | |
| 752 | for (i = 0; i < bcap->num_sensor_formats; i++) { |
| 753 | fmt = &sf[i]; |
| 754 | if (pixfmt->pixelformat == fmt->pixelformat) |
| 755 | break; |
| 756 | } |
| 757 | if (i == bcap->num_sensor_formats) |
| 758 | fmt = &sf[0]; |
| 759 | |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 760 | v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code); |
| 761 | ret = v4l2_subdev_call(bcap->sd, video, |
| 762 | try_mbus_fmt, &mbus_fmt); |
| 763 | if (ret < 0) |
| 764 | return ret; |
| 765 | v4l2_fill_pix_format(pixfmt, &mbus_fmt); |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 766 | if (bcap_fmt) { |
| 767 | for (i = 0; i < bcap->num_sensor_formats; i++) { |
| 768 | fmt = &sf[i]; |
| 769 | if (mbus_fmt.code == fmt->mbus_code) |
| 770 | break; |
| 771 | } |
| 772 | *bcap_fmt = *fmt; |
| 773 | } |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 774 | pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8; |
| 775 | pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height; |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static int bcap_enum_fmt_vid_cap(struct file *file, void *priv, |
| 780 | struct v4l2_fmtdesc *fmt) |
| 781 | { |
| 782 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 783 | struct bcap_format *sf = bcap_dev->sensor_formats; |
| 784 | |
| 785 | if (fmt->index >= bcap_dev->num_sensor_formats) |
| 786 | return -EINVAL; |
| 787 | |
| 788 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 789 | strlcpy(fmt->description, |
| 790 | sf[fmt->index].desc, |
| 791 | sizeof(fmt->description)); |
| 792 | fmt->pixelformat = sf[fmt->index].pixelformat; |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static int bcap_try_fmt_vid_cap(struct file *file, void *priv, |
| 797 | struct v4l2_format *fmt) |
| 798 | { |
| 799 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 800 | struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; |
| 801 | |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 802 | return bcap_try_format(bcap_dev, pixfmt, NULL); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static int bcap_g_fmt_vid_cap(struct file *file, void *priv, |
| 806 | struct v4l2_format *fmt) |
| 807 | { |
| 808 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 809 | |
| 810 | fmt->fmt.pix = bcap_dev->fmt; |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static int bcap_s_fmt_vid_cap(struct file *file, void *priv, |
| 815 | struct v4l2_format *fmt) |
| 816 | { |
| 817 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 818 | struct v4l2_mbus_framefmt mbus_fmt; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 819 | struct bcap_format bcap_fmt; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 820 | struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 821 | int ret; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 822 | |
| 823 | if (vb2_is_busy(&bcap_dev->buffer_queue)) |
| 824 | return -EBUSY; |
| 825 | |
| 826 | /* see if format works */ |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 827 | ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 831 | v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 832 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt); |
| 833 | if (ret < 0) |
| 834 | return ret; |
| 835 | bcap_dev->fmt = *pixfmt; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 836 | bcap_dev->bpp = bcap_fmt.bpp; |
| 837 | bcap_dev->dlen = bcap_fmt.dlen; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static int bcap_querycap(struct file *file, void *priv, |
| 842 | struct v4l2_capability *cap) |
| 843 | { |
| 844 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 845 | |
| 846 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 847 | strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver)); |
| 848 | strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info)); |
| 849 | strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card)); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | static int bcap_g_parm(struct file *file, void *fh, |
| 854 | struct v4l2_streamparm *a) |
| 855 | { |
| 856 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 857 | |
| 858 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 859 | return -EINVAL; |
| 860 | return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a); |
| 861 | } |
| 862 | |
| 863 | static int bcap_s_parm(struct file *file, void *fh, |
| 864 | struct v4l2_streamparm *a) |
| 865 | { |
| 866 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 867 | |
| 868 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 869 | return -EINVAL; |
| 870 | return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a); |
| 871 | } |
| 872 | |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 873 | static int bcap_log_status(struct file *file, void *priv) |
| 874 | { |
| 875 | struct bcap_device *bcap_dev = video_drvdata(file); |
| 876 | /* status for sub devices */ |
| 877 | v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status); |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static const struct v4l2_ioctl_ops bcap_ioctl_ops = { |
| 882 | .vidioc_querycap = bcap_querycap, |
| 883 | .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap, |
| 884 | .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap, |
| 885 | .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap, |
| 886 | .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap, |
| 887 | .vidioc_enum_input = bcap_enum_input, |
| 888 | .vidioc_g_input = bcap_g_input, |
| 889 | .vidioc_s_input = bcap_s_input, |
| 890 | .vidioc_querystd = bcap_querystd, |
| 891 | .vidioc_s_std = bcap_s_std, |
| 892 | .vidioc_g_std = bcap_g_std, |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 893 | .vidioc_s_dv_timings = bcap_s_dv_timings, |
| 894 | .vidioc_g_dv_timings = bcap_g_dv_timings, |
Scott Jiang | ead156c | 2013-04-12 19:52:59 -0300 | [diff] [blame] | 895 | .vidioc_query_dv_timings = bcap_query_dv_timings, |
| 896 | .vidioc_enum_dv_timings = bcap_enum_dv_timings, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 897 | .vidioc_reqbufs = bcap_reqbufs, |
| 898 | .vidioc_querybuf = bcap_querybuf, |
| 899 | .vidioc_qbuf = bcap_qbuf, |
| 900 | .vidioc_dqbuf = bcap_dqbuf, |
| 901 | .vidioc_streamon = bcap_streamon, |
| 902 | .vidioc_streamoff = bcap_streamoff, |
| 903 | .vidioc_g_parm = bcap_g_parm, |
| 904 | .vidioc_s_parm = bcap_s_parm, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 905 | .vidioc_log_status = bcap_log_status, |
| 906 | }; |
| 907 | |
| 908 | static struct v4l2_file_operations bcap_fops = { |
| 909 | .owner = THIS_MODULE, |
| 910 | .open = bcap_open, |
| 911 | .release = bcap_release, |
| 912 | .unlocked_ioctl = video_ioctl2, |
| 913 | .mmap = bcap_mmap, |
| 914 | #ifndef CONFIG_MMU |
| 915 | .get_unmapped_area = bcap_get_unmapped_area, |
| 916 | #endif |
| 917 | .poll = bcap_poll |
| 918 | }; |
| 919 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 920 | static int bcap_probe(struct platform_device *pdev) |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 921 | { |
| 922 | struct bcap_device *bcap_dev; |
| 923 | struct video_device *vfd; |
| 924 | struct i2c_adapter *i2c_adap; |
| 925 | struct bfin_capture_config *config; |
| 926 | struct vb2_queue *q; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 927 | struct bcap_route *route; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 928 | int ret; |
| 929 | |
| 930 | config = pdev->dev.platform_data; |
Wei Yongjun | d9fdbef | 2013-05-13 04:52:16 -0300 | [diff] [blame] | 931 | if (!config || !config->num_inputs) { |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 932 | v4l2_err(pdev->dev.driver, "Unable to get board config\n"); |
| 933 | return -ENODEV; |
| 934 | } |
| 935 | |
| 936 | bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL); |
| 937 | if (!bcap_dev) { |
| 938 | v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n"); |
| 939 | return -ENOMEM; |
| 940 | } |
| 941 | |
| 942 | bcap_dev->cfg = config; |
| 943 | |
| 944 | bcap_dev->ppi = ppi_create_instance(config->ppi_info); |
| 945 | if (!bcap_dev->ppi) { |
| 946 | v4l2_err(pdev->dev.driver, "Unable to create ppi\n"); |
| 947 | ret = -ENODEV; |
| 948 | goto err_free_dev; |
| 949 | } |
| 950 | bcap_dev->ppi->priv = bcap_dev; |
| 951 | |
| 952 | bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 953 | if (IS_ERR(bcap_dev->alloc_ctx)) { |
| 954 | ret = PTR_ERR(bcap_dev->alloc_ctx); |
| 955 | goto err_free_ppi; |
| 956 | } |
| 957 | |
| 958 | vfd = video_device_alloc(); |
| 959 | if (!vfd) { |
| 960 | ret = -ENOMEM; |
| 961 | v4l2_err(pdev->dev.driver, "Unable to alloc video device\n"); |
| 962 | goto err_cleanup_ctx; |
| 963 | } |
| 964 | |
| 965 | /* initialize field of video device */ |
| 966 | vfd->release = video_device_release; |
| 967 | vfd->fops = &bcap_fops; |
| 968 | vfd->ioctl_ops = &bcap_ioctl_ops; |
| 969 | vfd->tvnorms = 0; |
| 970 | vfd->v4l2_dev = &bcap_dev->v4l2_dev; |
| 971 | set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags); |
| 972 | strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name)); |
| 973 | bcap_dev->video_dev = vfd; |
| 974 | |
| 975 | ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev); |
| 976 | if (ret) { |
| 977 | v4l2_err(pdev->dev.driver, |
| 978 | "Unable to register v4l2 device\n"); |
| 979 | goto err_release_vdev; |
| 980 | } |
| 981 | v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n"); |
| 982 | |
| 983 | bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler; |
| 984 | ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0); |
| 985 | if (ret) { |
| 986 | v4l2_err(&bcap_dev->v4l2_dev, |
| 987 | "Unable to init control handler\n"); |
| 988 | goto err_unreg_v4l2; |
| 989 | } |
| 990 | |
| 991 | spin_lock_init(&bcap_dev->lock); |
| 992 | /* initialize queue */ |
| 993 | q = &bcap_dev->buffer_queue; |
| 994 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 995 | q->io_modes = VB2_MMAP; |
| 996 | q->drv_priv = bcap_dev; |
| 997 | q->buf_struct_size = sizeof(struct bcap_buffer); |
| 998 | q->ops = &bcap_video_qops; |
| 999 | q->mem_ops = &vb2_dma_contig_memops; |
Kamil Debski | 6aa69f9 | 2013-01-25 06:29:57 -0300 | [diff] [blame] | 1000 | q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1001 | |
Hans Verkuil | 20420f9 | 2013-06-20 16:28:15 -0300 | [diff] [blame] | 1002 | ret = vb2_queue_init(q); |
| 1003 | if (ret) |
| 1004 | goto err_free_handler; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1005 | |
| 1006 | mutex_init(&bcap_dev->mutex); |
| 1007 | init_completion(&bcap_dev->comp); |
| 1008 | |
| 1009 | /* init video dma queues */ |
| 1010 | INIT_LIST_HEAD(&bcap_dev->dma_queue); |
| 1011 | |
| 1012 | vfd->lock = &bcap_dev->mutex; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1013 | |
| 1014 | /* register video device */ |
| 1015 | ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1); |
| 1016 | if (ret) { |
| 1017 | v4l2_err(&bcap_dev->v4l2_dev, |
| 1018 | "Unable to register video device\n"); |
| 1019 | goto err_free_handler; |
| 1020 | } |
| 1021 | video_set_drvdata(bcap_dev->video_dev, bcap_dev); |
| 1022 | v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n", |
| 1023 | video_device_node_name(vfd)); |
| 1024 | |
| 1025 | /* load up the subdevice */ |
| 1026 | i2c_adap = i2c_get_adapter(config->i2c_adapter_id); |
| 1027 | if (!i2c_adap) { |
| 1028 | v4l2_err(&bcap_dev->v4l2_dev, |
| 1029 | "Unable to find i2c adapter\n"); |
Peter Senna Tschudin | 150a136 | 2012-09-06 11:23:58 -0300 | [diff] [blame] | 1030 | ret = -ENODEV; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1031 | goto err_unreg_vdev; |
| 1032 | |
| 1033 | } |
| 1034 | bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev, |
| 1035 | i2c_adap, |
| 1036 | &config->board_info, |
| 1037 | NULL); |
| 1038 | if (bcap_dev->sd) { |
| 1039 | int i; |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 1040 | |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1041 | /* update tvnorms from the sub devices */ |
| 1042 | for (i = 0; i < config->num_inputs; i++) |
| 1043 | vfd->tvnorms |= config->inputs[i].std; |
| 1044 | } else { |
| 1045 | v4l2_err(&bcap_dev->v4l2_dev, |
| 1046 | "Unable to register sub device\n"); |
Wei Yongjun | d9fdbef | 2013-05-13 04:52:16 -0300 | [diff] [blame] | 1047 | ret = -ENODEV; |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1048 | goto err_unreg_vdev; |
| 1049 | } |
| 1050 | |
| 1051 | v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n"); |
| 1052 | |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 1053 | /* |
| 1054 | * explicitly set input, otherwise some boards |
| 1055 | * may not work at the state as we expected |
| 1056 | */ |
| 1057 | route = &config->routes[0]; |
| 1058 | ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing, |
| 1059 | route->input, route->output, 0); |
| 1060 | if ((ret < 0) && (ret != -ENOIOCTLCMD)) { |
| 1061 | v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n"); |
| 1062 | goto err_unreg_vdev; |
| 1063 | } |
| 1064 | bcap_dev->cur_input = 0; |
| 1065 | /* if this route has specific config, update ppi control */ |
| 1066 | if (route->ppi_control) |
| 1067 | config->ppi_control = route->ppi_control; |
| 1068 | |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1069 | /* now we can probe the default state */ |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 1070 | if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) { |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1071 | v4l2_std_id std; |
| 1072 | ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std); |
| 1073 | if (ret) { |
| 1074 | v4l2_err(&bcap_dev->v4l2_dev, |
| 1075 | "Unable to get std\n"); |
| 1076 | goto err_unreg_vdev; |
| 1077 | } |
| 1078 | bcap_dev->std = std; |
| 1079 | } |
Hans Verkuil | a8451ed | 2013-02-15 15:12:01 -0300 | [diff] [blame] | 1080 | if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) { |
Scott Jiang | 45b8259 | 2012-11-20 15:49:36 -0300 | [diff] [blame] | 1081 | struct v4l2_dv_timings dv_timings; |
| 1082 | ret = v4l2_subdev_call(bcap_dev->sd, video, |
| 1083 | g_dv_timings, &dv_timings); |
| 1084 | if (ret) { |
| 1085 | v4l2_err(&bcap_dev->v4l2_dev, |
| 1086 | "Unable to get dv timings\n"); |
| 1087 | goto err_unreg_vdev; |
| 1088 | } |
| 1089 | bcap_dev->dv_timings = dv_timings; |
| 1090 | } |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1091 | ret = bcap_init_sensor_formats(bcap_dev); |
| 1092 | if (ret) { |
| 1093 | v4l2_err(&bcap_dev->v4l2_dev, |
| 1094 | "Unable to create sensor formats table\n"); |
| 1095 | goto err_unreg_vdev; |
| 1096 | } |
| 1097 | return 0; |
| 1098 | err_unreg_vdev: |
| 1099 | video_unregister_device(bcap_dev->video_dev); |
| 1100 | bcap_dev->video_dev = NULL; |
| 1101 | err_free_handler: |
| 1102 | v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler); |
| 1103 | err_unreg_v4l2: |
| 1104 | v4l2_device_unregister(&bcap_dev->v4l2_dev); |
| 1105 | err_release_vdev: |
| 1106 | if (bcap_dev->video_dev) |
| 1107 | video_device_release(bcap_dev->video_dev); |
| 1108 | err_cleanup_ctx: |
| 1109 | vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx); |
| 1110 | err_free_ppi: |
| 1111 | ppi_delete_instance(bcap_dev->ppi); |
| 1112 | err_free_dev: |
| 1113 | kfree(bcap_dev); |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1117 | static int bcap_remove(struct platform_device *pdev) |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1118 | { |
| 1119 | struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev); |
| 1120 | struct bcap_device *bcap_dev = container_of(v4l2_dev, |
| 1121 | struct bcap_device, v4l2_dev); |
| 1122 | |
| 1123 | bcap_free_sensor_formats(bcap_dev); |
| 1124 | video_unregister_device(bcap_dev->video_dev); |
| 1125 | v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler); |
| 1126 | v4l2_device_unregister(v4l2_dev); |
| 1127 | vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx); |
| 1128 | ppi_delete_instance(bcap_dev->ppi); |
| 1129 | kfree(bcap_dev); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static struct platform_driver bcap_driver = { |
| 1134 | .driver = { |
| 1135 | .name = CAPTURE_DRV_NAME, |
| 1136 | .owner = THIS_MODULE, |
| 1137 | }, |
| 1138 | .probe = bcap_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1139 | .remove = bcap_remove, |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1140 | }; |
Srinivas Kandagatla | 7f2b3a7d | 2012-10-10 14:33:38 -0300 | [diff] [blame] | 1141 | module_platform_driver(bcap_driver); |
Scott Jiang | 63b1a90 | 2012-03-08 17:44:17 -0300 | [diff] [blame] | 1142 | |
| 1143 | MODULE_DESCRIPTION("Analog Devices blackfin video capture driver"); |
| 1144 | MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>"); |
| 1145 | MODULE_LICENSE("GPL v2"); |