Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[wrt350n-kernel.git] / drivers / media / video / cafe_ccic.c
blob7ae499c9c54c8785e497e9ddabdcde740c017f01
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 * The data sheet for this device can be found at:
7 * http://www.marvell.com/products/pcconn/88ALP01.jsp
9 * Copyright 2006 One Laptop Per Child Association, Inc.
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
12 * Written by Jonathan Corbet, corbet@lwn.net.
14 * This file may be distributed under the terms of the GNU General
15 * Public License, version 2.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/fs.h>
22 #include <linux/pci.h>
23 #include <linux/i2c.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-chip-ident.h>
29 #include <linux/device.h>
30 #include <linux/wait.h>
31 #include <linux/list.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/delay.h>
34 #include <linux/debugfs.h>
35 #include <linux/jiffies.h>
36 #include <linux/vmalloc.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
41 #include "cafe_ccic-regs.h"
43 #define CAFE_VERSION 0x000002
47 * Parameters.
49 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
50 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
51 MODULE_LICENSE("GPL");
52 MODULE_SUPPORTED_DEVICE("Video");
55 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
56 * we must have physically contiguous buffers to bring frames into.
57 * These parameters control how many buffers we use, whether we
58 * allocate them at load time (better chance of success, but nails down
59 * memory) or when somebody tries to use the camera (riskier), and,
60 * for load-time allocation, how big they should be.
62 * The controller can cycle through three buffers. We could use
63 * more by flipping pointers around, but it probably makes little
64 * sense.
67 #define MAX_DMA_BUFS 3
68 static int alloc_bufs_at_read = 0;
69 module_param(alloc_bufs_at_read, bool, 0444);
70 MODULE_PARM_DESC(alloc_bufs_at_read,
71 "Non-zero value causes DMA buffers to be allocated when the "
72 "video capture device is read, rather than at module load "
73 "time. This saves memory, but decreases the chances of "
74 "successfully getting those buffers.");
76 static int n_dma_bufs = 3;
77 module_param(n_dma_bufs, uint, 0644);
78 MODULE_PARM_DESC(n_dma_bufs,
79 "The number of DMA buffers to allocate. Can be either two "
80 "(saves memory, makes timing tighter) or three.");
82 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
83 module_param(dma_buf_size, uint, 0444);
84 MODULE_PARM_DESC(dma_buf_size,
85 "The size of the allocated DMA buffers. If actual operating "
86 "parameters require larger buffers, an attempt to reallocate "
87 "will be made.");
89 static int min_buffers = 1;
90 module_param(min_buffers, uint, 0644);
91 MODULE_PARM_DESC(min_buffers,
92 "The minimum number of streaming I/O buffers we are willing "
93 "to work with.");
95 static int max_buffers = 10;
96 module_param(max_buffers, uint, 0644);
97 MODULE_PARM_DESC(max_buffers,
98 "The maximum number of streaming I/O buffers an application "
99 "will be allowed to allocate. These buffers are big and live "
100 "in vmalloc space.");
102 static int flip = 0;
103 module_param(flip, bool, 0444);
104 MODULE_PARM_DESC(flip,
105 "If set, the sensor will be instructed to flip the image "
106 "vertically.");
109 enum cafe_state {
110 S_NOTREADY, /* Not yet initialized */
111 S_IDLE, /* Just hanging around */
112 S_FLAKED, /* Some sort of problem */
113 S_SINGLEREAD, /* In read() */
114 S_SPECREAD, /* Speculative read (for future read()) */
115 S_STREAMING /* Streaming data */
119 * Tracking of streaming I/O buffers.
121 struct cafe_sio_buffer {
122 struct list_head list;
123 struct v4l2_buffer v4lbuf;
124 char *buffer; /* Where it lives in kernel space */
125 int mapcount;
126 struct cafe_camera *cam;
130 * A description of one of our devices.
131 * Locking: controlled by s_mutex. Certain fields, however, require
132 * the dev_lock spinlock; they are marked as such by comments.
133 * dev_lock is also required for access to device registers.
135 struct cafe_camera
137 enum cafe_state state;
138 unsigned long flags; /* Buffer status, mainly (dev_lock) */
139 int users; /* How many open FDs */
140 struct file *owner; /* Who has data access (v4l2) */
143 * Subsystem structures.
145 struct pci_dev *pdev;
146 struct video_device v4ldev;
147 struct i2c_adapter i2c_adapter;
148 struct i2c_client *sensor;
150 unsigned char __iomem *regs;
151 struct list_head dev_list; /* link to other devices */
153 /* DMA buffers */
154 unsigned int nbufs; /* How many are alloc'd */
155 int next_buf; /* Next to consume (dev_lock) */
156 unsigned int dma_buf_size; /* allocated size */
157 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
158 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
159 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
160 unsigned int sequence; /* Frame sequence number */
161 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
163 /* Streaming buffers */
164 unsigned int n_sbufs; /* How many we have */
165 struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
166 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
167 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
168 struct tasklet_struct s_tasklet;
170 /* Current operating parameters */
171 u32 sensor_type; /* Currently ov7670 only */
172 struct v4l2_pix_format pix_format;
174 /* Locks */
175 struct mutex s_mutex; /* Access to this structure */
176 spinlock_t dev_lock; /* Access to device */
178 /* Misc */
179 wait_queue_head_t smbus_wait; /* Waiting on i2c events */
180 wait_queue_head_t iowait; /* Waiting on frame data */
181 #ifdef CONFIG_VIDEO_ADV_DEBUG
182 struct dentry *dfs_regs;
183 struct dentry *dfs_cam_regs;
184 #endif
188 * Status flags. Always manipulated with bit operations.
190 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
191 #define CF_BUF1_VALID 1
192 #define CF_BUF2_VALID 2
193 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
194 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
199 * Start over with DMA buffers - dev_lock needed.
201 static void cafe_reset_buffers(struct cafe_camera *cam)
203 int i;
205 cam->next_buf = -1;
206 for (i = 0; i < cam->nbufs; i++)
207 clear_bit(i, &cam->flags);
208 cam->specframes = 0;
211 static inline int cafe_needs_config(struct cafe_camera *cam)
213 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
216 static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
218 if (needed)
219 set_bit(CF_CONFIG_NEEDED, &cam->flags);
220 else
221 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
228 * Debugging and related.
230 #define cam_err(cam, fmt, arg...) \
231 dev_err(&(cam)->pdev->dev, fmt, ##arg);
232 #define cam_warn(cam, fmt, arg...) \
233 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
234 #define cam_dbg(cam, fmt, arg...) \
235 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
238 /* ---------------------------------------------------------------------*/
240 * We keep a simple list of known devices to search at open time.
242 static LIST_HEAD(cafe_dev_list);
243 static DEFINE_MUTEX(cafe_dev_list_lock);
245 static void cafe_add_dev(struct cafe_camera *cam)
247 mutex_lock(&cafe_dev_list_lock);
248 list_add_tail(&cam->dev_list, &cafe_dev_list);
249 mutex_unlock(&cafe_dev_list_lock);
252 static void cafe_remove_dev(struct cafe_camera *cam)
254 mutex_lock(&cafe_dev_list_lock);
255 list_del(&cam->dev_list);
256 mutex_unlock(&cafe_dev_list_lock);
259 static struct cafe_camera *cafe_find_dev(int minor)
261 struct cafe_camera *cam;
263 mutex_lock(&cafe_dev_list_lock);
264 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
265 if (cam->v4ldev.minor == minor)
266 goto done;
268 cam = NULL;
269 done:
270 mutex_unlock(&cafe_dev_list_lock);
271 return cam;
275 static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
277 struct cafe_camera *cam;
279 mutex_lock(&cafe_dev_list_lock);
280 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
281 if (cam->pdev == pdev)
282 goto done;
284 cam = NULL;
285 done:
286 mutex_unlock(&cafe_dev_list_lock);
287 return cam;
291 /* ------------------------------------------------------------------------ */
293 * Device register I/O
295 static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
296 unsigned int val)
298 iowrite32(val, cam->regs + reg);
301 static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
302 unsigned int reg)
304 return ioread32(cam->regs + reg);
308 static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
309 unsigned int val, unsigned int mask)
311 unsigned int v = cafe_reg_read(cam, reg);
313 v = (v & ~mask) | (val & mask);
314 cafe_reg_write(cam, reg, v);
317 static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
318 unsigned int reg, unsigned int val)
320 cafe_reg_write_mask(cam, reg, 0, val);
323 static inline void cafe_reg_set_bit(struct cafe_camera *cam,
324 unsigned int reg, unsigned int val)
326 cafe_reg_write_mask(cam, reg, val, val);
331 /* -------------------------------------------------------------------- */
333 * The I2C/SMBUS interface to the camera itself starts here. The
334 * controller handles SMBUS itself, presenting a relatively simple register
335 * interface; all we have to do is to tell it where to route the data.
337 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
339 static int cafe_smbus_write_done(struct cafe_camera *cam)
341 unsigned long flags;
342 int c1;
345 * We must delay after the interrupt, or the controller gets confused
346 * and never does give us good status. Fortunately, we don't do this
347 * often.
349 udelay(20);
350 spin_lock_irqsave(&cam->dev_lock, flags);
351 c1 = cafe_reg_read(cam, REG_TWSIC1);
352 spin_unlock_irqrestore(&cam->dev_lock, flags);
353 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
356 static int cafe_smbus_write_data(struct cafe_camera *cam,
357 u16 addr, u8 command, u8 value)
359 unsigned int rval;
360 unsigned long flags;
361 DEFINE_WAIT(the_wait);
363 spin_lock_irqsave(&cam->dev_lock, flags);
364 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
365 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
367 * Marvell sez set clkdiv to all 1's for now.
369 rval |= TWSIC0_CLKDIV;
370 cafe_reg_write(cam, REG_TWSIC0, rval);
371 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
372 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
373 cafe_reg_write(cam, REG_TWSIC1, rval);
374 spin_unlock_irqrestore(&cam->dev_lock, flags);
377 * Time to wait for the write to complete. THIS IS A RACY
378 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
379 * register too quickly after starting the operation sends
380 * the device into a place that may be kinder and better, but
381 * which is absolutely useless for controlling the sensor. In
382 * practice we have plenty of time to get into our sleep state
383 * before the interrupt hits, and the worst case is that we
384 * time out and then see that things completed, so this seems
385 * the best way for now.
387 do {
388 prepare_to_wait(&cam->smbus_wait, &the_wait,
389 TASK_UNINTERRUPTIBLE);
390 schedule_timeout(1); /* even 1 jiffy is too long */
391 finish_wait(&cam->smbus_wait, &the_wait);
392 } while (!cafe_smbus_write_done(cam));
394 #ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
395 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
396 CAFE_SMBUS_TIMEOUT);
397 #endif
398 spin_lock_irqsave(&cam->dev_lock, flags);
399 rval = cafe_reg_read(cam, REG_TWSIC1);
400 spin_unlock_irqrestore(&cam->dev_lock, flags);
402 if (rval & TWSIC1_WSTAT) {
403 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
404 command, value);
405 return -EIO;
407 if (rval & TWSIC1_ERROR) {
408 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
409 command, value);
410 return -EIO;
412 return 0;
417 static int cafe_smbus_read_done(struct cafe_camera *cam)
419 unsigned long flags;
420 int c1;
423 * We must delay after the interrupt, or the controller gets confused
424 * and never does give us good status. Fortunately, we don't do this
425 * often.
427 udelay(20);
428 spin_lock_irqsave(&cam->dev_lock, flags);
429 c1 = cafe_reg_read(cam, REG_TWSIC1);
430 spin_unlock_irqrestore(&cam->dev_lock, flags);
431 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
436 static int cafe_smbus_read_data(struct cafe_camera *cam,
437 u16 addr, u8 command, u8 *value)
439 unsigned int rval;
440 unsigned long flags;
442 spin_lock_irqsave(&cam->dev_lock, flags);
443 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
444 rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
446 * Marvel sez set clkdiv to all 1's for now.
448 rval |= TWSIC0_CLKDIV;
449 cafe_reg_write(cam, REG_TWSIC0, rval);
450 (void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
451 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
452 cafe_reg_write(cam, REG_TWSIC1, rval);
453 spin_unlock_irqrestore(&cam->dev_lock, flags);
455 wait_event_timeout(cam->smbus_wait,
456 cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
457 spin_lock_irqsave(&cam->dev_lock, flags);
458 rval = cafe_reg_read(cam, REG_TWSIC1);
459 spin_unlock_irqrestore(&cam->dev_lock, flags);
461 if (rval & TWSIC1_ERROR) {
462 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
463 return -EIO;
465 if (! (rval & TWSIC1_RVALID)) {
466 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
467 command);
468 return -EIO;
470 *value = rval & 0xff;
471 return 0;
475 * Perform a transfer over SMBUS. This thing is called under
476 * the i2c bus lock, so we shouldn't race with ourselves...
478 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
479 unsigned short flags, char rw, u8 command,
480 int size, union i2c_smbus_data *data)
482 struct cafe_camera *cam = i2c_get_adapdata(adapter);
483 int ret = -EINVAL;
486 * Refuse to talk to anything but OV cam chips. We should
487 * never even see an attempt to do so, but one never knows.
489 if (cam->sensor && addr != cam->sensor->addr) {
490 cam_err(cam, "funky smbus addr %d\n", addr);
491 return -EINVAL;
494 * This interface would appear to only do byte data ops. OK
495 * it can do word too, but the cam chip has no use for that.
497 if (size != I2C_SMBUS_BYTE_DATA) {
498 cam_err(cam, "funky xfer size %d\n", size);
499 return -EINVAL;
502 if (rw == I2C_SMBUS_WRITE)
503 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
504 else if (rw == I2C_SMBUS_READ)
505 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
506 return ret;
510 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
512 unsigned long flags;
514 spin_lock_irqsave(&cam->dev_lock, flags);
515 cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
516 spin_unlock_irqrestore(&cam->dev_lock, flags);
519 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
521 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
522 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
525 static struct i2c_algorithm cafe_smbus_algo = {
526 .smbus_xfer = cafe_smbus_xfer,
527 .functionality = cafe_smbus_func
530 /* Somebody is on the bus */
531 static int cafe_cam_init(struct cafe_camera *cam);
532 static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
533 static void cafe_ctlr_power_down(struct cafe_camera *cam);
535 static int cafe_smbus_attach(struct i2c_client *client)
537 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
540 * Don't talk to chips we don't recognize.
542 if (client->driver->id == I2C_DRIVERID_OV7670) {
543 cam->sensor = client;
544 return cafe_cam_init(cam);
546 return -EINVAL;
549 static int cafe_smbus_detach(struct i2c_client *client)
551 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
553 if (cam->sensor == client) {
554 cafe_ctlr_stop_dma(cam);
555 cafe_ctlr_power_down(cam);
556 cam_err(cam, "lost the sensor!\n");
557 cam->sensor = NULL; /* Bummer, no camera */
558 cam->state = S_NOTREADY;
560 return 0;
563 static int cafe_smbus_setup(struct cafe_camera *cam)
565 struct i2c_adapter *adap = &cam->i2c_adapter;
566 int ret;
568 cafe_smbus_enable_irq(cam);
569 adap->id = I2C_HW_SMBUS_CAFE;
570 adap->class = I2C_CLASS_CAM_DIGITAL;
571 adap->owner = THIS_MODULE;
572 adap->client_register = cafe_smbus_attach;
573 adap->client_unregister = cafe_smbus_detach;
574 adap->algo = &cafe_smbus_algo;
575 strcpy(adap->name, "cafe_ccic");
576 adap->dev.parent = &cam->pdev->dev;
577 i2c_set_adapdata(adap, cam);
578 ret = i2c_add_adapter(adap);
579 if (ret)
580 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
581 return ret;
584 static void cafe_smbus_shutdown(struct cafe_camera *cam)
586 i2c_del_adapter(&cam->i2c_adapter);
590 /* ------------------------------------------------------------------- */
592 * Deal with the controller.
596 * Do everything we think we need to have the interface operating
597 * according to the desired format.
599 static void cafe_ctlr_dma(struct cafe_camera *cam)
602 * Store the first two Y buffers (we aren't supporting
603 * planar formats for now, so no UV bufs). Then either
604 * set the third if it exists, or tell the controller
605 * to just use two.
607 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
608 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
609 if (cam->nbufs > 2) {
610 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
611 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
613 else
614 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
615 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
618 static void cafe_ctlr_image(struct cafe_camera *cam)
620 int imgsz;
621 struct v4l2_pix_format *fmt = &cam->pix_format;
623 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
624 (fmt->bytesperline & IMGSZ_H_MASK);
625 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
626 cafe_reg_write(cam, REG_IMGOFFSET, 0);
627 /* YPITCH just drops the last two bits */
628 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
629 IMGP_YP_MASK);
631 * Tell the controller about the image format we are using.
633 switch (cam->pix_format.pixelformat) {
634 case V4L2_PIX_FMT_YUYV:
635 cafe_reg_write_mask(cam, REG_CTRL0,
636 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
637 C0_DF_MASK);
638 break;
640 case V4L2_PIX_FMT_RGB444:
641 cafe_reg_write_mask(cam, REG_CTRL0,
642 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
643 C0_DF_MASK);
644 /* Alpha value? */
645 break;
647 case V4L2_PIX_FMT_RGB565:
648 cafe_reg_write_mask(cam, REG_CTRL0,
649 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
650 C0_DF_MASK);
651 break;
653 default:
654 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
655 break;
658 * Make sure it knows we want to use hsync/vsync.
660 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
661 C0_SIFM_MASK);
666 * Configure the controller for operation; caller holds the
667 * device mutex.
669 static int cafe_ctlr_configure(struct cafe_camera *cam)
671 unsigned long flags;
673 spin_lock_irqsave(&cam->dev_lock, flags);
674 cafe_ctlr_dma(cam);
675 cafe_ctlr_image(cam);
676 cafe_set_config_needed(cam, 0);
677 spin_unlock_irqrestore(&cam->dev_lock, flags);
678 return 0;
681 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
684 * Clear any pending interrupts, since we do not
685 * expect to have I/O active prior to enabling.
687 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
688 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
691 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
693 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
697 * Make the controller start grabbing images. Everything must
698 * be set up before doing this.
700 static void cafe_ctlr_start(struct cafe_camera *cam)
702 /* set_bit performs a read, so no other barrier should be
703 needed here */
704 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
707 static void cafe_ctlr_stop(struct cafe_camera *cam)
709 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
712 static void cafe_ctlr_init(struct cafe_camera *cam)
714 unsigned long flags;
716 spin_lock_irqsave(&cam->dev_lock, flags);
718 * Added magic to bring up the hardware on the B-Test board
720 cafe_reg_write(cam, 0x3038, 0x8);
721 cafe_reg_write(cam, 0x315c, 0x80008);
723 * Go through the dance needed to wake the device up.
724 * Note that these registers are global and shared
725 * with the NAND and SD devices. Interaction between the
726 * three still needs to be examined.
728 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
729 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
730 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
732 * Here we must wait a bit for the controller to come around.
734 spin_unlock_irqrestore(&cam->dev_lock, flags);
735 msleep(5);
736 spin_lock_irqsave(&cam->dev_lock, flags);
738 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
739 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
741 * Make sure it's not powered down.
743 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
745 * Turn off the enable bit. It sure should be off anyway,
746 * but it's good to be sure.
748 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
750 * Mask all interrupts.
752 cafe_reg_write(cam, REG_IRQMASK, 0);
754 * Clock the sensor appropriately. Controller clock should
755 * be 48MHz, sensor "typical" value is half that.
757 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
758 spin_unlock_irqrestore(&cam->dev_lock, flags);
763 * Stop the controller, and don't return until we're really sure that no
764 * further DMA is going on.
766 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
768 unsigned long flags;
771 * Theory: stop the camera controller (whether it is operating
772 * or not). Delay briefly just in case we race with the SOF
773 * interrupt, then wait until no DMA is active.
775 spin_lock_irqsave(&cam->dev_lock, flags);
776 cafe_ctlr_stop(cam);
777 spin_unlock_irqrestore(&cam->dev_lock, flags);
778 mdelay(1);
779 wait_event_timeout(cam->iowait,
780 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
781 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
782 cam_err(cam, "Timeout waiting for DMA to end\n");
783 /* This would be bad news - what now? */
784 spin_lock_irqsave(&cam->dev_lock, flags);
785 cam->state = S_IDLE;
786 cafe_ctlr_irq_disable(cam);
787 spin_unlock_irqrestore(&cam->dev_lock, flags);
791 * Power up and down.
793 static void cafe_ctlr_power_up(struct cafe_camera *cam)
795 unsigned long flags;
797 spin_lock_irqsave(&cam->dev_lock, flags);
798 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
800 * Part one of the sensor dance: turn the global
801 * GPIO signal on.
803 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
804 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
806 * Put the sensor into operational mode (assumes OLPC-style
807 * wiring). Control 0 is reset - set to 1 to operate.
808 * Control 1 is power down, set to 0 to operate.
810 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
811 // mdelay(1); /* Marvell says 1ms will do it */
812 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
813 // mdelay(1); /* Enough? */
814 spin_unlock_irqrestore(&cam->dev_lock, flags);
815 msleep(5); /* Just to be sure */
818 static void cafe_ctlr_power_down(struct cafe_camera *cam)
820 unsigned long flags;
822 spin_lock_irqsave(&cam->dev_lock, flags);
823 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
824 cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
825 cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
826 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
827 spin_unlock_irqrestore(&cam->dev_lock, flags);
830 /* -------------------------------------------------------------------- */
832 * Communications with the sensor.
835 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
837 struct i2c_client *sc = cam->sensor;
838 int ret;
840 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
841 return -EINVAL;
842 ret = sc->driver->command(sc, cmd, arg);
843 if (ret == -EPERM) /* Unsupported command */
844 return 0;
845 return ret;
848 static int __cafe_cam_reset(struct cafe_camera *cam)
850 int zero = 0;
851 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
855 * We have found the sensor on the i2c. Let's try to have a
856 * conversation.
858 static int cafe_cam_init(struct cafe_camera *cam)
860 struct v4l2_chip_ident chip = { V4L2_CHIP_MATCH_I2C_ADDR, 0, 0, 0 };
861 int ret;
863 mutex_lock(&cam->s_mutex);
864 if (cam->state != S_NOTREADY)
865 cam_warn(cam, "Cam init with device in funky state %d",
866 cam->state);
867 ret = __cafe_cam_reset(cam);
868 if (ret)
869 goto out;
870 chip.match_chip = cam->sensor->addr;
871 ret = __cafe_cam_cmd(cam, VIDIOC_G_CHIP_IDENT, &chip);
872 if (ret)
873 goto out;
874 cam->sensor_type = chip.ident;
875 // if (cam->sensor->addr != OV7xx0_SID) {
876 if (cam->sensor_type != V4L2_IDENT_OV7670) {
877 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
878 ret = -EINVAL;
879 goto out;
881 /* Get/set parameters? */
882 ret = 0;
883 cam->state = S_IDLE;
884 out:
885 cafe_ctlr_power_down(cam);
886 mutex_unlock(&cam->s_mutex);
887 return ret;
891 * Configure the sensor to match the parameters we have. Caller should
892 * hold s_mutex
894 static int cafe_cam_set_flip(struct cafe_camera *cam)
896 struct v4l2_control ctrl;
898 memset(&ctrl, 0, sizeof(ctrl));
899 ctrl.id = V4L2_CID_VFLIP;
900 ctrl.value = flip;
901 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
905 static int cafe_cam_configure(struct cafe_camera *cam)
907 struct v4l2_format fmt;
908 int ret, zero = 0;
910 if (cam->state != S_IDLE)
911 return -EINVAL;
912 fmt.fmt.pix = cam->pix_format;
913 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
914 if (ret == 0)
915 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
917 * OV7670 does weird things if flip is set *before* format...
919 ret += cafe_cam_set_flip(cam);
920 return ret;
923 /* -------------------------------------------------------------------- */
925 * DMA buffer management. These functions need s_mutex held.
928 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
929 * does a get_free_pages() call, and we waste a good chunk of an orderN
930 * allocation. Should try to allocate the whole set in one chunk.
932 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
934 int i;
936 cafe_set_config_needed(cam, 1);
937 if (loadtime)
938 cam->dma_buf_size = dma_buf_size;
939 else
940 cam->dma_buf_size = cam->pix_format.sizeimage;
941 if (n_dma_bufs > 3)
942 n_dma_bufs = 3;
944 cam->nbufs = 0;
945 for (i = 0; i < n_dma_bufs; i++) {
946 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
947 cam->dma_buf_size, cam->dma_handles + i,
948 GFP_KERNEL);
949 if (cam->dma_bufs[i] == NULL) {
950 cam_warn(cam, "Failed to allocate DMA buffer\n");
951 break;
953 /* For debug, remove eventually */
954 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
955 (cam->nbufs)++;
958 switch (cam->nbufs) {
959 case 1:
960 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
961 cam->dma_bufs[0], cam->dma_handles[0]);
962 cam->nbufs = 0;
963 case 0:
964 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
965 return -ENOMEM;
967 case 2:
968 if (n_dma_bufs > 2)
969 cam_warn(cam, "Will limp along with only 2 buffers\n");
970 break;
972 return 0;
975 static void cafe_free_dma_bufs(struct cafe_camera *cam)
977 int i;
979 for (i = 0; i < cam->nbufs; i++) {
980 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
981 cam->dma_bufs[i], cam->dma_handles[i]);
982 cam->dma_bufs[i] = NULL;
984 cam->nbufs = 0;
991 /* ----------------------------------------------------------------------- */
993 * Here starts the V4L2 interface code.
997 * Read an image from the device.
999 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
1000 char __user *buffer, size_t len, loff_t *pos)
1002 int bufno;
1003 unsigned long flags;
1005 spin_lock_irqsave(&cam->dev_lock, flags);
1006 if (cam->next_buf < 0) {
1007 cam_err(cam, "deliver_buffer: No next buffer\n");
1008 spin_unlock_irqrestore(&cam->dev_lock, flags);
1009 return -EIO;
1011 bufno = cam->next_buf;
1012 clear_bit(bufno, &cam->flags);
1013 if (++(cam->next_buf) >= cam->nbufs)
1014 cam->next_buf = 0;
1015 if (! test_bit(cam->next_buf, &cam->flags))
1016 cam->next_buf = -1;
1017 cam->specframes = 0;
1018 spin_unlock_irqrestore(&cam->dev_lock, flags);
1020 if (len > cam->pix_format.sizeimage)
1021 len = cam->pix_format.sizeimage;
1022 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
1023 return -EFAULT;
1024 (*pos) += len;
1025 return len;
1029 * Get everything ready, and start grabbing frames.
1031 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
1033 int ret;
1034 unsigned long flags;
1037 * Configuration. If we still don't have DMA buffers,
1038 * make one last, desperate attempt.
1040 if (cam->nbufs == 0)
1041 if (cafe_alloc_dma_bufs(cam, 0))
1042 return -ENOMEM;
1044 if (cafe_needs_config(cam)) {
1045 cafe_cam_configure(cam);
1046 ret = cafe_ctlr_configure(cam);
1047 if (ret)
1048 return ret;
1052 * Turn it loose.
1054 spin_lock_irqsave(&cam->dev_lock, flags);
1055 cafe_reset_buffers(cam);
1056 cafe_ctlr_irq_enable(cam);
1057 cam->state = state;
1058 cafe_ctlr_start(cam);
1059 spin_unlock_irqrestore(&cam->dev_lock, flags);
1060 return 0;
1064 static ssize_t cafe_v4l_read(struct file *filp,
1065 char __user *buffer, size_t len, loff_t *pos)
1067 struct cafe_camera *cam = filp->private_data;
1068 int ret = 0;
1071 * Perhaps we're in speculative read mode and already
1072 * have data?
1074 mutex_lock(&cam->s_mutex);
1075 if (cam->state == S_SPECREAD) {
1076 if (cam->next_buf >= 0) {
1077 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1078 if (ret != 0)
1079 goto out_unlock;
1081 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1082 ret = -EIO;
1083 goto out_unlock;
1084 } else if (cam->state != S_IDLE) {
1085 ret = -EBUSY;
1086 goto out_unlock;
1090 * v4l2: multiple processes can open the device, but only
1091 * one gets to grab data from it.
1093 if (cam->owner && cam->owner != filp) {
1094 ret = -EBUSY;
1095 goto out_unlock;
1097 cam->owner = filp;
1100 * Do setup if need be.
1102 if (cam->state != S_SPECREAD) {
1103 ret = cafe_read_setup(cam, S_SINGLEREAD);
1104 if (ret)
1105 goto out_unlock;
1108 * Wait for something to happen. This should probably
1109 * be interruptible (FIXME).
1111 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1112 if (cam->next_buf < 0) {
1113 cam_err(cam, "read() operation timed out\n");
1114 cafe_ctlr_stop_dma(cam);
1115 ret = -EIO;
1116 goto out_unlock;
1119 * Give them their data and we should be done.
1121 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1123 out_unlock:
1124 mutex_unlock(&cam->s_mutex);
1125 return ret;
1136 * Streaming I/O support.
1141 static int cafe_vidioc_streamon(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_IDLE || cam->n_sbufs == 0)
1151 goto out_unlock;
1153 cam->sequence = 0;
1154 ret = cafe_read_setup(cam, S_STREAMING);
1156 out_unlock:
1157 mutex_unlock(&cam->s_mutex);
1158 out:
1159 return ret;
1163 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1164 enum v4l2_buf_type type)
1166 struct cafe_camera *cam = filp->private_data;
1167 int ret = -EINVAL;
1169 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1170 goto out;
1171 mutex_lock(&cam->s_mutex);
1172 if (cam->state != S_STREAMING)
1173 goto out_unlock;
1175 cafe_ctlr_stop_dma(cam);
1176 ret = 0;
1178 out_unlock:
1179 mutex_unlock(&cam->s_mutex);
1180 out:
1181 return ret;
1186 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1188 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1190 INIT_LIST_HEAD(&buf->list);
1191 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1192 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1193 if (buf->buffer == NULL)
1194 return -ENOMEM;
1195 buf->mapcount = 0;
1196 buf->cam = cam;
1198 buf->v4lbuf.index = index;
1199 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1200 buf->v4lbuf.field = V4L2_FIELD_NONE;
1201 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1203 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
1204 * just uses the length times the index, but the spec warns
1205 * against doing just that - vma merging problems. So we
1206 * leave a gap between each pair of buffers.
1208 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1209 return 0;
1212 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1214 int i;
1217 * If any buffers are mapped, we cannot free them at all.
1219 for (i = 0; i < cam->n_sbufs; i++)
1220 if (cam->sb_bufs[i].mapcount > 0)
1221 return -EBUSY;
1223 * OK, let's do it.
1225 for (i = 0; i < cam->n_sbufs; i++)
1226 vfree(cam->sb_bufs[i].buffer);
1227 cam->n_sbufs = 0;
1228 kfree(cam->sb_bufs);
1229 cam->sb_bufs = NULL;
1230 INIT_LIST_HEAD(&cam->sb_avail);
1231 INIT_LIST_HEAD(&cam->sb_full);
1232 return 0;
1237 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1238 struct v4l2_requestbuffers *req)
1240 struct cafe_camera *cam = filp->private_data;
1241 int ret = 0; /* Silence warning */
1244 * Make sure it's something we can do. User pointers could be
1245 * implemented without great pain, but that's not been done yet.
1247 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1248 return -EINVAL;
1249 if (req->memory != V4L2_MEMORY_MMAP)
1250 return -EINVAL;
1252 * If they ask for zero buffers, they really want us to stop streaming
1253 * (if it's happening) and free everything. Should we check owner?
1255 mutex_lock(&cam->s_mutex);
1256 if (req->count == 0) {
1257 if (cam->state == S_STREAMING)
1258 cafe_ctlr_stop_dma(cam);
1259 ret = cafe_free_sio_buffers (cam);
1260 goto out;
1263 * Device needs to be idle and working. We *could* try to do the
1264 * right thing in S_SPECREAD by shutting things down, but it
1265 * probably doesn't matter.
1267 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1268 ret = -EBUSY;
1269 goto out;
1271 cam->owner = filp;
1273 if (req->count < min_buffers)
1274 req->count = min_buffers;
1275 else if (req->count > max_buffers)
1276 req->count = max_buffers;
1277 if (cam->n_sbufs > 0) {
1278 ret = cafe_free_sio_buffers(cam);
1279 if (ret)
1280 goto out;
1283 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1284 GFP_KERNEL);
1285 if (cam->sb_bufs == NULL) {
1286 ret = -ENOMEM;
1287 goto out;
1289 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1290 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1291 if (ret)
1292 break;
1295 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1296 kfree(cam->sb_bufs);
1297 req->count = cam->n_sbufs; /* In case of partial success */
1299 out:
1300 mutex_unlock(&cam->s_mutex);
1301 return ret;
1305 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1306 struct v4l2_buffer *buf)
1308 struct cafe_camera *cam = filp->private_data;
1309 int ret = -EINVAL;
1311 mutex_lock(&cam->s_mutex);
1312 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1313 goto out;
1314 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1315 goto out;
1316 *buf = cam->sb_bufs[buf->index].v4lbuf;
1317 ret = 0;
1318 out:
1319 mutex_unlock(&cam->s_mutex);
1320 return ret;
1323 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1324 struct v4l2_buffer *buf)
1326 struct cafe_camera *cam = filp->private_data;
1327 struct cafe_sio_buffer *sbuf;
1328 int ret = -EINVAL;
1329 unsigned long flags;
1331 mutex_lock(&cam->s_mutex);
1332 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1333 goto out;
1334 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1335 goto out;
1336 sbuf = cam->sb_bufs + buf->index;
1337 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1338 ret = 0; /* Already queued?? */
1339 goto out;
1341 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1342 /* Spec doesn't say anything, seems appropriate tho */
1343 ret = -EBUSY;
1344 goto out;
1346 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1347 spin_lock_irqsave(&cam->dev_lock, flags);
1348 list_add(&sbuf->list, &cam->sb_avail);
1349 spin_unlock_irqrestore(&cam->dev_lock, flags);
1350 ret = 0;
1351 out:
1352 mutex_unlock(&cam->s_mutex);
1353 return ret;
1356 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1357 struct v4l2_buffer *buf)
1359 struct cafe_camera *cam = filp->private_data;
1360 struct cafe_sio_buffer *sbuf;
1361 int ret = -EINVAL;
1362 unsigned long flags;
1364 mutex_lock(&cam->s_mutex);
1365 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1366 goto out_unlock;
1367 if (cam->state != S_STREAMING)
1368 goto out_unlock;
1369 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1370 ret = -EAGAIN;
1371 goto out_unlock;
1374 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1375 mutex_unlock(&cam->s_mutex);
1376 if (wait_event_interruptible(cam->iowait,
1377 !list_empty(&cam->sb_full))) {
1378 ret = -ERESTARTSYS;
1379 goto out;
1381 mutex_lock(&cam->s_mutex);
1384 if (cam->state != S_STREAMING)
1385 ret = -EINTR;
1386 else {
1387 spin_lock_irqsave(&cam->dev_lock, flags);
1388 /* Should probably recheck !list_empty() here */
1389 sbuf = list_entry(cam->sb_full.next,
1390 struct cafe_sio_buffer, list);
1391 list_del_init(&sbuf->list);
1392 spin_unlock_irqrestore(&cam->dev_lock, flags);
1393 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1394 *buf = sbuf->v4lbuf;
1395 ret = 0;
1398 out_unlock:
1399 mutex_unlock(&cam->s_mutex);
1400 out:
1401 return ret;
1406 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1408 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1410 * Locking: done under mmap_sem, so we don't need to
1411 * go back to the camera lock here.
1413 sbuf->mapcount++;
1417 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1419 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1421 mutex_lock(&sbuf->cam->s_mutex);
1422 sbuf->mapcount--;
1423 /* Docs say we should stop I/O too... */
1424 if (sbuf->mapcount == 0)
1425 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1426 mutex_unlock(&sbuf->cam->s_mutex);
1429 static struct vm_operations_struct cafe_v4l_vm_ops = {
1430 .open = cafe_v4l_vm_open,
1431 .close = cafe_v4l_vm_close
1435 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1437 struct cafe_camera *cam = filp->private_data;
1438 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1439 int ret = -EINVAL;
1440 int i;
1441 struct cafe_sio_buffer *sbuf = NULL;
1443 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1444 return -EINVAL;
1446 * Find the buffer they are looking for.
1448 mutex_lock(&cam->s_mutex);
1449 for (i = 0; i < cam->n_sbufs; i++)
1450 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1451 sbuf = cam->sb_bufs + i;
1452 break;
1454 if (sbuf == NULL)
1455 goto out;
1457 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1458 if (ret)
1459 goto out;
1460 vma->vm_flags |= VM_DONTEXPAND;
1461 vma->vm_private_data = sbuf;
1462 vma->vm_ops = &cafe_v4l_vm_ops;
1463 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1464 cafe_v4l_vm_open(vma);
1465 ret = 0;
1466 out:
1467 mutex_unlock(&cam->s_mutex);
1468 return ret;
1473 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1475 struct cafe_camera *cam;
1477 cam = cafe_find_dev(iminor(inode));
1478 if (cam == NULL)
1479 return -ENODEV;
1480 filp->private_data = cam;
1482 mutex_lock(&cam->s_mutex);
1483 if (cam->users == 0) {
1484 cafe_ctlr_power_up(cam);
1485 __cafe_cam_reset(cam);
1486 cafe_set_config_needed(cam, 1);
1487 /* FIXME make sure this is complete */
1489 (cam->users)++;
1490 mutex_unlock(&cam->s_mutex);
1491 return 0;
1495 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1497 struct cafe_camera *cam = filp->private_data;
1499 mutex_lock(&cam->s_mutex);
1500 (cam->users)--;
1501 if (filp == cam->owner) {
1502 cafe_ctlr_stop_dma(cam);
1503 cafe_free_sio_buffers(cam);
1504 cam->owner = NULL;
1506 if (cam->users == 0) {
1507 cafe_ctlr_power_down(cam);
1508 if (alloc_bufs_at_read)
1509 cafe_free_dma_bufs(cam);
1511 mutex_unlock(&cam->s_mutex);
1512 return 0;
1517 static unsigned int cafe_v4l_poll(struct file *filp,
1518 struct poll_table_struct *pt)
1520 struct cafe_camera *cam = filp->private_data;
1522 poll_wait(filp, &cam->iowait, pt);
1523 if (cam->next_buf >= 0)
1524 return POLLIN | POLLRDNORM;
1525 return 0;
1530 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1531 struct v4l2_queryctrl *qc)
1533 struct cafe_camera *cam = filp->private_data;
1534 int ret;
1536 mutex_lock(&cam->s_mutex);
1537 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1538 mutex_unlock(&cam->s_mutex);
1539 return ret;
1543 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1544 struct v4l2_control *ctrl)
1546 struct cafe_camera *cam = filp->private_data;
1547 int ret;
1549 mutex_lock(&cam->s_mutex);
1550 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1551 mutex_unlock(&cam->s_mutex);
1552 return ret;
1556 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1557 struct v4l2_control *ctrl)
1559 struct cafe_camera *cam = filp->private_data;
1560 int ret;
1562 mutex_lock(&cam->s_mutex);
1563 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1564 mutex_unlock(&cam->s_mutex);
1565 return ret;
1572 static int cafe_vidioc_querycap(struct file *file, void *priv,
1573 struct v4l2_capability *cap)
1575 strcpy(cap->driver, "cafe_ccic");
1576 strcpy(cap->card, "cafe_ccic");
1577 cap->version = CAFE_VERSION;
1578 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1579 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1580 return 0;
1585 * The default format we use until somebody says otherwise.
1587 static struct v4l2_pix_format cafe_def_pix_format = {
1588 .width = VGA_WIDTH,
1589 .height = VGA_HEIGHT,
1590 .pixelformat = V4L2_PIX_FMT_YUYV,
1591 .field = V4L2_FIELD_NONE,
1592 .bytesperline = VGA_WIDTH*2,
1593 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1596 static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1597 void *priv, struct v4l2_fmtdesc *fmt)
1599 struct cafe_camera *cam = priv;
1600 int ret;
1602 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1603 return -EINVAL;
1604 mutex_lock(&cam->s_mutex);
1605 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1606 mutex_unlock(&cam->s_mutex);
1607 return ret;
1611 static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1612 struct v4l2_format *fmt)
1614 struct cafe_camera *cam = priv;
1615 int ret;
1617 mutex_lock(&cam->s_mutex);
1618 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1619 mutex_unlock(&cam->s_mutex);
1620 return ret;
1623 static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1624 struct v4l2_format *fmt)
1626 struct cafe_camera *cam = priv;
1627 int ret;
1630 * Can't do anything if the device is not idle
1631 * Also can't if there are streaming buffers in place.
1633 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1634 return -EBUSY;
1636 * See if the formatting works in principle.
1638 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1639 if (ret)
1640 return ret;
1642 * Now we start to change things for real, so let's do it
1643 * under lock.
1645 mutex_lock(&cam->s_mutex);
1646 cam->pix_format = fmt->fmt.pix;
1648 * Make sure we have appropriate DMA buffers.
1650 ret = -ENOMEM;
1651 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1652 cafe_free_dma_bufs(cam);
1653 if (cam->nbufs == 0) {
1654 if (cafe_alloc_dma_bufs(cam, 0))
1655 goto out;
1658 * It looks like this might work, so let's program the sensor.
1660 ret = cafe_cam_configure(cam);
1661 if (! ret)
1662 ret = cafe_ctlr_configure(cam);
1663 out:
1664 mutex_unlock(&cam->s_mutex);
1665 return ret;
1669 * Return our stored notion of how the camera is/should be configured.
1670 * The V4l2 spec wants us to be smarter, and actually get this from
1671 * the camera (and not mess with it at open time). Someday.
1673 static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1674 struct v4l2_format *f)
1676 struct cafe_camera *cam = priv;
1678 f->fmt.pix = cam->pix_format;
1679 return 0;
1683 * We only have one input - the sensor - so minimize the nonsense here.
1685 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1686 struct v4l2_input *input)
1688 if (input->index != 0)
1689 return -EINVAL;
1691 input->type = V4L2_INPUT_TYPE_CAMERA;
1692 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1693 strcpy(input->name, "Camera");
1694 return 0;
1697 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1699 *i = 0;
1700 return 0;
1703 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1705 if (i != 0)
1706 return -EINVAL;
1707 return 0;
1710 /* from vivi.c */
1711 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1713 return 0;
1717 * G/S_PARM. Most of this is done by the sensor, but we are
1718 * the level which controls the number of read buffers.
1720 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1721 struct v4l2_streamparm *parms)
1723 struct cafe_camera *cam = priv;
1724 int ret;
1726 mutex_lock(&cam->s_mutex);
1727 ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1728 mutex_unlock(&cam->s_mutex);
1729 parms->parm.capture.readbuffers = n_dma_bufs;
1730 return ret;
1733 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1734 struct v4l2_streamparm *parms)
1736 struct cafe_camera *cam = priv;
1737 int ret;
1739 mutex_lock(&cam->s_mutex);
1740 ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1741 mutex_unlock(&cam->s_mutex);
1742 parms->parm.capture.readbuffers = n_dma_bufs;
1743 return ret;
1747 static void cafe_v4l_dev_release(struct video_device *vd)
1749 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1751 kfree(cam);
1756 * This template device holds all of those v4l2 methods; we
1757 * clone it for specific real devices.
1760 static const struct file_operations cafe_v4l_fops = {
1761 .owner = THIS_MODULE,
1762 .open = cafe_v4l_open,
1763 .release = cafe_v4l_release,
1764 .read = cafe_v4l_read,
1765 .poll = cafe_v4l_poll,
1766 .mmap = cafe_v4l_mmap,
1767 .ioctl = video_ioctl2,
1768 .llseek = no_llseek,
1771 static struct video_device cafe_v4l_template = {
1772 .name = "cafe",
1773 .type = VFL_TYPE_GRABBER,
1774 .type2 = VID_TYPE_CAPTURE,
1775 .minor = -1, /* Get one dynamically */
1776 .tvnorms = V4L2_STD_NTSC_M,
1777 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1779 .fops = &cafe_v4l_fops,
1780 .release = cafe_v4l_dev_release,
1782 .vidioc_querycap = cafe_vidioc_querycap,
1783 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1784 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1785 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1786 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1787 .vidioc_enum_input = cafe_vidioc_enum_input,
1788 .vidioc_g_input = cafe_vidioc_g_input,
1789 .vidioc_s_input = cafe_vidioc_s_input,
1790 .vidioc_s_std = cafe_vidioc_s_std,
1791 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1792 .vidioc_querybuf = cafe_vidioc_querybuf,
1793 .vidioc_qbuf = cafe_vidioc_qbuf,
1794 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1795 .vidioc_streamon = cafe_vidioc_streamon,
1796 .vidioc_streamoff = cafe_vidioc_streamoff,
1797 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1798 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1799 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1800 .vidioc_g_parm = cafe_vidioc_g_parm,
1801 .vidioc_s_parm = cafe_vidioc_s_parm,
1810 /* ---------------------------------------------------------------------- */
1812 * Interrupt handler stuff
1817 static void cafe_frame_tasklet(unsigned long data)
1819 struct cafe_camera *cam = (struct cafe_camera *) data;
1820 int i;
1821 unsigned long flags;
1822 struct cafe_sio_buffer *sbuf;
1824 spin_lock_irqsave(&cam->dev_lock, flags);
1825 for (i = 0; i < cam->nbufs; i++) {
1826 int bufno = cam->next_buf;
1827 if (bufno < 0) { /* "will never happen" */
1828 cam_err(cam, "No valid bufs in tasklet!\n");
1829 break;
1831 if (++(cam->next_buf) >= cam->nbufs)
1832 cam->next_buf = 0;
1833 if (! test_bit(bufno, &cam->flags))
1834 continue;
1835 if (list_empty(&cam->sb_avail))
1836 break; /* Leave it valid, hope for better later */
1837 clear_bit(bufno, &cam->flags);
1838 sbuf = list_entry(cam->sb_avail.next,
1839 struct cafe_sio_buffer, list);
1841 * Drop the lock during the big copy. This *should* be safe...
1843 spin_unlock_irqrestore(&cam->dev_lock, flags);
1844 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1845 cam->pix_format.sizeimage);
1846 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1847 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1848 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1849 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1850 spin_lock_irqsave(&cam->dev_lock, flags);
1851 list_move_tail(&sbuf->list, &cam->sb_full);
1853 if (! list_empty(&cam->sb_full))
1854 wake_up(&cam->iowait);
1855 spin_unlock_irqrestore(&cam->dev_lock, flags);
1860 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1863 * Basic frame housekeeping.
1865 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1866 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1867 set_bit(frame, &cam->flags);
1868 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1869 if (cam->next_buf < 0)
1870 cam->next_buf = frame;
1871 cam->buf_seq[frame] = ++(cam->sequence);
1873 switch (cam->state) {
1875 * If in single read mode, try going speculative.
1877 case S_SINGLEREAD:
1878 cam->state = S_SPECREAD;
1879 cam->specframes = 0;
1880 wake_up(&cam->iowait);
1881 break;
1884 * If we are already doing speculative reads, and nobody is
1885 * reading them, just stop.
1887 case S_SPECREAD:
1888 if (++(cam->specframes) >= cam->nbufs) {
1889 cafe_ctlr_stop(cam);
1890 cafe_ctlr_irq_disable(cam);
1891 cam->state = S_IDLE;
1893 wake_up(&cam->iowait);
1894 break;
1896 * For the streaming case, we defer the real work to the
1897 * camera tasklet.
1899 * FIXME: if the application is not consuming the buffers,
1900 * we should eventually put things on hold and restart in
1901 * vidioc_dqbuf().
1903 case S_STREAMING:
1904 tasklet_schedule(&cam->s_tasklet);
1905 break;
1907 default:
1908 cam_err(cam, "Frame interrupt in non-operational state\n");
1909 break;
1916 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1918 unsigned int frame;
1920 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1922 * Handle any frame completions. There really should
1923 * not be more than one of these, or we have fallen
1924 * far behind.
1926 for (frame = 0; frame < cam->nbufs; frame++)
1927 if (irqs & (IRQ_EOF0 << frame))
1928 cafe_frame_complete(cam, frame);
1930 * If a frame starts, note that we have DMA active. This
1931 * code assumes that we won't get multiple frame interrupts
1932 * at once; may want to rethink that.
1934 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1935 set_bit(CF_DMA_ACTIVE, &cam->flags);
1940 static irqreturn_t cafe_irq(int irq, void *data)
1942 struct cafe_camera *cam = data;
1943 unsigned int irqs;
1945 spin_lock(&cam->dev_lock);
1946 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1947 if ((irqs & ALLIRQS) == 0) {
1948 spin_unlock(&cam->dev_lock);
1949 return IRQ_NONE;
1951 if (irqs & FRAMEIRQS)
1952 cafe_frame_irq(cam, irqs);
1953 if (irqs & TWSIIRQS) {
1954 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1955 wake_up(&cam->smbus_wait);
1957 spin_unlock(&cam->dev_lock);
1958 return IRQ_HANDLED;
1962 /* -------------------------------------------------------------------------- */
1963 #ifdef CONFIG_VIDEO_ADV_DEBUG
1965 * Debugfs stuff.
1968 static char cafe_debug_buf[1024];
1969 static struct dentry *cafe_dfs_root;
1971 static void cafe_dfs_setup(void)
1973 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1974 if (IS_ERR(cafe_dfs_root)) {
1975 cafe_dfs_root = NULL; /* Never mind */
1976 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1980 static void cafe_dfs_shutdown(void)
1982 if (cafe_dfs_root)
1983 debugfs_remove(cafe_dfs_root);
1986 static int cafe_dfs_open(struct inode *inode, struct file *file)
1988 file->private_data = inode->i_private;
1989 return 0;
1992 static ssize_t cafe_dfs_read_regs(struct file *file,
1993 char __user *buf, size_t count, loff_t *ppos)
1995 struct cafe_camera *cam = file->private_data;
1996 char *s = cafe_debug_buf;
1997 int offset;
1999 for (offset = 0; offset < 0x44; offset += 4)
2000 s += sprintf(s, "%02x: %08x\n", offset,
2001 cafe_reg_read(cam, offset));
2002 for (offset = 0x88; offset <= 0x90; offset += 4)
2003 s += sprintf(s, "%02x: %08x\n", offset,
2004 cafe_reg_read(cam, offset));
2005 for (offset = 0xb4; offset <= 0xbc; offset += 4)
2006 s += sprintf(s, "%02x: %08x\n", offset,
2007 cafe_reg_read(cam, offset));
2008 for (offset = 0x3000; offset <= 0x300c; offset += 4)
2009 s += sprintf(s, "%04x: %08x\n", offset,
2010 cafe_reg_read(cam, offset));
2011 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2012 s - cafe_debug_buf);
2015 static const struct file_operations cafe_dfs_reg_ops = {
2016 .owner = THIS_MODULE,
2017 .read = cafe_dfs_read_regs,
2018 .open = cafe_dfs_open
2021 static ssize_t cafe_dfs_read_cam(struct file *file,
2022 char __user *buf, size_t count, loff_t *ppos)
2024 struct cafe_camera *cam = file->private_data;
2025 char *s = cafe_debug_buf;
2026 int offset;
2028 if (! cam->sensor)
2029 return -EINVAL;
2030 for (offset = 0x0; offset < 0x8a; offset++)
2032 u8 v;
2034 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2035 s += sprintf(s, "%02x: %02x\n", offset, v);
2037 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2038 s - cafe_debug_buf);
2041 static const struct file_operations cafe_dfs_cam_ops = {
2042 .owner = THIS_MODULE,
2043 .read = cafe_dfs_read_cam,
2044 .open = cafe_dfs_open
2049 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2051 char fname[40];
2053 if (!cafe_dfs_root)
2054 return;
2055 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2056 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2057 cam, &cafe_dfs_reg_ops);
2058 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2059 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2060 cam, &cafe_dfs_cam_ops);
2064 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2066 if (! IS_ERR(cam->dfs_regs))
2067 debugfs_remove(cam->dfs_regs);
2068 if (! IS_ERR(cam->dfs_cam_regs))
2069 debugfs_remove(cam->dfs_cam_regs);
2072 #else
2074 #define cafe_dfs_setup()
2075 #define cafe_dfs_shutdown()
2076 #define cafe_dfs_cam_setup(cam)
2077 #define cafe_dfs_cam_shutdown(cam)
2078 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2083 /* ------------------------------------------------------------------------*/
2085 * PCI interface stuff.
2088 static int cafe_pci_probe(struct pci_dev *pdev,
2089 const struct pci_device_id *id)
2091 int ret;
2092 u16 classword;
2093 struct cafe_camera *cam;
2095 * Make sure we have a camera here - we'll get calls for
2096 * the other cafe devices as well.
2098 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2099 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2100 return -ENODEV;
2102 * Start putting together one of our big camera structures.
2104 ret = -ENOMEM;
2105 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2106 if (cam == NULL)
2107 goto out;
2108 mutex_init(&cam->s_mutex);
2109 mutex_lock(&cam->s_mutex);
2110 spin_lock_init(&cam->dev_lock);
2111 cam->state = S_NOTREADY;
2112 cafe_set_config_needed(cam, 1);
2113 init_waitqueue_head(&cam->smbus_wait);
2114 init_waitqueue_head(&cam->iowait);
2115 cam->pdev = pdev;
2116 cam->pix_format = cafe_def_pix_format;
2117 INIT_LIST_HEAD(&cam->dev_list);
2118 INIT_LIST_HEAD(&cam->sb_avail);
2119 INIT_LIST_HEAD(&cam->sb_full);
2120 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2122 * Get set up on the PCI bus.
2124 ret = pci_enable_device(pdev);
2125 if (ret)
2126 goto out_free;
2127 pci_set_master(pdev);
2129 ret = -EIO;
2130 cam->regs = pci_iomap(pdev, 0, 0);
2131 if (! cam->regs) {
2132 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2133 goto out_free;
2135 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2136 if (ret)
2137 goto out_iounmap;
2139 * Initialize the controller and leave it powered up. It will
2140 * stay that way until the sensor driver shows up.
2142 cafe_ctlr_init(cam);
2143 cafe_ctlr_power_up(cam);
2145 * Set up I2C/SMBUS communications. We have to drop the mutex here
2146 * because the sensor could attach in this call chain, leading to
2147 * unsightly deadlocks.
2149 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2150 ret = cafe_smbus_setup(cam);
2151 if (ret)
2152 goto out_freeirq;
2154 * Get the v4l2 setup done.
2156 mutex_lock(&cam->s_mutex);
2157 cam->v4ldev = cafe_v4l_template;
2158 cam->v4ldev.debug = 0;
2159 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2160 cam->v4ldev.dev = &pdev->dev;
2161 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2162 if (ret)
2163 goto out_smbus;
2165 * If so requested, try to get our DMA buffers now.
2167 if (!alloc_bufs_at_read) {
2168 if (cafe_alloc_dma_bufs(cam, 1))
2169 cam_warn(cam, "Unable to alloc DMA buffers at load"
2170 " will try again later.");
2173 cafe_dfs_cam_setup(cam);
2174 mutex_unlock(&cam->s_mutex);
2175 cafe_add_dev(cam);
2176 return 0;
2178 out_smbus:
2179 cafe_smbus_shutdown(cam);
2180 out_freeirq:
2181 cafe_ctlr_power_down(cam);
2182 free_irq(pdev->irq, cam);
2183 out_iounmap:
2184 pci_iounmap(pdev, cam->regs);
2185 out_free:
2186 kfree(cam);
2187 out:
2188 return ret;
2193 * Shut down an initialized device
2195 static void cafe_shutdown(struct cafe_camera *cam)
2197 /* FIXME: Make sure we take care of everything here */
2198 cafe_dfs_cam_shutdown(cam);
2199 if (cam->n_sbufs > 0)
2200 /* What if they are still mapped? Shouldn't be, but... */
2201 cafe_free_sio_buffers(cam);
2202 cafe_remove_dev(cam);
2203 cafe_ctlr_stop_dma(cam);
2204 cafe_ctlr_power_down(cam);
2205 cafe_smbus_shutdown(cam);
2206 cafe_free_dma_bufs(cam);
2207 free_irq(cam->pdev->irq, cam);
2208 pci_iounmap(cam->pdev, cam->regs);
2209 video_unregister_device(&cam->v4ldev);
2210 /* kfree(cam); done in v4l_release () */
2214 static void cafe_pci_remove(struct pci_dev *pdev)
2216 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2218 if (cam == NULL) {
2219 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2220 return;
2222 mutex_lock(&cam->s_mutex);
2223 if (cam->users > 0)
2224 cam_warn(cam, "Removing a device with users!\n");
2225 cafe_shutdown(cam);
2226 /* No unlock - it no longer exists */
2230 #ifdef CONFIG_PM
2232 * Basic power management.
2234 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2236 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2237 int ret;
2238 enum cafe_state cstate;
2240 ret = pci_save_state(pdev);
2241 if (ret)
2242 return ret;
2243 cstate = cam->state; /* HACK - stop_dma sets to idle */
2244 cafe_ctlr_stop_dma(cam);
2245 cafe_ctlr_power_down(cam);
2246 pci_disable_device(pdev);
2247 cam->state = cstate;
2248 return 0;
2252 static int cafe_pci_resume(struct pci_dev *pdev)
2254 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2255 int ret = 0;
2257 ret = pci_restore_state(pdev);
2258 if (ret)
2259 return ret;
2260 ret = pci_enable_device(pdev);
2262 if (ret) {
2263 cam_warn(cam, "Unable to re-enable device on resume!\n");
2264 return ret;
2266 cafe_ctlr_init(cam);
2267 cafe_ctlr_power_down(cam);
2269 mutex_lock(&cam->s_mutex);
2270 if (cam->users > 0) {
2271 cafe_ctlr_power_up(cam);
2272 __cafe_cam_reset(cam);
2274 mutex_unlock(&cam->s_mutex);
2276 set_bit(CF_CONFIG_NEEDED, &cam->flags);
2277 if (cam->state == S_SPECREAD)
2278 cam->state = S_IDLE; /* Don't bother restarting */
2279 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2280 ret = cafe_read_setup(cam, cam->state);
2281 return ret;
2284 #endif /* CONFIG_PM */
2287 static struct pci_device_id cafe_ids[] = {
2288 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2289 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2290 { 0, }
2293 MODULE_DEVICE_TABLE(pci, cafe_ids);
2295 static struct pci_driver cafe_pci_driver = {
2296 .name = "cafe1000-ccic",
2297 .id_table = cafe_ids,
2298 .probe = cafe_pci_probe,
2299 .remove = cafe_pci_remove,
2300 #ifdef CONFIG_PM
2301 .suspend = cafe_pci_suspend,
2302 .resume = cafe_pci_resume,
2303 #endif
2309 static int __init cafe_init(void)
2311 int ret;
2313 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2314 CAFE_VERSION);
2315 cafe_dfs_setup();
2316 ret = pci_register_driver(&cafe_pci_driver);
2317 if (ret) {
2318 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2319 goto out;
2321 request_module("ov7670"); /* FIXME want something more general */
2322 ret = 0;
2324 out:
2325 return ret;
2329 static void __exit cafe_exit(void)
2331 pci_unregister_driver(&cafe_pci_driver);
2332 cafe_dfs_shutdown();
2335 module_init(cafe_init);
2336 module_exit(cafe_exit);