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 * v4l2_device/v4l2_subdev conversion by:
15 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
17 * Note: this conversion is untested! Please contact the linux-media
18 * mailinglist if you can test this, together with the test results.
20 * This file may be distributed under the terms of the GNU General
21 * Public License, version 2.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
32 #include <linux/spinlock.h>
33 #include <linux/videodev2.h>
34 #include <linux/slab.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-chip-ident.h>
38 #include <linux/device.h>
39 #include <linux/wait.h>
40 #include <linux/list.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/delay.h>
43 #include <linux/jiffies.h>
44 #include <linux/vmalloc.h>
46 #include <asm/uaccess.h>
49 #include "cafe_ccic-regs.h"
51 #define CAFE_VERSION 0x000002
57 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
58 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
59 MODULE_LICENSE("GPL");
60 MODULE_SUPPORTED_DEVICE("Video");
63 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
64 * we must have physically contiguous buffers to bring frames into.
65 * These parameters control how many buffers we use, whether we
66 * allocate them at load time (better chance of success, but nails down
67 * memory) or when somebody tries to use the camera (riskier), and,
68 * for load-time allocation, how big they should be.
70 * The controller can cycle through three buffers. We could use
71 * more by flipping pointers around, but it probably makes little
75 #define MAX_DMA_BUFS 3
76 static int alloc_bufs_at_read;
77 module_param(alloc_bufs_at_read, bool, 0444);
78 MODULE_PARM_DESC(alloc_bufs_at_read,
79 "Non-zero value causes DMA buffers to be allocated when the "
80 "video capture device is read, rather than at module load "
81 "time. This saves memory, but decreases the chances of "
82 "successfully getting those buffers.");
84 static int n_dma_bufs = 3;
85 module_param(n_dma_bufs, uint, 0644);
86 MODULE_PARM_DESC(n_dma_bufs,
87 "The number of DMA buffers to allocate. Can be either two "
88 "(saves memory, makes timing tighter) or three.");
90 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
91 module_param(dma_buf_size, uint, 0444);
92 MODULE_PARM_DESC(dma_buf_size,
93 "The size of the allocated DMA buffers. If actual operating "
94 "parameters require larger buffers, an attempt to reallocate "
97 static int min_buffers = 1;
98 module_param(min_buffers, uint, 0644);
99 MODULE_PARM_DESC(min_buffers,
100 "The minimum number of streaming I/O buffers we are willing "
103 static int max_buffers = 10;
104 module_param(max_buffers, uint, 0644);
105 MODULE_PARM_DESC(max_buffers,
106 "The maximum number of streaming I/O buffers an application "
107 "will be allowed to allocate. These buffers are big and live "
108 "in vmalloc space.");
111 module_param(flip, bool, 0444);
112 MODULE_PARM_DESC(flip,
113 "If set, the sensor will be instructed to flip the image "
118 S_NOTREADY, /* Not yet initialized */
119 S_IDLE, /* Just hanging around */
120 S_FLAKED, /* Some sort of problem */
121 S_SINGLEREAD, /* In read() */
122 S_SPECREAD, /* Speculative read (for future read()) */
123 S_STREAMING /* Streaming data */
127 * Tracking of streaming I/O buffers.
129 struct cafe_sio_buffer {
130 struct list_head list;
131 struct v4l2_buffer v4lbuf;
132 char *buffer; /* Where it lives in kernel space */
134 struct cafe_camera *cam;
138 * A description of one of our devices.
139 * Locking: controlled by s_mutex. Certain fields, however, require
140 * the dev_lock spinlock; they are marked as such by comments.
141 * dev_lock is also required for access to device registers.
145 struct v4l2_device v4l2_dev;
146 enum cafe_state state;
147 unsigned long flags; /* Buffer status, mainly (dev_lock) */
148 int users; /* How many open FDs */
149 struct file *owner; /* Who has data access (v4l2) */
152 * Subsystem structures.
154 struct pci_dev *pdev;
155 struct video_device vdev;
156 struct i2c_adapter i2c_adapter;
157 struct v4l2_subdev *sensor;
158 unsigned short sensor_addr;
160 unsigned char __iomem *regs;
161 struct list_head dev_list; /* link to other devices */
164 unsigned int nbufs; /* How many are alloc'd */
165 int next_buf; /* Next to consume (dev_lock) */
166 unsigned int dma_buf_size; /* allocated size */
167 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
168 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
169 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
170 unsigned int sequence; /* Frame sequence number */
171 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
173 /* Streaming buffers */
174 unsigned int n_sbufs; /* How many we have */
175 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
176 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
177 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
178 struct tasklet_struct s_tasklet;
180 /* Current operating parameters */
181 u32 sensor_type; /* Currently ov7670 only */
182 struct v4l2_pix_format pix_format;
183 enum v4l2_mbus_pixelcode mbus_code;
186 struct mutex s_mutex; /* Access to this structure */
187 spinlock_t dev_lock; /* Access to device */
190 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
191 wait_queue_head_t iowait; /* Waiting on frame data */
195 * Status flags. Always manipulated with bit operations.
197 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
198 #define CF_BUF1_VALID 1
199 #define CF_BUF2_VALID 2
200 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
201 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
203 #define sensor_call(cam, o, f, args...) \
204 v4l2_subdev_call(cam->sensor, o, f, ##args)
206 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
208 return container_of(dev, struct cafe_camera, v4l2_dev);
211 static struct cafe_format_struct {
214 int bpp; /* Bytes per pixel */
215 enum v4l2_mbus_pixelcode mbus_code;
218 .desc = "YUYV 4:2:2",
219 .pixelformat = V4L2_PIX_FMT_YUYV,
220 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
225 .pixelformat = V4L2_PIX_FMT_RGB444,
226 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
231 .pixelformat = V4L2_PIX_FMT_RGB565,
232 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
236 .desc = "Raw RGB Bayer",
237 .pixelformat = V4L2_PIX_FMT_SBGGR8,
238 .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
242 #define N_CAFE_FMTS ARRAY_SIZE(cafe_formats)
244 static struct cafe_format_struct *cafe_find_format(u32 pixelformat)
248 for (i = 0; i < N_CAFE_FMTS; i++)
249 if (cafe_formats[i].pixelformat == pixelformat)
250 return cafe_formats + i;
251 /* Not found? Then return the first format. */
256 * Start over with DMA buffers - dev_lock needed.
258 static void cafe_reset_buffers(struct cafe_camera *cam)
263 for (i = 0; i < cam->nbufs; i++)
264 clear_bit(i, &cam->flags);
268 static inline int cafe_needs_config(struct cafe_camera *cam)
270 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
273 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
276 set_bit(CF_CONFIG_NEEDED, &cam->flags);
278 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
285 * Debugging and related.
287 #define cam_err(cam, fmt, arg...) \
288 dev_err(&(cam)->pdev->dev, fmt, ##arg);
289 #define cam_warn(cam, fmt, arg...) \
290 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
291 #define cam_dbg(cam, fmt, arg...) \
292 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
295 /* ---------------------------------------------------------------------*/
298 * Device register I/O
300 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
303 iowrite32(val, cam->regs + reg);
306 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
309 return ioread32(cam->regs + reg);
313 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
314 unsigned int val, unsigned int mask)
316 unsigned int v = cafe_reg_read(cam, reg);
318 v = (v & ~mask) | (val & mask);
319 cafe_reg_write(cam, reg, v);
322 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
323 unsigned int reg, unsigned int val)
325 cafe_reg_write_mask(cam, reg, 0, val);
328 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
329 unsigned int reg, unsigned int val)
331 cafe_reg_write_mask(cam, reg, val, val);
336 /* -------------------------------------------------------------------- */
338 * The I2C/SMBUS interface to the camera itself starts here. The
339 * controller handles SMBUS itself, presenting a relatively simple register
340 * interface; all we have to do is to tell it where to route the data.
342 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
344 static int cafe_smbus_write_done(struct cafe_camera *cam)
350 * We must delay after the interrupt, or the controller gets confused
351 * and never does give us good status. Fortunately, we don't do this
355 spin_lock_irqsave(&cam->dev_lock, flags);
356 c1 = cafe_reg_read(cam, REG_TWSIC1);
357 spin_unlock_irqrestore(&cam->dev_lock, flags);
358 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
361 static int cafe_smbus_write_data(struct cafe_camera *cam,
362 u16 addr, u8 command, u8 value)
367 spin_lock_irqsave(&cam->dev_lock, flags);
368 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
369 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
371 * Marvell sez set clkdiv to all 1's for now.
373 rval |= TWSIC0_CLKDIV;
374 cafe_reg_write(cam, REG_TWSIC0, rval);
375 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
376 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
377 cafe_reg_write(cam, REG_TWSIC1, rval);
378 spin_unlock_irqrestore(&cam->dev_lock, flags);
380 /* Unfortunately, reading TWSIC1 too soon after sending a command
381 * causes the device to die.
382 * Use a busy-wait because we often send a large quantity of small
383 * commands at-once; using msleep() would cause a lot of context
384 * switches which take longer than 2ms, resulting in a noticable
385 * boot-time and capture-start delays.
390 * Another sad fact is that sometimes, commands silently complete but
391 * cafe_smbus_write_done() never becomes aware of this.
392 * This happens at random and appears to possible occur with any
394 * We don't understand why this is. We work around this issue
395 * with the timeout in the wait below, assuming that all commands
396 * complete within the timeout.
398 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
401 spin_lock_irqsave(&cam->dev_lock, flags);
402 rval = cafe_reg_read(cam, REG_TWSIC1);
403 spin_unlock_irqrestore(&cam->dev_lock, flags);
405 if (rval & TWSIC1_WSTAT) {
406 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
410 if (rval & TWSIC1_ERROR) {
411 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
420 static int cafe_smbus_read_done(struct cafe_camera *cam)
426 * We must delay after the interrupt, or the controller gets confused
427 * and never does give us good status. Fortunately, we don't do this
431 spin_lock_irqsave(&cam->dev_lock, flags);
432 c1 = cafe_reg_read(cam, REG_TWSIC1);
433 spin_unlock_irqrestore(&cam->dev_lock, flags);
434 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
439 static int cafe_smbus_read_data(struct cafe_camera *cam,
440 u16 addr, u8 command, u8 *value)
445 spin_lock_irqsave(&cam->dev_lock, flags);
446 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
447 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
449 * Marvel sez set clkdiv to all 1's for now.
451 rval |= TWSIC0_CLKDIV;
452 cafe_reg_write(cam, REG_TWSIC0, rval);
453 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
454 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
455 cafe_reg_write(cam, REG_TWSIC1, rval);
456 spin_unlock_irqrestore(&cam->dev_lock, flags);
458 wait_event_timeout(cam->smbus_wait,
459 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
460 spin_lock_irqsave(&cam->dev_lock, flags);
461 rval = cafe_reg_read(cam, REG_TWSIC1);
462 spin_unlock_irqrestore(&cam->dev_lock, flags);
464 if (rval & TWSIC1_ERROR) {
465 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
468 if (! (rval & TWSIC1_RVALID)) {
469 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
473 *value = rval & 0xff;
478 * Perform a transfer over SMBUS. This thing is called under
479 * the i2c bus lock, so we shouldn't race with ourselves...
481 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
482 unsigned short flags, char rw, u8 command,
483 int size, union i2c_smbus_data *data)
485 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
486 struct cafe_camera *cam = to_cam(v4l2_dev);
490 * This interface would appear to only do byte data ops. OK
491 * it can do word too, but the cam chip has no use for that.
493 if (size != I2C_SMBUS_BYTE_DATA) {
494 cam_err(cam, "funky xfer size %d\n", size);
498 if (rw == I2C_SMBUS_WRITE)
499 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
500 else if (rw == I2C_SMBUS_READ)
501 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
506 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
510 spin_lock_irqsave(&cam->dev_lock, flags);
511 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
512 spin_unlock_irqrestore(&cam->dev_lock, flags);
515 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
517 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
518 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
521 static struct i2c_algorithm cafe_smbus_algo = {
522 .smbus_xfer = cafe_smbus_xfer,
523 .functionality = cafe_smbus_func
526 /* Somebody is on the bus */
527 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
528 static void cafe_ctlr_power_down(struct cafe_camera *cam);
530 static int cafe_smbus_setup(struct cafe_camera *cam)
532 struct i2c_adapter *adap = &cam->i2c_adapter;
535 cafe_smbus_enable_irq(cam);
536 adap->owner = THIS_MODULE;
537 adap->algo = &cafe_smbus_algo;
538 strcpy(adap->name, "cafe_ccic");
539 adap->dev.parent = &cam->pdev->dev;
540 i2c_set_adapdata(adap, &cam->v4l2_dev);
541 ret = i2c_add_adapter(adap);
543 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
547 static void cafe_smbus_shutdown(struct cafe_camera *cam)
549 i2c_del_adapter(&cam->i2c_adapter);
553 /* ------------------------------------------------------------------- */
555 * Deal with the controller.
559 * Do everything we think we need to have the interface operating
560 * according to the desired format.
562 static void cafe_ctlr_dma(struct cafe_camera *cam)
565 * Store the first two Y buffers (we aren't supporting
566 * planar formats for now, so no UV bufs). Then either
567 * set the third if it exists, or tell the controller
570 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
571 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
572 if (cam->nbufs > 2) {
573 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
574 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
577 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
578 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
581 static void cafe_ctlr_image(struct cafe_camera *cam)
584 struct v4l2_pix_format *fmt = &cam->pix_format;
586 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
587 (fmt->bytesperline & IMGSZ_H_MASK);
588 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
589 cafe_reg_write(cam, REG_IMGOFFSET, 0);
590 /* YPITCH just drops the last two bits */
591 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
594 * Tell the controller about the image format we are using.
596 switch (cam->pix_format.pixelformat) {
597 case V4L2_PIX_FMT_YUYV:
598 cafe_reg_write_mask(cam, REG_CTRL0,
599 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
603 case V4L2_PIX_FMT_RGB444:
604 cafe_reg_write_mask(cam, REG_CTRL0,
605 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
610 case V4L2_PIX_FMT_RGB565:
611 cafe_reg_write_mask(cam, REG_CTRL0,
612 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
617 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
621 * Make sure it knows we want to use hsync/vsync.
623 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
629 * Configure the controller for operation; caller holds the
632 static int cafe_ctlr_configure(struct cafe_camera *cam)
636 spin_lock_irqsave(&cam->dev_lock, flags);
638 cafe_ctlr_image(cam);
639 cafe_set_config_needed(cam, 0);
640 spin_unlock_irqrestore(&cam->dev_lock, flags);
644 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
647 * Clear any pending interrupts, since we do not
648 * expect to have I/O active prior to enabling.
650 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
651 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
654 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
656 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
660 * Make the controller start grabbing images. Everything must
661 * be set up before doing this.
663 static void cafe_ctlr_start(struct cafe_camera *cam)
665 /* set_bit performs a read, so no other barrier should be
667 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
670 static void cafe_ctlr_stop(struct cafe_camera *cam)
672 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
675 static void cafe_ctlr_init(struct cafe_camera *cam)
679 spin_lock_irqsave(&cam->dev_lock, flags);
681 * Added magic to bring up the hardware on the B-Test board
683 cafe_reg_write(cam, 0x3038, 0x8);
684 cafe_reg_write(cam, 0x315c, 0x80008);
686 * Go through the dance needed to wake the device up.
687 * Note that these registers are global and shared
688 * with the NAND and SD devices. Interaction between the
689 * three still needs to be examined.
691 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
692 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
693 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
695 * Here we must wait a bit for the controller to come around.
697 spin_unlock_irqrestore(&cam->dev_lock, flags);
699 spin_lock_irqsave(&cam->dev_lock, flags);
701 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
702 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
704 * Make sure it's not powered down.
706 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
708 * Turn off the enable bit. It sure should be off anyway,
709 * but it's good to be sure.
711 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
713 * Mask all interrupts.
715 cafe_reg_write(cam, REG_IRQMASK, 0);
717 * Clock the sensor appropriately. Controller clock should
718 * be 48MHz, sensor "typical" value is half that.
720 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
721 spin_unlock_irqrestore(&cam->dev_lock, flags);
726 * Stop the controller, and don't return until we're really sure that no
727 * further DMA is going on.
729 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
734 * Theory: stop the camera controller (whether it is operating
735 * or not). Delay briefly just in case we race with the SOF
736 * interrupt, then wait until no DMA is active.
738 spin_lock_irqsave(&cam->dev_lock, flags);
740 spin_unlock_irqrestore(&cam->dev_lock, flags);
742 wait_event_timeout(cam->iowait,
743 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
744 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
745 cam_err(cam, "Timeout waiting for DMA to end\n");
746 /* This would be bad news - what now? */
747 spin_lock_irqsave(&cam->dev_lock, flags);
749 cafe_ctlr_irq_disable(cam);
750 spin_unlock_irqrestore(&cam->dev_lock, flags);
756 static void cafe_ctlr_power_up(struct cafe_camera *cam)
760 spin_lock_irqsave(&cam->dev_lock, flags);
761 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
763 * Part one of the sensor dance: turn the global
766 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
767 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
769 * Put the sensor into operational mode (assumes OLPC-style
770 * wiring). Control 0 is reset - set to 1 to operate.
771 * Control 1 is power down, set to 0 to operate.
773 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
774 /* mdelay(1); */ /* Marvell says 1ms will do it */
775 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
776 /* mdelay(1); */ /* Enough? */
777 spin_unlock_irqrestore(&cam->dev_lock, flags);
778 msleep(5); /* Just to be sure */
781 static void cafe_ctlr_power_down(struct cafe_camera *cam)
785 spin_lock_irqsave(&cam->dev_lock, flags);
786 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
787 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
788 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
789 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
790 spin_unlock_irqrestore(&cam->dev_lock, flags);
793 /* -------------------------------------------------------------------- */
795 * Communications with the sensor.
798 static int __cafe_cam_reset(struct cafe_camera *cam)
800 return sensor_call(cam, core, reset, 0);
804 * We have found the sensor on the i2c. Let's try to have a
807 static int cafe_cam_init(struct cafe_camera *cam)
809 struct v4l2_dbg_chip_ident chip;
812 mutex_lock(&cam->s_mutex);
813 if (cam->state != S_NOTREADY)
814 cam_warn(cam, "Cam init with device in funky state %d",
816 ret = __cafe_cam_reset(cam);
819 chip.ident = V4L2_IDENT_NONE;
820 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
821 chip.match.addr = cam->sensor_addr;
822 ret = sensor_call(cam, core, g_chip_ident, &chip);
825 cam->sensor_type = chip.ident;
826 if (cam->sensor_type != V4L2_IDENT_OV7670) {
827 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
831 /* Get/set parameters? */
835 cafe_ctlr_power_down(cam);
836 mutex_unlock(&cam->s_mutex);
841 * Configure the sensor to match the parameters we have. Caller should
844 static int cafe_cam_set_flip(struct cafe_camera *cam)
846 struct v4l2_control ctrl;
848 memset(&ctrl, 0, sizeof(ctrl));
849 ctrl.id = V4L2_CID_VFLIP;
851 return sensor_call(cam, core, s_ctrl, &ctrl);
855 static int cafe_cam_configure(struct cafe_camera *cam)
857 struct v4l2_mbus_framefmt mbus_fmt;
860 if (cam->state != S_IDLE)
862 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
863 ret = sensor_call(cam, core, init, 0);
865 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
867 * OV7670 does weird things if flip is set *before* format...
869 ret += cafe_cam_set_flip(cam);
873 /* -------------------------------------------------------------------- */
875 * DMA buffer management. These functions need s_mutex held.
878 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
879 * does a get_free_pages() call, and we waste a good chunk of an orderN
880 * allocation. Should try to allocate the whole set in one chunk.
882 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
886 cafe_set_config_needed(cam, 1);
888 cam->dma_buf_size = dma_buf_size;
890 cam->dma_buf_size = cam->pix_format.sizeimage;
895 for (i = 0; i < n_dma_bufs; i++) {
896 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
897 cam->dma_buf_size, cam->dma_handles + i,
899 if (cam->dma_bufs[i] == NULL) {
900 cam_warn(cam, "Failed to allocate DMA buffer\n");
903 /* For debug, remove eventually */
904 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
908 switch (cam->nbufs) {
910 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
911 cam->dma_bufs[0], cam->dma_handles[0]);
914 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
919 cam_warn(cam, "Will limp along with only 2 buffers\n");
925 static void cafe_free_dma_bufs(struct cafe_camera *cam)
929 for (i = 0; i < cam->nbufs; i++) {
930 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
931 cam->dma_bufs[i], cam->dma_handles[i]);
932 cam->dma_bufs[i] = NULL;
941 /* ----------------------------------------------------------------------- */
943 * Here starts the V4L2 interface code.
947 * Read an image from the device.
949 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
950 char __user *buffer, size_t len, loff_t *pos)
955 spin_lock_irqsave(&cam->dev_lock, flags);
956 if (cam->next_buf < 0) {
957 cam_err(cam, "deliver_buffer: No next buffer\n");
958 spin_unlock_irqrestore(&cam->dev_lock, flags);
961 bufno = cam->next_buf;
962 clear_bit(bufno, &cam->flags);
963 if (++(cam->next_buf) >= cam->nbufs)
965 if (! test_bit(cam->next_buf, &cam->flags))
968 spin_unlock_irqrestore(&cam->dev_lock, flags);
970 if (len > cam->pix_format.sizeimage)
971 len = cam->pix_format.sizeimage;
972 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
979 * Get everything ready, and start grabbing frames.
981 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
987 * Configuration. If we still don't have DMA buffers,
988 * make one last, desperate attempt.
991 if (cafe_alloc_dma_bufs(cam, 0))
994 if (cafe_needs_config(cam)) {
995 cafe_cam_configure(cam);
996 ret = cafe_ctlr_configure(cam);
1004 spin_lock_irqsave(&cam->dev_lock, flags);
1005 cafe_reset_buffers(cam);
1006 cafe_ctlr_irq_enable(cam);
1008 cafe_ctlr_start(cam);
1009 spin_unlock_irqrestore(&cam->dev_lock, flags);
1014 static ssize_t cafe_v4l_read(struct file *filp,
1015 char __user *buffer, size_t len, loff_t *pos)
1017 struct cafe_camera *cam = filp->private_data;
1021 * Perhaps we're in speculative read mode and already
1024 mutex_lock(&cam->s_mutex);
1025 if (cam->state == S_SPECREAD) {
1026 if (cam->next_buf >= 0) {
1027 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1031 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1034 } else if (cam->state != S_IDLE) {
1040 * v4l2: multiple processes can open the device, but only
1041 * one gets to grab data from it.
1043 if (cam->owner && cam->owner != filp) {
1050 * Do setup if need be.
1052 if (cam->state != S_SPECREAD) {
1053 ret = cafe_read_setup(cam, S_SINGLEREAD);
1058 * Wait for something to happen. This should probably
1059 * be interruptible (FIXME).
1061 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1062 if (cam->next_buf < 0) {
1063 cam_err(cam, "read() operation timed out\n");
1064 cafe_ctlr_stop_dma(cam);
1069 * Give them their data and we should be done.
1071 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1074 mutex_unlock(&cam->s_mutex);
1086 * Streaming I/O support.
1091 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1092 enum v4l2_buf_type type)
1094 struct cafe_camera *cam = filp->private_data;
1097 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1099 mutex_lock(&cam->s_mutex);
1100 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1104 ret = cafe_read_setup(cam, S_STREAMING);
1107 mutex_unlock(&cam->s_mutex);
1113 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1114 enum v4l2_buf_type type)
1116 struct cafe_camera *cam = filp->private_data;
1119 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1121 mutex_lock(&cam->s_mutex);
1122 if (cam->state != S_STREAMING)
1125 cafe_ctlr_stop_dma(cam);
1129 mutex_unlock(&cam->s_mutex);
1136 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1138 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1140 INIT_LIST_HEAD(&buf->list);
1141 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1142 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1143 if (buf->buffer == NULL)
1148 buf->v4lbuf.index = index;
1149 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1150 buf->v4lbuf.field = V4L2_FIELD_NONE;
1151 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1153 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1154 * just uses the length times the index, but the spec warns
1155 * against doing just that - vma merging problems. So we
1156 * leave a gap between each pair of buffers.
1158 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1162 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1167 * If any buffers are mapped, we cannot free them at all.
1169 for (i = 0; i < cam->n_sbufs; i++)
1170 if (cam->sb_bufs[i].mapcount > 0)
1175 for (i = 0; i < cam->n_sbufs; i++)
1176 vfree(cam->sb_bufs[i].buffer);
1178 kfree(cam->sb_bufs);
1179 cam->sb_bufs = NULL;
1180 INIT_LIST_HEAD(&cam->sb_avail);
1181 INIT_LIST_HEAD(&cam->sb_full);
1187 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1188 struct v4l2_requestbuffers *req)
1190 struct cafe_camera *cam = filp->private_data;
1191 int ret = 0; /* Silence warning */
1194 * Make sure it's something we can do. User pointers could be
1195 * implemented without great pain, but that's not been done yet.
1197 if (req->memory != V4L2_MEMORY_MMAP)
1200 * If they ask for zero buffers, they really want us to stop streaming
1201 * (if it's happening) and free everything. Should we check owner?
1203 mutex_lock(&cam->s_mutex);
1204 if (req->count == 0) {
1205 if (cam->state == S_STREAMING)
1206 cafe_ctlr_stop_dma(cam);
1207 ret = cafe_free_sio_buffers (cam);
1211 * Device needs to be idle and working. We *could* try to do the
1212 * right thing in S_SPECREAD by shutting things down, but it
1213 * probably doesn't matter.
1215 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1221 if (req->count < min_buffers)
1222 req->count = min_buffers;
1223 else if (req->count > max_buffers)
1224 req->count = max_buffers;
1225 if (cam->n_sbufs > 0) {
1226 ret = cafe_free_sio_buffers(cam);
1231 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1233 if (cam->sb_bufs == NULL) {
1237 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1238 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1243 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1244 kfree(cam->sb_bufs);
1245 req->count = cam->n_sbufs; /* In case of partial success */
1248 mutex_unlock(&cam->s_mutex);
1253 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1254 struct v4l2_buffer *buf)
1256 struct cafe_camera *cam = filp->private_data;
1259 mutex_lock(&cam->s_mutex);
1260 if (buf->index >= cam->n_sbufs)
1262 *buf = cam->sb_bufs[buf->index].v4lbuf;
1265 mutex_unlock(&cam->s_mutex);
1269 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1270 struct v4l2_buffer *buf)
1272 struct cafe_camera *cam = filp->private_data;
1273 struct cafe_sio_buffer *sbuf;
1275 unsigned long flags;
1277 mutex_lock(&cam->s_mutex);
1278 if (buf->index >= cam->n_sbufs)
1280 sbuf = cam->sb_bufs + buf->index;
1281 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1282 ret = 0; /* Already queued?? */
1285 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1286 /* Spec doesn't say anything, seems appropriate tho */
1290 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1291 spin_lock_irqsave(&cam->dev_lock, flags);
1292 list_add(&sbuf->list, &cam->sb_avail);
1293 spin_unlock_irqrestore(&cam->dev_lock, flags);
1296 mutex_unlock(&cam->s_mutex);
1300 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1301 struct v4l2_buffer *buf)
1303 struct cafe_camera *cam = filp->private_data;
1304 struct cafe_sio_buffer *sbuf;
1306 unsigned long flags;
1308 mutex_lock(&cam->s_mutex);
1309 if (cam->state != S_STREAMING)
1311 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1316 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1317 mutex_unlock(&cam->s_mutex);
1318 if (wait_event_interruptible(cam->iowait,
1319 !list_empty(&cam->sb_full))) {
1323 mutex_lock(&cam->s_mutex);
1326 if (cam->state != S_STREAMING)
1329 spin_lock_irqsave(&cam->dev_lock, flags);
1330 /* Should probably recheck !list_empty() here */
1331 sbuf = list_entry(cam->sb_full.next,
1332 struct cafe_sio_buffer, list);
1333 list_del_init(&sbuf->list);
1334 spin_unlock_irqrestore(&cam->dev_lock, flags);
1335 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1336 *buf = sbuf->v4lbuf;
1341 mutex_unlock(&cam->s_mutex);
1348 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1350 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1352 * Locking: done under mmap_sem, so we don't need to
1353 * go back to the camera lock here.
1359 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1361 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1363 mutex_lock(&sbuf->cam->s_mutex);
1365 /* Docs say we should stop I/O too... */
1366 if (sbuf->mapcount == 0)
1367 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1368 mutex_unlock(&sbuf->cam->s_mutex);
1371 static const struct vm_operations_struct cafe_v4l_vm_ops = {
1372 .open = cafe_v4l_vm_open,
1373 .close = cafe_v4l_vm_close
1377 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1379 struct cafe_camera *cam = filp->private_data;
1380 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1383 struct cafe_sio_buffer *sbuf = NULL;
1385 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1388 * Find the buffer they are looking for.
1390 mutex_lock(&cam->s_mutex);
1391 for (i = 0; i < cam->n_sbufs; i++)
1392 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1393 sbuf = cam->sb_bufs + i;
1399 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1402 vma->vm_flags |= VM_DONTEXPAND;
1403 vma->vm_private_data = sbuf;
1404 vma->vm_ops = &cafe_v4l_vm_ops;
1405 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1406 cafe_v4l_vm_open(vma);
1409 mutex_unlock(&cam->s_mutex);
1415 static int cafe_v4l_open(struct file *filp)
1417 struct cafe_camera *cam = video_drvdata(filp);
1419 filp->private_data = cam;
1421 mutex_lock(&cam->s_mutex);
1422 if (cam->users == 0) {
1423 cafe_ctlr_power_up(cam);
1424 __cafe_cam_reset(cam);
1425 cafe_set_config_needed(cam, 1);
1426 /* FIXME make sure this is complete */
1429 mutex_unlock(&cam->s_mutex);
1434 static int cafe_v4l_release(struct file *filp)
1436 struct cafe_camera *cam = filp->private_data;
1438 mutex_lock(&cam->s_mutex);
1440 if (filp == cam->owner) {
1441 cafe_ctlr_stop_dma(cam);
1442 cafe_free_sio_buffers(cam);
1445 if (cam->users == 0) {
1446 cafe_ctlr_power_down(cam);
1447 if (alloc_bufs_at_read)
1448 cafe_free_dma_bufs(cam);
1450 mutex_unlock(&cam->s_mutex);
1456 static unsigned int cafe_v4l_poll(struct file *filp,
1457 struct poll_table_struct *pt)
1459 struct cafe_camera *cam = filp->private_data;
1461 poll_wait(filp, &cam->iowait, pt);
1462 if (cam->next_buf >= 0)
1463 return POLLIN | POLLRDNORM;
1469 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1470 struct v4l2_queryctrl *qc)
1472 struct cafe_camera *cam = priv;
1475 mutex_lock(&cam->s_mutex);
1476 ret = sensor_call(cam, core, queryctrl, qc);
1477 mutex_unlock(&cam->s_mutex);
1482 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1483 struct v4l2_control *ctrl)
1485 struct cafe_camera *cam = priv;
1488 mutex_lock(&cam->s_mutex);
1489 ret = sensor_call(cam, core, g_ctrl, ctrl);
1490 mutex_unlock(&cam->s_mutex);
1495 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1496 struct v4l2_control *ctrl)
1498 struct cafe_camera *cam = priv;
1501 mutex_lock(&cam->s_mutex);
1502 ret = sensor_call(cam, core, s_ctrl, ctrl);
1503 mutex_unlock(&cam->s_mutex);
1511 static int cafe_vidioc_querycap(struct file *file, void *priv,
1512 struct v4l2_capability *cap)
1514 strcpy(cap->driver, "cafe_ccic");
1515 strcpy(cap->card, "cafe_ccic");
1516 cap->version = CAFE_VERSION;
1517 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1518 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1524 * The default format we use until somebody says otherwise.
1526 static const struct v4l2_pix_format cafe_def_pix_format = {
1528 .height = VGA_HEIGHT,
1529 .pixelformat = V4L2_PIX_FMT_YUYV,
1530 .field = V4L2_FIELD_NONE,
1531 .bytesperline = VGA_WIDTH*2,
1532 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1535 static const enum v4l2_mbus_pixelcode cafe_def_mbus_code =
1536 V4L2_MBUS_FMT_YUYV8_2X8;
1538 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1539 void *priv, struct v4l2_fmtdesc *fmt)
1541 if (fmt->index >= N_CAFE_FMTS)
1543 strlcpy(fmt->description, cafe_formats[fmt->index].desc,
1544 sizeof(fmt->description));
1545 fmt->pixelformat = cafe_formats[fmt->index].pixelformat;
1549 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1550 struct v4l2_format *fmt)
1552 struct cafe_camera *cam = priv;
1553 struct cafe_format_struct *f;
1554 struct v4l2_pix_format *pix = &fmt->fmt.pix;
1555 struct v4l2_mbus_framefmt mbus_fmt;
1558 f = cafe_find_format(pix->pixelformat);
1559 pix->pixelformat = f->pixelformat;
1560 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
1561 mutex_lock(&cam->s_mutex);
1562 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
1563 mutex_unlock(&cam->s_mutex);
1564 v4l2_fill_pix_format(pix, &mbus_fmt);
1565 pix->bytesperline = pix->width * f->bpp;
1566 pix->sizeimage = pix->height * pix->bytesperline;
1570 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1571 struct v4l2_format *fmt)
1573 struct cafe_camera *cam = priv;
1574 struct cafe_format_struct *f;
1578 * Can't do anything if the device is not idle
1579 * Also can't if there are streaming buffers in place.
1581 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1584 f = cafe_find_format(fmt->fmt.pix.pixelformat);
1587 * See if the formatting works in principle.
1589 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1593 * Now we start to change things for real, so let's do it
1596 mutex_lock(&cam->s_mutex);
1597 cam->pix_format = fmt->fmt.pix;
1598 cam->mbus_code = f->mbus_code;
1601 * Make sure we have appropriate DMA buffers.
1604 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1605 cafe_free_dma_bufs(cam);
1606 if (cam->nbufs == 0) {
1607 if (cafe_alloc_dma_bufs(cam, 0))
1611 * It looks like this might work, so let's program the sensor.
1613 ret = cafe_cam_configure(cam);
1615 ret = cafe_ctlr_configure(cam);
1617 mutex_unlock(&cam->s_mutex);
1622 * Return our stored notion of how the camera is/should be configured.
1623 * The V4l2 spec wants us to be smarter, and actually get this from
1624 * the camera (and not mess with it at open time). Someday.
1626 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1627 struct v4l2_format *f)
1629 struct cafe_camera *cam = priv;
1631 f->fmt.pix = cam->pix_format;
1636 * We only have one input - the sensor - so minimize the nonsense here.
1638 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1639 struct v4l2_input *input)
1641 if (input->index != 0)
1644 input->type = V4L2_INPUT_TYPE_CAMERA;
1645 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1646 strcpy(input->name, "Camera");
1650 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1656 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1664 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1670 * G/S_PARM. Most of this is done by the sensor, but we are
1671 * the level which controls the number of read buffers.
1673 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1674 struct v4l2_streamparm *parms)
1676 struct cafe_camera *cam = priv;
1679 mutex_lock(&cam->s_mutex);
1680 ret = sensor_call(cam, video, g_parm, parms);
1681 mutex_unlock(&cam->s_mutex);
1682 parms->parm.capture.readbuffers = n_dma_bufs;
1686 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1687 struct v4l2_streamparm *parms)
1689 struct cafe_camera *cam = priv;
1692 mutex_lock(&cam->s_mutex);
1693 ret = sensor_call(cam, video, s_parm, parms);
1694 mutex_unlock(&cam->s_mutex);
1695 parms->parm.capture.readbuffers = n_dma_bufs;
1699 static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1700 struct v4l2_dbg_chip_ident *chip)
1702 struct cafe_camera *cam = priv;
1704 chip->ident = V4L2_IDENT_NONE;
1706 if (v4l2_chip_match_host(&chip->match)) {
1707 chip->ident = V4L2_IDENT_CAFE;
1710 return sensor_call(cam, core, g_chip_ident, chip);
1713 #ifdef CONFIG_VIDEO_ADV_DEBUG
1714 static int cafe_vidioc_g_register(struct file *file, void *priv,
1715 struct v4l2_dbg_register *reg)
1717 struct cafe_camera *cam = priv;
1719 if (v4l2_chip_match_host(®->match)) {
1720 reg->val = cafe_reg_read(cam, reg->reg);
1724 return sensor_call(cam, core, g_register, reg);
1727 static int cafe_vidioc_s_register(struct file *file, void *priv,
1728 struct v4l2_dbg_register *reg)
1730 struct cafe_camera *cam = priv;
1732 if (v4l2_chip_match_host(®->match)) {
1733 cafe_reg_write(cam, reg->reg, reg->val);
1736 return sensor_call(cam, core, s_register, reg);
1741 * This template device holds all of those v4l2 methods; we
1742 * clone it for specific real devices.
1745 static const struct v4l2_file_operations cafe_v4l_fops = {
1746 .owner = THIS_MODULE,
1747 .open = cafe_v4l_open,
1748 .release = cafe_v4l_release,
1749 .read = cafe_v4l_read,
1750 .poll = cafe_v4l_poll,
1751 .mmap = cafe_v4l_mmap,
1752 .ioctl = video_ioctl2,
1755 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1756 .vidioc_querycap = cafe_vidioc_querycap,
1757 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1758 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1759 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1760 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1761 .vidioc_enum_input = cafe_vidioc_enum_input,
1762 .vidioc_g_input = cafe_vidioc_g_input,
1763 .vidioc_s_input = cafe_vidioc_s_input,
1764 .vidioc_s_std = cafe_vidioc_s_std,
1765 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1766 .vidioc_querybuf = cafe_vidioc_querybuf,
1767 .vidioc_qbuf = cafe_vidioc_qbuf,
1768 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1769 .vidioc_streamon = cafe_vidioc_streamon,
1770 .vidioc_streamoff = cafe_vidioc_streamoff,
1771 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1772 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1773 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1774 .vidioc_g_parm = cafe_vidioc_g_parm,
1775 .vidioc_s_parm = cafe_vidioc_s_parm,
1776 .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
1777 #ifdef CONFIG_VIDEO_ADV_DEBUG
1778 .vidioc_g_register = cafe_vidioc_g_register,
1779 .vidioc_s_register = cafe_vidioc_s_register,
1783 static struct video_device cafe_v4l_template = {
1785 .tvnorms = V4L2_STD_NTSC_M,
1786 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1788 .fops = &cafe_v4l_fops,
1789 .ioctl_ops = &cafe_v4l_ioctl_ops,
1790 .release = video_device_release_empty,
1794 /* ---------------------------------------------------------------------- */
1796 * Interrupt handler stuff
1801 static void cafe_frame_tasklet(unsigned long data)
1803 struct cafe_camera *cam = (struct cafe_camera *) data;
1805 unsigned long flags;
1806 struct cafe_sio_buffer *sbuf;
1808 spin_lock_irqsave(&cam->dev_lock, flags);
1809 for (i = 0; i < cam->nbufs; i++) {
1810 int bufno = cam->next_buf;
1811 if (bufno < 0) { /* "will never happen" */
1812 cam_err(cam, "No valid bufs in tasklet!\n");
1815 if (++(cam->next_buf) >= cam->nbufs)
1817 if (! test_bit(bufno, &cam->flags))
1819 if (list_empty(&cam->sb_avail))
1820 break; /* Leave it valid, hope for better later */
1821 clear_bit(bufno, &cam->flags);
1822 sbuf = list_entry(cam->sb_avail.next,
1823 struct cafe_sio_buffer, list);
1825 * Drop the lock during the big copy. This *should* be safe...
1827 spin_unlock_irqrestore(&cam->dev_lock, flags);
1828 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1829 cam->pix_format.sizeimage);
1830 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1831 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1832 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1833 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1834 spin_lock_irqsave(&cam->dev_lock, flags);
1835 list_move_tail(&sbuf->list, &cam->sb_full);
1837 if (! list_empty(&cam->sb_full))
1838 wake_up(&cam->iowait);
1839 spin_unlock_irqrestore(&cam->dev_lock, flags);
1844 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1847 * Basic frame housekeeping.
1849 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1850 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1851 set_bit(frame, &cam->flags);
1852 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1853 if (cam->next_buf < 0)
1854 cam->next_buf = frame;
1855 cam->buf_seq[frame] = ++(cam->sequence);
1857 switch (cam->state) {
1859 * If in single read mode, try going speculative.
1862 cam->state = S_SPECREAD;
1863 cam->specframes = 0;
1864 wake_up(&cam->iowait);
1868 * If we are already doing speculative reads, and nobody is
1869 * reading them, just stop.
1872 if (++(cam->specframes) >= cam->nbufs) {
1873 cafe_ctlr_stop(cam);
1874 cafe_ctlr_irq_disable(cam);
1875 cam->state = S_IDLE;
1877 wake_up(&cam->iowait);
1880 * For the streaming case, we defer the real work to the
1883 * FIXME: if the application is not consuming the buffers,
1884 * we should eventually put things on hold and restart in
1888 tasklet_schedule(&cam->s_tasklet);
1892 cam_err(cam, "Frame interrupt in non-operational state\n");
1900 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1904 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1906 * Handle any frame completions. There really should
1907 * not be more than one of these, or we have fallen
1910 for (frame = 0; frame < cam->nbufs; frame++)
1911 if (irqs & (IRQ_EOF0 << frame))
1912 cafe_frame_complete(cam, frame);
1914 * If a frame starts, note that we have DMA active. This
1915 * code assumes that we won't get multiple frame interrupts
1916 * at once; may want to rethink that.
1918 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1919 set_bit(CF_DMA_ACTIVE, &cam->flags);
1924 static irqreturn_t cafe_irq(int irq, void *data)
1926 struct cafe_camera *cam = data;
1929 spin_lock(&cam->dev_lock);
1930 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1931 if ((irqs & ALLIRQS) == 0) {
1932 spin_unlock(&cam->dev_lock);
1935 if (irqs & FRAMEIRQS)
1936 cafe_frame_irq(cam, irqs);
1937 if (irqs & TWSIIRQS) {
1938 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1939 wake_up(&cam->smbus_wait);
1941 spin_unlock(&cam->dev_lock);
1946 /* -------------------------------------------------------------------------- */
1948 * PCI interface stuff.
1951 static int cafe_pci_probe(struct pci_dev *pdev,
1952 const struct pci_device_id *id)
1955 struct cafe_camera *cam;
1958 * Start putting together one of our big camera structures.
1961 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1964 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1968 mutex_init(&cam->s_mutex);
1969 spin_lock_init(&cam->dev_lock);
1970 cam->state = S_NOTREADY;
1971 cafe_set_config_needed(cam, 1);
1972 init_waitqueue_head(&cam->smbus_wait);
1973 init_waitqueue_head(&cam->iowait);
1975 cam->pix_format = cafe_def_pix_format;
1976 cam->mbus_code = cafe_def_mbus_code;
1977 INIT_LIST_HEAD(&cam->dev_list);
1978 INIT_LIST_HEAD(&cam->sb_avail);
1979 INIT_LIST_HEAD(&cam->sb_full);
1980 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
1982 * Get set up on the PCI bus.
1984 ret = pci_enable_device(pdev);
1987 pci_set_master(pdev);
1990 cam->regs = pci_iomap(pdev, 0, 0);
1992 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
1995 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
1999 * Initialize the controller and leave it powered up. It will
2000 * stay that way until the sensor driver shows up.
2002 cafe_ctlr_init(cam);
2003 cafe_ctlr_power_up(cam);
2005 * Set up I2C/SMBUS communications. We have to drop the mutex here
2006 * because the sensor could attach in this call chain, leading to
2007 * unsightly deadlocks.
2009 ret = cafe_smbus_setup(cam);
2013 cam->sensor_addr = 0x42;
2014 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
2015 "ov7670", "ov7670", cam->sensor_addr, NULL);
2016 if (cam->sensor == NULL) {
2020 ret = cafe_cam_init(cam);
2025 * Get the v4l2 setup done.
2027 mutex_lock(&cam->s_mutex);
2028 cam->vdev = cafe_v4l_template;
2029 cam->vdev.debug = 0;
2030 /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
2031 cam->vdev.v4l2_dev = &cam->v4l2_dev;
2032 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
2035 video_set_drvdata(&cam->vdev, cam);
2038 * If so requested, try to get our DMA buffers now.
2040 if (!alloc_bufs_at_read) {
2041 if (cafe_alloc_dma_bufs(cam, 1))
2042 cam_warn(cam, "Unable to alloc DMA buffers at load"
2043 " will try again later.");
2046 mutex_unlock(&cam->s_mutex);
2050 mutex_unlock(&cam->s_mutex);
2052 cafe_smbus_shutdown(cam);
2054 cafe_ctlr_power_down(cam);
2055 free_irq(pdev->irq, cam);
2057 pci_iounmap(pdev, cam->regs);
2059 v4l2_device_unregister(&cam->v4l2_dev);
2068 * Shut down an initialized device
2070 static void cafe_shutdown(struct cafe_camera *cam)
2072 /* FIXME: Make sure we take care of everything here */
2073 if (cam->n_sbufs > 0)
2074 /* What if they are still mapped? Shouldn't be, but... */
2075 cafe_free_sio_buffers(cam);
2076 cafe_ctlr_stop_dma(cam);
2077 cafe_ctlr_power_down(cam);
2078 cafe_smbus_shutdown(cam);
2079 cafe_free_dma_bufs(cam);
2080 free_irq(cam->pdev->irq, cam);
2081 pci_iounmap(cam->pdev, cam->regs);
2082 video_unregister_device(&cam->vdev);
2086 static void cafe_pci_remove(struct pci_dev *pdev)
2088 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2089 struct cafe_camera *cam = to_cam(v4l2_dev);
2092 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2095 mutex_lock(&cam->s_mutex);
2097 cam_warn(cam, "Removing a device with users!\n");
2099 v4l2_device_unregister(&cam->v4l2_dev);
2101 /* No unlock - it no longer exists */
2107 * Basic power management.
2109 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2111 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2112 struct cafe_camera *cam = to_cam(v4l2_dev);
2114 enum cafe_state cstate;
2116 ret = pci_save_state(pdev);
2119 cstate = cam->state; /* HACK - stop_dma sets to idle */
2120 cafe_ctlr_stop_dma(cam);
2121 cafe_ctlr_power_down(cam);
2122 pci_disable_device(pdev);
2123 cam->state = cstate;
2128 static int cafe_pci_resume(struct pci_dev *pdev)
2130 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2131 struct cafe_camera *cam = to_cam(v4l2_dev);
2134 ret = pci_restore_state(pdev);
2137 ret = pci_enable_device(pdev);
2140 cam_warn(cam, "Unable to re-enable device on resume!\n");
2143 cafe_ctlr_init(cam);
2144 cafe_ctlr_power_down(cam);
2146 mutex_lock(&cam->s_mutex);
2147 if (cam->users > 0) {
2148 cafe_ctlr_power_up(cam);
2149 __cafe_cam_reset(cam);
2151 mutex_unlock(&cam->s_mutex);
2153 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2154 if (cam->state == S_SPECREAD)
2155 cam->state = S_IDLE; /* Don't bother restarting */
2156 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2157 ret = cafe_read_setup(cam, cam->state);
2161 #endif /* CONFIG_PM */
2164 static struct pci_device_id cafe_ids[] = {
2165 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2166 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2170 MODULE_DEVICE_TABLE(pci, cafe_ids);
2172 static struct pci_driver cafe_pci_driver = {
2173 .name = "cafe1000-ccic",
2174 .id_table = cafe_ids,
2175 .probe = cafe_pci_probe,
2176 .remove = cafe_pci_remove,
2178 .suspend = cafe_pci_suspend,
2179 .resume = cafe_pci_resume,
2186 static int __init cafe_init(void)
2190 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2192 ret = pci_register_driver(&cafe_pci_driver);
2194 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2204 static void __exit cafe_exit(void)
2206 pci_unregister_driver(&cafe_pci_driver);
2209 module_init(cafe_init);
2210 module_exit(cafe_exit);