2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
6 * The data sheet for this device can be found at:
7 * http://www.marvell.com/products/pcconn/88ALP01.jsp
9 * Copyright 2006 One Laptop Per Child Association, Inc.
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
12 * Written by Jonathan Corbet, corbet@lwn.net.
14 * This file may be distributed under the terms of the GNU General
15 * Public License, version 2.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <linux/device.h>
32 #include <linux/wait.h>
33 #include <linux/list.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/debugfs.h>
37 #include <linux/jiffies.h>
38 #include <linux/vmalloc.h>
40 #include <asm/uaccess.h>
43 #include "cafe_ccic-regs.h"
45 #define CAFE_VERSION 0x000002
51 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
52 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
53 MODULE_LICENSE("GPL");
54 MODULE_SUPPORTED_DEVICE("Video");
57 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
58 * we must have physically contiguous buffers to bring frames into.
59 * These parameters control how many buffers we use, whether we
60 * allocate them at load time (better chance of success, but nails down
61 * memory) or when somebody tries to use the camera (riskier), and,
62 * for load-time allocation, how big they should be.
64 * The controller can cycle through three buffers. We could use
65 * more by flipping pointers around, but it probably makes little
69 #define MAX_DMA_BUFS 3
70 static int alloc_bufs_at_read;
71 module_param(alloc_bufs_at_read, bool, 0444);
72 MODULE_PARM_DESC(alloc_bufs_at_read,
73 "Non-zero value causes DMA buffers to be allocated when the "
74 "video capture device is read, rather than at module load "
75 "time. This saves memory, but decreases the chances of "
76 "successfully getting those buffers.");
78 static int n_dma_bufs = 3;
79 module_param(n_dma_bufs, uint, 0644);
80 MODULE_PARM_DESC(n_dma_bufs,
81 "The number of DMA buffers to allocate. Can be either two "
82 "(saves memory, makes timing tighter) or three.");
84 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
85 module_param(dma_buf_size, uint, 0444);
86 MODULE_PARM_DESC(dma_buf_size,
87 "The size of the allocated DMA buffers. If actual operating "
88 "parameters require larger buffers, an attempt to reallocate "
91 static int min_buffers = 1;
92 module_param(min_buffers, uint, 0644);
93 MODULE_PARM_DESC(min_buffers,
94 "The minimum number of streaming I/O buffers we are willing "
97 static int max_buffers = 10;
98 module_param(max_buffers, uint, 0644);
99 MODULE_PARM_DESC(max_buffers,
100 "The maximum number of streaming I/O buffers an application "
101 "will be allowed to allocate. These buffers are big and live "
102 "in vmalloc space.");
105 module_param(flip, bool, 0444);
106 MODULE_PARM_DESC(flip,
107 "If set, the sensor will be instructed to flip the image "
112 S_NOTREADY, /* Not yet initialized */
113 S_IDLE, /* Just hanging around */
114 S_FLAKED, /* Some sort of problem */
115 S_SINGLEREAD, /* In read() */
116 S_SPECREAD, /* Speculative read (for future read()) */
117 S_STREAMING /* Streaming data */
121 * Tracking of streaming I/O buffers.
123 struct cafe_sio_buffer {
124 struct list_head list;
125 struct v4l2_buffer v4lbuf;
126 char *buffer; /* Where it lives in kernel space */
128 struct cafe_camera *cam;
132 * A description of one of our devices.
133 * Locking: controlled by s_mutex. Certain fields, however, require
134 * the dev_lock spinlock; they are marked as such by comments.
135 * dev_lock is also required for access to device registers.
139 struct v4l2_device v4l2_dev;
140 enum cafe_state state;
141 unsigned long flags; /* Buffer status, mainly (dev_lock) */
142 int users; /* How many open FDs */
143 struct file *owner; /* Who has data access (v4l2) */
146 * Subsystem structures.
148 struct pci_dev *pdev;
149 struct video_device vdev;
150 struct i2c_adapter i2c_adapter;
151 struct i2c_client *sensor;
153 unsigned char __iomem *regs;
154 struct list_head dev_list; /* link to other devices */
157 unsigned int nbufs; /* How many are alloc'd */
158 int next_buf; /* Next to consume (dev_lock) */
159 unsigned int dma_buf_size; /* allocated size */
160 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
161 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
162 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
163 unsigned int sequence; /* Frame sequence number */
164 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
166 /* Streaming buffers */
167 unsigned int n_sbufs; /* How many we have */
168 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
169 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
170 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
171 struct tasklet_struct s_tasklet;
173 /* Current operating parameters */
174 u32 sensor_type; /* Currently ov7670 only */
175 struct v4l2_pix_format pix_format;
178 struct mutex s_mutex; /* Access to this structure */
179 spinlock_t dev_lock; /* Access to device */
182 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
183 wait_queue_head_t iowait; /* Waiting on frame data */
184 #ifdef CONFIG_VIDEO_ADV_DEBUG
185 struct dentry *dfs_regs;
186 struct dentry *dfs_cam_regs;
191 * Status flags. Always manipulated with bit operations.
193 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
194 #define CF_BUF1_VALID 1
195 #define CF_BUF2_VALID 2
196 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
197 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
200 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
202 return container_of(dev, struct cafe_camera, v4l2_dev);
207 * Start over with DMA buffers - dev_lock needed.
209 static void cafe_reset_buffers(struct cafe_camera *cam)
214 for (i = 0; i < cam->nbufs; i++)
215 clear_bit(i, &cam->flags);
219 static inline int cafe_needs_config(struct cafe_camera *cam)
221 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
224 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
227 set_bit(CF_CONFIG_NEEDED, &cam->flags);
229 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
236 * Debugging and related.
238 #define cam_err(cam, fmt, arg...) \
239 dev_err(&(cam)->pdev->dev, fmt, ##arg);
240 #define cam_warn(cam, fmt, arg...) \
241 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
242 #define cam_dbg(cam, fmt, arg...) \
243 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
246 /* ---------------------------------------------------------------------*/
249 * Device register I/O
251 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
254 iowrite32(val, cam->regs + reg);
257 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
260 return ioread32(cam->regs + reg);
264 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
265 unsigned int val, unsigned int mask)
267 unsigned int v = cafe_reg_read(cam, reg);
269 v = (v & ~mask) | (val & mask);
270 cafe_reg_write(cam, reg, v);
273 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
274 unsigned int reg, unsigned int val)
276 cafe_reg_write_mask(cam, reg, 0, val);
279 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
280 unsigned int reg, unsigned int val)
282 cafe_reg_write_mask(cam, reg, val, val);
287 /* -------------------------------------------------------------------- */
289 * The I2C/SMBUS interface to the camera itself starts here. The
290 * controller handles SMBUS itself, presenting a relatively simple register
291 * interface; all we have to do is to tell it where to route the data.
293 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
295 static int cafe_smbus_write_done(struct cafe_camera *cam)
301 * We must delay after the interrupt, or the controller gets confused
302 * and never does give us good status. Fortunately, we don't do this
306 spin_lock_irqsave(&cam->dev_lock, flags);
307 c1 = cafe_reg_read(cam, REG_TWSIC1);
308 spin_unlock_irqrestore(&cam->dev_lock, flags);
309 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
312 static int cafe_smbus_write_data(struct cafe_camera *cam,
313 u16 addr, u8 command, u8 value)
317 DEFINE_WAIT(the_wait);
319 spin_lock_irqsave(&cam->dev_lock, flags);
320 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
321 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
323 * Marvell sez set clkdiv to all 1's for now.
325 rval |= TWSIC0_CLKDIV;
326 cafe_reg_write(cam, REG_TWSIC0, rval);
327 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
328 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
329 cafe_reg_write(cam, REG_TWSIC1, rval);
330 spin_unlock_irqrestore(&cam->dev_lock, flags);
333 * Time to wait for the write to complete. THIS IS A RACY
334 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
335 * register too quickly after starting the operation sends
336 * the device into a place that may be kinder and better, but
337 * which is absolutely useless for controlling the sensor. In
338 * practice we have plenty of time to get into our sleep state
339 * before the interrupt hits, and the worst case is that we
340 * time out and then see that things completed, so this seems
341 * the best way for now.
344 prepare_to_wait(&cam->smbus_wait, &the_wait,
345 TASK_UNINTERRUPTIBLE);
346 schedule_timeout(1); /* even 1 jiffy is too long */
347 finish_wait(&cam->smbus_wait, &the_wait);
348 } while (!cafe_smbus_write_done(cam));
350 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
351 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
354 spin_lock_irqsave(&cam->dev_lock, flags);
355 rval = cafe_reg_read(cam, REG_TWSIC1);
356 spin_unlock_irqrestore(&cam->dev_lock, flags);
358 if (rval & TWSIC1_WSTAT) {
359 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
363 if (rval & TWSIC1_ERROR) {
364 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
373 static int cafe_smbus_read_done(struct cafe_camera *cam)
379 * We must delay after the interrupt, or the controller gets confused
380 * and never does give us good status. Fortunately, we don't do this
384 spin_lock_irqsave(&cam->dev_lock, flags);
385 c1 = cafe_reg_read(cam, REG_TWSIC1);
386 spin_unlock_irqrestore(&cam->dev_lock, flags);
387 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
392 static int cafe_smbus_read_data(struct cafe_camera *cam,
393 u16 addr, u8 command, u8 *value)
398 spin_lock_irqsave(&cam->dev_lock, flags);
399 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
400 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
402 * Marvel sez set clkdiv to all 1's for now.
404 rval |= TWSIC0_CLKDIV;
405 cafe_reg_write(cam, REG_TWSIC0, rval);
406 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
407 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
408 cafe_reg_write(cam, REG_TWSIC1, rval);
409 spin_unlock_irqrestore(&cam->dev_lock, flags);
411 wait_event_timeout(cam->smbus_wait,
412 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
413 spin_lock_irqsave(&cam->dev_lock, flags);
414 rval = cafe_reg_read(cam, REG_TWSIC1);
415 spin_unlock_irqrestore(&cam->dev_lock, flags);
417 if (rval & TWSIC1_ERROR) {
418 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
421 if (! (rval & TWSIC1_RVALID)) {
422 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
426 *value = rval & 0xff;
431 * Perform a transfer over SMBUS. This thing is called under
432 * the i2c bus lock, so we shouldn't race with ourselves...
434 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
435 unsigned short flags, char rw, u8 command,
436 int size, union i2c_smbus_data *data)
438 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
439 struct cafe_camera *cam = to_cam(v4l2_dev);
443 * Refuse to talk to anything but OV cam chips. We should
444 * never even see an attempt to do so, but one never knows.
446 if (cam->sensor && addr != cam->sensor->addr) {
447 cam_err(cam, "funky smbus addr %d\n", addr);
451 * This interface would appear to only do byte data ops. OK
452 * it can do word too, but the cam chip has no use for that.
454 if (size != I2C_SMBUS_BYTE_DATA) {
455 cam_err(cam, "funky xfer size %d\n", size);
459 if (rw == I2C_SMBUS_WRITE)
460 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
461 else if (rw == I2C_SMBUS_READ)
462 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
467 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
471 spin_lock_irqsave(&cam->dev_lock, flags);
472 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
473 spin_unlock_irqrestore(&cam->dev_lock, flags);
476 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
478 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
479 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
482 static struct i2c_algorithm cafe_smbus_algo = {
483 .smbus_xfer = cafe_smbus_xfer,
484 .functionality = cafe_smbus_func
487 /* Somebody is on the bus */
488 static int cafe_cam_init(struct cafe_camera *cam);
489 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
490 static void cafe_ctlr_power_down(struct cafe_camera *cam);
492 static int cafe_smbus_attach(struct i2c_client *client)
494 struct v4l2_device *v4l2_dev = i2c_get_adapdata(client->adapter);
495 struct cafe_camera *cam = to_cam(v4l2_dev);
498 * Don't talk to chips we don't recognize.
500 if (client->driver->id == I2C_DRIVERID_OV7670) {
501 cam->sensor = client;
502 return cafe_cam_init(cam);
507 static int cafe_smbus_detach(struct i2c_client *client)
509 struct v4l2_device *v4l2_dev = i2c_get_adapdata(client->adapter);
510 struct cafe_camera *cam = to_cam(v4l2_dev);
512 if (cam->sensor == client) {
513 cafe_ctlr_stop_dma(cam);
514 cafe_ctlr_power_down(cam);
515 cam_err(cam, "lost the sensor!\n");
516 cam->sensor = NULL; /* Bummer, no camera */
517 cam->state = S_NOTREADY;
522 static int cafe_smbus_setup(struct cafe_camera *cam)
524 struct i2c_adapter *adap = &cam->i2c_adapter;
527 cafe_smbus_enable_irq(cam);
528 adap->id = I2C_HW_SMBUS_CAFE;
529 adap->owner = THIS_MODULE;
530 adap->client_register = cafe_smbus_attach;
531 adap->client_unregister = cafe_smbus_detach;
532 adap->algo = &cafe_smbus_algo;
533 strcpy(adap->name, "cafe_ccic");
534 adap->dev.parent = &cam->pdev->dev;
535 i2c_set_adapdata(adap, &cam->v4l2_dev);
536 ret = i2c_add_adapter(adap);
538 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
542 static void cafe_smbus_shutdown(struct cafe_camera *cam)
544 i2c_del_adapter(&cam->i2c_adapter);
548 /* ------------------------------------------------------------------- */
550 * Deal with the controller.
554 * Do everything we think we need to have the interface operating
555 * according to the desired format.
557 static void cafe_ctlr_dma(struct cafe_camera *cam)
560 * Store the first two Y buffers (we aren't supporting
561 * planar formats for now, so no UV bufs). Then either
562 * set the third if it exists, or tell the controller
565 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
566 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
567 if (cam->nbufs > 2) {
568 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
569 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
572 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
573 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
576 static void cafe_ctlr_image(struct cafe_camera *cam)
579 struct v4l2_pix_format *fmt = &cam->pix_format;
581 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
582 (fmt->bytesperline & IMGSZ_H_MASK);
583 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
584 cafe_reg_write(cam, REG_IMGOFFSET, 0);
585 /* YPITCH just drops the last two bits */
586 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
589 * Tell the controller about the image format we are using.
591 switch (cam->pix_format.pixelformat) {
592 case V4L2_PIX_FMT_YUYV:
593 cafe_reg_write_mask(cam, REG_CTRL0,
594 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
598 case V4L2_PIX_FMT_RGB444:
599 cafe_reg_write_mask(cam, REG_CTRL0,
600 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
605 case V4L2_PIX_FMT_RGB565:
606 cafe_reg_write_mask(cam, REG_CTRL0,
607 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
612 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
616 * Make sure it knows we want to use hsync/vsync.
618 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
624 * Configure the controller for operation; caller holds the
627 static int cafe_ctlr_configure(struct cafe_camera *cam)
631 spin_lock_irqsave(&cam->dev_lock, flags);
633 cafe_ctlr_image(cam);
634 cafe_set_config_needed(cam, 0);
635 spin_unlock_irqrestore(&cam->dev_lock, flags);
639 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
642 * Clear any pending interrupts, since we do not
643 * expect to have I/O active prior to enabling.
645 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
646 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
649 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
651 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
655 * Make the controller start grabbing images. Everything must
656 * be set up before doing this.
658 static void cafe_ctlr_start(struct cafe_camera *cam)
660 /* set_bit performs a read, so no other barrier should be
662 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
665 static void cafe_ctlr_stop(struct cafe_camera *cam)
667 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
670 static void cafe_ctlr_init(struct cafe_camera *cam)
674 spin_lock_irqsave(&cam->dev_lock, flags);
676 * Added magic to bring up the hardware on the B-Test board
678 cafe_reg_write(cam, 0x3038, 0x8);
679 cafe_reg_write(cam, 0x315c, 0x80008);
681 * Go through the dance needed to wake the device up.
682 * Note that these registers are global and shared
683 * with the NAND and SD devices. Interaction between the
684 * three still needs to be examined.
686 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
687 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
688 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
690 * Here we must wait a bit for the controller to come around.
692 spin_unlock_irqrestore(&cam->dev_lock, flags);
694 spin_lock_irqsave(&cam->dev_lock, flags);
696 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
697 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
699 * Make sure it's not powered down.
701 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
703 * Turn off the enable bit. It sure should be off anyway,
704 * but it's good to be sure.
706 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
708 * Mask all interrupts.
710 cafe_reg_write(cam, REG_IRQMASK, 0);
712 * Clock the sensor appropriately. Controller clock should
713 * be 48MHz, sensor "typical" value is half that.
715 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
716 spin_unlock_irqrestore(&cam->dev_lock, flags);
721 * Stop the controller, and don't return until we're really sure that no
722 * further DMA is going on.
724 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
729 * Theory: stop the camera controller (whether it is operating
730 * or not). Delay briefly just in case we race with the SOF
731 * interrupt, then wait until no DMA is active.
733 spin_lock_irqsave(&cam->dev_lock, flags);
735 spin_unlock_irqrestore(&cam->dev_lock, flags);
737 wait_event_timeout(cam->iowait,
738 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
739 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
740 cam_err(cam, "Timeout waiting for DMA to end\n");
741 /* This would be bad news - what now? */
742 spin_lock_irqsave(&cam->dev_lock, flags);
744 cafe_ctlr_irq_disable(cam);
745 spin_unlock_irqrestore(&cam->dev_lock, flags);
751 static void cafe_ctlr_power_up(struct cafe_camera *cam)
755 spin_lock_irqsave(&cam->dev_lock, flags);
756 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
758 * Part one of the sensor dance: turn the global
761 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
762 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
764 * Put the sensor into operational mode (assumes OLPC-style
765 * wiring). Control 0 is reset - set to 1 to operate.
766 * Control 1 is power down, set to 0 to operate.
768 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
769 /* mdelay(1); */ /* Marvell says 1ms will do it */
770 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
771 /* mdelay(1); */ /* Enough? */
772 spin_unlock_irqrestore(&cam->dev_lock, flags);
773 msleep(5); /* Just to be sure */
776 static void cafe_ctlr_power_down(struct cafe_camera *cam)
780 spin_lock_irqsave(&cam->dev_lock, flags);
781 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
782 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
783 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
784 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
785 spin_unlock_irqrestore(&cam->dev_lock, flags);
788 /* -------------------------------------------------------------------- */
790 * Communications with the sensor.
793 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
795 struct i2c_client *sc = cam->sensor;
798 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
800 ret = sc->driver->command(sc, cmd, arg);
801 if (ret == -EPERM) /* Unsupported command */
806 static int __cafe_cam_reset(struct cafe_camera *cam)
809 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
813 * We have found the sensor on the i2c. Let's try to have a
816 static int cafe_cam_init(struct cafe_camera *cam)
818 struct v4l2_dbg_chip_ident chip;
821 mutex_lock(&cam->s_mutex);
822 if (cam->state != S_NOTREADY)
823 cam_warn(cam, "Cam init with device in funky state %d",
825 ret = __cafe_cam_reset(cam);
828 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
829 chip.match.addr = cam->sensor->addr;
830 ret = __cafe_cam_cmd(cam, VIDIOC_DBG_G_CHIP_IDENT, &chip);
833 cam->sensor_type = chip.ident;
834 /* if (cam->sensor->addr != OV7xx0_SID) { */
835 if (cam->sensor_type != V4L2_IDENT_OV7670) {
836 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
840 /* Get/set parameters? */
844 cafe_ctlr_power_down(cam);
845 mutex_unlock(&cam->s_mutex);
850 * Configure the sensor to match the parameters we have. Caller should
853 static int cafe_cam_set_flip(struct cafe_camera *cam)
855 struct v4l2_control ctrl;
857 memset(&ctrl, 0, sizeof(ctrl));
858 ctrl.id = V4L2_CID_VFLIP;
860 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
864 static int cafe_cam_configure(struct cafe_camera *cam)
866 struct v4l2_format fmt;
869 if (cam->state != S_IDLE)
871 fmt.fmt.pix = cam->pix_format;
872 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
874 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
876 * OV7670 does weird things if flip is set *before* format...
878 ret += cafe_cam_set_flip(cam);
882 /* -------------------------------------------------------------------- */
884 * DMA buffer management. These functions need s_mutex held.
887 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
888 * does a get_free_pages() call, and we waste a good chunk of an orderN
889 * allocation. Should try to allocate the whole set in one chunk.
891 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
895 cafe_set_config_needed(cam, 1);
897 cam->dma_buf_size = dma_buf_size;
899 cam->dma_buf_size = cam->pix_format.sizeimage;
904 for (i = 0; i < n_dma_bufs; i++) {
905 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
906 cam->dma_buf_size, cam->dma_handles + i,
908 if (cam->dma_bufs[i] == NULL) {
909 cam_warn(cam, "Failed to allocate DMA buffer\n");
912 /* For debug, remove eventually */
913 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
917 switch (cam->nbufs) {
919 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
920 cam->dma_bufs[0], cam->dma_handles[0]);
923 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
928 cam_warn(cam, "Will limp along with only 2 buffers\n");
934 static void cafe_free_dma_bufs(struct cafe_camera *cam)
938 for (i = 0; i < cam->nbufs; i++) {
939 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
940 cam->dma_bufs[i], cam->dma_handles[i]);
941 cam->dma_bufs[i] = NULL;
950 /* ----------------------------------------------------------------------- */
952 * Here starts the V4L2 interface code.
956 * Read an image from the device.
958 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
959 char __user *buffer, size_t len, loff_t *pos)
964 spin_lock_irqsave(&cam->dev_lock, flags);
965 if (cam->next_buf < 0) {
966 cam_err(cam, "deliver_buffer: No next buffer\n");
967 spin_unlock_irqrestore(&cam->dev_lock, flags);
970 bufno = cam->next_buf;
971 clear_bit(bufno, &cam->flags);
972 if (++(cam->next_buf) >= cam->nbufs)
974 if (! test_bit(cam->next_buf, &cam->flags))
977 spin_unlock_irqrestore(&cam->dev_lock, flags);
979 if (len > cam->pix_format.sizeimage)
980 len = cam->pix_format.sizeimage;
981 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
988 * Get everything ready, and start grabbing frames.
990 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
996 * Configuration. If we still don't have DMA buffers,
997 * make one last, desperate attempt.
1000 if (cafe_alloc_dma_bufs(cam, 0))
1003 if (cafe_needs_config(cam)) {
1004 cafe_cam_configure(cam);
1005 ret = cafe_ctlr_configure(cam);
1013 spin_lock_irqsave(&cam->dev_lock, flags);
1014 cafe_reset_buffers(cam);
1015 cafe_ctlr_irq_enable(cam);
1017 cafe_ctlr_start(cam);
1018 spin_unlock_irqrestore(&cam->dev_lock, flags);
1023 static ssize_t cafe_v4l_read(struct file *filp,
1024 char __user *buffer, size_t len, loff_t *pos)
1026 struct cafe_camera *cam = filp->private_data;
1030 * Perhaps we're in speculative read mode and already
1033 mutex_lock(&cam->s_mutex);
1034 if (cam->state == S_SPECREAD) {
1035 if (cam->next_buf >= 0) {
1036 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1040 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1043 } else if (cam->state != S_IDLE) {
1049 * v4l2: multiple processes can open the device, but only
1050 * one gets to grab data from it.
1052 if (cam->owner && cam->owner != filp) {
1059 * Do setup if need be.
1061 if (cam->state != S_SPECREAD) {
1062 ret = cafe_read_setup(cam, S_SINGLEREAD);
1067 * Wait for something to happen. This should probably
1068 * be interruptible (FIXME).
1070 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1071 if (cam->next_buf < 0) {
1072 cam_err(cam, "read() operation timed out\n");
1073 cafe_ctlr_stop_dma(cam);
1078 * Give them their data and we should be done.
1080 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1083 mutex_unlock(&cam->s_mutex);
1095 * Streaming I/O support.
1100 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1101 enum v4l2_buf_type type)
1103 struct cafe_camera *cam = filp->private_data;
1106 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1108 mutex_lock(&cam->s_mutex);
1109 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1113 ret = cafe_read_setup(cam, S_STREAMING);
1116 mutex_unlock(&cam->s_mutex);
1122 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1123 enum v4l2_buf_type type)
1125 struct cafe_camera *cam = filp->private_data;
1128 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1130 mutex_lock(&cam->s_mutex);
1131 if (cam->state != S_STREAMING)
1134 cafe_ctlr_stop_dma(cam);
1138 mutex_unlock(&cam->s_mutex);
1145 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1147 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1149 INIT_LIST_HEAD(&buf->list);
1150 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1151 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1152 if (buf->buffer == NULL)
1157 buf->v4lbuf.index = index;
1158 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1159 buf->v4lbuf.field = V4L2_FIELD_NONE;
1160 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1162 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1163 * just uses the length times the index, but the spec warns
1164 * against doing just that - vma merging problems. So we
1165 * leave a gap between each pair of buffers.
1167 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1171 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1176 * If any buffers are mapped, we cannot free them at all.
1178 for (i = 0; i < cam->n_sbufs; i++)
1179 if (cam->sb_bufs[i].mapcount > 0)
1184 for (i = 0; i < cam->n_sbufs; i++)
1185 vfree(cam->sb_bufs[i].buffer);
1187 kfree(cam->sb_bufs);
1188 cam->sb_bufs = NULL;
1189 INIT_LIST_HEAD(&cam->sb_avail);
1190 INIT_LIST_HEAD(&cam->sb_full);
1196 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1197 struct v4l2_requestbuffers *req)
1199 struct cafe_camera *cam = filp->private_data;
1200 int ret = 0; /* Silence warning */
1203 * Make sure it's something we can do. User pointers could be
1204 * implemented without great pain, but that's not been done yet.
1206 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1208 if (req->memory != V4L2_MEMORY_MMAP)
1211 * If they ask for zero buffers, they really want us to stop streaming
1212 * (if it's happening) and free everything. Should we check owner?
1214 mutex_lock(&cam->s_mutex);
1215 if (req->count == 0) {
1216 if (cam->state == S_STREAMING)
1217 cafe_ctlr_stop_dma(cam);
1218 ret = cafe_free_sio_buffers (cam);
1222 * Device needs to be idle and working. We *could* try to do the
1223 * right thing in S_SPECREAD by shutting things down, but it
1224 * probably doesn't matter.
1226 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1232 if (req->count < min_buffers)
1233 req->count = min_buffers;
1234 else if (req->count > max_buffers)
1235 req->count = max_buffers;
1236 if (cam->n_sbufs > 0) {
1237 ret = cafe_free_sio_buffers(cam);
1242 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1244 if (cam->sb_bufs == NULL) {
1248 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1249 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1254 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1255 kfree(cam->sb_bufs);
1256 req->count = cam->n_sbufs; /* In case of partial success */
1259 mutex_unlock(&cam->s_mutex);
1264 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1265 struct v4l2_buffer *buf)
1267 struct cafe_camera *cam = filp->private_data;
1270 mutex_lock(&cam->s_mutex);
1271 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1273 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1275 *buf = cam->sb_bufs[buf->index].v4lbuf;
1278 mutex_unlock(&cam->s_mutex);
1282 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1283 struct v4l2_buffer *buf)
1285 struct cafe_camera *cam = filp->private_data;
1286 struct cafe_sio_buffer *sbuf;
1288 unsigned long flags;
1290 mutex_lock(&cam->s_mutex);
1291 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1293 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1295 sbuf = cam->sb_bufs + buf->index;
1296 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1297 ret = 0; /* Already queued?? */
1300 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1301 /* Spec doesn't say anything, seems appropriate tho */
1305 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1306 spin_lock_irqsave(&cam->dev_lock, flags);
1307 list_add(&sbuf->list, &cam->sb_avail);
1308 spin_unlock_irqrestore(&cam->dev_lock, flags);
1311 mutex_unlock(&cam->s_mutex);
1315 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1316 struct v4l2_buffer *buf)
1318 struct cafe_camera *cam = filp->private_data;
1319 struct cafe_sio_buffer *sbuf;
1321 unsigned long flags;
1323 mutex_lock(&cam->s_mutex);
1324 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1326 if (cam->state != S_STREAMING)
1328 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1333 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1334 mutex_unlock(&cam->s_mutex);
1335 if (wait_event_interruptible(cam->iowait,
1336 !list_empty(&cam->sb_full))) {
1340 mutex_lock(&cam->s_mutex);
1343 if (cam->state != S_STREAMING)
1346 spin_lock_irqsave(&cam->dev_lock, flags);
1347 /* Should probably recheck !list_empty() here */
1348 sbuf = list_entry(cam->sb_full.next,
1349 struct cafe_sio_buffer, list);
1350 list_del_init(&sbuf->list);
1351 spin_unlock_irqrestore(&cam->dev_lock, flags);
1352 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1353 *buf = sbuf->v4lbuf;
1358 mutex_unlock(&cam->s_mutex);
1365 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1367 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1369 * Locking: done under mmap_sem, so we don't need to
1370 * go back to the camera lock here.
1376 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1378 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1380 mutex_lock(&sbuf->cam->s_mutex);
1382 /* Docs say we should stop I/O too... */
1383 if (sbuf->mapcount == 0)
1384 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1385 mutex_unlock(&sbuf->cam->s_mutex);
1388 static struct vm_operations_struct cafe_v4l_vm_ops = {
1389 .open = cafe_v4l_vm_open,
1390 .close = cafe_v4l_vm_close
1394 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1396 struct cafe_camera *cam = filp->private_data;
1397 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1400 struct cafe_sio_buffer *sbuf = NULL;
1402 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1405 * Find the buffer they are looking for.
1407 mutex_lock(&cam->s_mutex);
1408 for (i = 0; i < cam->n_sbufs; i++)
1409 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1410 sbuf = cam->sb_bufs + i;
1416 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1419 vma->vm_flags |= VM_DONTEXPAND;
1420 vma->vm_private_data = sbuf;
1421 vma->vm_ops = &cafe_v4l_vm_ops;
1422 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1423 cafe_v4l_vm_open(vma);
1426 mutex_unlock(&cam->s_mutex);
1432 static int cafe_v4l_open(struct file *filp)
1434 struct cafe_camera *cam = video_drvdata(filp);
1436 filp->private_data = cam;
1438 mutex_lock(&cam->s_mutex);
1439 if (cam->users == 0) {
1440 cafe_ctlr_power_up(cam);
1441 __cafe_cam_reset(cam);
1442 cafe_set_config_needed(cam, 1);
1443 /* FIXME make sure this is complete */
1446 mutex_unlock(&cam->s_mutex);
1451 static int cafe_v4l_release(struct file *filp)
1453 struct cafe_camera *cam = filp->private_data;
1455 mutex_lock(&cam->s_mutex);
1457 if (filp == cam->owner) {
1458 cafe_ctlr_stop_dma(cam);
1459 cafe_free_sio_buffers(cam);
1462 if (cam->users == 0) {
1463 cafe_ctlr_power_down(cam);
1464 if (alloc_bufs_at_read)
1465 cafe_free_dma_bufs(cam);
1467 mutex_unlock(&cam->s_mutex);
1473 static unsigned int cafe_v4l_poll(struct file *filp,
1474 struct poll_table_struct *pt)
1476 struct cafe_camera *cam = filp->private_data;
1478 poll_wait(filp, &cam->iowait, pt);
1479 if (cam->next_buf >= 0)
1480 return POLLIN | POLLRDNORM;
1486 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1487 struct v4l2_queryctrl *qc)
1489 struct cafe_camera *cam = priv;
1492 mutex_lock(&cam->s_mutex);
1493 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1494 mutex_unlock(&cam->s_mutex);
1499 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1500 struct v4l2_control *ctrl)
1502 struct cafe_camera *cam = priv;
1505 mutex_lock(&cam->s_mutex);
1506 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1507 mutex_unlock(&cam->s_mutex);
1512 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1513 struct v4l2_control *ctrl)
1515 struct cafe_camera *cam = priv;
1518 mutex_lock(&cam->s_mutex);
1519 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1520 mutex_unlock(&cam->s_mutex);
1528 static int cafe_vidioc_querycap(struct file *file, void *priv,
1529 struct v4l2_capability *cap)
1531 strcpy(cap->driver, "cafe_ccic");
1532 strcpy(cap->card, "cafe_ccic");
1533 cap->version = CAFE_VERSION;
1534 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1535 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1541 * The default format we use until somebody says otherwise.
1543 static struct v4l2_pix_format cafe_def_pix_format = {
1545 .height = VGA_HEIGHT,
1546 .pixelformat = V4L2_PIX_FMT_YUYV,
1547 .field = V4L2_FIELD_NONE,
1548 .bytesperline = VGA_WIDTH*2,
1549 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1552 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1553 void *priv, struct v4l2_fmtdesc *fmt)
1555 struct cafe_camera *cam = priv;
1558 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1560 mutex_lock(&cam->s_mutex);
1561 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1562 mutex_unlock(&cam->s_mutex);
1567 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1568 struct v4l2_format *fmt)
1570 struct cafe_camera *cam = priv;
1573 mutex_lock(&cam->s_mutex);
1574 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1575 mutex_unlock(&cam->s_mutex);
1579 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1580 struct v4l2_format *fmt)
1582 struct cafe_camera *cam = priv;
1586 * Can't do anything if the device is not idle
1587 * Also can't if there are streaming buffers in place.
1589 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1592 * See if the formatting works in principle.
1594 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1598 * Now we start to change things for real, so let's do it
1601 mutex_lock(&cam->s_mutex);
1602 cam->pix_format = fmt->fmt.pix;
1604 * Make sure we have appropriate DMA buffers.
1607 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1608 cafe_free_dma_bufs(cam);
1609 if (cam->nbufs == 0) {
1610 if (cafe_alloc_dma_bufs(cam, 0))
1614 * It looks like this might work, so let's program the sensor.
1616 ret = cafe_cam_configure(cam);
1618 ret = cafe_ctlr_configure(cam);
1620 mutex_unlock(&cam->s_mutex);
1625 * Return our stored notion of how the camera is/should be configured.
1626 * The V4l2 spec wants us to be smarter, and actually get this from
1627 * the camera (and not mess with it at open time). Someday.
1629 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1630 struct v4l2_format *f)
1632 struct cafe_camera *cam = priv;
1634 f->fmt.pix = cam->pix_format;
1639 * We only have one input - the sensor - so minimize the nonsense here.
1641 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1642 struct v4l2_input *input)
1644 if (input->index != 0)
1647 input->type = V4L2_INPUT_TYPE_CAMERA;
1648 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1649 strcpy(input->name, "Camera");
1653 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1659 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1667 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1673 * G/S_PARM. Most of this is done by the sensor, but we are
1674 * the level which controls the number of read buffers.
1676 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1677 struct v4l2_streamparm *parms)
1679 struct cafe_camera *cam = priv;
1682 mutex_lock(&cam->s_mutex);
1683 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1684 mutex_unlock(&cam->s_mutex);
1685 parms->parm.capture.readbuffers = n_dma_bufs;
1689 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1690 struct v4l2_streamparm *parms)
1692 struct cafe_camera *cam = priv;
1695 mutex_lock(&cam->s_mutex);
1696 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1697 mutex_unlock(&cam->s_mutex);
1698 parms->parm.capture.readbuffers = n_dma_bufs;
1703 * This template device holds all of those v4l2 methods; we
1704 * clone it for specific real devices.
1707 static const struct v4l2_file_operations cafe_v4l_fops = {
1708 .owner = THIS_MODULE,
1709 .open = cafe_v4l_open,
1710 .release = cafe_v4l_release,
1711 .read = cafe_v4l_read,
1712 .poll = cafe_v4l_poll,
1713 .mmap = cafe_v4l_mmap,
1714 .ioctl = video_ioctl2,
1717 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1718 .vidioc_querycap = cafe_vidioc_querycap,
1719 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1720 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1721 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1722 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1723 .vidioc_enum_input = cafe_vidioc_enum_input,
1724 .vidioc_g_input = cafe_vidioc_g_input,
1725 .vidioc_s_input = cafe_vidioc_s_input,
1726 .vidioc_s_std = cafe_vidioc_s_std,
1727 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1728 .vidioc_querybuf = cafe_vidioc_querybuf,
1729 .vidioc_qbuf = cafe_vidioc_qbuf,
1730 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1731 .vidioc_streamon = cafe_vidioc_streamon,
1732 .vidioc_streamoff = cafe_vidioc_streamoff,
1733 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1734 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1735 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1736 .vidioc_g_parm = cafe_vidioc_g_parm,
1737 .vidioc_s_parm = cafe_vidioc_s_parm,
1740 static struct video_device cafe_v4l_template = {
1742 .minor = -1, /* Get one dynamically */
1743 .tvnorms = V4L2_STD_NTSC_M,
1744 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1746 .fops = &cafe_v4l_fops,
1747 .ioctl_ops = &cafe_v4l_ioctl_ops,
1748 .release = video_device_release_empty,
1752 /* ---------------------------------------------------------------------- */
1754 * Interrupt handler stuff
1759 static void cafe_frame_tasklet(unsigned long data)
1761 struct cafe_camera *cam = (struct cafe_camera *) data;
1763 unsigned long flags;
1764 struct cafe_sio_buffer *sbuf;
1766 spin_lock_irqsave(&cam->dev_lock, flags);
1767 for (i = 0; i < cam->nbufs; i++) {
1768 int bufno = cam->next_buf;
1769 if (bufno < 0) { /* "will never happen" */
1770 cam_err(cam, "No valid bufs in tasklet!\n");
1773 if (++(cam->next_buf) >= cam->nbufs)
1775 if (! test_bit(bufno, &cam->flags))
1777 if (list_empty(&cam->sb_avail))
1778 break; /* Leave it valid, hope for better later */
1779 clear_bit(bufno, &cam->flags);
1780 sbuf = list_entry(cam->sb_avail.next,
1781 struct cafe_sio_buffer, list);
1783 * Drop the lock during the big copy. This *should* be safe...
1785 spin_unlock_irqrestore(&cam->dev_lock, flags);
1786 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1787 cam->pix_format.sizeimage);
1788 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1789 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1790 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1791 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1792 spin_lock_irqsave(&cam->dev_lock, flags);
1793 list_move_tail(&sbuf->list, &cam->sb_full);
1795 if (! list_empty(&cam->sb_full))
1796 wake_up(&cam->iowait);
1797 spin_unlock_irqrestore(&cam->dev_lock, flags);
1802 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1805 * Basic frame housekeeping.
1807 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1808 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1809 set_bit(frame, &cam->flags);
1810 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1811 if (cam->next_buf < 0)
1812 cam->next_buf = frame;
1813 cam->buf_seq[frame] = ++(cam->sequence);
1815 switch (cam->state) {
1817 * If in single read mode, try going speculative.
1820 cam->state = S_SPECREAD;
1821 cam->specframes = 0;
1822 wake_up(&cam->iowait);
1826 * If we are already doing speculative reads, and nobody is
1827 * reading them, just stop.
1830 if (++(cam->specframes) >= cam->nbufs) {
1831 cafe_ctlr_stop(cam);
1832 cafe_ctlr_irq_disable(cam);
1833 cam->state = S_IDLE;
1835 wake_up(&cam->iowait);
1838 * For the streaming case, we defer the real work to the
1841 * FIXME: if the application is not consuming the buffers,
1842 * we should eventually put things on hold and restart in
1846 tasklet_schedule(&cam->s_tasklet);
1850 cam_err(cam, "Frame interrupt in non-operational state\n");
1858 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1862 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1864 * Handle any frame completions. There really should
1865 * not be more than one of these, or we have fallen
1868 for (frame = 0; frame < cam->nbufs; frame++)
1869 if (irqs & (IRQ_EOF0 << frame))
1870 cafe_frame_complete(cam, frame);
1872 * If a frame starts, note that we have DMA active. This
1873 * code assumes that we won't get multiple frame interrupts
1874 * at once; may want to rethink that.
1876 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1877 set_bit(CF_DMA_ACTIVE, &cam->flags);
1882 static irqreturn_t cafe_irq(int irq, void *data)
1884 struct cafe_camera *cam = data;
1887 spin_lock(&cam->dev_lock);
1888 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1889 if ((irqs & ALLIRQS) == 0) {
1890 spin_unlock(&cam->dev_lock);
1893 if (irqs & FRAMEIRQS)
1894 cafe_frame_irq(cam, irqs);
1895 if (irqs & TWSIIRQS) {
1896 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1897 wake_up(&cam->smbus_wait);
1899 spin_unlock(&cam->dev_lock);
1904 /* -------------------------------------------------------------------------- */
1905 #ifdef CONFIG_VIDEO_ADV_DEBUG
1910 static char cafe_debug_buf[1024];
1911 static struct dentry *cafe_dfs_root;
1913 static void cafe_dfs_setup(void)
1915 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1916 if (IS_ERR(cafe_dfs_root)) {
1917 cafe_dfs_root = NULL; /* Never mind */
1918 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1922 static void cafe_dfs_shutdown(void)
1925 debugfs_remove(cafe_dfs_root);
1928 static int cafe_dfs_open(struct inode *inode, struct file *file)
1930 file->private_data = inode->i_private;
1934 static ssize_t cafe_dfs_read_regs(struct file *file,
1935 char __user *buf, size_t count, loff_t *ppos)
1937 struct cafe_camera *cam = file->private_data;
1938 char *s = cafe_debug_buf;
1941 for (offset = 0; offset < 0x44; offset += 4)
1942 s += sprintf(s, "%02x: %08x\n", offset,
1943 cafe_reg_read(cam, offset));
1944 for (offset = 0x88; offset <= 0x90; offset += 4)
1945 s += sprintf(s, "%02x: %08x\n", offset,
1946 cafe_reg_read(cam, offset));
1947 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1948 s += sprintf(s, "%02x: %08x\n", offset,
1949 cafe_reg_read(cam, offset));
1950 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1951 s += sprintf(s, "%04x: %08x\n", offset,
1952 cafe_reg_read(cam, offset));
1953 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1954 s - cafe_debug_buf);
1957 static const struct file_operations cafe_dfs_reg_ops = {
1958 .owner = THIS_MODULE,
1959 .read = cafe_dfs_read_regs,
1960 .open = cafe_dfs_open
1963 static ssize_t cafe_dfs_read_cam(struct file *file,
1964 char __user *buf, size_t count, loff_t *ppos)
1966 struct cafe_camera *cam = file->private_data;
1967 char *s = cafe_debug_buf;
1972 for (offset = 0x0; offset < 0x8a; offset++)
1976 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
1977 s += sprintf(s, "%02x: %02x\n", offset, v);
1979 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1980 s - cafe_debug_buf);
1983 static const struct file_operations cafe_dfs_cam_ops = {
1984 .owner = THIS_MODULE,
1985 .read = cafe_dfs_read_cam,
1986 .open = cafe_dfs_open
1991 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
1997 sprintf(fname, "regs-%d", cam->vdev.num);
1998 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
1999 cam, &cafe_dfs_reg_ops);
2000 sprintf(fname, "cam-%d", cam->vdev.num);
2001 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2002 cam, &cafe_dfs_cam_ops);
2006 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2008 if (! IS_ERR(cam->dfs_regs))
2009 debugfs_remove(cam->dfs_regs);
2010 if (! IS_ERR(cam->dfs_cam_regs))
2011 debugfs_remove(cam->dfs_cam_regs);
2016 #define cafe_dfs_setup()
2017 #define cafe_dfs_shutdown()
2018 #define cafe_dfs_cam_setup(cam)
2019 #define cafe_dfs_cam_shutdown(cam)
2020 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2025 /* ------------------------------------------------------------------------*/
2027 * PCI interface stuff.
2030 static int cafe_pci_probe(struct pci_dev *pdev,
2031 const struct pci_device_id *id)
2034 struct cafe_camera *cam;
2037 * Start putting together one of our big camera structures.
2040 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2043 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
2047 mutex_init(&cam->s_mutex);
2048 mutex_lock(&cam->s_mutex);
2049 spin_lock_init(&cam->dev_lock);
2050 cam->state = S_NOTREADY;
2051 cafe_set_config_needed(cam, 1);
2052 init_waitqueue_head(&cam->smbus_wait);
2053 init_waitqueue_head(&cam->iowait);
2055 cam->pix_format = cafe_def_pix_format;
2056 INIT_LIST_HEAD(&cam->dev_list);
2057 INIT_LIST_HEAD(&cam->sb_avail);
2058 INIT_LIST_HEAD(&cam->sb_full);
2059 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2061 * Get set up on the PCI bus.
2063 ret = pci_enable_device(pdev);
2066 pci_set_master(pdev);
2069 cam->regs = pci_iomap(pdev, 0, 0);
2071 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2074 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2078 * Initialize the controller and leave it powered up. It will
2079 * stay that way until the sensor driver shows up.
2081 cafe_ctlr_init(cam);
2082 cafe_ctlr_power_up(cam);
2084 * Set up I2C/SMBUS communications. We have to drop the mutex here
2085 * because the sensor could attach in this call chain, leading to
2086 * unsightly deadlocks.
2088 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2089 ret = cafe_smbus_setup(cam);
2093 * Get the v4l2 setup done.
2095 mutex_lock(&cam->s_mutex);
2096 cam->vdev = cafe_v4l_template;
2097 cam->vdev.debug = 0;
2098 /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
2099 cam->vdev.v4l2_dev = &cam->v4l2_dev;
2100 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
2103 video_set_drvdata(&cam->vdev, cam);
2106 * If so requested, try to get our DMA buffers now.
2108 if (!alloc_bufs_at_read) {
2109 if (cafe_alloc_dma_bufs(cam, 1))
2110 cam_warn(cam, "Unable to alloc DMA buffers at load"
2111 " will try again later.");
2114 cafe_dfs_cam_setup(cam);
2115 mutex_unlock(&cam->s_mutex);
2119 cafe_smbus_shutdown(cam);
2121 cafe_ctlr_power_down(cam);
2122 free_irq(pdev->irq, cam);
2124 pci_iounmap(pdev, cam->regs);
2126 v4l2_device_unregister(&cam->v4l2_dev);
2135 * Shut down an initialized device
2137 static void cafe_shutdown(struct cafe_camera *cam)
2139 /* FIXME: Make sure we take care of everything here */
2140 cafe_dfs_cam_shutdown(cam);
2141 if (cam->n_sbufs > 0)
2142 /* What if they are still mapped? Shouldn't be, but... */
2143 cafe_free_sio_buffers(cam);
2144 cafe_ctlr_stop_dma(cam);
2145 cafe_ctlr_power_down(cam);
2146 cafe_smbus_shutdown(cam);
2147 cafe_free_dma_bufs(cam);
2148 free_irq(cam->pdev->irq, cam);
2149 pci_iounmap(cam->pdev, cam->regs);
2150 video_unregister_device(&cam->vdev);
2154 static void cafe_pci_remove(struct pci_dev *pdev)
2156 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2157 struct cafe_camera *cam = to_cam(v4l2_dev);
2160 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2163 mutex_lock(&cam->s_mutex);
2165 cam_warn(cam, "Removing a device with users!\n");
2167 v4l2_device_unregister(&cam->v4l2_dev);
2169 /* No unlock - it no longer exists */
2175 * Basic power management.
2177 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2179 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2180 struct cafe_camera *cam = to_cam(v4l2_dev);
2182 enum cafe_state cstate;
2184 ret = pci_save_state(pdev);
2187 cstate = cam->state; /* HACK - stop_dma sets to idle */
2188 cafe_ctlr_stop_dma(cam);
2189 cafe_ctlr_power_down(cam);
2190 pci_disable_device(pdev);
2191 cam->state = cstate;
2196 static int cafe_pci_resume(struct pci_dev *pdev)
2198 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2199 struct cafe_camera *cam = to_cam(v4l2_dev);
2202 ret = pci_restore_state(pdev);
2205 ret = pci_enable_device(pdev);
2208 cam_warn(cam, "Unable to re-enable device on resume!\n");
2211 cafe_ctlr_init(cam);
2212 cafe_ctlr_power_down(cam);
2214 mutex_lock(&cam->s_mutex);
2215 if (cam->users > 0) {
2216 cafe_ctlr_power_up(cam);
2217 __cafe_cam_reset(cam);
2219 mutex_unlock(&cam->s_mutex);
2221 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2222 if (cam->state == S_SPECREAD)
2223 cam->state = S_IDLE; /* Don't bother restarting */
2224 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2225 ret = cafe_read_setup(cam, cam->state);
2229 #endif /* CONFIG_PM */
2232 static struct pci_device_id cafe_ids[] = {
2233 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2234 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2238 MODULE_DEVICE_TABLE(pci, cafe_ids);
2240 static struct pci_driver cafe_pci_driver = {
2241 .name = "cafe1000-ccic",
2242 .id_table = cafe_ids,
2243 .probe = cafe_pci_probe,
2244 .remove = cafe_pci_remove,
2246 .suspend = cafe_pci_suspend,
2247 .resume = cafe_pci_resume,
2254 static int __init cafe_init(void)
2258 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2261 ret = pci_register_driver(&cafe_pci_driver);
2263 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2266 request_module("ov7670"); /* FIXME want something more general */
2274 static void __exit cafe_exit(void)
2276 pci_unregister_driver(&cafe_pci_driver);
2277 cafe_dfs_shutdown();
2280 module_init(cafe_init);
2281 module_exit(cafe_exit);