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