18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
48c2ecf20Sopenharmony_ci *		      Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
58c2ecf20Sopenharmony_ci * Copyright (c) 2002, 2003 Tuukka Toivonen
68c2ecf20Sopenharmony_ci * Copyright (c) 2008 Erik Andrén
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * P/N 861037:      Sensor HDCS1000        ASIC STV0600
98c2ecf20Sopenharmony_ci * P/N 861050-0010: Sensor HDCS1000        ASIC STV0600
108c2ecf20Sopenharmony_ci * P/N 861050-0020: Sensor Photobit PB100  ASIC STV0600-1 - QuickCam Express
118c2ecf20Sopenharmony_ci * P/N 861055:      Sensor ST VV6410       ASIC STV0610   - LEGO cam
128c2ecf20Sopenharmony_ci * P/N 861075-0040: Sensor HDCS1000        ASIC
138c2ecf20Sopenharmony_ci * P/N 961179-0700: Sensor ST VV6410       ASIC STV0602   - Dexxa WebCam USB
148c2ecf20Sopenharmony_ci * P/N 861040-0000: Sensor ST VV6410       ASIC STV0610   - QuickCam Web
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci * The spec file for the PB-0100 suggests the following for best quality
198c2ecf20Sopenharmony_ci * images after the sensor has been reset :
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * PB_ADCGAINL      = R60 = 0x03 (3 dec)      : sets low reference of ADC
228c2ecf20Sopenharmony_ci						to produce good black level
238c2ecf20Sopenharmony_ci * PB_PREADCTRL     = R32 = 0x1400 (5120 dec) : Enables global gain changes
248c2ecf20Sopenharmony_ci						through R53
258c2ecf20Sopenharmony_ci * PB_ADCMINGAIN    = R52 = 0x10 (16 dec)     : Sets the minimum gain for
268c2ecf20Sopenharmony_ci						auto-exposure
278c2ecf20Sopenharmony_ci * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec)     : Sets the global gain
288c2ecf20Sopenharmony_ci * PB_EXPGAIN       = R14 = 0x11 (17 dec)     : Sets the auto-exposure value
298c2ecf20Sopenharmony_ci * PB_UPDATEINT     = R23 = 0x02 (2 dec)      : Sets the speed on
308c2ecf20Sopenharmony_ci						auto-exposure routine
318c2ecf20Sopenharmony_ci * PB_CFILLIN       = R5  = 0x0E (14 dec)     : Sets the frame rate
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include "stv06xx_pb0100.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistruct pb0100_ctrls {
398c2ecf20Sopenharmony_ci	struct { /* one big happy control cluster... */
408c2ecf20Sopenharmony_ci		struct v4l2_ctrl *autogain;
418c2ecf20Sopenharmony_ci		struct v4l2_ctrl *gain;
428c2ecf20Sopenharmony_ci		struct v4l2_ctrl *exposure;
438c2ecf20Sopenharmony_ci		struct v4l2_ctrl *red;
448c2ecf20Sopenharmony_ci		struct v4l2_ctrl *blue;
458c2ecf20Sopenharmony_ci		struct v4l2_ctrl *natural;
468c2ecf20Sopenharmony_ci	};
478c2ecf20Sopenharmony_ci	struct v4l2_ctrl *target;
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic struct v4l2_pix_format pb0100_mode[] = {
518c2ecf20Sopenharmony_ci/* low res / subsample modes disabled as they are only half res horizontal,
528c2ecf20Sopenharmony_ci   halving the vertical resolution does not seem to work */
538c2ecf20Sopenharmony_ci	{
548c2ecf20Sopenharmony_ci		320,
558c2ecf20Sopenharmony_ci		240,
568c2ecf20Sopenharmony_ci		V4L2_PIX_FMT_SGRBG8,
578c2ecf20Sopenharmony_ci		V4L2_FIELD_NONE,
588c2ecf20Sopenharmony_ci		.sizeimage = 320 * 240,
598c2ecf20Sopenharmony_ci		.bytesperline = 320,
608c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
618c2ecf20Sopenharmony_ci		.priv = PB0100_CROP_TO_VGA
628c2ecf20Sopenharmony_ci	},
638c2ecf20Sopenharmony_ci	{
648c2ecf20Sopenharmony_ci		352,
658c2ecf20Sopenharmony_ci		288,
668c2ecf20Sopenharmony_ci		V4L2_PIX_FMT_SGRBG8,
678c2ecf20Sopenharmony_ci		V4L2_FIELD_NONE,
688c2ecf20Sopenharmony_ci		.sizeimage = 352 * 288,
698c2ecf20Sopenharmony_ci		.bytesperline = 352,
708c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
718c2ecf20Sopenharmony_ci		.priv = 0
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int pb0100_s_ctrl(struct v4l2_ctrl *ctrl)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev =
788c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
798c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *)gspca_dev;
808c2ecf20Sopenharmony_ci	struct pb0100_ctrls *ctrls = sd->sensor_priv;
818c2ecf20Sopenharmony_ci	int err = -EINVAL;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	switch (ctrl->id) {
848c2ecf20Sopenharmony_ci	case V4L2_CID_AUTOGAIN:
858c2ecf20Sopenharmony_ci		err = pb0100_set_autogain(gspca_dev, ctrl->val);
868c2ecf20Sopenharmony_ci		if (err)
878c2ecf20Sopenharmony_ci			break;
888c2ecf20Sopenharmony_ci		if (ctrl->val)
898c2ecf20Sopenharmony_ci			break;
908c2ecf20Sopenharmony_ci		err = pb0100_set_gain(gspca_dev, ctrls->gain->val);
918c2ecf20Sopenharmony_ci		if (err)
928c2ecf20Sopenharmony_ci			break;
938c2ecf20Sopenharmony_ci		err = pb0100_set_exposure(gspca_dev, ctrls->exposure->val);
948c2ecf20Sopenharmony_ci		break;
958c2ecf20Sopenharmony_ci	case V4L2_CTRL_CLASS_USER + 0x1001:
968c2ecf20Sopenharmony_ci		err = pb0100_set_autogain_target(gspca_dev, ctrl->val);
978c2ecf20Sopenharmony_ci		break;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci	return err;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops pb0100_ctrl_ops = {
1038c2ecf20Sopenharmony_ci	.s_ctrl = pb0100_s_ctrl,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int pb0100_init_controls(struct sd *sd)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
1098c2ecf20Sopenharmony_ci	struct pb0100_ctrls *ctrls;
1108c2ecf20Sopenharmony_ci	static const struct v4l2_ctrl_config autogain_target = {
1118c2ecf20Sopenharmony_ci		.ops = &pb0100_ctrl_ops,
1128c2ecf20Sopenharmony_ci		.id = V4L2_CTRL_CLASS_USER + 0x1000,
1138c2ecf20Sopenharmony_ci		.type = V4L2_CTRL_TYPE_INTEGER,
1148c2ecf20Sopenharmony_ci		.name = "Automatic Gain Target",
1158c2ecf20Sopenharmony_ci		.max = 255,
1168c2ecf20Sopenharmony_ci		.step = 1,
1178c2ecf20Sopenharmony_ci		.def = 128,
1188c2ecf20Sopenharmony_ci	};
1198c2ecf20Sopenharmony_ci	static const struct v4l2_ctrl_config natural_light = {
1208c2ecf20Sopenharmony_ci		.ops = &pb0100_ctrl_ops,
1218c2ecf20Sopenharmony_ci		.id = V4L2_CTRL_CLASS_USER + 0x1001,
1228c2ecf20Sopenharmony_ci		.type = V4L2_CTRL_TYPE_BOOLEAN,
1238c2ecf20Sopenharmony_ci		.name = "Natural Light Source",
1248c2ecf20Sopenharmony_ci		.max = 1,
1258c2ecf20Sopenharmony_ci		.step = 1,
1268c2ecf20Sopenharmony_ci		.def = 1,
1278c2ecf20Sopenharmony_ci	};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	ctrls = kzalloc(sizeof(*ctrls), GFP_KERNEL);
1308c2ecf20Sopenharmony_ci	if (!ctrls)
1318c2ecf20Sopenharmony_ci		return -ENOMEM;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 6);
1348c2ecf20Sopenharmony_ci	ctrls->autogain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
1358c2ecf20Sopenharmony_ci			V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1368c2ecf20Sopenharmony_ci	ctrls->exposure = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
1378c2ecf20Sopenharmony_ci			V4L2_CID_EXPOSURE, 0, 511, 1, 12);
1388c2ecf20Sopenharmony_ci	ctrls->gain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
1398c2ecf20Sopenharmony_ci			V4L2_CID_GAIN, 0, 255, 1, 128);
1408c2ecf20Sopenharmony_ci	ctrls->red = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
1418c2ecf20Sopenharmony_ci			V4L2_CID_RED_BALANCE, -255, 255, 1, 0);
1428c2ecf20Sopenharmony_ci	ctrls->blue = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
1438c2ecf20Sopenharmony_ci			V4L2_CID_BLUE_BALANCE, -255, 255, 1, 0);
1448c2ecf20Sopenharmony_ci	ctrls->natural = v4l2_ctrl_new_custom(hdl, &natural_light, NULL);
1458c2ecf20Sopenharmony_ci	ctrls->target = v4l2_ctrl_new_custom(hdl, &autogain_target, NULL);
1468c2ecf20Sopenharmony_ci	if (hdl->error) {
1478c2ecf20Sopenharmony_ci		kfree(ctrls);
1488c2ecf20Sopenharmony_ci		return hdl->error;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci	sd->sensor_priv = ctrls;
1518c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(5, &ctrls->autogain, 0, false);
1528c2ecf20Sopenharmony_ci	return 0;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int pb0100_probe(struct sd *sd)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	u16 sensor;
1588c2ecf20Sopenharmony_ci	int err;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	err = stv06xx_read_sensor(sd, PB_IDENT, &sensor);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (err < 0)
1638c2ecf20Sopenharmony_ci		return -ENODEV;
1648c2ecf20Sopenharmony_ci	if ((sensor >> 8) != 0x64)
1658c2ecf20Sopenharmony_ci		return -ENODEV;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	pr_info("Photobit pb0100 sensor detected\n");
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	sd->gspca_dev.cam.cam_mode = pb0100_mode;
1708c2ecf20Sopenharmony_ci	sd->gspca_dev.cam.nmodes = ARRAY_SIZE(pb0100_mode);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic int pb0100_start(struct sd *sd)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	int err, packet_size, max_packet_size;
1788c2ecf20Sopenharmony_ci	struct usb_host_interface *alt;
1798c2ecf20Sopenharmony_ci	struct usb_interface *intf;
1808c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
1818c2ecf20Sopenharmony_ci	struct cam *cam = &sd->gspca_dev.cam;
1828c2ecf20Sopenharmony_ci	u32 mode = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
1858c2ecf20Sopenharmony_ci	alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
1868c2ecf20Sopenharmony_ci	if (!alt)
1878c2ecf20Sopenharmony_ci		return -ENODEV;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (alt->desc.bNumEndpoints < 1)
1908c2ecf20Sopenharmony_ci		return -ENODEV;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* If we don't have enough bandwidth use a lower framerate */
1958c2ecf20Sopenharmony_ci	max_packet_size = sd->sensor->max_packet_size[sd->gspca_dev.curr_mode];
1968c2ecf20Sopenharmony_ci	if (packet_size < max_packet_size)
1978c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
1988c2ecf20Sopenharmony_ci	else
1998c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(5)|BIT(3)|BIT(1));
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	/* Setup sensor window */
2028c2ecf20Sopenharmony_ci	if (mode & PB0100_CROP_TO_VGA) {
2038c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_RSTART, 30);
2048c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_CSTART, 20);
2058c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_RWSIZE, 240 - 1);
2068c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_CWSIZE, 320 - 1);
2078c2ecf20Sopenharmony_ci	} else {
2088c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_RSTART, 8);
2098c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_CSTART, 4);
2108c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_RWSIZE, 288 - 1);
2118c2ecf20Sopenharmony_ci		stv06xx_write_sensor(sd, PB_CWSIZE, 352 - 1);
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	if (mode & PB0100_SUBSAMPLE) {
2158c2ecf20Sopenharmony_ci		stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02); /* Wrong, FIXME */
2168c2ecf20Sopenharmony_ci		stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
2198c2ecf20Sopenharmony_ci	} else {
2208c2ecf20Sopenharmony_ci		stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
2218c2ecf20Sopenharmony_ci		stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
2228c2ecf20Sopenharmony_ci		/* larger -> slower */
2238c2ecf20Sopenharmony_ci		stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x20);
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3)|BIT(1));
2278c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_STREAM, "Started stream, status: %d\n", err);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	return (err < 0) ? err : 0;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic int pb0100_stop(struct sd *sd)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
2358c2ecf20Sopenharmony_ci	int err;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_ABORTFRAME, 1);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (err < 0)
2408c2ecf20Sopenharmony_ci		goto out;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* Set bit 1 to zero */
2438c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
2468c2ecf20Sopenharmony_ciout:
2478c2ecf20Sopenharmony_ci	return (err < 0) ? err : 0;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* FIXME: Sort the init commands out and put them into tables,
2518c2ecf20Sopenharmony_ci	  this is only for getting the camera to work */
2528c2ecf20Sopenharmony_ci/* FIXME: No error handling for now,
2538c2ecf20Sopenharmony_ci	  add this once the init has been converted to proper tables */
2548c2ecf20Sopenharmony_cistatic int pb0100_init(struct sd *sd)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_REG00, 1);
2578c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_SCAN_RATE, 0);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	/* Reset sensor */
2608c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_RESET, 1);
2618c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_RESET, 0);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* Disable chip */
2648c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	/* Gain stuff...*/
2678c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_PREADCTRL, BIT(12)|BIT(10)|BIT(6));
2688c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_ADCGLOBALGAIN, 12);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* Set up auto-exposure */
2718c2ecf20Sopenharmony_ci	/* ADC VREF_HI new setting for a transition
2728c2ecf20Sopenharmony_ci	  from the Expose1 to the Expose2 setting */
2738c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_R28, 12);
2748c2ecf20Sopenharmony_ci	/* gain max for autoexposure */
2758c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_ADCMAXGAIN, 180);
2768c2ecf20Sopenharmony_ci	/* gain min for autoexposure  */
2778c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_ADCMINGAIN, 12);
2788c2ecf20Sopenharmony_ci	/* Maximum frame integration time (programmed into R8)
2798c2ecf20Sopenharmony_ci	   allowed for auto-exposure routine */
2808c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_R54, 3);
2818c2ecf20Sopenharmony_ci	/* Minimum frame integration time (programmed into R8)
2828c2ecf20Sopenharmony_ci	   allowed for auto-exposure routine */
2838c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_R55, 0);
2848c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_UPDATEINT, 1);
2858c2ecf20Sopenharmony_ci	/* R15  Expose0 (maximum that auto-exposure may use) */
2868c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_R15, 800);
2878c2ecf20Sopenharmony_ci	/* R17  Expose2 (minimum that auto-exposure may use) */
2888c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_R17, 10);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_EXPGAIN, 0);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* 0x14 */
2938c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_VOFFSET, 0);
2948c2ecf20Sopenharmony_ci	/* 0x0D */
2958c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_ADCGAINH, 11);
2968c2ecf20Sopenharmony_ci	/* Set black level (important!) */
2978c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_ADCGAINL, 0);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	/* ??? */
3008c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_REG00, 0x11);
3018c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_REG03, 0x45);
3028c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_REG04, 0x07);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* Scan/timing for the sensor */
3058c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
3068c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_CFILLIN, 14);
3078c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_VBL, 0);
3088c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_FINTTIME, 0);
3098c2ecf20Sopenharmony_ci	stv06xx_write_sensor(sd, PB_RINTTIME, 123);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_REG01, 0xc2);
3128c2ecf20Sopenharmony_ci	stv06xx_write_bridge(sd, STV_REG02, 0xb0);
3138c2ecf20Sopenharmony_ci	return 0;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int pb0100_dump(struct sd *sd)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	return 0;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic int pb0100_set_gain(struct gspca_dev *gspca_dev, __s32 val)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	int err;
3248c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
3258c2ecf20Sopenharmony_ci	struct pb0100_ctrls *ctrls = sd->sensor_priv;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_G1GAIN, val);
3288c2ecf20Sopenharmony_ci	if (!err)
3298c2ecf20Sopenharmony_ci		err = stv06xx_write_sensor(sd, PB_G2GAIN, val);
3308c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Set green gain to %d, status: %d\n",
3318c2ecf20Sopenharmony_ci		  val, err);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (!err)
3348c2ecf20Sopenharmony_ci		err = pb0100_set_red_balance(gspca_dev, ctrls->red->val);
3358c2ecf20Sopenharmony_ci	if (!err)
3368c2ecf20Sopenharmony_ci		err = pb0100_set_blue_balance(gspca_dev, ctrls->blue->val);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	return err;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic int pb0100_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	int err;
3448c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
3458c2ecf20Sopenharmony_ci	struct pb0100_ctrls *ctrls = sd->sensor_priv;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	val += ctrls->gain->val;
3488c2ecf20Sopenharmony_ci	if (val < 0)
3498c2ecf20Sopenharmony_ci		val = 0;
3508c2ecf20Sopenharmony_ci	else if (val > 255)
3518c2ecf20Sopenharmony_ci		val = 255;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_RGAIN, val);
3548c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Set red gain to %d, status: %d\n",
3558c2ecf20Sopenharmony_ci		  val, err);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return err;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic int pb0100_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	int err;
3638c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
3648c2ecf20Sopenharmony_ci	struct pb0100_ctrls *ctrls = sd->sensor_priv;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	val += ctrls->gain->val;
3678c2ecf20Sopenharmony_ci	if (val < 0)
3688c2ecf20Sopenharmony_ci		val = 0;
3698c2ecf20Sopenharmony_ci	else if (val > 255)
3708c2ecf20Sopenharmony_ci		val = 255;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_BGAIN, val);
3738c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Set blue gain to %d, status: %d\n",
3748c2ecf20Sopenharmony_ci		  val, err);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return err;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic int pb0100_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
3828c2ecf20Sopenharmony_ci	int err;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_RINTTIME, val);
3858c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Set exposure to %d, status: %d\n",
3868c2ecf20Sopenharmony_ci		  val, err);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return err;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int pb0100_set_autogain(struct gspca_dev *gspca_dev, __s32 val)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	int err;
3948c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
3958c2ecf20Sopenharmony_ci	struct pb0100_ctrls *ctrls = sd->sensor_priv;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (val) {
3988c2ecf20Sopenharmony_ci		if (ctrls->natural->val)
3998c2ecf20Sopenharmony_ci			val = BIT(6)|BIT(4)|BIT(0);
4008c2ecf20Sopenharmony_ci		else
4018c2ecf20Sopenharmony_ci			val = BIT(4)|BIT(0);
4028c2ecf20Sopenharmony_ci	} else
4038c2ecf20Sopenharmony_ci		val = 0;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_EXPGAIN, val);
4068c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Set autogain to %d (natural: %d), status: %d\n",
4078c2ecf20Sopenharmony_ci		  val, ctrls->natural->val, err);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	return err;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic int pb0100_set_autogain_target(struct gspca_dev *gspca_dev, __s32 val)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	int err, totalpixels, brightpixels, darkpixels;
4158c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* Number of pixels counted by the sensor when subsampling the pixels.
4188c2ecf20Sopenharmony_ci	 * Slightly larger than the real value to avoid oscillation */
4198c2ecf20Sopenharmony_ci	totalpixels = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
4208c2ecf20Sopenharmony_ci	totalpixels = totalpixels/(8*8) + totalpixels/(64*64);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	brightpixels = (totalpixels * val) >> 8;
4238c2ecf20Sopenharmony_ci	darkpixels   = totalpixels - brightpixels;
4248c2ecf20Sopenharmony_ci	err = stv06xx_write_sensor(sd, PB_R21, brightpixels);
4258c2ecf20Sopenharmony_ci	if (!err)
4268c2ecf20Sopenharmony_ci		err = stv06xx_write_sensor(sd, PB_R22, darkpixels);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_CONF, "Set autogain target to %d, status: %d\n",
4298c2ecf20Sopenharmony_ci		  val, err);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	return err;
4328c2ecf20Sopenharmony_ci}
433