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 int ov9650_read_sensor(struct sd *sd, const u8 address,
22 u8 *i2c_data, const u8 len)
26 /* The ov9650 registers have a max depth of one byte */
31 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
32 } while ((*i2c_data & I2C_BUSY) && !err);
34 m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
36 m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
37 m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
38 m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
40 for (i = 0; i < len; i++) {
41 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
43 PDEBUG(D_CONF, "Reading sensor register "
44 "0x%x containing 0x%x ", address, *i2c_data);
46 return (err < 0) ? err : 0;
49 int ov9650_write_sensor(struct sd *sd, const u8 address,
50 u8 *i2c_data, const u8 len)
54 struct usb_device *udev = sd->gspca_dev.dev;
55 __u8 *buf = sd->gspca_dev.usb_buf;
57 /* The ov9650 only supports one byte writes */
61 memcpy(buf, sensor_urb_skeleton,
62 sizeof(sensor_urb_skeleton));
64 buf[11] = sd->sensor->i2c_slave_id;
67 /* Special case larger sensor writes */
70 /* Copy a four byte write sequence for each byte to be written to */
71 for (i = 0; i < len; i++) {
72 memcpy(p, sensor_urb_skeleton + 16, 4);
75 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
76 address, i2c_data[i]);
80 memcpy(p, sensor_urb_skeleton + 20, 4);
82 /* Set the total length */
85 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
88 20 + len * 4, M5602_URB_MSG_TIMEOUT);
90 return (err < 0) ? err : 0;
93 int ov9650_probe(struct sd *sd)
95 u8 prod_id = 0, ver_id = 0, i;
98 if (force_sensor == OV9650_SENSOR) {
99 info("Forcing an %s sensor", ov9650.name);
102 /* If we want to force another sensor,
103 don't try to probe this one */
107 info("Probing for an ov9650 sensor");
109 /* Run the pre-init to actually probe the unit */
110 for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
111 u8 data = preinit_ov9650[i][2];
112 if (preinit_ov9650[i][0] == SENSOR)
113 ov9650_write_sensor(sd,
114 preinit_ov9650[i][1], &data, 1);
116 m5602_write_bridge(sd, preinit_ov9650[i][1], data);
119 if (ov9650_read_sensor(sd, OV9650_PID, &prod_id, 1))
122 if (ov9650_read_sensor(sd, OV9650_VER, &ver_id, 1))
125 if ((prod_id == 0x96) && (ver_id == 0x52)) {
126 info("Detected an ov9650 sensor");
133 sd->gspca_dev.cam.cam_mode = ov9650.modes;
134 sd->gspca_dev.cam.nmodes = ov9650.nmodes;
135 sd->desc->ctrls = ov9650.ctrls;
136 sd->desc->nctrls = ov9650.nctrls;
140 int ov9650_init(struct sd *sd)
146 ov9650_dump_registers(sd);
148 for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
149 data = init_ov9650[i][2];
150 if (init_ov9650[i][0] == SENSOR)
151 err = ov9650_write_sensor(sd, init_ov9650[i][1],
154 err = m5602_write_bridge(sd, init_ov9650[i][1], data);
157 if (!err && dmi_check_system(ov9650_flip_dmi_table)) {
158 info("vflip quirk active");
160 err = ov9650_write_sensor(sd, OV9650_MVFP, &data, 1);
163 return (err < 0) ? err : 0;
166 int ov9650_power_down(struct sd *sd)
169 for (i = 0; i < ARRAY_SIZE(power_down_ov9650); i++) {
170 u8 data = power_down_ov9650[i][2];
171 if (power_down_ov9650[i][0] == SENSOR)
172 ov9650_write_sensor(sd,
173 power_down_ov9650[i][1], &data, 1);
175 m5602_write_bridge(sd, power_down_ov9650[i][1], data);
181 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
183 struct sd *sd = (struct sd *) gspca_dev;
187 err = ov9650_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
190 *val = i2c_data & 0x03;
192 err = ov9650_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
195 *val |= (i2c_data << 2);
197 err = ov9650_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
200 *val |= (i2c_data & 0x3f) << 10;
202 PDEBUG(D_V4L2, "Read exposure %d", *val);
204 return (err < 0) ? err : 0;
207 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
209 struct sd *sd = (struct sd *) gspca_dev;
213 PDEBUG(D_V4L2, "Set exposure to %d",
217 i2c_data = (val >> 10) & 0x3f;
218 err = ov9650_write_sensor(sd, OV9650_AECHM,
223 /* The 8 middle bits */
224 i2c_data = (val >> 2) & 0xff;
225 err = ov9650_write_sensor(sd, OV9650_AECH,
231 i2c_data = val & 0x03;
232 err = ov9650_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
235 return (err < 0) ? err : 0;
238 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
242 struct sd *sd = (struct sd *) gspca_dev;
244 ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
245 *val = (i2c_data & 0x03) << 8;
247 err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
249 PDEBUG(D_V4L2, "Read gain %d", *val);
250 return (err < 0) ? err : 0;
253 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
257 struct sd *sd = (struct sd *) gspca_dev;
260 /* Read the OV9650_VREF register first to avoid
261 corrupting the VREF high and low bits */
262 ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
263 /* Mask away all uninteresting bits */
264 i2c_data = ((val & 0x0300) >> 2) |
266 err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
269 i2c_data = val & 0xff;
270 err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
271 return (err < 0) ? err : 0;
274 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
278 struct sd *sd = (struct sd *) gspca_dev;
280 err = ov9650_read_sensor(sd, OV9650_RED, &i2c_data, 1);
283 PDEBUG(D_V4L2, "Read red gain %d", *val);
285 return (err < 0) ? err : 0;
288 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
292 struct sd *sd = (struct sd *) gspca_dev;
294 PDEBUG(D_V4L2, "Set red gain to %d",
297 i2c_data = val & 0xff;
298 err = ov9650_write_sensor(sd, OV9650_RED, &i2c_data, 1);
300 return (err < 0) ? err : 0;
303 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
307 struct sd *sd = (struct sd *) gspca_dev;
309 err = ov9650_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
312 PDEBUG(D_V4L2, "Read blue gain %d", *val);
314 return (err < 0) ? err : 0;
317 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
321 struct sd *sd = (struct sd *) gspca_dev;
323 PDEBUG(D_V4L2, "Set blue gain to %d",
326 i2c_data = val & 0xff;
327 err = ov9650_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
329 return (err < 0) ? err : 0;
332 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
336 struct sd *sd = (struct sd *) gspca_dev;
338 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
339 if (dmi_check_system(ov9650_flip_dmi_table))
340 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
342 *val = (i2c_data & OV9650_HFLIP) >> 5;
343 PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
345 return (err < 0) ? err : 0;
348 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
352 struct sd *sd = (struct sd *) gspca_dev;
354 PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
355 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
359 if (dmi_check_system(ov9650_flip_dmi_table))
360 i2c_data = ((i2c_data & 0xdf) |
361 (((val ? 0 : 1) & 0x01) << 5));
363 i2c_data = ((i2c_data & 0xdf) |
364 ((val & 0x01) << 5));
366 err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
368 return (err < 0) ? err : 0;
371 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
375 struct sd *sd = (struct sd *) gspca_dev;
377 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
378 if (dmi_check_system(ov9650_flip_dmi_table))
379 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
381 *val = (i2c_data & 0x10) >> 4;
382 PDEBUG(D_V4L2, "Read vertical flip %d", *val);
384 return (err < 0) ? err : 0;
387 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
391 struct sd *sd = (struct sd *) gspca_dev;
393 PDEBUG(D_V4L2, "Set vertical flip to %d", val);
394 err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
398 if (dmi_check_system(ov9650_flip_dmi_table))
399 i2c_data = ((i2c_data & 0xef) |
400 (((val ? 0 : 1) & 0x01) << 4));
402 i2c_data = ((i2c_data & 0xef) |
403 ((val & 0x01) << 4));
405 err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
407 return (err < 0) ? err : 0;
410 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
414 struct sd *sd = (struct sd *) gspca_dev;
416 err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
419 *val = (i2c_data & 0x03) << 8;
421 err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
423 PDEBUG(D_V4L2, "Read gain %d", *val);
425 return (err < 0) ? err : 0;
428 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
432 struct sd *sd = (struct sd *) gspca_dev;
434 PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
436 /* Read the OV9650_VREF register first to avoid
437 corrupting the VREF high and low bits */
438 err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
442 /* Mask away all uninteresting bits */
443 i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
444 err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
449 i2c_data = val & 0xff;
450 err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
453 return (err < 0) ? err : 0;
456 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
460 struct sd *sd = (struct sd *) gspca_dev;
462 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
463 *val = (i2c_data & OV9650_AWB_EN) >> 1;
464 PDEBUG(D_V4L2, "Read auto white balance %d", *val);
466 return (err < 0) ? err : 0;
469 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
473 struct sd *sd = (struct sd *) gspca_dev;
475 PDEBUG(D_V4L2, "Set auto white balance to %d", val);
476 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
480 i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
481 err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
483 return (err < 0) ? err : 0;
486 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
490 struct sd *sd = (struct sd *) gspca_dev;
492 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
493 *val = (i2c_data & OV9650_AGC_EN) >> 2;
494 PDEBUG(D_V4L2, "Read auto gain control %d", *val);
496 return (err < 0) ? err : 0;
499 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
503 struct sd *sd = (struct sd *) gspca_dev;
505 PDEBUG(D_V4L2, "Set auto gain control to %d", val);
506 err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
510 i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
511 err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
513 return (err < 0) ? err : 0;
516 void ov9650_dump_registers(struct sd *sd)
519 info("Dumping the ov9650 register state");
520 for (address = 0; address < 0xa9; address++) {
522 ov9650_read_sensor(sd, address, &value, 1);
523 info("register 0x%x contains 0x%x",
527 info("ov9650 register state dump complete");
529 info("Probing for which registers that are read/write");
530 for (address = 0; address < 0xff; address++) {
531 u8 old_value, ctrl_value;
532 u8 test_value[2] = {0xff, 0xff};
534 ov9650_read_sensor(sd, address, &old_value, 1);
535 ov9650_write_sensor(sd, address, test_value, 1);
536 ov9650_read_sensor(sd, address, &ctrl_value, 1);
538 if (ctrl_value == test_value[0])
539 info("register 0x%x is writeable", address);
541 info("register 0x%x is read only", address);
543 /* Restore original value */
544 ov9650_write_sensor(sd, address, &old_value, 1);