]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/omap/sensor_ov9640.c
Move i2c_driver out of ov9640_sensor structure and
[linux-2.6-omap-h63xx.git] / drivers / media / video / omap / sensor_ov9640.c
1
2 /*
3  * drivers/media/video/omap/sensor_ov9640.c
4  *
5  * Ov9640 Sensor driver for OMAP camera sensor interface
6  *
7  * Author: Andy Lowe (source@mvista.com)
8  *
9  * Copyright (C) 2004 MontaVista Software, Inc.
10  * Copyright (C) 2004 Texas Instruments.
11  *
12  * This file is licensed under the terms of the GNU General Public License 
13  * version 2. This program is licensed "as is" without any warranty of any 
14  * kind, whether express or implied.
15  */
16
17 #include <linux/errno.h>
18 #include <linux/i2c.h>
19 #include <linux/videodev.h>
20 #include <media/video-buf.h>
21 #include <linux/delay.h>
22 #include <asm/mach-types.h>
23
24 #include "sensor_if.h"
25 #include "ov9640.h"
26 #include "h3sensorpower.h"
27 #include "h4sensorpower.h"
28
29 #define CAMERA_OV9640
30 #ifdef CAMERA_OV9640
31
32 struct ov9640_sensor {
33         /* I2C parameters */
34         struct i2c_client client;
35         int ver; /* OV9640 version */
36 };
37
38 static struct ov9640_sensor ov9640;
39
40 /* list of image formats supported by OV9640 sensor */
41 const static struct v4l2_fmtdesc ov9640_formats[] = {
42         {
43                 /* Note:  V4L2 defines RGB565 as:
44                  *
45                  *      Byte 0                    Byte 1
46                  *      g2 g1 g0 r4 r3 r2 r1 r0   b4 b3 b2 b1 b0 g5 g4 g3
47                  *
48                  * We interpret RGB565 as:
49                  *
50                  *      Byte 0                    Byte 1
51                  *      g2 g1 g0 b4 b3 b2 b1 b0   r4 r3 r2 r1 r0 g5 g4 g3
52                  */
53                 .description    = "RGB565, le",
54                 .pixelformat    = V4L2_PIX_FMT_RGB565,
55         },{
56                 /* Note:  V4L2 defines RGB565X as:
57                  *
58                  *      Byte 0                    Byte 1
59                  *      b4 b3 b2 b1 b0 g5 g4 g3   g2 g1 g0 r4 r3 r2 r1 r0
60                  *
61                  * We interpret RGB565X as:
62                  *
63                  *      Byte 0                    Byte 1
64                  *      r4 r3 r2 r1 r0 g5 g4 g3   g2 g1 g0 b4 b3 b2 b1 b0
65                  */
66                 .description    = "RGB565, be",
67                 .pixelformat    = V4L2_PIX_FMT_RGB565X,
68         },
69         {
70                 .description    = "YUYV (YUV 4:2:2), packed",
71                 .pixelformat    = V4L2_PIX_FMT_YUYV,
72         },{
73                 .description    = "UYVY, packed",
74                 .pixelformat    = V4L2_PIX_FMT_UYVY,
75         },
76         {
77                 /* Note:  V4L2 defines RGB555 as:
78                  *
79                  *      Byte 0                    Byte 1
80                  *      g2 g1 g0 r4 r3 r2 r1 r0   x  b4 b3 b2 b1 b0 g4 g3
81                  *
82                  * We interpret RGB555 as:
83                  *
84                  *      Byte 0                    Byte 1
85                  *      g2 g1 g0 b4 b3 b2 b1 b0   x  r4 r3 r2 r1 r0 g4 g3
86                  */
87                 .description    = "RGB555, le",
88                 .pixelformat    = V4L2_PIX_FMT_RGB555,
89         },{
90                 /* Note:  V4L2 defines RGB555X as:
91                  *
92                  *      Byte 0                    Byte 1
93                  *      x  b4 b3 b2 b1 b0 g4 g3   g2 g1 g0 r4 r3 r2 r1 r0
94                  *
95                  * We interpret RGB555X as:
96                  *
97                  *      Byte 0                    Byte 1
98                  *      x  r4 r3 r2 r1 r0 g4 g3   g2 g1 g0 b4 b3 b2 b1 b0
99                  */
100                 .description    = "RGB555, be",
101                 .pixelformat    = V4L2_PIX_FMT_RGB555X,
102         }
103 };
104
105 #define NUM_CAPTURE_FORMATS (sizeof(ov9640_formats)/sizeof(ov9640_formats[0]))
106 #define NUM_OVERLAY_FORMATS 2
107
108 /* register initialization tables for OV9640 */
109
110 #define OV9640_REG_TERM 0xFF    /* terminating list entry for reg */
111 #define OV9640_VAL_TERM 0xFF    /* terminating list entry for val */
112
113 /* common OV9640 register initialization for all image sizes, pixel formats, 
114  * and frame rates
115  */
116 const static struct ov9640_reg ov9640_common[] = {
117         { 0x12, 0x80 }, { 0x11, 0x80 }, { 0x13, 0x88 }, /* COM7, CLKRC, COM8 */
118         { 0x01, 0x58 }, { 0x02, 0x24 }, { 0x04, 0x00 }, /* BLUE, RED, COM1 */
119         { 0x0E, 0x81 }, { 0x0F, 0x4F }, { 0x14, 0xcA }, /* COM5, COM6, COM9 */
120         { 0x16, 0x02 }, { 0x1B, 0x01 }, { 0x24, 0x70 }, /* ?, PSHFT, AEW */
121         { 0x25, 0x68 }, { 0x26, 0xD3 }, { 0x27, 0x90 }, /* AEB, VPT, BBIAS */
122         { 0x2A, 0x00 }, { 0x2B, 0x00 }, { 0x32, 0x24 }, /* EXHCH, EXHCL, HREF */
123         { 0x33, 0x02 }, { 0x37, 0x02 }, { 0x38, 0x13 }, /* CHLF, ADC, ACOM */
124         { 0x39, 0xF0 }, { 0x3A, 0x00 }, { 0x3B, 0x01 }, /* OFON, TSLB, COM11 */
125         { 0x3D, 0x90 }, { 0x3E, 0x02 }, { 0x3F, 0xF2 }, /* COM13, COM14, EDGE */
126         { 0x41, 0x02 }, { 0x42, 0xC8 },         /* COM16, COM17 */
127         { 0x43, 0xF0 }, { 0x44, 0x10 }, { 0x45, 0x6C }, /* ?, ?, ? */
128         { 0x46, 0x6C }, { 0x47, 0x44 }, { 0x48, 0x44 }, /* ?, ?, ? */
129         { 0x49, 0x03 }, { 0x59, 0x49 }, { 0x5A, 0x94 }, /* ?, ?, ? */
130         { 0x5B, 0x46 }, { 0x5C, 0x84 }, { 0x5D, 0x5C }, /* ?, ?, ? */
131         { 0x5E, 0x08 }, { 0x5F, 0x00 }, { 0x60, 0x14 }, /* ?, ?, ? */
132         { 0x61, 0xCE },                                 /* ? */
133         { 0x62, 0x70 }, { 0x63, 0x00 }, { 0x64, 0x04 }, /* LCC1, LCC2, LCC3 */
134         { 0x65, 0x00 }, { 0x66, 0x00 },                 /* LCC4, LCC5 */
135         { 0x69, 0x00 }, { 0x6A, 0x3E }, { 0x6B, 0x3F }, /* HV, MBD, DBLV */
136         { 0x6C, 0x40 }, { 0x6D, 0x30 }, { 0x6E, 0x4B }, /* GSP1, GSP2, GSP3 */
137         { 0x6F, 0x60 }, { 0x70, 0x70 }, { 0x71, 0x70 }, /* GSP4, GSP5, GSP6 */
138         { 0x72, 0x70 }, { 0x73, 0x70 }, { 0x74, 0x60 }, /* GSP7, GSP8, GSP9 */
139         { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 }, /* GSP10,GSP11,GSP12 */
140         { 0x78, 0x3A }, { 0x79, 0x2E }, { 0x7A, 0x28 }, /* GSP13,GSP14,GSP15 */
141         { 0x7B, 0x22 }, { 0x7C, 0x04 }, { 0x7D, 0x07 }, /* GSP16,GST1, GST2 */
142         { 0x7E, 0x10 }, { 0x7F, 0x28 }, { 0x80, 0x36 }, /* GST3, GST4, GST5 */
143         { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 }, /* GST6, GST7, GST8 */
144         { 0x84, 0x6C }, { 0x85, 0x78 }, { 0x86, 0x8C }, /* GST9, GST10,GST11 */
145         { 0x87, 0x9E }, { 0x88, 0xBB }, { 0x89, 0xD2 }, /* GST12,GST13,GST14 */
146         { 0x8A, 0xE6 }, { 0x13, 0xaF }, { 0x15, 0x02 }, /* GST15, COM8 */
147         { 0x22, 0x8a }, /* GROS */
148         { OV9640_REG_TERM, OV9640_VAL_TERM }
149 };
150
151 /* OV9640 register configuration for all combinations of pixel format and 
152  * image size
153  */
154         /* YUV (YCbCr) QQCIF */
155 const static struct ov9640_reg qqcif_yuv[] = {
156         { 0x12, 0x08 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
157         { 0x04, 0x24 }, { 0x0C, 0x00 }, { 0x0D, 0x40 }, /* COM1, COM3, COM4 */
158         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
159         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
160         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
161         { 0x58, 0x0F },                                 /* MTXS */
162         { OV9640_REG_TERM, OV9640_VAL_TERM }
163 };
164         /* YUV (YCbCr) QQVGA */
165 const static struct ov9640_reg qqvga_yuv[] = {
166         { 0x12, 0x10 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
167         { 0x04, 0x24 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
168         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
169         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
170         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
171         { 0x58, 0x0F },                                 /* MTXS */
172         { OV9640_REG_TERM, OV9640_VAL_TERM }
173 };
174         /* YUV (YCbCr) QCIF */
175 const static struct ov9640_reg qcif_yuv[] = {
176         { 0x12, 0x08 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
177         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
178         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
179         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
180         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
181         { 0x58, 0x0F },                                 /* MTXS */
182         { OV9640_REG_TERM, OV9640_VAL_TERM }
183 };
184         /* YUV (YCbCr) QVGA */
185 const static struct ov9640_reg qvga_yuv[] = {
186         { 0x12, 0x10 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
187         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
188         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
189         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
190         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
191         { 0x58, 0x0F },                                 /* MTXS */
192         { OV9640_REG_TERM, OV9640_VAL_TERM }
193 };
194         /* YUV (YCbCr) CIF */
195 const static struct ov9640_reg cif_yuv[] = {
196         { 0x12, 0x20 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
197         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
198         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
199         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
200         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
201         { 0x58, 0x0F },                                 /* MTXS */
202         { OV9640_REG_TERM, OV9640_VAL_TERM }
203 };
204         /* YUV (YCbCr) VGA */
205 const static struct ov9640_reg vga_yuv[] = {
206         { 0x12, 0x40 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
207         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
208         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
209         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
210         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
211         { 0x58, 0x0F },                                 /* MTXS */
212         { OV9640_REG_TERM, OV9640_VAL_TERM }
213 };
214         /* YUV (YCbCr) SXGA */
215 const static struct ov9640_reg sxga_yuv[] = {
216         { 0x12, 0x00 }, { 0x3C, 0x46 }, { 0x40, 0xC0 }, /* COM7, COM12, COM15 */
217         { 0x04, 0x00 }, { 0x0C, 0x00 }, { 0x0D, 0x40 }, /* COM1, COM3, COM4 */
218         { 0x4F, 0x50 }, { 0x50, 0x43 }, { 0x51, 0x0D }, /* MTX1, MTX2, MTX3 */
219         { 0x52, 0x19 }, { 0x53, 0x4C }, { 0x54, 0x65 }, /* MTX4, MTX5, MTX6 */
220         { 0x55, 0x40 }, { 0x56, 0x40 }, { 0x57, 0x40 }, /* MTX7, MTX8, MTX9 */
221         { 0x58, 0x0F },                                 /* MTXS */
222         { OV9640_REG_TERM, OV9640_VAL_TERM }
223 };
224         /* RGB565 QQCIF */
225 const static struct ov9640_reg qqcif_565[] = {
226         { 0x12, 0x0C }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
227         { 0x04, 0x24 }, { 0x0C, 0x00 }, { 0x0D, 0x40 }, /* COM1, COM3, COM4 */
228         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
229         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
230         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
231         { 0x58, 0x65 },                                 /* MTXS */
232         { OV9640_REG_TERM, OV9640_VAL_TERM }
233 };
234         /* RGB565 QQVGA */
235 const static struct ov9640_reg qqvga_565[] = {
236         { 0x12, 0x14 }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
237         { 0x04, 0x24 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
238         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
239         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
240         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
241         { 0x58, 0x65 },                                 /* MTXS */
242         { OV9640_REG_TERM, OV9640_VAL_TERM }
243 };
244         /* RGB565 QCIF */
245 const static struct ov9640_reg qcif_565[] = {
246         { 0x12, 0x0C }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
247         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
248         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
249         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
250         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
251         { 0x58, 0x65 },                                 /* MTXS */
252         { OV9640_REG_TERM, OV9640_VAL_TERM }
253 };
254         /* RGB565 QVGA */
255 const static struct ov9640_reg qvga_565[] = {
256         { 0x12, 0x14 }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
257         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
258         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
259         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
260         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
261         { 0x58, 0x65 },                                 /* MTXS */
262         { OV9640_REG_TERM, OV9640_VAL_TERM }
263 };
264         /* RGB565 CIF */
265 const static struct ov9640_reg cif_565[] = {
266         { 0x12, 0x24 }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
267         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
268         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
269         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
270         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
271         { 0x58, 0x65 },                                 /* MTXS */
272         { OV9640_REG_TERM, OV9640_VAL_TERM }
273 };
274         /* RGB565 VGA */
275 const static struct ov9640_reg vga_565[] = {
276         { 0x12, 0x44 }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
277         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
278         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
279         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
280         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
281         { 0x58, 0x65 },                                 /* MTXS */
282         { OV9640_REG_TERM, OV9640_VAL_TERM }
283 };
284         /* RGB565 SXGA */
285 const static struct ov9640_reg sxga_565[] = {
286         { 0x12, 0x04 }, { 0x3C, 0x40 }, { 0x40, 0x10 }, /* COM7, COM12, COM15 */
287         { 0x04, 0x00 }, { 0x0C, 0x00 }, { 0x0D, 0x40 }, /* COM1, COM3, COM4 */
288         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
289         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
290         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
291         { 0x58, 0x65 },                                 /* MTXS */
292         { OV9640_REG_TERM, OV9640_VAL_TERM }
293 };
294         /* RGB555 QQCIF */
295 const static struct ov9640_reg qqcif_555[] = {
296         { 0x12, 0x0C }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
297         { 0x04, 0x24 }, { 0x0C, 0x00 }, { 0x0D, 0x40 }, /* COM1, COM3, COM4 */
298         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
299         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
300         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
301         { 0x58, 0x65 },                                 /* MTXS */
302         { OV9640_REG_TERM, OV9640_VAL_TERM }
303 };
304         /* RGB555 QQVGA */
305 const static struct ov9640_reg qqvga_555[] = {
306         { 0x12, 0x14 }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
307         { 0x04, 0x24 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
308         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
309         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
310         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
311         { 0x58, 0x65 },                                 /* MTXS */
312         { OV9640_REG_TERM, OV9640_VAL_TERM }
313 };
314         /* RGB555 QCIF */
315 const static struct ov9640_reg qcif_555[] = {
316         { 0x12, 0x0C }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
317         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
318         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
319         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
320         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
321         { 0x58, 0x65 },                                 /* MTXS */
322         { OV9640_REG_TERM, OV9640_VAL_TERM }
323 };
324         /* RGB555 QVGA */
325 const static struct ov9640_reg qvga_555[] = {
326         { 0x12, 0x14 }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
327         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
328         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
329         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
330         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
331         { 0x58, 0x65 },                                 /* MTXS */
332         { OV9640_REG_TERM, OV9640_VAL_TERM }
333 };
334         /* RGB555 CIF */
335 const static struct ov9640_reg cif_555[] = {
336         { 0x12, 0x24 }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
337         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
338         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
339         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
340         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
341         { 0x58, 0x65 },                                 /* MTXS */
342         { OV9640_REG_TERM, OV9640_VAL_TERM }
343 };
344         /* RGB555 VGA */
345 const static struct ov9640_reg vga_555[] = {
346         { 0x12, 0x44 }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
347         { 0x04, 0x00 }, { 0x0C, 0x04 }, { 0x0D, 0xC0 }, /* COM1, COM3, COM4 */
348         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
349         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
350         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
351         { 0x58, 0x65 },                                 /* MTXS */
352         { OV9640_REG_TERM, OV9640_VAL_TERM }
353 };
354         /* RGB555 SXGA */
355 const static struct ov9640_reg sxga_555[] = {
356         { 0x12, 0x04 }, { 0x3C, 0x40 }, { 0x40, 0x30 }, /* COM7, COM12, COM15 */
357         { 0x04, 0x00 }, { 0x0C, 0x00 }, { 0x0D, 0x40 }, /* COM1, COM3, COM4 */
358         { 0x4F, 0x71 }, { 0x50, 0x3E }, { 0x51, 0x0C }, /* MTX1, MTX2, MTX3 */
359         { 0x52, 0x33 }, { 0x53, 0x72 }, { 0x54, 0x00 }, /* MTX4, MTX5, MTX6 */
360         { 0x55, 0x2B }, { 0x56, 0x66 }, { 0x57, 0xD2 }, /* MTX7, MTX8, MTX9 */
361         { 0x58, 0x65 },                                 /* MTXS */
362         { OV9640_REG_TERM, OV9640_VAL_TERM }
363 };
364
365  
366 #define DEF_GAIN         31
367 #define DEF_AUTOGAIN      1
368 #define DEF_EXPOSURE    154
369 #define DEF_AEC           1
370 #define DEF_FREEZE_AGCAEC 0
371 #define DEF_BLUE        153
372 #define DEF_RED         (255 - DEF_BLUE)
373 #define DEF_AWB           1
374 #define DEF_HFLIP         0
375 #define DEF_VFLIP         0
376
377 /* Our own specific controls */
378 #define V4L2_CID_FREEZE_AGCAEC V4L2_CID_PRIVATE_BASE+0
379 #define V4L2_CID_AUTOEXPOSURE V4L2_CID_PRIVATE_BASE+1
380 #define V4L2_CID_LAST_PRIV  V4L2_CID_AUTOEXPOSURE
381
382 /*  Video controls  */
383 static struct vcontrol {
384         struct v4l2_queryctrl qc;
385         int current_value;
386         u8 reg;
387         u8 mask;
388         u8 start_bit;
389 } control[] = {
390         { { V4L2_CID_GAIN, V4L2_CTRL_TYPE_INTEGER, "Gain", 0, 63, 1,
391             DEF_GAIN },
392           0, OV9640_GAIN, 0x3f, 0 },
393         { { V4L2_CID_AUTOGAIN, V4L2_CTRL_TYPE_BOOLEAN, "Auto Gain", 0, 1, 0,
394             DEF_AUTOGAIN },
395           0, OV9640_COM8, 0x04, 2 },
396         { { V4L2_CID_EXPOSURE, V4L2_CTRL_TYPE_INTEGER, "Exposure", 0, 255, 1,
397             DEF_EXPOSURE },
398           0, OV9640_AECH, 0xff, 0 },
399         { { V4L2_CID_AUTOEXPOSURE, V4L2_CTRL_TYPE_BOOLEAN, "Auto Exposure", 0, 1, 0,
400             DEF_AEC },
401           0, OV9640_COM8, 0x01, 0 },
402         { { V4L2_CID_FREEZE_AGCAEC, V4L2_CTRL_TYPE_BOOLEAN, "Freeze AGC/AEC", 0,1,0,
403             DEF_FREEZE_AGCAEC },
404           0, OV9640_COM9, 0x01, 0 },
405         { { V4L2_CID_RED_BALANCE, V4L2_CTRL_TYPE_INTEGER, "Red Balance", 0, 255, 1,
406             DEF_RED },
407           0, OV9640_RED, 0xff, 0 },
408         { { V4L2_CID_BLUE_BALANCE, V4L2_CTRL_TYPE_INTEGER, "Blue Balance", 0, 255, 1,
409             DEF_BLUE },
410           0, OV9640_BLUE, 0xff, 0 },
411         { { V4L2_CID_AUTO_WHITE_BALANCE, V4L2_CTRL_TYPE_BOOLEAN, "Auto White Balance", 0,1,0,
412             DEF_AWB },
413           0, OV9640_COM8, 0x02, 1 },
414         { { V4L2_CID_HFLIP, V4L2_CTRL_TYPE_BOOLEAN, "Mirror Image", 0, 1, 0,
415             DEF_HFLIP },
416           0, OV9640_MVFP, 0x20, 5 },
417         { { V4L2_CID_VFLIP, V4L2_CTRL_TYPE_BOOLEAN, "Vertical Flip", 0, 1, 0,
418             DEF_VFLIP },
419           0, OV9640_MVFP, 0x10, 4 },
420 };
421
422 #define NUM_CONTROLS (sizeof(control)/sizeof(control[0]))
423
424 const static struct ov9640_reg *
425         ov9640_reg_init[NUM_PIXEL_FORMATS][NUM_IMAGE_SIZES] =
426 {
427  { qqcif_yuv, qqvga_yuv, qcif_yuv, qvga_yuv, cif_yuv, vga_yuv, sxga_yuv },
428  { qqcif_565, qqvga_565, qcif_565, qvga_565, cif_565, vga_565, sxga_565 },
429  { qqcif_555, qqvga_555, qcif_555, qvga_555, cif_555, vga_555, sxga_555 },
430 };
431
432
433 /* 
434  * Read a value from a register in an OV9640 sensor device.  The value is 
435  * returned in 'val'.
436  * Returns zero if successful, or non-zero otherwise.
437  */
438 static int 
439 ov9640_read_reg(struct i2c_client *client, u8 reg, u8 *val)
440 {
441         int err;
442         struct i2c_msg msg[1];
443         unsigned char data[1];
444
445         if (!client->adapter)
446                 return -ENODEV;
447
448         msg->addr = client->addr;
449         msg->flags = 0;
450         msg->len = 1;
451         msg->buf = data;
452         *data = reg;
453         err = i2c_transfer(client->adapter, msg, 1);
454         if (err >= 0) {
455                 msg->flags = I2C_M_RD;
456                 err = i2c_transfer(client->adapter, msg, 1);
457         }
458         if (err >= 0) {
459                 *val = *data;
460                 return 0;
461         }
462         return err;
463 }
464
465 /* Write a value to a register in an OV9640 sensor device.
466  * Returns zero if successful, or non-zero otherwise.
467  */
468 static int 
469 ov9640_write_reg(struct i2c_client *client, u8 reg, u8 val)
470 {
471         int err;
472         struct i2c_msg msg[1];
473         unsigned char data[2];
474
475         if (!client->adapter)
476                 return -ENODEV;
477         
478         msg->addr = client->addr;
479         msg->flags = 0;
480         msg->len = 2;
481         msg->buf = data;
482         data[0] = reg;
483         data[1] = val;
484         err = i2c_transfer(client->adapter, msg, 1);
485         if (err >= 0)
486                 return 0;
487         return err;
488 }
489
490 static int 
491 ov9640_write_reg_mask(struct i2c_client *client, u8 reg, u8 *val, u8 mask)
492 {
493         u8 oldval, newval;
494         int rc;
495
496         if (mask == 0xff) {
497                 newval = *val;
498         } else {
499                 /* need to do read - modify - write */
500                 if ((rc = ov9640_read_reg(client, reg, &oldval)))
501                         return rc;
502                 oldval &= (~mask);              /* Clear the masked bits */
503                 *val &= mask;                  /* Enforce mask on value */
504                 newval = oldval | *val;        /* Set the desired bits */
505         }
506
507         /* write the new value to the register */
508         if ((rc = ov9640_write_reg(client, reg, newval)))
509                 return rc;
510
511         if ((rc = ov9640_read_reg(client, reg, &newval)))
512                 return rc;
513
514         *val = newval & mask;
515         return 0;
516 }
517
518 static int 
519 ov9640_read_reg_mask(struct i2c_client *client, u8 reg, u8 *val, u8 mask)
520 {
521         int rc;
522
523         if ((rc = ov9640_read_reg(client, reg, val)))
524                 return rc;
525         (*val) &= mask;
526
527         return 0;
528 }
529
530 /* Initialize a list of OV9640 registers.
531  * The list of registers is terminated by the pair of values 
532  * { OV9640_REG_TERM, OV9640_VAL_TERM }.
533  * Returns zero if successful, or non-zero otherwise.
534  */
535 static int 
536 ov9640_write_regs(struct i2c_client *client, const struct ov9640_reg reglist[])
537 {
538         int err;
539         const struct ov9640_reg *next = reglist;
540         
541         while (!((next->reg == OV9640_REG_TERM) 
542                 && (next->val == OV9640_VAL_TERM)))
543         {
544                 err = ov9640_write_reg(client, next->reg, next->val);
545                 udelay(100);
546                 if (err)
547                         return err;
548                 next++;
549         }
550         return 0;
551 }
552
553 /* Returns the index of the requested ID from the control structure array */
554 static int
555 find_vctrl(int id)
556 {
557         int i;
558
559         if (id < V4L2_CID_BASE)
560                 return -EDOM;
561
562         for (i = NUM_CONTROLS - 1; i >= 0; i--)
563                 if (control[i].qc.id == id)
564                         break;
565         if (i < 0)
566                 i = -EINVAL;
567         return i;
568 }
569
570 /* Calculate the internal clock divisor (value of the CLKRC register) of the 
571  * OV9640 given the image size, the frequency (in Hz) of its XCLK input and a 
572  * desired frame period (in seconds).  The frame period 'fper' is expressed as 
573  * a fraction.  The frame period is an input/output parameter.
574  * Returns the value of the OV9640 CLKRC register that will yield the frame 
575  * period returned in 'fper' at the specified xclk frequency.  The 
576  * returned period will be as close to the requested period as possible.
577  */
578 static unsigned char
579 ov9640_clkrc(enum image_size isize, unsigned long xclk, struct v4l2_fract *fper)
580 {
581         unsigned long fpm, fpm_max;     /* frames per minute */
582         unsigned long divisor;
583         const unsigned long divisor_max = 64;
584         const static unsigned long clks_per_frame[] = 
585                 { 200000, 200000, 200000, 200000, 400000, 800000, 3200000 };
586
587         if (fper->numerator > 0)
588                 fpm = (fper->denominator*60)/fper->numerator;
589         else
590                 fpm = 0xffffffff;
591         fpm_max = (xclk*60)/clks_per_frame[isize];
592         if (fpm_max == 0)
593                 fpm_max = 1;
594         if (fpm > fpm_max)
595                 fpm = fpm_max;
596         if (fpm == 0)
597                 fpm = 1;
598         divisor = fpm_max/fpm;
599         if (divisor > divisor_max)
600                 divisor = divisor_max;
601         fper->numerator = divisor*60;
602         fper->denominator = fpm_max;
603
604         /* try to reduce the fraction */
605         while (!(fper->denominator % 5) && !(fper->numerator % 5)) {
606                 fper->numerator /= 5;
607                 fper->denominator /= 5;
608         }
609         while (!(fper->denominator % 3) && !(fper->numerator % 3)) {
610                 fper->numerator /= 3;
611                 fper->denominator /= 3;
612         }
613         while (!(fper->denominator % 2) && !(fper->numerator % 2)) {
614                 fper->numerator /= 2;
615                 fper->denominator /= 2;
616         }
617         if (fper->numerator < fper->denominator) {
618                 if (!(fper->denominator % fper->numerator)) {
619                         fper->denominator /= fper->numerator;
620                         fper->numerator = 1;
621                 }
622         }
623         else {
624                 if (!(fper->numerator % fper->denominator)) {
625                         fper->numerator /= fper->denominator;
626                         fper->denominator = 1;
627                 }
628         }
629
630         /* we set bit 7 in CLKRC to enable the digital PLL */
631         return (0x80 | (divisor - 1));
632 }
633
634 /* Configure the OV9640 for a specified image size, pixel format, and frame 
635  * period.  xclk is the frequency (in Hz) of the xclk input to the OV9640.  
636  * fper is the frame period (in seconds) expressed as a fraction.
637  * Returns zero if successful, or non-zero otherwise.
638  * The actual frame period is returned in fper.
639  */
640 static int
641 ov9640_configure(struct i2c_client *client, 
642         enum image_size isize, 
643         enum pixel_format pfmt,
644         unsigned long xclk,
645         struct v4l2_fract *fper)
646 {
647         int err;
648         unsigned char clkrc;
649
650         /* common register initialization */
651         err = ov9640_write_regs(client, ov9640_common);
652         if (err)
653                 return err;
654
655         /* configure image size and pixel format */
656         err = ov9640_write_regs(client, ov9640_reg_init[pfmt][isize]);
657         if (err)
658                 return err;
659
660         /* configure frame rate */
661         clkrc = ov9640_clkrc(isize, xclk, fper);
662         err = ov9640_write_reg(client, OV9640_CLKRC, clkrc);
663         if (err)
664                 return err;
665
666         return 0;
667 }
668
669 static int
670 ov9640_powerup(void)
671 {
672         int err;
673
674         if (machine_is_omap_h2())
675                 return 0;
676
677         if (machine_is_omap_h3()) {
678                 err = h3_sensor_powerup();
679                 if (err)
680                         return err;
681         }
682
683         if (machine_is_omap_h4()) {
684                 err = h4_sensor_powerup();
685                 if (err)
686                         return err;
687         }
688
689         return 0;
690 }
691 static int
692 ov9640_powerdown(void)
693 {
694         int err;
695
696         if (machine_is_omap_h2())
697                 return 0;
698
699         if (machine_is_omap_h3()) {
700                 err = h3_sensor_powerdown();
701                 if (err)
702                         return err;
703         }
704
705         if (machine_is_omap_h4()) {
706                 err = h4_sensor_powerdown();
707                 if (err)
708                         return err;
709         }
710
711         return 0;
712 }
713
714 static int
715 ov9640sensor_power_on(void *priv)
716 {
717         return ov9640_powerup();
718 }
719
720 static int
721 ov9640sensor_power_off(void *priv)
722 {
723         return ov9640_powerdown();
724 }
725
726 /* Detect if an OV9640 is present, and if so which revision. 
727  * A device is considered to be detected if the manufacturer ID (MIDH and MIDL) 
728  * and the product ID (PID) registers match the expected values.  
729  * Any value of the version ID (VER) register is accepted.
730  * Here are the version numbers we know about:
731  *      0x48 --> OV9640 Revision 1 or OV9640 Revision 2
732  *      0x49 --> OV9640 Revision 3
733  * Returns a negative error number if no device is detected, or the 
734  * non-negative value of the version ID register if a device is detected.
735  */
736 static int
737 ov9640_detect(struct i2c_client *client)
738 {
739         u8 midh, midl, pid, ver;
740
741         if (!client)
742                 return -ENODEV;
743  
744         if (ov9640_read_reg(client, OV9640_MIDH, &midh))
745                 return -ENODEV;
746         if (ov9640_read_reg(client, OV9640_MIDL, &midl))
747                 return -ENODEV;
748         if (ov9640_read_reg(client, OV9640_PID, &pid))
749                 return -ENODEV;
750         if (ov9640_read_reg(client, OV9640_VER, &ver))
751                 return -ENODEV;
752
753         if ((midh != OV9640_MIDH_MAGIC) 
754                 || (midl != OV9640_MIDL_MAGIC)
755                 || (pid != OV9640_PID_MAGIC))
756         {
757                 /* We didn't read the values we expected, so 
758                  * this must not be an OV9640.
759                  */
760                 return -ENODEV;
761         }
762         return ver;
763 }
764
765 static struct i2c_driver ov9640sensor_i2c_driver;
766
767 /* This function registers an I2C client via i2c_attach_client() for an OV9640 
768  * sensor device.  If 'probe' is non-zero, then the I2C client is only 
769  * registered if the device can be detected.  If 'probe' is zero, then no 
770  * device detection is attempted and the I2C client is always registered.
771  * Returns zero if an I2C client is successfully registered, or non-zero 
772  * otherwise.
773  */
774 static int 
775 ov9640_i2c_attach_client(struct i2c_adapter *adap, int addr, int probe)
776 {
777         struct ov9640_sensor *sensor = &ov9640;
778         struct i2c_client *client = &sensor->client;
779         int err;
780
781         if (client->adapter)
782                 return -EBUSY;  /* our client is already attached */
783
784         client->addr = addr;
785         client->driver = &ov9640sensor_i2c_driver;
786         client->adapter = adap;
787
788         err = i2c_attach_client(client);
789         if (err) {
790                 client->adapter = NULL;
791                 return err;
792         }
793
794         if (probe) {
795                 err = ov9640_detect(client);
796                 if (err < 0) {
797                         i2c_detach_client(client);
798                         client->adapter = NULL;
799                         return err;
800                 }
801                 sensor->ver = err;
802         }
803         return 0;
804 }
805
806 /* This function is called by i2c_del_adapter() and i2c_del_driver() 
807  * if the adapter or driver with which this I2C client is associated is 
808  * removed.  This function unregisters the client via i2c_detach_client().
809  * Returns zero if the client is successfully detached, or non-zero 
810  * otherwise.
811  */
812 static int 
813 ov9640_i2c_detach_client(struct i2c_client *client)
814 {
815         int err;
816
817         if (!client->adapter)
818                 return -ENODEV; /* our client isn't attached */
819
820         err = i2c_detach_client(client);
821         client->adapter = NULL;
822
823         return err;
824 }
825
826 /* This function will be called for each registered I2C bus adapter when our 
827  * I2C driver is registered via i2c_add_driver().  It will also be called 
828  * whenever a new I2C adapter is registered after our I2C driver is registered.
829  * This function probes the specified I2C bus adapter to determine if an 
830  * OV9640 sensor device is present.  If a device is detected, an I2C client 
831  * is registered for it via ov9640_i2c_attach_client().  Note that we can't use 
832  * the standard i2c_probe() function to look for the sensor because the OMAP 
833  * I2C controller doesn't support probing.
834  * Returns zero if an OV9640 device is detected and an I2C client successfully 
835  * registered for it, or non-zero otherwise.
836  */
837 static int 
838 ov9640_i2c_probe_adapter(struct i2c_adapter *adap)
839 {
840         return ov9640_i2c_attach_client(adap, OV9640_I2C_ADDR, 1);
841 }
842
843 /* Find the best match for a requested image capture size.  The best match 
844  * is chosen as the nearest match that has the same number or fewer pixels 
845  * as the requested size, or the smallest image size if the requested size 
846  * has fewer pixels than the smallest image.
847  */
848 static enum image_size
849 ov9640_find_size(unsigned int width, unsigned int height)
850 {
851         enum image_size isize;
852         unsigned long pixels = width*height;
853
854         for (isize = QQCIF; isize < SXGA; isize++) {
855                 if (ov9640_sizes[isize + 1].height*
856                         ov9640_sizes[isize + 1].width > pixels)
857                 {
858                         return isize;
859                 }
860         }
861         return SXGA;
862 }
863
864 /* following are sensor interface functions implemented by 
865  * OV9640 sensor driver.
866  */
867 static int
868 ov9640sensor_query_control(struct v4l2_queryctrl *qc, void *priv)
869 {
870         int i;
871
872         i = find_vctrl (qc->id);
873         if (i == -EINVAL) {
874                 qc->flags = V4L2_CTRL_FLAG_DISABLED;
875                 return 0;
876         }
877         if (i < 0)
878                 return -EINVAL;
879
880         *qc = control[i].qc;
881         return 0;
882 }
883
884 static int
885 ov9640sensor_get_control(struct v4l2_control *vc, void *priv)
886 {
887         struct ov9640_sensor *sensor = (struct ov9640_sensor *) priv;
888         struct i2c_client *client = &sensor->client;
889         int i, val;
890         struct vcontrol * lvc;
891         
892         i = find_vctrl(vc->id);
893         if (i < 0)
894                 return -EINVAL;
895
896         lvc = &control[i];      
897         if (ov9640_read_reg_mask(client, lvc->reg, (u8 *)&val, lvc->mask))
898                 return -EIO;
899                 
900         val = val >> lvc->start_bit;    
901         if (val >= 0) {
902                 vc->value = lvc->current_value = val;
903                 return 0;
904         } else
905                 return val;
906 }
907
908 static int
909 ov9640sensor_set_control(struct v4l2_control *vc, void *priv)
910 {
911         struct ov9640_sensor *sensor = (struct ov9640_sensor *) priv;
912         struct i2c_client *client = &sensor->client;
913         struct vcontrol *lvc;
914         int val = vc->value;
915         int i;
916
917         i = find_vctrl(vc->id);
918         if (i < 0)
919                 return -EINVAL;
920
921         lvc = &control[i];
922         val = val << lvc->start_bit;
923         if (ov9640_write_reg_mask(client, lvc->reg, (u8 *)&val, (u8)lvc->mask))
924                 return -EIO;
925
926         val = val>> lvc->start_bit;
927         if (val >= 0) {
928                 lvc->current_value = val;
929                 return 0;
930         } else
931                 return val;
932 }
933
934 /* Implement the VIDIOC_ENUM_FMT ioctl for the CAPTURE buffer type.
935  */
936 static int
937 ov9640sensor_enum_pixformat(struct v4l2_fmtdesc *fmt, void *priv)
938 {
939         int index = fmt->index;
940         enum v4l2_buf_type type = fmt->type;
941
942         memset(fmt, 0, sizeof(*fmt));
943         fmt->index = index;
944         fmt->type = type;
945
946         switch (fmt->type) {
947                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
948                 if (index >= NUM_CAPTURE_FORMATS)
949                         return -EINVAL;
950                 break;
951
952                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
953                 if (index >= NUM_OVERLAY_FORMATS)
954                         return -EINVAL;
955                 break;
956
957                 default:
958                         return -EINVAL;
959         }
960
961         fmt->flags = ov9640_formats[index].flags;
962         strlcpy(fmt->description, ov9640_formats[index].description, sizeof(fmt->description));
963         fmt->pixelformat = ov9640_formats[index].pixelformat;
964
965         return 0;
966 }
967
968 /* Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type.  This 
969  * ioctl is used to negotiate the image capture size and pixel format 
970  * without actually making it take effect.
971  */
972 static int
973 ov9640sensor_try_format(struct v4l2_pix_format *pix, void *priv)
974 {
975         enum image_size isize;
976         int ifmt;
977
978         isize = ov9640_find_size(pix->width, pix->height);
979         pix->width = ov9640_sizes[isize].width;
980         pix->height = ov9640_sizes[isize].height;
981         for (ifmt = 0; ifmt < NUM_CAPTURE_FORMATS; ifmt++) {
982                 if (pix->pixelformat == ov9640_formats[ifmt].pixelformat)
983                         break;
984         }
985         if (ifmt == NUM_CAPTURE_FORMATS)
986                 ifmt = 0;
987         pix->pixelformat = ov9640_formats[ifmt].pixelformat;
988         pix->field = V4L2_FIELD_NONE;
989         pix->bytesperline = pix->width*2;
990         pix->sizeimage = pix->bytesperline*pix->height;
991         pix->priv = 0;
992         switch (pix->pixelformat) {
993                 case V4L2_PIX_FMT_YUYV:
994                 case V4L2_PIX_FMT_UYVY:
995                 default:
996                         pix->colorspace = V4L2_COLORSPACE_JPEG;
997                         break;
998                 case V4L2_PIX_FMT_RGB565:
999                 case V4L2_PIX_FMT_RGB565X:
1000                 case V4L2_PIX_FMT_RGB555:
1001                 case V4L2_PIX_FMT_RGB555X:
1002                         pix->colorspace = V4L2_COLORSPACE_SRGB;
1003                         break;
1004         }
1005         return 0;
1006 }
1007
1008 /* Given the image capture format in pix, the nominal frame period in 
1009  * timeperframe, calculate the required xclk frequency 
1010  * The nominal xclk input frequency of the OV9640 is 24MHz, maximum 
1011  * frequency is 48MHz, and minimum frequency is 10MHz.
1012  */
1013 static unsigned long
1014 ov9640sensor_calc_xclk(struct v4l2_pix_format *pix,
1015                         struct v4l2_fract *timeperframe, void *priv)
1016 {
1017         unsigned long tgt_xclk;                 /* target xclk */
1018         unsigned long tgt_fpm;                  /* target frames per minute */
1019         enum image_size isize;
1020
1021         /* We use arbitrary rules to select the xclk frequency.  If the 
1022          * capture size is VGA and the frame rate is greater than 900 
1023          * frames per minute, or if the capture size is SXGA and the 
1024          * frame rate is greater than 450 frames per minutes, then the 
1025          * xclk frequency will be set to 48MHz.  Otherwise, the xclk 
1026          * frequency will be set to 24MHz.  If the mclk frequency is such that 
1027          * the target xclk frequency is not achievable, then xclk will be set 
1028          * as close as to the target as possible.
1029          */
1030         if ((timeperframe->numerator == 0) 
1031                 || (timeperframe->denominator == 0))
1032         {
1033                 /* supply a default nominal_timeperframe of 15 fps */
1034                 timeperframe->numerator = 1;
1035                 timeperframe->denominator = 15;
1036         }
1037         tgt_fpm = (timeperframe->denominator*60)
1038                 / timeperframe->numerator;
1039         tgt_xclk = 24000000;
1040         isize = ov9640_find_size(pix->width, pix->height);
1041         switch (isize) {
1042                 case SXGA:
1043                         if (tgt_fpm > 450)
1044                                 tgt_xclk = 48000000;
1045                         break;
1046                 case VGA:
1047                         if (tgt_fpm > 900)
1048                                 tgt_xclk = 48000000;
1049                         break;
1050                 default:
1051                         break;
1052         }
1053         return tgt_xclk;
1054 }
1055
1056 /* Given a capture format in pix, the frame period in timeperframe, and
1057  * the xclk frequency, set the capture format of the OV9640 sensor.
1058  * The actual frame period will be returned in timeperframe.
1059  */
1060 static int
1061 ov9640sensor_configure(struct v4l2_pix_format *pix, unsigned long xclk,
1062                         struct v4l2_fract *timeperframe, void *priv)
1063 {
1064         struct ov9640_sensor *sensor = (struct ov9640_sensor *) priv;
1065         enum pixel_format pfmt = YUV;
1066
1067         switch (pix->pixelformat) {
1068                 case V4L2_PIX_FMT_RGB565:
1069                 case V4L2_PIX_FMT_RGB565X:
1070                         pfmt = RGB565;
1071                         break;
1072                 case V4L2_PIX_FMT_RGB555:
1073                 case V4L2_PIX_FMT_RGB555X:
1074                         pfmt = RGB555;
1075                         break;
1076                 case V4L2_PIX_FMT_YUYV:
1077                 case V4L2_PIX_FMT_UYVY:
1078                 default:
1079                         pfmt = YUV;
1080         }
1081
1082         return ov9640_configure(&sensor->client,
1083                                 ov9640_find_size(pix->width, pix->height),
1084                                 pfmt, xclk, timeperframe);
1085 }
1086
1087 /* Prepare for the driver to exit.
1088  * Balances ov9640sensor_init().
1089  * This function must de-initialize the sensor and its associated data 
1090  * structures.
1091  */
1092 static int
1093 ov9640sensor_cleanup(void *priv)
1094 {
1095         struct ov9640_sensor *sensor = (struct ov9640_sensor *) priv;
1096
1097         if (sensor) {
1098                 i2c_del_driver(&ov9640sensor_i2c_driver);
1099                 ov9640_powerdown();
1100         }
1101         return 0;
1102 }
1103
1104
1105 static struct i2c_driver ov9640sensor_i2c_driver = {
1106         .driver {
1107                 .name           = "ov9640",
1108         },
1109         .id             = I2C_DRIVERID_MISC, /*FIXME:accroding to i2c-ids.h */
1110         .attach_adapter = ov9640_i2c_probe_adapter,
1111         .detach_client  = ov9640_i2c_detach_client,
1112 };
1113
1114
1115 /* Initialize the OV9640 sensor.
1116  * This routine allocates and initializes the data structure for the sensor, 
1117  * powers up the sensor, registers the I2C driver, and sets a default image 
1118  * capture format in pix.  The capture format is not actually programmed 
1119  * into the OV9640 sensor by this routine.
1120  * This function must return a non-NULL value to indicate that 
1121  * initialization is successful.
1122  */
1123 static void *
1124 ov9640sensor_init(struct v4l2_pix_format *pix)
1125 {
1126         struct ov9640_sensor *sensor = &ov9640;
1127         int err;
1128
1129         memset(sensor, 0, sizeof(*sensor));
1130  
1131         /* power-up the sensor */
1132         if (ov9640_powerup())
1133                 return NULL;
1134
1135         err = i2c_add_driver(&ov9640sensor_i2c_driver);
1136         if (err) {
1137                 printk(KERN_ERR "Failed to register OV9640 I2C client.\n");
1138                 return NULL;
1139         }
1140         if (!sensor->client.adapter) {
1141                 printk(KERN_WARNING 
1142                         "Failed to detect OV9640 sensor chip.\n");
1143                 return NULL;
1144         }
1145         else {
1146                 printk(KERN_INFO 
1147                         "OV9640 sensor chip version 0x%02x detected\n", sensor->ver);
1148         }
1149
1150         /* Make the default capture format QCIF RGB565 */
1151         pix->width = ov9640_sizes[QCIF].width;
1152         pix->height = ov9640_sizes[QCIF].height;
1153         pix->pixelformat = V4L2_PIX_FMT_RGB565;
1154         ov9640sensor_try_format(pix, NULL);
1155
1156         return (void *)sensor;
1157 }
1158
1159 struct camera_sensor camera_sensor_if = {
1160         .version        = 0x01,
1161         .name           = "OV9640",
1162         .init           = ov9640sensor_init,
1163         .cleanup        = ov9640sensor_cleanup,
1164         .enum_pixformat = ov9640sensor_enum_pixformat,
1165         .try_format     = ov9640sensor_try_format,
1166         .calc_xclk      = ov9640sensor_calc_xclk,
1167         .configure      = ov9640sensor_configure,
1168         .query_control  = ov9640sensor_query_control,
1169         .get_control    = ov9640sensor_get_control,
1170         .set_control    = ov9640sensor_set_control,
1171         .power_on       = ov9640sensor_power_on,
1172         .power_off      = ov9640sensor_power_off,
1173 };
1174
1175 void print_ov9640_regs(void *priv)
1176 {
1177         struct ov9640_sensor *sensor = (struct ov9640_sensor *) priv;
1178         u8 reg, val;
1179         for (reg=0x00; reg <=0x8A; reg++)
1180                 if (ov9640_read_reg(&sensor->client,reg,&val))
1181                         printk("error reading %x\n", reg);
1182                 else
1183                         printk("reg %x = %x\n", reg, val);       
1184 }
1185
1186 #endif  /* ifdef CAMERA_OV9640 */