18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci    bt866 - BT866 Digital Video Encoder (Rockwell Part)
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci    Copyright (C) 1999 Mike Bernson <mike@mlb.org>
68c2ecf20Sopenharmony_ci    Copyright (C) 1998 Dave Perks <dperks@ibm.net>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci    Modifications for LML33/DC10plus unified driver
98c2ecf20Sopenharmony_ci    Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci    This code was modify/ported from the saa7111 driver written
128c2ecf20Sopenharmony_ci    by Dave Perks.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci    This code was adapted for the bt866 by Christer Weinigel and ported
158c2ecf20Sopenharmony_ci    to 2.6 by Martin Samuelsson.
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci*/
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/types.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/ioctl.h>
238c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
248c2ecf20Sopenharmony_ci#include <linux/i2c.h>
258c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Brooktree-866 video encoder driver");
298c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mike Bernson & Dave Perks");
308c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int debug;
338c2ecf20Sopenharmony_cimodule_param(debug, int, 0);
348c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level (0-1)");
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct bt866 {
408c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
418c2ecf20Sopenharmony_ci	u8 reg[256];
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic inline struct bt866 *to_bt866(struct v4l2_subdev *sd)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	return container_of(sd, struct bt866, sd);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int bt866_write(struct bt866 *encoder, u8 subaddr, u8 data)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
528c2ecf20Sopenharmony_ci	u8 buffer[2];
538c2ecf20Sopenharmony_ci	int err;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	buffer[0] = subaddr;
568c2ecf20Sopenharmony_ci	buffer[1] = data;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	encoder->reg[subaddr] = data;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	v4l_dbg(1, debug, client, "write 0x%02x = 0x%02x\n", subaddr, data);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	for (err = 0; err < 3;) {
638c2ecf20Sopenharmony_ci		if (i2c_master_send(client, buffer, 2) == 2)
648c2ecf20Sopenharmony_ci			break;
658c2ecf20Sopenharmony_ci		err++;
668c2ecf20Sopenharmony_ci		v4l_warn(client, "error #%d writing to 0x%02x\n",
678c2ecf20Sopenharmony_ci				err, subaddr);
688c2ecf20Sopenharmony_ci		schedule_timeout_interruptible(msecs_to_jiffies(100));
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci	if (err == 3) {
718c2ecf20Sopenharmony_ci		v4l_warn(client, "giving up\n");
728c2ecf20Sopenharmony_ci		return -1;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return 0;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int bt866_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* Only PAL supported by this driver at the moment! */
838c2ecf20Sopenharmony_ci	if (!(std & V4L2_STD_NTSC))
848c2ecf20Sopenharmony_ci		return -EINVAL;
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int bt866_s_routing(struct v4l2_subdev *sd,
898c2ecf20Sopenharmony_ci			   u32 input, u32 output, u32 config)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	static const __u8 init[] = {
928c2ecf20Sopenharmony_ci		0xc8, 0xcc, /* CRSCALE */
938c2ecf20Sopenharmony_ci		0xca, 0x91, /* CBSCALE */
948c2ecf20Sopenharmony_ci		0xcc, 0x24, /* YC16 | OSDNUM */
958c2ecf20Sopenharmony_ci		0xda, 0x00, /*  */
968c2ecf20Sopenharmony_ci		0xdc, 0x24, /* SETMODE | PAL */
978c2ecf20Sopenharmony_ci		0xde, 0x02, /* EACTIVE */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci		/* overlay colors */
1008c2ecf20Sopenharmony_ci		0x70, 0xEB, 0x90, 0x80, 0xB0, 0x80, /* white */
1018c2ecf20Sopenharmony_ci		0x72, 0xA2, 0x92, 0x8E, 0xB2, 0x2C, /* yellow */
1028c2ecf20Sopenharmony_ci		0x74, 0x83, 0x94, 0x2C, 0xB4, 0x9C, /* cyan */
1038c2ecf20Sopenharmony_ci		0x76, 0x70, 0x96, 0x3A, 0xB6, 0x48, /* green */
1048c2ecf20Sopenharmony_ci		0x78, 0x54, 0x98, 0xC6, 0xB8, 0xB8, /* magenta */
1058c2ecf20Sopenharmony_ci		0x7A, 0x41, 0x9A, 0xD4, 0xBA, 0x64, /* red */
1068c2ecf20Sopenharmony_ci		0x7C, 0x23, 0x9C, 0x72, 0xBC, 0xD4, /* blue */
1078c2ecf20Sopenharmony_ci		0x7E, 0x10, 0x9E, 0x80, 0xBE, 0x80, /* black */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		0x60, 0xEB, 0x80, 0x80, 0xc0, 0x80, /* white */
1108c2ecf20Sopenharmony_ci		0x62, 0xA2, 0x82, 0x8E, 0xc2, 0x2C, /* yellow */
1118c2ecf20Sopenharmony_ci		0x64, 0x83, 0x84, 0x2C, 0xc4, 0x9C, /* cyan */
1128c2ecf20Sopenharmony_ci		0x66, 0x70, 0x86, 0x3A, 0xc6, 0x48, /* green */
1138c2ecf20Sopenharmony_ci		0x68, 0x54, 0x88, 0xC6, 0xc8, 0xB8, /* magenta */
1148c2ecf20Sopenharmony_ci		0x6A, 0x41, 0x8A, 0xD4, 0xcA, 0x64, /* red */
1158c2ecf20Sopenharmony_ci		0x6C, 0x23, 0x8C, 0x72, 0xcC, 0xD4, /* blue */
1168c2ecf20Sopenharmony_ci		0x6E, 0x10, 0x8E, 0x80, 0xcE, 0x80, /* black */
1178c2ecf20Sopenharmony_ci	};
1188c2ecf20Sopenharmony_ci	struct bt866 *encoder = to_bt866(sd);
1198c2ecf20Sopenharmony_ci	u8 val;
1208c2ecf20Sopenharmony_ci	int i;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(init) / 2; i += 2)
1238c2ecf20Sopenharmony_ci		bt866_write(encoder, init[i], init[i+1]);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	val = encoder->reg[0xdc];
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	if (input == 0)
1288c2ecf20Sopenharmony_ci		val |= 0x40; /* CBSWAP */
1298c2ecf20Sopenharmony_ci	else
1308c2ecf20Sopenharmony_ci		val &= ~0x40; /* !CBSWAP */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	bt866_write(encoder, 0xdc, val);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	val = encoder->reg[0xcc];
1358c2ecf20Sopenharmony_ci	if (input == 2)
1368c2ecf20Sopenharmony_ci		val |= 0x01; /* OSDBAR */
1378c2ecf20Sopenharmony_ci	else
1388c2ecf20Sopenharmony_ci		val &= ~0x01; /* !OSDBAR */
1398c2ecf20Sopenharmony_ci	bt866_write(encoder, 0xcc, val);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "set input %d\n", input);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	switch (input) {
1448c2ecf20Sopenharmony_ci	case 0:
1458c2ecf20Sopenharmony_ci	case 1:
1468c2ecf20Sopenharmony_ci	case 2:
1478c2ecf20Sopenharmony_ci		break;
1488c2ecf20Sopenharmony_ci	default:
1498c2ecf20Sopenharmony_ci		return -EINVAL;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	return 0;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#if 0
1558c2ecf20Sopenharmony_ci/* Code to setup square pixels, might be of some use in the future,
1568c2ecf20Sopenharmony_ci   but is currently unused. */
1578c2ecf20Sopenharmony_ci	val = encoder->reg[0xdc];
1588c2ecf20Sopenharmony_ci	if (*iarg)
1598c2ecf20Sopenharmony_ci		val |= 1; /* SQUARE */
1608c2ecf20Sopenharmony_ci	else
1618c2ecf20Sopenharmony_ci		val &= ~1; /* !SQUARE */
1628c2ecf20Sopenharmony_ci	bt866_write(client, 0xdc, val);
1638c2ecf20Sopenharmony_ci#endif
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops bt866_video_ops = {
1688c2ecf20Sopenharmony_ci	.s_std_output = bt866_s_std_output,
1698c2ecf20Sopenharmony_ci	.s_routing = bt866_s_routing,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops bt866_ops = {
1738c2ecf20Sopenharmony_ci	.video = &bt866_video_ops,
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic int bt866_probe(struct i2c_client *client,
1778c2ecf20Sopenharmony_ci			const struct i2c_device_id *id)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	struct bt866 *encoder;
1808c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%x (%s)\n",
1838c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
1868c2ecf20Sopenharmony_ci	if (encoder == NULL)
1878c2ecf20Sopenharmony_ci		return -ENOMEM;
1888c2ecf20Sopenharmony_ci	sd = &encoder->sd;
1898c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &bt866_ops);
1908c2ecf20Sopenharmony_ci	return 0;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic int bt866_remove(struct i2c_client *client)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic const struct i2c_device_id bt866_id[] = {
2028c2ecf20Sopenharmony_ci	{ "bt866", 0 },
2038c2ecf20Sopenharmony_ci	{ }
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, bt866_id);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic struct i2c_driver bt866_driver = {
2088c2ecf20Sopenharmony_ci	.driver = {
2098c2ecf20Sopenharmony_ci		.name	= "bt866",
2108c2ecf20Sopenharmony_ci	},
2118c2ecf20Sopenharmony_ci	.probe		= bt866_probe,
2128c2ecf20Sopenharmony_ci	.remove		= bt866_remove,
2138c2ecf20Sopenharmony_ci	.id_table	= bt866_id,
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cimodule_i2c_driver(bt866_driver);
217