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 * This file may be distributed under the terms of the GNU General
15 * Public License, version 2.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <linux/device.h>
32 #include <linux/wait.h>
33 #include <linux/list.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/debugfs.h>
37 #include <linux/jiffies.h>
38 #include <linux/vmalloc.h>
40 #include <asm/uaccess.h>
43 #include "cafe_ccic-regs.h"
45 #define CAFE_VERSION 0x000002
51 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
52 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
53 MODULE_LICENSE("GPL");
54 MODULE_SUPPORTED_DEVICE("Video");
57 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
58 * we must have physically contiguous buffers to bring frames into.
59 * These parameters control how many buffers we use, whether we
60 * allocate them at load time (better chance of success, but nails down
61 * memory) or when somebody tries to use the camera (riskier), and,
62 * for load-time allocation, how big they should be.
64 * The controller can cycle through three buffers. We could use
65 * more by flipping pointers around, but it probably makes little
69 #define MAX_DMA_BUFS 3
70 static int alloc_bufs_at_read;
71 module_param(alloc_bufs_at_read, bool, 0444);
72 MODULE_PARM_DESC(alloc_bufs_at_read,
73 "Non-zero value causes DMA buffers to be allocated when the "
74 "video capture device is read, rather than at module load "
75 "time. This saves memory, but decreases the chances of "
76 "successfully getting those buffers.");
78 static int n_dma_bufs = 3;
79 module_param(n_dma_bufs, uint, 0644);
80 MODULE_PARM_DESC(n_dma_bufs,
81 "The number of DMA buffers to allocate. Can be either two "
82 "(saves memory, makes timing tighter) or three.");
84 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
85 module_param(dma_buf_size, uint, 0444);
86 MODULE_PARM_DESC(dma_buf_size,
87 "The size of the allocated DMA buffers. If actual operating "
88 "parameters require larger buffers, an attempt to reallocate "
91 static int min_buffers = 1;
92 module_param(min_buffers, uint, 0644);
93 MODULE_PARM_DESC(min_buffers,
94 "The minimum number of streaming I/O buffers we are willing "
97 static int max_buffers = 10;
98 module_param(max_buffers, uint, 0644);
99 MODULE_PARM_DESC(max_buffers,
100 "The maximum number of streaming I/O buffers an application "
101 "will be allowed to allocate. These buffers are big and live "
102 "in vmalloc space.");
105 module_param(flip, bool, 0444);
106 MODULE_PARM_DESC(flip,
107 "If set, the sensor will be instructed to flip the image "
112 S_NOTREADY, /* Not yet initialized */
113 S_IDLE, /* Just hanging around */
114 S_FLAKED, /* Some sort of problem */
115 S_SINGLEREAD, /* In read() */
116 S_SPECREAD, /* Speculative read (for future read()) */
117 S_STREAMING /* Streaming data */
121 * Tracking of streaming I/O buffers.
123 struct cafe_sio_buffer {
124 struct list_head list;
125 struct v4l2_buffer v4lbuf;
126 char *buffer; /* Where it lives in kernel space */
128 struct cafe_camera *cam;
132 * A description of one of our devices.
133 * Locking: controlled by s_mutex. Certain fields, however, require
134 * the dev_lock spinlock; they are marked as such by comments.
135 * dev_lock is also required for access to device registers.
139 struct v4l2_device v4l2_dev;
140 enum cafe_state state;
141 unsigned long flags; /* Buffer status, mainly (dev_lock) */
142 int users; /* How many open FDs */
143 struct file *owner; /* Who has data access (v4l2) */
146 * Subsystem structures.
148 struct pci_dev *pdev;
149 struct video_device vdev;
150 struct i2c_adapter i2c_adapter;
151 struct v4l2_subdev *sensor;
152 unsigned short sensor_addr;
154 unsigned char __iomem *regs;
155 struct list_head dev_list; /* link to other devices */
158 unsigned int nbufs; /* How many are alloc'd */
159 int next_buf; /* Next to consume (dev_lock) */
160 unsigned int dma_buf_size; /* allocated size */
161 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
162 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
163 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
164 unsigned int sequence; /* Frame sequence number */
165 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
167 /* Streaming buffers */
168 unsigned int n_sbufs; /* How many we have */
169 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
170 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
171 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
172 struct tasklet_struct s_tasklet;
174 /* Current operating parameters */
175 u32 sensor_type; /* Currently ov7670 only */
176 struct v4l2_pix_format pix_format;
179 struct mutex s_mutex; /* Access to this structure */
180 spinlock_t dev_lock; /* Access to device */
183 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
184 wait_queue_head_t iowait; /* Waiting on frame data */
185 #ifdef CONFIG_VIDEO_ADV_DEBUG
186 struct dentry *dfs_regs;
187 struct dentry *dfs_cam_regs;
192 * Status flags. Always manipulated with bit operations.
194 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
195 #define CF_BUF1_VALID 1
196 #define CF_BUF2_VALID 2
197 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
198 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
200 #define sensor_call(cam, o, f, args...) \
201 v4l2_subdev_call(cam->sensor, o, f, ##args)
203 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
205 return container_of(dev, struct cafe_camera, v4l2_dev);
210 * Start over with DMA buffers - dev_lock needed.
212 static void cafe_reset_buffers(struct cafe_camera *cam)
217 for (i = 0; i < cam->nbufs; i++)
218 clear_bit(i, &cam->flags);
222 static inline int cafe_needs_config(struct cafe_camera *cam)
224 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
227 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
230 set_bit(CF_CONFIG_NEEDED, &cam->flags);
232 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
239 * Debugging and related.
241 #define cam_err(cam, fmt, arg...) \
242 dev_err(&(cam)->pdev->dev, fmt, ##arg);
243 #define cam_warn(cam, fmt, arg...) \
244 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
245 #define cam_dbg(cam, fmt, arg...) \
246 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
249 /* ---------------------------------------------------------------------*/
252 * Device register I/O
254 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
257 iowrite32(val, cam->regs + reg);
260 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
263 return ioread32(cam->regs + reg);
267 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
268 unsigned int val, unsigned int mask)
270 unsigned int v = cafe_reg_read(cam, reg);
272 v = (v & ~mask) | (val & mask);
273 cafe_reg_write(cam, reg, v);
276 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
277 unsigned int reg, unsigned int val)
279 cafe_reg_write_mask(cam, reg, 0, val);
282 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
283 unsigned int reg, unsigned int val)
285 cafe_reg_write_mask(cam, reg, val, val);
290 /* -------------------------------------------------------------------- */
292 * The I2C/SMBUS interface to the camera itself starts here. The
293 * controller handles SMBUS itself, presenting a relatively simple register
294 * interface; all we have to do is to tell it where to route the data.
296 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
298 static int cafe_smbus_write_done(struct cafe_camera *cam)
304 * We must delay after the interrupt, or the controller gets confused
305 * and never does give us good status. Fortunately, we don't do this
309 spin_lock_irqsave(&cam->dev_lock, flags);
310 c1 = cafe_reg_read(cam, REG_TWSIC1);
311 spin_unlock_irqrestore(&cam->dev_lock, flags);
312 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
315 static int cafe_smbus_write_data(struct cafe_camera *cam,
316 u16 addr, u8 command, u8 value)
320 DEFINE_WAIT(the_wait);
322 spin_lock_irqsave(&cam->dev_lock, flags);
323 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
324 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
326 * Marvell sez set clkdiv to all 1's for now.
328 rval |= TWSIC0_CLKDIV;
329 cafe_reg_write(cam, REG_TWSIC0, rval);
330 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
331 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
332 cafe_reg_write(cam, REG_TWSIC1, rval);
333 spin_unlock_irqrestore(&cam->dev_lock, flags);
336 * Time to wait for the write to complete. THIS IS A RACY
337 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
338 * register too quickly after starting the operation sends
339 * the device into a place that may be kinder and better, but
340 * which is absolutely useless for controlling the sensor. In
341 * practice we have plenty of time to get into our sleep state
342 * before the interrupt hits, and the worst case is that we
343 * time out and then see that things completed, so this seems
344 * the best way for now.
347 prepare_to_wait(&cam->smbus_wait, &the_wait,
348 TASK_UNINTERRUPTIBLE);
349 schedule_timeout(1); /* even 1 jiffy is too long */
350 finish_wait(&cam->smbus_wait, &the_wait);
351 } while (!cafe_smbus_write_done(cam));
353 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
354 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
357 spin_lock_irqsave(&cam->dev_lock, flags);
358 rval = cafe_reg_read(cam, REG_TWSIC1);
359 spin_unlock_irqrestore(&cam->dev_lock, flags);
361 if (rval & TWSIC1_WSTAT) {
362 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
366 if (rval & TWSIC1_ERROR) {
367 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
376 static int cafe_smbus_read_done(struct cafe_camera *cam)
382 * We must delay after the interrupt, or the controller gets confused
383 * and never does give us good status. Fortunately, we don't do this
387 spin_lock_irqsave(&cam->dev_lock, flags);
388 c1 = cafe_reg_read(cam, REG_TWSIC1);
389 spin_unlock_irqrestore(&cam->dev_lock, flags);
390 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
395 static int cafe_smbus_read_data(struct cafe_camera *cam,
396 u16 addr, u8 command, u8 *value)
401 spin_lock_irqsave(&cam->dev_lock, flags);
402 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
403 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
405 * Marvel sez set clkdiv to all 1's for now.
407 rval |= TWSIC0_CLKDIV;
408 cafe_reg_write(cam, REG_TWSIC0, rval);
409 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
410 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
411 cafe_reg_write(cam, REG_TWSIC1, rval);
412 spin_unlock_irqrestore(&cam->dev_lock, flags);
414 wait_event_timeout(cam->smbus_wait,
415 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
416 spin_lock_irqsave(&cam->dev_lock, flags);
417 rval = cafe_reg_read(cam, REG_TWSIC1);
418 spin_unlock_irqrestore(&cam->dev_lock, flags);
420 if (rval & TWSIC1_ERROR) {
421 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
424 if (! (rval & TWSIC1_RVALID)) {
425 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
429 *value = rval & 0xff;
434 * Perform a transfer over SMBUS. This thing is called under
435 * the i2c bus lock, so we shouldn't race with ourselves...
437 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
438 unsigned short flags, char rw, u8 command,
439 int size, union i2c_smbus_data *data)
441 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
442 struct cafe_camera *cam = to_cam(v4l2_dev);
446 * This interface would appear to only do byte data ops. OK
447 * it can do word too, but the cam chip has no use for that.
449 if (size != I2C_SMBUS_BYTE_DATA) {
450 cam_err(cam, "funky xfer size %d\n", size);
454 if (rw == I2C_SMBUS_WRITE)
455 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
456 else if (rw == I2C_SMBUS_READ)
457 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
462 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
466 spin_lock_irqsave(&cam->dev_lock, flags);
467 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
468 spin_unlock_irqrestore(&cam->dev_lock, flags);
471 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
473 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
474 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
477 static struct i2c_algorithm cafe_smbus_algo = {
478 .smbus_xfer = cafe_smbus_xfer,
479 .functionality = cafe_smbus_func
482 /* Somebody is on the bus */
483 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
484 static void cafe_ctlr_power_down(struct cafe_camera *cam);
486 static int cafe_smbus_setup(struct cafe_camera *cam)
488 struct i2c_adapter *adap = &cam->i2c_adapter;
491 cafe_smbus_enable_irq(cam);
492 adap->id = I2C_HW_SMBUS_CAFE;
493 adap->owner = THIS_MODULE;
494 adap->algo = &cafe_smbus_algo;
495 strcpy(adap->name, "cafe_ccic");
496 adap->dev.parent = &cam->pdev->dev;
497 i2c_set_adapdata(adap, &cam->v4l2_dev);
498 ret = i2c_add_adapter(adap);
500 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
504 static void cafe_smbus_shutdown(struct cafe_camera *cam)
506 i2c_del_adapter(&cam->i2c_adapter);
510 /* ------------------------------------------------------------------- */
512 * Deal with the controller.
516 * Do everything we think we need to have the interface operating
517 * according to the desired format.
519 static void cafe_ctlr_dma(struct cafe_camera *cam)
522 * Store the first two Y buffers (we aren't supporting
523 * planar formats for now, so no UV bufs). Then either
524 * set the third if it exists, or tell the controller
527 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
528 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
529 if (cam->nbufs > 2) {
530 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
531 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
534 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
535 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
538 static void cafe_ctlr_image(struct cafe_camera *cam)
541 struct v4l2_pix_format *fmt = &cam->pix_format;
543 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
544 (fmt->bytesperline & IMGSZ_H_MASK);
545 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
546 cafe_reg_write(cam, REG_IMGOFFSET, 0);
547 /* YPITCH just drops the last two bits */
548 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
551 * Tell the controller about the image format we are using.
553 switch (cam->pix_format.pixelformat) {
554 case V4L2_PIX_FMT_YUYV:
555 cafe_reg_write_mask(cam, REG_CTRL0,
556 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
560 case V4L2_PIX_FMT_RGB444:
561 cafe_reg_write_mask(cam, REG_CTRL0,
562 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
567 case V4L2_PIX_FMT_RGB565:
568 cafe_reg_write_mask(cam, REG_CTRL0,
569 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
574 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
578 * Make sure it knows we want to use hsync/vsync.
580 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
586 * Configure the controller for operation; caller holds the
589 static int cafe_ctlr_configure(struct cafe_camera *cam)
593 spin_lock_irqsave(&cam->dev_lock, flags);
595 cafe_ctlr_image(cam);
596 cafe_set_config_needed(cam, 0);
597 spin_unlock_irqrestore(&cam->dev_lock, flags);
601 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
604 * Clear any pending interrupts, since we do not
605 * expect to have I/O active prior to enabling.
607 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
608 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
611 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
613 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
617 * Make the controller start grabbing images. Everything must
618 * be set up before doing this.
620 static void cafe_ctlr_start(struct cafe_camera *cam)
622 /* set_bit performs a read, so no other barrier should be
624 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
627 static void cafe_ctlr_stop(struct cafe_camera *cam)
629 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
632 static void cafe_ctlr_init(struct cafe_camera *cam)
636 spin_lock_irqsave(&cam->dev_lock, flags);
638 * Added magic to bring up the hardware on the B-Test board
640 cafe_reg_write(cam, 0x3038, 0x8);
641 cafe_reg_write(cam, 0x315c, 0x80008);
643 * Go through the dance needed to wake the device up.
644 * Note that these registers are global and shared
645 * with the NAND and SD devices. Interaction between the
646 * three still needs to be examined.
648 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
649 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
650 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
652 * Here we must wait a bit for the controller to come around.
654 spin_unlock_irqrestore(&cam->dev_lock, flags);
656 spin_lock_irqsave(&cam->dev_lock, flags);
658 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
659 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
661 * Make sure it's not powered down.
663 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
665 * Turn off the enable bit. It sure should be off anyway,
666 * but it's good to be sure.
668 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
670 * Mask all interrupts.
672 cafe_reg_write(cam, REG_IRQMASK, 0);
674 * Clock the sensor appropriately. Controller clock should
675 * be 48MHz, sensor "typical" value is half that.
677 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
678 spin_unlock_irqrestore(&cam->dev_lock, flags);
683 * Stop the controller, and don't return until we're really sure that no
684 * further DMA is going on.
686 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
691 * Theory: stop the camera controller (whether it is operating
692 * or not). Delay briefly just in case we race with the SOF
693 * interrupt, then wait until no DMA is active.
695 spin_lock_irqsave(&cam->dev_lock, flags);
697 spin_unlock_irqrestore(&cam->dev_lock, flags);
699 wait_event_timeout(cam->iowait,
700 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
701 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
702 cam_err(cam, "Timeout waiting for DMA to end\n");
703 /* This would be bad news - what now? */
704 spin_lock_irqsave(&cam->dev_lock, flags);
706 cafe_ctlr_irq_disable(cam);
707 spin_unlock_irqrestore(&cam->dev_lock, flags);
713 static void cafe_ctlr_power_up(struct cafe_camera *cam)
717 spin_lock_irqsave(&cam->dev_lock, flags);
718 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
720 * Part one of the sensor dance: turn the global
723 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
724 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
726 * Put the sensor into operational mode (assumes OLPC-style
727 * wiring). Control 0 is reset - set to 1 to operate.
728 * Control 1 is power down, set to 0 to operate.
730 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
731 /* mdelay(1); */ /* Marvell says 1ms will do it */
732 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
733 /* mdelay(1); */ /* Enough? */
734 spin_unlock_irqrestore(&cam->dev_lock, flags);
735 msleep(5); /* Just to be sure */
738 static void cafe_ctlr_power_down(struct cafe_camera *cam)
742 spin_lock_irqsave(&cam->dev_lock, flags);
743 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
744 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
745 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
746 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
747 spin_unlock_irqrestore(&cam->dev_lock, flags);
750 /* -------------------------------------------------------------------- */
752 * Communications with the sensor.
755 static int __cafe_cam_reset(struct cafe_camera *cam)
757 return sensor_call(cam, core, reset, 0);
761 * We have found the sensor on the i2c. Let's try to have a
764 static int cafe_cam_init(struct cafe_camera *cam)
766 struct v4l2_dbg_chip_ident chip;
769 mutex_lock(&cam->s_mutex);
770 if (cam->state != S_NOTREADY)
771 cam_warn(cam, "Cam init with device in funky state %d",
773 ret = __cafe_cam_reset(cam);
776 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
777 chip.match.addr = cam->sensor_addr;
778 ret = sensor_call(cam, core, g_chip_ident, &chip);
781 cam->sensor_type = chip.ident;
782 if (cam->sensor_type != V4L2_IDENT_OV7670) {
783 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
787 /* Get/set parameters? */
791 cafe_ctlr_power_down(cam);
792 mutex_unlock(&cam->s_mutex);
797 * Configure the sensor to match the parameters we have. Caller should
800 static int cafe_cam_set_flip(struct cafe_camera *cam)
802 struct v4l2_control ctrl;
804 memset(&ctrl, 0, sizeof(ctrl));
805 ctrl.id = V4L2_CID_VFLIP;
807 return sensor_call(cam, core, s_ctrl, &ctrl);
811 static int cafe_cam_configure(struct cafe_camera *cam)
813 struct v4l2_format fmt;
816 if (cam->state != S_IDLE)
818 fmt.fmt.pix = cam->pix_format;
819 ret = sensor_call(cam, core, init, 0);
821 ret = sensor_call(cam, video, s_fmt, &fmt);
823 * OV7670 does weird things if flip is set *before* format...
825 ret += cafe_cam_set_flip(cam);
829 /* -------------------------------------------------------------------- */
831 * DMA buffer management. These functions need s_mutex held.
834 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
835 * does a get_free_pages() call, and we waste a good chunk of an orderN
836 * allocation. Should try to allocate the whole set in one chunk.
838 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
842 cafe_set_config_needed(cam, 1);
844 cam->dma_buf_size = dma_buf_size;
846 cam->dma_buf_size = cam->pix_format.sizeimage;
851 for (i = 0; i < n_dma_bufs; i++) {
852 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
853 cam->dma_buf_size, cam->dma_handles + i,
855 if (cam->dma_bufs[i] == NULL) {
856 cam_warn(cam, "Failed to allocate DMA buffer\n");
859 /* For debug, remove eventually */
860 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
864 switch (cam->nbufs) {
866 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
867 cam->dma_bufs[0], cam->dma_handles[0]);
870 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
875 cam_warn(cam, "Will limp along with only 2 buffers\n");
881 static void cafe_free_dma_bufs(struct cafe_camera *cam)
885 for (i = 0; i < cam->nbufs; i++) {
886 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
887 cam->dma_bufs[i], cam->dma_handles[i]);
888 cam->dma_bufs[i] = NULL;
897 /* ----------------------------------------------------------------------- */
899 * Here starts the V4L2 interface code.
903 * Read an image from the device.
905 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
906 char __user *buffer, size_t len, loff_t *pos)
911 spin_lock_irqsave(&cam->dev_lock, flags);
912 if (cam->next_buf < 0) {
913 cam_err(cam, "deliver_buffer: No next buffer\n");
914 spin_unlock_irqrestore(&cam->dev_lock, flags);
917 bufno = cam->next_buf;
918 clear_bit(bufno, &cam->flags);
919 if (++(cam->next_buf) >= cam->nbufs)
921 if (! test_bit(cam->next_buf, &cam->flags))
924 spin_unlock_irqrestore(&cam->dev_lock, flags);
926 if (len > cam->pix_format.sizeimage)
927 len = cam->pix_format.sizeimage;
928 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
935 * Get everything ready, and start grabbing frames.
937 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
943 * Configuration. If we still don't have DMA buffers,
944 * make one last, desperate attempt.
947 if (cafe_alloc_dma_bufs(cam, 0))
950 if (cafe_needs_config(cam)) {
951 cafe_cam_configure(cam);
952 ret = cafe_ctlr_configure(cam);
960 spin_lock_irqsave(&cam->dev_lock, flags);
961 cafe_reset_buffers(cam);
962 cafe_ctlr_irq_enable(cam);
964 cafe_ctlr_start(cam);
965 spin_unlock_irqrestore(&cam->dev_lock, flags);
970 static ssize_t cafe_v4l_read(struct file *filp,
971 char __user *buffer, size_t len, loff_t *pos)
973 struct cafe_camera *cam = filp->private_data;
977 * Perhaps we're in speculative read mode and already
980 mutex_lock(&cam->s_mutex);
981 if (cam->state == S_SPECREAD) {
982 if (cam->next_buf >= 0) {
983 ret = cafe_deliver_buffer(cam, buffer, len, pos);
987 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
990 } else if (cam->state != S_IDLE) {
996 * v4l2: multiple processes can open the device, but only
997 * one gets to grab data from it.
999 if (cam->owner && cam->owner != filp) {
1006 * Do setup if need be.
1008 if (cam->state != S_SPECREAD) {
1009 ret = cafe_read_setup(cam, S_SINGLEREAD);
1014 * Wait for something to happen. This should probably
1015 * be interruptible (FIXME).
1017 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1018 if (cam->next_buf < 0) {
1019 cam_err(cam, "read() operation timed out\n");
1020 cafe_ctlr_stop_dma(cam);
1025 * Give them their data and we should be done.
1027 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1030 mutex_unlock(&cam->s_mutex);
1042 * Streaming I/O support.
1047 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1048 enum v4l2_buf_type type)
1050 struct cafe_camera *cam = filp->private_data;
1053 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1055 mutex_lock(&cam->s_mutex);
1056 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1060 ret = cafe_read_setup(cam, S_STREAMING);
1063 mutex_unlock(&cam->s_mutex);
1069 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1070 enum v4l2_buf_type type)
1072 struct cafe_camera *cam = filp->private_data;
1075 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1077 mutex_lock(&cam->s_mutex);
1078 if (cam->state != S_STREAMING)
1081 cafe_ctlr_stop_dma(cam);
1085 mutex_unlock(&cam->s_mutex);
1092 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1094 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1096 INIT_LIST_HEAD(&buf->list);
1097 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1098 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1099 if (buf->buffer == NULL)
1104 buf->v4lbuf.index = index;
1105 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1106 buf->v4lbuf.field = V4L2_FIELD_NONE;
1107 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1109 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1110 * just uses the length times the index, but the spec warns
1111 * against doing just that - vma merging problems. So we
1112 * leave a gap between each pair of buffers.
1114 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1118 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1123 * If any buffers are mapped, we cannot free them at all.
1125 for (i = 0; i < cam->n_sbufs; i++)
1126 if (cam->sb_bufs[i].mapcount > 0)
1131 for (i = 0; i < cam->n_sbufs; i++)
1132 vfree(cam->sb_bufs[i].buffer);
1134 kfree(cam->sb_bufs);
1135 cam->sb_bufs = NULL;
1136 INIT_LIST_HEAD(&cam->sb_avail);
1137 INIT_LIST_HEAD(&cam->sb_full);
1143 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1144 struct v4l2_requestbuffers *req)
1146 struct cafe_camera *cam = filp->private_data;
1147 int ret = 0; /* Silence warning */
1150 * Make sure it's something we can do. User pointers could be
1151 * implemented without great pain, but that's not been done yet.
1153 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1155 if (req->memory != V4L2_MEMORY_MMAP)
1158 * If they ask for zero buffers, they really want us to stop streaming
1159 * (if it's happening) and free everything. Should we check owner?
1161 mutex_lock(&cam->s_mutex);
1162 if (req->count == 0) {
1163 if (cam->state == S_STREAMING)
1164 cafe_ctlr_stop_dma(cam);
1165 ret = cafe_free_sio_buffers (cam);
1169 * Device needs to be idle and working. We *could* try to do the
1170 * right thing in S_SPECREAD by shutting things down, but it
1171 * probably doesn't matter.
1173 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1179 if (req->count < min_buffers)
1180 req->count = min_buffers;
1181 else if (req->count > max_buffers)
1182 req->count = max_buffers;
1183 if (cam->n_sbufs > 0) {
1184 ret = cafe_free_sio_buffers(cam);
1189 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1191 if (cam->sb_bufs == NULL) {
1195 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1196 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1201 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1202 kfree(cam->sb_bufs);
1203 req->count = cam->n_sbufs; /* In case of partial success */
1206 mutex_unlock(&cam->s_mutex);
1211 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1212 struct v4l2_buffer *buf)
1214 struct cafe_camera *cam = filp->private_data;
1217 mutex_lock(&cam->s_mutex);
1218 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1220 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1222 *buf = cam->sb_bufs[buf->index].v4lbuf;
1225 mutex_unlock(&cam->s_mutex);
1229 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1230 struct v4l2_buffer *buf)
1232 struct cafe_camera *cam = filp->private_data;
1233 struct cafe_sio_buffer *sbuf;
1235 unsigned long flags;
1237 mutex_lock(&cam->s_mutex);
1238 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1240 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1242 sbuf = cam->sb_bufs + buf->index;
1243 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1244 ret = 0; /* Already queued?? */
1247 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1248 /* Spec doesn't say anything, seems appropriate tho */
1252 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1253 spin_lock_irqsave(&cam->dev_lock, flags);
1254 list_add(&sbuf->list, &cam->sb_avail);
1255 spin_unlock_irqrestore(&cam->dev_lock, flags);
1258 mutex_unlock(&cam->s_mutex);
1262 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1263 struct v4l2_buffer *buf)
1265 struct cafe_camera *cam = filp->private_data;
1266 struct cafe_sio_buffer *sbuf;
1268 unsigned long flags;
1270 mutex_lock(&cam->s_mutex);
1271 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1273 if (cam->state != S_STREAMING)
1275 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1280 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1281 mutex_unlock(&cam->s_mutex);
1282 if (wait_event_interruptible(cam->iowait,
1283 !list_empty(&cam->sb_full))) {
1287 mutex_lock(&cam->s_mutex);
1290 if (cam->state != S_STREAMING)
1293 spin_lock_irqsave(&cam->dev_lock, flags);
1294 /* Should probably recheck !list_empty() here */
1295 sbuf = list_entry(cam->sb_full.next,
1296 struct cafe_sio_buffer, list);
1297 list_del_init(&sbuf->list);
1298 spin_unlock_irqrestore(&cam->dev_lock, flags);
1299 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1300 *buf = sbuf->v4lbuf;
1305 mutex_unlock(&cam->s_mutex);
1312 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1314 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1316 * Locking: done under mmap_sem, so we don't need to
1317 * go back to the camera lock here.
1323 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1325 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1327 mutex_lock(&sbuf->cam->s_mutex);
1329 /* Docs say we should stop I/O too... */
1330 if (sbuf->mapcount == 0)
1331 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1332 mutex_unlock(&sbuf->cam->s_mutex);
1335 static struct vm_operations_struct cafe_v4l_vm_ops = {
1336 .open = cafe_v4l_vm_open,
1337 .close = cafe_v4l_vm_close
1341 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1343 struct cafe_camera *cam = filp->private_data;
1344 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1347 struct cafe_sio_buffer *sbuf = NULL;
1349 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1352 * Find the buffer they are looking for.
1354 mutex_lock(&cam->s_mutex);
1355 for (i = 0; i < cam->n_sbufs; i++)
1356 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1357 sbuf = cam->sb_bufs + i;
1363 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1366 vma->vm_flags |= VM_DONTEXPAND;
1367 vma->vm_private_data = sbuf;
1368 vma->vm_ops = &cafe_v4l_vm_ops;
1369 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1370 cafe_v4l_vm_open(vma);
1373 mutex_unlock(&cam->s_mutex);
1379 static int cafe_v4l_open(struct file *filp)
1381 struct cafe_camera *cam = video_drvdata(filp);
1383 filp->private_data = cam;
1385 mutex_lock(&cam->s_mutex);
1386 if (cam->users == 0) {
1387 cafe_ctlr_power_up(cam);
1388 __cafe_cam_reset(cam);
1389 cafe_set_config_needed(cam, 1);
1390 /* FIXME make sure this is complete */
1393 mutex_unlock(&cam->s_mutex);
1398 static int cafe_v4l_release(struct file *filp)
1400 struct cafe_camera *cam = filp->private_data;
1402 mutex_lock(&cam->s_mutex);
1404 if (filp == cam->owner) {
1405 cafe_ctlr_stop_dma(cam);
1406 cafe_free_sio_buffers(cam);
1409 if (cam->users == 0) {
1410 cafe_ctlr_power_down(cam);
1411 if (alloc_bufs_at_read)
1412 cafe_free_dma_bufs(cam);
1414 mutex_unlock(&cam->s_mutex);
1420 static unsigned int cafe_v4l_poll(struct file *filp,
1421 struct poll_table_struct *pt)
1423 struct cafe_camera *cam = filp->private_data;
1425 poll_wait(filp, &cam->iowait, pt);
1426 if (cam->next_buf >= 0)
1427 return POLLIN | POLLRDNORM;
1433 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1434 struct v4l2_queryctrl *qc)
1436 struct cafe_camera *cam = priv;
1439 mutex_lock(&cam->s_mutex);
1440 ret = sensor_call(cam, core, queryctrl, qc);
1441 mutex_unlock(&cam->s_mutex);
1446 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1447 struct v4l2_control *ctrl)
1449 struct cafe_camera *cam = priv;
1452 mutex_lock(&cam->s_mutex);
1453 ret = sensor_call(cam, core, g_ctrl, ctrl);
1454 mutex_unlock(&cam->s_mutex);
1459 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1460 struct v4l2_control *ctrl)
1462 struct cafe_camera *cam = priv;
1465 mutex_lock(&cam->s_mutex);
1466 ret = sensor_call(cam, core, s_ctrl, ctrl);
1467 mutex_unlock(&cam->s_mutex);
1475 static int cafe_vidioc_querycap(struct file *file, void *priv,
1476 struct v4l2_capability *cap)
1478 strcpy(cap->driver, "cafe_ccic");
1479 strcpy(cap->card, "cafe_ccic");
1480 cap->version = CAFE_VERSION;
1481 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1482 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1488 * The default format we use until somebody says otherwise.
1490 static struct v4l2_pix_format cafe_def_pix_format = {
1492 .height = VGA_HEIGHT,
1493 .pixelformat = V4L2_PIX_FMT_YUYV,
1494 .field = V4L2_FIELD_NONE,
1495 .bytesperline = VGA_WIDTH*2,
1496 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1499 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1500 void *priv, struct v4l2_fmtdesc *fmt)
1502 struct cafe_camera *cam = priv;
1505 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1507 mutex_lock(&cam->s_mutex);
1508 ret = sensor_call(cam, video, enum_fmt, fmt);
1509 mutex_unlock(&cam->s_mutex);
1514 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1515 struct v4l2_format *fmt)
1517 struct cafe_camera *cam = priv;
1520 mutex_lock(&cam->s_mutex);
1521 ret = sensor_call(cam, video, try_fmt, fmt);
1522 mutex_unlock(&cam->s_mutex);
1526 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1527 struct v4l2_format *fmt)
1529 struct cafe_camera *cam = priv;
1533 * Can't do anything if the device is not idle
1534 * Also can't if there are streaming buffers in place.
1536 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1539 * See if the formatting works in principle.
1541 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1545 * Now we start to change things for real, so let's do it
1548 mutex_lock(&cam->s_mutex);
1549 cam->pix_format = fmt->fmt.pix;
1551 * Make sure we have appropriate DMA buffers.
1554 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1555 cafe_free_dma_bufs(cam);
1556 if (cam->nbufs == 0) {
1557 if (cafe_alloc_dma_bufs(cam, 0))
1561 * It looks like this might work, so let's program the sensor.
1563 ret = cafe_cam_configure(cam);
1565 ret = cafe_ctlr_configure(cam);
1567 mutex_unlock(&cam->s_mutex);
1572 * Return our stored notion of how the camera is/should be configured.
1573 * The V4l2 spec wants us to be smarter, and actually get this from
1574 * the camera (and not mess with it at open time). Someday.
1576 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1577 struct v4l2_format *f)
1579 struct cafe_camera *cam = priv;
1581 f->fmt.pix = cam->pix_format;
1586 * We only have one input - the sensor - so minimize the nonsense here.
1588 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1589 struct v4l2_input *input)
1591 if (input->index != 0)
1594 input->type = V4L2_INPUT_TYPE_CAMERA;
1595 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1596 strcpy(input->name, "Camera");
1600 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1606 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1614 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1620 * G/S_PARM. Most of this is done by the sensor, but we are
1621 * the level which controls the number of read buffers.
1623 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1624 struct v4l2_streamparm *parms)
1626 struct cafe_camera *cam = priv;
1629 mutex_lock(&cam->s_mutex);
1630 ret = sensor_call(cam, video, g_parm, parms);
1631 mutex_unlock(&cam->s_mutex);
1632 parms->parm.capture.readbuffers = n_dma_bufs;
1636 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1637 struct v4l2_streamparm *parms)
1639 struct cafe_camera *cam = priv;
1642 mutex_lock(&cam->s_mutex);
1643 ret = sensor_call(cam, video, s_parm, parms);
1644 mutex_unlock(&cam->s_mutex);
1645 parms->parm.capture.readbuffers = n_dma_bufs;
1650 * This template device holds all of those v4l2 methods; we
1651 * clone it for specific real devices.
1654 static const struct v4l2_file_operations cafe_v4l_fops = {
1655 .owner = THIS_MODULE,
1656 .open = cafe_v4l_open,
1657 .release = cafe_v4l_release,
1658 .read = cafe_v4l_read,
1659 .poll = cafe_v4l_poll,
1660 .mmap = cafe_v4l_mmap,
1661 .ioctl = video_ioctl2,
1664 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1665 .vidioc_querycap = cafe_vidioc_querycap,
1666 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1667 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1668 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1669 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1670 .vidioc_enum_input = cafe_vidioc_enum_input,
1671 .vidioc_g_input = cafe_vidioc_g_input,
1672 .vidioc_s_input = cafe_vidioc_s_input,
1673 .vidioc_s_std = cafe_vidioc_s_std,
1674 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1675 .vidioc_querybuf = cafe_vidioc_querybuf,
1676 .vidioc_qbuf = cafe_vidioc_qbuf,
1677 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1678 .vidioc_streamon = cafe_vidioc_streamon,
1679 .vidioc_streamoff = cafe_vidioc_streamoff,
1680 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1681 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1682 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1683 .vidioc_g_parm = cafe_vidioc_g_parm,
1684 .vidioc_s_parm = cafe_vidioc_s_parm,
1687 static struct video_device cafe_v4l_template = {
1689 .minor = -1, /* Get one dynamically */
1690 .tvnorms = V4L2_STD_NTSC_M,
1691 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1693 .fops = &cafe_v4l_fops,
1694 .ioctl_ops = &cafe_v4l_ioctl_ops,
1695 .release = video_device_release_empty,
1699 /* ---------------------------------------------------------------------- */
1701 * Interrupt handler stuff
1706 static void cafe_frame_tasklet(unsigned long data)
1708 struct cafe_camera *cam = (struct cafe_camera *) data;
1710 unsigned long flags;
1711 struct cafe_sio_buffer *sbuf;
1713 spin_lock_irqsave(&cam->dev_lock, flags);
1714 for (i = 0; i < cam->nbufs; i++) {
1715 int bufno = cam->next_buf;
1716 if (bufno < 0) { /* "will never happen" */
1717 cam_err(cam, "No valid bufs in tasklet!\n");
1720 if (++(cam->next_buf) >= cam->nbufs)
1722 if (! test_bit(bufno, &cam->flags))
1724 if (list_empty(&cam->sb_avail))
1725 break; /* Leave it valid, hope for better later */
1726 clear_bit(bufno, &cam->flags);
1727 sbuf = list_entry(cam->sb_avail.next,
1728 struct cafe_sio_buffer, list);
1730 * Drop the lock during the big copy. This *should* be safe...
1732 spin_unlock_irqrestore(&cam->dev_lock, flags);
1733 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1734 cam->pix_format.sizeimage);
1735 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1736 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1737 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1738 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1739 spin_lock_irqsave(&cam->dev_lock, flags);
1740 list_move_tail(&sbuf->list, &cam->sb_full);
1742 if (! list_empty(&cam->sb_full))
1743 wake_up(&cam->iowait);
1744 spin_unlock_irqrestore(&cam->dev_lock, flags);
1749 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1752 * Basic frame housekeeping.
1754 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1755 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1756 set_bit(frame, &cam->flags);
1757 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1758 if (cam->next_buf < 0)
1759 cam->next_buf = frame;
1760 cam->buf_seq[frame] = ++(cam->sequence);
1762 switch (cam->state) {
1764 * If in single read mode, try going speculative.
1767 cam->state = S_SPECREAD;
1768 cam->specframes = 0;
1769 wake_up(&cam->iowait);
1773 * If we are already doing speculative reads, and nobody is
1774 * reading them, just stop.
1777 if (++(cam->specframes) >= cam->nbufs) {
1778 cafe_ctlr_stop(cam);
1779 cafe_ctlr_irq_disable(cam);
1780 cam->state = S_IDLE;
1782 wake_up(&cam->iowait);
1785 * For the streaming case, we defer the real work to the
1788 * FIXME: if the application is not consuming the buffers,
1789 * we should eventually put things on hold and restart in
1793 tasklet_schedule(&cam->s_tasklet);
1797 cam_err(cam, "Frame interrupt in non-operational state\n");
1805 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1809 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1811 * Handle any frame completions. There really should
1812 * not be more than one of these, or we have fallen
1815 for (frame = 0; frame < cam->nbufs; frame++)
1816 if (irqs & (IRQ_EOF0 << frame))
1817 cafe_frame_complete(cam, frame);
1819 * If a frame starts, note that we have DMA active. This
1820 * code assumes that we won't get multiple frame interrupts
1821 * at once; may want to rethink that.
1823 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1824 set_bit(CF_DMA_ACTIVE, &cam->flags);
1829 static irqreturn_t cafe_irq(int irq, void *data)
1831 struct cafe_camera *cam = data;
1834 spin_lock(&cam->dev_lock);
1835 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1836 if ((irqs & ALLIRQS) == 0) {
1837 spin_unlock(&cam->dev_lock);
1840 if (irqs & FRAMEIRQS)
1841 cafe_frame_irq(cam, irqs);
1842 if (irqs & TWSIIRQS) {
1843 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1844 wake_up(&cam->smbus_wait);
1846 spin_unlock(&cam->dev_lock);
1851 /* -------------------------------------------------------------------------- */
1852 #ifdef CONFIG_VIDEO_ADV_DEBUG
1857 static char cafe_debug_buf[1024];
1858 static struct dentry *cafe_dfs_root;
1860 static void cafe_dfs_setup(void)
1862 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1863 if (IS_ERR(cafe_dfs_root)) {
1864 cafe_dfs_root = NULL; /* Never mind */
1865 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1869 static void cafe_dfs_shutdown(void)
1872 debugfs_remove(cafe_dfs_root);
1875 static int cafe_dfs_open(struct inode *inode, struct file *file)
1877 file->private_data = inode->i_private;
1881 static ssize_t cafe_dfs_read_regs(struct file *file,
1882 char __user *buf, size_t count, loff_t *ppos)
1884 struct cafe_camera *cam = file->private_data;
1885 char *s = cafe_debug_buf;
1888 for (offset = 0; offset < 0x44; offset += 4)
1889 s += sprintf(s, "%02x: %08x\n", offset,
1890 cafe_reg_read(cam, offset));
1891 for (offset = 0x88; offset <= 0x90; offset += 4)
1892 s += sprintf(s, "%02x: %08x\n", offset,
1893 cafe_reg_read(cam, offset));
1894 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1895 s += sprintf(s, "%02x: %08x\n", offset,
1896 cafe_reg_read(cam, offset));
1897 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1898 s += sprintf(s, "%04x: %08x\n", offset,
1899 cafe_reg_read(cam, offset));
1900 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1901 s - cafe_debug_buf);
1904 static const struct file_operations cafe_dfs_reg_ops = {
1905 .owner = THIS_MODULE,
1906 .read = cafe_dfs_read_regs,
1907 .open = cafe_dfs_open
1910 static ssize_t cafe_dfs_read_cam(struct file *file,
1911 char __user *buf, size_t count, loff_t *ppos)
1913 struct cafe_camera *cam = file->private_data;
1914 char *s = cafe_debug_buf;
1919 for (offset = 0x0; offset < 0x8a; offset++)
1923 cafe_smbus_read_data(cam, cam->sensor_addr, offset, &v);
1924 s += sprintf(s, "%02x: %02x\n", offset, v);
1926 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1927 s - cafe_debug_buf);
1930 static const struct file_operations cafe_dfs_cam_ops = {
1931 .owner = THIS_MODULE,
1932 .read = cafe_dfs_read_cam,
1933 .open = cafe_dfs_open
1938 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
1944 sprintf(fname, "regs-%d", cam->vdev.num);
1945 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
1946 cam, &cafe_dfs_reg_ops);
1947 sprintf(fname, "cam-%d", cam->vdev.num);
1948 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
1949 cam, &cafe_dfs_cam_ops);
1953 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
1955 if (! IS_ERR(cam->dfs_regs))
1956 debugfs_remove(cam->dfs_regs);
1957 if (! IS_ERR(cam->dfs_cam_regs))
1958 debugfs_remove(cam->dfs_cam_regs);
1963 #define cafe_dfs_setup()
1964 #define cafe_dfs_shutdown()
1965 #define cafe_dfs_cam_setup(cam)
1966 #define cafe_dfs_cam_shutdown(cam)
1967 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1972 /* ------------------------------------------------------------------------*/
1974 * PCI interface stuff.
1977 static int cafe_pci_probe(struct pci_dev *pdev,
1978 const struct pci_device_id *id)
1981 struct cafe_camera *cam;
1984 * Start putting together one of our big camera structures.
1987 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1990 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1994 mutex_init(&cam->s_mutex);
1995 mutex_lock(&cam->s_mutex);
1996 spin_lock_init(&cam->dev_lock);
1997 cam->state = S_NOTREADY;
1998 cafe_set_config_needed(cam, 1);
1999 init_waitqueue_head(&cam->smbus_wait);
2000 init_waitqueue_head(&cam->iowait);
2002 cam->pix_format = cafe_def_pix_format;
2003 INIT_LIST_HEAD(&cam->dev_list);
2004 INIT_LIST_HEAD(&cam->sb_avail);
2005 INIT_LIST_HEAD(&cam->sb_full);
2006 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2008 * Get set up on the PCI bus.
2010 ret = pci_enable_device(pdev);
2013 pci_set_master(pdev);
2016 cam->regs = pci_iomap(pdev, 0, 0);
2018 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2021 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2025 * Initialize the controller and leave it powered up. It will
2026 * stay that way until the sensor driver shows up.
2028 cafe_ctlr_init(cam);
2029 cafe_ctlr_power_up(cam);
2031 * Set up I2C/SMBUS communications. We have to drop the mutex here
2032 * because the sensor could attach in this call chain, leading to
2033 * unsightly deadlocks.
2035 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2036 ret = cafe_smbus_setup(cam);
2040 cam->sensor_addr = 0x42;
2041 cam->sensor = v4l2_i2c_new_subdev(&cam->i2c_adapter,
2042 "ov7670", "ov7670", cam->sensor_addr);
2043 if (cam->sensor == NULL) {
2047 ret = cafe_cam_init(cam);
2052 * Get the v4l2 setup done.
2054 mutex_lock(&cam->s_mutex);
2055 cam->vdev = cafe_v4l_template;
2056 cam->vdev.debug = 0;
2057 /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
2058 cam->vdev.v4l2_dev = &cam->v4l2_dev;
2059 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
2062 video_set_drvdata(&cam->vdev, cam);
2065 * If so requested, try to get our DMA buffers now.
2067 if (!alloc_bufs_at_read) {
2068 if (cafe_alloc_dma_bufs(cam, 1))
2069 cam_warn(cam, "Unable to alloc DMA buffers at load"
2070 " will try again later.");
2073 cafe_dfs_cam_setup(cam);
2074 mutex_unlock(&cam->s_mutex);
2078 cafe_smbus_shutdown(cam);
2080 cafe_ctlr_power_down(cam);
2081 free_irq(pdev->irq, cam);
2083 pci_iounmap(pdev, cam->regs);
2085 v4l2_device_unregister(&cam->v4l2_dev);
2094 * Shut down an initialized device
2096 static void cafe_shutdown(struct cafe_camera *cam)
2098 /* FIXME: Make sure we take care of everything here */
2099 cafe_dfs_cam_shutdown(cam);
2100 if (cam->n_sbufs > 0)
2101 /* What if they are still mapped? Shouldn't be, but... */
2102 cafe_free_sio_buffers(cam);
2103 cafe_ctlr_stop_dma(cam);
2104 cafe_ctlr_power_down(cam);
2105 cafe_smbus_shutdown(cam);
2106 cafe_free_dma_bufs(cam);
2107 free_irq(cam->pdev->irq, cam);
2108 pci_iounmap(cam->pdev, cam->regs);
2109 video_unregister_device(&cam->vdev);
2113 static void cafe_pci_remove(struct pci_dev *pdev)
2115 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2116 struct cafe_camera *cam = to_cam(v4l2_dev);
2119 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2122 mutex_lock(&cam->s_mutex);
2124 cam_warn(cam, "Removing a device with users!\n");
2126 v4l2_device_unregister(&cam->v4l2_dev);
2128 /* No unlock - it no longer exists */
2134 * Basic power management.
2136 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2138 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2139 struct cafe_camera *cam = to_cam(v4l2_dev);
2141 enum cafe_state cstate;
2143 ret = pci_save_state(pdev);
2146 cstate = cam->state; /* HACK - stop_dma sets to idle */
2147 cafe_ctlr_stop_dma(cam);
2148 cafe_ctlr_power_down(cam);
2149 pci_disable_device(pdev);
2150 cam->state = cstate;
2155 static int cafe_pci_resume(struct pci_dev *pdev)
2157 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2158 struct cafe_camera *cam = to_cam(v4l2_dev);
2161 ret = pci_restore_state(pdev);
2164 ret = pci_enable_device(pdev);
2167 cam_warn(cam, "Unable to re-enable device on resume!\n");
2170 cafe_ctlr_init(cam);
2171 cafe_ctlr_power_down(cam);
2173 mutex_lock(&cam->s_mutex);
2174 if (cam->users > 0) {
2175 cafe_ctlr_power_up(cam);
2176 __cafe_cam_reset(cam);
2178 mutex_unlock(&cam->s_mutex);
2180 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2181 if (cam->state == S_SPECREAD)
2182 cam->state = S_IDLE; /* Don't bother restarting */
2183 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2184 ret = cafe_read_setup(cam, cam->state);
2188 #endif /* CONFIG_PM */
2191 static struct pci_device_id cafe_ids[] = {
2192 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2193 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2197 MODULE_DEVICE_TABLE(pci, cafe_ids);
2199 static struct pci_driver cafe_pci_driver = {
2200 .name = "cafe1000-ccic",
2201 .id_table = cafe_ids,
2202 .probe = cafe_pci_probe,
2203 .remove = cafe_pci_remove,
2205 .suspend = cafe_pci_suspend,
2206 .resume = cafe_pci_resume,
2213 static int __init cafe_init(void)
2217 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2220 ret = pci_register_driver(&cafe_pci_driver);
2222 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2232 static void __exit cafe_exit(void)
2234 pci_unregister_driver(&cafe_pci_driver);
2235 cafe_dfs_shutdown();
2238 module_init(cafe_init);
2239 module_exit(cafe_exit);