2 * Driver for the po1030 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_po1030.h"
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_green_balance(struct gspca_dev
*gspca_dev
, __s32
*val
);
30 static int po1030_set_green_balance(struct gspca_dev
*gspca_dev
, __s32 val
);
31 static int po1030_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
32 static int po1030_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
);
33 static int po1030_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
34 static int po1030_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
);
35 static int po1030_set_auto_white_balance(struct gspca_dev
*gspca_dev
,
37 static int po1030_get_auto_white_balance(struct gspca_dev
*gspca_dev
,
39 static int po1030_set_auto_exposure(struct gspca_dev
*gspca_dev
,
41 static int po1030_get_auto_exposure(struct gspca_dev
*gspca_dev
,
44 static struct v4l2_pix_format po1030_modes
[] = {
50 .sizeimage
= 640 * 480,
52 .colorspace
= V4L2_COLORSPACE_SRGB
,
57 static const struct ctrl po1030_ctrls
[] = {
62 .type
= V4L2_CTRL_TYPE_INTEGER
,
67 .default_value
= PO1030_GLOBAL_GAIN_DEFAULT
,
68 .flags
= V4L2_CTRL_FLAG_SLIDER
70 .set
= po1030_set_gain
,
71 .get
= po1030_get_gain
73 #define EXPOSURE_IDX 1
76 .id
= V4L2_CID_EXPOSURE
,
77 .type
= V4L2_CTRL_TYPE_INTEGER
,
82 .default_value
= PO1030_EXPOSURE_DEFAULT
,
83 .flags
= V4L2_CTRL_FLAG_SLIDER
85 .set
= po1030_set_exposure
,
86 .get
= po1030_get_exposure
88 #define RED_BALANCE_IDX 2
91 .id
= V4L2_CID_RED_BALANCE
,
92 .type
= V4L2_CTRL_TYPE_INTEGER
,
93 .name
= "red balance",
97 .default_value
= PO1030_RED_GAIN_DEFAULT
,
98 .flags
= V4L2_CTRL_FLAG_SLIDER
100 .set
= po1030_set_red_balance
,
101 .get
= po1030_get_red_balance
103 #define BLUE_BALANCE_IDX 3
106 .id
= V4L2_CID_BLUE_BALANCE
,
107 .type
= V4L2_CTRL_TYPE_INTEGER
,
108 .name
= "blue balance",
112 .default_value
= PO1030_BLUE_GAIN_DEFAULT
,
113 .flags
= V4L2_CTRL_FLAG_SLIDER
115 .set
= po1030_set_blue_balance
,
116 .get
= po1030_get_blue_balance
121 .id
= V4L2_CID_HFLIP
,
122 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
123 .name
= "horizontal flip",
129 .set
= po1030_set_hflip
,
130 .get
= po1030_get_hflip
135 .id
= V4L2_CID_VFLIP
,
136 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
137 .name
= "vertical flip",
143 .set
= po1030_set_vflip
,
144 .get
= po1030_get_vflip
146 #define AUTO_WHITE_BALANCE_IDX 6
149 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
150 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
151 .name
= "auto white balance",
157 .set
= po1030_set_auto_white_balance
,
158 .get
= po1030_get_auto_white_balance
160 #define AUTO_EXPOSURE_IDX 7
163 .id
= V4L2_CID_EXPOSURE_AUTO
,
164 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
165 .name
= "auto exposure",
171 .set
= po1030_set_auto_exposure
,
172 .get
= po1030_get_auto_exposure
174 #define GREEN_BALANCE_IDX 8
177 .id
= M5602_V4L2_CID_GREEN_BALANCE
,
178 .type
= V4L2_CTRL_TYPE_INTEGER
,
179 .name
= "green balance",
183 .default_value
= PO1030_GREEN_GAIN_DEFAULT
,
184 .flags
= V4L2_CTRL_FLAG_SLIDER
186 .set
= po1030_set_green_balance
,
187 .get
= po1030_get_green_balance
191 static void po1030_dump_registers(struct sd
*sd
);
193 int po1030_probe(struct sd
*sd
)
196 s32
*sensor_settings
;
199 if (force_sensor
== PO1030_SENSOR
) {
200 info("Forcing a %s sensor", po1030
.name
);
203 /* If we want to force another sensor, don't try to probe this
208 info("Probing for a po1030 sensor");
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
)
214 m5602_write_sensor(sd
,
215 preinit_po1030
[i
][1], &data
, 1);
217 m5602_write_bridge(sd
, preinit_po1030
[i
][1], data
);
220 if (m5602_read_sensor(sd
, PO1030_DEVID_H
, &dev_id_h
, 1))
223 if (dev_id_h
== 0x30) {
224 info("Detected a po1030 sensor");
230 sensor_settings
= kmalloc(
231 ARRAY_SIZE(po1030_ctrls
) * sizeof(s32
), GFP_KERNEL
);
232 if (!sensor_settings
)
235 sd
->gspca_dev
.cam
.cam_mode
= po1030_modes
;
236 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(po1030_modes
);
237 sd
->desc
->ctrls
= po1030_ctrls
;
238 sd
->desc
->nctrls
= ARRAY_SIZE(po1030_ctrls
);
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
;
247 int po1030_init(struct sd
*sd
)
249 s32
*sensor_settings
= sd
->sensor_priv
;
252 /* Init the sensor */
253 for (i
= 0; i
< ARRAY_SIZE(init_po1030
) && !err
; i
++) {
254 u8 data
[2] = {0x00, 0x00};
256 switch (init_po1030
[i
][0]) {
258 err
= m5602_write_bridge(sd
,
264 data
[0] = init_po1030
[i
][2];
265 err
= m5602_write_sensor(sd
,
266 init_po1030
[i
][1], data
, 1);
270 info("Invalid stream command, exiting init");
278 po1030_dump_registers(sd
);
280 err
= po1030_set_exposure(&sd
->gspca_dev
,
281 sensor_settings
[EXPOSURE_IDX
]);
285 err
= po1030_set_gain(&sd
->gspca_dev
, sensor_settings
[GAIN_IDX
]);
289 err
= po1030_set_hflip(&sd
->gspca_dev
, sensor_settings
[HFLIP_IDX
]);
293 err
= po1030_set_vflip(&sd
->gspca_dev
, sensor_settings
[VFLIP_IDX
]);
297 err
= po1030_set_red_balance(&sd
->gspca_dev
,
298 sensor_settings
[RED_BALANCE_IDX
]);
302 err
= po1030_set_blue_balance(&sd
->gspca_dev
,
303 sensor_settings
[BLUE_BALANCE_IDX
]);
307 err
= po1030_set_green_balance(&sd
->gspca_dev
,
308 sensor_settings
[GREEN_BALANCE_IDX
]);
312 err
= po1030_set_auto_white_balance(&sd
->gspca_dev
,
313 sensor_settings
[AUTO_WHITE_BALANCE_IDX
]);
317 err
= po1030_set_auto_exposure(&sd
->gspca_dev
,
318 sensor_settings
[AUTO_EXPOSURE_IDX
]);
322 int po1030_start(struct sd
*sd
)
324 struct cam
*cam
= &sd
->gspca_dev
.cam
;
326 int width
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].width
;
327 int height
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].height
;
328 int ver_offs
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
;
333 data
= PO1030_SUBSAMPLING
;
334 err
= m5602_write_sensor(sd
, PO1030_CONTROL3
, &data
, 1);
338 data
= ((width
+ 3) >> 8) & 0xff;
339 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_H
, &data
, 1);
343 data
= (width
+ 3) & 0xff;
344 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_L
, &data
, 1);
348 data
= ((height
+ 1) >> 8) & 0xff;
349 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_H
, &data
, 1);
353 data
= (height
+ 1) & 0xff;
354 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_L
, &data
, 1);
362 err
= m5602_write_sensor(sd
, PO1030_CONTROL3
, &data
, 1);
366 data
= ((width
+ 7) >> 8) & 0xff;
367 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_H
, &data
, 1);
371 data
= (width
+ 7) & 0xff;
372 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_L
, &data
, 1);
376 data
= ((height
+ 3) >> 8) & 0xff;
377 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_H
, &data
, 1);
381 data
= (height
+ 3) & 0xff;
382 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_L
, &data
, 1);
388 err
= m5602_write_bridge(sd
, M5602_XB_SENSOR_TYPE
, 0x0c);
392 err
= m5602_write_bridge(sd
, M5602_XB_LINE_OF_FRAME_H
, 0x81);
396 err
= m5602_write_bridge(sd
, M5602_XB_PIX_OF_LINE_H
, 0x82);
400 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0x01);
404 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
,
405 ((ver_offs
>> 8) & 0xff));
409 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (ver_offs
& 0xff));
413 for (i
= 0; i
< 2 && !err
; i
++)
414 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, 0);
418 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (height
>> 8) & 0xff);
422 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (height
& 0xff));
426 for (i
= 0; i
< 2 && !err
; i
++)
427 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, 0);
429 for (i
= 0; i
< 2 && !err
; i
++)
430 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0);
432 for (i
= 0; i
< 2 && !err
; i
++)
433 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, 0);
437 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, (width
>> 8) & 0xff);
441 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, (width
& 0xff));
445 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0);
449 static int po1030_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
451 struct sd
*sd
= (struct sd
*) gspca_dev
;
452 s32
*sensor_settings
= sd
->sensor_priv
;
454 *val
= sensor_settings
[EXPOSURE_IDX
];
455 PDEBUG(D_V4L2
, "Exposure read as %d", *val
);
459 static int po1030_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
461 struct sd
*sd
= (struct sd
*) gspca_dev
;
462 s32
*sensor_settings
= sd
->sensor_priv
;
466 sensor_settings
[EXPOSURE_IDX
] = val
;
467 PDEBUG(D_V4L2
, "Set exposure to %d", val
& 0xffff);
469 i2c_data
= ((val
& 0xff00) >> 8);
470 PDEBUG(D_V4L2
, "Set exposure to high byte to 0x%x",
473 err
= m5602_write_sensor(sd
, PO1030_INTEGLINES_H
,
478 i2c_data
= (val
& 0xff);
479 PDEBUG(D_V4L2
, "Set exposure to low byte to 0x%x",
481 err
= m5602_write_sensor(sd
, PO1030_INTEGLINES_M
,
487 static int po1030_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
489 struct sd
*sd
= (struct sd
*) gspca_dev
;
490 s32
*sensor_settings
= sd
->sensor_priv
;
492 *val
= sensor_settings
[GAIN_IDX
];
493 PDEBUG(D_V4L2
, "Read global gain %d", *val
);
497 static int po1030_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
499 struct sd
*sd
= (struct sd
*) gspca_dev
;
500 s32
*sensor_settings
= sd
->sensor_priv
;
504 sensor_settings
[GAIN_IDX
] = val
;
506 i2c_data
= val
& 0xff;
507 PDEBUG(D_V4L2
, "Set global gain to %d", i2c_data
);
508 err
= m5602_write_sensor(sd
, PO1030_GLOBALGAIN
,
513 static int po1030_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
515 struct sd
*sd
= (struct sd
*) gspca_dev
;
516 s32
*sensor_settings
= sd
->sensor_priv
;
518 *val
= sensor_settings
[HFLIP_IDX
];
519 PDEBUG(D_V4L2
, "Read hflip %d", *val
);
524 static int po1030_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
)
526 struct sd
*sd
= (struct sd
*) gspca_dev
;
527 s32
*sensor_settings
= sd
->sensor_priv
;
531 sensor_settings
[HFLIP_IDX
] = val
;
533 PDEBUG(D_V4L2
, "Set hflip %d", val
);
534 err
= m5602_read_sensor(sd
, PO1030_CONTROL2
, &i2c_data
, 1);
538 i2c_data
= (0x7f & i2c_data
) | ((val
& 0x01) << 7);
540 err
= m5602_write_sensor(sd
, PO1030_CONTROL2
,
546 static int po1030_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
548 struct sd
*sd
= (struct sd
*) gspca_dev
;
549 s32
*sensor_settings
= sd
->sensor_priv
;
551 *val
= sensor_settings
[VFLIP_IDX
];
552 PDEBUG(D_V4L2
, "Read vflip %d", *val
);
557 static int po1030_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
)
559 struct sd
*sd
= (struct sd
*) gspca_dev
;
560 s32
*sensor_settings
= sd
->sensor_priv
;
564 sensor_settings
[VFLIP_IDX
] = val
;
566 PDEBUG(D_V4L2
, "Set vflip %d", val
);
567 err
= m5602_read_sensor(sd
, PO1030_CONTROL2
, &i2c_data
, 1);
571 i2c_data
= (i2c_data
& 0xbf) | ((val
& 0x01) << 6);
573 err
= m5602_write_sensor(sd
, PO1030_CONTROL2
,
579 static int po1030_get_red_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
581 struct sd
*sd
= (struct sd
*) gspca_dev
;
582 s32
*sensor_settings
= sd
->sensor_priv
;
584 *val
= sensor_settings
[RED_BALANCE_IDX
];
585 PDEBUG(D_V4L2
, "Read red gain %d", *val
);
589 static int po1030_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
591 struct sd
*sd
= (struct sd
*) gspca_dev
;
592 s32
*sensor_settings
= sd
->sensor_priv
;
596 sensor_settings
[RED_BALANCE_IDX
] = val
;
598 i2c_data
= val
& 0xff;
599 PDEBUG(D_V4L2
, "Set red gain to %d", i2c_data
);
600 err
= m5602_write_sensor(sd
, PO1030_RED_GAIN
,
605 static int po1030_get_blue_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
607 struct sd
*sd
= (struct sd
*) gspca_dev
;
608 s32
*sensor_settings
= sd
->sensor_priv
;
610 *val
= sensor_settings
[BLUE_BALANCE_IDX
];
611 PDEBUG(D_V4L2
, "Read blue gain %d", *val
);
616 static int po1030_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
618 struct sd
*sd
= (struct sd
*) gspca_dev
;
619 s32
*sensor_settings
= sd
->sensor_priv
;
623 sensor_settings
[BLUE_BALANCE_IDX
] = val
;
625 i2c_data
= val
& 0xff;
626 PDEBUG(D_V4L2
, "Set blue gain to %d", i2c_data
);
627 err
= m5602_write_sensor(sd
, PO1030_BLUE_GAIN
,
633 static int po1030_get_green_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
635 struct sd
*sd
= (struct sd
*) gspca_dev
;
636 s32
*sensor_settings
= sd
->sensor_priv
;
638 *val
= sensor_settings
[GREEN_BALANCE_IDX
];
639 PDEBUG(D_V4L2
, "Read green gain %d", *val
);
644 static int po1030_set_green_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
646 struct sd
*sd
= (struct sd
*) gspca_dev
;
647 s32
*sensor_settings
= sd
->sensor_priv
;
651 sensor_settings
[GREEN_BALANCE_IDX
] = val
;
652 i2c_data
= val
& 0xff;
653 PDEBUG(D_V4L2
, "Set green gain to %d", i2c_data
);
655 err
= m5602_write_sensor(sd
, PO1030_GREEN_1_GAIN
,
660 return m5602_write_sensor(sd
, PO1030_GREEN_2_GAIN
,
664 static int po1030_get_auto_white_balance(struct gspca_dev
*gspca_dev
,
667 struct sd
*sd
= (struct sd
*) gspca_dev
;
668 s32
*sensor_settings
= sd
->sensor_priv
;
670 *val
= sensor_settings
[AUTO_WHITE_BALANCE_IDX
];
671 PDEBUG(D_V4L2
, "Auto white balancing is %d", *val
);
676 static int po1030_set_auto_white_balance(struct gspca_dev
*gspca_dev
,
679 struct sd
*sd
= (struct sd
*) gspca_dev
;
680 s32
*sensor_settings
= sd
->sensor_priv
;
684 sensor_settings
[AUTO_WHITE_BALANCE_IDX
] = val
;
686 err
= m5602_read_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
690 PDEBUG(D_V4L2
, "Set auto white balance to %d", val
);
691 i2c_data
= (i2c_data
& 0xfe) | (val
& 0x01);
692 err
= m5602_write_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
696 static int po1030_get_auto_exposure(struct gspca_dev
*gspca_dev
,
699 struct sd
*sd
= (struct sd
*) gspca_dev
;
700 s32
*sensor_settings
= sd
->sensor_priv
;
702 *val
= sensor_settings
[AUTO_EXPOSURE_IDX
];
703 PDEBUG(D_V4L2
, "Auto exposure is %d", *val
);
707 static int po1030_set_auto_exposure(struct gspca_dev
*gspca_dev
,
710 struct sd
*sd
= (struct sd
*) gspca_dev
;
711 s32
*sensor_settings
= sd
->sensor_priv
;
715 sensor_settings
[AUTO_EXPOSURE_IDX
] = val
;
716 err
= m5602_read_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
720 PDEBUG(D_V4L2
, "Set auto exposure to %d", val
);
721 i2c_data
= (i2c_data
& 0xfd) | ((val
& 0x01) << 1);
722 return m5602_write_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
725 void po1030_disconnect(struct sd
*sd
)
728 kfree(sd
->sensor_priv
);
731 static void po1030_dump_registers(struct sd
*sd
)
736 info("Dumping the po1030 sensor core registers");
737 for (address
= 0; address
< 0x7f; address
++) {
738 m5602_read_sensor(sd
, address
, &value
, 1);
739 info("register 0x%x contains 0x%x",
743 info("po1030 register state dump complete");
745 info("Probing for which registers that are read/write");
746 for (address
= 0; address
< 0xff; address
++) {
747 u8 old_value
, ctrl_value
;
748 u8 test_value
[2] = {0xff, 0xff};
750 m5602_read_sensor(sd
, address
, &old_value
, 1);
751 m5602_write_sensor(sd
, address
, test_value
, 1);
752 m5602_read_sensor(sd
, address
, &ctrl_value
, 1);
754 if (ctrl_value
== test_value
[0])
755 info("register 0x%x is writeable", address
);
757 info("register 0x%x is read only", address
);
759 /* Restore original value */
760 m5602_write_sensor(sd
, address
, &old_value
, 1);