xref: /linux/drivers/media/usb/gspca/stv06xx/stv06xx_pb0100.c (revision ead5d1f4d877e92c051e1a1ade623d0d30e71619)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
7  *
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
15  */
16 
17 /*
18  * The spec file for the PB-0100 suggests the following for best quality
19  * images after the sensor has been reset :
20  *
21  * PB_ADCGAINL      = R60 = 0x03 (3 dec)      : sets low reference of ADC
22 						to produce good black level
23  * PB_PREADCTRL     = R32 = 0x1400 (5120 dec) : Enables global gain changes
24 						through R53
25  * PB_ADCMINGAIN    = R52 = 0x10 (16 dec)     : Sets the minimum gain for
26 						auto-exposure
27  * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec)     : Sets the global gain
28  * PB_EXPGAIN       = R14 = 0x11 (17 dec)     : Sets the auto-exposure value
29  * PB_UPDATEINT     = R23 = 0x02 (2 dec)      : Sets the speed on
30 						auto-exposure routine
31  * PB_CFILLIN       = R5  = 0x0E (14 dec)     : Sets the frame rate
32  */
33 
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 
36 #include "stv06xx_pb0100.h"
37 
38 struct pb0100_ctrls {
39 	struct { /* one big happy control cluster... */
40 		struct v4l2_ctrl *autogain;
41 		struct v4l2_ctrl *gain;
42 		struct v4l2_ctrl *exposure;
43 		struct v4l2_ctrl *red;
44 		struct v4l2_ctrl *blue;
45 		struct v4l2_ctrl *natural;
46 	};
47 	struct v4l2_ctrl *target;
48 };
49 
50 static struct v4l2_pix_format pb0100_mode[] = {
51 /* low res / subsample modes disabled as they are only half res horizontal,
52    halving the vertical resolution does not seem to work */
53 	{
54 		320,
55 		240,
56 		V4L2_PIX_FMT_SGRBG8,
57 		V4L2_FIELD_NONE,
58 		.sizeimage = 320 * 240,
59 		.bytesperline = 320,
60 		.colorspace = V4L2_COLORSPACE_SRGB,
61 		.priv = PB0100_CROP_TO_VGA
62 	},
63 	{
64 		352,
65 		288,
66 		V4L2_PIX_FMT_SGRBG8,
67 		V4L2_FIELD_NONE,
68 		.sizeimage = 352 * 288,
69 		.bytesperline = 352,
70 		.colorspace = V4L2_COLORSPACE_SRGB,
71 		.priv = 0
72 	}
73 };
74 
pb0100_s_ctrl(struct v4l2_ctrl * ctrl)75 static int pb0100_s_ctrl(struct v4l2_ctrl *ctrl)
76 {
77 	struct gspca_dev *gspca_dev =
78 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
79 	struct sd *sd = (struct sd *)gspca_dev;
80 	struct pb0100_ctrls *ctrls = sd->sensor_priv;
81 	int err = -EINVAL;
82 
83 	switch (ctrl->id) {
84 	case V4L2_CID_AUTOGAIN:
85 		err = pb0100_set_autogain(gspca_dev, ctrl->val);
86 		if (err)
87 			break;
88 		if (ctrl->val)
89 			break;
90 		err = pb0100_set_gain(gspca_dev, ctrls->gain->val);
91 		if (err)
92 			break;
93 		err = pb0100_set_exposure(gspca_dev, ctrls->exposure->val);
94 		break;
95 	case V4L2_CTRL_CLASS_USER + 0x1001:
96 		err = pb0100_set_autogain_target(gspca_dev, ctrl->val);
97 		break;
98 	}
99 	return err;
100 }
101 
102 static const struct v4l2_ctrl_ops pb0100_ctrl_ops = {
103 	.s_ctrl = pb0100_s_ctrl,
104 };
105 
pb0100_init_controls(struct sd * sd)106 static int pb0100_init_controls(struct sd *sd)
107 {
108 	struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
109 	struct pb0100_ctrls *ctrls;
110 	static const struct v4l2_ctrl_config autogain_target = {
111 		.ops = &pb0100_ctrl_ops,
112 		.id = V4L2_CTRL_CLASS_USER + 0x1000,
113 		.type = V4L2_CTRL_TYPE_INTEGER,
114 		.name = "Automatic Gain Target",
115 		.max = 255,
116 		.step = 1,
117 		.def = 128,
118 	};
119 	static const struct v4l2_ctrl_config natural_light = {
120 		.ops = &pb0100_ctrl_ops,
121 		.id = V4L2_CTRL_CLASS_USER + 0x1001,
122 		.type = V4L2_CTRL_TYPE_BOOLEAN,
123 		.name = "Natural Light Source",
124 		.max = 1,
125 		.step = 1,
126 		.def = 1,
127 	};
128 
129 	ctrls = kzalloc(sizeof(*ctrls), GFP_KERNEL);
130 	if (!ctrls)
131 		return -ENOMEM;
132 
133 	v4l2_ctrl_handler_init(hdl, 6);
134 	ctrls->autogain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
135 			V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
136 	ctrls->exposure = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
137 			V4L2_CID_EXPOSURE, 0, 511, 1, 12);
138 	ctrls->gain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
139 			V4L2_CID_GAIN, 0, 255, 1, 128);
140 	ctrls->red = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
141 			V4L2_CID_RED_BALANCE, -255, 255, 1, 0);
142 	ctrls->blue = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
143 			V4L2_CID_BLUE_BALANCE, -255, 255, 1, 0);
144 	ctrls->natural = v4l2_ctrl_new_custom(hdl, &natural_light, NULL);
145 	ctrls->target = v4l2_ctrl_new_custom(hdl, &autogain_target, NULL);
146 	if (hdl->error) {
147 		kfree(ctrls);
148 		return hdl->error;
149 	}
150 	sd->sensor_priv = ctrls;
151 	v4l2_ctrl_auto_cluster(5, &ctrls->autogain, 0, false);
152 	return 0;
153 }
154 
pb0100_probe(struct sd * sd)155 static int pb0100_probe(struct sd *sd)
156 {
157 	u16 sensor;
158 	int err;
159 
160 	err = stv06xx_read_sensor(sd, PB_IDENT, &sensor);
161 
162 	if (err < 0)
163 		return -ENODEV;
164 	if ((sensor >> 8) != 0x64)
165 		return -ENODEV;
166 
167 	pr_info("Photobit pb0100 sensor detected\n");
168 
169 	sd->gspca_dev.cam.cam_mode = pb0100_mode;
170 	sd->gspca_dev.cam.nmodes = ARRAY_SIZE(pb0100_mode);
171 
172 	return 0;
173 }
174 
pb0100_start(struct sd * sd)175 static int pb0100_start(struct sd *sd)
176 {
177 	int err, packet_size, max_packet_size;
178 	struct usb_host_interface *alt;
179 	struct usb_interface *intf;
180 	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
181 	struct cam *cam = &sd->gspca_dev.cam;
182 	u32 mode = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
183 
184 	intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
185 	alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
186 	if (!alt)
187 		return -ENODEV;
188 
189 	if (alt->desc.bNumEndpoints < 1)
190 		return -ENODEV;
191 
192 	packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
193 
194 	/* If we don't have enough bandwidth use a lower framerate */
195 	max_packet_size = sd->sensor->max_packet_size[sd->gspca_dev.curr_mode];
196 	if (packet_size < max_packet_size)
197 		stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
198 	else
199 		stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(5)|BIT(3)|BIT(1));
200 
201 	/* Setup sensor window */
202 	if (mode & PB0100_CROP_TO_VGA) {
203 		stv06xx_write_sensor(sd, PB_RSTART, 30);
204 		stv06xx_write_sensor(sd, PB_CSTART, 20);
205 		stv06xx_write_sensor(sd, PB_RWSIZE, 240 - 1);
206 		stv06xx_write_sensor(sd, PB_CWSIZE, 320 - 1);
207 	} else {
208 		stv06xx_write_sensor(sd, PB_RSTART, 8);
209 		stv06xx_write_sensor(sd, PB_CSTART, 4);
210 		stv06xx_write_sensor(sd, PB_RWSIZE, 288 - 1);
211 		stv06xx_write_sensor(sd, PB_CWSIZE, 352 - 1);
212 	}
213 
214 	if (mode & PB0100_SUBSAMPLE) {
215 		stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02); /* Wrong, FIXME */
216 		stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
217 
218 		stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
219 	} else {
220 		stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
221 		stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
222 		/* larger -> slower */
223 		stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x20);
224 	}
225 
226 	err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3)|BIT(1));
227 	gspca_dbg(gspca_dev, D_STREAM, "Started stream, status: %d\n", err);
228 
229 	return (err < 0) ? err : 0;
230 }
231 
pb0100_stop(struct sd * sd)232 static int pb0100_stop(struct sd *sd)
233 {
234 	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
235 	int err;
236 
237 	err = stv06xx_write_sensor(sd, PB_ABORTFRAME, 1);
238 
239 	if (err < 0)
240 		goto out;
241 
242 	/* Set bit 1 to zero */
243 	err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
244 
245 	gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
246 out:
247 	return (err < 0) ? err : 0;
248 }
249 
250 /* FIXME: Sort the init commands out and put them into tables,
251 	  this is only for getting the camera to work */
252 /* FIXME: No error handling for now,
253 	  add this once the init has been converted to proper tables */
pb0100_init(struct sd * sd)254 static int pb0100_init(struct sd *sd)
255 {
256 	stv06xx_write_bridge(sd, STV_REG00, 1);
257 	stv06xx_write_bridge(sd, STV_SCAN_RATE, 0);
258 
259 	/* Reset sensor */
260 	stv06xx_write_sensor(sd, PB_RESET, 1);
261 	stv06xx_write_sensor(sd, PB_RESET, 0);
262 
263 	/* Disable chip */
264 	stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
265 
266 	/* Gain stuff...*/
267 	stv06xx_write_sensor(sd, PB_PREADCTRL, BIT(12)|BIT(10)|BIT(6));
268 	stv06xx_write_sensor(sd, PB_ADCGLOBALGAIN, 12);
269 
270 	/* Set up auto-exposure */
271 	/* ADC VREF_HI new setting for a transition
272 	  from the Expose1 to the Expose2 setting */
273 	stv06xx_write_sensor(sd, PB_R28, 12);
274 	/* gain max for autoexposure */
275 	stv06xx_write_sensor(sd, PB_ADCMAXGAIN, 180);
276 	/* gain min for autoexposure  */
277 	stv06xx_write_sensor(sd, PB_ADCMINGAIN, 12);
278 	/* Maximum frame integration time (programmed into R8)
279 	   allowed for auto-exposure routine */
280 	stv06xx_write_sensor(sd, PB_R54, 3);
281 	/* Minimum frame integration time (programmed into R8)
282 	   allowed for auto-exposure routine */
283 	stv06xx_write_sensor(sd, PB_R55, 0);
284 	stv06xx_write_sensor(sd, PB_UPDATEINT, 1);
285 	/* R15  Expose0 (maximum that auto-exposure may use) */
286 	stv06xx_write_sensor(sd, PB_R15, 800);
287 	/* R17  Expose2 (minimum that auto-exposure may use) */
288 	stv06xx_write_sensor(sd, PB_R17, 10);
289 
290 	stv06xx_write_sensor(sd, PB_EXPGAIN, 0);
291 
292 	/* 0x14 */
293 	stv06xx_write_sensor(sd, PB_VOFFSET, 0);
294 	/* 0x0D */
295 	stv06xx_write_sensor(sd, PB_ADCGAINH, 11);
296 	/* Set black level (important!) */
297 	stv06xx_write_sensor(sd, PB_ADCGAINL, 0);
298 
299 	/* ??? */
300 	stv06xx_write_bridge(sd, STV_REG00, 0x11);
301 	stv06xx_write_bridge(sd, STV_REG03, 0x45);
302 	stv06xx_write_bridge(sd, STV_REG04, 0x07);
303 
304 	/* Scan/timing for the sensor */
305 	stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
306 	stv06xx_write_sensor(sd, PB_CFILLIN, 14);
307 	stv06xx_write_sensor(sd, PB_VBL, 0);
308 	stv06xx_write_sensor(sd, PB_FINTTIME, 0);
309 	stv06xx_write_sensor(sd, PB_RINTTIME, 123);
310 
311 	stv06xx_write_bridge(sd, STV_REG01, 0xc2);
312 	stv06xx_write_bridge(sd, STV_REG02, 0xb0);
313 	return 0;
314 }
315 
pb0100_dump(struct sd * sd)316 static int pb0100_dump(struct sd *sd)
317 {
318 	return 0;
319 }
320 
pb0100_set_gain(struct gspca_dev * gspca_dev,__s32 val)321 static int pb0100_set_gain(struct gspca_dev *gspca_dev, __s32 val)
322 {
323 	int err;
324 	struct sd *sd = (struct sd *) gspca_dev;
325 	struct pb0100_ctrls *ctrls = sd->sensor_priv;
326 
327 	err = stv06xx_write_sensor(sd, PB_G1GAIN, val);
328 	if (!err)
329 		err = stv06xx_write_sensor(sd, PB_G2GAIN, val);
330 	gspca_dbg(gspca_dev, D_CONF, "Set green gain to %d, status: %d\n",
331 		  val, err);
332 
333 	if (!err)
334 		err = pb0100_set_red_balance(gspca_dev, ctrls->red->val);
335 	if (!err)
336 		err = pb0100_set_blue_balance(gspca_dev, ctrls->blue->val);
337 
338 	return err;
339 }
340 
pb0100_set_red_balance(struct gspca_dev * gspca_dev,__s32 val)341 static int pb0100_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
342 {
343 	int err;
344 	struct sd *sd = (struct sd *) gspca_dev;
345 	struct pb0100_ctrls *ctrls = sd->sensor_priv;
346 
347 	val += ctrls->gain->val;
348 	if (val < 0)
349 		val = 0;
350 	else if (val > 255)
351 		val = 255;
352 
353 	err = stv06xx_write_sensor(sd, PB_RGAIN, val);
354 	gspca_dbg(gspca_dev, D_CONF, "Set red gain to %d, status: %d\n",
355 		  val, err);
356 
357 	return err;
358 }
359 
pb0100_set_blue_balance(struct gspca_dev * gspca_dev,__s32 val)360 static int pb0100_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
361 {
362 	int err;
363 	struct sd *sd = (struct sd *) gspca_dev;
364 	struct pb0100_ctrls *ctrls = sd->sensor_priv;
365 
366 	val += ctrls->gain->val;
367 	if (val < 0)
368 		val = 0;
369 	else if (val > 255)
370 		val = 255;
371 
372 	err = stv06xx_write_sensor(sd, PB_BGAIN, val);
373 	gspca_dbg(gspca_dev, D_CONF, "Set blue gain to %d, status: %d\n",
374 		  val, err);
375 
376 	return err;
377 }
378 
pb0100_set_exposure(struct gspca_dev * gspca_dev,__s32 val)379 static int pb0100_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
380 {
381 	struct sd *sd = (struct sd *) gspca_dev;
382 	int err;
383 
384 	err = stv06xx_write_sensor(sd, PB_RINTTIME, val);
385 	gspca_dbg(gspca_dev, D_CONF, "Set exposure to %d, status: %d\n",
386 		  val, err);
387 
388 	return err;
389 }
390 
pb0100_set_autogain(struct gspca_dev * gspca_dev,__s32 val)391 static int pb0100_set_autogain(struct gspca_dev *gspca_dev, __s32 val)
392 {
393 	int err;
394 	struct sd *sd = (struct sd *) gspca_dev;
395 	struct pb0100_ctrls *ctrls = sd->sensor_priv;
396 
397 	if (val) {
398 		if (ctrls->natural->val)
399 			val = BIT(6)|BIT(4)|BIT(0);
400 		else
401 			val = BIT(4)|BIT(0);
402 	} else
403 		val = 0;
404 
405 	err = stv06xx_write_sensor(sd, PB_EXPGAIN, val);
406 	gspca_dbg(gspca_dev, D_CONF, "Set autogain to %d (natural: %d), status: %d\n",
407 		  val, ctrls->natural->val, err);
408 
409 	return err;
410 }
411 
pb0100_set_autogain_target(struct gspca_dev * gspca_dev,__s32 val)412 static int pb0100_set_autogain_target(struct gspca_dev *gspca_dev, __s32 val)
413 {
414 	int err, totalpixels, brightpixels, darkpixels;
415 	struct sd *sd = (struct sd *) gspca_dev;
416 
417 	/* Number of pixels counted by the sensor when subsampling the pixels.
418 	 * Slightly larger than the real value to avoid oscillation */
419 	totalpixels = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
420 	totalpixels = totalpixels/(8*8) + totalpixels/(64*64);
421 
422 	brightpixels = (totalpixels * val) >> 8;
423 	darkpixels   = totalpixels - brightpixels;
424 	err = stv06xx_write_sensor(sd, PB_R21, brightpixels);
425 	if (!err)
426 		err = stv06xx_write_sensor(sd, PB_R22, darkpixels);
427 
428 	gspca_dbg(gspca_dev, D_CONF, "Set autogain target to %d, status: %d\n",
429 		  val, err);
430 
431 	return err;
432 }
433