]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/usb/gspca/m5602/m5602_po1030.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / drivers / media / usb / gspca / m5602 / m5602_po1030.c
CommitLineData
a10e763b 1// SPDX-License-Identifier: GPL-2.0-only
c109f816
EA
2/*
3 * Driver for the po1030 sensor
4 *
0c505e68 5 * Copyright (c) 2008 Erik Andrén
c109f816
EA
6 * Copyright (c) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
7 * Copyright (c) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
8 *
9 * Portions of code to USB interface and ALi driver software,
10 * Copyright (c) 2006 Willem Duinker
11 * v4l2 interface modeled after the V4L2 driver
12 * for SN9C10x PC Camera Controllers
c109f816
EA
13 */
14
bdfe91f4
JP
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
c109f816
EA
17#include "m5602_po1030.h"
18
c84e412f
HG
19static int po1030_s_ctrl(struct v4l2_ctrl *ctrl);
20static void po1030_dump_registers(struct sd *sd);
cf811d50 21
804c7812
MCC
22static const unsigned char preinit_po1030[][3] = {
23 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
24 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
25 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
26 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
27 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
28 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
29 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
30 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
31 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
32 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
33 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
34 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
35 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
36
37 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
38
39 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
40 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
41 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
42 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
43 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
44 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
45 {BRIDGE, M5602_XB_GPIO_DAT, 0x00}
46};
47
48static const unsigned char init_po1030[][3] = {
49 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
50 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
51 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
52 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
53 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
54 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
55 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
56
57 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
58
59 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
60 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
61 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
62 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
63 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
64 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
65 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
66 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
67 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
68 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
69
70 {SENSOR, PO1030_AUTOCTRL2, 0x04},
71
72 {SENSOR, PO1030_OUTFORMCTRL2, PO1030_RAW_RGB_BAYER},
73 {SENSOR, PO1030_AUTOCTRL1, PO1030_WEIGHT_WIN_2X},
74
75 {SENSOR, PO1030_CONTROL2, 0x03},
76 {SENSOR, 0x21, 0x90},
77 {SENSOR, PO1030_YTARGET, 0x60},
78 {SENSOR, 0x59, 0x13},
79 {SENSOR, PO1030_OUTFORMCTRL1, PO1030_HREF_ENABLE},
80 {SENSOR, PO1030_EDGE_ENH_OFF, 0x00},
81 {SENSOR, PO1030_EGA, 0x80},
82 {SENSOR, 0x78, 0x14},
83 {SENSOR, 0x6f, 0x01},
84 {SENSOR, PO1030_GLOBALGAINMAX, 0x14},
85 {SENSOR, PO1030_Cb_U_GAIN, 0x38},
86 {SENSOR, PO1030_Cr_V_GAIN, 0x38},
87 {SENSOR, PO1030_CONTROL1, PO1030_SHUTTER_MODE |
88 PO1030_AUTO_SUBSAMPLING |
89 PO1030_FRAME_EQUAL},
90 {SENSOR, PO1030_GC0, 0x10},
91 {SENSOR, PO1030_GC1, 0x20},
92 {SENSOR, PO1030_GC2, 0x40},
93 {SENSOR, PO1030_GC3, 0x60},
94 {SENSOR, PO1030_GC4, 0x80},
95 {SENSOR, PO1030_GC5, 0xa0},
96 {SENSOR, PO1030_GC6, 0xc0},
97 {SENSOR, PO1030_GC7, 0xff},
98
99 /* Set the width to 751 */
100 {SENSOR, PO1030_FRAMEWIDTH_H, 0x02},
101 {SENSOR, PO1030_FRAMEWIDTH_L, 0xef},
102
103 /* Set the height to 540 */
104 {SENSOR, PO1030_FRAMEHEIGHT_H, 0x02},
105 {SENSOR, PO1030_FRAMEHEIGHT_L, 0x1c},
106
107 /* Set the x window to 1 */
108 {SENSOR, PO1030_WINDOWX_H, 0x00},
109 {SENSOR, PO1030_WINDOWX_L, 0x01},
110
111 /* Set the y window to 1 */
112 {SENSOR, PO1030_WINDOWY_H, 0x00},
113 {SENSOR, PO1030_WINDOWY_L, 0x01},
114
115 /* with a very low lighted environment increase the exposure but
116 * decrease the FPS (Frame Per Second) */
117 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
118 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
119
120 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
121 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
122 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
123 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
124};
125
74cadfe1
EA
126static struct v4l2_pix_format po1030_modes[] = {
127 {
128 640,
129 480,
130 V4L2_PIX_FMT_SBGGR8,
131 V4L2_FIELD_NONE,
132 .sizeimage = 640 * 480,
133 .bytesperline = 640,
134 .colorspace = V4L2_COLORSPACE_SRGB,
9536a57e 135 .priv = 2
74cadfe1
EA
136 }
137};
138
c84e412f
HG
139static const struct v4l2_ctrl_ops po1030_ctrl_ops = {
140 .s_ctrl = po1030_s_ctrl,
e17cc08c
EA
141};
142
c84e412f
HG
143static const struct v4l2_ctrl_config po1030_greenbal_cfg = {
144 .ops = &po1030_ctrl_ops,
145 .id = M5602_V4L2_CID_GREEN_BALANCE,
146 .name = "Green Balance",
147 .type = V4L2_CTRL_TYPE_INTEGER,
148 .min = 0,
149 .max = 255,
150 .step = 1,
151 .def = PO1030_GREEN_GAIN_DEFAULT,
152 .flags = V4L2_CTRL_FLAG_SLIDER,
153};
658efb63 154
c109f816
EA
155int po1030_probe(struct sd *sd)
156{
a21a0eb5 157 int rc = 0;
3e5cbad0 158 u8 dev_id_h = 0, i;
c93396e1 159 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
c109f816
EA
160
161 if (force_sensor) {
162 if (force_sensor == PO1030_SENSOR) {
bdfe91f4 163 pr_info("Forcing a %s sensor\n", po1030.name);
c109f816
EA
164 goto sensor_found;
165 }
166 /* If we want to force another sensor, don't try to probe this
167 * one */
168 return -ENODEV;
169 }
170
37d5efb0 171 gspca_dbg(gspca_dev, D_PROBE, "Probing for a po1030 sensor\n");
c109f816
EA
172
173 /* Run the pre-init to actually probe the unit */
174 for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
175 u8 data = preinit_po1030[i][2];
176 if (preinit_po1030[i][0] == SENSOR)
a21a0eb5 177 rc |= m5602_write_sensor(sd,
c109f816
EA
178 preinit_po1030[i][1], &data, 1);
179 else
a21a0eb5
AP
180 rc |= m5602_write_bridge(sd, preinit_po1030[i][1],
181 data);
c109f816 182 }
a21a0eb5
AP
183 if (rc < 0)
184 return rc;
c109f816 185
fcb98108 186 if (m5602_read_sensor(sd, PO1030_DEVID_H, &dev_id_h, 1))
c109f816
EA
187 return -ENODEV;
188
3e5cbad0 189 if (dev_id_h == 0x30) {
bdfe91f4 190 pr_info("Detected a po1030 sensor\n");
c109f816
EA
191 goto sensor_found;
192 }
193 return -ENODEV;
194
195sensor_found:
74cadfe1
EA
196 sd->gspca_dev.cam.cam_mode = po1030_modes;
197 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
cb29e691 198
c109f816
EA
199 return 0;
200}
201
c109f816
EA
202int po1030_init(struct sd *sd)
203{
204 int i, err = 0;
205
206 /* Init the sensor */
7b9f2467 207 for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
c109f816
EA
208 u8 data[2] = {0x00, 0x00};
209
210 switch (init_po1030[i][0]) {
211 case BRIDGE:
212 err = m5602_write_bridge(sd,
213 init_po1030[i][1],
214 init_po1030[i][2]);
215 break;
216
217 case SENSOR:
218 data[0] = init_po1030[i][2];
6dc4cff0 219 err = m5602_write_sensor(sd,
c109f816
EA
220 init_po1030[i][1], data, 1);
221 break;
222
c109f816 223 default:
bdfe91f4 224 pr_info("Invalid stream command, exiting init\n");
c109f816
EA
225 return -EINVAL;
226 }
227 }
cb29e691
EA
228 if (err < 0)
229 return err;
c109f816 230
36e756c5
EA
231 if (dump_sensor)
232 po1030_dump_registers(sd);
233
c84e412f
HG
234 return 0;
235}
3b9ae658 236
c84e412f
HG
237int po1030_init_controls(struct sd *sd)
238{
239 struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
240
241 sd->gspca_dev.vdev.ctrl_handler = hdl;
242 v4l2_ctrl_handler_init(hdl, 9);
243
244 sd->auto_white_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
245 V4L2_CID_AUTO_WHITE_BALANCE,
246 0, 1, 1, 0);
247 sd->green_bal = v4l2_ctrl_new_custom(hdl, &po1030_greenbal_cfg, NULL);
248 sd->red_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
249 V4L2_CID_RED_BALANCE, 0, 255, 1,
250 PO1030_RED_GAIN_DEFAULT);
251 sd->blue_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
252 V4L2_CID_BLUE_BALANCE, 0, 255, 1,
253 PO1030_BLUE_GAIN_DEFAULT);
254
255 sd->autoexpo = v4l2_ctrl_new_std_menu(hdl, &po1030_ctrl_ops,
256 V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_MANUAL);
257 sd->expo = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_EXPOSURE,
258 0, 0x2ff, 1, PO1030_EXPOSURE_DEFAULT);
259
260 sd->gain = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_GAIN, 0,
261 0x4f, 1, PO1030_GLOBAL_GAIN_DEFAULT);
262
263 sd->hflip = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_HFLIP,
264 0, 1, 1, 0);
265 sd->vflip = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_VFLIP,
266 0, 1, 1, 0);
267
268 if (hdl->error) {
269 pr_err("Could not initialize controls\n");
270 return hdl->error;
271 }
b933d585 272
c84e412f
HG
273 v4l2_ctrl_auto_cluster(4, &sd->auto_white_bal, 0, false);
274 v4l2_ctrl_auto_cluster(2, &sd->autoexpo, 0, false);
275 v4l2_ctrl_cluster(2, &sd->hflip);
dd9ce84a 276
c84e412f 277 return 0;
c109f816
EA
278}
279
c996b368
EA
280int po1030_start(struct sd *sd)
281{
e807f20d 282 struct cam *cam = &sd->gspca_dev.cam;
c996b368 283 int i, err = 0;
e807f20d
EA
284 int width = cam->cam_mode[sd->gspca_dev.curr_mode].width;
285 int height = cam->cam_mode[sd->gspca_dev.curr_mode].height;
9536a57e 286 int ver_offs = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
e807f20d
EA
287 u8 data;
288
289 switch (width) {
931a1c8d
EA
290 case 320:
291 data = PO1030_SUBSAMPLING;
292 err = m5602_write_sensor(sd, PO1030_CONTROL3, &data, 1);
293 if (err < 0)
294 return err;
295
296 data = ((width + 3) >> 8) & 0xff;
297 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_H, &data, 1);
298 if (err < 0)
299 return err;
300
301 data = (width + 3) & 0xff;
302 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_L, &data, 1);
303 if (err < 0)
304 return err;
305
306 data = ((height + 1) >> 8) & 0xff;
307 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_H, &data, 1);
308 if (err < 0)
309 return err;
310
311 data = (height + 1) & 0xff;
312 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_L, &data, 1);
313
314 height += 6;
315 width -= 1;
316 break;
317
e807f20d 318 case 640:
894e4087
EA
319 data = 0;
320 err = m5602_write_sensor(sd, PO1030_CONTROL3, &data, 1);
321 if (err < 0)
322 return err;
323
e807f20d
EA
324 data = ((width + 7) >> 8) & 0xff;
325 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_H, &data, 1);
326 if (err < 0)
327 return err;
328
329 data = (width + 7) & 0xff;
330 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_L, &data, 1);
331 if (err < 0)
332 return err;
333
334 data = ((height + 3) >> 8) & 0xff;
335 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_H, &data, 1);
336 if (err < 0)
337 return err;
338
339 data = (height + 3) & 0xff;
340 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_L, &data, 1);
9536a57e
EA
341
342 height += 12;
343 width -= 2;
e807f20d
EA
344 break;
345 }
98192670
EA
346 err = m5602_write_bridge(sd, M5602_XB_SENSOR_TYPE, 0x0c);
347 if (err < 0)
348 return err;
e807f20d 349
9536a57e
EA
350 err = m5602_write_bridge(sd, M5602_XB_LINE_OF_FRAME_H, 0x81);
351 if (err < 0)
352 return err;
353
354 err = m5602_write_bridge(sd, M5602_XB_PIX_OF_LINE_H, 0x82);
355 if (err < 0)
356 return err;
357
98192670
EA
358 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0x01);
359 if (err < 0)
360 return err;
361
9536a57e
EA
362 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA,
363 ((ver_offs >> 8) & 0xff));
364 if (err < 0)
365 return err;
366
367 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (ver_offs & 0xff));
368 if (err < 0)
369 return err;
370
98192670
EA
371 for (i = 0; i < 2 && !err; i++)
372 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
9536a57e
EA
373 if (err < 0)
374 return err;
375
376 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height >> 8) & 0xff);
377 if (err < 0)
378 return err;
379
380 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height & 0xff));
381 if (err < 0)
382 return err;
383
384 for (i = 0; i < 2 && !err; i++)
385 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
386
98192670
EA
387 for (i = 0; i < 2 && !err; i++)
388 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
389
9536a57e
EA
390 for (i = 0; i < 2 && !err; i++)
391 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, 0);
392 if (err < 0)
393 return err;
394
395 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, (width >> 8) & 0xff);
396 if (err < 0)
397 return err;
398
399 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, (width & 0xff));
98192670
EA
400 if (err < 0)
401 return err;
402
403 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
c996b368
EA
404 return err;
405}
406
cf811d50 407static int po1030_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
c109f816
EA
408{
409 struct sd *sd = (struct sd *) gspca_dev;
410 u8 i2c_data;
411 int err;
412
37d5efb0 413 gspca_dbg(gspca_dev, D_CONF, "Set exposure to %d\n", val & 0xffff);
c109f816
EA
414
415 i2c_data = ((val & 0xff00) >> 8);
37d5efb0
JP
416 gspca_dbg(gspca_dev, D_CONF, "Set exposure to high byte to 0x%x\n",
417 i2c_data);
c109f816 418
bb9460eb 419 err = m5602_write_sensor(sd, PO1030_INTEGLINES_H,
c109f816
EA
420 &i2c_data, 1);
421 if (err < 0)
051781b3 422 return err;
c109f816
EA
423
424 i2c_data = (val & 0xff);
37d5efb0
JP
425 gspca_dbg(gspca_dev, D_CONF, "Set exposure to low byte to 0x%x\n",
426 i2c_data);
bb9460eb 427 err = m5602_write_sensor(sd, PO1030_INTEGLINES_M,
c109f816
EA
428 &i2c_data, 1);
429
7b9f2467 430 return err;
c109f816
EA
431}
432
cf811d50 433static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
e2c97491
EA
434{
435 struct sd *sd = (struct sd *) gspca_dev;
436 u8 i2c_data;
437 int err;
438
05d7d9ce 439 i2c_data = val & 0xff;
37d5efb0 440 gspca_dbg(gspca_dev, D_CONF, "Set global gain to %d\n", i2c_data);
bb9460eb 441 err = m5602_write_sensor(sd, PO1030_GLOBALGAIN,
e2c97491 442 &i2c_data, 1);
05d7d9ce
EA
443 return err;
444}
e2c97491 445
c84e412f 446static int po1030_set_hvflip(struct gspca_dev *gspca_dev)
e2c97491
EA
447{
448 struct sd *sd = (struct sd *) gspca_dev;
e2c97491
EA
449 u8 i2c_data;
450 int err;
451
37d5efb0
JP
452 gspca_dbg(gspca_dev, D_CONF, "Set hvflip %d %d\n",
453 sd->hflip->val, sd->vflip->val);
bb9460eb 454 err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
1d07b6f1 455 if (err < 0)
051781b3 456 return err;
e2c97491 457
c84e412f
HG
458 i2c_data = (0x3f & i2c_data) | (sd->hflip->val << 7) |
459 (sd->vflip->val << 6);
e2c97491 460
bb9460eb 461 err = m5602_write_sensor(sd, PO1030_CONTROL2,
1d07b6f1 462 &i2c_data, 1);
e2c97491 463
7b9f2467 464 return err;
e2c97491
EA
465}
466
cf811d50 467static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
c109f816
EA
468{
469 struct sd *sd = (struct sd *) gspca_dev;
470 u8 i2c_data;
471 int err;
472
473 i2c_data = val & 0xff;
37d5efb0 474 gspca_dbg(gspca_dev, D_CONF, "Set red gain to %d\n", i2c_data);
bb9460eb 475 err = m5602_write_sensor(sd, PO1030_RED_GAIN,
c109f816 476 &i2c_data, 1);
7b9f2467 477 return err;
c109f816
EA
478}
479
cf811d50 480static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
c109f816
EA
481{
482 struct sd *sd = (struct sd *) gspca_dev;
483 u8 i2c_data;
484 int err;
05d7d9ce 485
c109f816 486 i2c_data = val & 0xff;
37d5efb0 487 gspca_dbg(gspca_dev, D_CONF, "Set blue gain to %d\n", i2c_data);
bb9460eb 488 err = m5602_write_sensor(sd, PO1030_BLUE_GAIN,
c109f816
EA
489 &i2c_data, 1);
490
7b9f2467 491 return err;
c109f816
EA
492}
493
b933d585
EA
494static int po1030_set_green_balance(struct gspca_dev *gspca_dev, __s32 val)
495{
496 struct sd *sd = (struct sd *) gspca_dev;
b933d585
EA
497 u8 i2c_data;
498 int err;
499
b933d585 500 i2c_data = val & 0xff;
37d5efb0 501 gspca_dbg(gspca_dev, D_CONF, "Set green gain to %d\n", i2c_data);
b933d585
EA
502
503 err = m5602_write_sensor(sd, PO1030_GREEN_1_GAIN,
504 &i2c_data, 1);
505 if (err < 0)
506 return err;
507
508 return m5602_write_sensor(sd, PO1030_GREEN_2_GAIN,
509 &i2c_data, 1);
510}
511
cf811d50
EA
512static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
513 __s32 val)
3b9ae658
EA
514{
515 struct sd *sd = (struct sd *) gspca_dev;
3b9ae658
EA
516 u8 i2c_data;
517 int err;
518
3b9ae658
EA
519 err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
520 if (err < 0)
521 return err;
522
37d5efb0 523 gspca_dbg(gspca_dev, D_CONF, "Set auto white balance to %d\n", val);
3b9ae658
EA
524 i2c_data = (i2c_data & 0xfe) | (val & 0x01);
525 err = m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
526 return err;
527}
528
dd9ce84a
EA
529static int po1030_set_auto_exposure(struct gspca_dev *gspca_dev,
530 __s32 val)
531{
532 struct sd *sd = (struct sd *) gspca_dev;
dd9ce84a
EA
533 u8 i2c_data;
534 int err;
535
dd9ce84a
EA
536 err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
537 if (err < 0)
538 return err;
539
37d5efb0 540 gspca_dbg(gspca_dev, D_CONF, "Set auto exposure to %d\n", val);
c84e412f 541 val = (val == V4L2_EXPOSURE_AUTO);
dd9ce84a
EA
542 i2c_data = (i2c_data & 0xfd) | ((val & 0x01) << 1);
543 return m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
544}
545
05d7d9ce
EA
546void po1030_disconnect(struct sd *sd)
547{
548 sd->sensor = NULL;
c84e412f
HG
549}
550
551static int po1030_s_ctrl(struct v4l2_ctrl *ctrl)
552{
553 struct gspca_dev *gspca_dev =
554 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
555 struct sd *sd = (struct sd *) gspca_dev;
556 int err;
557
558 if (!gspca_dev->streaming)
559 return 0;
560
561 switch (ctrl->id) {
562 case V4L2_CID_AUTO_WHITE_BALANCE:
563 err = po1030_set_auto_white_balance(gspca_dev, ctrl->val);
564 if (err || ctrl->val)
565 return err;
566 err = po1030_set_green_balance(gspca_dev, sd->green_bal->val);
567 if (err)
568 return err;
569 err = po1030_set_red_balance(gspca_dev, sd->red_bal->val);
570 if (err)
571 return err;
572 err = po1030_set_blue_balance(gspca_dev, sd->blue_bal->val);
573 break;
574 case V4L2_CID_EXPOSURE_AUTO:
575 err = po1030_set_auto_exposure(gspca_dev, ctrl->val);
576 if (err || ctrl->val == V4L2_EXPOSURE_AUTO)
577 return err;
578 err = po1030_set_exposure(gspca_dev, sd->expo->val);
579 break;
580 case V4L2_CID_GAIN:
581 err = po1030_set_gain(gspca_dev, ctrl->val);
582 break;
583 case V4L2_CID_HFLIP:
584 err = po1030_set_hvflip(gspca_dev);
585 break;
586 default:
587 return -EINVAL;
588 }
589
590 return err;
05d7d9ce
EA
591}
592
658efb63 593static void po1030_dump_registers(struct sd *sd)
c109f816
EA
594{
595 int address;
596 u8 value = 0;
597
bdfe91f4 598 pr_info("Dumping the po1030 sensor core registers\n");
c109f816 599 for (address = 0; address < 0x7f; address++) {
c061c97e 600 m5602_read_sensor(sd, address, &value, 1);
bdfe91f4 601 pr_info("register 0x%x contains 0x%x\n", address, value);
c109f816
EA
602 }
603
bdfe91f4 604 pr_info("po1030 register state dump complete\n");
c109f816 605
bdfe91f4 606 pr_info("Probing for which registers that are read/write\n");
c109f816
EA
607 for (address = 0; address < 0xff; address++) {
608 u8 old_value, ctrl_value;
609 u8 test_value[2] = {0xff, 0xff};
610
c061c97e 611 m5602_read_sensor(sd, address, &old_value, 1);
6dc4cff0 612 m5602_write_sensor(sd, address, test_value, 1);
c061c97e 613 m5602_read_sensor(sd, address, &ctrl_value, 1);
c109f816
EA
614
615 if (ctrl_value == test_value[0])
bdfe91f4 616 pr_info("register 0x%x is writeable\n", address);
c109f816 617 else
bdfe91f4 618 pr_info("register 0x%x is read only\n", address);
c109f816
EA
619
620 /* Restore original value */
6dc4cff0 621 m5602_write_sensor(sd, address, &old_value, 1);
c109f816
EA
622 }
623}