1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
4 * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
5 * Copyright (c) 2002, 2003 Tuukka Toivonen
6 * Copyright (c) 2008 Erik Andrén
8 * P/N 861037: Sensor HDCS1000 ASIC STV0600
9 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
10 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
11 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
12 * P/N 861075-0040: Sensor HDCS1000 ASIC
13 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
14 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include "stv06xx_vv6410.h"
21 static struct v4l2_pix_format vv6410_mode
[] = {
27 .sizeimage
= 356 * 292,
29 .colorspace
= V4L2_COLORSPACE_SRGB
,
34 static int vv6410_s_ctrl(struct v4l2_ctrl
*ctrl
)
36 struct gspca_dev
*gspca_dev
=
37 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
42 if (!gspca_dev
->streaming
)
44 err
= vv6410_set_hflip(gspca_dev
, ctrl
->val
);
47 if (!gspca_dev
->streaming
)
49 err
= vv6410_set_vflip(gspca_dev
, ctrl
->val
);
52 err
= vv6410_set_analog_gain(gspca_dev
, ctrl
->val
);
54 case V4L2_CID_EXPOSURE
:
55 err
= vv6410_set_exposure(gspca_dev
, ctrl
->val
);
61 static const struct v4l2_ctrl_ops vv6410_ctrl_ops
= {
62 .s_ctrl
= vv6410_s_ctrl
,
65 static int vv6410_probe(struct sd
*sd
)
70 err
= stv06xx_read_sensor(sd
, VV6410_DEVICEH
, &data
);
77 pr_info("vv6410 sensor detected\n");
79 sd
->gspca_dev
.cam
.cam_mode
= vv6410_mode
;
80 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(vv6410_mode
);
84 static int vv6410_init_controls(struct sd
*sd
)
86 struct v4l2_ctrl_handler
*hdl
= &sd
->gspca_dev
.ctrl_handler
;
88 v4l2_ctrl_handler_init(hdl
, 2);
89 /* Disable the hardware VFLIP and HFLIP as we currently lack a
90 mechanism to adjust the image offset in such a way that
91 we don't need to renegotiate the announced format */
92 /* v4l2_ctrl_new_std(hdl, &vv6410_ctrl_ops, */
93 /* V4L2_CID_HFLIP, 0, 1, 1, 0); */
94 /* v4l2_ctrl_new_std(hdl, &vv6410_ctrl_ops, */
95 /* V4L2_CID_VFLIP, 0, 1, 1, 0); */
96 v4l2_ctrl_new_std(hdl
, &vv6410_ctrl_ops
,
97 V4L2_CID_EXPOSURE
, 0, 32768, 1, 20000);
98 v4l2_ctrl_new_std(hdl
, &vv6410_ctrl_ops
,
99 V4L2_CID_GAIN
, 0, 15, 1, 10);
103 static int vv6410_init(struct sd
*sd
)
107 for (i
= 0; i
< ARRAY_SIZE(stv_bridge_init
); i
++)
108 stv06xx_write_bridge(sd
, stv_bridge_init
[i
].addr
, stv_bridge_init
[i
].data
);
110 err
= stv06xx_write_sensor_bytes(sd
, (u8
*) vv6410_sensor_init
,
111 ARRAY_SIZE(vv6410_sensor_init
));
112 return (err
< 0) ? err
: 0;
115 static int vv6410_start(struct sd
*sd
)
118 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
119 struct cam
*cam
= &sd
->gspca_dev
.cam
;
120 u32 priv
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
;
122 if (priv
& VV6410_SUBSAMPLE
) {
123 gspca_dbg(gspca_dev
, D_CONF
, "Enabling subsampling\n");
124 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x02);
125 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x06);
127 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x10);
129 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x01);
130 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x0a);
131 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x00);
136 err
= stv06xx_write_bridge(sd
, STV_LED_CTRL
, LED_ON
);
140 err
= stv06xx_write_sensor(sd
, VV6410_SETUP0
, 0);
144 gspca_dbg(gspca_dev
, D_STREAM
, "Starting stream\n");
149 static int vv6410_stop(struct sd
*sd
)
151 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
155 err
= stv06xx_write_bridge(sd
, STV_LED_CTRL
, LED_OFF
);
159 err
= stv06xx_write_sensor(sd
, VV6410_SETUP0
, VV6410_LOW_POWER_MODE
);
163 gspca_dbg(gspca_dev
, D_STREAM
, "Halting stream\n");
168 static int vv6410_dump(struct sd
*sd
)
173 pr_info("Dumping all vv6410 sensor registers\n");
174 for (i
= 0; i
< 0xff && !err
; i
++) {
176 err
= stv06xx_read_sensor(sd
, i
, &data
);
177 pr_info("Register 0x%x contained 0x%x\n", i
, data
);
179 return (err
< 0) ? err
: 0;
182 static int vv6410_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
)
186 struct sd
*sd
= (struct sd
*) gspca_dev
;
188 err
= stv06xx_read_sensor(sd
, VV6410_DATAFORMAT
, &i2c_data
);
193 i2c_data
|= VV6410_HFLIP
;
195 i2c_data
&= ~VV6410_HFLIP
;
197 gspca_dbg(gspca_dev
, D_CONF
, "Set horizontal flip to %d\n", val
);
198 err
= stv06xx_write_sensor(sd
, VV6410_DATAFORMAT
, i2c_data
);
200 return (err
< 0) ? err
: 0;
203 static int vv6410_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
)
207 struct sd
*sd
= (struct sd
*) gspca_dev
;
209 err
= stv06xx_read_sensor(sd
, VV6410_DATAFORMAT
, &i2c_data
);
214 i2c_data
|= VV6410_VFLIP
;
216 i2c_data
&= ~VV6410_VFLIP
;
218 gspca_dbg(gspca_dev
, D_CONF
, "Set vertical flip to %d\n", val
);
219 err
= stv06xx_write_sensor(sd
, VV6410_DATAFORMAT
, i2c_data
);
221 return (err
< 0) ? err
: 0;
224 static int vv6410_set_analog_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
227 struct sd
*sd
= (struct sd
*) gspca_dev
;
229 gspca_dbg(gspca_dev
, D_CONF
, "Set analog gain to %d\n", val
);
230 err
= stv06xx_write_sensor(sd
, VV6410_ANALOGGAIN
, 0xf0 | (val
& 0xf));
232 return (err
< 0) ? err
: 0;
235 static int vv6410_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
238 struct sd
*sd
= (struct sd
*) gspca_dev
;
239 unsigned int fine
, coarse
;
241 val
= (val
* val
>> 14) + val
/ 4;
243 fine
= val
% VV6410_CIF_LINELENGTH
;
244 coarse
= min(512, val
/ VV6410_CIF_LINELENGTH
);
246 gspca_dbg(gspca_dev
, D_CONF
, "Set coarse exposure to %d, fine exposure to %d\n",
249 err
= stv06xx_write_sensor(sd
, VV6410_FINEH
, fine
>> 8);
253 err
= stv06xx_write_sensor(sd
, VV6410_FINEL
, fine
& 0xff);
257 err
= stv06xx_write_sensor(sd
, VV6410_COARSEH
, coarse
>> 8);
261 err
= stv06xx_write_sensor(sd
, VV6410_COARSEL
, coarse
& 0xff);