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 void s5k83a_dump_registers(struct sd *sd);
23 int s5k83a_probe(struct sd *sd)
25 u8 prod_id = 0, ver_id = 0;
29 if (force_sensor == S5K83A_SENSOR) {
30 info("Forcing a %s sensor", s5k83a.name);
33 /* If we want to force another sensor, don't try to probe this
38 info("Probing for a s5k83a sensor");
40 /* Preinit the sensor */
41 for (i = 0; i < ARRAY_SIZE(preinit_s5k83a) && !err; i++) {
42 u8 data[2] = {preinit_s5k83a[i][2], preinit_s5k83a[i][3]};
43 if (preinit_s5k83a[i][0] == SENSOR)
44 err = m5602_write_sensor(sd, preinit_s5k83a[i][1],
47 err = m5602_write_bridge(sd, preinit_s5k83a[i][1],
51 /* We don't know what register (if any) that contain the product id
52 * Just pick the first addresses that seem to produce the same results
53 * on multiple machines */
54 if (m5602_read_sensor(sd, 0x00, &prod_id, 1))
57 if (m5602_read_sensor(sd, 0x01, &ver_id, 1))
60 if ((prod_id == 0xff) || (ver_id == 0xff))
63 info("Detected a s5k83a sensor");
66 sd->gspca_dev.cam.cam_mode = s5k83a.modes;
67 sd->gspca_dev.cam.nmodes = s5k83a.nmodes;
68 sd->desc->ctrls = s5k83a.ctrls;
69 sd->desc->nctrls = s5k83a.nctrls;
73 int s5k83a_init(struct sd *sd)
77 for (i = 0; i < ARRAY_SIZE(init_s5k83a) && !err; i++) {
78 u8 data[2] = {0x00, 0x00};
80 switch (init_s5k83a[i][0]) {
82 err = m5602_write_bridge(sd,
88 data[0] = init_s5k83a[i][2];
89 err = m5602_write_sensor(sd,
90 init_s5k83a[i][1], data, 1);
94 data[0] = init_s5k83a[i][2];
95 data[1] = init_s5k83a[i][3];
96 err = m5602_write_sensor(sd,
97 init_s5k83a[i][1], data, 2);
100 info("Invalid stream command, exiting init");
106 s5k83a_dump_registers(sd);
108 return (err < 0) ? err : 0;
111 int s5k83a_power_down(struct sd *sd)
116 static void s5k83a_dump_registers(struct sd *sd)
120 m5602_read_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
122 for (page = 0; page < 16; page++) {
123 m5602_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
124 info("Dumping the s5k83a register state for page 0x%x", page);
125 for (address = 0; address <= 0xff; address++) {
127 m5602_read_sensor(sd, address, &val, 1);
128 info("register 0x%x contains 0x%x",
132 info("s5k83a register state dump complete");
134 for (page = 0; page < 16; page++) {
135 m5602_write_sensor(sd, S5K83A_PAGE_MAP, &page, 1);
136 info("Probing for which registers that are read/write "
137 "for page 0x%x", page);
138 for (address = 0; address <= 0xff; address++) {
139 u8 old_val, ctrl_val, test_val = 0xff;
141 m5602_read_sensor(sd, address, &old_val, 1);
142 m5602_write_sensor(sd, address, &test_val, 1);
143 m5602_read_sensor(sd, address, &ctrl_val, 1);
145 if (ctrl_val == test_val)
146 info("register 0x%x is writeable", address);
148 info("register 0x%x is read only", address);
150 /* Restore original val */
151 m5602_write_sensor(sd, address, &old_val, 1);
154 info("Read/write register probing complete");
155 m5602_write_sensor(sd, S5K83A_PAGE_MAP, &old_page, 1);
158 int s5k83a_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
162 struct sd *sd = (struct sd *) gspca_dev;
164 err = m5602_read_sensor(sd, S5K83A_BRIGHTNESS, data, 2);
168 data[1] = data[1] << 1;
175 int s5k83a_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
179 struct sd *sd = (struct sd *) gspca_dev;
183 err = m5602_write_sensor(sd, 0x14, data, 2);
189 err = m5602_write_sensor(sd, 0x0d, data, 2);
193 /* FIXME: This is not sane, we need to figure out the composition
194 of these registers */
195 data[0] = val >> 3; /* brightness, high 5 bits */
196 data[1] = val >> 1; /* brightness, high 7 bits */
197 err = m5602_write_sensor(sd, S5K83A_BRIGHTNESS, data, 2);
203 int s5k83a_get_whiteness(struct gspca_dev *gspca_dev, __s32 *val)
207 struct sd *sd = (struct sd *) gspca_dev;
209 err = m5602_read_sensor(sd, S5K83A_WHITENESS, &data, 1);
219 int s5k83a_set_whiteness(struct gspca_dev *gspca_dev, __s32 val)
223 struct sd *sd = (struct sd *) gspca_dev;
226 err = m5602_write_sensor(sd, S5K83A_WHITENESS, data, 1);
231 int s5k83a_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
235 struct sd *sd = (struct sd *) gspca_dev;
237 err = m5602_read_sensor(sd, S5K83A_GAIN, data, 2);
241 data[1] = data[1] & 0x3f;
242 if (data[1] > S5K83A_MAXIMUM_GAIN)
243 data[1] = S5K83A_MAXIMUM_GAIN;
251 int s5k83a_set_gain(struct gspca_dev *gspca_dev, __s32 val)
255 struct sd *sd = (struct sd *) gspca_dev;
259 err = m5602_write_sensor(sd, S5K83A_GAIN, data, 2);
263 int s5k83a_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
267 struct sd *sd = (struct sd *) gspca_dev;
270 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
274 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
275 *val = (data[0] | 0x40) ? 1 : 0;
281 int s5k83a_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
285 struct sd *sd = (struct sd *) gspca_dev;
288 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
292 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
296 /* set or zero six bit, seven is hflip */
297 data[0] = (val) ? (data[0] & 0x80) | 0x40 | S5K83A_FLIP_MASK
298 : (data[0] & 0x80) | S5K83A_FLIP_MASK;
299 err = m5602_write_sensor(sd, S5K83A_FLIP, data, 1);
303 data[0] = (val) ? 0x0b : 0x0a;
304 err = m5602_write_sensor(sd, S5K83A_VFLIP_TUNE, data, 1);
310 int s5k83a_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
314 struct sd *sd = (struct sd *) gspca_dev;
317 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
321 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
322 *val = (data[0] | 0x80) ? 1 : 0;
328 int s5k83a_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
332 struct sd *sd = (struct sd *) gspca_dev;
335 err = m5602_write_sensor(sd, S5K83A_PAGE_MAP, data, 1);
339 err = m5602_read_sensor(sd, S5K83A_FLIP, data, 1);
343 /* set or zero seven bit, six is vflip */
344 data[0] = (val) ? (data[0] & 0x40) | 0x80 | S5K83A_FLIP_MASK
345 : (data[0] & 0x40) | S5K83A_FLIP_MASK;
346 err = m5602_write_sensor(sd, S5K83A_FLIP, data, 1);
350 data[0] = (val) ? 0x0a : 0x0b;
351 err = m5602_write_sensor(sd, S5K83A_HFLIP_TUNE, data, 1);