2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
6 * Copyright 2006 One Laptop Per Child Association, Inc.
7 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
9 * Written by Jonathan Corbet, corbet@lwn.net.
11 * This file may be distributed under the terms of the GNU General
12 * Public License, version 2.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
20 #include <linux/pci.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-chip-ident.h>
27 #include <linux/device.h>
28 #include <linux/wait.h>
29 #include <linux/list.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/delay.h>
32 #include <linux/debugfs.h>
33 #include <linux/jiffies.h>
34 #include <linux/vmalloc.h>
36 #include <asm/uaccess.h>
39 #include "cafe_ccic-regs.h"
41 #define CAFE_VERSION 0x000002
47 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
48 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
49 MODULE_LICENSE("GPL");
50 MODULE_SUPPORTED_DEVICE("Video");
53 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
54 * we must have physically contiguous buffers to bring frames into.
55 * These parameters control how many buffers we use, whether we
56 * allocate them at load time (better chance of success, but nails down
57 * memory) or when somebody tries to use the camera (riskier), and,
58 * for load-time allocation, how big they should be.
60 * The controller can cycle through three buffers. We could use
61 * more by flipping pointers around, but it probably makes little
65 #define MAX_DMA_BUFS 3
66 static int alloc_bufs_at_load
= 0;
67 module_param(alloc_bufs_at_load
, bool, 0444);
68 MODULE_PARM_DESC(alloc_bufs_at_load
,
69 "Non-zero value causes DMA buffers to be allocated at module "
70 "load time. This increases the chances of successfully getting "
71 "those buffers, but at the cost of nailing down the memory from "
74 static int n_dma_bufs
= 3;
75 module_param(n_dma_bufs
, uint
, 0644);
76 MODULE_PARM_DESC(n_dma_bufs
,
77 "The number of DMA buffers to allocate. Can be either two "
78 "(saves memory, makes timing tighter) or three.");
80 static int dma_buf_size
= VGA_WIDTH
* VGA_HEIGHT
* 2; /* Worst case */
81 module_param(dma_buf_size
, uint
, 0444);
82 MODULE_PARM_DESC(dma_buf_size
,
83 "The size of the allocated DMA buffers. If actual operating "
84 "parameters require larger buffers, an attempt to reallocate "
87 static int min_buffers
= 1;
88 module_param(min_buffers
, uint
, 0644);
89 MODULE_PARM_DESC(min_buffers
,
90 "The minimum number of streaming I/O buffers we are willing "
93 static int max_buffers
= 10;
94 module_param(max_buffers
, uint
, 0644);
95 MODULE_PARM_DESC(max_buffers
,
96 "The maximum number of streaming I/O buffers an application "
97 "will be allowed to allocate. These buffers are big and live "
101 module_param(flip
, bool, 0444);
102 MODULE_PARM_DESC(flip
,
103 "If set, the sensor will be instructed to flip the image "
108 S_NOTREADY
, /* Not yet initialized */
109 S_IDLE
, /* Just hanging around */
110 S_FLAKED
, /* Some sort of problem */
111 S_SINGLEREAD
, /* In read() */
112 S_SPECREAD
, /* Speculative read (for future read()) */
113 S_STREAMING
/* Streaming data */
117 * Tracking of streaming I/O buffers.
119 struct cafe_sio_buffer
{
120 struct list_head list
;
121 struct v4l2_buffer v4lbuf
;
122 char *buffer
; /* Where it lives in kernel space */
124 struct cafe_camera
*cam
;
128 * A description of one of our devices.
129 * Locking: controlled by s_mutex. Certain fields, however, require
130 * the dev_lock spinlock; they are marked as such by comments.
131 * dev_lock is also required for access to device registers.
135 enum cafe_state state
;
136 unsigned long flags
; /* Buffer status, mainly (dev_lock) */
137 int users
; /* How many open FDs */
138 struct file
*owner
; /* Who has data access (v4l2) */
141 * Subsystem structures.
143 struct pci_dev
*pdev
;
144 struct video_device v4ldev
;
145 struct i2c_adapter i2c_adapter
;
146 struct i2c_client
*sensor
;
148 unsigned char __iomem
*regs
;
149 struct list_head dev_list
; /* link to other devices */
152 unsigned int nbufs
; /* How many are alloc'd */
153 int next_buf
; /* Next to consume (dev_lock) */
154 unsigned int dma_buf_size
; /* allocated size */
155 void *dma_bufs
[MAX_DMA_BUFS
]; /* Internal buffer addresses */
156 dma_addr_t dma_handles
[MAX_DMA_BUFS
]; /* Buffer bus addresses */
157 unsigned int specframes
; /* Unconsumed spec frames (dev_lock) */
158 unsigned int sequence
; /* Frame sequence number */
159 unsigned int buf_seq
[MAX_DMA_BUFS
]; /* Sequence for individual buffers */
161 /* Streaming buffers */
162 unsigned int n_sbufs
; /* How many we have */
163 struct cafe_sio_buffer
*sb_bufs
; /* The array of housekeeping structs */
164 struct list_head sb_avail
; /* Available for data (we own) (dev_lock) */
165 struct list_head sb_full
; /* With data (user space owns) (dev_lock) */
166 struct tasklet_struct s_tasklet
;
168 /* Current operating parameters */
169 u32 sensor_type
; /* Currently ov7670 only */
170 struct v4l2_pix_format pix_format
;
173 struct mutex s_mutex
; /* Access to this structure */
174 spinlock_t dev_lock
; /* Access to device */
177 wait_queue_head_t smbus_wait
; /* Waiting on i2c events */
178 wait_queue_head_t iowait
; /* Waiting on frame data */
179 #ifdef CONFIG_VIDEO_ADV_DEBUG
180 struct dentry
*dfs_regs
;
181 struct dentry
*dfs_cam_regs
;
186 * Status flags. Always manipulated with bit operations.
188 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
189 #define CF_BUF1_VALID 1
190 #define CF_BUF2_VALID 2
191 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
192 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
197 * Start over with DMA buffers - dev_lock needed.
199 static void cafe_reset_buffers(struct cafe_camera
*cam
)
204 for (i
= 0; i
< cam
->nbufs
; i
++)
205 clear_bit(i
, &cam
->flags
);
209 static inline int cafe_needs_config(struct cafe_camera
*cam
)
211 return test_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
214 static void cafe_set_config_needed(struct cafe_camera
*cam
, int needed
)
217 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
219 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
226 * Debugging and related.
228 #define cam_err(cam, fmt, arg...) \
229 dev_err(&(cam)->pdev->dev, fmt, ##arg);
230 #define cam_warn(cam, fmt, arg...) \
231 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
232 #define cam_dbg(cam, fmt, arg...) \
233 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
236 /* ---------------------------------------------------------------------*/
238 * We keep a simple list of known devices to search at open time.
240 static LIST_HEAD(cafe_dev_list
);
241 static DEFINE_MUTEX(cafe_dev_list_lock
);
243 static void cafe_add_dev(struct cafe_camera
*cam
)
245 mutex_lock(&cafe_dev_list_lock
);
246 list_add_tail(&cam
->dev_list
, &cafe_dev_list
);
247 mutex_unlock(&cafe_dev_list_lock
);
250 static void cafe_remove_dev(struct cafe_camera
*cam
)
252 mutex_lock(&cafe_dev_list_lock
);
253 list_del(&cam
->dev_list
);
254 mutex_unlock(&cafe_dev_list_lock
);
257 static struct cafe_camera
*cafe_find_dev(int minor
)
259 struct cafe_camera
*cam
;
261 mutex_lock(&cafe_dev_list_lock
);
262 list_for_each_entry(cam
, &cafe_dev_list
, dev_list
) {
263 if (cam
->v4ldev
.minor
== minor
)
268 mutex_unlock(&cafe_dev_list_lock
);
273 static struct cafe_camera
*cafe_find_by_pdev(struct pci_dev
*pdev
)
275 struct cafe_camera
*cam
;
277 mutex_lock(&cafe_dev_list_lock
);
278 list_for_each_entry(cam
, &cafe_dev_list
, dev_list
) {
279 if (cam
->pdev
== pdev
)
284 mutex_unlock(&cafe_dev_list_lock
);
289 /* ------------------------------------------------------------------------ */
291 * Device register I/O
293 static inline void cafe_reg_write(struct cafe_camera
*cam
, unsigned int reg
,
296 iowrite32(val
, cam
->regs
+ reg
);
299 static inline unsigned int cafe_reg_read(struct cafe_camera
*cam
,
302 return ioread32(cam
->regs
+ reg
);
306 static inline void cafe_reg_write_mask(struct cafe_camera
*cam
, unsigned int reg
,
307 unsigned int val
, unsigned int mask
)
309 unsigned int v
= cafe_reg_read(cam
, reg
);
311 v
= (v
& ~mask
) | (val
& mask
);
312 cafe_reg_write(cam
, reg
, v
);
315 static inline void cafe_reg_clear_bit(struct cafe_camera
*cam
,
316 unsigned int reg
, unsigned int val
)
318 cafe_reg_write_mask(cam
, reg
, 0, val
);
321 static inline void cafe_reg_set_bit(struct cafe_camera
*cam
,
322 unsigned int reg
, unsigned int val
)
324 cafe_reg_write_mask(cam
, reg
, val
, val
);
329 /* -------------------------------------------------------------------- */
331 * The I2C/SMBUS interface to the camera itself starts here. The
332 * controller handles SMBUS itself, presenting a relatively simple register
333 * interface; all we have to do is to tell it where to route the data.
335 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
337 static int cafe_smbus_write_done(struct cafe_camera
*cam
)
343 * We must delay after the interrupt, or the controller gets confused
344 * and never does give us good status. Fortunately, we don't do this
348 spin_lock_irqsave(&cam
->dev_lock
, flags
);
349 c1
= cafe_reg_read(cam
, REG_TWSIC1
);
350 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
351 return (c1
& (TWSIC1_WSTAT
|TWSIC1_ERROR
)) != TWSIC1_WSTAT
;
354 static int cafe_smbus_write_data(struct cafe_camera
*cam
,
355 u16 addr
, u8 command
, u8 value
)
360 spin_lock_irqsave(&cam
->dev_lock
, flags
);
361 rval
= TWSIC0_EN
| ((addr
<< TWSIC0_SID_SHIFT
) & TWSIC0_SID
);
362 rval
|= TWSIC0_OVMAGIC
; /* Make OV sensors work */
364 * Marvell sez set clkdiv to all 1's for now.
366 rval
|= TWSIC0_CLKDIV
;
367 cafe_reg_write(cam
, REG_TWSIC0
, rval
);
368 (void) cafe_reg_read(cam
, REG_TWSIC1
); /* force write */
369 rval
= value
| ((command
<< TWSIC1_ADDR_SHIFT
) & TWSIC1_ADDR
);
370 cafe_reg_write(cam
, REG_TWSIC1
, rval
);
371 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
372 msleep(2); /* Required or things flake */
374 wait_event_timeout(cam
->smbus_wait
, cafe_smbus_write_done(cam
),
376 spin_lock_irqsave(&cam
->dev_lock
, flags
);
377 rval
= cafe_reg_read(cam
, REG_TWSIC1
);
378 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
380 if (rval
& TWSIC1_WSTAT
) {
381 cam_err(cam
, "SMBUS write (%02x/%02x/%02x) timed out\n", addr
,
385 if (rval
& TWSIC1_ERROR
) {
386 cam_err(cam
, "SMBUS write (%02x/%02x/%02x) error\n", addr
,
395 static int cafe_smbus_read_done(struct cafe_camera
*cam
)
401 * We must delay after the interrupt, or the controller gets confused
402 * and never does give us good status. Fortunately, we don't do this
406 spin_lock_irqsave(&cam
->dev_lock
, flags
);
407 c1
= cafe_reg_read(cam
, REG_TWSIC1
);
408 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
409 return c1
& (TWSIC1_RVALID
|TWSIC1_ERROR
);
414 static int cafe_smbus_read_data(struct cafe_camera
*cam
,
415 u16 addr
, u8 command
, u8
*value
)
420 spin_lock_irqsave(&cam
->dev_lock
, flags
);
421 rval
= TWSIC0_EN
| ((addr
<< TWSIC0_SID_SHIFT
) & TWSIC0_SID
);
422 rval
|= TWSIC0_OVMAGIC
; /* Make OV sensors work */
424 * Marvel sez set clkdiv to all 1's for now.
426 rval
|= TWSIC0_CLKDIV
;
427 cafe_reg_write(cam
, REG_TWSIC0
, rval
);
428 (void) cafe_reg_read(cam
, REG_TWSIC1
); /* force write */
429 rval
= TWSIC1_READ
| ((command
<< TWSIC1_ADDR_SHIFT
) & TWSIC1_ADDR
);
430 cafe_reg_write(cam
, REG_TWSIC1
, rval
);
431 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
433 wait_event_timeout(cam
->smbus_wait
,
434 cafe_smbus_read_done(cam
), CAFE_SMBUS_TIMEOUT
);
435 spin_lock_irqsave(&cam
->dev_lock
, flags
);
436 rval
= cafe_reg_read(cam
, REG_TWSIC1
);
437 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
439 if (rval
& TWSIC1_ERROR
) {
440 cam_err(cam
, "SMBUS read (%02x/%02x) error\n", addr
, command
);
443 if (! (rval
& TWSIC1_RVALID
)) {
444 cam_err(cam
, "SMBUS read (%02x/%02x) timed out\n", addr
,
448 *value
= rval
& 0xff;
453 * Perform a transfer over SMBUS. This thing is called under
454 * the i2c bus lock, so we shouldn't race with ourselves...
456 static int cafe_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
457 unsigned short flags
, char rw
, u8 command
,
458 int size
, union i2c_smbus_data
*data
)
460 struct cafe_camera
*cam
= i2c_get_adapdata(adapter
);
464 * Refuse to talk to anything but OV cam chips. We should
465 * never even see an attempt to do so, but one never knows.
467 if (cam
->sensor
&& addr
!= cam
->sensor
->addr
) {
468 cam_err(cam
, "funky smbus addr %d\n", addr
);
472 * This interface would appear to only do byte data ops. OK
473 * it can do word too, but the cam chip has no use for that.
475 if (size
!= I2C_SMBUS_BYTE_DATA
) {
476 cam_err(cam
, "funky xfer size %d\n", size
);
480 if (rw
== I2C_SMBUS_WRITE
)
481 ret
= cafe_smbus_write_data(cam
, addr
, command
, data
->byte
);
482 else if (rw
== I2C_SMBUS_READ
)
483 ret
= cafe_smbus_read_data(cam
, addr
, command
, &data
->byte
);
488 static void cafe_smbus_enable_irq(struct cafe_camera
*cam
)
492 spin_lock_irqsave(&cam
->dev_lock
, flags
);
493 cafe_reg_set_bit(cam
, REG_IRQMASK
, TWSIIRQS
);
494 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
497 static u32
cafe_smbus_func(struct i2c_adapter
*adapter
)
499 return I2C_FUNC_SMBUS_READ_BYTE_DATA
|
500 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
;
503 static struct i2c_algorithm cafe_smbus_algo
= {
504 .smbus_xfer
= cafe_smbus_xfer
,
505 .functionality
= cafe_smbus_func
508 /* Somebody is on the bus */
509 static int cafe_cam_init(struct cafe_camera
*cam
);
510 static void cafe_ctlr_stop_dma(struct cafe_camera
*cam
);
511 static void cafe_ctlr_power_down(struct cafe_camera
*cam
);
513 static int cafe_smbus_attach(struct i2c_client
*client
)
515 struct cafe_camera
*cam
= i2c_get_adapdata(client
->adapter
);
518 * Don't talk to chips we don't recognize.
520 if (client
->driver
->id
== I2C_DRIVERID_OV7670
) {
521 cam
->sensor
= client
;
522 return cafe_cam_init(cam
);
527 static int cafe_smbus_detach(struct i2c_client
*client
)
529 struct cafe_camera
*cam
= i2c_get_adapdata(client
->adapter
);
531 if (cam
->sensor
== client
) {
532 cafe_ctlr_stop_dma(cam
);
533 cafe_ctlr_power_down(cam
);
534 cam_err(cam
, "lost the sensor!\n");
535 cam
->sensor
= NULL
; /* Bummer, no camera */
536 cam
->state
= S_NOTREADY
;
541 static int cafe_smbus_setup(struct cafe_camera
*cam
)
543 struct i2c_adapter
*adap
= &cam
->i2c_adapter
;
546 cafe_smbus_enable_irq(cam
);
547 adap
->id
= I2C_HW_SMBUS_CAFE
;
548 adap
->class = I2C_CLASS_CAM_DIGITAL
;
549 adap
->owner
= THIS_MODULE
;
550 adap
->client_register
= cafe_smbus_attach
;
551 adap
->client_unregister
= cafe_smbus_detach
;
552 adap
->algo
= &cafe_smbus_algo
;
553 strcpy(adap
->name
, "cafe_ccic");
554 adap
->dev
.parent
= &cam
->pdev
->dev
;
555 i2c_set_adapdata(adap
, cam
);
556 ret
= i2c_add_adapter(adap
);
558 printk(KERN_ERR
"Unable to register cafe i2c adapter\n");
562 static void cafe_smbus_shutdown(struct cafe_camera
*cam
)
564 i2c_del_adapter(&cam
->i2c_adapter
);
568 /* ------------------------------------------------------------------- */
570 * Deal with the controller.
574 * Do everything we think we need to have the interface operating
575 * according to the desired format.
577 static void cafe_ctlr_dma(struct cafe_camera
*cam
)
580 * Store the first two Y buffers (we aren't supporting
581 * planar formats for now, so no UV bufs). Then either
582 * set the third if it exists, or tell the controller
585 cafe_reg_write(cam
, REG_Y0BAR
, cam
->dma_handles
[0]);
586 cafe_reg_write(cam
, REG_Y1BAR
, cam
->dma_handles
[1]);
587 if (cam
->nbufs
> 2) {
588 cafe_reg_write(cam
, REG_Y2BAR
, cam
->dma_handles
[2]);
589 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
592 cafe_reg_set_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
593 cafe_reg_write(cam
, REG_UBAR
, 0); /* 32 bits only for now */
596 static void cafe_ctlr_image(struct cafe_camera
*cam
)
599 struct v4l2_pix_format
*fmt
= &cam
->pix_format
;
601 imgsz
= ((fmt
->height
<< IMGSZ_V_SHIFT
) & IMGSZ_V_MASK
) |
602 (fmt
->bytesperline
& IMGSZ_H_MASK
);
603 cafe_reg_write(cam
, REG_IMGSIZE
, imgsz
);
604 cafe_reg_write(cam
, REG_IMGOFFSET
, 0);
605 /* YPITCH just drops the last two bits */
606 cafe_reg_write_mask(cam
, REG_IMGPITCH
, fmt
->bytesperline
,
609 * Tell the controller about the image format we are using.
611 switch (cam
->pix_format
.pixelformat
) {
612 case V4L2_PIX_FMT_YUYV
:
613 cafe_reg_write_mask(cam
, REG_CTRL0
,
614 C0_DF_YUV
|C0_YUV_PACKED
|C0_YUVE_YUYV
,
618 case V4L2_PIX_FMT_RGB444
:
619 cafe_reg_write_mask(cam
, REG_CTRL0
,
620 C0_DF_RGB
|C0_RGBF_444
|C0_RGB4_XRGB
,
625 case V4L2_PIX_FMT_RGB565
:
626 cafe_reg_write_mask(cam
, REG_CTRL0
,
627 C0_DF_RGB
|C0_RGBF_565
|C0_RGB5_BGGR
,
632 cam_err(cam
, "Unknown format %x\n", cam
->pix_format
.pixelformat
);
636 * Make sure it knows we want to use hsync/vsync.
638 cafe_reg_write_mask(cam
, REG_CTRL0
, C0_SIF_HVSYNC
,
644 * Configure the controller for operation; caller holds the
647 static int cafe_ctlr_configure(struct cafe_camera
*cam
)
651 spin_lock_irqsave(&cam
->dev_lock
, flags
);
653 cafe_ctlr_image(cam
);
654 cafe_set_config_needed(cam
, 0);
655 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
659 static void cafe_ctlr_irq_enable(struct cafe_camera
*cam
)
662 * Clear any pending interrupts, since we do not
663 * expect to have I/O active prior to enabling.
665 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
);
666 cafe_reg_set_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
669 static void cafe_ctlr_irq_disable(struct cafe_camera
*cam
)
671 cafe_reg_clear_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
675 * Make the controller start grabbing images. Everything must
676 * be set up before doing this.
678 static void cafe_ctlr_start(struct cafe_camera
*cam
)
680 /* set_bit performs a read, so no other barrier should be
682 cafe_reg_set_bit(cam
, REG_CTRL0
, C0_ENABLE
);
685 static void cafe_ctlr_stop(struct cafe_camera
*cam
)
687 cafe_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
690 static void cafe_ctlr_init(struct cafe_camera
*cam
)
694 spin_lock_irqsave(&cam
->dev_lock
, flags
);
696 * Added magic to bring up the hardware on the B-Test board
698 cafe_reg_write(cam
, 0x3038, 0x8);
699 cafe_reg_write(cam
, 0x315c, 0x80008);
701 * Go through the dance needed to wake the device up.
702 * Note that these registers are global and shared
703 * with the NAND and SD devices. Interaction between the
704 * three still needs to be examined.
706 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRS
|GCSR_MRS
); /* Needed? */
707 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRC
|GCSR_MRC
);
708 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRC
|GCSR_MRS
);
710 * Here we must wait a bit for the controller to come around.
712 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
713 mdelay(5); /* FIXME revisit this */
714 spin_lock_irqsave(&cam
->dev_lock
, flags
);
716 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_CCIC_EN
|GCSR_SRC
|GCSR_MRC
);
717 cafe_reg_set_bit(cam
, REG_GL_IMASK
, GIMSK_CCIC_EN
);
719 * Make sure it's not powered down.
721 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
723 * Turn off the enable bit. It sure should be off anyway,
724 * but it's good to be sure.
726 cafe_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
728 * Mask all interrupts.
730 cafe_reg_write(cam
, REG_IRQMASK
, 0);
732 * Clock the sensor appropriately. Controller clock should
733 * be 48MHz, sensor "typical" value is half that.
735 cafe_reg_write_mask(cam
, REG_CLKCTRL
, 2, CLK_DIV_MASK
);
736 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
741 * Stop the controller, and don't return until we're really sure that no
742 * further DMA is going on.
744 static void cafe_ctlr_stop_dma(struct cafe_camera
*cam
)
749 * Theory: stop the camera controller (whether it is operating
750 * or not). Delay briefly just in case we race with the SOF
751 * interrupt, then wait until no DMA is active.
753 spin_lock_irqsave(&cam
->dev_lock
, flags
);
755 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
757 wait_event_timeout(cam
->iowait
,
758 !test_bit(CF_DMA_ACTIVE
, &cam
->flags
), HZ
);
759 if (test_bit(CF_DMA_ACTIVE
, &cam
->flags
))
760 cam_err(cam
, "Timeout waiting for DMA to end\n");
761 /* This would be bad news - what now? */
762 spin_lock_irqsave(&cam
->dev_lock
, flags
);
764 cafe_ctlr_irq_disable(cam
);
765 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
771 static void cafe_ctlr_power_up(struct cafe_camera
*cam
)
775 spin_lock_irqsave(&cam
->dev_lock
, flags
);
776 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
778 * Put the sensor into operational mode (assumes OLPC-style
779 * wiring). Control 0 is reset - set to 1 to operate.
780 * Control 1 is power down, set to 0 to operate.
782 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
); /* pwr up, reset */
783 // mdelay(1); /* Marvell says 1ms will do it */
784 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
|GPR_C0
);
785 // mdelay(1); /* Enough? */
786 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
789 static void cafe_ctlr_power_down(struct cafe_camera
*cam
)
793 spin_lock_irqsave(&cam
->dev_lock
, flags
);
794 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
|GPR_C1
);
795 cafe_reg_set_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
796 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
799 /* -------------------------------------------------------------------- */
801 * Communications with the sensor.
804 static int __cafe_cam_cmd(struct cafe_camera
*cam
, int cmd
, void *arg
)
806 struct i2c_client
*sc
= cam
->sensor
;
809 if (sc
== NULL
|| sc
->driver
== NULL
|| sc
->driver
->command
== NULL
)
811 ret
= sc
->driver
->command(sc
, cmd
, arg
);
812 if (ret
== -EPERM
) /* Unsupported command */
817 static int __cafe_cam_reset(struct cafe_camera
*cam
)
820 return __cafe_cam_cmd(cam
, VIDIOC_INT_RESET
, &zero
);
824 * We have found the sensor on the i2c. Let's try to have a
827 static int cafe_cam_init(struct cafe_camera
*cam
)
829 struct v4l2_chip_ident chip
= { V4L2_CHIP_MATCH_I2C_ADDR
, 0, 0, 0 };
832 mutex_lock(&cam
->s_mutex
);
833 if (cam
->state
!= S_NOTREADY
)
834 cam_warn(cam
, "Cam init with device in funky state %d",
836 ret
= __cafe_cam_reset(cam
);
839 chip
.match_chip
= cam
->sensor
->addr
;
840 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_CHIP_IDENT
, &chip
);
843 cam
->sensor_type
= chip
.ident
;
844 // if (cam->sensor->addr != OV7xx0_SID) {
845 if (cam
->sensor_type
!= V4L2_IDENT_OV7670
) {
846 cam_err(cam
, "Unsupported sensor type %d", cam
->sensor
->addr
);
850 /* Get/set parameters? */
854 mutex_unlock(&cam
->s_mutex
);
859 * Configure the sensor to match the parameters we have. Caller should
862 static int cafe_cam_set_flip(struct cafe_camera
*cam
)
864 struct v4l2_control ctrl
;
866 memset(&ctrl
, 0, sizeof(ctrl
));
867 ctrl
.id
= V4L2_CID_VFLIP
;
869 return __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, &ctrl
);
873 static int cafe_cam_configure(struct cafe_camera
*cam
)
875 struct v4l2_format fmt
;
878 if (cam
->state
!= S_IDLE
)
880 fmt
.fmt
.pix
= cam
->pix_format
;
881 ret
= __cafe_cam_cmd(cam
, VIDIOC_INT_INIT
, &zero
);
883 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_FMT
, &fmt
);
885 * OV7670 does weird things if flip is set *before* format...
887 ret
+= cafe_cam_set_flip(cam
);
891 /* -------------------------------------------------------------------- */
893 * DMA buffer management. These functions need s_mutex held.
896 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
897 * does a get_free_pages() call, and we waste a good chunk of an orderN
898 * allocation. Should try to allocate the whole set in one chunk.
900 static int cafe_alloc_dma_bufs(struct cafe_camera
*cam
, int loadtime
)
904 cafe_set_config_needed(cam
, 1);
906 cam
->dma_buf_size
= dma_buf_size
;
908 cam
->dma_buf_size
= cam
->pix_format
.sizeimage
;
913 for (i
= 0; i
< n_dma_bufs
; i
++) {
914 cam
->dma_bufs
[i
] = dma_alloc_coherent(&cam
->pdev
->dev
,
915 cam
->dma_buf_size
, cam
->dma_handles
+ i
,
917 if (cam
->dma_bufs
[i
] == NULL
) {
918 cam_warn(cam
, "Failed to allocate DMA buffer\n");
921 /* For debug, remove eventually */
922 memset(cam
->dma_bufs
[i
], 0xcc, cam
->dma_buf_size
);
926 switch (cam
->nbufs
) {
928 dma_free_coherent(&cam
->pdev
->dev
, cam
->dma_buf_size
,
929 cam
->dma_bufs
[0], cam
->dma_handles
[0]);
932 cam_err(cam
, "Insufficient DMA buffers, cannot operate\n");
937 cam_warn(cam
, "Will limp along with only 2 buffers\n");
943 static void cafe_free_dma_bufs(struct cafe_camera
*cam
)
947 for (i
= 0; i
< cam
->nbufs
; i
++) {
948 dma_free_coherent(&cam
->pdev
->dev
, cam
->dma_buf_size
,
949 cam
->dma_bufs
[i
], cam
->dma_handles
[i
]);
950 cam
->dma_bufs
[i
] = NULL
;
959 /* ----------------------------------------------------------------------- */
961 * Here starts the V4L2 interface code.
965 * Read an image from the device.
967 static ssize_t
cafe_deliver_buffer(struct cafe_camera
*cam
,
968 char __user
*buffer
, size_t len
, loff_t
*pos
)
973 spin_lock_irqsave(&cam
->dev_lock
, flags
);
974 if (cam
->next_buf
< 0) {
975 cam_err(cam
, "deliver_buffer: No next buffer\n");
976 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
979 bufno
= cam
->next_buf
;
980 clear_bit(bufno
, &cam
->flags
);
981 if (++(cam
->next_buf
) >= cam
->nbufs
)
983 if (! test_bit(cam
->next_buf
, &cam
->flags
))
986 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
988 if (len
> cam
->pix_format
.sizeimage
)
989 len
= cam
->pix_format
.sizeimage
;
990 if (copy_to_user(buffer
, cam
->dma_bufs
[bufno
], len
))
997 * Get everything ready, and start grabbing frames.
999 static int cafe_read_setup(struct cafe_camera
*cam
, enum cafe_state state
)
1002 unsigned long flags
;
1005 * Configuration. If we still don't have DMA buffers,
1006 * make one last, desperate attempt.
1008 if (cam
->nbufs
== 0)
1009 if (cafe_alloc_dma_bufs(cam
, 0))
1012 if (cafe_needs_config(cam
)) {
1013 cafe_cam_configure(cam
);
1014 ret
= cafe_ctlr_configure(cam
);
1022 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1023 cafe_reset_buffers(cam
);
1024 cafe_ctlr_irq_enable(cam
);
1026 cafe_ctlr_start(cam
);
1027 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1032 static ssize_t
cafe_v4l_read(struct file
*filp
,
1033 char __user
*buffer
, size_t len
, loff_t
*pos
)
1035 struct cafe_camera
*cam
= filp
->private_data
;
1039 * Perhaps we're in speculative read mode and already
1042 mutex_lock(&cam
->s_mutex
);
1043 if (cam
->state
== S_SPECREAD
) {
1044 if (cam
->next_buf
>= 0) {
1045 ret
= cafe_deliver_buffer(cam
, buffer
, len
, pos
);
1049 } else if (cam
->state
== S_FLAKED
|| cam
->state
== S_NOTREADY
) {
1052 } else if (cam
->state
!= S_IDLE
) {
1058 * v4l2: multiple processes can open the device, but only
1059 * one gets to grab data from it.
1061 if (cam
->owner
&& cam
->owner
!= filp
) {
1068 * Do setup if need be.
1070 if (cam
->state
!= S_SPECREAD
) {
1071 ret
= cafe_read_setup(cam
, S_SINGLEREAD
);
1076 * Wait for something to happen. This should probably
1077 * be interruptible (FIXME).
1079 wait_event_timeout(cam
->iowait
, cam
->next_buf
>= 0, HZ
);
1080 if (cam
->next_buf
< 0) {
1081 cam_err(cam
, "read() operation timed out\n");
1082 cafe_ctlr_stop_dma(cam
);
1087 * Give them their data and we should be done.
1089 ret
= cafe_deliver_buffer(cam
, buffer
, len
, pos
);
1092 mutex_unlock(&cam
->s_mutex
);
1104 * Streaming I/O support.
1109 static int cafe_vidioc_streamon(struct file
*filp
, void *priv
,
1110 enum v4l2_buf_type type
)
1112 struct cafe_camera
*cam
= filp
->private_data
;
1115 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1117 mutex_lock(&cam
->s_mutex
);
1118 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
== 0)
1122 ret
= cafe_read_setup(cam
, S_STREAMING
);
1125 mutex_unlock(&cam
->s_mutex
);
1131 static int cafe_vidioc_streamoff(struct file
*filp
, void *priv
,
1132 enum v4l2_buf_type type
)
1134 struct cafe_camera
*cam
= filp
->private_data
;
1137 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1139 mutex_lock(&cam
->s_mutex
);
1140 if (cam
->state
!= S_STREAMING
)
1143 cafe_ctlr_stop_dma(cam
);
1147 mutex_unlock(&cam
->s_mutex
);
1154 static int cafe_setup_siobuf(struct cafe_camera
*cam
, int index
)
1156 struct cafe_sio_buffer
*buf
= cam
->sb_bufs
+ index
;
1158 INIT_LIST_HEAD(&buf
->list
);
1159 buf
->v4lbuf
.length
= PAGE_ALIGN(cam
->pix_format
.sizeimage
);
1160 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
1161 if (buf
->buffer
== NULL
)
1166 buf
->v4lbuf
.index
= index
;
1167 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1168 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
1169 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
1171 * Offset: must be 32-bit even on a 64-bit system. video-buf
1172 * just uses the length times the index, but the spec warns
1173 * against doing just that - vma merging problems. So we
1174 * leave a gap between each pair of buffers.
1176 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
1180 static int cafe_free_sio_buffers(struct cafe_camera
*cam
)
1185 * If any buffers are mapped, we cannot free them at all.
1187 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1188 if (cam
->sb_bufs
[i
].mapcount
> 0)
1193 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1194 vfree(cam
->sb_bufs
[i
].buffer
);
1196 kfree(cam
->sb_bufs
);
1197 cam
->sb_bufs
= NULL
;
1198 INIT_LIST_HEAD(&cam
->sb_avail
);
1199 INIT_LIST_HEAD(&cam
->sb_full
);
1205 static int cafe_vidioc_reqbufs(struct file
*filp
, void *priv
,
1206 struct v4l2_requestbuffers
*req
)
1208 struct cafe_camera
*cam
= filp
->private_data
;
1209 int ret
= 0; /* Silence warning */
1212 * Make sure it's something we can do. User pointers could be
1213 * implemented without great pain, but that's not been done yet.
1215 if (req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1217 if (req
->memory
!= V4L2_MEMORY_MMAP
)
1220 * If they ask for zero buffers, they really want us to stop streaming
1221 * (if it's happening) and free everything. Should we check owner?
1223 mutex_lock(&cam
->s_mutex
);
1224 if (req
->count
== 0) {
1225 if (cam
->state
== S_STREAMING
)
1226 cafe_ctlr_stop_dma(cam
);
1227 ret
= cafe_free_sio_buffers (cam
);
1231 * Device needs to be idle and working. We *could* try to do the
1232 * right thing in S_SPECREAD by shutting things down, but it
1233 * probably doesn't matter.
1235 if (cam
->state
!= S_IDLE
|| (cam
->owner
&& cam
->owner
!= filp
)) {
1241 if (req
->count
< min_buffers
)
1242 req
->count
= min_buffers
;
1243 else if (req
->count
> max_buffers
)
1244 req
->count
= max_buffers
;
1245 if (cam
->n_sbufs
> 0) {
1246 ret
= cafe_free_sio_buffers(cam
);
1251 cam
->sb_bufs
= kzalloc(req
->count
*sizeof(struct cafe_sio_buffer
),
1253 if (cam
->sb_bufs
== NULL
) {
1257 for (cam
->n_sbufs
= 0; cam
->n_sbufs
< req
->count
; (cam
->n_sbufs
++)) {
1258 ret
= cafe_setup_siobuf(cam
, cam
->n_sbufs
);
1263 if (cam
->n_sbufs
== 0) /* no luck at all - ret already set */
1264 kfree(cam
->sb_bufs
);
1265 req
->count
= cam
->n_sbufs
; /* In case of partial success */
1268 mutex_unlock(&cam
->s_mutex
);
1273 static int cafe_vidioc_querybuf(struct file
*filp
, void *priv
,
1274 struct v4l2_buffer
*buf
)
1276 struct cafe_camera
*cam
= filp
->private_data
;
1279 mutex_lock(&cam
->s_mutex
);
1280 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1282 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1284 *buf
= cam
->sb_bufs
[buf
->index
].v4lbuf
;
1287 mutex_unlock(&cam
->s_mutex
);
1291 static int cafe_vidioc_qbuf(struct file
*filp
, void *priv
,
1292 struct v4l2_buffer
*buf
)
1294 struct cafe_camera
*cam
= filp
->private_data
;
1295 struct cafe_sio_buffer
*sbuf
;
1297 unsigned long flags
;
1299 mutex_lock(&cam
->s_mutex
);
1300 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1302 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1304 sbuf
= cam
->sb_bufs
+ buf
->index
;
1305 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
) {
1306 ret
= 0; /* Already queued?? */
1309 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_DONE
) {
1310 /* Spec doesn't say anything, seems appropriate tho */
1314 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1315 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1316 list_add(&sbuf
->list
, &cam
->sb_avail
);
1317 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1320 mutex_unlock(&cam
->s_mutex
);
1324 static int cafe_vidioc_dqbuf(struct file
*filp
, void *priv
,
1325 struct v4l2_buffer
*buf
)
1327 struct cafe_camera
*cam
= filp
->private_data
;
1328 struct cafe_sio_buffer
*sbuf
;
1330 unsigned long flags
;
1332 mutex_lock(&cam
->s_mutex
);
1333 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1335 if (cam
->state
!= S_STREAMING
)
1337 if (list_empty(&cam
->sb_full
) && filp
->f_flags
& O_NONBLOCK
) {
1342 while (list_empty(&cam
->sb_full
) && cam
->state
== S_STREAMING
) {
1343 mutex_unlock(&cam
->s_mutex
);
1344 if (wait_event_interruptible(cam
->iowait
,
1345 !list_empty(&cam
->sb_full
))) {
1349 mutex_lock(&cam
->s_mutex
);
1352 if (cam
->state
!= S_STREAMING
)
1355 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1356 /* Should probably recheck !list_empty() here */
1357 sbuf
= list_entry(cam
->sb_full
.next
,
1358 struct cafe_sio_buffer
, list
);
1359 list_del_init(&sbuf
->list
);
1360 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1361 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1362 *buf
= sbuf
->v4lbuf
;
1367 mutex_unlock(&cam
->s_mutex
);
1374 static void cafe_v4l_vm_open(struct vm_area_struct
*vma
)
1376 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1378 * Locking: done under mmap_sem, so we don't need to
1379 * go back to the camera lock here.
1385 static void cafe_v4l_vm_close(struct vm_area_struct
*vma
)
1387 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1389 mutex_lock(&sbuf
->cam
->s_mutex
);
1391 /* Docs say we should stop I/O too... */
1392 if (sbuf
->mapcount
== 0)
1393 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
1394 mutex_unlock(&sbuf
->cam
->s_mutex
);
1397 static struct vm_operations_struct cafe_v4l_vm_ops
= {
1398 .open
= cafe_v4l_vm_open
,
1399 .close
= cafe_v4l_vm_close
1403 static int cafe_v4l_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1405 struct cafe_camera
*cam
= filp
->private_data
;
1406 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1409 struct cafe_sio_buffer
*sbuf
= NULL
;
1411 if (! (vma
->vm_flags
& VM_WRITE
) || ! (vma
->vm_flags
& VM_SHARED
))
1414 * Find the buffer they are looking for.
1416 mutex_lock(&cam
->s_mutex
);
1417 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1418 if (cam
->sb_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
1419 sbuf
= cam
->sb_bufs
+ i
;
1425 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
1428 vma
->vm_flags
|= VM_DONTEXPAND
;
1429 vma
->vm_private_data
= sbuf
;
1430 vma
->vm_ops
= &cafe_v4l_vm_ops
;
1431 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
1432 cafe_v4l_vm_open(vma
);
1435 mutex_unlock(&cam
->s_mutex
);
1441 static int cafe_v4l_open(struct inode
*inode
, struct file
*filp
)
1443 struct cafe_camera
*cam
;
1445 cam
= cafe_find_dev(iminor(inode
));
1448 filp
->private_data
= cam
;
1450 mutex_lock(&cam
->s_mutex
);
1451 if (cam
->users
== 0) {
1452 cafe_ctlr_power_up(cam
);
1453 __cafe_cam_reset(cam
);
1454 cafe_set_config_needed(cam
, 1);
1455 /* FIXME make sure this is complete */
1458 mutex_unlock(&cam
->s_mutex
);
1463 static int cafe_v4l_release(struct inode
*inode
, struct file
*filp
)
1465 struct cafe_camera
*cam
= filp
->private_data
;
1467 mutex_lock(&cam
->s_mutex
);
1469 if (filp
== cam
->owner
) {
1470 cafe_ctlr_stop_dma(cam
);
1471 cafe_free_sio_buffers(cam
);
1474 if (cam
->users
== 0) {
1475 cafe_ctlr_power_down(cam
);
1476 if (! alloc_bufs_at_load
)
1477 cafe_free_dma_bufs(cam
);
1479 mutex_unlock(&cam
->s_mutex
);
1485 static unsigned int cafe_v4l_poll(struct file
*filp
,
1486 struct poll_table_struct
*pt
)
1488 struct cafe_camera
*cam
= filp
->private_data
;
1490 poll_wait(filp
, &cam
->iowait
, pt
);
1491 if (cam
->next_buf
>= 0)
1492 return POLLIN
| POLLRDNORM
;
1498 static int cafe_vidioc_queryctrl(struct file
*filp
, void *priv
,
1499 struct v4l2_queryctrl
*qc
)
1501 struct cafe_camera
*cam
= filp
->private_data
;
1504 mutex_lock(&cam
->s_mutex
);
1505 ret
= __cafe_cam_cmd(cam
, VIDIOC_QUERYCTRL
, qc
);
1506 mutex_unlock(&cam
->s_mutex
);
1511 static int cafe_vidioc_g_ctrl(struct file
*filp
, void *priv
,
1512 struct v4l2_control
*ctrl
)
1514 struct cafe_camera
*cam
= filp
->private_data
;
1517 mutex_lock(&cam
->s_mutex
);
1518 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_CTRL
, ctrl
);
1519 mutex_unlock(&cam
->s_mutex
);
1524 static int cafe_vidioc_s_ctrl(struct file
*filp
, void *priv
,
1525 struct v4l2_control
*ctrl
)
1527 struct cafe_camera
*cam
= filp
->private_data
;
1530 mutex_lock(&cam
->s_mutex
);
1531 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, ctrl
);
1532 mutex_unlock(&cam
->s_mutex
);
1540 static int cafe_vidioc_querycap(struct file
*file
, void *priv
,
1541 struct v4l2_capability
*cap
)
1543 strcpy(cap
->driver
, "cafe_ccic");
1544 strcpy(cap
->card
, "cafe_ccic");
1545 cap
->version
= CAFE_VERSION
;
1546 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
1547 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1553 * The default format we use until somebody says otherwise.
1555 static struct v4l2_pix_format cafe_def_pix_format
= {
1557 .height
= VGA_HEIGHT
,
1558 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1559 .field
= V4L2_FIELD_NONE
,
1560 .bytesperline
= VGA_WIDTH
*2,
1561 .sizeimage
= VGA_WIDTH
*VGA_HEIGHT
*2,
1564 static int cafe_vidioc_enum_fmt_cap(struct file
*filp
,
1565 void *priv
, struct v4l2_fmtdesc
*fmt
)
1567 struct cafe_camera
*cam
= priv
;
1570 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1572 mutex_lock(&cam
->s_mutex
);
1573 ret
= __cafe_cam_cmd(cam
, VIDIOC_ENUM_FMT
, fmt
);
1574 mutex_unlock(&cam
->s_mutex
);
1579 static int cafe_vidioc_try_fmt_cap (struct file
*filp
, void *priv
,
1580 struct v4l2_format
*fmt
)
1582 struct cafe_camera
*cam
= priv
;
1585 mutex_lock(&cam
->s_mutex
);
1586 ret
= __cafe_cam_cmd(cam
, VIDIOC_TRY_FMT
, fmt
);
1587 mutex_unlock(&cam
->s_mutex
);
1591 static int cafe_vidioc_s_fmt_cap(struct file
*filp
, void *priv
,
1592 struct v4l2_format
*fmt
)
1594 struct cafe_camera
*cam
= priv
;
1598 * Can't do anything if the device is not idle
1599 * Also can't if there are streaming buffers in place.
1601 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
> 0)
1604 * See if the formatting works in principle.
1606 ret
= cafe_vidioc_try_fmt_cap(filp
, priv
, fmt
);
1610 * Now we start to change things for real, so let's do it
1613 mutex_lock(&cam
->s_mutex
);
1614 cam
->pix_format
= fmt
->fmt
.pix
;
1616 * Make sure we have appropriate DMA buffers.
1619 if (cam
->nbufs
> 0 && cam
->dma_buf_size
< cam
->pix_format
.sizeimage
)
1620 cafe_free_dma_bufs(cam
);
1621 if (cam
->nbufs
== 0) {
1622 if (cafe_alloc_dma_bufs(cam
, 0))
1626 * It looks like this might work, so let's program the sensor.
1628 ret
= cafe_cam_configure(cam
);
1630 ret
= cafe_ctlr_configure(cam
);
1632 mutex_unlock(&cam
->s_mutex
);
1637 * Return our stored notion of how the camera is/should be configured.
1638 * The V4l2 spec wants us to be smarter, and actually get this from
1639 * the camera (and not mess with it at open time). Someday.
1641 static int cafe_vidioc_g_fmt_cap(struct file
*filp
, void *priv
,
1642 struct v4l2_format
*f
)
1644 struct cafe_camera
*cam
= priv
;
1646 f
->fmt
.pix
= cam
->pix_format
;
1651 * We only have one input - the sensor - so minimize the nonsense here.
1653 static int cafe_vidioc_enum_input(struct file
*filp
, void *priv
,
1654 struct v4l2_input
*input
)
1656 if (input
->index
!= 0)
1659 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1660 input
->std
= V4L2_STD_ALL
; /* Not sure what should go here */
1661 strcpy(input
->name
, "Camera");
1665 static int cafe_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
1671 static int cafe_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1679 static int cafe_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
1685 * G/S_PARM. Most of this is done by the sensor, but we are
1686 * the level which controls the number of read buffers.
1688 static int cafe_vidioc_g_parm(struct file
*filp
, void *priv
,
1689 struct v4l2_streamparm
*parms
)
1691 struct cafe_camera
*cam
= priv
;
1694 mutex_lock(&cam
->s_mutex
);
1695 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_PARM
, parms
);
1696 mutex_unlock(&cam
->s_mutex
);
1697 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1701 static int cafe_vidioc_s_parm(struct file
*filp
, void *priv
,
1702 struct v4l2_streamparm
*parms
)
1704 struct cafe_camera
*cam
= priv
;
1707 mutex_lock(&cam
->s_mutex
);
1708 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_PARM
, parms
);
1709 mutex_unlock(&cam
->s_mutex
);
1710 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1715 static void cafe_v4l_dev_release(struct video_device
*vd
)
1717 struct cafe_camera
*cam
= container_of(vd
, struct cafe_camera
, v4ldev
);
1724 * This template device holds all of those v4l2 methods; we
1725 * clone it for specific real devices.
1728 static const struct file_operations cafe_v4l_fops
= {
1729 .owner
= THIS_MODULE
,
1730 .open
= cafe_v4l_open
,
1731 .release
= cafe_v4l_release
,
1732 .read
= cafe_v4l_read
,
1733 .poll
= cafe_v4l_poll
,
1734 .mmap
= cafe_v4l_mmap
,
1735 .ioctl
= video_ioctl2
,
1736 .llseek
= no_llseek
,
1739 static struct video_device cafe_v4l_template
= {
1741 .type
= VFL_TYPE_GRABBER
,
1742 .type2
= VID_TYPE_CAPTURE
,
1743 .minor
= -1, /* Get one dynamically */
1744 .tvnorms
= V4L2_STD_NTSC_M
,
1745 .current_norm
= V4L2_STD_NTSC_M
, /* make mplayer happy */
1747 .fops
= &cafe_v4l_fops
,
1748 .release
= cafe_v4l_dev_release
,
1750 .vidioc_querycap
= cafe_vidioc_querycap
,
1751 .vidioc_enum_fmt_cap
= cafe_vidioc_enum_fmt_cap
,
1752 .vidioc_try_fmt_cap
= cafe_vidioc_try_fmt_cap
,
1753 .vidioc_s_fmt_cap
= cafe_vidioc_s_fmt_cap
,
1754 .vidioc_g_fmt_cap
= cafe_vidioc_g_fmt_cap
,
1755 .vidioc_enum_input
= cafe_vidioc_enum_input
,
1756 .vidioc_g_input
= cafe_vidioc_g_input
,
1757 .vidioc_s_input
= cafe_vidioc_s_input
,
1758 .vidioc_s_std
= cafe_vidioc_s_std
,
1759 .vidioc_reqbufs
= cafe_vidioc_reqbufs
,
1760 .vidioc_querybuf
= cafe_vidioc_querybuf
,
1761 .vidioc_qbuf
= cafe_vidioc_qbuf
,
1762 .vidioc_dqbuf
= cafe_vidioc_dqbuf
,
1763 .vidioc_streamon
= cafe_vidioc_streamon
,
1764 .vidioc_streamoff
= cafe_vidioc_streamoff
,
1765 .vidioc_queryctrl
= cafe_vidioc_queryctrl
,
1766 .vidioc_g_ctrl
= cafe_vidioc_g_ctrl
,
1767 .vidioc_s_ctrl
= cafe_vidioc_s_ctrl
,
1768 .vidioc_g_parm
= cafe_vidioc_g_parm
,
1769 .vidioc_s_parm
= cafe_vidioc_s_parm
,
1778 /* ---------------------------------------------------------------------- */
1780 * Interrupt handler stuff
1785 static void cafe_frame_tasklet(unsigned long data
)
1787 struct cafe_camera
*cam
= (struct cafe_camera
*) data
;
1789 unsigned long flags
;
1790 struct cafe_sio_buffer
*sbuf
;
1792 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1793 for (i
= 0; i
< cam
->nbufs
; i
++) {
1794 int bufno
= cam
->next_buf
;
1795 if (bufno
< 0) { /* "will never happen" */
1796 cam_err(cam
, "No valid bufs in tasklet!\n");
1799 if (++(cam
->next_buf
) >= cam
->nbufs
)
1801 if (! test_bit(bufno
, &cam
->flags
))
1803 if (list_empty(&cam
->sb_avail
))
1804 break; /* Leave it valid, hope for better later */
1805 clear_bit(bufno
, &cam
->flags
);
1806 sbuf
= list_entry(cam
->sb_avail
.next
,
1807 struct cafe_sio_buffer
, list
);
1809 * Drop the lock during the big copy. This *should* be safe...
1811 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1812 memcpy(sbuf
->buffer
, cam
->dma_bufs
[bufno
],
1813 cam
->pix_format
.sizeimage
);
1814 sbuf
->v4lbuf
.bytesused
= cam
->pix_format
.sizeimage
;
1815 sbuf
->v4lbuf
.sequence
= cam
->buf_seq
[bufno
];
1816 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1817 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1818 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1819 list_move_tail(&sbuf
->list
, &cam
->sb_full
);
1821 if (! list_empty(&cam
->sb_full
))
1822 wake_up(&cam
->iowait
);
1823 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1828 static void cafe_frame_complete(struct cafe_camera
*cam
, int frame
)
1831 * Basic frame housekeeping.
1833 if (test_bit(frame
, &cam
->flags
) && printk_ratelimit())
1834 cam_err(cam
, "Frame overrun on %d, frames lost\n", frame
);
1835 set_bit(frame
, &cam
->flags
);
1836 clear_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1837 if (cam
->next_buf
< 0)
1838 cam
->next_buf
= frame
;
1839 cam
->buf_seq
[frame
] = ++(cam
->sequence
);
1841 switch (cam
->state
) {
1843 * If in single read mode, try going speculative.
1846 cam
->state
= S_SPECREAD
;
1847 cam
->specframes
= 0;
1848 wake_up(&cam
->iowait
);
1852 * If we are already doing speculative reads, and nobody is
1853 * reading them, just stop.
1856 if (++(cam
->specframes
) >= cam
->nbufs
) {
1857 cafe_ctlr_stop(cam
);
1858 cafe_ctlr_irq_disable(cam
);
1859 cam
->state
= S_IDLE
;
1861 wake_up(&cam
->iowait
);
1864 * For the streaming case, we defer the real work to the
1867 * FIXME: if the application is not consuming the buffers,
1868 * we should eventually put things on hold and restart in
1872 tasklet_schedule(&cam
->s_tasklet
);
1876 cam_err(cam
, "Frame interrupt in non-operational state\n");
1884 static void cafe_frame_irq(struct cafe_camera
*cam
, unsigned int irqs
)
1888 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
); /* Clear'em all */
1890 * Handle any frame completions. There really should
1891 * not be more than one of these, or we have fallen
1894 for (frame
= 0; frame
< cam
->nbufs
; frame
++)
1895 if (irqs
& (IRQ_EOF0
<< frame
))
1896 cafe_frame_complete(cam
, frame
);
1898 * If a frame starts, note that we have DMA active. This
1899 * code assumes that we won't get multiple frame interrupts
1900 * at once; may want to rethink that.
1902 if (irqs
& (IRQ_SOF0
| IRQ_SOF1
| IRQ_SOF2
))
1903 set_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1908 static irqreturn_t
cafe_irq(int irq
, void *data
)
1910 struct cafe_camera
*cam
= data
;
1913 spin_lock(&cam
->dev_lock
);
1914 irqs
= cafe_reg_read(cam
, REG_IRQSTAT
);
1915 if ((irqs
& ALLIRQS
) == 0) {
1916 spin_unlock(&cam
->dev_lock
);
1919 if (irqs
& FRAMEIRQS
)
1920 cafe_frame_irq(cam
, irqs
);
1921 if (irqs
& TWSIIRQS
) {
1922 cafe_reg_write(cam
, REG_IRQSTAT
, TWSIIRQS
);
1923 wake_up(&cam
->smbus_wait
);
1925 spin_unlock(&cam
->dev_lock
);
1930 /* -------------------------------------------------------------------------- */
1931 #ifdef CONFIG_VIDEO_ADV_DEBUG
1936 static char cafe_debug_buf
[1024];
1937 static struct dentry
*cafe_dfs_root
;
1939 static void cafe_dfs_setup(void)
1941 cafe_dfs_root
= debugfs_create_dir("cafe_ccic", NULL
);
1942 if (IS_ERR(cafe_dfs_root
)) {
1943 cafe_dfs_root
= NULL
; /* Never mind */
1944 printk(KERN_NOTICE
"cafe_ccic unable to set up debugfs\n");
1948 static void cafe_dfs_shutdown(void)
1951 debugfs_remove(cafe_dfs_root
);
1954 static int cafe_dfs_open(struct inode
*inode
, struct file
*file
)
1956 file
->private_data
= inode
->i_private
;
1960 static ssize_t
cafe_dfs_read_regs(struct file
*file
,
1961 char __user
*buf
, size_t count
, loff_t
*ppos
)
1963 struct cafe_camera
*cam
= file
->private_data
;
1964 char *s
= cafe_debug_buf
;
1967 for (offset
= 0; offset
< 0x44; offset
+= 4)
1968 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1969 cafe_reg_read(cam
, offset
));
1970 for (offset
= 0x88; offset
<= 0x90; offset
+= 4)
1971 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1972 cafe_reg_read(cam
, offset
));
1973 for (offset
= 0xb4; offset
<= 0xbc; offset
+= 4)
1974 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1975 cafe_reg_read(cam
, offset
));
1976 for (offset
= 0x3000; offset
<= 0x300c; offset
+= 4)
1977 s
+= sprintf(s
, "%04x: %08x\n", offset
,
1978 cafe_reg_read(cam
, offset
));
1979 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1980 s
- cafe_debug_buf
);
1983 static const struct file_operations cafe_dfs_reg_ops
= {
1984 .owner
= THIS_MODULE
,
1985 .read
= cafe_dfs_read_regs
,
1986 .open
= cafe_dfs_open
1989 static ssize_t
cafe_dfs_read_cam(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
;
1998 for (offset
= 0x0; offset
< 0x8a; offset
++)
2002 cafe_smbus_read_data(cam
, cam
->sensor
->addr
, offset
, &v
);
2003 s
+= sprintf(s
, "%02x: %02x\n", offset
, v
);
2005 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
2006 s
- cafe_debug_buf
);
2009 static const struct file_operations cafe_dfs_cam_ops
= {
2010 .owner
= THIS_MODULE
,
2011 .read
= cafe_dfs_read_cam
,
2012 .open
= cafe_dfs_open
2017 static void cafe_dfs_cam_setup(struct cafe_camera
*cam
)
2023 sprintf(fname
, "regs-%d", cam
->v4ldev
.minor
);
2024 cam
->dfs_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2025 cam
, &cafe_dfs_reg_ops
);
2026 sprintf(fname
, "cam-%d", cam
->v4ldev
.minor
);
2027 cam
->dfs_cam_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2028 cam
, &cafe_dfs_cam_ops
);
2032 static void cafe_dfs_cam_shutdown(struct cafe_camera
*cam
)
2034 if (! IS_ERR(cam
->dfs_regs
))
2035 debugfs_remove(cam
->dfs_regs
);
2036 if (! IS_ERR(cam
->dfs_cam_regs
))
2037 debugfs_remove(cam
->dfs_cam_regs
);
2042 #define cafe_dfs_setup()
2043 #define cafe_dfs_shutdown()
2044 #define cafe_dfs_cam_setup(cam)
2045 #define cafe_dfs_cam_shutdown(cam)
2046 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2051 /* ------------------------------------------------------------------------*/
2053 * PCI interface stuff.
2056 static int cafe_pci_probe(struct pci_dev
*pdev
,
2057 const struct pci_device_id
*id
)
2061 struct cafe_camera
*cam
;
2063 * Make sure we have a camera here - we'll get calls for
2064 * the other cafe devices as well.
2066 pci_read_config_word(pdev
, PCI_CLASS_DEVICE
, &classword
);
2067 if (classword
!= PCI_CLASS_MULTIMEDIA_VIDEO
)
2070 * Start putting together one of our big camera structures.
2073 cam
= kzalloc(sizeof(struct cafe_camera
), GFP_KERNEL
);
2076 mutex_init(&cam
->s_mutex
);
2077 mutex_lock(&cam
->s_mutex
);
2078 spin_lock_init(&cam
->dev_lock
);
2079 cam
->state
= S_NOTREADY
;
2080 cafe_set_config_needed(cam
, 1);
2081 init_waitqueue_head(&cam
->smbus_wait
);
2082 init_waitqueue_head(&cam
->iowait
);
2084 cam
->pix_format
= cafe_def_pix_format
;
2085 INIT_LIST_HEAD(&cam
->dev_list
);
2086 INIT_LIST_HEAD(&cam
->sb_avail
);
2087 INIT_LIST_HEAD(&cam
->sb_full
);
2088 tasklet_init(&cam
->s_tasklet
, cafe_frame_tasklet
, (unsigned long) cam
);
2090 * Get set up on the PCI bus.
2092 ret
= pci_enable_device(pdev
);
2095 pci_set_master(pdev
);
2098 cam
->regs
= pci_iomap(pdev
, 0, 0);
2100 printk(KERN_ERR
"Unable to ioremap cafe-ccic regs\n");
2103 ret
= request_irq(pdev
->irq
, cafe_irq
, IRQF_SHARED
, "cafe-ccic", cam
);
2106 cafe_ctlr_init(cam
);
2107 cafe_ctlr_power_up(cam
);
2109 * Set up I2C/SMBUS communications
2111 mutex_unlock(&cam
->s_mutex
); /* attach can deadlock */
2112 ret
= cafe_smbus_setup(cam
);
2116 * Get the v4l2 setup done.
2118 mutex_lock(&cam
->s_mutex
);
2119 cam
->v4ldev
= cafe_v4l_template
;
2120 cam
->v4ldev
.debug
= 0;
2121 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2122 cam
->v4ldev
.dev
= &pdev
->dev
;
2123 ret
= video_register_device(&cam
->v4ldev
, VFL_TYPE_GRABBER
, -1);
2127 * If so requested, try to get our DMA buffers now.
2129 if (alloc_bufs_at_load
) {
2130 if (cafe_alloc_dma_bufs(cam
, 1))
2131 cam_warn(cam
, "Unable to alloc DMA buffers at load"
2132 " will try again later.");
2135 cafe_dfs_cam_setup(cam
);
2136 mutex_unlock(&cam
->s_mutex
);
2141 cafe_smbus_shutdown(cam
);
2143 cafe_ctlr_power_down(cam
);
2144 free_irq(pdev
->irq
, cam
);
2146 pci_iounmap(pdev
, cam
->regs
);
2155 * Shut down an initialized device
2157 static void cafe_shutdown(struct cafe_camera
*cam
)
2159 /* FIXME: Make sure we take care of everything here */
2160 cafe_dfs_cam_shutdown(cam
);
2161 if (cam
->n_sbufs
> 0)
2162 /* What if they are still mapped? Shouldn't be, but... */
2163 cafe_free_sio_buffers(cam
);
2164 cafe_remove_dev(cam
);
2165 cafe_ctlr_stop_dma(cam
);
2166 cafe_ctlr_power_down(cam
);
2167 cafe_smbus_shutdown(cam
);
2168 cafe_free_dma_bufs(cam
);
2169 free_irq(cam
->pdev
->irq
, cam
);
2170 pci_iounmap(cam
->pdev
, cam
->regs
);
2171 video_unregister_device(&cam
->v4ldev
);
2172 /* kfree(cam); done in v4l_release () */
2176 static void cafe_pci_remove(struct pci_dev
*pdev
)
2178 struct cafe_camera
*cam
= cafe_find_by_pdev(pdev
);
2181 printk(KERN_WARNING
"pci_remove on unknown pdev %p\n", pdev
);
2184 mutex_lock(&cam
->s_mutex
);
2186 cam_warn(cam
, "Removing a device with users!\n");
2188 /* No unlock - it no longer exists */
2194 * Basic power management.
2196 static int cafe_pci_suspend(struct pci_dev
*pdev
, pm_message_t state
)
2198 struct cafe_camera
*cam
= cafe_find_by_pdev(pdev
);
2201 ret
= pci_save_state(pdev
);
2204 cafe_ctlr_stop_dma(cam
);
2205 cafe_ctlr_power_down(cam
);
2206 pci_disable_device(pdev
);
2211 static int cafe_pci_resume(struct pci_dev
*pdev
)
2213 struct cafe_camera
*cam
= cafe_find_by_pdev(pdev
);
2216 ret
= pci_restore_state(pdev
);
2219 ret
= pci_enable_device(pdev
);
2221 cam_warn(cam
, "Unable to re-enable device on resume!\n");
2224 cafe_ctlr_init(cam
);
2225 cafe_ctlr_power_up(cam
);
2226 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
2227 if (cam
->state
== S_SPECREAD
)
2228 cam
->state
= S_IDLE
; /* Don't bother restarting */
2229 else if (cam
->state
== S_SINGLEREAD
|| cam
->state
== S_STREAMING
)
2230 ret
= cafe_read_setup(cam
, cam
->state
);
2234 #endif /* CONFIG_PM */
2237 static struct pci_device_id cafe_ids
[] = {
2238 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2239 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2243 MODULE_DEVICE_TABLE(pci
, cafe_ids
);
2245 static struct pci_driver cafe_pci_driver
= {
2246 .name
= "cafe1000-ccic",
2247 .id_table
= cafe_ids
,
2248 .probe
= cafe_pci_probe
,
2249 .remove
= cafe_pci_remove
,
2251 .suspend
= cafe_pci_suspend
,
2252 .resume
= cafe_pci_resume
,
2259 static int __init
cafe_init(void)
2263 printk(KERN_NOTICE
"Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2266 ret
= pci_register_driver(&cafe_pci_driver
);
2268 printk(KERN_ERR
"Unable to register cafe_ccic driver\n");
2271 request_module("ov7670"); /* FIXME want something more general */
2279 static void __exit
cafe_exit(void)
2281 pci_unregister_driver(&cafe_pci_driver
);
2282 cafe_dfs_shutdown();
2285 module_init(cafe_init
);
2286 module_exit(cafe_exit
);