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/jiffies.h>
37 #include <linux/vmalloc.h>
39 #include <asm/uaccess.h>
42 #include "cafe_ccic-regs.h"
44 #define CAFE_VERSION 0x000002
50 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
51 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
52 MODULE_LICENSE("GPL");
53 MODULE_SUPPORTED_DEVICE("Video");
56 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
57 * we must have physically contiguous buffers to bring frames into.
58 * These parameters control how many buffers we use, whether we
59 * allocate them at load time (better chance of success, but nails down
60 * memory) or when somebody tries to use the camera (riskier), and,
61 * for load-time allocation, how big they should be.
63 * The controller can cycle through three buffers. We could use
64 * more by flipping pointers around, but it probably makes little
68 #define MAX_DMA_BUFS 3
69 static int alloc_bufs_at_read;
70 module_param(alloc_bufs_at_read, bool, 0444);
71 MODULE_PARM_DESC(alloc_bufs_at_read,
72 "Non-zero value causes DMA buffers to be allocated when the "
73 "video capture device is read, rather than at module load "
74 "time. This saves memory, but decreases the chances of "
75 "successfully getting those buffers.");
77 static int n_dma_bufs = 3;
78 module_param(n_dma_bufs, uint, 0644);
79 MODULE_PARM_DESC(n_dma_bufs,
80 "The number of DMA buffers to allocate. Can be either two "
81 "(saves memory, makes timing tighter) or three.");
83 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
84 module_param(dma_buf_size, uint, 0444);
85 MODULE_PARM_DESC(dma_buf_size,
86 "The size of the allocated DMA buffers. If actual operating "
87 "parameters require larger buffers, an attempt to reallocate "
90 static int min_buffers = 1;
91 module_param(min_buffers, uint, 0644);
92 MODULE_PARM_DESC(min_buffers,
93 "The minimum number of streaming I/O buffers we are willing "
96 static int max_buffers = 10;
97 module_param(max_buffers, uint, 0644);
98 MODULE_PARM_DESC(max_buffers,
99 "The maximum number of streaming I/O buffers an application "
100 "will be allowed to allocate. These buffers are big and live "
101 "in vmalloc space.");
104 module_param(flip, bool, 0444);
105 MODULE_PARM_DESC(flip,
106 "If set, the sensor will be instructed to flip the image "
111 S_NOTREADY, /* Not yet initialized */
112 S_IDLE, /* Just hanging around */
113 S_FLAKED, /* Some sort of problem */
114 S_SINGLEREAD, /* In read() */
115 S_SPECREAD, /* Speculative read (for future read()) */
116 S_STREAMING /* Streaming data */
120 * Tracking of streaming I/O buffers.
122 struct cafe_sio_buffer {
123 struct list_head list;
124 struct v4l2_buffer v4lbuf;
125 char *buffer; /* Where it lives in kernel space */
127 struct cafe_camera *cam;
131 * A description of one of our devices.
132 * Locking: controlled by s_mutex. Certain fields, however, require
133 * the dev_lock spinlock; they are marked as such by comments.
134 * dev_lock is also required for access to device registers.
138 struct v4l2_device v4l2_dev;
139 enum cafe_state state;
140 unsigned long flags; /* Buffer status, mainly (dev_lock) */
141 int users; /* How many open FDs */
142 struct file *owner; /* Who has data access (v4l2) */
145 * Subsystem structures.
147 struct pci_dev *pdev;
148 struct video_device vdev;
149 struct i2c_adapter i2c_adapter;
150 struct v4l2_subdev *sensor;
151 unsigned short sensor_addr;
153 unsigned char __iomem *regs;
154 struct list_head dev_list; /* link to other devices */
157 unsigned int nbufs; /* How many are alloc'd */
158 int next_buf; /* Next to consume (dev_lock) */
159 unsigned int dma_buf_size; /* allocated size */
160 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
161 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
162 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
163 unsigned int sequence; /* Frame sequence number */
164 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
166 /* Streaming buffers */
167 unsigned int n_sbufs; /* How many we have */
168 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
169 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
170 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
171 struct tasklet_struct s_tasklet;
173 /* Current operating parameters */
174 u32 sensor_type; /* Currently ov7670 only */
175 struct v4l2_pix_format pix_format;
178 struct mutex s_mutex; /* Access to this structure */
179 spinlock_t dev_lock; /* Access to device */
182 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
183 wait_queue_head_t iowait; /* Waiting on frame data */
187 * Status flags. Always manipulated with bit operations.
189 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
190 #define CF_BUF1_VALID 1
191 #define CF_BUF2_VALID 2
192 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
193 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
195 #define sensor_call(cam, o, f, args...) \
196 v4l2_subdev_call(cam->sensor, o, f, ##args)
198 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
200 return container_of(dev, struct cafe_camera, v4l2_dev);
205 * Start over with DMA buffers - dev_lock needed.
207 static void cafe_reset_buffers(struct cafe_camera *cam)
212 for (i = 0; i < cam->nbufs; i++)
213 clear_bit(i, &cam->flags);
217 static inline int cafe_needs_config(struct cafe_camera *cam)
219 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
222 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
225 set_bit(CF_CONFIG_NEEDED, &cam->flags);
227 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
234 * Debugging and related.
236 #define cam_err(cam, fmt, arg...) \
237 dev_err(&(cam)->pdev->dev, fmt, ##arg);
238 #define cam_warn(cam, fmt, arg...) \
239 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
240 #define cam_dbg(cam, fmt, arg...) \
241 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
244 /* ---------------------------------------------------------------------*/
247 * Device register I/O
249 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
252 iowrite32(val, cam->regs + reg);
255 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
258 return ioread32(cam->regs + reg);
262 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
263 unsigned int val, unsigned int mask)
265 unsigned int v = cafe_reg_read(cam, reg);
267 v = (v & ~mask) | (val & mask);
268 cafe_reg_write(cam, reg, v);
271 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
272 unsigned int reg, unsigned int val)
274 cafe_reg_write_mask(cam, reg, 0, val);
277 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
278 unsigned int reg, unsigned int val)
280 cafe_reg_write_mask(cam, reg, val, val);
285 /* -------------------------------------------------------------------- */
287 * The I2C/SMBUS interface to the camera itself starts here. The
288 * controller handles SMBUS itself, presenting a relatively simple register
289 * interface; all we have to do is to tell it where to route the data.
291 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
293 static int cafe_smbus_write_done(struct cafe_camera *cam)
299 * We must delay after the interrupt, or the controller gets confused
300 * and never does give us good status. Fortunately, we don't do this
304 spin_lock_irqsave(&cam->dev_lock, flags);
305 c1 = cafe_reg_read(cam, REG_TWSIC1);
306 spin_unlock_irqrestore(&cam->dev_lock, flags);
307 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
310 static int cafe_smbus_write_data(struct cafe_camera *cam,
311 u16 addr, u8 command, u8 value)
315 DEFINE_WAIT(the_wait);
317 spin_lock_irqsave(&cam->dev_lock, flags);
318 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
319 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
321 * Marvell sez set clkdiv to all 1's for now.
323 rval |= TWSIC0_CLKDIV;
324 cafe_reg_write(cam, REG_TWSIC0, rval);
325 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
326 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
327 cafe_reg_write(cam, REG_TWSIC1, rval);
328 spin_unlock_irqrestore(&cam->dev_lock, flags);
331 * Time to wait for the write to complete. THIS IS A RACY
332 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
333 * register too quickly after starting the operation sends
334 * the device into a place that may be kinder and better, but
335 * which is absolutely useless for controlling the sensor. In
336 * practice we have plenty of time to get into our sleep state
337 * before the interrupt hits, and the worst case is that we
338 * time out and then see that things completed, so this seems
339 * the best way for now.
342 prepare_to_wait(&cam->smbus_wait, &the_wait,
343 TASK_UNINTERRUPTIBLE);
344 schedule_timeout(1); /* even 1 jiffy is too long */
345 finish_wait(&cam->smbus_wait, &the_wait);
346 } while (!cafe_smbus_write_done(cam));
348 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
349 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
352 spin_lock_irqsave(&cam->dev_lock, flags);
353 rval = cafe_reg_read(cam, REG_TWSIC1);
354 spin_unlock_irqrestore(&cam->dev_lock, flags);
356 if (rval & TWSIC1_WSTAT) {
357 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
361 if (rval & TWSIC1_ERROR) {
362 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
371 static int cafe_smbus_read_done(struct cafe_camera *cam)
377 * We must delay after the interrupt, or the controller gets confused
378 * and never does give us good status. Fortunately, we don't do this
382 spin_lock_irqsave(&cam->dev_lock, flags);
383 c1 = cafe_reg_read(cam, REG_TWSIC1);
384 spin_unlock_irqrestore(&cam->dev_lock, flags);
385 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
390 static int cafe_smbus_read_data(struct cafe_camera *cam,
391 u16 addr, u8 command, u8 *value)
396 spin_lock_irqsave(&cam->dev_lock, flags);
397 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
398 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
400 * Marvel sez set clkdiv to all 1's for now.
402 rval |= TWSIC0_CLKDIV;
403 cafe_reg_write(cam, REG_TWSIC0, rval);
404 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
405 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
406 cafe_reg_write(cam, REG_TWSIC1, rval);
407 spin_unlock_irqrestore(&cam->dev_lock, flags);
409 wait_event_timeout(cam->smbus_wait,
410 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
411 spin_lock_irqsave(&cam->dev_lock, flags);
412 rval = cafe_reg_read(cam, REG_TWSIC1);
413 spin_unlock_irqrestore(&cam->dev_lock, flags);
415 if (rval & TWSIC1_ERROR) {
416 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
419 if (! (rval & TWSIC1_RVALID)) {
420 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
424 *value = rval & 0xff;
429 * Perform a transfer over SMBUS. This thing is called under
430 * the i2c bus lock, so we shouldn't race with ourselves...
432 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
433 unsigned short flags, char rw, u8 command,
434 int size, union i2c_smbus_data *data)
436 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
437 struct cafe_camera *cam = to_cam(v4l2_dev);
441 * This interface would appear to only do byte data ops. OK
442 * it can do word too, but the cam chip has no use for that.
444 if (size != I2C_SMBUS_BYTE_DATA) {
445 cam_err(cam, "funky xfer size %d\n", size);
449 if (rw == I2C_SMBUS_WRITE)
450 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
451 else if (rw == I2C_SMBUS_READ)
452 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
457 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
461 spin_lock_irqsave(&cam->dev_lock, flags);
462 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
463 spin_unlock_irqrestore(&cam->dev_lock, flags);
466 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
468 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
469 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
472 static struct i2c_algorithm cafe_smbus_algo = {
473 .smbus_xfer = cafe_smbus_xfer,
474 .functionality = cafe_smbus_func
477 /* Somebody is on the bus */
478 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
479 static void cafe_ctlr_power_down(struct cafe_camera *cam);
481 static int cafe_smbus_setup(struct cafe_camera *cam)
483 struct i2c_adapter *adap = &cam->i2c_adapter;
486 cafe_smbus_enable_irq(cam);
487 adap->id = I2C_HW_SMBUS_CAFE;
488 adap->owner = THIS_MODULE;
489 adap->algo = &cafe_smbus_algo;
490 strcpy(adap->name, "cafe_ccic");
491 adap->dev.parent = &cam->pdev->dev;
492 i2c_set_adapdata(adap, &cam->v4l2_dev);
493 ret = i2c_add_adapter(adap);
495 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
499 static void cafe_smbus_shutdown(struct cafe_camera *cam)
501 i2c_del_adapter(&cam->i2c_adapter);
505 /* ------------------------------------------------------------------- */
507 * Deal with the controller.
511 * Do everything we think we need to have the interface operating
512 * according to the desired format.
514 static void cafe_ctlr_dma(struct cafe_camera *cam)
517 * Store the first two Y buffers (we aren't supporting
518 * planar formats for now, so no UV bufs). Then either
519 * set the third if it exists, or tell the controller
522 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
523 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
524 if (cam->nbufs > 2) {
525 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
526 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
529 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
530 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
533 static void cafe_ctlr_image(struct cafe_camera *cam)
536 struct v4l2_pix_format *fmt = &cam->pix_format;
538 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
539 (fmt->bytesperline & IMGSZ_H_MASK);
540 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
541 cafe_reg_write(cam, REG_IMGOFFSET, 0);
542 /* YPITCH just drops the last two bits */
543 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
546 * Tell the controller about the image format we are using.
548 switch (cam->pix_format.pixelformat) {
549 case V4L2_PIX_FMT_YUYV:
550 cafe_reg_write_mask(cam, REG_CTRL0,
551 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
555 case V4L2_PIX_FMT_RGB444:
556 cafe_reg_write_mask(cam, REG_CTRL0,
557 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
562 case V4L2_PIX_FMT_RGB565:
563 cafe_reg_write_mask(cam, REG_CTRL0,
564 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
569 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
573 * Make sure it knows we want to use hsync/vsync.
575 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
581 * Configure the controller for operation; caller holds the
584 static int cafe_ctlr_configure(struct cafe_camera *cam)
588 spin_lock_irqsave(&cam->dev_lock, flags);
590 cafe_ctlr_image(cam);
591 cafe_set_config_needed(cam, 0);
592 spin_unlock_irqrestore(&cam->dev_lock, flags);
596 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
599 * Clear any pending interrupts, since we do not
600 * expect to have I/O active prior to enabling.
602 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
603 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
606 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
608 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
612 * Make the controller start grabbing images. Everything must
613 * be set up before doing this.
615 static void cafe_ctlr_start(struct cafe_camera *cam)
617 /* set_bit performs a read, so no other barrier should be
619 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
622 static void cafe_ctlr_stop(struct cafe_camera *cam)
624 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
627 static void cafe_ctlr_init(struct cafe_camera *cam)
631 spin_lock_irqsave(&cam->dev_lock, flags);
633 * Added magic to bring up the hardware on the B-Test board
635 cafe_reg_write(cam, 0x3038, 0x8);
636 cafe_reg_write(cam, 0x315c, 0x80008);
638 * Go through the dance needed to wake the device up.
639 * Note that these registers are global and shared
640 * with the NAND and SD devices. Interaction between the
641 * three still needs to be examined.
643 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
644 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
645 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
647 * Here we must wait a bit for the controller to come around.
649 spin_unlock_irqrestore(&cam->dev_lock, flags);
651 spin_lock_irqsave(&cam->dev_lock, flags);
653 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
654 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
656 * Make sure it's not powered down.
658 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
660 * Turn off the enable bit. It sure should be off anyway,
661 * but it's good to be sure.
663 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
665 * Mask all interrupts.
667 cafe_reg_write(cam, REG_IRQMASK, 0);
669 * Clock the sensor appropriately. Controller clock should
670 * be 48MHz, sensor "typical" value is half that.
672 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
673 spin_unlock_irqrestore(&cam->dev_lock, flags);
678 * Stop the controller, and don't return until we're really sure that no
679 * further DMA is going on.
681 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
686 * Theory: stop the camera controller (whether it is operating
687 * or not). Delay briefly just in case we race with the SOF
688 * interrupt, then wait until no DMA is active.
690 spin_lock_irqsave(&cam->dev_lock, flags);
692 spin_unlock_irqrestore(&cam->dev_lock, flags);
694 wait_event_timeout(cam->iowait,
695 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
696 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
697 cam_err(cam, "Timeout waiting for DMA to end\n");
698 /* This would be bad news - what now? */
699 spin_lock_irqsave(&cam->dev_lock, flags);
701 cafe_ctlr_irq_disable(cam);
702 spin_unlock_irqrestore(&cam->dev_lock, flags);
708 static void cafe_ctlr_power_up(struct cafe_camera *cam)
712 spin_lock_irqsave(&cam->dev_lock, flags);
713 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
715 * Part one of the sensor dance: turn the global
718 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
719 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
721 * Put the sensor into operational mode (assumes OLPC-style
722 * wiring). Control 0 is reset - set to 1 to operate.
723 * Control 1 is power down, set to 0 to operate.
725 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
726 /* mdelay(1); */ /* Marvell says 1ms will do it */
727 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
728 /* mdelay(1); */ /* Enough? */
729 spin_unlock_irqrestore(&cam->dev_lock, flags);
730 msleep(5); /* Just to be sure */
733 static void cafe_ctlr_power_down(struct cafe_camera *cam)
737 spin_lock_irqsave(&cam->dev_lock, flags);
738 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
739 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
740 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
741 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
742 spin_unlock_irqrestore(&cam->dev_lock, flags);
745 /* -------------------------------------------------------------------- */
747 * Communications with the sensor.
750 static int __cafe_cam_reset(struct cafe_camera *cam)
752 return sensor_call(cam, core, reset, 0);
756 * We have found the sensor on the i2c. Let's try to have a
759 static int cafe_cam_init(struct cafe_camera *cam)
761 struct v4l2_dbg_chip_ident chip;
764 mutex_lock(&cam->s_mutex);
765 if (cam->state != S_NOTREADY)
766 cam_warn(cam, "Cam init with device in funky state %d",
768 ret = __cafe_cam_reset(cam);
771 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
772 chip.match.addr = cam->sensor_addr;
773 ret = sensor_call(cam, core, g_chip_ident, &chip);
776 cam->sensor_type = chip.ident;
777 if (cam->sensor_type != V4L2_IDENT_OV7670) {
778 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
782 /* Get/set parameters? */
786 cafe_ctlr_power_down(cam);
787 mutex_unlock(&cam->s_mutex);
792 * Configure the sensor to match the parameters we have. Caller should
795 static int cafe_cam_set_flip(struct cafe_camera *cam)
797 struct v4l2_control ctrl;
799 memset(&ctrl, 0, sizeof(ctrl));
800 ctrl.id = V4L2_CID_VFLIP;
802 return sensor_call(cam, core, s_ctrl, &ctrl);
806 static int cafe_cam_configure(struct cafe_camera *cam)
808 struct v4l2_format fmt;
811 if (cam->state != S_IDLE)
813 fmt.fmt.pix = cam->pix_format;
814 ret = sensor_call(cam, core, init, 0);
816 ret = sensor_call(cam, video, s_fmt, &fmt);
818 * OV7670 does weird things if flip is set *before* format...
820 ret += cafe_cam_set_flip(cam);
824 /* -------------------------------------------------------------------- */
826 * DMA buffer management. These functions need s_mutex held.
829 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
830 * does a get_free_pages() call, and we waste a good chunk of an orderN
831 * allocation. Should try to allocate the whole set in one chunk.
833 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
837 cafe_set_config_needed(cam, 1);
839 cam->dma_buf_size = dma_buf_size;
841 cam->dma_buf_size = cam->pix_format.sizeimage;
846 for (i = 0; i < n_dma_bufs; i++) {
847 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
848 cam->dma_buf_size, cam->dma_handles + i,
850 if (cam->dma_bufs[i] == NULL) {
851 cam_warn(cam, "Failed to allocate DMA buffer\n");
854 /* For debug, remove eventually */
855 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
859 switch (cam->nbufs) {
861 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
862 cam->dma_bufs[0], cam->dma_handles[0]);
865 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
870 cam_warn(cam, "Will limp along with only 2 buffers\n");
876 static void cafe_free_dma_bufs(struct cafe_camera *cam)
880 for (i = 0; i < cam->nbufs; i++) {
881 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
882 cam->dma_bufs[i], cam->dma_handles[i]);
883 cam->dma_bufs[i] = NULL;
892 /* ----------------------------------------------------------------------- */
894 * Here starts the V4L2 interface code.
898 * Read an image from the device.
900 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
901 char __user *buffer, size_t len, loff_t *pos)
906 spin_lock_irqsave(&cam->dev_lock, flags);
907 if (cam->next_buf < 0) {
908 cam_err(cam, "deliver_buffer: No next buffer\n");
909 spin_unlock_irqrestore(&cam->dev_lock, flags);
912 bufno = cam->next_buf;
913 clear_bit(bufno, &cam->flags);
914 if (++(cam->next_buf) >= cam->nbufs)
916 if (! test_bit(cam->next_buf, &cam->flags))
919 spin_unlock_irqrestore(&cam->dev_lock, flags);
921 if (len > cam->pix_format.sizeimage)
922 len = cam->pix_format.sizeimage;
923 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
930 * Get everything ready, and start grabbing frames.
932 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
938 * Configuration. If we still don't have DMA buffers,
939 * make one last, desperate attempt.
942 if (cafe_alloc_dma_bufs(cam, 0))
945 if (cafe_needs_config(cam)) {
946 cafe_cam_configure(cam);
947 ret = cafe_ctlr_configure(cam);
955 spin_lock_irqsave(&cam->dev_lock, flags);
956 cafe_reset_buffers(cam);
957 cafe_ctlr_irq_enable(cam);
959 cafe_ctlr_start(cam);
960 spin_unlock_irqrestore(&cam->dev_lock, flags);
965 static ssize_t cafe_v4l_read(struct file *filp,
966 char __user *buffer, size_t len, loff_t *pos)
968 struct cafe_camera *cam = filp->private_data;
972 * Perhaps we're in speculative read mode and already
975 mutex_lock(&cam->s_mutex);
976 if (cam->state == S_SPECREAD) {
977 if (cam->next_buf >= 0) {
978 ret = cafe_deliver_buffer(cam, buffer, len, pos);
982 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
985 } else if (cam->state != S_IDLE) {
991 * v4l2: multiple processes can open the device, but only
992 * one gets to grab data from it.
994 if (cam->owner && cam->owner != filp) {
1001 * Do setup if need be.
1003 if (cam->state != S_SPECREAD) {
1004 ret = cafe_read_setup(cam, S_SINGLEREAD);
1009 * Wait for something to happen. This should probably
1010 * be interruptible (FIXME).
1012 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1013 if (cam->next_buf < 0) {
1014 cam_err(cam, "read() operation timed out\n");
1015 cafe_ctlr_stop_dma(cam);
1020 * Give them their data and we should be done.
1022 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1025 mutex_unlock(&cam->s_mutex);
1037 * Streaming I/O support.
1042 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1043 enum v4l2_buf_type type)
1045 struct cafe_camera *cam = filp->private_data;
1048 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1050 mutex_lock(&cam->s_mutex);
1051 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1055 ret = cafe_read_setup(cam, S_STREAMING);
1058 mutex_unlock(&cam->s_mutex);
1064 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1065 enum v4l2_buf_type type)
1067 struct cafe_camera *cam = filp->private_data;
1070 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1072 mutex_lock(&cam->s_mutex);
1073 if (cam->state != S_STREAMING)
1076 cafe_ctlr_stop_dma(cam);
1080 mutex_unlock(&cam->s_mutex);
1087 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1089 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1091 INIT_LIST_HEAD(&buf->list);
1092 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1093 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1094 if (buf->buffer == NULL)
1099 buf->v4lbuf.index = index;
1100 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1101 buf->v4lbuf.field = V4L2_FIELD_NONE;
1102 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1104 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1105 * just uses the length times the index, but the spec warns
1106 * against doing just that - vma merging problems. So we
1107 * leave a gap between each pair of buffers.
1109 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1113 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1118 * If any buffers are mapped, we cannot free them at all.
1120 for (i = 0; i < cam->n_sbufs; i++)
1121 if (cam->sb_bufs[i].mapcount > 0)
1126 for (i = 0; i < cam->n_sbufs; i++)
1127 vfree(cam->sb_bufs[i].buffer);
1129 kfree(cam->sb_bufs);
1130 cam->sb_bufs = NULL;
1131 INIT_LIST_HEAD(&cam->sb_avail);
1132 INIT_LIST_HEAD(&cam->sb_full);
1138 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1139 struct v4l2_requestbuffers *req)
1141 struct cafe_camera *cam = filp->private_data;
1142 int ret = 0; /* Silence warning */
1145 * Make sure it's something we can do. User pointers could be
1146 * implemented without great pain, but that's not been done yet.
1148 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1150 if (req->memory != V4L2_MEMORY_MMAP)
1153 * If they ask for zero buffers, they really want us to stop streaming
1154 * (if it's happening) and free everything. Should we check owner?
1156 mutex_lock(&cam->s_mutex);
1157 if (req->count == 0) {
1158 if (cam->state == S_STREAMING)
1159 cafe_ctlr_stop_dma(cam);
1160 ret = cafe_free_sio_buffers (cam);
1164 * Device needs to be idle and working. We *could* try to do the
1165 * right thing in S_SPECREAD by shutting things down, but it
1166 * probably doesn't matter.
1168 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1174 if (req->count < min_buffers)
1175 req->count = min_buffers;
1176 else if (req->count > max_buffers)
1177 req->count = max_buffers;
1178 if (cam->n_sbufs > 0) {
1179 ret = cafe_free_sio_buffers(cam);
1184 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1186 if (cam->sb_bufs == NULL) {
1190 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1191 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1196 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1197 kfree(cam->sb_bufs);
1198 req->count = cam->n_sbufs; /* In case of partial success */
1201 mutex_unlock(&cam->s_mutex);
1206 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1207 struct v4l2_buffer *buf)
1209 struct cafe_camera *cam = filp->private_data;
1212 mutex_lock(&cam->s_mutex);
1213 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1215 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1217 *buf = cam->sb_bufs[buf->index].v4lbuf;
1220 mutex_unlock(&cam->s_mutex);
1224 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1225 struct v4l2_buffer *buf)
1227 struct cafe_camera *cam = filp->private_data;
1228 struct cafe_sio_buffer *sbuf;
1230 unsigned long flags;
1232 mutex_lock(&cam->s_mutex);
1233 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1235 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1237 sbuf = cam->sb_bufs + buf->index;
1238 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1239 ret = 0; /* Already queued?? */
1242 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1243 /* Spec doesn't say anything, seems appropriate tho */
1247 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1248 spin_lock_irqsave(&cam->dev_lock, flags);
1249 list_add(&sbuf->list, &cam->sb_avail);
1250 spin_unlock_irqrestore(&cam->dev_lock, flags);
1253 mutex_unlock(&cam->s_mutex);
1257 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1258 struct v4l2_buffer *buf)
1260 struct cafe_camera *cam = filp->private_data;
1261 struct cafe_sio_buffer *sbuf;
1263 unsigned long flags;
1265 mutex_lock(&cam->s_mutex);
1266 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1268 if (cam->state != S_STREAMING)
1270 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1275 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1276 mutex_unlock(&cam->s_mutex);
1277 if (wait_event_interruptible(cam->iowait,
1278 !list_empty(&cam->sb_full))) {
1282 mutex_lock(&cam->s_mutex);
1285 if (cam->state != S_STREAMING)
1288 spin_lock_irqsave(&cam->dev_lock, flags);
1289 /* Should probably recheck !list_empty() here */
1290 sbuf = list_entry(cam->sb_full.next,
1291 struct cafe_sio_buffer, list);
1292 list_del_init(&sbuf->list);
1293 spin_unlock_irqrestore(&cam->dev_lock, flags);
1294 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1295 *buf = sbuf->v4lbuf;
1300 mutex_unlock(&cam->s_mutex);
1307 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1309 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1311 * Locking: done under mmap_sem, so we don't need to
1312 * go back to the camera lock here.
1318 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1320 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1322 mutex_lock(&sbuf->cam->s_mutex);
1324 /* Docs say we should stop I/O too... */
1325 if (sbuf->mapcount == 0)
1326 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1327 mutex_unlock(&sbuf->cam->s_mutex);
1330 static struct vm_operations_struct cafe_v4l_vm_ops = {
1331 .open = cafe_v4l_vm_open,
1332 .close = cafe_v4l_vm_close
1336 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1338 struct cafe_camera *cam = filp->private_data;
1339 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1342 struct cafe_sio_buffer *sbuf = NULL;
1344 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1347 * Find the buffer they are looking for.
1349 mutex_lock(&cam->s_mutex);
1350 for (i = 0; i < cam->n_sbufs; i++)
1351 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1352 sbuf = cam->sb_bufs + i;
1358 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1361 vma->vm_flags |= VM_DONTEXPAND;
1362 vma->vm_private_data = sbuf;
1363 vma->vm_ops = &cafe_v4l_vm_ops;
1364 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1365 cafe_v4l_vm_open(vma);
1368 mutex_unlock(&cam->s_mutex);
1374 static int cafe_v4l_open(struct file *filp)
1376 struct cafe_camera *cam = video_drvdata(filp);
1378 filp->private_data = cam;
1380 mutex_lock(&cam->s_mutex);
1381 if (cam->users == 0) {
1382 cafe_ctlr_power_up(cam);
1383 __cafe_cam_reset(cam);
1384 cafe_set_config_needed(cam, 1);
1385 /* FIXME make sure this is complete */
1388 mutex_unlock(&cam->s_mutex);
1393 static int cafe_v4l_release(struct file *filp)
1395 struct cafe_camera *cam = filp->private_data;
1397 mutex_lock(&cam->s_mutex);
1399 if (filp == cam->owner) {
1400 cafe_ctlr_stop_dma(cam);
1401 cafe_free_sio_buffers(cam);
1404 if (cam->users == 0) {
1405 cafe_ctlr_power_down(cam);
1406 if (alloc_bufs_at_read)
1407 cafe_free_dma_bufs(cam);
1409 mutex_unlock(&cam->s_mutex);
1415 static unsigned int cafe_v4l_poll(struct file *filp,
1416 struct poll_table_struct *pt)
1418 struct cafe_camera *cam = filp->private_data;
1420 poll_wait(filp, &cam->iowait, pt);
1421 if (cam->next_buf >= 0)
1422 return POLLIN | POLLRDNORM;
1428 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1429 struct v4l2_queryctrl *qc)
1431 struct cafe_camera *cam = priv;
1434 mutex_lock(&cam->s_mutex);
1435 ret = sensor_call(cam, core, queryctrl, qc);
1436 mutex_unlock(&cam->s_mutex);
1441 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1442 struct v4l2_control *ctrl)
1444 struct cafe_camera *cam = priv;
1447 mutex_lock(&cam->s_mutex);
1448 ret = sensor_call(cam, core, g_ctrl, ctrl);
1449 mutex_unlock(&cam->s_mutex);
1454 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1455 struct v4l2_control *ctrl)
1457 struct cafe_camera *cam = priv;
1460 mutex_lock(&cam->s_mutex);
1461 ret = sensor_call(cam, core, s_ctrl, ctrl);
1462 mutex_unlock(&cam->s_mutex);
1470 static int cafe_vidioc_querycap(struct file *file, void *priv,
1471 struct v4l2_capability *cap)
1473 strcpy(cap->driver, "cafe_ccic");
1474 strcpy(cap->card, "cafe_ccic");
1475 cap->version = CAFE_VERSION;
1476 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1477 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1483 * The default format we use until somebody says otherwise.
1485 static struct v4l2_pix_format cafe_def_pix_format = {
1487 .height = VGA_HEIGHT,
1488 .pixelformat = V4L2_PIX_FMT_YUYV,
1489 .field = V4L2_FIELD_NONE,
1490 .bytesperline = VGA_WIDTH*2,
1491 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1494 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1495 void *priv, struct v4l2_fmtdesc *fmt)
1497 struct cafe_camera *cam = priv;
1500 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1502 mutex_lock(&cam->s_mutex);
1503 ret = sensor_call(cam, video, enum_fmt, fmt);
1504 mutex_unlock(&cam->s_mutex);
1509 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1510 struct v4l2_format *fmt)
1512 struct cafe_camera *cam = priv;
1515 mutex_lock(&cam->s_mutex);
1516 ret = sensor_call(cam, video, try_fmt, fmt);
1517 mutex_unlock(&cam->s_mutex);
1521 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1522 struct v4l2_format *fmt)
1524 struct cafe_camera *cam = priv;
1528 * Can't do anything if the device is not idle
1529 * Also can't if there are streaming buffers in place.
1531 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1534 * See if the formatting works in principle.
1536 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1540 * Now we start to change things for real, so let's do it
1543 mutex_lock(&cam->s_mutex);
1544 cam->pix_format = fmt->fmt.pix;
1546 * Make sure we have appropriate DMA buffers.
1549 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1550 cafe_free_dma_bufs(cam);
1551 if (cam->nbufs == 0) {
1552 if (cafe_alloc_dma_bufs(cam, 0))
1556 * It looks like this might work, so let's program the sensor.
1558 ret = cafe_cam_configure(cam);
1560 ret = cafe_ctlr_configure(cam);
1562 mutex_unlock(&cam->s_mutex);
1567 * Return our stored notion of how the camera is/should be configured.
1568 * The V4l2 spec wants us to be smarter, and actually get this from
1569 * the camera (and not mess with it at open time). Someday.
1571 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1572 struct v4l2_format *f)
1574 struct cafe_camera *cam = priv;
1576 f->fmt.pix = cam->pix_format;
1581 * We only have one input - the sensor - so minimize the nonsense here.
1583 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1584 struct v4l2_input *input)
1586 if (input->index != 0)
1589 input->type = V4L2_INPUT_TYPE_CAMERA;
1590 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1591 strcpy(input->name, "Camera");
1595 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1601 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1609 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1615 * G/S_PARM. Most of this is done by the sensor, but we are
1616 * the level which controls the number of read buffers.
1618 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1619 struct v4l2_streamparm *parms)
1621 struct cafe_camera *cam = priv;
1624 mutex_lock(&cam->s_mutex);
1625 ret = sensor_call(cam, video, g_parm, parms);
1626 mutex_unlock(&cam->s_mutex);
1627 parms->parm.capture.readbuffers = n_dma_bufs;
1631 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1632 struct v4l2_streamparm *parms)
1634 struct cafe_camera *cam = priv;
1637 mutex_lock(&cam->s_mutex);
1638 ret = sensor_call(cam, video, s_parm, parms);
1639 mutex_unlock(&cam->s_mutex);
1640 parms->parm.capture.readbuffers = n_dma_bufs;
1644 static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1645 struct v4l2_dbg_chip_ident *chip)
1647 struct cafe_camera *cam = priv;
1649 chip->ident = V4L2_IDENT_NONE;
1651 if (v4l2_chip_match_host(&chip->match)) {
1652 chip->ident = V4L2_IDENT_CAFE;
1655 return sensor_call(cam, core, g_chip_ident, chip);
1658 #ifdef CONFIG_VIDEO_ADV_DEBUG
1659 static int cafe_vidioc_g_register(struct file *file, void *priv,
1660 struct v4l2_dbg_register *reg)
1662 struct cafe_camera *cam = priv;
1664 if (v4l2_chip_match_host(®->match)) {
1665 reg->val = cafe_reg_read(cam, reg->reg);
1669 return sensor_call(cam, core, g_register, reg);
1672 static int cafe_vidioc_s_register(struct file *file, void *priv,
1673 struct v4l2_dbg_register *reg)
1675 struct cafe_camera *cam = priv;
1677 if (v4l2_chip_match_host(®->match)) {
1678 cafe_reg_write(cam, reg->reg, reg->val);
1681 return sensor_call(cam, core, s_register, reg);
1686 * This template device holds all of those v4l2 methods; we
1687 * clone it for specific real devices.
1690 static const struct v4l2_file_operations cafe_v4l_fops = {
1691 .owner = THIS_MODULE,
1692 .open = cafe_v4l_open,
1693 .release = cafe_v4l_release,
1694 .read = cafe_v4l_read,
1695 .poll = cafe_v4l_poll,
1696 .mmap = cafe_v4l_mmap,
1697 .ioctl = video_ioctl2,
1700 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1701 .vidioc_querycap = cafe_vidioc_querycap,
1702 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1703 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1704 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1705 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1706 .vidioc_enum_input = cafe_vidioc_enum_input,
1707 .vidioc_g_input = cafe_vidioc_g_input,
1708 .vidioc_s_input = cafe_vidioc_s_input,
1709 .vidioc_s_std = cafe_vidioc_s_std,
1710 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1711 .vidioc_querybuf = cafe_vidioc_querybuf,
1712 .vidioc_qbuf = cafe_vidioc_qbuf,
1713 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1714 .vidioc_streamon = cafe_vidioc_streamon,
1715 .vidioc_streamoff = cafe_vidioc_streamoff,
1716 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1717 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1718 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1719 .vidioc_g_parm = cafe_vidioc_g_parm,
1720 .vidioc_s_parm = cafe_vidioc_s_parm,
1721 .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
1722 #ifdef CONFIG_VIDEO_ADV_DEBUG
1723 .vidioc_g_register = cafe_vidioc_g_register,
1724 .vidioc_s_register = cafe_vidioc_s_register,
1728 static struct video_device cafe_v4l_template = {
1730 .minor = -1, /* Get one dynamically */
1731 .tvnorms = V4L2_STD_NTSC_M,
1732 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1734 .fops = &cafe_v4l_fops,
1735 .ioctl_ops = &cafe_v4l_ioctl_ops,
1736 .release = video_device_release_empty,
1740 /* ---------------------------------------------------------------------- */
1742 * Interrupt handler stuff
1747 static void cafe_frame_tasklet(unsigned long data)
1749 struct cafe_camera *cam = (struct cafe_camera *) data;
1751 unsigned long flags;
1752 struct cafe_sio_buffer *sbuf;
1754 spin_lock_irqsave(&cam->dev_lock, flags);
1755 for (i = 0; i < cam->nbufs; i++) {
1756 int bufno = cam->next_buf;
1757 if (bufno < 0) { /* "will never happen" */
1758 cam_err(cam, "No valid bufs in tasklet!\n");
1761 if (++(cam->next_buf) >= cam->nbufs)
1763 if (! test_bit(bufno, &cam->flags))
1765 if (list_empty(&cam->sb_avail))
1766 break; /* Leave it valid, hope for better later */
1767 clear_bit(bufno, &cam->flags);
1768 sbuf = list_entry(cam->sb_avail.next,
1769 struct cafe_sio_buffer, list);
1771 * Drop the lock during the big copy. This *should* be safe...
1773 spin_unlock_irqrestore(&cam->dev_lock, flags);
1774 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1775 cam->pix_format.sizeimage);
1776 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1777 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1778 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1779 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1780 spin_lock_irqsave(&cam->dev_lock, flags);
1781 list_move_tail(&sbuf->list, &cam->sb_full);
1783 if (! list_empty(&cam->sb_full))
1784 wake_up(&cam->iowait);
1785 spin_unlock_irqrestore(&cam->dev_lock, flags);
1790 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1793 * Basic frame housekeeping.
1795 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1796 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1797 set_bit(frame, &cam->flags);
1798 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1799 if (cam->next_buf < 0)
1800 cam->next_buf = frame;
1801 cam->buf_seq[frame] = ++(cam->sequence);
1803 switch (cam->state) {
1805 * If in single read mode, try going speculative.
1808 cam->state = S_SPECREAD;
1809 cam->specframes = 0;
1810 wake_up(&cam->iowait);
1814 * If we are already doing speculative reads, and nobody is
1815 * reading them, just stop.
1818 if (++(cam->specframes) >= cam->nbufs) {
1819 cafe_ctlr_stop(cam);
1820 cafe_ctlr_irq_disable(cam);
1821 cam->state = S_IDLE;
1823 wake_up(&cam->iowait);
1826 * For the streaming case, we defer the real work to the
1829 * FIXME: if the application is not consuming the buffers,
1830 * we should eventually put things on hold and restart in
1834 tasklet_schedule(&cam->s_tasklet);
1838 cam_err(cam, "Frame interrupt in non-operational state\n");
1846 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1850 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1852 * Handle any frame completions. There really should
1853 * not be more than one of these, or we have fallen
1856 for (frame = 0; frame < cam->nbufs; frame++)
1857 if (irqs & (IRQ_EOF0 << frame))
1858 cafe_frame_complete(cam, frame);
1860 * If a frame starts, note that we have DMA active. This
1861 * code assumes that we won't get multiple frame interrupts
1862 * at once; may want to rethink that.
1864 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1865 set_bit(CF_DMA_ACTIVE, &cam->flags);
1870 static irqreturn_t cafe_irq(int irq, void *data)
1872 struct cafe_camera *cam = data;
1875 spin_lock(&cam->dev_lock);
1876 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1877 if ((irqs & ALLIRQS) == 0) {
1878 spin_unlock(&cam->dev_lock);
1881 if (irqs & FRAMEIRQS)
1882 cafe_frame_irq(cam, irqs);
1883 if (irqs & TWSIIRQS) {
1884 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1885 wake_up(&cam->smbus_wait);
1887 spin_unlock(&cam->dev_lock);
1892 /* -------------------------------------------------------------------------- */
1894 * PCI interface stuff.
1897 static int cafe_pci_probe(struct pci_dev *pdev,
1898 const struct pci_device_id *id)
1901 struct cafe_camera *cam;
1904 * Start putting together one of our big camera structures.
1907 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1910 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1914 mutex_init(&cam->s_mutex);
1915 mutex_lock(&cam->s_mutex);
1916 spin_lock_init(&cam->dev_lock);
1917 cam->state = S_NOTREADY;
1918 cafe_set_config_needed(cam, 1);
1919 init_waitqueue_head(&cam->smbus_wait);
1920 init_waitqueue_head(&cam->iowait);
1922 cam->pix_format = cafe_def_pix_format;
1923 INIT_LIST_HEAD(&cam->dev_list);
1924 INIT_LIST_HEAD(&cam->sb_avail);
1925 INIT_LIST_HEAD(&cam->sb_full);
1926 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
1928 * Get set up on the PCI bus.
1930 ret = pci_enable_device(pdev);
1933 pci_set_master(pdev);
1936 cam->regs = pci_iomap(pdev, 0, 0);
1938 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
1941 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
1945 * Initialize the controller and leave it powered up. It will
1946 * stay that way until the sensor driver shows up.
1948 cafe_ctlr_init(cam);
1949 cafe_ctlr_power_up(cam);
1951 * Set up I2C/SMBUS communications. We have to drop the mutex here
1952 * because the sensor could attach in this call chain, leading to
1953 * unsightly deadlocks.
1955 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
1956 ret = cafe_smbus_setup(cam);
1960 cam->sensor_addr = 0x42;
1961 cam->sensor = v4l2_i2c_new_subdev(&cam->i2c_adapter,
1962 "ov7670", "ov7670", cam->sensor_addr);
1963 if (cam->sensor == NULL) {
1967 ret = cafe_cam_init(cam);
1972 * Get the v4l2 setup done.
1974 mutex_lock(&cam->s_mutex);
1975 cam->vdev = cafe_v4l_template;
1976 cam->vdev.debug = 0;
1977 /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
1978 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1979 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1982 video_set_drvdata(&cam->vdev, cam);
1985 * If so requested, try to get our DMA buffers now.
1987 if (!alloc_bufs_at_read) {
1988 if (cafe_alloc_dma_bufs(cam, 1))
1989 cam_warn(cam, "Unable to alloc DMA buffers at load"
1990 " will try again later.");
1993 mutex_unlock(&cam->s_mutex);
1997 cafe_smbus_shutdown(cam);
1999 cafe_ctlr_power_down(cam);
2000 free_irq(pdev->irq, cam);
2002 pci_iounmap(pdev, cam->regs);
2004 v4l2_device_unregister(&cam->v4l2_dev);
2013 * Shut down an initialized device
2015 static void cafe_shutdown(struct cafe_camera *cam)
2017 /* FIXME: Make sure we take care of everything here */
2018 if (cam->n_sbufs > 0)
2019 /* What if they are still mapped? Shouldn't be, but... */
2020 cafe_free_sio_buffers(cam);
2021 cafe_ctlr_stop_dma(cam);
2022 cafe_ctlr_power_down(cam);
2023 cafe_smbus_shutdown(cam);
2024 cafe_free_dma_bufs(cam);
2025 free_irq(cam->pdev->irq, cam);
2026 pci_iounmap(cam->pdev, cam->regs);
2027 video_unregister_device(&cam->vdev);
2031 static void cafe_pci_remove(struct pci_dev *pdev)
2033 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2034 struct cafe_camera *cam = to_cam(v4l2_dev);
2037 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2040 mutex_lock(&cam->s_mutex);
2042 cam_warn(cam, "Removing a device with users!\n");
2044 v4l2_device_unregister(&cam->v4l2_dev);
2046 /* No unlock - it no longer exists */
2052 * Basic power management.
2054 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2056 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2057 struct cafe_camera *cam = to_cam(v4l2_dev);
2059 enum cafe_state cstate;
2061 ret = pci_save_state(pdev);
2064 cstate = cam->state; /* HACK - stop_dma sets to idle */
2065 cafe_ctlr_stop_dma(cam);
2066 cafe_ctlr_power_down(cam);
2067 pci_disable_device(pdev);
2068 cam->state = cstate;
2073 static int cafe_pci_resume(struct pci_dev *pdev)
2075 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2076 struct cafe_camera *cam = to_cam(v4l2_dev);
2079 ret = pci_restore_state(pdev);
2082 ret = pci_enable_device(pdev);
2085 cam_warn(cam, "Unable to re-enable device on resume!\n");
2088 cafe_ctlr_init(cam);
2089 cafe_ctlr_power_down(cam);
2091 mutex_lock(&cam->s_mutex);
2092 if (cam->users > 0) {
2093 cafe_ctlr_power_up(cam);
2094 __cafe_cam_reset(cam);
2096 mutex_unlock(&cam->s_mutex);
2098 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2099 if (cam->state == S_SPECREAD)
2100 cam->state = S_IDLE; /* Don't bother restarting */
2101 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2102 ret = cafe_read_setup(cam, cam->state);
2106 #endif /* CONFIG_PM */
2109 static struct pci_device_id cafe_ids[] = {
2110 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2111 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2115 MODULE_DEVICE_TABLE(pci, cafe_ids);
2117 static struct pci_driver cafe_pci_driver = {
2118 .name = "cafe1000-ccic",
2119 .id_table = cafe_ids,
2120 .probe = cafe_pci_probe,
2121 .remove = cafe_pci_remove,
2123 .suspend = cafe_pci_suspend,
2124 .resume = cafe_pci_resume,
2131 static int __init cafe_init(void)
2135 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2137 ret = pci_register_driver(&cafe_pci_driver);
2139 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2149 static void __exit cafe_exit(void)
2151 pci_unregister_driver(&cafe_pci_driver);
2154 module_init(cafe_init);
2155 module_exit(cafe_exit);