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