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 */
25 struct dmi_system_id ov9650_flip_dmi_table[] = {
29 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
30 DMI_MATCH(DMI_PRODUCT_NAME, "A6VC")
36 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
37 DMI_MATCH(DMI_PRODUCT_NAME, "A6VM")
43 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
44 DMI_MATCH(DMI_PRODUCT_NAME, "A6JC")
50 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
51 DMI_MATCH(DMI_PRODUCT_NAME, "A6Kt")
57 static void ov9650_dump_registers(struct sd *sd);
59 int ov9650_read_sensor(struct sd *sd, const u8 address,
60 u8 *i2c_data, const u8 len)
64 /* The ov9650 registers have a max depth of one byte */
69 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
70 } while ((*i2c_data & I2C_BUSY) && !err);
72 err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
77 err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
81 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
85 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
87 for (i = 0; (i < len) && !err; i++) {
88 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
90 PDEBUG(D_CONF, "Reading sensor register "
91 "0x%x containing 0x%x ", address, *i2c_data);
95 return (err < 0) ? err : 0;
98 int ov9650_write_sensor(struct sd *sd, const u8 address,
99 u8 *i2c_data, const u8 len)
103 struct usb_device *udev = sd->gspca_dev.dev;
104 __u8 *buf = sd->gspca_dev.usb_buf;
106 /* The ov9650 only supports one byte writes */
110 memcpy(buf, sensor_urb_skeleton,
111 sizeof(sensor_urb_skeleton));
113 buf[11] = sd->sensor->i2c_slave_id;
116 /* Special case larger sensor writes */
119 /* Copy a four byte sequence for each byte to write over the I2C bus */
120 for (i = 0; i < len; i++) {
121 memcpy(p, sensor_urb_skeleton + 16, 4);
124 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
125 address, i2c_data[i]);
128 /* Copy the tailer */
129 memcpy(p, sensor_urb_skeleton + 20, 4);
131 /* Set the total length */
134 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
137 20 + len * 4, M5602_URB_MSG_TIMEOUT);
139 return (err < 0) ? err : 0;
142 int ov9650_probe(struct sd *sd)
144 u8 prod_id = 0, ver_id = 0, i;
147 if (force_sensor == OV9650_SENSOR) {
148 info("Forcing an %s sensor", ov9650.name);
151 /* If we want to force another sensor,
152 don't try to probe this one */
156 info("Probing for an ov9650 sensor");
158 /* Run the pre-init to actually probe the unit */
159 for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
160 u8 data = preinit_ov9650[i][2];
161 if (preinit_ov9650[i][0] == SENSOR)
162 ov9650_write_sensor(sd,
163 preinit_ov9650[i][1], &data, 1);
165 m5602_write_bridge(sd, preinit_ov9650[i][1], data);
168 if (ov9650_read_sensor(sd, OV9650_PID, &prod_id, 1))
171 if (ov9650_read_sensor(sd, OV9650_VER, &ver_id, 1))
174 if ((prod_id == 0x96) && (ver_id == 0x52)) {
175 info("Detected an ov9650 sensor");
182 sd->gspca_dev.cam.cam_mode = ov9650.modes;
183 sd->gspca_dev.cam.nmodes = ov9650.nmodes;
184 sd->desc->ctrls = ov9650.ctrls;
185 sd->desc->nctrls = ov9650.nctrls;
189 int ov9650_init(struct sd *sd)
195 ov9650_dump_registers(sd);
197 for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
198 data = init_ov9650[i][2];
199 if (init_ov9650[i][0] == SENSOR)
200 err = ov9650_write_sensor(sd, init_ov9650[i][1],
203 err = m5602_write_bridge(sd, init_ov9650[i][1], data);
206 if (dmi_check_system(ov9650_flip_dmi_table) && !err) {
207 info("vflip quirk active");
209 err = ov9650_write_sensor(sd, OV9650_MVFP, &data, 1);
215 int ov9650_power_down(struct sd *sd)
218 for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
219 u8 data = power_down_ov9650[i][2];
220 if (power_down_ov9650[i][0] == SENSOR)
221 err = ov9650_write_sensor(sd,
222 power_down_ov9650[i][1], &data, 1);
224 err = m5602_write_bridge(sd, power_down_ov9650[i][1],
231 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
233 struct sd *sd = (struct sd *) gspca_dev;
237 err = ov9650_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
240 *val = i2c_data & 0x03;
242 err = ov9650_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
245 *val |= (i2c_data << 2);
247 err = ov9650_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
250 *val |= (i2c_data & 0x3f) << 10;
252 PDEBUG(D_V4L2, "Read exposure %d", *val);
257 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
259 struct sd *sd = (struct sd *) gspca_dev;
263 PDEBUG(D_V4L2, "Set exposure to %d",
267 i2c_data = (val >> 10) & 0x3f;
268 err = ov9650_write_sensor(sd, OV9650_AECHM,
273 /* The 8 middle bits */
274 i2c_data = (val >> 2) & 0xff;
275 err = ov9650_write_sensor(sd, OV9650_AECH,
281 i2c_data = val & 0x03;
282 err = ov9650_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
288 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
292 struct sd *sd = (struct sd *) gspca_dev;
294 ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
295 *val = (i2c_data & 0x03) << 8;
297 err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
299 PDEBUG(D_V4L2, "Read gain %d", *val);
303 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
307 struct sd *sd = (struct sd *) gspca_dev;
310 /* Read the OV9650_VREF register first to avoid
311 corrupting the VREF high and low bits */
312 ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
313 /* Mask away all uninteresting bits */
314 i2c_data = ((val & 0x0300) >> 2) |
316 err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
319 i2c_data = val & 0xff;
320 err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
324 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
328 struct sd *sd = (struct sd *) gspca_dev;
330 err = ov9650_read_sensor(sd, OV9650_RED, &i2c_data, 1);
333 PDEBUG(D_V4L2, "Read red gain %d", *val);
338 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
342 struct sd *sd = (struct sd *) gspca_dev;
344 PDEBUG(D_V4L2, "Set red gain to %d",
347 i2c_data = val & 0xff;
348 err = ov9650_write_sensor(sd, OV9650_RED, &i2c_data, 1);
353 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
357 struct sd *sd = (struct sd *) gspca_dev;
359 err = ov9650_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
362 PDEBUG(D_V4L2, "Read blue gain %d", *val);
367 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
371 struct sd *sd = (struct sd *) gspca_dev;
373 PDEBUG(D_V4L2, "Set blue gain to %d",
376 i2c_data = val & 0xff;
377 err = ov9650_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
382 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
386 struct sd *sd = (struct sd *) gspca_dev;
388 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
389 if (dmi_check_system(ov9650_flip_dmi_table))
390 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
392 *val = (i2c_data & OV9650_HFLIP) >> 5;
393 PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
398 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
402 struct sd *sd = (struct sd *) gspca_dev;
404 PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
405 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
409 if (dmi_check_system(ov9650_flip_dmi_table))
410 i2c_data = ((i2c_data & 0xdf) |
411 (((val ? 0 : 1) & 0x01) << 5));
413 i2c_data = ((i2c_data & 0xdf) |
414 ((val & 0x01) << 5));
416 err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
421 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
425 struct sd *sd = (struct sd *) gspca_dev;
427 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
428 if (dmi_check_system(ov9650_flip_dmi_table))
429 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
431 *val = (i2c_data & 0x10) >> 4;
432 PDEBUG(D_V4L2, "Read vertical flip %d", *val);
437 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
441 struct sd *sd = (struct sd *) gspca_dev;
443 PDEBUG(D_V4L2, "Set vertical flip to %d", val);
444 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
448 if (dmi_check_system(ov9650_flip_dmi_table))
449 i2c_data = ((i2c_data & 0xef) |
450 (((val ? 0 : 1) & 0x01) << 4));
452 i2c_data = ((i2c_data & 0xef) |
453 ((val & 0x01) << 4));
455 err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
460 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
464 struct sd *sd = (struct sd *) gspca_dev;
466 err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
469 *val = (i2c_data & 0x03) << 8;
471 err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
473 PDEBUG(D_V4L2, "Read gain %d", *val);
478 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
482 struct sd *sd = (struct sd *) gspca_dev;
484 PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
486 /* Read the OV9650_VREF register first to avoid
487 corrupting the VREF high and low bits */
488 err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
492 /* Mask away all uninteresting bits */
493 i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
494 err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
499 i2c_data = val & 0xff;
500 err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
506 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
510 struct sd *sd = (struct sd *) gspca_dev;
512 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
513 *val = (i2c_data & OV9650_AWB_EN) >> 1;
514 PDEBUG(D_V4L2, "Read auto white balance %d", *val);
519 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
523 struct sd *sd = (struct sd *) gspca_dev;
525 PDEBUG(D_V4L2, "Set auto white balance to %d", val);
526 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
530 i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
531 err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
536 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
540 struct sd *sd = (struct sd *) gspca_dev;
542 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
543 *val = (i2c_data & OV9650_AGC_EN) >> 2;
544 PDEBUG(D_V4L2, "Read auto gain control %d", *val);
549 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
553 struct sd *sd = (struct sd *) gspca_dev;
555 PDEBUG(D_V4L2, "Set auto gain control to %d", val);
556 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
560 i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
561 err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
566 static void ov9650_dump_registers(struct sd *sd)
569 info("Dumping the ov9650 register state");
570 for (address = 0; address < 0xa9; address++) {
572 ov9650_read_sensor(sd, address, &value, 1);
573 info("register 0x%x contains 0x%x",
577 info("ov9650 register state dump complete");
579 info("Probing for which registers that are read/write");
580 for (address = 0; address < 0xff; address++) {
581 u8 old_value, ctrl_value;
582 u8 test_value[2] = {0xff, 0xff};
584 ov9650_read_sensor(sd, address, &old_value, 1);
585 ov9650_write_sensor(sd, address, test_value, 1);
586 ov9650_read_sensor(sd, address, &ctrl_value, 1);
588 if (ctrl_value == test_value[0])
589 info("register 0x%x is writeable", address);
591 info("register 0x%x is read only", address);
593 /* Restore original value */
594 ov9650_write_sensor(sd, address, &old_value, 1);