PRCM: OMAP3: Fix to wrongly modified omap2_clk_wait_ready
[linux-ginger.git] / arch / arm / mach-omap2 / board-n800-camera.c
blobaafa5471d4d0601f8b6c2e64afb75a00ca03da46
1 /*
2 * arch/arm/mach-omap2/board-n800-camera.c
4 * Copyright (C) 2007 Nokia Corporation
6 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
24 #include <linux/clk.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
27 #include <linux/videodev2.h>
29 #include <media/v4l2-int-device.h>
31 #include <asm/mach-types.h>
33 #include <asm/arch/menelaus.h>
34 #include <asm/arch/gpio.h>
35 #include <asm/arch/board.h>
37 #include <../drivers/cbus/retu.h>
38 #include <../drivers/media/video/tcm825x.h>
40 #if defined (CONFIG_VIDEO_TCM825X) || defined (CONFIG_VIDEO_TCM825X_MODULE)
42 #define OMAP24XX_CAMERA_JAM_HACK
44 #ifdef OMAP24XX_CAMERA_JAM_HACK
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.
51 #define CHECK_X 8
52 #define CHECK_Y 6
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).
58 #define JAM_CHECK_AFTER 3
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.
65 #define SATURATED_MAX 30
66 #endif
68 #define N800_CAM_SENSOR_RESET_GPIO 53
70 static int sensor_okay;
71 #ifdef OMAP24XX_CAMERA_JAM_HACK
72 static int frames_after_reset;
73 static int saturated_count;
74 #endif
76 const static struct tcm825x_reg tcm825x_regs_n800[] = {
77 /* initial settings for 2.5 V */
78 {0x00, 0x03}, {0x03, 0x29}, {0xaa, 0x2a}, {0xc0, 0x2b},
79 {0x10, 0x2c}, {0x4c, 0x2d}, {0x9c, 0x3f},
81 /* main settings */
82 {0x00, 0x00}, {0x30, 0x01}, {0x0e, 0x02}, /* initial */
83 {0x0f, 0x04}, {0x02, 0x05}, {0x0d, 0x06}, {0xc0, 0x07},
84 {0x38, 0x08}, {0x50, 0x09}, {0x80, 0x0a}, {0x40, 0x0b},
85 {0x40, 0x0c}, {0x00, 0x0d}, {0x04, 0x0e}, {0x04, 0x0f},
86 {0x22, 0x10}, {0x96, 0x11}, {0xf0, 0x12}, {0x08, 0x13},
87 {0x08, 0x14}, {0x30, 0x15}, {0x30, 0x16}, {0x01, 0x17},
88 {0x40, 0x18}, {0x87, 0x19}, {0x2b, 0x1a}, {0x84, 0x1b},
89 {0x52, 0x1c}, {0x44, 0x1d}, {0x68, 0x1e}, {0x00, 0x1f},
90 {0x00, 0x20}, {0x01, 0x21}, {0x27, 0x22}, {0x40, 0x23},
91 {0x27, 0x24}, {0x5f, 0x25}, {0x00, 0x26}, {0x16, 0x27},
92 {0x23, 0x28}, /* initial */ /* initial */ /* initial */
93 /* initial */ /* initial */ {0x00, 0x2e}, {0x00, 0x2f},
94 {0x00, 0x30}, {0x00, 0x31}, {0x00, 0x32}, {0x00, 0x33},
95 {0x00, 0x34}, {0x00, 0x35}, {0x00, 0x36}, {0x00, 0x37},
96 {0x00, 0x38}, {0x8c, 0x39}, {0xc8, 0x3A}, {0x80, 0x3b},
97 {0x00, 0x3c}, {0x17, 0x3d}, {0x85, 0x3e}, /* initial */
98 {0xa0, 0x40}, {0x00, 0x41}, {0x00, 0x42}, {0x00, 0x43},
99 {0x08, 0x44}, {0x12, 0x45}, {0x00, 0x46}, {0x20, 0x47},
100 {0x30, 0x48}, {0x18, 0x49}, {0x20, 0x4a}, {0x4d, 0x4b},
101 {0x0c, 0x4c}, {0xe0, 0x4d}, {0x20, 0x4e}, {0x89, 0x4f},
102 {0x21, 0x50}, {0x80, 0x51}, {0x02, 0x52}, {0x00, 0x53},
103 {0x30, 0x54}, {0x90, 0x55}, {0x40, 0x56}, {0x06, 0x57},
104 {0x0f, 0x58}, {0x23, 0x59}, {0x08, 0x5A}, {0x04, 0x5b},
105 {0x08, 0x5c}, {0x08, 0x5d}, {0x08, 0x5e}, {0x08, 0x5f},
106 {TCM825X_VAL_TERM, TCM825X_REG_TERM}
109 const static struct tcm825x_reg tcm825x_regs_n810[] = {
110 /* initial settings for 2.5 V */
111 {0x00, 0x03}, {0x03, 0x29}, {0xaa, 0x2a}, {0xc0, 0x2b},
112 {0x10, 0x2c}, {0x4c, 0x2d}, {0x9c, 0x3f},
114 /* main settings */
115 {0x00, 0x00}, {0x30, 0x01}, {0x0e, 0x02}, /* initial */
116 {0xcf, 0x04}, {0x02, 0x05}, {0x0d, 0x06}, {0xc0, 0x07},
117 {0x38, 0x08}, {0x50, 0x09}, {0x80, 0x0a}, {0x40, 0x0b},
118 {0x40, 0x0c}, {0x00, 0x0d}, {0x04, 0x0e}, {0x04, 0x0f},
119 {0x22, 0x10}, {0x96, 0x11}, {0xf0, 0x12}, {0x08, 0x13},
120 {0x08, 0x14}, {0x30, 0x15}, {0x30, 0x16}, {0x01, 0x17},
121 {0x40, 0x18}, {0x87, 0x19}, {0x2b, 0x1a}, {0x84, 0x1b},
122 {0x52, 0x1c}, {0x44, 0x1d}, {0x68, 0x1e}, {0x00, 0x1f},
123 {0x00, 0x20}, {0x01, 0x21}, {0x27, 0x22}, {0x40, 0x23},
124 {0x27, 0x24}, {0x5f, 0x25}, {0x00, 0x26}, {0x16, 0x27},
125 {0x23, 0x28}, /* initial */ /* initial */ /* initial */
126 /* initial */ /* initial */ {0x00, 0x2e}, {0x00, 0x2f},
127 {0x00, 0x30}, {0x00, 0x31}, {0x00, 0x32}, {0x00, 0x33},
128 {0x00, 0x34}, {0x00, 0x35}, {0x00, 0x36}, {0x00, 0x37},
129 {0x00, 0x38}, {0x8c, 0x39}, {0xc8, 0x3A}, {0x80, 0x3b},
130 {0x00, 0x3c}, {0x17, 0x3d}, {0x85, 0x3e}, /* initial */
131 {0xa0, 0x40}, {0x00, 0x41}, {0x00, 0x42}, {0x00, 0x43},
132 {0x08, 0x44}, {0x12, 0x45}, {0x00, 0x46}, {0x20, 0x47},
133 {0x30, 0x48}, {0x18, 0x49}, {0x20, 0x4a}, {0x4d, 0x4b},
134 {0x0c, 0x4c}, {0xe0, 0x4d}, {0x20, 0x4e}, {0x89, 0x4f},
135 {0x21, 0x50}, {0x80, 0x51}, {0x02, 0x52}, {0x00, 0x53},
136 {0x30, 0x54}, {0x90, 0x55}, {0x40, 0x56}, {0x06, 0x57},
137 {0x0f, 0x58}, {0x23, 0x59}, {0x08, 0x5A}, {0x04, 0x5b},
138 {0x08, 0x5c}, {0x08, 0x5d}, {0x08, 0x5e}, {0x08, 0x5f},
139 {TCM825X_VAL_TERM, TCM825X_REG_TERM}
142 static int tcm825x_is_okay(void)
144 return sensor_okay;
148 * VSIM1 --> CAM_IOVDD --> IOVDD (1.8 V)
150 static int tcm825x_power_on(void)
152 int ret;
154 /* Set VMEM to 1.5V and VIO to 2.5V */
155 ret = menelaus_set_vmem(1500);
156 if (ret < 0) {
157 /* Try once more, it seems the sensor power up causes
158 * some problems on the I2C bus. */
159 ret = menelaus_set_vmem(1500);
160 if (ret < 0)
161 return ret;
163 msleep(1);
165 ret = menelaus_set_vio(2500);
166 if (ret < 0)
167 return ret;
169 /* Set VSim1 on */
170 retu_write_reg(RETU_REG_CTRL_SET, 0x0080);
171 msleep(1);
173 omap_set_gpio_dataout(N800_CAM_SENSOR_RESET_GPIO, 1);
174 msleep(1);
176 saturated_count = 0;
177 frames_after_reset = 0;
179 return 0;
182 static int tcm825x_power_off(void)
184 int ret;
186 omap_set_gpio_dataout(N800_CAM_SENSOR_RESET_GPIO, 0);
187 msleep(1);
189 /* Set VSim1 off */
190 retu_write_reg(RETU_REG_CTRL_CLR, 0x0080);
191 msleep(1);
193 /* Set VIO_MODE to off */
194 ret = menelaus_set_vio(0);
195 if (ret < 0)
196 return ret;
197 msleep(1);
199 /* Set VMEM_MODE to off */
200 ret = menelaus_set_vmem(0);
201 if (ret < 0)
202 return ret;
203 msleep(1);
205 return 0;
208 static int tcm825x_power_set(int power)
210 BUG_ON(!sensor_okay);
212 if (power)
213 return tcm825x_power_on();
214 else
215 return tcm825x_power_off();
218 static const struct tcm825x_reg *tcm825x_default_regs(void)
220 if (machine_is_nokia_n810())
221 return tcm825x_regs_n810;
223 return tcm825x_regs_n800;
226 #ifdef OMAP24XX_CAMERA_JAM_HACK
228 * Check for jammed sensor, in which case all horizontal lines are
229 * equal. Handle also case where sensor could be saturated awhile in
230 * case of rapid increase of brightness.
232 static int tcm825x_needs_reset(struct v4l2_int_device *s, void *buf,
233 struct v4l2_pix_format *pix)
235 int i, j;
236 uint32_t xor, xor2;
237 uint32_t offset;
238 uint32_t dx_offset;
239 uint32_t saturated_pattern;
240 int is_saturated = 1;
242 switch (pix->pixelformat) {
243 default:
244 case V4L2_PIX_FMT_RGB565:
245 saturated_pattern = 0xffffffff; /* guess */
246 break;
247 case V4L2_PIX_FMT_UYVY:
248 saturated_pattern = 0xe080e080;
249 break;
252 /* This won't work for height under 2 at all. */
253 if (pix->height < 2)
254 return 0;
255 /* Check that there is enough image data. */
256 if (pix->width * TCM825X_BYTES_PER_PIXEL < sizeof(uint32_t))
257 return 0;
259 * Don't check for jamming immediately. Sometimes frames
260 * immediately after reset are black.
262 if (frames_after_reset < JAM_CHECK_AFTER) {
263 frames_after_reset++;
264 return 0;
267 dx_offset = ((pix->width - sizeof(uint32_t) / TCM825X_BYTES_PER_PIXEL)
268 * TCM825X_BYTES_PER_PIXEL) / (CHECK_X - 1);
269 dx_offset = dx_offset - dx_offset % TCM825X_BYTES_PER_PIXEL;
271 * Check two lines in the bottom first. They're unlikely to be
272 * saturated and quick to check.
274 offset = (pix->height - 2) * pix->bytesperline;
275 xor = xor2 = 0;
276 for (j = 0; j < CHECK_X; j++) {
277 uint32_t *val = buf + offset;
278 uint32_t *val2 = buf + offset + pix->bytesperline;
279 xor ^= *val;
280 if (*val != saturated_pattern)
281 is_saturated = 0;
282 xor2 ^= *val2;
283 if (xor2 != xor) {
284 saturated_count = 0;
285 return 0;
287 offset += dx_offset;
289 /* Check the rest of the picture. */
290 offset = 0;
291 for (i = 0; i < CHECK_Y; i++) {
292 uint32_t offset2 = offset;
293 xor2 = 0;
294 for (j = 0; j < CHECK_X; j++) {
295 uint32_t *val = buf + offset2;
296 xor2 ^= *val;
297 offset2 += dx_offset;
299 if (xor2 != xor) {
300 saturated_count = 0;
301 return 0;
303 offset += pix->bytesperline * ((pix->height - 2) / CHECK_Y);
306 if (is_saturated && saturated_count++ < SATURATED_MAX)
307 return 0;
309 return -EIO;
311 #else
312 static int tcm825x_needs_reset(struct v4l2_int_device *s, void *buf,
313 struct v4l2_pix_format *pix)
315 return 0;
317 #endif
319 static const struct v4l2_ifparm ifparm = {
320 .if_type = V4L2_IF_TYPE_BT656,
321 .u = {
322 .bt656 = {
323 .frame_start_on_rising_vs = 1,
324 .latch_clk_inv = 1,
325 .mode = V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT,
326 .clock_min = TCM825X_XCLK_MIN,
327 .clock_max = TCM825X_XCLK_MAX,
332 static int tcm825x_ifparm(struct v4l2_ifparm *p)
334 *p = ifparm;
336 return 0;
339 static int tcm825x_is_upside_down(void)
341 return machine_is_nokia_n810();
344 const struct tcm825x_platform_data n800_tcm825x_platform_data = {
345 .is_okay = tcm825x_is_okay,
346 .power_set = tcm825x_power_set,
347 .default_regs = tcm825x_default_regs,
348 .needs_reset = tcm825x_needs_reset,
349 .ifparm = tcm825x_ifparm,
350 .is_upside_down = tcm825x_is_upside_down,
353 void __init n800_cam_init(void)
355 int r;
357 r = omap_request_gpio(N800_CAM_SENSOR_RESET_GPIO);
358 if (r < 0) {
359 printk(KERN_WARNING "%s: failed to request gpio\n",
360 __func__);
361 return;
364 omap_set_gpio_dataout(N800_CAM_SENSOR_RESET_GPIO, 0);
365 omap_set_gpio_direction(N800_CAM_SENSOR_RESET_GPIO, 0);
367 sensor_okay = 1;
370 #else
371 void __init n800_cam_init(void)
375 #endif