V4L/DVB (11466): gspca - m5602: Let all ctrls on all sensors be static
[pandora-kernel.git] / drivers / media / video / gspca / m5602 / m5602_po1030.c
1 /*
2  * Driver for the po1030 sensor
3  *
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>
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
21 static int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val);
22 static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val);
23 static int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val);
24 static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val);
25 static int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val);
26 static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val);
27 static int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val);
28 static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val);
29 static int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val);
30 static int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val);
31 static int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val);
32 static int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val);
33 static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
34                                          __s32 val);
35 static int po1030_get_auto_white_balance(struct gspca_dev *gspca_dev,
36                                          __s32 *val);
37
38 static struct v4l2_pix_format po1030_modes[] = {
39         {
40                 640,
41                 480,
42                 V4L2_PIX_FMT_SBGGR8,
43                 V4L2_FIELD_NONE,
44                 .sizeimage = 640 * 480,
45                 .bytesperline = 640,
46                 .colorspace = V4L2_COLORSPACE_SRGB,
47                 .priv = 0
48         }
49 };
50
51 const static struct ctrl po1030_ctrls[] = {
52 #define GAIN_IDX 0
53         {
54                 {
55                         .id             = V4L2_CID_GAIN,
56                         .type           = V4L2_CTRL_TYPE_INTEGER,
57                         .name           = "gain",
58                         .minimum        = 0x00,
59                         .maximum        = 0x4f,
60                         .step           = 0x1,
61                         .default_value  = PO1030_GLOBAL_GAIN_DEFAULT,
62                         .flags          = V4L2_CTRL_FLAG_SLIDER
63                 },
64                 .set = po1030_set_gain,
65                 .get = po1030_get_gain
66         },
67 #define EXPOSURE_IDX 1
68         {
69                 {
70                         .id             = V4L2_CID_EXPOSURE,
71                         .type           = V4L2_CTRL_TYPE_INTEGER,
72                         .name           = "exposure",
73                         .minimum        = 0x00,
74                         .maximum        = 0x02ff,
75                         .step           = 0x1,
76                         .default_value  = PO1030_EXPOSURE_DEFAULT,
77                         .flags          = V4L2_CTRL_FLAG_SLIDER
78                 },
79                 .set = po1030_set_exposure,
80                 .get = po1030_get_exposure
81         },
82 #define RED_BALANCE_IDX 2
83         {
84                 {
85                         .id             = V4L2_CID_RED_BALANCE,
86                         .type           = V4L2_CTRL_TYPE_INTEGER,
87                         .name           = "red balance",
88                         .minimum        = 0x00,
89                         .maximum        = 0xff,
90                         .step           = 0x1,
91                         .default_value  = PO1030_RED_GAIN_DEFAULT,
92                         .flags          = V4L2_CTRL_FLAG_SLIDER
93                 },
94                 .set = po1030_set_red_balance,
95                 .get = po1030_get_red_balance
96         },
97 #define BLUE_BALANCE_IDX 3
98         {
99                 {
100                         .id             = V4L2_CID_BLUE_BALANCE,
101                         .type           = V4L2_CTRL_TYPE_INTEGER,
102                         .name           = "blue balance",
103                         .minimum        = 0x00,
104                         .maximum        = 0xff,
105                         .step           = 0x1,
106                         .default_value  = PO1030_BLUE_GAIN_DEFAULT,
107                         .flags          = V4L2_CTRL_FLAG_SLIDER
108                 },
109                 .set = po1030_set_blue_balance,
110                 .get = po1030_get_blue_balance
111         },
112 #define HFLIP_IDX 4
113         {
114                 {
115                         .id             = V4L2_CID_HFLIP,
116                         .type           = V4L2_CTRL_TYPE_BOOLEAN,
117                         .name           = "horizontal flip",
118                         .minimum        = 0,
119                         .maximum        = 1,
120                         .step           = 1,
121                         .default_value  = 0,
122                 },
123                 .set = po1030_set_hflip,
124                 .get = po1030_get_hflip
125         },
126 #define VFLIP_IDX 5
127         {
128                 {
129                         .id             = V4L2_CID_VFLIP,
130                         .type           = V4L2_CTRL_TYPE_BOOLEAN,
131                         .name           = "vertical flip",
132                         .minimum        = 0,
133                         .maximum        = 1,
134                         .step           = 1,
135                         .default_value  = 0,
136                 },
137                 .set = po1030_set_vflip,
138                 .get = po1030_get_vflip
139         },
140 #define AUTO_WHITE_BALANCE_IDX 6
141         {
142                 {
143                         .id             = V4L2_CID_AUTO_WHITE_BALANCE,
144                         .type           = V4L2_CTRL_TYPE_BOOLEAN,
145                         .name           = "auto white balance",
146                         .minimum        = 0,
147                         .maximum        = 1,
148                         .step           = 1,
149                         .default_value  = 0,
150                 },
151                 .set = po1030_set_auto_white_balance,
152                 .get = po1030_get_auto_white_balance
153         }
154 };
155
156 static void po1030_dump_registers(struct sd *sd);
157
158 int po1030_probe(struct sd *sd)
159 {
160         u8 dev_id_h = 0, dev_id_l = 0, i;
161         s32 *sensor_settings;
162
163         if (force_sensor) {
164                 if (force_sensor == PO1030_SENSOR) {
165                         info("Forcing a %s sensor", po1030.name);
166                         goto sensor_found;
167                 }
168                 /* If we want to force another sensor, don't try to probe this
169                  * one */
170                 return -ENODEV;
171         }
172
173         info("Probing for a po1030 sensor");
174
175         /* Run the pre-init to actually probe the unit */
176         for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
177                 u8 data = preinit_po1030[i][2];
178                 if (preinit_po1030[i][0] == SENSOR)
179                         m5602_write_sensor(sd,
180                                 preinit_po1030[i][1], &data, 1);
181                 else
182                         m5602_write_bridge(sd, preinit_po1030[i][1], data);
183         }
184
185         if (m5602_read_sensor(sd, PO1030_DEVID_H, &dev_id_h, 1))
186                 return -ENODEV;
187
188         if (m5602_read_sensor(sd, PO1030_DEVID_L, &dev_id_l, 1))
189                 return -ENODEV;
190
191         if ((dev_id_h == 0x10) && (dev_id_l == 0x30)) {
192                 info("Detected a po1030 sensor");
193                 goto sensor_found;
194         }
195         return -ENODEV;
196
197 sensor_found:
198         sensor_settings = kmalloc(
199                 ARRAY_SIZE(po1030_ctrls) * sizeof(s32), GFP_KERNEL);
200         if (!sensor_settings)
201                 return -ENOMEM;
202
203         sd->gspca_dev.cam.cam_mode = po1030_modes;
204         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
205         sd->desc->ctrls = po1030_ctrls;
206         sd->desc->nctrls = ARRAY_SIZE(po1030_ctrls);
207
208         for (i = 0; i < ARRAY_SIZE(po1030_ctrls); i++)
209                 sensor_settings[i] = po1030_ctrls[i].qctrl.default_value;
210         sd->sensor_priv = sensor_settings;
211
212         if (dump_sensor)
213                 po1030_dump_registers(sd);
214
215         return 0;
216 }
217
218 int po1030_init(struct sd *sd)
219 {
220         s32 *sensor_settings = sd->sensor_priv;
221         int i, err = 0;
222
223         /* Init the sensor */
224         for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
225                 u8 data[2] = {0x00, 0x00};
226
227                 switch (init_po1030[i][0]) {
228                 case BRIDGE:
229                         err = m5602_write_bridge(sd,
230                                 init_po1030[i][1],
231                                 init_po1030[i][2]);
232                         break;
233
234                 case SENSOR:
235                         data[0] = init_po1030[i][2];
236                         err = m5602_write_sensor(sd,
237                                 init_po1030[i][1], data, 1);
238                         break;
239
240                 default:
241                         info("Invalid stream command, exiting init");
242                         return -EINVAL;
243                 }
244         }
245         if (err < 0)
246                 return err;
247
248         err = po1030_set_exposure(&sd->gspca_dev,
249                                    sensor_settings[EXPOSURE_IDX]);
250         if (err < 0)
251                 return err;
252
253         err = po1030_set_gain(&sd->gspca_dev, sensor_settings[GAIN_IDX]);
254         if (err < 0)
255                 return err;
256
257         err = po1030_set_hflip(&sd->gspca_dev, sensor_settings[HFLIP_IDX]);
258         if (err < 0)
259                 return err;
260
261         err = po1030_set_vflip(&sd->gspca_dev, sensor_settings[VFLIP_IDX]);
262         if (err < 0)
263                 return err;
264
265         err = po1030_set_red_balance(&sd->gspca_dev,
266                                       sensor_settings[RED_BALANCE_IDX]);
267         if (err < 0)
268                 return err;
269
270         err = po1030_set_red_balance(&sd->gspca_dev,
271                                       sensor_settings[BLUE_BALANCE_IDX]);
272         if (err < 0)
273                 return err;
274
275         err = po1030_set_auto_white_balance(&sd->gspca_dev,
276                                       sensor_settings[AUTO_WHITE_BALANCE_IDX]);
277         return err;
278 }
279
280 int po1030_start(struct sd *sd)
281 {
282         int i, err = 0;
283         /* Synthesize the vsync/hsync setup */
284         for (i = 0; i < ARRAY_SIZE(start_po1030) && !err; i++) {
285                 if (start_po1030[i][0] == BRIDGE)
286                         err = m5602_write_bridge(sd, start_po1030[i][1],
287                                 start_po1030[i][2]);
288                 else if (start_po1030[i][0] == SENSOR) {
289                         u8 data = start_po1030[i][2];
290                         err = m5602_write_sensor(sd,
291                                         start_po1030[i][1], &data, 1);
292                 }
293         }
294         return err;
295 }
296
297 static int po1030_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
298 {
299         struct sd *sd = (struct sd *) gspca_dev;
300         s32 *sensor_settings = sd->sensor_priv;
301
302         *val = sensor_settings[EXPOSURE_IDX];
303         PDEBUG(D_V4L2, "Exposure read as %d", *val);
304         return 0;
305 }
306
307 static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
308 {
309         struct sd *sd = (struct sd *) gspca_dev;
310         s32 *sensor_settings = sd->sensor_priv;
311         u8 i2c_data;
312         int err;
313
314         sensor_settings[EXPOSURE_IDX] = val;
315         PDEBUG(D_V4L2, "Set exposure to %d", val & 0xffff);
316
317         i2c_data = ((val & 0xff00) >> 8);
318         PDEBUG(D_V4L2, "Set exposure to high byte to 0x%x",
319                i2c_data);
320
321         err = m5602_write_sensor(sd, PO1030_INTEGLINES_H,
322                                   &i2c_data, 1);
323         if (err < 0)
324                 return err;
325
326         i2c_data = (val & 0xff);
327         PDEBUG(D_V4L2, "Set exposure to low byte to 0x%x",
328                i2c_data);
329         err = m5602_write_sensor(sd, PO1030_INTEGLINES_M,
330                                   &i2c_data, 1);
331
332         return err;
333 }
334
335 static int po1030_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
336 {
337         struct sd *sd = (struct sd *) gspca_dev;
338         s32 *sensor_settings = sd->sensor_priv;
339
340         *val = sensor_settings[GAIN_IDX];
341         PDEBUG(D_V4L2, "Read global gain %d", *val);
342         return 0;
343 }
344
345 static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
346 {
347         struct sd *sd = (struct sd *) gspca_dev;
348         s32 *sensor_settings = sd->sensor_priv;
349         u8 i2c_data;
350         int err;
351
352         sensor_settings[GAIN_IDX] = val;
353
354         i2c_data = val & 0xff;
355         PDEBUG(D_V4L2, "Set global gain to %d", i2c_data);
356         err = m5602_write_sensor(sd, PO1030_GLOBALGAIN,
357                                  &i2c_data, 1);
358         return err;
359 }
360
361 static int po1030_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
362 {
363         struct sd *sd = (struct sd *) gspca_dev;
364         s32 *sensor_settings = sd->sensor_priv;
365
366         *val = sensor_settings[HFLIP_IDX];
367         PDEBUG(D_V4L2, "Read hflip %d", *val);
368
369         return 0;
370 }
371
372 static int po1030_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
373 {
374         struct sd *sd = (struct sd *) gspca_dev;
375         s32 *sensor_settings = sd->sensor_priv;
376         u8 i2c_data;
377         int err;
378
379         sensor_settings[HFLIP_IDX] = val;
380
381         PDEBUG(D_V4L2, "Set hflip %d", val);
382         err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
383         if (err < 0)
384                 return err;
385
386         i2c_data = (0x7f & i2c_data) | ((val & 0x01) << 7);
387
388         err = m5602_write_sensor(sd, PO1030_CONTROL2,
389                                  &i2c_data, 1);
390
391         return err;
392 }
393
394 static int po1030_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
395 {
396         struct sd *sd = (struct sd *) gspca_dev;
397         s32 *sensor_settings = sd->sensor_priv;
398
399         *val = sensor_settings[VFLIP_IDX];
400         PDEBUG(D_V4L2, "Read vflip %d", *val);
401
402         return 0;
403 }
404
405 static int po1030_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
406 {
407         struct sd *sd = (struct sd *) gspca_dev;
408         s32 *sensor_settings = sd->sensor_priv;
409         u8 i2c_data;
410         int err;
411
412         sensor_settings[VFLIP_IDX] = val;
413
414         PDEBUG(D_V4L2, "Set vflip %d", val);
415         err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
416         if (err < 0)
417                 return err;
418
419         i2c_data = (i2c_data & 0xbf) | ((val & 0x01) << 6);
420
421         err = m5602_write_sensor(sd, PO1030_CONTROL2,
422                                  &i2c_data, 1);
423
424         return err;
425 }
426
427 static int po1030_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
428 {
429         struct sd *sd = (struct sd *) gspca_dev;
430         s32 *sensor_settings = sd->sensor_priv;
431
432         *val = sensor_settings[RED_BALANCE_IDX];
433         PDEBUG(D_V4L2, "Read red gain %d", *val);
434         return 0;
435 }
436
437 static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
438 {
439         struct sd *sd = (struct sd *) gspca_dev;
440         s32 *sensor_settings = sd->sensor_priv;
441         u8 i2c_data;
442         int err;
443
444         sensor_settings[RED_BALANCE_IDX] = val;
445
446         i2c_data = val & 0xff;
447         PDEBUG(D_V4L2, "Set red gain to %d", i2c_data);
448         err = m5602_write_sensor(sd, PO1030_RED_GAIN,
449                                   &i2c_data, 1);
450         return err;
451 }
452
453 static int po1030_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
454 {
455         struct sd *sd = (struct sd *) gspca_dev;
456         s32 *sensor_settings = sd->sensor_priv;
457
458         *val = sensor_settings[BLUE_BALANCE_IDX];
459         PDEBUG(D_V4L2, "Read blue gain %d", *val);
460
461         return 0;
462 }
463
464 static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
465 {
466         struct sd *sd = (struct sd *) gspca_dev;
467         s32 *sensor_settings = sd->sensor_priv;
468         u8 i2c_data;
469         int err;
470
471         sensor_settings[BLUE_BALANCE_IDX] = val;
472
473         i2c_data = val & 0xff;
474         PDEBUG(D_V4L2, "Set blue gain to %d", i2c_data);
475         err = m5602_write_sensor(sd, PO1030_BLUE_GAIN,
476                                   &i2c_data, 1);
477
478         return err;
479 }
480
481 static int po1030_get_auto_white_balance(struct gspca_dev *gspca_dev,
482                                          __s32 *val)
483 {
484         struct sd *sd = (struct sd *) gspca_dev;
485         s32 *sensor_settings = sd->sensor_priv;
486
487         *val = sensor_settings[AUTO_WHITE_BALANCE_IDX];
488         PDEBUG(D_V4L2, "Auto white balancing is %d", *val);
489
490         return 0;
491 }
492
493 static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
494                                          __s32 val)
495 {
496         struct sd *sd = (struct sd *) gspca_dev;
497         s32 *sensor_settings = sd->sensor_priv;
498         u8 i2c_data;
499         int err;
500
501         sensor_settings[AUTO_WHITE_BALANCE_IDX] = val;
502
503         err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
504         if (err < 0)
505                 return err;
506
507         i2c_data = (i2c_data & 0xfe) | (val & 0x01);
508         err = m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
509         return err;
510 }
511
512 void po1030_disconnect(struct sd *sd)
513 {
514         sd->sensor = NULL;
515         kfree(sd->sensor_priv);
516 }
517
518 static void po1030_dump_registers(struct sd *sd)
519 {
520         int address;
521         u8 value = 0;
522
523         info("Dumping the po1030 sensor core registers");
524         for (address = 0; address < 0x7f; address++) {
525                 m5602_read_sensor(sd, address, &value, 1);
526                 info("register 0x%x contains 0x%x",
527                      address, value);
528         }
529
530         info("po1030 register state dump complete");
531
532         info("Probing for which registers that are read/write");
533         for (address = 0; address < 0xff; address++) {
534                 u8 old_value, ctrl_value;
535                 u8 test_value[2] = {0xff, 0xff};
536
537                 m5602_read_sensor(sd, address, &old_value, 1);
538                 m5602_write_sensor(sd, address, test_value, 1);
539                 m5602_read_sensor(sd, address, &ctrl_value, 1);
540
541                 if (ctrl_value == test_value[0])
542                         info("register 0x%x is writeable", address);
543                 else
544                         info("register 0x%x is read only", address);
545
546                 /* Restore original value */
547                 m5602_write_sensor(sd, address, &old_value, 1);
548         }
549 }