1/* 2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe" 3 * multifunction chip. Currently works with the Omnivision OV7670 4 * sensor. 5 * 6 * Copyright 2006 One Laptop Per Child Association, Inc. 7 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net> 8 * 9 * Written by Jonathan Corbet, corbet@lwn.net. 10 * 11 * This file may be distributed under the terms of the GNU General 12 * Public License, version 2. 13 */ 14 15#include <linux/kernel.h> 16#include <linux/module.h> 17#include <linux/moduleparam.h> 18#include <linux/init.h> 19#include <linux/fs.h> 20#include <linux/pci.h> 21#include <linux/i2c.h> 22#include <linux/interrupt.h> 23#include <linux/spinlock.h> 24#include <linux/videodev2.h> 25#include <media/v4l2-common.h> 26#include <media/v4l2-chip-ident.h> 27#include <linux/device.h> 28#include <linux/wait.h> 29#include <linux/list.h> 30#include <linux/dma-mapping.h> 31#include <linux/delay.h> 32#include <linux/debugfs.h> 33#include <linux/jiffies.h> 34#include <linux/vmalloc.h> 35 36#include <asm/uaccess.h> 37#include <asm/io.h> 38 39#include "cafe_ccic-regs.h" 40 41#define CAFE_VERSION 0x000002 42 43 44/* 45 * Parameters. 46 */ 47MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>"); 48MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver"); 49MODULE_LICENSE("GPL"); 50MODULE_SUPPORTED_DEVICE("Video"); 51 52/* 53 * Internal DMA buffer management. Since the controller cannot do S/G I/O, 54 * we must have physically contiguous buffers to bring frames into. 55 * These parameters control how many buffers we use, whether we 56 * allocate them at load time (better chance of success, but nails down 57 * memory) or when somebody tries to use the camera (riskier), and, 58 * for load-time allocation, how big they should be. 59 * 60 * The controller can cycle through three buffers. We could use 61 * more by flipping pointers around, but it probably makes little 62 * sense. 63 */ 64 65#define MAX_DMA_BUFS 3 66static int alloc_bufs_at_load = 0; 67module_param(alloc_bufs_at_load, bool, 0444); 68MODULE_PARM_DESC(alloc_bufs_at_load, 69 "Non-zero value causes DMA buffers to be allocated at module " 70 "load time. This increases the chances of successfully getting " 71 "those buffers, but at the cost of nailing down the memory from " 72 "the outset."); 73 74static int n_dma_bufs = 3; 75module_param(n_dma_bufs, uint, 0644); 76MODULE_PARM_DESC(n_dma_bufs, 77 "The number of DMA buffers to allocate. Can be either two " 78 "(saves memory, makes timing tighter) or three."); 79 80static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */ 81module_param(dma_buf_size, uint, 0444); 82MODULE_PARM_DESC(dma_buf_size, 83 "The size of the allocated DMA buffers. If actual operating " 84 "parameters require larger buffers, an attempt to reallocate " 85 "will be made."); 86 87static int min_buffers = 1; 88module_param(min_buffers, uint, 0644); 89MODULE_PARM_DESC(min_buffers, 90 "The minimum number of streaming I/O buffers we are willing " 91 "to work with."); 92 93static int max_buffers = 10; 94module_param(max_buffers, uint, 0644); 95MODULE_PARM_DESC(max_buffers, 96 "The maximum number of streaming I/O buffers an application " 97 "will be allowed to allocate. These buffers are big and live " 98 "in vmalloc space."); 99 100static int flip = 0; 101module_param(flip, bool, 0444); 102MODULE_PARM_DESC(flip, 103 "If set, the sensor will be instructed to flip the image " 104 "vertically."); 105 106 107enum cafe_state { 108 S_NOTREADY, /* Not yet initialized */ 109 S_IDLE, /* Just hanging around */ 110 S_FLAKED, /* Some sort of problem */ 111 S_SINGLEREAD, /* In read() */ 112 S_SPECREAD, /* Speculative read (for future read()) */ 113 S_STREAMING /* Streaming data */ 114}; 115 116/* 117 * Tracking of streaming I/O buffers. 118 */ 119struct cafe_sio_buffer { 120 struct list_head list; 121 struct v4l2_buffer v4lbuf; 122 char *buffer; /* Where it lives in kernel space */ 123 int mapcount; 124 struct cafe_camera *cam; 125}; 126 127/* 128 * A description of one of our devices. 129 * Locking: controlled by s_mutex. Certain fields, however, require 130 * the dev_lock spinlock; they are marked as such by comments. 131 * dev_lock is also required for access to device registers. 132 */ 133struct cafe_camera 134{ 135 enum cafe_state state; 136 unsigned long flags; /* Buffer status, mainly (dev_lock) */ 137 int users; /* How many open FDs */ 138 struct file *owner; /* Who has data access (v4l2) */ 139 140 /* 141 * Subsystem structures. 142 */ 143 struct pci_dev *pdev; 144 struct video_device v4ldev; 145 struct i2c_adapter i2c_adapter; 146 struct i2c_client *sensor; 147 148 unsigned char __iomem *regs; 149 struct list_head dev_list; /* link to other devices */ 150 151 /* DMA buffers */ 152 unsigned int nbufs; /* How many are alloc'd */ 153 int next_buf; /* Next to consume (dev_lock) */ 154 unsigned int dma_buf_size; /* allocated size */ 155 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */ 156 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */ 157 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */ 158 unsigned int sequence; /* Frame sequence number */ 159 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */ 160 161 /* Streaming buffers */ 162 unsigned int n_sbufs; /* How many we have */ 163 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */ 164 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */ 165 struct list_head sb_full; /* With data (user space owns) (dev_lock) */ 166 struct tasklet_struct s_tasklet; 167 168 /* Current operating parameters */ 169 u32 sensor_type; /* Currently ov7670 only */ 170 struct v4l2_pix_format pix_format; 171 172 /* Locks */ 173 struct mutex s_mutex; /* Access to this structure */ 174 spinlock_t dev_lock; /* Access to device */ 175 176 /* Misc */ 177 wait_queue_head_t smbus_wait; /* Waiting on i2c events */ 178 wait_queue_head_t iowait; /* Waiting on frame data */ 179#ifdef CONFIG_VIDEO_ADV_DEBUG 180 struct dentry *dfs_regs; 181 struct dentry *dfs_cam_regs; 182#endif 183}; 184 185/* 186 * Status flags. Always manipulated with bit operations. 187 */ 188#define CF_BUF0_VALID 0 /* Buffers valid - first three */ 189#define CF_BUF1_VALID 1 190#define CF_BUF2_VALID 2 191#define CF_DMA_ACTIVE 3 /* A frame is incoming */ 192#define CF_CONFIG_NEEDED 4 /* Must configure hardware */ 193 194 195 196/* 197 * Start over with DMA buffers - dev_lock needed. 198 */ 199static void cafe_reset_buffers(struct cafe_camera *cam) 200{ 201 int i; 202 203 cam->next_buf = -1; 204 for (i = 0; i < cam->nbufs; i++) 205 clear_bit(i, &cam->flags); 206 cam->specframes = 0; 207} 208 209static inline int cafe_needs_config(struct cafe_camera *cam) 210{ 211 return test_bit(CF_CONFIG_NEEDED, &cam->flags); 212} 213 214static void cafe_set_config_needed(struct cafe_camera *cam, int needed) 215{ 216 if (needed) 217 set_bit(CF_CONFIG_NEEDED, &cam->flags); 218 else 219 clear_bit(CF_CONFIG_NEEDED, &cam->flags); 220} 221 222 223 224 225/* 226 * Debugging and related. 227 */ 228#define cam_err(cam, fmt, arg...) \ 229 dev_err(&(cam)->pdev->dev, fmt, ##arg); 230#define cam_warn(cam, fmt, arg...) \ 231 dev_warn(&(cam)->pdev->dev, fmt, ##arg); 232#define cam_dbg(cam, fmt, arg...) \ 233 dev_dbg(&(cam)->pdev->dev, fmt, ##arg); 234 235 236/* ---------------------------------------------------------------------*/ 237/* 238 * We keep a simple list of known devices to search at open time. 239 */ 240static LIST_HEAD(cafe_dev_list); 241static DEFINE_MUTEX(cafe_dev_list_lock); 242 243static void cafe_add_dev(struct cafe_camera *cam) 244{ 245 mutex_lock(&cafe_dev_list_lock); 246 list_add_tail(&cam->dev_list, &cafe_dev_list); 247 mutex_unlock(&cafe_dev_list_lock); 248} 249 250static void cafe_remove_dev(struct cafe_camera *cam) 251{ 252 mutex_lock(&cafe_dev_list_lock); 253 list_del(&cam->dev_list); 254 mutex_unlock(&cafe_dev_list_lock); 255} 256 257static struct cafe_camera *cafe_find_dev(int minor) 258{ 259 struct cafe_camera *cam; 260 261 mutex_lock(&cafe_dev_list_lock); 262 list_for_each_entry(cam, &cafe_dev_list, dev_list) { 263 if (cam->v4ldev.minor == minor) 264 goto done; 265 } 266 cam = NULL; 267 done: 268 mutex_unlock(&cafe_dev_list_lock); 269 return cam; 270} 271 272 273static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev) 274{ 275 struct cafe_camera *cam; 276 277 mutex_lock(&cafe_dev_list_lock); 278 list_for_each_entry(cam, &cafe_dev_list, dev_list) { 279 if (cam->pdev == pdev) 280 goto done; 281 } 282 cam = NULL; 283 done: 284 mutex_unlock(&cafe_dev_list_lock); 285 return cam; 286} 287 288 289/* ------------------------------------------------------------------------ */ 290/* 291 * Device register I/O 292 */ 293static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg, 294 unsigned int val) 295{ 296 iowrite32(val, cam->regs + reg); 297} 298 299static inline unsigned int cafe_reg_read(struct cafe_camera *cam, 300 unsigned int reg) 301{ 302 return ioread32(cam->regs + reg); 303} 304 305 306static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg, 307 unsigned int val, unsigned int mask) 308{ 309 unsigned int v = cafe_reg_read(cam, reg); 310 311 v = (v & ~mask) | (val & mask); 312 cafe_reg_write(cam, reg, v); 313} 314 315static inline void cafe_reg_clear_bit(struct cafe_camera *cam, 316 unsigned int reg, unsigned int val) 317{ 318 cafe_reg_write_mask(cam, reg, 0, val); 319} 320 321static inline void cafe_reg_set_bit(struct cafe_camera *cam, 322 unsigned int reg, unsigned int val) 323{ 324 cafe_reg_write_mask(cam, reg, val, val); 325} 326 327 328 329/* -------------------------------------------------------------------- */ 330/* 331 * The I2C/SMBUS interface to the camera itself starts here. The 332 * controller handles SMBUS itself, presenting a relatively simple register 333 * interface; all we have to do is to tell it where to route the data. 334 */ 335#define CAFE_SMBUS_TIMEOUT (HZ) /* generous */ 336 337static int cafe_smbus_write_done(struct cafe_camera *cam) 338{ 339 unsigned long flags; 340 int c1; 341 342 /* 343 * We must delay after the interrupt, or the controller gets confused 344 * and never does give us good status. Fortunately, we don't do this 345 * often. 346 */ 347 udelay(20); 348 spin_lock_irqsave(&cam->dev_lock, flags); 349 c1 = cafe_reg_read(cam, REG_TWSIC1); 350 spin_unlock_irqrestore(&cam->dev_lock, flags); 351 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT; 352} 353 354static int cafe_smbus_write_data(struct cafe_camera *cam, 355 u16 addr, u8 command, u8 value) 356{ 357 unsigned int rval; 358 unsigned long flags; 359 360 spin_lock_irqsave(&cam->dev_lock, flags); 361 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID); 362 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */ 363 /* 364 * Marvell sez set clkdiv to all 1's for now. 365 */ 366 rval |= TWSIC0_CLKDIV; 367 cafe_reg_write(cam, REG_TWSIC0, rval); 368 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */ 369 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR); 370 cafe_reg_write(cam, REG_TWSIC1, rval); 371 spin_unlock_irqrestore(&cam->dev_lock, flags); 372 msleep(2); /* Required or things flake */ 373 374 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam), 375 CAFE_SMBUS_TIMEOUT); 376 spin_lock_irqsave(&cam->dev_lock, flags); 377 rval = cafe_reg_read(cam, REG_TWSIC1); 378 spin_unlock_irqrestore(&cam->dev_lock, flags); 379 380 if (rval & TWSIC1_WSTAT) { 381 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr, 382 command, value); 383 return -EIO; 384 } 385 if (rval & TWSIC1_ERROR) { 386 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr, 387 command, value); 388 return -EIO; 389 } 390 return 0; 391} 392 393 394 395static int cafe_smbus_read_done(struct cafe_camera *cam) 396{ 397 unsigned long flags; 398 int c1; 399 400 /* 401 * We must delay after the interrupt, or the controller gets confused 402 * and never does give us good status. Fortunately, we don't do this 403 * often. 404 */ 405 udelay(20); 406 spin_lock_irqsave(&cam->dev_lock, flags); 407 c1 = cafe_reg_read(cam, REG_TWSIC1); 408 spin_unlock_irqrestore(&cam->dev_lock, flags); 409 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR); 410} 411 412 413 414static int cafe_smbus_read_data(struct cafe_camera *cam, 415 u16 addr, u8 command, u8 *value) 416{ 417 unsigned int rval; 418 unsigned long flags; 419 420 spin_lock_irqsave(&cam->dev_lock, flags); 421 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID); 422 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */ 423 /* 424 * Marvel sez set clkdiv to all 1's for now. 425 */ 426 rval |= TWSIC0_CLKDIV; 427 cafe_reg_write(cam, REG_TWSIC0, rval); 428 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */ 429 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR); 430 cafe_reg_write(cam, REG_TWSIC1, rval); 431 spin_unlock_irqrestore(&cam->dev_lock, flags); 432 433 wait_event_timeout(cam->smbus_wait, 434 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT); 435 spin_lock_irqsave(&cam->dev_lock, flags); 436 rval = cafe_reg_read(cam, REG_TWSIC1); 437 spin_unlock_irqrestore(&cam->dev_lock, flags); 438 439 if (rval & TWSIC1_ERROR) { 440 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command); 441 return -EIO; 442 } 443 if (! (rval & TWSIC1_RVALID)) { 444 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr, 445 command); 446 return -EIO; 447 } 448 *value = rval & 0xff; 449 return 0; 450} 451 452/* 453 * Perform a transfer over SMBUS. This thing is called under 454 * the i2c bus lock, so we shouldn't race with ourselves... 455 */ 456static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 457 unsigned short flags, char rw, u8 command, 458 int size, union i2c_smbus_data *data) 459{ 460 struct cafe_camera *cam = i2c_get_adapdata(adapter); 461 int ret = -EINVAL; 462 463 /* 464 * Refuse to talk to anything but OV cam chips. We should 465 * never even see an attempt to do so, but one never knows. 466 */ 467 if (cam->sensor && addr != cam->sensor->addr) { 468 cam_err(cam, "funky smbus addr %d\n", addr); 469 return -EINVAL; 470 } 471 /* 472 * This interface would appear to only do byte data ops. OK 473 * it can do word too, but the cam chip has no use for that. 474 */ 475 if (size != I2C_SMBUS_BYTE_DATA) { 476 cam_err(cam, "funky xfer size %d\n", size); 477 return -EINVAL; 478 } 479 480 if (rw == I2C_SMBUS_WRITE) 481 ret = cafe_smbus_write_data(cam, addr, command, data->byte); 482 else if (rw == I2C_SMBUS_READ) 483 ret = cafe_smbus_read_data(cam, addr, command, &data->byte); 484 return ret; 485} 486 487 488static void cafe_smbus_enable_irq(struct cafe_camera *cam) 489{ 490 unsigned long flags; 491 492 spin_lock_irqsave(&cam->dev_lock, flags); 493 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS); 494 spin_unlock_irqrestore(&cam->dev_lock, flags); 495} 496 497static u32 cafe_smbus_func(struct i2c_adapter *adapter) 498{ 499 return I2C_FUNC_SMBUS_READ_BYTE_DATA | 500 I2C_FUNC_SMBUS_WRITE_BYTE_DATA; 501} 502 503static struct i2c_algorithm cafe_smbus_algo = { 504 .smbus_xfer = cafe_smbus_xfer, 505 .functionality = cafe_smbus_func 506}; 507 508/* Somebody is on the bus */ 509static int cafe_cam_init(struct cafe_camera *cam); 510static void cafe_ctlr_stop_dma(struct cafe_camera *cam); 511static void cafe_ctlr_power_down(struct cafe_camera *cam); 512 513static int cafe_smbus_attach(struct i2c_client *client) 514{ 515 struct cafe_camera *cam = i2c_get_adapdata(client->adapter); 516 517 /* 518 * Don't talk to chips we don't recognize. 519 */ 520 if (client->driver->id == I2C_DRIVERID_OV7670) { 521 cam->sensor = client; 522 return cafe_cam_init(cam); 523 } 524 return -EINVAL; 525} 526 527static int cafe_smbus_detach(struct i2c_client *client) 528{ 529 struct cafe_camera *cam = i2c_get_adapdata(client->adapter); 530 531 if (cam->sensor == client) { 532 cafe_ctlr_stop_dma(cam); 533 cafe_ctlr_power_down(cam); 534 cam_err(cam, "lost the sensor!\n"); 535 cam->sensor = NULL; /* Bummer, no camera */ 536 cam->state = S_NOTREADY; 537 } 538 return 0; 539} 540 541static int cafe_smbus_setup(struct cafe_camera *cam) 542{ 543 struct i2c_adapter *adap = &cam->i2c_adapter; 544 int ret; 545 546 cafe_smbus_enable_irq(cam); 547 adap->id = I2C_HW_SMBUS_CAFE; 548 adap->class = I2C_CLASS_CAM_DIGITAL; 549 adap->owner = THIS_MODULE; 550 adap->client_register = cafe_smbus_attach; 551 adap->client_unregister = cafe_smbus_detach; 552 adap->algo = &cafe_smbus_algo; 553 strcpy(adap->name, "cafe_ccic"); 554 adap->dev.parent = &cam->pdev->dev; 555 i2c_set_adapdata(adap, cam); 556 ret = i2c_add_adapter(adap); 557 if (ret) 558 printk(KERN_ERR "Unable to register cafe i2c adapter\n"); 559 return ret; 560} 561 562static void cafe_smbus_shutdown(struct cafe_camera *cam) 563{ 564 i2c_del_adapter(&cam->i2c_adapter); 565} 566 567 568/* ------------------------------------------------------------------- */ 569/* 570 * Deal with the controller. 571 */ 572 573/* 574 * Do everything we think we need to have the interface operating 575 * according to the desired format. 576 */ 577static void cafe_ctlr_dma(struct cafe_camera *cam) 578{ 579 /* 580 * Store the first two Y buffers (we aren't supporting 581 * planar formats for now, so no UV bufs). Then either 582 * set the third if it exists, or tell the controller 583 * to just use two. 584 */ 585 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]); 586 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]); 587 if (cam->nbufs > 2) { 588 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]); 589 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS); 590 } 591 else 592 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS); 593 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */ 594} 595 596static void cafe_ctlr_image(struct cafe_camera *cam) 597{ 598 int imgsz; 599 struct v4l2_pix_format *fmt = &cam->pix_format; 600 601 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) | 602 (fmt->bytesperline & IMGSZ_H_MASK); 603 cafe_reg_write(cam, REG_IMGSIZE, imgsz); 604 cafe_reg_write(cam, REG_IMGOFFSET, 0); 605 /* YPITCH just drops the last two bits */ 606 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline, 607 IMGP_YP_MASK); 608 /* 609 * Tell the controller about the image format we are using. 610 */ 611 switch (cam->pix_format.pixelformat) { 612 case V4L2_PIX_FMT_YUYV: 613 cafe_reg_write_mask(cam, REG_CTRL0, 614 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV, 615 C0_DF_MASK); 616 break; 617 618 case V4L2_PIX_FMT_RGB444: 619 cafe_reg_write_mask(cam, REG_CTRL0, 620 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB, 621 C0_DF_MASK); 622 /* Alpha value? */ 623 break; 624 625 case V4L2_PIX_FMT_RGB565: 626 cafe_reg_write_mask(cam, REG_CTRL0, 627 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR, 628 C0_DF_MASK); 629 break; 630 631 default: 632 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat); 633 break; 634 } 635 /* 636 * Make sure it knows we want to use hsync/vsync. 637 */ 638 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, 639 C0_SIFM_MASK); 640} 641 642 643/* 644 * Configure the controller for operation; caller holds the 645 * device mutex. 646 */ 647static int cafe_ctlr_configure(struct cafe_camera *cam) 648{ 649 unsigned long flags; 650 651 spin_lock_irqsave(&cam->dev_lock, flags); 652 cafe_ctlr_dma(cam); 653 cafe_ctlr_image(cam); 654 cafe_set_config_needed(cam, 0); 655 spin_unlock_irqrestore(&cam->dev_lock, flags); 656 return 0; 657} 658 659static void cafe_ctlr_irq_enable(struct cafe_camera *cam) 660{ 661 /* 662 * Clear any pending interrupts, since we do not 663 * expect to have I/O active prior to enabling. 664 */ 665 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); 666 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS); 667} 668 669static void cafe_ctlr_irq_disable(struct cafe_camera *cam) 670{ 671 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS); 672} 673 674/* 675 * Make the controller start grabbing images. Everything must 676 * be set up before doing this. 677 */ 678static void cafe_ctlr_start(struct cafe_camera *cam) 679{ 680 /* set_bit performs a read, so no other barrier should be 681 needed here */ 682 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE); 683} 684 685static void cafe_ctlr_stop(struct cafe_camera *cam) 686{ 687 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); 688} 689 690static void cafe_ctlr_init(struct cafe_camera *cam) 691{ 692 unsigned long flags; 693 694 spin_lock_irqsave(&cam->dev_lock, flags); 695 /* 696 * Added magic to bring up the hardware on the B-Test board 697 */ 698 cafe_reg_write(cam, 0x3038, 0x8); 699 cafe_reg_write(cam, 0x315c, 0x80008); 700 /* 701 * Go through the dance needed to wake the device up. 702 * Note that these registers are global and shared 703 * with the NAND and SD devices. Interaction between the 704 * three still needs to be examined. 705 */ 706 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */ 707 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC); 708 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS); 709 /* 710 * Here we must wait a bit for the controller to come around. 711 */ 712 spin_unlock_irqrestore(&cam->dev_lock, flags); 713 mdelay(5); 714 spin_lock_irqsave(&cam->dev_lock, flags); 715 716 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC); 717 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN); 718 /* 719 * Make sure it's not powered down. 720 */ 721 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); 722 /* 723 * Turn off the enable bit. It sure should be off anyway, 724 * but it's good to be sure. 725 */ 726 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); 727 /* 728 * Mask all interrupts. 729 */ 730 cafe_reg_write(cam, REG_IRQMASK, 0); 731 /* 732 * Clock the sensor appropriately. Controller clock should 733 * be 48MHz, sensor "typical" value is half that. 734 */ 735 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK); 736 spin_unlock_irqrestore(&cam->dev_lock, flags); 737} 738 739 740/* 741 * Stop the controller, and don't return until we're really sure that no 742 * further DMA is going on. 743 */ 744static void cafe_ctlr_stop_dma(struct cafe_camera *cam) 745{ 746 unsigned long flags; 747 748 /* 749 * Theory: stop the camera controller (whether it is operating 750 * or not). Delay briefly just in case we race with the SOF 751 * interrupt, then wait until no DMA is active. 752 */ 753 spin_lock_irqsave(&cam->dev_lock, flags); 754 cafe_ctlr_stop(cam); 755 spin_unlock_irqrestore(&cam->dev_lock, flags); 756 mdelay(1); 757 wait_event_timeout(cam->iowait, 758 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ); 759 if (test_bit(CF_DMA_ACTIVE, &cam->flags)) 760 cam_err(cam, "Timeout waiting for DMA to end\n"); 761 /* This would be bad news - what now? */ 762 spin_lock_irqsave(&cam->dev_lock, flags); 763 cam->state = S_IDLE; 764 cafe_ctlr_irq_disable(cam); 765 spin_unlock_irqrestore(&cam->dev_lock, flags); 766} 767 768/* 769 * Power up and down. 770 */ 771static void cafe_ctlr_power_up(struct cafe_camera *cam) 772{ 773 unsigned long flags; 774 775 spin_lock_irqsave(&cam->dev_lock, flags); 776 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); 777 /* 778 * Part one of the sensor dance: turn the global 779 * GPIO signal on. 780 */ 781 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON); 782 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL); 783 /* 784 * Put the sensor into operational mode (assumes OLPC-style 785 * wiring). Control 0 is reset - set to 1 to operate. 786 * Control 1 is power down, set to 0 to operate. 787 */ 788 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */ 789// mdelay(1); /* Marvell says 1ms will do it */ 790 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0); 791// mdelay(1); /* Enough? */ 792 spin_unlock_irqrestore(&cam->dev_lock, flags); 793 msleep(5); /* Just to be sure */ 794} 795 796static void cafe_ctlr_power_down(struct cafe_camera *cam) 797{ 798 unsigned long flags; 799 800 spin_lock_irqsave(&cam->dev_lock, flags); 801 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1); 802 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON); 803 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT); 804 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN); 805 spin_unlock_irqrestore(&cam->dev_lock, flags); 806} 807 808/* -------------------------------------------------------------------- */ 809/* 810 * Communications with the sensor. 811 */ 812 813static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg) 814{ 815 struct i2c_client *sc = cam->sensor; 816 int ret; 817 818 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL) 819 return -EINVAL; 820 ret = sc->driver->command(sc, cmd, arg); 821 if (ret == -EPERM) /* Unsupported command */ 822 return 0; 823 return ret; 824} 825 826static int __cafe_cam_reset(struct cafe_camera *cam) 827{ 828 int zero = 0; 829 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero); 830} 831 832/* 833 * We have found the sensor on the i2c. Let's try to have a 834 * conversation. 835 */ 836static int cafe_cam_init(struct cafe_camera *cam) 837{ 838 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 }; 839 int ret; 840 841 mutex_lock(&cam->s_mutex); 842 if (cam->state != S_NOTREADY) 843 cam_warn(cam, "Cam init with device in funky state %d", 844 cam->state); 845 ret = __cafe_cam_reset(cam); 846 if (ret) 847 goto out; 848 chip.match_chip = cam->sensor->addr; 849 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip); 850 if (ret) 851 goto out; 852 cam->sensor_type = chip.ident; 853// if (cam->sensor->addr != OV7xx0_SID) { 854 if (cam->sensor_type != V4L2_IDENT_OV7670) { 855 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr); 856 ret = -EINVAL; 857 goto out; 858 } 859/* Get/set parameters? */ 860 ret = 0; 861 cam->state = S_IDLE; 862 out: 863 cafe_ctlr_power_down(cam); 864 mutex_unlock(&cam->s_mutex); 865 return ret; 866} 867 868/* 869 * Configure the sensor to match the parameters we have. Caller should 870 * hold s_mutex 871 */ 872static int cafe_cam_set_flip(struct cafe_camera *cam) 873{ 874 struct v4l2_control ctrl; 875 876 memset(&ctrl, 0, sizeof(ctrl)); 877 ctrl.id = V4L2_CID_VFLIP; 878 ctrl.value = flip; 879 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl); 880} 881 882 883static int cafe_cam_configure(struct cafe_camera *cam) 884{ 885 struct v4l2_format fmt; 886 int ret, zero = 0; 887 888 if (cam->state != S_IDLE) 889 return -EINVAL; 890 fmt.fmt.pix = cam->pix_format; 891 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero); 892 if (ret == 0) 893 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt); 894 /* 895 * OV7670 does weird things if flip is set *before* format... 896 */ 897 ret += cafe_cam_set_flip(cam); 898 return ret; 899} 900 901/* -------------------------------------------------------------------- */ 902/* 903 * DMA buffer management. These functions need s_mutex held. 904 */ 905 906static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime) 907{ 908 int i; 909 910 cafe_set_config_needed(cam, 1); 911 if (loadtime) 912 cam->dma_buf_size = dma_buf_size; 913 else 914 cam->dma_buf_size = cam->pix_format.sizeimage; 915 if (n_dma_bufs > 3) 916 n_dma_bufs = 3; 917 918 cam->nbufs = 0; 919 for (i = 0; i < n_dma_bufs; i++) { 920 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev, 921 cam->dma_buf_size, cam->dma_handles + i, 922 GFP_KERNEL); 923 if (cam->dma_bufs[i] == NULL) { 924 cam_warn(cam, "Failed to allocate DMA buffer\n"); 925 break; 926 } 927 /* For debug, remove eventually */ 928 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size); 929 (cam->nbufs)++; 930 } 931 932 switch (cam->nbufs) { 933 case 1: 934 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size, 935 cam->dma_bufs[0], cam->dma_handles[0]); 936 cam->nbufs = 0; 937 case 0: 938 cam_err(cam, "Insufficient DMA buffers, cannot operate\n"); 939 return -ENOMEM; 940 941 case 2: 942 if (n_dma_bufs > 2) 943 cam_warn(cam, "Will limp along with only 2 buffers\n"); 944 break; 945 } 946 return 0; 947} 948 949static void cafe_free_dma_bufs(struct cafe_camera *cam) 950{ 951 int i; 952 953 for (i = 0; i < cam->nbufs; i++) { 954 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size, 955 cam->dma_bufs[i], cam->dma_handles[i]); 956 cam->dma_bufs[i] = NULL; 957 } 958 cam->nbufs = 0; 959} 960 961 962 963 964 965/* ----------------------------------------------------------------------- */ 966/* 967 * Here starts the V4L2 interface code. 968 */ 969 970/* 971 * Read an image from the device. 972 */ 973static ssize_t cafe_deliver_buffer(struct cafe_camera *cam, 974 char __user *buffer, size_t len, loff_t *pos) 975{ 976 int bufno; 977 unsigned long flags; 978 979 spin_lock_irqsave(&cam->dev_lock, flags); 980 if (cam->next_buf < 0) { 981 cam_err(cam, "deliver_buffer: No next buffer\n"); 982 spin_unlock_irqrestore(&cam->dev_lock, flags); 983 return -EIO; 984 } 985 bufno = cam->next_buf; 986 clear_bit(bufno, &cam->flags); 987 if (++(cam->next_buf) >= cam->nbufs) 988 cam->next_buf = 0; 989 if (! test_bit(cam->next_buf, &cam->flags)) 990 cam->next_buf = -1; 991 cam->specframes = 0; 992 spin_unlock_irqrestore(&cam->dev_lock, flags); 993 994 if (len > cam->pix_format.sizeimage) 995 len = cam->pix_format.sizeimage; 996 if (copy_to_user(buffer, cam->dma_bufs[bufno], len)) 997 return -EFAULT; 998 (*pos) += len; 999 return len; 1000} 1001 1002/* 1003 * Get everything ready, and start grabbing frames. 1004 */ 1005static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state) 1006{ 1007 int ret; 1008 unsigned long flags; 1009 1010 /* 1011 * Configuration. If we still don't have DMA buffers, 1012 * make one last, desperate attempt. 1013 */ 1014 if (cam->nbufs == 0) 1015 if (cafe_alloc_dma_bufs(cam, 0)) 1016 return -ENOMEM; 1017 1018 if (cafe_needs_config(cam)) { 1019 cafe_cam_configure(cam); 1020 ret = cafe_ctlr_configure(cam); 1021 if (ret) 1022 return ret; 1023 } 1024 1025 /* 1026 * Turn it loose. 1027 */ 1028 spin_lock_irqsave(&cam->dev_lock, flags); 1029 cafe_reset_buffers(cam); 1030 cafe_ctlr_irq_enable(cam); 1031 cam->state = state; 1032 cafe_ctlr_start(cam); 1033 spin_unlock_irqrestore(&cam->dev_lock, flags); 1034 return 0; 1035} 1036 1037 1038static ssize_t cafe_v4l_read(struct file *filp, 1039 char __user *buffer, size_t len, loff_t *pos) 1040{ 1041 struct cafe_camera *cam = filp->private_data; 1042 int ret = 0; 1043 1044 /* 1045 * Perhaps we're in speculative read mode and already 1046 * have data? 1047 */ 1048 mutex_lock(&cam->s_mutex); 1049 if (cam->state == S_SPECREAD) { 1050 if (cam->next_buf >= 0) { 1051 ret = cafe_deliver_buffer(cam, buffer, len, pos); 1052 if (ret != 0) 1053 goto out_unlock; 1054 } 1055 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) { 1056 ret = -EIO; 1057 goto out_unlock; 1058 } else if (cam->state != S_IDLE) { 1059 ret = -EBUSY; 1060 goto out_unlock; 1061 } 1062 1063 /* 1064 * v4l2: multiple processes can open the device, but only 1065 * one gets to grab data from it. 1066 */ 1067 if (cam->owner && cam->owner != filp) { 1068 ret = -EBUSY; 1069 goto out_unlock; 1070 } 1071 cam->owner = filp; 1072 1073 /* 1074 * Do setup if need be. 1075 */ 1076 if (cam->state != S_SPECREAD) { 1077 ret = cafe_read_setup(cam, S_SINGLEREAD); 1078 if (ret) 1079 goto out_unlock; 1080 } 1081 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ); 1082 if (cam->next_buf < 0) { 1083 cam_err(cam, "read() operation timed out\n"); 1084 cafe_ctlr_stop_dma(cam); 1085 ret = -EIO; 1086 goto out_unlock; 1087 } 1088 /* 1089 * Give them their data and we should be done. 1090 */ 1091 ret = cafe_deliver_buffer(cam, buffer, len, pos); 1092 1093 out_unlock: 1094 mutex_unlock(&cam->s_mutex); 1095 return ret; 1096} 1097 1098 1099 1100 1101 1102 1103 1104 1105/* 1106 * Streaming I/O support. 1107 */ 1108 1109 1110 1111static int cafe_vidioc_streamon(struct file *filp, void *priv, 1112 enum v4l2_buf_type type) 1113{ 1114 struct cafe_camera *cam = filp->private_data; 1115 int ret = -EINVAL; 1116 1117 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1118 goto out; 1119 mutex_lock(&cam->s_mutex); 1120 if (cam->state != S_IDLE || cam->n_sbufs == 0) 1121 goto out_unlock; 1122 1123 cam->sequence = 0; 1124 ret = cafe_read_setup(cam, S_STREAMING); 1125 1126 out_unlock: 1127 mutex_unlock(&cam->s_mutex); 1128 out: 1129 return ret; 1130} 1131 1132 1133static int cafe_vidioc_streamoff(struct file *filp, void *priv, 1134 enum v4l2_buf_type type) 1135{ 1136 struct cafe_camera *cam = filp->private_data; 1137 int ret = -EINVAL; 1138 1139 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1140 goto out; 1141 mutex_lock(&cam->s_mutex); 1142 if (cam->state != S_STREAMING) 1143 goto out_unlock; 1144 1145 cafe_ctlr_stop_dma(cam); 1146 ret = 0; 1147 1148 out_unlock: 1149 mutex_unlock(&cam->s_mutex); 1150 out: 1151 return ret; 1152} 1153 1154 1155 1156static int cafe_setup_siobuf(struct cafe_camera *cam, int index) 1157{ 1158 struct cafe_sio_buffer *buf = cam->sb_bufs + index; 1159 1160 INIT_LIST_HEAD(&buf->list); 1161 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage); 1162 buf->buffer = vmalloc_user(buf->v4lbuf.length); 1163 if (buf->buffer == NULL) 1164 return -ENOMEM; 1165 buf->mapcount = 0; 1166 buf->cam = cam; 1167 1168 buf->v4lbuf.index = index; 1169 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1170 buf->v4lbuf.field = V4L2_FIELD_NONE; 1171 buf->v4lbuf.memory = V4L2_MEMORY_MMAP; 1172 /* 1173 * Offset: must be 32-bit even on a 64-bit system. video-buf 1174 * just uses the length times the index, but the spec warns 1175 * against doing just that - vma merging problems. So we 1176 * leave a gap between each pair of buffers. 1177 */ 1178 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length; 1179 return 0; 1180} 1181 1182static int cafe_free_sio_buffers(struct cafe_camera *cam) 1183{ 1184 int i; 1185 1186 /* 1187 * If any buffers are mapped, we cannot free them at all. 1188 */ 1189 for (i = 0; i < cam->n_sbufs; i++) 1190 if (cam->sb_bufs[i].mapcount > 0) 1191 return -EBUSY; 1192 /* 1193 * OK, let's do it. 1194 */ 1195 for (i = 0; i < cam->n_sbufs; i++) 1196 vfree(cam->sb_bufs[i].buffer); 1197 cam->n_sbufs = 0; 1198 kfree(cam->sb_bufs); 1199 cam->sb_bufs = NULL; 1200 INIT_LIST_HEAD(&cam->sb_avail); 1201 INIT_LIST_HEAD(&cam->sb_full); 1202 return 0; 1203} 1204 1205 1206 1207static int cafe_vidioc_reqbufs(struct file *filp, void *priv, 1208 struct v4l2_requestbuffers *req) 1209{ 1210 struct cafe_camera *cam = filp->private_data; 1211 int ret = 0; /* Silence warning */ 1212 1213 /* 1214 * Make sure it's something we can do. User pointers could be 1215 * implemented without great pain, but that's not been done yet. 1216 */ 1217 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1218 return -EINVAL; 1219 if (req->memory != V4L2_MEMORY_MMAP) 1220 return -EINVAL; 1221 /* 1222 * If they ask for zero buffers, they really want us to stop streaming 1223 * (if it's happening) and free everything. Should we check owner? 1224 */ 1225 mutex_lock(&cam->s_mutex); 1226 if (req->count == 0) { 1227 if (cam->state == S_STREAMING) 1228 cafe_ctlr_stop_dma(cam); 1229 ret = cafe_free_sio_buffers (cam); 1230 goto out; 1231 } 1232 /* 1233 * Device needs to be idle and working. We *could* try to do the 1234 * right thing in S_SPECREAD by shutting things down, but it 1235 * probably doesn't matter. 1236 */ 1237 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) { 1238 ret = -EBUSY; 1239 goto out; 1240 } 1241 cam->owner = filp; 1242 1243 if (req->count < min_buffers) 1244 req->count = min_buffers; 1245 else if (req->count > max_buffers) 1246 req->count = max_buffers; 1247 if (cam->n_sbufs > 0) { 1248 ret = cafe_free_sio_buffers(cam); 1249 if (ret) 1250 goto out; 1251 } 1252 1253 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer), 1254 GFP_KERNEL); 1255 if (cam->sb_bufs == NULL) { 1256 ret = -ENOMEM; 1257 goto out; 1258 } 1259 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) { 1260 ret = cafe_setup_siobuf(cam, cam->n_sbufs); 1261 if (ret) 1262 break; 1263 } 1264 1265 if (cam->n_sbufs == 0) /* no luck at all - ret already set */ 1266 kfree(cam->sb_bufs); 1267 req->count = cam->n_sbufs; /* In case of partial success */ 1268 1269 out: 1270 mutex_unlock(&cam->s_mutex); 1271 return ret; 1272} 1273 1274 1275static int cafe_vidioc_querybuf(struct file *filp, void *priv, 1276 struct v4l2_buffer *buf) 1277{ 1278 struct cafe_camera *cam = filp->private_data; 1279 int ret = -EINVAL; 1280 1281 mutex_lock(&cam->s_mutex); 1282 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1283 goto out; 1284 if (buf->index < 0 || buf->index >= cam->n_sbufs) 1285 goto out; 1286 *buf = cam->sb_bufs[buf->index].v4lbuf; 1287 ret = 0; 1288 out: 1289 mutex_unlock(&cam->s_mutex); 1290 return ret; 1291} 1292 1293static int cafe_vidioc_qbuf(struct file *filp, void *priv, 1294 struct v4l2_buffer *buf) 1295{ 1296 struct cafe_camera *cam = filp->private_data; 1297 struct cafe_sio_buffer *sbuf; 1298 int ret = -EINVAL; 1299 unsigned long flags; 1300 1301 mutex_lock(&cam->s_mutex); 1302 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1303 goto out; 1304 if (buf->index < 0 || buf->index >= cam->n_sbufs) 1305 goto out; 1306 sbuf = cam->sb_bufs + buf->index; 1307 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) { 1308 ret = 0; /* Already queued?? */ 1309 goto out; 1310 } 1311 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) { 1312 /* Spec doesn't say anything, seems appropriate tho */ 1313 ret = -EBUSY; 1314 goto out; 1315 } 1316 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED; 1317 spin_lock_irqsave(&cam->dev_lock, flags); 1318 list_add(&sbuf->list, &cam->sb_avail); 1319 spin_unlock_irqrestore(&cam->dev_lock, flags); 1320 ret = 0; 1321 out: 1322 mutex_unlock(&cam->s_mutex); 1323 return ret; 1324} 1325 1326static int cafe_vidioc_dqbuf(struct file *filp, void *priv, 1327 struct v4l2_buffer *buf) 1328{ 1329 struct cafe_camera *cam = filp->private_data; 1330 struct cafe_sio_buffer *sbuf; 1331 int ret = -EINVAL; 1332 unsigned long flags; 1333 1334 mutex_lock(&cam->s_mutex); 1335 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1336 goto out_unlock; 1337 if (cam->state != S_STREAMING) 1338 goto out_unlock; 1339 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) { 1340 ret = -EAGAIN; 1341 goto out_unlock; 1342 } 1343 1344 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) { 1345 mutex_unlock(&cam->s_mutex); 1346 if (wait_event_interruptible(cam->iowait, 1347 !list_empty(&cam->sb_full))) { 1348 ret = -ERESTARTSYS; 1349 goto out; 1350 } 1351 mutex_lock(&cam->s_mutex); 1352 } 1353 1354 if (cam->state != S_STREAMING) 1355 ret = -EINTR; 1356 else { 1357 spin_lock_irqsave(&cam->dev_lock, flags); 1358 /* Should probably recheck !list_empty() here */ 1359 sbuf = list_entry(cam->sb_full.next, 1360 struct cafe_sio_buffer, list); 1361 list_del_init(&sbuf->list); 1362 spin_unlock_irqrestore(&cam->dev_lock, flags); 1363 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE; 1364 *buf = sbuf->v4lbuf; 1365 ret = 0; 1366 } 1367 1368 out_unlock: 1369 mutex_unlock(&cam->s_mutex); 1370 out: 1371 return ret; 1372} 1373 1374 1375 1376static void cafe_v4l_vm_open(struct vm_area_struct *vma) 1377{ 1378 struct cafe_sio_buffer *sbuf = vma->vm_private_data; 1379 /* 1380 * Locking: done under mmap_sem, so we don't need to 1381 * go back to the camera lock here. 1382 */ 1383 sbuf->mapcount++; 1384} 1385 1386 1387static void cafe_v4l_vm_close(struct vm_area_struct *vma) 1388{ 1389 struct cafe_sio_buffer *sbuf = vma->vm_private_data; 1390 1391 mutex_lock(&sbuf->cam->s_mutex); 1392 sbuf->mapcount--; 1393 /* Docs say we should stop I/O too... */ 1394 if (sbuf->mapcount == 0) 1395 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED; 1396 mutex_unlock(&sbuf->cam->s_mutex); 1397} 1398 1399static struct vm_operations_struct cafe_v4l_vm_ops = { 1400 .open = cafe_v4l_vm_open, 1401 .close = cafe_v4l_vm_close 1402}; 1403 1404 1405static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma) 1406{ 1407 struct cafe_camera *cam = filp->private_data; 1408 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; 1409 int ret = -EINVAL; 1410 int i; 1411 struct cafe_sio_buffer *sbuf = NULL; 1412 1413 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED)) 1414 return -EINVAL; 1415 /* 1416 * Find the buffer they are looking for. 1417 */ 1418 mutex_lock(&cam->s_mutex); 1419 for (i = 0; i < cam->n_sbufs; i++) 1420 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) { 1421 sbuf = cam->sb_bufs + i; 1422 break; 1423 } 1424 if (sbuf == NULL) 1425 goto out; 1426 1427 ret = remap_vmalloc_range(vma, sbuf->buffer, 0); 1428 if (ret) 1429 goto out; 1430 vma->vm_flags |= VM_DONTEXPAND; 1431 vma->vm_private_data = sbuf; 1432 vma->vm_ops = &cafe_v4l_vm_ops; 1433 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED; 1434 cafe_v4l_vm_open(vma); 1435 ret = 0; 1436 out: 1437 mutex_unlock(&cam->s_mutex); 1438 return ret; 1439} 1440 1441 1442 1443static int cafe_v4l_open(struct inode *inode, struct file *filp) 1444{ 1445 struct cafe_camera *cam; 1446 1447 cam = cafe_find_dev(iminor(inode)); 1448 if (cam == NULL) 1449 return -ENODEV; 1450 filp->private_data = cam; 1451 1452 mutex_lock(&cam->s_mutex); 1453 if (cam->users == 0) { 1454 cafe_ctlr_power_up(cam); 1455 __cafe_cam_reset(cam); 1456 cafe_set_config_needed(cam, 1); 1457 } 1458 (cam->users)++; 1459 mutex_unlock(&cam->s_mutex); 1460 return 0; 1461} 1462 1463 1464static int cafe_v4l_release(struct inode *inode, struct file *filp) 1465{ 1466 struct cafe_camera *cam = filp->private_data; 1467 1468 mutex_lock(&cam->s_mutex); 1469 (cam->users)--; 1470 if (filp == cam->owner) { 1471 cafe_ctlr_stop_dma(cam); 1472 cafe_free_sio_buffers(cam); 1473 cam->owner = NULL; 1474 } 1475 if (cam->users == 0) { 1476 cafe_ctlr_power_down(cam); 1477 if (! alloc_bufs_at_load) 1478 cafe_free_dma_bufs(cam); 1479 } 1480 mutex_unlock(&cam->s_mutex); 1481 return 0; 1482} 1483 1484 1485 1486static unsigned int cafe_v4l_poll(struct file *filp, 1487 struct poll_table_struct *pt) 1488{ 1489 struct cafe_camera *cam = filp->private_data; 1490 1491 poll_wait(filp, &cam->iowait, pt); 1492 if (cam->next_buf >= 0) 1493 return POLLIN | POLLRDNORM; 1494 return 0; 1495} 1496 1497 1498 1499static int cafe_vidioc_queryctrl(struct file *filp, void *priv, 1500 struct v4l2_queryctrl *qc) 1501{ 1502 struct cafe_camera *cam = filp->private_data; 1503 int ret; 1504 1505 mutex_lock(&cam->s_mutex); 1506 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc); 1507 mutex_unlock(&cam->s_mutex); 1508 return ret; 1509} 1510 1511 1512static int cafe_vidioc_g_ctrl(struct file *filp, void *priv, 1513 struct v4l2_control *ctrl) 1514{ 1515 struct cafe_camera *cam = filp->private_data; 1516 int ret; 1517 1518 mutex_lock(&cam->s_mutex); 1519 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl); 1520 mutex_unlock(&cam->s_mutex); 1521 return ret; 1522} 1523 1524 1525static int cafe_vidioc_s_ctrl(struct file *filp, void *priv, 1526 struct v4l2_control *ctrl) 1527{ 1528 struct cafe_camera *cam = filp->private_data; 1529 int ret; 1530 1531 mutex_lock(&cam->s_mutex); 1532 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl); 1533 mutex_unlock(&cam->s_mutex); 1534 return ret; 1535} 1536 1537 1538 1539 1540 1541static int cafe_vidioc_querycap(struct file *file, void *priv, 1542 struct v4l2_capability *cap) 1543{ 1544 strcpy(cap->driver, "cafe_ccic"); 1545 strcpy(cap->card, "cafe_ccic"); 1546 cap->version = CAFE_VERSION; 1547 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | 1548 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 1549 return 0; 1550} 1551 1552 1553/* 1554 * The default format we use until somebody says otherwise. 1555 */ 1556static struct v4l2_pix_format cafe_def_pix_format = { 1557 .width = VGA_WIDTH, 1558 .height = VGA_HEIGHT, 1559 .pixelformat = V4L2_PIX_FMT_YUYV, 1560 .field = V4L2_FIELD_NONE, 1561 .bytesperline = VGA_WIDTH*2, 1562 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2, 1563}; 1564 1565static int cafe_vidioc_enum_fmt_cap(struct file *filp, 1566 void *priv, struct v4l2_fmtdesc *fmt) 1567{ 1568 struct cafe_camera *cam = priv; 1569 int ret; 1570 1571 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1572 return -EINVAL; 1573 mutex_lock(&cam->s_mutex); 1574 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt); 1575 mutex_unlock(&cam->s_mutex); 1576 return ret; 1577} 1578 1579 1580static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv, 1581 struct v4l2_format *fmt) 1582{ 1583 struct cafe_camera *cam = priv; 1584 int ret; 1585 1586 mutex_lock(&cam->s_mutex); 1587 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt); 1588 mutex_unlock(&cam->s_mutex); 1589 return ret; 1590} 1591 1592static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv, 1593 struct v4l2_format *fmt) 1594{ 1595 struct cafe_camera *cam = priv; 1596 int ret; 1597 1598 /* 1599 * Can't do anything if the device is not idle 1600 * Also can't if there are streaming buffers in place. 1601 */ 1602 if (cam->state != S_IDLE || cam->n_sbufs > 0) 1603 return -EBUSY; 1604 /* 1605 * See if the formatting works in principle. 1606 */ 1607 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt); 1608 if (ret) 1609 return ret; 1610 /* 1611 * Now we start to change things for real, so let's do it 1612 * under lock. 1613 */ 1614 mutex_lock(&cam->s_mutex); 1615 cam->pix_format = fmt->fmt.pix; 1616 /* 1617 * Make sure we have appropriate DMA buffers. 1618 */ 1619 ret = -ENOMEM; 1620 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage) 1621 cafe_free_dma_bufs(cam); 1622 if (cam->nbufs == 0) { 1623 if (cafe_alloc_dma_bufs(cam, 0)) 1624 goto out; 1625 } 1626 /* 1627 * It looks like this might work, so let's program the sensor. 1628 */ 1629 ret = cafe_cam_configure(cam); 1630 if (! ret) 1631 ret = cafe_ctlr_configure(cam); 1632 out: 1633 mutex_unlock(&cam->s_mutex); 1634 return ret; 1635} 1636 1637/* 1638 * Return our stored notion of how the camera is/should be configured. 1639 * The V4l2 spec wants us to be smarter, and actually get this from 1640 * the camera (and not mess with it at open time). Someday. 1641 */ 1642static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv, 1643 struct v4l2_format *f) 1644{ 1645 struct cafe_camera *cam = priv; 1646 1647 f->fmt.pix = cam->pix_format; 1648 return 0; 1649} 1650 1651/* 1652 * We only have one input - the sensor - so minimize the nonsense here. 1653 */ 1654static int cafe_vidioc_enum_input(struct file *filp, void *priv, 1655 struct v4l2_input *input) 1656{ 1657 if (input->index != 0) 1658 return -EINVAL; 1659 1660 input->type = V4L2_INPUT_TYPE_CAMERA; 1661 input->std = V4L2_STD_ALL; /* Not sure what should go here */ 1662 strcpy(input->name, "Camera"); 1663 return 0; 1664} 1665 1666static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i) 1667{ 1668 *i = 0; 1669 return 0; 1670} 1671 1672static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i) 1673{ 1674 if (i != 0) 1675 return -EINVAL; 1676 return 0; 1677} 1678 1679/* from vivi.c */ 1680static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a) 1681{ 1682 return 0; 1683} 1684 1685/* 1686 * G/S_PARM. Most of this is done by the sensor, but we are 1687 * the level which controls the number of read buffers. 1688 */ 1689static int cafe_vidioc_g_parm(struct file *filp, void *priv, 1690 struct v4l2_streamparm *parms) 1691{ 1692 struct cafe_camera *cam = priv; 1693 int ret; 1694 1695 mutex_lock(&cam->s_mutex); 1696 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms); 1697 mutex_unlock(&cam->s_mutex); 1698 parms->parm.capture.readbuffers = n_dma_bufs; 1699 return ret; 1700} 1701 1702static int cafe_vidioc_s_parm(struct file *filp, void *priv, 1703 struct v4l2_streamparm *parms) 1704{ 1705 struct cafe_camera *cam = priv; 1706 int ret; 1707 1708 mutex_lock(&cam->s_mutex); 1709 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms); 1710 mutex_unlock(&cam->s_mutex); 1711 parms->parm.capture.readbuffers = n_dma_bufs; 1712 return ret; 1713} 1714 1715 1716static void cafe_v4l_dev_release(struct video_device *vd) 1717{ 1718 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev); 1719 1720 kfree(cam); 1721} 1722 1723 1724/* 1725 * This template device holds all of those v4l2 methods; we 1726 * clone it for specific real devices. 1727 */ 1728 1729static const struct file_operations cafe_v4l_fops = { 1730 .owner = THIS_MODULE, 1731 .open = cafe_v4l_open, 1732 .release = cafe_v4l_release, 1733 .read = cafe_v4l_read, 1734 .poll = cafe_v4l_poll, 1735 .mmap = cafe_v4l_mmap, 1736 .ioctl = video_ioctl2, 1737 .llseek = no_llseek, 1738}; 1739 1740static struct video_device cafe_v4l_template = { 1741 .name = "cafe", 1742 .type = VFL_TYPE_GRABBER, 1743 .type2 = VID_TYPE_CAPTURE, 1744 .minor = -1, /* Get one dynamically */ 1745 .tvnorms = V4L2_STD_NTSC_M, 1746 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */ 1747 1748 .fops = &cafe_v4l_fops, 1749 .release = cafe_v4l_dev_release, 1750 1751 .vidioc_querycap = cafe_vidioc_querycap, 1752 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap, 1753 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap, 1754 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap, 1755 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap, 1756 .vidioc_enum_input = cafe_vidioc_enum_input, 1757 .vidioc_g_input = cafe_vidioc_g_input, 1758 .vidioc_s_input = cafe_vidioc_s_input, 1759 .vidioc_s_std = cafe_vidioc_s_std, 1760 .vidioc_reqbufs = cafe_vidioc_reqbufs, 1761 .vidioc_querybuf = cafe_vidioc_querybuf, 1762 .vidioc_qbuf = cafe_vidioc_qbuf, 1763 .vidioc_dqbuf = cafe_vidioc_dqbuf, 1764 .vidioc_streamon = cafe_vidioc_streamon, 1765 .vidioc_streamoff = cafe_vidioc_streamoff, 1766 .vidioc_queryctrl = cafe_vidioc_queryctrl, 1767 .vidioc_g_ctrl = cafe_vidioc_g_ctrl, 1768 .vidioc_s_ctrl = cafe_vidioc_s_ctrl, 1769 .vidioc_g_parm = cafe_vidioc_g_parm, 1770 .vidioc_s_parm = cafe_vidioc_s_parm, 1771}; 1772 1773 1774 1775 1776 1777 1778 1779/* ---------------------------------------------------------------------- */ 1780/* 1781 * Interrupt handler stuff 1782 */ 1783 1784 1785 1786static void cafe_frame_tasklet(unsigned long data) 1787{ 1788 struct cafe_camera *cam = (struct cafe_camera *) data; 1789 int i; 1790 unsigned long flags; 1791 struct cafe_sio_buffer *sbuf; 1792 1793 spin_lock_irqsave(&cam->dev_lock, flags); 1794 for (i = 0; i < cam->nbufs; i++) { 1795 int bufno = cam->next_buf; 1796 if (bufno < 0) { /* "will never happen" */ 1797 cam_err(cam, "No valid bufs in tasklet!\n"); 1798 break; 1799 } 1800 if (++(cam->next_buf) >= cam->nbufs) 1801 cam->next_buf = 0; 1802 if (! test_bit(bufno, &cam->flags)) 1803 continue; 1804 if (list_empty(&cam->sb_avail)) 1805 break; /* Leave it valid, hope for better later */ 1806 clear_bit(bufno, &cam->flags); 1807 sbuf = list_entry(cam->sb_avail.next, 1808 struct cafe_sio_buffer, list); 1809 /* 1810 * Drop the lock during the big copy. This *should* be safe... 1811 */ 1812 spin_unlock_irqrestore(&cam->dev_lock, flags); 1813 memcpy(sbuf->buffer, cam->dma_bufs[bufno], 1814 cam->pix_format.sizeimage); 1815 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage; 1816 sbuf->v4lbuf.sequence = cam->buf_seq[bufno]; 1817 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED; 1818 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE; 1819 spin_lock_irqsave(&cam->dev_lock, flags); 1820 list_move_tail(&sbuf->list, &cam->sb_full); 1821 } 1822 if (! list_empty(&cam->sb_full)) 1823 wake_up(&cam->iowait); 1824 spin_unlock_irqrestore(&cam->dev_lock, flags); 1825} 1826 1827 1828 1829static void cafe_frame_complete(struct cafe_camera *cam, int frame) 1830{ 1831 /* 1832 * Basic frame housekeeping. 1833 */ 1834 if (test_bit(frame, &cam->flags) && printk_ratelimit()) 1835 cam_err(cam, "Frame overrun on %d, frames lost\n", frame); 1836 set_bit(frame, &cam->flags); 1837 clear_bit(CF_DMA_ACTIVE, &cam->flags); 1838 if (cam->next_buf < 0) 1839 cam->next_buf = frame; 1840 cam->buf_seq[frame] = ++(cam->sequence); 1841 1842 switch (cam->state) { 1843 /* 1844 * If in single read mode, try going speculative. 1845 */ 1846 case S_SINGLEREAD: 1847 cam->state = S_SPECREAD; 1848 cam->specframes = 0; 1849 wake_up(&cam->iowait); 1850 break; 1851 1852 /* 1853 * If we are already doing speculative reads, and nobody is 1854 * reading them, just stop. 1855 */ 1856 case S_SPECREAD: 1857 if (++(cam->specframes) >= cam->nbufs) { 1858 cafe_ctlr_stop(cam); 1859 cafe_ctlr_irq_disable(cam); 1860 cam->state = S_IDLE; 1861 } 1862 wake_up(&cam->iowait); 1863 break; 1864 case S_STREAMING: 1865 tasklet_schedule(&cam->s_tasklet); 1866 break; 1867 1868 default: 1869 cam_err(cam, "Frame interrupt in non-operational state\n"); 1870 break; 1871 } 1872} 1873 1874 1875 1876 1877static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs) 1878{ 1879 unsigned int frame; 1880 1881 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */ 1882 /* 1883 * Handle any frame completions. There really should 1884 * not be more than one of these, or we have fallen 1885 * far behind. 1886 */ 1887 for (frame = 0; frame < cam->nbufs; frame++) 1888 if (irqs & (IRQ_EOF0 << frame)) 1889 cafe_frame_complete(cam, frame); 1890 /* 1891 * If a frame starts, note that we have DMA active. This 1892 * code assumes that we won't get multiple frame interrupts 1893 * at once; may want to rethink that. 1894 */ 1895 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) 1896 set_bit(CF_DMA_ACTIVE, &cam->flags); 1897} 1898 1899 1900 1901static irqreturn_t cafe_irq(int irq, void *data) 1902{ 1903 struct cafe_camera *cam = data; 1904 unsigned int irqs; 1905 1906 spin_lock(&cam->dev_lock); 1907 irqs = cafe_reg_read(cam, REG_IRQSTAT); 1908 if ((irqs & ALLIRQS) == 0) { 1909 spin_unlock(&cam->dev_lock); 1910 return IRQ_NONE; 1911 } 1912 if (irqs & FRAMEIRQS) 1913 cafe_frame_irq(cam, irqs); 1914 if (irqs & TWSIIRQS) { 1915 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS); 1916 wake_up(&cam->smbus_wait); 1917 } 1918 spin_unlock(&cam->dev_lock); 1919 return IRQ_HANDLED; 1920} 1921 1922 1923/* -------------------------------------------------------------------------- */ 1924#ifdef CONFIG_VIDEO_ADV_DEBUG 1925/* 1926 * Debugfs stuff. 1927 */ 1928 1929static char cafe_debug_buf[1024]; 1930static struct dentry *cafe_dfs_root; 1931 1932static void cafe_dfs_setup(void) 1933{ 1934 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL); 1935 if (IS_ERR(cafe_dfs_root)) { 1936 cafe_dfs_root = NULL; /* Never mind */ 1937 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n"); 1938 } 1939} 1940 1941static void cafe_dfs_shutdown(void) 1942{ 1943 if (cafe_dfs_root) 1944 debugfs_remove(cafe_dfs_root); 1945} 1946 1947static int cafe_dfs_open(struct inode *inode, struct file *file) 1948{ 1949 file->private_data = inode->i_private; 1950 return 0; 1951} 1952 1953static ssize_t cafe_dfs_read_regs(struct file *file, 1954 char __user *buf, size_t count, loff_t *ppos) 1955{ 1956 struct cafe_camera *cam = file->private_data; 1957 char *s = cafe_debug_buf; 1958 int offset; 1959 1960 for (offset = 0; offset < 0x44; offset += 4) 1961 s += sprintf(s, "%02x: %08x\n", offset, 1962 cafe_reg_read(cam, offset)); 1963 for (offset = 0x88; offset <= 0x90; offset += 4) 1964 s += sprintf(s, "%02x: %08x\n", offset, 1965 cafe_reg_read(cam, offset)); 1966 for (offset = 0xb4; offset <= 0xbc; offset += 4) 1967 s += sprintf(s, "%02x: %08x\n", offset, 1968 cafe_reg_read(cam, offset)); 1969 for (offset = 0x3000; offset <= 0x300c; offset += 4) 1970 s += sprintf(s, "%04x: %08x\n", offset, 1971 cafe_reg_read(cam, offset)); 1972 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf, 1973 s - cafe_debug_buf); 1974} 1975 1976static const struct file_operations cafe_dfs_reg_ops = { 1977 .owner = THIS_MODULE, 1978 .read = cafe_dfs_read_regs, 1979 .open = cafe_dfs_open 1980}; 1981 1982static ssize_t cafe_dfs_read_cam(struct file *file, 1983 char __user *buf, size_t count, loff_t *ppos) 1984{ 1985 struct cafe_camera *cam = file->private_data; 1986 char *s = cafe_debug_buf; 1987 int offset; 1988 1989 if (! cam->sensor) 1990 return -EINVAL; 1991 for (offset = 0x0; offset < 0x8a; offset++) 1992 { 1993 u8 v; 1994 1995 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v); 1996 s += sprintf(s, "%02x: %02x\n", offset, v); 1997 } 1998 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf, 1999 s - cafe_debug_buf); 2000} 2001 2002static const struct file_operations cafe_dfs_cam_ops = { 2003 .owner = THIS_MODULE, 2004 .read = cafe_dfs_read_cam, 2005 .open = cafe_dfs_open 2006}; 2007 2008 2009 2010static void cafe_dfs_cam_setup(struct cafe_camera *cam) 2011{ 2012 char fname[40]; 2013 2014 if (!cafe_dfs_root) 2015 return; 2016 sprintf(fname, "regs-%d", cam->v4ldev.minor); 2017 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root, 2018 cam, &cafe_dfs_reg_ops); 2019 sprintf(fname, "cam-%d", cam->v4ldev.minor); 2020 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root, 2021 cam, &cafe_dfs_cam_ops); 2022} 2023 2024 2025static void cafe_dfs_cam_shutdown(struct cafe_camera *cam) 2026{ 2027 if (! IS_ERR(cam->dfs_regs)) 2028 debugfs_remove(cam->dfs_regs); 2029 if (! IS_ERR(cam->dfs_cam_regs)) 2030 debugfs_remove(cam->dfs_cam_regs); 2031} 2032 2033#else 2034 2035#define cafe_dfs_setup() 2036#define cafe_dfs_shutdown() 2037#define cafe_dfs_cam_setup(cam) 2038#define cafe_dfs_cam_shutdown(cam) 2039#endif /* CONFIG_VIDEO_ADV_DEBUG */ 2040 2041 2042 2043 2044/* ------------------------------------------------------------------------*/ 2045/* 2046 * PCI interface stuff. 2047 */ 2048 2049static int cafe_pci_probe(struct pci_dev *pdev, 2050 const struct pci_device_id *id) 2051{ 2052 int ret; 2053 u16 classword; 2054 struct cafe_camera *cam; 2055 /* 2056 * Make sure we have a camera here - we'll get calls for 2057 * the other cafe devices as well. 2058 */ 2059 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword); 2060 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO) 2061 return -ENODEV; 2062 /* 2063 * Start putting together one of our big camera structures. 2064 */ 2065 ret = -ENOMEM; 2066 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL); 2067 if (cam == NULL) 2068 goto out; 2069 mutex_init(&cam->s_mutex); 2070 mutex_lock(&cam->s_mutex); 2071 spin_lock_init(&cam->dev_lock); 2072 cam->state = S_NOTREADY; 2073 cafe_set_config_needed(cam, 1); 2074 init_waitqueue_head(&cam->smbus_wait); 2075 init_waitqueue_head(&cam->iowait); 2076 cam->pdev = pdev; 2077 cam->pix_format = cafe_def_pix_format; 2078 INIT_LIST_HEAD(&cam->dev_list); 2079 INIT_LIST_HEAD(&cam->sb_avail); 2080 INIT_LIST_HEAD(&cam->sb_full); 2081 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam); 2082 /* 2083 * Get set up on the PCI bus. 2084 */ 2085 ret = pci_enable_device(pdev); 2086 if (ret) 2087 goto out_free; 2088 pci_set_master(pdev); 2089 2090 ret = -EIO; 2091 cam->regs = pci_iomap(pdev, 0, 0); 2092 if (! cam->regs) { 2093 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n"); 2094 goto out_free; 2095 } 2096 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam); 2097 if (ret) 2098 goto out_iounmap; 2099 /* 2100 * Initialize the controller and leave it powered up. It will 2101 * stay that way until the sensor driver shows up. 2102 */ 2103 cafe_ctlr_init(cam); 2104 cafe_ctlr_power_up(cam); 2105 /* 2106 * Set up I2C/SMBUS communications. We have to drop the mutex here 2107 * because the sensor could attach in this call chain, leading to 2108 * unsightly deadlocks. 2109 */ 2110 mutex_unlock(&cam->s_mutex); /* attach can deadlock */ 2111 ret = cafe_smbus_setup(cam); 2112 if (ret) 2113 goto out_freeirq; 2114 /* 2115 * Get the v4l2 setup done. 2116 */ 2117 mutex_lock(&cam->s_mutex); 2118 cam->v4ldev = cafe_v4l_template; 2119 cam->v4ldev.debug = 0; 2120// cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG; 2121 cam->v4ldev.dev = &pdev->dev; 2122 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1); 2123 if (ret) 2124 goto out_smbus; 2125 /* 2126 * If so requested, try to get our DMA buffers now. 2127 */ 2128 if (alloc_bufs_at_load) { 2129 if (cafe_alloc_dma_bufs(cam, 1)) 2130 cam_warn(cam, "Unable to alloc DMA buffers at load" 2131 " will try again later."); 2132 } 2133 2134 cafe_dfs_cam_setup(cam); 2135 mutex_unlock(&cam->s_mutex); 2136 cafe_add_dev(cam); 2137 return 0; 2138 2139 out_smbus: 2140 cafe_smbus_shutdown(cam); 2141 out_freeirq: 2142 cafe_ctlr_power_down(cam); 2143 free_irq(pdev->irq, cam); 2144 out_iounmap: 2145 pci_iounmap(pdev, cam->regs); 2146 out_free: 2147 kfree(cam); 2148 out: 2149 return ret; 2150} 2151 2152 2153/* 2154 * Shut down an initialized device 2155 */ 2156static void cafe_shutdown(struct cafe_camera *cam) 2157{ 2158 cafe_dfs_cam_shutdown(cam); 2159 if (cam->n_sbufs > 0) 2160 /* What if they are still mapped? Shouldn't be, but... */ 2161 cafe_free_sio_buffers(cam); 2162 cafe_remove_dev(cam); 2163 cafe_ctlr_stop_dma(cam); 2164 cafe_ctlr_power_down(cam); 2165 cafe_smbus_shutdown(cam); 2166 cafe_free_dma_bufs(cam); 2167 free_irq(cam->pdev->irq, cam); 2168 pci_iounmap(cam->pdev, cam->regs); 2169 video_unregister_device(&cam->v4ldev); 2170 /* kfree(cam); done in v4l_release () */ 2171} 2172 2173 2174static void cafe_pci_remove(struct pci_dev *pdev) 2175{ 2176 struct cafe_camera *cam = cafe_find_by_pdev(pdev); 2177 2178 if (cam == NULL) { 2179 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev); 2180 return; 2181 } 2182 mutex_lock(&cam->s_mutex); 2183 if (cam->users > 0) 2184 cam_warn(cam, "Removing a device with users!\n"); 2185 cafe_shutdown(cam); 2186/* No unlock - it no longer exists */ 2187} 2188 2189 2190#ifdef CONFIG_PM 2191/* 2192 * Basic power management. 2193 */ 2194static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state) 2195{ 2196 struct cafe_camera *cam = cafe_find_by_pdev(pdev); 2197 int ret; 2198 2199 ret = pci_save_state(pdev); 2200 if (ret) 2201 return ret; 2202 cafe_ctlr_stop_dma(cam); 2203 cafe_ctlr_power_down(cam); 2204 pci_disable_device(pdev); 2205 return 0; 2206} 2207 2208 2209static int cafe_pci_resume(struct pci_dev *pdev) 2210{ 2211 struct cafe_camera *cam = cafe_find_by_pdev(pdev); 2212 int ret = 0; 2213 2214 ret = pci_restore_state(pdev); 2215 if (ret) 2216 return ret; 2217 ret = pci_enable_device(pdev); 2218 if (ret) { 2219 cam_warn(cam, "Unable to re-enable device on resume!\n"); 2220 return ret; 2221 } 2222 cafe_ctlr_init(cam); 2223 cafe_ctlr_power_up(cam); 2224 set_bit(CF_CONFIG_NEEDED, &cam->flags); 2225 if (cam->state == S_SPECREAD) 2226 cam->state = S_IDLE; /* Don't bother restarting */ 2227 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING) 2228 ret = cafe_read_setup(cam, cam->state); 2229 return ret; 2230} 2231 2232#endif /* CONFIG_PM */ 2233 2234 2235static struct pci_device_id cafe_ids[] = { 2236 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */ 2237 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */ 2238 { 0, } 2239}; 2240 2241MODULE_DEVICE_TABLE(pci, cafe_ids); 2242 2243static struct pci_driver cafe_pci_driver = { 2244 .name = "cafe1000-ccic", 2245 .id_table = cafe_ids, 2246 .probe = cafe_pci_probe, 2247 .remove = cafe_pci_remove, 2248#ifdef CONFIG_PM 2249 .suspend = cafe_pci_suspend, 2250 .resume = cafe_pci_resume, 2251#endif 2252}; 2253 2254 2255 2256 2257static int __init cafe_init(void) 2258{ 2259 int ret; 2260 2261 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n", 2262 CAFE_VERSION); 2263 cafe_dfs_setup(); 2264 ret = pci_register_driver(&cafe_pci_driver); 2265 if (ret) { 2266 printk(KERN_ERR "Unable to register cafe_ccic driver\n"); 2267 goto out; 2268 } 2269 request_module("ov7670"); 2270 ret = 0; 2271 2272 out: 2273 return ret; 2274} 2275 2276 2277static void __exit cafe_exit(void) 2278{ 2279 pci_unregister_driver(&cafe_pci_driver); 2280 cafe_dfs_shutdown(); 2281} 2282 2283module_init(cafe_init); 2284module_exit(cafe_exit); 2285