xref: /linux/drivers/media/usb/gspca/stv06xx/stv06xx_hdcs.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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  * Copyright (c) 2008 Chia-I Wu
8  *
9  * P/N 861037:      Sensor HDCS1000        ASIC STV0600
10  * P/N 861050-0010: Sensor HDCS1000        ASIC STV0600
11  * P/N 861050-0020: Sensor Photobit PB100  ASIC STV0600-1 - QuickCam Express
12  * P/N 861055:      Sensor ST VV6410       ASIC STV0610   - LEGO cam
13  * P/N 861075-0040: Sensor HDCS1000        ASIC
14  * P/N 961179-0700: Sensor ST VV6410       ASIC STV0602   - Dexxa WebCam USB
15  * P/N 861040-0000: Sensor ST VV6410       ASIC STV0610   - QuickCam Web
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include "stv06xx_hdcs.h"
21 
22 static struct v4l2_pix_format hdcs1x00_mode[] = {
23 	{
24 		HDCS_1X00_DEF_WIDTH,
25 		HDCS_1X00_DEF_HEIGHT,
26 		V4L2_PIX_FMT_SGRBG8,
27 		V4L2_FIELD_NONE,
28 		.sizeimage =
29 			HDCS_1X00_DEF_WIDTH * HDCS_1X00_DEF_HEIGHT,
30 		.bytesperline = HDCS_1X00_DEF_WIDTH,
31 		.colorspace = V4L2_COLORSPACE_SRGB,
32 		.priv = 1
33 	}
34 };
35 
36 static struct v4l2_pix_format hdcs1020_mode[] = {
37 	{
38 		HDCS_1020_DEF_WIDTH,
39 		HDCS_1020_DEF_HEIGHT,
40 		V4L2_PIX_FMT_SGRBG8,
41 		V4L2_FIELD_NONE,
42 		.sizeimage =
43 			HDCS_1020_DEF_WIDTH * HDCS_1020_DEF_HEIGHT,
44 		.bytesperline = HDCS_1020_DEF_WIDTH,
45 		.colorspace = V4L2_COLORSPACE_SRGB,
46 		.priv = 1
47 	}
48 };
49 
50 enum hdcs_power_state {
51 	HDCS_STATE_SLEEP,
52 	HDCS_STATE_IDLE,
53 	HDCS_STATE_RUN
54 };
55 
56 /* no lock? */
57 struct hdcs {
58 	enum hdcs_power_state state;
59 	int w, h;
60 
61 	/* visible area of the sensor array */
62 	struct {
63 		int left, top;
64 		int width, height;
65 		int border;
66 	} array;
67 
68 	struct {
69 		/* Column timing overhead */
70 		u8 cto;
71 		/* Column processing overhead */
72 		u8 cpo;
73 		/* Row sample period constant */
74 		u16 rs;
75 		/* Exposure reset duration */
76 		u16 er;
77 	} exp;
78 
79 	int psmp;
80 };
81 
82 static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
83 {
84 	u8 regs[I2C_MAX_BYTES * 2];
85 	int i;
86 
87 	if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
88 		     (reg + len > 0xff)))
89 		return -EINVAL;
90 
91 	for (i = 0; i < len; i++) {
92 		regs[2 * i] = reg;
93 		regs[2 * i + 1] = vals[i];
94 		/* All addresses are shifted left one bit
95 		 * as bit 0 toggles r/w */
96 		reg += 2;
97 	}
98 
99 	return stv06xx_write_sensor_bytes(sd, regs, len);
100 }
101 
102 static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
103 {
104 	struct hdcs *hdcs = sd->sensor_priv;
105 	u8 val;
106 	int ret;
107 
108 	if (hdcs->state == state)
109 		return 0;
110 
111 	/* we need to go idle before running or sleeping */
112 	if (hdcs->state != HDCS_STATE_IDLE) {
113 		ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
114 		if (ret)
115 			return ret;
116 	}
117 
118 	hdcs->state = HDCS_STATE_IDLE;
119 
120 	if (state == HDCS_STATE_IDLE)
121 		return 0;
122 
123 	switch (state) {
124 	case HDCS_STATE_SLEEP:
125 		val = HDCS_SLEEP_MODE;
126 		break;
127 
128 	case HDCS_STATE_RUN:
129 		val = HDCS_RUN_ENABLE;
130 		break;
131 
132 	default:
133 		return -EINVAL;
134 	}
135 
136 	ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
137 
138 	/* Update the state if the write succeeded */
139 	if (!ret)
140 		hdcs->state = state;
141 
142 	return ret;
143 }
144 
145 static int hdcs_reset(struct sd *sd)
146 {
147 	struct hdcs *hdcs = sd->sensor_priv;
148 	int err;
149 
150 	err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
151 	if (err < 0)
152 		return err;
153 
154 	err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
155 	if (err < 0)
156 		hdcs->state = HDCS_STATE_IDLE;
157 
158 	return err;
159 }
160 
161 static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
162 {
163 	struct sd *sd = (struct sd *) gspca_dev;
164 	struct hdcs *hdcs = sd->sensor_priv;
165 	int rowexp, srowexp;
166 	int max_srowexp;
167 	/* Column time period */
168 	int ct;
169 	/* Column processing period */
170 	int cp;
171 	/* Row processing period */
172 	int rp;
173 	/* Minimum number of column timing periods
174 	   within the column processing period */
175 	int mnct;
176 	int cycles, err;
177 	u8 exp[14];
178 
179 	cycles = val * HDCS_CLK_FREQ_MHZ * 257;
180 
181 	ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
182 	cp = hdcs->exp.cto + (hdcs->w * ct / 2);
183 
184 	/* the cycles one row takes */
185 	rp = hdcs->exp.rs + cp;
186 
187 	rowexp = cycles / rp;
188 
189 	/* the remaining cycles */
190 	cycles -= rowexp * rp;
191 
192 	/* calculate sub-row exposure */
193 	if (IS_1020(sd)) {
194 		/* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
195 		srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
196 
197 		mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
198 		max_srowexp = hdcs->w - mnct;
199 	} else {
200 		/* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
201 		srowexp = cp - hdcs->exp.er - 6 - cycles;
202 
203 		mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
204 		max_srowexp = cp - mnct * ct - 1;
205 	}
206 
207 	if (srowexp < 0)
208 		srowexp = 0;
209 	else if (srowexp > max_srowexp)
210 		srowexp = max_srowexp;
211 
212 	if (IS_1020(sd)) {
213 		exp[0] = HDCS20_CONTROL;
214 		exp[1] = 0x00;		/* Stop streaming */
215 		exp[2] = HDCS_ROWEXPL;
216 		exp[3] = rowexp & 0xff;
217 		exp[4] = HDCS_ROWEXPH;
218 		exp[5] = rowexp >> 8;
219 		exp[6] = HDCS20_SROWEXP;
220 		exp[7] = (srowexp >> 2) & 0xff;
221 		exp[8] = HDCS20_ERROR;
222 		exp[9] = 0x10;		/* Clear exposure error flag*/
223 		exp[10] = HDCS20_CONTROL;
224 		exp[11] = 0x04;		/* Restart streaming */
225 		err = stv06xx_write_sensor_bytes(sd, exp, 6);
226 	} else {
227 		exp[0] = HDCS00_CONTROL;
228 		exp[1] = 0x00;         /* Stop streaming */
229 		exp[2] = HDCS_ROWEXPL;
230 		exp[3] = rowexp & 0xff;
231 		exp[4] = HDCS_ROWEXPH;
232 		exp[5] = rowexp >> 8;
233 		exp[6] = HDCS00_SROWEXPL;
234 		exp[7] = srowexp & 0xff;
235 		exp[8] = HDCS00_SROWEXPH;
236 		exp[9] = srowexp >> 8;
237 		exp[10] = HDCS_STATUS;
238 		exp[11] = 0x10;         /* Clear exposure error flag*/
239 		exp[12] = HDCS00_CONTROL;
240 		exp[13] = 0x04;         /* Restart streaming */
241 		err = stv06xx_write_sensor_bytes(sd, exp, 7);
242 		if (err < 0)
243 			return err;
244 	}
245 	gspca_dbg(gspca_dev, D_CONF, "Writing exposure %d, rowexp %d, srowexp %d\n",
246 		  val, rowexp, srowexp);
247 	return err;
248 }
249 
250 static int hdcs_set_gains(struct sd *sd, u8 g)
251 {
252 	int err;
253 	u8 gains[4];
254 
255 	/* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
256 	if (g > 127)
257 		g = 0x80 | (g / 2);
258 
259 	gains[0] = g;
260 	gains[1] = g;
261 	gains[2] = g;
262 	gains[3] = g;
263 
264 	err = hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
265 	return err;
266 }
267 
268 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
269 {
270 	gspca_dbg(gspca_dev, D_CONF, "Writing gain %d\n", val);
271 	return hdcs_set_gains((struct sd *) gspca_dev,
272 			       val & 0xff);
273 }
274 
275 static int hdcs_set_size(struct sd *sd,
276 		unsigned int width, unsigned int height)
277 {
278 	struct hdcs *hdcs = sd->sensor_priv;
279 	u8 win[4];
280 	unsigned int x, y;
281 	int err;
282 
283 	/* must be multiple of 4 */
284 	width = (width + 3) & ~0x3;
285 	height = (height + 3) & ~0x3;
286 
287 	if (width > hdcs->array.width)
288 		width = hdcs->array.width;
289 
290 	if (IS_1020(sd)) {
291 		/* the borders are also invalid */
292 		if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
293 				  > hdcs->array.height)
294 			height = hdcs->array.height - 2 * hdcs->array.border -
295 				HDCS_1020_BOTTOM_Y_SKIP;
296 
297 		y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
298 				+ hdcs->array.top;
299 	} else {
300 		if (height > hdcs->array.height)
301 			height = hdcs->array.height;
302 
303 		y = hdcs->array.top + (hdcs->array.height - height) / 2;
304 	}
305 
306 	x = hdcs->array.left + (hdcs->array.width - width) / 2;
307 
308 	win[0] = y / 4;
309 	win[1] = x / 4;
310 	win[2] = (y + height) / 4 - 1;
311 	win[3] = (x + width) / 4 - 1;
312 
313 	err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
314 	if (err < 0)
315 		return err;
316 
317 	/* Update the current width and height */
318 	hdcs->w = width;
319 	hdcs->h = height;
320 	return err;
321 }
322 
323 static int hdcs_s_ctrl(struct v4l2_ctrl *ctrl)
324 {
325 	struct gspca_dev *gspca_dev =
326 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
327 	int err = -EINVAL;
328 
329 	switch (ctrl->id) {
330 	case V4L2_CID_GAIN:
331 		err = hdcs_set_gain(gspca_dev, ctrl->val);
332 		break;
333 	case V4L2_CID_EXPOSURE:
334 		err = hdcs_set_exposure(gspca_dev, ctrl->val);
335 		break;
336 	}
337 	return err;
338 }
339 
340 static const struct v4l2_ctrl_ops hdcs_ctrl_ops = {
341 	.s_ctrl = hdcs_s_ctrl,
342 };
343 
344 static int hdcs_init_controls(struct sd *sd)
345 {
346 	struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
347 
348 	v4l2_ctrl_handler_init(hdl, 2);
349 	v4l2_ctrl_new_std(hdl, &hdcs_ctrl_ops,
350 			V4L2_CID_EXPOSURE, 0, 0xff, 1, HDCS_DEFAULT_EXPOSURE);
351 	v4l2_ctrl_new_std(hdl, &hdcs_ctrl_ops,
352 			V4L2_CID_GAIN, 0, 0xff, 1, HDCS_DEFAULT_GAIN);
353 	return hdl->error;
354 }
355 
356 static int hdcs_probe_1x00(struct sd *sd)
357 {
358 	struct hdcs *hdcs;
359 	u16 sensor;
360 	int ret;
361 
362 	ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
363 	if (ret < 0 || sensor != 0x08)
364 		return -ENODEV;
365 
366 	pr_info("HDCS-1000/1100 sensor detected\n");
367 
368 	sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
369 	sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
370 
371 	hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
372 	if (!hdcs)
373 		return -ENOMEM;
374 
375 	hdcs->array.left = 8;
376 	hdcs->array.top = 8;
377 	hdcs->array.width = HDCS_1X00_DEF_WIDTH;
378 	hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
379 	hdcs->array.border = 4;
380 
381 	hdcs->exp.cto = 4;
382 	hdcs->exp.cpo = 2;
383 	hdcs->exp.rs = 186;
384 	hdcs->exp.er = 100;
385 
386 	/*
387 	 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
388 	 *  4 = doesn't work at all
389 	 *  5 = 7.8 fps,
390 	 *  6 = 6.9 fps,
391 	 *  8 = 6.3 fps,
392 	 * 10 = 5.5 fps,
393 	 * 15 = 4.4 fps,
394 	 * 31 = 2.8 fps
395 	 *
396 	 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
397 	 * 15 = doesn't work at all
398 	 * 18 = doesn't work at all
399 	 * 19 = 7.3 fps
400 	 * 20 = 7.4 fps
401 	 * 21 = 7.4 fps
402 	 * 22 = 7.4 fps
403 	 * 24 = 6.3 fps
404 	 * 30 = 5.4 fps
405 	 */
406 	hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
407 
408 	sd->sensor_priv = hdcs;
409 
410 	return 0;
411 }
412 
413 static int hdcs_probe_1020(struct sd *sd)
414 {
415 	struct hdcs *hdcs;
416 	u16 sensor;
417 	int ret;
418 
419 	ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
420 	if (ret < 0 || sensor != 0x10)
421 		return -ENODEV;
422 
423 	pr_info("HDCS-1020 sensor detected\n");
424 
425 	sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
426 	sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
427 
428 	hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
429 	if (!hdcs)
430 		return -ENOMEM;
431 
432 	/*
433 	 * From Andrey's test image: looks like HDCS-1020 upper-left
434 	 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
435 	 * visible pixel at 375,299 (x maybe even larger?)
436 	 */
437 	hdcs->array.left = 24;
438 	hdcs->array.top  = 4;
439 	hdcs->array.width = HDCS_1020_DEF_WIDTH;
440 	hdcs->array.height = 304;
441 	hdcs->array.border = 4;
442 
443 	hdcs->psmp = 6;
444 
445 	hdcs->exp.cto = 3;
446 	hdcs->exp.cpo = 3;
447 	hdcs->exp.rs = 155;
448 	hdcs->exp.er = 96;
449 
450 	sd->sensor_priv = hdcs;
451 
452 	return 0;
453 }
454 
455 static int hdcs_start(struct sd *sd)
456 {
457 	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
458 
459 	gspca_dbg(gspca_dev, D_STREAM, "Starting stream\n");
460 
461 	return hdcs_set_state(sd, HDCS_STATE_RUN);
462 }
463 
464 static int hdcs_stop(struct sd *sd)
465 {
466 	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
467 
468 	gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
469 
470 	return hdcs_set_state(sd, HDCS_STATE_SLEEP);
471 }
472 
473 static int hdcs_init(struct sd *sd)
474 {
475 	struct hdcs *hdcs = sd->sensor_priv;
476 	int i, err = 0;
477 
478 	/* Set the STV0602AA in STV0600 emulation mode */
479 	if (sd->bridge == BRIDGE_STV602)
480 		stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
481 
482 	/* Execute the bridge init */
483 	for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
484 		err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
485 					   stv_bridge_init[i][1]);
486 	}
487 	if (err < 0)
488 		return err;
489 
490 	/* sensor soft reset */
491 	hdcs_reset(sd);
492 
493 	/* Execute the sensor init */
494 	for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
495 		err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
496 					     stv_sensor_init[i][1]);
497 	}
498 	if (err < 0)
499 		return err;
500 
501 	/* Enable continuous frame capture, bit 2: stop when frame complete */
502 	err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
503 	if (err < 0)
504 		return err;
505 
506 	/* Set PGA sample duration
507 	(was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
508 	if (IS_1020(sd))
509 		err = stv06xx_write_sensor(sd, HDCS_TCTRL,
510 				(HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
511 	else
512 		err = stv06xx_write_sensor(sd, HDCS_TCTRL,
513 				(HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
514 	if (err < 0)
515 		return err;
516 
517 	return hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
518 }
519 
520 static int hdcs_dump(struct sd *sd)
521 {
522 	u16 reg, val;
523 
524 	pr_info("Dumping sensor registers:\n");
525 
526 	for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
527 		stv06xx_read_sensor(sd, reg, &val);
528 		pr_info("reg 0x%02x = 0x%02x\n", reg, val);
529 	}
530 	return 0;
531 }
532