V4L/DVB (9702): Move the ov9650 vflip table to avoid compilation warnings on older...
[linux-2.6/btrfs-unstable.git] / drivers / media / video / gspca / m5602 / m5602_ov9650.c
blob68f651fb50808c1acf3bb048bb0b1418d3ef68ce
1 /*
2 * Driver for the ov9650 sensor
4 * Copyright (C) 2008 Erik Andrén
5 * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
6 * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
8 * Portions of code to USB interface and ALi driver software,
9 * Copyright (c) 2006 Willem Duinker
10 * v4l2 interface modeled after the V4L2 driver
11 * for SN9C10x PC Camera Controllers
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation, version 2.
19 #include "m5602_ov9650.h"
21 /* Vertically and horizontally flips the image if matched, needed for machines
22 where the sensor is mounted upside down */
23 static
24 const
25 struct dmi_system_id ov9650_flip_dmi_table[] = {
27 .ident = "ASUS A6VC",
28 .matches = {
29 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
30 DMI_MATCH(DMI_PRODUCT_NAME, "A6VC")
34 .ident = "ASUS A6VM",
35 .matches = {
36 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
37 DMI_MATCH(DMI_PRODUCT_NAME, "A6VM")
41 .ident = "ASUS A6JC",
42 .matches = {
43 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
44 DMI_MATCH(DMI_PRODUCT_NAME, "A6JC")
48 .ident = "ASUS A6Kt",
49 .matches = {
50 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
51 DMI_MATCH(DMI_PRODUCT_NAME, "A6Kt")
54 { }
57 int ov9650_read_sensor(struct sd *sd, const u8 address,
58 u8 *i2c_data, const u8 len)
60 int err, i;
62 /* The ov9650 registers have a max depth of one byte */
63 if (len > 1 || !len)
64 return -EINVAL;
66 do {
67 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
68 } while ((*i2c_data & I2C_BUSY) && !err);
70 m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
71 ov9650.i2c_slave_id);
72 m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
73 m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
74 m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
76 for (i = 0; i < len; i++) {
77 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
79 PDEBUG(D_CONF, "Reading sensor register "
80 "0x%x containing 0x%x ", address, *i2c_data);
82 return (err < 0) ? err : 0;
85 int ov9650_write_sensor(struct sd *sd, const u8 address,
86 u8 *i2c_data, const u8 len)
88 int err, i;
89 u8 *p;
90 struct usb_device *udev = sd->gspca_dev.dev;
91 __u8 *buf = sd->gspca_dev.usb_buf;
93 /* The ov9650 only supports one byte writes */
94 if (len > 1 || !len)
95 return -EINVAL;
97 memcpy(buf, sensor_urb_skeleton,
98 sizeof(sensor_urb_skeleton));
100 buf[11] = sd->sensor->i2c_slave_id;
101 buf[15] = address;
103 /* Special case larger sensor writes */
104 p = buf + 16;
106 /* Copy a four byte write sequence for each byte to be written to */
107 for (i = 0; i < len; i++) {
108 memcpy(p, sensor_urb_skeleton + 16, 4);
109 p[3] = i2c_data[i];
110 p += 4;
111 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
112 address, i2c_data[i]);
115 /* Copy the tailer */
116 memcpy(p, sensor_urb_skeleton + 20, 4);
118 /* Set the total length */
119 p[3] = 0x10 + len;
121 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
122 0x04, 0x40, 0x19,
123 0x0000, buf,
124 20 + len * 4, M5602_URB_MSG_TIMEOUT);
126 return (err < 0) ? err : 0;
129 int ov9650_probe(struct sd *sd)
131 u8 prod_id = 0, ver_id = 0, i;
133 if (force_sensor) {
134 if (force_sensor == OV9650_SENSOR) {
135 info("Forcing an %s sensor", ov9650.name);
136 goto sensor_found;
138 /* If we want to force another sensor,
139 don't try to probe this one */
140 return -ENODEV;
143 info("Probing for an ov9650 sensor");
145 /* Run the pre-init to actually probe the unit */
146 for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
147 u8 data = preinit_ov9650[i][2];
148 if (preinit_ov9650[i][0] == SENSOR)
149 ov9650_write_sensor(sd,
150 preinit_ov9650[i][1], &data, 1);
151 else
152 m5602_write_bridge(sd, preinit_ov9650[i][1], data);
155 if (ov9650_read_sensor(sd, OV9650_PID, &prod_id, 1))
156 return -ENODEV;
158 if (ov9650_read_sensor(sd, OV9650_VER, &ver_id, 1))
159 return -ENODEV;
161 if ((prod_id == 0x96) && (ver_id == 0x52)) {
162 info("Detected an ov9650 sensor");
163 goto sensor_found;
166 return -ENODEV;
168 sensor_found:
169 sd->gspca_dev.cam.cam_mode = ov9650.modes;
170 sd->gspca_dev.cam.nmodes = ov9650.nmodes;
171 sd->desc->ctrls = ov9650.ctrls;
172 sd->desc->nctrls = ov9650.nctrls;
173 return 0;
176 int ov9650_init(struct sd *sd)
178 int i, err = 0;
179 u8 data;
181 if (dump_sensor)
182 ov9650_dump_registers(sd);
184 for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
185 data = init_ov9650[i][2];
186 if (init_ov9650[i][0] == SENSOR)
187 err = ov9650_write_sensor(sd, init_ov9650[i][1],
188 &data, 1);
189 else
190 err = m5602_write_bridge(sd, init_ov9650[i][1], data);
193 if (!err && dmi_check_system(ov9650_flip_dmi_table)) {
194 info("vflip quirk active");
195 data = 0x30;
196 err = ov9650_write_sensor(sd, OV9650_MVFP, &data, 1);
199 return (err < 0) ? err : 0;
202 int ov9650_power_down(struct sd *sd)
204 int i;
205 for (i = 0; i < ARRAY_SIZE(power_down_ov9650); i++) {
206 u8 data = power_down_ov9650[i][2];
207 if (power_down_ov9650[i][0] == SENSOR)
208 ov9650_write_sensor(sd,
209 power_down_ov9650[i][1], &data, 1);
210 else
211 m5602_write_bridge(sd, power_down_ov9650[i][1], data);
214 return 0;
217 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
219 struct sd *sd = (struct sd *) gspca_dev;
220 u8 i2c_data;
221 int err;
223 err = ov9650_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
224 if (err < 0)
225 goto out;
226 *val = i2c_data & 0x03;
228 err = ov9650_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
229 if (err < 0)
230 goto out;
231 *val |= (i2c_data << 2);
233 err = ov9650_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
234 if (err < 0)
235 goto out;
236 *val |= (i2c_data & 0x3f) << 10;
238 PDEBUG(D_V4L2, "Read exposure %d", *val);
239 out:
240 return (err < 0) ? err : 0;
243 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
245 struct sd *sd = (struct sd *) gspca_dev;
246 u8 i2c_data;
247 int err;
249 PDEBUG(D_V4L2, "Set exposure to %d",
250 val & 0xffff);
252 /* The 6 MSBs */
253 i2c_data = (val >> 10) & 0x3f;
254 err = ov9650_write_sensor(sd, OV9650_AECHM,
255 &i2c_data, 1);
256 if (err < 0)
257 goto out;
259 /* The 8 middle bits */
260 i2c_data = (val >> 2) & 0xff;
261 err = ov9650_write_sensor(sd, OV9650_AECH,
262 &i2c_data, 1);
263 if (err < 0)
264 goto out;
266 /* The 2 LSBs */
267 i2c_data = val & 0x03;
268 err = ov9650_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
270 out:
271 return (err < 0) ? err : 0;
274 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
276 int err;
277 u8 i2c_data;
278 struct sd *sd = (struct sd *) gspca_dev;
280 ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
281 *val = (i2c_data & 0x03) << 8;
283 err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
284 *val |= i2c_data;
285 PDEBUG(D_V4L2, "Read gain %d", *val);
286 return (err < 0) ? err : 0;
289 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
291 int err;
292 u8 i2c_data;
293 struct sd *sd = (struct sd *) gspca_dev;
295 /* The 2 MSB */
296 /* Read the OV9650_VREF register first to avoid
297 corrupting the VREF high and low bits */
298 ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
299 /* Mask away all uninteresting bits */
300 i2c_data = ((val & 0x0300) >> 2) |
301 (i2c_data & 0x3F);
302 err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
304 /* The 8 LSBs */
305 i2c_data = val & 0xff;
306 err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
307 return (err < 0) ? err : 0;
310 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
312 int err;
313 u8 i2c_data;
314 struct sd *sd = (struct sd *) gspca_dev;
316 err = ov9650_read_sensor(sd, OV9650_RED, &i2c_data, 1);
317 *val = i2c_data;
319 PDEBUG(D_V4L2, "Read red gain %d", *val);
321 return (err < 0) ? err : 0;
324 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
326 int err;
327 u8 i2c_data;
328 struct sd *sd = (struct sd *) gspca_dev;
330 PDEBUG(D_V4L2, "Set red gain to %d",
331 val & 0xff);
333 i2c_data = val & 0xff;
334 err = ov9650_write_sensor(sd, OV9650_RED, &i2c_data, 1);
336 return (err < 0) ? err : 0;
339 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
341 int err;
342 u8 i2c_data;
343 struct sd *sd = (struct sd *) gspca_dev;
345 err = ov9650_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
346 *val = i2c_data;
348 PDEBUG(D_V4L2, "Read blue gain %d", *val);
350 return (err < 0) ? err : 0;
353 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
355 int err;
356 u8 i2c_data;
357 struct sd *sd = (struct sd *) gspca_dev;
359 PDEBUG(D_V4L2, "Set blue gain to %d",
360 val & 0xff);
362 i2c_data = val & 0xff;
363 err = ov9650_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
365 return (err < 0) ? err : 0;
368 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
370 int err;
371 u8 i2c_data;
372 struct sd *sd = (struct sd *) gspca_dev;
374 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
375 if (dmi_check_system(ov9650_flip_dmi_table))
376 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
377 else
378 *val = (i2c_data & OV9650_HFLIP) >> 5;
379 PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
381 return (err < 0) ? err : 0;
384 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
386 int err;
387 u8 i2c_data;
388 struct sd *sd = (struct sd *) gspca_dev;
390 PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
391 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
392 if (err < 0)
393 goto out;
395 if (dmi_check_system(ov9650_flip_dmi_table))
396 i2c_data = ((i2c_data & 0xdf) |
397 (((val ? 0 : 1) & 0x01) << 5));
398 else
399 i2c_data = ((i2c_data & 0xdf) |
400 ((val & 0x01) << 5));
402 err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
403 out:
404 return (err < 0) ? err : 0;
407 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
409 int err;
410 u8 i2c_data;
411 struct sd *sd = (struct sd *) gspca_dev;
413 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
414 if (dmi_check_system(ov9650_flip_dmi_table))
415 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
416 else
417 *val = (i2c_data & 0x10) >> 4;
418 PDEBUG(D_V4L2, "Read vertical flip %d", *val);
420 return (err < 0) ? err : 0;
423 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
425 int err;
426 u8 i2c_data;
427 struct sd *sd = (struct sd *) gspca_dev;
429 PDEBUG(D_V4L2, "Set vertical flip to %d", val);
430 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
431 if (err < 0)
432 goto out;
434 if (dmi_check_system(ov9650_flip_dmi_table))
435 i2c_data = ((i2c_data & 0xef) |
436 (((val ? 0 : 1) & 0x01) << 4));
437 else
438 i2c_data = ((i2c_data & 0xef) |
439 ((val & 0x01) << 4));
441 err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
442 out:
443 return (err < 0) ? err : 0;
446 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
448 int err;
449 u8 i2c_data;
450 struct sd *sd = (struct sd *) gspca_dev;
452 err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
453 if (err < 0)
454 goto out;
455 *val = (i2c_data & 0x03) << 8;
457 err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
458 *val |= i2c_data;
459 PDEBUG(D_V4L2, "Read gain %d", *val);
460 out:
461 return (err < 0) ? err : 0;
464 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
466 int err;
467 u8 i2c_data;
468 struct sd *sd = (struct sd *) gspca_dev;
470 PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
472 /* Read the OV9650_VREF register first to avoid
473 corrupting the VREF high and low bits */
474 err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
475 if (err < 0)
476 goto out;
478 /* Mask away all uninteresting bits */
479 i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
480 err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
481 if (err < 0)
482 goto out;
484 /* The 8 LSBs */
485 i2c_data = val & 0xff;
486 err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
488 out:
489 return (err < 0) ? err : 0;
492 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
494 int err;
495 u8 i2c_data;
496 struct sd *sd = (struct sd *) gspca_dev;
498 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
499 *val = (i2c_data & OV9650_AWB_EN) >> 1;
500 PDEBUG(D_V4L2, "Read auto white balance %d", *val);
502 return (err < 0) ? err : 0;
505 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
507 int err;
508 u8 i2c_data;
509 struct sd *sd = (struct sd *) gspca_dev;
511 PDEBUG(D_V4L2, "Set auto white balance to %d", val);
512 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
513 if (err < 0)
514 goto out;
516 i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
517 err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
518 out:
519 return (err < 0) ? err : 0;
522 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
524 int err;
525 u8 i2c_data;
526 struct sd *sd = (struct sd *) gspca_dev;
528 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
529 *val = (i2c_data & OV9650_AGC_EN) >> 2;
530 PDEBUG(D_V4L2, "Read auto gain control %d", *val);
532 return (err < 0) ? err : 0;
535 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
537 int err;
538 u8 i2c_data;
539 struct sd *sd = (struct sd *) gspca_dev;
541 PDEBUG(D_V4L2, "Set auto gain control to %d", val);
542 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
543 if (err < 0)
544 goto out;
546 i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
547 err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
548 out:
549 return (err < 0) ? err : 0;
552 void ov9650_dump_registers(struct sd *sd)
554 int address;
555 info("Dumping the ov9650 register state");
556 for (address = 0; address < 0xa9; address++) {
557 u8 value;
558 ov9650_read_sensor(sd, address, &value, 1);
559 info("register 0x%x contains 0x%x",
560 address, value);
563 info("ov9650 register state dump complete");
565 info("Probing for which registers that are read/write");
566 for (address = 0; address < 0xff; address++) {
567 u8 old_value, ctrl_value;
568 u8 test_value[2] = {0xff, 0xff};
570 ov9650_read_sensor(sd, address, &old_value, 1);
571 ov9650_write_sensor(sd, address, test_value, 1);
572 ov9650_read_sensor(sd, address, &ctrl_value, 1);
574 if (ctrl_value == test_value[0])
575 info("register 0x%x is writeable", address);
576 else
577 info("register 0x%x is read only", address);
579 /* Restore original value */
580 ov9650_write_sensor(sd, address, &old_value, 1);