1/* 2 * drivers/media/video/omap24xxcam.c 3 * 4 * OMAP 2 camera block driver. 5 * 6 * Copyright (C) 2004 MontaVista Software, Inc. 7 * Copyright (C) 2004 Texas Instruments. 8 * Copyright (C) 2007-2008 Nokia Corporation. 9 * 10 * Contact: Sakari Ailus <sakari.ailus@nokia.com> 11 * 12 * Based on code from Andy Lowe <source@mvista.com> 13 * 14 * This program is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU General Public License 16 * version 2 as published by the Free Software Foundation. 17 * 18 * This program is distributed in the hope that it will be useful, but 19 * WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 * General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 26 * 02110-1301 USA 27 */ 28 29#include <linux/delay.h> 30#include <linux/kernel.h> 31#include <linux/interrupt.h> 32#include <linux/videodev2.h> 33#include <linux/pci.h> /* needed for videobufs */ 34#include <linux/version.h> 35#include <linux/platform_device.h> 36#include <linux/clk.h> 37#include <linux/io.h> 38#include <linux/slab.h> 39 40#include <media/v4l2-common.h> 41#include <media/v4l2-ioctl.h> 42 43#include "omap24xxcam.h" 44 45#define OMAP24XXCAM_VERSION KERNEL_VERSION(0, 0, 0) 46 47#define RESET_TIMEOUT_NS 10000 48 49static void omap24xxcam_reset(struct omap24xxcam_device *cam); 50static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam); 51static void omap24xxcam_device_unregister(struct v4l2_int_device *s); 52static int omap24xxcam_remove(struct platform_device *pdev); 53 54/* module parameters */ 55static int video_nr = -1; /* video device minor (-1 ==> auto assign) */ 56/* 57 * Maximum amount of memory to use for capture buffers. 58 * Default is 4800KB, enough to double-buffer SXGA. 59 */ 60static int capture_mem = 1280 * 960 * 2 * 2; 61 62static struct v4l2_int_device omap24xxcam; 63 64/* 65 * 66 * Clocks. 67 * 68 */ 69 70static void omap24xxcam_clock_put(struct omap24xxcam_device *cam) 71{ 72 if (cam->ick != NULL && !IS_ERR(cam->ick)) 73 clk_put(cam->ick); 74 if (cam->fck != NULL && !IS_ERR(cam->fck)) 75 clk_put(cam->fck); 76 77 cam->ick = cam->fck = NULL; 78} 79 80static int omap24xxcam_clock_get(struct omap24xxcam_device *cam) 81{ 82 int rval = 0; 83 84 cam->fck = clk_get(cam->dev, "fck"); 85 if (IS_ERR(cam->fck)) { 86 dev_err(cam->dev, "can't get camera fck"); 87 rval = PTR_ERR(cam->fck); 88 omap24xxcam_clock_put(cam); 89 return rval; 90 } 91 92 cam->ick = clk_get(cam->dev, "ick"); 93 if (IS_ERR(cam->ick)) { 94 dev_err(cam->dev, "can't get camera ick"); 95 rval = PTR_ERR(cam->ick); 96 omap24xxcam_clock_put(cam); 97 } 98 99 return rval; 100} 101 102static void omap24xxcam_clock_on(struct omap24xxcam_device *cam) 103{ 104 clk_enable(cam->fck); 105 clk_enable(cam->ick); 106} 107 108static void omap24xxcam_clock_off(struct omap24xxcam_device *cam) 109{ 110 clk_disable(cam->fck); 111 clk_disable(cam->ick); 112} 113 114/* 115 * 116 * Camera core 117 * 118 */ 119 120/* 121 * Set xclk. 122 * 123 * To disable xclk, use value zero. 124 */ 125static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam, 126 u32 xclk) 127{ 128 if (xclk) { 129 u32 divisor = CAM_MCLK / xclk; 130 131 if (divisor == 1) 132 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, 133 CC_CTRL_XCLK, 134 CC_CTRL_XCLK_DIV_BYPASS); 135 else 136 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, 137 CC_CTRL_XCLK, divisor); 138 } else 139 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, 140 CC_CTRL_XCLK, CC_CTRL_XCLK_DIV_STABLE_LOW); 141} 142 143static void omap24xxcam_core_hwinit(const struct omap24xxcam_device *cam) 144{ 145 /* 146 * Setting the camera core AUTOIDLE bit causes problems with frame 147 * synchronization, so we will clear the AUTOIDLE bit instead. 148 */ 149 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_SYSCONFIG, 150 CC_SYSCONFIG_AUTOIDLE); 151 152 /* program the camera interface DMA packet size */ 153 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL_DMA, 154 CC_CTRL_DMA_EN | (DMA_THRESHOLD / 4 - 1)); 155 156 /* enable camera core error interrupts */ 157 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQENABLE, 158 CC_IRQENABLE_FW_ERR_IRQ 159 | CC_IRQENABLE_FSC_ERR_IRQ 160 | CC_IRQENABLE_SSC_ERR_IRQ 161 | CC_IRQENABLE_FIFO_OF_IRQ); 162} 163 164/* 165 * Enable the camera core. 166 * 167 * Data transfer to the camera DMA starts from next starting frame. 168 */ 169static void omap24xxcam_core_enable(const struct omap24xxcam_device *cam) 170{ 171 172 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL, 173 cam->cc_ctrl); 174} 175 176/* 177 * Disable camera core. 178 * 179 * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The 180 * core internal state machines will be reset. Use 181 * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current 182 * frame completely. 183 */ 184static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam) 185{ 186 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL, 187 CC_CTRL_CC_RST); 188} 189 190/* Interrupt service routine for camera core interrupts. */ 191static void omap24xxcam_core_isr(struct omap24xxcam_device *cam) 192{ 193 u32 cc_irqstatus; 194 const u32 cc_irqstatus_err = 195 CC_IRQSTATUS_FW_ERR_IRQ 196 | CC_IRQSTATUS_FSC_ERR_IRQ 197 | CC_IRQSTATUS_SSC_ERR_IRQ 198 | CC_IRQSTATUS_FIFO_UF_IRQ 199 | CC_IRQSTATUS_FIFO_OF_IRQ; 200 201 cc_irqstatus = omap24xxcam_reg_in(cam->mmio_base + CC_REG_OFFSET, 202 CC_IRQSTATUS); 203 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQSTATUS, 204 cc_irqstatus); 205 206 if (cc_irqstatus & cc_irqstatus_err 207 && !atomic_read(&cam->in_reset)) { 208 dev_dbg(cam->dev, "resetting camera, cc_irqstatus 0x%x\n", 209 cc_irqstatus); 210 omap24xxcam_reset(cam); 211 } 212} 213 214/* 215 * 216 * videobuf_buffer handling. 217 * 218 * Memory for mmapped videobuf_buffers is not allocated 219 * conventionally, but by several kmalloc allocations and then 220 * creating the scatterlist on our own. User-space buffers are handled 221 * normally. 222 * 223 */ 224 225/* 226 * Free the memory-mapped buffer memory allocated for a 227 * videobuf_buffer and the associated scatterlist. 228 */ 229static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer *vb) 230{ 231 struct videobuf_dmabuf *dma = videobuf_to_dma(vb); 232 size_t alloc_size; 233 struct page *page; 234 int i; 235 236 if (dma->sglist == NULL) 237 return; 238 239 i = dma->sglen; 240 while (i) { 241 i--; 242 alloc_size = sg_dma_len(&dma->sglist[i]); 243 page = sg_page(&dma->sglist[i]); 244 do { 245 ClearPageReserved(page++); 246 } while (alloc_size -= PAGE_SIZE); 247 __free_pages(sg_page(&dma->sglist[i]), 248 get_order(sg_dma_len(&dma->sglist[i]))); 249 } 250 251 kfree(dma->sglist); 252 dma->sglist = NULL; 253} 254 255/* Release all memory related to the videobuf_queue. */ 256static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq) 257{ 258 int i; 259 260 mutex_lock(&vbq->vb_lock); 261 262 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 263 if (NULL == vbq->bufs[i]) 264 continue; 265 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory) 266 continue; 267 vbq->ops->buf_release(vbq, vbq->bufs[i]); 268 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]); 269 kfree(vbq->bufs[i]); 270 vbq->bufs[i] = NULL; 271 } 272 273 mutex_unlock(&vbq->vb_lock); 274 275 videobuf_mmap_free(vbq); 276} 277 278/* 279 * Allocate physically as contiguous as possible buffer for video 280 * frame and allocate and build DMA scatter-gather list for it. 281 */ 282static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb) 283{ 284 unsigned int order; 285 size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */ 286 struct page *page; 287 int max_pages, err = 0, i = 0; 288 struct videobuf_dmabuf *dma = videobuf_to_dma(vb); 289 290 /* 291 * allocate maximum size scatter-gather list. Note this is 292 * overhead. We may not use as many entries as we allocate 293 */ 294 max_pages = vb->bsize >> PAGE_SHIFT; 295 dma->sglist = kcalloc(max_pages, sizeof(*dma->sglist), GFP_KERNEL); 296 if (dma->sglist == NULL) { 297 err = -ENOMEM; 298 goto out; 299 } 300 301 while (size) { 302 order = get_order(size); 303 /* 304 * do not over-allocate even if we would get larger 305 * contiguous chunk that way 306 */ 307 if ((PAGE_SIZE << order) > size) 308 order--; 309 310 /* try to allocate as many contiguous pages as possible */ 311 page = alloc_pages(GFP_KERNEL | GFP_DMA, order); 312 /* if allocation fails, try to allocate smaller amount */ 313 while (page == NULL) { 314 order--; 315 page = alloc_pages(GFP_KERNEL | GFP_DMA, order); 316 if (page == NULL && !order) { 317 err = -ENOMEM; 318 goto out; 319 } 320 } 321 size -= (PAGE_SIZE << order); 322 323 /* append allocated chunk of pages into scatter-gather list */ 324 sg_set_page(&dma->sglist[i], page, PAGE_SIZE << order, 0); 325 dma->sglen++; 326 i++; 327 328 alloc_size = (PAGE_SIZE << order); 329 330 /* clear pages before giving them to user space */ 331 memset(page_address(page), 0, alloc_size); 332 333 /* mark allocated pages reserved */ 334 do { 335 SetPageReserved(page++); 336 } while (alloc_size -= PAGE_SIZE); 337 } 338 /* 339 * REVISIT: not fully correct to assign nr_pages == sglen but 340 * video-buf is passing nr_pages for e.g. unmap_sg calls 341 */ 342 dma->nr_pages = dma->sglen; 343 dma->direction = PCI_DMA_FROMDEVICE; 344 345 return 0; 346 347out: 348 omap24xxcam_vbq_free_mmap_buffer(vb); 349 return err; 350} 351 352static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq, 353 unsigned int count) 354{ 355 int i, err = 0; 356 struct omap24xxcam_fh *fh = 357 container_of(vbq, struct omap24xxcam_fh, vbq); 358 359 mutex_lock(&vbq->vb_lock); 360 361 for (i = 0; i < count; i++) { 362 err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]); 363 if (err) 364 goto out; 365 dev_dbg(fh->cam->dev, "sglen is %d for buffer %d\n", 366 videobuf_to_dma(vbq->bufs[i])->sglen, i); 367 } 368 369 mutex_unlock(&vbq->vb_lock); 370 371 return 0; 372out: 373 while (i) { 374 i--; 375 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]); 376 } 377 378 mutex_unlock(&vbq->vb_lock); 379 380 return err; 381} 382 383/* 384 * This routine is called from interrupt context when a scatter-gather DMA 385 * transfer of a videobuf_buffer completes. 386 */ 387static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma *sgdma, 388 u32 csr, void *arg) 389{ 390 struct omap24xxcam_device *cam = 391 container_of(sgdma, struct omap24xxcam_device, sgdma); 392 struct omap24xxcam_fh *fh = cam->streaming->private_data; 393 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg; 394 const u32 csr_error = CAMDMA_CSR_MISALIGNED_ERR 395 | CAMDMA_CSR_SUPERVISOR_ERR | CAMDMA_CSR_SECURE_ERR 396 | CAMDMA_CSR_TRANS_ERR | CAMDMA_CSR_DROP; 397 unsigned long flags; 398 399 spin_lock_irqsave(&cam->core_enable_disable_lock, flags); 400 if (--cam->sgdma_in_queue == 0) 401 omap24xxcam_core_disable(cam); 402 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags); 403 404 do_gettimeofday(&vb->ts); 405 vb->field_count = atomic_add_return(2, &fh->field_count); 406 if (csr & csr_error) { 407 vb->state = VIDEOBUF_ERROR; 408 if (!atomic_read(&fh->cam->in_reset)) { 409 dev_dbg(cam->dev, "resetting camera, csr 0x%x\n", csr); 410 omap24xxcam_reset(cam); 411 } 412 } else 413 vb->state = VIDEOBUF_DONE; 414 wake_up(&vb->done); 415} 416 417static void omap24xxcam_vbq_release(struct videobuf_queue *vbq, 418 struct videobuf_buffer *vb) 419{ 420 struct videobuf_dmabuf *dma = videobuf_to_dma(vb); 421 422 /* wait for buffer, especially to get out of the sgdma queue */ 423 videobuf_waiton(vb, 0, 0); 424 if (vb->memory == V4L2_MEMORY_MMAP) { 425 dma_unmap_sg(vbq->dev, dma->sglist, dma->sglen, 426 dma->direction); 427 dma->direction = DMA_NONE; 428 } else { 429 videobuf_dma_unmap(vbq->dev, videobuf_to_dma(vb)); 430 videobuf_dma_free(videobuf_to_dma(vb)); 431 } 432 433 vb->state = VIDEOBUF_NEEDS_INIT; 434} 435 436/* 437 * Limit the number of available kernel image capture buffers based on the 438 * number requested, the currently selected image size, and the maximum 439 * amount of memory permitted for kernel capture buffers. 440 */ 441static int omap24xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt, 442 unsigned int *size) 443{ 444 struct omap24xxcam_fh *fh = vbq->priv_data; 445 446 if (*cnt <= 0) 447 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */ 448 449 if (*cnt > VIDEO_MAX_FRAME) 450 *cnt = VIDEO_MAX_FRAME; 451 452 *size = fh->pix.sizeimage; 453 454 /* accessing fh->cam->capture_mem is ok, it's constant */ 455 if (*size * *cnt > fh->cam->capture_mem) 456 *cnt = fh->cam->capture_mem / *size; 457 458 return 0; 459} 460 461static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq, 462 struct videobuf_dmabuf *dma) 463{ 464 int err = 0; 465 466 dma->direction = PCI_DMA_FROMDEVICE; 467 if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) { 468 kfree(dma->sglist); 469 dma->sglist = NULL; 470 dma->sglen = 0; 471 err = -EIO; 472 } 473 474 return err; 475} 476 477static int omap24xxcam_vbq_prepare(struct videobuf_queue *vbq, 478 struct videobuf_buffer *vb, 479 enum v4l2_field field) 480{ 481 struct omap24xxcam_fh *fh = vbq->priv_data; 482 int err = 0; 483 484 /* 485 * Accessing pix here is okay since it's constant while 486 * streaming is on (and we only get called then). 487 */ 488 if (vb->baddr) { 489 /* This is a userspace buffer. */ 490 if (fh->pix.sizeimage > vb->bsize) { 491 /* The buffer isn't big enough. */ 492 err = -EINVAL; 493 } else 494 vb->size = fh->pix.sizeimage; 495 } else { 496 if (vb->state != VIDEOBUF_NEEDS_INIT) { 497 /* 498 * We have a kernel bounce buffer that has 499 * already been allocated. 500 */ 501 if (fh->pix.sizeimage > vb->size) { 502 /* 503 * The image size has been changed to 504 * a larger size since this buffer was 505 * allocated, so we need to free and 506 * reallocate it. 507 */ 508 omap24xxcam_vbq_release(vbq, vb); 509 vb->size = fh->pix.sizeimage; 510 } 511 } else { 512 /* We need to allocate a new kernel bounce buffer. */ 513 vb->size = fh->pix.sizeimage; 514 } 515 } 516 517 if (err) 518 return err; 519 520 vb->width = fh->pix.width; 521 vb->height = fh->pix.height; 522 vb->field = field; 523 524 if (vb->state == VIDEOBUF_NEEDS_INIT) { 525 if (vb->memory == V4L2_MEMORY_MMAP) 526 /* 527 * we have built the scatter-gather list by ourself so 528 * do the scatter-gather mapping as well 529 */ 530 err = omap24xxcam_dma_iolock(vbq, videobuf_to_dma(vb)); 531 else 532 err = videobuf_iolock(vbq, vb, NULL); 533 } 534 535 if (!err) 536 vb->state = VIDEOBUF_PREPARED; 537 else 538 omap24xxcam_vbq_release(vbq, vb); 539 540 return err; 541} 542 543static void omap24xxcam_vbq_queue(struct videobuf_queue *vbq, 544 struct videobuf_buffer *vb) 545{ 546 struct omap24xxcam_fh *fh = vbq->priv_data; 547 struct omap24xxcam_device *cam = fh->cam; 548 enum videobuf_state state = vb->state; 549 unsigned long flags; 550 int err; 551 552 vb->state = VIDEOBUF_ACTIVE; 553 554 err = omap24xxcam_sgdma_queue(&fh->cam->sgdma, 555 videobuf_to_dma(vb)->sglist, 556 videobuf_to_dma(vb)->sglen, vb->size, 557 omap24xxcam_vbq_complete, vb); 558 559 if (!err) { 560 spin_lock_irqsave(&cam->core_enable_disable_lock, flags); 561 if (++cam->sgdma_in_queue == 1 562 && !atomic_read(&cam->in_reset)) 563 omap24xxcam_core_enable(cam); 564 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags); 565 } else { 566 /* 567 * Oops. We're not supposed to get any errors here. 568 * The only way we could get an error is if we ran out 569 * of scatter-gather DMA slots, but we are supposed to 570 * have at least as many scatter-gather DMA slots as 571 * video buffers so that can't happen. 572 */ 573 dev_err(cam->dev, "failed to queue a video buffer for dma!\n"); 574 dev_err(cam->dev, "likely a bug in the driver!\n"); 575 vb->state = state; 576 } 577} 578 579static struct videobuf_queue_ops omap24xxcam_vbq_ops = { 580 .buf_setup = omap24xxcam_vbq_setup, 581 .buf_prepare = omap24xxcam_vbq_prepare, 582 .buf_queue = omap24xxcam_vbq_queue, 583 .buf_release = omap24xxcam_vbq_release, 584}; 585 586/* 587 * 588 * OMAP main camera system 589 * 590 */ 591 592/* 593 * Reset camera block to power-on state. 594 */ 595static void omap24xxcam_poweron_reset(struct omap24xxcam_device *cam) 596{ 597 int max_loop = RESET_TIMEOUT_NS; 598 599 /* Reset whole camera subsystem */ 600 omap24xxcam_reg_out(cam->mmio_base, 601 CAM_SYSCONFIG, 602 CAM_SYSCONFIG_SOFTRESET); 603 604 /* Wait till it's finished */ 605 while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS) 606 & CAM_SYSSTATUS_RESETDONE) 607 && --max_loop) { 608 ndelay(1); 609 } 610 611 if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS) 612 & CAM_SYSSTATUS_RESETDONE)) 613 dev_err(cam->dev, "camera soft reset timeout\n"); 614} 615 616/* 617 * (Re)initialise the camera block. 618 */ 619static void omap24xxcam_hwinit(struct omap24xxcam_device *cam) 620{ 621 omap24xxcam_poweron_reset(cam); 622 623 /* set the camera subsystem autoidle bit */ 624 omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG, 625 CAM_SYSCONFIG_AUTOIDLE); 626 627 /* set the camera MMU autoidle bit */ 628 omap24xxcam_reg_out(cam->mmio_base, 629 CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG, 630 CAMMMU_SYSCONFIG_AUTOIDLE); 631 632 omap24xxcam_core_hwinit(cam); 633 634 omap24xxcam_dma_hwinit(&cam->sgdma.dma); 635} 636 637/* 638 * Callback for dma transfer stalling. 639 */ 640static void omap24xxcam_stalled_dma_reset(unsigned long data) 641{ 642 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data; 643 644 if (!atomic_read(&cam->in_reset)) { 645 dev_dbg(cam->dev, "dma stalled, resetting camera\n"); 646 omap24xxcam_reset(cam); 647 } 648} 649 650/* 651 * Stop capture. Mark we're doing a reset, stop DMA transfers and 652 * core. (No new scatter-gather transfers will be queued whilst 653 * in_reset is non-zero.) 654 * 655 * If omap24xxcam_capture_stop is called from several places at 656 * once, only the first call will have an effect. Similarly, the last 657 * call omap24xxcam_streaming_cont will have effect. 658 * 659 * Serialisation is ensured by using cam->core_enable_disable_lock. 660 */ 661static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam) 662{ 663 unsigned long flags; 664 665 spin_lock_irqsave(&cam->core_enable_disable_lock, flags); 666 667 if (atomic_inc_return(&cam->in_reset) != 1) { 668 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags); 669 return; 670 } 671 672 omap24xxcam_core_disable(cam); 673 674 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags); 675 676 omap24xxcam_sgdma_sync(&cam->sgdma); 677} 678 679/* 680 * Reset and continue streaming. 681 * 682 * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL 683 * register is supposed to be sufficient to recover from a camera 684 * interface error, but it doesn't seem to be enough. If we only do 685 * that then subsequent image captures are out of sync by either one 686 * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the 687 * entire camera subsystem prevents the problem with frame 688 * synchronization. 689 */ 690static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam) 691{ 692 unsigned long flags; 693 694 spin_lock_irqsave(&cam->core_enable_disable_lock, flags); 695 696 if (atomic_read(&cam->in_reset) != 1) 697 goto out; 698 699 omap24xxcam_hwinit(cam); 700 701 omap24xxcam_sensor_if_enable(cam); 702 703 omap24xxcam_sgdma_process(&cam->sgdma); 704 705 if (cam->sgdma_in_queue) 706 omap24xxcam_core_enable(cam); 707 708out: 709 atomic_dec(&cam->in_reset); 710 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags); 711} 712 713static ssize_t 714omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr, 715 char *buf) 716{ 717 struct omap24xxcam_device *cam = dev_get_drvdata(dev); 718 719 return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive"); 720} 721static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL); 722 723/* 724 * Stop capture and restart it. I.e. reset the camera during use. 725 */ 726static void omap24xxcam_reset(struct omap24xxcam_device *cam) 727{ 728 omap24xxcam_capture_stop(cam); 729 omap24xxcam_capture_cont(cam); 730} 731 732/* 733 * The main interrupt handler. 734 */ 735static irqreturn_t omap24xxcam_isr(int irq, void *arg) 736{ 737 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg; 738 u32 irqstatus; 739 unsigned int irqhandled = 0; 740 741 irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS); 742 743 if (irqstatus & 744 (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1 745 | CAM_IRQSTATUS_DMA_IRQ0)) { 746 omap24xxcam_dma_isr(&cam->sgdma.dma); 747 irqhandled = 1; 748 } 749 if (irqstatus & CAM_IRQSTATUS_CC_IRQ) { 750 omap24xxcam_core_isr(cam); 751 irqhandled = 1; 752 } 753 if (irqstatus & CAM_IRQSTATUS_MMU_IRQ) 754 dev_err(cam->dev, "unhandled camera MMU interrupt!\n"); 755 756 return IRQ_RETVAL(irqhandled); 757} 758 759/* 760 * 761 * Sensor handling. 762 * 763 */ 764 765/* 766 * Enable the external sensor interface. Try to negotiate interface 767 * parameters with the sensor and start using the new ones. The calls 768 * to sensor_if_enable and sensor_if_disable need not to be balanced. 769 */ 770static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam) 771{ 772 int rval; 773 struct v4l2_ifparm p; 774 775 rval = vidioc_int_g_ifparm(cam->sdev, &p); 776 if (rval) { 777 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval); 778 return rval; 779 } 780 781 cam->if_type = p.if_type; 782 783 cam->cc_ctrl = CC_CTRL_CC_EN; 784 785 switch (p.if_type) { 786 case V4L2_IF_TYPE_BT656: 787 if (p.u.bt656.frame_start_on_rising_vs) 788 cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO; 789 if (p.u.bt656.bt_sync_correct) 790 cam->cc_ctrl |= CC_CTRL_BT_CORRECT; 791 if (p.u.bt656.swap) 792 cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM; 793 if (p.u.bt656.latch_clk_inv) 794 cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL; 795 if (p.u.bt656.nobt_hs_inv) 796 cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL; 797 if (p.u.bt656.nobt_vs_inv) 798 cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL; 799 800 switch (p.u.bt656.mode) { 801 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT: 802 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8; 803 break; 804 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT: 805 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10; 806 break; 807 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT: 808 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12; 809 break; 810 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT: 811 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8; 812 break; 813 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT: 814 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10; 815 break; 816 default: 817 dev_err(cam->dev, 818 "bt656 interface mode %d not supported\n", 819 p.u.bt656.mode); 820 return -EINVAL; 821 } 822 /* 823 * The clock rate that the sensor wants has changed. 824 * We have to adjust the xclk from OMAP 2 side to 825 * match the sensor's wish as closely as possible. 826 */ 827 if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) { 828 u32 xclk = p.u.bt656.clock_curr; 829 u32 divisor; 830 831 if (xclk == 0) 832 return -EINVAL; 833 834 if (xclk > CAM_MCLK) 835 xclk = CAM_MCLK; 836 837 divisor = CAM_MCLK / xclk; 838 if (divisor * xclk < CAM_MCLK) 839 divisor++; 840 if (CAM_MCLK / divisor < p.u.bt656.clock_min 841 && divisor > 1) 842 divisor--; 843 if (divisor > 30) 844 divisor = 30; 845 846 xclk = CAM_MCLK / divisor; 847 848 if (xclk < p.u.bt656.clock_min 849 || xclk > p.u.bt656.clock_max) 850 return -EINVAL; 851 852 cam->if_u.bt656.xclk = xclk; 853 } 854 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk); 855 break; 856 default: 857 dev_err(cam->dev, "interface type %d not supported\n", 858 p.if_type); 859 return -EINVAL; 860 } 861 862 return 0; 863} 864 865static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam) 866{ 867 switch (cam->if_type) { 868 case V4L2_IF_TYPE_BT656: 869 omap24xxcam_core_xclk_set(cam, 0); 870 break; 871 } 872} 873 874/* 875 * Initialise the sensor hardware. 876 */ 877static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam) 878{ 879 int err = 0; 880 struct v4l2_int_device *sdev = cam->sdev; 881 882 omap24xxcam_clock_on(cam); 883 err = omap24xxcam_sensor_if_enable(cam); 884 if (err) { 885 dev_err(cam->dev, "sensor interface could not be enabled at " 886 "initialisation, %d\n", err); 887 cam->sdev = NULL; 888 goto out; 889 } 890 891 /* power up sensor during sensor initialization */ 892 vidioc_int_s_power(sdev, 1); 893 894 err = vidioc_int_dev_init(sdev); 895 if (err) { 896 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err); 897 /* Sensor init failed --- it's nonexistent to us! */ 898 cam->sdev = NULL; 899 goto out; 900 } 901 902 dev_info(cam->dev, "sensor is %s\n", sdev->name); 903 904out: 905 omap24xxcam_sensor_if_disable(cam); 906 omap24xxcam_clock_off(cam); 907 908 vidioc_int_s_power(sdev, 0); 909 910 return err; 911} 912 913static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam) 914{ 915 if (cam->sdev) 916 vidioc_int_dev_exit(cam->sdev); 917} 918 919static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam) 920{ 921 omap24xxcam_sensor_if_disable(cam); 922 omap24xxcam_clock_off(cam); 923 vidioc_int_s_power(cam->sdev, 0); 924} 925 926/* 927 * Power-up and configure camera sensor. It's ready for capturing now. 928 */ 929static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam) 930{ 931 int rval; 932 933 omap24xxcam_clock_on(cam); 934 935 omap24xxcam_sensor_if_enable(cam); 936 937 rval = vidioc_int_s_power(cam->sdev, 1); 938 if (rval) 939 goto out; 940 941 rval = vidioc_int_init(cam->sdev); 942 if (rval) 943 goto out; 944 945 return 0; 946 947out: 948 omap24xxcam_sensor_disable(cam); 949 950 return rval; 951} 952 953static void omap24xxcam_sensor_reset_work(struct work_struct *work) 954{ 955 struct omap24xxcam_device *cam = 956 container_of(work, struct omap24xxcam_device, 957 sensor_reset_work); 958 959 if (atomic_read(&cam->reset_disable)) 960 return; 961 962 omap24xxcam_capture_stop(cam); 963 964 if (vidioc_int_reset(cam->sdev) == 0) { 965 vidioc_int_init(cam->sdev); 966 } else { 967 /* Can't reset it by vidioc_int_reset. */ 968 omap24xxcam_sensor_disable(cam); 969 omap24xxcam_sensor_enable(cam); 970 } 971 972 omap24xxcam_capture_cont(cam); 973} 974 975/* 976 * 977 * IOCTL interface. 978 * 979 */ 980 981static int vidioc_querycap(struct file *file, void *fh, 982 struct v4l2_capability *cap) 983{ 984 struct omap24xxcam_fh *ofh = fh; 985 struct omap24xxcam_device *cam = ofh->cam; 986 987 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver)); 988 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card)); 989 cap->version = OMAP24XXCAM_VERSION; 990 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 991 992 return 0; 993} 994 995static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, 996 struct v4l2_fmtdesc *f) 997{ 998 struct omap24xxcam_fh *ofh = fh; 999 struct omap24xxcam_device *cam = ofh->cam; 1000 int rval; 1001 1002 rval = vidioc_int_enum_fmt_cap(cam->sdev, f); 1003 1004 return rval; 1005} 1006 1007static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, 1008 struct v4l2_format *f) 1009{ 1010 struct omap24xxcam_fh *ofh = fh; 1011 struct omap24xxcam_device *cam = ofh->cam; 1012 int rval; 1013 1014 mutex_lock(&cam->mutex); 1015 rval = vidioc_int_g_fmt_cap(cam->sdev, f); 1016 mutex_unlock(&cam->mutex); 1017 1018 return rval; 1019} 1020 1021static int vidioc_s_fmt_vid_cap(struct file *file, void *fh, 1022 struct v4l2_format *f) 1023{ 1024 struct omap24xxcam_fh *ofh = fh; 1025 struct omap24xxcam_device *cam = ofh->cam; 1026 int rval; 1027 1028 mutex_lock(&cam->mutex); 1029 if (cam->streaming) { 1030 rval = -EBUSY; 1031 goto out; 1032 } 1033 1034 rval = vidioc_int_s_fmt_cap(cam->sdev, f); 1035 1036out: 1037 mutex_unlock(&cam->mutex); 1038 1039 if (!rval) { 1040 mutex_lock(&ofh->vbq.vb_lock); 1041 ofh->pix = f->fmt.pix; 1042 mutex_unlock(&ofh->vbq.vb_lock); 1043 } 1044 1045 memset(f, 0, sizeof(*f)); 1046 vidioc_g_fmt_vid_cap(file, fh, f); 1047 1048 return rval; 1049} 1050 1051static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, 1052 struct v4l2_format *f) 1053{ 1054 struct omap24xxcam_fh *ofh = fh; 1055 struct omap24xxcam_device *cam = ofh->cam; 1056 int rval; 1057 1058 mutex_lock(&cam->mutex); 1059 rval = vidioc_int_try_fmt_cap(cam->sdev, f); 1060 mutex_unlock(&cam->mutex); 1061 1062 return rval; 1063} 1064 1065static int vidioc_reqbufs(struct file *file, void *fh, 1066 struct v4l2_requestbuffers *b) 1067{ 1068 struct omap24xxcam_fh *ofh = fh; 1069 struct omap24xxcam_device *cam = ofh->cam; 1070 int rval; 1071 1072 mutex_lock(&cam->mutex); 1073 if (cam->streaming) { 1074 mutex_unlock(&cam->mutex); 1075 return -EBUSY; 1076 } 1077 1078 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq); 1079 mutex_unlock(&cam->mutex); 1080 1081 rval = videobuf_reqbufs(&ofh->vbq, b); 1082 1083 /* 1084 * Either videobuf_reqbufs failed or the buffers are not 1085 * memory-mapped (which would need special attention). 1086 */ 1087 if (rval < 0 || b->memory != V4L2_MEMORY_MMAP) 1088 goto out; 1089 1090 rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval); 1091 if (rval) 1092 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq); 1093 1094out: 1095 return rval; 1096} 1097 1098static int vidioc_querybuf(struct file *file, void *fh, 1099 struct v4l2_buffer *b) 1100{ 1101 struct omap24xxcam_fh *ofh = fh; 1102 1103 return videobuf_querybuf(&ofh->vbq, b); 1104} 1105 1106static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) 1107{ 1108 struct omap24xxcam_fh *ofh = fh; 1109 1110 return videobuf_qbuf(&ofh->vbq, b); 1111} 1112 1113static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) 1114{ 1115 struct omap24xxcam_fh *ofh = fh; 1116 struct omap24xxcam_device *cam = ofh->cam; 1117 struct videobuf_buffer *vb; 1118 int rval; 1119 1120videobuf_dqbuf_again: 1121 rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK); 1122 if (rval) 1123 goto out; 1124 1125 vb = ofh->vbq.bufs[b->index]; 1126 1127 mutex_lock(&cam->mutex); 1128 /* _needs_reset returns -EIO if reset is required. */ 1129 rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr); 1130 mutex_unlock(&cam->mutex); 1131 if (rval == -EIO) 1132 schedule_work(&cam->sensor_reset_work); 1133 else 1134 rval = 0; 1135 1136out: 1137 /* 1138 * This is a hack. We don't want to show -EIO to the user 1139 * space. Requeue the buffer and try again if we're not doing 1140 * this in non-blocking mode. 1141 */ 1142 if (rval == -EIO) { 1143 videobuf_qbuf(&ofh->vbq, b); 1144 if (!(file->f_flags & O_NONBLOCK)) 1145 goto videobuf_dqbuf_again; 1146 /* 1147 * We don't have a videobuf_buffer now --- maybe next 1148 * time... 1149 */ 1150 rval = -EAGAIN; 1151 } 1152 1153 return rval; 1154} 1155 1156static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i) 1157{ 1158 struct omap24xxcam_fh *ofh = fh; 1159 struct omap24xxcam_device *cam = ofh->cam; 1160 int rval; 1161 1162 mutex_lock(&cam->mutex); 1163 if (cam->streaming) { 1164 rval = -EBUSY; 1165 goto out; 1166 } 1167 1168 rval = omap24xxcam_sensor_if_enable(cam); 1169 if (rval) { 1170 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n"); 1171 goto out; 1172 } 1173 1174 rval = videobuf_streamon(&ofh->vbq); 1175 if (!rval) { 1176 cam->streaming = file; 1177 sysfs_notify(&cam->dev->kobj, NULL, "streaming"); 1178 } 1179 1180out: 1181 mutex_unlock(&cam->mutex); 1182 1183 return rval; 1184} 1185 1186static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i) 1187{ 1188 struct omap24xxcam_fh *ofh = fh; 1189 struct omap24xxcam_device *cam = ofh->cam; 1190 struct videobuf_queue *q = &ofh->vbq; 1191 int rval; 1192 1193 atomic_inc(&cam->reset_disable); 1194 1195 flush_scheduled_work(); 1196 1197 rval = videobuf_streamoff(q); 1198 if (!rval) { 1199 mutex_lock(&cam->mutex); 1200 cam->streaming = NULL; 1201 mutex_unlock(&cam->mutex); 1202 sysfs_notify(&cam->dev->kobj, NULL, "streaming"); 1203 } 1204 1205 atomic_dec(&cam->reset_disable); 1206 1207 return rval; 1208} 1209 1210static int vidioc_enum_input(struct file *file, void *fh, 1211 struct v4l2_input *inp) 1212{ 1213 if (inp->index > 0) 1214 return -EINVAL; 1215 1216 strlcpy(inp->name, "camera", sizeof(inp->name)); 1217 inp->type = V4L2_INPUT_TYPE_CAMERA; 1218 1219 return 0; 1220} 1221 1222static int vidioc_g_input(struct file *file, void *fh, unsigned int *i) 1223{ 1224 *i = 0; 1225 1226 return 0; 1227} 1228 1229static int vidioc_s_input(struct file *file, void *fh, unsigned int i) 1230{ 1231 if (i > 0) 1232 return -EINVAL; 1233 1234 return 0; 1235} 1236 1237static int vidioc_queryctrl(struct file *file, void *fh, 1238 struct v4l2_queryctrl *a) 1239{ 1240 struct omap24xxcam_fh *ofh = fh; 1241 struct omap24xxcam_device *cam = ofh->cam; 1242 int rval; 1243 1244 rval = vidioc_int_queryctrl(cam->sdev, a); 1245 1246 return rval; 1247} 1248 1249static int vidioc_g_ctrl(struct file *file, void *fh, 1250 struct v4l2_control *a) 1251{ 1252 struct omap24xxcam_fh *ofh = fh; 1253 struct omap24xxcam_device *cam = ofh->cam; 1254 int rval; 1255 1256 mutex_lock(&cam->mutex); 1257 rval = vidioc_int_g_ctrl(cam->sdev, a); 1258 mutex_unlock(&cam->mutex); 1259 1260 return rval; 1261} 1262 1263static int vidioc_s_ctrl(struct file *file, void *fh, 1264 struct v4l2_control *a) 1265{ 1266 struct omap24xxcam_fh *ofh = fh; 1267 struct omap24xxcam_device *cam = ofh->cam; 1268 int rval; 1269 1270 mutex_lock(&cam->mutex); 1271 rval = vidioc_int_s_ctrl(cam->sdev, a); 1272 mutex_unlock(&cam->mutex); 1273 1274 return rval; 1275} 1276 1277static int vidioc_g_parm(struct file *file, void *fh, 1278 struct v4l2_streamparm *a) { 1279 struct omap24xxcam_fh *ofh = fh; 1280 struct omap24xxcam_device *cam = ofh->cam; 1281 int rval; 1282 1283 mutex_lock(&cam->mutex); 1284 rval = vidioc_int_g_parm(cam->sdev, a); 1285 mutex_unlock(&cam->mutex); 1286 1287 return rval; 1288} 1289 1290static int vidioc_s_parm(struct file *file, void *fh, 1291 struct v4l2_streamparm *a) 1292{ 1293 struct omap24xxcam_fh *ofh = fh; 1294 struct omap24xxcam_device *cam = ofh->cam; 1295 struct v4l2_streamparm old_streamparm; 1296 int rval; 1297 1298 mutex_lock(&cam->mutex); 1299 if (cam->streaming) { 1300 rval = -EBUSY; 1301 goto out; 1302 } 1303 1304 old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1305 rval = vidioc_int_g_parm(cam->sdev, &old_streamparm); 1306 if (rval) 1307 goto out; 1308 1309 rval = vidioc_int_s_parm(cam->sdev, a); 1310 if (rval) 1311 goto out; 1312 1313 rval = omap24xxcam_sensor_if_enable(cam); 1314 /* 1315 * Revert to old streaming parameters if enabling sensor 1316 * interface with the new ones failed. 1317 */ 1318 if (rval) 1319 vidioc_int_s_parm(cam->sdev, &old_streamparm); 1320 1321out: 1322 mutex_unlock(&cam->mutex); 1323 1324 return rval; 1325} 1326 1327/* 1328 * 1329 * File operations. 1330 * 1331 */ 1332 1333static unsigned int omap24xxcam_poll(struct file *file, 1334 struct poll_table_struct *wait) 1335{ 1336 struct omap24xxcam_fh *fh = file->private_data; 1337 struct omap24xxcam_device *cam = fh->cam; 1338 struct videobuf_buffer *vb; 1339 1340 mutex_lock(&cam->mutex); 1341 if (cam->streaming != file) { 1342 mutex_unlock(&cam->mutex); 1343 return POLLERR; 1344 } 1345 mutex_unlock(&cam->mutex); 1346 1347 mutex_lock(&fh->vbq.vb_lock); 1348 if (list_empty(&fh->vbq.stream)) { 1349 mutex_unlock(&fh->vbq.vb_lock); 1350 return POLLERR; 1351 } 1352 vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream); 1353 mutex_unlock(&fh->vbq.vb_lock); 1354 1355 poll_wait(file, &vb->done, wait); 1356 1357 if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR) 1358 return POLLIN | POLLRDNORM; 1359 1360 return 0; 1361} 1362 1363static int omap24xxcam_mmap_buffers(struct file *file, 1364 struct vm_area_struct *vma) 1365{ 1366 struct omap24xxcam_fh *fh = file->private_data; 1367 struct omap24xxcam_device *cam = fh->cam; 1368 struct videobuf_queue *vbq = &fh->vbq; 1369 unsigned int first, last, size, i, j; 1370 int err = 0; 1371 1372 mutex_lock(&cam->mutex); 1373 if (cam->streaming) { 1374 mutex_unlock(&cam->mutex); 1375 return -EBUSY; 1376 } 1377 mutex_unlock(&cam->mutex); 1378 mutex_lock(&vbq->vb_lock); 1379 1380 /* look for first buffer to map */ 1381 for (first = 0; first < VIDEO_MAX_FRAME; first++) { 1382 if (NULL == vbq->bufs[first]) 1383 continue; 1384 if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory) 1385 continue; 1386 if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT)) 1387 break; 1388 } 1389 1390 /* look for last buffer to map */ 1391 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) { 1392 if (NULL == vbq->bufs[last]) 1393 continue; 1394 if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory) 1395 continue; 1396 size += vbq->bufs[last]->bsize; 1397 if (size == (vma->vm_end - vma->vm_start)) 1398 break; 1399 } 1400 1401 size = 0; 1402 for (i = first; i <= last && i < VIDEO_MAX_FRAME; i++) { 1403 struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]); 1404 1405 for (j = 0; j < dma->sglen; j++) { 1406 err = remap_pfn_range( 1407 vma, vma->vm_start + size, 1408 page_to_pfn(sg_page(&dma->sglist[j])), 1409 sg_dma_len(&dma->sglist[j]), vma->vm_page_prot); 1410 if (err) 1411 goto out; 1412 size += sg_dma_len(&dma->sglist[j]); 1413 } 1414 } 1415 1416out: 1417 mutex_unlock(&vbq->vb_lock); 1418 1419 return err; 1420} 1421 1422static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma) 1423{ 1424 struct omap24xxcam_fh *fh = file->private_data; 1425 int rval; 1426 1427 /* let the video-buf mapper check arguments and set-up structures */ 1428 rval = videobuf_mmap_mapper(&fh->vbq, vma); 1429 if (rval) 1430 return rval; 1431 1432 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1433 1434 /* do mapping to our allocated buffers */ 1435 rval = omap24xxcam_mmap_buffers(file, vma); 1436 /* 1437 * In case of error, free vma->vm_private_data allocated by 1438 * videobuf_mmap_mapper. 1439 */ 1440 if (rval) 1441 kfree(vma->vm_private_data); 1442 1443 return rval; 1444} 1445 1446static int omap24xxcam_open(struct file *file) 1447{ 1448 struct omap24xxcam_device *cam = omap24xxcam.priv; 1449 struct omap24xxcam_fh *fh; 1450 struct v4l2_format format; 1451 1452 if (!cam || !cam->vfd) 1453 return -ENODEV; 1454 1455 fh = kzalloc(sizeof(*fh), GFP_KERNEL); 1456 if (fh == NULL) 1457 return -ENOMEM; 1458 1459 mutex_lock(&cam->mutex); 1460 if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) { 1461 mutex_unlock(&cam->mutex); 1462 goto out_try_module_get; 1463 } 1464 1465 if (atomic_inc_return(&cam->users) == 1) { 1466 omap24xxcam_hwinit(cam); 1467 if (omap24xxcam_sensor_enable(cam)) { 1468 mutex_unlock(&cam->mutex); 1469 goto out_omap24xxcam_sensor_enable; 1470 } 1471 } 1472 mutex_unlock(&cam->mutex); 1473 1474 fh->cam = cam; 1475 mutex_lock(&cam->mutex); 1476 vidioc_int_g_fmt_cap(cam->sdev, &format); 1477 mutex_unlock(&cam->mutex); 1478 fh->pix = format.fmt.pix; 1479 1480 file->private_data = fh; 1481 1482 spin_lock_init(&fh->vbq_lock); 1483 1484 videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL, 1485 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE, 1486 V4L2_FIELD_NONE, 1487 sizeof(struct videobuf_buffer), fh); 1488 1489 return 0; 1490 1491out_omap24xxcam_sensor_enable: 1492 omap24xxcam_poweron_reset(cam); 1493 module_put(cam->sdev->module); 1494 1495out_try_module_get: 1496 kfree(fh); 1497 1498 return -ENODEV; 1499} 1500 1501static int omap24xxcam_release(struct file *file) 1502{ 1503 struct omap24xxcam_fh *fh = file->private_data; 1504 struct omap24xxcam_device *cam = fh->cam; 1505 1506 atomic_inc(&cam->reset_disable); 1507 1508 flush_scheduled_work(); 1509 1510 /* stop streaming capture */ 1511 videobuf_streamoff(&fh->vbq); 1512 1513 mutex_lock(&cam->mutex); 1514 if (cam->streaming == file) { 1515 cam->streaming = NULL; 1516 mutex_unlock(&cam->mutex); 1517 sysfs_notify(&cam->dev->kobj, NULL, "streaming"); 1518 } else { 1519 mutex_unlock(&cam->mutex); 1520 } 1521 1522 atomic_dec(&cam->reset_disable); 1523 1524 omap24xxcam_vbq_free_mmap_buffers(&fh->vbq); 1525 1526 /* 1527 * Make sure the reset work we might have scheduled is not 1528 * pending! It may be run *only* if we have users. (And it may 1529 * not be scheduled anymore since streaming is already 1530 * disabled.) 1531 */ 1532 flush_scheduled_work(); 1533 1534 mutex_lock(&cam->mutex); 1535 if (atomic_dec_return(&cam->users) == 0) { 1536 omap24xxcam_sensor_disable(cam); 1537 omap24xxcam_poweron_reset(cam); 1538 } 1539 mutex_unlock(&cam->mutex); 1540 1541 file->private_data = NULL; 1542 1543 module_put(cam->sdev->module); 1544 kfree(fh); 1545 1546 return 0; 1547} 1548 1549static struct v4l2_file_operations omap24xxcam_fops = { 1550 .ioctl = video_ioctl2, 1551 .poll = omap24xxcam_poll, 1552 .mmap = omap24xxcam_mmap, 1553 .open = omap24xxcam_open, 1554 .release = omap24xxcam_release, 1555}; 1556 1557/* 1558 * 1559 * Power management. 1560 * 1561 */ 1562 1563#ifdef CONFIG_PM 1564static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state) 1565{ 1566 struct omap24xxcam_device *cam = platform_get_drvdata(pdev); 1567 1568 if (atomic_read(&cam->users) == 0) 1569 return 0; 1570 1571 if (!atomic_read(&cam->reset_disable)) 1572 omap24xxcam_capture_stop(cam); 1573 1574 omap24xxcam_sensor_disable(cam); 1575 omap24xxcam_poweron_reset(cam); 1576 1577 return 0; 1578} 1579 1580static int omap24xxcam_resume(struct platform_device *pdev) 1581{ 1582 struct omap24xxcam_device *cam = platform_get_drvdata(pdev); 1583 1584 if (atomic_read(&cam->users) == 0) 1585 return 0; 1586 1587 omap24xxcam_hwinit(cam); 1588 omap24xxcam_sensor_enable(cam); 1589 1590 if (!atomic_read(&cam->reset_disable)) 1591 omap24xxcam_capture_cont(cam); 1592 1593 return 0; 1594} 1595#endif /* CONFIG_PM */ 1596 1597static const struct v4l2_ioctl_ops omap24xxcam_ioctl_fops = { 1598 .vidioc_querycap = vidioc_querycap, 1599 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1600 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1601 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1602 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1603 .vidioc_reqbufs = vidioc_reqbufs, 1604 .vidioc_querybuf = vidioc_querybuf, 1605 .vidioc_qbuf = vidioc_qbuf, 1606 .vidioc_dqbuf = vidioc_dqbuf, 1607 .vidioc_streamon = vidioc_streamon, 1608 .vidioc_streamoff = vidioc_streamoff, 1609 .vidioc_enum_input = vidioc_enum_input, 1610 .vidioc_g_input = vidioc_g_input, 1611 .vidioc_s_input = vidioc_s_input, 1612 .vidioc_queryctrl = vidioc_queryctrl, 1613 .vidioc_g_ctrl = vidioc_g_ctrl, 1614 .vidioc_s_ctrl = vidioc_s_ctrl, 1615 .vidioc_g_parm = vidioc_g_parm, 1616 .vidioc_s_parm = vidioc_s_parm, 1617}; 1618 1619/* 1620 * 1621 * Camera device (i.e. /dev/video). 1622 * 1623 */ 1624 1625static int omap24xxcam_device_register(struct v4l2_int_device *s) 1626{ 1627 struct omap24xxcam_device *cam = s->u.slave->master->priv; 1628 struct video_device *vfd; 1629 int rval; 1630 1631 /* We already have a slave. */ 1632 if (cam->sdev) 1633 return -EBUSY; 1634 1635 cam->sdev = s; 1636 1637 if (device_create_file(cam->dev, &dev_attr_streaming) != 0) { 1638 dev_err(cam->dev, "could not register sysfs entry\n"); 1639 rval = -EBUSY; 1640 goto err; 1641 } 1642 1643 /* initialize the video_device struct */ 1644 vfd = cam->vfd = video_device_alloc(); 1645 if (!vfd) { 1646 dev_err(cam->dev, "could not allocate video device struct\n"); 1647 rval = -ENOMEM; 1648 goto err; 1649 } 1650 vfd->release = video_device_release; 1651 1652 vfd->parent = cam->dev; 1653 1654 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name)); 1655 vfd->fops = &omap24xxcam_fops; 1656 vfd->ioctl_ops = &omap24xxcam_ioctl_fops; 1657 1658 omap24xxcam_hwinit(cam); 1659 1660 rval = omap24xxcam_sensor_init(cam); 1661 if (rval) 1662 goto err; 1663 1664 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) { 1665 dev_err(cam->dev, "could not register V4L device\n"); 1666 rval = -EBUSY; 1667 goto err; 1668 } 1669 1670 omap24xxcam_poweron_reset(cam); 1671 1672 dev_info(cam->dev, "registered device %s\n", 1673 video_device_node_name(vfd)); 1674 1675 return 0; 1676 1677err: 1678 omap24xxcam_device_unregister(s); 1679 1680 return rval; 1681} 1682 1683static void omap24xxcam_device_unregister(struct v4l2_int_device *s) 1684{ 1685 struct omap24xxcam_device *cam = s->u.slave->master->priv; 1686 1687 omap24xxcam_sensor_exit(cam); 1688 1689 if (cam->vfd) { 1690 if (!video_is_registered(cam->vfd)) { 1691 /* 1692 * The device was never registered, so release the 1693 * video_device struct directly. 1694 */ 1695 video_device_release(cam->vfd); 1696 } else { 1697 /* 1698 * The unregister function will release the 1699 * video_device struct as well as 1700 * unregistering it. 1701 */ 1702 video_unregister_device(cam->vfd); 1703 } 1704 cam->vfd = NULL; 1705 } 1706 1707 device_remove_file(cam->dev, &dev_attr_streaming); 1708 1709 cam->sdev = NULL; 1710} 1711 1712static struct v4l2_int_master omap24xxcam_master = { 1713 .attach = omap24xxcam_device_register, 1714 .detach = omap24xxcam_device_unregister, 1715}; 1716 1717static struct v4l2_int_device omap24xxcam = { 1718 .module = THIS_MODULE, 1719 .name = CAM_NAME, 1720 .type = v4l2_int_type_master, 1721 .u = { 1722 .master = &omap24xxcam_master 1723 }, 1724}; 1725 1726/* 1727 * 1728 * Driver initialisation and deinitialisation. 1729 * 1730 */ 1731 1732static int __devinit omap24xxcam_probe(struct platform_device *pdev) 1733{ 1734 struct omap24xxcam_device *cam; 1735 struct resource *mem; 1736 int irq; 1737 1738 cam = kzalloc(sizeof(*cam), GFP_KERNEL); 1739 if (!cam) { 1740 dev_err(&pdev->dev, "could not allocate memory\n"); 1741 goto err; 1742 } 1743 1744 platform_set_drvdata(pdev, cam); 1745 1746 cam->dev = &pdev->dev; 1747 1748 /* 1749 * Impose a lower limit on the amount of memory allocated for 1750 * capture. We require at least enough memory to double-buffer 1751 * QVGA (300KB). 1752 */ 1753 if (capture_mem < 320 * 240 * 2 * 2) 1754 capture_mem = 320 * 240 * 2 * 2; 1755 cam->capture_mem = capture_mem; 1756 1757 /* request the mem region for the camera registers */ 1758 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1759 if (!mem) { 1760 dev_err(cam->dev, "no mem resource?\n"); 1761 goto err; 1762 } 1763 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1, 1764 pdev->name)) { 1765 dev_err(cam->dev, 1766 "cannot reserve camera register I/O region\n"); 1767 goto err; 1768 } 1769 cam->mmio_base_phys = mem->start; 1770 cam->mmio_size = (mem->end - mem->start) + 1; 1771 1772 /* map the region */ 1773 cam->mmio_base = (unsigned long) 1774 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size); 1775 if (!cam->mmio_base) { 1776 dev_err(cam->dev, "cannot map camera register I/O region\n"); 1777 goto err; 1778 } 1779 1780 irq = platform_get_irq(pdev, 0); 1781 if (irq <= 0) { 1782 dev_err(cam->dev, "no irq for camera?\n"); 1783 goto err; 1784 } 1785 1786 /* install the interrupt service routine */ 1787 if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) { 1788 dev_err(cam->dev, 1789 "could not install interrupt service routine\n"); 1790 goto err; 1791 } 1792 cam->irq = irq; 1793 1794 if (omap24xxcam_clock_get(cam)) 1795 goto err; 1796 1797 INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work); 1798 1799 mutex_init(&cam->mutex); 1800 spin_lock_init(&cam->core_enable_disable_lock); 1801 1802 omap24xxcam_sgdma_init(&cam->sgdma, 1803 cam->mmio_base + CAMDMA_REG_OFFSET, 1804 omap24xxcam_stalled_dma_reset, 1805 (unsigned long)cam); 1806 1807 omap24xxcam.priv = cam; 1808 1809 if (v4l2_int_device_register(&omap24xxcam)) 1810 goto err; 1811 1812 return 0; 1813 1814err: 1815 omap24xxcam_remove(pdev); 1816 return -ENODEV; 1817} 1818 1819static int omap24xxcam_remove(struct platform_device *pdev) 1820{ 1821 struct omap24xxcam_device *cam = platform_get_drvdata(pdev); 1822 1823 if (!cam) 1824 return 0; 1825 1826 if (omap24xxcam.priv != NULL) 1827 v4l2_int_device_unregister(&omap24xxcam); 1828 omap24xxcam.priv = NULL; 1829 1830 omap24xxcam_clock_put(cam); 1831 1832 if (cam->irq) { 1833 free_irq(cam->irq, cam); 1834 cam->irq = 0; 1835 } 1836 1837 if (cam->mmio_base) { 1838 iounmap((void *)cam->mmio_base); 1839 cam->mmio_base = 0; 1840 } 1841 1842 if (cam->mmio_base_phys) { 1843 release_mem_region(cam->mmio_base_phys, cam->mmio_size); 1844 cam->mmio_base_phys = 0; 1845 } 1846 1847 kfree(cam); 1848 1849 return 0; 1850} 1851 1852static struct platform_driver omap24xxcam_driver = { 1853 .probe = omap24xxcam_probe, 1854 .remove = omap24xxcam_remove, 1855#ifdef CONFIG_PM 1856 .suspend = omap24xxcam_suspend, 1857 .resume = omap24xxcam_resume, 1858#endif 1859 .driver = { 1860 .name = CAM_NAME, 1861 .owner = THIS_MODULE, 1862 }, 1863}; 1864 1865/* 1866 * 1867 * Module initialisation and deinitialisation 1868 * 1869 */ 1870 1871static int __init omap24xxcam_init(void) 1872{ 1873 return platform_driver_register(&omap24xxcam_driver); 1874} 1875 1876static void __exit omap24xxcam_cleanup(void) 1877{ 1878 platform_driver_unregister(&omap24xxcam_driver); 1879} 1880 1881MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>"); 1882MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver"); 1883MODULE_LICENSE("GPL"); 1884module_param(video_nr, int, 0); 1885MODULE_PARM_DESC(video_nr, 1886 "Minor number for video device (-1 ==> auto assign)"); 1887module_param(capture_mem, int, 0); 1888MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture " 1889 "buffers (default 4800kiB)"); 1890 1891module_init(omap24xxcam_init); 1892module_exit(omap24xxcam_cleanup); 1893