18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Support for the sensor part which is integrated (I think) into the
48c2ecf20Sopenharmony_ci * st6422 stv06xx alike bridge, as its integrated there are no i2c writes
58c2ecf20Sopenharmony_ci * but instead direct bridge writes.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2009 Hans de Goede <hdegoede@redhat.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Strongly based on qc-usb-messenger, which is:
108c2ecf20Sopenharmony_ci * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
118c2ecf20Sopenharmony_ci *		      Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
128c2ecf20Sopenharmony_ci * Copyright (c) 2002, 2003 Tuukka Toivonen
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "stv06xx_st6422.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic struct v4l2_pix_format st6422_mode[] = {
208c2ecf20Sopenharmony_ci	/* Note we actually get 124 lines of data, of which we skip the 4st
218c2ecf20Sopenharmony_ci	   4 as they are garbage */
228c2ecf20Sopenharmony_ci	{
238c2ecf20Sopenharmony_ci		162,
248c2ecf20Sopenharmony_ci		120,
258c2ecf20Sopenharmony_ci		V4L2_PIX_FMT_SGRBG8,
268c2ecf20Sopenharmony_ci		V4L2_FIELD_NONE,
278c2ecf20Sopenharmony_ci		.sizeimage = 162 * 120,
288c2ecf20Sopenharmony_ci		.bytesperline = 162,
298c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
308c2ecf20Sopenharmony_ci		.priv = 1
318c2ecf20Sopenharmony_ci	},
328c2ecf20Sopenharmony_ci	/* Note we actually get 248 lines of data, of which we skip the 4st
338c2ecf20Sopenharmony_ci	   4 as they are garbage, and we tell the app it only gets the
348c2ecf20Sopenharmony_ci	   first 240 of the 244 lines it actually gets, so that it ignores
358c2ecf20Sopenharmony_ci	   the last 4. */
368c2ecf20Sopenharmony_ci	{
378c2ecf20Sopenharmony_ci		324,
388c2ecf20Sopenharmony_ci		240,
398c2ecf20Sopenharmony_ci		V4L2_PIX_FMT_SGRBG8,
408c2ecf20Sopenharmony_ci		V4L2_FIELD_NONE,
418c2ecf20Sopenharmony_ci		.sizeimage = 324 * 244,
428c2ecf20Sopenharmony_ci		.bytesperline = 324,
438c2ecf20Sopenharmony_ci		.colorspace = V4L2_COLORSPACE_SRGB,
448c2ecf20Sopenharmony_ci		.priv = 0
458c2ecf20Sopenharmony_ci	},
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* V4L2 controls supported by the driver */
498c2ecf20Sopenharmony_cistatic int setbrightness(struct sd *sd, s32 val);
508c2ecf20Sopenharmony_cistatic int setcontrast(struct sd *sd, s32 val);
518c2ecf20Sopenharmony_cistatic int setgain(struct sd *sd, u8 gain);
528c2ecf20Sopenharmony_cistatic int setexposure(struct sd *sd, s16 expo);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int st6422_s_ctrl(struct v4l2_ctrl *ctrl)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev =
578c2ecf20Sopenharmony_ci		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
588c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *)gspca_dev;
598c2ecf20Sopenharmony_ci	int err = -EINVAL;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	switch (ctrl->id) {
628c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
638c2ecf20Sopenharmony_ci		err = setbrightness(sd, ctrl->val);
648c2ecf20Sopenharmony_ci		break;
658c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
668c2ecf20Sopenharmony_ci		err = setcontrast(sd, ctrl->val);
678c2ecf20Sopenharmony_ci		break;
688c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN:
698c2ecf20Sopenharmony_ci		err = setgain(sd, ctrl->val);
708c2ecf20Sopenharmony_ci		break;
718c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE:
728c2ecf20Sopenharmony_ci		err = setexposure(sd, ctrl->val);
738c2ecf20Sopenharmony_ci		break;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* commit settings */
778c2ecf20Sopenharmony_ci	if (err >= 0)
788c2ecf20Sopenharmony_ci		err = stv06xx_write_bridge(sd, 0x143f, 0x01);
798c2ecf20Sopenharmony_ci	sd->gspca_dev.usb_err = err;
808c2ecf20Sopenharmony_ci	return err;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops st6422_ctrl_ops = {
848c2ecf20Sopenharmony_ci	.s_ctrl = st6422_s_ctrl,
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int st6422_init_controls(struct sd *sd)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 4);
928c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
938c2ecf20Sopenharmony_ci			V4L2_CID_BRIGHTNESS, 0, 31, 1, 3);
948c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
958c2ecf20Sopenharmony_ci			V4L2_CID_CONTRAST, 0, 15, 1, 11);
968c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
978c2ecf20Sopenharmony_ci			V4L2_CID_EXPOSURE, 0, 1023, 1, 256);
988c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
998c2ecf20Sopenharmony_ci			V4L2_CID_GAIN, 0, 255, 1, 64);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return hdl->error;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int st6422_probe(struct sd *sd)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	if (sd->bridge != BRIDGE_ST6422)
1078c2ecf20Sopenharmony_ci		return -ENODEV;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	pr_info("st6422 sensor detected\n");
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	sd->gspca_dev.cam.cam_mode = st6422_mode;
1128c2ecf20Sopenharmony_ci	sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode);
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int st6422_init(struct sd *sd)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	int err = 0, i;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	static const u16 st6422_bridge_init[][2] = {
1218c2ecf20Sopenharmony_ci		{ STV_ISO_ENABLE, 0x00 }, /* disable capture */
1228c2ecf20Sopenharmony_ci		{ 0x1436, 0x00 },
1238c2ecf20Sopenharmony_ci		{ 0x1432, 0x03 },	/* 0x00-0x1F brightness */
1248c2ecf20Sopenharmony_ci		{ 0x143a, 0xf9 },	/* 0x00-0x0F contrast */
1258c2ecf20Sopenharmony_ci		{ 0x0509, 0x38 },	/* R */
1268c2ecf20Sopenharmony_ci		{ 0x050a, 0x38 },	/* G */
1278c2ecf20Sopenharmony_ci		{ 0x050b, 0x38 },	/* B */
1288c2ecf20Sopenharmony_ci		{ 0x050c, 0x2a },
1298c2ecf20Sopenharmony_ci		{ 0x050d, 0x01 },
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		{ 0x1431, 0x00 },	/* 0x00-0x07 ??? */
1338c2ecf20Sopenharmony_ci		{ 0x1433, 0x34 },	/* 160x120, 0x00-0x01 night filter */
1348c2ecf20Sopenharmony_ci		{ 0x1438, 0x18 },	/* 640x480 */
1358c2ecf20Sopenharmony_ci/* 18 bayes */
1368c2ecf20Sopenharmony_ci/* 10 compressed? */
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		{ 0x1439, 0x00 },
1398c2ecf20Sopenharmony_ci/* anti-noise?  0xa2 gives a perfect image */
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		{ 0x143b, 0x05 },
1428c2ecf20Sopenharmony_ci		{ 0x143c, 0x00 },	/* 0x00-0x01 - ??? */
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* shutter time 0x0000-0x03FF */
1468c2ecf20Sopenharmony_ci/* low value  give good picures on moving objects (but requires much light) */
1478c2ecf20Sopenharmony_ci/* high value gives good picures in darkness (but tends to be overexposed) */
1488c2ecf20Sopenharmony_ci		{ 0x143e, 0x01 },
1498c2ecf20Sopenharmony_ci		{ 0x143d, 0x00 },
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		{ 0x1442, 0xe2 },
1528c2ecf20Sopenharmony_ci/* write: 1x1x xxxx */
1538c2ecf20Sopenharmony_ci/* read:  1x1x xxxx */
1548c2ecf20Sopenharmony_ci/*        bit 5 == button pressed and hold if 0 */
1558c2ecf20Sopenharmony_ci/* write 0xe2,0xea */
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* 0x144a */
1588c2ecf20Sopenharmony_ci/* 0x00 init */
1598c2ecf20Sopenharmony_ci/* bit 7 == button has been pressed, but not handled */
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* interrupt */
1628c2ecf20Sopenharmony_ci/* if(urb->iso_frame_desc[i].status == 0x80) { */
1638c2ecf20Sopenharmony_ci/* if(urb->iso_frame_desc[i].status == 0x88) { */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		{ 0x1500, 0xd0 },
1668c2ecf20Sopenharmony_ci		{ 0x1500, 0xd0 },
1678c2ecf20Sopenharmony_ci		{ 0x1500, 0x50 },	/* 0x00 - 0xFF  0x80 == compr ? */
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		{ 0x1501, 0xaf },
1708c2ecf20Sopenharmony_ci/* high val-> light area gets darker */
1718c2ecf20Sopenharmony_ci/* low val -> light area gets lighter */
1728c2ecf20Sopenharmony_ci		{ 0x1502, 0xc2 },
1738c2ecf20Sopenharmony_ci/* high val-> light area gets darker */
1748c2ecf20Sopenharmony_ci/* low val -> light area gets lighter */
1758c2ecf20Sopenharmony_ci		{ 0x1503, 0x45 },
1768c2ecf20Sopenharmony_ci/* high val-> light area gets darker */
1778c2ecf20Sopenharmony_ci/* low val -> light area gets lighter */
1788c2ecf20Sopenharmony_ci		{ 0x1505, 0x02 },
1798c2ecf20Sopenharmony_ci/* 2  : 324x248  80352 bytes */
1808c2ecf20Sopenharmony_ci/* 7  : 248x162  40176 bytes */
1818c2ecf20Sopenharmony_ci/* c+f: 162*124  20088 bytes */
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		{ 0x150e, 0x8e },
1848c2ecf20Sopenharmony_ci		{ 0x150f, 0x37 },
1858c2ecf20Sopenharmony_ci		{ 0x15c0, 0x00 },
1868c2ecf20Sopenharmony_ci		{ 0x15c3, 0x08 },	/* 0x04/0x14 ... test pictures ??? */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		{ 0x143f, 0x01 },	/* commit settings */
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	};
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) {
1948c2ecf20Sopenharmony_ci		err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0],
1958c2ecf20Sopenharmony_ci					       st6422_bridge_init[i][1]);
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	return err;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int setbrightness(struct sd *sd, s32 val)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	/* val goes from 0 -> 31 */
2048c2ecf20Sopenharmony_ci	return stv06xx_write_bridge(sd, 0x1432, val);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int setcontrast(struct sd *sd, s32 val)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	/* Val goes from 0 -> 15 */
2108c2ecf20Sopenharmony_ci	return stv06xx_write_bridge(sd, 0x143a, val | 0xf0);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int setgain(struct sd *sd, u8 gain)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	int err;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Set red, green, blue, gain */
2188c2ecf20Sopenharmony_ci	err = stv06xx_write_bridge(sd, 0x0509, gain);
2198c2ecf20Sopenharmony_ci	if (err < 0)
2208c2ecf20Sopenharmony_ci		return err;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	err = stv06xx_write_bridge(sd, 0x050a, gain);
2238c2ecf20Sopenharmony_ci	if (err < 0)
2248c2ecf20Sopenharmony_ci		return err;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	err = stv06xx_write_bridge(sd, 0x050b, gain);
2278c2ecf20Sopenharmony_ci	if (err < 0)
2288c2ecf20Sopenharmony_ci		return err;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* 2 mystery writes */
2318c2ecf20Sopenharmony_ci	err = stv06xx_write_bridge(sd, 0x050c, 0x2a);
2328c2ecf20Sopenharmony_ci	if (err < 0)
2338c2ecf20Sopenharmony_ci		return err;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return stv06xx_write_bridge(sd, 0x050d, 0x01);
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int setexposure(struct sd *sd, s16 expo)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	int err;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	err = stv06xx_write_bridge(sd, 0x143d, expo & 0xff);
2438c2ecf20Sopenharmony_ci	if (err < 0)
2448c2ecf20Sopenharmony_ci		return err;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return stv06xx_write_bridge(sd, 0x143e, expo >> 8);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic int st6422_start(struct sd *sd)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int err;
2528c2ecf20Sopenharmony_ci	struct cam *cam = &sd->gspca_dev.cam;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	if (cam->cam_mode[sd->gspca_dev.curr_mode].priv)
2558c2ecf20Sopenharmony_ci		err = stv06xx_write_bridge(sd, 0x1505, 0x0f);
2568c2ecf20Sopenharmony_ci	else
2578c2ecf20Sopenharmony_ci		err = stv06xx_write_bridge(sd, 0x1505, 0x02);
2588c2ecf20Sopenharmony_ci	if (err < 0)
2598c2ecf20Sopenharmony_ci		return err;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* commit settings */
2628c2ecf20Sopenharmony_ci	err = stv06xx_write_bridge(sd, 0x143f, 0x01);
2638c2ecf20Sopenharmony_ci	return (err < 0) ? err : 0;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int st6422_stop(struct sd *sd)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ci}
274