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
;
1198 int ret
= 0; /* Silence warning */
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
);
1254 req
->count
= cam
->n_sbufs
; /* In case of partial success */
1257 mutex_unlock(&cam
->s_mutex
);
1262 static int cafe_vidioc_querybuf(struct file
*filp
, void *priv
,
1263 struct v4l2_buffer
*buf
)
1265 struct cafe_camera
*cam
= filp
->private_data
;
1268 mutex_lock(&cam
->s_mutex
);
1269 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1271 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1273 *buf
= cam
->sb_bufs
[buf
->index
].v4lbuf
;
1276 mutex_unlock(&cam
->s_mutex
);
1280 static int cafe_vidioc_qbuf(struct file
*filp
, void *priv
,
1281 struct v4l2_buffer
*buf
)
1283 struct cafe_camera
*cam
= filp
->private_data
;
1284 struct cafe_sio_buffer
*sbuf
;
1286 unsigned long flags
;
1288 mutex_lock(&cam
->s_mutex
);
1289 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1291 if (buf
->index
< 0 || buf
->index
>= cam
->n_sbufs
)
1293 sbuf
= cam
->sb_bufs
+ buf
->index
;
1294 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
) {
1295 ret
= 0; /* Already queued?? */
1298 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_DONE
) {
1299 /* Spec doesn't say anything, seems appropriate tho */
1303 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1304 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1305 list_add(&sbuf
->list
, &cam
->sb_avail
);
1306 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1309 mutex_unlock(&cam
->s_mutex
);
1313 static int cafe_vidioc_dqbuf(struct file
*filp
, void *priv
,
1314 struct v4l2_buffer
*buf
)
1316 struct cafe_camera
*cam
= filp
->private_data
;
1317 struct cafe_sio_buffer
*sbuf
;
1319 unsigned long flags
;
1321 mutex_lock(&cam
->s_mutex
);
1322 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1324 if (cam
->state
!= S_STREAMING
)
1326 if (list_empty(&cam
->sb_full
) && filp
->f_flags
& O_NONBLOCK
) {
1331 while (list_empty(&cam
->sb_full
) && cam
->state
== S_STREAMING
) {
1332 mutex_unlock(&cam
->s_mutex
);
1333 if (wait_event_interruptible(cam
->iowait
,
1334 !list_empty(&cam
->sb_full
))) {
1338 mutex_lock(&cam
->s_mutex
);
1341 if (cam
->state
!= S_STREAMING
)
1344 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1345 /* Should probably recheck !list_empty() here */
1346 sbuf
= list_entry(cam
->sb_full
.next
,
1347 struct cafe_sio_buffer
, list
);
1348 list_del_init(&sbuf
->list
);
1349 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1350 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1351 *buf
= sbuf
->v4lbuf
;
1356 mutex_unlock(&cam
->s_mutex
);
1363 static void cafe_v4l_vm_open(struct vm_area_struct
*vma
)
1365 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1367 * Locking: done under mmap_sem, so we don't need to
1368 * go back to the camera lock here.
1374 static void cafe_v4l_vm_close(struct vm_area_struct
*vma
)
1376 struct cafe_sio_buffer
*sbuf
= vma
->vm_private_data
;
1378 mutex_lock(&sbuf
->cam
->s_mutex
);
1380 /* Docs say we should stop I/O too... */
1381 if (sbuf
->mapcount
== 0)
1382 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
1383 mutex_unlock(&sbuf
->cam
->s_mutex
);
1386 static struct vm_operations_struct cafe_v4l_vm_ops
= {
1387 .open
= cafe_v4l_vm_open
,
1388 .close
= cafe_v4l_vm_close
1392 static int cafe_v4l_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1394 struct cafe_camera
*cam
= filp
->private_data
;
1395 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1398 struct cafe_sio_buffer
*sbuf
= NULL
;
1400 if (! (vma
->vm_flags
& VM_WRITE
) || ! (vma
->vm_flags
& VM_SHARED
))
1403 * Find the buffer they are looking for.
1405 mutex_lock(&cam
->s_mutex
);
1406 for (i
= 0; i
< cam
->n_sbufs
; i
++)
1407 if (cam
->sb_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
1408 sbuf
= cam
->sb_bufs
+ i
;
1414 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
1417 vma
->vm_flags
|= VM_DONTEXPAND
;
1418 vma
->vm_private_data
= sbuf
;
1419 vma
->vm_ops
= &cafe_v4l_vm_ops
;
1420 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
1421 cafe_v4l_vm_open(vma
);
1424 mutex_unlock(&cam
->s_mutex
);
1430 static int cafe_v4l_open(struct inode
*inode
, struct file
*filp
)
1432 struct cafe_camera
*cam
;
1434 cam
= cafe_find_dev(iminor(inode
));
1437 filp
->private_data
= cam
;
1439 mutex_lock(&cam
->s_mutex
);
1440 if (cam
->users
== 0) {
1441 cafe_ctlr_power_up(cam
);
1442 __cafe_cam_reset(cam
);
1443 cafe_set_config_needed(cam
, 1);
1444 /* FIXME make sure this is complete */
1447 mutex_unlock(&cam
->s_mutex
);
1452 static int cafe_v4l_release(struct inode
*inode
, struct file
*filp
)
1454 struct cafe_camera
*cam
= filp
->private_data
;
1456 mutex_lock(&cam
->s_mutex
);
1458 if (filp
== cam
->owner
) {
1459 cafe_ctlr_stop_dma(cam
);
1460 cafe_free_sio_buffers(cam
);
1463 if (cam
->users
== 0) {
1464 cafe_ctlr_power_down(cam
);
1465 if (! alloc_bufs_at_load
)
1466 cafe_free_dma_bufs(cam
);
1468 mutex_unlock(&cam
->s_mutex
);
1474 static unsigned int cafe_v4l_poll(struct file
*filp
,
1475 struct poll_table_struct
*pt
)
1477 struct cafe_camera
*cam
= filp
->private_data
;
1479 poll_wait(filp
, &cam
->iowait
, pt
);
1480 if (cam
->next_buf
>= 0)
1481 return POLLIN
| POLLRDNORM
;
1487 static int cafe_vidioc_queryctrl(struct file
*filp
, void *priv
,
1488 struct v4l2_queryctrl
*qc
)
1490 struct cafe_camera
*cam
= filp
->private_data
;
1493 mutex_lock(&cam
->s_mutex
);
1494 ret
= __cafe_cam_cmd(cam
, VIDIOC_QUERYCTRL
, qc
);
1495 mutex_unlock(&cam
->s_mutex
);
1500 static int cafe_vidioc_g_ctrl(struct file
*filp
, void *priv
,
1501 struct v4l2_control
*ctrl
)
1503 struct cafe_camera
*cam
= filp
->private_data
;
1506 mutex_lock(&cam
->s_mutex
);
1507 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_CTRL
, ctrl
);
1508 mutex_unlock(&cam
->s_mutex
);
1513 static int cafe_vidioc_s_ctrl(struct file
*filp
, void *priv
,
1514 struct v4l2_control
*ctrl
)
1516 struct cafe_camera
*cam
= filp
->private_data
;
1519 mutex_lock(&cam
->s_mutex
);
1520 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_CTRL
, ctrl
);
1521 mutex_unlock(&cam
->s_mutex
);
1529 static int cafe_vidioc_querycap(struct file
*file
, void *priv
,
1530 struct v4l2_capability
*cap
)
1532 strcpy(cap
->driver
, "cafe_ccic");
1533 strcpy(cap
->card
, "cafe_ccic");
1534 cap
->version
= CAFE_VERSION
;
1535 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
1536 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1542 * The default format we use until somebody says otherwise.
1544 static struct v4l2_pix_format cafe_def_pix_format
= {
1546 .height
= VGA_HEIGHT
,
1547 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1548 .field
= V4L2_FIELD_NONE
,
1549 .bytesperline
= VGA_WIDTH
*2,
1550 .sizeimage
= VGA_WIDTH
*VGA_HEIGHT
*2,
1553 static int cafe_vidioc_enum_fmt_cap(struct file
*filp
,
1554 void *priv
, struct v4l2_fmtdesc
*fmt
)
1556 struct cafe_camera
*cam
= priv
;
1559 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1561 mutex_lock(&cam
->s_mutex
);
1562 ret
= __cafe_cam_cmd(cam
, VIDIOC_ENUM_FMT
, fmt
);
1563 mutex_unlock(&cam
->s_mutex
);
1568 static int cafe_vidioc_try_fmt_cap (struct file
*filp
, void *priv
,
1569 struct v4l2_format
*fmt
)
1571 struct cafe_camera
*cam
= priv
;
1574 mutex_lock(&cam
->s_mutex
);
1575 ret
= __cafe_cam_cmd(cam
, VIDIOC_TRY_FMT
, fmt
);
1576 mutex_unlock(&cam
->s_mutex
);
1580 static int cafe_vidioc_s_fmt_cap(struct file
*filp
, void *priv
,
1581 struct v4l2_format
*fmt
)
1583 struct cafe_camera
*cam
= priv
;
1587 * Can't do anything if the device is not idle
1588 * Also can't if there are streaming buffers in place.
1590 if (cam
->state
!= S_IDLE
|| cam
->n_sbufs
> 0)
1593 * See if the formatting works in principle.
1595 ret
= cafe_vidioc_try_fmt_cap(filp
, priv
, fmt
);
1599 * Now we start to change things for real, so let's do it
1602 mutex_lock(&cam
->s_mutex
);
1603 cam
->pix_format
= fmt
->fmt
.pix
;
1605 * Make sure we have appropriate DMA buffers.
1608 if (cam
->nbufs
> 0 && cam
->dma_buf_size
< cam
->pix_format
.sizeimage
)
1609 cafe_free_dma_bufs(cam
);
1610 if (cam
->nbufs
== 0) {
1611 if (cafe_alloc_dma_bufs(cam
, 0))
1615 * It looks like this might work, so let's program the sensor.
1617 ret
= cafe_cam_configure(cam
);
1619 ret
= cafe_ctlr_configure(cam
);
1621 mutex_unlock(&cam
->s_mutex
);
1626 * Return our stored notion of how the camera is/should be configured.
1627 * The V4l2 spec wants us to be smarter, and actually get this from
1628 * the camera (and not mess with it at open time). Someday.
1630 static int cafe_vidioc_g_fmt_cap(struct file
*filp
, void *priv
,
1631 struct v4l2_format
*f
)
1633 struct cafe_camera
*cam
= priv
;
1635 f
->fmt
.pix
= cam
->pix_format
;
1640 * We only have one input - the sensor - so minimize the nonsense here.
1642 static int cafe_vidioc_enum_input(struct file
*filp
, void *priv
,
1643 struct v4l2_input
*input
)
1645 if (input
->index
!= 0)
1648 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1649 input
->std
= V4L2_STD_ALL
; /* Not sure what should go here */
1650 strcpy(input
->name
, "Camera");
1654 static int cafe_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
1660 static int cafe_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1668 static int cafe_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
1674 * G/S_PARM. Most of this is done by the sensor, but we are
1675 * the level which controls the number of read buffers.
1677 static int cafe_vidioc_g_parm(struct file
*filp
, void *priv
,
1678 struct v4l2_streamparm
*parms
)
1680 struct cafe_camera
*cam
= priv
;
1683 mutex_lock(&cam
->s_mutex
);
1684 ret
= __cafe_cam_cmd(cam
, VIDIOC_G_PARM
, parms
);
1685 mutex_unlock(&cam
->s_mutex
);
1686 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1690 static int cafe_vidioc_s_parm(struct file
*filp
, void *priv
,
1691 struct v4l2_streamparm
*parms
)
1693 struct cafe_camera
*cam
= priv
;
1696 mutex_lock(&cam
->s_mutex
);
1697 ret
= __cafe_cam_cmd(cam
, VIDIOC_S_PARM
, parms
);
1698 mutex_unlock(&cam
->s_mutex
);
1699 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1704 static void cafe_v4l_dev_release(struct video_device
*vd
)
1706 struct cafe_camera
*cam
= container_of(vd
, struct cafe_camera
, v4ldev
);
1713 * This template device holds all of those v4l2 methods; we
1714 * clone it for specific real devices.
1717 static const struct file_operations cafe_v4l_fops
= {
1718 .owner
= THIS_MODULE
,
1719 .open
= cafe_v4l_open
,
1720 .release
= cafe_v4l_release
,
1721 .read
= cafe_v4l_read
,
1722 .poll
= cafe_v4l_poll
,
1723 .mmap
= cafe_v4l_mmap
,
1724 .ioctl
= video_ioctl2
,
1725 .llseek
= no_llseek
,
1728 static struct video_device cafe_v4l_template
= {
1730 .type
= VFL_TYPE_GRABBER
,
1731 .type2
= VID_TYPE_CAPTURE
,
1732 .minor
= -1, /* Get one dynamically */
1733 .tvnorms
= V4L2_STD_NTSC_M
,
1734 .current_norm
= V4L2_STD_NTSC_M
, /* make mplayer happy */
1736 .fops
= &cafe_v4l_fops
,
1737 .release
= cafe_v4l_dev_release
,
1739 .vidioc_querycap
= cafe_vidioc_querycap
,
1740 .vidioc_enum_fmt_cap
= cafe_vidioc_enum_fmt_cap
,
1741 .vidioc_try_fmt_cap
= cafe_vidioc_try_fmt_cap
,
1742 .vidioc_s_fmt_cap
= cafe_vidioc_s_fmt_cap
,
1743 .vidioc_g_fmt_cap
= cafe_vidioc_g_fmt_cap
,
1744 .vidioc_enum_input
= cafe_vidioc_enum_input
,
1745 .vidioc_g_input
= cafe_vidioc_g_input
,
1746 .vidioc_s_input
= cafe_vidioc_s_input
,
1747 .vidioc_s_std
= cafe_vidioc_s_std
,
1748 .vidioc_reqbufs
= cafe_vidioc_reqbufs
,
1749 .vidioc_querybuf
= cafe_vidioc_querybuf
,
1750 .vidioc_qbuf
= cafe_vidioc_qbuf
,
1751 .vidioc_dqbuf
= cafe_vidioc_dqbuf
,
1752 .vidioc_streamon
= cafe_vidioc_streamon
,
1753 .vidioc_streamoff
= cafe_vidioc_streamoff
,
1754 .vidioc_queryctrl
= cafe_vidioc_queryctrl
,
1755 .vidioc_g_ctrl
= cafe_vidioc_g_ctrl
,
1756 .vidioc_s_ctrl
= cafe_vidioc_s_ctrl
,
1757 .vidioc_g_parm
= cafe_vidioc_g_parm
,
1758 .vidioc_s_parm
= cafe_vidioc_s_parm
,
1767 /* ---------------------------------------------------------------------- */
1769 * Interrupt handler stuff
1774 static void cafe_frame_tasklet(unsigned long data
)
1776 struct cafe_camera
*cam
= (struct cafe_camera
*) data
;
1778 unsigned long flags
;
1779 struct cafe_sio_buffer
*sbuf
;
1781 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1782 for (i
= 0; i
< cam
->nbufs
; i
++) {
1783 int bufno
= cam
->next_buf
;
1784 if (bufno
< 0) { /* "will never happen" */
1785 cam_err(cam
, "No valid bufs in tasklet!\n");
1788 if (++(cam
->next_buf
) >= cam
->nbufs
)
1790 if (! test_bit(bufno
, &cam
->flags
))
1792 if (list_empty(&cam
->sb_avail
))
1793 break; /* Leave it valid, hope for better later */
1794 clear_bit(bufno
, &cam
->flags
);
1796 * We could perhaps drop the spinlock during this
1797 * big copy. Something to consider.
1799 sbuf
= list_entry(cam
->sb_avail
.next
,
1800 struct cafe_sio_buffer
, list
);
1801 memcpy(sbuf
->buffer
, cam
->dma_bufs
[bufno
],
1802 cam
->pix_format
.sizeimage
);
1803 sbuf
->v4lbuf
.bytesused
= cam
->pix_format
.sizeimage
;
1804 sbuf
->v4lbuf
.sequence
= cam
->buf_seq
[bufno
];
1805 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1806 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1807 list_move_tail(&sbuf
->list
, &cam
->sb_full
);
1809 if (! list_empty(&cam
->sb_full
))
1810 wake_up(&cam
->iowait
);
1811 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1816 static void cafe_frame_complete(struct cafe_camera
*cam
, int frame
)
1819 * Basic frame housekeeping.
1821 if (test_bit(frame
, &cam
->flags
) && printk_ratelimit())
1822 cam_err(cam
, "Frame overrun on %d, frames lost\n", frame
);
1823 set_bit(frame
, &cam
->flags
);
1824 clear_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1825 if (cam
->next_buf
< 0)
1826 cam
->next_buf
= frame
;
1827 cam
->buf_seq
[frame
] = ++(cam
->sequence
);
1829 switch (cam
->state
) {
1831 * If in single read mode, try going speculative.
1834 cam
->state
= S_SPECREAD
;
1835 cam
->specframes
= 0;
1836 wake_up(&cam
->iowait
);
1840 * If we are already doing speculative reads, and nobody is
1841 * reading them, just stop.
1844 if (++(cam
->specframes
) >= cam
->nbufs
) {
1845 cafe_ctlr_stop(cam
);
1846 cafe_ctlr_irq_disable(cam
);
1847 cam
->state
= S_IDLE
;
1849 wake_up(&cam
->iowait
);
1852 * For the streaming case, we defer the real work to the
1855 * FIXME: if the application is not consuming the buffers,
1856 * we should eventually put things on hold and restart in
1860 tasklet_schedule(&cam
->s_tasklet
);
1864 cam_err(cam
, "Frame interrupt in non-operational state\n");
1872 static void cafe_frame_irq(struct cafe_camera
*cam
, unsigned int irqs
)
1876 cafe_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
); /* Clear'em all */
1878 * Handle any frame completions. There really should
1879 * not be more than one of these, or we have fallen
1882 for (frame
= 0; frame
< cam
->nbufs
; frame
++)
1883 if (irqs
& (IRQ_EOF0
<< frame
))
1884 cafe_frame_complete(cam
, frame
);
1886 * If a frame starts, note that we have DMA active. This
1887 * code assumes that we won't get multiple frame interrupts
1888 * at once; may want to rethink that.
1890 if (irqs
& (IRQ_SOF0
| IRQ_SOF1
| IRQ_SOF2
))
1891 set_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1896 static irqreturn_t
cafe_irq(int irq
, void *data
)
1898 struct cafe_camera
*cam
= data
;
1901 spin_lock(&cam
->dev_lock
);
1902 irqs
= cafe_reg_read(cam
, REG_IRQSTAT
);
1903 if ((irqs
& ALLIRQS
) == 0) {
1904 spin_unlock(&cam
->dev_lock
);
1907 if (irqs
& FRAMEIRQS
)
1908 cafe_frame_irq(cam
, irqs
);
1909 if (irqs
& TWSIIRQS
) {
1910 cafe_reg_write(cam
, REG_IRQSTAT
, TWSIIRQS
);
1911 wake_up(&cam
->smbus_wait
);
1913 spin_unlock(&cam
->dev_lock
);
1918 /* -------------------------------------------------------------------------- */
1919 #ifdef CONFIG_VIDEO_ADV_DEBUG
1924 static char cafe_debug_buf
[1024];
1925 static struct dentry
*cafe_dfs_root
;
1927 static void cafe_dfs_setup(void)
1929 cafe_dfs_root
= debugfs_create_dir("cafe_ccic", NULL
);
1930 if (IS_ERR(cafe_dfs_root
)) {
1931 cafe_dfs_root
= NULL
; /* Never mind */
1932 printk(KERN_NOTICE
"cafe_ccic unable to set up debugfs\n");
1936 static void cafe_dfs_shutdown(void)
1939 debugfs_remove(cafe_dfs_root
);
1942 static int cafe_dfs_open(struct inode
*inode
, struct file
*file
)
1944 file
->private_data
= inode
->i_private
;
1948 static ssize_t
cafe_dfs_read_regs(struct file
*file
,
1949 char __user
*buf
, size_t count
, loff_t
*ppos
)
1951 struct cafe_camera
*cam
= file
->private_data
;
1952 char *s
= cafe_debug_buf
;
1955 for (offset
= 0; offset
< 0x44; offset
+= 4)
1956 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1957 cafe_reg_read(cam
, offset
));
1958 for (offset
= 0x88; offset
<= 0x90; offset
+= 4)
1959 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1960 cafe_reg_read(cam
, offset
));
1961 for (offset
= 0xb4; offset
<= 0xbc; offset
+= 4)
1962 s
+= sprintf(s
, "%02x: %08x\n", offset
,
1963 cafe_reg_read(cam
, offset
));
1964 for (offset
= 0x3000; offset
<= 0x300c; offset
+= 4)
1965 s
+= sprintf(s
, "%04x: %08x\n", offset
,
1966 cafe_reg_read(cam
, offset
));
1967 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1968 s
- cafe_debug_buf
);
1971 static const struct file_operations cafe_dfs_reg_ops
= {
1972 .owner
= THIS_MODULE
,
1973 .read
= cafe_dfs_read_regs
,
1974 .open
= cafe_dfs_open
1977 static ssize_t
cafe_dfs_read_cam(struct file
*file
,
1978 char __user
*buf
, size_t count
, loff_t
*ppos
)
1980 struct cafe_camera
*cam
= file
->private_data
;
1981 char *s
= cafe_debug_buf
;
1986 for (offset
= 0x0; offset
< 0x8a; offset
++)
1990 cafe_smbus_read_data(cam
, cam
->sensor
->addr
, offset
, &v
);
1991 s
+= sprintf(s
, "%02x: %02x\n", offset
, v
);
1993 return simple_read_from_buffer(buf
, count
, ppos
, cafe_debug_buf
,
1994 s
- cafe_debug_buf
);
1997 static const struct file_operations cafe_dfs_cam_ops
= {
1998 .owner
= THIS_MODULE
,
1999 .read
= cafe_dfs_read_cam
,
2000 .open
= cafe_dfs_open
2005 static void cafe_dfs_cam_setup(struct cafe_camera
*cam
)
2011 sprintf(fname
, "regs-%d", cam
->v4ldev
.minor
);
2012 cam
->dfs_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2013 cam
, &cafe_dfs_reg_ops
);
2014 sprintf(fname
, "cam-%d", cam
->v4ldev
.minor
);
2015 cam
->dfs_cam_regs
= debugfs_create_file(fname
, 0444, cafe_dfs_root
,
2016 cam
, &cafe_dfs_cam_ops
);
2020 static void cafe_dfs_cam_shutdown(struct cafe_camera
*cam
)
2022 if (! IS_ERR(cam
->dfs_regs
))
2023 debugfs_remove(cam
->dfs_regs
);
2024 if (! IS_ERR(cam
->dfs_cam_regs
))
2025 debugfs_remove(cam
->dfs_cam_regs
);
2030 #define cafe_dfs_setup()
2031 #define cafe_dfs_shutdown()
2032 #define cafe_dfs_cam_setup(cam)
2033 #define cafe_dfs_cam_shutdown(cam)
2034 #endif /* CONFIG_VIDEO_ADV_DEBUG */
2039 /* ------------------------------------------------------------------------*/
2041 * PCI interface stuff.
2044 static int cafe_pci_probe(struct pci_dev
*pdev
,
2045 const struct pci_device_id
*id
)
2049 struct cafe_camera
*cam
;
2051 * Make sure we have a camera here - we'll get calls for
2052 * the other cafe devices as well.
2054 pci_read_config_word(pdev
, PCI_CLASS_DEVICE
, &classword
);
2055 if (classword
!= PCI_CLASS_MULTIMEDIA_VIDEO
)
2058 * Start putting together one of our big camera structures.
2061 cam
= kzalloc(sizeof(struct cafe_camera
), GFP_KERNEL
);
2064 mutex_init(&cam
->s_mutex
);
2065 mutex_lock(&cam
->s_mutex
);
2066 spin_lock_init(&cam
->dev_lock
);
2067 cam
->state
= S_NOTREADY
;
2068 cafe_set_config_needed(cam
, 1);
2069 init_waitqueue_head(&cam
->smbus_wait
);
2070 init_waitqueue_head(&cam
->iowait
);
2072 cam
->pix_format
= cafe_def_pix_format
;
2073 INIT_LIST_HEAD(&cam
->dev_list
);
2074 INIT_LIST_HEAD(&cam
->sb_avail
);
2075 INIT_LIST_HEAD(&cam
->sb_full
);
2076 tasklet_init(&cam
->s_tasklet
, cafe_frame_tasklet
, (unsigned long) cam
);
2078 * Get set up on the PCI bus.
2080 ret
= pci_enable_device(pdev
);
2083 pci_set_master(pdev
);
2086 cam
->regs
= pci_iomap(pdev
, 0, 0);
2088 printk(KERN_ERR
"Unable to ioremap cafe-ccic regs\n");
2091 ret
= request_irq(pdev
->irq
, cafe_irq
, IRQF_SHARED
, "cafe-ccic", cam
);
2094 cafe_ctlr_init(cam
);
2095 cafe_ctlr_power_up(cam
);
2097 * Set up I2C/SMBUS communications
2099 mutex_unlock(&cam
->s_mutex
); /* attach can deadlock */
2100 ret
= cafe_smbus_setup(cam
);
2104 * Get the v4l2 setup done.
2106 mutex_lock(&cam
->s_mutex
);
2107 cam
->v4ldev
= cafe_v4l_template
;
2108 cam
->v4ldev
.debug
= 0;
2109 // cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2110 ret
= video_register_device(&cam
->v4ldev
, VFL_TYPE_GRABBER
, -1);
2114 * If so requested, try to get our DMA buffers now.
2116 if (alloc_bufs_at_load
) {
2117 if (cafe_alloc_dma_bufs(cam
, 1))
2118 cam_warn(cam
, "Unable to alloc DMA buffers at load"
2119 " will try again later.");
2122 cafe_dfs_cam_setup(cam
);
2123 mutex_unlock(&cam
->s_mutex
);
2128 cafe_smbus_shutdown(cam
);
2130 cafe_ctlr_power_down(cam
);
2131 free_irq(pdev
->irq
, cam
);
2133 pci_iounmap(pdev
, cam
->regs
);
2142 * Shut down an initialized device
2144 static void cafe_shutdown(struct cafe_camera
*cam
)
2146 /* FIXME: Make sure we take care of everything here */
2147 cafe_dfs_cam_shutdown(cam
);
2148 if (cam
->n_sbufs
> 0)
2149 /* What if they are still mapped? Shouldn't be, but... */
2150 cafe_free_sio_buffers(cam
);
2151 cafe_remove_dev(cam
);
2152 cafe_ctlr_stop_dma(cam
);
2153 cafe_ctlr_power_down(cam
);
2154 cafe_smbus_shutdown(cam
);
2155 cafe_free_dma_bufs(cam
);
2156 free_irq(cam
->pdev
->irq
, cam
);
2157 pci_iounmap(cam
->pdev
, cam
->regs
);
2158 video_unregister_device(&cam
->v4ldev
);
2159 /* kfree(cam); done in v4l_release () */
2163 static void cafe_pci_remove(struct pci_dev
*pdev
)
2165 struct cafe_camera
*cam
= cafe_find_by_pdev(pdev
);
2168 printk(KERN_WARNING
"pci_remove on unknown pdev %p\n", pdev
);
2171 mutex_lock(&cam
->s_mutex
);
2173 cam_warn(cam
, "Removing a device with users!\n");
2175 /* No unlock - it no longer exists */
2181 static struct pci_device_id cafe_ids
[] = {
2182 { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
2183 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2184 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2188 MODULE_DEVICE_TABLE(pci
, cafe_ids
);
2190 static struct pci_driver cafe_pci_driver
= {
2191 .name
= "cafe1000-ccic",
2192 .id_table
= cafe_ids
,
2193 .probe
= cafe_pci_probe
,
2194 .remove
= cafe_pci_remove
,
2200 static int __init
cafe_init(void)
2204 printk(KERN_NOTICE
"Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2207 ret
= pci_register_driver(&cafe_pci_driver
);
2209 printk(KERN_ERR
"Unable to register cafe_ccic driver\n");
2212 request_module("ov7670"); /* FIXME want something more general */
2220 static void __exit
cafe_exit(void)
2222 pci_unregister_driver(&cafe_pci_driver
);
2223 cafe_dfs_shutdown();
2226 module_init(cafe_init
);
2227 module_exit(cafe_exit
);