18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2005-2006 Micronas USA Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/module.h>
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/i2c.h>
98c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
108c2ecf20Sopenharmony_ci#include <linux/ioctl.h>
118c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
128c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TW9903 I2C subdev driver");
168c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * This driver is based on the wis-tw9903.c source that was in
208c2ecf20Sopenharmony_ci * drivers/staging/media/go7007. That source had commented out code for
218c2ecf20Sopenharmony_ci * saturation and scaling (neither seemed to work). If anyone ever gets
228c2ecf20Sopenharmony_ci * hardware to test this driver, then that code might be useful to look at.
238c2ecf20Sopenharmony_ci * You need to get the kernel sources of, say, kernel 3.8 where that
248c2ecf20Sopenharmony_ci * wis-tw9903 driver is still present.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct tw9903 {
288c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
298c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
308c2ecf20Sopenharmony_ci	v4l2_std_id norm;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic inline struct tw9903 *to_state(struct v4l2_subdev *sd)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	return container_of(sd, struct tw9903, sd);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const u8 initial_registers[] = {
398c2ecf20Sopenharmony_ci	0x02, 0x44, /* input 1, composite */
408c2ecf20Sopenharmony_ci	0x03, 0x92, /* correct digital format */
418c2ecf20Sopenharmony_ci	0x04, 0x00,
428c2ecf20Sopenharmony_ci	0x05, 0x80, /* or 0x00 for PAL */
438c2ecf20Sopenharmony_ci	0x06, 0x40, /* second internal current reference */
448c2ecf20Sopenharmony_ci	0x07, 0x02, /* window */
458c2ecf20Sopenharmony_ci	0x08, 0x14, /* window */
468c2ecf20Sopenharmony_ci	0x09, 0xf0, /* window */
478c2ecf20Sopenharmony_ci	0x0a, 0x81, /* window */
488c2ecf20Sopenharmony_ci	0x0b, 0xd0, /* window */
498c2ecf20Sopenharmony_ci	0x0c, 0x8c,
508c2ecf20Sopenharmony_ci	0x0d, 0x00, /* scaling */
518c2ecf20Sopenharmony_ci	0x0e, 0x11, /* scaling */
528c2ecf20Sopenharmony_ci	0x0f, 0x00, /* scaling */
538c2ecf20Sopenharmony_ci	0x10, 0x00, /* brightness */
548c2ecf20Sopenharmony_ci	0x11, 0x60, /* contrast */
558c2ecf20Sopenharmony_ci	0x12, 0x01, /* sharpness */
568c2ecf20Sopenharmony_ci	0x13, 0x7f, /* U gain */
578c2ecf20Sopenharmony_ci	0x14, 0x5a, /* V gain */
588c2ecf20Sopenharmony_ci	0x15, 0x00, /* hue */
598c2ecf20Sopenharmony_ci	0x16, 0xc3, /* sharpness */
608c2ecf20Sopenharmony_ci	0x18, 0x00,
618c2ecf20Sopenharmony_ci	0x19, 0x58, /* vbi */
628c2ecf20Sopenharmony_ci	0x1a, 0x80,
638c2ecf20Sopenharmony_ci	0x1c, 0x0f, /* video norm */
648c2ecf20Sopenharmony_ci	0x1d, 0x7f, /* video norm */
658c2ecf20Sopenharmony_ci	0x20, 0xa0, /* clamping gain (working 0x50) */
668c2ecf20Sopenharmony_ci	0x21, 0x22,
678c2ecf20Sopenharmony_ci	0x22, 0xf0,
688c2ecf20Sopenharmony_ci	0x23, 0xfe,
698c2ecf20Sopenharmony_ci	0x24, 0x3c,
708c2ecf20Sopenharmony_ci	0x25, 0x38,
718c2ecf20Sopenharmony_ci	0x26, 0x44,
728c2ecf20Sopenharmony_ci	0x27, 0x20,
738c2ecf20Sopenharmony_ci	0x28, 0x00,
748c2ecf20Sopenharmony_ci	0x29, 0x15,
758c2ecf20Sopenharmony_ci	0x2a, 0xa0,
768c2ecf20Sopenharmony_ci	0x2b, 0x44,
778c2ecf20Sopenharmony_ci	0x2c, 0x37,
788c2ecf20Sopenharmony_ci	0x2d, 0x00,
798c2ecf20Sopenharmony_ci	0x2e, 0xa5, /* burst PLL control (working: a9) */
808c2ecf20Sopenharmony_ci	0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
818c2ecf20Sopenharmony_ci	0x31, 0x00,
828c2ecf20Sopenharmony_ci	0x33, 0x22,
838c2ecf20Sopenharmony_ci	0x34, 0x11,
848c2ecf20Sopenharmony_ci	0x35, 0x35,
858c2ecf20Sopenharmony_ci	0x3b, 0x05,
868c2ecf20Sopenharmony_ci	0x06, 0xc0, /* reset device */
878c2ecf20Sopenharmony_ci	0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, reg, value);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int write_regs(struct v4l2_subdev *sd, const u8 *regs)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	int i;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	for (i = 0; regs[i] != 0x00; i += 2)
1028c2ecf20Sopenharmony_ci		if (write_reg(sd, regs[i], regs[i + 1]) < 0)
1038c2ecf20Sopenharmony_ci			return -1;
1048c2ecf20Sopenharmony_ci	return 0;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int tw9903_s_video_routing(struct v4l2_subdev *sd, u32 input,
1088c2ecf20Sopenharmony_ci				      u32 output, u32 config)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	write_reg(sd, 0x02, 0x40 | (input << 1));
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int tw9903_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct tw9903 *dec = to_state(sd);
1178c2ecf20Sopenharmony_ci	bool is_60hz = norm & V4L2_STD_525_60;
1188c2ecf20Sopenharmony_ci	static const u8 config_60hz[] = {
1198c2ecf20Sopenharmony_ci		0x05, 0x80,
1208c2ecf20Sopenharmony_ci		0x07, 0x02,
1218c2ecf20Sopenharmony_ci		0x08, 0x14,
1228c2ecf20Sopenharmony_ci		0x09, 0xf0,
1238c2ecf20Sopenharmony_ci		0,    0,
1248c2ecf20Sopenharmony_ci	};
1258c2ecf20Sopenharmony_ci	static const u8 config_50hz[] = {
1268c2ecf20Sopenharmony_ci		0x05, 0x00,
1278c2ecf20Sopenharmony_ci		0x07, 0x12,
1288c2ecf20Sopenharmony_ci		0x08, 0x18,
1298c2ecf20Sopenharmony_ci		0x09, 0x20,
1308c2ecf20Sopenharmony_ci		0,    0,
1318c2ecf20Sopenharmony_ci	};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	write_regs(sd, is_60hz ? config_60hz : config_50hz);
1348c2ecf20Sopenharmony_ci	dec->norm = norm;
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int tw9903_s_ctrl(struct v4l2_ctrl *ctrl)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct tw9903 *dec = container_of(ctrl->handler, struct tw9903, hdl);
1428c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &dec->sd;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	switch (ctrl->id) {
1458c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
1468c2ecf20Sopenharmony_ci		write_reg(sd, 0x10, ctrl->val);
1478c2ecf20Sopenharmony_ci		break;
1488c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
1498c2ecf20Sopenharmony_ci		write_reg(sd, 0x11, ctrl->val);
1508c2ecf20Sopenharmony_ci		break;
1518c2ecf20Sopenharmony_ci	case V4L2_CID_HUE:
1528c2ecf20Sopenharmony_ci		write_reg(sd, 0x15, ctrl->val);
1538c2ecf20Sopenharmony_ci		break;
1548c2ecf20Sopenharmony_ci	default:
1558c2ecf20Sopenharmony_ci		return -EINVAL;
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci	return 0;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int tw9903_log_status(struct v4l2_subdev *sd)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct tw9903 *dec = to_state(sd);
1638c2ecf20Sopenharmony_ci	bool is_60hz = dec->norm & V4L2_STD_525_60;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
1668c2ecf20Sopenharmony_ci	v4l2_ctrl_subdev_log_status(sd);
1678c2ecf20Sopenharmony_ci	return 0;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/* --------------------------------------------------------------------------*/
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops tw9903_ctrl_ops = {
1738c2ecf20Sopenharmony_ci	.s_ctrl = tw9903_s_ctrl,
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops tw9903_core_ops = {
1778c2ecf20Sopenharmony_ci	.log_status = tw9903_log_status,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops tw9903_video_ops = {
1818c2ecf20Sopenharmony_ci	.s_std = tw9903_s_std,
1828c2ecf20Sopenharmony_ci	.s_routing = tw9903_s_video_routing,
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops tw9903_ops = {
1868c2ecf20Sopenharmony_ci	.core = &tw9903_core_ops,
1878c2ecf20Sopenharmony_ci	.video = &tw9903_video_ops,
1888c2ecf20Sopenharmony_ci};
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/* --------------------------------------------------------------------------*/
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int tw9903_probe(struct i2c_client *client,
1938c2ecf20Sopenharmony_ci			     const struct i2c_device_id *id)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct tw9903 *dec;
1968c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
1978c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* Check if the adapter supports the needed features */
2008c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2018c2ecf20Sopenharmony_ci		return -EIO;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%02x (%s)\n",
2048c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	dec = devm_kzalloc(&client->dev, sizeof(*dec), GFP_KERNEL);
2078c2ecf20Sopenharmony_ci	if (dec == NULL)
2088c2ecf20Sopenharmony_ci		return -ENOMEM;
2098c2ecf20Sopenharmony_ci	sd = &dec->sd;
2108c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &tw9903_ops);
2118c2ecf20Sopenharmony_ci	hdl = &dec->hdl;
2128c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(hdl, 4);
2138c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
2148c2ecf20Sopenharmony_ci		V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2158c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
2168c2ecf20Sopenharmony_ci		V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
2178c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
2188c2ecf20Sopenharmony_ci		V4L2_CID_HUE, -128, 127, 1, 0);
2198c2ecf20Sopenharmony_ci	sd->ctrl_handler = hdl;
2208c2ecf20Sopenharmony_ci	if (hdl->error) {
2218c2ecf20Sopenharmony_ci		int err = hdl->error;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(hdl);
2248c2ecf20Sopenharmony_ci		return err;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* Initialize tw9903 */
2288c2ecf20Sopenharmony_ci	dec->norm = V4L2_STD_NTSC;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (write_regs(sd, initial_registers) < 0) {
2318c2ecf20Sopenharmony_ci		v4l2_err(client, "error initializing TW9903\n");
2328c2ecf20Sopenharmony_ci		return -EINVAL;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return 0;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int tw9903_remove(struct i2c_client *client)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
2438c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&to_state(sd)->hdl);
2448c2ecf20Sopenharmony_ci	return 0;
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic const struct i2c_device_id tw9903_id[] = {
2508c2ecf20Sopenharmony_ci	{ "tw9903", 0 },
2518c2ecf20Sopenharmony_ci	{ }
2528c2ecf20Sopenharmony_ci};
2538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tw9903_id);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic struct i2c_driver tw9903_driver = {
2568c2ecf20Sopenharmony_ci	.driver = {
2578c2ecf20Sopenharmony_ci		.name	= "tw9903",
2588c2ecf20Sopenharmony_ci	},
2598c2ecf20Sopenharmony_ci	.probe = tw9903_probe,
2608c2ecf20Sopenharmony_ci	.remove = tw9903_remove,
2618c2ecf20Sopenharmony_ci	.id_table = tw9903_id,
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_cimodule_i2c_driver(tw9903_driver);
264