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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include "m5602_po1030.h"
23 static int po1030_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
);
24 static int po1030_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
);
25 static int po1030_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
);
26 static int po1030_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
);
27 static int po1030_get_red_balance(struct gspca_dev
*gspca_dev
, __s32
*val
);
28 static int po1030_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
);
29 static int po1030_get_blue_balance(struct gspca_dev
*gspca_dev
, __s32
*val
);
30 static int po1030_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
);
31 static int po1030_get_green_balance(struct gspca_dev
*gspca_dev
, __s32
*val
);
32 static int po1030_set_green_balance(struct gspca_dev
*gspca_dev
, __s32 val
);
33 static int po1030_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
34 static int po1030_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
);
35 static int po1030_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
36 static int po1030_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
);
37 static int po1030_set_auto_white_balance(struct gspca_dev
*gspca_dev
,
39 static int po1030_get_auto_white_balance(struct gspca_dev
*gspca_dev
,
41 static int po1030_set_auto_exposure(struct gspca_dev
*gspca_dev
,
43 static int po1030_get_auto_exposure(struct gspca_dev
*gspca_dev
,
46 static struct v4l2_pix_format po1030_modes
[] = {
52 .sizeimage
= 640 * 480,
54 .colorspace
= V4L2_COLORSPACE_SRGB
,
59 static const struct ctrl po1030_ctrls
[] = {
64 .type
= V4L2_CTRL_TYPE_INTEGER
,
69 .default_value
= PO1030_GLOBAL_GAIN_DEFAULT
,
70 .flags
= V4L2_CTRL_FLAG_SLIDER
72 .set
= po1030_set_gain
,
73 .get
= po1030_get_gain
75 #define EXPOSURE_IDX 1
78 .id
= V4L2_CID_EXPOSURE
,
79 .type
= V4L2_CTRL_TYPE_INTEGER
,
84 .default_value
= PO1030_EXPOSURE_DEFAULT
,
85 .flags
= V4L2_CTRL_FLAG_SLIDER
87 .set
= po1030_set_exposure
,
88 .get
= po1030_get_exposure
90 #define RED_BALANCE_IDX 2
93 .id
= V4L2_CID_RED_BALANCE
,
94 .type
= V4L2_CTRL_TYPE_INTEGER
,
95 .name
= "red balance",
99 .default_value
= PO1030_RED_GAIN_DEFAULT
,
100 .flags
= V4L2_CTRL_FLAG_SLIDER
102 .set
= po1030_set_red_balance
,
103 .get
= po1030_get_red_balance
105 #define BLUE_BALANCE_IDX 3
108 .id
= V4L2_CID_BLUE_BALANCE
,
109 .type
= V4L2_CTRL_TYPE_INTEGER
,
110 .name
= "blue balance",
114 .default_value
= PO1030_BLUE_GAIN_DEFAULT
,
115 .flags
= V4L2_CTRL_FLAG_SLIDER
117 .set
= po1030_set_blue_balance
,
118 .get
= po1030_get_blue_balance
123 .id
= V4L2_CID_HFLIP
,
124 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
125 .name
= "horizontal flip",
131 .set
= po1030_set_hflip
,
132 .get
= po1030_get_hflip
137 .id
= V4L2_CID_VFLIP
,
138 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
139 .name
= "vertical flip",
145 .set
= po1030_set_vflip
,
146 .get
= po1030_get_vflip
148 #define AUTO_WHITE_BALANCE_IDX 6
151 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
152 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
153 .name
= "auto white balance",
159 .set
= po1030_set_auto_white_balance
,
160 .get
= po1030_get_auto_white_balance
162 #define AUTO_EXPOSURE_IDX 7
165 .id
= V4L2_CID_EXPOSURE_AUTO
,
166 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
167 .name
= "auto exposure",
173 .set
= po1030_set_auto_exposure
,
174 .get
= po1030_get_auto_exposure
176 #define GREEN_BALANCE_IDX 8
179 .id
= M5602_V4L2_CID_GREEN_BALANCE
,
180 .type
= V4L2_CTRL_TYPE_INTEGER
,
181 .name
= "green balance",
185 .default_value
= PO1030_GREEN_GAIN_DEFAULT
,
186 .flags
= V4L2_CTRL_FLAG_SLIDER
188 .set
= po1030_set_green_balance
,
189 .get
= po1030_get_green_balance
193 static void po1030_dump_registers(struct sd
*sd
);
195 int po1030_probe(struct sd
*sd
)
198 s32
*sensor_settings
;
201 if (force_sensor
== PO1030_SENSOR
) {
202 pr_info("Forcing a %s sensor\n", po1030
.name
);
205 /* If we want to force another sensor, don't try to probe this
210 PDEBUG(D_PROBE
, "Probing for a po1030 sensor");
212 /* Run the pre-init to actually probe the unit */
213 for (i
= 0; i
< ARRAY_SIZE(preinit_po1030
); i
++) {
214 u8 data
= preinit_po1030
[i
][2];
215 if (preinit_po1030
[i
][0] == SENSOR
)
216 m5602_write_sensor(sd
,
217 preinit_po1030
[i
][1], &data
, 1);
219 m5602_write_bridge(sd
, preinit_po1030
[i
][1], data
);
222 if (m5602_read_sensor(sd
, PO1030_DEVID_H
, &dev_id_h
, 1))
225 if (dev_id_h
== 0x30) {
226 pr_info("Detected a po1030 sensor\n");
232 sensor_settings
= kmalloc(
233 ARRAY_SIZE(po1030_ctrls
) * sizeof(s32
), GFP_KERNEL
);
234 if (!sensor_settings
)
237 sd
->gspca_dev
.cam
.cam_mode
= po1030_modes
;
238 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(po1030_modes
);
239 sd
->desc
->ctrls
= po1030_ctrls
;
240 sd
->desc
->nctrls
= ARRAY_SIZE(po1030_ctrls
);
242 for (i
= 0; i
< ARRAY_SIZE(po1030_ctrls
); i
++)
243 sensor_settings
[i
] = po1030_ctrls
[i
].qctrl
.default_value
;
244 sd
->sensor_priv
= sensor_settings
;
249 int po1030_init(struct sd
*sd
)
251 s32
*sensor_settings
= sd
->sensor_priv
;
254 /* Init the sensor */
255 for (i
= 0; i
< ARRAY_SIZE(init_po1030
) && !err
; i
++) {
256 u8 data
[2] = {0x00, 0x00};
258 switch (init_po1030
[i
][0]) {
260 err
= m5602_write_bridge(sd
,
266 data
[0] = init_po1030
[i
][2];
267 err
= m5602_write_sensor(sd
,
268 init_po1030
[i
][1], data
, 1);
272 pr_info("Invalid stream command, exiting init\n");
280 po1030_dump_registers(sd
);
282 err
= po1030_set_exposure(&sd
->gspca_dev
,
283 sensor_settings
[EXPOSURE_IDX
]);
287 err
= po1030_set_gain(&sd
->gspca_dev
, sensor_settings
[GAIN_IDX
]);
291 err
= po1030_set_hflip(&sd
->gspca_dev
, sensor_settings
[HFLIP_IDX
]);
295 err
= po1030_set_vflip(&sd
->gspca_dev
, sensor_settings
[VFLIP_IDX
]);
299 err
= po1030_set_red_balance(&sd
->gspca_dev
,
300 sensor_settings
[RED_BALANCE_IDX
]);
304 err
= po1030_set_blue_balance(&sd
->gspca_dev
,
305 sensor_settings
[BLUE_BALANCE_IDX
]);
309 err
= po1030_set_green_balance(&sd
->gspca_dev
,
310 sensor_settings
[GREEN_BALANCE_IDX
]);
314 err
= po1030_set_auto_white_balance(&sd
->gspca_dev
,
315 sensor_settings
[AUTO_WHITE_BALANCE_IDX
]);
319 err
= po1030_set_auto_exposure(&sd
->gspca_dev
,
320 sensor_settings
[AUTO_EXPOSURE_IDX
]);
324 int po1030_start(struct sd
*sd
)
326 struct cam
*cam
= &sd
->gspca_dev
.cam
;
328 int width
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].width
;
329 int height
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].height
;
330 int ver_offs
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
;
335 data
= PO1030_SUBSAMPLING
;
336 err
= m5602_write_sensor(sd
, PO1030_CONTROL3
, &data
, 1);
340 data
= ((width
+ 3) >> 8) & 0xff;
341 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_H
, &data
, 1);
345 data
= (width
+ 3) & 0xff;
346 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_L
, &data
, 1);
350 data
= ((height
+ 1) >> 8) & 0xff;
351 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_H
, &data
, 1);
355 data
= (height
+ 1) & 0xff;
356 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_L
, &data
, 1);
364 err
= m5602_write_sensor(sd
, PO1030_CONTROL3
, &data
, 1);
368 data
= ((width
+ 7) >> 8) & 0xff;
369 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_H
, &data
, 1);
373 data
= (width
+ 7) & 0xff;
374 err
= m5602_write_sensor(sd
, PO1030_WINDOWWIDTH_L
, &data
, 1);
378 data
= ((height
+ 3) >> 8) & 0xff;
379 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_H
, &data
, 1);
383 data
= (height
+ 3) & 0xff;
384 err
= m5602_write_sensor(sd
, PO1030_WINDOWHEIGHT_L
, &data
, 1);
390 err
= m5602_write_bridge(sd
, M5602_XB_SENSOR_TYPE
, 0x0c);
394 err
= m5602_write_bridge(sd
, M5602_XB_LINE_OF_FRAME_H
, 0x81);
398 err
= m5602_write_bridge(sd
, M5602_XB_PIX_OF_LINE_H
, 0x82);
402 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0x01);
406 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
,
407 ((ver_offs
>> 8) & 0xff));
411 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (ver_offs
& 0xff));
415 for (i
= 0; i
< 2 && !err
; i
++)
416 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, 0);
420 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (height
>> 8) & 0xff);
424 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (height
& 0xff));
428 for (i
= 0; i
< 2 && !err
; i
++)
429 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, 0);
431 for (i
= 0; i
< 2 && !err
; i
++)
432 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0);
434 for (i
= 0; i
< 2 && !err
; i
++)
435 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, 0);
439 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, (width
>> 8) & 0xff);
443 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, (width
& 0xff));
447 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0);
451 static int po1030_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
453 struct sd
*sd
= (struct sd
*) gspca_dev
;
454 s32
*sensor_settings
= sd
->sensor_priv
;
456 *val
= sensor_settings
[EXPOSURE_IDX
];
457 PDEBUG(D_V4L2
, "Exposure read as %d", *val
);
461 static int po1030_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
463 struct sd
*sd
= (struct sd
*) gspca_dev
;
464 s32
*sensor_settings
= sd
->sensor_priv
;
468 sensor_settings
[EXPOSURE_IDX
] = val
;
469 PDEBUG(D_V4L2
, "Set exposure to %d", val
& 0xffff);
471 i2c_data
= ((val
& 0xff00) >> 8);
472 PDEBUG(D_V4L2
, "Set exposure to high byte to 0x%x",
475 err
= m5602_write_sensor(sd
, PO1030_INTEGLINES_H
,
480 i2c_data
= (val
& 0xff);
481 PDEBUG(D_V4L2
, "Set exposure to low byte to 0x%x",
483 err
= m5602_write_sensor(sd
, PO1030_INTEGLINES_M
,
489 static int po1030_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
491 struct sd
*sd
= (struct sd
*) gspca_dev
;
492 s32
*sensor_settings
= sd
->sensor_priv
;
494 *val
= sensor_settings
[GAIN_IDX
];
495 PDEBUG(D_V4L2
, "Read global gain %d", *val
);
499 static int po1030_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
501 struct sd
*sd
= (struct sd
*) gspca_dev
;
502 s32
*sensor_settings
= sd
->sensor_priv
;
506 sensor_settings
[GAIN_IDX
] = val
;
508 i2c_data
= val
& 0xff;
509 PDEBUG(D_V4L2
, "Set global gain to %d", i2c_data
);
510 err
= m5602_write_sensor(sd
, PO1030_GLOBALGAIN
,
515 static int po1030_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
517 struct sd
*sd
= (struct sd
*) gspca_dev
;
518 s32
*sensor_settings
= sd
->sensor_priv
;
520 *val
= sensor_settings
[HFLIP_IDX
];
521 PDEBUG(D_V4L2
, "Read hflip %d", *val
);
526 static int po1030_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
)
528 struct sd
*sd
= (struct sd
*) gspca_dev
;
529 s32
*sensor_settings
= sd
->sensor_priv
;
533 sensor_settings
[HFLIP_IDX
] = val
;
535 PDEBUG(D_V4L2
, "Set hflip %d", val
);
536 err
= m5602_read_sensor(sd
, PO1030_CONTROL2
, &i2c_data
, 1);
540 i2c_data
= (0x7f & i2c_data
) | ((val
& 0x01) << 7);
542 err
= m5602_write_sensor(sd
, PO1030_CONTROL2
,
548 static int po1030_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
550 struct sd
*sd
= (struct sd
*) gspca_dev
;
551 s32
*sensor_settings
= sd
->sensor_priv
;
553 *val
= sensor_settings
[VFLIP_IDX
];
554 PDEBUG(D_V4L2
, "Read vflip %d", *val
);
559 static int po1030_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
)
561 struct sd
*sd
= (struct sd
*) gspca_dev
;
562 s32
*sensor_settings
= sd
->sensor_priv
;
566 sensor_settings
[VFLIP_IDX
] = val
;
568 PDEBUG(D_V4L2
, "Set vflip %d", val
);
569 err
= m5602_read_sensor(sd
, PO1030_CONTROL2
, &i2c_data
, 1);
573 i2c_data
= (i2c_data
& 0xbf) | ((val
& 0x01) << 6);
575 err
= m5602_write_sensor(sd
, PO1030_CONTROL2
,
581 static int po1030_get_red_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
583 struct sd
*sd
= (struct sd
*) gspca_dev
;
584 s32
*sensor_settings
= sd
->sensor_priv
;
586 *val
= sensor_settings
[RED_BALANCE_IDX
];
587 PDEBUG(D_V4L2
, "Read red gain %d", *val
);
591 static int po1030_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
593 struct sd
*sd
= (struct sd
*) gspca_dev
;
594 s32
*sensor_settings
= sd
->sensor_priv
;
598 sensor_settings
[RED_BALANCE_IDX
] = val
;
600 i2c_data
= val
& 0xff;
601 PDEBUG(D_V4L2
, "Set red gain to %d", i2c_data
);
602 err
= m5602_write_sensor(sd
, PO1030_RED_GAIN
,
607 static int po1030_get_blue_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
609 struct sd
*sd
= (struct sd
*) gspca_dev
;
610 s32
*sensor_settings
= sd
->sensor_priv
;
612 *val
= sensor_settings
[BLUE_BALANCE_IDX
];
613 PDEBUG(D_V4L2
, "Read blue gain %d", *val
);
618 static int po1030_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
620 struct sd
*sd
= (struct sd
*) gspca_dev
;
621 s32
*sensor_settings
= sd
->sensor_priv
;
625 sensor_settings
[BLUE_BALANCE_IDX
] = val
;
627 i2c_data
= val
& 0xff;
628 PDEBUG(D_V4L2
, "Set blue gain to %d", i2c_data
);
629 err
= m5602_write_sensor(sd
, PO1030_BLUE_GAIN
,
635 static int po1030_get_green_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
637 struct sd
*sd
= (struct sd
*) gspca_dev
;
638 s32
*sensor_settings
= sd
->sensor_priv
;
640 *val
= sensor_settings
[GREEN_BALANCE_IDX
];
641 PDEBUG(D_V4L2
, "Read green gain %d", *val
);
646 static int po1030_set_green_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
648 struct sd
*sd
= (struct sd
*) gspca_dev
;
649 s32
*sensor_settings
= sd
->sensor_priv
;
653 sensor_settings
[GREEN_BALANCE_IDX
] = val
;
654 i2c_data
= val
& 0xff;
655 PDEBUG(D_V4L2
, "Set green gain to %d", i2c_data
);
657 err
= m5602_write_sensor(sd
, PO1030_GREEN_1_GAIN
,
662 return m5602_write_sensor(sd
, PO1030_GREEN_2_GAIN
,
666 static int po1030_get_auto_white_balance(struct gspca_dev
*gspca_dev
,
669 struct sd
*sd
= (struct sd
*) gspca_dev
;
670 s32
*sensor_settings
= sd
->sensor_priv
;
672 *val
= sensor_settings
[AUTO_WHITE_BALANCE_IDX
];
673 PDEBUG(D_V4L2
, "Auto white balancing is %d", *val
);
678 static int po1030_set_auto_white_balance(struct gspca_dev
*gspca_dev
,
681 struct sd
*sd
= (struct sd
*) gspca_dev
;
682 s32
*sensor_settings
= sd
->sensor_priv
;
686 sensor_settings
[AUTO_WHITE_BALANCE_IDX
] = val
;
688 err
= m5602_read_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
692 PDEBUG(D_V4L2
, "Set auto white balance to %d", val
);
693 i2c_data
= (i2c_data
& 0xfe) | (val
& 0x01);
694 err
= m5602_write_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
698 static int po1030_get_auto_exposure(struct gspca_dev
*gspca_dev
,
701 struct sd
*sd
= (struct sd
*) gspca_dev
;
702 s32
*sensor_settings
= sd
->sensor_priv
;
704 *val
= sensor_settings
[AUTO_EXPOSURE_IDX
];
705 PDEBUG(D_V4L2
, "Auto exposure is %d", *val
);
709 static int po1030_set_auto_exposure(struct gspca_dev
*gspca_dev
,
712 struct sd
*sd
= (struct sd
*) gspca_dev
;
713 s32
*sensor_settings
= sd
->sensor_priv
;
717 sensor_settings
[AUTO_EXPOSURE_IDX
] = val
;
718 err
= m5602_read_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
722 PDEBUG(D_V4L2
, "Set auto exposure to %d", val
);
723 i2c_data
= (i2c_data
& 0xfd) | ((val
& 0x01) << 1);
724 return m5602_write_sensor(sd
, PO1030_AUTOCTRL1
, &i2c_data
, 1);
727 void po1030_disconnect(struct sd
*sd
)
730 kfree(sd
->sensor_priv
);
733 static void po1030_dump_registers(struct sd
*sd
)
738 pr_info("Dumping the po1030 sensor core registers\n");
739 for (address
= 0; address
< 0x7f; address
++) {
740 m5602_read_sensor(sd
, address
, &value
, 1);
741 pr_info("register 0x%x contains 0x%x\n", address
, value
);
744 pr_info("po1030 register state dump complete\n");
746 pr_info("Probing for which registers that are read/write\n");
747 for (address
= 0; address
< 0xff; address
++) {
748 u8 old_value
, ctrl_value
;
749 u8 test_value
[2] = {0xff, 0xff};
751 m5602_read_sensor(sd
, address
, &old_value
, 1);
752 m5602_write_sensor(sd
, address
, test_value
, 1);
753 m5602_read_sensor(sd
, address
, &ctrl_value
, 1);
755 if (ctrl_value
== test_value
[0])
756 pr_info("register 0x%x is writeable\n", address
);
758 pr_info("register 0x%x is read only\n", address
);
760 /* Restore original value */
761 m5602_write_sensor(sd
, address
, &old_value
, 1);