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 <linux/device.h>
26 #include <linux/wait.h>
27 #include <linux/list.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/delay.h>
30 #include <linux/debugfs.h>
31 #include <linux/jiffies.h>
32 #include <linux/vmalloc.h>
34 #include <asm/uaccess.h>
37 #include "cafe_ccic-regs.h"
39 #define CAFE_VERSION 0x000001
45 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
46 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("Video");
51 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
52 * we must have physically contiguous buffers to bring frames into.
53 * These parameters control how many buffers we use, whether we
54 * allocate them at load time (better chance of success, but nails down
55 * memory) or when somebody tries to use the camera (riskier), and,
56 * for load-time allocation, how big they should be.
58 * The controller can cycle through three buffers. We could use
59 * more by flipping pointers around, but it probably makes little
63 #define MAX_DMA_BUFS 3
64 static int alloc_bufs_at_load
= 0;
65 module_param(alloc_bufs_at_load
, bool, 0444);
66 MODULE_PARM_DESC(alloc_bufs_at_load
,
67 "Non-zero value causes DMA buffers to be allocated at module "
68 "load time. This increases the chances of successfully getting "
69 "those buffers, but at the cost of nailing down the memory from "
72 static int n_dma_bufs
= 3;
73 module_param(n_dma_bufs
, uint
, 0644);
74 MODULE_PARM_DESC(n_dma_bufs
,
75 "The number of DMA buffers to allocate. Can be either two "
76 "(saves memory, makes timing tighter) or three.");
78 static int dma_buf_size
= VGA_WIDTH
* VGA_HEIGHT
* 2; /* Worst case */
79 module_param(dma_buf_size
, uint
, 0444);
80 MODULE_PARM_DESC(dma_buf_size
,
81 "The size of the allocated DMA buffers. If actual operating "
82 "parameters require larger buffers, an attempt to reallocate "
85 static int min_buffers
= 1;
86 module_param(min_buffers
, uint
, 0644);
87 MODULE_PARM_DESC(min_buffers
,
88 "The minimum number of streaming I/O buffers we are willing "
91 static int max_buffers
= 10;
92 module_param(max_buffers
, uint
, 0644);
93 MODULE_PARM_DESC(max_buffers
,
94 "The maximum number of streaming I/O buffers an application "
95 "will be allowed to allocate. These buffers are big and live "
99 module_param(flip
, bool, 0444);
100 MODULE_PARM_DESC(flip
,
101 "If set, the sensor will be instructed to flip the image "
106 S_NOTREADY
, /* Not yet initialized */
107 S_IDLE
, /* Just hanging around */
108 S_FLAKED
, /* Some sort of problem */
109 S_SINGLEREAD
, /* In read() */
110 S_SPECREAD
, /* Speculative read (for future read()) */
111 S_STREAMING
/* Streaming data */
115 * Tracking of streaming I/O buffers.
117 struct cafe_sio_buffer
{
118 struct list_head list
;
119 struct v4l2_buffer v4lbuf
;
120 char *buffer
; /* Where it lives in kernel space */
122 struct cafe_camera
*cam
;
126 * A description of one of our devices.
127 * Locking: controlled by s_mutex. Certain fields, however, require
128 * the dev_lock spinlock; they are marked as such by comments.
129 * dev_lock is also required for access to device registers.
133 enum cafe_state state
;
134 unsigned long flags
; /* Buffer status, mainly (dev_lock) */
135 int users
; /* How many open FDs */
136 struct file
*owner
; /* Who has data access (v4l2) */
139 * Subsystem structures.
141 struct pci_dev
*pdev
;
142 struct video_device v4ldev
;
143 struct i2c_adapter i2c_adapter
;
144 struct i2c_client
*sensor
;
146 unsigned char __iomem
*regs
;
147 struct list_head dev_list
; /* link to other devices */
150 unsigned int nbufs
; /* How many are alloc'd */
151 int next_buf
; /* Next to consume (dev_lock) */
152 unsigned int dma_buf_size
; /* allocated size */
153 void *dma_bufs
[MAX_DMA_BUFS
]; /* Internal buffer addresses */
154 dma_addr_t dma_handles
[MAX_DMA_BUFS
]; /* Buffer bus addresses */
155 unsigned int specframes
; /* Unconsumed spec frames (dev_lock) */
156 unsigned int sequence
; /* Frame sequence number */
157 unsigned int buf_seq
[MAX_DMA_BUFS
]; /* Sequence for individual buffers */
159 /* Streaming buffers */
160 unsigned int n_sbufs
; /* How many we have */
161 struct cafe_sio_buffer
*sb_bufs
; /* The array of housekeeping structs */
162 struct list_head sb_avail
; /* Available for data (we own) (dev_lock) */
163 struct list_head sb_full
; /* With data (user space owns) (dev_lock) */
164 struct tasklet_struct s_tasklet
;
166 /* Current operating parameters */
167 enum v4l2_chip_ident sensor_type
; /* Currently ov7670 only */
168 struct v4l2_pix_format pix_format
;
171 struct mutex s_mutex
; /* Access to this structure */
172 spinlock_t dev_lock
; /* Access to device */
175 wait_queue_head_t smbus_wait
; /* Waiting on i2c events */
176 wait_queue_head_t iowait
; /* Waiting on frame data */
177 #ifdef CONFIG_VIDEO_ADV_DEBUG
178 struct dentry
*dfs_regs
;
179 struct dentry
*dfs_cam_regs
;
184 * Status flags. Always manipulated with bit operations.
186 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
187 #define CF_BUF1_VALID 1
188 #define CF_BUF2_VALID 2
189 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
190 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
195 * Start over with DMA buffers - dev_lock needed.
197 static void cafe_reset_buffers(struct cafe_camera
*cam
)
202 for (i
= 0; i
< cam
->nbufs
; i
++)
203 clear_bit(i
, &cam
->flags
);
207 static inline int cafe_needs_config(struct cafe_camera
*cam
)
209 return test_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
212 static void cafe_set_config_needed(struct cafe_camera
*cam
, int needed
)
215 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
217 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
224 * Debugging and related.
226 #define cam_err(cam, fmt, arg...) \
227 dev_err(&(cam)->pdev->dev, fmt, ##arg);
228 #define cam_warn(cam, fmt, arg...) \
229 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
230 #define cam_dbg(cam, fmt, arg...) \
231 dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
234 /* ---------------------------------------------------------------------*/
236 * We keep a simple list of known devices to search at open time.
238 static LIST_HEAD(cafe_dev_list
);
239 static DEFINE_MUTEX(cafe_dev_list_lock
);
241 static void cafe_add_dev(struct cafe_camera
*cam
)
243 mutex_lock(&cafe_dev_list_lock
);
244 list_add_tail(&cam
->dev_list
, &cafe_dev_list
);
245 mutex_unlock(&cafe_dev_list_lock
);
248 static void cafe_remove_dev(struct cafe_camera
*cam
)
250 mutex_lock(&cafe_dev_list_lock
);
251 list_del(&cam
->dev_list
);
252 mutex_unlock(&cafe_dev_list_lock
);
255 static struct cafe_camera
*cafe_find_dev(int minor
)
257 struct cafe_camera
*cam
;
259 mutex_lock(&cafe_dev_list_lock
);
260 list_for_each_entry(cam
, &cafe_dev_list
, dev_list
) {
261 if (cam
->v4ldev
.minor
== minor
)
266 mutex_unlock(&cafe_dev_list_lock
);
271 static struct cafe_camera
*cafe_find_by_pdev(struct pci_dev
*pdev
)
273 struct cafe_camera
*cam
;
275 mutex_lock(&cafe_dev_list_lock
);
276 list_for_each_entry(cam
, &cafe_dev_list
, dev_list
) {
277 if (cam
->pdev
== pdev
)
282 mutex_unlock(&cafe_dev_list_lock
);
287 /* ------------------------------------------------------------------------ */
289 * Device register I/O
291 static inline void cafe_reg_write(struct cafe_camera
*cam
, unsigned int reg
,
294 iowrite32(val
, cam
->regs
+ reg
);
297 static inline unsigned int cafe_reg_read(struct cafe_camera
*cam
,
300 return ioread32(cam
->regs
+ reg
);
304 static inline void cafe_reg_write_mask(struct cafe_camera
*cam
, unsigned int reg
,
305 unsigned int val
, unsigned int mask
)
307 unsigned int v
= cafe_reg_read(cam
, reg
);
309 v
= (v
& ~mask
) | (val
& mask
);
310 cafe_reg_write(cam
, reg
, v
);
313 static inline void cafe_reg_clear_bit(struct cafe_camera
*cam
,
314 unsigned int reg
, unsigned int val
)
316 cafe_reg_write_mask(cam
, reg
, 0, val
);
319 static inline void cafe_reg_set_bit(struct cafe_camera
*cam
,
320 unsigned int reg
, unsigned int val
)
322 cafe_reg_write_mask(cam
, reg
, val
, val
);
327 /* -------------------------------------------------------------------- */
329 * The I2C/SMBUS interface to the camera itself starts here. The
330 * controller handles SMBUS itself, presenting a relatively simple register
331 * interface; all we have to do is to tell it where to route the data.
333 #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
335 static int cafe_smbus_write_done(struct cafe_camera
*cam
)
341 * We must delay after the interrupt, or the controller gets confused
342 * and never does give us good status. Fortunately, we don't do this
346 spin_lock_irqsave(&cam
->dev_lock
, flags
);
347 c1
= cafe_reg_read(cam
, REG_TWSIC1
);
348 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
349 return (c1
& (TWSIC1_WSTAT
|TWSIC1_ERROR
)) != TWSIC1_WSTAT
;
352 static int cafe_smbus_write_data(struct cafe_camera
*cam
,
353 u16 addr
, u8 command
, u8 value
)
358 spin_lock_irqsave(&cam
->dev_lock
, flags
);
359 rval
= TWSIC0_EN
| ((addr
<< TWSIC0_SID_SHIFT
) & TWSIC0_SID
);
360 rval
|= TWSIC0_OVMAGIC
; /* Make OV sensors work */
362 * Marvell sez set clkdiv to all 1's for now.
364 rval
|= TWSIC0_CLKDIV
;
365 cafe_reg_write(cam
, REG_TWSIC0
, rval
);
366 (void) cafe_reg_read(cam
, REG_TWSIC1
); /* force write */
367 rval
= value
| ((command
<< TWSIC1_ADDR_SHIFT
) & TWSIC1_ADDR
);
368 cafe_reg_write(cam
, REG_TWSIC1
, rval
);
369 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
370 msleep(2); /* Required or things flake */
372 wait_event_timeout(cam
->smbus_wait
, cafe_smbus_write_done(cam
),
374 spin_lock_irqsave(&cam
->dev_lock
, flags
);
375 rval
= cafe_reg_read(cam
, REG_TWSIC1
);
376 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
378 if (rval
& TWSIC1_WSTAT
) {
379 cam_err(cam
, "SMBUS write (%02x/%02x/%02x) timed out\n", addr
,
383 if (rval
& TWSIC1_ERROR
) {
384 cam_err(cam
, "SMBUS write (%02x/%02x/%02x) error\n", addr
,
393 static int cafe_smbus_read_done(struct cafe_camera
*cam
)
399 * We must delay after the interrupt, or the controller gets confused
400 * and never does give us good status. Fortunately, we don't do this
404 spin_lock_irqsave(&cam
->dev_lock
, flags
);
405 c1
= cafe_reg_read(cam
, REG_TWSIC1
);
406 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
407 return c1
& (TWSIC1_RVALID
|TWSIC1_ERROR
);
412 static int cafe_smbus_read_data(struct cafe_camera
*cam
,
413 u16 addr
, u8 command
, u8
*value
)
418 spin_lock_irqsave(&cam
->dev_lock
, flags
);
419 rval
= TWSIC0_EN
| ((addr
<< TWSIC0_SID_SHIFT
) & TWSIC0_SID
);
420 rval
|= TWSIC0_OVMAGIC
; /* Make OV sensors work */
422 * Marvel sez set clkdiv to all 1's for now.
424 rval
|= TWSIC0_CLKDIV
;
425 cafe_reg_write(cam
, REG_TWSIC0
, rval
);
426 (void) cafe_reg_read(cam
, REG_TWSIC1
); /* force write */
427 rval
= TWSIC1_READ
| ((command
<< TWSIC1_ADDR_SHIFT
) & TWSIC1_ADDR
);
428 cafe_reg_write(cam
, REG_TWSIC1
, rval
);
429 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
431 wait_event_timeout(cam
->smbus_wait
,
432 cafe_smbus_read_done(cam
), CAFE_SMBUS_TIMEOUT
);
433 spin_lock_irqsave(&cam
->dev_lock
, flags
);
434 rval
= cafe_reg_read(cam
, REG_TWSIC1
);
435 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
437 if (rval
& TWSIC1_ERROR
) {
438 cam_err(cam
, "SMBUS read (%02x/%02x) error\n", addr
, command
);
441 if (! (rval
& TWSIC1_RVALID
)) {
442 cam_err(cam
, "SMBUS read (%02x/%02x) timed out\n", addr
,
446 *value
= rval
& 0xff;
451 * Perform a transfer over SMBUS. This thing is called under
452 * the i2c bus lock, so we shouldn't race with ourselves...
454 static int cafe_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
455 unsigned short flags
, char rw
, u8 command
,
456 int size
, union i2c_smbus_data
*data
)
458 struct cafe_camera
*cam
= i2c_get_adapdata(adapter
);
462 * Refuse to talk to anything but OV cam chips. We should
463 * never even see an attempt to do so, but one never knows.
465 if (cam
->sensor
&& addr
!= cam
->sensor
->addr
) {
466 cam_err(cam
, "funky smbus addr %d\n", addr
);
470 * This interface would appear to only do byte data ops. OK
471 * it can do word too, but the cam chip has no use for that.
473 if (size
!= I2C_SMBUS_BYTE_DATA
) {
474 cam_err(cam
, "funky xfer size %d\n", size
);
478 if (rw
== I2C_SMBUS_WRITE
)
479 ret
= cafe_smbus_write_data(cam
, addr
, command
, data
->byte
);
480 else if (rw
== I2C_SMBUS_READ
)
481 ret
= cafe_smbus_read_data(cam
, addr
, command
, &data
->byte
);
486 static void cafe_smbus_enable_irq(struct cafe_camera
*cam
)
490 spin_lock_irqsave(&cam
->dev_lock
, flags
);
491 cafe_reg_set_bit(cam
, REG_IRQMASK
, TWSIIRQS
);
492 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
495 static u32
cafe_smbus_func(struct i2c_adapter
*adapter
)
497 return I2C_FUNC_SMBUS_READ_BYTE_DATA
|
498 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
;
501 static struct i2c_algorithm cafe_smbus_algo
= {
502 .smbus_xfer
= cafe_smbus_xfer
,
503 .functionality
= cafe_smbus_func
506 /* Somebody is on the bus */
507 static int cafe_cam_init(struct cafe_camera
*cam
);
508 static void cafe_ctlr_stop_dma(struct cafe_camera
*cam
);
509 static void cafe_ctlr_power_down(struct cafe_camera
*cam
);
511 static int cafe_smbus_attach(struct i2c_client
*client
)
513 struct cafe_camera
*cam
= i2c_get_adapdata(client
->adapter
);
516 * Don't talk to chips we don't recognize.
518 if (client
->driver
->id
== I2C_DRIVERID_OV7670
) {
519 cam
->sensor
= client
;
520 return cafe_cam_init(cam
);
525 static int cafe_smbus_detach(struct i2c_client
*client
)
527 struct cafe_camera
*cam
= i2c_get_adapdata(client
->adapter
);
529 if (cam
->sensor
== client
) {
530 cafe_ctlr_stop_dma(cam
);
531 cafe_ctlr_power_down(cam
);
532 cam_err(cam
, "lost the sensor!\n");
533 cam
->sensor
= NULL
; /* Bummer, no camera */
534 cam
->state
= S_NOTREADY
;
539 static int cafe_smbus_setup(struct cafe_camera
*cam
)
541 struct i2c_adapter
*adap
= &cam
->i2c_adapter
;
544 cafe_smbus_enable_irq(cam
);
545 adap
->id
= I2C_HW_SMBUS_CAFE
;
546 adap
->class = I2C_CLASS_CAM_DIGITAL
;
547 adap
->owner
= THIS_MODULE
;
548 adap
->client_register
= cafe_smbus_attach
;
549 adap
->client_unregister
= cafe_smbus_detach
;
550 adap
->algo
= &cafe_smbus_algo
;
551 strcpy(adap
->name
, "cafe_ccic");
552 adap
->dev
.parent
= &cam
->pdev
->dev
;
553 i2c_set_adapdata(adap
, cam
);
554 ret
= i2c_add_adapter(adap
);
556 printk(KERN_ERR
"Unable to register cafe i2c adapter\n");
560 static void cafe_smbus_shutdown(struct cafe_camera
*cam
)
562 i2c_del_adapter(&cam
->i2c_adapter
);
566 /* ------------------------------------------------------------------- */
568 * Deal with the controller.
572 * Do everything we think we need to have the interface operating
573 * according to the desired format.
575 static void cafe_ctlr_dma(struct cafe_camera
*cam
)
578 * Store the first two Y buffers (we aren't supporting
579 * planar formats for now, so no UV bufs). Then either
580 * set the third if it exists, or tell the controller
583 cafe_reg_write(cam
, REG_Y0BAR
, cam
->dma_handles
[0]);
584 cafe_reg_write(cam
, REG_Y1BAR
, cam
->dma_handles
[1]);
585 if (cam
->nbufs
> 2) {
586 cafe_reg_write(cam
, REG_Y2BAR
, cam
->dma_handles
[2]);
587 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
590 cafe_reg_set_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
591 cafe_reg_write(cam
, REG_UBAR
, 0); /* 32 bits only for now */
594 static void cafe_ctlr_image(struct cafe_camera
*cam
)
597 struct v4l2_pix_format
*fmt
= &cam
->pix_format
;
599 imgsz
= ((fmt
->height
<< IMGSZ_V_SHIFT
) & IMGSZ_V_MASK
) |
600 (fmt
->bytesperline
& IMGSZ_H_MASK
);
601 cafe_reg_write(cam
, REG_IMGSIZE
, imgsz
);
602 cafe_reg_write(cam
, REG_IMGOFFSET
, 0);
603 /* YPITCH just drops the last two bits */
604 cafe_reg_write_mask(cam
, REG_IMGPITCH
, fmt
->bytesperline
,
607 * Tell the controller about the image format we are using.
609 switch (cam
->pix_format
.pixelformat
) {
610 case V4L2_PIX_FMT_YUYV
:
611 cafe_reg_write_mask(cam
, REG_CTRL0
,
612 C0_DF_YUV
|C0_YUV_PACKED
|C0_YUVE_YUYV
,
616 case V4L2_PIX_FMT_RGB444
:
617 cafe_reg_write_mask(cam
, REG_CTRL0
,
618 C0_DF_RGB
|C0_RGBF_444
|C0_RGB4_XRGB
,
623 case V4L2_PIX_FMT_RGB565
:
624 cafe_reg_write_mask(cam
, REG_CTRL0
,
625 C0_DF_RGB
|C0_RGBF_565
|C0_RGB5_BGGR
,
630 cam_err(cam
, "Unknown format %x\n", cam
->pix_format
.pixelformat
);
634 * Make sure it knows we want to use hsync/vsync.
636 cafe_reg_write_mask(cam
, REG_CTRL0
, C0_SIF_HVSYNC
,
642 * Configure the controller for operation; caller holds the
645 static int cafe_ctlr_configure(struct cafe_camera
*cam
)
649 spin_lock_irqsave(&cam
->dev_lock
, flags
);
651 cafe_ctlr_image(cam
);
652 cafe_set_config_needed(cam
, 0);
653 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
657 static void cafe_ctlr_irq_enable(struct cafe_camera
*cam
)
660 * Clear any pending interrupts, since we do not
661 * expect to have I/O active prior to enabling.
663 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
);
664 cafe_reg_set_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
667 static void cafe_ctlr_irq_disable(struct cafe_camera
*cam
)
669 cafe_reg_clear_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
673 * Make the controller start grabbing images. Everything must
674 * be set up before doing this.
676 static void cafe_ctlr_start(struct cafe_camera
*cam
)
678 /* set_bit performs a read, so no other barrier should be
680 cafe_reg_set_bit(cam
, REG_CTRL0
, C0_ENABLE
);
683 static void cafe_ctlr_stop(struct cafe_camera
*cam
)
685 cafe_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
688 static void cafe_ctlr_init(struct cafe_camera
*cam
)
692 spin_lock_irqsave(&cam
->dev_lock
, flags
);
694 * Added magic to bring up the hardware on the B-Test board
696 cafe_reg_write(cam
, 0x3038, 0x8);
697 cafe_reg_write(cam
, 0x315c, 0x80008);
699 * Go through the dance needed to wake the device up.
700 * Note that these registers are global and shared
701 * with the NAND and SD devices. Interaction between the
702 * three still needs to be examined.
704 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRS
|GCSR_MRS
); /* Needed? */
705 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRC
|GCSR_MRC
);
706 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_SRC
|GCSR_MRS
);
707 mdelay(5); /* FIXME revisit this */
708 cafe_reg_write(cam
, REG_GL_CSR
, GCSR_CCIC_EN
|GCSR_SRC
|GCSR_MRC
);
709 cafe_reg_set_bit(cam
, REG_GL_IMASK
, GIMSK_CCIC_EN
);
711 * Make sure it's not powered down.
713 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
715 * Turn off the enable bit. It sure should be off anyway,
716 * but it's good to be sure.
718 cafe_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
720 * Mask all interrupts.
722 cafe_reg_write(cam
, REG_IRQMASK
, 0);
724 * Clock the sensor appropriately. Controller clock should
725 * be 48MHz, sensor "typical" value is half that.
727 cafe_reg_write_mask(cam
, REG_CLKCTRL
, 2, CLK_DIV_MASK
);
728 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
733 * Stop the controller, and don't return until we're really sure that no
734 * further DMA is going on.
736 static void cafe_ctlr_stop_dma(struct cafe_camera
*cam
)
741 * Theory: stop the camera controller (whether it is operating
742 * or not). Delay briefly just in case we race with the SOF
743 * interrupt, then wait until no DMA is active.
745 spin_lock_irqsave(&cam
->dev_lock
, flags
);
747 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
749 wait_event_timeout(cam
->iowait
,
750 !test_bit(CF_DMA_ACTIVE
, &cam
->flags
), HZ
);
751 if (test_bit(CF_DMA_ACTIVE
, &cam
->flags
))
752 cam_err(cam
, "Timeout waiting for DMA to end\n");
753 /* This would be bad news - what now? */
754 spin_lock_irqsave(&cam
->dev_lock
, flags
);
756 cafe_ctlr_irq_disable(cam
);
757 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
763 static void cafe_ctlr_power_up(struct cafe_camera
*cam
)
767 spin_lock_irqsave(&cam
->dev_lock
, flags
);
768 cafe_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
770 * Put the sensor into operational mode (assumes OLPC-style
771 * wiring). Control 0 is reset - set to 1 to operate.
772 * Control 1 is power down, set to 0 to operate.
774 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
); /* pwr up, reset */
775 mdelay(1); /* Marvell says 1ms will do it */
776 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
|GPR_C0
);
777 mdelay(1); /* Enough? */
778 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
781 static void cafe_ctlr_power_down(struct cafe_camera
*cam
)
785 spin_lock_irqsave(&cam
->dev_lock
, flags
);
786 cafe_reg_write(cam
, REG_GPR
, GPR_C1EN
|GPR_C0EN
|GPR_C1
);
787 cafe_reg_set_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
788 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
791 /* -------------------------------------------------------------------- */
793 * Communications with the sensor.
796 static int __cafe_cam_cmd(struct cafe_camera
*cam
, int cmd
, void *arg
)
798 struct i2c_client
*sc
= cam
->sensor
;
801 if (sc
== NULL
|| sc
->driver
== NULL
|| sc
->driver
->command
== NULL
)
803 ret
= sc
->driver
->command(sc
, cmd
, arg
);
804 if (ret
== -EPERM
) /* Unsupported command */
809 static int __cafe_cam_reset(struct cafe_camera
*cam
)
812 return __cafe_cam_cmd(cam
, VIDIOC_INT_RESET
, &zero
);
816 * We have found the sensor on the i2c. Let's try to have a
819 static int cafe_cam_init(struct cafe_camera
*cam
)
823 mutex_lock(&cam
->s_mutex
);
824 if (cam
->state
!= S_NOTREADY
)
825 cam_warn(cam
, "Cam init with device in funky state %d",
827 ret
= __cafe_cam_reset(cam
);
830 ret
= __cafe_cam_cmd(cam
, VIDIOC_INT_G_CHIP_IDENT
, &cam
->sensor_type
);
833 // if (cam->sensor->addr != OV7xx0_SID) {
834 if (cam
->sensor_type
!= V4L2_IDENT_OV7670
) {
835 cam_err(cam
, "Unsupported sensor type %d", cam
->sensor
->addr
);
839 /* Get/set parameters? */
843 mutex_unlock(&cam
->s_mutex
);
848 * Configure the sensor to match the parameters we have. Caller should
851 static int cafe_cam_set_flip(struct cafe_camera
*cam
)
853 struct v4l2_control ctrl
;
855 memset(&ctrl
, 0, sizeof(ctrl
));
856 ctrl
.id
= V4L2_CID_VFLIP
;
858 return __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, &ctrl
);
862 static int cafe_cam_configure(struct cafe_camera
*cam
)
864 struct v4l2_format fmt
;
867 if (cam
->state
!= S_IDLE
)
869 fmt
.fmt
.pix
= cam
->pix_format
;
870 ret
= __cafe_cam_cmd(cam
, VIDIOC_INT_INIT
, &zero
);
872 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_FMT
, &fmt
);
874 * OV7670 does weird things if flip is set *before* format...
876 ret
+= cafe_cam_set_flip(cam
);
880 /* -------------------------------------------------------------------- */
882 * DMA buffer management. These functions need s_mutex held.
885 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
886 * does a get_free_pages() call, and we waste a good chunk of an orderN
887 * allocation. Should try to allocate the whole set in one chunk.
889 static int cafe_alloc_dma_bufs(struct cafe_camera
*cam
, int loadtime
)
893 cafe_set_config_needed(cam
, 1);
895 cam
->dma_buf_size
= dma_buf_size
;
897 cam
->dma_buf_size
= cam
->pix_format
.sizeimage
;
902 for (i
= 0; i
< n_dma_bufs
; i
++) {
903 cam
->dma_bufs
[i
] = dma_alloc_coherent(&cam
->pdev
->dev
,
904 cam
->dma_buf_size
, cam
->dma_handles
+ i
,
906 if (cam
->dma_bufs
[i
] == NULL
) {
907 cam_warn(cam
, "Failed to allocate DMA buffer\n");
910 /* For debug, remove eventually */
911 memset(cam
->dma_bufs
[i
], 0xcc, cam
->dma_buf_size
);
915 switch (cam
->nbufs
) {
917 dma_free_coherent(&cam
->pdev
->dev
, cam
->dma_buf_size
,
918 cam
->dma_bufs
[0], cam
->dma_handles
[0]);
921 cam_err(cam
, "Insufficient DMA buffers, cannot operate\n");
926 cam_warn(cam
, "Will limp along with only 2 buffers\n");
932 static void cafe_free_dma_bufs(struct cafe_camera
*cam
)
936 for (i
= 0; i
< cam
->nbufs
; i
++) {
937 dma_free_coherent(&cam
->pdev
->dev
, cam
->dma_buf_size
,
938 cam
->dma_bufs
[i
], cam
->dma_handles
[i
]);
939 cam
->dma_bufs
[i
] = NULL
;
948 /* ----------------------------------------------------------------------- */
950 * Here starts the V4L2 interface code.
954 * Read an image from the device.
956 static ssize_t
cafe_deliver_buffer(struct cafe_camera
*cam
,
957 char __user
*buffer
, size_t len
, loff_t
*pos
)
962 spin_lock_irqsave(&cam
->dev_lock
, flags
);
963 if (cam
->next_buf
< 0) {
964 cam_err(cam
, "deliver_buffer: No next buffer\n");
965 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
968 bufno
= cam
->next_buf
;
969 clear_bit(bufno
, &cam
->flags
);
970 if (++(cam
->next_buf
) >= cam
->nbufs
)
972 if (! test_bit(cam
->next_buf
, &cam
->flags
))
975 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
977 if (len
> cam
->pix_format
.sizeimage
)
978 len
= cam
->pix_format
.sizeimage
;
979 if (copy_to_user(buffer
, cam
->dma_bufs
[bufno
], len
))
986 * Get everything ready, and start grabbing frames.
988 static int cafe_read_setup(struct cafe_camera
*cam
, enum cafe_state state
)
994 * Configuration. If we still don't have DMA buffers,
995 * make one last, desperate attempt.
998 if (cafe_alloc_dma_bufs(cam
, 0))
1001 if (cafe_needs_config(cam
)) {
1002 cafe_cam_configure(cam
);
1003 ret
= cafe_ctlr_configure(cam
);
1011 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1012 cafe_reset_buffers(cam
);
1013 cafe_ctlr_irq_enable(cam
);
1015 cafe_ctlr_start(cam
);
1016 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1021 static ssize_t
cafe_v4l_read(struct file
*filp
,
1022 char __user
*buffer
, size_t len
, loff_t
*pos
)
1024 struct cafe_camera
*cam
= filp
->private_data
;
1028 * Perhaps we're in speculative read mode and already
1031 mutex_lock(&cam
->s_mutex
);
1032 if (cam
->state
== S_SPECREAD
) {
1033 if (cam
->next_buf
>= 0) {
1034 ret
= cafe_deliver_buffer(cam
, buffer
, len
, pos
);
1038 } else if (cam
->state
== S_FLAKED
|| cam
->state
== S_NOTREADY
) {
1041 } else if (cam
->state
!= S_IDLE
) {
1047 * v4l2: multiple processes can open the device, but only
1048 * one gets to grab data from it.
1050 if (cam
->owner
&& cam
->owner
!= filp
) {
1057 * Do setup if need be.
1059 if (cam
->state
!= S_SPECREAD
) {
1060 ret
= cafe_read_setup(cam
, S_SINGLEREAD
);
1065 * Wait for something to happen. This should probably
1066 * be interruptible (FIXME).
1068 wait_event_timeout(cam
->iowait
, cam
->next_buf
>= 0, HZ
);
1069 if (cam
->next_buf
< 0) {
1070 cam_err(cam
, "read() operation timed out\n");
1071 cafe_ctlr_stop_dma(cam
);
1076 * Give them their data and we should be done.
1078 ret
= cafe_deliver_buffer(cam
, buffer
, len
, pos
);
1081 mutex_unlock(&cam
->s_mutex
);
1093 * Streaming I/O support.
1098 static int cafe_vidioc_streamon(struct file
*filp
, void *priv
,
1099 enum v4l2_buf_type type
)
1101 struct cafe_camera
*cam
= filp
->private_data
;
1104 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1106 mutex_lock(&cam
->s_mutex
);
1107 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
== 0)
1111 ret
= cafe_read_setup(cam
, S_STREAMING
);
1114 mutex_unlock(&cam
->s_mutex
);
1120 static int cafe_vidioc_streamoff(struct file
*filp
, void *priv
,
1121 enum v4l2_buf_type type
)
1123 struct cafe_camera
*cam
= filp
->private_data
;
1126 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1128 mutex_lock(&cam
->s_mutex
);
1129 if (cam
->state
!= S_STREAMING
)
1132 cafe_ctlr_stop_dma(cam
);
1136 mutex_unlock(&cam
->s_mutex
);
1143 static int cafe_setup_siobuf(struct cafe_camera
*cam
, int index
)
1145 struct cafe_sio_buffer
*buf
= cam
->sb_bufs
+ index
;
1147 INIT_LIST_HEAD(&buf
->list
);
1148 buf
->v4lbuf
.length
= PAGE_ALIGN(cam
->pix_format
.sizeimage
);
1149 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
1150 if (buf
->buffer
== NULL
)
1155 buf
->v4lbuf
.index
= index
;
1156 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1157 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
1158 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
1160 * Offset: must be 32-bit even on a 64-bit system. video-buf
1161 * just uses the length times the index, but the spec warns
1162 * against doing just that - vma merging problems. So we
1163 * leave a gap between each pair of buffers.
1165 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
1169 static int cafe_free_sio_buffers(struct cafe_camera
*cam
)
1174 * If any buffers are mapped, we cannot free them at all.
1176 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1177 if (cam
->sb_bufs
[i
].mapcount
> 0)
1182 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1183 vfree(cam
->sb_bufs
[i
].buffer
);
1185 kfree(cam
->sb_bufs
);
1186 cam
->sb_bufs
= NULL
;
1187 INIT_LIST_HEAD(&cam
->sb_avail
);
1188 INIT_LIST_HEAD(&cam
->sb_full
);
1194 static int cafe_vidioc_reqbufs(struct file
*filp
, void *priv
,
1195 struct v4l2_requestbuffers
*req
)
1197 struct cafe_camera
*cam
= filp
->private_data
;
1201 * Make sure it's something we can do. User pointers could be
1202 * implemented without great pain, but that's not been done yet.
1204 if (req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1206 if (req
->memory
!= V4L2_MEMORY_MMAP
)
1209 * If they ask for zero buffers, they really want us to stop streaming
1210 * (if it's happening) and free everything. Should we check owner?
1212 mutex_lock(&cam
->s_mutex
);
1213 if (req
->count
== 0) {
1214 if (cam
->state
== S_STREAMING
)
1215 cafe_ctlr_stop_dma(cam
);
1216 ret
= cafe_free_sio_buffers (cam
);
1220 * Device needs to be idle and working. We *could* try to do the
1221 * right thing in S_SPECREAD by shutting things down, but it
1222 * probably doesn't matter.
1224 if (cam
->state
!= S_IDLE
|| (cam
->owner
&& cam
->owner
!= filp
)) {
1230 if (req
->count
< min_buffers
)
1231 req
->count
= min_buffers
;
1232 else if (req
->count
> max_buffers
)
1233 req
->count
= max_buffers
;
1234 if (cam
->n_sbufs
> 0) {
1235 ret
= cafe_free_sio_buffers(cam
);
1240 cam
->sb_bufs
= kzalloc(req
->count
*sizeof(struct cafe_sio_buffer
),
1242 if (cam
->sb_bufs
== NULL
) {
1246 for (cam
->n_sbufs
= 0; cam
->n_sbufs
< req
->count
; (cam
->n_sbufs
++)) {
1247 ret
= cafe_setup_siobuf(cam
, cam
->n_sbufs
);
1252 if (cam
->n_sbufs
== 0) /* no luck at all - ret already set */
1253 kfree(cam
->sb_bufs
);
1256 req
->count
= cam
->n_sbufs
; /* In case of partial success */
1259 mutex_unlock(&cam
->s_mutex
);
1264 static int cafe_vidioc_querybuf(struct file
*filp
, void *priv
,
1265 struct v4l2_buffer
*buf
)
1267 struct cafe_camera
*cam
= filp
->private_data
;
1270 mutex_lock(&cam
->s_mutex
);
1271 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1273 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1275 *buf
= cam
->sb_bufs
[buf
->index
].v4lbuf
;
1278 mutex_unlock(&cam
->s_mutex
);
1282 static int cafe_vidioc_qbuf(struct file
*filp
, void *priv
,
1283 struct v4l2_buffer
*buf
)
1285 struct cafe_camera
*cam
= filp
->private_data
;
1286 struct cafe_sio_buffer
*sbuf
;
1288 unsigned long flags
;
1290 mutex_lock(&cam
->s_mutex
);
1291 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1293 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1295 sbuf
= cam
->sb_bufs
+ buf
->index
;
1296 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
) {
1297 ret
= 0; /* Already queued?? */
1300 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_DONE
) {
1301 /* Spec doesn't say anything, seems appropriate tho */
1305 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1306 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1307 list_add(&sbuf
->list
, &cam
->sb_avail
);
1308 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1311 mutex_unlock(&cam
->s_mutex
);
1315 static int cafe_vidioc_dqbuf(struct file
*filp
, void *priv
,
1316 struct v4l2_buffer
*buf
)
1318 struct cafe_camera
*cam
= filp
->private_data
;
1319 struct cafe_sio_buffer
*sbuf
;
1321 unsigned long flags
;
1323 mutex_lock(&cam
->s_mutex
);
1324 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1326 if (cam
->state
!= S_STREAMING
)
1328 if (list_empty(&cam
->sb_full
) && filp
->f_flags
& O_NONBLOCK
) {
1333 while (list_empty(&cam
->sb_full
) && cam
->state
== S_STREAMING
) {
1334 mutex_unlock(&cam
->s_mutex
);
1335 if (wait_event_interruptible(cam
->iowait
,
1336 !list_empty(&cam
->sb_full
))) {
1340 mutex_lock(&cam
->s_mutex
);
1343 if (cam
->state
!= S_STREAMING
)
1346 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1347 /* Should probably recheck !list_empty() here */
1348 sbuf
= list_entry(cam
->sb_full
.next
,
1349 struct cafe_sio_buffer
, list
);
1350 list_del_init(&sbuf
->list
);
1351 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1352 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1353 *buf
= sbuf
->v4lbuf
;
1358 mutex_unlock(&cam
->s_mutex
);
1365 static void cafe_v4l_vm_open(struct vm_area_struct
*vma
)
1367 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1369 * Locking: done under mmap_sem, so we don't need to
1370 * go back to the camera lock here.
1376 static void cafe_v4l_vm_close(struct vm_area_struct
*vma
)
1378 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1380 mutex_lock(&sbuf
->cam
->s_mutex
);
1382 /* Docs say we should stop I/O too... */
1383 if (sbuf
->mapcount
== 0)
1384 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
1385 mutex_unlock(&sbuf
->cam
->s_mutex
);
1388 static struct vm_operations_struct cafe_v4l_vm_ops
= {
1389 .open
= cafe_v4l_vm_open
,
1390 .close
= cafe_v4l_vm_close
1394 static int cafe_v4l_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1396 struct cafe_camera
*cam
= filp
->private_data
;
1397 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1400 struct cafe_sio_buffer
*sbuf
= NULL
;
1402 if (! (vma
->vm_flags
& VM_WRITE
) || ! (vma
->vm_flags
& VM_SHARED
))
1405 * Find the buffer they are looking for.
1407 mutex_lock(&cam
->s_mutex
);
1408 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1409 if (cam
->sb_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
1410 sbuf
= cam
->sb_bufs
+ i
;
1416 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
1419 vma
->vm_flags
|= VM_DONTEXPAND
;
1420 vma
->vm_private_data
= sbuf
;
1421 vma
->vm_ops
= &cafe_v4l_vm_ops
;
1422 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
1423 cafe_v4l_vm_open(vma
);
1426 mutex_unlock(&cam
->s_mutex
);
1432 static int cafe_v4l_open(struct inode
*inode
, struct file
*filp
)
1434 struct cafe_camera
*cam
;
1436 cam
= cafe_find_dev(iminor(inode
));
1439 filp
->private_data
= cam
;
1441 mutex_lock(&cam
->s_mutex
);
1442 if (cam
->users
== 0) {
1443 cafe_ctlr_power_up(cam
);
1444 __cafe_cam_reset(cam
);
1445 cafe_set_config_needed(cam
, 1);
1446 /* FIXME make sure this is complete */
1449 mutex_unlock(&cam
->s_mutex
);
1454 static int cafe_v4l_release(struct inode
*inode
, struct file
*filp
)
1456 struct cafe_camera
*cam
= filp
->private_data
;
1458 mutex_lock(&cam
->s_mutex
);
1460 if (filp
== cam
->owner
) {
1461 cafe_ctlr_stop_dma(cam
);
1462 cafe_free_sio_buffers(cam
);
1465 if (cam
->users
== 0) {
1466 cafe_ctlr_power_down(cam
);
1467 if (! alloc_bufs_at_load
)
1468 cafe_free_dma_bufs(cam
);
1470 mutex_unlock(&cam
->s_mutex
);
1476 static unsigned int cafe_v4l_poll(struct file
*filp
,
1477 struct poll_table_struct
*pt
)
1479 struct cafe_camera
*cam
= filp
->private_data
;
1481 poll_wait(filp
, &cam
->iowait
, pt
);
1482 if (cam
->next_buf
>= 0)
1483 return POLLIN
| POLLRDNORM
;
1489 static int cafe_vidioc_queryctrl(struct file
*filp
, void *priv
,
1490 struct v4l2_queryctrl
*qc
)
1492 struct cafe_camera
*cam
= filp
->private_data
;
1495 mutex_lock(&cam
->s_mutex
);
1496 ret
= __cafe_cam_cmd(cam
, VIDIOC_QUERYCTRL
, qc
);
1497 mutex_unlock(&cam
->s_mutex
);
1502 static int cafe_vidioc_g_ctrl(struct file
*filp
, void *priv
,
1503 struct v4l2_control
*ctrl
)
1505 struct cafe_camera
*cam
= filp
->private_data
;
1508 mutex_lock(&cam
->s_mutex
);
1509 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_CTRL
, ctrl
);
1510 mutex_unlock(&cam
->s_mutex
);
1515 static int cafe_vidioc_s_ctrl(struct file
*filp
, void *priv
,
1516 struct v4l2_control
*ctrl
)
1518 struct cafe_camera
*cam
= filp
->private_data
;
1521 mutex_lock(&cam
->s_mutex
);
1522 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, ctrl
);
1523 mutex_unlock(&cam
->s_mutex
);
1531 static int cafe_vidioc_querycap(struct file
*file
, void *priv
,
1532 struct v4l2_capability
*cap
)
1534 strcpy(cap
->driver
, "cafe_ccic");
1535 strcpy(cap
->card
, "cafe_ccic");
1536 cap
->version
= CAFE_VERSION
;
1537 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
1538 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1544 * The default format we use until somebody says otherwise.
1546 static struct v4l2_pix_format cafe_def_pix_format
= {
1548 .height
= VGA_HEIGHT
,
1549 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1550 .field
= V4L2_FIELD_NONE
,
1551 .bytesperline
= VGA_WIDTH
*2,
1552 .sizeimage
= VGA_WIDTH
*VGA_HEIGHT
*2,
1555 static int cafe_vidioc_enum_fmt_cap(struct file
*filp
,
1556 void *priv
, struct v4l2_fmtdesc
*fmt
)
1558 struct cafe_camera
*cam
= priv
;
1561 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1563 mutex_lock(&cam
->s_mutex
);
1564 ret
= __cafe_cam_cmd(cam
, VIDIOC_ENUM_FMT
, fmt
);
1565 mutex_unlock(&cam
->s_mutex
);
1570 static int cafe_vidioc_try_fmt_cap (struct file
*filp
, void *priv
,
1571 struct v4l2_format
*fmt
)
1573 struct cafe_camera
*cam
= priv
;
1576 mutex_lock(&cam
->s_mutex
);
1577 ret
= __cafe_cam_cmd(cam
, VIDIOC_TRY_FMT
, fmt
);
1578 mutex_unlock(&cam
->s_mutex
);
1582 static int cafe_vidioc_s_fmt_cap(struct file
*filp
, void *priv
,
1583 struct v4l2_format
*fmt
)
1585 struct cafe_camera
*cam
= priv
;
1589 * Can't do anything if the device is not idle
1590 * Also can't if there are streaming buffers in place.
1592 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
> 0)
1595 * See if the formatting works in principle.
1597 ret
= cafe_vidioc_try_fmt_cap(filp
, priv
, fmt
);
1601 * Now we start to change things for real, so let's do it
1604 mutex_lock(&cam
->s_mutex
);
1605 cam
->pix_format
= fmt
->fmt
.pix
;
1607 * Make sure we have appropriate DMA buffers.
1610 if (cam
->nbufs
> 0 && cam
->dma_buf_size
< cam
->pix_format
.sizeimage
)
1611 cafe_free_dma_bufs(cam
);
1612 if (cam
->nbufs
== 0) {
1613 if (cafe_alloc_dma_bufs(cam
, 0))
1617 * It looks like this might work, so let's program the sensor.
1619 ret
= cafe_cam_configure(cam
);
1621 ret
= cafe_ctlr_configure(cam
);
1623 mutex_unlock(&cam
->s_mutex
);
1628 * Return our stored notion of how the camera is/should be configured.
1629 * The V4l2 spec wants us to be smarter, and actually get this from
1630 * the camera (and not mess with it at open time). Someday.
1632 static int cafe_vidioc_g_fmt_cap(struct file
*filp
, void *priv
,
1633 struct v4l2_format
*f
)
1635 struct cafe_camera
*cam
= priv
;
1637 f
->fmt
.pix
= cam
->pix_format
;
1642 * We only have one input - the sensor - so minimize the nonsense here.
1644 static int cafe_vidioc_enum_input(struct file
*filp
, void *priv
,
1645 struct v4l2_input
*input
)
1647 if (input
->index
!= 0)
1650 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1651 input
->std
= V4L2_STD_ALL
; /* Not sure what should go here */
1652 strcpy(input
->name
, "Camera");
1656 static int cafe_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
1662 static int cafe_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1670 static int cafe_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
1676 * G/S_PARM. Most of this is done by the sensor, but we are
1677 * the level which controls the number of read buffers.
1679 static int cafe_vidioc_g_parm(struct file
*filp
, void *priv
,
1680 struct v4l2_streamparm
*parms
)
1682 struct cafe_camera
*cam
= priv
;
1685 mutex_lock(&cam
->s_mutex
);
1686 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_PARM
, parms
);
1687 mutex_unlock(&cam
->s_mutex
);
1688 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1692 static int cafe_vidioc_s_parm(struct file
*filp
, void *priv
,
1693 struct v4l2_streamparm
*parms
)
1695 struct cafe_camera
*cam
= priv
;
1698 mutex_lock(&cam
->s_mutex
);
1699 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_PARM
, parms
);
1700 mutex_unlock(&cam
->s_mutex
);
1701 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1706 static void cafe_v4l_dev_release(struct video_device
*vd
)
1708 struct cafe_camera
*cam
= container_of(vd
, struct cafe_camera
, v4ldev
);
1715 * This template device holds all of those v4l2 methods; we
1716 * clone it for specific real devices.
1719 static const struct file_operations cafe_v4l_fops
= {
1720 .owner
= THIS_MODULE
,
1721 .open
= cafe_v4l_open
,
1722 .release
= cafe_v4l_release
,
1723 .read
= cafe_v4l_read
,
1724 .poll
= cafe_v4l_poll
,
1725 .mmap
= cafe_v4l_mmap
,
1726 .ioctl
= video_ioctl2
,
1727 .llseek
= no_llseek
,
1730 static struct video_device cafe_v4l_template
= {
1732 .type
= VFL_TYPE_GRABBER
,
1733 .type2
= VID_TYPE_CAPTURE
,
1734 .minor
= -1, /* Get one dynamically */
1735 .tvnorms
= V4L2_STD_NTSC_M
,
1736 .current_norm
= V4L2_STD_NTSC_M
, /* make mplayer happy */
1738 .fops
= &cafe_v4l_fops
,
1739 .release
= cafe_v4l_dev_release
,
1741 .vidioc_querycap
= cafe_vidioc_querycap
,
1742 .vidioc_enum_fmt_cap
= cafe_vidioc_enum_fmt_cap
,
1743 .vidioc_try_fmt_cap
= cafe_vidioc_try_fmt_cap
,
1744 .vidioc_s_fmt_cap
= cafe_vidioc_s_fmt_cap
,
1745 .vidioc_g_fmt_cap
= cafe_vidioc_g_fmt_cap
,
1746 .vidioc_enum_input
= cafe_vidioc_enum_input
,
1747 .vidioc_g_input
= cafe_vidioc_g_input
,
1748 .vidioc_s_input
= cafe_vidioc_s_input
,
1749 .vidioc_s_std
= cafe_vidioc_s_std
,
1750 .vidioc_reqbufs
= cafe_vidioc_reqbufs
,
1751 .vidioc_querybuf
= cafe_vidioc_querybuf
,
1752 .vidioc_qbuf
= cafe_vidioc_qbuf
,
1753 .vidioc_dqbuf
= cafe_vidioc_dqbuf
,
1754 .vidioc_streamon
= cafe_vidioc_streamon
,
1755 .vidioc_streamoff
= cafe_vidioc_streamoff
,
1756 .vidioc_queryctrl
= cafe_vidioc_queryctrl
,
1757 .vidioc_g_ctrl
= cafe_vidioc_g_ctrl
,
1758 .vidioc_s_ctrl
= cafe_vidioc_s_ctrl
,
1759 .vidioc_g_parm
= cafe_vidioc_g_parm
,
1760 .vidioc_s_parm
= cafe_vidioc_s_parm
,
1769 /* ---------------------------------------------------------------------- */
1771 * Interrupt handler stuff
1776 static void cafe_frame_tasklet(unsigned long data
)
1778 struct cafe_camera
*cam
= (struct cafe_camera
*) data
;
1780 unsigned long flags
;
1781 struct cafe_sio_buffer
*sbuf
;
1783 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1784 for (i
= 0; i
< cam
->nbufs
; i
++) {
1785 int bufno
= cam
->next_buf
;
1786 if (bufno
< 0) { /* "will never happen" */
1787 cam_err(cam
, "No valid bufs in tasklet!\n");
1790 if (++(cam
->next_buf
) >= cam
->nbufs
)
1792 if (! test_bit(bufno
, &cam
->flags
))
1794 if (list_empty(&cam
->sb_avail
))
1795 break; /* Leave it valid, hope for better later */
1796 clear_bit(bufno
, &cam
->flags
);
1798 * We could perhaps drop the spinlock during this
1799 * big copy. Something to consider.
1801 sbuf
= list_entry(cam
->sb_avail
.next
,
1802 struct cafe_sio_buffer
, list
);
1803 memcpy(sbuf
->buffer
, cam
->dma_bufs
[bufno
],
1804 cam
->pix_format
.sizeimage
);
1805 sbuf
->v4lbuf
.bytesused
= cam
->pix_format
.sizeimage
;
1806 sbuf
->v4lbuf
.sequence
= cam
->buf_seq
[bufno
];
1807 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1808 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1809 list_move_tail(&sbuf
->list
, &cam
->sb_full
);
1811 if (! list_empty(&cam
->sb_full
))
1812 wake_up(&cam
->iowait
);
1813 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1818 static void cafe_frame_complete(struct cafe_camera
*cam
, int frame
)
1821 * Basic frame housekeeping.
1823 if (test_bit(frame
, &cam
->flags
) && printk_ratelimit())
1824 cam_err(cam
, "Frame overrun on %d, frames lost\n", frame
);
1825 set_bit(frame
, &cam
->flags
);
1826 clear_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1827 if (cam
->next_buf
< 0)
1828 cam
->next_buf
= frame
;
1829 cam
->buf_seq
[frame
] = ++(cam
->sequence
);
1831 switch (cam
->state
) {
1833 * If in single read mode, try going speculative.
1836 cam
->state
= S_SPECREAD
;
1837 cam
->specframes
= 0;
1838 wake_up(&cam
->iowait
);
1842 * If we are already doing speculative reads, and nobody is
1843 * reading them, just stop.
1846 if (++(cam
->specframes
) >= cam
->nbufs
) {
1847 cafe_ctlr_stop(cam
);
1848 cafe_ctlr_irq_disable(cam
);
1849 cam
->state
= S_IDLE
;
1851 wake_up(&cam
->iowait
);
1854 * For the streaming case, we defer the real work to the
1857 * FIXME: if the application is not consuming the buffers,
1858 * we should eventually put things on hold and restart in
1862 tasklet_schedule(&cam
->s_tasklet
);
1866 cam_err(cam
, "Frame interrupt in non-operational state\n");
1874 static void cafe_frame_irq(struct cafe_camera
*cam
, unsigned int irqs
)
1878 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
); /* Clear'em all */
1880 * Handle any frame completions. There really should
1881 * not be more than one of these, or we have fallen
1884 for (frame
= 0; frame
< cam
->nbufs
; frame
++)
1885 if (irqs
& (IRQ_EOF0
<< frame
))
1886 cafe_frame_complete(cam
, frame
);
1888 * If a frame starts, note that we have DMA active. This
1889 * code assumes that we won't get multiple frame interrupts
1890 * at once; may want to rethink that.
1892 if (irqs
& (IRQ_SOF0
| IRQ_SOF1
| IRQ_SOF2
))
1893 set_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1898 static irqreturn_t
cafe_irq(int irq
, void *data
)
1900 struct cafe_camera
*cam
= data
;
1903 spin_lock(&cam
->dev_lock
);
1904 irqs
= cafe_reg_read(cam
, REG_IRQSTAT
);
1905 if ((irqs
& ALLIRQS
) == 0) {
1906 spin_unlock(&cam
->dev_lock
);
1909 if (irqs
& FRAMEIRQS
)
1910 cafe_frame_irq(cam
, irqs
);
1911 if (irqs
& TWSIIRQS
) {
1912 cafe_reg_write(cam
, REG_IRQSTAT
, TWSIIRQS
);
1913 wake_up(&cam
->smbus_wait
);
1915 spin_unlock(&cam
->dev_lock
);
1920 /* -------------------------------------------------------------------------- */
1921 #ifdef CONFIG_VIDEO_ADV_DEBUG
1926 static char cafe_debug_buf
[1024];
1927 static struct dentry
*cafe_dfs_root
;
1929 static void cafe_dfs_setup(void)
1931 cafe_dfs_root
= debugfs_create_dir("cafe_ccic", NULL
);
1932 if (IS_ERR(cafe_dfs_root
)) {
1933 cafe_dfs_root
= NULL
; /* Never mind */
1934 printk(KERN_NOTICE
"cafe_ccic unable to set up debugfs\n");
1938 static void cafe_dfs_shutdown(void)
1941 debugfs_remove(cafe_dfs_root
);
1944 static int cafe_dfs_open(struct inode
*inode
, struct file
*file
)
1946 file
->private_data
= inode
->i_private
;
1950 static ssize_t
cafe_dfs_read_regs(struct file
*file
,
1951 char __user
*buf
, size_t count
, loff_t
*ppos
)
1953 struct cafe_camera
*cam
= file
->private_data
;
1954 char *s
= cafe_debug_buf
;
1957 for (offset
= 0; offset
< 0x44; offset
+= 4)
1958 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1959 cafe_reg_read(cam
, offset
));
1960 for (offset
= 0x88; offset
<= 0x90; offset
+= 4)
1961 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1962 cafe_reg_read(cam
, offset
));
1963 for (offset
= 0xb4; offset
<= 0xbc; offset
+= 4)
1964 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1965 cafe_reg_read(cam
, offset
));
1966 for (offset
= 0x3000; offset
<= 0x300c; offset
+= 4)
1967 s
+= sprintf(s
, "%04x: %08x\n", offset
,
1968 cafe_reg_read(cam
, offset
));
1969 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1970 s
- cafe_debug_buf
);
1973 static const struct file_operations cafe_dfs_reg_ops
= {
1974 .owner
= THIS_MODULE
,
1975 .read
= cafe_dfs_read_regs
,
1976 .open
= cafe_dfs_open
1979 static ssize_t
cafe_dfs_read_cam(struct file
*file
,
1980 char __user
*buf
, size_t count
, loff_t
*ppos
)
1982 struct cafe_camera
*cam
= file
->private_data
;
1983 char *s
= cafe_debug_buf
;
1988 for (offset
= 0x0; offset
< 0x8a; offset
++)
1992 cafe_smbus_read_data(cam
, cam
->sensor
->addr
, offset
, &v
);
1993 s
+= sprintf(s
, "%02x: %02x\n", offset
, v
);
1995 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1996 s
- cafe_debug_buf
);
1999 static const struct file_operations cafe_dfs_cam_ops
= {
2000 .owner
= THIS_MODULE
,
2001 .read
= cafe_dfs_read_cam
,
2002 .open
= cafe_dfs_open
2007 static void cafe_dfs_cam_setup(struct cafe_camera
*cam
)
2013 sprintf(fname
, "regs-%d", cam
->v4ldev
.minor
);
2014 cam
->dfs_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2015 cam
, &cafe_dfs_reg_ops
);
2016 sprintf(fname
, "cam-%d", cam
->v4ldev
.minor
);
2017 cam
->dfs_cam_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2018 cam
, &cafe_dfs_cam_ops
);
2022 static void cafe_dfs_cam_shutdown(struct cafe_camera
*cam
)
2024 if (! IS_ERR(cam
->dfs_regs
))
2025 debugfs_remove(cam
->dfs_regs
);
2026 if (! IS_ERR(cam
->dfs_cam_regs
))
2027 debugfs_remove(cam
->dfs_cam_regs
);
2032 #define cafe_dfs_setup()
2033 #define cafe_dfs_shutdown()
2034 #define cafe_dfs_cam_setup(cam)
2035 #define cafe_dfs_cam_shutdown(cam)
2036 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2041 /* ------------------------------------------------------------------------*/
2043 * PCI interface stuff.
2046 static int cafe_pci_probe(struct pci_dev
*pdev
,
2047 const struct pci_device_id
*id
)
2051 struct cafe_camera
*cam
;
2053 * Make sure we have a camera here - we'll get calls for
2054 * the other cafe devices as well.
2056 pci_read_config_word(pdev
, PCI_CLASS_DEVICE
, &classword
);
2057 if (classword
!= PCI_CLASS_MULTIMEDIA_VIDEO
)
2060 * Start putting together one of our big camera structures.
2063 cam
= kzalloc(sizeof(struct cafe_camera
), GFP_KERNEL
);
2066 mutex_init(&cam
->s_mutex
);
2067 mutex_lock(&cam
->s_mutex
);
2068 spin_lock_init(&cam
->dev_lock
);
2069 cam
->state
= S_NOTREADY
;
2070 cafe_set_config_needed(cam
, 1);
2071 init_waitqueue_head(&cam
->smbus_wait
);
2072 init_waitqueue_head(&cam
->iowait
);
2074 cam
->pix_format
= cafe_def_pix_format
;
2075 INIT_LIST_HEAD(&cam
->dev_list
);
2076 INIT_LIST_HEAD(&cam
->sb_avail
);
2077 INIT_LIST_HEAD(&cam
->sb_full
);
2078 tasklet_init(&cam
->s_tasklet
, cafe_frame_tasklet
, (unsigned long) cam
);
2080 * Get set up on the PCI bus.
2082 ret
= pci_enable_device(pdev
);
2085 pci_set_master(pdev
);
2088 cam
->regs
= pci_iomap(pdev
, 0, 0);
2090 printk(KERN_ERR
"Unable to ioremap cafe-ccic regs\n");
2093 ret
= request_irq(pdev
->irq
, cafe_irq
, IRQF_SHARED
, "cafe-ccic", cam
);
2096 cafe_ctlr_init(cam
);
2097 cafe_ctlr_power_up(cam
);
2099 * Set up I2C/SMBUS communications
2101 mutex_unlock(&cam
->s_mutex
); /* attach can deadlock */
2102 ret
= cafe_smbus_setup(cam
);
2106 * Get the v4l2 setup done.
2108 mutex_lock(&cam
->s_mutex
);
2109 cam
->v4ldev
= cafe_v4l_template
;
2110 cam
->v4ldev
.debug
= 0;
2111 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2112 ret
= video_register_device(&cam
->v4ldev
, VFL_TYPE_GRABBER
, -1);
2116 * If so requested, try to get our DMA buffers now.
2118 if (alloc_bufs_at_load
) {
2119 if (cafe_alloc_dma_bufs(cam
, 1))
2120 cam_warn(cam
, "Unable to alloc DMA buffers at load"
2121 " will try again later.");
2124 cafe_dfs_cam_setup(cam
);
2125 mutex_unlock(&cam
->s_mutex
);
2130 cafe_smbus_shutdown(cam
);
2132 cafe_ctlr_power_down(cam
);
2133 free_irq(pdev
->irq
, cam
);
2135 pci_iounmap(pdev
, cam
->regs
);
2144 * Shut down an initialized device
2146 static void cafe_shutdown(struct cafe_camera
*cam
)
2148 /* FIXME: Make sure we take care of everything here */
2149 cafe_dfs_cam_shutdown(cam
);
2150 if (cam
->n_sbufs
> 0)
2151 /* What if they are still mapped? Shouldn't be, but... */
2152 cafe_free_sio_buffers(cam
);
2153 cafe_remove_dev(cam
);
2154 cafe_ctlr_stop_dma(cam
);
2155 cafe_ctlr_power_down(cam
);
2156 cafe_smbus_shutdown(cam
);
2157 cafe_free_dma_bufs(cam
);
2158 free_irq(cam
->pdev
->irq
, cam
);
2159 pci_iounmap(cam
->pdev
, cam
->regs
);
2160 video_unregister_device(&cam
->v4ldev
);
2161 /* kfree(cam); done in v4l_release () */
2165 static void cafe_pci_remove(struct pci_dev
*pdev
)
2167 struct cafe_camera
*cam
= cafe_find_by_pdev(pdev
);
2170 printk(KERN_WARNING
"pci_remove on unknown pdev %p\n", pdev
);
2173 mutex_lock(&cam
->s_mutex
);
2175 cam_warn(cam
, "Removing a device with users!\n");
2177 /* No unlock - it no longer exists */
2183 static struct pci_device_id cafe_ids
[] = {
2184 { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
2185 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2186 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2190 MODULE_DEVICE_TABLE(pci
, cafe_ids
);
2192 static struct pci_driver cafe_pci_driver
= {
2193 .name
= "cafe1000-ccic",
2194 .id_table
= cafe_ids
,
2195 .probe
= cafe_pci_probe
,
2196 .remove
= cafe_pci_remove
,
2202 static int __init
cafe_init(void)
2206 printk(KERN_NOTICE
"Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2209 ret
= pci_register_driver(&cafe_pci_driver
);
2211 printk(KERN_ERR
"Unable to register cafe_ccic driver\n");
2214 request_module("ov7670"); /* FIXME want something more general */
2222 static void __exit
cafe_exit(void)
2224 pci_unregister_driver(&cafe_pci_driver
);
2225 cafe_dfs_shutdown();
2228 module_init(cafe_init
);
2229 module_exit(cafe_exit
);