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)
366 DEFINE_WAIT(the_wait);
368 spin_lock_irqsave(&cam->dev_lock, flags);
369 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
370 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
372 * Marvell sez set clkdiv to all 1's for now.
374 rval |= TWSIC0_CLKDIV;
375 cafe_reg_write(cam, REG_TWSIC0, rval);
376 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
377 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
378 cafe_reg_write(cam, REG_TWSIC1, rval);
379 spin_unlock_irqrestore(&cam->dev_lock, flags);
382 * Time to wait for the write to complete. THIS IS A RACY
383 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
384 * register too quickly after starting the operation sends
385 * the device into a place that may be kinder and better, but
386 * which is absolutely useless for controlling the sensor. In
387 * practice we have plenty of time to get into our sleep state
388 * before the interrupt hits, and the worst case is that we
389 * time out and then see that things completed, so this seems
390 * the best way for now.
393 prepare_to_wait(&cam->smbus_wait, &the_wait,
394 TASK_UNINTERRUPTIBLE);
395 schedule_timeout(1); /* even 1 jiffy is too long */
396 finish_wait(&cam->smbus_wait, &the_wait);
397 } while (!cafe_smbus_write_done(cam));
399 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
400 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
403 spin_lock_irqsave(&cam->dev_lock, flags);
404 rval = cafe_reg_read(cam, REG_TWSIC1);
405 spin_unlock_irqrestore(&cam->dev_lock, flags);
407 if (rval & TWSIC1_WSTAT) {
408 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
412 if (rval & TWSIC1_ERROR) {
413 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
422 static int cafe_smbus_read_done(struct cafe_camera *cam)
428 * We must delay after the interrupt, or the controller gets confused
429 * and never does give us good status. Fortunately, we don't do this
433 spin_lock_irqsave(&cam->dev_lock, flags);
434 c1 = cafe_reg_read(cam, REG_TWSIC1);
435 spin_unlock_irqrestore(&cam->dev_lock, flags);
436 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
441 static int cafe_smbus_read_data(struct cafe_camera *cam,
442 u16 addr, u8 command, u8 *value)
447 spin_lock_irqsave(&cam->dev_lock, flags);
448 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
449 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
451 * Marvel sez set clkdiv to all 1's for now.
453 rval |= TWSIC0_CLKDIV;
454 cafe_reg_write(cam, REG_TWSIC0, rval);
455 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
456 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
457 cafe_reg_write(cam, REG_TWSIC1, rval);
458 spin_unlock_irqrestore(&cam->dev_lock, flags);
460 wait_event_timeout(cam->smbus_wait,
461 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
462 spin_lock_irqsave(&cam->dev_lock, flags);
463 rval = cafe_reg_read(cam, REG_TWSIC1);
464 spin_unlock_irqrestore(&cam->dev_lock, flags);
466 if (rval & TWSIC1_ERROR) {
467 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
470 if (! (rval & TWSIC1_RVALID)) {
471 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
475 *value = rval & 0xff;
480 * Perform a transfer over SMBUS. This thing is called under
481 * the i2c bus lock, so we shouldn't race with ourselves...
483 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
484 unsigned short flags, char rw, u8 command,
485 int size, union i2c_smbus_data *data)
487 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
488 struct cafe_camera *cam = to_cam(v4l2_dev);
492 * This interface would appear to only do byte data ops. OK
493 * it can do word too, but the cam chip has no use for that.
495 if (size != I2C_SMBUS_BYTE_DATA) {
496 cam_err(cam, "funky xfer size %d\n", size);
500 if (rw == I2C_SMBUS_WRITE)
501 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
502 else if (rw == I2C_SMBUS_READ)
503 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
508 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
512 spin_lock_irqsave(&cam->dev_lock, flags);
513 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
514 spin_unlock_irqrestore(&cam->dev_lock, flags);
517 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
519 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
520 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
523 static struct i2c_algorithm cafe_smbus_algo = {
524 .smbus_xfer = cafe_smbus_xfer,
525 .functionality = cafe_smbus_func
528 /* Somebody is on the bus */
529 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
530 static void cafe_ctlr_power_down(struct cafe_camera *cam);
532 static int cafe_smbus_setup(struct cafe_camera *cam)
534 struct i2c_adapter *adap = &cam->i2c_adapter;
537 cafe_smbus_enable_irq(cam);
538 adap->owner = THIS_MODULE;
539 adap->algo = &cafe_smbus_algo;
540 strcpy(adap->name, "cafe_ccic");
541 adap->dev.parent = &cam->pdev->dev;
542 i2c_set_adapdata(adap, &cam->v4l2_dev);
543 ret = i2c_add_adapter(adap);
545 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
549 static void cafe_smbus_shutdown(struct cafe_camera *cam)
551 i2c_del_adapter(&cam->i2c_adapter);
555 /* ------------------------------------------------------------------- */
557 * Deal with the controller.
561 * Do everything we think we need to have the interface operating
562 * according to the desired format.
564 static void cafe_ctlr_dma(struct cafe_camera *cam)
567 * Store the first two Y buffers (we aren't supporting
568 * planar formats for now, so no UV bufs). Then either
569 * set the third if it exists, or tell the controller
572 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
573 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
574 if (cam->nbufs > 2) {
575 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
576 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
579 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
580 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
583 static void cafe_ctlr_image(struct cafe_camera *cam)
586 struct v4l2_pix_format *fmt = &cam->pix_format;
588 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
589 (fmt->bytesperline & IMGSZ_H_MASK);
590 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
591 cafe_reg_write(cam, REG_IMGOFFSET, 0);
592 /* YPITCH just drops the last two bits */
593 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
596 * Tell the controller about the image format we are using.
598 switch (cam->pix_format.pixelformat) {
599 case V4L2_PIX_FMT_YUYV:
600 cafe_reg_write_mask(cam, REG_CTRL0,
601 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
605 case V4L2_PIX_FMT_RGB444:
606 cafe_reg_write_mask(cam, REG_CTRL0,
607 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
612 case V4L2_PIX_FMT_RGB565:
613 cafe_reg_write_mask(cam, REG_CTRL0,
614 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
619 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
623 * Make sure it knows we want to use hsync/vsync.
625 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
631 * Configure the controller for operation; caller holds the
634 static int cafe_ctlr_configure(struct cafe_camera *cam)
638 spin_lock_irqsave(&cam->dev_lock, flags);
640 cafe_ctlr_image(cam);
641 cafe_set_config_needed(cam, 0);
642 spin_unlock_irqrestore(&cam->dev_lock, flags);
646 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
649 * Clear any pending interrupts, since we do not
650 * expect to have I/O active prior to enabling.
652 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
653 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
656 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
658 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
662 * Make the controller start grabbing images. Everything must
663 * be set up before doing this.
665 static void cafe_ctlr_start(struct cafe_camera *cam)
667 /* set_bit performs a read, so no other barrier should be
669 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
672 static void cafe_ctlr_stop(struct cafe_camera *cam)
674 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
677 static void cafe_ctlr_init(struct cafe_camera *cam)
681 spin_lock_irqsave(&cam->dev_lock, flags);
683 * Added magic to bring up the hardware on the B-Test board
685 cafe_reg_write(cam, 0x3038, 0x8);
686 cafe_reg_write(cam, 0x315c, 0x80008);
688 * Go through the dance needed to wake the device up.
689 * Note that these registers are global and shared
690 * with the NAND and SD devices. Interaction between the
691 * three still needs to be examined.
693 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
694 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
695 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
697 * Here we must wait a bit for the controller to come around.
699 spin_unlock_irqrestore(&cam->dev_lock, flags);
701 spin_lock_irqsave(&cam->dev_lock, flags);
703 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
704 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
706 * Make sure it's not powered down.
708 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
710 * Turn off the enable bit. It sure should be off anyway,
711 * but it's good to be sure.
713 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
715 * Mask all interrupts.
717 cafe_reg_write(cam, REG_IRQMASK, 0);
719 * Clock the sensor appropriately. Controller clock should
720 * be 48MHz, sensor "typical" value is half that.
722 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
723 spin_unlock_irqrestore(&cam->dev_lock, flags);
728 * Stop the controller, and don't return until we're really sure that no
729 * further DMA is going on.
731 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
736 * Theory: stop the camera controller (whether it is operating
737 * or not). Delay briefly just in case we race with the SOF
738 * interrupt, then wait until no DMA is active.
740 spin_lock_irqsave(&cam->dev_lock, flags);
742 spin_unlock_irqrestore(&cam->dev_lock, flags);
744 wait_event_timeout(cam->iowait,
745 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
746 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
747 cam_err(cam, "Timeout waiting for DMA to end\n");
748 /* This would be bad news - what now? */
749 spin_lock_irqsave(&cam->dev_lock, flags);
751 cafe_ctlr_irq_disable(cam);
752 spin_unlock_irqrestore(&cam->dev_lock, flags);
758 static void cafe_ctlr_power_up(struct cafe_camera *cam)
762 spin_lock_irqsave(&cam->dev_lock, flags);
763 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
765 * Part one of the sensor dance: turn the global
768 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
769 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
771 * Put the sensor into operational mode (assumes OLPC-style
772 * wiring). Control 0 is reset - set to 1 to operate.
773 * Control 1 is power down, set to 0 to operate.
775 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
776 /* mdelay(1); */ /* Marvell says 1ms will do it */
777 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
778 /* mdelay(1); */ /* Enough? */
779 spin_unlock_irqrestore(&cam->dev_lock, flags);
780 msleep(5); /* Just to be sure */
783 static void cafe_ctlr_power_down(struct cafe_camera *cam)
787 spin_lock_irqsave(&cam->dev_lock, flags);
788 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
789 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
790 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
791 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
792 spin_unlock_irqrestore(&cam->dev_lock, flags);
795 /* -------------------------------------------------------------------- */
797 * Communications with the sensor.
800 static int __cafe_cam_reset(struct cafe_camera *cam)
802 return sensor_call(cam, core, reset, 0);
806 * We have found the sensor on the i2c. Let's try to have a
809 static int cafe_cam_init(struct cafe_camera *cam)
811 struct v4l2_dbg_chip_ident chip;
814 mutex_lock(&cam->s_mutex);
815 if (cam->state != S_NOTREADY)
816 cam_warn(cam, "Cam init with device in funky state %d",
818 ret = __cafe_cam_reset(cam);
821 chip.ident = V4L2_IDENT_NONE;
822 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
823 chip.match.addr = cam->sensor_addr;
824 ret = sensor_call(cam, core, g_chip_ident, &chip);
827 cam->sensor_type = chip.ident;
828 if (cam->sensor_type != V4L2_IDENT_OV7670) {
829 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
833 /* Get/set parameters? */
837 cafe_ctlr_power_down(cam);
838 mutex_unlock(&cam->s_mutex);
843 * Configure the sensor to match the parameters we have. Caller should
846 static int cafe_cam_set_flip(struct cafe_camera *cam)
848 struct v4l2_control ctrl;
850 memset(&ctrl, 0, sizeof(ctrl));
851 ctrl.id = V4L2_CID_VFLIP;
853 return sensor_call(cam, core, s_ctrl, &ctrl);
857 static int cafe_cam_configure(struct cafe_camera *cam)
859 struct v4l2_mbus_framefmt mbus_fmt;
862 if (cam->state != S_IDLE)
864 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
865 ret = sensor_call(cam, core, init, 0);
867 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
869 * OV7670 does weird things if flip is set *before* format...
871 ret += cafe_cam_set_flip(cam);
875 /* -------------------------------------------------------------------- */
877 * DMA buffer management. These functions need s_mutex held.
880 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
881 * does a get_free_pages() call, and we waste a good chunk of an orderN
882 * allocation. Should try to allocate the whole set in one chunk.
884 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
888 cafe_set_config_needed(cam, 1);
890 cam->dma_buf_size = dma_buf_size;
892 cam->dma_buf_size = cam->pix_format.sizeimage;
897 for (i = 0; i < n_dma_bufs; i++) {
898 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
899 cam->dma_buf_size, cam->dma_handles + i,
901 if (cam->dma_bufs[i] == NULL) {
902 cam_warn(cam, "Failed to allocate DMA buffer\n");
905 /* For debug, remove eventually */
906 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
910 switch (cam->nbufs) {
912 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
913 cam->dma_bufs[0], cam->dma_handles[0]);
916 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
921 cam_warn(cam, "Will limp along with only 2 buffers\n");
927 static void cafe_free_dma_bufs(struct cafe_camera *cam)
931 for (i = 0; i < cam->nbufs; i++) {
932 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
933 cam->dma_bufs[i], cam->dma_handles[i]);
934 cam->dma_bufs[i] = NULL;
943 /* ----------------------------------------------------------------------- */
945 * Here starts the V4L2 interface code.
949 * Read an image from the device.
951 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
952 char __user *buffer, size_t len, loff_t *pos)
957 spin_lock_irqsave(&cam->dev_lock, flags);
958 if (cam->next_buf < 0) {
959 cam_err(cam, "deliver_buffer: No next buffer\n");
960 spin_unlock_irqrestore(&cam->dev_lock, flags);
963 bufno = cam->next_buf;
964 clear_bit(bufno, &cam->flags);
965 if (++(cam->next_buf) >= cam->nbufs)
967 if (! test_bit(cam->next_buf, &cam->flags))
970 spin_unlock_irqrestore(&cam->dev_lock, flags);
972 if (len > cam->pix_format.sizeimage)
973 len = cam->pix_format.sizeimage;
974 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
981 * Get everything ready, and start grabbing frames.
983 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
989 * Configuration. If we still don't have DMA buffers,
990 * make one last, desperate attempt.
993 if (cafe_alloc_dma_bufs(cam, 0))
996 if (cafe_needs_config(cam)) {
997 cafe_cam_configure(cam);
998 ret = cafe_ctlr_configure(cam);
1006 spin_lock_irqsave(&cam->dev_lock, flags);
1007 cafe_reset_buffers(cam);
1008 cafe_ctlr_irq_enable(cam);
1010 cafe_ctlr_start(cam);
1011 spin_unlock_irqrestore(&cam->dev_lock, flags);
1016 static ssize_t cafe_v4l_read(struct file *filp,
1017 char __user *buffer, size_t len, loff_t *pos)
1019 struct cafe_camera *cam = filp->private_data;
1023 * Perhaps we're in speculative read mode and already
1026 mutex_lock(&cam->s_mutex);
1027 if (cam->state == S_SPECREAD) {
1028 if (cam->next_buf >= 0) {
1029 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1033 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1036 } else if (cam->state != S_IDLE) {
1042 * v4l2: multiple processes can open the device, but only
1043 * one gets to grab data from it.
1045 if (cam->owner && cam->owner != filp) {
1052 * Do setup if need be.
1054 if (cam->state != S_SPECREAD) {
1055 ret = cafe_read_setup(cam, S_SINGLEREAD);
1060 * Wait for something to happen. This should probably
1061 * be interruptible (FIXME).
1063 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1064 if (cam->next_buf < 0) {
1065 cam_err(cam, "read() operation timed out\n");
1066 cafe_ctlr_stop_dma(cam);
1071 * Give them their data and we should be done.
1073 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1076 mutex_unlock(&cam->s_mutex);
1088 * Streaming I/O support.
1093 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1094 enum v4l2_buf_type type)
1096 struct cafe_camera *cam = filp->private_data;
1099 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1101 mutex_lock(&cam->s_mutex);
1102 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1106 ret = cafe_read_setup(cam, S_STREAMING);
1109 mutex_unlock(&cam->s_mutex);
1115 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1116 enum v4l2_buf_type type)
1118 struct cafe_camera *cam = filp->private_data;
1121 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1123 mutex_lock(&cam->s_mutex);
1124 if (cam->state != S_STREAMING)
1127 cafe_ctlr_stop_dma(cam);
1131 mutex_unlock(&cam->s_mutex);
1138 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1140 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1142 INIT_LIST_HEAD(&buf->list);
1143 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1144 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1145 if (buf->buffer == NULL)
1150 buf->v4lbuf.index = index;
1151 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1152 buf->v4lbuf.field = V4L2_FIELD_NONE;
1153 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1155 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1156 * just uses the length times the index, but the spec warns
1157 * against doing just that - vma merging problems. So we
1158 * leave a gap between each pair of buffers.
1160 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1164 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1169 * If any buffers are mapped, we cannot free them at all.
1171 for (i = 0; i < cam->n_sbufs; i++)
1172 if (cam->sb_bufs[i].mapcount > 0)
1177 for (i = 0; i < cam->n_sbufs; i++)
1178 vfree(cam->sb_bufs[i].buffer);
1180 kfree(cam->sb_bufs);
1181 cam->sb_bufs = NULL;
1182 INIT_LIST_HEAD(&cam->sb_avail);
1183 INIT_LIST_HEAD(&cam->sb_full);
1189 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1190 struct v4l2_requestbuffers *req)
1192 struct cafe_camera *cam = filp->private_data;
1193 int ret = 0; /* Silence warning */
1196 * Make sure it's something we can do. User pointers could be
1197 * implemented without great pain, but that's not been done yet.
1199 if (req->memory != V4L2_MEMORY_MMAP)
1202 * If they ask for zero buffers, they really want us to stop streaming
1203 * (if it's happening) and free everything. Should we check owner?
1205 mutex_lock(&cam->s_mutex);
1206 if (req->count == 0) {
1207 if (cam->state == S_STREAMING)
1208 cafe_ctlr_stop_dma(cam);
1209 ret = cafe_free_sio_buffers (cam);
1213 * Device needs to be idle and working. We *could* try to do the
1214 * right thing in S_SPECREAD by shutting things down, but it
1215 * probably doesn't matter.
1217 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1223 if (req->count < min_buffers)
1224 req->count = min_buffers;
1225 else if (req->count > max_buffers)
1226 req->count = max_buffers;
1227 if (cam->n_sbufs > 0) {
1228 ret = cafe_free_sio_buffers(cam);
1233 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1235 if (cam->sb_bufs == NULL) {
1239 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1240 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1245 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1246 kfree(cam->sb_bufs);
1247 req->count = cam->n_sbufs; /* In case of partial success */
1250 mutex_unlock(&cam->s_mutex);
1255 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1256 struct v4l2_buffer *buf)
1258 struct cafe_camera *cam = filp->private_data;
1261 mutex_lock(&cam->s_mutex);
1262 if (buf->index >= cam->n_sbufs)
1264 *buf = cam->sb_bufs[buf->index].v4lbuf;
1267 mutex_unlock(&cam->s_mutex);
1271 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1272 struct v4l2_buffer *buf)
1274 struct cafe_camera *cam = filp->private_data;
1275 struct cafe_sio_buffer *sbuf;
1277 unsigned long flags;
1279 mutex_lock(&cam->s_mutex);
1280 if (buf->index >= cam->n_sbufs)
1282 sbuf = cam->sb_bufs + buf->index;
1283 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1284 ret = 0; /* Already queued?? */
1287 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1288 /* Spec doesn't say anything, seems appropriate tho */
1292 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1293 spin_lock_irqsave(&cam->dev_lock, flags);
1294 list_add(&sbuf->list, &cam->sb_avail);
1295 spin_unlock_irqrestore(&cam->dev_lock, flags);
1298 mutex_unlock(&cam->s_mutex);
1302 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1303 struct v4l2_buffer *buf)
1305 struct cafe_camera *cam = filp->private_data;
1306 struct cafe_sio_buffer *sbuf;
1308 unsigned long flags;
1310 mutex_lock(&cam->s_mutex);
1311 if (cam->state != S_STREAMING)
1313 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1318 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1319 mutex_unlock(&cam->s_mutex);
1320 if (wait_event_interruptible(cam->iowait,
1321 !list_empty(&cam->sb_full))) {
1325 mutex_lock(&cam->s_mutex);
1328 if (cam->state != S_STREAMING)
1331 spin_lock_irqsave(&cam->dev_lock, flags);
1332 /* Should probably recheck !list_empty() here */
1333 sbuf = list_entry(cam->sb_full.next,
1334 struct cafe_sio_buffer, list);
1335 list_del_init(&sbuf->list);
1336 spin_unlock_irqrestore(&cam->dev_lock, flags);
1337 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1338 *buf = sbuf->v4lbuf;
1343 mutex_unlock(&cam->s_mutex);
1350 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1352 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1354 * Locking: done under mmap_sem, so we don't need to
1355 * go back to the camera lock here.
1361 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1363 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1365 mutex_lock(&sbuf->cam->s_mutex);
1367 /* Docs say we should stop I/O too... */
1368 if (sbuf->mapcount == 0)
1369 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1370 mutex_unlock(&sbuf->cam->s_mutex);
1373 static const struct vm_operations_struct cafe_v4l_vm_ops = {
1374 .open = cafe_v4l_vm_open,
1375 .close = cafe_v4l_vm_close
1379 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1381 struct cafe_camera *cam = filp->private_data;
1382 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1385 struct cafe_sio_buffer *sbuf = NULL;
1387 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1390 * Find the buffer they are looking for.
1392 mutex_lock(&cam->s_mutex);
1393 for (i = 0; i < cam->n_sbufs; i++)
1394 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1395 sbuf = cam->sb_bufs + i;
1401 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1404 vma->vm_flags |= VM_DONTEXPAND;
1405 vma->vm_private_data = sbuf;
1406 vma->vm_ops = &cafe_v4l_vm_ops;
1407 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1408 cafe_v4l_vm_open(vma);
1411 mutex_unlock(&cam->s_mutex);
1417 static int cafe_v4l_open(struct file *filp)
1419 struct cafe_camera *cam = video_drvdata(filp);
1421 filp->private_data = cam;
1423 mutex_lock(&cam->s_mutex);
1424 if (cam->users == 0) {
1425 cafe_ctlr_power_up(cam);
1426 __cafe_cam_reset(cam);
1427 cafe_set_config_needed(cam, 1);
1428 /* FIXME make sure this is complete */
1431 mutex_unlock(&cam->s_mutex);
1436 static int cafe_v4l_release(struct file *filp)
1438 struct cafe_camera *cam = filp->private_data;
1440 mutex_lock(&cam->s_mutex);
1442 if (filp == cam->owner) {
1443 cafe_ctlr_stop_dma(cam);
1444 cafe_free_sio_buffers(cam);
1447 if (cam->users == 0) {
1448 cafe_ctlr_power_down(cam);
1449 if (alloc_bufs_at_read)
1450 cafe_free_dma_bufs(cam);
1452 mutex_unlock(&cam->s_mutex);
1458 static unsigned int cafe_v4l_poll(struct file *filp,
1459 struct poll_table_struct *pt)
1461 struct cafe_camera *cam = filp->private_data;
1463 poll_wait(filp, &cam->iowait, pt);
1464 if (cam->next_buf >= 0)
1465 return POLLIN | POLLRDNORM;
1471 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1472 struct v4l2_queryctrl *qc)
1474 struct cafe_camera *cam = priv;
1477 mutex_lock(&cam->s_mutex);
1478 ret = sensor_call(cam, core, queryctrl, qc);
1479 mutex_unlock(&cam->s_mutex);
1484 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1485 struct v4l2_control *ctrl)
1487 struct cafe_camera *cam = priv;
1490 mutex_lock(&cam->s_mutex);
1491 ret = sensor_call(cam, core, g_ctrl, ctrl);
1492 mutex_unlock(&cam->s_mutex);
1497 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1498 struct v4l2_control *ctrl)
1500 struct cafe_camera *cam = priv;
1503 mutex_lock(&cam->s_mutex);
1504 ret = sensor_call(cam, core, s_ctrl, ctrl);
1505 mutex_unlock(&cam->s_mutex);
1513 static int cafe_vidioc_querycap(struct file *file, void *priv,
1514 struct v4l2_capability *cap)
1516 strcpy(cap->driver, "cafe_ccic");
1517 strcpy(cap->card, "cafe_ccic");
1518 cap->version = CAFE_VERSION;
1519 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1520 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1526 * The default format we use until somebody says otherwise.
1528 static const struct v4l2_pix_format cafe_def_pix_format = {
1530 .height = VGA_HEIGHT,
1531 .pixelformat = V4L2_PIX_FMT_YUYV,
1532 .field = V4L2_FIELD_NONE,
1533 .bytesperline = VGA_WIDTH*2,
1534 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1537 static const enum v4l2_mbus_pixelcode cafe_def_mbus_code =
1538 V4L2_MBUS_FMT_YUYV8_2X8;
1540 static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1541 void *priv, struct v4l2_fmtdesc *fmt)
1543 if (fmt->index >= N_CAFE_FMTS)
1545 strlcpy(fmt->description, cafe_formats[fmt->index].desc,
1546 sizeof(fmt->description));
1547 fmt->pixelformat = cafe_formats[fmt->index].pixelformat;
1551 static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1552 struct v4l2_format *fmt)
1554 struct cafe_camera *cam = priv;
1555 struct cafe_format_struct *f;
1556 struct v4l2_pix_format *pix = &fmt->fmt.pix;
1557 struct v4l2_mbus_framefmt mbus_fmt;
1560 f = cafe_find_format(pix->pixelformat);
1561 pix->pixelformat = f->pixelformat;
1562 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
1563 mutex_lock(&cam->s_mutex);
1564 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
1565 mutex_unlock(&cam->s_mutex);
1566 v4l2_fill_pix_format(pix, &mbus_fmt);
1567 pix->bytesperline = pix->width * f->bpp;
1568 pix->sizeimage = pix->height * pix->bytesperline;
1572 static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1573 struct v4l2_format *fmt)
1575 struct cafe_camera *cam = priv;
1576 struct cafe_format_struct *f;
1580 * Can't do anything if the device is not idle
1581 * Also can't if there are streaming buffers in place.
1583 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1586 f = cafe_find_format(fmt->fmt.pix.pixelformat);
1589 * See if the formatting works in principle.
1591 ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1595 * Now we start to change things for real, so let's do it
1598 mutex_lock(&cam->s_mutex);
1599 cam->pix_format = fmt->fmt.pix;
1600 cam->mbus_code = f->mbus_code;
1603 * Make sure we have appropriate DMA buffers.
1606 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1607 cafe_free_dma_bufs(cam);
1608 if (cam->nbufs == 0) {
1609 if (cafe_alloc_dma_bufs(cam, 0))
1613 * It looks like this might work, so let's program the sensor.
1615 ret = cafe_cam_configure(cam);
1617 ret = cafe_ctlr_configure(cam);
1619 mutex_unlock(&cam->s_mutex);
1624 * Return our stored notion of how the camera is/should be configured.
1625 * The V4l2 spec wants us to be smarter, and actually get this from
1626 * the camera (and not mess with it at open time). Someday.
1628 static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1629 struct v4l2_format *f)
1631 struct cafe_camera *cam = priv;
1633 f->fmt.pix = cam->pix_format;
1638 * We only have one input - the sensor - so minimize the nonsense here.
1640 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1641 struct v4l2_input *input)
1643 if (input->index != 0)
1646 input->type = V4L2_INPUT_TYPE_CAMERA;
1647 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1648 strcpy(input->name, "Camera");
1652 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1658 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1666 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1672 * G/S_PARM. Most of this is done by the sensor, but we are
1673 * the level which controls the number of read buffers.
1675 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1676 struct v4l2_streamparm *parms)
1678 struct cafe_camera *cam = priv;
1681 mutex_lock(&cam->s_mutex);
1682 ret = sensor_call(cam, video, g_parm, parms);
1683 mutex_unlock(&cam->s_mutex);
1684 parms->parm.capture.readbuffers = n_dma_bufs;
1688 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1689 struct v4l2_streamparm *parms)
1691 struct cafe_camera *cam = priv;
1694 mutex_lock(&cam->s_mutex);
1695 ret = sensor_call(cam, video, s_parm, parms);
1696 mutex_unlock(&cam->s_mutex);
1697 parms->parm.capture.readbuffers = n_dma_bufs;
1701 static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1702 struct v4l2_dbg_chip_ident *chip)
1704 struct cafe_camera *cam = priv;
1706 chip->ident = V4L2_IDENT_NONE;
1708 if (v4l2_chip_match_host(&chip->match)) {
1709 chip->ident = V4L2_IDENT_CAFE;
1712 return sensor_call(cam, core, g_chip_ident, chip);
1715 #ifdef CONFIG_VIDEO_ADV_DEBUG
1716 static int cafe_vidioc_g_register(struct file *file, void *priv,
1717 struct v4l2_dbg_register *reg)
1719 struct cafe_camera *cam = priv;
1721 if (v4l2_chip_match_host(®->match)) {
1722 reg->val = cafe_reg_read(cam, reg->reg);
1726 return sensor_call(cam, core, g_register, reg);
1729 static int cafe_vidioc_s_register(struct file *file, void *priv,
1730 struct v4l2_dbg_register *reg)
1732 struct cafe_camera *cam = priv;
1734 if (v4l2_chip_match_host(®->match)) {
1735 cafe_reg_write(cam, reg->reg, reg->val);
1738 return sensor_call(cam, core, s_register, reg);
1743 * This template device holds all of those v4l2 methods; we
1744 * clone it for specific real devices.
1747 static const struct v4l2_file_operations cafe_v4l_fops = {
1748 .owner = THIS_MODULE,
1749 .open = cafe_v4l_open,
1750 .release = cafe_v4l_release,
1751 .read = cafe_v4l_read,
1752 .poll = cafe_v4l_poll,
1753 .mmap = cafe_v4l_mmap,
1754 .ioctl = video_ioctl2,
1757 static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1758 .vidioc_querycap = cafe_vidioc_querycap,
1759 .vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1760 .vidioc_try_fmt_vid_cap = cafe_vidioc_try_fmt_vid_cap,
1761 .vidioc_s_fmt_vid_cap = cafe_vidioc_s_fmt_vid_cap,
1762 .vidioc_g_fmt_vid_cap = cafe_vidioc_g_fmt_vid_cap,
1763 .vidioc_enum_input = cafe_vidioc_enum_input,
1764 .vidioc_g_input = cafe_vidioc_g_input,
1765 .vidioc_s_input = cafe_vidioc_s_input,
1766 .vidioc_s_std = cafe_vidioc_s_std,
1767 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1768 .vidioc_querybuf = cafe_vidioc_querybuf,
1769 .vidioc_qbuf = cafe_vidioc_qbuf,
1770 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1771 .vidioc_streamon = cafe_vidioc_streamon,
1772 .vidioc_streamoff = cafe_vidioc_streamoff,
1773 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1774 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1775 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1776 .vidioc_g_parm = cafe_vidioc_g_parm,
1777 .vidioc_s_parm = cafe_vidioc_s_parm,
1778 .vidioc_g_chip_ident = cafe_vidioc_g_chip_ident,
1779 #ifdef CONFIG_VIDEO_ADV_DEBUG
1780 .vidioc_g_register = cafe_vidioc_g_register,
1781 .vidioc_s_register = cafe_vidioc_s_register,
1785 static struct video_device cafe_v4l_template = {
1787 .tvnorms = V4L2_STD_NTSC_M,
1788 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1790 .fops = &cafe_v4l_fops,
1791 .ioctl_ops = &cafe_v4l_ioctl_ops,
1792 .release = video_device_release_empty,
1796 /* ---------------------------------------------------------------------- */
1798 * Interrupt handler stuff
1803 static void cafe_frame_tasklet(unsigned long data)
1805 struct cafe_camera *cam = (struct cafe_camera *) data;
1807 unsigned long flags;
1808 struct cafe_sio_buffer *sbuf;
1810 spin_lock_irqsave(&cam->dev_lock, flags);
1811 for (i = 0; i < cam->nbufs; i++) {
1812 int bufno = cam->next_buf;
1813 if (bufno < 0) { /* "will never happen" */
1814 cam_err(cam, "No valid bufs in tasklet!\n");
1817 if (++(cam->next_buf) >= cam->nbufs)
1819 if (! test_bit(bufno, &cam->flags))
1821 if (list_empty(&cam->sb_avail))
1822 break; /* Leave it valid, hope for better later */
1823 clear_bit(bufno, &cam->flags);
1824 sbuf = list_entry(cam->sb_avail.next,
1825 struct cafe_sio_buffer, list);
1827 * Drop the lock during the big copy. This *should* be safe...
1829 spin_unlock_irqrestore(&cam->dev_lock, flags);
1830 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1831 cam->pix_format.sizeimage);
1832 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1833 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1834 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1835 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1836 spin_lock_irqsave(&cam->dev_lock, flags);
1837 list_move_tail(&sbuf->list, &cam->sb_full);
1839 if (! list_empty(&cam->sb_full))
1840 wake_up(&cam->iowait);
1841 spin_unlock_irqrestore(&cam->dev_lock, flags);
1846 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1849 * Basic frame housekeeping.
1851 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1852 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1853 set_bit(frame, &cam->flags);
1854 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1855 if (cam->next_buf < 0)
1856 cam->next_buf = frame;
1857 cam->buf_seq[frame] = ++(cam->sequence);
1859 switch (cam->state) {
1861 * If in single read mode, try going speculative.
1864 cam->state = S_SPECREAD;
1865 cam->specframes = 0;
1866 wake_up(&cam->iowait);
1870 * If we are already doing speculative reads, and nobody is
1871 * reading them, just stop.
1874 if (++(cam->specframes) >= cam->nbufs) {
1875 cafe_ctlr_stop(cam);
1876 cafe_ctlr_irq_disable(cam);
1877 cam->state = S_IDLE;
1879 wake_up(&cam->iowait);
1882 * For the streaming case, we defer the real work to the
1885 * FIXME: if the application is not consuming the buffers,
1886 * we should eventually put things on hold and restart in
1890 tasklet_schedule(&cam->s_tasklet);
1894 cam_err(cam, "Frame interrupt in non-operational state\n");
1902 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1906 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1908 * Handle any frame completions. There really should
1909 * not be more than one of these, or we have fallen
1912 for (frame = 0; frame < cam->nbufs; frame++)
1913 if (irqs & (IRQ_EOF0 << frame))
1914 cafe_frame_complete(cam, frame);
1916 * If a frame starts, note that we have DMA active. This
1917 * code assumes that we won't get multiple frame interrupts
1918 * at once; may want to rethink that.
1920 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1921 set_bit(CF_DMA_ACTIVE, &cam->flags);
1926 static irqreturn_t cafe_irq(int irq, void *data)
1928 struct cafe_camera *cam = data;
1931 spin_lock(&cam->dev_lock);
1932 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1933 if ((irqs & ALLIRQS) == 0) {
1934 spin_unlock(&cam->dev_lock);
1937 if (irqs & FRAMEIRQS)
1938 cafe_frame_irq(cam, irqs);
1939 if (irqs & TWSIIRQS) {
1940 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1941 wake_up(&cam->smbus_wait);
1943 spin_unlock(&cam->dev_lock);
1948 /* -------------------------------------------------------------------------- */
1950 * PCI interface stuff.
1953 static int cafe_pci_probe(struct pci_dev *pdev,
1954 const struct pci_device_id *id)
1957 struct cafe_camera *cam;
1960 * Start putting together one of our big camera structures.
1963 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1966 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1970 mutex_init(&cam->s_mutex);
1971 spin_lock_init(&cam->dev_lock);
1972 cam->state = S_NOTREADY;
1973 cafe_set_config_needed(cam, 1);
1974 init_waitqueue_head(&cam->smbus_wait);
1975 init_waitqueue_head(&cam->iowait);
1977 cam->pix_format = cafe_def_pix_format;
1978 cam->mbus_code = cafe_def_mbus_code;
1979 INIT_LIST_HEAD(&cam->dev_list);
1980 INIT_LIST_HEAD(&cam->sb_avail);
1981 INIT_LIST_HEAD(&cam->sb_full);
1982 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
1984 * Get set up on the PCI bus.
1986 ret = pci_enable_device(pdev);
1989 pci_set_master(pdev);
1992 cam->regs = pci_iomap(pdev, 0, 0);
1994 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
1997 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2001 * Initialize the controller and leave it powered up. It will
2002 * stay that way until the sensor driver shows up.
2004 cafe_ctlr_init(cam);
2005 cafe_ctlr_power_up(cam);
2007 * Set up I2C/SMBUS communications. We have to drop the mutex here
2008 * because the sensor could attach in this call chain, leading to
2009 * unsightly deadlocks.
2011 ret = cafe_smbus_setup(cam);
2015 cam->sensor_addr = 0x42;
2016 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
2017 "ov7670", "ov7670", cam->sensor_addr, NULL);
2018 if (cam->sensor == NULL) {
2022 ret = cafe_cam_init(cam);
2027 * Get the v4l2 setup done.
2029 mutex_lock(&cam->s_mutex);
2030 cam->vdev = cafe_v4l_template;
2031 cam->vdev.debug = 0;
2032 /* cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
2033 cam->vdev.v4l2_dev = &cam->v4l2_dev;
2034 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
2037 video_set_drvdata(&cam->vdev, cam);
2040 * If so requested, try to get our DMA buffers now.
2042 if (!alloc_bufs_at_read) {
2043 if (cafe_alloc_dma_bufs(cam, 1))
2044 cam_warn(cam, "Unable to alloc DMA buffers at load"
2045 " will try again later.");
2048 mutex_unlock(&cam->s_mutex);
2052 mutex_unlock(&cam->s_mutex);
2054 cafe_smbus_shutdown(cam);
2056 cafe_ctlr_power_down(cam);
2057 free_irq(pdev->irq, cam);
2059 pci_iounmap(pdev, cam->regs);
2061 v4l2_device_unregister(&cam->v4l2_dev);
2070 * Shut down an initialized device
2072 static void cafe_shutdown(struct cafe_camera *cam)
2074 /* FIXME: Make sure we take care of everything here */
2075 if (cam->n_sbufs > 0)
2076 /* What if they are still mapped? Shouldn't be, but... */
2077 cafe_free_sio_buffers(cam);
2078 cafe_ctlr_stop_dma(cam);
2079 cafe_ctlr_power_down(cam);
2080 cafe_smbus_shutdown(cam);
2081 cafe_free_dma_bufs(cam);
2082 free_irq(cam->pdev->irq, cam);
2083 pci_iounmap(cam->pdev, cam->regs);
2084 video_unregister_device(&cam->vdev);
2088 static void cafe_pci_remove(struct pci_dev *pdev)
2090 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2091 struct cafe_camera *cam = to_cam(v4l2_dev);
2094 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2097 mutex_lock(&cam->s_mutex);
2099 cam_warn(cam, "Removing a device with users!\n");
2101 v4l2_device_unregister(&cam->v4l2_dev);
2103 /* No unlock - it no longer exists */
2109 * Basic power management.
2111 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2113 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2114 struct cafe_camera *cam = to_cam(v4l2_dev);
2116 enum cafe_state cstate;
2118 ret = pci_save_state(pdev);
2121 cstate = cam->state; /* HACK - stop_dma sets to idle */
2122 cafe_ctlr_stop_dma(cam);
2123 cafe_ctlr_power_down(cam);
2124 pci_disable_device(pdev);
2125 cam->state = cstate;
2130 static int cafe_pci_resume(struct pci_dev *pdev)
2132 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2133 struct cafe_camera *cam = to_cam(v4l2_dev);
2136 ret = pci_restore_state(pdev);
2139 ret = pci_enable_device(pdev);
2142 cam_warn(cam, "Unable to re-enable device on resume!\n");
2145 cafe_ctlr_init(cam);
2146 cafe_ctlr_power_down(cam);
2148 mutex_lock(&cam->s_mutex);
2149 if (cam->users > 0) {
2150 cafe_ctlr_power_up(cam);
2151 __cafe_cam_reset(cam);
2153 mutex_unlock(&cam->s_mutex);
2155 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2156 if (cam->state == S_SPECREAD)
2157 cam->state = S_IDLE; /* Don't bother restarting */
2158 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2159 ret = cafe_read_setup(cam, cam->state);
2163 #endif /* CONFIG_PM */
2166 static struct pci_device_id cafe_ids[] = {
2167 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2168 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2172 MODULE_DEVICE_TABLE(pci, cafe_ids);
2174 static struct pci_driver cafe_pci_driver = {
2175 .name = "cafe1000-ccic",
2176 .id_table = cafe_ids,
2177 .probe = cafe_pci_probe,
2178 .remove = cafe_pci_remove,
2180 .suspend = cafe_pci_suspend,
2181 .resume = cafe_pci_resume,
2188 static int __init cafe_init(void)
2192 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2194 ret = pci_register_driver(&cafe_pci_driver);
2196 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2206 static void __exit cafe_exit(void)
2208 pci_unregister_driver(&cafe_pci_driver);
2211 module_init(cafe_init);
2212 module_exit(cafe_exit);