Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1 | /* |
| 2 | * The Marvell camera core. This device appears in a number of settings, |
| 3 | * so it needs platform-specific support outside of the core. |
| 4 | * |
| 5 | * Copyright 2011 Jonathan Corbet corbet@lwn.net |
| 6 | */ |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/fs.h> |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/videodev2.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <media/v4l2-device.h> |
| 17 | #include <media/v4l2-ioctl.h> |
| 18 | #include <media/v4l2-chip-ident.h> |
| 19 | #include <media/ov7670.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/wait.h> |
| 22 | #include <linux/list.h> |
| 23 | #include <linux/dma-mapping.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/io.h> |
| 29 | |
| 30 | #include "mcam-core.h" |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * Internal DMA buffer management. Since the controller cannot do S/G I/O, |
| 35 | * we must have physically contiguous buffers to bring frames into. |
| 36 | * These parameters control how many buffers we use, whether we |
| 37 | * allocate them at load time (better chance of success, but nails down |
| 38 | * memory) or when somebody tries to use the camera (riskier), and, |
| 39 | * for load-time allocation, how big they should be. |
| 40 | * |
| 41 | * The controller can cycle through three buffers. We could use |
| 42 | * more by flipping pointers around, but it probably makes little |
| 43 | * sense. |
| 44 | */ |
| 45 | |
| 46 | static int alloc_bufs_at_read; |
| 47 | module_param(alloc_bufs_at_read, bool, 0444); |
| 48 | MODULE_PARM_DESC(alloc_bufs_at_read, |
| 49 | "Non-zero value causes DMA buffers to be allocated when the " |
| 50 | "video capture device is read, rather than at module load " |
| 51 | "time. This saves memory, but decreases the chances of " |
| 52 | "successfully getting those buffers."); |
| 53 | |
| 54 | static int n_dma_bufs = 3; |
| 55 | module_param(n_dma_bufs, uint, 0644); |
| 56 | MODULE_PARM_DESC(n_dma_bufs, |
| 57 | "The number of DMA buffers to allocate. Can be either two " |
| 58 | "(saves memory, makes timing tighter) or three."); |
| 59 | |
| 60 | static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */ |
| 61 | module_param(dma_buf_size, uint, 0444); |
| 62 | MODULE_PARM_DESC(dma_buf_size, |
| 63 | "The size of the allocated DMA buffers. If actual operating " |
| 64 | "parameters require larger buffers, an attempt to reallocate " |
| 65 | "will be made."); |
| 66 | |
| 67 | static int min_buffers = 1; |
| 68 | module_param(min_buffers, uint, 0644); |
| 69 | MODULE_PARM_DESC(min_buffers, |
| 70 | "The minimum number of streaming I/O buffers we are willing " |
| 71 | "to work with."); |
| 72 | |
| 73 | static int max_buffers = 10; |
| 74 | module_param(max_buffers, uint, 0644); |
| 75 | MODULE_PARM_DESC(max_buffers, |
| 76 | "The maximum number of streaming I/O buffers an application " |
| 77 | "will be allowed to allocate. These buffers are big and live " |
| 78 | "in vmalloc space."); |
| 79 | |
| 80 | static int flip; |
| 81 | module_param(flip, bool, 0444); |
| 82 | MODULE_PARM_DESC(flip, |
| 83 | "If set, the sensor will be instructed to flip the image " |
| 84 | "vertically."); |
| 85 | |
| 86 | /* |
| 87 | * Status flags. Always manipulated with bit operations. |
| 88 | */ |
| 89 | #define CF_BUF0_VALID 0 /* Buffers valid - first three */ |
| 90 | #define CF_BUF1_VALID 1 |
| 91 | #define CF_BUF2_VALID 2 |
| 92 | #define CF_DMA_ACTIVE 3 /* A frame is incoming */ |
| 93 | #define CF_CONFIG_NEEDED 4 /* Must configure hardware */ |
| 94 | |
| 95 | #define sensor_call(cam, o, f, args...) \ |
| 96 | v4l2_subdev_call(cam->sensor, o, f, ##args) |
| 97 | |
| 98 | static struct mcam_format_struct { |
| 99 | __u8 *desc; |
| 100 | __u32 pixelformat; |
| 101 | int bpp; /* Bytes per pixel */ |
| 102 | enum v4l2_mbus_pixelcode mbus_code; |
| 103 | } mcam_formats[] = { |
| 104 | { |
| 105 | .desc = "YUYV 4:2:2", |
| 106 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 107 | .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8, |
| 108 | .bpp = 2, |
| 109 | }, |
| 110 | { |
| 111 | .desc = "RGB 444", |
| 112 | .pixelformat = V4L2_PIX_FMT_RGB444, |
| 113 | .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE, |
| 114 | .bpp = 2, |
| 115 | }, |
| 116 | { |
| 117 | .desc = "RGB 565", |
| 118 | .pixelformat = V4L2_PIX_FMT_RGB565, |
| 119 | .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE, |
| 120 | .bpp = 2, |
| 121 | }, |
| 122 | { |
| 123 | .desc = "Raw RGB Bayer", |
| 124 | .pixelformat = V4L2_PIX_FMT_SBGGR8, |
| 125 | .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8, |
| 126 | .bpp = 1 |
| 127 | }, |
| 128 | }; |
| 129 | #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats) |
| 130 | |
| 131 | static struct mcam_format_struct *mcam_find_format(u32 pixelformat) |
| 132 | { |
| 133 | unsigned i; |
| 134 | |
| 135 | for (i = 0; i < N_MCAM_FMTS; i++) |
| 136 | if (mcam_formats[i].pixelformat == pixelformat) |
| 137 | return mcam_formats + i; |
| 138 | /* Not found? Then return the first format. */ |
| 139 | return mcam_formats; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Start over with DMA buffers - dev_lock needed. |
| 144 | */ |
| 145 | static void mcam_reset_buffers(struct mcam_camera *cam) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | cam->next_buf = -1; |
| 150 | for (i = 0; i < cam->nbufs; i++) |
| 151 | clear_bit(i, &cam->flags); |
| 152 | cam->specframes = 0; |
| 153 | } |
| 154 | |
| 155 | static inline int mcam_needs_config(struct mcam_camera *cam) |
| 156 | { |
| 157 | return test_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 158 | } |
| 159 | |
| 160 | static void mcam_set_config_needed(struct mcam_camera *cam, int needed) |
| 161 | { |
| 162 | if (needed) |
| 163 | set_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 164 | else |
| 165 | clear_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | * Debugging and related. FIXME these are broken |
| 171 | */ |
| 172 | #define cam_err(cam, fmt, arg...) \ |
| 173 | dev_err((cam)->dev, fmt, ##arg); |
| 174 | #define cam_warn(cam, fmt, arg...) \ |
| 175 | dev_warn((cam)->dev, fmt, ##arg); |
| 176 | #define cam_dbg(cam, fmt, arg...) \ |
| 177 | dev_dbg((cam)->dev, fmt, ##arg); |
| 178 | |
| 179 | |
| 180 | |
| 181 | /* ------------------------------------------------------------------- */ |
| 182 | /* |
| 183 | * Deal with the controller. |
| 184 | */ |
| 185 | |
| 186 | /* |
| 187 | * Do everything we think we need to have the interface operating |
| 188 | * according to the desired format. |
| 189 | */ |
| 190 | static void mcam_ctlr_dma(struct mcam_camera *cam) |
| 191 | { |
| 192 | /* |
| 193 | * Store the first two Y buffers (we aren't supporting |
| 194 | * planar formats for now, so no UV bufs). Then either |
| 195 | * set the third if it exists, or tell the controller |
| 196 | * to just use two. |
| 197 | */ |
| 198 | mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]); |
| 199 | mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]); |
| 200 | if (cam->nbufs > 2) { |
| 201 | mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]); |
| 202 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS); |
| 203 | } else |
| 204 | mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS); |
| 205 | mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */ |
| 206 | } |
| 207 | |
| 208 | static void mcam_ctlr_image(struct mcam_camera *cam) |
| 209 | { |
| 210 | int imgsz; |
| 211 | struct v4l2_pix_format *fmt = &cam->pix_format; |
| 212 | |
| 213 | imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) | |
| 214 | (fmt->bytesperline & IMGSZ_H_MASK); |
| 215 | mcam_reg_write(cam, REG_IMGSIZE, imgsz); |
| 216 | mcam_reg_write(cam, REG_IMGOFFSET, 0); |
| 217 | /* YPITCH just drops the last two bits */ |
| 218 | mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline, |
| 219 | IMGP_YP_MASK); |
| 220 | /* |
| 221 | * Tell the controller about the image format we are using. |
| 222 | */ |
| 223 | switch (cam->pix_format.pixelformat) { |
| 224 | case V4L2_PIX_FMT_YUYV: |
| 225 | mcam_reg_write_mask(cam, REG_CTRL0, |
| 226 | C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV, |
| 227 | C0_DF_MASK); |
| 228 | break; |
| 229 | |
| 230 | case V4L2_PIX_FMT_RGB444: |
| 231 | mcam_reg_write_mask(cam, REG_CTRL0, |
| 232 | C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB, |
| 233 | C0_DF_MASK); |
| 234 | /* Alpha value? */ |
| 235 | break; |
| 236 | |
| 237 | case V4L2_PIX_FMT_RGB565: |
| 238 | mcam_reg_write_mask(cam, REG_CTRL0, |
| 239 | C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR, |
| 240 | C0_DF_MASK); |
| 241 | break; |
| 242 | |
| 243 | default: |
| 244 | cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat); |
| 245 | break; |
| 246 | } |
| 247 | /* |
| 248 | * Make sure it knows we want to use hsync/vsync. |
| 249 | */ |
| 250 | mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, |
| 251 | C0_SIFM_MASK); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * Configure the controller for operation; caller holds the |
| 257 | * device mutex. |
| 258 | */ |
| 259 | static int mcam_ctlr_configure(struct mcam_camera *cam) |
| 260 | { |
| 261 | unsigned long flags; |
| 262 | |
| 263 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 264 | mcam_ctlr_dma(cam); |
| 265 | mcam_ctlr_image(cam); |
| 266 | mcam_set_config_needed(cam, 0); |
| 267 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static void mcam_ctlr_irq_enable(struct mcam_camera *cam) |
| 272 | { |
| 273 | /* |
| 274 | * Clear any pending interrupts, since we do not |
| 275 | * expect to have I/O active prior to enabling. |
| 276 | */ |
| 277 | mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); |
| 278 | mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS); |
| 279 | } |
| 280 | |
| 281 | static void mcam_ctlr_irq_disable(struct mcam_camera *cam) |
| 282 | { |
| 283 | mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Make the controller start grabbing images. Everything must |
| 288 | * be set up before doing this. |
| 289 | */ |
| 290 | static void mcam_ctlr_start(struct mcam_camera *cam) |
| 291 | { |
| 292 | /* set_bit performs a read, so no other barrier should be |
| 293 | needed here */ |
| 294 | mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE); |
| 295 | } |
| 296 | |
| 297 | static void mcam_ctlr_stop(struct mcam_camera *cam) |
| 298 | { |
| 299 | mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); |
| 300 | } |
| 301 | |
| 302 | static void mcam_ctlr_init(struct mcam_camera *cam) |
| 303 | { |
| 304 | unsigned long flags; |
| 305 | |
| 306 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 307 | /* |
| 308 | * Make sure it's not powered down. |
| 309 | */ |
| 310 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); |
| 311 | /* |
| 312 | * Turn off the enable bit. It sure should be off anyway, |
| 313 | * but it's good to be sure. |
| 314 | */ |
| 315 | mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); |
| 316 | /* |
| 317 | * Clock the sensor appropriately. Controller clock should |
| 318 | * be 48MHz, sensor "typical" value is half that. |
| 319 | */ |
| 320 | mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK); |
| 321 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | /* |
| 326 | * Stop the controller, and don't return until we're really sure that no |
| 327 | * further DMA is going on. |
| 328 | */ |
| 329 | static void mcam_ctlr_stop_dma(struct mcam_camera *cam) |
| 330 | { |
| 331 | unsigned long flags; |
| 332 | |
| 333 | /* |
| 334 | * Theory: stop the camera controller (whether it is operating |
| 335 | * or not). Delay briefly just in case we race with the SOF |
| 336 | * interrupt, then wait until no DMA is active. |
| 337 | */ |
| 338 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 339 | mcam_ctlr_stop(cam); |
| 340 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 341 | mdelay(1); |
| 342 | wait_event_timeout(cam->iowait, |
| 343 | !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ); |
| 344 | if (test_bit(CF_DMA_ACTIVE, &cam->flags)) |
| 345 | cam_err(cam, "Timeout waiting for DMA to end\n"); |
| 346 | /* This would be bad news - what now? */ |
| 347 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 348 | cam->state = S_IDLE; |
| 349 | mcam_ctlr_irq_disable(cam); |
| 350 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Power up and down. |
| 355 | */ |
| 356 | static void mcam_ctlr_power_up(struct mcam_camera *cam) |
| 357 | { |
| 358 | unsigned long flags; |
| 359 | |
| 360 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 361 | mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); |
| 362 | cam->plat_power_up(cam); |
| 363 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 364 | msleep(5); /* Just to be sure */ |
| 365 | } |
| 366 | |
| 367 | static void mcam_ctlr_power_down(struct mcam_camera *cam) |
| 368 | { |
| 369 | unsigned long flags; |
| 370 | |
| 371 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 372 | cam->plat_power_down(cam); |
| 373 | mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN); |
| 374 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 375 | } |
| 376 | |
| 377 | /* -------------------------------------------------------------------- */ |
| 378 | /* |
| 379 | * Communications with the sensor. |
| 380 | */ |
| 381 | |
| 382 | static int __mcam_cam_reset(struct mcam_camera *cam) |
| 383 | { |
| 384 | return sensor_call(cam, core, reset, 0); |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * We have found the sensor on the i2c. Let's try to have a |
| 389 | * conversation. |
| 390 | */ |
| 391 | static int mcam_cam_init(struct mcam_camera *cam) |
| 392 | { |
| 393 | struct v4l2_dbg_chip_ident chip; |
| 394 | int ret; |
| 395 | |
| 396 | mutex_lock(&cam->s_mutex); |
| 397 | if (cam->state != S_NOTREADY) |
| 398 | cam_warn(cam, "Cam init with device in funky state %d", |
| 399 | cam->state); |
| 400 | ret = __mcam_cam_reset(cam); |
| 401 | if (ret) |
| 402 | goto out; |
| 403 | chip.ident = V4L2_IDENT_NONE; |
| 404 | chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR; |
| 405 | chip.match.addr = cam->sensor_addr; |
| 406 | ret = sensor_call(cam, core, g_chip_ident, &chip); |
| 407 | if (ret) |
| 408 | goto out; |
| 409 | cam->sensor_type = chip.ident; |
| 410 | if (cam->sensor_type != V4L2_IDENT_OV7670) { |
| 411 | cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type); |
| 412 | ret = -EINVAL; |
| 413 | goto out; |
| 414 | } |
| 415 | /* Get/set parameters? */ |
| 416 | ret = 0; |
| 417 | cam->state = S_IDLE; |
| 418 | out: |
| 419 | mcam_ctlr_power_down(cam); |
| 420 | mutex_unlock(&cam->s_mutex); |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Configure the sensor to match the parameters we have. Caller should |
| 426 | * hold s_mutex |
| 427 | */ |
| 428 | static int mcam_cam_set_flip(struct mcam_camera *cam) |
| 429 | { |
| 430 | struct v4l2_control ctrl; |
| 431 | |
| 432 | memset(&ctrl, 0, sizeof(ctrl)); |
| 433 | ctrl.id = V4L2_CID_VFLIP; |
| 434 | ctrl.value = flip; |
| 435 | return sensor_call(cam, core, s_ctrl, &ctrl); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | static int mcam_cam_configure(struct mcam_camera *cam) |
| 440 | { |
| 441 | struct v4l2_mbus_framefmt mbus_fmt; |
| 442 | int ret; |
| 443 | |
| 444 | v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code); |
| 445 | ret = sensor_call(cam, core, init, 0); |
| 446 | if (ret == 0) |
| 447 | ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt); |
| 448 | /* |
| 449 | * OV7670 does weird things if flip is set *before* format... |
| 450 | */ |
| 451 | ret += mcam_cam_set_flip(cam); |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | /* -------------------------------------------------------------------- */ |
| 456 | /* |
| 457 | * DMA buffer management. These functions need s_mutex held. |
| 458 | */ |
| 459 | |
| 460 | /* FIXME: this is inefficient as hell, since dma_alloc_coherent just |
| 461 | * does a get_free_pages() call, and we waste a good chunk of an orderN |
| 462 | * allocation. Should try to allocate the whole set in one chunk. |
| 463 | */ |
| 464 | static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime) |
| 465 | { |
| 466 | int i; |
| 467 | |
| 468 | mcam_set_config_needed(cam, 1); |
| 469 | if (loadtime) |
| 470 | cam->dma_buf_size = dma_buf_size; |
| 471 | else |
| 472 | cam->dma_buf_size = cam->pix_format.sizeimage; |
| 473 | if (n_dma_bufs > 3) |
| 474 | n_dma_bufs = 3; |
| 475 | |
| 476 | cam->nbufs = 0; |
| 477 | for (i = 0; i < n_dma_bufs; i++) { |
| 478 | cam->dma_bufs[i] = dma_alloc_coherent(cam->dev, |
| 479 | cam->dma_buf_size, cam->dma_handles + i, |
| 480 | GFP_KERNEL); |
| 481 | if (cam->dma_bufs[i] == NULL) { |
| 482 | cam_warn(cam, "Failed to allocate DMA buffer\n"); |
| 483 | break; |
| 484 | } |
| 485 | /* For debug, remove eventually */ |
| 486 | memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size); |
| 487 | (cam->nbufs)++; |
| 488 | } |
| 489 | |
| 490 | switch (cam->nbufs) { |
| 491 | case 1: |
| 492 | dma_free_coherent(cam->dev, cam->dma_buf_size, |
| 493 | cam->dma_bufs[0], cam->dma_handles[0]); |
| 494 | cam->nbufs = 0; |
| 495 | case 0: |
| 496 | cam_err(cam, "Insufficient DMA buffers, cannot operate\n"); |
| 497 | return -ENOMEM; |
| 498 | |
| 499 | case 2: |
| 500 | if (n_dma_bufs > 2) |
| 501 | cam_warn(cam, "Will limp along with only 2 buffers\n"); |
| 502 | break; |
| 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static void mcam_free_dma_bufs(struct mcam_camera *cam) |
| 508 | { |
| 509 | int i; |
| 510 | |
| 511 | for (i = 0; i < cam->nbufs; i++) { |
| 512 | dma_free_coherent(cam->dev, cam->dma_buf_size, |
| 513 | cam->dma_bufs[i], cam->dma_handles[i]); |
| 514 | cam->dma_bufs[i] = NULL; |
| 515 | } |
| 516 | cam->nbufs = 0; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | /* ----------------------------------------------------------------------- */ |
| 524 | /* |
| 525 | * Here starts the V4L2 interface code. |
| 526 | */ |
| 527 | |
| 528 | /* |
| 529 | * Read an image from the device. |
| 530 | */ |
| 531 | static ssize_t mcam_deliver_buffer(struct mcam_camera *cam, |
| 532 | char __user *buffer, size_t len, loff_t *pos) |
| 533 | { |
| 534 | int bufno; |
| 535 | unsigned long flags; |
| 536 | |
| 537 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 538 | if (cam->next_buf < 0) { |
| 539 | cam_err(cam, "deliver_buffer: No next buffer\n"); |
| 540 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 541 | return -EIO; |
| 542 | } |
| 543 | bufno = cam->next_buf; |
| 544 | clear_bit(bufno, &cam->flags); |
| 545 | if (++(cam->next_buf) >= cam->nbufs) |
| 546 | cam->next_buf = 0; |
| 547 | if (!test_bit(cam->next_buf, &cam->flags)) |
| 548 | cam->next_buf = -1; |
| 549 | cam->specframes = 0; |
| 550 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 551 | |
| 552 | if (len > cam->pix_format.sizeimage) |
| 553 | len = cam->pix_format.sizeimage; |
| 554 | if (copy_to_user(buffer, cam->dma_bufs[bufno], len)) |
| 555 | return -EFAULT; |
| 556 | (*pos) += len; |
| 557 | return len; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Get everything ready, and start grabbing frames. |
| 562 | */ |
| 563 | static int mcam_read_setup(struct mcam_camera *cam, enum mcam_state state) |
| 564 | { |
| 565 | int ret; |
| 566 | unsigned long flags; |
| 567 | |
| 568 | /* |
| 569 | * Configuration. If we still don't have DMA buffers, |
| 570 | * make one last, desperate attempt. |
| 571 | */ |
| 572 | if (cam->nbufs == 0) |
| 573 | if (mcam_alloc_dma_bufs(cam, 0)) |
| 574 | return -ENOMEM; |
| 575 | |
| 576 | if (mcam_needs_config(cam)) { |
| 577 | mcam_cam_configure(cam); |
| 578 | ret = mcam_ctlr_configure(cam); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Turn it loose. |
| 585 | */ |
| 586 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 587 | mcam_reset_buffers(cam); |
| 588 | mcam_ctlr_irq_enable(cam); |
| 589 | cam->state = state; |
| 590 | mcam_ctlr_start(cam); |
| 591 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | static ssize_t mcam_v4l_read(struct file *filp, |
| 597 | char __user *buffer, size_t len, loff_t *pos) |
| 598 | { |
| 599 | struct mcam_camera *cam = filp->private_data; |
| 600 | int ret = 0; |
| 601 | |
| 602 | /* |
| 603 | * Perhaps we're in speculative read mode and already |
| 604 | * have data? |
| 605 | */ |
| 606 | mutex_lock(&cam->s_mutex); |
| 607 | if (cam->state == S_SPECREAD) { |
| 608 | if (cam->next_buf >= 0) { |
| 609 | ret = mcam_deliver_buffer(cam, buffer, len, pos); |
| 610 | if (ret != 0) |
| 611 | goto out_unlock; |
| 612 | } |
| 613 | } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) { |
| 614 | ret = -EIO; |
| 615 | goto out_unlock; |
| 616 | } else if (cam->state != S_IDLE) { |
| 617 | ret = -EBUSY; |
| 618 | goto out_unlock; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * v4l2: multiple processes can open the device, but only |
| 623 | * one gets to grab data from it. |
| 624 | */ |
| 625 | if (cam->owner && cam->owner != filp) { |
| 626 | ret = -EBUSY; |
| 627 | goto out_unlock; |
| 628 | } |
| 629 | cam->owner = filp; |
| 630 | |
| 631 | /* |
| 632 | * Do setup if need be. |
| 633 | */ |
| 634 | if (cam->state != S_SPECREAD) { |
| 635 | ret = mcam_read_setup(cam, S_SINGLEREAD); |
| 636 | if (ret) |
| 637 | goto out_unlock; |
| 638 | } |
| 639 | /* |
| 640 | * Wait for something to happen. This should probably |
| 641 | * be interruptible (FIXME). |
| 642 | */ |
| 643 | wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ); |
| 644 | if (cam->next_buf < 0) { |
| 645 | cam_err(cam, "read() operation timed out\n"); |
| 646 | mcam_ctlr_stop_dma(cam); |
| 647 | ret = -EIO; |
| 648 | goto out_unlock; |
| 649 | } |
| 650 | /* |
| 651 | * Give them their data and we should be done. |
| 652 | */ |
| 653 | ret = mcam_deliver_buffer(cam, buffer, len, pos); |
| 654 | |
| 655 | out_unlock: |
| 656 | mutex_unlock(&cam->s_mutex); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | /* |
| 668 | * Streaming I/O support. |
| 669 | */ |
| 670 | |
| 671 | |
| 672 | |
| 673 | static int mcam_vidioc_streamon(struct file *filp, void *priv, |
| 674 | enum v4l2_buf_type type) |
| 675 | { |
| 676 | struct mcam_camera *cam = filp->private_data; |
| 677 | int ret = -EINVAL; |
| 678 | |
| 679 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 680 | goto out; |
| 681 | mutex_lock(&cam->s_mutex); |
| 682 | if (cam->state != S_IDLE || cam->n_sbufs == 0) |
| 683 | goto out_unlock; |
| 684 | |
| 685 | cam->sequence = 0; |
| 686 | ret = mcam_read_setup(cam, S_STREAMING); |
| 687 | |
| 688 | out_unlock: |
| 689 | mutex_unlock(&cam->s_mutex); |
| 690 | out: |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | |
| 695 | static int mcam_vidioc_streamoff(struct file *filp, void *priv, |
| 696 | enum v4l2_buf_type type) |
| 697 | { |
| 698 | struct mcam_camera *cam = filp->private_data; |
| 699 | int ret = -EINVAL; |
| 700 | |
| 701 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 702 | goto out; |
| 703 | mutex_lock(&cam->s_mutex); |
| 704 | if (cam->state != S_STREAMING) |
| 705 | goto out_unlock; |
| 706 | |
| 707 | mcam_ctlr_stop_dma(cam); |
| 708 | ret = 0; |
| 709 | |
| 710 | out_unlock: |
| 711 | mutex_unlock(&cam->s_mutex); |
| 712 | out: |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | |
| 718 | static int mcam_setup_siobuf(struct mcam_camera *cam, int index) |
| 719 | { |
| 720 | struct mcam_sio_buffer *buf = cam->sb_bufs + index; |
| 721 | |
| 722 | INIT_LIST_HEAD(&buf->list); |
| 723 | buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage); |
| 724 | buf->buffer = vmalloc_user(buf->v4lbuf.length); |
| 725 | if (buf->buffer == NULL) |
| 726 | return -ENOMEM; |
| 727 | buf->mapcount = 0; |
| 728 | buf->cam = cam; |
| 729 | |
| 730 | buf->v4lbuf.index = index; |
| 731 | buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 732 | buf->v4lbuf.field = V4L2_FIELD_NONE; |
| 733 | buf->v4lbuf.memory = V4L2_MEMORY_MMAP; |
| 734 | /* |
| 735 | * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg |
| 736 | * just uses the length times the index, but the spec warns |
| 737 | * against doing just that - vma merging problems. So we |
| 738 | * leave a gap between each pair of buffers. |
| 739 | */ |
| 740 | buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length; |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int mcam_free_sio_buffers(struct mcam_camera *cam) |
| 745 | { |
| 746 | int i; |
| 747 | |
| 748 | /* |
| 749 | * If any buffers are mapped, we cannot free them at all. |
| 750 | */ |
| 751 | for (i = 0; i < cam->n_sbufs; i++) |
| 752 | if (cam->sb_bufs[i].mapcount > 0) |
| 753 | return -EBUSY; |
| 754 | /* |
| 755 | * OK, let's do it. |
| 756 | */ |
| 757 | for (i = 0; i < cam->n_sbufs; i++) |
| 758 | vfree(cam->sb_bufs[i].buffer); |
| 759 | cam->n_sbufs = 0; |
| 760 | kfree(cam->sb_bufs); |
| 761 | cam->sb_bufs = NULL; |
| 762 | INIT_LIST_HEAD(&cam->sb_avail); |
| 763 | INIT_LIST_HEAD(&cam->sb_full); |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | |
| 769 | static int mcam_vidioc_reqbufs(struct file *filp, void *priv, |
| 770 | struct v4l2_requestbuffers *req) |
| 771 | { |
| 772 | struct mcam_camera *cam = filp->private_data; |
| 773 | int ret = 0; /* Silence warning */ |
| 774 | |
| 775 | /* |
| 776 | * Make sure it's something we can do. User pointers could be |
| 777 | * implemented without great pain, but that's not been done yet. |
| 778 | */ |
| 779 | if (req->memory != V4L2_MEMORY_MMAP) |
| 780 | return -EINVAL; |
| 781 | /* |
| 782 | * If they ask for zero buffers, they really want us to stop streaming |
| 783 | * (if it's happening) and free everything. Should we check owner? |
| 784 | */ |
| 785 | mutex_lock(&cam->s_mutex); |
| 786 | if (req->count == 0) { |
| 787 | if (cam->state == S_STREAMING) |
| 788 | mcam_ctlr_stop_dma(cam); |
| 789 | ret = mcam_free_sio_buffers(cam); |
| 790 | goto out; |
| 791 | } |
| 792 | /* |
| 793 | * Device needs to be idle and working. We *could* try to do the |
| 794 | * right thing in S_SPECREAD by shutting things down, but it |
| 795 | * probably doesn't matter. |
| 796 | */ |
| 797 | if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) { |
| 798 | ret = -EBUSY; |
| 799 | goto out; |
| 800 | } |
| 801 | cam->owner = filp; |
| 802 | |
| 803 | if (req->count < min_buffers) |
| 804 | req->count = min_buffers; |
| 805 | else if (req->count > max_buffers) |
| 806 | req->count = max_buffers; |
| 807 | if (cam->n_sbufs > 0) { |
| 808 | ret = mcam_free_sio_buffers(cam); |
| 809 | if (ret) |
| 810 | goto out; |
| 811 | } |
| 812 | |
| 813 | cam->sb_bufs = kzalloc(req->count*sizeof(struct mcam_sio_buffer), |
| 814 | GFP_KERNEL); |
| 815 | if (cam->sb_bufs == NULL) { |
| 816 | ret = -ENOMEM; |
| 817 | goto out; |
| 818 | } |
| 819 | for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) { |
| 820 | ret = mcam_setup_siobuf(cam, cam->n_sbufs); |
| 821 | if (ret) |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | if (cam->n_sbufs == 0) /* no luck at all - ret already set */ |
| 826 | kfree(cam->sb_bufs); |
| 827 | req->count = cam->n_sbufs; /* In case of partial success */ |
| 828 | |
| 829 | out: |
| 830 | mutex_unlock(&cam->s_mutex); |
| 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | |
| 835 | static int mcam_vidioc_querybuf(struct file *filp, void *priv, |
| 836 | struct v4l2_buffer *buf) |
| 837 | { |
| 838 | struct mcam_camera *cam = filp->private_data; |
| 839 | int ret = -EINVAL; |
| 840 | |
| 841 | mutex_lock(&cam->s_mutex); |
| 842 | if (buf->index >= cam->n_sbufs) |
| 843 | goto out; |
| 844 | *buf = cam->sb_bufs[buf->index].v4lbuf; |
| 845 | ret = 0; |
| 846 | out: |
| 847 | mutex_unlock(&cam->s_mutex); |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | static int mcam_vidioc_qbuf(struct file *filp, void *priv, |
| 852 | struct v4l2_buffer *buf) |
| 853 | { |
| 854 | struct mcam_camera *cam = filp->private_data; |
| 855 | struct mcam_sio_buffer *sbuf; |
| 856 | int ret = -EINVAL; |
| 857 | unsigned long flags; |
| 858 | |
| 859 | mutex_lock(&cam->s_mutex); |
| 860 | if (buf->index >= cam->n_sbufs) |
| 861 | goto out; |
| 862 | sbuf = cam->sb_bufs + buf->index; |
| 863 | if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) { |
| 864 | ret = 0; /* Already queued?? */ |
| 865 | goto out; |
| 866 | } |
| 867 | if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) { |
| 868 | /* Spec doesn't say anything, seems appropriate tho */ |
| 869 | ret = -EBUSY; |
| 870 | goto out; |
| 871 | } |
| 872 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED; |
| 873 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 874 | list_add(&sbuf->list, &cam->sb_avail); |
| 875 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 876 | ret = 0; |
| 877 | out: |
| 878 | mutex_unlock(&cam->s_mutex); |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | static int mcam_vidioc_dqbuf(struct file *filp, void *priv, |
| 883 | struct v4l2_buffer *buf) |
| 884 | { |
| 885 | struct mcam_camera *cam = filp->private_data; |
| 886 | struct mcam_sio_buffer *sbuf; |
| 887 | int ret = -EINVAL; |
| 888 | unsigned long flags; |
| 889 | |
| 890 | mutex_lock(&cam->s_mutex); |
| 891 | if (cam->state != S_STREAMING) |
| 892 | goto out_unlock; |
| 893 | if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) { |
| 894 | ret = -EAGAIN; |
| 895 | goto out_unlock; |
| 896 | } |
| 897 | |
| 898 | while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) { |
| 899 | mutex_unlock(&cam->s_mutex); |
| 900 | if (wait_event_interruptible(cam->iowait, |
| 901 | !list_empty(&cam->sb_full))) { |
| 902 | ret = -ERESTARTSYS; |
| 903 | goto out; |
| 904 | } |
| 905 | mutex_lock(&cam->s_mutex); |
| 906 | } |
| 907 | |
| 908 | if (cam->state != S_STREAMING) |
| 909 | ret = -EINTR; |
| 910 | else { |
| 911 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 912 | /* Should probably recheck !list_empty() here */ |
| 913 | sbuf = list_entry(cam->sb_full.next, |
| 914 | struct mcam_sio_buffer, list); |
| 915 | list_del_init(&sbuf->list); |
| 916 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 917 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 918 | *buf = sbuf->v4lbuf; |
| 919 | ret = 0; |
| 920 | } |
| 921 | |
| 922 | out_unlock: |
| 923 | mutex_unlock(&cam->s_mutex); |
| 924 | out: |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | |
| 930 | static void mcam_v4l_vm_open(struct vm_area_struct *vma) |
| 931 | { |
| 932 | struct mcam_sio_buffer *sbuf = vma->vm_private_data; |
| 933 | /* |
| 934 | * Locking: done under mmap_sem, so we don't need to |
| 935 | * go back to the camera lock here. |
| 936 | */ |
| 937 | sbuf->mapcount++; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | static void mcam_v4l_vm_close(struct vm_area_struct *vma) |
| 942 | { |
| 943 | struct mcam_sio_buffer *sbuf = vma->vm_private_data; |
| 944 | |
| 945 | mutex_lock(&sbuf->cam->s_mutex); |
| 946 | sbuf->mapcount--; |
| 947 | /* Docs say we should stop I/O too... */ |
| 948 | if (sbuf->mapcount == 0) |
| 949 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED; |
| 950 | mutex_unlock(&sbuf->cam->s_mutex); |
| 951 | } |
| 952 | |
| 953 | static const struct vm_operations_struct mcam_v4l_vm_ops = { |
| 954 | .open = mcam_v4l_vm_open, |
| 955 | .close = mcam_v4l_vm_close |
| 956 | }; |
| 957 | |
| 958 | |
| 959 | static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma) |
| 960 | { |
| 961 | struct mcam_camera *cam = filp->private_data; |
| 962 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
| 963 | int ret = -EINVAL; |
| 964 | int i; |
| 965 | struct mcam_sio_buffer *sbuf = NULL; |
| 966 | |
| 967 | if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) |
| 968 | return -EINVAL; |
| 969 | /* |
| 970 | * Find the buffer they are looking for. |
| 971 | */ |
| 972 | mutex_lock(&cam->s_mutex); |
| 973 | for (i = 0; i < cam->n_sbufs; i++) |
| 974 | if (cam->sb_bufs[i].v4lbuf.m.offset == offset) { |
| 975 | sbuf = cam->sb_bufs + i; |
| 976 | break; |
| 977 | } |
| 978 | if (sbuf == NULL) |
| 979 | goto out; |
| 980 | |
| 981 | ret = remap_vmalloc_range(vma, sbuf->buffer, 0); |
| 982 | if (ret) |
| 983 | goto out; |
| 984 | vma->vm_flags |= VM_DONTEXPAND; |
| 985 | vma->vm_private_data = sbuf; |
| 986 | vma->vm_ops = &mcam_v4l_vm_ops; |
| 987 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED; |
| 988 | mcam_v4l_vm_open(vma); |
| 989 | ret = 0; |
| 990 | out: |
| 991 | mutex_unlock(&cam->s_mutex); |
| 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | |
| 996 | |
| 997 | static int mcam_v4l_open(struct file *filp) |
| 998 | { |
| 999 | struct mcam_camera *cam = video_drvdata(filp); |
| 1000 | |
| 1001 | filp->private_data = cam; |
| 1002 | |
| 1003 | mutex_lock(&cam->s_mutex); |
| 1004 | if (cam->users == 0) { |
| 1005 | mcam_ctlr_power_up(cam); |
| 1006 | __mcam_cam_reset(cam); |
| 1007 | mcam_set_config_needed(cam, 1); |
| 1008 | /* FIXME make sure this is complete */ |
| 1009 | } |
| 1010 | (cam->users)++; |
| 1011 | mutex_unlock(&cam->s_mutex); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | static int mcam_v4l_release(struct file *filp) |
| 1017 | { |
| 1018 | struct mcam_camera *cam = filp->private_data; |
| 1019 | |
| 1020 | mutex_lock(&cam->s_mutex); |
| 1021 | (cam->users)--; |
| 1022 | if (filp == cam->owner) { |
| 1023 | mcam_ctlr_stop_dma(cam); |
| 1024 | mcam_free_sio_buffers(cam); |
| 1025 | cam->owner = NULL; |
| 1026 | } |
| 1027 | if (cam->users == 0) { |
| 1028 | mcam_ctlr_power_down(cam); |
| 1029 | if (alloc_bufs_at_read) |
| 1030 | mcam_free_dma_bufs(cam); |
| 1031 | } |
| 1032 | mutex_unlock(&cam->s_mutex); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | static unsigned int mcam_v4l_poll(struct file *filp, |
| 1039 | struct poll_table_struct *pt) |
| 1040 | { |
| 1041 | struct mcam_camera *cam = filp->private_data; |
| 1042 | |
| 1043 | poll_wait(filp, &cam->iowait, pt); |
| 1044 | if (cam->next_buf >= 0) |
| 1045 | return POLLIN | POLLRDNORM; |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | static int mcam_vidioc_queryctrl(struct file *filp, void *priv, |
| 1052 | struct v4l2_queryctrl *qc) |
| 1053 | { |
| 1054 | struct mcam_camera *cam = priv; |
| 1055 | int ret; |
| 1056 | |
| 1057 | mutex_lock(&cam->s_mutex); |
| 1058 | ret = sensor_call(cam, core, queryctrl, qc); |
| 1059 | mutex_unlock(&cam->s_mutex); |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | |
| 1064 | static int mcam_vidioc_g_ctrl(struct file *filp, void *priv, |
| 1065 | struct v4l2_control *ctrl) |
| 1066 | { |
| 1067 | struct mcam_camera *cam = priv; |
| 1068 | int ret; |
| 1069 | |
| 1070 | mutex_lock(&cam->s_mutex); |
| 1071 | ret = sensor_call(cam, core, g_ctrl, ctrl); |
| 1072 | mutex_unlock(&cam->s_mutex); |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | static int mcam_vidioc_s_ctrl(struct file *filp, void *priv, |
| 1078 | struct v4l2_control *ctrl) |
| 1079 | { |
| 1080 | struct mcam_camera *cam = priv; |
| 1081 | int ret; |
| 1082 | |
| 1083 | mutex_lock(&cam->s_mutex); |
| 1084 | ret = sensor_call(cam, core, s_ctrl, ctrl); |
| 1085 | mutex_unlock(&cam->s_mutex); |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | static int mcam_vidioc_querycap(struct file *file, void *priv, |
| 1094 | struct v4l2_capability *cap) |
| 1095 | { |
| 1096 | strcpy(cap->driver, "marvell_ccic"); |
| 1097 | strcpy(cap->card, "marvell_ccic"); |
| 1098 | cap->version = 1; |
| 1099 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | |
| 1100 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | /* |
| 1106 | * The default format we use until somebody says otherwise. |
| 1107 | */ |
| 1108 | static const struct v4l2_pix_format mcam_def_pix_format = { |
| 1109 | .width = VGA_WIDTH, |
| 1110 | .height = VGA_HEIGHT, |
| 1111 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 1112 | .field = V4L2_FIELD_NONE, |
| 1113 | .bytesperline = VGA_WIDTH*2, |
| 1114 | .sizeimage = VGA_WIDTH*VGA_HEIGHT*2, |
| 1115 | }; |
| 1116 | |
| 1117 | static const enum v4l2_mbus_pixelcode mcam_def_mbus_code = |
| 1118 | V4L2_MBUS_FMT_YUYV8_2X8; |
| 1119 | |
| 1120 | static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp, |
| 1121 | void *priv, struct v4l2_fmtdesc *fmt) |
| 1122 | { |
| 1123 | if (fmt->index >= N_MCAM_FMTS) |
| 1124 | return -EINVAL; |
| 1125 | strlcpy(fmt->description, mcam_formats[fmt->index].desc, |
| 1126 | sizeof(fmt->description)); |
| 1127 | fmt->pixelformat = mcam_formats[fmt->index].pixelformat; |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv, |
| 1132 | struct v4l2_format *fmt) |
| 1133 | { |
| 1134 | struct mcam_camera *cam = priv; |
| 1135 | struct mcam_format_struct *f; |
| 1136 | struct v4l2_pix_format *pix = &fmt->fmt.pix; |
| 1137 | struct v4l2_mbus_framefmt mbus_fmt; |
| 1138 | int ret; |
| 1139 | |
| 1140 | f = mcam_find_format(pix->pixelformat); |
| 1141 | pix->pixelformat = f->pixelformat; |
| 1142 | v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code); |
| 1143 | mutex_lock(&cam->s_mutex); |
| 1144 | ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt); |
| 1145 | mutex_unlock(&cam->s_mutex); |
| 1146 | v4l2_fill_pix_format(pix, &mbus_fmt); |
| 1147 | pix->bytesperline = pix->width * f->bpp; |
| 1148 | pix->sizeimage = pix->height * pix->bytesperline; |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
| 1152 | static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv, |
| 1153 | struct v4l2_format *fmt) |
| 1154 | { |
| 1155 | struct mcam_camera *cam = priv; |
| 1156 | struct mcam_format_struct *f; |
| 1157 | int ret; |
| 1158 | |
| 1159 | /* |
| 1160 | * Can't do anything if the device is not idle |
| 1161 | * Also can't if there are streaming buffers in place. |
| 1162 | */ |
| 1163 | if (cam->state != S_IDLE || cam->n_sbufs > 0) |
| 1164 | return -EBUSY; |
| 1165 | |
| 1166 | f = mcam_find_format(fmt->fmt.pix.pixelformat); |
| 1167 | |
| 1168 | /* |
| 1169 | * See if the formatting works in principle. |
| 1170 | */ |
| 1171 | ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt); |
| 1172 | if (ret) |
| 1173 | return ret; |
| 1174 | /* |
| 1175 | * Now we start to change things for real, so let's do it |
| 1176 | * under lock. |
| 1177 | */ |
| 1178 | mutex_lock(&cam->s_mutex); |
| 1179 | cam->pix_format = fmt->fmt.pix; |
| 1180 | cam->mbus_code = f->mbus_code; |
| 1181 | |
| 1182 | /* |
| 1183 | * Make sure we have appropriate DMA buffers. |
| 1184 | */ |
| 1185 | ret = -ENOMEM; |
| 1186 | if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage) |
| 1187 | mcam_free_dma_bufs(cam); |
| 1188 | if (cam->nbufs == 0) { |
| 1189 | if (mcam_alloc_dma_bufs(cam, 0)) |
| 1190 | goto out; |
| 1191 | } |
| 1192 | /* |
| 1193 | * It looks like this might work, so let's program the sensor. |
| 1194 | */ |
| 1195 | ret = mcam_cam_configure(cam); |
| 1196 | if (!ret) |
| 1197 | ret = mcam_ctlr_configure(cam); |
| 1198 | out: |
| 1199 | mutex_unlock(&cam->s_mutex); |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * Return our stored notion of how the camera is/should be configured. |
| 1205 | * The V4l2 spec wants us to be smarter, and actually get this from |
| 1206 | * the camera (and not mess with it at open time). Someday. |
| 1207 | */ |
| 1208 | static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv, |
| 1209 | struct v4l2_format *f) |
| 1210 | { |
| 1211 | struct mcam_camera *cam = priv; |
| 1212 | |
| 1213 | f->fmt.pix = cam->pix_format; |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * We only have one input - the sensor - so minimize the nonsense here. |
| 1219 | */ |
| 1220 | static int mcam_vidioc_enum_input(struct file *filp, void *priv, |
| 1221 | struct v4l2_input *input) |
| 1222 | { |
| 1223 | if (input->index != 0) |
| 1224 | return -EINVAL; |
| 1225 | |
| 1226 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 1227 | input->std = V4L2_STD_ALL; /* Not sure what should go here */ |
| 1228 | strcpy(input->name, "Camera"); |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 1233 | { |
| 1234 | *i = 0; |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 1239 | { |
| 1240 | if (i != 0) |
| 1241 | return -EINVAL; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | /* from vivi.c */ |
| 1246 | static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a) |
| 1247 | { |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * G/S_PARM. Most of this is done by the sensor, but we are |
| 1253 | * the level which controls the number of read buffers. |
| 1254 | */ |
| 1255 | static int mcam_vidioc_g_parm(struct file *filp, void *priv, |
| 1256 | struct v4l2_streamparm *parms) |
| 1257 | { |
| 1258 | struct mcam_camera *cam = priv; |
| 1259 | int ret; |
| 1260 | |
| 1261 | mutex_lock(&cam->s_mutex); |
| 1262 | ret = sensor_call(cam, video, g_parm, parms); |
| 1263 | mutex_unlock(&cam->s_mutex); |
| 1264 | parms->parm.capture.readbuffers = n_dma_bufs; |
| 1265 | return ret; |
| 1266 | } |
| 1267 | |
| 1268 | static int mcam_vidioc_s_parm(struct file *filp, void *priv, |
| 1269 | struct v4l2_streamparm *parms) |
| 1270 | { |
| 1271 | struct mcam_camera *cam = priv; |
| 1272 | int ret; |
| 1273 | |
| 1274 | mutex_lock(&cam->s_mutex); |
| 1275 | ret = sensor_call(cam, video, s_parm, parms); |
| 1276 | mutex_unlock(&cam->s_mutex); |
| 1277 | parms->parm.capture.readbuffers = n_dma_bufs; |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | static int mcam_vidioc_g_chip_ident(struct file *file, void *priv, |
| 1282 | struct v4l2_dbg_chip_ident *chip) |
| 1283 | { |
| 1284 | struct mcam_camera *cam = priv; |
| 1285 | |
| 1286 | chip->ident = V4L2_IDENT_NONE; |
| 1287 | chip->revision = 0; |
| 1288 | if (v4l2_chip_match_host(&chip->match)) { |
| 1289 | chip->ident = cam->chip_id; |
| 1290 | return 0; |
| 1291 | } |
| 1292 | return sensor_call(cam, core, g_chip_ident, chip); |
| 1293 | } |
| 1294 | |
| 1295 | static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv, |
| 1296 | struct v4l2_frmsizeenum *sizes) |
| 1297 | { |
| 1298 | struct mcam_camera *cam = priv; |
| 1299 | int ret; |
| 1300 | |
| 1301 | mutex_lock(&cam->s_mutex); |
| 1302 | ret = sensor_call(cam, video, enum_framesizes, sizes); |
| 1303 | mutex_unlock(&cam->s_mutex); |
| 1304 | return ret; |
| 1305 | } |
| 1306 | |
| 1307 | static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv, |
| 1308 | struct v4l2_frmivalenum *interval) |
| 1309 | { |
| 1310 | struct mcam_camera *cam = priv; |
| 1311 | int ret; |
| 1312 | |
| 1313 | mutex_lock(&cam->s_mutex); |
| 1314 | ret = sensor_call(cam, video, enum_frameintervals, interval); |
| 1315 | mutex_unlock(&cam->s_mutex); |
| 1316 | return ret; |
| 1317 | } |
| 1318 | |
| 1319 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1320 | static int mcam_vidioc_g_register(struct file *file, void *priv, |
| 1321 | struct v4l2_dbg_register *reg) |
| 1322 | { |
| 1323 | struct mcam_camera *cam = priv; |
| 1324 | |
| 1325 | if (v4l2_chip_match_host(®->match)) { |
| 1326 | reg->val = mcam_reg_read(cam, reg->reg); |
| 1327 | reg->size = 4; |
| 1328 | return 0; |
| 1329 | } |
| 1330 | return sensor_call(cam, core, g_register, reg); |
| 1331 | } |
| 1332 | |
| 1333 | static int mcam_vidioc_s_register(struct file *file, void *priv, |
| 1334 | struct v4l2_dbg_register *reg) |
| 1335 | { |
| 1336 | struct mcam_camera *cam = priv; |
| 1337 | |
| 1338 | if (v4l2_chip_match_host(®->match)) { |
| 1339 | mcam_reg_write(cam, reg->reg, reg->val); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | return sensor_call(cam, core, s_register, reg); |
| 1343 | } |
| 1344 | #endif |
| 1345 | |
| 1346 | /* |
| 1347 | * This template device holds all of those v4l2 methods; we |
| 1348 | * clone it for specific real devices. |
| 1349 | */ |
| 1350 | |
| 1351 | static const struct v4l2_file_operations mcam_v4l_fops = { |
| 1352 | .owner = THIS_MODULE, |
| 1353 | .open = mcam_v4l_open, |
| 1354 | .release = mcam_v4l_release, |
| 1355 | .read = mcam_v4l_read, |
| 1356 | .poll = mcam_v4l_poll, |
| 1357 | .mmap = mcam_v4l_mmap, |
| 1358 | .unlocked_ioctl = video_ioctl2, |
| 1359 | }; |
| 1360 | |
| 1361 | static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = { |
| 1362 | .vidioc_querycap = mcam_vidioc_querycap, |
| 1363 | .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap, |
| 1364 | .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap, |
| 1365 | .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap, |
| 1366 | .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap, |
| 1367 | .vidioc_enum_input = mcam_vidioc_enum_input, |
| 1368 | .vidioc_g_input = mcam_vidioc_g_input, |
| 1369 | .vidioc_s_input = mcam_vidioc_s_input, |
| 1370 | .vidioc_s_std = mcam_vidioc_s_std, |
| 1371 | .vidioc_reqbufs = mcam_vidioc_reqbufs, |
| 1372 | .vidioc_querybuf = mcam_vidioc_querybuf, |
| 1373 | .vidioc_qbuf = mcam_vidioc_qbuf, |
| 1374 | .vidioc_dqbuf = mcam_vidioc_dqbuf, |
| 1375 | .vidioc_streamon = mcam_vidioc_streamon, |
| 1376 | .vidioc_streamoff = mcam_vidioc_streamoff, |
| 1377 | .vidioc_queryctrl = mcam_vidioc_queryctrl, |
| 1378 | .vidioc_g_ctrl = mcam_vidioc_g_ctrl, |
| 1379 | .vidioc_s_ctrl = mcam_vidioc_s_ctrl, |
| 1380 | .vidioc_g_parm = mcam_vidioc_g_parm, |
| 1381 | .vidioc_s_parm = mcam_vidioc_s_parm, |
| 1382 | .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes, |
| 1383 | .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals, |
| 1384 | .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident, |
| 1385 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1386 | .vidioc_g_register = mcam_vidioc_g_register, |
| 1387 | .vidioc_s_register = mcam_vidioc_s_register, |
| 1388 | #endif |
| 1389 | }; |
| 1390 | |
| 1391 | static struct video_device mcam_v4l_template = { |
| 1392 | .name = "mcam", |
| 1393 | .tvnorms = V4L2_STD_NTSC_M, |
| 1394 | .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */ |
| 1395 | |
| 1396 | .fops = &mcam_v4l_fops, |
| 1397 | .ioctl_ops = &mcam_v4l_ioctl_ops, |
| 1398 | .release = video_device_release_empty, |
| 1399 | }; |
| 1400 | |
| 1401 | /* ---------------------------------------------------------------------- */ |
| 1402 | /* |
| 1403 | * Interrupt handler stuff |
| 1404 | */ |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | static void mcam_frame_tasklet(unsigned long data) |
| 1409 | { |
| 1410 | struct mcam_camera *cam = (struct mcam_camera *) data; |
| 1411 | int i; |
| 1412 | unsigned long flags; |
| 1413 | struct mcam_sio_buffer *sbuf; |
| 1414 | |
| 1415 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 1416 | for (i = 0; i < cam->nbufs; i++) { |
| 1417 | int bufno = cam->next_buf; |
| 1418 | if (bufno < 0) { /* "will never happen" */ |
| 1419 | cam_err(cam, "No valid bufs in tasklet!\n"); |
| 1420 | break; |
| 1421 | } |
| 1422 | if (++(cam->next_buf) >= cam->nbufs) |
| 1423 | cam->next_buf = 0; |
| 1424 | if (!test_bit(bufno, &cam->flags)) |
| 1425 | continue; |
| 1426 | if (list_empty(&cam->sb_avail)) |
| 1427 | break; /* Leave it valid, hope for better later */ |
| 1428 | clear_bit(bufno, &cam->flags); |
| 1429 | sbuf = list_entry(cam->sb_avail.next, |
| 1430 | struct mcam_sio_buffer, list); |
| 1431 | /* |
| 1432 | * Drop the lock during the big copy. This *should* be safe... |
| 1433 | */ |
| 1434 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 1435 | memcpy(sbuf->buffer, cam->dma_bufs[bufno], |
| 1436 | cam->pix_format.sizeimage); |
| 1437 | sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage; |
| 1438 | sbuf->v4lbuf.sequence = cam->buf_seq[bufno]; |
| 1439 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED; |
| 1440 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE; |
| 1441 | spin_lock_irqsave(&cam->dev_lock, flags); |
| 1442 | list_move_tail(&sbuf->list, &cam->sb_full); |
| 1443 | } |
| 1444 | if (!list_empty(&cam->sb_full)) |
| 1445 | wake_up(&cam->iowait); |
| 1446 | spin_unlock_irqrestore(&cam->dev_lock, flags); |
| 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | static void mcam_frame_complete(struct mcam_camera *cam, int frame) |
| 1452 | { |
| 1453 | /* |
| 1454 | * Basic frame housekeeping. |
| 1455 | */ |
| 1456 | if (test_bit(frame, &cam->flags) && printk_ratelimit()) |
| 1457 | cam_err(cam, "Frame overrun on %d, frames lost\n", frame); |
| 1458 | set_bit(frame, &cam->flags); |
| 1459 | clear_bit(CF_DMA_ACTIVE, &cam->flags); |
| 1460 | if (cam->next_buf < 0) |
| 1461 | cam->next_buf = frame; |
| 1462 | cam->buf_seq[frame] = ++(cam->sequence); |
| 1463 | |
| 1464 | switch (cam->state) { |
| 1465 | /* |
| 1466 | * If in single read mode, try going speculative. |
| 1467 | */ |
| 1468 | case S_SINGLEREAD: |
| 1469 | cam->state = S_SPECREAD; |
| 1470 | cam->specframes = 0; |
| 1471 | wake_up(&cam->iowait); |
| 1472 | break; |
| 1473 | |
| 1474 | /* |
| 1475 | * If we are already doing speculative reads, and nobody is |
| 1476 | * reading them, just stop. |
| 1477 | */ |
| 1478 | case S_SPECREAD: |
| 1479 | if (++(cam->specframes) >= cam->nbufs) { |
| 1480 | mcam_ctlr_stop(cam); |
| 1481 | mcam_ctlr_irq_disable(cam); |
| 1482 | cam->state = S_IDLE; |
| 1483 | } |
| 1484 | wake_up(&cam->iowait); |
| 1485 | break; |
| 1486 | /* |
| 1487 | * For the streaming case, we defer the real work to the |
| 1488 | * camera tasklet. |
| 1489 | * |
| 1490 | * FIXME: if the application is not consuming the buffers, |
| 1491 | * we should eventually put things on hold and restart in |
| 1492 | * vidioc_dqbuf(). |
| 1493 | */ |
| 1494 | case S_STREAMING: |
| 1495 | tasklet_schedule(&cam->s_tasklet); |
| 1496 | break; |
| 1497 | |
| 1498 | default: |
| 1499 | cam_err(cam, "Frame interrupt in non-operational state\n"); |
| 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | int mccic_irq(struct mcam_camera *cam, unsigned int irqs) |
| 1508 | { |
| 1509 | unsigned int frame, handled = 0; |
| 1510 | |
| 1511 | mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */ |
| 1512 | /* |
| 1513 | * Handle any frame completions. There really should |
| 1514 | * not be more than one of these, or we have fallen |
| 1515 | * far behind. |
| 1516 | */ |
| 1517 | for (frame = 0; frame < cam->nbufs; frame++) |
| 1518 | if (irqs & (IRQ_EOF0 << frame)) { |
| 1519 | mcam_frame_complete(cam, frame); |
| 1520 | handled = 1; |
| 1521 | } |
| 1522 | /* |
| 1523 | * If a frame starts, note that we have DMA active. This |
| 1524 | * code assumes that we won't get multiple frame interrupts |
| 1525 | * at once; may want to rethink that. |
| 1526 | */ |
| 1527 | if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) { |
| 1528 | set_bit(CF_DMA_ACTIVE, &cam->flags); |
| 1529 | handled = 1; |
| 1530 | } |
| 1531 | return handled; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * Registration and such. |
| 1536 | */ |
| 1537 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1538 | static struct ov7670_config sensor_cfg = { |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1539 | /* |
| 1540 | * Exclude QCIF mode, because it only captures a tiny portion |
| 1541 | * of the sensor FOV |
| 1542 | */ |
| 1543 | .min_width = 320, |
| 1544 | .min_height = 240, |
| 1545 | }; |
| 1546 | |
| 1547 | |
| 1548 | int mccic_register(struct mcam_camera *cam) |
| 1549 | { |
| 1550 | struct i2c_board_info ov7670_info = { |
| 1551 | .type = "ov7670", |
Jonathan Corbet | 1c68f88 | 2011-06-11 14:46:47 -0300 | [diff] [blame] | 1552 | .addr = 0x42 >> 1, |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1553 | .platform_data = &sensor_cfg, |
| 1554 | }; |
| 1555 | int ret; |
| 1556 | |
| 1557 | /* |
| 1558 | * Register with V4L |
| 1559 | */ |
| 1560 | ret = v4l2_device_register(cam->dev, &cam->v4l2_dev); |
| 1561 | if (ret) |
| 1562 | return ret; |
| 1563 | |
| 1564 | mutex_init(&cam->s_mutex); |
| 1565 | cam->state = S_NOTREADY; |
| 1566 | mcam_set_config_needed(cam, 1); |
| 1567 | init_waitqueue_head(&cam->iowait); |
| 1568 | cam->pix_format = mcam_def_pix_format; |
| 1569 | cam->mbus_code = mcam_def_mbus_code; |
| 1570 | INIT_LIST_HEAD(&cam->dev_list); |
| 1571 | INIT_LIST_HEAD(&cam->sb_avail); |
| 1572 | INIT_LIST_HEAD(&cam->sb_full); |
| 1573 | tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam); |
| 1574 | |
| 1575 | mcam_ctlr_init(cam); |
| 1576 | |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1577 | /* |
| 1578 | * Try to find the sensor. |
| 1579 | */ |
Jonathan Corbet | 2164b5a | 2011-06-11 14:46:44 -0300 | [diff] [blame] | 1580 | sensor_cfg.clock_speed = cam->clock_speed; |
| 1581 | sensor_cfg.use_smbus = cam->use_smbus; |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1582 | cam->sensor_addr = ov7670_info.addr; |
| 1583 | cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, |
Jonathan Corbet | 595a93a | 2011-06-11 14:46:48 -0300 | [diff] [blame^] | 1584 | cam->i2c_adapter, &ov7670_info, NULL); |
Jonathan Corbet | abfa3df | 2011-06-11 14:46:43 -0300 | [diff] [blame] | 1585 | if (cam->sensor == NULL) { |
| 1586 | ret = -ENODEV; |
| 1587 | goto out_unregister; |
| 1588 | } |
| 1589 | |
| 1590 | ret = mcam_cam_init(cam); |
| 1591 | if (ret) |
| 1592 | goto out_unregister; |
| 1593 | /* |
| 1594 | * Get the v4l2 setup done. |
| 1595 | */ |
| 1596 | mutex_lock(&cam->s_mutex); |
| 1597 | cam->vdev = mcam_v4l_template; |
| 1598 | cam->vdev.debug = 0; |
| 1599 | cam->vdev.v4l2_dev = &cam->v4l2_dev; |
| 1600 | ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1); |
| 1601 | if (ret) |
| 1602 | goto out; |
| 1603 | video_set_drvdata(&cam->vdev, cam); |
| 1604 | |
| 1605 | /* |
| 1606 | * If so requested, try to get our DMA buffers now. |
| 1607 | */ |
| 1608 | if (!alloc_bufs_at_read) { |
| 1609 | if (mcam_alloc_dma_bufs(cam, 1)) |
| 1610 | cam_warn(cam, "Unable to alloc DMA buffers at load" |
| 1611 | " will try again later."); |
| 1612 | } |
| 1613 | |
| 1614 | out: |
| 1615 | mutex_unlock(&cam->s_mutex); |
| 1616 | return ret; |
| 1617 | out_unregister: |
| 1618 | v4l2_device_unregister(&cam->v4l2_dev); |
| 1619 | return ret; |
| 1620 | } |
| 1621 | |
| 1622 | |
| 1623 | void mccic_shutdown(struct mcam_camera *cam) |
| 1624 | { |
| 1625 | if (cam->users > 0) |
| 1626 | cam_warn(cam, "Removing a device with users!\n"); |
| 1627 | if (cam->n_sbufs > 0) |
| 1628 | /* What if they are still mapped? Shouldn't be, but... */ |
| 1629 | mcam_free_sio_buffers(cam); |
| 1630 | mcam_ctlr_stop_dma(cam); |
| 1631 | mcam_ctlr_power_down(cam); |
| 1632 | mcam_free_dma_bufs(cam); |
| 1633 | video_unregister_device(&cam->vdev); |
| 1634 | v4l2_device_unregister(&cam->v4l2_dev); |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * Power management |
| 1639 | */ |
| 1640 | #ifdef CONFIG_PM |
| 1641 | |
| 1642 | void mccic_suspend(struct mcam_camera *cam) |
| 1643 | { |
| 1644 | enum mcam_state cstate = cam->state; |
| 1645 | |
| 1646 | mcam_ctlr_stop_dma(cam); |
| 1647 | mcam_ctlr_power_down(cam); |
| 1648 | cam->state = cstate; |
| 1649 | } |
| 1650 | |
| 1651 | int mccic_resume(struct mcam_camera *cam) |
| 1652 | { |
| 1653 | int ret = 0; |
| 1654 | |
| 1655 | mutex_lock(&cam->s_mutex); |
| 1656 | if (cam->users > 0) { |
| 1657 | mcam_ctlr_power_up(cam); |
| 1658 | __mcam_cam_reset(cam); |
| 1659 | } else { |
| 1660 | mcam_ctlr_power_down(cam); |
| 1661 | } |
| 1662 | mutex_unlock(&cam->s_mutex); |
| 1663 | |
| 1664 | set_bit(CF_CONFIG_NEEDED, &cam->flags); |
| 1665 | if (cam->state == S_SPECREAD) |
| 1666 | cam->state = S_IDLE; /* Don't bother restarting */ |
| 1667 | else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING) |
| 1668 | ret = mcam_read_setup(cam, cam->state); |
| 1669 | return ret; |
| 1670 | } |
| 1671 | #endif /* CONFIG_PM */ |