Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / media / video / marvell-ccic / mcam-core.h
blob917200e63255d2ee5b778edb71da245076910619
1 /*
2 * Marvell camera core structures.
4 * Copyright 2011 Jonathan Corbet corbet@lwn.net
5 */
6 #ifndef _MCAM_CORE_H
7 #define _MCAM_CORE_H
9 #include <linux/list.h>
10 #include <media/v4l2-common.h>
11 #include <media/v4l2-dev.h>
12 #include <media/videobuf2-core.h>
15 * Create our own symbols for the supported buffer modes, but, for now,
16 * base them entirely on which videobuf2 options have been selected.
18 #if defined(CONFIG_VIDEOBUF2_VMALLOC) || defined(CONFIG_VIDEOBUF2_VMALLOC_MODULE)
19 #define MCAM_MODE_VMALLOC 1
20 #endif
22 #if defined(CONFIG_VIDEOBUF2_DMA_CONTIG) || defined(CONFIG_VIDEOBUF2_DMA_CONTIG_MODULE)
23 #define MCAM_MODE_DMA_CONTIG 1
24 #endif
26 #if defined(CONFIG_VIDEOBUF2_DMA_SG) || defined(CONFIG_VIDEOBUF2_DMA_SG_MODULE)
27 #define MCAM_MODE_DMA_SG 1
28 #endif
30 #if !defined(MCAM_MODE_VMALLOC) && !defined(MCAM_MODE_DMA_CONTIG) && \
31 !defined(MCAM_MODE_DMA_SG)
32 #error One of the videobuf buffer modes must be selected in the config
33 #endif
36 enum mcam_state {
37 S_NOTREADY, /* Not yet initialized */
38 S_IDLE, /* Just hanging around */
39 S_FLAKED, /* Some sort of problem */
40 S_STREAMING, /* Streaming data */
41 S_BUFWAIT /* streaming requested but no buffers yet */
43 #define MAX_DMA_BUFS 3
46 * Different platforms work best with different buffer modes, so we
47 * let the platform pick.
49 enum mcam_buffer_mode {
50 B_vmalloc = 0,
51 B_DMA_contig = 1,
52 B_DMA_sg = 2
56 * Is a given buffer mode supported by the current kernel configuration?
58 static inline int mcam_buffer_mode_supported(enum mcam_buffer_mode mode)
60 switch (mode) {
61 #ifdef MCAM_MODE_VMALLOC
62 case B_vmalloc:
63 #endif
64 #ifdef MCAM_MODE_DMA_CONTIG
65 case B_DMA_contig:
66 #endif
67 #ifdef MCAM_MODE_DMA_SG
68 case B_DMA_sg:
69 #endif
70 return 1;
71 default:
72 return 0;
78 * A description of one of our devices.
79 * Locking: controlled by s_mutex. Certain fields, however, require
80 * the dev_lock spinlock; they are marked as such by comments.
81 * dev_lock is also required for access to device registers.
83 struct mcam_camera {
85 * These fields should be set by the platform code prior to
86 * calling mcam_register().
88 struct i2c_adapter *i2c_adapter;
89 unsigned char __iomem *regs;
90 spinlock_t dev_lock;
91 struct device *dev; /* For messages, dma alloc */
92 unsigned int chip_id;
93 short int clock_speed; /* Sensor clock speed, default 30 */
94 short int use_smbus; /* SMBUS or straight I2c? */
95 enum mcam_buffer_mode buffer_mode;
97 * Callbacks from the core to the platform code.
99 void (*plat_power_up) (struct mcam_camera *cam);
100 void (*plat_power_down) (struct mcam_camera *cam);
103 * Everything below here is private to the mcam core and
104 * should not be touched by the platform code.
106 struct v4l2_device v4l2_dev;
107 enum mcam_state state;
108 unsigned long flags; /* Buffer status, mainly (dev_lock) */
109 int users; /* How many open FDs */
110 struct file *owner; /* Who has data access (v4l2) */
113 * Subsystem structures.
115 struct video_device vdev;
116 struct v4l2_subdev *sensor;
117 unsigned short sensor_addr;
119 /* Videobuf2 stuff */
120 struct vb2_queue vb_queue;
121 struct list_head buffers; /* Available frames */
123 unsigned int nbufs; /* How many are alloc'd */
124 int next_buf; /* Next to consume (dev_lock) */
126 /* DMA buffers - vmalloc mode */
127 #ifdef MCAM_MODE_VMALLOC
128 unsigned int dma_buf_size; /* allocated size */
129 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
130 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
131 struct tasklet_struct s_tasklet;
132 #endif
133 unsigned int sequence; /* Frame sequence number */
134 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual bufs */
136 /* DMA buffers - DMA modes */
137 struct mcam_vb_buffer *vb_bufs[MAX_DMA_BUFS];
138 struct vb2_alloc_ctx *vb_alloc_ctx;
140 /* Mode-specific ops, set at open time */
141 void (*dma_setup)(struct mcam_camera *cam);
142 void (*frame_complete)(struct mcam_camera *cam, int frame);
144 /* Current operating parameters */
145 u32 sensor_type; /* Currently ov7670 only */
146 struct v4l2_pix_format pix_format;
147 enum v4l2_mbus_pixelcode mbus_code;
149 /* Locks */
150 struct mutex s_mutex; /* Access to this structure */
155 * Register I/O functions. These are here because the platform code
156 * may legitimately need to mess with the register space.
159 * Device register I/O
161 static inline void mcam_reg_write(struct mcam_camera *cam, unsigned int reg,
162 unsigned int val)
164 iowrite32(val, cam->regs + reg);
167 static inline unsigned int mcam_reg_read(struct mcam_camera *cam,
168 unsigned int reg)
170 return ioread32(cam->regs + reg);
174 static inline void mcam_reg_write_mask(struct mcam_camera *cam, unsigned int reg,
175 unsigned int val, unsigned int mask)
177 unsigned int v = mcam_reg_read(cam, reg);
179 v = (v & ~mask) | (val & mask);
180 mcam_reg_write(cam, reg, v);
183 static inline void mcam_reg_clear_bit(struct mcam_camera *cam,
184 unsigned int reg, unsigned int val)
186 mcam_reg_write_mask(cam, reg, 0, val);
189 static inline void mcam_reg_set_bit(struct mcam_camera *cam,
190 unsigned int reg, unsigned int val)
192 mcam_reg_write_mask(cam, reg, val, val);
196 * Functions for use by platform code.
198 int mccic_register(struct mcam_camera *cam);
199 int mccic_irq(struct mcam_camera *cam, unsigned int irqs);
200 void mccic_shutdown(struct mcam_camera *cam);
201 #ifdef CONFIG_PM
202 void mccic_suspend(struct mcam_camera *cam);
203 int mccic_resume(struct mcam_camera *cam);
204 #endif
207 * Register definitions for the m88alp01 camera interface. Offsets in bytes
208 * as given in the spec.
210 #define REG_Y0BAR 0x00
211 #define REG_Y1BAR 0x04
212 #define REG_Y2BAR 0x08
213 /* ... */
215 #define REG_IMGPITCH 0x24 /* Image pitch register */
216 #define IMGP_YP_SHFT 2 /* Y pitch params */
217 #define IMGP_YP_MASK 0x00003ffc /* Y pitch field */
218 #define IMGP_UVP_SHFT 18 /* UV pitch (planar) */
219 #define IMGP_UVP_MASK 0x3ffc0000
220 #define REG_IRQSTATRAW 0x28 /* RAW IRQ Status */
221 #define IRQ_EOF0 0x00000001 /* End of frame 0 */
222 #define IRQ_EOF1 0x00000002 /* End of frame 1 */
223 #define IRQ_EOF2 0x00000004 /* End of frame 2 */
224 #define IRQ_SOF0 0x00000008 /* Start of frame 0 */
225 #define IRQ_SOF1 0x00000010 /* Start of frame 1 */
226 #define IRQ_SOF2 0x00000020 /* Start of frame 2 */
227 #define IRQ_OVERFLOW 0x00000040 /* FIFO overflow */
228 #define IRQ_TWSIW 0x00010000 /* TWSI (smbus) write */
229 #define IRQ_TWSIR 0x00020000 /* TWSI read */
230 #define IRQ_TWSIE 0x00040000 /* TWSI error */
231 #define TWSIIRQS (IRQ_TWSIW|IRQ_TWSIR|IRQ_TWSIE)
232 #define FRAMEIRQS (IRQ_EOF0|IRQ_EOF1|IRQ_EOF2|IRQ_SOF0|IRQ_SOF1|IRQ_SOF2)
233 #define ALLIRQS (TWSIIRQS|FRAMEIRQS|IRQ_OVERFLOW)
234 #define REG_IRQMASK 0x2c /* IRQ mask - same bits as IRQSTAT */
235 #define REG_IRQSTAT 0x30 /* IRQ status / clear */
237 #define REG_IMGSIZE 0x34 /* Image size */
238 #define IMGSZ_V_MASK 0x1fff0000
239 #define IMGSZ_V_SHIFT 16
240 #define IMGSZ_H_MASK 0x00003fff
241 #define REG_IMGOFFSET 0x38 /* IMage offset */
243 #define REG_CTRL0 0x3c /* Control 0 */
244 #define C0_ENABLE 0x00000001 /* Makes the whole thing go */
246 /* Mask for all the format bits */
247 #define C0_DF_MASK 0x00fffffc /* Bits 2-23 */
249 /* RGB ordering */
250 #define C0_RGB4_RGBX 0x00000000
251 #define C0_RGB4_XRGB 0x00000004
252 #define C0_RGB4_BGRX 0x00000008
253 #define C0_RGB4_XBGR 0x0000000c
254 #define C0_RGB5_RGGB 0x00000000
255 #define C0_RGB5_GRBG 0x00000004
256 #define C0_RGB5_GBRG 0x00000008
257 #define C0_RGB5_BGGR 0x0000000c
259 /* Spec has two fields for DIN and DOUT, but they must match, so
260 combine them here. */
261 #define C0_DF_YUV 0x00000000 /* Data is YUV */
262 #define C0_DF_RGB 0x000000a0 /* ... RGB */
263 #define C0_DF_BAYER 0x00000140 /* ... Bayer */
264 /* 8-8-8 must be missing from the below - ask */
265 #define C0_RGBF_565 0x00000000
266 #define C0_RGBF_444 0x00000800
267 #define C0_RGB_BGR 0x00001000 /* Blue comes first */
268 #define C0_YUV_PLANAR 0x00000000 /* YUV 422 planar format */
269 #define C0_YUV_PACKED 0x00008000 /* YUV 422 packed */
270 #define C0_YUV_420PL 0x0000a000 /* YUV 420 planar */
271 /* Think that 420 packed must be 111 - ask */
272 #define C0_YUVE_YUYV 0x00000000 /* Y1CbY0Cr */
273 #define C0_YUVE_YVYU 0x00010000 /* Y1CrY0Cb */
274 #define C0_YUVE_VYUY 0x00020000 /* CrY1CbY0 */
275 #define C0_YUVE_UYVY 0x00030000 /* CbY1CrY0 */
276 #define C0_YUVE_XYUV 0x00000000 /* 420: .YUV */
277 #define C0_YUVE_XYVU 0x00010000 /* 420: .YVU */
278 #define C0_YUVE_XUVY 0x00020000 /* 420: .UVY */
279 #define C0_YUVE_XVUY 0x00030000 /* 420: .VUY */
280 /* Bayer bits 18,19 if needed */
281 #define C0_HPOL_LOW 0x01000000 /* HSYNC polarity active low */
282 #define C0_VPOL_LOW 0x02000000 /* VSYNC polarity active low */
283 #define C0_VCLK_LOW 0x04000000 /* VCLK on falling edge */
284 #define C0_DOWNSCALE 0x08000000 /* Enable downscaler */
285 #define C0_SIFM_MASK 0xc0000000 /* SIF mode bits */
286 #define C0_SIF_HVSYNC 0x00000000 /* Use H/VSYNC */
287 #define CO_SOF_NOSYNC 0x40000000 /* Use inband active signaling */
289 /* Bits below C1_444ALPHA are not present in Cafe */
290 #define REG_CTRL1 0x40 /* Control 1 */
291 #define C1_CLKGATE 0x00000001 /* Sensor clock gate */
292 #define C1_DESC_ENA 0x00000100 /* DMA descriptor enable */
293 #define C1_DESC_3WORD 0x00000200 /* Three-word descriptors used */
294 #define C1_444ALPHA 0x00f00000 /* Alpha field in RGB444 */
295 #define C1_ALPHA_SHFT 20
296 #define C1_DMAB32 0x00000000 /* 32-byte DMA burst */
297 #define C1_DMAB16 0x02000000 /* 16-byte DMA burst */
298 #define C1_DMAB64 0x04000000 /* 64-byte DMA burst */
299 #define C1_DMAB_MASK 0x06000000
300 #define C1_TWOBUFS 0x08000000 /* Use only two DMA buffers */
301 #define C1_PWRDWN 0x10000000 /* Power down */
303 #define REG_CLKCTRL 0x88 /* Clock control */
304 #define CLK_DIV_MASK 0x0000ffff /* Upper bits RW "reserved" */
306 /* This appears to be a Cafe-only register */
307 #define REG_UBAR 0xc4 /* Upper base address register */
309 /* Armada 610 DMA descriptor registers */
310 #define REG_DMA_DESC_Y 0x200
311 #define REG_DMA_DESC_U 0x204
312 #define REG_DMA_DESC_V 0x208
313 #define REG_DESC_LEN_Y 0x20c /* Lengths are in bytes */
314 #define REG_DESC_LEN_U 0x210
315 #define REG_DESC_LEN_V 0x214
318 * Useful stuff that probably belongs somewhere global.
320 #define VGA_WIDTH 640
321 #define VGA_HEIGHT 480
323 #endif /* _MCAM_CORE_H */