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