2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
6 * The data sheet for this device can be found at:
7 * http://www.marvell.com/products/pcconn/88ALP01.jsp
9 * Copyright 2006 One Laptop Per Child Association, Inc.
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
12 * Written by Jonathan Corbet, corbet@lwn.net.
14 * v4l2_device/v4l2_subdev conversion by:
15 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
17 * Note: this conversion is untested! Please contact the linux-media
18 * mailinglist if you can test this, together with the test results.
20 * This file may be distributed under the terms of the GNU General
21 * Public License, version 2.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
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>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-ioctl.h>
36 #include <media/v4l2-chip-ident.h>
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>
42 #include <linux/jiffies.h>
43 #include <linux/vmalloc.h>
45 #include <asm/uaccess.h>
48 #include "cafe_ccic-regs.h"
50 #define CAFE_VERSION 0x000002
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");
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.
69 * The controller can cycle through three buffers. We could use
70 * more by flipping pointers around, but it probably makes little
74 #define MAX_DMA_BUFS 3
75 static int alloc_bufs_at_read;
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.");
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.");
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 "
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 "
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.");
110 module_param(flip, bool, 0444);
111 MODULE_PARM_DESC(flip,
112 "If set, the sensor will be instructed to flip the image "
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 */
126 * Tracking of streaming I/O buffers.
128 struct cafe_sio_buffer {
129 struct list_head list;
130 struct v4l2_buffer v4lbuf;
131 char *buffer; /* Where it lives in kernel space */
133 struct cafe_camera *cam;
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.
144 struct v4l2_device v4l2_dev;
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) */
151 * Subsystem structures.
153 struct pci_dev *pdev;
154 struct video_device vdev;
155 struct i2c_adapter i2c_adapter;
156 struct v4l2_subdev *sensor;
157 unsigned short sensor_addr;
159 unsigned char __iomem *regs;
160 struct list_head dev_list; /* link to other devices */
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 */
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;
179 /* Current operating parameters */
180 u32 sensor_type; /* Currently ov7670 only */
181 struct v4l2_pix_format pix_format;
184 struct mutex s_mutex; /* Access to this structure */
185 spinlock_t dev_lock; /* Access to device */
188 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
189 wait_queue_head_t iowait; /* Waiting on frame data */
193 * Status flags. Always manipulated with bit operations.
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 */
201 #define sensor_call(cam, o, f, args...) \
202 v4l2_subdev_call(cam->sensor, o, f, ##args)
204 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
206 return container_of(dev, struct cafe_camera, v4l2_dev);
211 * Start over with DMA buffers - dev_lock needed.
213 static void cafe_reset_buffers(struct cafe_camera *cam)
218 for (i = 0; i < cam->nbufs; i++)
219 clear_bit(i, &cam->flags);
223 static inline int cafe_needs_config(struct cafe_camera *cam)
225 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
228 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
231 set_bit(CF_CONFIG_NEEDED, &cam->flags);
233 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
240 * Debugging and related.
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);
250 /* ---------------------------------------------------------------------*/
253 * Device register I/O
255 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
258 iowrite32(val, cam->regs + reg);
261 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
264 return ioread32(cam->regs + reg);
268 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
269 unsigned int val, unsigned int mask)
271 unsigned int v = cafe_reg_read(cam, reg);
273 v = (v & ~mask) | (val & mask);
274 cafe_reg_write(cam, reg, v);
277 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
278 unsigned int reg, unsigned int val)
280 cafe_reg_write_mask(cam, reg, 0, val);
283 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
284 unsigned int reg, unsigned int val)
286 cafe_reg_write_mask(cam, reg, val, val);
291 /* -------------------------------------------------------------------- */
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.
297 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
299 static int cafe_smbus_write_done(struct cafe_camera *cam)
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
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;
316 static int cafe_smbus_write_data(struct cafe_camera *cam,
317 u16 addr, u8 command, u8 value)
321 DEFINE_WAIT(the_wait);
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 */
327 * Marvell sez set clkdiv to all 1's for now.
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);
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.
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));
354 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
355 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
358 spin_lock_irqsave(&cam->dev_lock, flags);
359 rval = cafe_reg_read(cam, REG_TWSIC1);
360 spin_unlock_irqrestore(&cam->dev_lock, flags);
362 if (rval & TWSIC1_WSTAT) {
363 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
367 if (rval & TWSIC1_ERROR) {
368 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
377 static int cafe_smbus_read_done(struct cafe_camera *cam)
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
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);
396 static int cafe_smbus_read_data(struct cafe_camera *cam,
397 u16 addr, u8 command, u8 *value)
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 */
406 * Marvel sez set clkdiv to all 1's for now.
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);
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);
421 if (rval & TWSIC1_ERROR) {
422 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
425 if (! (rval & TWSIC1_RVALID)) {
426 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
430 *value = rval & 0xff;
435 * Perform a transfer over SMBUS. This thing is called under
436 * the i2c bus lock, so we shouldn't race with ourselves...
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)
442 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
443 struct cafe_camera *cam = to_cam(v4l2_dev);
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.
450 if (size != I2C_SMBUS_BYTE_DATA) {
451 cam_err(cam, "funky xfer size %d\n", size);
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);
463 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
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);
472 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
474 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
475 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
478 static struct i2c_algorithm cafe_smbus_algo = {
479 .smbus_xfer = cafe_smbus_xfer,
480 .functionality = cafe_smbus_func
483 /* Somebody is on the bus */
484 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
485 static void cafe_ctlr_power_down(struct cafe_camera *cam);
487 static int cafe_smbus_setup(struct cafe_camera *cam)
489 struct i2c_adapter *adap = &cam->i2c_adapter;
492 cafe_smbus_enable_irq(cam);
493 adap->id = I2C_HW_SMBUS_CAFE;
494 adap->owner = THIS_MODULE;
495 adap->algo = &cafe_smbus_algo;
496 strcpy(adap->name, "cafe_ccic");
497 adap->dev.parent = &cam->pdev->dev;
498 i2c_set_adapdata(adap, &cam->v4l2_dev);
499 ret = i2c_add_adapter(adap);
501 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
505 static void cafe_smbus_shutdown(struct cafe_camera *cam)
507 i2c_del_adapter(&cam->i2c_adapter);
511 /* ------------------------------------------------------------------- */
513 * Deal with the controller.
517 * Do everything we think we need to have the interface operating
518 * according to the desired format.
520 static void cafe_ctlr_dma(struct cafe_camera *cam)
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
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);
535 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
536 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
539 static void cafe_ctlr_image(struct cafe_camera *cam)
542 struct v4l2_pix_format *fmt = &cam->pix_format;
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,
552 * Tell the controller about the image format we are using.
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,
561 case V4L2_PIX_FMT_RGB444:
562 cafe_reg_write_mask(cam, REG_CTRL0,
563 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
568 case V4L2_PIX_FMT_RGB565:
569 cafe_reg_write_mask(cam, REG_CTRL0,
570 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
575 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
579 * Make sure it knows we want to use hsync/vsync.
581 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
587 * Configure the controller for operation; caller holds the
590 static int cafe_ctlr_configure(struct cafe_camera *cam)
594 spin_lock_irqsave(&cam->dev_lock, flags);
596 cafe_ctlr_image(cam);
597 cafe_set_config_needed(cam, 0);
598 spin_unlock_irqrestore(&cam->dev_lock, flags);
602 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
605 * Clear any pending interrupts, since we do not
606 * expect to have I/O active prior to enabling.
608 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
609 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
612 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
614 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
618 * Make the controller start grabbing images. Everything must
619 * be set up before doing this.
621 static void cafe_ctlr_start(struct cafe_camera *cam)
623 /* set_bit performs a read, so no other barrier should be
625 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
628 static void cafe_ctlr_stop(struct cafe_camera *cam)
630 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
633 static void cafe_ctlr_init(struct cafe_camera *cam)
637 spin_lock_irqsave(&cam->dev_lock, flags);
639 * Added magic to bring up the hardware on the B-Test board
641 cafe_reg_write(cam, 0x3038, 0x8);
642 cafe_reg_write(cam, 0x315c, 0x80008);
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.
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);
653 * Here we must wait a bit for the controller to come around.
655 spin_unlock_irqrestore(&cam->dev_lock, flags);
657 spin_lock_irqsave(&cam->dev_lock, flags);
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);
662 * Make sure it's not powered down.
664 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
666 * Turn off the enable bit. It sure should be off anyway,
667 * but it's good to be sure.
669 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
671 * Mask all interrupts.
673 cafe_reg_write(cam, REG_IRQMASK, 0);
675 * Clock the sensor appropriately. Controller clock should
676 * be 48MHz, sensor "typical" value is half that.
678 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
679 spin_unlock_irqrestore(&cam->dev_lock, flags);
684 * Stop the controller, and don't return until we're really sure that no
685 * further DMA is going on.
687 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
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.
696 spin_lock_irqsave(&cam->dev_lock, flags);
698 spin_unlock_irqrestore(&cam->dev_lock, flags);
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);
707 cafe_ctlr_irq_disable(cam);
708 spin_unlock_irqrestore(&cam->dev_lock, flags);
714 static void cafe_ctlr_power_up(struct cafe_camera *cam)
718 spin_lock_irqsave(&cam->dev_lock, flags);
719 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
721 * Part one of the sensor dance: turn the global
724 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
725 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
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.
731 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
732 /* mdelay(1); */ /* Marvell says 1ms will do it */
733 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
734 /* mdelay(1); */ /* Enough? */
735 spin_unlock_irqrestore(&cam->dev_lock, flags);
736 msleep(5); /* Just to be sure */
739 static void cafe_ctlr_power_down(struct cafe_camera *cam)
743 spin_lock_irqsave(&cam->dev_lock, flags);
744 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
745 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
746 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
747 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
748 spin_unlock_irqrestore(&cam->dev_lock, flags);
751 /* -------------------------------------------------------------------- */
753 * Communications with the sensor.
756 static int __cafe_cam_reset(struct cafe_camera *cam)
758 return sensor_call(cam, core, reset, 0);
762 * We have found the sensor on the i2c. Let's try to have a
765 static int cafe_cam_init(struct cafe_camera *cam)
767 struct v4l2_dbg_chip_ident chip;
770 mutex_lock(&cam->s_mutex);
771 if (cam->state != S_NOTREADY)
772 cam_warn(cam, "Cam init with device in funky state %d",
774 ret = __cafe_cam_reset(cam);
777 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
778 chip.match.addr = cam->sensor_addr;
779 ret = sensor_call(cam, core, g_chip_ident, &chip);
782 cam->sensor_type = chip.ident;
783 if (cam->sensor_type != V4L2_IDENT_OV7670) {
784 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
788 /* Get/set parameters? */
792 cafe_ctlr_power_down(cam);
793 mutex_unlock(&cam->s_mutex);
798 * Configure the sensor to match the parameters we have. Caller should
801 static int cafe_cam_set_flip(struct cafe_camera *cam)
803 struct v4l2_control ctrl;
805 memset(&ctrl, 0, sizeof(ctrl));
806 ctrl.id = V4L2_CID_VFLIP;
808 return sensor_call(cam, core, s_ctrl, &ctrl);
812 static int cafe_cam_configure(struct cafe_camera *cam)
814 struct v4l2_format fmt;
817 if (cam->state != S_IDLE)
819 fmt.fmt.pix = cam->pix_format;
820 ret = sensor_call(cam, core, init, 0);
822 ret = sensor_call(cam, video, s_fmt, &fmt);
824 * OV7670 does weird things if flip is set *before* format...
826 ret += cafe_cam_set_flip(cam);
830 /* -------------------------------------------------------------------- */
832 * DMA buffer management. These functions need s_mutex held.
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.
839 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
843 cafe_set_config_needed(cam, 1);
845 cam->dma_buf_size = dma_buf_size;
847 cam->dma_buf_size = cam->pix_format.sizeimage;
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,
856 if (cam->dma_bufs[i] == NULL) {
857 cam_warn(cam, "Failed to allocate DMA buffer\n");
860 /* For debug, remove eventually */
861 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
865 switch (cam->nbufs) {
867 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
868 cam->dma_bufs[0], cam->dma_handles[0]);
871 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
876 cam_warn(cam, "Will limp along with only 2 buffers\n");
882 static void cafe_free_dma_bufs(struct cafe_camera *cam)
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;
898 /* ----------------------------------------------------------------------- */
900 * Here starts the V4L2 interface code.
904 * Read an image from the device.
906 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
907 char __user *buffer, size_t len, loff_t *pos)
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);
918 bufno = cam->next_buf;
919 clear_bit(bufno, &cam->flags);
920 if (++(cam->next_buf) >= cam->nbufs)
922 if (! test_bit(cam->next_buf, &cam->flags))
925 spin_unlock_irqrestore(&cam->dev_lock, flags);
927 if (len > cam->pix_format.sizeimage)
928 len = cam->pix_format.sizeimage;
929 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
936 * Get everything ready, and start grabbing frames.
938 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
944 * Configuration. If we still don't have DMA buffers,
945 * make one last, desperate attempt.
948 if (cafe_alloc_dma_bufs(cam, 0))
951 if (cafe_needs_config(cam)) {
952 cafe_cam_configure(cam);
953 ret = cafe_ctlr_configure(cam);
961 spin_lock_irqsave(&cam->dev_lock, flags);
962 cafe_reset_buffers(cam);
963 cafe_ctlr_irq_enable(cam);
965 cafe_ctlr_start(cam);
966 spin_unlock_irqrestore(&cam->dev_lock, flags);
971 static ssize_t cafe_v4l_read(struct file *filp,
972 char __user *buffer, size_t len, loff_t *pos)
974 struct cafe_camera *cam = filp->private_data;
978 * Perhaps we're in speculative read mode and already
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);
988 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
991 } else if (cam->state != S_IDLE) {
997 * v4l2: multiple processes can open the device, but only
998 * one gets to grab data from it.
1000 if (cam->owner && cam->owner != filp) {
1007 * Do setup if need be.
1009 if (cam->state != S_SPECREAD) {
1010 ret = cafe_read_setup(cam, S_SINGLEREAD);
1015 * Wait for something to happen. This should probably
1016 * be interruptible (FIXME).
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);
1026 * Give them their data and we should be done.
1028 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1031 mutex_unlock(&cam->s_mutex);
1043 * Streaming I/O support.
1048 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1049 enum v4l2_buf_type type)
1051 struct cafe_camera *cam = filp->private_data;
1054 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1056 mutex_lock(&cam->s_mutex);
1057 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1061 ret = cafe_read_setup(cam, S_STREAMING);
1064 mutex_unlock(&cam->s_mutex);
1070 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1071 enum v4l2_buf_type type)
1073 struct cafe_camera *cam = filp->private_data;
1076 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1078 mutex_lock(&cam->s_mutex);
1079 if (cam->state != S_STREAMING)
1082 cafe_ctlr_stop_dma(cam);
1086 mutex_unlock(&cam->s_mutex);
1093 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1095 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
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)
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;
1110 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
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.
1115 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1119 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1124 * If any buffers are mapped, we cannot free them at all.
1126 for (i = 0; i < cam->n_sbufs; i++)
1127 if (cam->sb_bufs[i].mapcount > 0)
1132 for (i = 0; i < cam->n_sbufs; i++)
1133 vfree(cam->sb_bufs[i].buffer);
1135 kfree(cam->sb_bufs);
1136 cam->sb_bufs = NULL;
1137 INIT_LIST_HEAD(&cam->sb_avail);
1138 INIT_LIST_HEAD(&cam->sb_full);
1144 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1145 struct v4l2_requestbuffers *req)
1147 struct cafe_camera *cam = filp->private_data;
1148 int ret = 0; /* Silence warning */
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.
1154 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1156 if (req->memory != V4L2_MEMORY_MMAP)
1159 * If they ask for zero buffers, they really want us to stop streaming
1160 * (if it's happening) and free everything. Should we check owner?
1162 mutex_lock(&cam->s_mutex);
1163 if (req->count == 0) {
1164 if (cam->state == S_STREAMING)
1165 cafe_ctlr_stop_dma(cam);
1166 ret = cafe_free_sio_buffers (cam);
1170 * Device needs to be idle and working. We *could* try to do the
1171 * right thing in S_SPECREAD by shutting things down, but it
1172 * probably doesn't matter.
1174 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1180 if (req->count < min_buffers)
1181 req->count = min_buffers;
1182 else if (req->count > max_buffers)
1183 req->count = max_buffers;
1184 if (cam->n_sbufs > 0) {
1185 ret = cafe_free_sio_buffers(cam);
1190 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1192 if (cam->sb_bufs == NULL) {
1196 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1197 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1202 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1203 kfree(cam->sb_bufs);
1204 req->count = cam->n_sbufs; /* In case of partial success */
1207 mutex_unlock(&cam->s_mutex);
1212 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1213 struct v4l2_buffer *buf)
1215 struct cafe_camera *cam = filp->private_data;
1218 mutex_lock(&cam->s_mutex);
1219 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1221 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1223 *buf = cam->sb_bufs[buf->index].v4lbuf;
1226 mutex_unlock(&cam->s_mutex);
1230 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1231 struct v4l2_buffer *buf)
1233 struct cafe_camera *cam = filp->private_data;
1234 struct cafe_sio_buffer *sbuf;
1236 unsigned long flags;
1238 mutex_lock(&cam->s_mutex);
1239 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1241 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1243 sbuf = cam->sb_bufs + buf->index;
1244 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1245 ret = 0; /* Already queued?? */
1248 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1249 /* Spec doesn't say anything, seems appropriate tho */
1253 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1254 spin_lock_irqsave(&cam->dev_lock, flags);
1255 list_add(&sbuf->list, &cam->sb_avail);
1256 spin_unlock_irqrestore(&cam->dev_lock, flags);
1259 mutex_unlock(&cam->s_mutex);
1263 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1264 struct v4l2_buffer *buf)
1266 struct cafe_camera *cam = filp->private_data;
1267 struct cafe_sio_buffer *sbuf;
1269 unsigned long flags;
1271 mutex_lock(&cam->s_mutex);
1272 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1274 if (cam->state != S_STREAMING)
1276 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1281 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1282 mutex_unlock(&cam->s_mutex);
1283 if (wait_event_interruptible(cam->iowait,
1284 !list_empty(&cam->sb_full))) {
1288 mutex_lock(&cam->s_mutex);
1291 if (cam->state != S_STREAMING)
1294 spin_lock_irqsave(&cam->dev_lock, flags);
1295 /* Should probably recheck !list_empty() here */
1296 sbuf = list_entry(cam->sb_full.next,
1297 struct cafe_sio_buffer, list);
1298 list_del_init(&sbuf->list);
1299 spin_unlock_irqrestore(&cam->dev_lock, flags);
1300 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1301 *buf = sbuf->v4lbuf;
1306 mutex_unlock(&cam->s_mutex);
1313 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1315 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1317 * Locking: done under mmap_sem, so we don't need to
1318 * go back to the camera lock here.
1324 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1326 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1328 mutex_lock(&sbuf->cam->s_mutex);
1330 /* Docs say we should stop I/O too... */
1331 if (sbuf->mapcount == 0)
1332 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1333 mutex_unlock(&sbuf->cam->s_mutex);
1336 static struct vm_operations_struct cafe_v4l_vm_ops = {
1337 .open = cafe_v4l_vm_open,
1338 .close = cafe_v4l_vm_close
1342 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1344 struct cafe_camera *cam = filp->private_data;
1345 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1348 struct cafe_sio_buffer *sbuf = NULL;
1350 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1353 * Find the buffer they are looking for.
1355 mutex_lock(&cam->s_mutex);
1356 for (i = 0; i < cam->n_sbufs; i++)
1357 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1358 sbuf = cam->sb_bufs + i;
1364 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1367 vma->vm_flags |= VM_DONTEXPAND;
1368 vma->vm_private_data = sbuf;
1369 vma->vm_ops = &cafe_v4l_vm_ops;
1370 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1371 cafe_v4l_vm_open(vma);
1374 mutex_unlock(&cam->s_mutex);
1380 static int cafe_v4l_open(struct file *filp)
1382 struct cafe_camera *cam = video_drvdata(filp);
1384 filp->private_data = cam;
1386 mutex_lock(&cam->s_mutex);
1387 if (cam->users == 0) {
1388 cafe_ctlr_power_up(cam);
1389 __cafe_cam_reset(cam);
1390 cafe_set_config_needed(cam, 1);
1391 /* FIXME make sure this is complete */
1394 mutex_unlock(&cam->s_mutex);
1399 static int cafe_v4l_release(struct file *filp)
1401 struct cafe_camera *cam = filp->private_data;
1403 mutex_lock(&cam->s_mutex);
1405 if (filp == cam->owner) {
1406 cafe_ctlr_stop_dma(cam);
1407 cafe_free_sio_buffers(cam);
1410 if (cam->users == 0) {
1411 cafe_ctlr_power_down(cam);
1412 if (alloc_bufs_at_read)
1413 cafe_free_dma_bufs(cam);
1415 mutex_unlock(&cam->s_mutex);
1421 static unsigned int cafe_v4l_poll(struct file *filp,
1422 struct poll_table_struct *pt)
1424 struct cafe_camera *cam = filp->private_data;
1426 poll_wait(filp, &cam->iowait, pt);
1427 if (cam->next_buf >= 0)
1428 return POLLIN | POLLRDNORM;
1434 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1435 struct v4l2_queryctrl *qc)
1437 struct cafe_camera *cam = priv;
1440 mutex_lock(&cam->s_mutex);
1441 ret = sensor_call(cam, core, queryctrl, qc);
1442 mutex_unlock(&cam->s_mutex);
1447 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1448 struct v4l2_control *ctrl)
1450 struct cafe_camera *cam = priv;
1453 mutex_lock(&cam->s_mutex);
1454 ret = sensor_call(cam, core, g_ctrl, ctrl);
1455 mutex_unlock(&cam->s_mutex);
1460 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1461 struct v4l2_control *ctrl)
1463 struct cafe_camera *cam = priv;
1466 mutex_lock(&cam->s_mutex);
1467 ret = sensor_call(cam, core, s_ctrl, ctrl);
1468 mutex_unlock(&cam->s_mutex);
1476 static int cafe_vidioc_querycap(struct file *file, void *priv,
1477 struct v4l2_capability *cap)
1479 strcpy(cap->driver, "cafe_ccic");
1480 strcpy(cap->card, "cafe_ccic");
1481 cap->version = CAFE_VERSION;
1482 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1483 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1489 * The default format we use until somebody says otherwise.
1491 static struct v4l2_pix_format cafe_def_pix_format = {
1493 .height = VGA_HEIGHT,
1494 .pixelformat = V4L2_PIX_FMT_YUYV,
1495 .field = V4L2_FIELD_NONE,
1496 .bytesperline = VGA_WIDTH*2,
1497 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1500 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1501 void *priv, struct v4l2_fmtdesc *fmt)
1503 struct cafe_camera *cam = priv;
1506 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1508 mutex_lock(&cam->s_mutex);
1509 ret = sensor_call(cam, video, enum_fmt, fmt);
1510 mutex_unlock(&cam->s_mutex);
1515 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1516 struct v4l2_format *fmt)
1518 struct cafe_camera *cam = priv;
1521 mutex_lock(&cam->s_mutex);
1522 ret = sensor_call(cam, video, try_fmt, fmt);
1523 mutex_unlock(&cam->s_mutex);
1527 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1528 struct v4l2_format *fmt)
1530 struct cafe_camera *cam = priv;
1534 * Can't do anything if the device is not idle
1535 * Also can't if there are streaming buffers in place.
1537 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1540 * See if the formatting works in principle.
1542 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1546 * Now we start to change things for real, so let's do it
1549 mutex_lock(&cam->s_mutex);
1550 cam->pix_format = fmt->fmt.pix;
1552 * Make sure we have appropriate DMA buffers.
1555 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1556 cafe_free_dma_bufs(cam);
1557 if (cam->nbufs == 0) {
1558 if (cafe_alloc_dma_bufs(cam, 0))
1562 * It looks like this might work, so let's program the sensor.
1564 ret = cafe_cam_configure(cam);
1566 ret = cafe_ctlr_configure(cam);
1568 mutex_unlock(&cam->s_mutex);
1573 * Return our stored notion of how the camera is/should be configured.
1574 * The V4l2 spec wants us to be smarter, and actually get this from
1575 * the camera (and not mess with it at open time). Someday.
1577 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1578 struct v4l2_format *f)
1580 struct cafe_camera *cam = priv;
1582 f->fmt.pix = cam->pix_format;
1587 * We only have one input - the sensor - so minimize the nonsense here.
1589 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1590 struct v4l2_input *input)
1592 if (input->index != 0)
1595 input->type = V4L2_INPUT_TYPE_CAMERA;
1596 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1597 strcpy(input->name, "Camera");
1601 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1607 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1615 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1621 * G/S_PARM. Most of this is done by the sensor, but we are
1622 * the level which controls the number of read buffers.
1624 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1625 struct v4l2_streamparm *parms)
1627 struct cafe_camera *cam = priv;
1630 mutex_lock(&cam->s_mutex);
1631 ret = sensor_call(cam, video, g_parm, parms);
1632 mutex_unlock(&cam->s_mutex);
1633 parms->parm.capture.readbuffers = n_dma_bufs;
1637 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1638 struct v4l2_streamparm *parms)
1640 struct cafe_camera *cam = priv;
1643 mutex_lock(&cam->s_mutex);
1644 ret = sensor_call(cam, video, s_parm, parms);
1645 mutex_unlock(&cam->s_mutex);
1646 parms->parm.capture.readbuffers = n_dma_bufs;
1650 static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1651 struct v4l2_dbg_chip_ident *chip)
1653 struct cafe_camera *cam = priv;
1655 chip->ident = V4L2_IDENT_NONE;
1657 if (v4l2_chip_match_host(&chip->match)) {
1658 chip->ident = V4L2_IDENT_CAFE;
1661 return sensor_call(cam, core, g_chip_ident, chip);
1664 #ifdef CONFIG_VIDEO_ADV_DEBUG
1665 static int cafe_vidioc_g_register(struct file *file, void *priv,
1666 struct v4l2_dbg_register *reg)
1668 struct cafe_camera *cam = priv;
1670 if (v4l2_chip_match_host(®->match)) {
1671 reg->val = cafe_reg_read(cam, reg->reg);
1675 return sensor_call(cam, core, g_register, reg);
1678 static int cafe_vidioc_s_register(struct file *file, void *priv,
1679 struct v4l2_dbg_register *reg)
1681 struct cafe_camera *cam = priv;
1683 if (v4l2_chip_match_host(®->match)) {
1684 cafe_reg_write(cam, reg->reg, reg->val);
1687 return sensor_call(cam, core, s_register, reg);
1692 * This template device holds all of those v4l2 methods; we
1693 * clone it for specific real devices.
1696 static const struct v4l2_file_operations cafe_v4l_fops = {
1697 .owner = THIS_MODULE,
1698 .open = cafe_v4l_open,
1699 .release = cafe_v4l_release,
1700 .read = cafe_v4l_read,
1701 .poll = cafe_v4l_poll,
1702 .mmap = cafe_v4l_mmap,
1703 .ioctl = video_ioctl2,
1706 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1707 .vidioc_querycap = cafe_vidioc_querycap,
1708 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1709 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1710 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1711 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1712 .vidioc_enum_input = cafe_vidioc_enum_input,
1713 .vidioc_g_input = cafe_vidioc_g_input,
1714 .vidioc_s_input = cafe_vidioc_s_input,
1715 .vidioc_s_std = cafe_vidioc_s_std,
1716 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1717 .vidioc_querybuf = cafe_vidioc_querybuf,
1718 .vidioc_qbuf = cafe_vidioc_qbuf,
1719 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1720 .vidioc_streamon = cafe_vidioc_streamon,
1721 .vidioc_streamoff = cafe_vidioc_streamoff,
1722 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1723 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1724 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1725 .vidioc_g_parm = cafe_vidioc_g_parm,
1726 .vidioc_s_parm = cafe_vidioc_s_parm,
1727 .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
1728 #ifdef CONFIG_VIDEO_ADV_DEBUG
1729 .vidioc_g_register = cafe_vidioc_g_register,
1730 .vidioc_s_register = cafe_vidioc_s_register,
1734 static struct video_device cafe_v4l_template = {
1736 .minor = -1, /* Get one dynamically */
1737 .tvnorms = V4L2_STD_NTSC_M,
1738 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1740 .fops = &cafe_v4l_fops,
1741 .ioctl_ops = &cafe_v4l_ioctl_ops,
1742 .release = video_device_release_empty,
1746 /* ---------------------------------------------------------------------- */
1748 * Interrupt handler stuff
1753 static void cafe_frame_tasklet(unsigned long data)
1755 struct cafe_camera *cam = (struct cafe_camera *) data;
1757 unsigned long flags;
1758 struct cafe_sio_buffer *sbuf;
1760 spin_lock_irqsave(&cam->dev_lock, flags);
1761 for (i = 0; i < cam->nbufs; i++) {
1762 int bufno = cam->next_buf;
1763 if (bufno < 0) { /* "will never happen" */
1764 cam_err(cam, "No valid bufs in tasklet!\n");
1767 if (++(cam->next_buf) >= cam->nbufs)
1769 if (! test_bit(bufno, &cam->flags))
1771 if (list_empty(&cam->sb_avail))
1772 break; /* Leave it valid, hope for better later */
1773 clear_bit(bufno, &cam->flags);
1774 sbuf = list_entry(cam->sb_avail.next,
1775 struct cafe_sio_buffer, list);
1777 * Drop the lock during the big copy. This *should* be safe...
1779 spin_unlock_irqrestore(&cam->dev_lock, flags);
1780 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1781 cam->pix_format.sizeimage);
1782 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1783 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1784 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1785 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1786 spin_lock_irqsave(&cam->dev_lock, flags);
1787 list_move_tail(&sbuf->list, &cam->sb_full);
1789 if (! list_empty(&cam->sb_full))
1790 wake_up(&cam->iowait);
1791 spin_unlock_irqrestore(&cam->dev_lock, flags);
1796 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1799 * Basic frame housekeeping.
1801 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1802 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1803 set_bit(frame, &cam->flags);
1804 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1805 if (cam->next_buf < 0)
1806 cam->next_buf = frame;
1807 cam->buf_seq[frame] = ++(cam->sequence);
1809 switch (cam->state) {
1811 * If in single read mode, try going speculative.
1814 cam->state = S_SPECREAD;
1815 cam->specframes = 0;
1816 wake_up(&cam->iowait);
1820 * If we are already doing speculative reads, and nobody is
1821 * reading them, just stop.
1824 if (++(cam->specframes) >= cam->nbufs) {
1825 cafe_ctlr_stop(cam);
1826 cafe_ctlr_irq_disable(cam);
1827 cam->state = S_IDLE;
1829 wake_up(&cam->iowait);
1832 * For the streaming case, we defer the real work to the
1835 * FIXME: if the application is not consuming the buffers,
1836 * we should eventually put things on hold and restart in
1840 tasklet_schedule(&cam->s_tasklet);
1844 cam_err(cam, "Frame interrupt in non-operational state\n");
1852 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1856 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1858 * Handle any frame completions. There really should
1859 * not be more than one of these, or we have fallen
1862 for (frame = 0; frame < cam->nbufs; frame++)
1863 if (irqs & (IRQ_EOF0 << frame))
1864 cafe_frame_complete(cam, frame);
1866 * If a frame starts, note that we have DMA active. This
1867 * code assumes that we won't get multiple frame interrupts
1868 * at once; may want to rethink that.
1870 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1871 set_bit(CF_DMA_ACTIVE, &cam->flags);
1876 static irqreturn_t cafe_irq(int irq, void *data)
1878 struct cafe_camera *cam = data;
1881 spin_lock(&cam->dev_lock);
1882 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1883 if ((irqs & ALLIRQS) == 0) {
1884 spin_unlock(&cam->dev_lock);
1887 if (irqs & FRAMEIRQS)
1888 cafe_frame_irq(cam, irqs);
1889 if (irqs & TWSIIRQS) {
1890 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1891 wake_up(&cam->smbus_wait);
1893 spin_unlock(&cam->dev_lock);
1898 /* -------------------------------------------------------------------------- */
1900 * PCI interface stuff.
1903 static int cafe_pci_probe(struct pci_dev *pdev,
1904 const struct pci_device_id *id)
1907 struct cafe_camera *cam;
1910 * Start putting together one of our big camera structures.
1913 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1916 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1920 mutex_init(&cam->s_mutex);
1921 mutex_lock(&cam->s_mutex);
1922 spin_lock_init(&cam->dev_lock);
1923 cam->state = S_NOTREADY;
1924 cafe_set_config_needed(cam, 1);
1925 init_waitqueue_head(&cam->smbus_wait);
1926 init_waitqueue_head(&cam->iowait);
1928 cam->pix_format = cafe_def_pix_format;
1929 INIT_LIST_HEAD(&cam->dev_list);
1930 INIT_LIST_HEAD(&cam->sb_avail);
1931 INIT_LIST_HEAD(&cam->sb_full);
1932 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
1934 * Get set up on the PCI bus.
1936 ret = pci_enable_device(pdev);
1939 pci_set_master(pdev);
1942 cam->regs = pci_iomap(pdev, 0, 0);
1944 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
1947 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
1951 * Initialize the controller and leave it powered up. It will
1952 * stay that way until the sensor driver shows up.
1954 cafe_ctlr_init(cam);
1955 cafe_ctlr_power_up(cam);
1957 * Set up I2C/SMBUS communications. We have to drop the mutex here
1958 * because the sensor could attach in this call chain, leading to
1959 * unsightly deadlocks.
1961 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
1962 ret = cafe_smbus_setup(cam);
1966 cam->sensor_addr = 0x42;
1967 cam->sensor = v4l2_i2c_new_subdev(&cam->i2c_adapter,
1968 "ov7670", "ov7670", cam->sensor_addr);
1969 if (cam->sensor == NULL) {
1973 ret = cafe_cam_init(cam);
1978 * Get the v4l2 setup done.
1980 mutex_lock(&cam->s_mutex);
1981 cam->vdev = cafe_v4l_template;
1982 cam->vdev.debug = 0;
1983 /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
1984 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1985 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1988 video_set_drvdata(&cam->vdev, cam);
1991 * If so requested, try to get our DMA buffers now.
1993 if (!alloc_bufs_at_read) {
1994 if (cafe_alloc_dma_bufs(cam, 1))
1995 cam_warn(cam, "Unable to alloc DMA buffers at load"
1996 " will try again later.");
1999 mutex_unlock(&cam->s_mutex);
2003 cafe_smbus_shutdown(cam);
2005 cafe_ctlr_power_down(cam);
2006 free_irq(pdev->irq, cam);
2008 pci_iounmap(pdev, cam->regs);
2010 v4l2_device_unregister(&cam->v4l2_dev);
2019 * Shut down an initialized device
2021 static void cafe_shutdown(struct cafe_camera *cam)
2023 /* FIXME: Make sure we take care of everything here */
2024 if (cam->n_sbufs > 0)
2025 /* What if they are still mapped? Shouldn't be, but... */
2026 cafe_free_sio_buffers(cam);
2027 cafe_ctlr_stop_dma(cam);
2028 cafe_ctlr_power_down(cam);
2029 cafe_smbus_shutdown(cam);
2030 cafe_free_dma_bufs(cam);
2031 free_irq(cam->pdev->irq, cam);
2032 pci_iounmap(cam->pdev, cam->regs);
2033 video_unregister_device(&cam->vdev);
2037 static void cafe_pci_remove(struct pci_dev *pdev)
2039 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2040 struct cafe_camera *cam = to_cam(v4l2_dev);
2043 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2046 mutex_lock(&cam->s_mutex);
2048 cam_warn(cam, "Removing a device with users!\n");
2050 v4l2_device_unregister(&cam->v4l2_dev);
2052 /* No unlock - it no longer exists */
2058 * Basic power management.
2060 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2062 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2063 struct cafe_camera *cam = to_cam(v4l2_dev);
2065 enum cafe_state cstate;
2067 ret = pci_save_state(pdev);
2070 cstate = cam->state; /* HACK - stop_dma sets to idle */
2071 cafe_ctlr_stop_dma(cam);
2072 cafe_ctlr_power_down(cam);
2073 pci_disable_device(pdev);
2074 cam->state = cstate;
2079 static int cafe_pci_resume(struct pci_dev *pdev)
2081 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2082 struct cafe_camera *cam = to_cam(v4l2_dev);
2085 ret = pci_restore_state(pdev);
2088 ret = pci_enable_device(pdev);
2091 cam_warn(cam, "Unable to re-enable device on resume!\n");
2094 cafe_ctlr_init(cam);
2095 cafe_ctlr_power_down(cam);
2097 mutex_lock(&cam->s_mutex);
2098 if (cam->users > 0) {
2099 cafe_ctlr_power_up(cam);
2100 __cafe_cam_reset(cam);
2102 mutex_unlock(&cam->s_mutex);
2104 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2105 if (cam->state == S_SPECREAD)
2106 cam->state = S_IDLE; /* Don't bother restarting */
2107 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2108 ret = cafe_read_setup(cam, cam->state);
2112 #endif /* CONFIG_PM */
2115 static struct pci_device_id cafe_ids[] = {
2116 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2117 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2121 MODULE_DEVICE_TABLE(pci, cafe_ids);
2123 static struct pci_driver cafe_pci_driver = {
2124 .name = "cafe1000-ccic",
2125 .id_table = cafe_ids,
2126 .probe = cafe_pci_probe,
2127 .remove = cafe_pci_remove,
2129 .suspend = cafe_pci_suspend,
2130 .resume = cafe_pci_resume,
2137 static int __init cafe_init(void)
2141 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2143 ret = pci_register_driver(&cafe_pci_driver);
2145 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2155 static void __exit cafe_exit(void)
2157 pci_unregister_driver(&cafe_pci_driver);
2160 module_init(cafe_init);
2161 module_exit(cafe_exit);