18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * upd64031A - NEC Electronics Ghost Reduction for NTSC in Japan
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * 2003 by T.Adachi <tadachi@tadachi-net.com>
68c2ecf20Sopenharmony_ci * 2003 by Takeru KOMORIYA <komoriya@paken.org>
78c2ecf20Sopenharmony_ci * 2006 by Hans Verkuil <hverkuil@xs4all.nl>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
178c2ecf20Sopenharmony_ci#include <media/i2c/upd64031a.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* --------------------- read registers functions define -------------------- */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* bit masks */
228c2ecf20Sopenharmony_ci#define GR_MODE_MASK              0xc0
238c2ecf20Sopenharmony_ci#define DIRECT_3DYCS_CONNECT_MASK 0xc0
248c2ecf20Sopenharmony_ci#define SYNC_CIRCUIT_MASK         0xa0
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* -------------------------------------------------------------------------- */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("uPD64031A driver");
298c2ecf20Sopenharmony_ciMODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil");
308c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int debug;
338c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level (0-1)");
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cienum {
398c2ecf20Sopenharmony_ci	R00 = 0, R01, R02, R03, R04,
408c2ecf20Sopenharmony_ci	R05, R06, R07, R08, R09,
418c2ecf20Sopenharmony_ci	R0A, R0B, R0C, R0D, R0E, R0F,
428c2ecf20Sopenharmony_ci	/* unused registers
438c2ecf20Sopenharmony_ci	 R10, R11, R12, R13, R14,
448c2ecf20Sopenharmony_ci	 R15, R16, R17,
458c2ecf20Sopenharmony_ci	 */
468c2ecf20Sopenharmony_ci	TOT_REGS
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct upd64031a_state {
508c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
518c2ecf20Sopenharmony_ci	u8 regs[TOT_REGS];
528c2ecf20Sopenharmony_ci	u8 gr_mode;
538c2ecf20Sopenharmony_ci	u8 direct_3dycs_connect;
548c2ecf20Sopenharmony_ci	u8 ext_comp_sync;
558c2ecf20Sopenharmony_ci	u8 ext_vert_sync;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline struct upd64031a_state *to_state(struct v4l2_subdev *sd)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return container_of(sd, struct upd64031a_state, sd);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic u8 upd64031a_init[] = {
648c2ecf20Sopenharmony_ci	0x00, 0xb8, 0x48, 0xd2, 0xe6,
658c2ecf20Sopenharmony_ci	0x03, 0x10, 0x0b, 0xaf, 0x7f,
668c2ecf20Sopenharmony_ci	0x00, 0x00, 0x1d, 0x5e, 0x00,
678c2ecf20Sopenharmony_ci	0xd0
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------ */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic u8 upd64031a_read(struct v4l2_subdev *sd, u8 reg)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
758c2ecf20Sopenharmony_ci	u8 buf[2];
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (reg >= sizeof(buf))
788c2ecf20Sopenharmony_ci		return 0xff;
798c2ecf20Sopenharmony_ci	i2c_master_recv(client, buf, 2);
808c2ecf20Sopenharmony_ci	return buf[reg];
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------ */
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void upd64031a_write(struct v4l2_subdev *sd, u8 reg, u8 val)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
888c2ecf20Sopenharmony_ci	u8 buf[2];
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	buf[0] = reg;
918c2ecf20Sopenharmony_ci	buf[1] = val;
928c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "write reg: %02X val: %02X\n", reg, val);
938c2ecf20Sopenharmony_ci	if (i2c_master_send(client, buf, 2) != 2)
948c2ecf20Sopenharmony_ci		v4l2_err(sd, "I/O error write 0x%02x/0x%02x\n", reg, val);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------ */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* The input changed due to new input or channel changed */
1008c2ecf20Sopenharmony_cistatic int upd64031a_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct upd64031a_state *state = to_state(sd);
1038c2ecf20Sopenharmony_ci	u8 reg = state->regs[R00];
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "changed input or channel\n");
1068c2ecf20Sopenharmony_ci	upd64031a_write(sd, R00, reg | 0x10);
1078c2ecf20Sopenharmony_ci	upd64031a_write(sd, R00, reg & ~0x10);
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------ */
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int upd64031a_s_routing(struct v4l2_subdev *sd,
1148c2ecf20Sopenharmony_ci			       u32 input, u32 output, u32 config)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct upd64031a_state *state = to_state(sd);
1178c2ecf20Sopenharmony_ci	u8 r00, r05, r08;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	state->gr_mode = (input & 3) << 6;
1208c2ecf20Sopenharmony_ci	state->direct_3dycs_connect = (input & 0xc) << 4;
1218c2ecf20Sopenharmony_ci	state->ext_comp_sync =
1228c2ecf20Sopenharmony_ci		(input & UPD64031A_COMPOSITE_EXTERNAL) << 1;
1238c2ecf20Sopenharmony_ci	state->ext_vert_sync =
1248c2ecf20Sopenharmony_ci		(input & UPD64031A_VERTICAL_EXTERNAL) << 2;
1258c2ecf20Sopenharmony_ci	r00 = (state->regs[R00] & ~GR_MODE_MASK) | state->gr_mode;
1268c2ecf20Sopenharmony_ci	r05 = (state->regs[R00] & ~SYNC_CIRCUIT_MASK) |
1278c2ecf20Sopenharmony_ci		state->ext_comp_sync | state->ext_vert_sync;
1288c2ecf20Sopenharmony_ci	r08 = (state->regs[R08] & ~DIRECT_3DYCS_CONNECT_MASK) |
1298c2ecf20Sopenharmony_ci		state->direct_3dycs_connect;
1308c2ecf20Sopenharmony_ci	upd64031a_write(sd, R00, r00);
1318c2ecf20Sopenharmony_ci	upd64031a_write(sd, R05, r05);
1328c2ecf20Sopenharmony_ci	upd64031a_write(sd, R08, r08);
1338c2ecf20Sopenharmony_ci	return upd64031a_s_frequency(sd, NULL);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic int upd64031a_log_status(struct v4l2_subdev *sd)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	v4l2_info(sd, "Status: SA00=0x%02x SA01=0x%02x\n",
1398c2ecf20Sopenharmony_ci			upd64031a_read(sd, 0), upd64031a_read(sd, 1));
1408c2ecf20Sopenharmony_ci	return 0;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
1448c2ecf20Sopenharmony_cistatic int upd64031a_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	reg->val = upd64031a_read(sd, reg->reg & 0xff);
1478c2ecf20Sopenharmony_ci	reg->size = 1;
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int upd64031a_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	upd64031a_write(sd, reg->reg & 0xff, reg->val & 0xff);
1548c2ecf20Sopenharmony_ci	return 0;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci#endif
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops upd64031a_core_ops = {
1618c2ecf20Sopenharmony_ci	.log_status = upd64031a_log_status,
1628c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
1638c2ecf20Sopenharmony_ci	.g_register = upd64031a_g_register,
1648c2ecf20Sopenharmony_ci	.s_register = upd64031a_s_register,
1658c2ecf20Sopenharmony_ci#endif
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_tuner_ops upd64031a_tuner_ops = {
1698c2ecf20Sopenharmony_ci	.s_frequency = upd64031a_s_frequency,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops upd64031a_video_ops = {
1738c2ecf20Sopenharmony_ci	.s_routing = upd64031a_s_routing,
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops upd64031a_ops = {
1778c2ecf20Sopenharmony_ci	.core = &upd64031a_core_ops,
1788c2ecf20Sopenharmony_ci	.tuner = &upd64031a_tuner_ops,
1798c2ecf20Sopenharmony_ci	.video = &upd64031a_video_ops,
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------ */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* i2c implementation */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int upd64031a_probe(struct i2c_client *client,
1878c2ecf20Sopenharmony_ci			   const struct i2c_device_id *id)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct upd64031a_state *state;
1908c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
1918c2ecf20Sopenharmony_ci	int i;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1948c2ecf20Sopenharmony_ci		return -EIO;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%x (%s)\n",
1978c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
2008c2ecf20Sopenharmony_ci	if (state == NULL)
2018c2ecf20Sopenharmony_ci		return -ENOMEM;
2028c2ecf20Sopenharmony_ci	sd = &state->sd;
2038c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &upd64031a_ops);
2048c2ecf20Sopenharmony_ci	memcpy(state->regs, upd64031a_init, sizeof(state->regs));
2058c2ecf20Sopenharmony_ci	state->gr_mode = UPD64031A_GR_ON << 6;
2068c2ecf20Sopenharmony_ci	state->direct_3dycs_connect = UPD64031A_3DYCS_COMPOSITE << 4;
2078c2ecf20Sopenharmony_ci	state->ext_comp_sync = state->ext_vert_sync = 0;
2088c2ecf20Sopenharmony_ci	for (i = 0; i < TOT_REGS; i++)
2098c2ecf20Sopenharmony_ci		upd64031a_write(sd, i, state->regs[i]);
2108c2ecf20Sopenharmony_ci	return 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int upd64031a_remove(struct i2c_client *client)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
2188c2ecf20Sopenharmony_ci	return 0;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------- */
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic const struct i2c_device_id upd64031a_id[] = {
2248c2ecf20Sopenharmony_ci	{ "upd64031a", 0 },
2258c2ecf20Sopenharmony_ci	{ }
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, upd64031a_id);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic struct i2c_driver upd64031a_driver = {
2308c2ecf20Sopenharmony_ci	.driver = {
2318c2ecf20Sopenharmony_ci		.name	= "upd64031a",
2328c2ecf20Sopenharmony_ci	},
2338c2ecf20Sopenharmony_ci	.probe		= upd64031a_probe,
2348c2ecf20Sopenharmony_ci	.remove		= upd64031a_remove,
2358c2ecf20Sopenharmony_ci	.id_table	= upd64031a_id,
2368c2ecf20Sopenharmony_ci};
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cimodule_i2c_driver(upd64031a_driver);
239