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