22
22
/* Some documentation about various registers as determined by trial and error.
23
When the register addresses differ between the 7202 and the 7311 the 2
24
different addresses are written as 7302addr/7311addr, when one of the 2
25
addresses is a - sign that register description is not valid for the
31
-/0x08 Unknown compressor related, must always be 8 except when not
32
in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
33
-/0x1b Auto white balance related, bit 0 is AWB enable (inverted)
34
bits 345 seem to toggle per color gains on/off (inverted)
35
0x78 Global control, bit 6 controls the LED (inverted)
36
-/0x80 JPEG compression ratio ? Best not touched
41
0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
42
the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
43
-/0x0f Master gain 1-245, low value = high gain
44
0x10/- Master gain 0-31
45
-/0x10 Another gain 0-15, limited influence (1-2x gain I guess)
46
0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
47
-/0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
48
completely disable the analog amplification block. Set to 0x68
49
for max gain, 0x14 for minimal gain.
27
* 0x08 Unknown compressor related, must always be 8 except when not
28
* in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
29
* 0x1b Auto white balance related, bit 0 is AWB enable (inverted)
30
* bits 345 seem to toggle per color gains on/off (inverted)
31
* 0x78 Global control, bit 6 controls the LED (inverted)
32
* 0x80 Compression balance, interesting settings:
33
* 0x01 Use this to allow the camera to switch to higher compr.
34
* on the fly. Needed to stay within bandwidth @ 640x480@30
35
* 0x1c From usb captures under Windows for 640x480
36
* 0x2a Values >= this switch the camera to a lower compression,
37
* using the same table for both luminance and chrominance.
38
* This gives a sharper picture. Usable only at 640x480@ <
39
* 15 fps or 320x240 / 160x120. Note currently the driver
40
* does not use this as the quality gain is small and the
41
* generated JPG-s are only understood by v4l-utils >= 0.8.9
42
* 0x3f From usb captures under Windows for 320x240
43
* 0x69 From usb captures under Windows for 160x120
48
* 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
49
* the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
50
* 0x0f Master gain 1-245, low value = high gain
51
* 0x10 Another gain 0-15, limited influence (1-2x gain I guess)
52
* 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
53
* Note setting vflip disabled leads to a much lower image quality,
54
* so we always vflip, and tell userspace to flip it back
55
* 0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
56
* completely disable the analog amplification block. Set to 0x68
57
* for max gain, 0x14 for minimal gain.
52
60
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
80
/* V4L2 controls supported by the driver */
81
static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
82
static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
83
static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
84
static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
85
static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
86
static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
87
static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
88
static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
89
static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
90
static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
91
static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
92
static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
94
static const struct ctrl sd_ctrls[] = {
95
/* This control is for both the 7302 and the 7311 */
98
.id = V4L2_CID_CONTRAST,
99
.type = V4L2_CTRL_TYPE_INTEGER,
102
#define CONTRAST_MAX 255
103
.maximum = CONTRAST_MAX,
105
#define CONTRAST_DEF 127
106
.default_value = CONTRAST_DEF,
108
.set = sd_setcontrast,
109
.get = sd_getcontrast,
111
/* All controls below are for both the 7302 and the 7311 */
115
.type = V4L2_CTRL_TYPE_INTEGER,
122
#define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
123
.default_value = GAIN_DEF,
130
.id = V4L2_CID_EXPOSURE,
131
.type = V4L2_CTRL_TYPE_INTEGER,
134
#define EXPOSURE_MAX 255
135
.maximum = EXPOSURE_MAX,
137
#define EXPOSURE_DEF 16 /* 32 ms / 30 fps */
138
#define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
139
.default_value = EXPOSURE_DEF,
141
.set = sd_setexposure,
142
.get = sd_getexposure,
146
.id = V4L2_CID_AUTOGAIN,
147
.type = V4L2_CTRL_TYPE_BOOLEAN,
152
#define AUTOGAIN_DEF 1
153
.default_value = AUTOGAIN_DEF,
155
.set = sd_setautogain,
156
.get = sd_getautogain,
160
.id = V4L2_CID_HFLIP,
161
.type = V4L2_CTRL_TYPE_BOOLEAN,
167
.default_value = HFLIP_DEF,
174
.id = V4L2_CID_VFLIP,
175
.type = V4L2_CTRL_TYPE_BOOLEAN,
181
.default_value = VFLIP_DEF,
188
88
static const struct v4l2_pix_format vga_mode[] = {
189
89
{160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
190
90
.bytesperline = 160,
387
287
static int sd_config(struct gspca_dev *gspca_dev,
388
288
const struct usb_device_id *id)
390
struct sd *sd = (struct sd *) gspca_dev;
393
cam = &gspca_dev->cam;
395
PDEBUG(D_CONF, "Find Sensor PAC7311");
290
struct cam *cam = &gspca_dev->cam;
396
292
cam->cam_mode = vga_mode;
397
293
cam->nmodes = ARRAY_SIZE(vga_mode);
294
cam->input_flags = V4L2_IN_ST_VFLIP;
399
sd->contrast = CONTRAST_DEF;
401
sd->exposure = EXPOSURE_DEF;
402
sd->autogain = AUTOGAIN_DEF;
403
sd->hflip = HFLIP_DEF;
404
sd->vflip = VFLIP_DEF;
408
/* This function is used by pac7311 only */
409
static void setcontrast(struct gspca_dev *gspca_dev)
299
static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
411
struct sd *sd = (struct sd *) gspca_dev;
413
301
reg_w(gspca_dev, 0xff, 0x04);
414
reg_w(gspca_dev, 0x10, sd->contrast >> 4);
302
reg_w(gspca_dev, 0x10, val);
415
303
/* load registers to sensor (Bit 0, auto clear) */
416
304
reg_w(gspca_dev, 0x11, 0x01);
419
static void setgain(struct gspca_dev *gspca_dev)
307
static void setgain(struct gspca_dev *gspca_dev, s32 val)
421
struct sd *sd = (struct sd *) gspca_dev;
422
int gain = GAIN_MAX - sd->gain;
428
309
reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
429
310
reg_w(gspca_dev, 0x0e, 0x00);
430
reg_w(gspca_dev, 0x0f, gain);
311
reg_w(gspca_dev, 0x0f, gspca_dev->gain->maximum - val + 1);
432
313
/* load registers to sensor (Bit 0, auto clear) */
433
314
reg_w(gspca_dev, 0x11, 0x01);
436
static void setexposure(struct gspca_dev *gspca_dev)
317
static void setexposure(struct gspca_dev *gspca_dev, s32 val)
438
struct sd *sd = (struct sd *) gspca_dev;
441
/* register 2 of frame 3/4 contains the clock divider configuring the
442
no fps according to the formula: 60 / reg. sd->exposure is the
443
desired exposure time in ms. */
444
reg = 120 * sd->exposure / 1000;
450
319
reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
451
reg_w(gspca_dev, 0x02, reg);
453
/* Page 1 register 8 must always be 0x08 except when not in
454
640x480 mode and Page3/4 reg 2 <= 3 then it must be 9 */
320
reg_w(gspca_dev, 0x02, val);
322
/* load registers to sensor (Bit 0, auto clear) */
323
reg_w(gspca_dev, 0x11, 0x01);
326
* Page 1 register 8 must always be 0x08 except when not in
327
* 640x480 mode and page 4 reg 2 <= 3 then it must be 9
455
329
reg_w(gspca_dev, 0xff, 0x01);
456
if (gspca_dev->cam.cam_mode[(int)gspca_dev->curr_mode].priv &&
330
if (gspca_dev->width != 640 && val <= 3)
458
331
reg_w(gspca_dev, 0x08, 0x09);
460
333
reg_w(gspca_dev, 0x08, 0x08);
336
* Page1 register 80 sets the compression balance, normally we
337
* want / use 0x1c, but for 640x480@30fps we must allow the
338
* camera to use higher compression or we may run out of
341
if (gspca_dev->width == 640 && val == 2)
342
reg_w(gspca_dev, 0x80, 0x01);
344
reg_w(gspca_dev, 0x80, 0x1c);
463
346
/* load registers to sensor (Bit 0, auto clear) */
464
347
reg_w(gspca_dev, 0x11, 0x01);
467
static void sethvflip(struct gspca_dev *gspca_dev)
350
static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
469
struct sd *sd = (struct sd *) gspca_dev;
472
354
reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
473
data = (sd->hflip ? 0x04 : 0x00) | (sd->vflip ? 0x08 : 0x00);
355
data = (hflip ? 0x04 : 0x00) |
356
(vflip ? 0x08 : 0x00);
474
357
reg_w(gspca_dev, 0x21, data);
476
359
/* load registers to sensor (Bit 0, auto clear) */
484
367
return gspca_dev->usb_err;
370
static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
372
struct gspca_dev *gspca_dev =
373
container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
374
struct sd *sd = (struct sd *)gspca_dev;
376
gspca_dev->usb_err = 0;
378
if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
379
/* when switching to autogain set defaults to make sure
380
we are on a valid point of the autogain gain /
381
exposure knee graph, and give this change time to
382
take effect before doing autogain. */
383
gspca_dev->exposure->val = PAC7311_EXPOSURE_DEFAULT;
384
gspca_dev->gain->val = PAC7311_GAIN_DEFAULT;
385
sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
388
if (!gspca_dev->streaming)
392
case V4L2_CID_CONTRAST:
393
setcontrast(gspca_dev, ctrl->val);
395
case V4L2_CID_AUTOGAIN:
396
if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
397
setexposure(gspca_dev, gspca_dev->exposure->val);
398
if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
399
setgain(gspca_dev, gspca_dev->gain->val);
402
sethvflip(gspca_dev, sd->hflip->val, 1);
407
return gspca_dev->usb_err;
410
static const struct v4l2_ctrl_ops sd_ctrl_ops = {
414
/* this function is called at probe time */
415
static int sd_init_controls(struct gspca_dev *gspca_dev)
417
struct sd *sd = (struct sd *) gspca_dev;
418
struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
420
gspca_dev->vdev.ctrl_handler = hdl;
421
v4l2_ctrl_handler_init(hdl, 5);
423
sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
424
V4L2_CID_CONTRAST, 0, 15, 1, 7);
425
gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
426
V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
427
gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
428
V4L2_CID_EXPOSURE, 2, 63, 1,
429
PAC7311_EXPOSURE_DEFAULT);
430
gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
431
V4L2_CID_GAIN, 0, 244, 1,
432
PAC7311_GAIN_DEFAULT);
433
sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
434
V4L2_CID_HFLIP, 0, 1, 1, 0);
437
pr_err("Could not initialize controls\n");
441
v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
445
/* -- start the camera -- */
487
446
static int sd_start(struct gspca_dev *gspca_dev)
489
448
struct sd *sd = (struct sd *) gspca_dev;
493
452
reg_w_var(gspca_dev, start_7311,
494
453
page4_7311, sizeof(page4_7311));
495
setcontrast(gspca_dev);
497
setexposure(gspca_dev);
498
sethvflip(gspca_dev);
454
setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
455
setgain(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->gain));
456
setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure));
457
sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip), 1);
500
459
/* set correct resolution */
501
460
switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
502
case 2: /* 160x120 pac7311 */
461
case 2: /* 160x120 */
503
462
reg_w(gspca_dev, 0xff, 0x01);
504
463
reg_w(gspca_dev, 0x17, 0x20);
505
464
reg_w(gspca_dev, 0x87, 0x10);
507
case 1: /* 320x240 pac7311 */
466
case 1: /* 320x240 */
508
467
reg_w(gspca_dev, 0xff, 0x01);
509
468
reg_w(gspca_dev, 0x17, 0x30);
510
469
reg_w(gspca_dev, 0x87, 0x11);
668
621
gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
671
static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
673
struct sd *sd = (struct sd *) gspca_dev;
676
if (gspca_dev->streaming)
677
setcontrast(gspca_dev);
678
return gspca_dev->usb_err;
681
static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
683
struct sd *sd = (struct sd *) gspca_dev;
689
static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
691
struct sd *sd = (struct sd *) gspca_dev;
694
if (gspca_dev->streaming)
696
return gspca_dev->usb_err;
699
static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
701
struct sd *sd = (struct sd *) gspca_dev;
707
static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
709
struct sd *sd = (struct sd *) gspca_dev;
712
if (gspca_dev->streaming)
713
setexposure(gspca_dev);
714
return gspca_dev->usb_err;
717
static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
719
struct sd *sd = (struct sd *) gspca_dev;
725
static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
727
struct sd *sd = (struct sd *) gspca_dev;
730
/* when switching to autogain set defaults to make sure
731
we are on a valid point of the autogain gain /
732
exposure knee graph, and give this change time to
733
take effect before doing autogain. */
735
sd->exposure = EXPOSURE_DEF;
737
if (gspca_dev->streaming) {
738
sd->autogain_ignore_frames =
739
PAC_AUTOGAIN_IGNORE_FRAMES;
740
setexposure(gspca_dev);
745
return gspca_dev->usb_err;
748
static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
750
struct sd *sd = (struct sd *) gspca_dev;
756
static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
758
struct sd *sd = (struct sd *) gspca_dev;
761
if (gspca_dev->streaming)
762
sethvflip(gspca_dev);
763
return gspca_dev->usb_err;
766
static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
768
struct sd *sd = (struct sd *) gspca_dev;
774
static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
776
struct sd *sd = (struct sd *) gspca_dev;
779
if (gspca_dev->streaming)
780
sethvflip(gspca_dev);
781
return gspca_dev->usb_err;
784
static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
786
struct sd *sd = (struct sd *) gspca_dev;
792
624
#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
793
625
static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
794
626
u8 *data, /* interrupt packet data */