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.
8 * Written by Jonathan Corbet, corbet@lwn.net.
10 * This file may be distributed under the terms of the GNU General
11 * Public License, version 2.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <linux/device.h>
26 #include <linux/wait.h>
27 #include <linux/list.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/delay.h>
30 #include <linux/debugfs.h>
31 #include <linux/jiffies.h>
32 #include <linux/vmalloc.h>
34 #include <asm/uaccess.h>
37 #include "cafe_ccic-regs.h"
39 #define CAFE_VERSION 0x000001
45 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
46 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("Video");
51 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
52 * we must have physically contiguous buffers to bring frames into.
53 * These parameters control how many buffers we use, whether we
54 * allocate them at load time (better chance of success, but nails down
55 * memory) or when somebody tries to use the camera (riskier), and,
56 * for load-time allocation, how big they should be.
58 * The controller can cycle through three buffers. We could use
59 * more by flipping pointers around, but it probably makes little
63 #define MAX_DMA_BUFS 3
64 static int alloc_bufs_at_load = 0;
65 module_param(alloc_bufs_at_load, bool, 0444);
66 MODULE_PARM_DESC(alloc_bufs_at_load,
67 "Non-zero value causes DMA buffers to be allocated at module "
68 "load time. This increases the chances of successfully getting "
69 "those buffers, but at the cost of nailing down the memory from "
72 static int n_dma_bufs = 3;
73 module_param(n_dma_bufs, uint, 0644);
74 MODULE_PARM_DESC(n_dma_bufs,
75 "The number of DMA buffers to allocate. Can be either two "
76 "(saves memory, makes timing tighter) or three.");
78 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
79 module_param(dma_buf_size, uint, 0444);
80 MODULE_PARM_DESC(dma_buf_size,
81 "The size of the allocated DMA buffers. If actual operating "
82 "parameters require larger buffers, an attempt to reallocate "
85 static int min_buffers = 1;
86 module_param(min_buffers, uint, 0644);
87 MODULE_PARM_DESC(min_buffers,
88 "The minimum number of streaming I/O buffers we are willing "
91 static int max_buffers = 10;
92 module_param(max_buffers, uint, 0644);
93 MODULE_PARM_DESC(max_buffers,
94 "The maximum number of streaming I/O buffers an application "
95 "will be allowed to allocate. These buffers are big and live "
99 module_param(flip, bool, 0444);
100 MODULE_PARM_DESC(flip,
101 "If set, the sensor will be instructed to flip the image "
106 S_NOTREADY, /* Not yet initialized */
107 S_IDLE, /* Just hanging around */
108 S_FLAKED, /* Some sort of problem */
109 S_SINGLEREAD, /* In read() */
110 S_SPECREAD, /* Speculative read (for future read()) */
111 S_STREAMING /* Streaming data */
115 * Tracking of streaming I/O buffers.
117 struct cafe_sio_buffer {
118 struct list_head list;
119 struct v4l2_buffer v4lbuf;
120 char *buffer; /* Where it lives in kernel space */
122 struct cafe_camera *cam;
126 * A description of one of our devices.
127 * Locking: controlled by s_mutex. Certain fields, however, require
128 * the dev_lock spinlock; they are marked as such by comments.
129 * dev_lock is also required for access to device registers.
133 enum cafe_state state;
134 unsigned long flags; /* Buffer status, mainly (dev_lock) */
135 int users; /* How many open FDs */
136 struct file *owner; /* Who has data access (v4l2) */
139 * Subsystem structures.
141 struct pci_dev *pdev;
142 struct video_device v4ldev;
143 struct i2c_adapter i2c_adapter;
144 struct i2c_client *sensor;
146 unsigned char __iomem *regs;
147 struct list_head dev_list; /* link to other devices */
150 unsigned int nbufs; /* How many are alloc'd */
151 int next_buf; /* Next to consume (dev_lock) */
152 unsigned int dma_buf_size; /* allocated size */
153 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
154 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
155 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
156 unsigned int sequence; /* Frame sequence number */
157 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
159 /* Streaming buffers */
160 unsigned int n_sbufs; /* How many we have */
161 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
162 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
163 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
164 struct tasklet_struct s_tasklet;
166 /* Current operating parameters */
167 enum v4l2_chip_ident sensor_type; /* Currently ov7670 only */
168 struct v4l2_pix_format pix_format;
171 struct mutex s_mutex; /* Access to this structure */
172 spinlock_t dev_lock; /* Access to device */
175 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
176 wait_queue_head_t iowait; /* Waiting on frame data */
177 #ifdef CONFIG_VIDEO_ADV_DEBUG
178 struct dentry *dfs_regs;
179 struct dentry *dfs_cam_regs;
184 * Status flags. Always manipulated with bit operations.
186 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
187 #define CF_BUF1_VALID 1
188 #define CF_BUF2_VALID 2
189 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
190 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
195 * Start over with DMA buffers - dev_lock needed.
197 static void cafe_reset_buffers(struct cafe_camera *cam)
202 for (i = 0; i < cam->nbufs; i++)
203 clear_bit(i, &cam->flags);
207 static inline int cafe_needs_config(struct cafe_camera *cam)
209 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
212 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
215 set_bit(CF_CONFIG_NEEDED, &cam->flags);
217 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
224 * Debugging and related.
226 #define cam_err(cam, fmt, arg...) \
227 dev_err(&(cam)->pdev->dev, fmt, ##arg);
228 #define cam_warn(cam, fmt, arg...) \
229 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
230 #define cam_dbg(cam, fmt, arg...) \
231 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
234 /* ---------------------------------------------------------------------*/
236 * We keep a simple list of known devices to search at open time.
238 static LIST_HEAD(cafe_dev_list);
239 static DEFINE_MUTEX(cafe_dev_list_lock);
241 static void cafe_add_dev(struct cafe_camera *cam)
243 mutex_lock(&cafe_dev_list_lock);
244 list_add_tail(&cam->dev_list, &cafe_dev_list);
245 mutex_unlock(&cafe_dev_list_lock);
248 static void cafe_remove_dev(struct cafe_camera *cam)
250 mutex_lock(&cafe_dev_list_lock);
251 list_del(&cam->dev_list);
252 mutex_unlock(&cafe_dev_list_lock);
255 static struct cafe_camera *cafe_find_dev(int minor)
257 struct cafe_camera *cam;
259 mutex_lock(&cafe_dev_list_lock);
260 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
261 if (cam->v4ldev.minor == minor)
266 mutex_unlock(&cafe_dev_list_lock);
271 static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
273 struct cafe_camera *cam;
275 mutex_lock(&cafe_dev_list_lock);
276 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
277 if (cam->pdev == pdev)
282 mutex_unlock(&cafe_dev_list_lock);
287 /* ------------------------------------------------------------------------ */
289 * Device register I/O
291 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
294 iowrite32(val, cam->regs + reg);
297 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
300 return ioread32(cam->regs + reg);
304 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
305 unsigned int val, unsigned int mask)
307 unsigned int v = cafe_reg_read(cam, reg);
309 v = (v & ~mask) | (val & mask);
310 cafe_reg_write(cam, reg, v);
313 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
314 unsigned int reg, unsigned int val)
316 cafe_reg_write_mask(cam, reg, 0, val);
319 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
320 unsigned int reg, unsigned int val)
322 cafe_reg_write_mask(cam, reg, val, val);
327 /* -------------------------------------------------------------------- */
329 * The I2C/SMBUS interface to the camera itself starts here. The
330 * controller handles SMBUS itself, presenting a relatively simple register
331 * interface; all we have to do is to tell it where to route the data.
333 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
335 static int cafe_smbus_write_done(struct cafe_camera *cam)
341 * We must delay after the interrupt, or the controller gets confused
342 * and never does give us good status. Fortunately, we don't do this
346 spin_lock_irqsave(&cam->dev_lock, flags);
347 c1 = cafe_reg_read(cam, REG_TWSIC1);
348 spin_unlock_irqrestore(&cam->dev_lock, flags);
349 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
352 static int cafe_smbus_write_data(struct cafe_camera *cam,
353 u16 addr, u8 command, u8 value)
358 spin_lock_irqsave(&cam->dev_lock, flags);
359 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
360 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
362 * Marvell sez set clkdiv to all 1's for now.
364 rval |= TWSIC0_CLKDIV;
365 cafe_reg_write(cam, REG_TWSIC0, rval);
366 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
367 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
368 cafe_reg_write(cam, REG_TWSIC1, rval);
369 spin_unlock_irqrestore(&cam->dev_lock, flags);
370 msleep(2); /* Required or things flake */
372 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
374 spin_lock_irqsave(&cam->dev_lock, flags);
375 rval = cafe_reg_read(cam, REG_TWSIC1);
376 spin_unlock_irqrestore(&cam->dev_lock, flags);
378 if (rval & TWSIC1_WSTAT) {
379 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
383 if (rval & TWSIC1_ERROR) {
384 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
393 static int cafe_smbus_read_done(struct cafe_camera *cam)
399 * We must delay after the interrupt, or the controller gets confused
400 * and never does give us good status. Fortunately, we don't do this
404 spin_lock_irqsave(&cam->dev_lock, flags);
405 c1 = cafe_reg_read(cam, REG_TWSIC1);
406 spin_unlock_irqrestore(&cam->dev_lock, flags);
407 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
412 static int cafe_smbus_read_data(struct cafe_camera *cam,
413 u16 addr, u8 command, u8 *value)
418 spin_lock_irqsave(&cam->dev_lock, flags);
419 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
420 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
422 * Marvel sez set clkdiv to all 1's for now.
424 rval |= TWSIC0_CLKDIV;
425 cafe_reg_write(cam, REG_TWSIC0, rval);
426 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
427 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
428 cafe_reg_write(cam, REG_TWSIC1, rval);
429 spin_unlock_irqrestore(&cam->dev_lock, flags);
431 wait_event_timeout(cam->smbus_wait,
432 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
433 spin_lock_irqsave(&cam->dev_lock, flags);
434 rval = cafe_reg_read(cam, REG_TWSIC1);
435 spin_unlock_irqrestore(&cam->dev_lock, flags);
437 if (rval & TWSIC1_ERROR) {
438 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
441 if (! (rval & TWSIC1_RVALID)) {
442 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
446 *value = rval & 0xff;
451 * Perform a transfer over SMBUS. This thing is called under
452 * the i2c bus lock, so we shouldn't race with ourselves...
454 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
455 unsigned short flags, char rw, u8 command,
456 int size, union i2c_smbus_data *data)
458 struct cafe_camera *cam = i2c_get_adapdata(adapter);
462 * Refuse to talk to anything but OV cam chips. We should
463 * never even see an attempt to do so, but one never knows.
465 if (cam->sensor && addr != cam->sensor->addr) {
466 cam_err(cam, "funky smbus addr %d\n", addr);
470 * This interface would appear to only do byte data ops. OK
471 * it can do word too, but the cam chip has no use for that.
473 if (size != I2C_SMBUS_BYTE_DATA) {
474 cam_err(cam, "funky xfer size %d\n", size);
478 if (rw == I2C_SMBUS_WRITE)
479 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
480 else if (rw == I2C_SMBUS_READ)
481 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
486 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
490 spin_lock_irqsave(&cam->dev_lock, flags);
491 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
492 spin_unlock_irqrestore(&cam->dev_lock, flags);
495 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
497 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
498 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
501 static struct i2c_algorithm cafe_smbus_algo = {
502 .smbus_xfer = cafe_smbus_xfer,
503 .functionality = cafe_smbus_func
506 /* Somebody is on the bus */
507 static int cafe_cam_init(struct cafe_camera *cam);
508 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
509 static void cafe_ctlr_power_down(struct cafe_camera *cam);
511 static int cafe_smbus_attach(struct i2c_client *client)
513 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
516 * Don't talk to chips we don't recognize.
518 if (client->driver->id == I2C_DRIVERID_OV7670) {
519 cam->sensor = client;
520 return cafe_cam_init(cam);
525 static int cafe_smbus_detach(struct i2c_client *client)
527 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
529 if (cam->sensor == client) {
530 cafe_ctlr_stop_dma(cam);
531 cafe_ctlr_power_down(cam);
532 cam_err(cam, "lost the sensor!\n");
533 cam->sensor = NULL; /* Bummer, no camera */
534 cam->state = S_NOTREADY;
539 static int cafe_smbus_setup(struct cafe_camera *cam)
541 struct i2c_adapter *adap = &cam->i2c_adapter;
544 cafe_smbus_enable_irq(cam);
545 adap->id = I2C_HW_SMBUS_CAFE;
546 adap->class = I2C_CLASS_CAM_DIGITAL;
547 adap->owner = THIS_MODULE;
548 adap->client_register = cafe_smbus_attach;
549 adap->client_unregister = cafe_smbus_detach;
550 adap->algo = &cafe_smbus_algo;
551 strcpy(adap->name, "cafe_ccic");
552 i2c_set_adapdata(adap, cam);
553 ret = i2c_add_adapter(adap);
555 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
559 static void cafe_smbus_shutdown(struct cafe_camera *cam)
561 i2c_del_adapter(&cam->i2c_adapter);
565 /* ------------------------------------------------------------------- */
567 * Deal with the controller.
571 * Do everything we think we need to have the interface operating
572 * according to the desired format.
574 static void cafe_ctlr_dma(struct cafe_camera *cam)
577 * Store the first two Y buffers (we aren't supporting
578 * planar formats for now, so no UV bufs). Then either
579 * set the third if it exists, or tell the controller
582 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
583 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
584 if (cam->nbufs > 2) {
585 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
586 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
589 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
590 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
593 static void cafe_ctlr_image(struct cafe_camera *cam)
596 struct v4l2_pix_format *fmt = &cam->pix_format;
598 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
599 (fmt->bytesperline & IMGSZ_H_MASK);
600 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
601 cafe_reg_write(cam, REG_IMGOFFSET, 0);
602 /* YPITCH just drops the last two bits */
603 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
606 * Tell the controller about the image format we are using.
608 switch (cam->pix_format.pixelformat) {
609 case V4L2_PIX_FMT_YUYV:
610 cafe_reg_write_mask(cam, REG_CTRL0,
611 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
615 case V4L2_PIX_FMT_RGB444:
616 cafe_reg_write_mask(cam, REG_CTRL0,
617 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
622 case V4L2_PIX_FMT_RGB565:
623 cafe_reg_write_mask(cam, REG_CTRL0,
624 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
629 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
633 * Make sure it knows we want to use hsync/vsync.
635 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
641 * Configure the controller for operation; caller holds the
644 static int cafe_ctlr_configure(struct cafe_camera *cam)
648 spin_lock_irqsave(&cam->dev_lock, flags);
650 cafe_ctlr_image(cam);
651 cafe_set_config_needed(cam, 0);
652 spin_unlock_irqrestore(&cam->dev_lock, flags);
656 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
659 * Clear any pending interrupts, since we do not
660 * expect to have I/O active prior to enabling.
662 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
663 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
666 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
668 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
672 * Make the controller start grabbing images. Everything must
673 * be set up before doing this.
675 static void cafe_ctlr_start(struct cafe_camera *cam)
677 /* set_bit performs a read, so no other barrier should be
679 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
682 static void cafe_ctlr_stop(struct cafe_camera *cam)
684 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
687 static void cafe_ctlr_init(struct cafe_camera *cam)
691 spin_lock_irqsave(&cam->dev_lock, flags);
693 * Added magic to bring up the hardware on the B-Test board
695 cafe_reg_write(cam, 0x3038, 0x8);
696 cafe_reg_write(cam, 0x315c, 0x80008);
698 * Go through the dance needed to wake the device up.
699 * Note that these registers are global and shared
700 * with the NAND and SD devices. Interaction between the
701 * three still needs to be examined.
703 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
704 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
705 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
706 mdelay(5); /* FIXME revisit this */
707 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
708 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
710 * Make sure it's not powered down.
712 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
714 * Turn off the enable bit. It sure should be off anyway,
715 * but it's good to be sure.
717 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
719 * Mask all interrupts.
721 cafe_reg_write(cam, REG_IRQMASK, 0);
723 * Clock the sensor appropriately. Controller clock should
724 * be 48MHz, sensor "typical" value is half that.
726 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
727 spin_unlock_irqrestore(&cam->dev_lock, flags);
732 * Stop the controller, and don't return until we're really sure that no
733 * further DMA is going on.
735 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
740 * Theory: stop the camera controller (whether it is operating
741 * or not). Delay briefly just in case we race with the SOF
742 * interrupt, then wait until no DMA is active.
744 spin_lock_irqsave(&cam->dev_lock, flags);
746 spin_unlock_irqrestore(&cam->dev_lock, flags);
748 wait_event_timeout(cam->iowait,
749 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
750 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
751 cam_err(cam, "Timeout waiting for DMA to end\n");
752 /* This would be bad news - what now? */
753 spin_lock_irqsave(&cam->dev_lock, flags);
755 cafe_ctlr_irq_disable(cam);
756 spin_unlock_irqrestore(&cam->dev_lock, flags);
762 static void cafe_ctlr_power_up(struct cafe_camera *cam)
766 spin_lock_irqsave(&cam->dev_lock, flags);
767 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
769 * Put the sensor into operational mode (assumes OLPC-style
770 * wiring). Control 0 is reset - set to 1 to operate.
771 * Control 1 is power down, set to 0 to operate.
773 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
774 mdelay(1); /* Marvell says 1ms will do it */
775 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
776 mdelay(1); /* Enough? */
777 spin_unlock_irqrestore(&cam->dev_lock, flags);
780 static void cafe_ctlr_power_down(struct cafe_camera *cam)
784 spin_lock_irqsave(&cam->dev_lock, flags);
785 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
786 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
787 spin_unlock_irqrestore(&cam->dev_lock, flags);
790 /* -------------------------------------------------------------------- */
792 * Communications with the sensor.
795 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
797 struct i2c_client *sc = cam->sensor;
800 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
802 ret = sc->driver->command(sc, cmd, arg);
803 if (ret == -EPERM) /* Unsupported command */
808 static int __cafe_cam_reset(struct cafe_camera *cam)
811 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
815 * We have found the sensor on the i2c. Let's try to have a
818 static int cafe_cam_init(struct cafe_camera *cam)
822 mutex_lock(&cam->s_mutex);
823 if (cam->state != S_NOTREADY)
824 cam_warn(cam, "Cam init with device in funky state %d",
826 ret = __cafe_cam_reset(cam);
829 ret = __cafe_cam_cmd(cam, VIDIOC_INT_G_CHIP_IDENT, &cam->sensor_type);
832 // if (cam->sensor->addr != OV7xx0_SID) {
833 if (cam->sensor_type != V4L2_IDENT_OV7670) {
834 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
838 /* Get/set parameters? */
842 mutex_unlock(&cam->s_mutex);
847 * Configure the sensor to match the parameters we have. Caller should
850 static int cafe_cam_set_flip(struct cafe_camera *cam)
852 struct v4l2_control ctrl;
854 memset(&ctrl, 0, sizeof(ctrl));
855 ctrl.id = V4L2_CID_VFLIP;
857 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
861 static int cafe_cam_configure(struct cafe_camera *cam)
863 struct v4l2_format fmt;
866 if (cam->state != S_IDLE)
868 fmt.fmt.pix = cam->pix_format;
869 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
871 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
873 * OV7670 does weird things if flip is set *before* format...
875 ret += cafe_cam_set_flip(cam);
879 /* -------------------------------------------------------------------- */
881 * DMA buffer management. These functions need s_mutex held.
884 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
885 * does a get_free_pages() call, and we waste a good chunk of an orderN
886 * allocation. Should try to allocate the whole set in one chunk.
888 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
892 cafe_set_config_needed(cam, 1);
894 cam->dma_buf_size = dma_buf_size;
896 cam->dma_buf_size = cam->pix_format.sizeimage;
901 for (i = 0; i < n_dma_bufs; i++) {
902 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
903 cam->dma_buf_size, cam->dma_handles + i,
905 if (cam->dma_bufs[i] == NULL) {
906 cam_warn(cam, "Failed to allocate DMA buffer\n");
909 /* For debug, remove eventually */
910 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
914 switch (cam->nbufs) {
916 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
917 cam->dma_bufs[0], cam->dma_handles[0]);
920 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
925 cam_warn(cam, "Will limp along with only 2 buffers\n");
931 static void cafe_free_dma_bufs(struct cafe_camera *cam)
935 for (i = 0; i < cam->nbufs; i++) {
936 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
937 cam->dma_bufs[i], cam->dma_handles[i]);
938 cam->dma_bufs[i] = NULL;
947 /* ----------------------------------------------------------------------- */
949 * Here starts the V4L2 interface code.
953 * Read an image from the device.
955 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
956 char __user *buffer, size_t len, loff_t *pos)
961 spin_lock_irqsave(&cam->dev_lock, flags);
962 if (cam->next_buf < 0) {
963 cam_err(cam, "deliver_buffer: No next buffer\n");
964 spin_unlock_irqrestore(&cam->dev_lock, flags);
967 bufno = cam->next_buf;
968 clear_bit(bufno, &cam->flags);
969 if (++(cam->next_buf) >= cam->nbufs)
971 if (! test_bit(cam->next_buf, &cam->flags))
974 spin_unlock_irqrestore(&cam->dev_lock, flags);
976 if (len > cam->pix_format.sizeimage)
977 len = cam->pix_format.sizeimage;
978 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
985 * Get everything ready, and start grabbing frames.
987 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
993 * Configuration. If we still don't have DMA buffers,
994 * make one last, desperate attempt.
997 if (cafe_alloc_dma_bufs(cam, 0))
1000 if (cafe_needs_config(cam)) {
1001 cafe_cam_configure(cam);
1002 ret = cafe_ctlr_configure(cam);
1010 spin_lock_irqsave(&cam->dev_lock, flags);
1011 cafe_reset_buffers(cam);
1012 cafe_ctlr_irq_enable(cam);
1014 cafe_ctlr_start(cam);
1015 spin_unlock_irqrestore(&cam->dev_lock, flags);
1020 static ssize_t cafe_v4l_read(struct file *filp,
1021 char __user *buffer, size_t len, loff_t *pos)
1023 struct cafe_camera *cam = filp->private_data;
1027 * Perhaps we're in speculative read mode and already
1030 mutex_lock(&cam->s_mutex);
1031 if (cam->state == S_SPECREAD) {
1032 if (cam->next_buf >= 0) {
1033 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1037 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1040 } else if (cam->state != S_IDLE) {
1046 * v4l2: multiple processes can open the device, but only
1047 * one gets to grab data from it.
1049 if (cam->owner && cam->owner != filp) {
1056 * Do setup if need be.
1058 if (cam->state != S_SPECREAD) {
1059 ret = cafe_read_setup(cam, S_SINGLEREAD);
1064 * Wait for something to happen. This should probably
1065 * be interruptible (FIXME).
1067 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1068 if (cam->next_buf < 0) {
1069 cam_err(cam, "read() operation timed out\n");
1070 cafe_ctlr_stop_dma(cam);
1075 * Give them their data and we should be done.
1077 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1080 mutex_unlock(&cam->s_mutex);
1092 * Streaming I/O support.
1097 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1098 enum v4l2_buf_type type)
1100 struct cafe_camera *cam = filp->private_data;
1103 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1105 mutex_lock(&cam->s_mutex);
1106 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1110 ret = cafe_read_setup(cam, S_STREAMING);
1113 mutex_unlock(&cam->s_mutex);
1119 static int cafe_vidioc_streamoff(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_STREAMING)
1131 cafe_ctlr_stop_dma(cam);
1135 mutex_unlock(&cam->s_mutex);
1142 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1144 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1146 INIT_LIST_HEAD(&buf->list);
1147 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1148 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1149 if (buf->buffer == NULL)
1154 buf->v4lbuf.index = index;
1155 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1156 buf->v4lbuf.field = V4L2_FIELD_NONE;
1157 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1159 * Offset: must be 32-bit even on a 64-bit system. video-buf
1160 * just uses the length times the index, but the spec warns
1161 * against doing just that - vma merging problems. So we
1162 * leave a gap between each pair of buffers.
1164 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1168 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1173 * If any buffers are mapped, we cannot free them at all.
1175 for (i = 0; i < cam->n_sbufs; i++)
1176 if (cam->sb_bufs[i].mapcount > 0)
1181 for (i = 0; i < cam->n_sbufs; i++)
1182 vfree(cam->sb_bufs[i].buffer);
1184 kfree(cam->sb_bufs);
1185 cam->sb_bufs = NULL;
1186 INIT_LIST_HEAD(&cam->sb_avail);
1187 INIT_LIST_HEAD(&cam->sb_full);
1193 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1194 struct v4l2_requestbuffers *req)
1196 struct cafe_camera *cam = filp->private_data;
1200 * Make sure it's something we can do. User pointers could be
1201 * implemented without great pain, but that's not been done yet.
1203 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 if (req->memory != V4L2_MEMORY_MMAP)
1208 * If they ask for zero buffers, they really want us to stop streaming
1209 * (if it's happening) and free everything. Should we check owner?
1211 mutex_lock(&cam->s_mutex);
1212 if (req->count == 0) {
1213 if (cam->state == S_STREAMING)
1214 cafe_ctlr_stop_dma(cam);
1215 ret = cafe_free_sio_buffers (cam);
1219 * Device needs to be idle and working. We *could* try to do the
1220 * right thing in S_SPECREAD by shutting things down, but it
1221 * probably doesn't matter.
1223 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1229 if (req->count < min_buffers)
1230 req->count = min_buffers;
1231 else if (req->count > max_buffers)
1232 req->count = max_buffers;
1233 if (cam->n_sbufs > 0) {
1234 ret = cafe_free_sio_buffers(cam);
1239 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1241 if (cam->sb_bufs == NULL) {
1245 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1246 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1251 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1252 kfree(cam->sb_bufs);
1255 req->count = cam->n_sbufs; /* In case of partial success */
1258 mutex_unlock(&cam->s_mutex);
1263 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1264 struct v4l2_buffer *buf)
1266 struct cafe_camera *cam = filp->private_data;
1269 mutex_lock(&cam->s_mutex);
1270 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1272 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1274 *buf = cam->sb_bufs[buf->index].v4lbuf;
1277 mutex_unlock(&cam->s_mutex);
1281 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1282 struct v4l2_buffer *buf)
1284 struct cafe_camera *cam = filp->private_data;
1285 struct cafe_sio_buffer *sbuf;
1287 unsigned long flags;
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 sbuf = cam->sb_bufs + buf->index;
1295 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1296 ret = 0; /* Already queued?? */
1299 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1300 /* Spec doesn't say anything, seems appropriate tho */
1304 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1305 spin_lock_irqsave(&cam->dev_lock, flags);
1306 list_add(&sbuf->list, &cam->sb_avail);
1307 spin_unlock_irqrestore(&cam->dev_lock, flags);
1310 mutex_unlock(&cam->s_mutex);
1314 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1315 struct v4l2_buffer *buf)
1317 struct cafe_camera *cam = filp->private_data;
1318 struct cafe_sio_buffer *sbuf;
1320 unsigned long flags;
1322 mutex_lock(&cam->s_mutex);
1323 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1325 if (cam->state != S_STREAMING)
1327 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1332 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1333 mutex_unlock(&cam->s_mutex);
1334 if (wait_event_interruptible(cam->iowait,
1335 !list_empty(&cam->sb_full))) {
1339 mutex_lock(&cam->s_mutex);
1342 if (cam->state != S_STREAMING)
1345 spin_lock_irqsave(&cam->dev_lock, flags);
1346 /* Should probably recheck !list_empty() here */
1347 sbuf = list_entry(cam->sb_full.next,
1348 struct cafe_sio_buffer, list);
1349 list_del_init(&sbuf->list);
1350 spin_unlock_irqrestore(&cam->dev_lock, flags);
1351 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1352 *buf = sbuf->v4lbuf;
1357 mutex_unlock(&cam->s_mutex);
1364 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1366 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1368 * Locking: done under mmap_sem, so we don't need to
1369 * go back to the camera lock here.
1375 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1377 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1379 mutex_lock(&sbuf->cam->s_mutex);
1381 /* Docs say we should stop I/O too... */
1382 if (sbuf->mapcount == 0)
1383 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1384 mutex_unlock(&sbuf->cam->s_mutex);
1387 static struct vm_operations_struct cafe_v4l_vm_ops = {
1388 .open = cafe_v4l_vm_open,
1389 .close = cafe_v4l_vm_close
1393 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1395 struct cafe_camera *cam = filp->private_data;
1396 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1399 struct cafe_sio_buffer *sbuf = NULL;
1401 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1404 * Find the buffer they are looking for.
1406 mutex_lock(&cam->s_mutex);
1407 for (i = 0; i < cam->n_sbufs; i++)
1408 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1409 sbuf = cam->sb_bufs + i;
1415 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1418 vma->vm_flags |= VM_DONTEXPAND;
1419 vma->vm_private_data = sbuf;
1420 vma->vm_ops = &cafe_v4l_vm_ops;
1421 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1422 cafe_v4l_vm_open(vma);
1425 mutex_unlock(&cam->s_mutex);
1431 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1433 struct cafe_camera *cam;
1435 cam = cafe_find_dev(iminor(inode));
1438 filp->private_data = cam;
1440 mutex_lock(&cam->s_mutex);
1441 if (cam->users == 0) {
1442 cafe_ctlr_power_up(cam);
1443 __cafe_cam_reset(cam);
1444 cafe_set_config_needed(cam, 1);
1445 /* FIXME make sure this is complete */
1448 mutex_unlock(&cam->s_mutex);
1453 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1455 struct cafe_camera *cam = filp->private_data;
1457 mutex_lock(&cam->s_mutex);
1459 if (filp == cam->owner) {
1460 cafe_ctlr_stop_dma(cam);
1461 cafe_free_sio_buffers(cam);
1464 if (cam->users == 0) {
1465 cafe_ctlr_power_down(cam);
1466 if (! alloc_bufs_at_load)
1467 cafe_free_dma_bufs(cam);
1469 mutex_unlock(&cam->s_mutex);
1475 static unsigned int cafe_v4l_poll(struct file *filp,
1476 struct poll_table_struct *pt)
1478 struct cafe_camera *cam = filp->private_data;
1480 poll_wait(filp, &cam->iowait, pt);
1481 if (cam->next_buf >= 0)
1482 return POLLIN | POLLRDNORM;
1488 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1489 struct v4l2_queryctrl *qc)
1491 struct cafe_camera *cam = filp->private_data;
1494 mutex_lock(&cam->s_mutex);
1495 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1496 mutex_unlock(&cam->s_mutex);
1501 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1502 struct v4l2_control *ctrl)
1504 struct cafe_camera *cam = filp->private_data;
1507 mutex_lock(&cam->s_mutex);
1508 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1509 mutex_unlock(&cam->s_mutex);
1514 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1515 struct v4l2_control *ctrl)
1517 struct cafe_camera *cam = filp->private_data;
1520 mutex_lock(&cam->s_mutex);
1521 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1522 mutex_unlock(&cam->s_mutex);
1530 static int cafe_vidioc_querycap(struct file *file, void *priv,
1531 struct v4l2_capability *cap)
1533 strcpy(cap->driver, "cafe_ccic");
1534 strcpy(cap->card, "cafe_ccic");
1535 cap->version = CAFE_VERSION;
1536 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1537 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1543 * The default format we use until somebody says otherwise.
1545 static struct v4l2_pix_format cafe_def_pix_format = {
1547 .height = VGA_HEIGHT,
1548 .pixelformat = V4L2_PIX_FMT_YUYV,
1549 .field = V4L2_FIELD_NONE,
1550 .bytesperline = VGA_WIDTH*2,
1551 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1554 static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1555 void *priv, struct v4l2_fmtdesc *fmt)
1557 struct cafe_camera *cam = priv;
1560 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1562 mutex_lock(&cam->s_mutex);
1563 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1564 mutex_unlock(&cam->s_mutex);
1569 static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1570 struct v4l2_format *fmt)
1572 struct cafe_camera *cam = priv;
1575 mutex_lock(&cam->s_mutex);
1576 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1577 mutex_unlock(&cam->s_mutex);
1581 static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1582 struct v4l2_format *fmt)
1584 struct cafe_camera *cam = priv;
1588 * Can't do anything if the device is not idle
1589 * Also can't if there are streaming buffers in place.
1591 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1594 * See if the formatting works in principle.
1596 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1600 * Now we start to change things for real, so let's do it
1603 mutex_lock(&cam->s_mutex);
1604 cam->pix_format = fmt->fmt.pix;
1606 * Make sure we have appropriate DMA buffers.
1609 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1610 cafe_free_dma_bufs(cam);
1611 if (cam->nbufs == 0) {
1612 if (cafe_alloc_dma_bufs(cam, 0))
1616 * It looks like this might work, so let's program the sensor.
1618 ret = cafe_cam_configure(cam);
1620 ret = cafe_ctlr_configure(cam);
1622 mutex_unlock(&cam->s_mutex);
1627 * Return our stored notion of how the camera is/should be configured.
1628 * The V4l2 spec wants us to be smarter, and actually get this from
1629 * the camera (and not mess with it at open time). Someday.
1631 static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1632 struct v4l2_format *f)
1634 struct cafe_camera *cam = priv;
1636 f->fmt.pix = cam->pix_format;
1641 * We only have one input - the sensor - so minimize the nonsense here.
1643 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1644 struct v4l2_input *input)
1646 if (input->index != 0)
1649 input->type = V4L2_INPUT_TYPE_CAMERA;
1650 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1651 strcpy(input->name, "Camera");
1655 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1661 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1669 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1675 * G/S_PARM. Most of this is done by the sensor, but we are
1676 * the level which controls the number of read buffers.
1678 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1679 struct v4l2_streamparm *parms)
1681 struct cafe_camera *cam = priv;
1684 mutex_lock(&cam->s_mutex);
1685 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1686 mutex_unlock(&cam->s_mutex);
1687 parms->parm.capture.readbuffers = n_dma_bufs;
1691 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1692 struct v4l2_streamparm *parms)
1694 struct cafe_camera *cam = priv;
1697 mutex_lock(&cam->s_mutex);
1698 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1699 mutex_unlock(&cam->s_mutex);
1700 parms->parm.capture.readbuffers = n_dma_bufs;
1705 static void cafe_v4l_dev_release(struct video_device *vd)
1707 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1714 * This template device holds all of those v4l2 methods; we
1715 * clone it for specific real devices.
1718 static const struct file_operations cafe_v4l_fops = {
1719 .owner = THIS_MODULE,
1720 .open = cafe_v4l_open,
1721 .release = cafe_v4l_release,
1722 .read = cafe_v4l_read,
1723 .poll = cafe_v4l_poll,
1724 .mmap = cafe_v4l_mmap,
1725 .ioctl = video_ioctl2,
1726 .llseek = no_llseek,
1729 static struct video_device cafe_v4l_template = {
1731 .type = VFL_TYPE_GRABBER,
1732 .type2 = VID_TYPE_CAPTURE,
1733 .minor = -1, /* Get one dynamically */
1734 .tvnorms = V4L2_STD_NTSC_M,
1735 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1737 .fops = &cafe_v4l_fops,
1738 .release = cafe_v4l_dev_release,
1740 .vidioc_querycap = cafe_vidioc_querycap,
1741 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1742 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1743 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1744 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1745 .vidioc_enum_input = cafe_vidioc_enum_input,
1746 .vidioc_g_input = cafe_vidioc_g_input,
1747 .vidioc_s_input = cafe_vidioc_s_input,
1748 .vidioc_s_std = cafe_vidioc_s_std,
1749 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1750 .vidioc_querybuf = cafe_vidioc_querybuf,
1751 .vidioc_qbuf = cafe_vidioc_qbuf,
1752 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1753 .vidioc_streamon = cafe_vidioc_streamon,
1754 .vidioc_streamoff = cafe_vidioc_streamoff,
1755 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1756 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1757 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1758 .vidioc_g_parm = cafe_vidioc_g_parm,
1759 .vidioc_s_parm = cafe_vidioc_s_parm,
1768 /* ---------------------------------------------------------------------- */
1770 * Interrupt handler stuff
1775 static void cafe_frame_tasklet(unsigned long data)
1777 struct cafe_camera *cam = (struct cafe_camera *) data;
1779 unsigned long flags;
1780 struct cafe_sio_buffer *sbuf;
1782 spin_lock_irqsave(&cam->dev_lock, flags);
1783 for (i = 0; i < cam->nbufs; i++) {
1784 int bufno = cam->next_buf;
1785 if (bufno < 0) { /* "will never happen" */
1786 cam_err(cam, "No valid bufs in tasklet!\n");
1789 if (++(cam->next_buf) >= cam->nbufs)
1791 if (! test_bit(bufno, &cam->flags))
1793 if (list_empty(&cam->sb_avail))
1794 break; /* Leave it valid, hope for better later */
1795 clear_bit(bufno, &cam->flags);
1797 * We could perhaps drop the spinlock during this
1798 * big copy. Something to consider.
1800 sbuf = list_entry(cam->sb_avail.next,
1801 struct cafe_sio_buffer, list);
1802 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1803 cam->pix_format.sizeimage);
1804 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1805 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1806 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1807 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1808 list_move_tail(&sbuf->list, &cam->sb_full);
1810 if (! list_empty(&cam->sb_full))
1811 wake_up(&cam->iowait);
1812 spin_unlock_irqrestore(&cam->dev_lock, flags);
1817 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1820 * Basic frame housekeeping.
1822 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1823 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1824 set_bit(frame, &cam->flags);
1825 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1826 if (cam->next_buf < 0)
1827 cam->next_buf = frame;
1828 cam->buf_seq[frame] = ++(cam->sequence);
1830 switch (cam->state) {
1832 * If in single read mode, try going speculative.
1835 cam->state = S_SPECREAD;
1836 cam->specframes = 0;
1837 wake_up(&cam->iowait);
1841 * If we are already doing speculative reads, and nobody is
1842 * reading them, just stop.
1845 if (++(cam->specframes) >= cam->nbufs) {
1846 cafe_ctlr_stop(cam);
1847 cafe_ctlr_irq_disable(cam);
1848 cam->state = S_IDLE;
1850 wake_up(&cam->iowait);
1853 * For the streaming case, we defer the real work to the
1856 * FIXME: if the application is not consuming the buffers,
1857 * we should eventually put things on hold and restart in
1861 tasklet_schedule(&cam->s_tasklet);
1865 cam_err(cam, "Frame interrupt in non-operational state\n");
1873 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1877 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1879 * Handle any frame completions. There really should
1880 * not be more than one of these, or we have fallen
1883 for (frame = 0; frame < cam->nbufs; frame++)
1884 if (irqs & (IRQ_EOF0 << frame))
1885 cafe_frame_complete(cam, frame);
1887 * If a frame starts, note that we have DMA active. This
1888 * code assumes that we won't get multiple frame interrupts
1889 * at once; may want to rethink that.
1891 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1892 set_bit(CF_DMA_ACTIVE, &cam->flags);
1897 static irqreturn_t cafe_irq(int irq, void *data)
1899 struct cafe_camera *cam = data;
1902 spin_lock(&cam->dev_lock);
1903 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1904 if ((irqs & ALLIRQS) == 0) {
1905 spin_unlock(&cam->dev_lock);
1908 if (irqs & FRAMEIRQS)
1909 cafe_frame_irq(cam, irqs);
1910 if (irqs & TWSIIRQS) {
1911 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1912 wake_up(&cam->smbus_wait);
1914 spin_unlock(&cam->dev_lock);
1919 /* -------------------------------------------------------------------------- */
1920 #ifdef CONFIG_VIDEO_ADV_DEBUG
1925 static char cafe_debug_buf[1024];
1926 static struct dentry *cafe_dfs_root;
1928 static void cafe_dfs_setup(void)
1930 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1931 if (IS_ERR(cafe_dfs_root)) {
1932 cafe_dfs_root = NULL; /* Never mind */
1933 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1937 static void cafe_dfs_shutdown(void)
1940 debugfs_remove(cafe_dfs_root);
1943 static int cafe_dfs_open(struct inode *inode, struct file *file)
1945 file->private_data = inode->i_private;
1949 static ssize_t cafe_dfs_read_regs(struct file *file,
1950 char __user *buf, size_t count, loff_t *ppos)
1952 struct cafe_camera *cam = file->private_data;
1953 char *s = cafe_debug_buf;
1956 for (offset = 0; offset < 0x44; offset += 4)
1957 s += sprintf(s, "%02x: %08x\n", offset,
1958 cafe_reg_read(cam, offset));
1959 for (offset = 0x88; offset <= 0x90; offset += 4)
1960 s += sprintf(s, "%02x: %08x\n", offset,
1961 cafe_reg_read(cam, offset));
1962 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1963 s += sprintf(s, "%02x: %08x\n", offset,
1964 cafe_reg_read(cam, offset));
1965 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1966 s += sprintf(s, "%04x: %08x\n", offset,
1967 cafe_reg_read(cam, offset));
1968 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1969 s - cafe_debug_buf);
1972 static const struct file_operations cafe_dfs_reg_ops = {
1973 .owner = THIS_MODULE,
1974 .read = cafe_dfs_read_regs,
1975 .open = cafe_dfs_open
1978 static ssize_t cafe_dfs_read_cam(struct file *file,
1979 char __user *buf, size_t count, loff_t *ppos)
1981 struct cafe_camera *cam = file->private_data;
1982 char *s = cafe_debug_buf;
1987 for (offset = 0x0; offset < 0x8a; offset++)
1991 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
1992 s += sprintf(s, "%02x: %02x\n", offset, v);
1994 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1995 s - cafe_debug_buf);
1998 static const struct file_operations cafe_dfs_cam_ops = {
1999 .owner = THIS_MODULE,
2000 .read = cafe_dfs_read_cam,
2001 .open = cafe_dfs_open
2006 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2012 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2013 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2014 cam, &cafe_dfs_reg_ops);
2015 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2016 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2017 cam, &cafe_dfs_cam_ops);
2021 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2023 if (! IS_ERR(cam->dfs_regs))
2024 debugfs_remove(cam->dfs_regs);
2025 if (! IS_ERR(cam->dfs_cam_regs))
2026 debugfs_remove(cam->dfs_cam_regs);
2031 #define cafe_dfs_setup()
2032 #define cafe_dfs_shutdown()
2033 #define cafe_dfs_cam_setup(cam)
2034 #define cafe_dfs_cam_shutdown(cam)
2035 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2040 /* ------------------------------------------------------------------------*/
2042 * PCI interface stuff.
2045 static int cafe_pci_probe(struct pci_dev *pdev,
2046 const struct pci_device_id *id)
2050 struct cafe_camera *cam;
2052 * Make sure we have a camera here - we'll get calls for
2053 * the other cafe devices as well.
2055 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2056 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2059 * Start putting together one of our big camera structures.
2062 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2065 mutex_init(&cam->s_mutex);
2066 mutex_lock(&cam->s_mutex);
2067 spin_lock_init(&cam->dev_lock);
2068 cam->state = S_NOTREADY;
2069 cafe_set_config_needed(cam, 1);
2070 init_waitqueue_head(&cam->smbus_wait);
2071 init_waitqueue_head(&cam->iowait);
2073 cam->pix_format = cafe_def_pix_format;
2074 INIT_LIST_HEAD(&cam->dev_list);
2075 INIT_LIST_HEAD(&cam->sb_avail);
2076 INIT_LIST_HEAD(&cam->sb_full);
2077 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2079 * Get set up on the PCI bus.
2081 ret = pci_enable_device(pdev);
2084 pci_set_master(pdev);
2087 cam->regs = pci_iomap(pdev, 0, 0);
2089 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2092 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2095 cafe_ctlr_init(cam);
2096 cafe_ctlr_power_up(cam);
2098 * Set up I2C/SMBUS communications
2100 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2101 ret = cafe_smbus_setup(cam);
2105 * Get the v4l2 setup done.
2107 mutex_lock(&cam->s_mutex);
2108 cam->v4ldev = cafe_v4l_template;
2109 cam->v4ldev.debug = 0;
2110 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2111 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2115 * If so requested, try to get our DMA buffers now.
2117 if (alloc_bufs_at_load) {
2118 if (cafe_alloc_dma_bufs(cam, 1))
2119 cam_warn(cam, "Unable to alloc DMA buffers at load"
2120 " will try again later.");
2123 cafe_dfs_cam_setup(cam);
2124 mutex_unlock(&cam->s_mutex);
2129 cafe_smbus_shutdown(cam);
2131 cafe_ctlr_power_down(cam);
2132 free_irq(pdev->irq, cam);
2134 pci_iounmap(pdev, cam->regs);
2143 * Shut down an initialized device
2145 static void cafe_shutdown(struct cafe_camera *cam)
2147 /* FIXME: Make sure we take care of everything here */
2148 cafe_dfs_cam_shutdown(cam);
2149 if (cam->n_sbufs > 0)
2150 /* What if they are still mapped? Shouldn't be, but... */
2151 cafe_free_sio_buffers(cam);
2152 cafe_remove_dev(cam);
2153 cafe_ctlr_stop_dma(cam);
2154 cafe_ctlr_power_down(cam);
2155 cafe_smbus_shutdown(cam);
2156 cafe_free_dma_bufs(cam);
2157 free_irq(cam->pdev->irq, cam);
2158 pci_iounmap(cam->pdev, cam->regs);
2159 video_unregister_device(&cam->v4ldev);
2160 /* kfree(cam); done in v4l_release () */
2164 static void cafe_pci_remove(struct pci_dev *pdev)
2166 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2169 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2172 mutex_lock(&cam->s_mutex);
2174 cam_warn(cam, "Removing a device with users!\n");
2176 /* No unlock - it no longer exists */
2182 static struct pci_device_id cafe_ids[] = {
2183 { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
2184 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2185 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2189 MODULE_DEVICE_TABLE(pci, cafe_ids);
2191 static struct pci_driver cafe_pci_driver = {
2192 .name = "cafe1000-ccic",
2193 .id_table = cafe_ids,
2194 .probe = cafe_pci_probe,
2195 .remove = cafe_pci_remove,
2201 static int __init cafe_init(void)
2205 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2208 ret = pci_register_driver(&cafe_pci_driver);
2210 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2213 request_module("ov7670"); /* FIXME want something more general */
2221 static void __exit cafe_exit(void)
2223 pci_unregister_driver(&cafe_pci_driver);
2224 cafe_dfs_shutdown();
2227 module_init(cafe_init);
2228 module_exit(cafe_exit);