]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/board-n800-camera.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / board-n800-camera.c
1 /*
2  * arch/arm/mach-omap2/board-n800-camera.c
3  *
4  * Copyright (C) 2007 Nokia Corporation
5  *
6  * Contact: Sakari Ailus <sakari.ailus@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/clk.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
27 #include <linux/videodev2.h>
28
29 #include <media/v4l2-int-device.h>
30
31 #include <asm/arch/menelaus.h>
32 #include <asm/arch/gpio.h>
33 #include <asm/arch/board.h>
34
35 #include <../drivers/cbus/retu.h>
36 #include <../drivers/media/video/tcm825x.h>
37
38 #if defined (CONFIG_VIDEO_TCM825X) || defined (CONFIG_VIDEO_TCM825X_MODULE)
39
40 #define OMAP24XX_CAMERA_JAM_HACK
41
42 #ifdef OMAP24XX_CAMERA_JAM_HACK
43 /*
44  * We don't need to check every pixel to assume that the frame is
45  * corrupt and the sensor is jammed. CHECK_X and CHECK_Y are the
46  * number of u32s to check per line / row, plus there are two lines in
47  * the bottom of the frame.
48  */
49 #define CHECK_X 8
50 #define CHECK_Y 6
51 /*
52  * Start checking after this many frames since resetting the sensor.
53  * Sometimes the first frame(s) is(/are) black which could trigger
54  * unwanted reset(s).
55  */
56 #define JAM_CHECK_AFTER 3
57 /*
58  * If the sensor is quickly brought into bright conditions from dark,
59  * it may temporarily be saturated, leaving out the normal background
60  * noise. This many saturated frames may go through before the sensor
61  * is considered jammed.
62  */
63 #define SATURATED_MAX 30
64 #endif
65
66 #define N800_CAM_SENSOR_RESET_GPIO      53
67
68 static int sensor_okay;
69 #ifdef OMAP24XX_CAMERA_JAM_HACK
70 static int frames_after_reset;
71 static int saturated_count;
72 #endif
73
74 const static struct tcm825x_reg tcm825x_default_regs_[] = {
75         /* initial settings for 2.5 V */
76         {0x00, 0x03}, {0x03, 0x29}, {0xaa, 0x2a}, {0xc0, 0x2b},
77         {0x10, 0x2c}, {0x4c, 0x2d}, {0x9c, 0x3f},
78
79         /* main settings */
80         {0x00, 0x00}, {0x30, 0x01}, {0x0e, 0x02}, /* initial */
81         {0x0f, 0x04}, {0x02, 0x05}, {0x0d, 0x06}, {0xc0, 0x07},
82         {0x38, 0x08}, {0x50, 0x09}, {0x80, 0x0a}, {0x40, 0x0b},
83         {0x40, 0x0c}, {0x00, 0x0d}, {0x04, 0x0e}, {0x04, 0x0f},
84         {0x22, 0x10}, {0x96, 0x11}, {0xf0, 0x12}, {0x08, 0x13},
85         {0x08, 0x14}, {0x30, 0x15}, {0x30, 0x16}, {0x01, 0x17},
86         {0x40, 0x18}, {0x87, 0x19}, {0x2b, 0x1a}, {0x84, 0x1b},
87         {0x52, 0x1c}, {0x44, 0x1d}, {0x68, 0x1e}, {0x00, 0x1f},
88         {0x00, 0x20}, {0x01, 0x21}, {0x27, 0x22}, {0x40, 0x23},
89         {0x27, 0x24}, {0x5f, 0x25}, {0x00, 0x26}, {0x16, 0x27},
90         {0x23, 0x28}, /* initial */ /* initial */ /* initial */
91         /* initial */ /* initial */ {0x00, 0x2e}, {0x00, 0x2f},
92         {0x00, 0x30}, {0x00, 0x31}, {0x00, 0x32}, {0x00, 0x33},
93         {0x00, 0x34}, {0x00, 0x35}, {0x00, 0x36}, {0x00, 0x37},
94         {0x00, 0x38}, {0x8c, 0x39}, {0xc8, 0x3A}, {0x80, 0x3b},
95         {0x00, 0x3c}, {0x17, 0x3d}, {0x85, 0x3e}, /* initial */
96         {0xa0, 0x40}, {0x00, 0x41}, {0x00, 0x42}, {0x00, 0x43},
97         {0x08, 0x44}, {0x12, 0x45}, {0x00, 0x46}, {0x20, 0x47},
98         {0x30, 0x48}, {0x18, 0x49}, {0x20, 0x4a}, {0x4d, 0x4b},
99         {0x0c, 0x4c}, {0xe0, 0x4d}, {0x20, 0x4e}, {0x89, 0x4f},
100         {0x21, 0x50}, {0x80, 0x51}, {0x02, 0x52}, {0x00, 0x53},
101         {0x30, 0x54}, {0x90, 0x55}, {0x40, 0x56}, {0x06, 0x57},
102         {0x0f, 0x58}, {0x23, 0x59}, {0x08, 0x5A}, {0x04, 0x5b},
103         {0x08, 0x5c}, {0x08, 0x5d}, {0x08, 0x5e}, {0x08, 0x5f},
104         {TCM825X_VAL_TERM, TCM825X_REG_TERM}
105 };
106
107 static int tcm825x_is_okay(void)
108 {
109         return sensor_okay;
110 }
111
112 /*
113  * VSIM1        --> CAM_IOVDD   --> IOVDD (1.8 V)
114  */
115 static int tcm825x_power_on(void)
116 {
117         int ret;
118
119         /* Set VMEM to 1.5V and VIO to 2.5V */
120         ret = menelaus_set_vmem(1500);
121         if (ret < 0) {
122                 /* Try once more, it seems the sensor power up causes
123                  * some problems on the I2C bus. */
124                 ret = menelaus_set_vmem(1500);
125                 if (ret < 0)
126                         return ret;
127         }
128         msleep(1);
129
130         ret = menelaus_set_vio(2500);
131         if (ret < 0)
132                 return ret;
133
134         /* Set VSim1 on */
135         retu_write_reg(RETU_REG_CTRL_SET, 0x0080);
136         msleep(1);
137
138         omap_set_gpio_dataout(N800_CAM_SENSOR_RESET_GPIO, 1);
139         msleep(1);
140
141         saturated_count = 0;
142         frames_after_reset = 0;
143
144         return 0;
145 }
146
147 static int tcm825x_power_off(void)
148 {
149         int ret;
150
151         omap_set_gpio_dataout(N800_CAM_SENSOR_RESET_GPIO, 0);
152         msleep(1);
153
154         /* Set VSim1 off */
155         retu_write_reg(RETU_REG_CTRL_CLR, 0x0080);
156         msleep(1);
157
158         /* Set VIO_MODE to off */
159         ret = menelaus_set_vio(0);
160         if (ret < 0)
161                 return ret;
162         msleep(1);
163
164         /* Set VMEM_MODE to off */
165         ret = menelaus_set_vmem(0);
166         if (ret < 0)
167                 return ret;
168         msleep(1);
169
170         return 0;
171 }
172
173 static int tcm825x_power_set(int power)
174 {
175         BUG_ON(!sensor_okay);
176
177         if (power)
178                 return tcm825x_power_on();
179         else
180                 return tcm825x_power_off();
181 }
182
183 static const struct tcm825x_reg *tcm825x_default_regs(void)
184 {
185         return tcm825x_default_regs_;
186 }
187
188 #ifdef OMAP24XX_CAMERA_JAM_HACK
189 /*
190  * Check for jammed sensor, in which case all horizontal lines are
191  * equal. Handle also case where sensor could be saturated awhile in
192  * case of rapid increase of brightness.
193  */
194 static int tcm825x_needs_reset(struct v4l2_int_device *s, void *buf,
195                                struct v4l2_pix_format *pix)
196 {
197         int i, j;
198         uint32_t xor, xor2;
199         uint32_t offset;
200         uint32_t dx_offset;
201         uint32_t saturated_pattern;
202         int is_saturated = 1;
203
204         switch (pix->pixelformat) {
205         default:
206         case V4L2_PIX_FMT_RGB565:
207                 saturated_pattern = 0xffffffff; /* guess */
208                 break;
209         case V4L2_PIX_FMT_UYVY:
210                 saturated_pattern = 0xe080e080;
211                 break;
212         }
213
214         /* This won't work for height under 2 at all. */
215         if (pix->height < 2)
216                 return 0;
217         /* Check that there is enough image data. */
218         if (pix->width * TCM825X_BYTES_PER_PIXEL < sizeof(uint32_t))
219                 return 0;
220         /*
221          * Don't check for jamming immediately. Sometimes frames
222          * immediately after reset are black.
223          */
224         if (frames_after_reset < JAM_CHECK_AFTER) {
225                 frames_after_reset++;
226                 return 0;
227         }
228
229         dx_offset = ((pix->width - sizeof(uint32_t) / TCM825X_BYTES_PER_PIXEL)
230                      * TCM825X_BYTES_PER_PIXEL) / (CHECK_X - 1);
231         dx_offset = dx_offset - dx_offset % TCM825X_BYTES_PER_PIXEL;
232         /*
233          * Check two lines in the bottom first. They're unlikely to be
234          * saturated and quick to check.
235          */
236         offset = (pix->height - 2) * pix->bytesperline;
237         xor = xor2 = 0;
238         for (j = 0; j < CHECK_X; j++) {
239                 uint32_t *val = buf + offset;
240                 uint32_t *val2 = buf + offset + pix->bytesperline;
241                 xor ^= *val;
242                 if (*val != saturated_pattern)
243                         is_saturated = 0;
244                 xor2 ^= *val2;
245                 if (xor2 != xor) {
246                         saturated_count = 0;
247                         return 0;
248                 }
249                 offset += dx_offset;
250         }
251         /* Check the rest of the picture. */
252         offset = 0;
253         for (i = 0; i < CHECK_Y; i++) {
254                 uint32_t offset2 = offset;
255                 xor2 = 0;
256                 for (j = 0; j < CHECK_X; j++) {
257                         uint32_t *val = buf + offset2;
258                         xor2 ^= *val;
259                         offset2 += dx_offset;
260                 }
261                 if (xor2 != xor) {
262                         saturated_count = 0;
263                         return 0;
264                 }
265                 offset += pix->bytesperline * ((pix->height - 2) / CHECK_Y);
266         }
267
268         if (is_saturated && saturated_count++ < SATURATED_MAX)
269                 return 0;
270
271         return -EIO;
272 }
273 #else
274 static int tcm825x_needs_reset(struct v4l2_int_device *s, void *buf,
275                                struct v4l2_pix_format *pix)
276 {
277         return 0;
278 }
279 #endif
280
281 static const struct v4l2_ifparm ifparm = {
282         .if_type = V4L2_IF_TYPE_BT656,
283         .u = {
284                 .bt656 = {
285                          .frame_start_on_rising_vs = 1,
286                          .latch_clk_inv = 1,
287                          .mode = V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT,
288                          .clock_min = TCM825X_XCLK_MIN,
289                          .clock_max = TCM825X_XCLK_MAX,
290                  },
291         },
292 };
293
294 static int tcm825x_ifparm(struct v4l2_ifparm *p)
295 {
296         *p = ifparm;
297
298         return 0;
299 }
300
301 const struct tcm825x_platform_data n800_tcm825x_platform_data = {
302         .is_okay      = tcm825x_is_okay,
303         .power_set    = tcm825x_power_set,
304         .default_regs = tcm825x_default_regs,
305         .needs_reset  = tcm825x_needs_reset,
306         .ifparm       = tcm825x_ifparm,
307 };
308
309 void __init n800_cam_init(void)
310 {
311         int r;
312
313         r = omap_request_gpio(N800_CAM_SENSOR_RESET_GPIO);
314         if (r < 0) {
315                 printk(KERN_WARNING "%s: failed to request gpio\n",
316                         __FUNCTION__);
317                 return;
318         }
319
320         omap_set_gpio_dataout(N800_CAM_SENSOR_RESET_GPIO, 0);
321         omap_set_gpio_direction(N800_CAM_SENSOR_RESET_GPIO, 0);
322
323         sensor_okay = 1;
324 }
325
326 #else
327 void __init n800_cam_init(void)
328 {
329 }
330
331 #endif