2 * Driver for the s5k83a 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_s5k83a.h"
21 static struct v4l2_pix_format s5k83a_modes[] = {
30 .colorspace = V4L2_COLORSPACE_SRGB,
35 const static struct ctrl s5k83a_ctrls[] = {
38 .id = V4L2_CID_BRIGHTNESS,
39 .type = V4L2_CTRL_TYPE_INTEGER,
44 .default_value = S5K83A_DEFAULT_BRIGHTNESS,
45 .flags = V4L2_CTRL_FLAG_SLIDER
47 .set = s5k83a_set_brightness,
48 .get = s5k83a_get_brightness
52 .id = V4L2_CID_WHITENESS,
53 .type = V4L2_CTRL_TYPE_INTEGER,
58 .default_value = S5K83A_DEFAULT_WHITENESS,
59 .flags = V4L2_CTRL_FLAG_SLIDER
61 .set = s5k83a_set_whiteness,
62 .get = s5k83a_get_whiteness,
66 .type = V4L2_CTRL_TYPE_INTEGER,
69 .maximum = S5K83A_MAXIMUM_GAIN,
71 .default_value = S5K83A_DEFAULT_GAIN,
72 .flags = V4L2_CTRL_FLAG_SLIDER
74 .set = s5k83a_set_gain,
75 .get = s5k83a_get_gain
79 .type = V4L2_CTRL_TYPE_BOOLEAN,
80 .name = "horizontal flip",
86 .set = s5k83a_set_hflip,
87 .get = s5k83a_get_hflip
91 .type = V4L2_CTRL_TYPE_BOOLEAN,
92 .name = "vertical flip",
98 .set = s5k83a_set_vflip,
99 .get = s5k83a_get_vflip
103 static void s5k83a_dump_registers(struct sd *sd);
105 int s5k83a_probe(struct sd *sd)
107 u8 prod_id = 0, ver_id = 0;
111 if (force_sensor == S5K83A_SENSOR) {
112 info("Forcing a %s sensor", s5k83a.name);
115 /* If we want to force another sensor, don't try to probe this
120 info("Probing for a s5k83a sensor");
122 /* Preinit the sensor */
123 for (i = 0; i < ARRAY_SIZE(preinit_s5k83a) && !err; i++) {
124 u8 data[2] = {preinit_s5k83a[i][2], preinit_s5k83a[i][3]};
125 if (preinit_s5k83a[i][0] == SENSOR)
126 err = m5602_write_sensor(sd, preinit_s5k83a[i][1],
129 err = m5602_write_bridge(sd, preinit_s5k83a[i][1],
133 /* We don't know what register (if any) that contain the product id
134 * Just pick the first addresses that seem to produce the same results
135 * on multiple machines */
136 if (m5602_read_sensor(sd, 0x00, &prod_id, 1))
139 if (m5602_read_sensor(sd, 0x01, &ver_id, 1))
142 if ((prod_id == 0xff) || (ver_id == 0xff))
145 info("Detected a s5k83a sensor");
148 sd->gspca_dev.cam.cam_mode = s5k83a_modes;
149 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(s5k83a_modes);
150 sd->desc->ctrls = s5k83a_ctrls;
151 sd->desc->nctrls = ARRAY_SIZE(s5k83a_ctrls);
155 int s5k83a_init(struct sd *sd)
159 for (i = 0; i < ARRAY_SIZE(init_s5k83a) && !err; i++) {
160 u8 data[2] = {0x00, 0x00};
162 switch (init_s5k83a[i][0]) {
164 err = m5602_write_bridge(sd,
170 data[0] = init_s5k83a[i][2];
171 err = m5602_write_sensor(sd,
172 init_s5k83a[i][1], data, 1);
176 data[0] = init_s5k83a[i][2];
177 data[1] = init_s5k83a[i][3];
178 err = m5602_write_sensor(sd,
179 init_s5k83a[i][1], data, 2);
182 info("Invalid stream command, exiting init");
188 s5k83a_dump_registers(sd);
190 return (err < 0) ? err : 0;
193 int s5k83a_start(struct sd *sd)
195 return s5k83a_set_led_indication(sd, 1);
198 int s5k83a_stop(struct sd *sd)
200 return s5k83a_set_led_indication(sd, 0);
203 int s5k83a_power_down(struct sd *sd)
208 static void s5k83a_dump_registers(struct sd *sd)
212 m5602_read_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
214 for (page = 0; page < 16; page++) {
215 m5602_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
216 info("Dumping the s5k83a register state for page 0x%x", page);
217 for (address = 0; address <= 0xff; address++) {
219 m5602_read_sensor(sd, address, &val, 1);
220 info("register 0x%x contains 0x%x",
224 info("s5k83a register state dump complete");
226 for (page = 0; page < 16; page++) {
227 m5602_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
228 info("Probing for which registers that are read/write "
229 "for page 0x%x", page);
230 for (address = 0; address <= 0xff; address++) {
231 u8 old_val, ctrl_val, test_val = 0xff;
233 m5602_read_sensor(sd, address, &old_val, 1);
234 m5602_write_sensor(sd, address, &test_val, 1);
235 m5602_read_sensor(sd, address, &ctrl_val, 1);
237 if (ctrl_val == test_val)
238 info("register 0x%x is writeable", address);
240 info("register 0x%x is read only", address);
242 /* Restore original val */
243 m5602_write_sensor(sd, address, &old_val, 1);
246 info("Read/write register probing complete");
247 m5602_write_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
250 int s5k83a_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
254 struct sd *sd = (struct sd *) gspca_dev;
256 err = m5602_read_sensor(sd, S5K83A_BRIGHTNESS, data, 2);
260 data[1] = data[1] << 1;
266 int s5k83a_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
270 struct sd *sd = (struct sd *) gspca_dev;
274 err = m5602_write_sensor(sd, 0x14, data, 2);
280 err = m5602_write_sensor(sd, 0x0d, data, 2);
284 /* FIXME: This is not sane, we need to figure out the composition
285 of these registers */
286 data[0] = val >> 3; /* brightness, high 5 bits */
287 data[1] = val >> 1; /* brightness, high 7 bits */
288 err = m5602_write_sensor(sd, S5K83A_BRIGHTNESS, data, 2);
293 int s5k83a_get_whiteness(struct gspca_dev *gspca_dev, __s32 *val)
297 struct sd *sd = (struct sd *) gspca_dev;
299 err = m5602_read_sensor(sd, S5K83A_WHITENESS, &data, 1);
308 int s5k83a_set_whiteness(struct gspca_dev *gspca_dev, __s32 val)
312 struct sd *sd = (struct sd *) gspca_dev;
315 err = m5602_write_sensor(sd, S5K83A_WHITENESS, data, 1);
320 int s5k83a_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
324 struct sd *sd = (struct sd *) gspca_dev;
326 err = m5602_read_sensor(sd, S5K83A_GAIN, data, 2);
330 data[1] = data[1] & 0x3f;
331 if (data[1] > S5K83A_MAXIMUM_GAIN)
332 data[1] = S5K83A_MAXIMUM_GAIN;
339 int s5k83a_set_gain(struct gspca_dev *gspca_dev, __s32 val)
343 struct sd *sd = (struct sd *) gspca_dev;
347 err = m5602_write_sensor(sd, S5K83A_GAIN, data, 2);
351 int s5k83a_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
355 struct sd *sd = (struct sd *) gspca_dev;
358 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
362 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
363 *val = (data[0] | 0x40) ? 1 : 0;
368 int s5k83a_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
372 struct sd *sd = (struct sd *) gspca_dev;
375 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
379 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
383 /* set or zero six bit, seven is hflip */
384 data[0] = (val) ? (data[0] & 0x80) | 0x40 | S5K83A_FLIP_MASK
385 : (data[0] & 0x80) | S5K83A_FLIP_MASK;
386 err = m5602_write_sensor(sd, S5K83A_FLIP, data, 1);
390 data[0] = (val) ? 0x0b : 0x0a;
391 err = m5602_write_sensor(sd, S5K83A_VFLIP_TUNE, data, 1);
396 int s5k83a_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
400 struct sd *sd = (struct sd *) gspca_dev;
403 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
407 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
408 *val = (data[0] | 0x80) ? 1 : 0;
413 int s5k83a_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
417 struct sd *sd = (struct sd *) gspca_dev;
420 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
424 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
428 /* set or zero seven bit, six is vflip */
429 data[0] = (val) ? (data[0] & 0x40) | 0x80 | S5K83A_FLIP_MASK
430 : (data[0] & 0x40) | S5K83A_FLIP_MASK;
431 err = m5602_write_sensor(sd, S5K83A_FLIP, data, 1);
435 data[0] = (val) ? 0x0a : 0x0b;
436 err = m5602_write_sensor(sd, S5K83A_HFLIP_TUNE, data, 1);
441 int s5k83a_set_led_indication(struct sd *sd, u8 val)
446 err = m5602_read_bridge(sd, M5602_XB_GPIO_DAT, data);
451 data[0] = data[0] | S5K83A_GPIO_LED_MASK;
453 data[0] = data[0] & ~S5K83A_GPIO_LED_MASK;
455 err = m5602_write_bridge(sd, M5602_XB_GPIO_DAT, data[0]);
457 return (err < 0) ? err : 0;