GNU Linux-libre 4.4.287-gnu1
[releases.git] / drivers / media / usb / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include "m5602_po1030.h"
22
23 static int po1030_s_ctrl(struct v4l2_ctrl *ctrl);
24 static void po1030_dump_registers(struct sd *sd);
25
26 static struct v4l2_pix_format po1030_modes[] = {
27         {
28                 640,
29                 480,
30                 V4L2_PIX_FMT_SBGGR8,
31                 V4L2_FIELD_NONE,
32                 .sizeimage = 640 * 480,
33                 .bytesperline = 640,
34                 .colorspace = V4L2_COLORSPACE_SRGB,
35                 .priv = 2
36         }
37 };
38
39 static const struct v4l2_ctrl_ops po1030_ctrl_ops = {
40         .s_ctrl = po1030_s_ctrl,
41 };
42
43 static const struct v4l2_ctrl_config po1030_greenbal_cfg = {
44         .ops    = &po1030_ctrl_ops,
45         .id     = M5602_V4L2_CID_GREEN_BALANCE,
46         .name   = "Green Balance",
47         .type   = V4L2_CTRL_TYPE_INTEGER,
48         .min    = 0,
49         .max    = 255,
50         .step   = 1,
51         .def    = PO1030_GREEN_GAIN_DEFAULT,
52         .flags  = V4L2_CTRL_FLAG_SLIDER,
53 };
54
55 int po1030_probe(struct sd *sd)
56 {
57         u8 dev_id_h = 0, i;
58         int err;
59         struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
60
61         if (force_sensor) {
62                 if (force_sensor == PO1030_SENSOR) {
63                         pr_info("Forcing a %s sensor\n", po1030.name);
64                         goto sensor_found;
65                 }
66                 /* If we want to force another sensor, don't try to probe this
67                  * one */
68                 return -ENODEV;
69         }
70
71         PDEBUG(D_PROBE, "Probing for a po1030 sensor");
72
73         /* Run the pre-init to actually probe the unit */
74         for (i = 0; i < ARRAY_SIZE(preinit_po1030); i++) {
75                 u8 data = preinit_po1030[i][2];
76                 if (preinit_po1030[i][0] == SENSOR)
77                         err = m5602_write_sensor(sd, preinit_po1030[i][1],
78                                                  &data, 1);
79                 else
80                         err = m5602_write_bridge(sd, preinit_po1030[i][1],
81                                                  data);
82                 if (err < 0)
83                         return err;
84         }
85
86         if (m5602_read_sensor(sd, PO1030_DEVID_H, &dev_id_h, 1))
87                 return -ENODEV;
88
89         if (dev_id_h == 0x30) {
90                 pr_info("Detected a po1030 sensor\n");
91                 goto sensor_found;
92         }
93         return -ENODEV;
94
95 sensor_found:
96         sd->gspca_dev.cam.cam_mode = po1030_modes;
97         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(po1030_modes);
98
99         return 0;
100 }
101
102 int po1030_init(struct sd *sd)
103 {
104         int i, err = 0;
105
106         /* Init the sensor */
107         for (i = 0; i < ARRAY_SIZE(init_po1030) && !err; i++) {
108                 u8 data[2] = {0x00, 0x00};
109
110                 switch (init_po1030[i][0]) {
111                 case BRIDGE:
112                         err = m5602_write_bridge(sd,
113                                 init_po1030[i][1],
114                                 init_po1030[i][2]);
115                         break;
116
117                 case SENSOR:
118                         data[0] = init_po1030[i][2];
119                         err = m5602_write_sensor(sd,
120                                 init_po1030[i][1], data, 1);
121                         break;
122
123                 default:
124                         pr_info("Invalid stream command, exiting init\n");
125                         return -EINVAL;
126                 }
127         }
128         if (err < 0)
129                 return err;
130
131         if (dump_sensor)
132                 po1030_dump_registers(sd);
133
134         return 0;
135 }
136
137 int po1030_init_controls(struct sd *sd)
138 {
139         struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
140
141         sd->gspca_dev.vdev.ctrl_handler = hdl;
142         v4l2_ctrl_handler_init(hdl, 9);
143
144         sd->auto_white_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
145                                                V4L2_CID_AUTO_WHITE_BALANCE,
146                                                0, 1, 1, 0);
147         sd->green_bal = v4l2_ctrl_new_custom(hdl, &po1030_greenbal_cfg, NULL);
148         sd->red_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
149                                         V4L2_CID_RED_BALANCE, 0, 255, 1,
150                                         PO1030_RED_GAIN_DEFAULT);
151         sd->blue_bal = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops,
152                                         V4L2_CID_BLUE_BALANCE, 0, 255, 1,
153                                         PO1030_BLUE_GAIN_DEFAULT);
154
155         sd->autoexpo = v4l2_ctrl_new_std_menu(hdl, &po1030_ctrl_ops,
156                           V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_MANUAL);
157         sd->expo = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_EXPOSURE,
158                           0, 0x2ff, 1, PO1030_EXPOSURE_DEFAULT);
159
160         sd->gain = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_GAIN, 0,
161                                      0x4f, 1, PO1030_GLOBAL_GAIN_DEFAULT);
162
163         sd->hflip = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_HFLIP,
164                                       0, 1, 1, 0);
165         sd->vflip = v4l2_ctrl_new_std(hdl, &po1030_ctrl_ops, V4L2_CID_VFLIP,
166                                       0, 1, 1, 0);
167
168         if (hdl->error) {
169                 pr_err("Could not initialize controls\n");
170                 return hdl->error;
171         }
172
173         v4l2_ctrl_auto_cluster(4, &sd->auto_white_bal, 0, false);
174         v4l2_ctrl_auto_cluster(2, &sd->autoexpo, 0, false);
175         v4l2_ctrl_cluster(2, &sd->hflip);
176
177         return 0;
178 }
179
180 int po1030_start(struct sd *sd)
181 {
182         struct cam *cam = &sd->gspca_dev.cam;
183         int i, err = 0;
184         int width = cam->cam_mode[sd->gspca_dev.curr_mode].width;
185         int height = cam->cam_mode[sd->gspca_dev.curr_mode].height;
186         int ver_offs = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
187         u8 data;
188
189         switch (width) {
190         case 320:
191                 data = PO1030_SUBSAMPLING;
192                 err = m5602_write_sensor(sd, PO1030_CONTROL3, &data, 1);
193                 if (err < 0)
194                         return err;
195
196                 data = ((width + 3) >> 8) & 0xff;
197                 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_H, &data, 1);
198                 if (err < 0)
199                         return err;
200
201                 data = (width + 3) & 0xff;
202                 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_L, &data, 1);
203                 if (err < 0)
204                         return err;
205
206                 data = ((height + 1) >> 8) & 0xff;
207                 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_H, &data, 1);
208                 if (err < 0)
209                         return err;
210
211                 data = (height + 1) & 0xff;
212                 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_L, &data, 1);
213
214                 height += 6;
215                 width -= 1;
216                 break;
217
218         case 640:
219                 data = 0;
220                 err = m5602_write_sensor(sd, PO1030_CONTROL3, &data, 1);
221                 if (err < 0)
222                         return err;
223
224                 data = ((width + 7) >> 8) & 0xff;
225                 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_H, &data, 1);
226                 if (err < 0)
227                         return err;
228
229                 data = (width + 7) & 0xff;
230                 err = m5602_write_sensor(sd, PO1030_WINDOWWIDTH_L, &data, 1);
231                 if (err < 0)
232                         return err;
233
234                 data = ((height + 3) >> 8) & 0xff;
235                 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_H, &data, 1);
236                 if (err < 0)
237                         return err;
238
239                 data = (height + 3) & 0xff;
240                 err = m5602_write_sensor(sd, PO1030_WINDOWHEIGHT_L, &data, 1);
241
242                 height += 12;
243                 width -= 2;
244                 break;
245         }
246         err = m5602_write_bridge(sd, M5602_XB_SENSOR_TYPE, 0x0c);
247         if (err < 0)
248                 return err;
249
250         err = m5602_write_bridge(sd, M5602_XB_LINE_OF_FRAME_H, 0x81);
251         if (err < 0)
252                 return err;
253
254         err = m5602_write_bridge(sd, M5602_XB_PIX_OF_LINE_H, 0x82);
255         if (err < 0)
256                 return err;
257
258         err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0x01);
259         if (err < 0)
260                 return err;
261
262         err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA,
263                                  ((ver_offs >> 8) & 0xff));
264         if (err < 0)
265                 return err;
266
267         err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (ver_offs & 0xff));
268         if (err < 0)
269                 return err;
270
271         for (i = 0; i < 2 && !err; i++)
272                 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
273         if (err < 0)
274                 return err;
275
276         err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height >> 8) & 0xff);
277         if (err < 0)
278                 return err;
279
280         err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height & 0xff));
281         if (err < 0)
282                 return err;
283
284         for (i = 0; i < 2 && !err; i++)
285                 err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
286
287         for (i = 0; i < 2 && !err; i++)
288                 err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
289
290         for (i = 0; i < 2 && !err; i++)
291                 err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, 0);
292         if (err < 0)
293                 return err;
294
295         err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, (width >> 8) & 0xff);
296         if (err < 0)
297                 return err;
298
299         err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, (width & 0xff));
300         if (err < 0)
301                 return err;
302
303         err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
304         return err;
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         u8 i2c_data;
311         int err;
312
313         PDEBUG(D_CONF, "Set exposure to %d", val & 0xffff);
314
315         i2c_data = ((val & 0xff00) >> 8);
316         PDEBUG(D_CONF, "Set exposure to high byte to 0x%x",
317                i2c_data);
318
319         err = m5602_write_sensor(sd, PO1030_INTEGLINES_H,
320                                   &i2c_data, 1);
321         if (err < 0)
322                 return err;
323
324         i2c_data = (val & 0xff);
325         PDEBUG(D_CONF, "Set exposure to low byte to 0x%x",
326                i2c_data);
327         err = m5602_write_sensor(sd, PO1030_INTEGLINES_M,
328                                   &i2c_data, 1);
329
330         return err;
331 }
332
333 static int po1030_set_gain(struct gspca_dev *gspca_dev, __s32 val)
334 {
335         struct sd *sd = (struct sd *) gspca_dev;
336         u8 i2c_data;
337         int err;
338
339         i2c_data = val & 0xff;
340         PDEBUG(D_CONF, "Set global gain to %d", i2c_data);
341         err = m5602_write_sensor(sd, PO1030_GLOBALGAIN,
342                                  &i2c_data, 1);
343         return err;
344 }
345
346 static int po1030_set_hvflip(struct gspca_dev *gspca_dev)
347 {
348         struct sd *sd = (struct sd *) gspca_dev;
349         u8 i2c_data;
350         int err;
351
352         PDEBUG(D_CONF, "Set hvflip %d %d", sd->hflip->val, sd->vflip->val);
353         err = m5602_read_sensor(sd, PO1030_CONTROL2, &i2c_data, 1);
354         if (err < 0)
355                 return err;
356
357         i2c_data = (0x3f & i2c_data) | (sd->hflip->val << 7) |
358                    (sd->vflip->val << 6);
359
360         err = m5602_write_sensor(sd, PO1030_CONTROL2,
361                                  &i2c_data, 1);
362
363         return err;
364 }
365
366 static int po1030_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
367 {
368         struct sd *sd = (struct sd *) gspca_dev;
369         u8 i2c_data;
370         int err;
371
372         i2c_data = val & 0xff;
373         PDEBUG(D_CONF, "Set red gain to %d", i2c_data);
374         err = m5602_write_sensor(sd, PO1030_RED_GAIN,
375                                   &i2c_data, 1);
376         return err;
377 }
378
379 static int po1030_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
380 {
381         struct sd *sd = (struct sd *) gspca_dev;
382         u8 i2c_data;
383         int err;
384
385         i2c_data = val & 0xff;
386         PDEBUG(D_CONF, "Set blue gain to %d", i2c_data);
387         err = m5602_write_sensor(sd, PO1030_BLUE_GAIN,
388                                   &i2c_data, 1);
389
390         return err;
391 }
392
393 static int po1030_set_green_balance(struct gspca_dev *gspca_dev, __s32 val)
394 {
395         struct sd *sd = (struct sd *) gspca_dev;
396         u8 i2c_data;
397         int err;
398
399         i2c_data = val & 0xff;
400         PDEBUG(D_CONF, "Set green gain to %d", i2c_data);
401
402         err = m5602_write_sensor(sd, PO1030_GREEN_1_GAIN,
403                            &i2c_data, 1);
404         if (err < 0)
405                 return err;
406
407         return m5602_write_sensor(sd, PO1030_GREEN_2_GAIN,
408                                  &i2c_data, 1);
409 }
410
411 static int po1030_set_auto_white_balance(struct gspca_dev *gspca_dev,
412                                          __s32 val)
413 {
414         struct sd *sd = (struct sd *) gspca_dev;
415         u8 i2c_data;
416         int err;
417
418         err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
419         if (err < 0)
420                 return err;
421
422         PDEBUG(D_CONF, "Set auto white balance to %d", val);
423         i2c_data = (i2c_data & 0xfe) | (val & 0x01);
424         err = m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
425         return err;
426 }
427
428 static int po1030_set_auto_exposure(struct gspca_dev *gspca_dev,
429                                     __s32 val)
430 {
431         struct sd *sd = (struct sd *) gspca_dev;
432         u8 i2c_data;
433         int err;
434
435         err = m5602_read_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
436         if (err < 0)
437                 return err;
438
439         PDEBUG(D_CONF, "Set auto exposure to %d", val);
440         val = (val == V4L2_EXPOSURE_AUTO);
441         i2c_data = (i2c_data & 0xfd) | ((val & 0x01) << 1);
442         return m5602_write_sensor(sd, PO1030_AUTOCTRL1, &i2c_data, 1);
443 }
444
445 void po1030_disconnect(struct sd *sd)
446 {
447         sd->sensor = NULL;
448 }
449
450 static int po1030_s_ctrl(struct v4l2_ctrl *ctrl)
451 {
452         struct gspca_dev *gspca_dev =
453                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
454         struct sd *sd = (struct sd *) gspca_dev;
455         int err;
456
457         if (!gspca_dev->streaming)
458                 return 0;
459
460         switch (ctrl->id) {
461         case V4L2_CID_AUTO_WHITE_BALANCE:
462                 err = po1030_set_auto_white_balance(gspca_dev, ctrl->val);
463                 if (err || ctrl->val)
464                         return err;
465                 err = po1030_set_green_balance(gspca_dev, sd->green_bal->val);
466                 if (err)
467                         return err;
468                 err = po1030_set_red_balance(gspca_dev, sd->red_bal->val);
469                 if (err)
470                         return err;
471                 err = po1030_set_blue_balance(gspca_dev, sd->blue_bal->val);
472                 break;
473         case V4L2_CID_EXPOSURE_AUTO:
474                 err = po1030_set_auto_exposure(gspca_dev, ctrl->val);
475                 if (err || ctrl->val == V4L2_EXPOSURE_AUTO)
476                         return err;
477                 err = po1030_set_exposure(gspca_dev, sd->expo->val);
478                 break;
479         case V4L2_CID_GAIN:
480                 err = po1030_set_gain(gspca_dev, ctrl->val);
481                 break;
482         case V4L2_CID_HFLIP:
483                 err = po1030_set_hvflip(gspca_dev);
484                 break;
485         default:
486                 return -EINVAL;
487         }
488
489         return err;
490 }
491
492 static void po1030_dump_registers(struct sd *sd)
493 {
494         int address;
495         u8 value = 0;
496
497         pr_info("Dumping the po1030 sensor core registers\n");
498         for (address = 0; address < 0x7f; address++) {
499                 m5602_read_sensor(sd, address, &value, 1);
500                 pr_info("register 0x%x contains 0x%x\n", address, value);
501         }
502
503         pr_info("po1030 register state dump complete\n");
504
505         pr_info("Probing for which registers that are read/write\n");
506         for (address = 0; address < 0xff; address++) {
507                 u8 old_value, ctrl_value;
508                 u8 test_value[2] = {0xff, 0xff};
509
510                 m5602_read_sensor(sd, address, &old_value, 1);
511                 m5602_write_sensor(sd, address, test_value, 1);
512                 m5602_read_sensor(sd, address, &ctrl_value, 1);
513
514                 if (ctrl_value == test_value[0])
515                         pr_info("register 0x%x is writeable\n", address);
516                 else
517                         pr_info("register 0x%x is read only\n", address);
518
519                 /* Restore original value */
520                 m5602_write_sensor(sd, address, &old_value, 1);
521         }
522 }