2 * Driver for the ov7660 sensor
4 * Copyright (C) 2009 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_ov7660.h"
21 static int ov7660_get_gain(struct gspca_dev *gspca_dev, __s32 *val);
22 static int ov7660_set_gain(struct gspca_dev *gspca_dev, __s32 val);
24 const static struct ctrl ov7660_ctrls[] = {
29 .type = V4L2_CTRL_TYPE_INTEGER,
34 .default_value = OV7660_DEFAULT_GAIN,
35 .flags = V4L2_CTRL_FLAG_SLIDER
37 .set = ov7660_set_gain,
38 .get = ov7660_get_gain
42 static struct v4l2_pix_format ov7660_modes[] = {
51 .colorspace = V4L2_COLORSPACE_SRGB,
56 static void ov7660_dump_registers(struct sd *sd);
58 int ov7660_probe(struct sd *sd)
61 u8 prod_id = 0, ver_id = 0;
66 if (force_sensor == OV7660_SENSOR) {
67 info("Forcing an %s sensor", ov7660.name);
70 /* If we want to force another sensor,
71 don't try to probe this one */
76 for (i = 0; i < ARRAY_SIZE(preinit_ov7660) && !err; i++) {
79 if (preinit_ov7660[i][0] == BRIDGE) {
80 err = m5602_write_bridge(sd,
82 preinit_ov7660[i][2]);
84 data[0] = preinit_ov7660[i][2];
85 err = m5602_write_sensor(sd,
86 preinit_ov7660[i][1], data, 1);
92 if (m5602_read_sensor(sd, OV7660_PID, &prod_id, 1))
95 if (m5602_read_sensor(sd, OV7660_VER, &ver_id, 1))
98 info("Sensor reported 0x%x%x", prod_id, ver_id);
100 if ((prod_id == 0x76) && (ver_id == 0x60)) {
101 info("Detected a ov7660 sensor");
107 sensor_settings = kmalloc(
108 ARRAY_SIZE(ov7660_ctrls) * sizeof(s32), GFP_KERNEL);
109 if (!sensor_settings)
112 sd->gspca_dev.cam.cam_mode = ov7660_modes;
113 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(ov7660_modes);
114 sd->desc->ctrls = ov7660_ctrls;
115 sd->desc->nctrls = ARRAY_SIZE(ov7660_ctrls);
117 for (i = 0; i < ARRAY_SIZE(ov7660_ctrls); i++)
118 sensor_settings[i] = ov7660_ctrls[i].qctrl.default_value;
119 sd->sensor_priv = sensor_settings;
124 int ov7660_init(struct sd *sd)
127 s32 *sensor_settings = sd->sensor_priv;
129 /* Init the sensor */
130 for (i = 0; i < ARRAY_SIZE(init_ov7660); i++) {
133 if (init_ov7660[i][0] == BRIDGE) {
134 err = m5602_write_bridge(sd,
138 data[0] = init_ov7660[i][2];
139 err = m5602_write_sensor(sd,
140 init_ov7660[i][1], data, 1);
145 ov7660_dump_registers(sd);
147 err = ov7660_set_gain(&sd->gspca_dev, sensor_settings[GAIN_IDX]);
154 int ov7660_start(struct sd *sd)
159 int ov7660_stop(struct sd *sd)
164 void ov7660_disconnect(struct sd *sd)
169 kfree(sd->sensor_priv);
172 static int ov7660_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
174 struct sd *sd = (struct sd *) gspca_dev;
175 s32 *sensor_settings = sd->sensor_priv;
177 *val = sensor_settings[GAIN_IDX];
178 PDEBUG(D_V4L2, "Read gain %d", *val);
182 static int ov7660_set_gain(struct gspca_dev *gspca_dev, __s32 val)
186 struct sd *sd = (struct sd *) gspca_dev;
187 s32 *sensor_settings = sd->sensor_priv;
189 PDEBUG(D_V4L2, "Setting gain to %d", val);
191 sensor_settings[GAIN_IDX] = val;
193 err = m5602_write_sensor(sd, OV7660_GAIN, &i2c_data, 1);
197 static void ov7660_dump_registers(struct sd *sd)
200 info("Dumping the ov7660 register state");
201 for (address = 0; address < 0xa9; address++) {
203 m5602_read_sensor(sd, address, &value, 1);
204 info("register 0x%x contains 0x%x",
208 info("ov7660 register state dump complete");
210 info("Probing for which registers that are read/write");
211 for (address = 0; address < 0xff; address++) {
212 u8 old_value, ctrl_value;
213 u8 test_value[2] = {0xff, 0xff};
215 m5602_read_sensor(sd, address, &old_value, 1);
216 m5602_write_sensor(sd, address, test_value, 1);
217 m5602_read_sensor(sd, address, &ctrl_value, 1);
219 if (ctrl_value == test_value[0])
220 info("register 0x%x is writeable", address);
222 info("register 0x%x is read only", address);
224 /* Restore original value */
225 m5602_write_sensor(sd, address, &old_value, 1);