blob: ee8e496090a2dbdc7d45225ffb7f82b3f9d06d0f [file] [log] [blame]
Erik Andrenc109f812008-10-01 04:51:53 -03001/*
2 * Driver for the po1030 sensor
3 *
Erik Andren0c505e682008-10-16 16:43:16 -03004 * Copyright (c) 2008 Erik Andrén
Erik Andrenc109f812008-10-01 04:51:53 -03005 * Copyright (c) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
6 * Copyright (c) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
7 *
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
12 *
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.
16 *
17 */
18
19#include "m5602_po1030.h"
20
Erik Andr?ncf811d52009-04-03 02:49:10 -030021static int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val);
22static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val);
23static int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val);
24static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val);
25static int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val);
26static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val);
27static int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val);
28static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val);
Erik Andr?nb933d582009-01-13 13:55:52 -030029static int po1030_get_green_balance(struct gspca_dev *gspca_dev, __s32 *val);
30static int po1030_set_green_balance(struct gspca_dev *gspca_dev, __s32 val);
Erik Andr?ncf811d52009-04-03 02:49:10 -030031static int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val);
32static int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val);
33static int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val);
34static int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val);
35static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
36 __s32 val);
37static int po1030_get_auto_white_balance(struct gspca_dev *gspca_dev,
38 __s32 *val);
Erik Andr?ndd9ce842009-01-13 13:43:46 -030039static int po1030_set_auto_exposure(struct gspca_dev *gspca_dev,
40 __s32 val);
41static int po1030_get_auto_exposure(struct gspca_dev *gspca_dev,
42 __s32 *val);
Erik Andr?ncf811d52009-04-03 02:49:10 -030043
Erik Andr?n74cadfe2008-12-30 16:48:42 -030044static struct v4l2_pix_format po1030_modes[] = {
45 {
46 640,
47 480,
48 V4L2_PIX_FMT_SBGGR8,
49 V4L2_FIELD_NONE,
50 .sizeimage = 640 * 480,
51 .bytesperline = 640,
52 .colorspace = V4L2_COLORSPACE_SRGB,
53 .priv = 0
54 }
55};
56
Erik Andr?ne17cc082008-12-30 17:06:55 -030057const static struct ctrl po1030_ctrls[] = {
Erik Andr?n05d7d9c2009-01-06 06:39:11 -030058#define GAIN_IDX 0
Erik Andr?ne17cc082008-12-30 17:06:55 -030059 {
60 {
61 .id = V4L2_CID_GAIN,
62 .type = V4L2_CTRL_TYPE_INTEGER,
63 .name = "gain",
64 .minimum = 0x00,
65 .maximum = 0x4f,
66 .step = 0x1,
67 .default_value = PO1030_GLOBAL_GAIN_DEFAULT,
68 .flags = V4L2_CTRL_FLAG_SLIDER
69 },
70 .set = po1030_set_gain,
71 .get = po1030_get_gain
Erik Andr?n05d7d9c2009-01-06 06:39:11 -030072 },
73#define EXPOSURE_IDX 1
74 {
Erik Andr?ne17cc082008-12-30 17:06:55 -030075 {
76 .id = V4L2_CID_EXPOSURE,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "exposure",
79 .minimum = 0x00,
80 .maximum = 0x02ff,
81 .step = 0x1,
82 .default_value = PO1030_EXPOSURE_DEFAULT,
83 .flags = V4L2_CTRL_FLAG_SLIDER
84 },
85 .set = po1030_set_exposure,
86 .get = po1030_get_exposure
Erik Andr?n05d7d9c2009-01-06 06:39:11 -030087 },
88#define RED_BALANCE_IDX 2
89 {
Erik Andr?ne17cc082008-12-30 17:06:55 -030090 {
91 .id = V4L2_CID_RED_BALANCE,
92 .type = V4L2_CTRL_TYPE_INTEGER,
93 .name = "red balance",
94 .minimum = 0x00,
95 .maximum = 0xff,
96 .step = 0x1,
97 .default_value = PO1030_RED_GAIN_DEFAULT,
98 .flags = V4L2_CTRL_FLAG_SLIDER
99 },
100 .set = po1030_set_red_balance,
101 .get = po1030_get_red_balance
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300102 },
103#define BLUE_BALANCE_IDX 3
104 {
Erik Andr?ne17cc082008-12-30 17:06:55 -0300105 {
106 .id = V4L2_CID_BLUE_BALANCE,
107 .type = V4L2_CTRL_TYPE_INTEGER,
108 .name = "blue balance",
109 .minimum = 0x00,
110 .maximum = 0xff,
111 .step = 0x1,
112 .default_value = PO1030_BLUE_GAIN_DEFAULT,
113 .flags = V4L2_CTRL_FLAG_SLIDER
114 },
115 .set = po1030_set_blue_balance,
116 .get = po1030_get_blue_balance
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300117 },
118#define HFLIP_IDX 4
119 {
Erik Andr?ne17cc082008-12-30 17:06:55 -0300120 {
121 .id = V4L2_CID_HFLIP,
122 .type = V4L2_CTRL_TYPE_BOOLEAN,
123 .name = "horizontal flip",
124 .minimum = 0,
125 .maximum = 1,
126 .step = 1,
127 .default_value = 0,
128 },
129 .set = po1030_set_hflip,
130 .get = po1030_get_hflip
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300131 },
132#define VFLIP_IDX 5
133 {
Erik Andr?ne17cc082008-12-30 17:06:55 -0300134 {
135 .id = V4L2_CID_VFLIP,
136 .type = V4L2_CTRL_TYPE_BOOLEAN,
137 .name = "vertical flip",
138 .minimum = 0,
139 .maximum = 1,
140 .step = 1,
141 .default_value = 0,
142 },
143 .set = po1030_set_vflip,
144 .get = po1030_get_vflip
Erik Andr?n3b9ae652009-01-08 03:53:50 -0300145 },
146#define AUTO_WHITE_BALANCE_IDX 6
147 {
148 {
149 .id = V4L2_CID_AUTO_WHITE_BALANCE,
150 .type = V4L2_CTRL_TYPE_BOOLEAN,
151 .name = "auto white balance",
152 .minimum = 0,
153 .maximum = 1,
154 .step = 1,
155 .default_value = 0,
156 },
157 .set = po1030_set_auto_white_balance,
158 .get = po1030_get_auto_white_balance
Erik Andr?ndd9ce842009-01-13 13:43:46 -0300159 },
160#define AUTO_EXPOSURE_IDX 7
161 {
162 {
163 .id = V4L2_CID_EXPOSURE_AUTO,
164 .type = V4L2_CTRL_TYPE_BOOLEAN,
165 .name = "auto exposure",
166 .minimum = 0,
167 .maximum = 1,
168 .step = 1,
169 .default_value = 0,
170 },
171 .set = po1030_set_auto_exposure,
172 .get = po1030_get_auto_exposure
173 },
Erik Andr?nb933d582009-01-13 13:55:52 -0300174#define GREEN_BALANCE_IDX 8
175 {
176 {
177 .id = M5602_V4L2_CID_GREEN_BALANCE,
178 .type = V4L2_CTRL_TYPE_INTEGER,
179 .name = "green balance",
180 .minimum = 0x00,
181 .maximum = 0xff,
182 .step = 0x1,
183 .default_value = PO1030_GREEN_GAIN_DEFAULT,
184 .flags = V4L2_CTRL_FLAG_SLIDER
185 },
186 .set = po1030_set_green_balance,
187 .get = po1030_get_green_balance
188 },
Erik Andr?ne17cc082008-12-30 17:06:55 -0300189};
190
Erik Andrén658efb62008-11-24 14:21:29 -0300191static void po1030_dump_registers(struct sd *sd);
192
Erik Andrenc109f812008-10-01 04:51:53 -0300193int po1030_probe(struct sd *sd)
194{
Erik Andr?n3e5cbad2009-01-11 12:52:10 -0300195 u8 dev_id_h = 0, i;
Erik Andr?ncb29e692009-01-07 18:05:08 -0300196 s32 *sensor_settings;
Erik Andrenc109f812008-10-01 04:51:53 -0300197
198 if (force_sensor) {
199 if (force_sensor == PO1030_SENSOR) {
200 info("Forcing a %s sensor", po1030.name);
201 goto sensor_found;
202 }
203 /* If we want to force another sensor, don't try to probe this
204 * one */
205 return -ENODEV;
206 }
207
208 info("Probing for a po1030 sensor");
209
210 /* Run the pre-init to actually probe the unit */
211 for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
212 u8 data = preinit_po1030[i][2];
213 if (preinit_po1030[i][0] == SENSOR)
Erik Andrén6dc4cff2008-11-26 04:08:10 -0300214 m5602_write_sensor(sd,
Erik Andrenc109f812008-10-01 04:51:53 -0300215 preinit_po1030[i][1], &data, 1);
216 else
217 m5602_write_bridge(sd, preinit_po1030[i][1], data);
218 }
219
Erik Andr?nfcb98102009-01-08 04:04:19 -0300220 if (m5602_read_sensor(sd, PO1030_DEVID_H, &dev_id_h, 1))
Erik Andrenc109f812008-10-01 04:51:53 -0300221 return -ENODEV;
222
Erik Andr?n3e5cbad2009-01-11 12:52:10 -0300223 if (dev_id_h == 0x30) {
Erik Andrenc109f812008-10-01 04:51:53 -0300224 info("Detected a po1030 sensor");
225 goto sensor_found;
226 }
227 return -ENODEV;
228
229sensor_found:
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300230 sensor_settings = kmalloc(
231 ARRAY_SIZE(po1030_ctrls) * sizeof(s32), GFP_KERNEL);
232 if (!sensor_settings)
233 return -ENOMEM;
234
Erik Andr?n74cadfe2008-12-30 16:48:42 -0300235 sd->gspca_dev.cam.cam_mode = po1030_modes;
236 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
Erik Andr?ne17cc082008-12-30 17:06:55 -0300237 sd->desc->ctrls = po1030_ctrls;
Erik Andr?ne4cc4fc2008-12-30 15:27:17 -0300238 sd->desc->nctrls = ARRAY_SIZE(po1030_ctrls);
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300239
240 for (i = 0; i < ARRAY_SIZE(po1030_ctrls); i++)
241 sensor_settings[i] = po1030_ctrls[i].qctrl.default_value;
242 sd->sensor_priv = sensor_settings;
Erik Andr?ncb29e692009-01-07 18:05:08 -0300243
Erik Andrenc109f812008-10-01 04:51:53 -0300244 return 0;
245}
246
Erik Andrenc109f812008-10-01 04:51:53 -0300247int po1030_init(struct sd *sd)
248{
Erik Andr?ncb29e692009-01-07 18:05:08 -0300249 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300250 int i, err = 0;
251
252 /* Init the sensor */
Erik Andrén7b9f2462008-11-20 03:59:02 -0300253 for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
Erik Andrenc109f812008-10-01 04:51:53 -0300254 u8 data[2] = {0x00, 0x00};
255
256 switch (init_po1030[i][0]) {
257 case BRIDGE:
258 err = m5602_write_bridge(sd,
259 init_po1030[i][1],
260 init_po1030[i][2]);
261 break;
262
263 case SENSOR:
264 data[0] = init_po1030[i][2];
Erik Andrén6dc4cff2008-11-26 04:08:10 -0300265 err = m5602_write_sensor(sd,
Erik Andrenc109f812008-10-01 04:51:53 -0300266 init_po1030[i][1], data, 1);
267 break;
268
Erik Andrenc109f812008-10-01 04:51:53 -0300269 default:
270 info("Invalid stream command, exiting init");
271 return -EINVAL;
272 }
273 }
Erik Andr?ncb29e692009-01-07 18:05:08 -0300274 if (err < 0)
275 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300276
Erik Andr?n36e756c2009-01-09 13:35:00 -0300277 if (dump_sensor)
278 po1030_dump_registers(sd);
279
Erik Andr?ncb29e692009-01-07 18:05:08 -0300280 err = po1030_set_exposure(&sd->gspca_dev,
281 sensor_settings[EXPOSURE_IDX]);
282 if (err < 0)
283 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300284
Erik Andr?ncb29e692009-01-07 18:05:08 -0300285 err = po1030_set_gain(&sd->gspca_dev, sensor_settings[GAIN_IDX]);
286 if (err < 0)
287 return err;
288
289 err = po1030_set_hflip(&sd->gspca_dev, sensor_settings[HFLIP_IDX]);
290 if (err < 0)
291 return err;
292
293 err = po1030_set_vflip(&sd->gspca_dev, sensor_settings[VFLIP_IDX]);
294 if (err < 0)
295 return err;
296
297 err = po1030_set_red_balance(&sd->gspca_dev,
298 sensor_settings[RED_BALANCE_IDX]);
299 if (err < 0)
300 return err;
301
Erik Andr?n926d5032009-01-11 15:23:46 -0300302 err = po1030_set_blue_balance(&sd->gspca_dev,
Erik Andr?ncb29e692009-01-07 18:05:08 -0300303 sensor_settings[BLUE_BALANCE_IDX]);
Erik Andr?n3b9ae652009-01-08 03:53:50 -0300304 if (err < 0)
305 return err;
306
Erik Andr?nb933d582009-01-13 13:55:52 -0300307 err = po1030_set_green_balance(&sd->gspca_dev,
308 sensor_settings[GREEN_BALANCE_IDX]);
309 if (err < 0)
310 return err;
311
Erik Andr?n3b9ae652009-01-08 03:53:50 -0300312 err = po1030_set_auto_white_balance(&sd->gspca_dev,
Erik Andr?ndd9ce842009-01-13 13:43:46 -0300313 sensor_settings[AUTO_WHITE_BALANCE_IDX]);
314 if (err < 0)
315 return err;
316
317 err = po1030_set_auto_exposure(&sd->gspca_dev,
318 sensor_settings[AUTO_EXPOSURE_IDX]);
Erik Andrén7b9f2462008-11-20 03:59:02 -0300319 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300320}
321
Erik Andr?nc996b362009-01-08 14:15:06 -0300322int po1030_start(struct sd *sd)
323{
324 int i, err = 0;
325 /* Synthesize the vsync/hsync setup */
326 for (i = 0; i < ARRAY_SIZE(start_po1030) && !err; i++) {
327 if (start_po1030[i][0] == BRIDGE)
328 err = m5602_write_bridge(sd, start_po1030[i][1],
329 start_po1030[i][2]);
330 else if (start_po1030[i][0] == SENSOR) {
331 u8 data = start_po1030[i][2];
332 err = m5602_write_sensor(sd,
333 start_po1030[i][1], &data, 1);
334 }
335 }
336 return err;
337}
338
Erik Andr?ncf811d52009-04-03 02:49:10 -0300339static int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
Erik Andrenc109f812008-10-01 04:51:53 -0300340{
341 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300342 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300343
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300344 *val = sensor_settings[EXPOSURE_IDX];
Erik Andren17ea88a2008-10-16 16:46:07 -0300345 PDEBUG(D_V4L2, "Exposure read as %d", *val);
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300346 return 0;
Erik Andrenc109f812008-10-01 04:51:53 -0300347}
348
Erik Andr?ncf811d52009-04-03 02:49:10 -0300349static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
Erik Andrenc109f812008-10-01 04:51:53 -0300350{
351 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300352 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300353 u8 i2c_data;
354 int err;
355
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300356 sensor_settings[EXPOSURE_IDX] = val;
Erik Andren17ea88a2008-10-16 16:46:07 -0300357 PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
Erik Andrenc109f812008-10-01 04:51:53 -0300358
359 i2c_data = ((val & 0xff00) >> 8);
Erik Andren17ea88a2008-10-16 16:46:07 -0300360 PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
Erik Andrenc109f812008-10-01 04:51:53 -0300361 i2c_data);
362
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300363 err = m5602_write_sensor(sd, PO1030_INTEGLINES_H,
Erik Andrenc109f812008-10-01 04:51:53 -0300364 &i2c_data, 1);
365 if (err < 0)
Erik Andr?n051781b2008-12-27 12:28:00 -0300366 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300367
368 i2c_data = (val & 0xff);
Erik Andren17ea88a2008-10-16 16:46:07 -0300369 PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
Erik Andrenc109f812008-10-01 04:51:53 -0300370 i2c_data);
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300371 err = m5602_write_sensor(sd, PO1030_INTEGLINES_M,
Erik Andrenc109f812008-10-01 04:51:53 -0300372 &i2c_data, 1);
373
Erik Andrén7b9f2462008-11-20 03:59:02 -0300374 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300375}
376
Erik Andr?ncf811d52009-04-03 02:49:10 -0300377static int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
Erik Andrenc109f812008-10-01 04:51:53 -0300378{
379 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300380 s32 *sensor_settings = sd->sensor_priv;
381
382 *val = sensor_settings[GAIN_IDX];
383 PDEBUG(D_V4L2, "Read global gain %d", *val);
384 return 0;
385}
386
Erik Andr?ncf811d52009-04-03 02:49:10 -0300387static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300388{
389 struct sd *sd = (struct sd *) gspca_dev;
390 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300391 u8 i2c_data;
392 int err;
393
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300394 sensor_settings[GAIN_IDX] = val;
Erik Andrenc109f812008-10-01 04:51:53 -0300395
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300396 i2c_data = val & 0xff;
397 PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300398 err = m5602_write_sensor(sd, PO1030_GLOBALGAIN,
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300399 &i2c_data, 1);
Erik Andrén7b9f2462008-11-20 03:59:02 -0300400 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300401}
402
Erik Andr?ncf811d52009-04-03 02:49:10 -0300403static int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
Erik Andrene2c97492008-10-16 16:49:17 -0300404{
405 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300406 s32 *sensor_settings = sd->sensor_priv;
Erik Andrene2c97492008-10-16 16:49:17 -0300407
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300408 *val = sensor_settings[HFLIP_IDX];
Erik Andrene2c97492008-10-16 16:49:17 -0300409 PDEBUG(D_V4L2, "Read hflip %d", *val);
410
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300411 return 0;
Erik Andrene2c97492008-10-16 16:49:17 -0300412}
413
Erik Andr?ncf811d52009-04-03 02:49:10 -0300414static int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
Erik Andrene2c97492008-10-16 16:49:17 -0300415{
416 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300417 s32 *sensor_settings = sd->sensor_priv;
Erik Andrene2c97492008-10-16 16:49:17 -0300418 u8 i2c_data;
419 int err;
420
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300421 sensor_settings[HFLIP_IDX] = val;
422
Erik Andrene2c97492008-10-16 16:49:17 -0300423 PDEBUG(D_V4L2, "Set hflip %d", val);
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300424 err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
Erik Andrén1d07b6f2008-11-27 14:07:24 -0300425 if (err < 0)
Erik Andr?n051781b2008-12-27 12:28:00 -0300426 return err;
Erik Andrene2c97492008-10-16 16:49:17 -0300427
Erik Andrén1d07b6f2008-11-27 14:07:24 -0300428 i2c_data = (0x7f & i2c_data) | ((val & 0x01) << 7);
Erik Andrene2c97492008-10-16 16:49:17 -0300429
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300430 err = m5602_write_sensor(sd, PO1030_CONTROL2,
Erik Andrén1d07b6f2008-11-27 14:07:24 -0300431 &i2c_data, 1);
Erik Andrene2c97492008-10-16 16:49:17 -0300432
Erik Andrén7b9f2462008-11-20 03:59:02 -0300433 return err;
Erik Andrene2c97492008-10-16 16:49:17 -0300434}
435
Erik Andr?ncf811d52009-04-03 02:49:10 -0300436static int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
Erik Andrene2c97492008-10-16 16:49:17 -0300437{
438 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300439 s32 *sensor_settings = sd->sensor_priv;
Erik Andrene2c97492008-10-16 16:49:17 -0300440
Erik Andr?n27429082009-01-07 17:41:47 -0300441 *val = sensor_settings[VFLIP_IDX];
Erik Andrene2c97492008-10-16 16:49:17 -0300442 PDEBUG(D_V4L2, "Read vflip %d", *val);
443
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300444 return 0;
Erik Andrene2c97492008-10-16 16:49:17 -0300445}
446
Erik Andr?ncf811d52009-04-03 02:49:10 -0300447static int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
Erik Andrene2c97492008-10-16 16:49:17 -0300448{
449 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300450 s32 *sensor_settings = sd->sensor_priv;
Erik Andrene2c97492008-10-16 16:49:17 -0300451 u8 i2c_data;
452 int err;
453
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300454 sensor_settings[VFLIP_IDX] = val;
455
Erik Andrene2c97492008-10-16 16:49:17 -0300456 PDEBUG(D_V4L2, "Set vflip %d", val);
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300457 err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
Erik Andrén1d07b6f2008-11-27 14:07:24 -0300458 if (err < 0)
Erik Andr?n051781b2008-12-27 12:28:00 -0300459 return err;
Erik Andrene2c97492008-10-16 16:49:17 -0300460
Erik Andrén1d07b6f2008-11-27 14:07:24 -0300461 i2c_data = (i2c_data & 0xbf) | ((val & 0x01) << 6);
Erik Andrene2c97492008-10-16 16:49:17 -0300462
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300463 err = m5602_write_sensor(sd, PO1030_CONTROL2,
Erik Andrén1d07b6f2008-11-27 14:07:24 -0300464 &i2c_data, 1);
Erik Andrene2c97492008-10-16 16:49:17 -0300465
Erik Andrén7b9f2462008-11-20 03:59:02 -0300466 return err;
Erik Andrene2c97492008-10-16 16:49:17 -0300467}
468
Erik Andr?ncf811d52009-04-03 02:49:10 -0300469static int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
Erik Andrenc109f812008-10-01 04:51:53 -0300470{
471 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300472 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300473
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300474 *val = sensor_settings[RED_BALANCE_IDX];
Erik Andren17ea88a2008-10-16 16:46:07 -0300475 PDEBUG(D_V4L2, "Read red gain %d", *val);
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300476 return 0;
Erik Andrenc109f812008-10-01 04:51:53 -0300477}
478
Erik Andr?ncf811d52009-04-03 02:49:10 -0300479static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
Erik Andrenc109f812008-10-01 04:51:53 -0300480{
481 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300482 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300483 u8 i2c_data;
484 int err;
485
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300486 sensor_settings[RED_BALANCE_IDX] = val;
487
Erik Andrenc109f812008-10-01 04:51:53 -0300488 i2c_data = val & 0xff;
Erik Andren17ea88a2008-10-16 16:46:07 -0300489 PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300490 err = m5602_write_sensor(sd, PO1030_RED_GAIN,
Erik Andrenc109f812008-10-01 04:51:53 -0300491 &i2c_data, 1);
Erik Andrén7b9f2462008-11-20 03:59:02 -0300492 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300493}
494
Erik Andr?ncf811d52009-04-03 02:49:10 -0300495static int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
Erik Andrenc109f812008-10-01 04:51:53 -0300496{
497 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300498 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300499
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300500 *val = sensor_settings[BLUE_BALANCE_IDX];
Erik Andren17ea88a2008-10-16 16:46:07 -0300501 PDEBUG(D_V4L2, "Read blue gain %d", *val);
Erik Andrenc109f812008-10-01 04:51:53 -0300502
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300503 return 0;
Erik Andrenc109f812008-10-01 04:51:53 -0300504}
505
Erik Andr?ncf811d52009-04-03 02:49:10 -0300506static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
Erik Andrenc109f812008-10-01 04:51:53 -0300507{
508 struct sd *sd = (struct sd *) gspca_dev;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300509 s32 *sensor_settings = sd->sensor_priv;
Erik Andrenc109f812008-10-01 04:51:53 -0300510 u8 i2c_data;
511 int err;
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300512
513 sensor_settings[BLUE_BALANCE_IDX] = val;
514
Erik Andrenc109f812008-10-01 04:51:53 -0300515 i2c_data = val & 0xff;
Erik Andren17ea88a2008-10-16 16:46:07 -0300516 PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
Erik Andr?nbb9460e2009-01-07 13:54:13 -0300517 err = m5602_write_sensor(sd, PO1030_BLUE_GAIN,
Erik Andrenc109f812008-10-01 04:51:53 -0300518 &i2c_data, 1);
519
Erik Andrén7b9f2462008-11-20 03:59:02 -0300520 return err;
Erik Andrenc109f812008-10-01 04:51:53 -0300521}
522
Erik Andr?nb933d582009-01-13 13:55:52 -0300523static int po1030_get_green_balance(struct gspca_dev *gspca_dev, __s32 *val)
524{
525 struct sd *sd = (struct sd *) gspca_dev;
526 s32 *sensor_settings = sd->sensor_priv;
527
528 *val = sensor_settings[GREEN_BALANCE_IDX];
529 PDEBUG(D_V4L2, "Read green gain %d", *val);
530
531 return 0;
532}
533
534static int po1030_set_green_balance(struct gspca_dev *gspca_dev, __s32 val)
535{
536 struct sd *sd = (struct sd *) gspca_dev;
537 s32 *sensor_settings = sd->sensor_priv;
538 u8 i2c_data;
539 int err;
540
541 sensor_settings[GREEN_BALANCE_IDX] = val;
542 i2c_data = val & 0xff;
543 PDEBUG(D_V4L2, "Set green gain to %d", i2c_data);
544
545 err = m5602_write_sensor(sd, PO1030_GREEN_1_GAIN,
546 &i2c_data, 1);
547 if (err < 0)
548 return err;
549
550 return m5602_write_sensor(sd, PO1030_GREEN_2_GAIN,
551 &i2c_data, 1);
552}
553
Erik Andr?ncf811d52009-04-03 02:49:10 -0300554static int po1030_get_auto_white_balance(struct gspca_dev *gspca_dev,
555 __s32 *val)
Erik Andr?n3b9ae652009-01-08 03:53:50 -0300556{
557 struct sd *sd = (struct sd *) gspca_dev;
558 s32 *sensor_settings = sd->sensor_priv;
559
560 *val = sensor_settings[AUTO_WHITE_BALANCE_IDX];
561 PDEBUG(D_V4L2, "Auto white balancing is %d", *val);
562
563 return 0;
564}
565
Erik Andr?ncf811d52009-04-03 02:49:10 -0300566static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
567 __s32 val)
Erik Andr?n3b9ae652009-01-08 03:53:50 -0300568{
569 struct sd *sd = (struct sd *) gspca_dev;
570 s32 *sensor_settings = sd->sensor_priv;
571 u8 i2c_data;
572 int err;
573
574 sensor_settings[AUTO_WHITE_BALANCE_IDX] = val;
575
576 err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
577 if (err < 0)
578 return err;
579
Erik Andr?ndd9ce842009-01-13 13:43:46 -0300580 PDEBUG(D_V4L2, "Set auto white balance to %d", val);
Erik Andr?n3b9ae652009-01-08 03:53:50 -0300581 i2c_data = (i2c_data & 0xfe) | (val & 0x01);
582 err = m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
583 return err;
584}
585
Erik Andr?ndd9ce842009-01-13 13:43:46 -0300586static int po1030_get_auto_exposure(struct gspca_dev *gspca_dev,
587 __s32 *val)
588{
589 struct sd *sd = (struct sd *) gspca_dev;
590 s32 *sensor_settings = sd->sensor_priv;
591
592 *val = sensor_settings[AUTO_EXPOSURE_IDX];
593 PDEBUG(D_V4L2, "Auto exposure is %d", *val);
594 return 0;
595}
596
597static int po1030_set_auto_exposure(struct gspca_dev *gspca_dev,
598 __s32 val)
599{
600 struct sd *sd = (struct sd *) gspca_dev;
601 s32 *sensor_settings = sd->sensor_priv;
602 u8 i2c_data;
603 int err;
604
605 sensor_settings[AUTO_EXPOSURE_IDX] = val;
606 err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
607 if (err < 0)
608 return err;
609
610 PDEBUG(D_V4L2, "Set auto exposure to %d", val);
611 i2c_data = (i2c_data & 0xfd) | ((val & 0x01) << 1);
612 return m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
613}
614
Erik Andr?n05d7d9c2009-01-06 06:39:11 -0300615void po1030_disconnect(struct sd *sd)
616{
617 sd->sensor = NULL;
618 kfree(sd->sensor_priv);
619}
620
Erik Andrén658efb62008-11-24 14:21:29 -0300621static void po1030_dump_registers(struct sd *sd)
Erik Andrenc109f812008-10-01 04:51:53 -0300622{
623 int address;
624 u8 value = 0;
625
626 info("Dumping the po1030 sensor core registers");
627 for (address = 0; address < 0x7f; address++) {
Erik Andrénc061c972008-11-27 13:46:39 -0300628 m5602_read_sensor(sd, address, &value, 1);
Erik Andrenc109f812008-10-01 04:51:53 -0300629 info("register 0x%x contains 0x%x",
630 address, value);
631 }
632
633 info("po1030 register state dump complete");
634
635 info("Probing for which registers that are read/write");
636 for (address = 0; address < 0xff; address++) {
637 u8 old_value, ctrl_value;
638 u8 test_value[2] = {0xff, 0xff};
639
Erik Andrénc061c972008-11-27 13:46:39 -0300640 m5602_read_sensor(sd, address, &old_value, 1);
Erik Andrén6dc4cff2008-11-26 04:08:10 -0300641 m5602_write_sensor(sd, address, test_value, 1);
Erik Andrénc061c972008-11-27 13:46:39 -0300642 m5602_read_sensor(sd, address, &ctrl_value, 1);
Erik Andrenc109f812008-10-01 04:51:53 -0300643
644 if (ctrl_value == test_value[0])
645 info("register 0x%x is writeable", address);
646 else
647 info("register 0x%x is read only", address);
648
649 /* Restore original value */
Erik Andrén6dc4cff2008-11-26 04:08:10 -0300650 m5602_write_sensor(sd, address, &old_value, 1);
Erik Andrenc109f812008-10-01 04:51:53 -0300651 }
652}