RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / media / video / cafe_ccic.c
blobc08f650df42337df25a517eb7538e7a29c9a8a8d
1 /*
2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
4 * sensor.
6 * Copyright 2006 One Laptop Per Child Association, Inc.
7 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
9 * Written by Jonathan Corbet, corbet@lwn.net.
11 * This file may be distributed under the terms of the GNU General
12 * Public License, version 2.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/pci.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-chip-ident.h>
27 #include <linux/device.h>
28 #include <linux/wait.h>
29 #include <linux/list.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/delay.h>
32 #include <linux/debugfs.h>
33 #include <linux/jiffies.h>
34 #include <linux/vmalloc.h>
36 #include <asm/uaccess.h>
37 #include <asm/io.h>
39 #include "cafe_ccic-regs.h"
41 #define CAFE_VERSION 0x000002
45 * Parameters.
47 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
48 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
49 MODULE_LICENSE("GPL");
50 MODULE_SUPPORTED_DEVICE("Video");
53 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
54 * we must have physically contiguous buffers to bring frames into.
55 * These parameters control how many buffers we use, whether we
56 * allocate them at load time (better chance of success, but nails down
57 * memory) or when somebody tries to use the camera (riskier), and,
58 * for load-time allocation, how big they should be.
60 * The controller can cycle through three buffers. We could use
61 * more by flipping pointers around, but it probably makes little
62 * sense.
65 #define MAX_DMA_BUFS 3
66 static int alloc_bufs_at_load = 0;
67 module_param(alloc_bufs_at_load, bool, 0444);
68 MODULE_PARM_DESC(alloc_bufs_at_load,
69 "Non-zero value causes DMA buffers to be allocated at module "
70 "load time. This increases the chances of successfully getting "
71 "those buffers, but at the cost of nailing down the memory from "
72 "the outset.");
74 static int n_dma_bufs = 3;
75 module_param(n_dma_bufs, uint, 0644);
76 MODULE_PARM_DESC(n_dma_bufs,
77 "The number of DMA buffers to allocate. Can be either two "
78 "(saves memory, makes timing tighter) or three.");
80 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
81 module_param(dma_buf_size, uint, 0444);
82 MODULE_PARM_DESC(dma_buf_size,
83 "The size of the allocated DMA buffers. If actual operating "
84 "parameters require larger buffers, an attempt to reallocate "
85 "will be made.");
87 static int min_buffers = 1;
88 module_param(min_buffers, uint, 0644);
89 MODULE_PARM_DESC(min_buffers,
90 "The minimum number of streaming I/O buffers we are willing "
91 "to work with.");
93 static int max_buffers = 10;
94 module_param(max_buffers, uint, 0644);
95 MODULE_PARM_DESC(max_buffers,
96 "The maximum number of streaming I/O buffers an application "
97 "will be allowed to allocate. These buffers are big and live "
98 "in vmalloc space.");
100 static int flip = 0;
101 module_param(flip, bool, 0444);
102 MODULE_PARM_DESC(flip,
103 "If set, the sensor will be instructed to flip the image "
104 "vertically.");
107 enum cafe_state {
108 S_NOTREADY, /* Not yet initialized */
109 S_IDLE, /* Just hanging around */
110 S_FLAKED, /* Some sort of problem */
111 S_SINGLEREAD, /* In read() */
112 S_SPECREAD, /* Speculative read (for future read()) */
113 S_STREAMING /* Streaming data */
117 * Tracking of streaming I/O buffers.
119 struct cafe_sio_buffer {
120 struct list_head list;
121 struct v4l2_buffer v4lbuf;
122 char *buffer; /* Where it lives in kernel space */
123 int mapcount;
124 struct cafe_camera *cam;
128 * A description of one of our devices.
129 * Locking: controlled by s_mutex. Certain fields, however, require
130 * the dev_lock spinlock; they are marked as such by comments.
131 * dev_lock is also required for access to device registers.
133 struct cafe_camera
135 enum cafe_state state;
136 unsigned long flags; /* Buffer status, mainly (dev_lock) */
137 int users; /* How many open FDs */
138 struct file *owner; /* Who has data access (v4l2) */
141 * Subsystem structures.
143 struct pci_dev *pdev;
144 struct video_device v4ldev;
145 struct i2c_adapter i2c_adapter;
146 struct i2c_client *sensor;
148 unsigned char __iomem *regs;
149 struct list_head dev_list; /* link to other devices */
151 /* DMA buffers */
152 unsigned int nbufs; /* How many are alloc'd */
153 int next_buf; /* Next to consume (dev_lock) */
154 unsigned int dma_buf_size; /* allocated size */
155 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
156 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
157 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
158 unsigned int sequence; /* Frame sequence number */
159 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
161 /* Streaming buffers */
162 unsigned int n_sbufs; /* How many we have */
163 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
164 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
165 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
166 struct tasklet_struct s_tasklet;
168 /* Current operating parameters */
169 u32 sensor_type; /* Currently ov7670 only */
170 struct v4l2_pix_format pix_format;
172 /* Locks */
173 struct mutex s_mutex; /* Access to this structure */
174 spinlock_t dev_lock; /* Access to device */
176 /* Misc */
177 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
178 wait_queue_head_t iowait; /* Waiting on frame data */
179 #ifdef CONFIG_VIDEO_ADV_DEBUG
180 struct dentry *dfs_regs;
181 struct dentry *dfs_cam_regs;
182 #endif
186 * Status flags. Always manipulated with bit operations.
188 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
189 #define CF_BUF1_VALID 1
190 #define CF_BUF2_VALID 2
191 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
192 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
197 * Start over with DMA buffers - dev_lock needed.
199 static void cafe_reset_buffers(struct cafe_camera *cam)
201 int i;
203 cam->next_buf = -1;
204 for (i = 0; i < cam->nbufs; i++)
205 clear_bit(i, &cam->flags);
206 cam->specframes = 0;
209 static inline int cafe_needs_config(struct cafe_camera *cam)
211 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
214 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
216 if (needed)
217 set_bit(CF_CONFIG_NEEDED, &cam->flags);
218 else
219 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
226 * Debugging and related.
228 #define cam_err(cam, fmt, arg...) \
229 dev_err(&(cam)->pdev->dev, fmt, ##arg);
230 #define cam_warn(cam, fmt, arg...) \
231 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
232 #define cam_dbg(cam, fmt, arg...) \
233 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
236 /* ---------------------------------------------------------------------*/
238 * We keep a simple list of known devices to search at open time.
240 static LIST_HEAD(cafe_dev_list);
241 static DEFINE_MUTEX(cafe_dev_list_lock);
243 static void cafe_add_dev(struct cafe_camera *cam)
245 mutex_lock(&cafe_dev_list_lock);
246 list_add_tail(&cam->dev_list, &cafe_dev_list);
247 mutex_unlock(&cafe_dev_list_lock);
250 static void cafe_remove_dev(struct cafe_camera *cam)
252 mutex_lock(&cafe_dev_list_lock);
253 list_del(&cam->dev_list);
254 mutex_unlock(&cafe_dev_list_lock);
257 static struct cafe_camera *cafe_find_dev(int minor)
259 struct cafe_camera *cam;
261 mutex_lock(&cafe_dev_list_lock);
262 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
263 if (cam->v4ldev.minor == minor)
264 goto done;
266 cam = NULL;
267 done:
268 mutex_unlock(&cafe_dev_list_lock);
269 return cam;
273 static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
275 struct cafe_camera *cam;
277 mutex_lock(&cafe_dev_list_lock);
278 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
279 if (cam->pdev == pdev)
280 goto done;
282 cam = NULL;
283 done:
284 mutex_unlock(&cafe_dev_list_lock);
285 return cam;
289 /* ------------------------------------------------------------------------ */
291 * Device register I/O
293 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
294 unsigned int val)
296 iowrite32(val, cam->regs + reg);
299 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
300 unsigned int reg)
302 return ioread32(cam->regs + reg);
306 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
307 unsigned int val, unsigned int mask)
309 unsigned int v = cafe_reg_read(cam, reg);
311 v = (v & ~mask) | (val & mask);
312 cafe_reg_write(cam, reg, v);
315 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
316 unsigned int reg, unsigned int val)
318 cafe_reg_write_mask(cam, reg, 0, val);
321 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
322 unsigned int reg, unsigned int val)
324 cafe_reg_write_mask(cam, reg, val, val);
329 /* -------------------------------------------------------------------- */
331 * The I2C/SMBUS interface to the camera itself starts here. The
332 * controller handles SMBUS itself, presenting a relatively simple register
333 * interface; all we have to do is to tell it where to route the data.
335 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
337 static int cafe_smbus_write_done(struct cafe_camera *cam)
339 unsigned long flags;
340 int c1;
343 * We must delay after the interrupt, or the controller gets confused
344 * and never does give us good status. Fortunately, we don't do this
345 * often.
347 udelay(20);
348 spin_lock_irqsave(&cam->dev_lock, flags);
349 c1 = cafe_reg_read(cam, REG_TWSIC1);
350 spin_unlock_irqrestore(&cam->dev_lock, flags);
351 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
354 static int cafe_smbus_write_data(struct cafe_camera *cam,
355 u16 addr, u8 command, u8 value)
357 unsigned int rval;
358 unsigned long flags;
360 spin_lock_irqsave(&cam->dev_lock, flags);
361 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
362 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
364 * Marvell sez set clkdiv to all 1's for now.
366 rval |= TWSIC0_CLKDIV;
367 cafe_reg_write(cam, REG_TWSIC0, rval);
368 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
369 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
370 cafe_reg_write(cam, REG_TWSIC1, rval);
371 spin_unlock_irqrestore(&cam->dev_lock, flags);
372 msleep(2); /* Required or things flake */
374 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
375 CAFE_SMBUS_TIMEOUT);
376 spin_lock_irqsave(&cam->dev_lock, flags);
377 rval = cafe_reg_read(cam, REG_TWSIC1);
378 spin_unlock_irqrestore(&cam->dev_lock, flags);
380 if (rval & TWSIC1_WSTAT) {
381 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
382 command, value);
383 return -EIO;
385 if (rval & TWSIC1_ERROR) {
386 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
387 command, value);
388 return -EIO;
390 return 0;
395 static int cafe_smbus_read_done(struct cafe_camera *cam)
397 unsigned long flags;
398 int c1;
401 * We must delay after the interrupt, or the controller gets confused
402 * and never does give us good status. Fortunately, we don't do this
403 * often.
405 udelay(20);
406 spin_lock_irqsave(&cam->dev_lock, flags);
407 c1 = cafe_reg_read(cam, REG_TWSIC1);
408 spin_unlock_irqrestore(&cam->dev_lock, flags);
409 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
414 static int cafe_smbus_read_data(struct cafe_camera *cam,
415 u16 addr, u8 command, u8 *value)
417 unsigned int rval;
418 unsigned long flags;
420 spin_lock_irqsave(&cam->dev_lock, flags);
421 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
422 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
424 * Marvel sez set clkdiv to all 1's for now.
426 rval |= TWSIC0_CLKDIV;
427 cafe_reg_write(cam, REG_TWSIC0, rval);
428 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
429 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
430 cafe_reg_write(cam, REG_TWSIC1, rval);
431 spin_unlock_irqrestore(&cam->dev_lock, flags);
433 wait_event_timeout(cam->smbus_wait,
434 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
435 spin_lock_irqsave(&cam->dev_lock, flags);
436 rval = cafe_reg_read(cam, REG_TWSIC1);
437 spin_unlock_irqrestore(&cam->dev_lock, flags);
439 if (rval & TWSIC1_ERROR) {
440 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
441 return -EIO;
443 if (! (rval & TWSIC1_RVALID)) {
444 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
445 command);
446 return -EIO;
448 *value = rval & 0xff;
449 return 0;
453 * Perform a transfer over SMBUS. This thing is called under
454 * the i2c bus lock, so we shouldn't race with ourselves...
456 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
457 unsigned short flags, char rw, u8 command,
458 int size, union i2c_smbus_data *data)
460 struct cafe_camera *cam = i2c_get_adapdata(adapter);
461 int ret = -EINVAL;
464 * Refuse to talk to anything but OV cam chips. We should
465 * never even see an attempt to do so, but one never knows.
467 if (cam->sensor && addr != cam->sensor->addr) {
468 cam_err(cam, "funky smbus addr %d\n", addr);
469 return -EINVAL;
472 * This interface would appear to only do byte data ops. OK
473 * it can do word too, but the cam chip has no use for that.
475 if (size != I2C_SMBUS_BYTE_DATA) {
476 cam_err(cam, "funky xfer size %d\n", size);
477 return -EINVAL;
480 if (rw == I2C_SMBUS_WRITE)
481 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
482 else if (rw == I2C_SMBUS_READ)
483 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
484 return ret;
488 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
490 unsigned long flags;
492 spin_lock_irqsave(&cam->dev_lock, flags);
493 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
494 spin_unlock_irqrestore(&cam->dev_lock, flags);
497 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
499 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
500 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
503 static struct i2c_algorithm cafe_smbus_algo = {
504 .smbus_xfer = cafe_smbus_xfer,
505 .functionality = cafe_smbus_func
508 /* Somebody is on the bus */
509 static int cafe_cam_init(struct cafe_camera *cam);
510 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
511 static void cafe_ctlr_power_down(struct cafe_camera *cam);
513 static int cafe_smbus_attach(struct i2c_client *client)
515 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
518 * Don't talk to chips we don't recognize.
520 if (client->driver->id == I2C_DRIVERID_OV7670) {
521 cam->sensor = client;
522 return cafe_cam_init(cam);
524 return -EINVAL;
527 static int cafe_smbus_detach(struct i2c_client *client)
529 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
531 if (cam->sensor == client) {
532 cafe_ctlr_stop_dma(cam);
533 cafe_ctlr_power_down(cam);
534 cam_err(cam, "lost the sensor!\n");
535 cam->sensor = NULL; /* Bummer, no camera */
536 cam->state = S_NOTREADY;
538 return 0;
541 static int cafe_smbus_setup(struct cafe_camera *cam)
543 struct i2c_adapter *adap = &cam->i2c_adapter;
544 int ret;
546 cafe_smbus_enable_irq(cam);
547 adap->id = I2C_HW_SMBUS_CAFE;
548 adap->class = I2C_CLASS_CAM_DIGITAL;
549 adap->owner = THIS_MODULE;
550 adap->client_register = cafe_smbus_attach;
551 adap->client_unregister = cafe_smbus_detach;
552 adap->algo = &cafe_smbus_algo;
553 strcpy(adap->name, "cafe_ccic");
554 adap->dev.parent = &cam->pdev->dev;
555 i2c_set_adapdata(adap, cam);
556 ret = i2c_add_adapter(adap);
557 if (ret)
558 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
559 return ret;
562 static void cafe_smbus_shutdown(struct cafe_camera *cam)
564 i2c_del_adapter(&cam->i2c_adapter);
568 /* ------------------------------------------------------------------- */
570 * Deal with the controller.
574 * Do everything we think we need to have the interface operating
575 * according to the desired format.
577 static void cafe_ctlr_dma(struct cafe_camera *cam)
580 * Store the first two Y buffers (we aren't supporting
581 * planar formats for now, so no UV bufs). Then either
582 * set the third if it exists, or tell the controller
583 * to just use two.
585 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
586 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
587 if (cam->nbufs > 2) {
588 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
589 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
591 else
592 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
593 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
596 static void cafe_ctlr_image(struct cafe_camera *cam)
598 int imgsz;
599 struct v4l2_pix_format *fmt = &cam->pix_format;
601 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
602 (fmt->bytesperline & IMGSZ_H_MASK);
603 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
604 cafe_reg_write(cam, REG_IMGOFFSET, 0);
605 /* YPITCH just drops the last two bits */
606 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
607 IMGP_YP_MASK);
609 * Tell the controller about the image format we are using.
611 switch (cam->pix_format.pixelformat) {
612 case V4L2_PIX_FMT_YUYV:
613 cafe_reg_write_mask(cam, REG_CTRL0,
614 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
615 C0_DF_MASK);
616 break;
618 case V4L2_PIX_FMT_RGB444:
619 cafe_reg_write_mask(cam, REG_CTRL0,
620 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
621 C0_DF_MASK);
622 /* Alpha value? */
623 break;
625 case V4L2_PIX_FMT_RGB565:
626 cafe_reg_write_mask(cam, REG_CTRL0,
627 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
628 C0_DF_MASK);
629 break;
631 default:
632 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
633 break;
636 * Make sure it knows we want to use hsync/vsync.
638 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
639 C0_SIFM_MASK);
644 * Configure the controller for operation; caller holds the
645 * device mutex.
647 static int cafe_ctlr_configure(struct cafe_camera *cam)
649 unsigned long flags;
651 spin_lock_irqsave(&cam->dev_lock, flags);
652 cafe_ctlr_dma(cam);
653 cafe_ctlr_image(cam);
654 cafe_set_config_needed(cam, 0);
655 spin_unlock_irqrestore(&cam->dev_lock, flags);
656 return 0;
659 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
662 * Clear any pending interrupts, since we do not
663 * expect to have I/O active prior to enabling.
665 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
666 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
669 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
671 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
675 * Make the controller start grabbing images. Everything must
676 * be set up before doing this.
678 static void cafe_ctlr_start(struct cafe_camera *cam)
680 /* set_bit performs a read, so no other barrier should be
681 needed here */
682 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
685 static void cafe_ctlr_stop(struct cafe_camera *cam)
687 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
690 static void cafe_ctlr_init(struct cafe_camera *cam)
692 unsigned long flags;
694 spin_lock_irqsave(&cam->dev_lock, flags);
696 * Added magic to bring up the hardware on the B-Test board
698 cafe_reg_write(cam, 0x3038, 0x8);
699 cafe_reg_write(cam, 0x315c, 0x80008);
701 * Go through the dance needed to wake the device up.
702 * Note that these registers are global and shared
703 * with the NAND and SD devices. Interaction between the
704 * three still needs to be examined.
706 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
707 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
708 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
710 * Here we must wait a bit for the controller to come around.
712 spin_unlock_irqrestore(&cam->dev_lock, flags);
713 mdelay(5); /* FIXME revisit this */
714 spin_lock_irqsave(&cam->dev_lock, flags);
716 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
717 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
719 * Make sure it's not powered down.
721 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
723 * Turn off the enable bit. It sure should be off anyway,
724 * but it's good to be sure.
726 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
728 * Mask all interrupts.
730 cafe_reg_write(cam, REG_IRQMASK, 0);
732 * Clock the sensor appropriately. Controller clock should
733 * be 48MHz, sensor "typical" value is half that.
735 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
736 spin_unlock_irqrestore(&cam->dev_lock, flags);
741 * Stop the controller, and don't return until we're really sure that no
742 * further DMA is going on.
744 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
746 unsigned long flags;
749 * Theory: stop the camera controller (whether it is operating
750 * or not). Delay briefly just in case we race with the SOF
751 * interrupt, then wait until no DMA is active.
753 spin_lock_irqsave(&cam->dev_lock, flags);
754 cafe_ctlr_stop(cam);
755 spin_unlock_irqrestore(&cam->dev_lock, flags);
756 mdelay(1);
757 wait_event_timeout(cam->iowait,
758 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
759 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
760 cam_err(cam, "Timeout waiting for DMA to end\n");
761 /* This would be bad news - what now? */
762 spin_lock_irqsave(&cam->dev_lock, flags);
763 cam->state = S_IDLE;
764 cafe_ctlr_irq_disable(cam);
765 spin_unlock_irqrestore(&cam->dev_lock, flags);
769 * Power up and down.
771 static void cafe_ctlr_power_up(struct cafe_camera *cam)
773 unsigned long flags;
775 spin_lock_irqsave(&cam->dev_lock, flags);
776 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
778 * Part one of the sensor dance: turn the global
779 * GPIO signal on.
781 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
782 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
784 * Put the sensor into operational mode (assumes OLPC-style
785 * wiring). Control 0 is reset - set to 1 to operate.
786 * Control 1 is power down, set to 0 to operate.
788 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
789 // mdelay(1); /* Marvell says 1ms will do it */
790 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
791 // mdelay(1); /* Enough? */
792 spin_unlock_irqrestore(&cam->dev_lock, flags);
793 msleep(5); /* Just to be sure */
796 static void cafe_ctlr_power_down(struct cafe_camera *cam)
798 unsigned long flags;
800 spin_lock_irqsave(&cam->dev_lock, flags);
801 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
802 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
803 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
804 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
805 spin_unlock_irqrestore(&cam->dev_lock, flags);
808 /* -------------------------------------------------------------------- */
810 * Communications with the sensor.
813 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
815 struct i2c_client *sc = cam->sensor;
816 int ret;
818 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
819 return -EINVAL;
820 ret = sc->driver->command(sc, cmd, arg);
821 if (ret == -EPERM) /* Unsupported command */
822 return 0;
823 return ret;
826 static int __cafe_cam_reset(struct cafe_camera *cam)
828 int zero = 0;
829 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
833 * We have found the sensor on the i2c. Let's try to have a
834 * conversation.
836 static int cafe_cam_init(struct cafe_camera *cam)
838 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
839 int ret;
841 mutex_lock(&cam->s_mutex);
842 if (cam->state != S_NOTREADY)
843 cam_warn(cam, "Cam init with device in funky state %d",
844 cam->state);
845 ret = __cafe_cam_reset(cam);
846 if (ret)
847 goto out;
848 chip.match_chip = cam->sensor->addr;
849 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
850 if (ret)
851 goto out;
852 cam->sensor_type = chip.ident;
853 // if (cam->sensor->addr != OV7xx0_SID) {
854 if (cam->sensor_type != V4L2_IDENT_OV7670) {
855 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
856 ret = -EINVAL;
857 goto out;
859 /* Get/set parameters? */
860 ret = 0;
861 cam->state = S_IDLE;
862 out:
863 cafe_ctlr_power_down(cam);
864 mutex_unlock(&cam->s_mutex);
865 return ret;
869 * Configure the sensor to match the parameters we have. Caller should
870 * hold s_mutex
872 static int cafe_cam_set_flip(struct cafe_camera *cam)
874 struct v4l2_control ctrl;
876 memset(&ctrl, 0, sizeof(ctrl));
877 ctrl.id = V4L2_CID_VFLIP;
878 ctrl.value = flip;
879 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
883 static int cafe_cam_configure(struct cafe_camera *cam)
885 struct v4l2_format fmt;
886 int ret, zero = 0;
888 if (cam->state != S_IDLE)
889 return -EINVAL;
890 fmt.fmt.pix = cam->pix_format;
891 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
892 if (ret == 0)
893 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
895 * OV7670 does weird things if flip is set *before* format...
897 ret += cafe_cam_set_flip(cam);
898 return ret;
901 /* -------------------------------------------------------------------- */
903 * DMA buffer management. These functions need s_mutex held.
906 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
907 * does a get_free_pages() call, and we waste a good chunk of an orderN
908 * allocation. Should try to allocate the whole set in one chunk.
910 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
912 int i;
914 cafe_set_config_needed(cam, 1);
915 if (loadtime)
916 cam->dma_buf_size = dma_buf_size;
917 else
918 cam->dma_buf_size = cam->pix_format.sizeimage;
919 if (n_dma_bufs > 3)
920 n_dma_bufs = 3;
922 cam->nbufs = 0;
923 for (i = 0; i < n_dma_bufs; i++) {
924 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
925 cam->dma_buf_size, cam->dma_handles + i,
926 GFP_KERNEL);
927 if (cam->dma_bufs[i] == NULL) {
928 cam_warn(cam, "Failed to allocate DMA buffer\n");
929 break;
931 /* For debug, remove eventually */
932 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
933 (cam->nbufs)++;
936 switch (cam->nbufs) {
937 case 1:
938 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
939 cam->dma_bufs[0], cam->dma_handles[0]);
940 cam->nbufs = 0;
941 case 0:
942 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
943 return -ENOMEM;
945 case 2:
946 if (n_dma_bufs > 2)
947 cam_warn(cam, "Will limp along with only 2 buffers\n");
948 break;
950 return 0;
953 static void cafe_free_dma_bufs(struct cafe_camera *cam)
955 int i;
957 for (i = 0; i < cam->nbufs; i++) {
958 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
959 cam->dma_bufs[i], cam->dma_handles[i]);
960 cam->dma_bufs[i] = NULL;
962 cam->nbufs = 0;
969 /* ----------------------------------------------------------------------- */
971 * Here starts the V4L2 interface code.
975 * Read an image from the device.
977 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
978 char __user *buffer, size_t len, loff_t *pos)
980 int bufno;
981 unsigned long flags;
983 spin_lock_irqsave(&cam->dev_lock, flags);
984 if (cam->next_buf < 0) {
985 cam_err(cam, "deliver_buffer: No next buffer\n");
986 spin_unlock_irqrestore(&cam->dev_lock, flags);
987 return -EIO;
989 bufno = cam->next_buf;
990 clear_bit(bufno, &cam->flags);
991 if (++(cam->next_buf) >= cam->nbufs)
992 cam->next_buf = 0;
993 if (! test_bit(cam->next_buf, &cam->flags))
994 cam->next_buf = -1;
995 cam->specframes = 0;
996 spin_unlock_irqrestore(&cam->dev_lock, flags);
998 if (len > cam->pix_format.sizeimage)
999 len = cam->pix_format.sizeimage;
1000 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
1001 return -EFAULT;
1002 (*pos) += len;
1003 return len;
1007 * Get everything ready, and start grabbing frames.
1009 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
1011 int ret;
1012 unsigned long flags;
1015 * Configuration. If we still don't have DMA buffers,
1016 * make one last, desperate attempt.
1018 if (cam->nbufs == 0)
1019 if (cafe_alloc_dma_bufs(cam, 0))
1020 return -ENOMEM;
1022 if (cafe_needs_config(cam)) {
1023 cafe_cam_configure(cam);
1024 ret = cafe_ctlr_configure(cam);
1025 if (ret)
1026 return ret;
1030 * Turn it loose.
1032 spin_lock_irqsave(&cam->dev_lock, flags);
1033 cafe_reset_buffers(cam);
1034 cafe_ctlr_irq_enable(cam);
1035 cam->state = state;
1036 cafe_ctlr_start(cam);
1037 spin_unlock_irqrestore(&cam->dev_lock, flags);
1038 return 0;
1042 static ssize_t cafe_v4l_read(struct file *filp,
1043 char __user *buffer, size_t len, loff_t *pos)
1045 struct cafe_camera *cam = filp->private_data;
1046 int ret = 0;
1049 * Perhaps we're in speculative read mode and already
1050 * have data?
1052 mutex_lock(&cam->s_mutex);
1053 if (cam->state == S_SPECREAD) {
1054 if (cam->next_buf >= 0) {
1055 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1056 if (ret != 0)
1057 goto out_unlock;
1059 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1060 ret = -EIO;
1061 goto out_unlock;
1062 } else if (cam->state != S_IDLE) {
1063 ret = -EBUSY;
1064 goto out_unlock;
1068 * v4l2: multiple processes can open the device, but only
1069 * one gets to grab data from it.
1071 if (cam->owner && cam->owner != filp) {
1072 ret = -EBUSY;
1073 goto out_unlock;
1075 cam->owner = filp;
1078 * Do setup if need be.
1080 if (cam->state != S_SPECREAD) {
1081 ret = cafe_read_setup(cam, S_SINGLEREAD);
1082 if (ret)
1083 goto out_unlock;
1086 * Wait for something to happen. This should probably
1087 * be interruptible (FIXME).
1089 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1090 if (cam->next_buf < 0) {
1091 cam_err(cam, "read() operation timed out\n");
1092 cafe_ctlr_stop_dma(cam);
1093 ret = -EIO;
1094 goto out_unlock;
1097 * Give them their data and we should be done.
1099 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1101 out_unlock:
1102 mutex_unlock(&cam->s_mutex);
1103 return ret;
1114 * Streaming I/O support.
1119 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1120 enum v4l2_buf_type type)
1122 struct cafe_camera *cam = filp->private_data;
1123 int ret = -EINVAL;
1125 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1126 goto out;
1127 mutex_lock(&cam->s_mutex);
1128 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1129 goto out_unlock;
1131 cam->sequence = 0;
1132 ret = cafe_read_setup(cam, S_STREAMING);
1134 out_unlock:
1135 mutex_unlock(&cam->s_mutex);
1136 out:
1137 return ret;
1141 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1142 enum v4l2_buf_type type)
1144 struct cafe_camera *cam = filp->private_data;
1145 int ret = -EINVAL;
1147 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1148 goto out;
1149 mutex_lock(&cam->s_mutex);
1150 if (cam->state != S_STREAMING)
1151 goto out_unlock;
1153 cafe_ctlr_stop_dma(cam);
1154 ret = 0;
1156 out_unlock:
1157 mutex_unlock(&cam->s_mutex);
1158 out:
1159 return ret;
1164 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1166 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1168 INIT_LIST_HEAD(&buf->list);
1169 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1170 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1171 if (buf->buffer == NULL)
1172 return -ENOMEM;
1173 buf->mapcount = 0;
1174 buf->cam = cam;
1176 buf->v4lbuf.index = index;
1177 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1178 buf->v4lbuf.field = V4L2_FIELD_NONE;
1179 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1181 * Offset: must be 32-bit even on a 64-bit system. video-buf
1182 * just uses the length times the index, but the spec warns
1183 * against doing just that - vma merging problems. So we
1184 * leave a gap between each pair of buffers.
1186 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1187 return 0;
1190 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1192 int i;
1195 * If any buffers are mapped, we cannot free them at all.
1197 for (i = 0; i < cam->n_sbufs; i++)
1198 if (cam->sb_bufs[i].mapcount > 0)
1199 return -EBUSY;
1201 * OK, let's do it.
1203 for (i = 0; i < cam->n_sbufs; i++)
1204 vfree(cam->sb_bufs[i].buffer);
1205 cam->n_sbufs = 0;
1206 kfree(cam->sb_bufs);
1207 cam->sb_bufs = NULL;
1208 INIT_LIST_HEAD(&cam->sb_avail);
1209 INIT_LIST_HEAD(&cam->sb_full);
1210 return 0;
1215 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1216 struct v4l2_requestbuffers *req)
1218 struct cafe_camera *cam = filp->private_data;
1219 int ret = 0; /* Silence warning */
1222 * Make sure it's something we can do. User pointers could be
1223 * implemented without great pain, but that's not been done yet.
1225 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1226 return -EINVAL;
1227 if (req->memory != V4L2_MEMORY_MMAP)
1228 return -EINVAL;
1230 * If they ask for zero buffers, they really want us to stop streaming
1231 * (if it's happening) and free everything. Should we check owner?
1233 mutex_lock(&cam->s_mutex);
1234 if (req->count == 0) {
1235 if (cam->state == S_STREAMING)
1236 cafe_ctlr_stop_dma(cam);
1237 ret = cafe_free_sio_buffers (cam);
1238 goto out;
1241 * Device needs to be idle and working. We *could* try to do the
1242 * right thing in S_SPECREAD by shutting things down, but it
1243 * probably doesn't matter.
1245 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1246 ret = -EBUSY;
1247 goto out;
1249 cam->owner = filp;
1251 if (req->count < min_buffers)
1252 req->count = min_buffers;
1253 else if (req->count > max_buffers)
1254 req->count = max_buffers;
1255 if (cam->n_sbufs > 0) {
1256 ret = cafe_free_sio_buffers(cam);
1257 if (ret)
1258 goto out;
1261 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1262 GFP_KERNEL);
1263 if (cam->sb_bufs == NULL) {
1264 ret = -ENOMEM;
1265 goto out;
1267 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1268 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1269 if (ret)
1270 break;
1273 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1274 kfree(cam->sb_bufs);
1275 req->count = cam->n_sbufs; /* In case of partial success */
1277 out:
1278 mutex_unlock(&cam->s_mutex);
1279 return ret;
1283 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1284 struct v4l2_buffer *buf)
1286 struct cafe_camera *cam = filp->private_data;
1287 int ret = -EINVAL;
1289 mutex_lock(&cam->s_mutex);
1290 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1291 goto out;
1292 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1293 goto out;
1294 *buf = cam->sb_bufs[buf->index].v4lbuf;
1295 ret = 0;
1296 out:
1297 mutex_unlock(&cam->s_mutex);
1298 return ret;
1301 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1302 struct v4l2_buffer *buf)
1304 struct cafe_camera *cam = filp->private_data;
1305 struct cafe_sio_buffer *sbuf;
1306 int ret = -EINVAL;
1307 unsigned long flags;
1309 mutex_lock(&cam->s_mutex);
1310 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1311 goto out;
1312 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1313 goto out;
1314 sbuf = cam->sb_bufs + buf->index;
1315 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1316 ret = 0; /* Already queued?? */
1317 goto out;
1319 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1320 /* Spec doesn't say anything, seems appropriate tho */
1321 ret = -EBUSY;
1322 goto out;
1324 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1325 spin_lock_irqsave(&cam->dev_lock, flags);
1326 list_add(&sbuf->list, &cam->sb_avail);
1327 spin_unlock_irqrestore(&cam->dev_lock, flags);
1328 ret = 0;
1329 out:
1330 mutex_unlock(&cam->s_mutex);
1331 return ret;
1334 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1335 struct v4l2_buffer *buf)
1337 struct cafe_camera *cam = filp->private_data;
1338 struct cafe_sio_buffer *sbuf;
1339 int ret = -EINVAL;
1340 unsigned long flags;
1342 mutex_lock(&cam->s_mutex);
1343 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1344 goto out_unlock;
1345 if (cam->state != S_STREAMING)
1346 goto out_unlock;
1347 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1348 ret = -EAGAIN;
1349 goto out_unlock;
1352 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1353 mutex_unlock(&cam->s_mutex);
1354 if (wait_event_interruptible(cam->iowait,
1355 !list_empty(&cam->sb_full))) {
1356 ret = -ERESTARTSYS;
1357 goto out;
1359 mutex_lock(&cam->s_mutex);
1362 if (cam->state != S_STREAMING)
1363 ret = -EINTR;
1364 else {
1365 spin_lock_irqsave(&cam->dev_lock, flags);
1366 /* Should probably recheck !list_empty() here */
1367 sbuf = list_entry(cam->sb_full.next,
1368 struct cafe_sio_buffer, list);
1369 list_del_init(&sbuf->list);
1370 spin_unlock_irqrestore(&cam->dev_lock, flags);
1371 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1372 *buf = sbuf->v4lbuf;
1373 ret = 0;
1376 out_unlock:
1377 mutex_unlock(&cam->s_mutex);
1378 out:
1379 return ret;
1384 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1386 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1388 * Locking: done under mmap_sem, so we don't need to
1389 * go back to the camera lock here.
1391 sbuf->mapcount++;
1395 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1397 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1399 mutex_lock(&sbuf->cam->s_mutex);
1400 sbuf->mapcount--;
1401 /* Docs say we should stop I/O too... */
1402 if (sbuf->mapcount == 0)
1403 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1404 mutex_unlock(&sbuf->cam->s_mutex);
1407 static struct vm_operations_struct cafe_v4l_vm_ops = {
1408 .open = cafe_v4l_vm_open,
1409 .close = cafe_v4l_vm_close
1413 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1415 struct cafe_camera *cam = filp->private_data;
1416 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1417 int ret = -EINVAL;
1418 int i;
1419 struct cafe_sio_buffer *sbuf = NULL;
1421 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1422 return -EINVAL;
1424 * Find the buffer they are looking for.
1426 mutex_lock(&cam->s_mutex);
1427 for (i = 0; i < cam->n_sbufs; i++)
1428 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1429 sbuf = cam->sb_bufs + i;
1430 break;
1432 if (sbuf == NULL)
1433 goto out;
1435 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1436 if (ret)
1437 goto out;
1438 vma->vm_flags |= VM_DONTEXPAND;
1439 vma->vm_private_data = sbuf;
1440 vma->vm_ops = &cafe_v4l_vm_ops;
1441 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1442 cafe_v4l_vm_open(vma);
1443 ret = 0;
1444 out:
1445 mutex_unlock(&cam->s_mutex);
1446 return ret;
1451 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1453 struct cafe_camera *cam;
1455 cam = cafe_find_dev(iminor(inode));
1456 if (cam == NULL)
1457 return -ENODEV;
1458 filp->private_data = cam;
1460 mutex_lock(&cam->s_mutex);
1461 if (cam->users == 0) {
1462 cafe_ctlr_power_up(cam);
1463 __cafe_cam_reset(cam);
1464 cafe_set_config_needed(cam, 1);
1465 /* FIXME make sure this is complete */
1467 (cam->users)++;
1468 mutex_unlock(&cam->s_mutex);
1469 return 0;
1473 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1475 struct cafe_camera *cam = filp->private_data;
1477 mutex_lock(&cam->s_mutex);
1478 (cam->users)--;
1479 if (filp == cam->owner) {
1480 cafe_ctlr_stop_dma(cam);
1481 cafe_free_sio_buffers(cam);
1482 cam->owner = NULL;
1484 if (cam->users == 0) {
1485 cafe_ctlr_power_down(cam);
1486 if (! alloc_bufs_at_load)
1487 cafe_free_dma_bufs(cam);
1489 mutex_unlock(&cam->s_mutex);
1490 return 0;
1495 static unsigned int cafe_v4l_poll(struct file *filp,
1496 struct poll_table_struct *pt)
1498 struct cafe_camera *cam = filp->private_data;
1500 poll_wait(filp, &cam->iowait, pt);
1501 if (cam->next_buf >= 0)
1502 return POLLIN | POLLRDNORM;
1503 return 0;
1508 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1509 struct v4l2_queryctrl *qc)
1511 struct cafe_camera *cam = filp->private_data;
1512 int ret;
1514 mutex_lock(&cam->s_mutex);
1515 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1516 mutex_unlock(&cam->s_mutex);
1517 return ret;
1521 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1522 struct v4l2_control *ctrl)
1524 struct cafe_camera *cam = filp->private_data;
1525 int ret;
1527 mutex_lock(&cam->s_mutex);
1528 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1529 mutex_unlock(&cam->s_mutex);
1530 return ret;
1534 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1535 struct v4l2_control *ctrl)
1537 struct cafe_camera *cam = filp->private_data;
1538 int ret;
1540 mutex_lock(&cam->s_mutex);
1541 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1542 mutex_unlock(&cam->s_mutex);
1543 return ret;
1550 static int cafe_vidioc_querycap(struct file *file, void *priv,
1551 struct v4l2_capability *cap)
1553 strcpy(cap->driver, "cafe_ccic");
1554 strcpy(cap->card, "cafe_ccic");
1555 cap->version = CAFE_VERSION;
1556 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1557 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1558 return 0;
1563 * The default format we use until somebody says otherwise.
1565 static struct v4l2_pix_format cafe_def_pix_format = {
1566 .width = VGA_WIDTH,
1567 .height = VGA_HEIGHT,
1568 .pixelformat = V4L2_PIX_FMT_YUYV,
1569 .field = V4L2_FIELD_NONE,
1570 .bytesperline = VGA_WIDTH*2,
1571 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1574 static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1575 void *priv, struct v4l2_fmtdesc *fmt)
1577 struct cafe_camera *cam = priv;
1578 int ret;
1580 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1581 return -EINVAL;
1582 mutex_lock(&cam->s_mutex);
1583 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1584 mutex_unlock(&cam->s_mutex);
1585 return ret;
1589 static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1590 struct v4l2_format *fmt)
1592 struct cafe_camera *cam = priv;
1593 int ret;
1595 mutex_lock(&cam->s_mutex);
1596 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1597 mutex_unlock(&cam->s_mutex);
1598 return ret;
1601 static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1602 struct v4l2_format *fmt)
1604 struct cafe_camera *cam = priv;
1605 int ret;
1608 * Can't do anything if the device is not idle
1609 * Also can't if there are streaming buffers in place.
1611 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1612 return -EBUSY;
1614 * See if the formatting works in principle.
1616 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1617 if (ret)
1618 return ret;
1620 * Now we start to change things for real, so let's do it
1621 * under lock.
1623 mutex_lock(&cam->s_mutex);
1624 cam->pix_format = fmt->fmt.pix;
1626 * Make sure we have appropriate DMA buffers.
1628 ret = -ENOMEM;
1629 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1630 cafe_free_dma_bufs(cam);
1631 if (cam->nbufs == 0) {
1632 if (cafe_alloc_dma_bufs(cam, 0))
1633 goto out;
1636 * It looks like this might work, so let's program the sensor.
1638 ret = cafe_cam_configure(cam);
1639 if (! ret)
1640 ret = cafe_ctlr_configure(cam);
1641 out:
1642 mutex_unlock(&cam->s_mutex);
1643 return ret;
1647 * Return our stored notion of how the camera is/should be configured.
1648 * The V4l2 spec wants us to be smarter, and actually get this from
1649 * the camera (and not mess with it at open time). Someday.
1651 static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1652 struct v4l2_format *f)
1654 struct cafe_camera *cam = priv;
1656 f->fmt.pix = cam->pix_format;
1657 return 0;
1661 * We only have one input - the sensor - so minimize the nonsense here.
1663 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1664 struct v4l2_input *input)
1666 if (input->index != 0)
1667 return -EINVAL;
1669 input->type = V4L2_INPUT_TYPE_CAMERA;
1670 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1671 strcpy(input->name, "Camera");
1672 return 0;
1675 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1677 *i = 0;
1678 return 0;
1681 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1683 if (i != 0)
1684 return -EINVAL;
1685 return 0;
1688 /* from vivi.c */
1689 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1691 return 0;
1695 * G/S_PARM. Most of this is done by the sensor, but we are
1696 * the level which controls the number of read buffers.
1698 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1699 struct v4l2_streamparm *parms)
1701 struct cafe_camera *cam = priv;
1702 int ret;
1704 mutex_lock(&cam->s_mutex);
1705 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1706 mutex_unlock(&cam->s_mutex);
1707 parms->parm.capture.readbuffers = n_dma_bufs;
1708 return ret;
1711 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1712 struct v4l2_streamparm *parms)
1714 struct cafe_camera *cam = priv;
1715 int ret;
1717 mutex_lock(&cam->s_mutex);
1718 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1719 mutex_unlock(&cam->s_mutex);
1720 parms->parm.capture.readbuffers = n_dma_bufs;
1721 return ret;
1725 static void cafe_v4l_dev_release(struct video_device *vd)
1727 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1729 kfree(cam);
1734 * This template device holds all of those v4l2 methods; we
1735 * clone it for specific real devices.
1738 static const struct file_operations cafe_v4l_fops = {
1739 .owner = THIS_MODULE,
1740 .open = cafe_v4l_open,
1741 .release = cafe_v4l_release,
1742 .read = cafe_v4l_read,
1743 .poll = cafe_v4l_poll,
1744 .mmap = cafe_v4l_mmap,
1745 .ioctl = video_ioctl2,
1746 .llseek = no_llseek,
1749 static struct video_device cafe_v4l_template = {
1750 .name = "cafe",
1751 .type = VFL_TYPE_GRABBER,
1752 .type2 = VID_TYPE_CAPTURE,
1753 .minor = -1, /* Get one dynamically */
1754 .tvnorms = V4L2_STD_NTSC_M,
1755 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1757 .fops = &cafe_v4l_fops,
1758 .release = cafe_v4l_dev_release,
1760 .vidioc_querycap = cafe_vidioc_querycap,
1761 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1762 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1763 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1764 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1765 .vidioc_enum_input = cafe_vidioc_enum_input,
1766 .vidioc_g_input = cafe_vidioc_g_input,
1767 .vidioc_s_input = cafe_vidioc_s_input,
1768 .vidioc_s_std = cafe_vidioc_s_std,
1769 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1770 .vidioc_querybuf = cafe_vidioc_querybuf,
1771 .vidioc_qbuf = cafe_vidioc_qbuf,
1772 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1773 .vidioc_streamon = cafe_vidioc_streamon,
1774 .vidioc_streamoff = cafe_vidioc_streamoff,
1775 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1776 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1777 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1778 .vidioc_g_parm = cafe_vidioc_g_parm,
1779 .vidioc_s_parm = cafe_vidioc_s_parm,
1788 /* ---------------------------------------------------------------------- */
1790 * Interrupt handler stuff
1795 static void cafe_frame_tasklet(unsigned long data)
1797 struct cafe_camera *cam = (struct cafe_camera *) data;
1798 int i;
1799 unsigned long flags;
1800 struct cafe_sio_buffer *sbuf;
1802 spin_lock_irqsave(&cam->dev_lock, flags);
1803 for (i = 0; i < cam->nbufs; i++) {
1804 int bufno = cam->next_buf;
1805 if (bufno < 0) { /* "will never happen" */
1806 cam_err(cam, "No valid bufs in tasklet!\n");
1807 break;
1809 if (++(cam->next_buf) >= cam->nbufs)
1810 cam->next_buf = 0;
1811 if (! test_bit(bufno, &cam->flags))
1812 continue;
1813 if (list_empty(&cam->sb_avail))
1814 break; /* Leave it valid, hope for better later */
1815 clear_bit(bufno, &cam->flags);
1816 sbuf = list_entry(cam->sb_avail.next,
1817 struct cafe_sio_buffer, list);
1819 * Drop the lock during the big copy. This *should* be safe...
1821 spin_unlock_irqrestore(&cam->dev_lock, flags);
1822 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1823 cam->pix_format.sizeimage);
1824 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1825 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1826 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1827 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1828 spin_lock_irqsave(&cam->dev_lock, flags);
1829 list_move_tail(&sbuf->list, &cam->sb_full);
1831 if (! list_empty(&cam->sb_full))
1832 wake_up(&cam->iowait);
1833 spin_unlock_irqrestore(&cam->dev_lock, flags);
1838 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1841 * Basic frame housekeeping.
1843 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1844 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1845 set_bit(frame, &cam->flags);
1846 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1847 if (cam->next_buf < 0)
1848 cam->next_buf = frame;
1849 cam->buf_seq[frame] = ++(cam->sequence);
1851 switch (cam->state) {
1853 * If in single read mode, try going speculative.
1855 case S_SINGLEREAD:
1856 cam->state = S_SPECREAD;
1857 cam->specframes = 0;
1858 wake_up(&cam->iowait);
1859 break;
1862 * If we are already doing speculative reads, and nobody is
1863 * reading them, just stop.
1865 case S_SPECREAD:
1866 if (++(cam->specframes) >= cam->nbufs) {
1867 cafe_ctlr_stop(cam);
1868 cafe_ctlr_irq_disable(cam);
1869 cam->state = S_IDLE;
1871 wake_up(&cam->iowait);
1872 break;
1874 * For the streaming case, we defer the real work to the
1875 * camera tasklet.
1877 * FIXME: if the application is not consuming the buffers,
1878 * we should eventually put things on hold and restart in
1879 * vidioc_dqbuf().
1881 case S_STREAMING:
1882 tasklet_schedule(&cam->s_tasklet);
1883 break;
1885 default:
1886 cam_err(cam, "Frame interrupt in non-operational state\n");
1887 break;
1894 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1896 unsigned int frame;
1898 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1900 * Handle any frame completions. There really should
1901 * not be more than one of these, or we have fallen
1902 * far behind.
1904 for (frame = 0; frame < cam->nbufs; frame++)
1905 if (irqs & (IRQ_EOF0 << frame))
1906 cafe_frame_complete(cam, frame);
1908 * If a frame starts, note that we have DMA active. This
1909 * code assumes that we won't get multiple frame interrupts
1910 * at once; may want to rethink that.
1912 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1913 set_bit(CF_DMA_ACTIVE, &cam->flags);
1918 static irqreturn_t cafe_irq(int irq, void *data)
1920 struct cafe_camera *cam = data;
1921 unsigned int irqs;
1923 spin_lock(&cam->dev_lock);
1924 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1925 if ((irqs & ALLIRQS) == 0) {
1926 spin_unlock(&cam->dev_lock);
1927 return IRQ_NONE;
1929 if (irqs & FRAMEIRQS)
1930 cafe_frame_irq(cam, irqs);
1931 if (irqs & TWSIIRQS) {
1932 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1933 wake_up(&cam->smbus_wait);
1935 spin_unlock(&cam->dev_lock);
1936 return IRQ_HANDLED;
1940 /* -------------------------------------------------------------------------- */
1941 #ifdef CONFIG_VIDEO_ADV_DEBUG
1943 * Debugfs stuff.
1946 static char cafe_debug_buf[1024];
1947 static struct dentry *cafe_dfs_root;
1949 static void cafe_dfs_setup(void)
1951 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1952 if (IS_ERR(cafe_dfs_root)) {
1953 cafe_dfs_root = NULL; /* Never mind */
1954 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1958 static void cafe_dfs_shutdown(void)
1960 if (cafe_dfs_root)
1961 debugfs_remove(cafe_dfs_root);
1964 static int cafe_dfs_open(struct inode *inode, struct file *file)
1966 file->private_data = inode->i_private;
1967 return 0;
1970 static ssize_t cafe_dfs_read_regs(struct file *file,
1971 char __user *buf, size_t count, loff_t *ppos)
1973 struct cafe_camera *cam = file->private_data;
1974 char *s = cafe_debug_buf;
1975 int offset;
1977 for (offset = 0; offset < 0x44; offset += 4)
1978 s += sprintf(s, "%02x: %08x\n", offset,
1979 cafe_reg_read(cam, offset));
1980 for (offset = 0x88; offset <= 0x90; offset += 4)
1981 s += sprintf(s, "%02x: %08x\n", offset,
1982 cafe_reg_read(cam, offset));
1983 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1984 s += sprintf(s, "%02x: %08x\n", offset,
1985 cafe_reg_read(cam, offset));
1986 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1987 s += sprintf(s, "%04x: %08x\n", offset,
1988 cafe_reg_read(cam, offset));
1989 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1990 s - cafe_debug_buf);
1993 static const struct file_operations cafe_dfs_reg_ops = {
1994 .owner = THIS_MODULE,
1995 .read = cafe_dfs_read_regs,
1996 .open = cafe_dfs_open
1999 static ssize_t cafe_dfs_read_cam(struct file *file,
2000 char __user *buf, size_t count, loff_t *ppos)
2002 struct cafe_camera *cam = file->private_data;
2003 char *s = cafe_debug_buf;
2004 int offset;
2006 if (! cam->sensor)
2007 return -EINVAL;
2008 for (offset = 0x0; offset < 0x8a; offset++)
2010 u8 v;
2012 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2013 s += sprintf(s, "%02x: %02x\n", offset, v);
2015 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2016 s - cafe_debug_buf);
2019 static const struct file_operations cafe_dfs_cam_ops = {
2020 .owner = THIS_MODULE,
2021 .read = cafe_dfs_read_cam,
2022 .open = cafe_dfs_open
2027 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2029 char fname[40];
2031 if (!cafe_dfs_root)
2032 return;
2033 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2034 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2035 cam, &cafe_dfs_reg_ops);
2036 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2037 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2038 cam, &cafe_dfs_cam_ops);
2042 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2044 if (! IS_ERR(cam->dfs_regs))
2045 debugfs_remove(cam->dfs_regs);
2046 if (! IS_ERR(cam->dfs_cam_regs))
2047 debugfs_remove(cam->dfs_cam_regs);
2050 #else
2052 #define cafe_dfs_setup()
2053 #define cafe_dfs_shutdown()
2054 #define cafe_dfs_cam_setup(cam)
2055 #define cafe_dfs_cam_shutdown(cam)
2056 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2061 /* ------------------------------------------------------------------------*/
2063 * PCI interface stuff.
2066 static int cafe_pci_probe(struct pci_dev *pdev,
2067 const struct pci_device_id *id)
2069 int ret;
2070 u16 classword;
2071 struct cafe_camera *cam;
2073 * Make sure we have a camera here - we'll get calls for
2074 * the other cafe devices as well.
2076 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2077 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2078 return -ENODEV;
2080 * Start putting together one of our big camera structures.
2082 ret = -ENOMEM;
2083 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2084 if (cam == NULL)
2085 goto out;
2086 mutex_init(&cam->s_mutex);
2087 mutex_lock(&cam->s_mutex);
2088 spin_lock_init(&cam->dev_lock);
2089 cam->state = S_NOTREADY;
2090 cafe_set_config_needed(cam, 1);
2091 init_waitqueue_head(&cam->smbus_wait);
2092 init_waitqueue_head(&cam->iowait);
2093 cam->pdev = pdev;
2094 cam->pix_format = cafe_def_pix_format;
2095 INIT_LIST_HEAD(&cam->dev_list);
2096 INIT_LIST_HEAD(&cam->sb_avail);
2097 INIT_LIST_HEAD(&cam->sb_full);
2098 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2100 * Get set up on the PCI bus.
2102 ret = pci_enable_device(pdev);
2103 if (ret)
2104 goto out_free;
2105 pci_set_master(pdev);
2107 ret = -EIO;
2108 cam->regs = pci_iomap(pdev, 0, 0);
2109 if (! cam->regs) {
2110 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2111 goto out_free;
2113 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2114 if (ret)
2115 goto out_iounmap;
2117 * Initialize the controller and leave it powered up. It will
2118 * stay that way until the sensor driver shows up.
2120 cafe_ctlr_init(cam);
2121 cafe_ctlr_power_up(cam);
2123 * Set up I2C/SMBUS communications. We have to drop the mutex here
2124 * because the sensor could attach in this call chain, leading to
2125 * unsightly deadlocks.
2127 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2128 ret = cafe_smbus_setup(cam);
2129 if (ret)
2130 goto out_freeirq;
2132 * Get the v4l2 setup done.
2134 mutex_lock(&cam->s_mutex);
2135 cam->v4ldev = cafe_v4l_template;
2136 cam->v4ldev.debug = 0;
2137 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2138 cam->v4ldev.dev = &pdev->dev;
2139 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2140 if (ret)
2141 goto out_smbus;
2143 * If so requested, try to get our DMA buffers now.
2145 if (alloc_bufs_at_load) {
2146 if (cafe_alloc_dma_bufs(cam, 1))
2147 cam_warn(cam, "Unable to alloc DMA buffers at load"
2148 " will try again later.");
2151 cafe_dfs_cam_setup(cam);
2152 mutex_unlock(&cam->s_mutex);
2153 cafe_add_dev(cam);
2154 return 0;
2156 out_smbus:
2157 cafe_smbus_shutdown(cam);
2158 out_freeirq:
2159 cafe_ctlr_power_down(cam);
2160 free_irq(pdev->irq, cam);
2161 out_iounmap:
2162 pci_iounmap(pdev, cam->regs);
2163 out_free:
2164 kfree(cam);
2165 out:
2166 return ret;
2171 * Shut down an initialized device
2173 static void cafe_shutdown(struct cafe_camera *cam)
2175 /* FIXME: Make sure we take care of everything here */
2176 cafe_dfs_cam_shutdown(cam);
2177 if (cam->n_sbufs > 0)
2178 /* What if they are still mapped? Shouldn't be, but... */
2179 cafe_free_sio_buffers(cam);
2180 cafe_remove_dev(cam);
2181 cafe_ctlr_stop_dma(cam);
2182 cafe_ctlr_power_down(cam);
2183 cafe_smbus_shutdown(cam);
2184 cafe_free_dma_bufs(cam);
2185 free_irq(cam->pdev->irq, cam);
2186 pci_iounmap(cam->pdev, cam->regs);
2187 video_unregister_device(&cam->v4ldev);
2188 /* kfree(cam); done in v4l_release () */
2192 static void cafe_pci_remove(struct pci_dev *pdev)
2194 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2196 if (cam == NULL) {
2197 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2198 return;
2200 mutex_lock(&cam->s_mutex);
2201 if (cam->users > 0)
2202 cam_warn(cam, "Removing a device with users!\n");
2203 cafe_shutdown(cam);
2204 /* No unlock - it no longer exists */
2208 #ifdef CONFIG_PM
2210 * Basic power management.
2212 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2214 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2215 int ret;
2217 ret = pci_save_state(pdev);
2218 if (ret)
2219 return ret;
2220 cafe_ctlr_stop_dma(cam);
2221 cafe_ctlr_power_down(cam);
2222 pci_disable_device(pdev);
2223 return 0;
2227 static int cafe_pci_resume(struct pci_dev *pdev)
2229 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2230 int ret = 0;
2232 ret = pci_restore_state(pdev);
2233 if (ret)
2234 return ret;
2235 ret = pci_enable_device(pdev);
2236 if (ret) {
2237 cam_warn(cam, "Unable to re-enable device on resume!\n");
2238 return ret;
2240 cafe_ctlr_init(cam);
2241 cafe_ctlr_power_up(cam);
2242 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2243 if (cam->state == S_SPECREAD)
2244 cam->state = S_IDLE; /* Don't bother restarting */
2245 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2246 ret = cafe_read_setup(cam, cam->state);
2247 return ret;
2250 #endif /* CONFIG_PM */
2253 static struct pci_device_id cafe_ids[] = {
2254 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2255 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2256 { 0, }
2259 MODULE_DEVICE_TABLE(pci, cafe_ids);
2261 static struct pci_driver cafe_pci_driver = {
2262 .name = "cafe1000-ccic",
2263 .id_table = cafe_ids,
2264 .probe = cafe_pci_probe,
2265 .remove = cafe_pci_remove,
2266 #ifdef CONFIG_PM
2267 .suspend = cafe_pci_suspend,
2268 .resume = cafe_pci_resume,
2269 #endif
2275 static int __init cafe_init(void)
2277 int ret;
2279 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2280 CAFE_VERSION);
2281 cafe_dfs_setup();
2282 ret = pci_register_driver(&cafe_pci_driver);
2283 if (ret) {
2284 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2285 goto out;
2287 request_module("ov7670"); /* FIXME want something more general */
2288 ret = 0;
2290 out:
2291 return ret;
2295 static void __exit cafe_exit(void)
2297 pci_unregister_driver(&cafe_pci_driver);
2298 cafe_dfs_shutdown();
2301 module_init(cafe_init);
2302 module_exit(cafe_exit);