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.
8 * Written by Jonathan Corbet, corbet@lwn.net.
10 * This file may be distributed under the terms of the GNU General
11 * Public License, version 2.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-chip-ident.h>
26 #include <linux/device.h>
27 #include <linux/wait.h>
28 #include <linux/list.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/delay.h>
31 #include <linux/debugfs.h>
32 #include <linux/jiffies.h>
33 #include <linux/vmalloc.h>
35 #include <asm/uaccess.h>
38 #include "cafe_ccic-regs.h"
40 #define CAFE_VERSION 0x000001
46 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
47 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
48 MODULE_LICENSE("GPL");
49 MODULE_SUPPORTED_DEVICE("Video");
52 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
53 * we must have physically contiguous buffers to bring frames into.
54 * These parameters control how many buffers we use, whether we
55 * allocate them at load time (better chance of success, but nails down
56 * memory) or when somebody tries to use the camera (riskier), and,
57 * for load-time allocation, how big they should be.
59 * The controller can cycle through three buffers. We could use
60 * more by flipping pointers around, but it probably makes little
64 #define MAX_DMA_BUFS 3
65 static int alloc_bufs_at_load
= 0;
66 module_param(alloc_bufs_at_load
, bool, 0444);
67 MODULE_PARM_DESC(alloc_bufs_at_load
,
68 "Non-zero value causes DMA buffers to be allocated at module "
69 "load time. This increases the chances of successfully getting "
70 "those buffers, but at the cost of nailing down the memory from "
73 static int n_dma_bufs
= 3;
74 module_param(n_dma_bufs
, uint
, 0644);
75 MODULE_PARM_DESC(n_dma_bufs
,
76 "The number of DMA buffers to allocate. Can be either two "
77 "(saves memory, makes timing tighter) or three.");
79 static int dma_buf_size
= VGA_WIDTH
* VGA_HEIGHT
* 2; /* Worst case */
80 module_param(dma_buf_size
, uint
, 0444);
81 MODULE_PARM_DESC(dma_buf_size
,
82 "The size of the allocated DMA buffers. If actual operating "
83 "parameters require larger buffers, an attempt to reallocate "
86 static int min_buffers
= 1;
87 module_param(min_buffers
, uint
, 0644);
88 MODULE_PARM_DESC(min_buffers
,
89 "The minimum number of streaming I/O buffers we are willing "
92 static int max_buffers
= 10;
93 module_param(max_buffers
, uint
, 0644);
94 MODULE_PARM_DESC(max_buffers
,
95 "The maximum number of streaming I/O buffers an application "
96 "will be allowed to allocate. These buffers are big and live "
100 module_param(flip
, bool, 0444);
101 MODULE_PARM_DESC(flip
,
102 "If set, the sensor will be instructed to flip the image "
107 S_NOTREADY
, /* Not yet initialized */
108 S_IDLE
, /* Just hanging around */
109 S_FLAKED
, /* Some sort of problem */
110 S_SINGLEREAD
, /* In read() */
111 S_SPECREAD
, /* Speculative read (for future read()) */
112 S_STREAMING
/* Streaming data */
116 * Tracking of streaming I/O buffers.
118 struct cafe_sio_buffer
{
119 struct list_head list
;
120 struct v4l2_buffer v4lbuf
;
121 char *buffer
; /* Where it lives in kernel space */
123 struct cafe_camera
*cam
;
127 * A description of one of our devices.
128 * Locking: controlled by s_mutex. Certain fields, however, require
129 * the dev_lock spinlock; they are marked as such by comments.
130 * dev_lock is also required for access to device registers.
134 enum cafe_state state
;
135 unsigned long flags
; /* Buffer status, mainly (dev_lock) */
136 int users
; /* How many open FDs */
137 struct file
*owner
; /* Who has data access (v4l2) */
140 * Subsystem structures.
142 struct pci_dev
*pdev
;
143 struct video_device v4ldev
;
144 struct i2c_adapter i2c_adapter
;
145 struct i2c_client
*sensor
;
147 unsigned char __iomem
*regs
;
148 struct list_head dev_list
; /* link to other devices */
151 unsigned int nbufs
; /* How many are alloc'd */
152 int next_buf
; /* Next to consume (dev_lock) */
153 unsigned int dma_buf_size
; /* allocated size */
154 void *dma_bufs
[MAX_DMA_BUFS
]; /* Internal buffer addresses */
155 dma_addr_t dma_handles
[MAX_DMA_BUFS
]; /* Buffer bus addresses */
156 unsigned int specframes
; /* Unconsumed spec frames (dev_lock) */
157 unsigned int sequence
; /* Frame sequence number */
158 unsigned int buf_seq
[MAX_DMA_BUFS
]; /* Sequence for individual buffers */
160 /* Streaming buffers */
161 unsigned int n_sbufs
; /* How many we have */
162 struct cafe_sio_buffer
*sb_bufs
; /* The array of housekeeping structs */
163 struct list_head sb_avail
; /* Available for data (we own) (dev_lock) */
164 struct list_head sb_full
; /* With data (user space owns) (dev_lock) */
165 struct tasklet_struct s_tasklet
;
167 /* Current operating parameters */
168 u32 sensor_type
; /* Currently ov7670 only */
169 struct v4l2_pix_format pix_format
;
172 struct mutex s_mutex
; /* Access to this structure */
173 spinlock_t dev_lock
; /* Access to device */
176 wait_queue_head_t smbus_wait
; /* Waiting on i2c events */
177 wait_queue_head_t iowait
; /* Waiting on frame data */
178 #ifdef CONFIG_VIDEO_ADV_DEBUG
179 struct dentry
*dfs_regs
;
180 struct dentry
*dfs_cam_regs
;
185 * Status flags. Always manipulated with bit operations.
187 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
188 #define CF_BUF1_VALID 1
189 #define CF_BUF2_VALID 2
190 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
191 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
196 * Start over with DMA buffers - dev_lock needed.
198 static void cafe_reset_buffers(struct cafe_camera
*cam
)
203 for (i
= 0; i
< cam
->nbufs
; i
++)
204 clear_bit(i
, &cam
->flags
);
208 static inline int cafe_needs_config(struct cafe_camera
*cam
)
210 return test_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
213 static void cafe_set_config_needed(struct cafe_camera
*cam
, int needed
)
216 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
218 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
225 * Debugging and related.
227 #define cam_err(cam, fmt, arg...) \
228 dev_err(&(cam)->pdev->dev, fmt, ##arg);
229 #define cam_warn(cam, fmt, arg...) \
230 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
231 #define cam_dbg(cam, fmt, arg...) \
232 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
235 /* ---------------------------------------------------------------------*/
237 * We keep a simple list of known devices to search at open time.
239 static LIST_HEAD(cafe_dev_list
);
240 static DEFINE_MUTEX(cafe_dev_list_lock
);
242 static void cafe_add_dev(struct cafe_camera
*cam
)
244 mutex_lock(&cafe_dev_list_lock
);
245 list_add_tail(&cam
->dev_list
, &cafe_dev_list
);
246 mutex_unlock(&cafe_dev_list_lock
);
249 static void cafe_remove_dev(struct cafe_camera
*cam
)
251 mutex_lock(&cafe_dev_list_lock
);
252 list_del(&cam
->dev_list
);
253 mutex_unlock(&cafe_dev_list_lock
);
256 static struct cafe_camera
*cafe_find_dev(int minor
)
258 struct cafe_camera
*cam
;
260 mutex_lock(&cafe_dev_list_lock
);
261 list_for_each_entry(cam
, &cafe_dev_list
, dev_list
) {
262 if (cam
->v4ldev
.minor
== minor
)
267 mutex_unlock(&cafe_dev_list_lock
);
272 static struct cafe_camera
*cafe_find_by_pdev(struct pci_dev
*pdev
)
274 struct cafe_camera
*cam
;
276 mutex_lock(&cafe_dev_list_lock
);
277 list_for_each_entry(cam
, &cafe_dev_list
, dev_list
) {
278 if (cam
->pdev
== pdev
)
283 mutex_unlock(&cafe_dev_list_lock
);
288 /* ------------------------------------------------------------------------ */
290 * Device register I/O
292 static inline void cafe_reg_write(struct cafe_camera
*cam
, unsigned int reg
,
295 iowrite32(val
, cam
->regs
+ reg
);
298 static inline unsigned int cafe_reg_read(struct cafe_camera
*cam
,
301 return ioread32(cam
->regs
+ reg
);
305 static inline void cafe_reg_write_mask(struct cafe_camera
*cam
, unsigned int reg
,
306 unsigned int val
, unsigned int mask
)
308 unsigned int v
= cafe_reg_read(cam
, reg
);
310 v
= (v
& ~mask
) | (val
& mask
);
311 cafe_reg_write(cam
, reg
, v
);
314 static inline void cafe_reg_clear_bit(struct cafe_camera
*cam
,
315 unsigned int reg
, unsigned int val
)
317 cafe_reg_write_mask(cam
, reg
, 0, val
);
320 static inline void cafe_reg_set_bit(struct cafe_camera
*cam
,
321 unsigned int reg
, unsigned int val
)
323 cafe_reg_write_mask(cam
, reg
, val
, val
);
328 /* -------------------------------------------------------------------- */
330 * The I2C/SMBUS interface to the camera itself starts here. The
331 * controller handles SMBUS itself, presenting a relatively simple register
332 * interface; all we have to do is to tell it where to route the data.
334 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
336 static int cafe_smbus_write_done(struct cafe_camera
*cam
)
342 * We must delay after the interrupt, or the controller gets confused
343 * and never does give us good status. Fortunately, we don't do this
347 spin_lock_irqsave(&cam
->dev_lock
, flags
);
348 c1
= cafe_reg_read(cam
, REG_TWSIC1
);
349 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
350 return (c1
& (TWSIC1_WSTAT
|TWSIC1_ERROR
)) != TWSIC1_WSTAT
;
353 static int cafe_smbus_write_data(struct cafe_camera
*cam
,
354 u16 addr
, u8 command
, u8 value
)
359 spin_lock_irqsave(&cam
->dev_lock
, flags
);
360 rval
= TWSIC0_EN
| ((addr
<< TWSIC0_SID_SHIFT
) & TWSIC0_SID
);
361 rval
|= TWSIC0_OVMAGIC
; /* Make OV sensors work */
363 * Marvell sez set clkdiv to all 1's for now.
365 rval
|= TWSIC0_CLKDIV
;
366 cafe_reg_write(cam
, REG_TWSIC0
, rval
);
367 (void) cafe_reg_read(cam
, REG_TWSIC1
); /* force write */
368 rval
= value
| ((command
<< TWSIC1_ADDR_SHIFT
) & TWSIC1_ADDR
);
369 cafe_reg_write(cam
, REG_TWSIC1
, rval
);
370 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
371 msleep(2); /* Required or things flake */
373 wait_event_timeout(cam
->smbus_wait
, cafe_smbus_write_done(cam
),
375 spin_lock_irqsave(&cam
->dev_lock
, flags
);
376 rval
= cafe_reg_read(cam
, REG_TWSIC1
);
377 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
379 if (rval
& TWSIC1_WSTAT
) {
380 cam_err(cam
, "SMBUS write (%02x/%02x/%02x) timed out\n", addr
,
384 if (rval
& TWSIC1_ERROR
) {
385 cam_err(cam
, "SMBUS write (%02x/%02x/%02x) error\n", addr
,
394 static int cafe_smbus_read_done(struct cafe_camera
*cam
)
400 * We must delay after the interrupt, or the controller gets confused
401 * and never does give us good status. Fortunately, we don't do this
405 spin_lock_irqsave(&cam
->dev_lock
, flags
);
406 c1
= cafe_reg_read(cam
, REG_TWSIC1
);
407 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
408 return c1
& (TWSIC1_RVALID
|TWSIC1_ERROR
);
413 static int cafe_smbus_read_data(struct cafe_camera
*cam
,
414 u16 addr
, u8 command
, u8
*value
)
419 spin_lock_irqsave(&cam
->dev_lock
, flags
);
420 rval
= TWSIC0_EN
| ((addr
<< TWSIC0_SID_SHIFT
) & TWSIC0_SID
);
421 rval
|= TWSIC0_OVMAGIC
; /* Make OV sensors work */
423 * Marvel sez set clkdiv to all 1's for now.
425 rval
|= TWSIC0_CLKDIV
;
426 cafe_reg_write(cam
, REG_TWSIC0
, rval
);
427 (void) cafe_reg_read(cam
, REG_TWSIC1
); /* force write */
428 rval
= TWSIC1_READ
| ((command
<< TWSIC1_ADDR_SHIFT
) & TWSIC1_ADDR
);
429 cafe_reg_write(cam
, REG_TWSIC1
, rval
);
430 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
432 wait_event_timeout(cam
->smbus_wait
,
433 cafe_smbus_read_done(cam
), CAFE_SMBUS_TIMEOUT
);
434 spin_lock_irqsave(&cam
->dev_lock
, flags
);
435 rval
= cafe_reg_read(cam
, REG_TWSIC1
);
436 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
438 if (rval
& TWSIC1_ERROR
) {
439 cam_err(cam
, "SMBUS read (%02x/%02x) error\n", addr
, command
);
442 if (! (rval
& TWSIC1_RVALID
)) {
443 cam_err(cam
, "SMBUS read (%02x/%02x) timed out\n", addr
,
447 *value
= rval
& 0xff;
452 * Perform a transfer over SMBUS. This thing is called under
453 * the i2c bus lock, so we shouldn't race with ourselves...
455 static int cafe_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
456 unsigned short flags
, char rw
, u8 command
,
457 int size
, union i2c_smbus_data
*data
)
459 struct cafe_camera
*cam
= i2c_get_adapdata(adapter
);
463 * Refuse to talk to anything but OV cam chips. We should
464 * never even see an attempt to do so, but one never knows.
466 if (cam
->sensor
&& addr
!= cam
->sensor
->addr
) {
467 cam_err(cam
, "funky smbus addr %d\n", addr
);
471 * This interface would appear to only do byte data ops. OK
472 * it can do word too, but the cam chip has no use for that.
474 if (size
!= I2C_SMBUS_BYTE_DATA
) {
475 cam_err(cam
, "funky xfer size %d\n", size
);
479 if (rw
== I2C_SMBUS_WRITE
)
480 ret
= cafe_smbus_write_data(cam
, addr
, command
, data
->byte
);
481 else if (rw
== I2C_SMBUS_READ
)
482 ret
= cafe_smbus_read_data(cam
, addr
, command
, &data
->byte
);
487 static void cafe_smbus_enable_irq(struct cafe_camera
*cam
)
491 spin_lock_irqsave(&cam
->dev_lock
, flags
);
492 cafe_reg_set_bit(cam
, REG_IRQMASK
, TWSIIRQS
);
493 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
496 static u32
cafe_smbus_func(struct i2c_adapter
*adapter
)
498 return I2C_FUNC_SMBUS_READ_BYTE_DATA
|
499 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
;
502 static struct i2c_algorithm cafe_smbus_algo
= {
503 .smbus_xfer
= cafe_smbus_xfer
,
504 .functionality
= cafe_smbus_func
507 /* Somebody is on the bus */
508 static int cafe_cam_init(struct cafe_camera
*cam
);
509 static void cafe_ctlr_stop_dma(struct cafe_camera
*cam
);
510 static void cafe_ctlr_power_down(struct cafe_camera
*cam
);
512 static int cafe_smbus_attach(struct i2c_client
*client
)
514 struct cafe_camera
*cam
= i2c_get_adapdata(client
->adapter
);
517 * Don't talk to chips we don't recognize.
519 if (client
->driver
->id
== I2C_DRIVERID_OV7670
) {
520 cam
->sensor
= client
;
521 return cafe_cam_init(cam
);
526 static int cafe_smbus_detach(struct i2c_client
*client
)
528 struct cafe_camera
*cam
= i2c_get_adapdata(client
->adapter
);
530 if (cam
->sensor
== client
) {
531 cafe_ctlr_stop_dma(cam
);
532 cafe_ctlr_power_down(cam
);
533 cam_err(cam
, "lost the sensor!\n");
534 cam
->sensor
= NULL
; /* Bummer, no camera */
535 cam
->state
= S_NOTREADY
;
540 static int cafe_smbus_setup(struct cafe_camera
*cam
)
542 struct i2c_adapter
*adap
= &cam
->i2c_adapter
;
545 cafe_smbus_enable_irq(cam
);
546 adap
->id
= I2C_HW_SMBUS_CAFE
;
547 adap
->class = I2C_CLASS_CAM_DIGITAL
;
548 adap
->owner
= THIS_MODULE
;
549 adap
->client_register
= cafe_smbus_attach
;
550 adap
->client_unregister
= cafe_smbus_detach
;
551 adap
->algo
= &cafe_smbus_algo
;
552 strcpy(adap
->name
, "cafe_ccic");
553 adap
->dev
.parent
= &cam
->pdev
->dev
;
554 i2c_set_adapdata(adap
, cam
);
555 ret
= i2c_add_adapter(adap
);
557 printk(KERN_ERR
"Unable to register cafe i2c adapter\n");
561 static void cafe_smbus_shutdown(struct cafe_camera
*cam
)
563 i2c_del_adapter(&cam
->i2c_adapter
);
567 /* ------------------------------------------------------------------- */
569 * Deal with the controller.
573 * Do everything we think we need to have the interface operating
574 * according to the desired format.
576 static void cafe_ctlr_dma(struct cafe_camera
*cam
)
579 * Store the first two Y buffers (we aren't supporting
580 * planar formats for now, so no UV bufs). Then either
581 * set the third if it exists, or tell the controller
584 cafe_reg_write(cam
, REG_Y0BAR
, cam
->dma_handles
[0]);
585 cafe_reg_write(cam
, REG_Y1BAR
, cam
->dma_handles
[1]);
586 if (cam
->nbufs
> 2) {
587 cafe_reg_write(cam
, REG_Y2BAR
, cam
->dma_handles
[2]);
588 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
591 cafe_reg_set_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
592 cafe_reg_write(cam
, REG_UBAR
, 0); /* 32 bits only for now */
595 static void cafe_ctlr_image(struct cafe_camera
*cam
)
598 struct v4l2_pix_format
*fmt
= &cam
->pix_format
;
600 imgsz
= ((fmt
->height
<< IMGSZ_V_SHIFT
) & IMGSZ_V_MASK
) |
601 (fmt
->bytesperline
& IMGSZ_H_MASK
);
602 cafe_reg_write(cam
, REG_IMGSIZE
, imgsz
);
603 cafe_reg_write(cam
, REG_IMGOFFSET
, 0);
604 /* YPITCH just drops the last two bits */
605 cafe_reg_write_mask(cam
, REG_IMGPITCH
, fmt
->bytesperline
,
608 * Tell the controller about the image format we are using.
610 switch (cam
->pix_format
.pixelformat
) {
611 case V4L2_PIX_FMT_YUYV
:
612 cafe_reg_write_mask(cam
, REG_CTRL0
,
613 C0_DF_YUV
|C0_YUV_PACKED
|C0_YUVE_YUYV
,
617 case V4L2_PIX_FMT_RGB444
:
618 cafe_reg_write_mask(cam
, REG_CTRL0
,
619 C0_DF_RGB
|C0_RGBF_444
|C0_RGB4_XRGB
,
624 case V4L2_PIX_FMT_RGB565
:
625 cafe_reg_write_mask(cam
, REG_CTRL0
,
626 C0_DF_RGB
|C0_RGBF_565
|C0_RGB5_BGGR
,
631 cam_err(cam
, "Unknown format %x\n", cam
->pix_format
.pixelformat
);
635 * Make sure it knows we want to use hsync/vsync.
637 cafe_reg_write_mask(cam
, REG_CTRL0
, C0_SIF_HVSYNC
,
643 * Configure the controller for operation; caller holds the
646 static int cafe_ctlr_configure(struct cafe_camera
*cam
)
650 spin_lock_irqsave(&cam
->dev_lock
, flags
);
652 cafe_ctlr_image(cam
);
653 cafe_set_config_needed(cam
, 0);
654 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
658 static void cafe_ctlr_irq_enable(struct cafe_camera
*cam
)
661 * Clear any pending interrupts, since we do not
662 * expect to have I/O active prior to enabling.
664 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
);
665 cafe_reg_set_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
668 static void cafe_ctlr_irq_disable(struct cafe_camera
*cam
)
670 cafe_reg_clear_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
674 * Make the controller start grabbing images. Everything must
675 * be set up before doing this.
677 static void cafe_ctlr_start(struct cafe_camera
*cam
)
679 /* set_bit performs a read, so no other barrier should be
681 cafe_reg_set_bit(cam
, REG_CTRL0
, C0_ENABLE
);
684 static void cafe_ctlr_stop(struct cafe_camera
*cam
)
686 cafe_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
689 static void cafe_ctlr_init(struct cafe_camera
*cam
)
693 spin_lock_irqsave(&cam
->dev_lock
, flags
);
695 * Added magic to bring up the hardware on the B-Test board
697 cafe_reg_write(cam
, 0x3038, 0x8);
698 cafe_reg_write(cam
, 0x315c, 0x80008);
700 * Go through the dance needed to wake the device up.
701 * Note that these registers are global and shared
702 * with the NAND and SD devices. Interaction between the
703 * three still needs to be examined.
705 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRS
|GCSR_MRS
); /* Needed? */
706 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRC
|GCSR_MRC
);
707 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRC
|GCSR_MRS
);
708 mdelay(5); /* FIXME revisit this */
709 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_CCIC_EN
|GCSR_SRC
|GCSR_MRC
);
710 cafe_reg_set_bit(cam
, REG_GL_IMASK
, GIMSK_CCIC_EN
);
712 * Make sure it's not powered down.
714 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
716 * Turn off the enable bit. It sure should be off anyway,
717 * but it's good to be sure.
719 cafe_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
721 * Mask all interrupts.
723 cafe_reg_write(cam
, REG_IRQMASK
, 0);
725 * Clock the sensor appropriately. Controller clock should
726 * be 48MHz, sensor "typical" value is half that.
728 cafe_reg_write_mask(cam
, REG_CLKCTRL
, 2, CLK_DIV_MASK
);
729 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
734 * Stop the controller, and don't return until we're really sure that no
735 * further DMA is going on.
737 static void cafe_ctlr_stop_dma(struct cafe_camera
*cam
)
742 * Theory: stop the camera controller (whether it is operating
743 * or not). Delay briefly just in case we race with the SOF
744 * interrupt, then wait until no DMA is active.
746 spin_lock_irqsave(&cam
->dev_lock
, flags
);
748 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
750 wait_event_timeout(cam
->iowait
,
751 !test_bit(CF_DMA_ACTIVE
, &cam
->flags
), HZ
);
752 if (test_bit(CF_DMA_ACTIVE
, &cam
->flags
))
753 cam_err(cam
, "Timeout waiting for DMA to end\n");
754 /* This would be bad news - what now? */
755 spin_lock_irqsave(&cam
->dev_lock
, flags
);
757 cafe_ctlr_irq_disable(cam
);
758 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
764 static void cafe_ctlr_power_up(struct cafe_camera
*cam
)
768 spin_lock_irqsave(&cam
->dev_lock
, flags
);
769 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
771 * Put the sensor into operational mode (assumes OLPC-style
772 * wiring). Control 0 is reset - set to 1 to operate.
773 * Control 1 is power down, set to 0 to operate.
775 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
); /* pwr up, reset */
776 mdelay(1); /* Marvell says 1ms will do it */
777 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
|GPR_C0
);
778 mdelay(1); /* Enough? */
779 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
782 static void cafe_ctlr_power_down(struct cafe_camera
*cam
)
786 spin_lock_irqsave(&cam
->dev_lock
, flags
);
787 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
|GPR_C1
);
788 cafe_reg_set_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
789 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
792 /* -------------------------------------------------------------------- */
794 * Communications with the sensor.
797 static int __cafe_cam_cmd(struct cafe_camera
*cam
, int cmd
, void *arg
)
799 struct i2c_client
*sc
= cam
->sensor
;
802 if (sc
== NULL
|| sc
->driver
== NULL
|| sc
->driver
->command
== NULL
)
804 ret
= sc
->driver
->command(sc
, cmd
, arg
);
805 if (ret
== -EPERM
) /* Unsupported command */
810 static int __cafe_cam_reset(struct cafe_camera
*cam
)
813 return __cafe_cam_cmd(cam
, VIDIOC_INT_RESET
, &zero
);
817 * We have found the sensor on the i2c. Let's try to have a
820 static int cafe_cam_init(struct cafe_camera
*cam
)
822 struct v4l2_chip_ident chip
= { V4L2_CHIP_MATCH_I2C_ADDR
, 0, 0, 0 };
825 mutex_lock(&cam
->s_mutex
);
826 if (cam
->state
!= S_NOTREADY
)
827 cam_warn(cam
, "Cam init with device in funky state %d",
829 ret
= __cafe_cam_reset(cam
);
832 chip
.match_chip
= cam
->sensor
->addr
;
833 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_CHIP_IDENT
, &chip
);
836 cam
->sensor_type
= chip
.ident
;
837 // if (cam->sensor->addr != OV7xx0_SID) {
838 if (cam
->sensor_type
!= V4L2_IDENT_OV7670
) {
839 cam_err(cam
, "Unsupported sensor type %d", cam
->sensor
->addr
);
843 /* Get/set parameters? */
847 mutex_unlock(&cam
->s_mutex
);
852 * Configure the sensor to match the parameters we have. Caller should
855 static int cafe_cam_set_flip(struct cafe_camera
*cam
)
857 struct v4l2_control ctrl
;
859 memset(&ctrl
, 0, sizeof(ctrl
));
860 ctrl
.id
= V4L2_CID_VFLIP
;
862 return __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, &ctrl
);
866 static int cafe_cam_configure(struct cafe_camera
*cam
)
868 struct v4l2_format fmt
;
871 if (cam
->state
!= S_IDLE
)
873 fmt
.fmt
.pix
= cam
->pix_format
;
874 ret
= __cafe_cam_cmd(cam
, VIDIOC_INT_INIT
, &zero
);
876 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_FMT
, &fmt
);
878 * OV7670 does weird things if flip is set *before* format...
880 ret
+= cafe_cam_set_flip(cam
);
884 /* -------------------------------------------------------------------- */
886 * DMA buffer management. These functions need s_mutex held.
889 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
890 * does a get_free_pages() call, and we waste a good chunk of an orderN
891 * allocation. Should try to allocate the whole set in one chunk.
893 static int cafe_alloc_dma_bufs(struct cafe_camera
*cam
, int loadtime
)
897 cafe_set_config_needed(cam
, 1);
899 cam
->dma_buf_size
= dma_buf_size
;
901 cam
->dma_buf_size
= cam
->pix_format
.sizeimage
;
906 for (i
= 0; i
< n_dma_bufs
; i
++) {
907 cam
->dma_bufs
[i
] = dma_alloc_coherent(&cam
->pdev
->dev
,
908 cam
->dma_buf_size
, cam
->dma_handles
+ i
,
910 if (cam
->dma_bufs
[i
] == NULL
) {
911 cam_warn(cam
, "Failed to allocate DMA buffer\n");
914 /* For debug, remove eventually */
915 memset(cam
->dma_bufs
[i
], 0xcc, cam
->dma_buf_size
);
919 switch (cam
->nbufs
) {
921 dma_free_coherent(&cam
->pdev
->dev
, cam
->dma_buf_size
,
922 cam
->dma_bufs
[0], cam
->dma_handles
[0]);
925 cam_err(cam
, "Insufficient DMA buffers, cannot operate\n");
930 cam_warn(cam
, "Will limp along with only 2 buffers\n");
936 static void cafe_free_dma_bufs(struct cafe_camera
*cam
)
940 for (i
= 0; i
< cam
->nbufs
; i
++) {
941 dma_free_coherent(&cam
->pdev
->dev
, cam
->dma_buf_size
,
942 cam
->dma_bufs
[i
], cam
->dma_handles
[i
]);
943 cam
->dma_bufs
[i
] = NULL
;
952 /* ----------------------------------------------------------------------- */
954 * Here starts the V4L2 interface code.
958 * Read an image from the device.
960 static ssize_t
cafe_deliver_buffer(struct cafe_camera
*cam
,
961 char __user
*buffer
, size_t len
, loff_t
*pos
)
966 spin_lock_irqsave(&cam
->dev_lock
, flags
);
967 if (cam
->next_buf
< 0) {
968 cam_err(cam
, "deliver_buffer: No next buffer\n");
969 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
972 bufno
= cam
->next_buf
;
973 clear_bit(bufno
, &cam
->flags
);
974 if (++(cam
->next_buf
) >= cam
->nbufs
)
976 if (! test_bit(cam
->next_buf
, &cam
->flags
))
979 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
981 if (len
> cam
->pix_format
.sizeimage
)
982 len
= cam
->pix_format
.sizeimage
;
983 if (copy_to_user(buffer
, cam
->dma_bufs
[bufno
], len
))
990 * Get everything ready, and start grabbing frames.
992 static int cafe_read_setup(struct cafe_camera
*cam
, enum cafe_state state
)
998 * Configuration. If we still don't have DMA buffers,
999 * make one last, desperate attempt.
1001 if (cam
->nbufs
== 0)
1002 if (cafe_alloc_dma_bufs(cam
, 0))
1005 if (cafe_needs_config(cam
)) {
1006 cafe_cam_configure(cam
);
1007 ret
= cafe_ctlr_configure(cam
);
1015 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1016 cafe_reset_buffers(cam
);
1017 cafe_ctlr_irq_enable(cam
);
1019 cafe_ctlr_start(cam
);
1020 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1025 static ssize_t
cafe_v4l_read(struct file
*filp
,
1026 char __user
*buffer
, size_t len
, loff_t
*pos
)
1028 struct cafe_camera
*cam
= filp
->private_data
;
1032 * Perhaps we're in speculative read mode and already
1035 mutex_lock(&cam
->s_mutex
);
1036 if (cam
->state
== S_SPECREAD
) {
1037 if (cam
->next_buf
>= 0) {
1038 ret
= cafe_deliver_buffer(cam
, buffer
, len
, pos
);
1042 } else if (cam
->state
== S_FLAKED
|| cam
->state
== S_NOTREADY
) {
1045 } else if (cam
->state
!= S_IDLE
) {
1051 * v4l2: multiple processes can open the device, but only
1052 * one gets to grab data from it.
1054 if (cam
->owner
&& cam
->owner
!= filp
) {
1061 * Do setup if need be.
1063 if (cam
->state
!= S_SPECREAD
) {
1064 ret
= cafe_read_setup(cam
, S_SINGLEREAD
);
1069 * Wait for something to happen. This should probably
1070 * be interruptible (FIXME).
1072 wait_event_timeout(cam
->iowait
, cam
->next_buf
>= 0, HZ
);
1073 if (cam
->next_buf
< 0) {
1074 cam_err(cam
, "read() operation timed out\n");
1075 cafe_ctlr_stop_dma(cam
);
1080 * Give them their data and we should be done.
1082 ret
= cafe_deliver_buffer(cam
, buffer
, len
, pos
);
1085 mutex_unlock(&cam
->s_mutex
);
1097 * Streaming I/O support.
1102 static int cafe_vidioc_streamon(struct file
*filp
, void *priv
,
1103 enum v4l2_buf_type type
)
1105 struct cafe_camera
*cam
= filp
->private_data
;
1108 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1110 mutex_lock(&cam
->s_mutex
);
1111 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
== 0)
1115 ret
= cafe_read_setup(cam
, S_STREAMING
);
1118 mutex_unlock(&cam
->s_mutex
);
1124 static int cafe_vidioc_streamoff(struct file
*filp
, void *priv
,
1125 enum v4l2_buf_type type
)
1127 struct cafe_camera
*cam
= filp
->private_data
;
1130 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1132 mutex_lock(&cam
->s_mutex
);
1133 if (cam
->state
!= S_STREAMING
)
1136 cafe_ctlr_stop_dma(cam
);
1140 mutex_unlock(&cam
->s_mutex
);
1147 static int cafe_setup_siobuf(struct cafe_camera
*cam
, int index
)
1149 struct cafe_sio_buffer
*buf
= cam
->sb_bufs
+ index
;
1151 INIT_LIST_HEAD(&buf
->list
);
1152 buf
->v4lbuf
.length
= PAGE_ALIGN(cam
->pix_format
.sizeimage
);
1153 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
1154 if (buf
->buffer
== NULL
)
1159 buf
->v4lbuf
.index
= index
;
1160 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1161 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
1162 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
1164 * Offset: must be 32-bit even on a 64-bit system. video-buf
1165 * just uses the length times the index, but the spec warns
1166 * against doing just that - vma merging problems. So we
1167 * leave a gap between each pair of buffers.
1169 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
1173 static int cafe_free_sio_buffers(struct cafe_camera
*cam
)
1178 * If any buffers are mapped, we cannot free them at all.
1180 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1181 if (cam
->sb_bufs
[i
].mapcount
> 0)
1186 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1187 vfree(cam
->sb_bufs
[i
].buffer
);
1189 kfree(cam
->sb_bufs
);
1190 cam
->sb_bufs
= NULL
;
1191 INIT_LIST_HEAD(&cam
->sb_avail
);
1192 INIT_LIST_HEAD(&cam
->sb_full
);
1198 static int cafe_vidioc_reqbufs(struct file
*filp
, void *priv
,
1199 struct v4l2_requestbuffers
*req
)
1201 struct cafe_camera
*cam
= filp
->private_data
;
1202 int ret
= 0; /* Silence warning */
1205 * Make sure it's something we can do. User pointers could be
1206 * implemented without great pain, but that's not been done yet.
1208 if (req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1210 if (req
->memory
!= V4L2_MEMORY_MMAP
)
1213 * If they ask for zero buffers, they really want us to stop streaming
1214 * (if it's happening) and free everything. Should we check owner?
1216 mutex_lock(&cam
->s_mutex
);
1217 if (req
->count
== 0) {
1218 if (cam
->state
== S_STREAMING
)
1219 cafe_ctlr_stop_dma(cam
);
1220 ret
= cafe_free_sio_buffers (cam
);
1224 * Device needs to be idle and working. We *could* try to do the
1225 * right thing in S_SPECREAD by shutting things down, but it
1226 * probably doesn't matter.
1228 if (cam
->state
!= S_IDLE
|| (cam
->owner
&& cam
->owner
!= filp
)) {
1234 if (req
->count
< min_buffers
)
1235 req
->count
= min_buffers
;
1236 else if (req
->count
> max_buffers
)
1237 req
->count
= max_buffers
;
1238 if (cam
->n_sbufs
> 0) {
1239 ret
= cafe_free_sio_buffers(cam
);
1244 cam
->sb_bufs
= kzalloc(req
->count
*sizeof(struct cafe_sio_buffer
),
1246 if (cam
->sb_bufs
== NULL
) {
1250 for (cam
->n_sbufs
= 0; cam
->n_sbufs
< req
->count
; (cam
->n_sbufs
++)) {
1251 ret
= cafe_setup_siobuf(cam
, cam
->n_sbufs
);
1256 if (cam
->n_sbufs
== 0) /* no luck at all - ret already set */
1257 kfree(cam
->sb_bufs
);
1258 req
->count
= cam
->n_sbufs
; /* In case of partial success */
1261 mutex_unlock(&cam
->s_mutex
);
1266 static int cafe_vidioc_querybuf(struct file
*filp
, void *priv
,
1267 struct v4l2_buffer
*buf
)
1269 struct cafe_camera
*cam
= filp
->private_data
;
1272 mutex_lock(&cam
->s_mutex
);
1273 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1275 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1277 *buf
= cam
->sb_bufs
[buf
->index
].v4lbuf
;
1280 mutex_unlock(&cam
->s_mutex
);
1284 static int cafe_vidioc_qbuf(struct file
*filp
, void *priv
,
1285 struct v4l2_buffer
*buf
)
1287 struct cafe_camera
*cam
= filp
->private_data
;
1288 struct cafe_sio_buffer
*sbuf
;
1290 unsigned long flags
;
1292 mutex_lock(&cam
->s_mutex
);
1293 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1295 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1297 sbuf
= cam
->sb_bufs
+ buf
->index
;
1298 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
) {
1299 ret
= 0; /* Already queued?? */
1302 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_DONE
) {
1303 /* Spec doesn't say anything, seems appropriate tho */
1307 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1308 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1309 list_add(&sbuf
->list
, &cam
->sb_avail
);
1310 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1313 mutex_unlock(&cam
->s_mutex
);
1317 static int cafe_vidioc_dqbuf(struct file
*filp
, void *priv
,
1318 struct v4l2_buffer
*buf
)
1320 struct cafe_camera
*cam
= filp
->private_data
;
1321 struct cafe_sio_buffer
*sbuf
;
1323 unsigned long flags
;
1325 mutex_lock(&cam
->s_mutex
);
1326 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1328 if (cam
->state
!= S_STREAMING
)
1330 if (list_empty(&cam
->sb_full
) && filp
->f_flags
& O_NONBLOCK
) {
1335 while (list_empty(&cam
->sb_full
) && cam
->state
== S_STREAMING
) {
1336 mutex_unlock(&cam
->s_mutex
);
1337 if (wait_event_interruptible(cam
->iowait
,
1338 !list_empty(&cam
->sb_full
))) {
1342 mutex_lock(&cam
->s_mutex
);
1345 if (cam
->state
!= S_STREAMING
)
1348 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1349 /* Should probably recheck !list_empty() here */
1350 sbuf
= list_entry(cam
->sb_full
.next
,
1351 struct cafe_sio_buffer
, list
);
1352 list_del_init(&sbuf
->list
);
1353 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1354 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1355 *buf
= sbuf
->v4lbuf
;
1360 mutex_unlock(&cam
->s_mutex
);
1367 static void cafe_v4l_vm_open(struct vm_area_struct
*vma
)
1369 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1371 * Locking: done under mmap_sem, so we don't need to
1372 * go back to the camera lock here.
1378 static void cafe_v4l_vm_close(struct vm_area_struct
*vma
)
1380 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1382 mutex_lock(&sbuf
->cam
->s_mutex
);
1384 /* Docs say we should stop I/O too... */
1385 if (sbuf
->mapcount
== 0)
1386 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
1387 mutex_unlock(&sbuf
->cam
->s_mutex
);
1390 static struct vm_operations_struct cafe_v4l_vm_ops
= {
1391 .open
= cafe_v4l_vm_open
,
1392 .close
= cafe_v4l_vm_close
1396 static int cafe_v4l_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1398 struct cafe_camera
*cam
= filp
->private_data
;
1399 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1402 struct cafe_sio_buffer
*sbuf
= NULL
;
1404 if (! (vma
->vm_flags
& VM_WRITE
) || ! (vma
->vm_flags
& VM_SHARED
))
1407 * Find the buffer they are looking for.
1409 mutex_lock(&cam
->s_mutex
);
1410 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1411 if (cam
->sb_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
1412 sbuf
= cam
->sb_bufs
+ i
;
1418 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
1421 vma
->vm_flags
|= VM_DONTEXPAND
;
1422 vma
->vm_private_data
= sbuf
;
1423 vma
->vm_ops
= &cafe_v4l_vm_ops
;
1424 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
1425 cafe_v4l_vm_open(vma
);
1428 mutex_unlock(&cam
->s_mutex
);
1434 static int cafe_v4l_open(struct inode
*inode
, struct file
*filp
)
1436 struct cafe_camera
*cam
;
1438 cam
= cafe_find_dev(iminor(inode
));
1441 filp
->private_data
= cam
;
1443 mutex_lock(&cam
->s_mutex
);
1444 if (cam
->users
== 0) {
1445 cafe_ctlr_power_up(cam
);
1446 __cafe_cam_reset(cam
);
1447 cafe_set_config_needed(cam
, 1);
1448 /* FIXME make sure this is complete */
1451 mutex_unlock(&cam
->s_mutex
);
1456 static int cafe_v4l_release(struct inode
*inode
, struct file
*filp
)
1458 struct cafe_camera
*cam
= filp
->private_data
;
1460 mutex_lock(&cam
->s_mutex
);
1462 if (filp
== cam
->owner
) {
1463 cafe_ctlr_stop_dma(cam
);
1464 cafe_free_sio_buffers(cam
);
1467 if (cam
->users
== 0) {
1468 cafe_ctlr_power_down(cam
);
1469 if (! alloc_bufs_at_load
)
1470 cafe_free_dma_bufs(cam
);
1472 mutex_unlock(&cam
->s_mutex
);
1478 static unsigned int cafe_v4l_poll(struct file
*filp
,
1479 struct poll_table_struct
*pt
)
1481 struct cafe_camera
*cam
= filp
->private_data
;
1483 poll_wait(filp
, &cam
->iowait
, pt
);
1484 if (cam
->next_buf
>= 0)
1485 return POLLIN
| POLLRDNORM
;
1491 static int cafe_vidioc_queryctrl(struct file
*filp
, void *priv
,
1492 struct v4l2_queryctrl
*qc
)
1494 struct cafe_camera
*cam
= filp
->private_data
;
1497 mutex_lock(&cam
->s_mutex
);
1498 ret
= __cafe_cam_cmd(cam
, VIDIOC_QUERYCTRL
, qc
);
1499 mutex_unlock(&cam
->s_mutex
);
1504 static int cafe_vidioc_g_ctrl(struct file
*filp
, void *priv
,
1505 struct v4l2_control
*ctrl
)
1507 struct cafe_camera
*cam
= filp
->private_data
;
1510 mutex_lock(&cam
->s_mutex
);
1511 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_CTRL
, ctrl
);
1512 mutex_unlock(&cam
->s_mutex
);
1517 static int cafe_vidioc_s_ctrl(struct file
*filp
, void *priv
,
1518 struct v4l2_control
*ctrl
)
1520 struct cafe_camera
*cam
= filp
->private_data
;
1523 mutex_lock(&cam
->s_mutex
);
1524 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, ctrl
);
1525 mutex_unlock(&cam
->s_mutex
);
1533 static int cafe_vidioc_querycap(struct file
*file
, void *priv
,
1534 struct v4l2_capability
*cap
)
1536 strcpy(cap
->driver
, "cafe_ccic");
1537 strcpy(cap
->card
, "cafe_ccic");
1538 cap
->version
= CAFE_VERSION
;
1539 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
1540 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1546 * The default format we use until somebody says otherwise.
1548 static struct v4l2_pix_format cafe_def_pix_format
= {
1550 .height
= VGA_HEIGHT
,
1551 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1552 .field
= V4L2_FIELD_NONE
,
1553 .bytesperline
= VGA_WIDTH
*2,
1554 .sizeimage
= VGA_WIDTH
*VGA_HEIGHT
*2,
1557 static int cafe_vidioc_enum_fmt_cap(struct file
*filp
,
1558 void *priv
, struct v4l2_fmtdesc
*fmt
)
1560 struct cafe_camera
*cam
= priv
;
1563 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1565 mutex_lock(&cam
->s_mutex
);
1566 ret
= __cafe_cam_cmd(cam
, VIDIOC_ENUM_FMT
, fmt
);
1567 mutex_unlock(&cam
->s_mutex
);
1572 static int cafe_vidioc_try_fmt_cap (struct file
*filp
, void *priv
,
1573 struct v4l2_format
*fmt
)
1575 struct cafe_camera
*cam
= priv
;
1578 mutex_lock(&cam
->s_mutex
);
1579 ret
= __cafe_cam_cmd(cam
, VIDIOC_TRY_FMT
, fmt
);
1580 mutex_unlock(&cam
->s_mutex
);
1584 static int cafe_vidioc_s_fmt_cap(struct file
*filp
, void *priv
,
1585 struct v4l2_format
*fmt
)
1587 struct cafe_camera
*cam
= priv
;
1591 * Can't do anything if the device is not idle
1592 * Also can't if there are streaming buffers in place.
1594 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
> 0)
1597 * See if the formatting works in principle.
1599 ret
= cafe_vidioc_try_fmt_cap(filp
, priv
, fmt
);
1603 * Now we start to change things for real, so let's do it
1606 mutex_lock(&cam
->s_mutex
);
1607 cam
->pix_format
= fmt
->fmt
.pix
;
1609 * Make sure we have appropriate DMA buffers.
1612 if (cam
->nbufs
> 0 && cam
->dma_buf_size
< cam
->pix_format
.sizeimage
)
1613 cafe_free_dma_bufs(cam
);
1614 if (cam
->nbufs
== 0) {
1615 if (cafe_alloc_dma_bufs(cam
, 0))
1619 * It looks like this might work, so let's program the sensor.
1621 ret
= cafe_cam_configure(cam
);
1623 ret
= cafe_ctlr_configure(cam
);
1625 mutex_unlock(&cam
->s_mutex
);
1630 * Return our stored notion of how the camera is/should be configured.
1631 * The V4l2 spec wants us to be smarter, and actually get this from
1632 * the camera (and not mess with it at open time). Someday.
1634 static int cafe_vidioc_g_fmt_cap(struct file
*filp
, void *priv
,
1635 struct v4l2_format
*f
)
1637 struct cafe_camera
*cam
= priv
;
1639 f
->fmt
.pix
= cam
->pix_format
;
1644 * We only have one input - the sensor - so minimize the nonsense here.
1646 static int cafe_vidioc_enum_input(struct file
*filp
, void *priv
,
1647 struct v4l2_input
*input
)
1649 if (input
->index
!= 0)
1652 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1653 input
->std
= V4L2_STD_ALL
; /* Not sure what should go here */
1654 strcpy(input
->name
, "Camera");
1658 static int cafe_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
1664 static int cafe_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1672 static int cafe_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
1678 * G/S_PARM. Most of this is done by the sensor, but we are
1679 * the level which controls the number of read buffers.
1681 static int cafe_vidioc_g_parm(struct file
*filp
, void *priv
,
1682 struct v4l2_streamparm
*parms
)
1684 struct cafe_camera
*cam
= priv
;
1687 mutex_lock(&cam
->s_mutex
);
1688 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_PARM
, parms
);
1689 mutex_unlock(&cam
->s_mutex
);
1690 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1694 static int cafe_vidioc_s_parm(struct file
*filp
, void *priv
,
1695 struct v4l2_streamparm
*parms
)
1697 struct cafe_camera
*cam
= priv
;
1700 mutex_lock(&cam
->s_mutex
);
1701 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_PARM
, parms
);
1702 mutex_unlock(&cam
->s_mutex
);
1703 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1708 static void cafe_v4l_dev_release(struct video_device
*vd
)
1710 struct cafe_camera
*cam
= container_of(vd
, struct cafe_camera
, v4ldev
);
1717 * This template device holds all of those v4l2 methods; we
1718 * clone it for specific real devices.
1721 static const struct file_operations cafe_v4l_fops
= {
1722 .owner
= THIS_MODULE
,
1723 .open
= cafe_v4l_open
,
1724 .release
= cafe_v4l_release
,
1725 .read
= cafe_v4l_read
,
1726 .poll
= cafe_v4l_poll
,
1727 .mmap
= cafe_v4l_mmap
,
1728 .ioctl
= video_ioctl2
,
1729 .llseek
= no_llseek
,
1732 static struct video_device cafe_v4l_template
= {
1734 .type
= VFL_TYPE_GRABBER
,
1735 .type2
= VID_TYPE_CAPTURE
,
1736 .minor
= -1, /* Get one dynamically */
1737 .tvnorms
= V4L2_STD_NTSC_M
,
1738 .current_norm
= V4L2_STD_NTSC_M
, /* make mplayer happy */
1740 .fops
= &cafe_v4l_fops
,
1741 .release
= cafe_v4l_dev_release
,
1743 .vidioc_querycap
= cafe_vidioc_querycap
,
1744 .vidioc_enum_fmt_cap
= cafe_vidioc_enum_fmt_cap
,
1745 .vidioc_try_fmt_cap
= cafe_vidioc_try_fmt_cap
,
1746 .vidioc_s_fmt_cap
= cafe_vidioc_s_fmt_cap
,
1747 .vidioc_g_fmt_cap
= cafe_vidioc_g_fmt_cap
,
1748 .vidioc_enum_input
= cafe_vidioc_enum_input
,
1749 .vidioc_g_input
= cafe_vidioc_g_input
,
1750 .vidioc_s_input
= cafe_vidioc_s_input
,
1751 .vidioc_s_std
= cafe_vidioc_s_std
,
1752 .vidioc_reqbufs
= cafe_vidioc_reqbufs
,
1753 .vidioc_querybuf
= cafe_vidioc_querybuf
,
1754 .vidioc_qbuf
= cafe_vidioc_qbuf
,
1755 .vidioc_dqbuf
= cafe_vidioc_dqbuf
,
1756 .vidioc_streamon
= cafe_vidioc_streamon
,
1757 .vidioc_streamoff
= cafe_vidioc_streamoff
,
1758 .vidioc_queryctrl
= cafe_vidioc_queryctrl
,
1759 .vidioc_g_ctrl
= cafe_vidioc_g_ctrl
,
1760 .vidioc_s_ctrl
= cafe_vidioc_s_ctrl
,
1761 .vidioc_g_parm
= cafe_vidioc_g_parm
,
1762 .vidioc_s_parm
= cafe_vidioc_s_parm
,
1771 /* ---------------------------------------------------------------------- */
1773 * Interrupt handler stuff
1778 static void cafe_frame_tasklet(unsigned long data
)
1780 struct cafe_camera
*cam
= (struct cafe_camera
*) data
;
1782 unsigned long flags
;
1783 struct cafe_sio_buffer
*sbuf
;
1785 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1786 for (i
= 0; i
< cam
->nbufs
; i
++) {
1787 int bufno
= cam
->next_buf
;
1788 if (bufno
< 0) { /* "will never happen" */
1789 cam_err(cam
, "No valid bufs in tasklet!\n");
1792 if (++(cam
->next_buf
) >= cam
->nbufs
)
1794 if (! test_bit(bufno
, &cam
->flags
))
1796 if (list_empty(&cam
->sb_avail
))
1797 break; /* Leave it valid, hope for better later */
1798 clear_bit(bufno
, &cam
->flags
);
1800 * We could perhaps drop the spinlock during this
1801 * big copy. Something to consider.
1803 sbuf
= list_entry(cam
->sb_avail
.next
,
1804 struct cafe_sio_buffer
, list
);
1805 memcpy(sbuf
->buffer
, cam
->dma_bufs
[bufno
],
1806 cam
->pix_format
.sizeimage
);
1807 sbuf
->v4lbuf
.bytesused
= cam
->pix_format
.sizeimage
;
1808 sbuf
->v4lbuf
.sequence
= cam
->buf_seq
[bufno
];
1809 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1810 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1811 list_move_tail(&sbuf
->list
, &cam
->sb_full
);
1813 if (! list_empty(&cam
->sb_full
))
1814 wake_up(&cam
->iowait
);
1815 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1820 static void cafe_frame_complete(struct cafe_camera
*cam
, int frame
)
1823 * Basic frame housekeeping.
1825 if (test_bit(frame
, &cam
->flags
) && printk_ratelimit())
1826 cam_err(cam
, "Frame overrun on %d, frames lost\n", frame
);
1827 set_bit(frame
, &cam
->flags
);
1828 clear_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1829 if (cam
->next_buf
< 0)
1830 cam
->next_buf
= frame
;
1831 cam
->buf_seq
[frame
] = ++(cam
->sequence
);
1833 switch (cam
->state
) {
1835 * If in single read mode, try going speculative.
1838 cam
->state
= S_SPECREAD
;
1839 cam
->specframes
= 0;
1840 wake_up(&cam
->iowait
);
1844 * If we are already doing speculative reads, and nobody is
1845 * reading them, just stop.
1848 if (++(cam
->specframes
) >= cam
->nbufs
) {
1849 cafe_ctlr_stop(cam
);
1850 cafe_ctlr_irq_disable(cam
);
1851 cam
->state
= S_IDLE
;
1853 wake_up(&cam
->iowait
);
1856 * For the streaming case, we defer the real work to the
1859 * FIXME: if the application is not consuming the buffers,
1860 * we should eventually put things on hold and restart in
1864 tasklet_schedule(&cam
->s_tasklet
);
1868 cam_err(cam
, "Frame interrupt in non-operational state\n");
1876 static void cafe_frame_irq(struct cafe_camera
*cam
, unsigned int irqs
)
1880 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
); /* Clear'em all */
1882 * Handle any frame completions. There really should
1883 * not be more than one of these, or we have fallen
1886 for (frame
= 0; frame
< cam
->nbufs
; frame
++)
1887 if (irqs
& (IRQ_EOF0
<< frame
))
1888 cafe_frame_complete(cam
, frame
);
1890 * If a frame starts, note that we have DMA active. This
1891 * code assumes that we won't get multiple frame interrupts
1892 * at once; may want to rethink that.
1894 if (irqs
& (IRQ_SOF0
| IRQ_SOF1
| IRQ_SOF2
))
1895 set_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1900 static irqreturn_t
cafe_irq(int irq
, void *data
)
1902 struct cafe_camera
*cam
= data
;
1905 spin_lock(&cam
->dev_lock
);
1906 irqs
= cafe_reg_read(cam
, REG_IRQSTAT
);
1907 if ((irqs
& ALLIRQS
) == 0) {
1908 spin_unlock(&cam
->dev_lock
);
1911 if (irqs
& FRAMEIRQS
)
1912 cafe_frame_irq(cam
, irqs
);
1913 if (irqs
& TWSIIRQS
) {
1914 cafe_reg_write(cam
, REG_IRQSTAT
, TWSIIRQS
);
1915 wake_up(&cam
->smbus_wait
);
1917 spin_unlock(&cam
->dev_lock
);
1922 /* -------------------------------------------------------------------------- */
1923 #ifdef CONFIG_VIDEO_ADV_DEBUG
1928 static char cafe_debug_buf
[1024];
1929 static struct dentry
*cafe_dfs_root
;
1931 static void cafe_dfs_setup(void)
1933 cafe_dfs_root
= debugfs_create_dir("cafe_ccic", NULL
);
1934 if (IS_ERR(cafe_dfs_root
)) {
1935 cafe_dfs_root
= NULL
; /* Never mind */
1936 printk(KERN_NOTICE
"cafe_ccic unable to set up debugfs\n");
1940 static void cafe_dfs_shutdown(void)
1943 debugfs_remove(cafe_dfs_root
);
1946 static int cafe_dfs_open(struct inode
*inode
, struct file
*file
)
1948 file
->private_data
= inode
->i_private
;
1952 static ssize_t
cafe_dfs_read_regs(struct file
*file
,
1953 char __user
*buf
, size_t count
, loff_t
*ppos
)
1955 struct cafe_camera
*cam
= file
->private_data
;
1956 char *s
= cafe_debug_buf
;
1959 for (offset
= 0; offset
< 0x44; offset
+= 4)
1960 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1961 cafe_reg_read(cam
, offset
));
1962 for (offset
= 0x88; offset
<= 0x90; offset
+= 4)
1963 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1964 cafe_reg_read(cam
, offset
));
1965 for (offset
= 0xb4; offset
<= 0xbc; offset
+= 4)
1966 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1967 cafe_reg_read(cam
, offset
));
1968 for (offset
= 0x3000; offset
<= 0x300c; offset
+= 4)
1969 s
+= sprintf(s
, "%04x: %08x\n", offset
,
1970 cafe_reg_read(cam
, offset
));
1971 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1972 s
- cafe_debug_buf
);
1975 static const struct file_operations cafe_dfs_reg_ops
= {
1976 .owner
= THIS_MODULE
,
1977 .read
= cafe_dfs_read_regs
,
1978 .open
= cafe_dfs_open
1981 static ssize_t
cafe_dfs_read_cam(struct file
*file
,
1982 char __user
*buf
, size_t count
, loff_t
*ppos
)
1984 struct cafe_camera
*cam
= file
->private_data
;
1985 char *s
= cafe_debug_buf
;
1990 for (offset
= 0x0; offset
< 0x8a; offset
++)
1994 cafe_smbus_read_data(cam
, cam
->sensor
->addr
, offset
, &v
);
1995 s
+= sprintf(s
, "%02x: %02x\n", offset
, v
);
1997 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1998 s
- cafe_debug_buf
);
2001 static const struct file_operations cafe_dfs_cam_ops
= {
2002 .owner
= THIS_MODULE
,
2003 .read
= cafe_dfs_read_cam
,
2004 .open
= cafe_dfs_open
2009 static void cafe_dfs_cam_setup(struct cafe_camera
*cam
)
2015 sprintf(fname
, "regs-%d", cam
->v4ldev
.minor
);
2016 cam
->dfs_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2017 cam
, &cafe_dfs_reg_ops
);
2018 sprintf(fname
, "cam-%d", cam
->v4ldev
.minor
);
2019 cam
->dfs_cam_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2020 cam
, &cafe_dfs_cam_ops
);
2024 static void cafe_dfs_cam_shutdown(struct cafe_camera
*cam
)
2026 if (! IS_ERR(cam
->dfs_regs
))
2027 debugfs_remove(cam
->dfs_regs
);
2028 if (! IS_ERR(cam
->dfs_cam_regs
))
2029 debugfs_remove(cam
->dfs_cam_regs
);
2034 #define cafe_dfs_setup()
2035 #define cafe_dfs_shutdown()
2036 #define cafe_dfs_cam_setup(cam)
2037 #define cafe_dfs_cam_shutdown(cam)
2038 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2043 /* ------------------------------------------------------------------------*/
2045 * PCI interface stuff.
2048 static int cafe_pci_probe(struct pci_dev
*pdev
,
2049 const struct pci_device_id
*id
)
2053 struct cafe_camera
*cam
;
2055 * Make sure we have a camera here - we'll get calls for
2056 * the other cafe devices as well.
2058 pci_read_config_word(pdev
, PCI_CLASS_DEVICE
, &classword
);
2059 if (classword
!= PCI_CLASS_MULTIMEDIA_VIDEO
)
2062 * Start putting together one of our big camera structures.
2065 cam
= kzalloc(sizeof(struct cafe_camera
), GFP_KERNEL
);
2068 mutex_init(&cam
->s_mutex
);
2069 mutex_lock(&cam
->s_mutex
);
2070 spin_lock_init(&cam
->dev_lock
);
2071 cam
->state
= S_NOTREADY
;
2072 cafe_set_config_needed(cam
, 1);
2073 init_waitqueue_head(&cam
->smbus_wait
);
2074 init_waitqueue_head(&cam
->iowait
);
2076 cam
->pix_format
= cafe_def_pix_format
;
2077 INIT_LIST_HEAD(&cam
->dev_list
);
2078 INIT_LIST_HEAD(&cam
->sb_avail
);
2079 INIT_LIST_HEAD(&cam
->sb_full
);
2080 tasklet_init(&cam
->s_tasklet
, cafe_frame_tasklet
, (unsigned long) cam
);
2082 * Get set up on the PCI bus.
2084 ret
= pci_enable_device(pdev
);
2087 pci_set_master(pdev
);
2090 cam
->regs
= pci_iomap(pdev
, 0, 0);
2092 printk(KERN_ERR
"Unable to ioremap cafe-ccic regs\n");
2095 ret
= request_irq(pdev
->irq
, cafe_irq
, IRQF_SHARED
, "cafe-ccic", cam
);
2098 cafe_ctlr_init(cam
);
2099 cafe_ctlr_power_up(cam
);
2101 * Set up I2C/SMBUS communications
2103 mutex_unlock(&cam
->s_mutex
); /* attach can deadlock */
2104 ret
= cafe_smbus_setup(cam
);
2108 * Get the v4l2 setup done.
2110 mutex_lock(&cam
->s_mutex
);
2111 cam
->v4ldev
= cafe_v4l_template
;
2112 cam
->v4ldev
.debug
= 0;
2113 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2114 cam
->v4ldev
.dev
= &pdev
->dev
;
2115 ret
= video_register_device(&cam
->v4ldev
, VFL_TYPE_GRABBER
, -1);
2119 * If so requested, try to get our DMA buffers now.
2121 if (alloc_bufs_at_load
) {
2122 if (cafe_alloc_dma_bufs(cam
, 1))
2123 cam_warn(cam
, "Unable to alloc DMA buffers at load"
2124 " will try again later.");
2127 cafe_dfs_cam_setup(cam
);
2128 mutex_unlock(&cam
->s_mutex
);
2133 cafe_smbus_shutdown(cam
);
2135 cafe_ctlr_power_down(cam
);
2136 free_irq(pdev
->irq
, cam
);
2138 pci_iounmap(pdev
, cam
->regs
);
2147 * Shut down an initialized device
2149 static void cafe_shutdown(struct cafe_camera
*cam
)
2151 /* FIXME: Make sure we take care of everything here */
2152 cafe_dfs_cam_shutdown(cam
);
2153 if (cam
->n_sbufs
> 0)
2154 /* What if they are still mapped? Shouldn't be, but... */
2155 cafe_free_sio_buffers(cam
);
2156 cafe_remove_dev(cam
);
2157 cafe_ctlr_stop_dma(cam
);
2158 cafe_ctlr_power_down(cam
);
2159 cafe_smbus_shutdown(cam
);
2160 cafe_free_dma_bufs(cam
);
2161 free_irq(cam
->pdev
->irq
, cam
);
2162 pci_iounmap(cam
->pdev
, cam
->regs
);
2163 video_unregister_device(&cam
->v4ldev
);
2164 /* kfree(cam); done in v4l_release () */
2168 static void cafe_pci_remove(struct pci_dev
*pdev
)
2170 struct cafe_camera
*cam
= cafe_find_by_pdev(pdev
);
2173 printk(KERN_WARNING
"pci_remove on unknown pdev %p\n", pdev
);
2176 mutex_lock(&cam
->s_mutex
);
2178 cam_warn(cam
, "Removing a device with users!\n");
2180 /* No unlock - it no longer exists */
2186 static struct pci_device_id cafe_ids
[] = {
2187 { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
2188 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2189 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2193 MODULE_DEVICE_TABLE(pci
, cafe_ids
);
2195 static struct pci_driver cafe_pci_driver
= {
2196 .name
= "cafe1000-ccic",
2197 .id_table
= cafe_ids
,
2198 .probe
= cafe_pci_probe
,
2199 .remove
= cafe_pci_remove
,
2205 static int __init
cafe_init(void)
2209 printk(KERN_NOTICE
"Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2212 ret
= pci_register_driver(&cafe_pci_driver
);
2214 printk(KERN_ERR
"Unable to register cafe_ccic driver\n");
2217 request_module("ov7670"); /* FIXME want something more general */
2225 static void __exit
cafe_exit(void)
2227 pci_unregister_driver(&cafe_pci_driver
);
2228 cafe_dfs_shutdown();
2231 module_init(cafe_init
);
2232 module_exit(cafe_exit
);