2 * Driver for the s5k4aa sensor
4 * Copyright (C) 2008 Erik Andren
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_s5k4aa.h"
21 int s5k4aa_probe(struct sd *sd)
23 u8 prod_id[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
24 const u8 expected_prod_id[6] = {0x00, 0x10, 0x00, 0x4b, 0x33, 0x75};
28 if (force_sensor == S5K4AA_SENSOR) {
29 info("Forcing a %s sensor", s5k4aa.name);
32 /* If we want to force another sensor, don't try to probe this
37 info("Probing for a s5k4aa sensor");
39 /* Preinit the sensor */
40 for (i = 0; i < ARRAY_SIZE(preinit_s5k4aa) && !err; i++) {
41 u8 data[2] = {0x00, 0x00};
43 switch (preinit_s5k4aa[i][0]) {
45 err = m5602_write_bridge(sd,
47 preinit_s5k4aa[i][2]);
51 data[0] = preinit_s5k4aa[i][2];
52 err = s5k4aa_write_sensor(sd,
58 data[0] = preinit_s5k4aa[i][2];
59 data[1] = preinit_s5k4aa[i][3];
60 err = s5k4aa_write_sensor(sd,
65 info("Invalid stream command, exiting init");
70 /* Test some registers, but we don't know their exact meaning yet */
71 if (s5k4aa_read_sensor(sd, 0x00, prod_id, sizeof(prod_id)))
74 if (memcmp(prod_id, expected_prod_id, sizeof(prod_id)))
77 info("Detected a s5k4aa sensor");
79 sd->gspca_dev.cam.cam_mode = s5k4aa.modes;
80 sd->gspca_dev.cam.nmodes = s5k4aa.nmodes;
81 sd->desc->ctrls = s5k4aa.ctrls;
82 sd->desc->nctrls = s5k4aa.nctrls;
87 int s5k4aa_read_sensor(struct sd *sd, const u8 address,
88 u8 *i2c_data, const u8 len)
93 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
94 } while ((*i2c_data & I2C_BUSY) && !err);
98 err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
99 sd->sensor->i2c_slave_id);
103 err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
107 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len);
112 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
113 } while ((*i2c_data & I2C_BUSY) && !err);
117 for (i = 0; (i < len) & !err; i++) {
118 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
120 PDEBUG(DBG_TRACE, "Reading sensor register "
121 "0x%x containing 0x%x ", address, *i2c_data);
124 return (err < 0) ? err : 0;
127 int s5k4aa_write_sensor(struct sd *sd, const u8 address,
128 u8 *i2c_data, const u8 len)
132 struct usb_device *udev = sd->gspca_dev.dev;
133 __u8 *buf = sd->gspca_dev.usb_buf;
135 /* No sensor with a data width larger than 16 bits has yet been seen */
139 memcpy(buf, sensor_urb_skeleton,
140 sizeof(sensor_urb_skeleton));
142 buf[11] = sd->sensor->i2c_slave_id;
145 /* Special case larger sensor writes */
148 /* Copy a four byte write sequence for each byte to be written to */
149 for (i = 0; i < len; i++) {
150 memcpy(p, sensor_urb_skeleton + 16, 4);
153 PDEBUG(DBG_TRACE, "Writing sensor register 0x%x with 0x%x",
154 address, i2c_data[i]);
157 /* Copy the tailer */
158 memcpy(p, sensor_urb_skeleton + 20, 4);
160 /* Set the total length */
163 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
166 20 + len * 4, M5602_URB_MSG_TIMEOUT);
168 return (err < 0) ? err : 0;
171 int s5k4aa_init(struct sd *sd)
175 for (i = 0; i < ARRAY_SIZE(init_s5k4aa) && !err; i++) {
176 u8 data[2] = {0x00, 0x00};
178 switch (init_s5k4aa[i][0]) {
180 err = m5602_write_bridge(sd,
186 data[0] = init_s5k4aa[i][2];
187 err = s5k4aa_write_sensor(sd,
188 init_s5k4aa[i][1], data, 1);
192 data[0] = init_s5k4aa[i][2];
193 data[1] = init_s5k4aa[i][3];
194 err = s5k4aa_write_sensor(sd,
195 init_s5k4aa[i][1], data, 2);
198 info("Invalid stream command, exiting init");
204 s5k4aa_dump_registers(sd);
206 if (!err && dmi_check_system(s5k4aa_vflip_dmi_table)) {
208 info("vertical flip quirk active");
209 s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
210 s5k4aa_read_sensor(sd, S5K4AA_READ_MODE, &data, 1);
211 data |= S5K4AA_RM_V_FLIP;
212 data &= ~S5K4AA_RM_H_FLIP;
213 s5k4aa_write_sensor(sd, S5K4AA_READ_MODE, &data, 1);
215 /* Decrement COLSTART to preserve color order (BGGR) */
216 s5k4aa_read_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
218 s5k4aa_write_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
220 /* Increment ROWSTART to preserve color order (BGGR) */
221 s5k4aa_read_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
223 s5k4aa_write_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
226 return (err < 0) ? err : 0;
229 int s5k4aa_power_down(struct sd *sd)
234 int s5k4aa_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
236 struct sd *sd = (struct sd *) gspca_dev;
237 u8 data = S5K4AA_PAGE_MAP_2;
240 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
244 err = s5k4aa_read_sensor(sd, S5K4AA_EXPOSURE_HI, &data, 1);
249 err = s5k4aa_read_sensor(sd, S5K4AA_EXPOSURE_LO, &data, 1);
251 PDEBUG(DBG_V4L2_CID, "Read exposure %d", *val);
253 return (err < 0) ? err : 0;
256 int s5k4aa_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
258 struct sd *sd = (struct sd *) gspca_dev;
259 u8 data = S5K4AA_PAGE_MAP_2;
262 PDEBUG(DBG_V4L2_CID, "Set exposure to %d", val);
263 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
266 data = (val >> 8) & 0xff;
267 err = s5k4aa_write_sensor(sd, S5K4AA_EXPOSURE_HI, &data, 1);
271 err = s5k4aa_write_sensor(sd, S5K4AA_EXPOSURE_LO, &data, 1);
273 return (err < 0) ? err : 0;
276 int s5k4aa_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
278 struct sd *sd = (struct sd *) gspca_dev;
279 u8 data = S5K4AA_PAGE_MAP_2;
282 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
286 err = s5k4aa_read_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
287 *val = (data & S5K4AA_RM_V_FLIP) >> 7;
288 PDEBUG(DBG_V4L2_CID, "Read vertical flip %d", *val);
291 return (err < 0) ? err : 0;
294 int s5k4aa_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
296 struct sd *sd = (struct sd *) gspca_dev;
297 u8 data = S5K4AA_PAGE_MAP_2;
300 PDEBUG(DBG_V4L2_CID, "Set vertical flip to %d", val);
301 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
304 err = s5k4aa_write_sensor(sd, S5K4AA_READ_MODE, &data, 1);
307 data = ((data & ~S5K4AA_RM_V_FLIP)
308 | ((val & 0x01) << 7));
309 err = s5k4aa_write_sensor(sd, S5K4AA_READ_MODE, &data, 1);
314 err = s5k4aa_read_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
319 err = s5k4aa_write_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
321 err = s5k4aa_read_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
326 err = s5k4aa_write_sensor(sd, S5K4AA_ROWSTART_LO, &data, 1);
329 return (err < 0) ? err : 0;
332 int s5k4aa_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
334 struct sd *sd = (struct sd *) gspca_dev;
335 u8 data = S5K4AA_PAGE_MAP_2;
338 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
342 err = s5k4aa_read_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
343 *val = (data & S5K4AA_RM_H_FLIP) >> 6;
344 PDEBUG(DBG_V4L2_CID, "Read horizontal flip %d", *val);
346 return (err < 0) ? err : 0;
349 int s5k4aa_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
351 struct sd *sd = (struct sd *) gspca_dev;
352 u8 data = S5K4AA_PAGE_MAP_2;
355 PDEBUG(DBG_V4L2_CID, "Set horizontal flip to %d",
357 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
360 err = s5k4aa_write_sensor(sd, S5K4AA_READ_MODE, &data, 1);
364 data = ((data & ~S5K4AA_RM_H_FLIP) | ((val & 0x01) << 6));
365 err = s5k4aa_write_sensor(sd, S5K4AA_READ_MODE, &data, 1);
370 err = s5k4aa_read_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
374 err = s5k4aa_write_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
378 err = s5k4aa_read_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
382 err = s5k4aa_write_sensor(sd, S5K4AA_COLSTART_LO, &data, 1);
385 return (err < 0) ? err : 0;
388 int s5k4aa_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
390 struct sd *sd = (struct sd *) gspca_dev;
391 u8 data = S5K4AA_PAGE_MAP_2;
394 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
398 err = s5k4aa_read_sensor(sd, S5K4AA_GAIN_2, &data, 1);
400 PDEBUG(DBG_V4L2_CID, "Read gain %d", *val);
403 return (err < 0) ? err : 0;
406 int s5k4aa_set_gain(struct gspca_dev *gspca_dev, __s32 val)
408 struct sd *sd = (struct sd *) gspca_dev;
409 u8 data = S5K4AA_PAGE_MAP_2;
412 PDEBUG(DBG_V4L2_CID, "Set gain to %d", val);
413 err = s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &data, 1);
418 err = s5k4aa_write_sensor(sd, S5K4AA_GAIN_2, &data, 1);
421 return (err < 0) ? err : 0;
424 void s5k4aa_dump_registers(struct sd *sd)
428 s5k4aa_read_sensor(sd, S5K4AA_PAGE_MAP, &old_page, 1);
429 for (page = 0; page < 16; page++) {
430 s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &page, 1);
431 info("Dumping the s5k4aa register state for page 0x%x", page);
432 for (address = 0; address <= 0xff; address++) {
434 s5k4aa_read_sensor(sd, address, &value, 1);
435 info("register 0x%x contains 0x%x",
439 info("s5k4aa register state dump complete");
441 for (page = 0; page < 16; page++) {
442 s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &page, 1);
443 info("Probing for which registers that are "
444 "read/write for page 0x%x", page);
445 for (address = 0; address <= 0xff; address++) {
446 u8 old_value, ctrl_value, test_value = 0xff;
448 s5k4aa_read_sensor(sd, address, &old_value, 1);
449 s5k4aa_write_sensor(sd, address, &test_value, 1);
450 s5k4aa_read_sensor(sd, address, &ctrl_value, 1);
452 if (ctrl_value == test_value)
453 info("register 0x%x is writeable", address);
455 info("register 0x%x is read only", address);
457 /* Restore original value */
458 s5k4aa_write_sensor(sd, address, &old_value, 1);
461 info("Read/write register probing complete");
462 s5k4aa_write_sensor(sd, S5K4AA_PAGE_MAP, &old_page, 1);