2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
6 * The data sheet for this device can be found at:
7 * http://www.marvell.com/products/pcconn/88ALP01.jsp
9 * Copyright 2006 One Laptop Per Child Association, Inc.
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
12 * Written by Jonathan Corbet, corbet@lwn.net.
14 * This file may be distributed under the terms of the GNU General
15 * Public License, version 2.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <linux/device.h>
32 #include <linux/wait.h>
33 #include <linux/list.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/debugfs.h>
37 #include <linux/jiffies.h>
38 #include <linux/vmalloc.h>
40 #include <asm/uaccess.h>
43 #include "cafe_ccic-regs.h"
45 #define CAFE_VERSION 0x000002
51 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
52 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
53 MODULE_LICENSE("GPL");
54 MODULE_SUPPORTED_DEVICE("Video");
57 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
58 * we must have physically contiguous buffers to bring frames into.
59 * These parameters control how many buffers we use, whether we
60 * allocate them at load time (better chance of success, but nails down
61 * memory) or when somebody tries to use the camera (riskier), and,
62 * for load-time allocation, how big they should be.
64 * The controller can cycle through three buffers. We could use
65 * more by flipping pointers around, but it probably makes little
69 #define MAX_DMA_BUFS 3
70 static int alloc_bufs_at_read;
71 module_param(alloc_bufs_at_read, bool, 0444);
72 MODULE_PARM_DESC(alloc_bufs_at_read,
73 "Non-zero value causes DMA buffers to be allocated when the "
74 "video capture device is read, rather than at module load "
75 "time. This saves memory, but decreases the chances of "
76 "successfully getting those buffers.");
78 static int n_dma_bufs = 3;
79 module_param(n_dma_bufs, uint, 0644);
80 MODULE_PARM_DESC(n_dma_bufs,
81 "The number of DMA buffers to allocate. Can be either two "
82 "(saves memory, makes timing tighter) or three.");
84 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
85 module_param(dma_buf_size, uint, 0444);
86 MODULE_PARM_DESC(dma_buf_size,
87 "The size of the allocated DMA buffers. If actual operating "
88 "parameters require larger buffers, an attempt to reallocate "
91 static int min_buffers = 1;
92 module_param(min_buffers, uint, 0644);
93 MODULE_PARM_DESC(min_buffers,
94 "The minimum number of streaming I/O buffers we are willing "
97 static int max_buffers = 10;
98 module_param(max_buffers, uint, 0644);
99 MODULE_PARM_DESC(max_buffers,
100 "The maximum number of streaming I/O buffers an application "
101 "will be allowed to allocate. These buffers are big and live "
102 "in vmalloc space.");
105 module_param(flip, bool, 0444);
106 MODULE_PARM_DESC(flip,
107 "If set, the sensor will be instructed to flip the image "
112 S_NOTREADY, /* Not yet initialized */
113 S_IDLE, /* Just hanging around */
114 S_FLAKED, /* Some sort of problem */
115 S_SINGLEREAD, /* In read() */
116 S_SPECREAD, /* Speculative read (for future read()) */
117 S_STREAMING /* Streaming data */
121 * Tracking of streaming I/O buffers.
123 struct cafe_sio_buffer {
124 struct list_head list;
125 struct v4l2_buffer v4lbuf;
126 char *buffer; /* Where it lives in kernel space */
128 struct cafe_camera *cam;
132 * A description of one of our devices.
133 * Locking: controlled by s_mutex. Certain fields, however, require
134 * the dev_lock spinlock; they are marked as such by comments.
135 * dev_lock is also required for access to device registers.
139 enum cafe_state state;
140 unsigned long flags; /* Buffer status, mainly (dev_lock) */
141 int users; /* How many open FDs */
142 struct file *owner; /* Who has data access (v4l2) */
145 * Subsystem structures.
147 struct pci_dev *pdev;
148 struct video_device v4ldev;
149 struct i2c_adapter i2c_adapter;
150 struct i2c_client *sensor;
152 unsigned char __iomem *regs;
153 struct list_head dev_list; /* link to other devices */
156 unsigned int nbufs; /* How many are alloc'd */
157 int next_buf; /* Next to consume (dev_lock) */
158 unsigned int dma_buf_size; /* allocated size */
159 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
160 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
161 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
162 unsigned int sequence; /* Frame sequence number */
163 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
165 /* Streaming buffers */
166 unsigned int n_sbufs; /* How many we have */
167 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
168 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
169 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
170 struct tasklet_struct s_tasklet;
172 /* Current operating parameters */
173 u32 sensor_type; /* Currently ov7670 only */
174 struct v4l2_pix_format pix_format;
177 struct mutex s_mutex; /* Access to this structure */
178 spinlock_t dev_lock; /* Access to device */
181 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
182 wait_queue_head_t iowait; /* Waiting on frame data */
183 #ifdef CONFIG_VIDEO_ADV_DEBUG
184 struct dentry *dfs_regs;
185 struct dentry *dfs_cam_regs;
190 * Status flags. Always manipulated with bit operations.
192 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
193 #define CF_BUF1_VALID 1
194 #define CF_BUF2_VALID 2
195 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
196 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
201 * Start over with DMA buffers - dev_lock needed.
203 static void cafe_reset_buffers(struct cafe_camera *cam)
208 for (i = 0; i < cam->nbufs; i++)
209 clear_bit(i, &cam->flags);
213 static inline int cafe_needs_config(struct cafe_camera *cam)
215 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
218 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
221 set_bit(CF_CONFIG_NEEDED, &cam->flags);
223 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
230 * Debugging and related.
232 #define cam_err(cam, fmt, arg...) \
233 dev_err(&(cam)->pdev->dev, fmt, ##arg);
234 #define cam_warn(cam, fmt, arg...) \
235 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
236 #define cam_dbg(cam, fmt, arg...) \
237 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
240 /* ---------------------------------------------------------------------*/
242 * We keep a simple list of known devices to search at open time.
244 static LIST_HEAD(cafe_dev_list);
245 static DEFINE_MUTEX(cafe_dev_list_lock);
247 static void cafe_add_dev(struct cafe_camera *cam)
249 mutex_lock(&cafe_dev_list_lock);
250 list_add_tail(&cam->dev_list, &cafe_dev_list);
251 mutex_unlock(&cafe_dev_list_lock);
254 static void cafe_remove_dev(struct cafe_camera *cam)
256 mutex_lock(&cafe_dev_list_lock);
257 list_del(&cam->dev_list);
258 mutex_unlock(&cafe_dev_list_lock);
261 static struct cafe_camera *cafe_find_dev(int minor)
263 struct cafe_camera *cam;
265 mutex_lock(&cafe_dev_list_lock);
266 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
267 if (cam->v4ldev.minor == minor)
272 mutex_unlock(&cafe_dev_list_lock);
277 static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
279 struct cafe_camera *cam;
281 mutex_lock(&cafe_dev_list_lock);
282 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
283 if (cam->pdev == pdev)
288 mutex_unlock(&cafe_dev_list_lock);
293 /* ------------------------------------------------------------------------ */
295 * Device register I/O
297 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
300 iowrite32(val, cam->regs + reg);
303 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
306 return ioread32(cam->regs + reg);
310 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
311 unsigned int val, unsigned int mask)
313 unsigned int v = cafe_reg_read(cam, reg);
315 v = (v & ~mask) | (val & mask);
316 cafe_reg_write(cam, reg, v);
319 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
320 unsigned int reg, unsigned int val)
322 cafe_reg_write_mask(cam, reg, 0, val);
325 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
326 unsigned int reg, unsigned int val)
328 cafe_reg_write_mask(cam, reg, val, val);
333 /* -------------------------------------------------------------------- */
335 * The I2C/SMBUS interface to the camera itself starts here. The
336 * controller handles SMBUS itself, presenting a relatively simple register
337 * interface; all we have to do is to tell it where to route the data.
339 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
341 static int cafe_smbus_write_done(struct cafe_camera *cam)
347 * We must delay after the interrupt, or the controller gets confused
348 * and never does give us good status. Fortunately, we don't do this
352 spin_lock_irqsave(&cam->dev_lock, flags);
353 c1 = cafe_reg_read(cam, REG_TWSIC1);
354 spin_unlock_irqrestore(&cam->dev_lock, flags);
355 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
358 static int cafe_smbus_write_data(struct cafe_camera *cam,
359 u16 addr, u8 command, u8 value)
363 DEFINE_WAIT(the_wait);
365 spin_lock_irqsave(&cam->dev_lock, flags);
366 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
367 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
369 * Marvell sez set clkdiv to all 1's for now.
371 rval |= TWSIC0_CLKDIV;
372 cafe_reg_write(cam, REG_TWSIC0, rval);
373 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
374 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
375 cafe_reg_write(cam, REG_TWSIC1, rval);
376 spin_unlock_irqrestore(&cam->dev_lock, flags);
379 * Time to wait for the write to complete. THIS IS A RACY
380 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
381 * register too quickly after starting the operation sends
382 * the device into a place that may be kinder and better, but
383 * which is absolutely useless for controlling the sensor. In
384 * practice we have plenty of time to get into our sleep state
385 * before the interrupt hits, and the worst case is that we
386 * time out and then see that things completed, so this seems
387 * the best way for now.
390 prepare_to_wait(&cam->smbus_wait, &the_wait,
391 TASK_UNINTERRUPTIBLE);
392 schedule_timeout(1); /* even 1 jiffy is too long */
393 finish_wait(&cam->smbus_wait, &the_wait);
394 } while (!cafe_smbus_write_done(cam));
396 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
397 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
400 spin_lock_irqsave(&cam->dev_lock, flags);
401 rval = cafe_reg_read(cam, REG_TWSIC1);
402 spin_unlock_irqrestore(&cam->dev_lock, flags);
404 if (rval & TWSIC1_WSTAT) {
405 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
409 if (rval & TWSIC1_ERROR) {
410 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
419 static int cafe_smbus_read_done(struct cafe_camera *cam)
425 * We must delay after the interrupt, or the controller gets confused
426 * and never does give us good status. Fortunately, we don't do this
430 spin_lock_irqsave(&cam->dev_lock, flags);
431 c1 = cafe_reg_read(cam, REG_TWSIC1);
432 spin_unlock_irqrestore(&cam->dev_lock, flags);
433 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
438 static int cafe_smbus_read_data(struct cafe_camera *cam,
439 u16 addr, u8 command, u8 *value)
444 spin_lock_irqsave(&cam->dev_lock, flags);
445 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
446 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
448 * Marvel sez set clkdiv to all 1's for now.
450 rval |= TWSIC0_CLKDIV;
451 cafe_reg_write(cam, REG_TWSIC0, rval);
452 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
453 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
454 cafe_reg_write(cam, REG_TWSIC1, rval);
455 spin_unlock_irqrestore(&cam->dev_lock, flags);
457 wait_event_timeout(cam->smbus_wait,
458 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
459 spin_lock_irqsave(&cam->dev_lock, flags);
460 rval = cafe_reg_read(cam, REG_TWSIC1);
461 spin_unlock_irqrestore(&cam->dev_lock, flags);
463 if (rval & TWSIC1_ERROR) {
464 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
467 if (! (rval & TWSIC1_RVALID)) {
468 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
472 *value = rval & 0xff;
477 * Perform a transfer over SMBUS. This thing is called under
478 * the i2c bus lock, so we shouldn't race with ourselves...
480 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
481 unsigned short flags, char rw, u8 command,
482 int size, union i2c_smbus_data *data)
484 struct cafe_camera *cam = i2c_get_adapdata(adapter);
488 * Refuse to talk to anything but OV cam chips. We should
489 * never even see an attempt to do so, but one never knows.
491 if (cam->sensor && addr != cam->sensor->addr) {
492 cam_err(cam, "funky smbus addr %d\n", addr);
496 * This interface would appear to only do byte data ops. OK
497 * it can do word too, but the cam chip has no use for that.
499 if (size != I2C_SMBUS_BYTE_DATA) {
500 cam_err(cam, "funky xfer size %d\n", size);
504 if (rw == I2C_SMBUS_WRITE)
505 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
506 else if (rw == I2C_SMBUS_READ)
507 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
512 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
516 spin_lock_irqsave(&cam->dev_lock, flags);
517 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
518 spin_unlock_irqrestore(&cam->dev_lock, flags);
521 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
523 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
524 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
527 static struct i2c_algorithm cafe_smbus_algo = {
528 .smbus_xfer = cafe_smbus_xfer,
529 .functionality = cafe_smbus_func
532 /* Somebody is on the bus */
533 static int cafe_cam_init(struct cafe_camera *cam);
534 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
535 static void cafe_ctlr_power_down(struct cafe_camera *cam);
537 static int cafe_smbus_attach(struct i2c_client *client)
539 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
542 * Don't talk to chips we don't recognize.
544 if (client->driver->id == I2C_DRIVERID_OV7670) {
545 cam->sensor = client;
546 return cafe_cam_init(cam);
551 static int cafe_smbus_detach(struct i2c_client *client)
553 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
555 if (cam->sensor == client) {
556 cafe_ctlr_stop_dma(cam);
557 cafe_ctlr_power_down(cam);
558 cam_err(cam, "lost the sensor!\n");
559 cam->sensor = NULL; /* Bummer, no camera */
560 cam->state = S_NOTREADY;
565 static int cafe_smbus_setup(struct cafe_camera *cam)
567 struct i2c_adapter *adap = &cam->i2c_adapter;
570 cafe_smbus_enable_irq(cam);
571 adap->id = I2C_HW_SMBUS_CAFE;
572 adap->class = I2C_CLASS_CAM_DIGITAL;
573 adap->owner = THIS_MODULE;
574 adap->client_register = cafe_smbus_attach;
575 adap->client_unregister = cafe_smbus_detach;
576 adap->algo = &cafe_smbus_algo;
577 strcpy(adap->name, "cafe_ccic");
578 adap->dev.parent = &cam->pdev->dev;
579 i2c_set_adapdata(adap, cam);
580 ret = i2c_add_adapter(adap);
582 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
586 static void cafe_smbus_shutdown(struct cafe_camera *cam)
588 i2c_del_adapter(&cam->i2c_adapter);
592 /* ------------------------------------------------------------------- */
594 * Deal with the controller.
598 * Do everything we think we need to have the interface operating
599 * according to the desired format.
601 static void cafe_ctlr_dma(struct cafe_camera *cam)
604 * Store the first two Y buffers (we aren't supporting
605 * planar formats for now, so no UV bufs). Then either
606 * set the third if it exists, or tell the controller
609 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
610 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
611 if (cam->nbufs > 2) {
612 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
613 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
616 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
617 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
620 static void cafe_ctlr_image(struct cafe_camera *cam)
623 struct v4l2_pix_format *fmt = &cam->pix_format;
625 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
626 (fmt->bytesperline & IMGSZ_H_MASK);
627 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
628 cafe_reg_write(cam, REG_IMGOFFSET, 0);
629 /* YPITCH just drops the last two bits */
630 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
633 * Tell the controller about the image format we are using.
635 switch (cam->pix_format.pixelformat) {
636 case V4L2_PIX_FMT_YUYV:
637 cafe_reg_write_mask(cam, REG_CTRL0,
638 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
642 case V4L2_PIX_FMT_RGB444:
643 cafe_reg_write_mask(cam, REG_CTRL0,
644 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
649 case V4L2_PIX_FMT_RGB565:
650 cafe_reg_write_mask(cam, REG_CTRL0,
651 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
656 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
660 * Make sure it knows we want to use hsync/vsync.
662 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
668 * Configure the controller for operation; caller holds the
671 static int cafe_ctlr_configure(struct cafe_camera *cam)
675 spin_lock_irqsave(&cam->dev_lock, flags);
677 cafe_ctlr_image(cam);
678 cafe_set_config_needed(cam, 0);
679 spin_unlock_irqrestore(&cam->dev_lock, flags);
683 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
686 * Clear any pending interrupts, since we do not
687 * expect to have I/O active prior to enabling.
689 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
690 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
693 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
695 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
699 * Make the controller start grabbing images. Everything must
700 * be set up before doing this.
702 static void cafe_ctlr_start(struct cafe_camera *cam)
704 /* set_bit performs a read, so no other barrier should be
706 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
709 static void cafe_ctlr_stop(struct cafe_camera *cam)
711 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
714 static void cafe_ctlr_init(struct cafe_camera *cam)
718 spin_lock_irqsave(&cam->dev_lock, flags);
720 * Added magic to bring up the hardware on the B-Test board
722 cafe_reg_write(cam, 0x3038, 0x8);
723 cafe_reg_write(cam, 0x315c, 0x80008);
725 * Go through the dance needed to wake the device up.
726 * Note that these registers are global and shared
727 * with the NAND and SD devices. Interaction between the
728 * three still needs to be examined.
730 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
731 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
732 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
734 * Here we must wait a bit for the controller to come around.
736 spin_unlock_irqrestore(&cam->dev_lock, flags);
738 spin_lock_irqsave(&cam->dev_lock, flags);
740 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
741 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
743 * Make sure it's not powered down.
745 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
747 * Turn off the enable bit. It sure should be off anyway,
748 * but it's good to be sure.
750 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
752 * Mask all interrupts.
754 cafe_reg_write(cam, REG_IRQMASK, 0);
756 * Clock the sensor appropriately. Controller clock should
757 * be 48MHz, sensor "typical" value is half that.
759 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
760 spin_unlock_irqrestore(&cam->dev_lock, flags);
765 * Stop the controller, and don't return until we're really sure that no
766 * further DMA is going on.
768 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
773 * Theory: stop the camera controller (whether it is operating
774 * or not). Delay briefly just in case we race with the SOF
775 * interrupt, then wait until no DMA is active.
777 spin_lock_irqsave(&cam->dev_lock, flags);
779 spin_unlock_irqrestore(&cam->dev_lock, flags);
781 wait_event_timeout(cam->iowait,
782 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
783 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
784 cam_err(cam, "Timeout waiting for DMA to end\n");
785 /* This would be bad news - what now? */
786 spin_lock_irqsave(&cam->dev_lock, flags);
788 cafe_ctlr_irq_disable(cam);
789 spin_unlock_irqrestore(&cam->dev_lock, flags);
795 static void cafe_ctlr_power_up(struct cafe_camera *cam)
799 spin_lock_irqsave(&cam->dev_lock, flags);
800 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
802 * Part one of the sensor dance: turn the global
805 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
806 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
808 * Put the sensor into operational mode (assumes OLPC-style
809 * wiring). Control 0 is reset - set to 1 to operate.
810 * Control 1 is power down, set to 0 to operate.
812 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
813 // mdelay(1); /* Marvell says 1ms will do it */
814 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
815 // mdelay(1); /* Enough? */
816 spin_unlock_irqrestore(&cam->dev_lock, flags);
817 msleep(5); /* Just to be sure */
820 static void cafe_ctlr_power_down(struct cafe_camera *cam)
824 spin_lock_irqsave(&cam->dev_lock, flags);
825 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
826 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
827 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
828 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
829 spin_unlock_irqrestore(&cam->dev_lock, flags);
832 /* -------------------------------------------------------------------- */
834 * Communications with the sensor.
837 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
839 struct i2c_client *sc = cam->sensor;
842 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
844 ret = sc->driver->command(sc, cmd, arg);
845 if (ret == -EPERM) /* Unsupported command */
850 static int __cafe_cam_reset(struct cafe_camera *cam)
853 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
857 * We have found the sensor on the i2c. Let's try to have a
860 static int cafe_cam_init(struct cafe_camera *cam)
862 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
865 mutex_lock(&cam->s_mutex);
866 if (cam->state != S_NOTREADY)
867 cam_warn(cam, "Cam init with device in funky state %d",
869 ret = __cafe_cam_reset(cam);
872 chip.match_chip = cam->sensor->addr;
873 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
876 cam->sensor_type = chip.ident;
877 // if (cam->sensor->addr != OV7xx0_SID) {
878 if (cam->sensor_type != V4L2_IDENT_OV7670) {
879 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
883 /* Get/set parameters? */
887 cafe_ctlr_power_down(cam);
888 mutex_unlock(&cam->s_mutex);
893 * Configure the sensor to match the parameters we have. Caller should
896 static int cafe_cam_set_flip(struct cafe_camera *cam)
898 struct v4l2_control ctrl;
900 memset(&ctrl, 0, sizeof(ctrl));
901 ctrl.id = V4L2_CID_VFLIP;
903 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
907 static int cafe_cam_configure(struct cafe_camera *cam)
909 struct v4l2_format fmt;
912 if (cam->state != S_IDLE)
914 fmt.fmt.pix = cam->pix_format;
915 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
917 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
919 * OV7670 does weird things if flip is set *before* format...
921 ret += cafe_cam_set_flip(cam);
925 /* -------------------------------------------------------------------- */
927 * DMA buffer management. These functions need s_mutex held.
930 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
931 * does a get_free_pages() call, and we waste a good chunk of an orderN
932 * allocation. Should try to allocate the whole set in one chunk.
934 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
938 cafe_set_config_needed(cam, 1);
940 cam->dma_buf_size = dma_buf_size;
942 cam->dma_buf_size = cam->pix_format.sizeimage;
947 for (i = 0; i < n_dma_bufs; i++) {
948 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
949 cam->dma_buf_size, cam->dma_handles + i,
951 if (cam->dma_bufs[i] == NULL) {
952 cam_warn(cam, "Failed to allocate DMA buffer\n");
955 /* For debug, remove eventually */
956 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
960 switch (cam->nbufs) {
962 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
963 cam->dma_bufs[0], cam->dma_handles[0]);
966 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
971 cam_warn(cam, "Will limp along with only 2 buffers\n");
977 static void cafe_free_dma_bufs(struct cafe_camera *cam)
981 for (i = 0; i < cam->nbufs; i++) {
982 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
983 cam->dma_bufs[i], cam->dma_handles[i]);
984 cam->dma_bufs[i] = NULL;
993 /* ----------------------------------------------------------------------- */
995 * Here starts the V4L2 interface code.
999 * Read an image from the device.
1001 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
1002 char __user *buffer, size_t len, loff_t *pos)
1005 unsigned long flags;
1007 spin_lock_irqsave(&cam->dev_lock, flags);
1008 if (cam->next_buf < 0) {
1009 cam_err(cam, "deliver_buffer: No next buffer\n");
1010 spin_unlock_irqrestore(&cam->dev_lock, flags);
1013 bufno = cam->next_buf;
1014 clear_bit(bufno, &cam->flags);
1015 if (++(cam->next_buf) >= cam->nbufs)
1017 if (! test_bit(cam->next_buf, &cam->flags))
1019 cam->specframes = 0;
1020 spin_unlock_irqrestore(&cam->dev_lock, flags);
1022 if (len > cam->pix_format.sizeimage)
1023 len = cam->pix_format.sizeimage;
1024 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
1031 * Get everything ready, and start grabbing frames.
1033 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
1036 unsigned long flags;
1039 * Configuration. If we still don't have DMA buffers,
1040 * make one last, desperate attempt.
1042 if (cam->nbufs == 0)
1043 if (cafe_alloc_dma_bufs(cam, 0))
1046 if (cafe_needs_config(cam)) {
1047 cafe_cam_configure(cam);
1048 ret = cafe_ctlr_configure(cam);
1056 spin_lock_irqsave(&cam->dev_lock, flags);
1057 cafe_reset_buffers(cam);
1058 cafe_ctlr_irq_enable(cam);
1060 cafe_ctlr_start(cam);
1061 spin_unlock_irqrestore(&cam->dev_lock, flags);
1066 static ssize_t cafe_v4l_read(struct file *filp,
1067 char __user *buffer, size_t len, loff_t *pos)
1069 struct cafe_camera *cam = filp->private_data;
1073 * Perhaps we're in speculative read mode and already
1076 mutex_lock(&cam->s_mutex);
1077 if (cam->state == S_SPECREAD) {
1078 if (cam->next_buf >= 0) {
1079 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1083 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1086 } else if (cam->state != S_IDLE) {
1092 * v4l2: multiple processes can open the device, but only
1093 * one gets to grab data from it.
1095 if (cam->owner && cam->owner != filp) {
1102 * Do setup if need be.
1104 if (cam->state != S_SPECREAD) {
1105 ret = cafe_read_setup(cam, S_SINGLEREAD);
1110 * Wait for something to happen. This should probably
1111 * be interruptible (FIXME).
1113 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1114 if (cam->next_buf < 0) {
1115 cam_err(cam, "read() operation timed out\n");
1116 cafe_ctlr_stop_dma(cam);
1121 * Give them their data and we should be done.
1123 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1126 mutex_unlock(&cam->s_mutex);
1138 * Streaming I/O support.
1143 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1144 enum v4l2_buf_type type)
1146 struct cafe_camera *cam = filp->private_data;
1149 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1151 mutex_lock(&cam->s_mutex);
1152 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1156 ret = cafe_read_setup(cam, S_STREAMING);
1159 mutex_unlock(&cam->s_mutex);
1165 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1166 enum v4l2_buf_type type)
1168 struct cafe_camera *cam = filp->private_data;
1171 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1173 mutex_lock(&cam->s_mutex);
1174 if (cam->state != S_STREAMING)
1177 cafe_ctlr_stop_dma(cam);
1181 mutex_unlock(&cam->s_mutex);
1188 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1190 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1192 INIT_LIST_HEAD(&buf->list);
1193 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1194 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1195 if (buf->buffer == NULL)
1200 buf->v4lbuf.index = index;
1201 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1202 buf->v4lbuf.field = V4L2_FIELD_NONE;
1203 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1205 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1206 * just uses the length times the index, but the spec warns
1207 * against doing just that - vma merging problems. So we
1208 * leave a gap between each pair of buffers.
1210 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1214 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1219 * If any buffers are mapped, we cannot free them at all.
1221 for (i = 0; i < cam->n_sbufs; i++)
1222 if (cam->sb_bufs[i].mapcount > 0)
1227 for (i = 0; i < cam->n_sbufs; i++)
1228 vfree(cam->sb_bufs[i].buffer);
1230 kfree(cam->sb_bufs);
1231 cam->sb_bufs = NULL;
1232 INIT_LIST_HEAD(&cam->sb_avail);
1233 INIT_LIST_HEAD(&cam->sb_full);
1239 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1240 struct v4l2_requestbuffers *req)
1242 struct cafe_camera *cam = filp->private_data;
1243 int ret = 0; /* Silence warning */
1246 * Make sure it's something we can do. User pointers could be
1247 * implemented without great pain, but that's not been done yet.
1249 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1251 if (req->memory != V4L2_MEMORY_MMAP)
1254 * If they ask for zero buffers, they really want us to stop streaming
1255 * (if it's happening) and free everything. Should we check owner?
1257 mutex_lock(&cam->s_mutex);
1258 if (req->count == 0) {
1259 if (cam->state == S_STREAMING)
1260 cafe_ctlr_stop_dma(cam);
1261 ret = cafe_free_sio_buffers (cam);
1265 * Device needs to be idle and working. We *could* try to do the
1266 * right thing in S_SPECREAD by shutting things down, but it
1267 * probably doesn't matter.
1269 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1275 if (req->count < min_buffers)
1276 req->count = min_buffers;
1277 else if (req->count > max_buffers)
1278 req->count = max_buffers;
1279 if (cam->n_sbufs > 0) {
1280 ret = cafe_free_sio_buffers(cam);
1285 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1287 if (cam->sb_bufs == NULL) {
1291 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1292 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1297 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1298 kfree(cam->sb_bufs);
1299 req->count = cam->n_sbufs; /* In case of partial success */
1302 mutex_unlock(&cam->s_mutex);
1307 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1308 struct v4l2_buffer *buf)
1310 struct cafe_camera *cam = filp->private_data;
1313 mutex_lock(&cam->s_mutex);
1314 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1316 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1318 *buf = cam->sb_bufs[buf->index].v4lbuf;
1321 mutex_unlock(&cam->s_mutex);
1325 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1326 struct v4l2_buffer *buf)
1328 struct cafe_camera *cam = filp->private_data;
1329 struct cafe_sio_buffer *sbuf;
1331 unsigned long flags;
1333 mutex_lock(&cam->s_mutex);
1334 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1336 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1338 sbuf = cam->sb_bufs + buf->index;
1339 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1340 ret = 0; /* Already queued?? */
1343 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1344 /* Spec doesn't say anything, seems appropriate tho */
1348 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1349 spin_lock_irqsave(&cam->dev_lock, flags);
1350 list_add(&sbuf->list, &cam->sb_avail);
1351 spin_unlock_irqrestore(&cam->dev_lock, flags);
1354 mutex_unlock(&cam->s_mutex);
1358 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1359 struct v4l2_buffer *buf)
1361 struct cafe_camera *cam = filp->private_data;
1362 struct cafe_sio_buffer *sbuf;
1364 unsigned long flags;
1366 mutex_lock(&cam->s_mutex);
1367 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1369 if (cam->state != S_STREAMING)
1371 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1376 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1377 mutex_unlock(&cam->s_mutex);
1378 if (wait_event_interruptible(cam->iowait,
1379 !list_empty(&cam->sb_full))) {
1383 mutex_lock(&cam->s_mutex);
1386 if (cam->state != S_STREAMING)
1389 spin_lock_irqsave(&cam->dev_lock, flags);
1390 /* Should probably recheck !list_empty() here */
1391 sbuf = list_entry(cam->sb_full.next,
1392 struct cafe_sio_buffer, list);
1393 list_del_init(&sbuf->list);
1394 spin_unlock_irqrestore(&cam->dev_lock, flags);
1395 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1396 *buf = sbuf->v4lbuf;
1401 mutex_unlock(&cam->s_mutex);
1408 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1410 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1412 * Locking: done under mmap_sem, so we don't need to
1413 * go back to the camera lock here.
1419 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1421 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1423 mutex_lock(&sbuf->cam->s_mutex);
1425 /* Docs say we should stop I/O too... */
1426 if (sbuf->mapcount == 0)
1427 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1428 mutex_unlock(&sbuf->cam->s_mutex);
1431 static struct vm_operations_struct cafe_v4l_vm_ops = {
1432 .open = cafe_v4l_vm_open,
1433 .close = cafe_v4l_vm_close
1437 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1439 struct cafe_camera *cam = filp->private_data;
1440 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1443 struct cafe_sio_buffer *sbuf = NULL;
1445 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1448 * Find the buffer they are looking for.
1450 mutex_lock(&cam->s_mutex);
1451 for (i = 0; i < cam->n_sbufs; i++)
1452 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1453 sbuf = cam->sb_bufs + i;
1459 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1462 vma->vm_flags |= VM_DONTEXPAND;
1463 vma->vm_private_data = sbuf;
1464 vma->vm_ops = &cafe_v4l_vm_ops;
1465 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1466 cafe_v4l_vm_open(vma);
1469 mutex_unlock(&cam->s_mutex);
1475 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1477 struct cafe_camera *cam;
1480 cam = cafe_find_dev(iminor(inode));
1485 filp->private_data = cam;
1487 mutex_lock(&cam->s_mutex);
1488 if (cam->users == 0) {
1489 cafe_ctlr_power_up(cam);
1490 __cafe_cam_reset(cam);
1491 cafe_set_config_needed(cam, 1);
1492 /* FIXME make sure this is complete */
1495 mutex_unlock(&cam->s_mutex);
1501 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1503 struct cafe_camera *cam = filp->private_data;
1505 mutex_lock(&cam->s_mutex);
1507 if (filp == cam->owner) {
1508 cafe_ctlr_stop_dma(cam);
1509 cafe_free_sio_buffers(cam);
1512 if (cam->users == 0) {
1513 cafe_ctlr_power_down(cam);
1514 if (alloc_bufs_at_read)
1515 cafe_free_dma_bufs(cam);
1517 mutex_unlock(&cam->s_mutex);
1523 static unsigned int cafe_v4l_poll(struct file *filp,
1524 struct poll_table_struct *pt)
1526 struct cafe_camera *cam = filp->private_data;
1528 poll_wait(filp, &cam->iowait, pt);
1529 if (cam->next_buf >= 0)
1530 return POLLIN | POLLRDNORM;
1536 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1537 struct v4l2_queryctrl *qc)
1539 struct cafe_camera *cam = filp->private_data;
1542 mutex_lock(&cam->s_mutex);
1543 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1544 mutex_unlock(&cam->s_mutex);
1549 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1550 struct v4l2_control *ctrl)
1552 struct cafe_camera *cam = filp->private_data;
1555 mutex_lock(&cam->s_mutex);
1556 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1557 mutex_unlock(&cam->s_mutex);
1562 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1563 struct v4l2_control *ctrl)
1565 struct cafe_camera *cam = filp->private_data;
1568 mutex_lock(&cam->s_mutex);
1569 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1570 mutex_unlock(&cam->s_mutex);
1578 static int cafe_vidioc_querycap(struct file *file, void *priv,
1579 struct v4l2_capability *cap)
1581 strcpy(cap->driver, "cafe_ccic");
1582 strcpy(cap->card, "cafe_ccic");
1583 cap->version = CAFE_VERSION;
1584 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1585 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1591 * The default format we use until somebody says otherwise.
1593 static struct v4l2_pix_format cafe_def_pix_format = {
1595 .height = VGA_HEIGHT,
1596 .pixelformat = V4L2_PIX_FMT_YUYV,
1597 .field = V4L2_FIELD_NONE,
1598 .bytesperline = VGA_WIDTH*2,
1599 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1602 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1603 void *priv, struct v4l2_fmtdesc *fmt)
1605 struct cafe_camera *cam = priv;
1608 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1610 mutex_lock(&cam->s_mutex);
1611 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1612 mutex_unlock(&cam->s_mutex);
1617 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1618 struct v4l2_format *fmt)
1620 struct cafe_camera *cam = priv;
1623 mutex_lock(&cam->s_mutex);
1624 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1625 mutex_unlock(&cam->s_mutex);
1629 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1630 struct v4l2_format *fmt)
1632 struct cafe_camera *cam = priv;
1636 * Can't do anything if the device is not idle
1637 * Also can't if there are streaming buffers in place.
1639 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1642 * See if the formatting works in principle.
1644 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1648 * Now we start to change things for real, so let's do it
1651 mutex_lock(&cam->s_mutex);
1652 cam->pix_format = fmt->fmt.pix;
1654 * Make sure we have appropriate DMA buffers.
1657 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1658 cafe_free_dma_bufs(cam);
1659 if (cam->nbufs == 0) {
1660 if (cafe_alloc_dma_bufs(cam, 0))
1664 * It looks like this might work, so let's program the sensor.
1666 ret = cafe_cam_configure(cam);
1668 ret = cafe_ctlr_configure(cam);
1670 mutex_unlock(&cam->s_mutex);
1675 * Return our stored notion of how the camera is/should be configured.
1676 * The V4l2 spec wants us to be smarter, and actually get this from
1677 * the camera (and not mess with it at open time). Someday.
1679 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1680 struct v4l2_format *f)
1682 struct cafe_camera *cam = priv;
1684 f->fmt.pix = cam->pix_format;
1689 * We only have one input - the sensor - so minimize the nonsense here.
1691 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1692 struct v4l2_input *input)
1694 if (input->index != 0)
1697 input->type = V4L2_INPUT_TYPE_CAMERA;
1698 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1699 strcpy(input->name, "Camera");
1703 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1709 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1717 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1723 * G/S_PARM. Most of this is done by the sensor, but we are
1724 * the level which controls the number of read buffers.
1726 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1727 struct v4l2_streamparm *parms)
1729 struct cafe_camera *cam = priv;
1732 mutex_lock(&cam->s_mutex);
1733 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1734 mutex_unlock(&cam->s_mutex);
1735 parms->parm.capture.readbuffers = n_dma_bufs;
1739 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1740 struct v4l2_streamparm *parms)
1742 struct cafe_camera *cam = priv;
1745 mutex_lock(&cam->s_mutex);
1746 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1747 mutex_unlock(&cam->s_mutex);
1748 parms->parm.capture.readbuffers = n_dma_bufs;
1753 static void cafe_v4l_dev_release(struct video_device *vd)
1755 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1762 * This template device holds all of those v4l2 methods; we
1763 * clone it for specific real devices.
1766 static const struct file_operations cafe_v4l_fops = {
1767 .owner = THIS_MODULE,
1768 .open = cafe_v4l_open,
1769 .release = cafe_v4l_release,
1770 .read = cafe_v4l_read,
1771 .poll = cafe_v4l_poll,
1772 .mmap = cafe_v4l_mmap,
1773 .ioctl = video_ioctl2,
1774 .llseek = no_llseek,
1777 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1778 .vidioc_querycap = cafe_vidioc_querycap,
1779 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1780 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1781 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1782 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1783 .vidioc_enum_input = cafe_vidioc_enum_input,
1784 .vidioc_g_input = cafe_vidioc_g_input,
1785 .vidioc_s_input = cafe_vidioc_s_input,
1786 .vidioc_s_std = cafe_vidioc_s_std,
1787 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1788 .vidioc_querybuf = cafe_vidioc_querybuf,
1789 .vidioc_qbuf = cafe_vidioc_qbuf,
1790 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1791 .vidioc_streamon = cafe_vidioc_streamon,
1792 .vidioc_streamoff = cafe_vidioc_streamoff,
1793 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1794 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1795 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1796 .vidioc_g_parm = cafe_vidioc_g_parm,
1797 .vidioc_s_parm = cafe_vidioc_s_parm,
1800 static struct video_device cafe_v4l_template = {
1802 .minor = -1, /* Get one dynamically */
1803 .tvnorms = V4L2_STD_NTSC_M,
1804 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1806 .fops = &cafe_v4l_fops,
1807 .ioctl_ops = &cafe_v4l_ioctl_ops,
1808 .release = cafe_v4l_dev_release,
1817 /* ---------------------------------------------------------------------- */
1819 * Interrupt handler stuff
1824 static void cafe_frame_tasklet(unsigned long data)
1826 struct cafe_camera *cam = (struct cafe_camera *) data;
1828 unsigned long flags;
1829 struct cafe_sio_buffer *sbuf;
1831 spin_lock_irqsave(&cam->dev_lock, flags);
1832 for (i = 0; i < cam->nbufs; i++) {
1833 int bufno = cam->next_buf;
1834 if (bufno < 0) { /* "will never happen" */
1835 cam_err(cam, "No valid bufs in tasklet!\n");
1838 if (++(cam->next_buf) >= cam->nbufs)
1840 if (! test_bit(bufno, &cam->flags))
1842 if (list_empty(&cam->sb_avail))
1843 break; /* Leave it valid, hope for better later */
1844 clear_bit(bufno, &cam->flags);
1845 sbuf = list_entry(cam->sb_avail.next,
1846 struct cafe_sio_buffer, list);
1848 * Drop the lock during the big copy. This *should* be safe...
1850 spin_unlock_irqrestore(&cam->dev_lock, flags);
1851 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1852 cam->pix_format.sizeimage);
1853 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1854 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1855 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1856 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1857 spin_lock_irqsave(&cam->dev_lock, flags);
1858 list_move_tail(&sbuf->list, &cam->sb_full);
1860 if (! list_empty(&cam->sb_full))
1861 wake_up(&cam->iowait);
1862 spin_unlock_irqrestore(&cam->dev_lock, flags);
1867 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1870 * Basic frame housekeeping.
1872 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1873 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1874 set_bit(frame, &cam->flags);
1875 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1876 if (cam->next_buf < 0)
1877 cam->next_buf = frame;
1878 cam->buf_seq[frame] = ++(cam->sequence);
1880 switch (cam->state) {
1882 * If in single read mode, try going speculative.
1885 cam->state = S_SPECREAD;
1886 cam->specframes = 0;
1887 wake_up(&cam->iowait);
1891 * If we are already doing speculative reads, and nobody is
1892 * reading them, just stop.
1895 if (++(cam->specframes) >= cam->nbufs) {
1896 cafe_ctlr_stop(cam);
1897 cafe_ctlr_irq_disable(cam);
1898 cam->state = S_IDLE;
1900 wake_up(&cam->iowait);
1903 * For the streaming case, we defer the real work to the
1906 * FIXME: if the application is not consuming the buffers,
1907 * we should eventually put things on hold and restart in
1911 tasklet_schedule(&cam->s_tasklet);
1915 cam_err(cam, "Frame interrupt in non-operational state\n");
1923 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1927 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1929 * Handle any frame completions. There really should
1930 * not be more than one of these, or we have fallen
1933 for (frame = 0; frame < cam->nbufs; frame++)
1934 if (irqs & (IRQ_EOF0 << frame))
1935 cafe_frame_complete(cam, frame);
1937 * If a frame starts, note that we have DMA active. This
1938 * code assumes that we won't get multiple frame interrupts
1939 * at once; may want to rethink that.
1941 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1942 set_bit(CF_DMA_ACTIVE, &cam->flags);
1947 static irqreturn_t cafe_irq(int irq, void *data)
1949 struct cafe_camera *cam = data;
1952 spin_lock(&cam->dev_lock);
1953 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1954 if ((irqs & ALLIRQS) == 0) {
1955 spin_unlock(&cam->dev_lock);
1958 if (irqs & FRAMEIRQS)
1959 cafe_frame_irq(cam, irqs);
1960 if (irqs & TWSIIRQS) {
1961 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1962 wake_up(&cam->smbus_wait);
1964 spin_unlock(&cam->dev_lock);
1969 /* -------------------------------------------------------------------------- */
1970 #ifdef CONFIG_VIDEO_ADV_DEBUG
1975 static char cafe_debug_buf[1024];
1976 static struct dentry *cafe_dfs_root;
1978 static void cafe_dfs_setup(void)
1980 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1981 if (IS_ERR(cafe_dfs_root)) {
1982 cafe_dfs_root = NULL; /* Never mind */
1983 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1987 static void cafe_dfs_shutdown(void)
1990 debugfs_remove(cafe_dfs_root);
1993 static int cafe_dfs_open(struct inode *inode, struct file *file)
1995 file->private_data = inode->i_private;
1999 static ssize_t cafe_dfs_read_regs(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;
2006 for (offset = 0; offset < 0x44; offset += 4)
2007 s += sprintf(s, "%02x: %08x\n", offset,
2008 cafe_reg_read(cam, offset));
2009 for (offset = 0x88; offset <= 0x90; offset += 4)
2010 s += sprintf(s, "%02x: %08x\n", offset,
2011 cafe_reg_read(cam, offset));
2012 for (offset = 0xb4; offset <= 0xbc; offset += 4)
2013 s += sprintf(s, "%02x: %08x\n", offset,
2014 cafe_reg_read(cam, offset));
2015 for (offset = 0x3000; offset <= 0x300c; offset += 4)
2016 s += sprintf(s, "%04x: %08x\n", offset,
2017 cafe_reg_read(cam, offset));
2018 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2019 s - cafe_debug_buf);
2022 static const struct file_operations cafe_dfs_reg_ops = {
2023 .owner = THIS_MODULE,
2024 .read = cafe_dfs_read_regs,
2025 .open = cafe_dfs_open
2028 static ssize_t cafe_dfs_read_cam(struct file *file,
2029 char __user *buf, size_t count, loff_t *ppos)
2031 struct cafe_camera *cam = file->private_data;
2032 char *s = cafe_debug_buf;
2037 for (offset = 0x0; offset < 0x8a; offset++)
2041 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2042 s += sprintf(s, "%02x: %02x\n", offset, v);
2044 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2045 s - cafe_debug_buf);
2048 static const struct file_operations cafe_dfs_cam_ops = {
2049 .owner = THIS_MODULE,
2050 .read = cafe_dfs_read_cam,
2051 .open = cafe_dfs_open
2056 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2062 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2063 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2064 cam, &cafe_dfs_reg_ops);
2065 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2066 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2067 cam, &cafe_dfs_cam_ops);
2071 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2073 if (! IS_ERR(cam->dfs_regs))
2074 debugfs_remove(cam->dfs_regs);
2075 if (! IS_ERR(cam->dfs_cam_regs))
2076 debugfs_remove(cam->dfs_cam_regs);
2081 #define cafe_dfs_setup()
2082 #define cafe_dfs_shutdown()
2083 #define cafe_dfs_cam_setup(cam)
2084 #define cafe_dfs_cam_shutdown(cam)
2085 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2090 /* ------------------------------------------------------------------------*/
2092 * PCI interface stuff.
2095 static int cafe_pci_probe(struct pci_dev *pdev,
2096 const struct pci_device_id *id)
2100 struct cafe_camera *cam;
2102 * Make sure we have a camera here - we'll get calls for
2103 * the other cafe devices as well.
2105 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2106 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2109 * Start putting together one of our big camera structures.
2112 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2115 mutex_init(&cam->s_mutex);
2116 mutex_lock(&cam->s_mutex);
2117 spin_lock_init(&cam->dev_lock);
2118 cam->state = S_NOTREADY;
2119 cafe_set_config_needed(cam, 1);
2120 init_waitqueue_head(&cam->smbus_wait);
2121 init_waitqueue_head(&cam->iowait);
2123 cam->pix_format = cafe_def_pix_format;
2124 INIT_LIST_HEAD(&cam->dev_list);
2125 INIT_LIST_HEAD(&cam->sb_avail);
2126 INIT_LIST_HEAD(&cam->sb_full);
2127 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2129 * Get set up on the PCI bus.
2131 ret = pci_enable_device(pdev);
2134 pci_set_master(pdev);
2137 cam->regs = pci_iomap(pdev, 0, 0);
2139 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2142 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2146 * Initialize the controller and leave it powered up. It will
2147 * stay that way until the sensor driver shows up.
2149 cafe_ctlr_init(cam);
2150 cafe_ctlr_power_up(cam);
2152 * Set up I2C/SMBUS communications. We have to drop the mutex here
2153 * because the sensor could attach in this call chain, leading to
2154 * unsightly deadlocks.
2156 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2157 ret = cafe_smbus_setup(cam);
2161 * Get the v4l2 setup done.
2163 mutex_lock(&cam->s_mutex);
2164 cam->v4ldev = cafe_v4l_template;
2165 cam->v4ldev.debug = 0;
2166 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2167 cam->v4ldev.parent = &pdev->dev;
2168 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2172 * If so requested, try to get our DMA buffers now.
2174 if (!alloc_bufs_at_read) {
2175 if (cafe_alloc_dma_bufs(cam, 1))
2176 cam_warn(cam, "Unable to alloc DMA buffers at load"
2177 " will try again later.");
2180 cafe_dfs_cam_setup(cam);
2181 mutex_unlock(&cam->s_mutex);
2186 cafe_smbus_shutdown(cam);
2188 cafe_ctlr_power_down(cam);
2189 free_irq(pdev->irq, cam);
2191 pci_iounmap(pdev, cam->regs);
2200 * Shut down an initialized device
2202 static void cafe_shutdown(struct cafe_camera *cam)
2204 /* FIXME: Make sure we take care of everything here */
2205 cafe_dfs_cam_shutdown(cam);
2206 if (cam->n_sbufs > 0)
2207 /* What if they are still mapped? Shouldn't be, but... */
2208 cafe_free_sio_buffers(cam);
2209 cafe_remove_dev(cam);
2210 cafe_ctlr_stop_dma(cam);
2211 cafe_ctlr_power_down(cam);
2212 cafe_smbus_shutdown(cam);
2213 cafe_free_dma_bufs(cam);
2214 free_irq(cam->pdev->irq, cam);
2215 pci_iounmap(cam->pdev, cam->regs);
2216 video_unregister_device(&cam->v4ldev);
2217 /* kfree(cam); done in v4l_release () */
2221 static void cafe_pci_remove(struct pci_dev *pdev)
2223 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2226 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2229 mutex_lock(&cam->s_mutex);
2231 cam_warn(cam, "Removing a device with users!\n");
2233 /* No unlock - it no longer exists */
2239 * Basic power management.
2241 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2243 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2245 enum cafe_state cstate;
2247 ret = pci_save_state(pdev);
2250 cstate = cam->state; /* HACK - stop_dma sets to idle */
2251 cafe_ctlr_stop_dma(cam);
2252 cafe_ctlr_power_down(cam);
2253 pci_disable_device(pdev);
2254 cam->state = cstate;
2259 static int cafe_pci_resume(struct pci_dev *pdev)
2261 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2264 ret = pci_restore_state(pdev);
2267 ret = pci_enable_device(pdev);
2270 cam_warn(cam, "Unable to re-enable device on resume!\n");
2273 cafe_ctlr_init(cam);
2274 cafe_ctlr_power_down(cam);
2276 mutex_lock(&cam->s_mutex);
2277 if (cam->users > 0) {
2278 cafe_ctlr_power_up(cam);
2279 __cafe_cam_reset(cam);
2281 mutex_unlock(&cam->s_mutex);
2283 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2284 if (cam->state == S_SPECREAD)
2285 cam->state = S_IDLE; /* Don't bother restarting */
2286 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2287 ret = cafe_read_setup(cam, cam->state);
2291 #endif /* CONFIG_PM */
2294 static struct pci_device_id cafe_ids[] = {
2295 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2296 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2300 MODULE_DEVICE_TABLE(pci, cafe_ids);
2302 static struct pci_driver cafe_pci_driver = {
2303 .name = "cafe1000-ccic",
2304 .id_table = cafe_ids,
2305 .probe = cafe_pci_probe,
2306 .remove = cafe_pci_remove,
2308 .suspend = cafe_pci_suspend,
2309 .resume = cafe_pci_resume,
2316 static int __init cafe_init(void)
2320 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2323 ret = pci_register_driver(&cafe_pci_driver);
2325 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2328 request_module("ov7670"); /* FIXME want something more general */
2336 static void __exit cafe_exit(void)
2338 pci_unregister_driver(&cafe_pci_driver);
2339 cafe_dfs_shutdown();
2342 module_init(cafe_init);
2343 module_exit(cafe_exit);