2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
6 * Copyright 2006 One Laptop Per Child Association, Inc.
7 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
9 * Written by Jonathan Corbet, corbet@lwn.net.
11 * This file may be distributed under the terms of the GNU General
12 * Public License, version 2.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
20 #include <linux/pci.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-chip-ident.h>
27 #include <linux/device.h>
28 #include <linux/wait.h>
29 #include <linux/list.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/delay.h>
32 #include <linux/debugfs.h>
33 #include <linux/jiffies.h>
34 #include <linux/vmalloc.h>
36 #include <asm/uaccess.h>
39 #include "cafe_ccic-regs.h"
41 #define CAFE_VERSION 0x000002
47 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
48 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
49 MODULE_LICENSE("GPL");
50 MODULE_SUPPORTED_DEVICE("Video");
53 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
54 * we must have physically contiguous buffers to bring frames into.
55 * These parameters control how many buffers we use, whether we
56 * allocate them at load time (better chance of success, but nails down
57 * memory) or when somebody tries to use the camera (riskier), and,
58 * for load-time allocation, how big they should be.
60 * The controller can cycle through three buffers. We could use
61 * more by flipping pointers around, but it probably makes little
65 #define MAX_DMA_BUFS 3
66 static int alloc_bufs_at_load = 0;
67 module_param(alloc_bufs_at_load, bool, 0444);
68 MODULE_PARM_DESC(alloc_bufs_at_load,
69 "Non-zero value causes DMA buffers to be allocated at module "
70 "load time. This increases the chances of successfully getting "
71 "those buffers, but at the cost of nailing down the memory from "
74 static int n_dma_bufs = 3;
75 module_param(n_dma_bufs, uint, 0644);
76 MODULE_PARM_DESC(n_dma_bufs,
77 "The number of DMA buffers to allocate. Can be either two "
78 "(saves memory, makes timing tighter) or three.");
80 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
81 module_param(dma_buf_size, uint, 0444);
82 MODULE_PARM_DESC(dma_buf_size,
83 "The size of the allocated DMA buffers. If actual operating "
84 "parameters require larger buffers, an attempt to reallocate "
87 static int min_buffers = 1;
88 module_param(min_buffers, uint, 0644);
89 MODULE_PARM_DESC(min_buffers,
90 "The minimum number of streaming I/O buffers we are willing "
93 static int max_buffers = 10;
94 module_param(max_buffers, uint, 0644);
95 MODULE_PARM_DESC(max_buffers,
96 "The maximum number of streaming I/O buffers an application "
97 "will be allowed to allocate. These buffers are big and live "
101 module_param(flip, bool, 0444);
102 MODULE_PARM_DESC(flip,
103 "If set, the sensor will be instructed to flip the image "
108 S_NOTREADY, /* Not yet initialized */
109 S_IDLE, /* Just hanging around */
110 S_FLAKED, /* Some sort of problem */
111 S_SINGLEREAD, /* In read() */
112 S_SPECREAD, /* Speculative read (for future read()) */
113 S_STREAMING /* Streaming data */
117 * Tracking of streaming I/O buffers.
119 struct cafe_sio_buffer {
120 struct list_head list;
121 struct v4l2_buffer v4lbuf;
122 char *buffer; /* Where it lives in kernel space */
124 struct cafe_camera *cam;
128 * A description of one of our devices.
129 * Locking: controlled by s_mutex. Certain fields, however, require
130 * the dev_lock spinlock; they are marked as such by comments.
131 * dev_lock is also required for access to device registers.
135 enum cafe_state state;
136 unsigned long flags; /* Buffer status, mainly (dev_lock) */
137 int users; /* How many open FDs */
138 struct file *owner; /* Who has data access (v4l2) */
141 * Subsystem structures.
143 struct pci_dev *pdev;
144 struct video_device v4ldev;
145 struct i2c_adapter i2c_adapter;
146 struct i2c_client *sensor;
148 unsigned char __iomem *regs;
149 struct list_head dev_list; /* link to other devices */
152 unsigned int nbufs; /* How many are alloc'd */
153 int next_buf; /* Next to consume (dev_lock) */
154 unsigned int dma_buf_size; /* allocated size */
155 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
156 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
157 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
158 unsigned int sequence; /* Frame sequence number */
159 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
161 /* Streaming buffers */
162 unsigned int n_sbufs; /* How many we have */
163 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
164 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
165 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
166 struct tasklet_struct s_tasklet;
168 /* Current operating parameters */
169 u32 sensor_type; /* Currently ov7670 only */
170 struct v4l2_pix_format pix_format;
173 struct mutex s_mutex; /* Access to this structure */
174 spinlock_t dev_lock; /* Access to device */
177 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
178 wait_queue_head_t iowait; /* Waiting on frame data */
179 #ifdef CONFIG_VIDEO_ADV_DEBUG
180 struct dentry *dfs_regs;
181 struct dentry *dfs_cam_regs;
186 * Status flags. Always manipulated with bit operations.
188 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
189 #define CF_BUF1_VALID 1
190 #define CF_BUF2_VALID 2
191 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
192 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
197 * Start over with DMA buffers - dev_lock needed.
199 static void cafe_reset_buffers(struct cafe_camera *cam)
204 for (i = 0; i < cam->nbufs; i++)
205 clear_bit(i, &cam->flags);
209 static inline int cafe_needs_config(struct cafe_camera *cam)
211 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
214 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
217 set_bit(CF_CONFIG_NEEDED, &cam->flags);
219 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
226 * Debugging and related.
228 #define cam_err(cam, fmt, arg...) \
229 dev_err(&(cam)->pdev->dev, fmt, ##arg);
230 #define cam_warn(cam, fmt, arg...) \
231 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
232 #define cam_dbg(cam, fmt, arg...) \
233 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
236 /* ---------------------------------------------------------------------*/
238 * We keep a simple list of known devices to search at open time.
240 static LIST_HEAD(cafe_dev_list);
241 static DEFINE_MUTEX(cafe_dev_list_lock);
243 static void cafe_add_dev(struct cafe_camera *cam)
245 mutex_lock(&cafe_dev_list_lock);
246 list_add_tail(&cam->dev_list, &cafe_dev_list);
247 mutex_unlock(&cafe_dev_list_lock);
250 static void cafe_remove_dev(struct cafe_camera *cam)
252 mutex_lock(&cafe_dev_list_lock);
253 list_del(&cam->dev_list);
254 mutex_unlock(&cafe_dev_list_lock);
257 static struct cafe_camera *cafe_find_dev(int minor)
259 struct cafe_camera *cam;
261 mutex_lock(&cafe_dev_list_lock);
262 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
263 if (cam->v4ldev.minor == minor)
268 mutex_unlock(&cafe_dev_list_lock);
273 static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
275 struct cafe_camera *cam;
277 mutex_lock(&cafe_dev_list_lock);
278 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
279 if (cam->pdev == pdev)
284 mutex_unlock(&cafe_dev_list_lock);
289 /* ------------------------------------------------------------------------ */
291 * Device register I/O
293 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
296 iowrite32(val, cam->regs + reg);
299 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
302 return ioread32(cam->regs + reg);
306 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
307 unsigned int val, unsigned int mask)
309 unsigned int v = cafe_reg_read(cam, reg);
311 v = (v & ~mask) | (val & mask);
312 cafe_reg_write(cam, reg, v);
315 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
316 unsigned int reg, unsigned int val)
318 cafe_reg_write_mask(cam, reg, 0, val);
321 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
322 unsigned int reg, unsigned int val)
324 cafe_reg_write_mask(cam, reg, val, val);
329 /* -------------------------------------------------------------------- */
331 * The I2C/SMBUS interface to the camera itself starts here. The
332 * controller handles SMBUS itself, presenting a relatively simple register
333 * interface; all we have to do is to tell it where to route the data.
335 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
337 static int cafe_smbus_write_done(struct cafe_camera *cam)
343 * We must delay after the interrupt, or the controller gets confused
344 * and never does give us good status. Fortunately, we don't do this
348 spin_lock_irqsave(&cam->dev_lock, flags);
349 c1 = cafe_reg_read(cam, REG_TWSIC1);
350 spin_unlock_irqrestore(&cam->dev_lock, flags);
351 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
354 static int cafe_smbus_write_data(struct cafe_camera *cam,
355 u16 addr, u8 command, u8 value)
360 spin_lock_irqsave(&cam->dev_lock, flags);
361 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
362 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
364 * Marvell sez set clkdiv to all 1's for now.
366 rval |= TWSIC0_CLKDIV;
367 cafe_reg_write(cam, REG_TWSIC0, rval);
368 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
369 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
370 cafe_reg_write(cam, REG_TWSIC1, rval);
371 spin_unlock_irqrestore(&cam->dev_lock, flags);
372 msleep(2); /* Required or things flake */
374 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
376 spin_lock_irqsave(&cam->dev_lock, flags);
377 rval = cafe_reg_read(cam, REG_TWSIC1);
378 spin_unlock_irqrestore(&cam->dev_lock, flags);
380 if (rval & TWSIC1_WSTAT) {
381 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
385 if (rval & TWSIC1_ERROR) {
386 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
395 static int cafe_smbus_read_done(struct cafe_camera *cam)
401 * We must delay after the interrupt, or the controller gets confused
402 * and never does give us good status. Fortunately, we don't do this
406 spin_lock_irqsave(&cam->dev_lock, flags);
407 c1 = cafe_reg_read(cam, REG_TWSIC1);
408 spin_unlock_irqrestore(&cam->dev_lock, flags);
409 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
414 static int cafe_smbus_read_data(struct cafe_camera *cam,
415 u16 addr, u8 command, u8 *value)
420 spin_lock_irqsave(&cam->dev_lock, flags);
421 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
422 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
424 * Marvel sez set clkdiv to all 1's for now.
426 rval |= TWSIC0_CLKDIV;
427 cafe_reg_write(cam, REG_TWSIC0, rval);
428 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
429 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
430 cafe_reg_write(cam, REG_TWSIC1, rval);
431 spin_unlock_irqrestore(&cam->dev_lock, flags);
433 wait_event_timeout(cam->smbus_wait,
434 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
435 spin_lock_irqsave(&cam->dev_lock, flags);
436 rval = cafe_reg_read(cam, REG_TWSIC1);
437 spin_unlock_irqrestore(&cam->dev_lock, flags);
439 if (rval & TWSIC1_ERROR) {
440 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
443 if (! (rval & TWSIC1_RVALID)) {
444 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
448 *value = rval & 0xff;
453 * Perform a transfer over SMBUS. This thing is called under
454 * the i2c bus lock, so we shouldn't race with ourselves...
456 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
457 unsigned short flags, char rw, u8 command,
458 int size, union i2c_smbus_data *data)
460 struct cafe_camera *cam = i2c_get_adapdata(adapter);
464 * Refuse to talk to anything but OV cam chips. We should
465 * never even see an attempt to do so, but one never knows.
467 if (cam->sensor && addr != cam->sensor->addr) {
468 cam_err(cam, "funky smbus addr %d\n", addr);
472 * This interface would appear to only do byte data ops. OK
473 * it can do word too, but the cam chip has no use for that.
475 if (size != I2C_SMBUS_BYTE_DATA) {
476 cam_err(cam, "funky xfer size %d\n", size);
480 if (rw == I2C_SMBUS_WRITE)
481 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
482 else if (rw == I2C_SMBUS_READ)
483 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
488 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
492 spin_lock_irqsave(&cam->dev_lock, flags);
493 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
494 spin_unlock_irqrestore(&cam->dev_lock, flags);
497 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
499 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
500 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
503 static struct i2c_algorithm cafe_smbus_algo = {
504 .smbus_xfer = cafe_smbus_xfer,
505 .functionality = cafe_smbus_func
508 /* Somebody is on the bus */
509 static int cafe_cam_init(struct cafe_camera *cam);
510 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
511 static void cafe_ctlr_power_down(struct cafe_camera *cam);
513 static int cafe_smbus_attach(struct i2c_client *client)
515 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
518 * Don't talk to chips we don't recognize.
520 if (client->driver->id == I2C_DRIVERID_OV7670) {
521 cam->sensor = client;
522 return cafe_cam_init(cam);
527 static int cafe_smbus_detach(struct i2c_client *client)
529 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
531 if (cam->sensor == client) {
532 cafe_ctlr_stop_dma(cam);
533 cafe_ctlr_power_down(cam);
534 cam_err(cam, "lost the sensor!\n");
535 cam->sensor = NULL; /* Bummer, no camera */
536 cam->state = S_NOTREADY;
541 static int cafe_smbus_setup(struct cafe_camera *cam)
543 struct i2c_adapter *adap = &cam->i2c_adapter;
546 cafe_smbus_enable_irq(cam);
547 adap->id = I2C_HW_SMBUS_CAFE;
548 adap->class = I2C_CLASS_CAM_DIGITAL;
549 adap->owner = THIS_MODULE;
550 adap->client_register = cafe_smbus_attach;
551 adap->client_unregister = cafe_smbus_detach;
552 adap->algo = &cafe_smbus_algo;
553 strcpy(adap->name, "cafe_ccic");
554 adap->dev.parent = &cam->pdev->dev;
555 i2c_set_adapdata(adap, cam);
556 ret = i2c_add_adapter(adap);
558 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
562 static void cafe_smbus_shutdown(struct cafe_camera *cam)
564 i2c_del_adapter(&cam->i2c_adapter);
568 /* ------------------------------------------------------------------- */
570 * Deal with the controller.
574 * Do everything we think we need to have the interface operating
575 * according to the desired format.
577 static void cafe_ctlr_dma(struct cafe_camera *cam)
580 * Store the first two Y buffers (we aren't supporting
581 * planar formats for now, so no UV bufs). Then either
582 * set the third if it exists, or tell the controller
585 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
586 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
587 if (cam->nbufs > 2) {
588 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
589 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
592 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
593 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
596 static void cafe_ctlr_image(struct cafe_camera *cam)
599 struct v4l2_pix_format *fmt = &cam->pix_format;
601 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
602 (fmt->bytesperline & IMGSZ_H_MASK);
603 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
604 cafe_reg_write(cam, REG_IMGOFFSET, 0);
605 /* YPITCH just drops the last two bits */
606 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
609 * Tell the controller about the image format we are using.
611 switch (cam->pix_format.pixelformat) {
612 case V4L2_PIX_FMT_YUYV:
613 cafe_reg_write_mask(cam, REG_CTRL0,
614 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
618 case V4L2_PIX_FMT_RGB444:
619 cafe_reg_write_mask(cam, REG_CTRL0,
620 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
625 case V4L2_PIX_FMT_RGB565:
626 cafe_reg_write_mask(cam, REG_CTRL0,
627 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
632 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
636 * Make sure it knows we want to use hsync/vsync.
638 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
644 * Configure the controller for operation; caller holds the
647 static int cafe_ctlr_configure(struct cafe_camera *cam)
651 spin_lock_irqsave(&cam->dev_lock, flags);
653 cafe_ctlr_image(cam);
654 cafe_set_config_needed(cam, 0);
655 spin_unlock_irqrestore(&cam->dev_lock, flags);
659 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
662 * Clear any pending interrupts, since we do not
663 * expect to have I/O active prior to enabling.
665 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
666 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
669 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
671 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
675 * Make the controller start grabbing images. Everything must
676 * be set up before doing this.
678 static void cafe_ctlr_start(struct cafe_camera *cam)
680 /* set_bit performs a read, so no other barrier should be
682 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
685 static void cafe_ctlr_stop(struct cafe_camera *cam)
687 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
690 static void cafe_ctlr_init(struct cafe_camera *cam)
694 spin_lock_irqsave(&cam->dev_lock, flags);
696 * Added magic to bring up the hardware on the B-Test board
698 cafe_reg_write(cam, 0x3038, 0x8);
699 cafe_reg_write(cam, 0x315c, 0x80008);
701 * Go through the dance needed to wake the device up.
702 * Note that these registers are global and shared
703 * with the NAND and SD devices. Interaction between the
704 * three still needs to be examined.
706 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
707 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
708 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
710 * Here we must wait a bit for the controller to come around.
712 spin_unlock_irqrestore(&cam->dev_lock, flags);
713 mdelay(5); /* FIXME revisit this */
714 spin_lock_irqsave(&cam->dev_lock, flags);
716 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
717 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
719 * Make sure it's not powered down.
721 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
723 * Turn off the enable bit. It sure should be off anyway,
724 * but it's good to be sure.
726 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
728 * Mask all interrupts.
730 cafe_reg_write(cam, REG_IRQMASK, 0);
732 * Clock the sensor appropriately. Controller clock should
733 * be 48MHz, sensor "typical" value is half that.
735 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
736 spin_unlock_irqrestore(&cam->dev_lock, flags);
741 * Stop the controller, and don't return until we're really sure that no
742 * further DMA is going on.
744 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
749 * Theory: stop the camera controller (whether it is operating
750 * or not). Delay briefly just in case we race with the SOF
751 * interrupt, then wait until no DMA is active.
753 spin_lock_irqsave(&cam->dev_lock, flags);
755 spin_unlock_irqrestore(&cam->dev_lock, flags);
757 wait_event_timeout(cam->iowait,
758 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
759 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
760 cam_err(cam, "Timeout waiting for DMA to end\n");
761 /* This would be bad news - what now? */
762 spin_lock_irqsave(&cam->dev_lock, flags);
764 cafe_ctlr_irq_disable(cam);
765 spin_unlock_irqrestore(&cam->dev_lock, flags);
771 static void cafe_ctlr_power_up(struct cafe_camera *cam)
775 spin_lock_irqsave(&cam->dev_lock, flags);
776 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
778 * Part one of the sensor dance: turn the global
781 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
782 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
784 * Put the sensor into operational mode (assumes OLPC-style
785 * wiring). Control 0 is reset - set to 1 to operate.
786 * Control 1 is power down, set to 0 to operate.
788 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
789 // mdelay(1); /* Marvell says 1ms will do it */
790 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
791 // mdelay(1); /* Enough? */
792 spin_unlock_irqrestore(&cam->dev_lock, flags);
793 msleep(5); /* Just to be sure */
796 static void cafe_ctlr_power_down(struct cafe_camera *cam)
800 spin_lock_irqsave(&cam->dev_lock, flags);
801 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
802 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
803 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
804 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
805 spin_unlock_irqrestore(&cam->dev_lock, flags);
808 /* -------------------------------------------------------------------- */
810 * Communications with the sensor.
813 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
815 struct i2c_client *sc = cam->sensor;
818 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
820 ret = sc->driver->command(sc, cmd, arg);
821 if (ret == -EPERM) /* Unsupported command */
826 static int __cafe_cam_reset(struct cafe_camera *cam)
829 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
833 * We have found the sensor on the i2c. Let's try to have a
836 static int cafe_cam_init(struct cafe_camera *cam)
838 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
841 mutex_lock(&cam->s_mutex);
842 if (cam->state != S_NOTREADY)
843 cam_warn(cam, "Cam init with device in funky state %d",
845 ret = __cafe_cam_reset(cam);
848 chip.match_chip = cam->sensor->addr;
849 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
852 cam->sensor_type = chip.ident;
853 // if (cam->sensor->addr != OV7xx0_SID) {
854 if (cam->sensor_type != V4L2_IDENT_OV7670) {
855 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
859 /* Get/set parameters? */
863 cafe_ctlr_power_down(cam);
864 mutex_unlock(&cam->s_mutex);
869 * Configure the sensor to match the parameters we have. Caller should
872 static int cafe_cam_set_flip(struct cafe_camera *cam)
874 struct v4l2_control ctrl;
876 memset(&ctrl, 0, sizeof(ctrl));
877 ctrl.id = V4L2_CID_VFLIP;
879 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
883 static int cafe_cam_configure(struct cafe_camera *cam)
885 struct v4l2_format fmt;
888 if (cam->state != S_IDLE)
890 fmt.fmt.pix = cam->pix_format;
891 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
893 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
895 * OV7670 does weird things if flip is set *before* format...
897 ret += cafe_cam_set_flip(cam);
901 /* -------------------------------------------------------------------- */
903 * DMA buffer management. These functions need s_mutex held.
906 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
907 * does a get_free_pages() call, and we waste a good chunk of an orderN
908 * allocation. Should try to allocate the whole set in one chunk.
910 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
914 cafe_set_config_needed(cam, 1);
916 cam->dma_buf_size = dma_buf_size;
918 cam->dma_buf_size = cam->pix_format.sizeimage;
923 for (i = 0; i < n_dma_bufs; i++) {
924 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
925 cam->dma_buf_size, cam->dma_handles + i,
927 if (cam->dma_bufs[i] == NULL) {
928 cam_warn(cam, "Failed to allocate DMA buffer\n");
931 /* For debug, remove eventually */
932 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
936 switch (cam->nbufs) {
938 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
939 cam->dma_bufs[0], cam->dma_handles[0]);
942 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
947 cam_warn(cam, "Will limp along with only 2 buffers\n");
953 static void cafe_free_dma_bufs(struct cafe_camera *cam)
957 for (i = 0; i < cam->nbufs; i++) {
958 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
959 cam->dma_bufs[i], cam->dma_handles[i]);
960 cam->dma_bufs[i] = NULL;
969 /* ----------------------------------------------------------------------- */
971 * Here starts the V4L2 interface code.
975 * Read an image from the device.
977 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
978 char __user *buffer, size_t len, loff_t *pos)
983 spin_lock_irqsave(&cam->dev_lock, flags);
984 if (cam->next_buf < 0) {
985 cam_err(cam, "deliver_buffer: No next buffer\n");
986 spin_unlock_irqrestore(&cam->dev_lock, flags);
989 bufno = cam->next_buf;
990 clear_bit(bufno, &cam->flags);
991 if (++(cam->next_buf) >= cam->nbufs)
993 if (! test_bit(cam->next_buf, &cam->flags))
996 spin_unlock_irqrestore(&cam->dev_lock, flags);
998 if (len > cam->pix_format.sizeimage)
999 len = cam->pix_format.sizeimage;
1000 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
1007 * Get everything ready, and start grabbing frames.
1009 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
1012 unsigned long flags;
1015 * Configuration. If we still don't have DMA buffers,
1016 * make one last, desperate attempt.
1018 if (cam->nbufs == 0)
1019 if (cafe_alloc_dma_bufs(cam, 0))
1022 if (cafe_needs_config(cam)) {
1023 cafe_cam_configure(cam);
1024 ret = cafe_ctlr_configure(cam);
1032 spin_lock_irqsave(&cam->dev_lock, flags);
1033 cafe_reset_buffers(cam);
1034 cafe_ctlr_irq_enable(cam);
1036 cafe_ctlr_start(cam);
1037 spin_unlock_irqrestore(&cam->dev_lock, flags);
1042 static ssize_t cafe_v4l_read(struct file *filp,
1043 char __user *buffer, size_t len, loff_t *pos)
1045 struct cafe_camera *cam = filp->private_data;
1049 * Perhaps we're in speculative read mode and already
1052 mutex_lock(&cam->s_mutex);
1053 if (cam->state == S_SPECREAD) {
1054 if (cam->next_buf >= 0) {
1055 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1059 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1062 } else if (cam->state != S_IDLE) {
1068 * v4l2: multiple processes can open the device, but only
1069 * one gets to grab data from it.
1071 if (cam->owner && cam->owner != filp) {
1078 * Do setup if need be.
1080 if (cam->state != S_SPECREAD) {
1081 ret = cafe_read_setup(cam, S_SINGLEREAD);
1086 * Wait for something to happen. This should probably
1087 * be interruptible (FIXME).
1089 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1090 if (cam->next_buf < 0) {
1091 cam_err(cam, "read() operation timed out\n");
1092 cafe_ctlr_stop_dma(cam);
1097 * Give them their data and we should be done.
1099 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1102 mutex_unlock(&cam->s_mutex);
1114 * Streaming I/O support.
1119 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1120 enum v4l2_buf_type type)
1122 struct cafe_camera *cam = filp->private_data;
1125 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1127 mutex_lock(&cam->s_mutex);
1128 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1132 ret = cafe_read_setup(cam, S_STREAMING);
1135 mutex_unlock(&cam->s_mutex);
1141 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1142 enum v4l2_buf_type type)
1144 struct cafe_camera *cam = filp->private_data;
1147 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1149 mutex_lock(&cam->s_mutex);
1150 if (cam->state != S_STREAMING)
1153 cafe_ctlr_stop_dma(cam);
1157 mutex_unlock(&cam->s_mutex);
1164 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1166 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1168 INIT_LIST_HEAD(&buf->list);
1169 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1170 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1171 if (buf->buffer == NULL)
1176 buf->v4lbuf.index = index;
1177 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1178 buf->v4lbuf.field = V4L2_FIELD_NONE;
1179 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1181 * Offset: must be 32-bit even on a 64-bit system. video-buf
1182 * just uses the length times the index, but the spec warns
1183 * against doing just that - vma merging problems. So we
1184 * leave a gap between each pair of buffers.
1186 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1190 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1195 * If any buffers are mapped, we cannot free them at all.
1197 for (i = 0; i < cam->n_sbufs; i++)
1198 if (cam->sb_bufs[i].mapcount > 0)
1203 for (i = 0; i < cam->n_sbufs; i++)
1204 vfree(cam->sb_bufs[i].buffer);
1206 kfree(cam->sb_bufs);
1207 cam->sb_bufs = NULL;
1208 INIT_LIST_HEAD(&cam->sb_avail);
1209 INIT_LIST_HEAD(&cam->sb_full);
1215 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1216 struct v4l2_requestbuffers *req)
1218 struct cafe_camera *cam = filp->private_data;
1219 int ret = 0; /* Silence warning */
1222 * Make sure it's something we can do. User pointers could be
1223 * implemented without great pain, but that's not been done yet.
1225 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1227 if (req->memory != V4L2_MEMORY_MMAP)
1230 * If they ask for zero buffers, they really want us to stop streaming
1231 * (if it's happening) and free everything. Should we check owner?
1233 mutex_lock(&cam->s_mutex);
1234 if (req->count == 0) {
1235 if (cam->state == S_STREAMING)
1236 cafe_ctlr_stop_dma(cam);
1237 ret = cafe_free_sio_buffers (cam);
1241 * Device needs to be idle and working. We *could* try to do the
1242 * right thing in S_SPECREAD by shutting things down, but it
1243 * probably doesn't matter.
1245 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1251 if (req->count < min_buffers)
1252 req->count = min_buffers;
1253 else if (req->count > max_buffers)
1254 req->count = max_buffers;
1255 if (cam->n_sbufs > 0) {
1256 ret = cafe_free_sio_buffers(cam);
1261 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1263 if (cam->sb_bufs == NULL) {
1267 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1268 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1273 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1274 kfree(cam->sb_bufs);
1275 req->count = cam->n_sbufs; /* In case of partial success */
1278 mutex_unlock(&cam->s_mutex);
1283 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1284 struct v4l2_buffer *buf)
1286 struct cafe_camera *cam = filp->private_data;
1289 mutex_lock(&cam->s_mutex);
1290 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1292 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1294 *buf = cam->sb_bufs[buf->index].v4lbuf;
1297 mutex_unlock(&cam->s_mutex);
1301 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1302 struct v4l2_buffer *buf)
1304 struct cafe_camera *cam = filp->private_data;
1305 struct cafe_sio_buffer *sbuf;
1307 unsigned long flags;
1309 mutex_lock(&cam->s_mutex);
1310 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1312 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1314 sbuf = cam->sb_bufs + buf->index;
1315 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1316 ret = 0; /* Already queued?? */
1319 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1320 /* Spec doesn't say anything, seems appropriate tho */
1324 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1325 spin_lock_irqsave(&cam->dev_lock, flags);
1326 list_add(&sbuf->list, &cam->sb_avail);
1327 spin_unlock_irqrestore(&cam->dev_lock, flags);
1330 mutex_unlock(&cam->s_mutex);
1334 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1335 struct v4l2_buffer *buf)
1337 struct cafe_camera *cam = filp->private_data;
1338 struct cafe_sio_buffer *sbuf;
1340 unsigned long flags;
1342 mutex_lock(&cam->s_mutex);
1343 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1345 if (cam->state != S_STREAMING)
1347 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1352 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1353 mutex_unlock(&cam->s_mutex);
1354 if (wait_event_interruptible(cam->iowait,
1355 !list_empty(&cam->sb_full))) {
1359 mutex_lock(&cam->s_mutex);
1362 if (cam->state != S_STREAMING)
1365 spin_lock_irqsave(&cam->dev_lock, flags);
1366 /* Should probably recheck !list_empty() here */
1367 sbuf = list_entry(cam->sb_full.next,
1368 struct cafe_sio_buffer, list);
1369 list_del_init(&sbuf->list);
1370 spin_unlock_irqrestore(&cam->dev_lock, flags);
1371 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1372 *buf = sbuf->v4lbuf;
1377 mutex_unlock(&cam->s_mutex);
1384 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1386 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1388 * Locking: done under mmap_sem, so we don't need to
1389 * go back to the camera lock here.
1395 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1397 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1399 mutex_lock(&sbuf->cam->s_mutex);
1401 /* Docs say we should stop I/O too... */
1402 if (sbuf->mapcount == 0)
1403 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1404 mutex_unlock(&sbuf->cam->s_mutex);
1407 static struct vm_operations_struct cafe_v4l_vm_ops = {
1408 .open = cafe_v4l_vm_open,
1409 .close = cafe_v4l_vm_close
1413 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1415 struct cafe_camera *cam = filp->private_data;
1416 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1419 struct cafe_sio_buffer *sbuf = NULL;
1421 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1424 * Find the buffer they are looking for.
1426 mutex_lock(&cam->s_mutex);
1427 for (i = 0; i < cam->n_sbufs; i++)
1428 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1429 sbuf = cam->sb_bufs + i;
1435 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1438 vma->vm_flags |= VM_DONTEXPAND;
1439 vma->vm_private_data = sbuf;
1440 vma->vm_ops = &cafe_v4l_vm_ops;
1441 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1442 cafe_v4l_vm_open(vma);
1445 mutex_unlock(&cam->s_mutex);
1451 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1453 struct cafe_camera *cam;
1455 cam = cafe_find_dev(iminor(inode));
1458 filp->private_data = cam;
1460 mutex_lock(&cam->s_mutex);
1461 if (cam->users == 0) {
1462 cafe_ctlr_power_up(cam);
1463 __cafe_cam_reset(cam);
1464 cafe_set_config_needed(cam, 1);
1465 /* FIXME make sure this is complete */
1468 mutex_unlock(&cam->s_mutex);
1473 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1475 struct cafe_camera *cam = filp->private_data;
1477 mutex_lock(&cam->s_mutex);
1479 if (filp == cam->owner) {
1480 cafe_ctlr_stop_dma(cam);
1481 cafe_free_sio_buffers(cam);
1484 if (cam->users == 0) {
1485 cafe_ctlr_power_down(cam);
1486 if (! alloc_bufs_at_load)
1487 cafe_free_dma_bufs(cam);
1489 mutex_unlock(&cam->s_mutex);
1495 static unsigned int cafe_v4l_poll(struct file *filp,
1496 struct poll_table_struct *pt)
1498 struct cafe_camera *cam = filp->private_data;
1500 poll_wait(filp, &cam->iowait, pt);
1501 if (cam->next_buf >= 0)
1502 return POLLIN | POLLRDNORM;
1508 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1509 struct v4l2_queryctrl *qc)
1511 struct cafe_camera *cam = filp->private_data;
1514 mutex_lock(&cam->s_mutex);
1515 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1516 mutex_unlock(&cam->s_mutex);
1521 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1522 struct v4l2_control *ctrl)
1524 struct cafe_camera *cam = filp->private_data;
1527 mutex_lock(&cam->s_mutex);
1528 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1529 mutex_unlock(&cam->s_mutex);
1534 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1535 struct v4l2_control *ctrl)
1537 struct cafe_camera *cam = filp->private_data;
1540 mutex_lock(&cam->s_mutex);
1541 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1542 mutex_unlock(&cam->s_mutex);
1550 static int cafe_vidioc_querycap(struct file *file, void *priv,
1551 struct v4l2_capability *cap)
1553 strcpy(cap->driver, "cafe_ccic");
1554 strcpy(cap->card, "cafe_ccic");
1555 cap->version = CAFE_VERSION;
1556 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1557 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1563 * The default format we use until somebody says otherwise.
1565 static struct v4l2_pix_format cafe_def_pix_format = {
1567 .height = VGA_HEIGHT,
1568 .pixelformat = V4L2_PIX_FMT_YUYV,
1569 .field = V4L2_FIELD_NONE,
1570 .bytesperline = VGA_WIDTH*2,
1571 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1574 static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1575 void *priv, struct v4l2_fmtdesc *fmt)
1577 struct cafe_camera *cam = priv;
1580 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1582 mutex_lock(&cam->s_mutex);
1583 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1584 mutex_unlock(&cam->s_mutex);
1589 static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1590 struct v4l2_format *fmt)
1592 struct cafe_camera *cam = priv;
1595 mutex_lock(&cam->s_mutex);
1596 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1597 mutex_unlock(&cam->s_mutex);
1601 static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1602 struct v4l2_format *fmt)
1604 struct cafe_camera *cam = priv;
1608 * Can't do anything if the device is not idle
1609 * Also can't if there are streaming buffers in place.
1611 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1614 * See if the formatting works in principle.
1616 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1620 * Now we start to change things for real, so let's do it
1623 mutex_lock(&cam->s_mutex);
1624 cam->pix_format = fmt->fmt.pix;
1626 * Make sure we have appropriate DMA buffers.
1629 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1630 cafe_free_dma_bufs(cam);
1631 if (cam->nbufs == 0) {
1632 if (cafe_alloc_dma_bufs(cam, 0))
1636 * It looks like this might work, so let's program the sensor.
1638 ret = cafe_cam_configure(cam);
1640 ret = cafe_ctlr_configure(cam);
1642 mutex_unlock(&cam->s_mutex);
1647 * Return our stored notion of how the camera is/should be configured.
1648 * The V4l2 spec wants us to be smarter, and actually get this from
1649 * the camera (and not mess with it at open time). Someday.
1651 static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1652 struct v4l2_format *f)
1654 struct cafe_camera *cam = priv;
1656 f->fmt.pix = cam->pix_format;
1661 * We only have one input - the sensor - so minimize the nonsense here.
1663 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1664 struct v4l2_input *input)
1666 if (input->index != 0)
1669 input->type = V4L2_INPUT_TYPE_CAMERA;
1670 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1671 strcpy(input->name, "Camera");
1675 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1681 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1689 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1695 * G/S_PARM. Most of this is done by the sensor, but we are
1696 * the level which controls the number of read buffers.
1698 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1699 struct v4l2_streamparm *parms)
1701 struct cafe_camera *cam = priv;
1704 mutex_lock(&cam->s_mutex);
1705 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1706 mutex_unlock(&cam->s_mutex);
1707 parms->parm.capture.readbuffers = n_dma_bufs;
1711 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1712 struct v4l2_streamparm *parms)
1714 struct cafe_camera *cam = priv;
1717 mutex_lock(&cam->s_mutex);
1718 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1719 mutex_unlock(&cam->s_mutex);
1720 parms->parm.capture.readbuffers = n_dma_bufs;
1725 static void cafe_v4l_dev_release(struct video_device *vd)
1727 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1734 * This template device holds all of those v4l2 methods; we
1735 * clone it for specific real devices.
1738 static const struct file_operations cafe_v4l_fops = {
1739 .owner = THIS_MODULE,
1740 .open = cafe_v4l_open,
1741 .release = cafe_v4l_release,
1742 .read = cafe_v4l_read,
1743 .poll = cafe_v4l_poll,
1744 .mmap = cafe_v4l_mmap,
1745 .ioctl = video_ioctl2,
1746 .llseek = no_llseek,
1749 static struct video_device cafe_v4l_template = {
1751 .type = VFL_TYPE_GRABBER,
1752 .type2 = VID_TYPE_CAPTURE,
1753 .minor = -1, /* Get one dynamically */
1754 .tvnorms = V4L2_STD_NTSC_M,
1755 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1757 .fops = &cafe_v4l_fops,
1758 .release = cafe_v4l_dev_release,
1760 .vidioc_querycap = cafe_vidioc_querycap,
1761 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1762 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1763 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1764 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1765 .vidioc_enum_input = cafe_vidioc_enum_input,
1766 .vidioc_g_input = cafe_vidioc_g_input,
1767 .vidioc_s_input = cafe_vidioc_s_input,
1768 .vidioc_s_std = cafe_vidioc_s_std,
1769 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1770 .vidioc_querybuf = cafe_vidioc_querybuf,
1771 .vidioc_qbuf = cafe_vidioc_qbuf,
1772 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1773 .vidioc_streamon = cafe_vidioc_streamon,
1774 .vidioc_streamoff = cafe_vidioc_streamoff,
1775 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1776 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1777 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1778 .vidioc_g_parm = cafe_vidioc_g_parm,
1779 .vidioc_s_parm = cafe_vidioc_s_parm,
1788 /* ---------------------------------------------------------------------- */
1790 * Interrupt handler stuff
1795 static void cafe_frame_tasklet(unsigned long data)
1797 struct cafe_camera *cam = (struct cafe_camera *) data;
1799 unsigned long flags;
1800 struct cafe_sio_buffer *sbuf;
1802 spin_lock_irqsave(&cam->dev_lock, flags);
1803 for (i = 0; i < cam->nbufs; i++) {
1804 int bufno = cam->next_buf;
1805 if (bufno < 0) { /* "will never happen" */
1806 cam_err(cam, "No valid bufs in tasklet!\n");
1809 if (++(cam->next_buf) >= cam->nbufs)
1811 if (! test_bit(bufno, &cam->flags))
1813 if (list_empty(&cam->sb_avail))
1814 break; /* Leave it valid, hope for better later */
1815 clear_bit(bufno, &cam->flags);
1816 sbuf = list_entry(cam->sb_avail.next,
1817 struct cafe_sio_buffer, list);
1819 * Drop the lock during the big copy. This *should* be safe...
1821 spin_unlock_irqrestore(&cam->dev_lock, flags);
1822 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1823 cam->pix_format.sizeimage);
1824 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1825 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1826 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1827 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1828 spin_lock_irqsave(&cam->dev_lock, flags);
1829 list_move_tail(&sbuf->list, &cam->sb_full);
1831 if (! list_empty(&cam->sb_full))
1832 wake_up(&cam->iowait);
1833 spin_unlock_irqrestore(&cam->dev_lock, flags);
1838 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1841 * Basic frame housekeeping.
1843 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1844 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1845 set_bit(frame, &cam->flags);
1846 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1847 if (cam->next_buf < 0)
1848 cam->next_buf = frame;
1849 cam->buf_seq[frame] = ++(cam->sequence);
1851 switch (cam->state) {
1853 * If in single read mode, try going speculative.
1856 cam->state = S_SPECREAD;
1857 cam->specframes = 0;
1858 wake_up(&cam->iowait);
1862 * If we are already doing speculative reads, and nobody is
1863 * reading them, just stop.
1866 if (++(cam->specframes) >= cam->nbufs) {
1867 cafe_ctlr_stop(cam);
1868 cafe_ctlr_irq_disable(cam);
1869 cam->state = S_IDLE;
1871 wake_up(&cam->iowait);
1874 * For the streaming case, we defer the real work to the
1877 * FIXME: if the application is not consuming the buffers,
1878 * we should eventually put things on hold and restart in
1882 tasklet_schedule(&cam->s_tasklet);
1886 cam_err(cam, "Frame interrupt in non-operational state\n");
1894 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1898 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1900 * Handle any frame completions. There really should
1901 * not be more than one of these, or we have fallen
1904 for (frame = 0; frame < cam->nbufs; frame++)
1905 if (irqs & (IRQ_EOF0 << frame))
1906 cafe_frame_complete(cam, frame);
1908 * If a frame starts, note that we have DMA active. This
1909 * code assumes that we won't get multiple frame interrupts
1910 * at once; may want to rethink that.
1912 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1913 set_bit(CF_DMA_ACTIVE, &cam->flags);
1918 static irqreturn_t cafe_irq(int irq, void *data)
1920 struct cafe_camera *cam = data;
1923 spin_lock(&cam->dev_lock);
1924 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1925 if ((irqs & ALLIRQS) == 0) {
1926 spin_unlock(&cam->dev_lock);
1929 if (irqs & FRAMEIRQS)
1930 cafe_frame_irq(cam, irqs);
1931 if (irqs & TWSIIRQS) {
1932 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1933 wake_up(&cam->smbus_wait);
1935 spin_unlock(&cam->dev_lock);
1940 /* -------------------------------------------------------------------------- */
1941 #ifdef CONFIG_VIDEO_ADV_DEBUG
1946 static char cafe_debug_buf[1024];
1947 static struct dentry *cafe_dfs_root;
1949 static void cafe_dfs_setup(void)
1951 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1952 if (IS_ERR(cafe_dfs_root)) {
1953 cafe_dfs_root = NULL; /* Never mind */
1954 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1958 static void cafe_dfs_shutdown(void)
1961 debugfs_remove(cafe_dfs_root);
1964 static int cafe_dfs_open(struct inode *inode, struct file *file)
1966 file->private_data = inode->i_private;
1970 static ssize_t cafe_dfs_read_regs(struct file *file,
1971 char __user *buf, size_t count, loff_t *ppos)
1973 struct cafe_camera *cam = file->private_data;
1974 char *s = cafe_debug_buf;
1977 for (offset = 0; offset < 0x44; offset += 4)
1978 s += sprintf(s, "%02x: %08x\n", offset,
1979 cafe_reg_read(cam, offset));
1980 for (offset = 0x88; offset <= 0x90; offset += 4)
1981 s += sprintf(s, "%02x: %08x\n", offset,
1982 cafe_reg_read(cam, offset));
1983 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1984 s += sprintf(s, "%02x: %08x\n", offset,
1985 cafe_reg_read(cam, offset));
1986 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1987 s += sprintf(s, "%04x: %08x\n", offset,
1988 cafe_reg_read(cam, offset));
1989 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1990 s - cafe_debug_buf);
1993 static const struct file_operations cafe_dfs_reg_ops = {
1994 .owner = THIS_MODULE,
1995 .read = cafe_dfs_read_regs,
1996 .open = cafe_dfs_open
1999 static ssize_t cafe_dfs_read_cam(struct file *file,
2000 char __user *buf, size_t count, loff_t *ppos)
2002 struct cafe_camera *cam = file->private_data;
2003 char *s = cafe_debug_buf;
2008 for (offset = 0x0; offset < 0x8a; offset++)
2012 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2013 s += sprintf(s, "%02x: %02x\n", offset, v);
2015 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2016 s - cafe_debug_buf);
2019 static const struct file_operations cafe_dfs_cam_ops = {
2020 .owner = THIS_MODULE,
2021 .read = cafe_dfs_read_cam,
2022 .open = cafe_dfs_open
2027 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2033 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2034 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2035 cam, &cafe_dfs_reg_ops);
2036 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2037 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2038 cam, &cafe_dfs_cam_ops);
2042 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2044 if (! IS_ERR(cam->dfs_regs))
2045 debugfs_remove(cam->dfs_regs);
2046 if (! IS_ERR(cam->dfs_cam_regs))
2047 debugfs_remove(cam->dfs_cam_regs);
2052 #define cafe_dfs_setup()
2053 #define cafe_dfs_shutdown()
2054 #define cafe_dfs_cam_setup(cam)
2055 #define cafe_dfs_cam_shutdown(cam)
2056 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2061 /* ------------------------------------------------------------------------*/
2063 * PCI interface stuff.
2066 static int cafe_pci_probe(struct pci_dev *pdev,
2067 const struct pci_device_id *id)
2071 struct cafe_camera *cam;
2073 * Make sure we have a camera here - we'll get calls for
2074 * the other cafe devices as well.
2076 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2077 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2080 * Start putting together one of our big camera structures.
2083 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2086 mutex_init(&cam->s_mutex);
2087 mutex_lock(&cam->s_mutex);
2088 spin_lock_init(&cam->dev_lock);
2089 cam->state = S_NOTREADY;
2090 cafe_set_config_needed(cam, 1);
2091 init_waitqueue_head(&cam->smbus_wait);
2092 init_waitqueue_head(&cam->iowait);
2094 cam->pix_format = cafe_def_pix_format;
2095 INIT_LIST_HEAD(&cam->dev_list);
2096 INIT_LIST_HEAD(&cam->sb_avail);
2097 INIT_LIST_HEAD(&cam->sb_full);
2098 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2100 * Get set up on the PCI bus.
2102 ret = pci_enable_device(pdev);
2105 pci_set_master(pdev);
2108 cam->regs = pci_iomap(pdev, 0, 0);
2110 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2113 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2117 * Initialize the controller and leave it powered up. It will
2118 * stay that way until the sensor driver shows up.
2120 cafe_ctlr_init(cam);
2121 cafe_ctlr_power_up(cam);
2123 * Set up I2C/SMBUS communications. We have to drop the mutex here
2124 * because the sensor could attach in this call chain, leading to
2125 * unsightly deadlocks.
2127 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2128 ret = cafe_smbus_setup(cam);
2132 * Get the v4l2 setup done.
2134 mutex_lock(&cam->s_mutex);
2135 cam->v4ldev = cafe_v4l_template;
2136 cam->v4ldev.debug = 0;
2137 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2138 cam->v4ldev.dev = &pdev->dev;
2139 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2143 * If so requested, try to get our DMA buffers now.
2145 if (alloc_bufs_at_load) {
2146 if (cafe_alloc_dma_bufs(cam, 1))
2147 cam_warn(cam, "Unable to alloc DMA buffers at load"
2148 " will try again later.");
2151 cafe_dfs_cam_setup(cam);
2152 mutex_unlock(&cam->s_mutex);
2157 cafe_smbus_shutdown(cam);
2159 cafe_ctlr_power_down(cam);
2160 free_irq(pdev->irq, cam);
2162 pci_iounmap(pdev, cam->regs);
2171 * Shut down an initialized device
2173 static void cafe_shutdown(struct cafe_camera *cam)
2175 /* FIXME: Make sure we take care of everything here */
2176 cafe_dfs_cam_shutdown(cam);
2177 if (cam->n_sbufs > 0)
2178 /* What if they are still mapped? Shouldn't be, but... */
2179 cafe_free_sio_buffers(cam);
2180 cafe_remove_dev(cam);
2181 cafe_ctlr_stop_dma(cam);
2182 cafe_ctlr_power_down(cam);
2183 cafe_smbus_shutdown(cam);
2184 cafe_free_dma_bufs(cam);
2185 free_irq(cam->pdev->irq, cam);
2186 pci_iounmap(cam->pdev, cam->regs);
2187 video_unregister_device(&cam->v4ldev);
2188 /* kfree(cam); done in v4l_release () */
2192 static void cafe_pci_remove(struct pci_dev *pdev)
2194 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2197 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2200 mutex_lock(&cam->s_mutex);
2202 cam_warn(cam, "Removing a device with users!\n");
2204 /* No unlock - it no longer exists */
2210 * Basic power management.
2212 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2214 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2217 ret = pci_save_state(pdev);
2220 cafe_ctlr_stop_dma(cam);
2221 cafe_ctlr_power_down(cam);
2222 pci_disable_device(pdev);
2227 static int cafe_pci_resume(struct pci_dev *pdev)
2229 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2232 ret = pci_restore_state(pdev);
2235 ret = pci_enable_device(pdev);
2237 cam_warn(cam, "Unable to re-enable device on resume!\n");
2240 cafe_ctlr_init(cam);
2241 cafe_ctlr_power_up(cam);
2242 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2243 if (cam->state == S_SPECREAD)
2244 cam->state = S_IDLE; /* Don't bother restarting */
2245 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2246 ret = cafe_read_setup(cam, cam->state);
2250 #endif /* CONFIG_PM */
2253 static struct pci_device_id cafe_ids[] = {
2254 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2255 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2259 MODULE_DEVICE_TABLE(pci, cafe_ids);
2261 static struct pci_driver cafe_pci_driver = {
2262 .name = "cafe1000-ccic",
2263 .id_table = cafe_ids,
2264 .probe = cafe_pci_probe,
2265 .remove = cafe_pci_remove,
2267 .suspend = cafe_pci_suspend,
2268 .resume = cafe_pci_resume,
2275 static int __init cafe_init(void)
2279 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2282 ret = pci_register_driver(&cafe_pci_driver);
2284 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2287 request_module("ov7670"); /* FIXME want something more general */
2295 static void __exit cafe_exit(void)
2297 pci_unregister_driver(&cafe_pci_driver);
2298 cafe_dfs_shutdown();
2301 module_init(cafe_init);
2302 module_exit(cafe_exit);