KVM: Cleanup mark_page_dirty
[linux-2.6/btrfs-unstable.git] / drivers / media / video / cafe_ccic.c
blobb63cab33692072a6163463211aaace9fb29619e1
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/init.h>
18 #include <linux/fs.h>
19 #include <linux/pci.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-chip-ident.h>
26 #include <linux/device.h>
27 #include <linux/wait.h>
28 #include <linux/list.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/delay.h>
31 #include <linux/debugfs.h>
32 #include <linux/jiffies.h>
33 #include <linux/vmalloc.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
38 #include "cafe_ccic-regs.h"
40 #define CAFE_VERSION 0x000002
44 * Parameters.
46 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
47 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
48 MODULE_LICENSE("GPL");
49 MODULE_SUPPORTED_DEVICE("Video");
52 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
53 * we must have physically contiguous buffers to bring frames into.
54 * These parameters control how many buffers we use, whether we
55 * allocate them at load time (better chance of success, but nails down
56 * memory) or when somebody tries to use the camera (riskier), and,
57 * for load-time allocation, how big they should be.
59 * The controller can cycle through three buffers. We could use
60 * more by flipping pointers around, but it probably makes little
61 * sense.
64 #define MAX_DMA_BUFS 3
65 static int alloc_bufs_at_read = 0;
66 module_param(alloc_bufs_at_read, bool, 0444);
67 MODULE_PARM_DESC(alloc_bufs_at_read,
68 "Non-zero value causes DMA buffers to be allocated when the "
69 "video capture device is read, rather than at module load "
70 "time. This saves memory, but decreases the chances of "
71 "successfully getting those buffers.");
73 static int n_dma_bufs = 3;
74 module_param(n_dma_bufs, uint, 0644);
75 MODULE_PARM_DESC(n_dma_bufs,
76 "The number of DMA buffers to allocate. Can be either two "
77 "(saves memory, makes timing tighter) or three.");
79 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
80 module_param(dma_buf_size, uint, 0444);
81 MODULE_PARM_DESC(dma_buf_size,
82 "The size of the allocated DMA buffers. If actual operating "
83 "parameters require larger buffers, an attempt to reallocate "
84 "will be made.");
86 static int min_buffers = 1;
87 module_param(min_buffers, uint, 0644);
88 MODULE_PARM_DESC(min_buffers,
89 "The minimum number of streaming I/O buffers we are willing "
90 "to work with.");
92 static int max_buffers = 10;
93 module_param(max_buffers, uint, 0644);
94 MODULE_PARM_DESC(max_buffers,
95 "The maximum number of streaming I/O buffers an application "
96 "will be allowed to allocate. These buffers are big and live "
97 "in vmalloc space.");
99 static int flip = 0;
100 module_param(flip, bool, 0444);
101 MODULE_PARM_DESC(flip,
102 "If set, the sensor will be instructed to flip the image "
103 "vertically.");
106 enum cafe_state {
107 S_NOTREADY, /* Not yet initialized */
108 S_IDLE, /* Just hanging around */
109 S_FLAKED, /* Some sort of problem */
110 S_SINGLEREAD, /* In read() */
111 S_SPECREAD, /* Speculative read (for future read()) */
112 S_STREAMING /* Streaming data */
116 * Tracking of streaming I/O buffers.
118 struct cafe_sio_buffer {
119 struct list_head list;
120 struct v4l2_buffer v4lbuf;
121 char *buffer; /* Where it lives in kernel space */
122 int mapcount;
123 struct cafe_camera *cam;
127 * A description of one of our devices.
128 * Locking: controlled by s_mutex. Certain fields, however, require
129 * the dev_lock spinlock; they are marked as such by comments.
130 * dev_lock is also required for access to device registers.
132 struct cafe_camera
134 enum cafe_state state;
135 unsigned long flags; /* Buffer status, mainly (dev_lock) */
136 int users; /* How many open FDs */
137 struct file *owner; /* Who has data access (v4l2) */
140 * Subsystem structures.
142 struct pci_dev *pdev;
143 struct video_device v4ldev;
144 struct i2c_adapter i2c_adapter;
145 struct i2c_client *sensor;
147 unsigned char __iomem *regs;
148 struct list_head dev_list; /* link to other devices */
150 /* DMA buffers */
151 unsigned int nbufs; /* How many are alloc'd */
152 int next_buf; /* Next to consume (dev_lock) */
153 unsigned int dma_buf_size; /* allocated size */
154 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
155 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
156 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
157 unsigned int sequence; /* Frame sequence number */
158 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
160 /* Streaming buffers */
161 unsigned int n_sbufs; /* How many we have */
162 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
163 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
164 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
165 struct tasklet_struct s_tasklet;
167 /* Current operating parameters */
168 u32 sensor_type; /* Currently ov7670 only */
169 struct v4l2_pix_format pix_format;
171 /* Locks */
172 struct mutex s_mutex; /* Access to this structure */
173 spinlock_t dev_lock; /* Access to device */
175 /* Misc */
176 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
177 wait_queue_head_t iowait; /* Waiting on frame data */
178 #ifdef CONFIG_VIDEO_ADV_DEBUG
179 struct dentry *dfs_regs;
180 struct dentry *dfs_cam_regs;
181 #endif
185 * Status flags. Always manipulated with bit operations.
187 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
188 #define CF_BUF1_VALID 1
189 #define CF_BUF2_VALID 2
190 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
191 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
196 * Start over with DMA buffers - dev_lock needed.
198 static void cafe_reset_buffers(struct cafe_camera *cam)
200 int i;
202 cam->next_buf = -1;
203 for (i = 0; i < cam->nbufs; i++)
204 clear_bit(i, &cam->flags);
205 cam->specframes = 0;
208 static inline int cafe_needs_config(struct cafe_camera *cam)
210 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
213 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
215 if (needed)
216 set_bit(CF_CONFIG_NEEDED, &cam->flags);
217 else
218 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
225 * Debugging and related.
227 #define cam_err(cam, fmt, arg...) \
228 dev_err(&(cam)->pdev->dev, fmt, ##arg);
229 #define cam_warn(cam, fmt, arg...) \
230 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
231 #define cam_dbg(cam, fmt, arg...) \
232 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
235 /* ---------------------------------------------------------------------*/
237 * We keep a simple list of known devices to search at open time.
239 static LIST_HEAD(cafe_dev_list);
240 static DEFINE_MUTEX(cafe_dev_list_lock);
242 static void cafe_add_dev(struct cafe_camera *cam)
244 mutex_lock(&cafe_dev_list_lock);
245 list_add_tail(&cam->dev_list, &cafe_dev_list);
246 mutex_unlock(&cafe_dev_list_lock);
249 static void cafe_remove_dev(struct cafe_camera *cam)
251 mutex_lock(&cafe_dev_list_lock);
252 list_del(&cam->dev_list);
253 mutex_unlock(&cafe_dev_list_lock);
256 static struct cafe_camera *cafe_find_dev(int minor)
258 struct cafe_camera *cam;
260 mutex_lock(&cafe_dev_list_lock);
261 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
262 if (cam->v4ldev.minor == minor)
263 goto done;
265 cam = NULL;
266 done:
267 mutex_unlock(&cafe_dev_list_lock);
268 return cam;
272 static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
274 struct cafe_camera *cam;
276 mutex_lock(&cafe_dev_list_lock);
277 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
278 if (cam->pdev == pdev)
279 goto done;
281 cam = NULL;
282 done:
283 mutex_unlock(&cafe_dev_list_lock);
284 return cam;
288 /* ------------------------------------------------------------------------ */
290 * Device register I/O
292 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
293 unsigned int val)
295 iowrite32(val, cam->regs + reg);
298 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
299 unsigned int reg)
301 return ioread32(cam->regs + reg);
305 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
306 unsigned int val, unsigned int mask)
308 unsigned int v = cafe_reg_read(cam, reg);
310 v = (v & ~mask) | (val & mask);
311 cafe_reg_write(cam, reg, v);
314 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
315 unsigned int reg, unsigned int val)
317 cafe_reg_write_mask(cam, reg, 0, val);
320 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
321 unsigned int reg, unsigned int val)
323 cafe_reg_write_mask(cam, reg, val, val);
328 /* -------------------------------------------------------------------- */
330 * The I2C/SMBUS interface to the camera itself starts here. The
331 * controller handles SMBUS itself, presenting a relatively simple register
332 * interface; all we have to do is to tell it where to route the data.
334 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
336 static int cafe_smbus_write_done(struct cafe_camera *cam)
338 unsigned long flags;
339 int c1;
342 * We must delay after the interrupt, or the controller gets confused
343 * and never does give us good status. Fortunately, we don't do this
344 * often.
346 udelay(20);
347 spin_lock_irqsave(&cam->dev_lock, flags);
348 c1 = cafe_reg_read(cam, REG_TWSIC1);
349 spin_unlock_irqrestore(&cam->dev_lock, flags);
350 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
353 static int cafe_smbus_write_data(struct cafe_camera *cam,
354 u16 addr, u8 command, u8 value)
356 unsigned int rval;
357 unsigned long flags;
358 DEFINE_WAIT(the_wait);
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);
374 * Time to wait for the write to complete. THIS IS A RACY
375 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
376 * register too quickly after starting the operation sends
377 * the device into a place that may be kinder and better, but
378 * which is absolutely useless for controlling the sensor. In
379 * practice we have plenty of time to get into our sleep state
380 * before the interrupt hits, and the worst case is that we
381 * time out and then see that things completed, so this seems
382 * the best way for now.
384 do {
385 prepare_to_wait(&cam->smbus_wait, &the_wait,
386 TASK_UNINTERRUPTIBLE);
387 schedule_timeout(1); /* even 1 jiffy is too long */
388 finish_wait(&cam->smbus_wait, &the_wait);
389 } while (!cafe_smbus_write_done(cam));
391 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
392 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
393 CAFE_SMBUS_TIMEOUT);
394 #endif
395 spin_lock_irqsave(&cam->dev_lock, flags);
396 rval = cafe_reg_read(cam, REG_TWSIC1);
397 spin_unlock_irqrestore(&cam->dev_lock, flags);
399 if (rval & TWSIC1_WSTAT) {
400 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
401 command, value);
402 return -EIO;
404 if (rval & TWSIC1_ERROR) {
405 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
406 command, value);
407 return -EIO;
409 return 0;
414 static int cafe_smbus_read_done(struct cafe_camera *cam)
416 unsigned long flags;
417 int c1;
420 * We must delay after the interrupt, or the controller gets confused
421 * and never does give us good status. Fortunately, we don't do this
422 * often.
424 udelay(20);
425 spin_lock_irqsave(&cam->dev_lock, flags);
426 c1 = cafe_reg_read(cam, REG_TWSIC1);
427 spin_unlock_irqrestore(&cam->dev_lock, flags);
428 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
433 static int cafe_smbus_read_data(struct cafe_camera *cam,
434 u16 addr, u8 command, u8 *value)
436 unsigned int rval;
437 unsigned long flags;
439 spin_lock_irqsave(&cam->dev_lock, flags);
440 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
441 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
443 * Marvel sez set clkdiv to all 1's for now.
445 rval |= TWSIC0_CLKDIV;
446 cafe_reg_write(cam, REG_TWSIC0, rval);
447 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
448 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
449 cafe_reg_write(cam, REG_TWSIC1, rval);
450 spin_unlock_irqrestore(&cam->dev_lock, flags);
452 wait_event_timeout(cam->smbus_wait,
453 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
454 spin_lock_irqsave(&cam->dev_lock, flags);
455 rval = cafe_reg_read(cam, REG_TWSIC1);
456 spin_unlock_irqrestore(&cam->dev_lock, flags);
458 if (rval & TWSIC1_ERROR) {
459 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
460 return -EIO;
462 if (! (rval & TWSIC1_RVALID)) {
463 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
464 command);
465 return -EIO;
467 *value = rval & 0xff;
468 return 0;
472 * Perform a transfer over SMBUS. This thing is called under
473 * the i2c bus lock, so we shouldn't race with ourselves...
475 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
476 unsigned short flags, char rw, u8 command,
477 int size, union i2c_smbus_data *data)
479 struct cafe_camera *cam = i2c_get_adapdata(adapter);
480 int ret = -EINVAL;
483 * Refuse to talk to anything but OV cam chips. We should
484 * never even see an attempt to do so, but one never knows.
486 if (cam->sensor && addr != cam->sensor->addr) {
487 cam_err(cam, "funky smbus addr %d\n", addr);
488 return -EINVAL;
491 * This interface would appear to only do byte data ops. OK
492 * it can do word too, but the cam chip has no use for that.
494 if (size != I2C_SMBUS_BYTE_DATA) {
495 cam_err(cam, "funky xfer size %d\n", size);
496 return -EINVAL;
499 if (rw == I2C_SMBUS_WRITE)
500 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
501 else if (rw == I2C_SMBUS_READ)
502 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
503 return ret;
507 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
509 unsigned long flags;
511 spin_lock_irqsave(&cam->dev_lock, flags);
512 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
513 spin_unlock_irqrestore(&cam->dev_lock, flags);
516 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
518 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
519 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
522 static struct i2c_algorithm cafe_smbus_algo = {
523 .smbus_xfer = cafe_smbus_xfer,
524 .functionality = cafe_smbus_func
527 /* Somebody is on the bus */
528 static int cafe_cam_init(struct cafe_camera *cam);
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_attach(struct i2c_client *client)
534 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
537 * Don't talk to chips we don't recognize.
539 if (client->driver->id == I2C_DRIVERID_OV7670) {
540 cam->sensor = client;
541 return cafe_cam_init(cam);
543 return -EINVAL;
546 static int cafe_smbus_detach(struct i2c_client *client)
548 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
550 if (cam->sensor == client) {
551 cafe_ctlr_stop_dma(cam);
552 cafe_ctlr_power_down(cam);
553 cam_err(cam, "lost the sensor!\n");
554 cam->sensor = NULL; /* Bummer, no camera */
555 cam->state = S_NOTREADY;
557 return 0;
560 static int cafe_smbus_setup(struct cafe_camera *cam)
562 struct i2c_adapter *adap = &cam->i2c_adapter;
563 int ret;
565 cafe_smbus_enable_irq(cam);
566 adap->id = I2C_HW_SMBUS_CAFE;
567 adap->class = I2C_CLASS_CAM_DIGITAL;
568 adap->owner = THIS_MODULE;
569 adap->client_register = cafe_smbus_attach;
570 adap->client_unregister = cafe_smbus_detach;
571 adap->algo = &cafe_smbus_algo;
572 strcpy(adap->name, "cafe_ccic");
573 adap->dev.parent = &cam->pdev->dev;
574 i2c_set_adapdata(adap, cam);
575 ret = i2c_add_adapter(adap);
576 if (ret)
577 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
578 return ret;
581 static void cafe_smbus_shutdown(struct cafe_camera *cam)
583 i2c_del_adapter(&cam->i2c_adapter);
587 /* ------------------------------------------------------------------- */
589 * Deal with the controller.
593 * Do everything we think we need to have the interface operating
594 * according to the desired format.
596 static void cafe_ctlr_dma(struct cafe_camera *cam)
599 * Store the first two Y buffers (we aren't supporting
600 * planar formats for now, so no UV bufs). Then either
601 * set the third if it exists, or tell the controller
602 * to just use two.
604 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
605 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
606 if (cam->nbufs > 2) {
607 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
608 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
610 else
611 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
612 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
615 static void cafe_ctlr_image(struct cafe_camera *cam)
617 int imgsz;
618 struct v4l2_pix_format *fmt = &cam->pix_format;
620 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
621 (fmt->bytesperline & IMGSZ_H_MASK);
622 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
623 cafe_reg_write(cam, REG_IMGOFFSET, 0);
624 /* YPITCH just drops the last two bits */
625 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
626 IMGP_YP_MASK);
628 * Tell the controller about the image format we are using.
630 switch (cam->pix_format.pixelformat) {
631 case V4L2_PIX_FMT_YUYV:
632 cafe_reg_write_mask(cam, REG_CTRL0,
633 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
634 C0_DF_MASK);
635 break;
637 case V4L2_PIX_FMT_RGB444:
638 cafe_reg_write_mask(cam, REG_CTRL0,
639 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
640 C0_DF_MASK);
641 /* Alpha value? */
642 break;
644 case V4L2_PIX_FMT_RGB565:
645 cafe_reg_write_mask(cam, REG_CTRL0,
646 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
647 C0_DF_MASK);
648 break;
650 default:
651 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
652 break;
655 * Make sure it knows we want to use hsync/vsync.
657 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
658 C0_SIFM_MASK);
663 * Configure the controller for operation; caller holds the
664 * device mutex.
666 static int cafe_ctlr_configure(struct cafe_camera *cam)
668 unsigned long flags;
670 spin_lock_irqsave(&cam->dev_lock, flags);
671 cafe_ctlr_dma(cam);
672 cafe_ctlr_image(cam);
673 cafe_set_config_needed(cam, 0);
674 spin_unlock_irqrestore(&cam->dev_lock, flags);
675 return 0;
678 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
681 * Clear any pending interrupts, since we do not
682 * expect to have I/O active prior to enabling.
684 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
685 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
688 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
690 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
694 * Make the controller start grabbing images. Everything must
695 * be set up before doing this.
697 static void cafe_ctlr_start(struct cafe_camera *cam)
699 /* set_bit performs a read, so no other barrier should be
700 needed here */
701 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
704 static void cafe_ctlr_stop(struct cafe_camera *cam)
706 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
709 static void cafe_ctlr_init(struct cafe_camera *cam)
711 unsigned long flags;
713 spin_lock_irqsave(&cam->dev_lock, flags);
715 * Added magic to bring up the hardware on the B-Test board
717 cafe_reg_write(cam, 0x3038, 0x8);
718 cafe_reg_write(cam, 0x315c, 0x80008);
720 * Go through the dance needed to wake the device up.
721 * Note that these registers are global and shared
722 * with the NAND and SD devices. Interaction between the
723 * three still needs to be examined.
725 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
726 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
727 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
729 * Here we must wait a bit for the controller to come around.
731 spin_unlock_irqrestore(&cam->dev_lock, flags);
732 msleep(5);
733 spin_lock_irqsave(&cam->dev_lock, flags);
735 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
736 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
738 * Make sure it's not powered down.
740 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
742 * Turn off the enable bit. It sure should be off anyway,
743 * but it's good to be sure.
745 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
747 * Mask all interrupts.
749 cafe_reg_write(cam, REG_IRQMASK, 0);
751 * Clock the sensor appropriately. Controller clock should
752 * be 48MHz, sensor "typical" value is half that.
754 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
755 spin_unlock_irqrestore(&cam->dev_lock, flags);
760 * Stop the controller, and don't return until we're really sure that no
761 * further DMA is going on.
763 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
765 unsigned long flags;
768 * Theory: stop the camera controller (whether it is operating
769 * or not). Delay briefly just in case we race with the SOF
770 * interrupt, then wait until no DMA is active.
772 spin_lock_irqsave(&cam->dev_lock, flags);
773 cafe_ctlr_stop(cam);
774 spin_unlock_irqrestore(&cam->dev_lock, flags);
775 mdelay(1);
776 wait_event_timeout(cam->iowait,
777 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
778 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
779 cam_err(cam, "Timeout waiting for DMA to end\n");
780 /* This would be bad news - what now? */
781 spin_lock_irqsave(&cam->dev_lock, flags);
782 cam->state = S_IDLE;
783 cafe_ctlr_irq_disable(cam);
784 spin_unlock_irqrestore(&cam->dev_lock, flags);
788 * Power up and down.
790 static void cafe_ctlr_power_up(struct cafe_camera *cam)
792 unsigned long flags;
794 spin_lock_irqsave(&cam->dev_lock, flags);
795 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
797 * Part one of the sensor dance: turn the global
798 * GPIO signal on.
800 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
801 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
803 * Put the sensor into operational mode (assumes OLPC-style
804 * wiring). Control 0 is reset - set to 1 to operate.
805 * Control 1 is power down, set to 0 to operate.
807 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
808 // mdelay(1); /* Marvell says 1ms will do it */
809 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
810 // mdelay(1); /* Enough? */
811 spin_unlock_irqrestore(&cam->dev_lock, flags);
812 msleep(5); /* Just to be sure */
815 static void cafe_ctlr_power_down(struct cafe_camera *cam)
817 unsigned long flags;
819 spin_lock_irqsave(&cam->dev_lock, flags);
820 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
821 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
822 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
823 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
824 spin_unlock_irqrestore(&cam->dev_lock, flags);
827 /* -------------------------------------------------------------------- */
829 * Communications with the sensor.
832 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
834 struct i2c_client *sc = cam->sensor;
835 int ret;
837 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
838 return -EINVAL;
839 ret = sc->driver->command(sc, cmd, arg);
840 if (ret == -EPERM) /* Unsupported command */
841 return 0;
842 return ret;
845 static int __cafe_cam_reset(struct cafe_camera *cam)
847 int zero = 0;
848 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
852 * We have found the sensor on the i2c. Let's try to have a
853 * conversation.
855 static int cafe_cam_init(struct cafe_camera *cam)
857 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
858 int ret;
860 mutex_lock(&cam->s_mutex);
861 if (cam->state != S_NOTREADY)
862 cam_warn(cam, "Cam init with device in funky state %d",
863 cam->state);
864 ret = __cafe_cam_reset(cam);
865 if (ret)
866 goto out;
867 chip.match_chip = cam->sensor->addr;
868 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
869 if (ret)
870 goto out;
871 cam->sensor_type = chip.ident;
872 // if (cam->sensor->addr != OV7xx0_SID) {
873 if (cam->sensor_type != V4L2_IDENT_OV7670) {
874 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
875 ret = -EINVAL;
876 goto out;
878 /* Get/set parameters? */
879 ret = 0;
880 cam->state = S_IDLE;
881 out:
882 cafe_ctlr_power_down(cam);
883 mutex_unlock(&cam->s_mutex);
884 return ret;
888 * Configure the sensor to match the parameters we have. Caller should
889 * hold s_mutex
891 static int cafe_cam_set_flip(struct cafe_camera *cam)
893 struct v4l2_control ctrl;
895 memset(&ctrl, 0, sizeof(ctrl));
896 ctrl.id = V4L2_CID_VFLIP;
897 ctrl.value = flip;
898 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
902 static int cafe_cam_configure(struct cafe_camera *cam)
904 struct v4l2_format fmt;
905 int ret, zero = 0;
907 if (cam->state != S_IDLE)
908 return -EINVAL;
909 fmt.fmt.pix = cam->pix_format;
910 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
911 if (ret == 0)
912 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
914 * OV7670 does weird things if flip is set *before* format...
916 ret += cafe_cam_set_flip(cam);
917 return ret;
920 /* -------------------------------------------------------------------- */
922 * DMA buffer management. These functions need s_mutex held.
925 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
926 * does a get_free_pages() call, and we waste a good chunk of an orderN
927 * allocation. Should try to allocate the whole set in one chunk.
929 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
931 int i;
933 cafe_set_config_needed(cam, 1);
934 if (loadtime)
935 cam->dma_buf_size = dma_buf_size;
936 else
937 cam->dma_buf_size = cam->pix_format.sizeimage;
938 if (n_dma_bufs > 3)
939 n_dma_bufs = 3;
941 cam->nbufs = 0;
942 for (i = 0; i < n_dma_bufs; i++) {
943 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
944 cam->dma_buf_size, cam->dma_handles + i,
945 GFP_KERNEL);
946 if (cam->dma_bufs[i] == NULL) {
947 cam_warn(cam, "Failed to allocate DMA buffer\n");
948 break;
950 /* For debug, remove eventually */
951 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
952 (cam->nbufs)++;
955 switch (cam->nbufs) {
956 case 1:
957 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
958 cam->dma_bufs[0], cam->dma_handles[0]);
959 cam->nbufs = 0;
960 case 0:
961 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
962 return -ENOMEM;
964 case 2:
965 if (n_dma_bufs > 2)
966 cam_warn(cam, "Will limp along with only 2 buffers\n");
967 break;
969 return 0;
972 static void cafe_free_dma_bufs(struct cafe_camera *cam)
974 int i;
976 for (i = 0; i < cam->nbufs; i++) {
977 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
978 cam->dma_bufs[i], cam->dma_handles[i]);
979 cam->dma_bufs[i] = NULL;
981 cam->nbufs = 0;
988 /* ----------------------------------------------------------------------- */
990 * Here starts the V4L2 interface code.
994 * Read an image from the device.
996 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
997 char __user *buffer, size_t len, loff_t *pos)
999 int bufno;
1000 unsigned long flags;
1002 spin_lock_irqsave(&cam->dev_lock, flags);
1003 if (cam->next_buf < 0) {
1004 cam_err(cam, "deliver_buffer: No next buffer\n");
1005 spin_unlock_irqrestore(&cam->dev_lock, flags);
1006 return -EIO;
1008 bufno = cam->next_buf;
1009 clear_bit(bufno, &cam->flags);
1010 if (++(cam->next_buf) >= cam->nbufs)
1011 cam->next_buf = 0;
1012 if (! test_bit(cam->next_buf, &cam->flags))
1013 cam->next_buf = -1;
1014 cam->specframes = 0;
1015 spin_unlock_irqrestore(&cam->dev_lock, flags);
1017 if (len > cam->pix_format.sizeimage)
1018 len = cam->pix_format.sizeimage;
1019 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
1020 return -EFAULT;
1021 (*pos) += len;
1022 return len;
1026 * Get everything ready, and start grabbing frames.
1028 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
1030 int ret;
1031 unsigned long flags;
1034 * Configuration. If we still don't have DMA buffers,
1035 * make one last, desperate attempt.
1037 if (cam->nbufs == 0)
1038 if (cafe_alloc_dma_bufs(cam, 0))
1039 return -ENOMEM;
1041 if (cafe_needs_config(cam)) {
1042 cafe_cam_configure(cam);
1043 ret = cafe_ctlr_configure(cam);
1044 if (ret)
1045 return ret;
1049 * Turn it loose.
1051 spin_lock_irqsave(&cam->dev_lock, flags);
1052 cafe_reset_buffers(cam);
1053 cafe_ctlr_irq_enable(cam);
1054 cam->state = state;
1055 cafe_ctlr_start(cam);
1056 spin_unlock_irqrestore(&cam->dev_lock, flags);
1057 return 0;
1061 static ssize_t cafe_v4l_read(struct file *filp,
1062 char __user *buffer, size_t len, loff_t *pos)
1064 struct cafe_camera *cam = filp->private_data;
1065 int ret = 0;
1068 * Perhaps we're in speculative read mode and already
1069 * have data?
1071 mutex_lock(&cam->s_mutex);
1072 if (cam->state == S_SPECREAD) {
1073 if (cam->next_buf >= 0) {
1074 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1075 if (ret != 0)
1076 goto out_unlock;
1078 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1079 ret = -EIO;
1080 goto out_unlock;
1081 } else if (cam->state != S_IDLE) {
1082 ret = -EBUSY;
1083 goto out_unlock;
1087 * v4l2: multiple processes can open the device, but only
1088 * one gets to grab data from it.
1090 if (cam->owner && cam->owner != filp) {
1091 ret = -EBUSY;
1092 goto out_unlock;
1094 cam->owner = filp;
1097 * Do setup if need be.
1099 if (cam->state != S_SPECREAD) {
1100 ret = cafe_read_setup(cam, S_SINGLEREAD);
1101 if (ret)
1102 goto out_unlock;
1105 * Wait for something to happen. This should probably
1106 * be interruptible (FIXME).
1108 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1109 if (cam->next_buf < 0) {
1110 cam_err(cam, "read() operation timed out\n");
1111 cafe_ctlr_stop_dma(cam);
1112 ret = -EIO;
1113 goto out_unlock;
1116 * Give them their data and we should be done.
1118 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1120 out_unlock:
1121 mutex_unlock(&cam->s_mutex);
1122 return ret;
1133 * Streaming I/O support.
1138 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1139 enum v4l2_buf_type type)
1141 struct cafe_camera *cam = filp->private_data;
1142 int ret = -EINVAL;
1144 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1145 goto out;
1146 mutex_lock(&cam->s_mutex);
1147 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1148 goto out_unlock;
1150 cam->sequence = 0;
1151 ret = cafe_read_setup(cam, S_STREAMING);
1153 out_unlock:
1154 mutex_unlock(&cam->s_mutex);
1155 out:
1156 return ret;
1160 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1161 enum v4l2_buf_type type)
1163 struct cafe_camera *cam = filp->private_data;
1164 int ret = -EINVAL;
1166 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1167 goto out;
1168 mutex_lock(&cam->s_mutex);
1169 if (cam->state != S_STREAMING)
1170 goto out_unlock;
1172 cafe_ctlr_stop_dma(cam);
1173 ret = 0;
1175 out_unlock:
1176 mutex_unlock(&cam->s_mutex);
1177 out:
1178 return ret;
1183 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1185 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1187 INIT_LIST_HEAD(&buf->list);
1188 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1189 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1190 if (buf->buffer == NULL)
1191 return -ENOMEM;
1192 buf->mapcount = 0;
1193 buf->cam = cam;
1195 buf->v4lbuf.index = index;
1196 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1197 buf->v4lbuf.field = V4L2_FIELD_NONE;
1198 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1200 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1201 * just uses the length times the index, but the spec warns
1202 * against doing just that - vma merging problems. So we
1203 * leave a gap between each pair of buffers.
1205 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1206 return 0;
1209 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1211 int i;
1214 * If any buffers are mapped, we cannot free them at all.
1216 for (i = 0; i < cam->n_sbufs; i++)
1217 if (cam->sb_bufs[i].mapcount > 0)
1218 return -EBUSY;
1220 * OK, let's do it.
1222 for (i = 0; i < cam->n_sbufs; i++)
1223 vfree(cam->sb_bufs[i].buffer);
1224 cam->n_sbufs = 0;
1225 kfree(cam->sb_bufs);
1226 cam->sb_bufs = NULL;
1227 INIT_LIST_HEAD(&cam->sb_avail);
1228 INIT_LIST_HEAD(&cam->sb_full);
1229 return 0;
1234 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1235 struct v4l2_requestbuffers *req)
1237 struct cafe_camera *cam = filp->private_data;
1238 int ret = 0; /* Silence warning */
1241 * Make sure it's something we can do. User pointers could be
1242 * implemented without great pain, but that's not been done yet.
1244 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1245 return -EINVAL;
1246 if (req->memory != V4L2_MEMORY_MMAP)
1247 return -EINVAL;
1249 * If they ask for zero buffers, they really want us to stop streaming
1250 * (if it's happening) and free everything. Should we check owner?
1252 mutex_lock(&cam->s_mutex);
1253 if (req->count == 0) {
1254 if (cam->state == S_STREAMING)
1255 cafe_ctlr_stop_dma(cam);
1256 ret = cafe_free_sio_buffers (cam);
1257 goto out;
1260 * Device needs to be idle and working. We *could* try to do the
1261 * right thing in S_SPECREAD by shutting things down, but it
1262 * probably doesn't matter.
1264 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1265 ret = -EBUSY;
1266 goto out;
1268 cam->owner = filp;
1270 if (req->count < min_buffers)
1271 req->count = min_buffers;
1272 else if (req->count > max_buffers)
1273 req->count = max_buffers;
1274 if (cam->n_sbufs > 0) {
1275 ret = cafe_free_sio_buffers(cam);
1276 if (ret)
1277 goto out;
1280 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1281 GFP_KERNEL);
1282 if (cam->sb_bufs == NULL) {
1283 ret = -ENOMEM;
1284 goto out;
1286 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1287 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1288 if (ret)
1289 break;
1292 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1293 kfree(cam->sb_bufs);
1294 req->count = cam->n_sbufs; /* In case of partial success */
1296 out:
1297 mutex_unlock(&cam->s_mutex);
1298 return ret;
1302 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1303 struct v4l2_buffer *buf)
1305 struct cafe_camera *cam = filp->private_data;
1306 int ret = -EINVAL;
1308 mutex_lock(&cam->s_mutex);
1309 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1310 goto out;
1311 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1312 goto out;
1313 *buf = cam->sb_bufs[buf->index].v4lbuf;
1314 ret = 0;
1315 out:
1316 mutex_unlock(&cam->s_mutex);
1317 return ret;
1320 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1321 struct v4l2_buffer *buf)
1323 struct cafe_camera *cam = filp->private_data;
1324 struct cafe_sio_buffer *sbuf;
1325 int ret = -EINVAL;
1326 unsigned long flags;
1328 mutex_lock(&cam->s_mutex);
1329 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1330 goto out;
1331 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1332 goto out;
1333 sbuf = cam->sb_bufs + buf->index;
1334 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1335 ret = 0; /* Already queued?? */
1336 goto out;
1338 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1339 /* Spec doesn't say anything, seems appropriate tho */
1340 ret = -EBUSY;
1341 goto out;
1343 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1344 spin_lock_irqsave(&cam->dev_lock, flags);
1345 list_add(&sbuf->list, &cam->sb_avail);
1346 spin_unlock_irqrestore(&cam->dev_lock, flags);
1347 ret = 0;
1348 out:
1349 mutex_unlock(&cam->s_mutex);
1350 return ret;
1353 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1354 struct v4l2_buffer *buf)
1356 struct cafe_camera *cam = filp->private_data;
1357 struct cafe_sio_buffer *sbuf;
1358 int ret = -EINVAL;
1359 unsigned long flags;
1361 mutex_lock(&cam->s_mutex);
1362 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1363 goto out_unlock;
1364 if (cam->state != S_STREAMING)
1365 goto out_unlock;
1366 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1367 ret = -EAGAIN;
1368 goto out_unlock;
1371 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1372 mutex_unlock(&cam->s_mutex);
1373 if (wait_event_interruptible(cam->iowait,
1374 !list_empty(&cam->sb_full))) {
1375 ret = -ERESTARTSYS;
1376 goto out;
1378 mutex_lock(&cam->s_mutex);
1381 if (cam->state != S_STREAMING)
1382 ret = -EINTR;
1383 else {
1384 spin_lock_irqsave(&cam->dev_lock, flags);
1385 /* Should probably recheck !list_empty() here */
1386 sbuf = list_entry(cam->sb_full.next,
1387 struct cafe_sio_buffer, list);
1388 list_del_init(&sbuf->list);
1389 spin_unlock_irqrestore(&cam->dev_lock, flags);
1390 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1391 *buf = sbuf->v4lbuf;
1392 ret = 0;
1395 out_unlock:
1396 mutex_unlock(&cam->s_mutex);
1397 out:
1398 return ret;
1403 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1405 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1407 * Locking: done under mmap_sem, so we don't need to
1408 * go back to the camera lock here.
1410 sbuf->mapcount++;
1414 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1416 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1418 mutex_lock(&sbuf->cam->s_mutex);
1419 sbuf->mapcount--;
1420 /* Docs say we should stop I/O too... */
1421 if (sbuf->mapcount == 0)
1422 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1423 mutex_unlock(&sbuf->cam->s_mutex);
1426 static struct vm_operations_struct cafe_v4l_vm_ops = {
1427 .open = cafe_v4l_vm_open,
1428 .close = cafe_v4l_vm_close
1432 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1434 struct cafe_camera *cam = filp->private_data;
1435 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1436 int ret = -EINVAL;
1437 int i;
1438 struct cafe_sio_buffer *sbuf = NULL;
1440 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1441 return -EINVAL;
1443 * Find the buffer they are looking for.
1445 mutex_lock(&cam->s_mutex);
1446 for (i = 0; i < cam->n_sbufs; i++)
1447 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1448 sbuf = cam->sb_bufs + i;
1449 break;
1451 if (sbuf == NULL)
1452 goto out;
1454 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1455 if (ret)
1456 goto out;
1457 vma->vm_flags |= VM_DONTEXPAND;
1458 vma->vm_private_data = sbuf;
1459 vma->vm_ops = &cafe_v4l_vm_ops;
1460 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1461 cafe_v4l_vm_open(vma);
1462 ret = 0;
1463 out:
1464 mutex_unlock(&cam->s_mutex);
1465 return ret;
1470 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1472 struct cafe_camera *cam;
1474 cam = cafe_find_dev(iminor(inode));
1475 if (cam == NULL)
1476 return -ENODEV;
1477 filp->private_data = cam;
1479 mutex_lock(&cam->s_mutex);
1480 if (cam->users == 0) {
1481 cafe_ctlr_power_up(cam);
1482 __cafe_cam_reset(cam);
1483 cafe_set_config_needed(cam, 1);
1484 /* FIXME make sure this is complete */
1486 (cam->users)++;
1487 mutex_unlock(&cam->s_mutex);
1488 return 0;
1492 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1494 struct cafe_camera *cam = filp->private_data;
1496 mutex_lock(&cam->s_mutex);
1497 (cam->users)--;
1498 if (filp == cam->owner) {
1499 cafe_ctlr_stop_dma(cam);
1500 cafe_free_sio_buffers(cam);
1501 cam->owner = NULL;
1503 if (cam->users == 0) {
1504 cafe_ctlr_power_down(cam);
1505 if (alloc_bufs_at_read)
1506 cafe_free_dma_bufs(cam);
1508 mutex_unlock(&cam->s_mutex);
1509 return 0;
1514 static unsigned int cafe_v4l_poll(struct file *filp,
1515 struct poll_table_struct *pt)
1517 struct cafe_camera *cam = filp->private_data;
1519 poll_wait(filp, &cam->iowait, pt);
1520 if (cam->next_buf >= 0)
1521 return POLLIN | POLLRDNORM;
1522 return 0;
1527 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1528 struct v4l2_queryctrl *qc)
1530 struct cafe_camera *cam = filp->private_data;
1531 int ret;
1533 mutex_lock(&cam->s_mutex);
1534 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1535 mutex_unlock(&cam->s_mutex);
1536 return ret;
1540 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1541 struct v4l2_control *ctrl)
1543 struct cafe_camera *cam = filp->private_data;
1544 int ret;
1546 mutex_lock(&cam->s_mutex);
1547 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1548 mutex_unlock(&cam->s_mutex);
1549 return ret;
1553 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1554 struct v4l2_control *ctrl)
1556 struct cafe_camera *cam = filp->private_data;
1557 int ret;
1559 mutex_lock(&cam->s_mutex);
1560 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1561 mutex_unlock(&cam->s_mutex);
1562 return ret;
1569 static int cafe_vidioc_querycap(struct file *file, void *priv,
1570 struct v4l2_capability *cap)
1572 strcpy(cap->driver, "cafe_ccic");
1573 strcpy(cap->card, "cafe_ccic");
1574 cap->version = CAFE_VERSION;
1575 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1576 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1577 return 0;
1582 * The default format we use until somebody says otherwise.
1584 static struct v4l2_pix_format cafe_def_pix_format = {
1585 .width = VGA_WIDTH,
1586 .height = VGA_HEIGHT,
1587 .pixelformat = V4L2_PIX_FMT_YUYV,
1588 .field = V4L2_FIELD_NONE,
1589 .bytesperline = VGA_WIDTH*2,
1590 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1593 static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1594 void *priv, struct v4l2_fmtdesc *fmt)
1596 struct cafe_camera *cam = priv;
1597 int ret;
1599 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1600 return -EINVAL;
1601 mutex_lock(&cam->s_mutex);
1602 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1603 mutex_unlock(&cam->s_mutex);
1604 return ret;
1608 static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1609 struct v4l2_format *fmt)
1611 struct cafe_camera *cam = priv;
1612 int ret;
1614 mutex_lock(&cam->s_mutex);
1615 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1616 mutex_unlock(&cam->s_mutex);
1617 return ret;
1620 static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1621 struct v4l2_format *fmt)
1623 struct cafe_camera *cam = priv;
1624 int ret;
1627 * Can't do anything if the device is not idle
1628 * Also can't if there are streaming buffers in place.
1630 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1631 return -EBUSY;
1633 * See if the formatting works in principle.
1635 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1636 if (ret)
1637 return ret;
1639 * Now we start to change things for real, so let's do it
1640 * under lock.
1642 mutex_lock(&cam->s_mutex);
1643 cam->pix_format = fmt->fmt.pix;
1645 * Make sure we have appropriate DMA buffers.
1647 ret = -ENOMEM;
1648 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1649 cafe_free_dma_bufs(cam);
1650 if (cam->nbufs == 0) {
1651 if (cafe_alloc_dma_bufs(cam, 0))
1652 goto out;
1655 * It looks like this might work, so let's program the sensor.
1657 ret = cafe_cam_configure(cam);
1658 if (! ret)
1659 ret = cafe_ctlr_configure(cam);
1660 out:
1661 mutex_unlock(&cam->s_mutex);
1662 return ret;
1666 * Return our stored notion of how the camera is/should be configured.
1667 * The V4l2 spec wants us to be smarter, and actually get this from
1668 * the camera (and not mess with it at open time). Someday.
1670 static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1671 struct v4l2_format *f)
1673 struct cafe_camera *cam = priv;
1675 f->fmt.pix = cam->pix_format;
1676 return 0;
1680 * We only have one input - the sensor - so minimize the nonsense here.
1682 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1683 struct v4l2_input *input)
1685 if (input->index != 0)
1686 return -EINVAL;
1688 input->type = V4L2_INPUT_TYPE_CAMERA;
1689 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1690 strcpy(input->name, "Camera");
1691 return 0;
1694 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1696 *i = 0;
1697 return 0;
1700 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1702 if (i != 0)
1703 return -EINVAL;
1704 return 0;
1707 /* from vivi.c */
1708 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1710 return 0;
1714 * G/S_PARM. Most of this is done by the sensor, but we are
1715 * the level which controls the number of read buffers.
1717 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1718 struct v4l2_streamparm *parms)
1720 struct cafe_camera *cam = priv;
1721 int ret;
1723 mutex_lock(&cam->s_mutex);
1724 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1725 mutex_unlock(&cam->s_mutex);
1726 parms->parm.capture.readbuffers = n_dma_bufs;
1727 return ret;
1730 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1731 struct v4l2_streamparm *parms)
1733 struct cafe_camera *cam = priv;
1734 int ret;
1736 mutex_lock(&cam->s_mutex);
1737 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1738 mutex_unlock(&cam->s_mutex);
1739 parms->parm.capture.readbuffers = n_dma_bufs;
1740 return ret;
1744 static void cafe_v4l_dev_release(struct video_device *vd)
1746 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1748 kfree(cam);
1753 * This template device holds all of those v4l2 methods; we
1754 * clone it for specific real devices.
1757 static const struct file_operations cafe_v4l_fops = {
1758 .owner = THIS_MODULE,
1759 .open = cafe_v4l_open,
1760 .release = cafe_v4l_release,
1761 .read = cafe_v4l_read,
1762 .poll = cafe_v4l_poll,
1763 .mmap = cafe_v4l_mmap,
1764 .ioctl = video_ioctl2,
1765 .llseek = no_llseek,
1768 static struct video_device cafe_v4l_template = {
1769 .name = "cafe",
1770 .type = VFL_TYPE_GRABBER,
1771 .type2 = VID_TYPE_CAPTURE,
1772 .minor = -1, /* Get one dynamically */
1773 .tvnorms = V4L2_STD_NTSC_M,
1774 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1776 .fops = &cafe_v4l_fops,
1777 .release = cafe_v4l_dev_release,
1779 .vidioc_querycap = cafe_vidioc_querycap,
1780 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1781 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1782 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1783 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1784 .vidioc_enum_input = cafe_vidioc_enum_input,
1785 .vidioc_g_input = cafe_vidioc_g_input,
1786 .vidioc_s_input = cafe_vidioc_s_input,
1787 .vidioc_s_std = cafe_vidioc_s_std,
1788 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1789 .vidioc_querybuf = cafe_vidioc_querybuf,
1790 .vidioc_qbuf = cafe_vidioc_qbuf,
1791 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1792 .vidioc_streamon = cafe_vidioc_streamon,
1793 .vidioc_streamoff = cafe_vidioc_streamoff,
1794 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1795 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1796 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1797 .vidioc_g_parm = cafe_vidioc_g_parm,
1798 .vidioc_s_parm = cafe_vidioc_s_parm,
1807 /* ---------------------------------------------------------------------- */
1809 * Interrupt handler stuff
1814 static void cafe_frame_tasklet(unsigned long data)
1816 struct cafe_camera *cam = (struct cafe_camera *) data;
1817 int i;
1818 unsigned long flags;
1819 struct cafe_sio_buffer *sbuf;
1821 spin_lock_irqsave(&cam->dev_lock, flags);
1822 for (i = 0; i < cam->nbufs; i++) {
1823 int bufno = cam->next_buf;
1824 if (bufno < 0) { /* "will never happen" */
1825 cam_err(cam, "No valid bufs in tasklet!\n");
1826 break;
1828 if (++(cam->next_buf) >= cam->nbufs)
1829 cam->next_buf = 0;
1830 if (! test_bit(bufno, &cam->flags))
1831 continue;
1832 if (list_empty(&cam->sb_avail))
1833 break; /* Leave it valid, hope for better later */
1834 clear_bit(bufno, &cam->flags);
1835 sbuf = list_entry(cam->sb_avail.next,
1836 struct cafe_sio_buffer, list);
1838 * Drop the lock during the big copy. This *should* be safe...
1840 spin_unlock_irqrestore(&cam->dev_lock, flags);
1841 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1842 cam->pix_format.sizeimage);
1843 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1844 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1845 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1846 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1847 spin_lock_irqsave(&cam->dev_lock, flags);
1848 list_move_tail(&sbuf->list, &cam->sb_full);
1850 if (! list_empty(&cam->sb_full))
1851 wake_up(&cam->iowait);
1852 spin_unlock_irqrestore(&cam->dev_lock, flags);
1857 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1860 * Basic frame housekeeping.
1862 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1863 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1864 set_bit(frame, &cam->flags);
1865 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1866 if (cam->next_buf < 0)
1867 cam->next_buf = frame;
1868 cam->buf_seq[frame] = ++(cam->sequence);
1870 switch (cam->state) {
1872 * If in single read mode, try going speculative.
1874 case S_SINGLEREAD:
1875 cam->state = S_SPECREAD;
1876 cam->specframes = 0;
1877 wake_up(&cam->iowait);
1878 break;
1881 * If we are already doing speculative reads, and nobody is
1882 * reading them, just stop.
1884 case S_SPECREAD:
1885 if (++(cam->specframes) >= cam->nbufs) {
1886 cafe_ctlr_stop(cam);
1887 cafe_ctlr_irq_disable(cam);
1888 cam->state = S_IDLE;
1890 wake_up(&cam->iowait);
1891 break;
1893 * For the streaming case, we defer the real work to the
1894 * camera tasklet.
1896 * FIXME: if the application is not consuming the buffers,
1897 * we should eventually put things on hold and restart in
1898 * vidioc_dqbuf().
1900 case S_STREAMING:
1901 tasklet_schedule(&cam->s_tasklet);
1902 break;
1904 default:
1905 cam_err(cam, "Frame interrupt in non-operational state\n");
1906 break;
1913 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1915 unsigned int frame;
1917 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1919 * Handle any frame completions. There really should
1920 * not be more than one of these, or we have fallen
1921 * far behind.
1923 for (frame = 0; frame < cam->nbufs; frame++)
1924 if (irqs & (IRQ_EOF0 << frame))
1925 cafe_frame_complete(cam, frame);
1927 * If a frame starts, note that we have DMA active. This
1928 * code assumes that we won't get multiple frame interrupts
1929 * at once; may want to rethink that.
1931 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1932 set_bit(CF_DMA_ACTIVE, &cam->flags);
1937 static irqreturn_t cafe_irq(int irq, void *data)
1939 struct cafe_camera *cam = data;
1940 unsigned int irqs;
1942 spin_lock(&cam->dev_lock);
1943 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1944 if ((irqs & ALLIRQS) == 0) {
1945 spin_unlock(&cam->dev_lock);
1946 return IRQ_NONE;
1948 if (irqs & FRAMEIRQS)
1949 cafe_frame_irq(cam, irqs);
1950 if (irqs & TWSIIRQS) {
1951 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1952 wake_up(&cam->smbus_wait);
1954 spin_unlock(&cam->dev_lock);
1955 return IRQ_HANDLED;
1959 /* -------------------------------------------------------------------------- */
1960 #ifdef CONFIG_VIDEO_ADV_DEBUG
1962 * Debugfs stuff.
1965 static char cafe_debug_buf[1024];
1966 static struct dentry *cafe_dfs_root;
1968 static void cafe_dfs_setup(void)
1970 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1971 if (IS_ERR(cafe_dfs_root)) {
1972 cafe_dfs_root = NULL; /* Never mind */
1973 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1977 static void cafe_dfs_shutdown(void)
1979 if (cafe_dfs_root)
1980 debugfs_remove(cafe_dfs_root);
1983 static int cafe_dfs_open(struct inode *inode, struct file *file)
1985 file->private_data = inode->i_private;
1986 return 0;
1989 static ssize_t cafe_dfs_read_regs(struct file *file,
1990 char __user *buf, size_t count, loff_t *ppos)
1992 struct cafe_camera *cam = file->private_data;
1993 char *s = cafe_debug_buf;
1994 int offset;
1996 for (offset = 0; offset < 0x44; offset += 4)
1997 s += sprintf(s, "%02x: %08x\n", offset,
1998 cafe_reg_read(cam, offset));
1999 for (offset = 0x88; offset <= 0x90; offset += 4)
2000 s += sprintf(s, "%02x: %08x\n", offset,
2001 cafe_reg_read(cam, offset));
2002 for (offset = 0xb4; offset <= 0xbc; offset += 4)
2003 s += sprintf(s, "%02x: %08x\n", offset,
2004 cafe_reg_read(cam, offset));
2005 for (offset = 0x3000; offset <= 0x300c; offset += 4)
2006 s += sprintf(s, "%04x: %08x\n", offset,
2007 cafe_reg_read(cam, offset));
2008 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2009 s - cafe_debug_buf);
2012 static const struct file_operations cafe_dfs_reg_ops = {
2013 .owner = THIS_MODULE,
2014 .read = cafe_dfs_read_regs,
2015 .open = cafe_dfs_open
2018 static ssize_t cafe_dfs_read_cam(struct file *file,
2019 char __user *buf, size_t count, loff_t *ppos)
2021 struct cafe_camera *cam = file->private_data;
2022 char *s = cafe_debug_buf;
2023 int offset;
2025 if (! cam->sensor)
2026 return -EINVAL;
2027 for (offset = 0x0; offset < 0x8a; offset++)
2029 u8 v;
2031 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2032 s += sprintf(s, "%02x: %02x\n", offset, v);
2034 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2035 s - cafe_debug_buf);
2038 static const struct file_operations cafe_dfs_cam_ops = {
2039 .owner = THIS_MODULE,
2040 .read = cafe_dfs_read_cam,
2041 .open = cafe_dfs_open
2046 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2048 char fname[40];
2050 if (!cafe_dfs_root)
2051 return;
2052 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2053 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2054 cam, &cafe_dfs_reg_ops);
2055 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2056 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2057 cam, &cafe_dfs_cam_ops);
2061 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2063 if (! IS_ERR(cam->dfs_regs))
2064 debugfs_remove(cam->dfs_regs);
2065 if (! IS_ERR(cam->dfs_cam_regs))
2066 debugfs_remove(cam->dfs_cam_regs);
2069 #else
2071 #define cafe_dfs_setup()
2072 #define cafe_dfs_shutdown()
2073 #define cafe_dfs_cam_setup(cam)
2074 #define cafe_dfs_cam_shutdown(cam)
2075 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2080 /* ------------------------------------------------------------------------*/
2082 * PCI interface stuff.
2085 static int cafe_pci_probe(struct pci_dev *pdev,
2086 const struct pci_device_id *id)
2088 int ret;
2089 u16 classword;
2090 struct cafe_camera *cam;
2092 * Make sure we have a camera here - we'll get calls for
2093 * the other cafe devices as well.
2095 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2096 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2097 return -ENODEV;
2099 * Start putting together one of our big camera structures.
2101 ret = -ENOMEM;
2102 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2103 if (cam == NULL)
2104 goto out;
2105 mutex_init(&cam->s_mutex);
2106 mutex_lock(&cam->s_mutex);
2107 spin_lock_init(&cam->dev_lock);
2108 cam->state = S_NOTREADY;
2109 cafe_set_config_needed(cam, 1);
2110 init_waitqueue_head(&cam->smbus_wait);
2111 init_waitqueue_head(&cam->iowait);
2112 cam->pdev = pdev;
2113 cam->pix_format = cafe_def_pix_format;
2114 INIT_LIST_HEAD(&cam->dev_list);
2115 INIT_LIST_HEAD(&cam->sb_avail);
2116 INIT_LIST_HEAD(&cam->sb_full);
2117 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2119 * Get set up on the PCI bus.
2121 ret = pci_enable_device(pdev);
2122 if (ret)
2123 goto out_free;
2124 pci_set_master(pdev);
2126 ret = -EIO;
2127 cam->regs = pci_iomap(pdev, 0, 0);
2128 if (! cam->regs) {
2129 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2130 goto out_free;
2132 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2133 if (ret)
2134 goto out_iounmap;
2136 * Initialize the controller and leave it powered up. It will
2137 * stay that way until the sensor driver shows up.
2139 cafe_ctlr_init(cam);
2140 cafe_ctlr_power_up(cam);
2142 * Set up I2C/SMBUS communications. We have to drop the mutex here
2143 * because the sensor could attach in this call chain, leading to
2144 * unsightly deadlocks.
2146 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2147 ret = cafe_smbus_setup(cam);
2148 if (ret)
2149 goto out_freeirq;
2151 * Get the v4l2 setup done.
2153 mutex_lock(&cam->s_mutex);
2154 cam->v4ldev = cafe_v4l_template;
2155 cam->v4ldev.debug = 0;
2156 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2157 cam->v4ldev.dev = &pdev->dev;
2158 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2159 if (ret)
2160 goto out_smbus;
2162 * If so requested, try to get our DMA buffers now.
2164 if (!alloc_bufs_at_read) {
2165 if (cafe_alloc_dma_bufs(cam, 1))
2166 cam_warn(cam, "Unable to alloc DMA buffers at load"
2167 " will try again later.");
2170 cafe_dfs_cam_setup(cam);
2171 mutex_unlock(&cam->s_mutex);
2172 cafe_add_dev(cam);
2173 return 0;
2175 out_smbus:
2176 cafe_smbus_shutdown(cam);
2177 out_freeirq:
2178 cafe_ctlr_power_down(cam);
2179 free_irq(pdev->irq, cam);
2180 out_iounmap:
2181 pci_iounmap(pdev, cam->regs);
2182 out_free:
2183 kfree(cam);
2184 out:
2185 return ret;
2190 * Shut down an initialized device
2192 static void cafe_shutdown(struct cafe_camera *cam)
2194 /* FIXME: Make sure we take care of everything here */
2195 cafe_dfs_cam_shutdown(cam);
2196 if (cam->n_sbufs > 0)
2197 /* What if they are still mapped? Shouldn't be, but... */
2198 cafe_free_sio_buffers(cam);
2199 cafe_remove_dev(cam);
2200 cafe_ctlr_stop_dma(cam);
2201 cafe_ctlr_power_down(cam);
2202 cafe_smbus_shutdown(cam);
2203 cafe_free_dma_bufs(cam);
2204 free_irq(cam->pdev->irq, cam);
2205 pci_iounmap(cam->pdev, cam->regs);
2206 video_unregister_device(&cam->v4ldev);
2207 /* kfree(cam); done in v4l_release () */
2211 static void cafe_pci_remove(struct pci_dev *pdev)
2213 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2215 if (cam == NULL) {
2216 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2217 return;
2219 mutex_lock(&cam->s_mutex);
2220 if (cam->users > 0)
2221 cam_warn(cam, "Removing a device with users!\n");
2222 cafe_shutdown(cam);
2223 /* No unlock - it no longer exists */
2227 #ifdef CONFIG_PM
2229 * Basic power management.
2231 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2233 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2234 int ret;
2236 ret = pci_save_state(pdev);
2237 if (ret)
2238 return ret;
2239 cafe_ctlr_stop_dma(cam);
2240 cafe_ctlr_power_down(cam);
2241 pci_disable_device(pdev);
2242 return 0;
2246 static int cafe_pci_resume(struct pci_dev *pdev)
2248 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2249 int ret = 0;
2251 ret = pci_restore_state(pdev);
2252 if (ret)
2253 return ret;
2254 ret = pci_enable_device(pdev);
2256 if (ret) {
2257 cam_warn(cam, "Unable to re-enable device on resume!\n");
2258 return ret;
2260 cafe_ctlr_init(cam);
2261 cafe_ctlr_power_down(cam);
2263 mutex_lock(&cam->s_mutex);
2264 if (cam->users > 0) {
2265 cafe_ctlr_power_up(cam);
2266 __cafe_cam_reset(cam);
2268 mutex_unlock(&cam->s_mutex);
2270 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2271 if (cam->state == S_SPECREAD)
2272 cam->state = S_IDLE; /* Don't bother restarting */
2273 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2274 ret = cafe_read_setup(cam, cam->state);
2275 return ret;
2278 #endif /* CONFIG_PM */
2281 static struct pci_device_id cafe_ids[] = {
2282 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2283 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2284 { 0, }
2287 MODULE_DEVICE_TABLE(pci, cafe_ids);
2289 static struct pci_driver cafe_pci_driver = {
2290 .name = "cafe1000-ccic",
2291 .id_table = cafe_ids,
2292 .probe = cafe_pci_probe,
2293 .remove = cafe_pci_remove,
2294 #ifdef CONFIG_PM
2295 .suspend = cafe_pci_suspend,
2296 .resume = cafe_pci_resume,
2297 #endif
2303 static int __init cafe_init(void)
2305 int ret;
2307 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2308 CAFE_VERSION);
2309 cafe_dfs_setup();
2310 ret = pci_register_driver(&cafe_pci_driver);
2311 if (ret) {
2312 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2313 goto out;
2315 request_module("ov7670"); /* FIXME want something more general */
2316 ret = 0;
2318 out:
2319 return ret;
2323 static void __exit cafe_exit(void)
2325 pci_unregister_driver(&cafe_pci_driver);
2326 cafe_dfs_shutdown();
2329 module_init(cafe_init);
2330 module_exit(cafe_exit);