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> |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 34 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 35 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 36 | #include <media/v4l2-chip-ident.h> |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 37 | #include <linux/device.h> |
| 38 | #include <linux/wait.h> |
| 39 | #include <linux/list.h> |
| 40 | #include <linux/dma-mapping.h> |
| 41 | #include <linux/delay.h> |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 42 | #include <linux/jiffies.h> |
| 43 | #include <linux/vmalloc.h> |
| 44 | |
| 45 | #include <asm/uaccess.h> |
| 46 | #include <asm/io.h> |
| 47 | |
| 48 | #include "cafe_ccic-regs.h" |
| 49 | |
Jonathan Corbet | ff68def | 2007-03-25 11:36:28 -0300 | [diff] [blame] | 50 | #define CAFE_VERSION 0x000002 |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 51 | |
| 52 | |
| 53 | /* |
| 54 | * Parameters. |
| 55 | */ |
| 56 | MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>"); |
| 57 | MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver"); |
| 58 | MODULE_LICENSE("GPL"); |
| 59 | MODULE_SUPPORTED_DEVICE("Video"); |
| 60 | |
| 61 | /* |
| 62 | * Internal DMA buffer management. Since the controller cannot do S/G I/O, |
| 63 | * we must have physically contiguous buffers to bring frames into. |
| 64 | * These parameters control how many buffers we use, whether we |
| 65 | * allocate them at load time (better chance of success, but nails down |
| 66 | * memory) or when somebody tries to use the camera (riskier), and, |
| 67 | * for load-time allocation, how big they should be. |
| 68 | * |
| 69 | * The controller can cycle through three buffers. We could use |
| 70 | * more by flipping pointers around, but it probably makes little |
| 71 | * sense. |
| 72 | */ |
| 73 | |
| 74 | #define MAX_DMA_BUFS 3 |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 75 | static int alloc_bufs_at_read; |
Andres Salomon | 23869e2 | 2007-09-19 02:44:18 -0300 | [diff] [blame] | 76 | module_param(alloc_bufs_at_read, bool, 0444); |
| 77 | MODULE_PARM_DESC(alloc_bufs_at_read, |
| 78 | "Non-zero value causes DMA buffers to be allocated when the " |
| 79 | "video capture device is read, rather than at module load " |
| 80 | "time. This saves memory, but decreases the chances of " |
| 81 | "successfully getting those buffers."); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 82 | |
| 83 | static int n_dma_bufs = 3; |
| 84 | module_param(n_dma_bufs, uint, 0644); |
| 85 | MODULE_PARM_DESC(n_dma_bufs, |
| 86 | "The number of DMA buffers to allocate. Can be either two " |
| 87 | "(saves memory, makes timing tighter) or three."); |
| 88 | |
| 89 | static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */ |
| 90 | module_param(dma_buf_size, uint, 0444); |
| 91 | MODULE_PARM_DESC(dma_buf_size, |
| 92 | "The size of the allocated DMA buffers. If actual operating " |
| 93 | "parameters require larger buffers, an attempt to reallocate " |
| 94 | "will be made."); |
| 95 | |
| 96 | static int min_buffers = 1; |
| 97 | module_param(min_buffers, uint, 0644); |
| 98 | MODULE_PARM_DESC(min_buffers, |
| 99 | "The minimum number of streaming I/O buffers we are willing " |
| 100 | "to work with."); |
| 101 | |
| 102 | static int max_buffers = 10; |
| 103 | module_param(max_buffers, uint, 0644); |
| 104 | MODULE_PARM_DESC(max_buffers, |
| 105 | "The maximum number of streaming I/O buffers an application " |
| 106 | "will be allowed to allocate. These buffers are big and live " |
| 107 | "in vmalloc space."); |
| 108 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 109 | static int flip; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 110 | module_param(flip, bool, 0444); |
| 111 | MODULE_PARM_DESC(flip, |
| 112 | "If set, the sensor will be instructed to flip the image " |
| 113 | "vertically."); |
| 114 | |
| 115 | |
| 116 | enum cafe_state { |
| 117 | S_NOTREADY, /* Not yet initialized */ |
| 118 | S_IDLE, /* Just hanging around */ |
| 119 | S_FLAKED, /* Some sort of problem */ |
| 120 | S_SINGLEREAD, /* In read() */ |
| 121 | S_SPECREAD, /* Speculative read (for future read()) */ |
| 122 | S_STREAMING /* Streaming data */ |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Tracking of streaming I/O buffers. |
| 127 | */ |
| 128 | struct cafe_sio_buffer { |
| 129 | struct list_head list; |
| 130 | struct v4l2_buffer v4lbuf; |
| 131 | char *buffer; /* Where it lives in kernel space */ |
| 132 | int mapcount; |
| 133 | struct cafe_camera *cam; |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * A description of one of our devices. |
| 138 | * Locking: controlled by s_mutex. Certain fields, however, require |
| 139 | * the dev_lock spinlock; they are marked as such by comments. |
| 140 | * dev_lock is also required for access to device registers. |
| 141 | */ |
| 142 | struct cafe_camera |
| 143 | { |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 144 | struct v4l2_device v4l2_dev; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 145 | enum cafe_state state; |
| 146 | unsigned long flags; /* Buffer status, mainly (dev_lock) */ |
| 147 | int users; /* How many open FDs */ |
| 148 | struct file *owner; /* Who has data access (v4l2) */ |
| 149 | |
| 150 | /* |
| 151 | * Subsystem structures. |
| 152 | */ |
| 153 | struct pci_dev *pdev; |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 154 | struct video_device vdev; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 155 | struct i2c_adapter i2c_adapter; |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 156 | struct v4l2_subdev *sensor; |
| 157 | unsigned short sensor_addr; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 158 | |
| 159 | unsigned char __iomem *regs; |
| 160 | struct list_head dev_list; /* link to other devices */ |
| 161 | |
| 162 | /* DMA buffers */ |
| 163 | unsigned int nbufs; /* How many are alloc'd */ |
| 164 | int next_buf; /* Next to consume (dev_lock) */ |
| 165 | unsigned int dma_buf_size; /* allocated size */ |
| 166 | void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */ |
| 167 | dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */ |
| 168 | unsigned int specframes; /* Unconsumed spec frames (dev_lock) */ |
| 169 | unsigned int sequence; /* Frame sequence number */ |
| 170 | unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */ |
| 171 | |
| 172 | /* Streaming buffers */ |
| 173 | unsigned int n_sbufs; /* How many we have */ |
| 174 | struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */ |
| 175 | struct list_head sb_avail; /* Available for data (we own) (dev_lock) */ |
| 176 | struct list_head sb_full; /* With data (user space owns) (dev_lock) */ |
| 177 | struct tasklet_struct s_tasklet; |
| 178 | |
| 179 | /* Current operating parameters */ |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 180 | u32 sensor_type; /* Currently ov7670 only */ |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 181 | struct v4l2_pix_format pix_format; |
| 182 | |
| 183 | /* Locks */ |
| 184 | struct mutex s_mutex; /* Access to this structure */ |
| 185 | spinlock_t dev_lock; /* Access to device */ |
| 186 | |
| 187 | /* Misc */ |
| 188 | wait_queue_head_t smbus_wait; /* Waiting on i2c events */ |
| 189 | wait_queue_head_t iowait; /* Waiting on frame data */ |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /* |
| 193 | * Status flags. Always manipulated with bit operations. |
| 194 | */ |
| 195 | #define CF_BUF0_VALID 0 /* Buffers valid - first three */ |
| 196 | #define CF_BUF1_VALID 1 |
| 197 | #define CF_BUF2_VALID 2 |
| 198 | #define CF_DMA_ACTIVE 3 /* A frame is incoming */ |
| 199 | #define CF_CONFIG_NEEDED 4 /* Must configure hardware */ |
| 200 | |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 201 | #define sensor_call(cam, o, f, args...) \ |
| 202 | v4l2_subdev_call(cam->sensor, o, f, ##args) |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 203 | |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 204 | static inline struct cafe_camera *to_cam(struct v4l2_device *dev) |
| 205 | { |
| 206 | return container_of(dev, struct cafe_camera, v4l2_dev); |
| 207 | } |
| 208 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Start over with DMA buffers - dev_lock needed. |
| 212 | */ |
| 213 | static void cafe_reset_buffers(struct cafe_camera *cam) |
| 214 | { |
| 215 | int i; |
| 216 | |
| 217 | cam->next_buf = -1; |
| 218 | for (i = 0; i < cam->nbufs; i++) |
| 219 | clear_bit(i, &cam->flags); |
| 220 | cam->specframes = 0; |
| 221 | } |
| 222 | |
| 223 | static inline int cafe_needs_config(struct cafe_camera *cam) |
| 224 | { |
| 225 | return test_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 226 | } |
| 227 | |
| 228 | static void cafe_set_config_needed(struct cafe_camera *cam, int needed) |
| 229 | { |
| 230 | if (needed) |
| 231 | set_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 232 | else |
| 233 | clear_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | /* |
| 240 | * Debugging and related. |
| 241 | */ |
| 242 | #define cam_err(cam, fmt, arg...) \ |
| 243 | dev_err(&(cam)->pdev->dev, fmt, ##arg); |
| 244 | #define cam_warn(cam, fmt, arg...) \ |
| 245 | dev_warn(&(cam)->pdev->dev, fmt, ##arg); |
| 246 | #define cam_dbg(cam, fmt, arg...) \ |
| 247 | dev_dbg(&(cam)->pdev->dev, fmt, ##arg); |
| 248 | |
| 249 | |
| 250 | /* ---------------------------------------------------------------------*/ |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 251 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 252 | /* |
| 253 | * Device register I/O |
| 254 | */ |
| 255 | static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg, |
| 256 | unsigned int val) |
| 257 | { |
| 258 | iowrite32(val, cam->regs + reg); |
| 259 | } |
| 260 | |
| 261 | static inline unsigned int cafe_reg_read(struct cafe_camera *cam, |
| 262 | unsigned int reg) |
| 263 | { |
| 264 | return ioread32(cam->regs + reg); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg, |
| 269 | unsigned int val, unsigned int mask) |
| 270 | { |
| 271 | unsigned int v = cafe_reg_read(cam, reg); |
| 272 | |
| 273 | v = (v & ~mask) | (val & mask); |
| 274 | cafe_reg_write(cam, reg, v); |
| 275 | } |
| 276 | |
| 277 | static inline void cafe_reg_clear_bit(struct cafe_camera *cam, |
| 278 | unsigned int reg, unsigned int val) |
| 279 | { |
| 280 | cafe_reg_write_mask(cam, reg, 0, val); |
| 281 | } |
| 282 | |
| 283 | static inline void cafe_reg_set_bit(struct cafe_camera *cam, |
| 284 | unsigned int reg, unsigned int val) |
| 285 | { |
| 286 | cafe_reg_write_mask(cam, reg, val, val); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | |
| 291 | /* -------------------------------------------------------------------- */ |
| 292 | /* |
| 293 | * The I2C/SMBUS interface to the camera itself starts here. The |
| 294 | * controller handles SMBUS itself, presenting a relatively simple register |
| 295 | * interface; all we have to do is to tell it where to route the data. |
| 296 | */ |
| 297 | #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */ |
| 298 | |
| 299 | static int cafe_smbus_write_done(struct cafe_camera *cam) |
| 300 | { |
| 301 | unsigned long flags; |
| 302 | int c1; |
| 303 | |
| 304 | /* |
| 305 | * We must delay after the interrupt, or the controller gets confused |
| 306 | * and never does give us good status. Fortunately, we don't do this |
| 307 | * often. |
| 308 | */ |
| 309 | udelay(20); |
| 310 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 311 | c1 = cafe_reg_read(cam, REG_TWSIC1); |
| 312 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 313 | return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT; |
| 314 | } |
| 315 | |
| 316 | static int cafe_smbus_write_data(struct cafe_camera *cam, |
| 317 | u16 addr, u8 command, u8 value) |
| 318 | { |
| 319 | unsigned int rval; |
| 320 | unsigned long flags; |
Jonathan Corbet | 6d77444 | 2007-08-17 01:02:33 -0300 | [diff] [blame] | 321 | DEFINE_WAIT(the_wait); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 322 | |
| 323 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 324 | rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID); |
| 325 | rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */ |
| 326 | /* |
| 327 | * Marvell sez set clkdiv to all 1's for now. |
| 328 | */ |
| 329 | rval |= TWSIC0_CLKDIV; |
| 330 | cafe_reg_write(cam, REG_TWSIC0, rval); |
| 331 | (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */ |
| 332 | rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR); |
| 333 | cafe_reg_write(cam, REG_TWSIC1, rval); |
| 334 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 335 | |
Jonathan Corbet | 6d77444 | 2007-08-17 01:02:33 -0300 | [diff] [blame] | 336 | /* |
| 337 | * Time to wait for the write to complete. THIS IS A RACY |
| 338 | * WAY TO DO IT, but the sad fact is that reading the TWSIC1 |
| 339 | * register too quickly after starting the operation sends |
| 340 | * the device into a place that may be kinder and better, but |
| 341 | * which is absolutely useless for controlling the sensor. In |
| 342 | * practice we have plenty of time to get into our sleep state |
| 343 | * before the interrupt hits, and the worst case is that we |
| 344 | * time out and then see that things completed, so this seems |
| 345 | * the best way for now. |
| 346 | */ |
| 347 | do { |
| 348 | prepare_to_wait(&cam->smbus_wait, &the_wait, |
| 349 | TASK_UNINTERRUPTIBLE); |
| 350 | schedule_timeout(1); /* even 1 jiffy is too long */ |
| 351 | finish_wait(&cam->smbus_wait, &the_wait); |
| 352 | } while (!cafe_smbus_write_done(cam)); |
| 353 | |
| 354 | #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 355 | wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam), |
| 356 | CAFE_SMBUS_TIMEOUT); |
Jonathan Corbet | 6d77444 | 2007-08-17 01:02:33 -0300 | [diff] [blame] | 357 | #endif |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 358 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 359 | rval = cafe_reg_read(cam, REG_TWSIC1); |
| 360 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 361 | |
| 362 | if (rval & TWSIC1_WSTAT) { |
| 363 | cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr, |
| 364 | command, value); |
| 365 | return -EIO; |
| 366 | } |
| 367 | if (rval & TWSIC1_ERROR) { |
| 368 | cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr, |
| 369 | command, value); |
| 370 | return -EIO; |
| 371 | } |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | static int cafe_smbus_read_done(struct cafe_camera *cam) |
| 378 | { |
| 379 | unsigned long flags; |
| 380 | int c1; |
| 381 | |
| 382 | /* |
| 383 | * We must delay after the interrupt, or the controller gets confused |
| 384 | * and never does give us good status. Fortunately, we don't do this |
| 385 | * often. |
| 386 | */ |
| 387 | udelay(20); |
| 388 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 389 | c1 = cafe_reg_read(cam, REG_TWSIC1); |
| 390 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 391 | return c1 & (TWSIC1_RVALID|TWSIC1_ERROR); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | static int cafe_smbus_read_data(struct cafe_camera *cam, |
| 397 | u16 addr, u8 command, u8 *value) |
| 398 | { |
| 399 | unsigned int rval; |
| 400 | unsigned long flags; |
| 401 | |
| 402 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 403 | rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID); |
| 404 | rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */ |
| 405 | /* |
| 406 | * Marvel sez set clkdiv to all 1's for now. |
| 407 | */ |
| 408 | rval |= TWSIC0_CLKDIV; |
| 409 | cafe_reg_write(cam, REG_TWSIC0, rval); |
| 410 | (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */ |
| 411 | rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR); |
| 412 | cafe_reg_write(cam, REG_TWSIC1, rval); |
| 413 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 414 | |
| 415 | wait_event_timeout(cam->smbus_wait, |
| 416 | cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT); |
| 417 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 418 | rval = cafe_reg_read(cam, REG_TWSIC1); |
| 419 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 420 | |
| 421 | if (rval & TWSIC1_ERROR) { |
| 422 | cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command); |
| 423 | return -EIO; |
| 424 | } |
| 425 | if (! (rval & TWSIC1_RVALID)) { |
| 426 | cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr, |
| 427 | command); |
| 428 | return -EIO; |
| 429 | } |
| 430 | *value = rval & 0xff; |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Perform a transfer over SMBUS. This thing is called under |
| 436 | * the i2c bus lock, so we shouldn't race with ourselves... |
| 437 | */ |
| 438 | static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr, |
| 439 | unsigned short flags, char rw, u8 command, |
| 440 | int size, union i2c_smbus_data *data) |
| 441 | { |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 442 | struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter); |
| 443 | struct cafe_camera *cam = to_cam(v4l2_dev); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 444 | int ret = -EINVAL; |
| 445 | |
| 446 | /* |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 447 | * This interface would appear to only do byte data ops. OK |
| 448 | * it can do word too, but the cam chip has no use for that. |
| 449 | */ |
| 450 | if (size != I2C_SMBUS_BYTE_DATA) { |
| 451 | cam_err(cam, "funky xfer size %d\n", size); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | if (rw == I2C_SMBUS_WRITE) |
| 456 | ret = cafe_smbus_write_data(cam, addr, command, data->byte); |
| 457 | else if (rw == I2C_SMBUS_READ) |
| 458 | ret = cafe_smbus_read_data(cam, addr, command, &data->byte); |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | static void cafe_smbus_enable_irq(struct cafe_camera *cam) |
| 464 | { |
| 465 | unsigned long flags; |
| 466 | |
| 467 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 468 | cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS); |
| 469 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 470 | } |
| 471 | |
| 472 | static u32 cafe_smbus_func(struct i2c_adapter *adapter) |
| 473 | { |
| 474 | return I2C_FUNC_SMBUS_READ_BYTE_DATA | |
| 475 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA; |
| 476 | } |
| 477 | |
| 478 | static struct i2c_algorithm cafe_smbus_algo = { |
| 479 | .smbus_xfer = cafe_smbus_xfer, |
| 480 | .functionality = cafe_smbus_func |
| 481 | }; |
| 482 | |
| 483 | /* Somebody is on the bus */ |
Jonathan Corbet | f9a7615 | 2006-11-19 19:04:55 -0300 | [diff] [blame] | 484 | static void cafe_ctlr_stop_dma(struct cafe_camera *cam); |
| 485 | static void cafe_ctlr_power_down(struct cafe_camera *cam); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 486 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 487 | static int cafe_smbus_setup(struct cafe_camera *cam) |
| 488 | { |
| 489 | struct i2c_adapter *adap = &cam->i2c_adapter; |
| 490 | int ret; |
| 491 | |
| 492 | cafe_smbus_enable_irq(cam); |
| 493 | adap->id = I2C_HW_SMBUS_CAFE; |
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; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 777 | chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR; |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 778 | chip.match.addr = cam->sensor_addr; |
| 779 | ret = sensor_call(cam, core, g_chip_ident, &chip); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 780 | if (ret) |
| 781 | goto out; |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 782 | cam->sensor_type = chip.ident; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 783 | if (cam->sensor_type != V4L2_IDENT_OV7670) { |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 784 | cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 785 | ret = -EINVAL; |
| 786 | goto out; |
| 787 | } |
| 788 | /* Get/set parameters? */ |
| 789 | ret = 0; |
| 790 | cam->state = S_IDLE; |
| 791 | out: |
Jonathan Corbet | 7acf90c | 2007-05-22 00:37:58 -0300 | [diff] [blame] | 792 | cafe_ctlr_power_down(cam); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 793 | mutex_unlock(&cam->s_mutex); |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Configure the sensor to match the parameters we have. Caller should |
| 799 | * hold s_mutex |
| 800 | */ |
| 801 | static int cafe_cam_set_flip(struct cafe_camera *cam) |
| 802 | { |
| 803 | struct v4l2_control ctrl; |
| 804 | |
| 805 | memset(&ctrl, 0, sizeof(ctrl)); |
| 806 | ctrl.id = V4L2_CID_VFLIP; |
| 807 | ctrl.value = flip; |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 808 | return sensor_call(cam, core, s_ctrl, &ctrl); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | |
| 812 | static int cafe_cam_configure(struct cafe_camera *cam) |
| 813 | { |
| 814 | struct v4l2_format fmt; |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 815 | int ret; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 816 | |
| 817 | if (cam->state != S_IDLE) |
| 818 | return -EINVAL; |
| 819 | fmt.fmt.pix = cam->pix_format; |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 820 | ret = sensor_call(cam, core, init, 0); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 821 | if (ret == 0) |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 822 | ret = sensor_call(cam, video, s_fmt, &fmt); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 823 | /* |
| 824 | * OV7670 does weird things if flip is set *before* format... |
| 825 | */ |
| 826 | ret += cafe_cam_set_flip(cam); |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | /* -------------------------------------------------------------------- */ |
| 831 | /* |
| 832 | * DMA buffer management. These functions need s_mutex held. |
| 833 | */ |
| 834 | |
| 835 | /* FIXME: this is inefficient as hell, since dma_alloc_coherent just |
| 836 | * does a get_free_pages() call, and we waste a good chunk of an orderN |
| 837 | * allocation. Should try to allocate the whole set in one chunk. |
| 838 | */ |
| 839 | static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime) |
| 840 | { |
| 841 | int i; |
| 842 | |
| 843 | cafe_set_config_needed(cam, 1); |
| 844 | if (loadtime) |
| 845 | cam->dma_buf_size = dma_buf_size; |
Jonathan Corbet | a66d233 | 2006-12-01 15:37:49 -0300 | [diff] [blame] | 846 | else |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 847 | cam->dma_buf_size = cam->pix_format.sizeimage; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 848 | if (n_dma_bufs > 3) |
| 849 | n_dma_bufs = 3; |
| 850 | |
| 851 | cam->nbufs = 0; |
| 852 | for (i = 0; i < n_dma_bufs; i++) { |
| 853 | cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev, |
| 854 | cam->dma_buf_size, cam->dma_handles + i, |
| 855 | GFP_KERNEL); |
| 856 | if (cam->dma_bufs[i] == NULL) { |
| 857 | cam_warn(cam, "Failed to allocate DMA buffer\n"); |
| 858 | break; |
| 859 | } |
| 860 | /* For debug, remove eventually */ |
| 861 | memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size); |
| 862 | (cam->nbufs)++; |
| 863 | } |
| 864 | |
| 865 | switch (cam->nbufs) { |
| 866 | case 1: |
| 867 | dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size, |
| 868 | cam->dma_bufs[0], cam->dma_handles[0]); |
| 869 | cam->nbufs = 0; |
| 870 | case 0: |
| 871 | cam_err(cam, "Insufficient DMA buffers, cannot operate\n"); |
| 872 | return -ENOMEM; |
| 873 | |
| 874 | case 2: |
| 875 | if (n_dma_bufs > 2) |
| 876 | cam_warn(cam, "Will limp along with only 2 buffers\n"); |
| 877 | break; |
| 878 | } |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | static void cafe_free_dma_bufs(struct cafe_camera *cam) |
| 883 | { |
| 884 | int i; |
| 885 | |
| 886 | for (i = 0; i < cam->nbufs; i++) { |
| 887 | dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size, |
| 888 | cam->dma_bufs[i], cam->dma_handles[i]); |
| 889 | cam->dma_bufs[i] = NULL; |
| 890 | } |
| 891 | cam->nbufs = 0; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | /* ----------------------------------------------------------------------- */ |
| 899 | /* |
| 900 | * Here starts the V4L2 interface code. |
| 901 | */ |
| 902 | |
| 903 | /* |
| 904 | * Read an image from the device. |
| 905 | */ |
| 906 | static ssize_t cafe_deliver_buffer(struct cafe_camera *cam, |
| 907 | char __user *buffer, size_t len, loff_t *pos) |
| 908 | { |
| 909 | int bufno; |
| 910 | unsigned long flags; |
| 911 | |
| 912 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 913 | if (cam->next_buf < 0) { |
| 914 | cam_err(cam, "deliver_buffer: No next buffer\n"); |
| 915 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 916 | return -EIO; |
| 917 | } |
| 918 | bufno = cam->next_buf; |
| 919 | clear_bit(bufno, &cam->flags); |
| 920 | if (++(cam->next_buf) >= cam->nbufs) |
| 921 | cam->next_buf = 0; |
| 922 | if (! test_bit(cam->next_buf, &cam->flags)) |
| 923 | cam->next_buf = -1; |
| 924 | cam->specframes = 0; |
| 925 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 926 | |
| 927 | if (len > cam->pix_format.sizeimage) |
| 928 | len = cam->pix_format.sizeimage; |
| 929 | if (copy_to_user(buffer, cam->dma_bufs[bufno], len)) |
| 930 | return -EFAULT; |
| 931 | (*pos) += len; |
| 932 | return len; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Get everything ready, and start grabbing frames. |
| 937 | */ |
| 938 | static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state) |
| 939 | { |
| 940 | int ret; |
| 941 | unsigned long flags; |
| 942 | |
| 943 | /* |
| 944 | * Configuration. If we still don't have DMA buffers, |
| 945 | * make one last, desperate attempt. |
| 946 | */ |
| 947 | if (cam->nbufs == 0) |
| 948 | if (cafe_alloc_dma_bufs(cam, 0)) |
| 949 | return -ENOMEM; |
| 950 | |
| 951 | if (cafe_needs_config(cam)) { |
| 952 | cafe_cam_configure(cam); |
| 953 | ret = cafe_ctlr_configure(cam); |
| 954 | if (ret) |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Turn it loose. |
| 960 | */ |
| 961 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 962 | cafe_reset_buffers(cam); |
| 963 | cafe_ctlr_irq_enable(cam); |
| 964 | cam->state = state; |
| 965 | cafe_ctlr_start(cam); |
| 966 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | |
| 971 | static ssize_t cafe_v4l_read(struct file *filp, |
| 972 | char __user *buffer, size_t len, loff_t *pos) |
| 973 | { |
| 974 | struct cafe_camera *cam = filp->private_data; |
Jean Delvare | b9109b75 | 2007-02-13 19:31:45 -0300 | [diff] [blame] | 975 | int ret = 0; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 976 | |
| 977 | /* |
| 978 | * Perhaps we're in speculative read mode and already |
| 979 | * have data? |
| 980 | */ |
| 981 | mutex_lock(&cam->s_mutex); |
| 982 | if (cam->state == S_SPECREAD) { |
| 983 | if (cam->next_buf >= 0) { |
| 984 | ret = cafe_deliver_buffer(cam, buffer, len, pos); |
| 985 | if (ret != 0) |
| 986 | goto out_unlock; |
| 987 | } |
| 988 | } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) { |
| 989 | ret = -EIO; |
| 990 | goto out_unlock; |
| 991 | } else if (cam->state != S_IDLE) { |
| 992 | ret = -EBUSY; |
| 993 | goto out_unlock; |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * v4l2: multiple processes can open the device, but only |
| 998 | * one gets to grab data from it. |
| 999 | */ |
| 1000 | if (cam->owner && cam->owner != filp) { |
| 1001 | ret = -EBUSY; |
| 1002 | goto out_unlock; |
| 1003 | } |
| 1004 | cam->owner = filp; |
| 1005 | |
| 1006 | /* |
| 1007 | * Do setup if need be. |
| 1008 | */ |
| 1009 | if (cam->state != S_SPECREAD) { |
| 1010 | ret = cafe_read_setup(cam, S_SINGLEREAD); |
| 1011 | if (ret) |
| 1012 | goto out_unlock; |
| 1013 | } |
| 1014 | /* |
| 1015 | * Wait for something to happen. This should probably |
| 1016 | * be interruptible (FIXME). |
| 1017 | */ |
| 1018 | wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ); |
| 1019 | if (cam->next_buf < 0) { |
| 1020 | cam_err(cam, "read() operation timed out\n"); |
| 1021 | cafe_ctlr_stop_dma(cam); |
| 1022 | ret = -EIO; |
| 1023 | goto out_unlock; |
| 1024 | } |
| 1025 | /* |
| 1026 | * Give them their data and we should be done. |
| 1027 | */ |
| 1028 | ret = cafe_deliver_buffer(cam, buffer, len, pos); |
| 1029 | |
| 1030 | out_unlock: |
| 1031 | mutex_unlock(&cam->s_mutex); |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | /* |
| 1043 | * Streaming I/O support. |
| 1044 | */ |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | static int cafe_vidioc_streamon(struct file *filp, void *priv, |
| 1049 | enum v4l2_buf_type type) |
| 1050 | { |
| 1051 | struct cafe_camera *cam = filp->private_data; |
| 1052 | int ret = -EINVAL; |
| 1053 | |
| 1054 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1055 | goto out; |
| 1056 | mutex_lock(&cam->s_mutex); |
| 1057 | if (cam->state != S_IDLE || cam->n_sbufs == 0) |
| 1058 | goto out_unlock; |
| 1059 | |
| 1060 | cam->sequence = 0; |
| 1061 | ret = cafe_read_setup(cam, S_STREAMING); |
| 1062 | |
| 1063 | out_unlock: |
| 1064 | mutex_unlock(&cam->s_mutex); |
| 1065 | out: |
| 1066 | return ret; |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | static int cafe_vidioc_streamoff(struct file *filp, void *priv, |
| 1071 | enum v4l2_buf_type type) |
| 1072 | { |
| 1073 | struct cafe_camera *cam = filp->private_data; |
| 1074 | int ret = -EINVAL; |
| 1075 | |
| 1076 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1077 | goto out; |
| 1078 | mutex_lock(&cam->s_mutex); |
| 1079 | if (cam->state != S_STREAMING) |
| 1080 | goto out_unlock; |
| 1081 | |
| 1082 | cafe_ctlr_stop_dma(cam); |
| 1083 | ret = 0; |
| 1084 | |
| 1085 | out_unlock: |
| 1086 | mutex_unlock(&cam->s_mutex); |
| 1087 | out: |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | static int cafe_setup_siobuf(struct cafe_camera *cam, int index) |
| 1094 | { |
| 1095 | struct cafe_sio_buffer *buf = cam->sb_bufs + index; |
| 1096 | |
| 1097 | INIT_LIST_HEAD(&buf->list); |
| 1098 | buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage); |
| 1099 | buf->buffer = vmalloc_user(buf->v4lbuf.length); |
| 1100 | if (buf->buffer == NULL) |
| 1101 | return -ENOMEM; |
| 1102 | buf->mapcount = 0; |
| 1103 | buf->cam = cam; |
| 1104 | |
| 1105 | buf->v4lbuf.index = index; |
| 1106 | buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1107 | buf->v4lbuf.field = V4L2_FIELD_NONE; |
| 1108 | buf->v4lbuf.memory = V4L2_MEMORY_MMAP; |
| 1109 | /* |
Mauro Carvalho Chehab | c1accaa | 2007-08-23 16:37:49 -0300 | [diff] [blame] | 1110 | * 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] | 1111 | * just uses the length times the index, but the spec warns |
| 1112 | * against doing just that - vma merging problems. So we |
| 1113 | * leave a gap between each pair of buffers. |
| 1114 | */ |
| 1115 | buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length; |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static int cafe_free_sio_buffers(struct cafe_camera *cam) |
| 1120 | { |
| 1121 | int i; |
| 1122 | |
| 1123 | /* |
| 1124 | * If any buffers are mapped, we cannot free them at all. |
| 1125 | */ |
| 1126 | for (i = 0; i < cam->n_sbufs; i++) |
| 1127 | if (cam->sb_bufs[i].mapcount > 0) |
| 1128 | return -EBUSY; |
| 1129 | /* |
| 1130 | * OK, let's do it. |
| 1131 | */ |
| 1132 | for (i = 0; i < cam->n_sbufs; i++) |
| 1133 | vfree(cam->sb_bufs[i].buffer); |
| 1134 | cam->n_sbufs = 0; |
| 1135 | kfree(cam->sb_bufs); |
| 1136 | cam->sb_bufs = NULL; |
| 1137 | INIT_LIST_HEAD(&cam->sb_avail); |
| 1138 | INIT_LIST_HEAD(&cam->sb_full); |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | static int cafe_vidioc_reqbufs(struct file *filp, void *priv, |
| 1145 | struct v4l2_requestbuffers *req) |
| 1146 | { |
| 1147 | struct cafe_camera *cam = filp->private_data; |
Jonathan Corbet | 3198cf6 | 2007-02-06 21:52:36 -0300 | [diff] [blame] | 1148 | int ret = 0; /* Silence warning */ |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1149 | |
| 1150 | /* |
| 1151 | * Make sure it's something we can do. User pointers could be |
| 1152 | * implemented without great pain, but that's not been done yet. |
| 1153 | */ |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1154 | if (req->memory != V4L2_MEMORY_MMAP) |
| 1155 | return -EINVAL; |
| 1156 | /* |
| 1157 | * If they ask for zero buffers, they really want us to stop streaming |
| 1158 | * (if it's happening) and free everything. Should we check owner? |
| 1159 | */ |
| 1160 | mutex_lock(&cam->s_mutex); |
| 1161 | if (req->count == 0) { |
| 1162 | if (cam->state == S_STREAMING) |
| 1163 | cafe_ctlr_stop_dma(cam); |
| 1164 | ret = cafe_free_sio_buffers (cam); |
| 1165 | goto out; |
| 1166 | } |
| 1167 | /* |
| 1168 | * Device needs to be idle and working. We *could* try to do the |
| 1169 | * right thing in S_SPECREAD by shutting things down, but it |
| 1170 | * probably doesn't matter. |
| 1171 | */ |
| 1172 | if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) { |
| 1173 | ret = -EBUSY; |
| 1174 | goto out; |
| 1175 | } |
| 1176 | cam->owner = filp; |
| 1177 | |
| 1178 | if (req->count < min_buffers) |
| 1179 | req->count = min_buffers; |
| 1180 | else if (req->count > max_buffers) |
| 1181 | req->count = max_buffers; |
| 1182 | if (cam->n_sbufs > 0) { |
| 1183 | ret = cafe_free_sio_buffers(cam); |
| 1184 | if (ret) |
| 1185 | goto out; |
| 1186 | } |
| 1187 | |
| 1188 | cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer), |
| 1189 | GFP_KERNEL); |
| 1190 | if (cam->sb_bufs == NULL) { |
| 1191 | ret = -ENOMEM; |
| 1192 | goto out; |
| 1193 | } |
| 1194 | for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) { |
| 1195 | ret = cafe_setup_siobuf(cam, cam->n_sbufs); |
| 1196 | if (ret) |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | if (cam->n_sbufs == 0) /* no luck at all - ret already set */ |
| 1201 | kfree(cam->sb_bufs); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1202 | req->count = cam->n_sbufs; /* In case of partial success */ |
| 1203 | |
| 1204 | out: |
| 1205 | mutex_unlock(&cam->s_mutex); |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | static int cafe_vidioc_querybuf(struct file *filp, void *priv, |
| 1211 | struct v4l2_buffer *buf) |
| 1212 | { |
| 1213 | struct cafe_camera *cam = filp->private_data; |
| 1214 | int ret = -EINVAL; |
| 1215 | |
| 1216 | mutex_lock(&cam->s_mutex); |
Trent Piepho | e33ee31 | 2009-03-28 22:25:36 -0300 | [diff] [blame^] | 1217 | if (buf->index >= cam->n_sbufs) |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1218 | goto out; |
| 1219 | *buf = cam->sb_bufs[buf->index].v4lbuf; |
| 1220 | ret = 0; |
| 1221 | out: |
| 1222 | mutex_unlock(&cam->s_mutex); |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
| 1226 | static int cafe_vidioc_qbuf(struct file *filp, void *priv, |
| 1227 | struct v4l2_buffer *buf) |
| 1228 | { |
| 1229 | struct cafe_camera *cam = filp->private_data; |
| 1230 | struct cafe_sio_buffer *sbuf; |
| 1231 | int ret = -EINVAL; |
| 1232 | unsigned long flags; |
| 1233 | |
| 1234 | mutex_lock(&cam->s_mutex); |
Trent Piepho | e33ee31 | 2009-03-28 22:25:36 -0300 | [diff] [blame^] | 1235 | if (buf->index >= cam->n_sbufs) |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1236 | goto out; |
| 1237 | sbuf = cam->sb_bufs + buf->index; |
| 1238 | if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) { |
| 1239 | ret = 0; /* Already queued?? */ |
| 1240 | goto out; |
| 1241 | } |
| 1242 | if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) { |
| 1243 | /* Spec doesn't say anything, seems appropriate tho */ |
| 1244 | ret = -EBUSY; |
| 1245 | goto out; |
| 1246 | } |
| 1247 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED; |
| 1248 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 1249 | list_add(&sbuf->list, &cam->sb_avail); |
| 1250 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 1251 | ret = 0; |
| 1252 | out: |
| 1253 | mutex_unlock(&cam->s_mutex); |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | static int cafe_vidioc_dqbuf(struct file *filp, void *priv, |
| 1258 | struct v4l2_buffer *buf) |
| 1259 | { |
| 1260 | struct cafe_camera *cam = filp->private_data; |
| 1261 | struct cafe_sio_buffer *sbuf; |
| 1262 | int ret = -EINVAL; |
| 1263 | unsigned long flags; |
| 1264 | |
| 1265 | mutex_lock(&cam->s_mutex); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1266 | if (cam->state != S_STREAMING) |
| 1267 | goto out_unlock; |
| 1268 | if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) { |
| 1269 | ret = -EAGAIN; |
| 1270 | goto out_unlock; |
| 1271 | } |
| 1272 | |
| 1273 | while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) { |
| 1274 | mutex_unlock(&cam->s_mutex); |
| 1275 | if (wait_event_interruptible(cam->iowait, |
| 1276 | !list_empty(&cam->sb_full))) { |
| 1277 | ret = -ERESTARTSYS; |
| 1278 | goto out; |
| 1279 | } |
| 1280 | mutex_lock(&cam->s_mutex); |
| 1281 | } |
| 1282 | |
| 1283 | if (cam->state != S_STREAMING) |
| 1284 | ret = -EINTR; |
| 1285 | else { |
| 1286 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 1287 | /* Should probably recheck !list_empty() here */ |
| 1288 | sbuf = list_entry(cam->sb_full.next, |
| 1289 | struct cafe_sio_buffer, list); |
| 1290 | list_del_init(&sbuf->list); |
| 1291 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 1292 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 1293 | *buf = sbuf->v4lbuf; |
| 1294 | ret = 0; |
| 1295 | } |
| 1296 | |
| 1297 | out_unlock: |
| 1298 | mutex_unlock(&cam->s_mutex); |
| 1299 | out: |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | static void cafe_v4l_vm_open(struct vm_area_struct *vma) |
| 1306 | { |
| 1307 | struct cafe_sio_buffer *sbuf = vma->vm_private_data; |
| 1308 | /* |
| 1309 | * Locking: done under mmap_sem, so we don't need to |
| 1310 | * go back to the camera lock here. |
| 1311 | */ |
| 1312 | sbuf->mapcount++; |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | static void cafe_v4l_vm_close(struct vm_area_struct *vma) |
| 1317 | { |
| 1318 | struct cafe_sio_buffer *sbuf = vma->vm_private_data; |
| 1319 | |
| 1320 | mutex_lock(&sbuf->cam->s_mutex); |
| 1321 | sbuf->mapcount--; |
| 1322 | /* Docs say we should stop I/O too... */ |
| 1323 | if (sbuf->mapcount == 0) |
| 1324 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED; |
| 1325 | mutex_unlock(&sbuf->cam->s_mutex); |
| 1326 | } |
| 1327 | |
| 1328 | static struct vm_operations_struct cafe_v4l_vm_ops = { |
| 1329 | .open = cafe_v4l_vm_open, |
| 1330 | .close = cafe_v4l_vm_close |
| 1331 | }; |
| 1332 | |
| 1333 | |
| 1334 | static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1335 | { |
| 1336 | struct cafe_camera *cam = filp->private_data; |
| 1337 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
| 1338 | int ret = -EINVAL; |
| 1339 | int i; |
| 1340 | struct cafe_sio_buffer *sbuf = NULL; |
| 1341 | |
| 1342 | if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED)) |
| 1343 | return -EINVAL; |
| 1344 | /* |
| 1345 | * Find the buffer they are looking for. |
| 1346 | */ |
| 1347 | mutex_lock(&cam->s_mutex); |
| 1348 | for (i = 0; i < cam->n_sbufs; i++) |
| 1349 | if (cam->sb_bufs[i].v4lbuf.m.offset == offset) { |
| 1350 | sbuf = cam->sb_bufs + i; |
| 1351 | break; |
| 1352 | } |
| 1353 | if (sbuf == NULL) |
| 1354 | goto out; |
| 1355 | |
| 1356 | ret = remap_vmalloc_range(vma, sbuf->buffer, 0); |
| 1357 | if (ret) |
| 1358 | goto out; |
| 1359 | vma->vm_flags |= VM_DONTEXPAND; |
| 1360 | vma->vm_private_data = sbuf; |
| 1361 | vma->vm_ops = &cafe_v4l_vm_ops; |
| 1362 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED; |
| 1363 | cafe_v4l_vm_open(vma); |
| 1364 | ret = 0; |
| 1365 | out: |
| 1366 | mutex_unlock(&cam->s_mutex); |
| 1367 | return ret; |
| 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1372 | static int cafe_v4l_open(struct file *filp) |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1373 | { |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1374 | struct cafe_camera *cam = video_drvdata(filp); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1375 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1376 | filp->private_data = cam; |
| 1377 | |
| 1378 | mutex_lock(&cam->s_mutex); |
| 1379 | if (cam->users == 0) { |
| 1380 | cafe_ctlr_power_up(cam); |
| 1381 | __cafe_cam_reset(cam); |
| 1382 | cafe_set_config_needed(cam, 1); |
| 1383 | /* FIXME make sure this is complete */ |
| 1384 | } |
| 1385 | (cam->users)++; |
| 1386 | mutex_unlock(&cam->s_mutex); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1391 | static int cafe_v4l_release(struct file *filp) |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1392 | { |
| 1393 | struct cafe_camera *cam = filp->private_data; |
| 1394 | |
| 1395 | mutex_lock(&cam->s_mutex); |
| 1396 | (cam->users)--; |
| 1397 | if (filp == cam->owner) { |
| 1398 | cafe_ctlr_stop_dma(cam); |
| 1399 | cafe_free_sio_buffers(cam); |
| 1400 | cam->owner = NULL; |
| 1401 | } |
Jonathan Corbet | f9a7615 | 2006-11-19 19:04:55 -0300 | [diff] [blame] | 1402 | if (cam->users == 0) { |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1403 | cafe_ctlr_power_down(cam); |
Andres Salomon | 23869e2 | 2007-09-19 02:44:18 -0300 | [diff] [blame] | 1404 | if (alloc_bufs_at_read) |
Jonathan Corbet | f9a7615 | 2006-11-19 19:04:55 -0300 | [diff] [blame] | 1405 | cafe_free_dma_bufs(cam); |
| 1406 | } |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1407 | mutex_unlock(&cam->s_mutex); |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | static unsigned int cafe_v4l_poll(struct file *filp, |
| 1414 | struct poll_table_struct *pt) |
| 1415 | { |
| 1416 | struct cafe_camera *cam = filp->private_data; |
| 1417 | |
| 1418 | poll_wait(filp, &cam->iowait, pt); |
| 1419 | if (cam->next_buf >= 0) |
| 1420 | return POLLIN | POLLRDNORM; |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | static int cafe_vidioc_queryctrl(struct file *filp, void *priv, |
| 1427 | struct v4l2_queryctrl *qc) |
| 1428 | { |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1429 | struct cafe_camera *cam = priv; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1430 | int ret; |
| 1431 | |
| 1432 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1433 | ret = sensor_call(cam, core, queryctrl, qc); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1434 | mutex_unlock(&cam->s_mutex); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
| 1438 | |
| 1439 | static int cafe_vidioc_g_ctrl(struct file *filp, void *priv, |
| 1440 | struct v4l2_control *ctrl) |
| 1441 | { |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1442 | struct cafe_camera *cam = priv; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1443 | int ret; |
| 1444 | |
| 1445 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1446 | ret = sensor_call(cam, core, g_ctrl, ctrl); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1447 | mutex_unlock(&cam->s_mutex); |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | static int cafe_vidioc_s_ctrl(struct file *filp, void *priv, |
| 1453 | struct v4l2_control *ctrl) |
| 1454 | { |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1455 | struct cafe_camera *cam = priv; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1456 | int ret; |
| 1457 | |
| 1458 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1459 | ret = sensor_call(cam, core, s_ctrl, ctrl); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1460 | mutex_unlock(&cam->s_mutex); |
| 1461 | return ret; |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | static int cafe_vidioc_querycap(struct file *file, void *priv, |
| 1469 | struct v4l2_capability *cap) |
| 1470 | { |
| 1471 | strcpy(cap->driver, "cafe_ccic"); |
| 1472 | strcpy(cap->card, "cafe_ccic"); |
| 1473 | cap->version = CAFE_VERSION; |
| 1474 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | |
| 1475 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | /* |
| 1481 | * The default format we use until somebody says otherwise. |
| 1482 | */ |
| 1483 | static struct v4l2_pix_format cafe_def_pix_format = { |
| 1484 | .width = VGA_WIDTH, |
| 1485 | .height = VGA_HEIGHT, |
| 1486 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 1487 | .field = V4L2_FIELD_NONE, |
| 1488 | .bytesperline = VGA_WIDTH*2, |
| 1489 | .sizeimage = VGA_WIDTH*VGA_HEIGHT*2, |
| 1490 | }; |
| 1491 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1492 | static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp, |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1493 | void *priv, struct v4l2_fmtdesc *fmt) |
| 1494 | { |
| 1495 | struct cafe_camera *cam = priv; |
| 1496 | int ret; |
| 1497 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1498 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1499 | ret = sensor_call(cam, video, enum_fmt, fmt); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1500 | mutex_unlock(&cam->s_mutex); |
| 1501 | return ret; |
| 1502 | } |
| 1503 | |
| 1504 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1505 | 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] | 1506 | struct v4l2_format *fmt) |
| 1507 | { |
| 1508 | struct cafe_camera *cam = priv; |
| 1509 | int ret; |
| 1510 | |
| 1511 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1512 | ret = sensor_call(cam, video, try_fmt, fmt); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1513 | mutex_unlock(&cam->s_mutex); |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1517 | 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] | 1518 | struct v4l2_format *fmt) |
| 1519 | { |
| 1520 | struct cafe_camera *cam = priv; |
| 1521 | int ret; |
| 1522 | |
| 1523 | /* |
| 1524 | * Can't do anything if the device is not idle |
| 1525 | * Also can't if there are streaming buffers in place. |
| 1526 | */ |
| 1527 | if (cam->state != S_IDLE || cam->n_sbufs > 0) |
| 1528 | return -EBUSY; |
| 1529 | /* |
| 1530 | * See if the formatting works in principle. |
| 1531 | */ |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1532 | ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1533 | if (ret) |
| 1534 | return ret; |
| 1535 | /* |
| 1536 | * Now we start to change things for real, so let's do it |
| 1537 | * under lock. |
| 1538 | */ |
| 1539 | mutex_lock(&cam->s_mutex); |
| 1540 | cam->pix_format = fmt->fmt.pix; |
| 1541 | /* |
| 1542 | * Make sure we have appropriate DMA buffers. |
| 1543 | */ |
| 1544 | ret = -ENOMEM; |
| 1545 | if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage) |
| 1546 | cafe_free_dma_bufs(cam); |
| 1547 | if (cam->nbufs == 0) { |
| 1548 | if (cafe_alloc_dma_bufs(cam, 0)) |
| 1549 | goto out; |
| 1550 | } |
| 1551 | /* |
| 1552 | * It looks like this might work, so let's program the sensor. |
| 1553 | */ |
| 1554 | ret = cafe_cam_configure(cam); |
| 1555 | if (! ret) |
| 1556 | ret = cafe_ctlr_configure(cam); |
| 1557 | out: |
| 1558 | mutex_unlock(&cam->s_mutex); |
| 1559 | return ret; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Return our stored notion of how the camera is/should be configured. |
| 1564 | * The V4l2 spec wants us to be smarter, and actually get this from |
| 1565 | * the camera (and not mess with it at open time). Someday. |
| 1566 | */ |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1567 | 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] | 1568 | struct v4l2_format *f) |
| 1569 | { |
| 1570 | struct cafe_camera *cam = priv; |
| 1571 | |
| 1572 | f->fmt.pix = cam->pix_format; |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * We only have one input - the sensor - so minimize the nonsense here. |
| 1578 | */ |
| 1579 | static int cafe_vidioc_enum_input(struct file *filp, void *priv, |
| 1580 | struct v4l2_input *input) |
| 1581 | { |
| 1582 | if (input->index != 0) |
| 1583 | return -EINVAL; |
| 1584 | |
| 1585 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 1586 | input->std = V4L2_STD_ALL; /* Not sure what should go here */ |
| 1587 | strcpy(input->name, "Camera"); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 1592 | { |
| 1593 | *i = 0; |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 1598 | { |
| 1599 | if (i != 0) |
| 1600 | return -EINVAL; |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | /* from vivi.c */ |
Mauro Carvalho Chehab | e75f9ce | 2006-11-20 13:19:20 -0300 | [diff] [blame] | 1605 | 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] | 1606 | { |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
Jonathan Corbet | c8f5b2f5 | 2006-12-01 15:50:59 -0300 | [diff] [blame] | 1610 | /* |
| 1611 | * G/S_PARM. Most of this is done by the sensor, but we are |
| 1612 | * the level which controls the number of read buffers. |
| 1613 | */ |
| 1614 | static int cafe_vidioc_g_parm(struct file *filp, void *priv, |
| 1615 | struct v4l2_streamparm *parms) |
| 1616 | { |
| 1617 | struct cafe_camera *cam = priv; |
| 1618 | int ret; |
| 1619 | |
| 1620 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1621 | ret = sensor_call(cam, video, g_parm, parms); |
Jonathan Corbet | c8f5b2f5 | 2006-12-01 15:50:59 -0300 | [diff] [blame] | 1622 | mutex_unlock(&cam->s_mutex); |
| 1623 | parms->parm.capture.readbuffers = n_dma_bufs; |
| 1624 | return ret; |
| 1625 | } |
| 1626 | |
| 1627 | static int cafe_vidioc_s_parm(struct file *filp, void *priv, |
| 1628 | struct v4l2_streamparm *parms) |
| 1629 | { |
| 1630 | struct cafe_camera *cam = priv; |
| 1631 | int ret; |
| 1632 | |
| 1633 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1634 | ret = sensor_call(cam, video, s_parm, parms); |
Jonathan Corbet | c8f5b2f5 | 2006-12-01 15:50:59 -0300 | [diff] [blame] | 1635 | mutex_unlock(&cam->s_mutex); |
| 1636 | parms->parm.capture.readbuffers = n_dma_bufs; |
| 1637 | return ret; |
| 1638 | } |
| 1639 | |
Hans Verkuil | 69d94f7 | 2009-03-18 13:25:54 -0300 | [diff] [blame] | 1640 | static int cafe_vidioc_g_chip_ident(struct file *file, void *priv, |
| 1641 | struct v4l2_dbg_chip_ident *chip) |
| 1642 | { |
| 1643 | struct cafe_camera *cam = priv; |
| 1644 | |
| 1645 | chip->ident = V4L2_IDENT_NONE; |
| 1646 | chip->revision = 0; |
| 1647 | if (v4l2_chip_match_host(&chip->match)) { |
| 1648 | chip->ident = V4L2_IDENT_CAFE; |
| 1649 | return 0; |
| 1650 | } |
| 1651 | return sensor_call(cam, core, g_chip_ident, chip); |
| 1652 | } |
| 1653 | |
| 1654 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1655 | static int cafe_vidioc_g_register(struct file *file, void *priv, |
| 1656 | struct v4l2_dbg_register *reg) |
| 1657 | { |
| 1658 | struct cafe_camera *cam = priv; |
| 1659 | |
| 1660 | if (v4l2_chip_match_host(®->match)) { |
| 1661 | reg->val = cafe_reg_read(cam, reg->reg); |
| 1662 | reg->size = 4; |
| 1663 | return 0; |
| 1664 | } |
| 1665 | return sensor_call(cam, core, g_register, reg); |
| 1666 | } |
| 1667 | |
| 1668 | static int cafe_vidioc_s_register(struct file *file, void *priv, |
| 1669 | struct v4l2_dbg_register *reg) |
| 1670 | { |
| 1671 | struct cafe_camera *cam = priv; |
| 1672 | |
| 1673 | if (v4l2_chip_match_host(®->match)) { |
| 1674 | cafe_reg_write(cam, reg->reg, reg->val); |
| 1675 | return 0; |
| 1676 | } |
| 1677 | return sensor_call(cam, core, s_register, reg); |
| 1678 | } |
| 1679 | #endif |
| 1680 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1681 | /* |
| 1682 | * This template device holds all of those v4l2 methods; we |
| 1683 | * clone it for specific real devices. |
| 1684 | */ |
| 1685 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1686 | static const struct v4l2_file_operations cafe_v4l_fops = { |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1687 | .owner = THIS_MODULE, |
| 1688 | .open = cafe_v4l_open, |
| 1689 | .release = cafe_v4l_release, |
| 1690 | .read = cafe_v4l_read, |
| 1691 | .poll = cafe_v4l_poll, |
| 1692 | .mmap = cafe_v4l_mmap, |
| 1693 | .ioctl = video_ioctl2, |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1694 | }; |
| 1695 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1696 | static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = { |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1697 | .vidioc_querycap = cafe_vidioc_querycap, |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1698 | .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap, |
| 1699 | .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap, |
| 1700 | .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap, |
| 1701 | .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap, |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1702 | .vidioc_enum_input = cafe_vidioc_enum_input, |
| 1703 | .vidioc_g_input = cafe_vidioc_g_input, |
| 1704 | .vidioc_s_input = cafe_vidioc_s_input, |
| 1705 | .vidioc_s_std = cafe_vidioc_s_std, |
| 1706 | .vidioc_reqbufs = cafe_vidioc_reqbufs, |
| 1707 | .vidioc_querybuf = cafe_vidioc_querybuf, |
| 1708 | .vidioc_qbuf = cafe_vidioc_qbuf, |
| 1709 | .vidioc_dqbuf = cafe_vidioc_dqbuf, |
| 1710 | .vidioc_streamon = cafe_vidioc_streamon, |
| 1711 | .vidioc_streamoff = cafe_vidioc_streamoff, |
| 1712 | .vidioc_queryctrl = cafe_vidioc_queryctrl, |
| 1713 | .vidioc_g_ctrl = cafe_vidioc_g_ctrl, |
| 1714 | .vidioc_s_ctrl = cafe_vidioc_s_ctrl, |
Jonathan Corbet | c8f5b2f5 | 2006-12-01 15:50:59 -0300 | [diff] [blame] | 1715 | .vidioc_g_parm = cafe_vidioc_g_parm, |
| 1716 | .vidioc_s_parm = cafe_vidioc_s_parm, |
Hans Verkuil | 69d94f7 | 2009-03-18 13:25:54 -0300 | [diff] [blame] | 1717 | .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident, |
| 1718 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1719 | .vidioc_g_register = cafe_vidioc_g_register, |
| 1720 | .vidioc_s_register = cafe_vidioc_s_register, |
| 1721 | #endif |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1722 | }; |
| 1723 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1724 | static struct video_device cafe_v4l_template = { |
| 1725 | .name = "cafe", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1726 | .minor = -1, /* Get one dynamically */ |
| 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); |
| 1911 | mutex_lock(&cam->s_mutex); |
| 1912 | spin_lock_init(&cam->dev_lock); |
| 1913 | cam->state = S_NOTREADY; |
| 1914 | cafe_set_config_needed(cam, 1); |
| 1915 | init_waitqueue_head(&cam->smbus_wait); |
| 1916 | init_waitqueue_head(&cam->iowait); |
| 1917 | cam->pdev = pdev; |
| 1918 | cam->pix_format = cafe_def_pix_format; |
| 1919 | INIT_LIST_HEAD(&cam->dev_list); |
| 1920 | INIT_LIST_HEAD(&cam->sb_avail); |
| 1921 | INIT_LIST_HEAD(&cam->sb_full); |
| 1922 | tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam); |
| 1923 | /* |
| 1924 | * Get set up on the PCI bus. |
| 1925 | */ |
| 1926 | ret = pci_enable_device(pdev); |
| 1927 | if (ret) |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1928 | goto out_unreg; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1929 | pci_set_master(pdev); |
| 1930 | |
| 1931 | ret = -EIO; |
| 1932 | cam->regs = pci_iomap(pdev, 0, 0); |
| 1933 | if (! cam->regs) { |
| 1934 | printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n"); |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1935 | goto out_unreg; |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1936 | } |
| 1937 | ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam); |
| 1938 | if (ret) |
| 1939 | goto out_iounmap; |
Jonathan Corbet | 7acf90c | 2007-05-22 00:37:58 -0300 | [diff] [blame] | 1940 | /* |
| 1941 | * Initialize the controller and leave it powered up. It will |
| 1942 | * stay that way until the sensor driver shows up. |
| 1943 | */ |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1944 | cafe_ctlr_init(cam); |
| 1945 | cafe_ctlr_power_up(cam); |
| 1946 | /* |
Jonathan Corbet | 7acf90c | 2007-05-22 00:37:58 -0300 | [diff] [blame] | 1947 | * Set up I2C/SMBUS communications. We have to drop the mutex here |
| 1948 | * because the sensor could attach in this call chain, leading to |
| 1949 | * unsightly deadlocks. |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1950 | */ |
| 1951 | mutex_unlock(&cam->s_mutex); /* attach can deadlock */ |
| 1952 | ret = cafe_smbus_setup(cam); |
| 1953 | if (ret) |
| 1954 | goto out_freeirq; |
Hans Verkuil | 8bcfd7a | 2009-03-18 13:16:44 -0300 | [diff] [blame] | 1955 | |
| 1956 | cam->sensor_addr = 0x42; |
| 1957 | cam->sensor = v4l2_i2c_new_subdev(&cam->i2c_adapter, |
| 1958 | "ov7670", "ov7670", cam->sensor_addr); |
| 1959 | if (cam->sensor == NULL) { |
| 1960 | ret = -ENODEV; |
| 1961 | goto out_smbus; |
| 1962 | } |
| 1963 | ret = cafe_cam_init(cam); |
| 1964 | if (ret) |
| 1965 | goto out_smbus; |
| 1966 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1967 | /* |
| 1968 | * Get the v4l2 setup done. |
| 1969 | */ |
| 1970 | mutex_lock(&cam->s_mutex); |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1971 | cam->vdev = cafe_v4l_template; |
| 1972 | cam->vdev.debug = 0; |
| 1973 | /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/ |
| 1974 | cam->vdev.v4l2_dev = &cam->v4l2_dev; |
| 1975 | ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1976 | if (ret) |
| 1977 | goto out_smbus; |
Hans Verkuil | 21508b90 | 2009-03-18 13:13:09 -0300 | [diff] [blame] | 1978 | video_set_drvdata(&cam->vdev, cam); |
| 1979 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1980 | /* |
| 1981 | * If so requested, try to get our DMA buffers now. |
| 1982 | */ |
Andres Salomon | 23869e2 | 2007-09-19 02:44:18 -0300 | [diff] [blame] | 1983 | if (!alloc_bufs_at_read) { |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1984 | if (cafe_alloc_dma_bufs(cam, 1)) |
| 1985 | cam_warn(cam, "Unable to alloc DMA buffers at load" |
| 1986 | " will try again later."); |
| 1987 | } |
| 1988 | |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1989 | mutex_unlock(&cam->s_mutex); |
Jonathan Corbet | d905b38 | 2006-11-04 09:25:53 -0300 | [diff] [blame] | 1990 | return 0; |
| 1991 | |
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); |