aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/video4linux/cafe_ccic54
-rw-r--r--drivers/media/video/Kconfig9
-rw-r--r--drivers/media/video/Makefile2
-rw-r--r--drivers/media/video/cafe_ccic-regs.h160
-rw-r--r--drivers/media/video/cafe_ccic.c2237
-rw-r--r--include/linux/i2c-id.h1
6 files changed, 2463 insertions, 0 deletions
diff --git a/Documentation/video4linux/cafe_ccic b/Documentation/video4linux/cafe_ccic
new file mode 100644
index 000000000000..88821022a5de
--- /dev/null
+++ b/Documentation/video4linux/cafe_ccic
@@ -0,0 +1,54 @@
1"cafe_ccic" is a driver for the Marvell 88ALP01 "cafe" CMOS camera
2controller. This is the controller found in first-generation OLPC systems,
3and this driver was written with support from the OLPC project.
4
5Current status: the core driver works. It can generate data in YUV422,
6RGB565, and RGB444 formats. (Anybody looking at the code will see RGB32 as
7well, but that is a debugging aid which will be removed shortly). VGA and
8QVGA modes work; CIF is there but the colors remain funky. Only the OV7670
9sensor is known to work with this controller at this time.
10
11To try it out: either of these commands will work:
12
13 mplayer tv:// -tv driver=v4l2:width=640:height=480 -nosound
14 mplayer tv:// -tv driver=v4l2:width=640:height=480:outfmt=bgr16 -nosound
15
16The "xawtv" utility also works; gqcam does not, for unknown reasons.
17
18There are a few load-time options, most of which can be changed after
19loading via sysfs as well:
20
21 - alloc_bufs_at_load: Normally, the driver will not allocate any DMA
22 buffers until the time comes to transfer data. If this option is set,
23 then worst-case-sized buffers will be allocated at module load time.
24 This option nails down the memory for the life of the module, but
25 perhaps decreases the chances of an allocation failure later on.
26
27 - dma_buf_size: The size of DMA buffers to allocate. Note that this
28 option is only consulted for load-time allocation; when buffers are
29 allocated at run time, they will be sized appropriately for the current
30 camera settings.
31
32 - n_dma_bufs: The controller can cycle through either two or three DMA
33 buffers. Normally, the driver tries to use three buffers; on faster
34 systems, however, it will work well with only two.
35
36 - min_buffers: The minimum number of streaming I/O buffers that the driver
37 will consent to work with. Default is one, but, on slower systems,
38 better behavior with mplayer can be achieved by setting to a higher
39 value (like six).
40
41 - max_buffers: The maximum number of streaming I/O buffers; default is
42 ten. That number was carefully picked out of a hat and should not be
43 assumed to actually mean much of anything.
44
45 - flip: If this boolean parameter is set, the sensor will be instructed to
46 invert the video image. Whether it makes sense is determined by how
47 your particular camera is mounted.
48
49Work is ongoing with this driver, stay tuned.
50
51jon
52
53Jonathan Corbet
54corbet@lwn.net
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index b8fde5cf4735..4ea1d0ebf5fd 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -670,6 +670,15 @@ config VIDEO_M32R_AR_M64278
670 To compile this driver as a module, choose M here: the 670 To compile this driver as a module, choose M here: the
671 module will be called arv. 671 module will be called arv.
672 672
673config VIDEO_CAFE_CCIC
674 tristate "Marvell 88ALP01 (Cafe) CMOS Camera Controller support"
675 depends on I2C && VIDEO_V4L2
676 select VIDEO_OV7670
677 ---help---
678 This is a video4linux2 driver for the Marvell 88ALP01 integrated
679 CMOS camera controller. This is the controller found on first-
680 generation OLPC systems.
681
673# 682#
674# USB Multimedia device configuration 683# USB Multimedia device configuration
675# 684#
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index af57abce8a6e..8ff787a4cf6a 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -92,6 +92,8 @@ obj-$(CONFIG_VIDEO_UPD64031A) += upd64031a.o
92obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o 92obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o
93obj-$(CONFIG_VIDEO_CX2341X) += cx2341x.o 93obj-$(CONFIG_VIDEO_CX2341X) += cx2341x.o
94 94
95obj-$(CONFIG_VIDEO_CAFE_CCIC) += cafe_ccic.o
96
95obj-$(CONFIG_USB_DABUSB) += dabusb.o 97obj-$(CONFIG_USB_DABUSB) += dabusb.o
96obj-$(CONFIG_USB_OV511) += ov511.o 98obj-$(CONFIG_USB_OV511) += ov511.o
97obj-$(CONFIG_USB_SE401) += se401.o 99obj-$(CONFIG_USB_SE401) += se401.o
diff --git a/drivers/media/video/cafe_ccic-regs.h b/drivers/media/video/cafe_ccic-regs.h
new file mode 100644
index 000000000000..b2c22a0d6643
--- /dev/null
+++ b/drivers/media/video/cafe_ccic-regs.h
@@ -0,0 +1,160 @@
1/*
2 * Register definitions for the m88alp01 camera interface. Offsets in bytes
3 * as given in the spec.
4 *
5 * Copyright 2006 One Laptop Per Child Association, Inc.
6 *
7 * Written by Jonathan Corbet, corbet@lwn.net.
8 *
9 * This file may be distributed under the terms of the GNU General
10 * Public License, version 2.
11 */
12#define REG_Y0BAR 0x00
13#define REG_Y1BAR 0x04
14#define REG_Y2BAR 0x08
15/* ... */
16
17#define REG_IMGPITCH 0x24 /* Image pitch register */
18#define IMGP_YP_SHFT 2 /* Y pitch params */
19#define IMGP_YP_MASK 0x00003ffc /* Y pitch field */
20#define IMGP_UVP_SHFT 18 /* UV pitch (planar) */
21#define IMGP_UVP_MASK 0x3ffc0000
22#define REG_IRQSTATRAW 0x28 /* RAW IRQ Status */
23#define IRQ_EOF0 0x00000001 /* End of frame 0 */
24#define IRQ_EOF1 0x00000002 /* End of frame 1 */
25#define IRQ_EOF2 0x00000004 /* End of frame 2 */
26#define IRQ_SOF0 0x00000008 /* Start of frame 0 */
27#define IRQ_SOF1 0x00000010 /* Start of frame 1 */
28#define IRQ_SOF2 0x00000020 /* Start of frame 2 */
29#define IRQ_OVERFLOW 0x00000040 /* FIFO overflow */
30#define IRQ_TWSIW 0x00010000 /* TWSI (smbus) write */
31#define IRQ_TWSIR 0x00020000 /* TWSI read */
32#define IRQ_TWSIE 0x00040000 /* TWSI error */
33#define TWSIIRQS (IRQ_TWSIW|IRQ_TWSIR|IRQ_TWSIE)
34#define FRAMEIRQS (IRQ_EOF0|IRQ_EOF1|IRQ_EOF2|IRQ_SOF0|IRQ_SOF1|IRQ_SOF2)
35#define ALLIRQS (TWSIIRQS|FRAMEIRQS|IRQ_OVERFLOW)
36#define REG_IRQMASK 0x2c /* IRQ mask - same bits as IRQSTAT */
37#define REG_IRQSTAT 0x30 /* IRQ status / clear */
38
39#define REG_IMGSIZE 0x34 /* Image size */
40#define IMGSZ_V_MASK 0x1fff0000
41#define IMGSZ_V_SHIFT 16
42#define IMGSZ_H_MASK 0x00003fff
43#define REG_IMGOFFSET 0x38 /* IMage offset */
44
45#define REG_CTRL0 0x3c /* Control 0 */
46#define C0_ENABLE 0x00000001 /* Makes the whole thing go */
47
48/* Mask for all the format bits */
49#define C0_DF_MASK 0x00fffffc /* Bits 2-23 */
50
51/* RGB ordering */
52#define C0_RGB4_RGBX 0x00000000
53#define C0_RGB4_XRGB 0x00000004
54#define C0_RGB4_BGRX 0x00000008
55#define C0_RGB4_XBGR 0x0000000c
56#define C0_RGB5_RGGB 0x00000000
57#define C0_RGB5_GRBG 0x00000004
58#define C0_RGB5_GBRG 0x00000008
59#define C0_RGB5_BGGR 0x0000000c
60
61/* Spec has two fields for DIN and DOUT, but they must match, so
62 combine them here. */
63#define C0_DF_YUV 0x00000000 /* Data is YUV */
64#define C0_DF_RGB 0x000000a0 /* ... RGB */
65#define C0_DF_BAYER 0x00000140 /* ... Bayer */
66/* 8-8-8 must be missing from the below - ask */
67#define C0_RGBF_565 0x00000000
68#define C0_RGBF_444 0x00000800
69#define C0_RGB_BGR 0x00001000 /* Blue comes first */
70#define C0_YUV_PLANAR 0x00000000 /* YUV 422 planar format */
71#define C0_YUV_PACKED 0x00008000 /* YUV 422 packed */
72#define C0_YUV_420PL 0x0000a000 /* YUV 420 planar */
73/* Think that 420 packed must be 111 - ask */
74#define C0_YUVE_YUYV 0x00000000 /* Y1CbY0Cr */
75#define C0_YUVE_YVYU 0x00010000 /* Y1CrY0Cb */
76#define C0_YUVE_VYUY 0x00020000 /* CrY1CbY0 */
77#define C0_YUVE_UYVY 0x00030000 /* CbY1CrY0 */
78#define C0_YUVE_XYUV 0x00000000 /* 420: .YUV */
79#define C0_YUVE_XYVU 0x00010000 /* 420: .YVU */
80#define C0_YUVE_XUVY 0x00020000 /* 420: .UVY */
81#define C0_YUVE_XVUY 0x00030000 /* 420: .VUY */
82/* Bayer bits 18,19 if needed */
83#define C0_HPOL_LOW 0x01000000 /* HSYNC polarity active low */
84#define C0_VPOL_LOW 0x02000000 /* VSYNC polarity active low */
85#define C0_VCLK_LOW 0x04000000 /* VCLK on falling edge */
86#define C0_DOWNSCALE 0x08000000 /* Enable downscaler */
87#define C0_SIFM_MASK 0xc0000000 /* SIF mode bits */
88#define C0_SIF_HVSYNC 0x00000000 /* Use H/VSYNC */
89#define CO_SOF_NOSYNC 0x40000000 /* Use inband active signaling */
90
91
92#define REG_CTRL1 0x40 /* Control 1 */
93#define C1_444ALPHA 0x00f00000 /* Alpha field in RGB444 */
94#define C1_ALPHA_SHFT 20
95#define C1_DMAB32 0x00000000 /* 32-byte DMA burst */
96#define C1_DMAB16 0x02000000 /* 16-byte DMA burst */
97#define C1_DMAB64 0x04000000 /* 64-byte DMA burst */
98#define C1_DMAB_MASK 0x06000000
99#define C1_TWOBUFS 0x08000000 /* Use only two DMA buffers */
100#define C1_PWRDWN 0x10000000 /* Power down */
101
102#define REG_CLKCTRL 0x88 /* Clock control */
103#define CLK_DIV_MASK 0x0000ffff /* Upper bits RW "reserved" */
104
105#define REG_GPR 0xb4 /* General purpose register. This
106 controls inputs to the power and reset
107 pins on the OV7670 used with OLPC;
108 other deployments could differ. */
109#define GPR_C1EN 0x00000020 /* Pad 1 (power down) enable */
110#define GPR_C0EN 0x00000010 /* Pad 0 (reset) enable */
111#define GPR_C1 0x00000002 /* Control 1 value */
112/*
113 * Control 0 is wired to reset on OLPC machines. For ov7x sensors,
114 * it is active low, for 0v6x, instead, it's active high. What
115 * fun.
116 */
117#define GPR_C0 0x00000001 /* Control 0 value */
118
119#define REG_TWSIC0 0xb8 /* TWSI (smbus) control 0 */
120#define TWSIC0_EN 0x00000001 /* TWSI enable */
121#define TWSIC0_MODE 0x00000002 /* 1 = 16-bit, 0 = 8-bit */
122#define TWSIC0_SID 0x000003fc /* Slave ID */
123#define TWSIC0_SID_SHIFT 2
124#define TWSIC0_CLKDIV 0x0007fc00 /* Clock divider */
125#define TWSIC0_MASKACK 0x00400000 /* Mask ack from sensor */
126#define TWSIC0_OVMAGIC 0x00800000 /* Make it work on OV sensors */
127
128#define REG_TWSIC1 0xbc /* TWSI control 1 */
129#define TWSIC1_DATA 0x0000ffff /* Data to/from camchip */
130#define TWSIC1_ADDR 0x00ff0000 /* Address (register) */
131#define TWSIC1_ADDR_SHIFT 16
132#define TWSIC1_READ 0x01000000 /* Set for read op */
133#define TWSIC1_WSTAT 0x02000000 /* Write status */
134#define TWSIC1_RVALID 0x04000000 /* Read data valid */
135#define TWSIC1_ERROR 0x08000000 /* Something screwed up */
136
137
138#define REG_UBAR 0xc4 /* Upper base address register */
139
140/*
141 * Here's the weird global control registers which are said to live
142 * way up here.
143 */
144#define REG_GL_CSR 0x3004 /* Control/status register */
145#define GCSR_SRS 0x00000001 /* SW Reset set */
146#define GCSR_SRC 0x00000002 /* SW Reset clear */
147#define GCSR_MRS 0x00000004 /* Master reset set */
148#define GCSR_MRC 0x00000008 /* HW Reset clear */
149#define GCSR_CCIC_EN 0x00004000 /* CCIC Clock enable */
150#define REG_GL_IMASK 0x300c /* Interrupt mask register */
151#define GIMSK_CCIC_EN 0x00000004 /* CCIC Interrupt enable */
152
153#define REG_LEN REG_GL_IMASK + 4
154
155
156/*
157 * Useful stuff that probably belongs somewhere global.
158 */
159#define VGA_WIDTH 640
160#define VGA_HEIGHT 480
diff --git a/drivers/media/video/cafe_ccic.c b/drivers/media/video/cafe_ccic.c
new file mode 100644
index 000000000000..2026c4983b26
--- /dev/null
+++ b/drivers/media/video/cafe_ccic.c
@@ -0,0 +1,2237 @@
1/*
2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip. Currently works with the Omnivision OV7670
4 * sensor.
5 *
6 * Copyright 2006 One Laptop Per Child Association, Inc.
7 *
8 * Written by Jonathan Corbet, corbet@lwn.net.
9 *
10 * This file may be distributed under the terms of the GNU General
11 * Public License, version 2.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/init.h>
18#include <linux/fs.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>
33
34#include <asm/uaccess.h>
35#include <asm/io.h>
36
37#include "cafe_ccic-regs.h"
38
39#define CAFE_VERSION 0x000001
40
41
42/*
43 * Parameters.
44 */
45MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
46MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
47MODULE_LICENSE("GPL");
48MODULE_SUPPORTED_DEVICE("Video");
49
50/*
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.
57 *
58 * The controller can cycle through three buffers. We could use
59 * more by flipping pointers around, but it probably makes little
60 * sense.
61 */
62
63#define MAX_DMA_BUFS 3
64static int alloc_bufs_at_load = 0;
65module_param(alloc_bufs_at_load, bool, 0444);
66MODULE_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 "
70 "the outset.");
71
72static int n_dma_bufs = 3;
73module_param(n_dma_bufs, uint, 0644);
74MODULE_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.");
77
78static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
79module_param(dma_buf_size, uint, 0444);
80MODULE_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 "
83 "will be made.");
84
85static int min_buffers = 1;
86module_param(min_buffers, uint, 0644);
87MODULE_PARM_DESC(min_buffers,
88 "The minimum number of streaming I/O buffers we are willing "
89 "to work with.");
90
91static int max_buffers = 10;
92module_param(max_buffers, uint, 0644);
93MODULE_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 "
96 "in vmalloc space.");
97
98static int flip = 0;
99module_param(flip, bool, 0444);
100MODULE_PARM_DESC(flip,
101 "If set, the sensor will be instructed to flip the image "
102 "vertically.");
103
104
105enum cafe_state {
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 */
112};
113
114/*
115 * Tracking of streaming I/O buffers.
116 */
117struct cafe_sio_buffer {
118 struct list_head list;
119 struct v4l2_buffer v4lbuf;
120 char *buffer; /* Where it lives in kernel space */
121 int mapcount;
122 struct cafe_camera *cam;
123};
124
125/*
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.
130 */
131struct cafe_camera
132{
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) */
137
138 /*
139 * Subsystem structures.
140 */
141 struct pci_dev *pdev;
142 struct video_device v4ldev;
143 struct i2c_adapter i2c_adapter;
144 struct i2c_client *sensor;
145
146 unsigned char __iomem *regs;
147 struct list_head dev_list; /* link to other devices */
148
149 /* DMA buffers */
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 */
158
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;
165
166 /* Current operating parameters */
167 enum v4l2_chip_ident sensor_type; /* Currently ov7670 only */
168 struct v4l2_pix_format pix_format;
169
170 /* Locks */
171 struct mutex s_mutex; /* Access to this structure */
172 spinlock_t dev_lock; /* Access to device */
173
174 /* Misc */
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;
180#endif
181};
182
183/*
184 * Status flags. Always manipulated with bit operations.
185 */
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 */
191
192
193
194/*
195 * Start over with DMA buffers - dev_lock needed.
196 */
197static void cafe_reset_buffers(struct cafe_camera *cam)
198{
199 int i;
200
201 cam->next_buf = -1;
202 for (i = 0; i < cam->nbufs; i++)
203 clear_bit(i, &cam->flags);
204 cam->specframes = 0;
205}
206
207static inline int cafe_needs_config(struct cafe_camera *cam)
208{
209 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
210}
211
212static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
213{
214 if (needed)
215 set_bit(CF_CONFIG_NEEDED, &cam->flags);
216 else
217 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
218}
219
220
221
222
223/*
224 * Debugging and related.
225 */
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);
232
233
234/* ---------------------------------------------------------------------*/
235/*
236 * We keep a simple list of known devices to search at open time.
237 */
238static LIST_HEAD(cafe_dev_list);
239static DEFINE_MUTEX(cafe_dev_list_lock);
240
241static void cafe_add_dev(struct cafe_camera *cam)
242{
243 mutex_lock(&cafe_dev_list_lock);
244 list_add_tail(&cam->dev_list, &cafe_dev_list);
245 mutex_unlock(&cafe_dev_list_lock);
246}
247
248static void cafe_remove_dev(struct cafe_camera *cam)
249{
250 mutex_lock(&cafe_dev_list_lock);
251 list_del(&cam->dev_list);
252 mutex_unlock(&cafe_dev_list_lock);
253}
254
255static struct cafe_camera *cafe_find_dev(int minor)
256{
257 struct cafe_camera *cam;
258
259 mutex_lock(&cafe_dev_list_lock);
260 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
261 if (cam->v4ldev.minor == minor)
262 goto done;
263 }
264 cam = NULL;
265 done:
266 mutex_unlock(&cafe_dev_list_lock);
267 return cam;
268}
269
270
271static struct cafe_camera *cafe_find_by_pdev(struct pci_dev *pdev)
272{
273 struct cafe_camera *cam;
274
275 mutex_lock(&cafe_dev_list_lock);
276 list_for_each_entry(cam, &cafe_dev_list, dev_list) {
277 if (cam->pdev == pdev)
278 goto done;
279 }
280 cam = NULL;
281 done:
282 mutex_unlock(&cafe_dev_list_lock);
283 return cam;
284}
285
286
287/* ------------------------------------------------------------------------ */
288/*
289 * Device register I/O
290 */
291static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
292 unsigned int val)
293{
294 iowrite32(val, cam->regs + reg);
295}
296
297static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
298 unsigned int reg)
299{
300 return ioread32(cam->regs + reg);
301}
302
303
304static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
305 unsigned int val, unsigned int mask)
306{
307 unsigned int v = cafe_reg_read(cam, reg);
308
309 v = (v & ~mask) | (val & mask);
310 cafe_reg_write(cam, reg, v);
311}
312
313static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
314 unsigned int reg, unsigned int val)
315{
316 cafe_reg_write_mask(cam, reg, 0, val);
317}
318
319static inline void cafe_reg_set_bit(struct cafe_camera *cam,
320 unsigned int reg, unsigned int val)
321{
322 cafe_reg_write_mask(cam, reg, val, val);
323}
324
325
326
327/* -------------------------------------------------------------------- */
328/*
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.
332 */
333#define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
334
335static int cafe_smbus_write_done(struct cafe_camera *cam)
336{
337 unsigned long flags;
338 int c1;
339
340 /*
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
343 * often.
344 */
345 udelay(20);
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;
350}
351
352static int cafe_smbus_write_data(struct cafe_camera *cam,
353 u16 addr, u8 command, u8 value)
354{
355 unsigned int rval;
356 unsigned long flags;
357
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 */
361 /*
362 * Marvell sez set clkdiv to all 1's for now.
363 */
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 */
371
372 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
373 CAFE_SMBUS_TIMEOUT);
374 spin_lock_irqsave(&cam->dev_lock, flags);
375 rval = cafe_reg_read(cam, REG_TWSIC1);
376 spin_unlock_irqrestore(&cam->dev_lock, flags);
377
378 if (rval & TWSIC1_WSTAT) {
379 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
380 command, value);
381 return -EIO;
382 }
383 if (rval & TWSIC1_ERROR) {
384 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
385 command, value);
386 return -EIO;
387 }
388 return 0;
389}
390
391
392
393static int cafe_smbus_read_done(struct cafe_camera *cam)
394{
395 unsigned long flags;
396 int c1;
397
398 /*
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
401 * often.
402 */
403 udelay(20);
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);
408}
409
410
411
412static int cafe_smbus_read_data(struct cafe_camera *cam,
413 u16 addr, u8 command, u8 *value)
414{
415 unsigned int rval;
416 unsigned long flags;
417
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 */
421 /*
422 * Marvel sez set clkdiv to all 1's for now.
423 */
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);
430
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);
436
437 if (rval & TWSIC1_ERROR) {
438 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
439 return -EIO;
440 }
441 if (! (rval & TWSIC1_RVALID)) {
442 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
443 command);
444 return -EIO;
445 }
446 *value = rval & 0xff;
447 return 0;
448}
449
450/*
451 * Perform a transfer over SMBUS. This thing is called under
452 * the i2c bus lock, so we shouldn't race with ourselves...
453 */
454static 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)
457{
458 struct cafe_camera *cam = i2c_get_adapdata(adapter);
459 int ret = -EINVAL;
460
461 /*
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.
464 */
465 if (cam->sensor && addr != cam->sensor->addr) {
466 cam_err(cam, "funky smbus addr %d\n", addr);
467 return -EINVAL;
468 }
469 /*
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.
472 */
473 if (size != I2C_SMBUS_BYTE_DATA) {
474 cam_err(cam, "funky xfer size %d\n", size);
475 return -EINVAL;
476 }
477
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);
482 return ret;
483}
484
485
486static void cafe_smbus_enable_irq(struct cafe_camera *cam)
487{
488 unsigned long flags;
489
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);
493}
494
495static u32 cafe_smbus_func(struct i2c_adapter *adapter)
496{
497 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
498 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
499}
500
501static struct i2c_algorithm cafe_smbus_algo = {
502 .smbus_xfer = cafe_smbus_xfer,
503 .functionality = cafe_smbus_func
504};
505
506/* Somebody is on the bus */
507static int cafe_cam_init(struct cafe_camera *cam);
508
509static int cafe_smbus_attach(struct i2c_client *client)
510{
511 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
512
513 /*
514 * Don't talk to chips we don't recognize.
515 */
516 cam_err(cam, "smbus_attach id = %d\n", client->driver->id);
517 if (client->driver->id == I2C_DRIVERID_OV7670) {
518 cam->sensor = client;
519 return cafe_cam_init(cam);
520 }
521 return -EINVAL;
522}
523
524static int cafe_smbus_detach(struct i2c_client *client)
525{
526 struct cafe_camera *cam = i2c_get_adapdata(client->adapter);
527
528 if (cam->sensor == client)
529 cam->sensor = NULL; /* Bummer, no camera */
530 return 0;
531}
532
533static int cafe_smbus_setup(struct cafe_camera *cam)
534{
535 struct i2c_adapter *adap = &cam->i2c_adapter;
536 int ret;
537
538 cafe_smbus_enable_irq(cam);
539 adap->id = I2C_HW_SMBUS_CAFE;
540 adap->class = I2C_CLASS_CAM_DIGITAL;
541 adap->owner = THIS_MODULE;
542 adap->client_register = cafe_smbus_attach;
543 adap->client_unregister = cafe_smbus_detach;
544 adap->algo = &cafe_smbus_algo;
545 strcpy(adap->name, "cafe_ccic");
546 i2c_set_adapdata(adap, cam);
547 ret = i2c_add_adapter(adap);
548 if (ret)
549 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
550 return ret;
551}
552
553static void cafe_smbus_shutdown(struct cafe_camera *cam)
554{
555 i2c_del_adapter(&cam->i2c_adapter);
556}
557
558
559/* ------------------------------------------------------------------- */
560/*
561 * Deal with the controller.
562 */
563
564/*
565 * Do everything we think we need to have the interface operating
566 * according to the desired format.
567 */
568static void cafe_ctlr_dma(struct cafe_camera *cam)
569{
570 /*
571 * Store the first two Y buffers (we aren't supporting
572 * planar formats for now, so no UV bufs). Then either
573 * set the third if it exists, or tell the controller
574 * to just use two.
575 */
576 cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
577 cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
578 if (cam->nbufs > 2) {
579 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
580 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
581 }
582 else
583 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
584 cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
585}
586
587static void cafe_ctlr_image(struct cafe_camera *cam)
588{
589 int imgsz;
590 struct v4l2_pix_format *fmt = &cam->pix_format;
591
592 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
593 (fmt->bytesperline & IMGSZ_H_MASK);
594 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
595 cafe_reg_write(cam, REG_IMGOFFSET, 0);
596 /* YPITCH just drops the last two bits */
597 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
598 IMGP_YP_MASK);
599 /*
600 * Tell the controller about the image format we are using.
601 */
602 switch (cam->pix_format.pixelformat) {
603 case V4L2_PIX_FMT_YUYV:
604 cafe_reg_write_mask(cam, REG_CTRL0,
605 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
606 C0_DF_MASK);
607 break;
608
609 /*
610 * For "fake rgb32" get the image pitch right.
611 */
612 case V4L2_PIX_FMT_RGB32:
613 cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline/2,
614 IMGP_YP_MASK);
615 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
616 ((fmt->bytesperline/2) & IMGSZ_H_MASK);
617 cafe_reg_write(cam, REG_IMGSIZE, imgsz);
618 /* fall into ... */
619 case V4L2_PIX_FMT_RGB444:
620 cafe_reg_write_mask(cam, REG_CTRL0,
621 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
622 C0_DF_MASK);
623 /* Alpha value? */
624 break;
625
626 case V4L2_PIX_FMT_RGB565:
627 cafe_reg_write_mask(cam, REG_CTRL0,
628 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
629 C0_DF_MASK);
630 break;
631
632 default:
633 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
634 break;
635 }
636 /*
637 * Make sure it knows we want to use hsync/vsync.
638 */
639 cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
640 C0_SIFM_MASK);
641}
642
643
644/*
645 * Configure the controller for operation; caller holds the
646 * device mutex.
647 */
648static int cafe_ctlr_configure(struct cafe_camera *cam)
649{
650 unsigned long flags;
651
652 spin_lock_irqsave(&cam->dev_lock, flags);
653 cafe_ctlr_dma(cam);
654 cafe_ctlr_image(cam);
655 cafe_set_config_needed(cam, 0);
656 spin_unlock_irqrestore(&cam->dev_lock, flags);
657 return 0;
658}
659
660static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
661{
662 /*
663 * Clear any pending interrupts, since we do not
664 * expect to have I/O active prior to enabling.
665 */
666 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
667 cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
668}
669
670static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
671{
672 cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
673}
674
675/*
676 * Make the controller start grabbing images. Everything must
677 * be set up before doing this.
678 */
679static void cafe_ctlr_start(struct cafe_camera *cam)
680{
681 /* set_bit performs a read, so no other barrier should be
682 needed here */
683 cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
684}
685
686static void cafe_ctlr_stop(struct cafe_camera *cam)
687{
688 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
689}
690
691static void cafe_ctlr_init(struct cafe_camera *cam)
692{
693 unsigned long flags;
694
695 spin_lock_irqsave(&cam->dev_lock, flags);
696 /*
697 * Added magic to bring up the hardware on the B-Test board
698 */
699 cafe_reg_write(cam, 0x3038, 0x8);
700 cafe_reg_write(cam, 0x315c, 0x80008);
701 /*
702 * Go through the dance needed to wake the device up.
703 * Note that these registers are global and shared
704 * with the NAND and SD devices. Interaction between the
705 * three still needs to be examined.
706 */
707 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
708 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
709 cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
710 mdelay(5); /* FIXME revisit this */
711 cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
712 cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
713 /*
714 * Make sure it's not powered down.
715 */
716 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
717 /*
718 * Turn off the enable bit. It sure should be off anyway,
719 * but it's good to be sure.
720 */
721 cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
722 /*
723 * Mask all interrupts.
724 */
725 cafe_reg_write(cam, REG_IRQMASK, 0);
726 /*
727 * Clock the sensor appropriately. Controller clock should
728 * be 48MHz, sensor "typical" value is half that.
729 */
730 cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
731 spin_unlock_irqrestore(&cam->dev_lock, flags);
732}
733
734
735/*
736 * Stop the controller, and don't return until we're really sure that no
737 * further DMA is going on.
738 */
739static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
740{
741 unsigned long flags;
742
743 /*
744 * Theory: stop the camera controller (whether it is operating
745 * or not). Delay briefly just in case we race with the SOF
746 * interrupt, then wait until no DMA is active.
747 */
748 spin_lock_irqsave(&cam->dev_lock, flags);
749 cafe_ctlr_stop(cam);
750 spin_unlock_irqrestore(&cam->dev_lock, flags);
751 mdelay(1);
752 wait_event_timeout(cam->iowait,
753 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
754 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
755 cam_err(cam, "Timeout waiting for DMA to end\n");
756 /* This would be bad news - what now? */
757 spin_lock_irqsave(&cam->dev_lock, flags);
758 cam->state = S_IDLE;
759 cafe_ctlr_irq_disable(cam);
760 spin_unlock_irqrestore(&cam->dev_lock, flags);
761}
762
763/*
764 * Power up and down.
765 */
766static void cafe_ctlr_power_up(struct cafe_camera *cam)
767{
768 unsigned long flags;
769
770 spin_lock_irqsave(&cam->dev_lock, flags);
771 cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
772 /*
773 * Put the sensor into operational mode (assumes OLPC-style
774 * wiring). Control 0 is reset - set to 1 to operate.
775 * Control 1 is power down, set to 0 to operate.
776 */
777 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
778 mdelay(1); /* Marvell says 1ms will do it */
779 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
780 mdelay(1); /* Enough? */
781 spin_unlock_irqrestore(&cam->dev_lock, flags);
782}
783
784static void cafe_ctlr_power_down(struct cafe_camera *cam)
785{
786 unsigned long flags;
787
788 spin_lock_irqsave(&cam->dev_lock, flags);
789 cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
790 cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
791 spin_unlock_irqrestore(&cam->dev_lock, flags);
792}
793
794/* -------------------------------------------------------------------- */
795/*
796 * Communications with the sensor.
797 */
798
799static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
800{
801 struct i2c_client *sc = cam->sensor;
802 int ret;
803
804 if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
805 return -EINVAL;
806 ret = sc->driver->command(sc, cmd, arg);
807 if (ret == -EPERM) /* Unsupported command */
808 return 0;
809 return ret;
810}
811
812static int __cafe_cam_reset(struct cafe_camera *cam)
813{
814 int zero = 0;
815 return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
816}
817
818/*
819 * We have found the sensor on the i2c. Let's try to have a
820 * conversation.
821 */
822static int cafe_cam_init(struct cafe_camera *cam)
823{
824 int ret;
825
826 mutex_lock(&cam->s_mutex);
827 if (cam->state != S_NOTREADY)
828 cam_warn(cam, "Cam init with device in funky state %d",
829 cam->state);
830 ret = __cafe_cam_reset(cam);
831 if (ret)
832 goto out;
833 ret = __cafe_cam_cmd(cam, VIDIOC_INT_G_CHIP_IDENT, &cam->sensor_type);
834 if (ret)
835 goto out;
836// if (cam->sensor->addr != OV7xx0_SID) {
837 if (cam->sensor_type != V4L2_IDENT_OV7670) {
838 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
839 ret = -EINVAL;
840 goto out;
841 }
842/* Get/set parameters? */
843 ret = 0;
844 cam->state = S_IDLE;
845 out:
846 mutex_unlock(&cam->s_mutex);
847 return ret;
848}
849
850/*
851 * Configure the sensor to match the parameters we have. Caller should
852 * hold s_mutex
853 */
854static int cafe_cam_set_flip(struct cafe_camera *cam)
855{
856 struct v4l2_control ctrl;
857
858 memset(&ctrl, 0, sizeof(ctrl));
859 ctrl.id = V4L2_CID_VFLIP;
860 ctrl.value = flip;
861 return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
862}
863
864
865static int cafe_cam_configure(struct cafe_camera *cam)
866{
867 struct v4l2_format fmt;
868 int ret, zero = 0;
869
870 if (cam->state != S_IDLE)
871 return -EINVAL;
872 fmt.fmt.pix = cam->pix_format;
873 ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
874 if (ret == 0)
875 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
876 /*
877 * OV7670 does weird things if flip is set *before* format...
878 */
879 ret += cafe_cam_set_flip(cam);
880 return ret;
881}
882
883/* -------------------------------------------------------------------- */
884/*
885 * DMA buffer management. These functions need s_mutex held.
886 */
887
888/* FIXME: this is inefficient as hell, since dma_alloc_coherent just
889 * does a get_free_pages() call, and we waste a good chunk of an orderN
890 * allocation. Should try to allocate the whole set in one chunk.
891 */
892static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
893{
894 int i;
895
896 cafe_set_config_needed(cam, 1);
897 if (loadtime)
898 cam->dma_buf_size = dma_buf_size;
899 else {
900 cam->dma_buf_size = cam->pix_format.sizeimage;
901 if (cam->pix_format.pixelformat == V4L2_PIX_FMT_RGB32)
902 cam->dma_buf_size /= 2;
903 }
904 if (n_dma_bufs > 3)
905 n_dma_bufs = 3;
906
907 cam->nbufs = 0;
908 for (i = 0; i < n_dma_bufs; i++) {
909 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
910 cam->dma_buf_size, cam->dma_handles + i,
911 GFP_KERNEL);
912 if (cam->dma_bufs[i] == NULL) {
913 cam_warn(cam, "Failed to allocate DMA buffer\n");
914 break;
915 }
916 /* For debug, remove eventually */
917 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
918 (cam->nbufs)++;
919 }
920
921 switch (cam->nbufs) {
922 case 1:
923 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
924 cam->dma_bufs[0], cam->dma_handles[0]);
925 cam->nbufs = 0;
926 case 0:
927 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
928 return -ENOMEM;
929
930 case 2:
931 if (n_dma_bufs > 2)
932 cam_warn(cam, "Will limp along with only 2 buffers\n");
933 break;
934 }
935 return 0;
936}
937
938static void cafe_free_dma_bufs(struct cafe_camera *cam)
939{
940 int i;
941
942 for (i = 0; i < cam->nbufs; i++) {
943 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
944 cam->dma_bufs[i], cam->dma_handles[i]);
945 cam->dma_bufs[i] = NULL;
946 }
947 cam->nbufs = 0;
948}
949
950
951
952
953
954/* ----------------------------------------------------------------------- */
955/*
956 * Here starts the V4L2 interface code.
957 */
958
959/*
960 * Read an image from the device.
961 */
962static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
963 char __user *buffer, size_t len, loff_t *pos)
964{
965 int bufno;
966 unsigned long flags;
967
968 spin_lock_irqsave(&cam->dev_lock, flags);
969 if (cam->next_buf < 0) {
970 cam_err(cam, "deliver_buffer: No next buffer\n");
971 spin_unlock_irqrestore(&cam->dev_lock, flags);
972 return -EIO;
973 }
974 bufno = cam->next_buf;
975 clear_bit(bufno, &cam->flags);
976 if (++(cam->next_buf) >= cam->nbufs)
977 cam->next_buf = 0;
978 if (! test_bit(cam->next_buf, &cam->flags))
979 cam->next_buf = -1;
980 cam->specframes = 0;
981 spin_unlock_irqrestore(&cam->dev_lock, flags);
982
983 if (len > cam->pix_format.sizeimage)
984 len = cam->pix_format.sizeimage;
985 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
986 return -EFAULT;
987 (*pos) += len;
988 return len;
989}
990
991/*
992 * Get everything ready, and start grabbing frames.
993 */
994static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
995{
996 int ret;
997 unsigned long flags;
998
999 /*
1000 * Configuration. If we still don't have DMA buffers,
1001 * make one last, desperate attempt.
1002 */
1003 if (cam->nbufs == 0)
1004 if (cafe_alloc_dma_bufs(cam, 0))
1005 return -ENOMEM;
1006
1007 if (cafe_needs_config(cam)) {
1008 cafe_cam_configure(cam);
1009 ret = cafe_ctlr_configure(cam);
1010 if (ret)
1011 return ret;
1012 }
1013
1014 /*
1015 * Turn it loose.
1016 */
1017 spin_lock_irqsave(&cam->dev_lock, flags);
1018 cafe_reset_buffers(cam);
1019 cafe_ctlr_irq_enable(cam);
1020 cam->state = state;
1021 cafe_ctlr_start(cam);
1022 spin_unlock_irqrestore(&cam->dev_lock, flags);
1023 return 0;
1024}
1025
1026
1027static ssize_t cafe_v4l_read(struct file *filp,
1028 char __user *buffer, size_t len, loff_t *pos)
1029{
1030 struct cafe_camera *cam = filp->private_data;
1031 int ret;
1032
1033 /*
1034 * Perhaps we're in speculative read mode and already
1035 * have data?
1036 */
1037 mutex_lock(&cam->s_mutex);
1038 if (cam->state == S_SPECREAD) {
1039 if (cam->next_buf >= 0) {
1040 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1041 if (ret != 0)
1042 goto out_unlock;
1043 }
1044 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1045 ret = -EIO;
1046 goto out_unlock;
1047 } else if (cam->state != S_IDLE) {
1048 ret = -EBUSY;
1049 goto out_unlock;
1050 }
1051
1052 /*
1053 * v4l2: multiple processes can open the device, but only
1054 * one gets to grab data from it.
1055 */
1056 if (cam->owner && cam->owner != filp) {
1057 ret = -EBUSY;
1058 goto out_unlock;
1059 }
1060 cam->owner = filp;
1061
1062 /*
1063 * Do setup if need be.
1064 */
1065 if (cam->state != S_SPECREAD) {
1066 ret = cafe_read_setup(cam, S_SINGLEREAD);
1067 if (ret)
1068 goto out_unlock;
1069 }
1070 /*
1071 * Wait for something to happen. This should probably
1072 * be interruptible (FIXME).
1073 */
1074 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1075 if (cam->next_buf < 0) {
1076 cam_err(cam, "read() operation timed out\n");
1077 cafe_ctlr_stop_dma(cam);
1078 ret = -EIO;
1079 goto out_unlock;
1080 }
1081 /*
1082 * Give them their data and we should be done.
1083 */
1084 ret = cafe_deliver_buffer(cam, buffer, len, pos);
1085
1086 out_unlock:
1087 mutex_unlock(&cam->s_mutex);
1088 return ret;
1089}
1090
1091
1092
1093
1094
1095
1096
1097
1098/*
1099 * Streaming I/O support.
1100 */
1101
1102
1103
1104static int cafe_vidioc_streamon(struct file *filp, void *priv,
1105 enum v4l2_buf_type type)
1106{
1107 struct cafe_camera *cam = filp->private_data;
1108 int ret = -EINVAL;
1109
1110 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1111 goto out;
1112 mutex_lock(&cam->s_mutex);
1113 if (cam->state != S_IDLE || cam->n_sbufs == 0)
1114 goto out_unlock;
1115
1116 cam->sequence = 0;
1117 ret = cafe_read_setup(cam, S_STREAMING);
1118
1119 out_unlock:
1120 mutex_unlock(&cam->s_mutex);
1121 out:
1122 return ret;
1123}
1124
1125
1126static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1127 enum v4l2_buf_type type)
1128{
1129 struct cafe_camera *cam = filp->private_data;
1130 int ret = -EINVAL;
1131
1132 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1133 goto out;
1134 mutex_lock(&cam->s_mutex);
1135 if (cam->state != S_STREAMING)
1136 goto out_unlock;
1137
1138 cafe_ctlr_stop_dma(cam);
1139 ret = 0;
1140
1141 out_unlock:
1142 mutex_unlock(&cam->s_mutex);
1143 out:
1144 return ret;
1145}
1146
1147
1148
1149static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1150{
1151 struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1152
1153 INIT_LIST_HEAD(&buf->list);
1154 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1155 buf->buffer = vmalloc_user(buf->v4lbuf.length);
1156 if (buf->buffer == NULL)
1157 return -ENOMEM;
1158 buf->mapcount = 0;
1159 buf->cam = cam;
1160
1161 buf->v4lbuf.index = index;
1162 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1163 buf->v4lbuf.field = V4L2_FIELD_NONE;
1164 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1165 /*
1166 * Offset: must be 32-bit even on a 64-bit system. video-buf
1167 * just uses the length times the index, but the spec warns
1168 * against doing just that - vma merging problems. So we
1169 * leave a gap between each pair of buffers.
1170 */
1171 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1172 return 0;
1173}
1174
1175static int cafe_free_sio_buffers(struct cafe_camera *cam)
1176{
1177 int i;
1178
1179 /*
1180 * If any buffers are mapped, we cannot free them at all.
1181 */
1182 for (i = 0; i < cam->n_sbufs; i++)
1183 if (cam->sb_bufs[i].mapcount > 0)
1184 return -EBUSY;
1185 /*
1186 * OK, let's do it.
1187 */
1188 for (i = 0; i < cam->n_sbufs; i++)
1189 vfree(cam->sb_bufs[i].buffer);
1190 cam->n_sbufs = 0;
1191 kfree(cam->sb_bufs);
1192 cam->sb_bufs = NULL;
1193 INIT_LIST_HEAD(&cam->sb_avail);
1194 INIT_LIST_HEAD(&cam->sb_full);
1195 return 0;
1196}
1197
1198
1199
1200static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1201 struct v4l2_requestbuffers *req)
1202{
1203 struct cafe_camera *cam = filp->private_data;
1204 int ret;
1205
1206 /*
1207 * Make sure it's something we can do. User pointers could be
1208 * implemented without great pain, but that's not been done yet.
1209 */
1210 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1211 return -EINVAL;
1212 if (req->memory != V4L2_MEMORY_MMAP)
1213 return -EINVAL;
1214 /*
1215 * If they ask for zero buffers, they really want us to stop streaming
1216 * (if it's happening) and free everything. Should we check owner?
1217 */
1218 mutex_lock(&cam->s_mutex);
1219 if (req->count == 0) {
1220 if (cam->state == S_STREAMING)
1221 cafe_ctlr_stop_dma(cam);
1222 ret = cafe_free_sio_buffers (cam);
1223 goto out;
1224 }
1225 /*
1226 * Device needs to be idle and working. We *could* try to do the
1227 * right thing in S_SPECREAD by shutting things down, but it
1228 * probably doesn't matter.
1229 */
1230 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1231 ret = -EBUSY;
1232 goto out;
1233 }
1234 cam->owner = filp;
1235
1236 if (req->count < min_buffers)
1237 req->count = min_buffers;
1238 else if (req->count > max_buffers)
1239 req->count = max_buffers;
1240 if (cam->n_sbufs > 0) {
1241 ret = cafe_free_sio_buffers(cam);
1242 if (ret)
1243 goto out;
1244 }
1245
1246 cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1247 GFP_KERNEL);
1248 if (cam->sb_bufs == NULL) {
1249 ret = -ENOMEM;
1250 goto out;
1251 }
1252 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1253 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1254 if (ret)
1255 break;
1256 }
1257
1258 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
1259 kfree(cam->sb_bufs);
1260 else
1261 ret = 0;
1262 req->count = cam->n_sbufs; /* In case of partial success */
1263
1264 out:
1265 mutex_unlock(&cam->s_mutex);
1266 return ret;
1267}
1268
1269
1270static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1271 struct v4l2_buffer *buf)
1272{
1273 struct cafe_camera *cam = filp->private_data;
1274 int ret = -EINVAL;
1275
1276 mutex_lock(&cam->s_mutex);
1277 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278 goto out;
1279 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1280 goto out;
1281 *buf = cam->sb_bufs[buf->index].v4lbuf;
1282 ret = 0;
1283 out:
1284 mutex_unlock(&cam->s_mutex);
1285 return ret;
1286}
1287
1288static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1289 struct v4l2_buffer *buf)
1290{
1291 struct cafe_camera *cam = filp->private_data;
1292 struct cafe_sio_buffer *sbuf;
1293 int ret = -EINVAL;
1294 unsigned long flags;
1295
1296 mutex_lock(&cam->s_mutex);
1297 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1298 goto out;
1299 if (buf->index < 0 || buf->index >= cam->n_sbufs)
1300 goto out;
1301 sbuf = cam->sb_bufs + buf->index;
1302 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1303 ret = 0; /* Already queued?? */
1304 goto out;
1305 }
1306 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1307 /* Spec doesn't say anything, seems appropriate tho */
1308 ret = -EBUSY;
1309 goto out;
1310 }
1311 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1312 spin_lock_irqsave(&cam->dev_lock, flags);
1313 list_add(&sbuf->list, &cam->sb_avail);
1314 spin_unlock_irqrestore(&cam->dev_lock, flags);
1315 ret = 0;
1316 out:
1317 mutex_unlock(&cam->s_mutex);
1318 return ret;
1319}
1320
1321static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1322 struct v4l2_buffer *buf)
1323{
1324 struct cafe_camera *cam = filp->private_data;
1325 struct cafe_sio_buffer *sbuf;
1326 int ret = -EINVAL;
1327 unsigned long flags;
1328
1329 mutex_lock(&cam->s_mutex);
1330 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1331 goto out_unlock;
1332 if (cam->state != S_STREAMING)
1333 goto out_unlock;
1334 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1335 ret = -EAGAIN;
1336 goto out_unlock;
1337 }
1338
1339 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1340 mutex_unlock(&cam->s_mutex);
1341 if (wait_event_interruptible(cam->iowait,
1342 !list_empty(&cam->sb_full))) {
1343 ret = -ERESTARTSYS;
1344 goto out;
1345 }
1346 mutex_lock(&cam->s_mutex);
1347 }
1348
1349 if (cam->state != S_STREAMING)
1350 ret = -EINTR;
1351 else {
1352 spin_lock_irqsave(&cam->dev_lock, flags);
1353 /* Should probably recheck !list_empty() here */
1354 sbuf = list_entry(cam->sb_full.next,
1355 struct cafe_sio_buffer, list);
1356 list_del_init(&sbuf->list);
1357 spin_unlock_irqrestore(&cam->dev_lock, flags);
1358 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1359 *buf = sbuf->v4lbuf;
1360 ret = 0;
1361 }
1362
1363 out_unlock:
1364 mutex_unlock(&cam->s_mutex);
1365 out:
1366 return ret;
1367}
1368
1369
1370
1371static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1372{
1373 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1374 /*
1375 * Locking: done under mmap_sem, so we don't need to
1376 * go back to the camera lock here.
1377 */
1378 sbuf->mapcount++;
1379}
1380
1381
1382static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1383{
1384 struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1385
1386 mutex_lock(&sbuf->cam->s_mutex);
1387 sbuf->mapcount--;
1388 /* Docs say we should stop I/O too... */
1389 if (sbuf->mapcount == 0)
1390 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1391 mutex_unlock(&sbuf->cam->s_mutex);
1392}
1393
1394static struct vm_operations_struct cafe_v4l_vm_ops = {
1395 .open = cafe_v4l_vm_open,
1396 .close = cafe_v4l_vm_close
1397};
1398
1399
1400static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1401{
1402 struct cafe_camera *cam = filp->private_data;
1403 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1404 int ret = -EINVAL;
1405 int i;
1406 struct cafe_sio_buffer *sbuf = NULL;
1407
1408 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1409 return -EINVAL;
1410 /*
1411 * Find the buffer they are looking for.
1412 */
1413 mutex_lock(&cam->s_mutex);
1414 for (i = 0; i < cam->n_sbufs; i++)
1415 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1416 sbuf = cam->sb_bufs + i;
1417 break;
1418 }
1419 if (sbuf == NULL)
1420 goto out;
1421
1422 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1423 if (ret)
1424 goto out;
1425 vma->vm_flags |= VM_DONTEXPAND;
1426 vma->vm_private_data = sbuf;
1427 vma->vm_ops = &cafe_v4l_vm_ops;
1428 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1429 cafe_v4l_vm_open(vma);
1430 ret = 0;
1431 out:
1432 mutex_unlock(&cam->s_mutex);
1433 return ret;
1434}
1435
1436
1437
1438static int cafe_v4l_open(struct inode *inode, struct file *filp)
1439{
1440 struct cafe_camera *cam;
1441
1442 cam = cafe_find_dev(iminor(inode));
1443 if (cam == NULL)
1444 return -ENODEV;
1445 filp->private_data = cam;
1446
1447 mutex_lock(&cam->s_mutex);
1448 if (cam->users == 0) {
1449 cafe_ctlr_power_up(cam);
1450 __cafe_cam_reset(cam);
1451 cafe_set_config_needed(cam, 1);
1452 /* FIXME make sure this is complete */
1453 }
1454 (cam->users)++;
1455 mutex_unlock(&cam->s_mutex);
1456 return 0;
1457}
1458
1459
1460static int cafe_v4l_release(struct inode *inode, struct file *filp)
1461{
1462 struct cafe_camera *cam = filp->private_data;
1463
1464 mutex_lock(&cam->s_mutex);
1465 (cam->users)--;
1466 if (filp == cam->owner) {
1467 cafe_ctlr_stop_dma(cam);
1468 cafe_free_sio_buffers(cam);
1469 cam->owner = NULL;
1470 }
1471 if (cam->users == 0)
1472 cafe_ctlr_power_down(cam);
1473 mutex_unlock(&cam->s_mutex);
1474 return 0;
1475}
1476
1477
1478
1479static unsigned int cafe_v4l_poll(struct file *filp,
1480 struct poll_table_struct *pt)
1481{
1482 struct cafe_camera *cam = filp->private_data;
1483
1484 poll_wait(filp, &cam->iowait, pt);
1485 if (cam->next_buf >= 0)
1486 return POLLIN | POLLRDNORM;
1487 return 0;
1488}
1489
1490
1491
1492static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1493 struct v4l2_queryctrl *qc)
1494{
1495 struct cafe_camera *cam = filp->private_data;
1496 int ret;
1497
1498 mutex_lock(&cam->s_mutex);
1499 ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1500 mutex_unlock(&cam->s_mutex);
1501 return ret;
1502}
1503
1504
1505static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1506 struct v4l2_control *ctrl)
1507{
1508 struct cafe_camera *cam = filp->private_data;
1509 int ret;
1510
1511 mutex_lock(&cam->s_mutex);
1512 ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1513 mutex_unlock(&cam->s_mutex);
1514 return ret;
1515}
1516
1517
1518static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1519 struct v4l2_control *ctrl)
1520{
1521 struct cafe_camera *cam = filp->private_data;
1522 int ret;
1523
1524 mutex_lock(&cam->s_mutex);
1525 ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1526 mutex_unlock(&cam->s_mutex);
1527 return ret;
1528}
1529
1530
1531
1532
1533
1534static int cafe_vidioc_querycap(struct file *file, void *priv,
1535 struct v4l2_capability *cap)
1536{
1537 strcpy(cap->driver, "cafe_ccic");
1538 strcpy(cap->card, "cafe_ccic");
1539 cap->version = CAFE_VERSION;
1540 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1541 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1542 return 0;
1543}
1544
1545
1546/*
1547 * The default format we use until somebody says otherwise.
1548 */
1549static struct v4l2_pix_format cafe_def_pix_format = {
1550 .width = VGA_WIDTH,
1551 .height = VGA_HEIGHT,
1552 .pixelformat = V4L2_PIX_FMT_YUYV,
1553 .field = V4L2_FIELD_NONE,
1554 .bytesperline = VGA_WIDTH*2,
1555 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
1556};
1557
1558static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1559 void *priv, struct v4l2_fmtdesc *fmt)
1560{
1561 struct cafe_camera *cam = priv;
1562 int ret;
1563
1564 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1565 return -EINVAL;
1566 mutex_lock(&cam->s_mutex);
1567 ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1568 mutex_unlock(&cam->s_mutex);
1569 return ret;
1570}
1571
1572
1573static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1574 struct v4l2_format *fmt)
1575{
1576 struct cafe_camera *cam = priv;
1577 int ret;
1578
1579 mutex_lock(&cam->s_mutex);
1580 ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1581 mutex_unlock(&cam->s_mutex);
1582 return ret;
1583}
1584
1585static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1586 struct v4l2_format *fmt)
1587{
1588 struct cafe_camera *cam = priv;
1589 int ret;
1590
1591 /*
1592 * Can't do anything if the device is not idle
1593 * Also can't if there are streaming buffers in place.
1594 */
1595 if (cam->state != S_IDLE || cam->n_sbufs > 0)
1596 return -EBUSY;
1597 /*
1598 * See if the formatting works in principle.
1599 */
1600 ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1601 if (ret)
1602 return ret;
1603 /*
1604 * Now we start to change things for real, so let's do it
1605 * under lock.
1606 */
1607 mutex_lock(&cam->s_mutex);
1608 cam->pix_format = fmt->fmt.pix;
1609 /*
1610 * Make sure we have appropriate DMA buffers.
1611 */
1612 ret = -ENOMEM;
1613 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1614 cafe_free_dma_bufs(cam);
1615 if (cam->nbufs == 0) {
1616 if (cafe_alloc_dma_bufs(cam, 0))
1617 goto out;
1618 }
1619 /*
1620 * It looks like this might work, so let's program the sensor.
1621 */
1622 ret = cafe_cam_configure(cam);
1623 if (! ret)
1624 ret = cafe_ctlr_configure(cam);
1625 out:
1626 mutex_unlock(&cam->s_mutex);
1627 return ret;
1628}
1629
1630/*
1631 * Return our stored notion of how the camera is/should be configured.
1632 * The V4l2 spec wants us to be smarter, and actually get this from
1633 * the camera (and not mess with it at open time). Someday.
1634 */
1635static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1636 struct v4l2_format *f)
1637{
1638 struct cafe_camera *cam = priv;
1639
1640 f->fmt.pix = cam->pix_format;
1641 return 0;
1642}
1643
1644/*
1645 * We only have one input - the sensor - so minimize the nonsense here.
1646 */
1647static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1648 struct v4l2_input *input)
1649{
1650 if (input->index != 0)
1651 return -EINVAL;
1652
1653 input->type = V4L2_INPUT_TYPE_CAMERA;
1654 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1655 strcpy(input->name, "Camera");
1656 return 0;
1657}
1658
1659static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1660{
1661 *i = 0;
1662 return 0;
1663}
1664
1665static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1666{
1667 if (i != 0)
1668 return -EINVAL;
1669 return 0;
1670}
1671
1672/* from vivi.c */
1673static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id a)
1674{
1675 return 0;
1676}
1677
1678
1679/*
1680 * The TV Norm stuff is weird - we're a camera with little to do with TV,
1681 * really. The following is what vivi does.
1682 */
1683static struct v4l2_tvnorm cafe_tvnorm[] = {
1684 {
1685 .name = "NTSC-M",
1686 .id = V4L2_STD_NTSC_M,
1687 }
1688};
1689
1690
1691void cafe_v4l_dev_release(struct video_device *vd)
1692{
1693 struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1694
1695 kfree(cam);
1696}
1697
1698
1699/*
1700 * This template device holds all of those v4l2 methods; we
1701 * clone it for specific real devices.
1702 */
1703
1704static struct file_operations cafe_v4l_fops = {
1705 .owner = THIS_MODULE,
1706 .open = cafe_v4l_open,
1707 .release = cafe_v4l_release,
1708 .read = cafe_v4l_read,
1709 .poll = cafe_v4l_poll,
1710 .mmap = cafe_v4l_mmap,
1711 .ioctl = video_ioctl2,
1712 .llseek = no_llseek,
1713};
1714
1715static struct video_device cafe_v4l_template = {
1716 .name = "cafe",
1717 .type = VFL_TYPE_GRABBER,
1718 .type2 = VID_TYPE_CAPTURE,
1719 .minor = -1, /* Get one dynamically */
1720 .tvnorms = cafe_tvnorm,
1721 .tvnormsize = 1,
1722 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1723
1724 .fops = &cafe_v4l_fops,
1725 .release = cafe_v4l_dev_release,
1726
1727 .vidioc_querycap = cafe_vidioc_querycap,
1728 .vidioc_enum_fmt_cap = cafe_vidioc_enum_fmt_cap,
1729 .vidioc_try_fmt_cap = cafe_vidioc_try_fmt_cap,
1730 .vidioc_s_fmt_cap = cafe_vidioc_s_fmt_cap,
1731 .vidioc_g_fmt_cap = cafe_vidioc_g_fmt_cap,
1732 .vidioc_enum_input = cafe_vidioc_enum_input,
1733 .vidioc_g_input = cafe_vidioc_g_input,
1734 .vidioc_s_input = cafe_vidioc_s_input,
1735 .vidioc_s_std = cafe_vidioc_s_std,
1736 .vidioc_reqbufs = cafe_vidioc_reqbufs,
1737 .vidioc_querybuf = cafe_vidioc_querybuf,
1738 .vidioc_qbuf = cafe_vidioc_qbuf,
1739 .vidioc_dqbuf = cafe_vidioc_dqbuf,
1740 .vidioc_streamon = cafe_vidioc_streamon,
1741 .vidioc_streamoff = cafe_vidioc_streamoff,
1742 .vidioc_queryctrl = cafe_vidioc_queryctrl,
1743 .vidioc_g_ctrl = cafe_vidioc_g_ctrl,
1744 .vidioc_s_ctrl = cafe_vidioc_s_ctrl,
1745 /* Do cropping someday */
1746};
1747
1748
1749
1750
1751
1752
1753
1754/* ---------------------------------------------------------------------- */
1755/*
1756 * Interrupt handler stuff
1757 */
1758
1759/*
1760 * Create RGB32 from RGB444 so it can be displayed before the applications
1761 * know about the latter format.
1762 */
1763static void cafe_fake_rgb32(struct cafe_camera *cam, char *dest, char *src)
1764{
1765 int i;
1766 u16 *ssrc = (u16 *) src;
1767
1768 /* RGB444 version */
1769 for (i = 0; i < cam->pix_format.sizeimage; i += 4) {
1770 // dest[0] = (*ssrc & 0xf000) >> 8;
1771 dest[0] = (*ssrc & 0x000f) << 4;
1772 dest[1] = (*ssrc & 0x00f0);
1773 dest[2] = (*ssrc & 0x0f00) >> 4;
1774 dest[3] = (*ssrc & 0xf000); /* Alpha */
1775 dest += 4;
1776 ssrc++;
1777 }
1778}
1779
1780
1781static void cafe_frame_tasklet(unsigned long data)
1782{
1783 struct cafe_camera *cam = (struct cafe_camera *) data;
1784 int i;
1785 unsigned long flags;
1786 struct cafe_sio_buffer *sbuf;
1787
1788 spin_lock_irqsave(&cam->dev_lock, flags);
1789 for (i = 0; i < cam->nbufs; i++) {
1790 int bufno = cam->next_buf;
1791 if (bufno < 0) { /* "will never happen" */
1792 cam_err(cam, "No valid bufs in tasklet!\n");
1793 break;
1794 }
1795 if (++(cam->next_buf) >= cam->nbufs)
1796 cam->next_buf = 0;
1797 if (! test_bit(bufno, &cam->flags))
1798 continue;
1799 if (list_empty(&cam->sb_avail))
1800 break; /* Leave it valid, hope for better later */
1801 clear_bit(bufno, &cam->flags);
1802 /*
1803 * We could perhaps drop the spinlock during this
1804 * big copy. Something to consider.
1805 */
1806 sbuf = list_entry(cam->sb_avail.next,
1807 struct cafe_sio_buffer, list);
1808 if (cam->pix_format.pixelformat == V4L2_PIX_FMT_RGB32)
1809 cafe_fake_rgb32(cam, sbuf->buffer, cam->dma_bufs[bufno]);
1810 else
1811 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1812 cam->pix_format.sizeimage);
1813 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1814 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1815 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1816 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1817 list_move_tail(&sbuf->list, &cam->sb_full);
1818 }
1819 if (! list_empty(&cam->sb_full))
1820 wake_up(&cam->iowait);
1821 spin_unlock_irqrestore(&cam->dev_lock, flags);
1822}
1823
1824
1825
1826static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1827{
1828 /*
1829 * Basic frame housekeeping.
1830 */
1831 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1832 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1833 set_bit(frame, &cam->flags);
1834 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1835 if (cam->next_buf < 0)
1836 cam->next_buf = frame;
1837 cam->buf_seq[frame] = ++(cam->sequence);
1838
1839 switch (cam->state) {
1840 /*
1841 * If in single read mode, try going speculative.
1842 */
1843 case S_SINGLEREAD:
1844 cam->state = S_SPECREAD;
1845 cam->specframes = 0;
1846 wake_up(&cam->iowait);
1847 break;
1848
1849 /*
1850 * If we are already doing speculative reads, and nobody is
1851 * reading them, just stop.
1852 */
1853 case S_SPECREAD:
1854 if (++(cam->specframes) >= cam->nbufs) {
1855 cafe_ctlr_stop(cam);
1856 cafe_ctlr_irq_disable(cam);
1857 cam->state = S_IDLE;
1858 }
1859 wake_up(&cam->iowait);
1860 break;
1861 /*
1862 * For the streaming case, we defer the real work to the
1863 * camera tasklet.
1864 *
1865 * FIXME: if the application is not consuming the buffers,
1866 * we should eventually put things on hold and restart in
1867 * vidioc_dqbuf().
1868 */
1869 case S_STREAMING:
1870 tasklet_schedule(&cam->s_tasklet);
1871 break;
1872
1873 default:
1874 cam_err(cam, "Frame interrupt in non-operational state\n");
1875 break;
1876 }
1877}
1878
1879
1880
1881
1882static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1883{
1884 unsigned int frame;
1885
1886 cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1887 /*
1888 * Handle any frame completions. There really should
1889 * not be more than one of these, or we have fallen
1890 * far behind.
1891 */
1892 for (frame = 0; frame < cam->nbufs; frame++)
1893 if (irqs & (IRQ_EOF0 << frame))
1894 cafe_frame_complete(cam, frame);
1895 /*
1896 * If a frame starts, note that we have DMA active. This
1897 * code assumes that we won't get multiple frame interrupts
1898 * at once; may want to rethink that.
1899 */
1900 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1901 set_bit(CF_DMA_ACTIVE, &cam->flags);
1902}
1903
1904
1905
1906static irqreturn_t cafe_irq(int irq, void *data)
1907{
1908 struct cafe_camera *cam = data;
1909 unsigned int irqs;
1910
1911 spin_lock(&cam->dev_lock);
1912 irqs = cafe_reg_read(cam, REG_IRQSTAT);
1913 if ((irqs & ALLIRQS) == 0) {
1914 spin_unlock(&cam->dev_lock);
1915 return IRQ_NONE;
1916 }
1917 if (irqs & FRAMEIRQS)
1918 cafe_frame_irq(cam, irqs);
1919 if (irqs & TWSIIRQS) {
1920 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1921 wake_up(&cam->smbus_wait);
1922 }
1923 spin_unlock(&cam->dev_lock);
1924 return IRQ_HANDLED;
1925}
1926
1927
1928/* -------------------------------------------------------------------------- */
1929#ifdef CONFIG_VIDEO_ADV_DEBUG
1930/*
1931 * Debugfs stuff.
1932 */
1933
1934static char cafe_debug_buf[1024];
1935static struct dentry *cafe_dfs_root;
1936
1937static void cafe_dfs_setup(void)
1938{
1939 cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1940 if (IS_ERR(cafe_dfs_root)) {
1941 cafe_dfs_root = NULL; /* Never mind */
1942 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1943 }
1944}
1945
1946static void cafe_dfs_shutdown(void)
1947{
1948 if (cafe_dfs_root)
1949 debugfs_remove(cafe_dfs_root);
1950}
1951
1952static int cafe_dfs_open(struct inode *inode, struct file *file)
1953{
1954 file->private_data = inode->i_private;
1955 return 0;
1956}
1957
1958static ssize_t cafe_dfs_read_regs(struct file *file,
1959 char __user *buf, size_t count, loff_t *ppos)
1960{
1961 struct cafe_camera *cam = file->private_data;
1962 char *s = cafe_debug_buf;
1963 int offset;
1964
1965 for (offset = 0; offset < 0x44; offset += 4)
1966 s += sprintf(s, "%02x: %08x\n", offset,
1967 cafe_reg_read(cam, offset));
1968 for (offset = 0x88; offset <= 0x90; offset += 4)
1969 s += sprintf(s, "%02x: %08x\n", offset,
1970 cafe_reg_read(cam, offset));
1971 for (offset = 0xb4; offset <= 0xbc; offset += 4)
1972 s += sprintf(s, "%02x: %08x\n", offset,
1973 cafe_reg_read(cam, offset));
1974 for (offset = 0x3000; offset <= 0x300c; offset += 4)
1975 s += sprintf(s, "%04x: %08x\n", offset,
1976 cafe_reg_read(cam, offset));
1977 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1978 s - cafe_debug_buf);
1979}
1980
1981static struct file_operations cafe_dfs_reg_ops = {
1982 .owner = THIS_MODULE,
1983 .read = cafe_dfs_read_regs,
1984 .open = cafe_dfs_open
1985};
1986
1987static ssize_t cafe_dfs_read_cam(struct file *file,
1988 char __user *buf, size_t count, loff_t *ppos)
1989{
1990 struct cafe_camera *cam = file->private_data;
1991 char *s = cafe_debug_buf;
1992 int offset;
1993
1994 if (! cam->sensor)
1995 return -EINVAL;
1996 for (offset = 0x0; offset < 0x8a; offset++)
1997 {
1998 u8 v;
1999
2000 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
2001 s += sprintf(s, "%02x: %02x\n", offset, v);
2002 }
2003 return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
2004 s - cafe_debug_buf);
2005}
2006
2007static struct file_operations cafe_dfs_cam_ops = {
2008 .owner = THIS_MODULE,
2009 .read = cafe_dfs_read_cam,
2010 .open = cafe_dfs_open
2011};
2012
2013
2014
2015static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2016{
2017 char fname[40];
2018
2019 if (!cafe_dfs_root)
2020 return;
2021 sprintf(fname, "regs-%d", cam->v4ldev.minor);
2022 cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2023 cam, &cafe_dfs_reg_ops);
2024 sprintf(fname, "cam-%d", cam->v4ldev.minor);
2025 cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2026 cam, &cafe_dfs_cam_ops);
2027}
2028
2029
2030static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2031{
2032 if (! IS_ERR(cam->dfs_regs))
2033 debugfs_remove(cam->dfs_regs);
2034 if (! IS_ERR(cam->dfs_cam_regs))
2035 debugfs_remove(cam->dfs_cam_regs);
2036}
2037
2038#else
2039
2040#define cafe_dfs_setup()
2041#define cafe_dfs_shutdown()
2042#define cafe_dfs_cam_setup(cam)
2043#define cafe_dfs_cam_shutdown(cam)
2044#endif /* CONFIG_VIDEO_ADV_DEBUG */
2045
2046
2047
2048
2049/* ------------------------------------------------------------------------*/
2050/*
2051 * PCI interface stuff.
2052 */
2053
2054static int cafe_pci_probe(struct pci_dev *pdev,
2055 const struct pci_device_id *id)
2056{
2057 int ret;
2058 u16 classword;
2059 struct cafe_camera *cam;
2060 /*
2061 * Make sure we have a camera here - we'll get calls for
2062 * the other cafe devices as well.
2063 */
2064 pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2065 if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2066 return -ENODEV;
2067 /*
2068 * Start putting together one of our big camera structures.
2069 */
2070 ret = -ENOMEM;
2071 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2072 if (cam == NULL)
2073 goto out;
2074 mutex_init(&cam->s_mutex);
2075 mutex_lock(&cam->s_mutex);
2076 spin_lock_init(&cam->dev_lock);
2077 cam->state = S_NOTREADY;
2078 cafe_set_config_needed(cam, 1);
2079 init_waitqueue_head(&cam->smbus_wait);
2080 init_waitqueue_head(&cam->iowait);
2081 cam->pdev = pdev;
2082 cam->pix_format = cafe_def_pix_format;
2083 INIT_LIST_HEAD(&cam->dev_list);
2084 INIT_LIST_HEAD(&cam->sb_avail);
2085 INIT_LIST_HEAD(&cam->sb_full);
2086 tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2087 /*
2088 * Get set up on the PCI bus.
2089 */
2090 ret = pci_enable_device(pdev);
2091 if (ret)
2092 goto out_free;
2093 pci_set_master(pdev);
2094
2095 ret = -EIO;
2096 cam->regs = pci_iomap(pdev, 0, 0);
2097 if (! cam->regs) {
2098 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2099 goto out_free;
2100 }
2101 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2102 if (ret)
2103 goto out_iounmap;
2104 cafe_ctlr_init(cam);
2105 cafe_ctlr_power_up(cam);
2106 /*
2107 * Set up I2C/SMBUS communications
2108 */
2109 mutex_unlock(&cam->s_mutex); /* attach can deadlock */
2110 ret = cafe_smbus_setup(cam);
2111 if (ret)
2112 goto out_freeirq;
2113 /*
2114 * Get the v4l2 setup done.
2115 */
2116 mutex_lock(&cam->s_mutex);
2117 cam->v4ldev = cafe_v4l_template;
2118 cam->v4ldev.debug = 0;
2119// cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2120 ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2121 if (ret)
2122 goto out_smbus;
2123 /*
2124 * If so requested, try to get our DMA buffers now.
2125 */
2126 if (alloc_bufs_at_load) {
2127 if (cafe_alloc_dma_bufs(cam, 1))
2128 cam_warn(cam, "Unable to alloc DMA buffers at load"
2129 " will try again later.");
2130 }
2131
2132 cafe_dfs_cam_setup(cam);
2133 mutex_unlock(&cam->s_mutex);
2134 cafe_add_dev(cam);
2135 return 0;
2136
2137 out_smbus:
2138 cafe_smbus_shutdown(cam);
2139 out_freeirq:
2140 cafe_ctlr_power_down(cam);
2141 free_irq(pdev->irq, cam);
2142 out_iounmap:
2143 pci_iounmap(pdev, cam->regs);
2144 out_free:
2145 kfree(cam);
2146 out:
2147 return ret;
2148}
2149
2150
2151/*
2152 * Shut down an initialized device
2153 */
2154static void cafe_shutdown(struct cafe_camera *cam)
2155{
2156/* FIXME: Make sure we take care of everything here */
2157 cafe_dfs_cam_shutdown(cam);
2158 if (cam->n_sbufs > 0)
2159 /* What if they are still mapped? Shouldn't be, but... */
2160 cafe_free_sio_buffers(cam);
2161 cafe_remove_dev(cam);
2162 cafe_ctlr_stop_dma(cam);
2163 cafe_ctlr_power_down(cam);
2164 cafe_smbus_shutdown(cam);
2165 cafe_free_dma_bufs(cam);
2166 free_irq(cam->pdev->irq, cam);
2167 pci_iounmap(cam->pdev, cam->regs);
2168 video_unregister_device(&cam->v4ldev);
2169 /* kfree(cam); done in v4l_release () */
2170}
2171
2172
2173static void cafe_pci_remove(struct pci_dev *pdev)
2174{
2175 struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2176
2177 if (cam == NULL) {
2178 cam_warn(cam, "pci_remove on unknown pdev %p\n", pdev);
2179 return;
2180 }
2181 mutex_lock(&cam->s_mutex);
2182 if (cam->users > 0)
2183 cam_warn(cam, "Removing a device with users!\n");
2184 cafe_shutdown(cam);
2185/* No unlock - it no longer exists */
2186}
2187
2188
2189
2190
2191static struct pci_device_id cafe_ids[] = {
2192 { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
2193 { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2194 { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2195 { 0, }
2196};
2197
2198MODULE_DEVICE_TABLE(pci, cafe_ids);
2199
2200static struct pci_driver cafe_pci_driver = {
2201 .name = "cafe1000-ccic",
2202 .id_table = cafe_ids,
2203 .probe = cafe_pci_probe,
2204 .remove = cafe_pci_remove,
2205};
2206
2207
2208
2209
2210static int __init cafe_init(void)
2211{
2212 int ret;
2213
2214 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2215 CAFE_VERSION);
2216 cafe_dfs_setup();
2217 ret = pci_register_driver(&cafe_pci_driver);
2218 if (ret) {
2219 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2220 goto out;
2221 }
2222 request_module("ov7670"); /* FIXME want something more general */
2223 ret = 0;
2224
2225 out:
2226 return ret;
2227}
2228
2229
2230static void __exit cafe_exit(void)
2231{
2232 pci_unregister_driver(&cafe_pci_driver);
2233 cafe_dfs_shutdown();
2234}
2235
2236module_init(cafe_init);
2237module_exit(cafe_exit);
diff --git a/include/linux/i2c-id.h b/include/linux/i2c-id.h
index 0a8f750cbede..a32db163cbc0 100644
--- a/include/linux/i2c-id.h
+++ b/include/linux/i2c-id.h
@@ -250,6 +250,7 @@
250#define I2C_HW_SMBUS_OV518 0x04000f /* OV518(+) USB 1.1 webcam ICs */ 250#define I2C_HW_SMBUS_OV518 0x04000f /* OV518(+) USB 1.1 webcam ICs */
251#define I2C_HW_SMBUS_OV519 0x040010 /* OV519 USB 1.1 webcam IC */ 251#define I2C_HW_SMBUS_OV519 0x040010 /* OV519 USB 1.1 webcam IC */
252#define I2C_HW_SMBUS_OVFX2 0x040011 /* Cypress/OmniVision FX2 webcam */ 252#define I2C_HW_SMBUS_OVFX2 0x040011 /* Cypress/OmniVision FX2 webcam */
253#define I2C_HW_SMBUS_CAFE 0x040012 /* Marvell 88ALP01 "CAFE" cam */
253 254
254/* --- ISA pseudo-adapter */ 255/* --- ISA pseudo-adapter */
255#define I2C_HW_ISA 0x050000 256#define I2C_HW_ISA 0x050000