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_s_ctrl(struct v4l2_ctrl *ctrl);
24 static void po1030_dump_registers(struct sd *sd);
26 static const unsigned char preinit_po1030[][3] = {
27 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
28 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
29 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
30 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
31 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
32 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
33 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
34 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
35 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
36 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
37 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
38 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
39 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
41 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
43 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
44 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
45 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
46 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
47 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
48 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
49 {BRIDGE, M5602_XB_GPIO_DAT, 0x00}
52 static const unsigned char init_po1030[][3] = {
53 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
54 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
55 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
56 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
57 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
58 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
59 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
61 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
63 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
64 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
65 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
66 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
67 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
68 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
69 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
70 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
71 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
72 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
74 {SENSOR, PO1030_AUTOCTRL2, 0x04},
76 {SENSOR, PO1030_OUTFORMCTRL2, PO1030_RAW_RGB_BAYER},
77 {SENSOR, PO1030_AUTOCTRL1, PO1030_WEIGHT_WIN_2X},
79 {SENSOR, PO1030_CONTROL2, 0x03},
81 {SENSOR, PO1030_YTARGET, 0x60},
83 {SENSOR, PO1030_OUTFORMCTRL1, PO1030_HREF_ENABLE},
84 {SENSOR, PO1030_EDGE_ENH_OFF, 0x00},
85 {SENSOR, PO1030_EGA, 0x80},
88 {SENSOR, PO1030_GLOBALGAINMAX, 0x14},
89 {SENSOR, PO1030_Cb_U_GAIN, 0x38},
90 {SENSOR, PO1030_Cr_V_GAIN, 0x38},
91 {SENSOR, PO1030_CONTROL1, PO1030_SHUTTER_MODE |
92 PO1030_AUTO_SUBSAMPLING |
94 {SENSOR, PO1030_GC0, 0x10},
95 {SENSOR, PO1030_GC1, 0x20},
96 {SENSOR, PO1030_GC2, 0x40},
97 {SENSOR, PO1030_GC3, 0x60},
98 {SENSOR, PO1030_GC4, 0x80},
99 {SENSOR, PO1030_GC5, 0xa0},
100 {SENSOR, PO1030_GC6, 0xc0},
101 {SENSOR, PO1030_GC7, 0xff},
103 /* Set the width to 751 */
104 {SENSOR, PO1030_FRAMEWIDTH_H, 0x02},
105 {SENSOR, PO1030_FRAMEWIDTH_L, 0xef},
107 /* Set the height to 540 */
108 {SENSOR, PO1030_FRAMEHEIGHT_H, 0x02},
109 {SENSOR, PO1030_FRAMEHEIGHT_L, 0x1c},
111 /* Set the x window to 1 */
112 {SENSOR, PO1030_WINDOWX_H, 0x00},
113 {SENSOR, PO1030_WINDOWX_L, 0x01},
115 /* Set the y window to 1 */
116 {SENSOR, PO1030_WINDOWY_H, 0x00},
117 {SENSOR, PO1030_WINDOWY_L, 0x01},
119 /* with a very low lighted environment increase the exposure but
120 * decrease the FPS (Frame Per Second) */
121 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
122 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
124 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
125 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
126 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
127 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
130 static struct v4l2_pix_format po1030_modes[] = {
136 .sizeimage = 640 * 480,
138 .colorspace = V4L2_COLORSPACE_SRGB,
143 static const struct v4l2_ctrl_ops po1030_ctrl_ops = {
144 .s_ctrl = po1030_s_ctrl,
147 static const struct v4l2_ctrl_config po1030_greenbal_cfg = {
148 .ops = &po1030_ctrl_ops,
149 .id = M5602_V4L2_CID_GREEN_BALANCE,
150 .name = "Green Balance",
151 .type = V4L2_CTRL_TYPE_INTEGER,
155 .def = PO1030_GREEN_GAIN_DEFAULT,
156 .flags = V4L2_CTRL_FLAG_SLIDER,
159 int po1030_probe(struct sd *sd)
163 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
166 if (force_sensor == PO1030_SENSOR) {
167 pr_info("Forcing a %s sensor\n", po1030.name);
170 /* If we want to force another sensor, don't try to probe this
175 PDEBUG(D_PROBE, "Probing for a po1030 sensor");
177 /* Run the pre-init to actually probe the unit */
178 for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
179 u8 data = preinit_po1030[i][2];
180 if (preinit_po1030[i][0] == SENSOR)
181 err = m5602_write_sensor(sd, preinit_po1030[i][1],
184 err = m5602_write_bridge(sd, preinit_po1030[i][1],
190 if (m5602_read_sensor(sd, PO1030_DEVID_H, &dev_id_h, 1))
193 if (dev_id_h == 0x30) {
194 pr_info("Detected a po1030 sensor\n");
200 sd->gspca_dev.cam.cam_mode = po1030_modes;
201 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
206 int po1030_init(struct sd *sd)
210 /* Init the sensor */
211 for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
212 u8 data[2] = {0x00, 0x00};
214 switch (init_po1030[i][0]) {
216 err = m5602_write_bridge(sd,
222 data[0] = init_po1030[i][2];
223 err = m5602_write_sensor(sd,
224 init_po1030[i][1], data, 1);
228 pr_info("Invalid stream command, exiting init\n");
236 po1030_dump_registers(sd);
241 int po1030_init_controls(struct sd *sd)
243 struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
245 sd->gspca_dev.vdev.ctrl_handler = hdl;
246 v4l2_ctrl_handler_init(hdl, 9);
248 sd->auto_white_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
249 V4L2_CID_AUTO_WHITE_BALANCE,
251 sd->green_bal = v4l2_ctrl_new_custom(hdl, &po1030_greenbal_cfg, NULL);
252 sd->red_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
253 V4L2_CID_RED_BALANCE, 0, 255, 1,
254 PO1030_RED_GAIN_DEFAULT);
255 sd->blue_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
256 V4L2_CID_BLUE_BALANCE, 0, 255, 1,
257 PO1030_BLUE_GAIN_DEFAULT);
259 sd->autoexpo = v4l2_ctrl_new_std_menu(hdl, &po1030_ctrl_ops,
260 V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_MANUAL);
261 sd->expo = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_EXPOSURE,
262 0, 0x2ff, 1, PO1030_EXPOSURE_DEFAULT);
264 sd->gain = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_GAIN, 0,
265 0x4f, 1, PO1030_GLOBAL_GAIN_DEFAULT);
267 sd->hflip = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_HFLIP,
269 sd->vflip = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_VFLIP,
273 pr_err("Could not initialize controls\n");
277 v4l2_ctrl_auto_cluster(4, &sd->auto_white_bal, 0, false);
278 v4l2_ctrl_auto_cluster(2, &sd->autoexpo, 0, false);
279 v4l2_ctrl_cluster(2, &sd->hflip);
284 int po1030_start(struct sd *sd)
286 struct cam *cam = &sd->gspca_dev.cam;
288 int width = cam->cam_mode[sd->gspca_dev.curr_mode].width;
289 int height = cam->cam_mode[sd->gspca_dev.curr_mode].height;
290 int ver_offs = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
295 data = PO1030_SUBSAMPLING;
296 err = m5602_write_sensor(sd, PO1030_CONTROL3, &data, 1);
300 data = ((width + 3) >> 8) & 0xff;
301 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_H, &data, 1);
305 data = (width + 3) & 0xff;
306 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_L, &data, 1);
310 data = ((height + 1) >> 8) & 0xff;
311 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_H, &data, 1);
315 data = (height + 1) & 0xff;
316 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_L, &data, 1);
324 err = m5602_write_sensor(sd, PO1030_CONTROL3, &data, 1);
328 data = ((width + 7) >> 8) & 0xff;
329 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_H, &data, 1);
333 data = (width + 7) & 0xff;
334 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_L, &data, 1);
338 data = ((height + 3) >> 8) & 0xff;
339 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_H, &data, 1);
343 data = (height + 3) & 0xff;
344 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_L, &data, 1);
350 err = m5602_write_bridge(sd, M5602_XB_SENSOR_TYPE, 0x0c);
354 err = m5602_write_bridge(sd, M5602_XB_LINE_OF_FRAME_H, 0x81);
358 err = m5602_write_bridge(sd, M5602_XB_PIX_OF_LINE_H, 0x82);
362 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0x01);
366 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA,
367 ((ver_offs >> 8) & 0xff));
371 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (ver_offs & 0xff));
375 for (i = 0; i < 2 && !err; i++)
376 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
380 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height >> 8) & 0xff);
384 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height & 0xff));
388 for (i = 0; i < 2 && !err; i++)
389 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
391 for (i = 0; i < 2 && !err; i++)
392 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
394 for (i = 0; i < 2 && !err; i++)
395 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, 0);
399 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, (width >> 8) & 0xff);
403 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, (width & 0xff));
407 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
411 static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
413 struct sd *sd = (struct sd *) gspca_dev;
417 PDEBUG(D_CONF, "Set exposure to %d", val & 0xffff);
419 i2c_data = ((val & 0xff00) >> 8);
420 PDEBUG(D_CONF, "Set exposure to high byte to 0x%x",
423 err = m5602_write_sensor(sd, PO1030_INTEGLINES_H,
428 i2c_data = (val & 0xff);
429 PDEBUG(D_CONF, "Set exposure to low byte to 0x%x",
431 err = m5602_write_sensor(sd, PO1030_INTEGLINES_M,
437 static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
439 struct sd *sd = (struct sd *) gspca_dev;
443 i2c_data = val & 0xff;
444 PDEBUG(D_CONF, "Set global gain to %d", i2c_data);
445 err = m5602_write_sensor(sd, PO1030_GLOBALGAIN,
450 static int po1030_set_hvflip(struct gspca_dev *gspca_dev)
452 struct sd *sd = (struct sd *) gspca_dev;
456 PDEBUG(D_CONF, "Set hvflip %d %d", sd->hflip->val, sd->vflip->val);
457 err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
461 i2c_data = (0x3f & i2c_data) | (sd->hflip->val << 7) |
462 (sd->vflip->val << 6);
464 err = m5602_write_sensor(sd, PO1030_CONTROL2,
470 static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
472 struct sd *sd = (struct sd *) gspca_dev;
476 i2c_data = val & 0xff;
477 PDEBUG(D_CONF, "Set red gain to %d", i2c_data);
478 err = m5602_write_sensor(sd, PO1030_RED_GAIN,
483 static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
485 struct sd *sd = (struct sd *) gspca_dev;
489 i2c_data = val & 0xff;
490 PDEBUG(D_CONF, "Set blue gain to %d", i2c_data);
491 err = m5602_write_sensor(sd, PO1030_BLUE_GAIN,
497 static int po1030_set_green_balance(struct gspca_dev *gspca_dev, __s32 val)
499 struct sd *sd = (struct sd *) gspca_dev;
503 i2c_data = val & 0xff;
504 PDEBUG(D_CONF, "Set green gain to %d", i2c_data);
506 err = m5602_write_sensor(sd, PO1030_GREEN_1_GAIN,
511 return m5602_write_sensor(sd, PO1030_GREEN_2_GAIN,
515 static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
518 struct sd *sd = (struct sd *) gspca_dev;
522 err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
526 PDEBUG(D_CONF, "Set auto white balance to %d", val);
527 i2c_data = (i2c_data & 0xfe) | (val & 0x01);
528 err = m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
532 static int po1030_set_auto_exposure(struct gspca_dev *gspca_dev,
535 struct sd *sd = (struct sd *) gspca_dev;
539 err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
543 PDEBUG(D_CONF, "Set auto exposure to %d", val);
544 val = (val == V4L2_EXPOSURE_AUTO);
545 i2c_data = (i2c_data & 0xfd) | ((val & 0x01) << 1);
546 return m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
549 void po1030_disconnect(struct sd *sd)
554 static int po1030_s_ctrl(struct v4l2_ctrl *ctrl)
556 struct gspca_dev *gspca_dev =
557 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
558 struct sd *sd = (struct sd *) gspca_dev;
561 if (!gspca_dev->streaming)
565 case V4L2_CID_AUTO_WHITE_BALANCE:
566 err = po1030_set_auto_white_balance(gspca_dev, ctrl->val);
567 if (err || ctrl->val)
569 err = po1030_set_green_balance(gspca_dev, sd->green_bal->val);
572 err = po1030_set_red_balance(gspca_dev, sd->red_bal->val);
575 err = po1030_set_blue_balance(gspca_dev, sd->blue_bal->val);
577 case V4L2_CID_EXPOSURE_AUTO:
578 err = po1030_set_auto_exposure(gspca_dev, ctrl->val);
579 if (err || ctrl->val == V4L2_EXPOSURE_AUTO)
581 err = po1030_set_exposure(gspca_dev, sd->expo->val);
584 err = po1030_set_gain(gspca_dev, ctrl->val);
587 err = po1030_set_hvflip(gspca_dev);
596 static void po1030_dump_registers(struct sd *sd)
601 pr_info("Dumping the po1030 sensor core registers\n");
602 for (address = 0; address < 0x7f; address++) {
603 m5602_read_sensor(sd, address, &value, 1);
604 pr_info("register 0x%x contains 0x%x\n", address, value);
607 pr_info("po1030 register state dump complete\n");
609 pr_info("Probing for which registers that are read/write\n");
610 for (address = 0; address < 0xff; address++) {
611 u8 old_value, ctrl_value;
612 u8 test_value[2] = {0xff, 0xff};
614 m5602_read_sensor(sd, address, &old_value, 1);
615 m5602_write_sensor(sd, address, test_value, 1);
616 m5602_read_sensor(sd, address, &ctrl_value, 1);
618 if (ctrl_value == test_value[0])
619 pr_info("register 0x%x is writeable\n", address);
621 pr_info("register 0x%x is read only\n", address);
623 /* Restore original value */
624 m5602_write_sensor(sd, address, &old_value, 1);