2 * Driver for the s5k83a 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_s5k83a.h"
21 int s5k83a_probe(struct sd *sd)
23 u8 prod_id = 0, ver_id = 0;
27 if (force_sensor == S5K83A_SENSOR) {
28 info("Forcing a %s sensor", s5k83a.name);
31 /* If we want to force another sensor, don't try to probe this
36 info("Probing for a s5k83a sensor");
38 /* Preinit the sensor */
39 for (i = 0; i < ARRAY_SIZE(preinit_s5k83a) && !err; i++) {
40 u8 data[2] = {preinit_s5k83a[i][2], preinit_s5k83a[i][3]};
41 if (preinit_s5k83a[i][0] == SENSOR)
42 err = s5k83a_write_sensor(sd, preinit_s5k83a[i][1],
45 err = m5602_write_bridge(sd, preinit_s5k83a[i][1],
49 /* We don't know what register (if any) that contain the product id
50 * Just pick the first addresses that seem to produce the same results
51 * on multiple machines */
52 if (s5k83a_read_sensor(sd, 0x00, &prod_id, 1))
55 if (s5k83a_read_sensor(sd, 0x01, &ver_id, 1))
58 if ((prod_id == 0xff) || (ver_id == 0xff))
61 info("Detected a s5k83a sensor");
64 sd->gspca_dev.cam.cam_mode = s5k83a.modes;
65 sd->gspca_dev.cam.nmodes = s5k83a.nmodes;
66 sd->desc->ctrls = s5k83a.ctrls;
67 sd->desc->nctrls = s5k83a.nctrls;
71 int s5k83a_read_sensor(struct sd *sd, const u8 address,
72 u8 *i2c_data, const u8 len)
77 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
78 } while ((*i2c_data & I2C_BUSY) && !err);
82 err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
83 sd->sensor->i2c_slave_id);
87 err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
91 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len);
96 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
97 } while ((*i2c_data & I2C_BUSY) && !err);
101 for (i = 0; i < len && !len; i++) {
102 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
104 PDEBUG(DBG_TRACE, "Reading sensor register "
105 "0x%x containing 0x%x ", address, *i2c_data);
109 return (err < 0) ? err : 0;
112 int s5k83a_write_sensor(struct sd *sd, const u8 address,
113 u8 *i2c_data, const u8 len)
117 struct usb_device *udev = sd->gspca_dev.dev;
118 __u8 *buf = sd->gspca_dev.usb_buf;
120 /* No sensor with a data width larger than 16 bits has yet been seen */
124 memcpy(buf, sensor_urb_skeleton,
125 sizeof(sensor_urb_skeleton));
127 buf[11] = sd->sensor->i2c_slave_id;
130 /* Special case larger sensor writes */
133 /* Copy a four byte write sequence for each byte to be written to */
134 for (i = 0; i < len; i++) {
135 memcpy(p, sensor_urb_skeleton + 16, 4);
138 PDEBUG(DBG_TRACE, "Writing sensor register 0x%x with 0x%x",
139 address, i2c_data[i]);
142 /* Copy the tailer */
143 memcpy(p, sensor_urb_skeleton + 20, 4);
145 /* Set the total length */
148 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
151 20 + len * 4, M5602_URB_MSG_TIMEOUT);
153 return (err < 0) ? err : 0;
156 int s5k83a_init(struct sd *sd)
160 for (i = 0; i < ARRAY_SIZE(init_s5k83a) && !err; i++) {
161 u8 data[2] = {0x00, 0x00};
163 switch (init_s5k83a[i][0]) {
165 err = m5602_write_bridge(sd,
171 data[0] = init_s5k83a[i][2];
172 err = s5k83a_write_sensor(sd,
173 init_s5k83a[i][1], data, 1);
177 data[0] = init_s5k83a[i][2];
178 data[1] = init_s5k83a[i][3];
179 err = s5k83a_write_sensor(sd,
180 init_s5k83a[i][1], data, 2);
183 info("Invalid stream command, exiting init");
189 s5k83a_dump_registers(sd);
191 return (err < 0) ? err : 0;
194 int s5k83a_power_down(struct sd *sd)
199 void s5k83a_dump_registers(struct sd *sd)
203 s5k83a_read_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
205 for (page = 0; page < 16; page++) {
206 s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
207 info("Dumping the s5k83a register state for page 0x%x", page);
208 for (address = 0; address <= 0xff; address++) {
210 s5k83a_read_sensor(sd, address, &val, 1);
211 info("register 0x%x contains 0x%x",
215 info("s5k83a register state dump complete");
217 for (page = 0; page < 16; page++) {
218 s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
219 info("Probing for which registers that are read/write "
220 "for page 0x%x", page);
221 for (address = 0; address <= 0xff; address++) {
222 u8 old_val, ctrl_val, test_val = 0xff;
224 s5k83a_read_sensor(sd, address, &old_val, 1);
225 s5k83a_write_sensor(sd, address, &test_val, 1);
226 s5k83a_read_sensor(sd, address, &ctrl_val, 1);
228 if (ctrl_val == test_val)
229 info("register 0x%x is writeable", address);
231 info("register 0x%x is read only", address);
233 /* Restore original val */
234 s5k83a_write_sensor(sd, address, &old_val, 1);
237 info("Read/write register probing complete");
238 s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
241 int s5k83a_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
245 struct sd *sd = (struct sd *) gspca_dev;
247 err = s5k83a_read_sensor(sd, S5K83A_BRIGHTNESS, data, 2);
248 data[1] = data[1] << 1;
251 return (err < 0) ? err : 0;
254 int s5k83a_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
258 struct sd *sd = (struct sd *) gspca_dev;
262 err = s5k83a_write_sensor(sd, 0x14, data, 2);
268 err = s5k83a_write_sensor(sd, 0x0d, data, 2);
272 /* FIXME: This is not sane, we need to figure out the composition
273 of these registers */
274 data[0] = val >> 3; /* brightness, high 5 bits */
275 data[1] = val >> 1; /* brightness, high 7 bits */
276 err = s5k83a_write_sensor(sd, S5K83A_BRIGHTNESS, data, 2);
278 return (err < 0) ? err : 0;
281 int s5k83a_get_whiteness(struct gspca_dev *gspca_dev, __s32 *val)
285 struct sd *sd = (struct sd *) gspca_dev;
287 err = s5k83a_read_sensor(sd, S5K83A_WHITENESS, &data, 1);
290 return (err < 0) ? err : 0;
293 int s5k83a_set_whiteness(struct gspca_dev *gspca_dev, __s32 val)
297 struct sd *sd = (struct sd *) gspca_dev;
300 err = s5k83a_write_sensor(sd, S5K83A_WHITENESS, data, 1);
302 return (err < 0) ? err : 0;
305 int s5k83a_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
309 struct sd *sd = (struct sd *) gspca_dev;
311 err = s5k83a_read_sensor(sd, S5K83A_GAIN, data, 2);
313 data[1] = data[1] & 0x3f;
314 if (data[1] > S5K83A_MAXIMUM_GAIN)
315 data[1] = S5K83A_MAXIMUM_GAIN;
319 return (err < 0) ? err : 0;
322 int s5k83a_set_gain(struct gspca_dev *gspca_dev, __s32 val)
326 struct sd *sd = (struct sd *) gspca_dev;
330 err = s5k83a_write_sensor(sd, S5K83A_GAIN, data, 2);
332 return (err < 0) ? err : 0;
335 int s5k83a_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
339 struct sd *sd = (struct sd *) gspca_dev;
342 err = s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
346 err = s5k83a_read_sensor(sd, S5K83A_FLIP, data, 1);
347 *val = (data[0] | 0x40) ? 1 : 0;
349 return (err < 0) ? err : 0;
352 int s5k83a_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
356 struct sd *sd = (struct sd *) gspca_dev;
359 err = s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
363 err = s5k83a_read_sensor(sd, S5K83A_FLIP, data, 1);
367 /* set or zero six bit, seven is hflip */
368 data[0] = (val) ? (data[0] & 0x80) | 0x40 | S5K83A_FLIP_MASK
369 : (data[0] & 0x80) | S5K83A_FLIP_MASK;
370 err = s5k83a_write_sensor(sd, S5K83A_FLIP, data, 1);
374 data[0] = (val) ? 0x0b : 0x0a;
375 err = s5k83a_write_sensor(sd, S5K83A_VFLIP_TUNE, data, 1);
377 return (err < 0) ? err : 0;
380 int s5k83a_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
384 struct sd *sd = (struct sd *) gspca_dev;
387 err = s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
391 err = s5k83a_read_sensor(sd, S5K83A_FLIP, data, 1);
392 *val = (data[0] | 0x80) ? 1 : 0;
394 return (err < 0) ? err : 0;
397 int s5k83a_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
401 struct sd *sd = (struct sd *) gspca_dev;
404 err = s5k83a_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
408 err = s5k83a_read_sensor(sd, S5K83A_FLIP, data, 1);
412 /* set or zero seven bit, six is vflip */
413 data[0] = (val) ? (data[0] & 0x40) | 0x80 | S5K83A_FLIP_MASK
414 : (data[0] & 0x40) | S5K83A_FLIP_MASK;
415 err = s5k83a_write_sensor(sd, S5K83A_FLIP, data, 1);
419 data[0] = (val) ? 0x0a : 0x0b;
420 err = s5k83a_write_sensor(sd, S5K83A_HFLIP_TUNE, data, 1);
422 return (err < 0) ? err : 0;