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, "A6J")
57 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
58 DMI_MATCH(DMI_PRODUCT_NAME, "A6Kt")
62 .ident = "Alienware Aurora m9700",
64 DMI_MATCH(DMI_SYS_VENDOR, "alienware"),
65 DMI_MATCH(DMI_PRODUCT_NAME, "Aurora m9700")
71 static void ov9650_dump_registers(struct sd *sd);
73 int ov9650_probe(struct sd *sd)
75 u8 prod_id = 0, ver_id = 0, i;
78 if (force_sensor == OV9650_SENSOR) {
79 info("Forcing an %s sensor", ov9650.name);
82 /* If we want to force another sensor,
83 don't try to probe this one */
87 info("Probing for an ov9650 sensor");
89 /* Run the pre-init to actually probe the unit */
90 for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
91 u8 data = preinit_ov9650[i][2];
92 if (preinit_ov9650[i][0] == SENSOR)
93 m5602_write_sensor(sd,
94 preinit_ov9650[i][1], &data, 1);
96 m5602_write_bridge(sd, preinit_ov9650[i][1], data);
99 if (m5602_read_sensor(sd, OV9650_PID, &prod_id, 1))
102 if (m5602_read_sensor(sd, OV9650_VER, &ver_id, 1))
105 if ((prod_id == 0x96) && (ver_id == 0x52)) {
106 info("Detected an ov9650 sensor");
113 sd->gspca_dev.cam.cam_mode = ov9650.modes;
114 sd->gspca_dev.cam.nmodes = ov9650.nmodes;
115 sd->desc->ctrls = ov9650.ctrls;
116 sd->desc->nctrls = ov9650.nctrls;
120 int ov9650_init(struct sd *sd)
126 ov9650_dump_registers(sd);
128 for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
129 data = init_ov9650[i][2];
130 if (init_ov9650[i][0] == SENSOR)
131 err = m5602_write_sensor(sd, init_ov9650[i][1],
134 err = m5602_write_bridge(sd, init_ov9650[i][1], data);
137 if (dmi_check_system(ov9650_flip_dmi_table) && !err) {
138 info("vflip quirk active");
140 err = m5602_write_sensor(sd, OV9650_MVFP, &data, 1);
145 int ov9650_start(struct sd *sd)
148 struct cam *cam = &sd->gspca_dev.cam;
150 for (i = 0; i < ARRAY_SIZE(res_init_ov9650) && !err; i++)
151 err = m5602_write_bridge(sd, res_init_ov9650[i][0], res_init_ov9650[i][1]);
155 switch (cam->cam_mode[sd->gspca_dev.curr_mode].width)
158 PDEBUG(D_V4L2, "Configuring camera for VGA mode");
160 for (i = 0; i < ARRAY_SIZE(VGA_ov9650) && !err; i++) {
161 if (VGA_ov9650[i][0] == SENSOR) {
162 u8 data = VGA_ov9650[i][2];
164 err = m5602_write_sensor(sd,
165 VGA_ov9650[i][1], &data, 1);
167 err = m5602_write_bridge(sd, VGA_ov9650[i][1], VGA_ov9650[i][2]);
173 PDEBUG(D_V4L2, "Configuring camera for CIF mode");
175 for (i = 0; i < ARRAY_SIZE(CIF_ov9650) && !err; i++) {
176 if (CIF_ov9650[i][0] == SENSOR) {
177 u8 data = CIF_ov9650[i][2];
179 err = m5602_write_sensor(sd,
180 CIF_ov9650[i][1], &data, 1);
182 err = m5602_write_bridge(sd, CIF_ov9650[i][1], CIF_ov9650[i][2]);
188 PDEBUG(D_V4L2, "Configuring camera for QVGA mode");
190 for (i = 0; i < ARRAY_SIZE(QVGA_ov9650) && !err; i++) {
191 if (QVGA_ov9650[i][0] == SENSOR) {
192 u8 data = QVGA_ov9650[i][2];
194 err = m5602_write_sensor(sd,
195 QVGA_ov9650[i][1], &data, 1);
197 err = m5602_write_bridge(sd, QVGA_ov9650[i][1], QVGA_ov9650[i][2]);
203 PDEBUG(D_V4L2, "Configuring camera for QCIF mode");
205 for (i = 0; i < ARRAY_SIZE(QCIF_ov9650) && !err; i++) {
206 if (QCIF_ov9650[i][0] == SENSOR) {
207 u8 data = QCIF_ov9650[i][2];
208 err = m5602_write_sensor(sd,
209 QCIF_ov9650[i][1], &data, 1);
211 err = m5602_write_bridge(sd, QCIF_ov9650[i][1], QCIF_ov9650[i][2]);
220 int ov9650_power_down(struct sd *sd)
223 for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
224 u8 data = power_down_ov9650[i][2];
225 if (power_down_ov9650[i][0] == SENSOR)
226 err = m5602_write_sensor(sd,
227 power_down_ov9650[i][1], &data, 1);
229 err = m5602_write_bridge(sd, power_down_ov9650[i][1],
236 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
238 struct sd *sd = (struct sd *) gspca_dev;
242 err = m5602_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
245 *val = i2c_data & 0x03;
247 err = m5602_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
250 *val |= (i2c_data << 2);
252 err = m5602_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
255 *val |= (i2c_data & 0x3f) << 10;
257 PDEBUG(D_V4L2, "Read exposure %d", *val);
262 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
264 struct sd *sd = (struct sd *) gspca_dev;
268 PDEBUG(D_V4L2, "Set exposure to %d",
272 i2c_data = (val >> 10) & 0x3f;
273 err = m5602_write_sensor(sd, OV9650_AECHM,
278 /* The 8 middle bits */
279 i2c_data = (val >> 2) & 0xff;
280 err = m5602_write_sensor(sd, OV9650_AECH,
286 i2c_data = val & 0x03;
287 err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
292 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
296 struct sd *sd = (struct sd *) gspca_dev;
298 m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
299 *val = (i2c_data & 0x03) << 8;
301 err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
303 PDEBUG(D_V4L2, "Read gain %d", *val);
307 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
311 struct sd *sd = (struct sd *) gspca_dev;
314 /* Read the OV9650_VREF register first to avoid
315 corrupting the VREF high and low bits */
316 m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
317 /* Mask away all uninteresting bits */
318 i2c_data = ((val & 0x0300) >> 2) |
320 err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
323 i2c_data = val & 0xff;
324 err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
328 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
332 struct sd *sd = (struct sd *) gspca_dev;
334 err = m5602_read_sensor(sd, OV9650_RED, &i2c_data, 1);
337 PDEBUG(D_V4L2, "Read red gain %d", *val);
342 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
346 struct sd *sd = (struct sd *) gspca_dev;
348 PDEBUG(D_V4L2, "Set red gain to %d",
351 i2c_data = val & 0xff;
352 err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
357 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
361 struct sd *sd = (struct sd *) gspca_dev;
363 err = m5602_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
366 PDEBUG(D_V4L2, "Read blue gain %d", *val);
371 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
375 struct sd *sd = (struct sd *) gspca_dev;
377 PDEBUG(D_V4L2, "Set blue gain to %d",
380 i2c_data = val & 0xff;
381 err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
386 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
390 struct sd *sd = (struct sd *) gspca_dev;
392 err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
393 if (dmi_check_system(ov9650_flip_dmi_table))
394 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
396 *val = (i2c_data & OV9650_HFLIP) >> 5;
397 PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
402 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
406 struct sd *sd = (struct sd *) gspca_dev;
408 PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
409 err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
413 if (dmi_check_system(ov9650_flip_dmi_table))
414 i2c_data = ((i2c_data & 0xdf) |
415 (((val ? 0 : 1) & 0x01) << 5));
417 i2c_data = ((i2c_data & 0xdf) |
418 ((val & 0x01) << 5));
420 err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
425 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
429 struct sd *sd = (struct sd *) gspca_dev;
431 err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
432 if (dmi_check_system(ov9650_flip_dmi_table))
433 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
435 *val = (i2c_data & 0x10) >> 4;
436 PDEBUG(D_V4L2, "Read vertical flip %d", *val);
441 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
445 struct sd *sd = (struct sd *) gspca_dev;
447 PDEBUG(D_V4L2, "Set vertical flip to %d", val);
448 err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
452 if (dmi_check_system(ov9650_flip_dmi_table))
453 i2c_data = ((i2c_data & 0xef) |
454 (((val ? 0 : 1) & 0x01) << 4));
456 i2c_data = ((i2c_data & 0xef) |
457 ((val & 0x01) << 4));
459 err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
464 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
468 struct sd *sd = (struct sd *) gspca_dev;
470 err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
473 *val = (i2c_data & 0x03) << 8;
475 err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
477 PDEBUG(D_V4L2, "Read gain %d", *val);
482 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
486 struct sd *sd = (struct sd *) gspca_dev;
488 PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
490 /* Read the OV9650_VREF register first to avoid
491 corrupting the VREF high and low bits */
492 err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
496 /* Mask away all uninteresting bits */
497 i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
498 err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
503 i2c_data = val & 0xff;
504 err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
509 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
513 struct sd *sd = (struct sd *) gspca_dev;
515 err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
516 *val = (i2c_data & OV9650_AWB_EN) >> 1;
517 PDEBUG(D_V4L2, "Read auto white balance %d", *val);
522 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
526 struct sd *sd = (struct sd *) gspca_dev;
528 PDEBUG(D_V4L2, "Set auto white balance to %d", val);
529 err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
533 i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
534 err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
539 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
543 struct sd *sd = (struct sd *) gspca_dev;
545 err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
546 *val = (i2c_data & OV9650_AGC_EN) >> 2;
547 PDEBUG(D_V4L2, "Read auto gain control %d", *val);
552 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
556 struct sd *sd = (struct sd *) gspca_dev;
558 PDEBUG(D_V4L2, "Set auto gain control to %d", val);
559 err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
563 i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
564 err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
569 static void ov9650_dump_registers(struct sd *sd)
572 info("Dumping the ov9650 register state");
573 for (address = 0; address < 0xa9; address++) {
575 m5602_read_sensor(sd, address, &value, 1);
576 info("register 0x%x contains 0x%x",
580 info("ov9650 register state dump complete");
582 info("Probing for which registers that are read/write");
583 for (address = 0; address < 0xff; address++) {
584 u8 old_value, ctrl_value;
585 u8 test_value[2] = {0xff, 0xff};
587 m5602_read_sensor(sd, address, &old_value, 1);
588 m5602_write_sensor(sd, address, test_value, 1);
589 m5602_read_sensor(sd, address, &ctrl_value, 1);
591 if (ctrl_value == test_value[0])
592 info("register 0x%x is writeable", address);
594 info("register 0x%x is read only", address);
596 /* Restore original value */
597 m5602_write_sensor(sd, address, &old_value, 1);