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 <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
138c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
148c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define TW2804_REG_AUTOGAIN		0x02
178c2ecf20Sopenharmony_ci#define TW2804_REG_HUE			0x0f
188c2ecf20Sopenharmony_ci#define TW2804_REG_SATURATION		0x10
198c2ecf20Sopenharmony_ci#define TW2804_REG_CONTRAST		0x11
208c2ecf20Sopenharmony_ci#define TW2804_REG_BRIGHTNESS		0x12
218c2ecf20Sopenharmony_ci#define TW2804_REG_COLOR_KILLER		0x14
228c2ecf20Sopenharmony_ci#define TW2804_REG_GAIN			0x3c
238c2ecf20Sopenharmony_ci#define TW2804_REG_CHROMA_GAIN		0x3d
248c2ecf20Sopenharmony_ci#define TW2804_REG_BLUE_BALANCE		0x3e
258c2ecf20Sopenharmony_ci#define TW2804_REG_RED_BALANCE		0x3f
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct tw2804 {
288c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
298c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
308c2ecf20Sopenharmony_ci	u8 channel:2;
318c2ecf20Sopenharmony_ci	u8 input:1;
328c2ecf20Sopenharmony_ci	int norm;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const u8 global_registers[] = {
368c2ecf20Sopenharmony_ci	0x39, 0x00,
378c2ecf20Sopenharmony_ci	0x3a, 0xff,
388c2ecf20Sopenharmony_ci	0x3b, 0x84,
398c2ecf20Sopenharmony_ci	0x3c, 0x80,
408c2ecf20Sopenharmony_ci	0x3d, 0x80,
418c2ecf20Sopenharmony_ci	0x3e, 0x82,
428c2ecf20Sopenharmony_ci	0x3f, 0x82,
438c2ecf20Sopenharmony_ci	0x78, 0x00,
448c2ecf20Sopenharmony_ci	0xff, 0xff, /* Terminator (reg 0xff does not exist) */
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic const u8 channel_registers[] = {
488c2ecf20Sopenharmony_ci	0x01, 0xc4,
498c2ecf20Sopenharmony_ci	0x02, 0xa5,
508c2ecf20Sopenharmony_ci	0x03, 0x20,
518c2ecf20Sopenharmony_ci	0x04, 0xd0,
528c2ecf20Sopenharmony_ci	0x05, 0x20,
538c2ecf20Sopenharmony_ci	0x06, 0xd0,
548c2ecf20Sopenharmony_ci	0x07, 0x88,
558c2ecf20Sopenharmony_ci	0x08, 0x20,
568c2ecf20Sopenharmony_ci	0x09, 0x07,
578c2ecf20Sopenharmony_ci	0x0a, 0xf0,
588c2ecf20Sopenharmony_ci	0x0b, 0x07,
598c2ecf20Sopenharmony_ci	0x0c, 0xf0,
608c2ecf20Sopenharmony_ci	0x0d, 0x40,
618c2ecf20Sopenharmony_ci	0x0e, 0xd2,
628c2ecf20Sopenharmony_ci	0x0f, 0x80,
638c2ecf20Sopenharmony_ci	0x10, 0x80,
648c2ecf20Sopenharmony_ci	0x11, 0x80,
658c2ecf20Sopenharmony_ci	0x12, 0x80,
668c2ecf20Sopenharmony_ci	0x13, 0x1f,
678c2ecf20Sopenharmony_ci	0x14, 0x00,
688c2ecf20Sopenharmony_ci	0x15, 0x00,
698c2ecf20Sopenharmony_ci	0x16, 0x00,
708c2ecf20Sopenharmony_ci	0x17, 0x00,
718c2ecf20Sopenharmony_ci	0x18, 0xff,
728c2ecf20Sopenharmony_ci	0x19, 0xff,
738c2ecf20Sopenharmony_ci	0x1a, 0xff,
748c2ecf20Sopenharmony_ci	0x1b, 0xff,
758c2ecf20Sopenharmony_ci	0x1c, 0xff,
768c2ecf20Sopenharmony_ci	0x1d, 0xff,
778c2ecf20Sopenharmony_ci	0x1e, 0xff,
788c2ecf20Sopenharmony_ci	0x1f, 0xff,
798c2ecf20Sopenharmony_ci	0x20, 0x07,
808c2ecf20Sopenharmony_ci	0x21, 0x07,
818c2ecf20Sopenharmony_ci	0x22, 0x00,
828c2ecf20Sopenharmony_ci	0x23, 0x91,
838c2ecf20Sopenharmony_ci	0x24, 0x51,
848c2ecf20Sopenharmony_ci	0x25, 0x03,
858c2ecf20Sopenharmony_ci	0x26, 0x00,
868c2ecf20Sopenharmony_ci	0x27, 0x00,
878c2ecf20Sopenharmony_ci	0x28, 0x00,
888c2ecf20Sopenharmony_ci	0x29, 0x00,
898c2ecf20Sopenharmony_ci	0x2a, 0x00,
908c2ecf20Sopenharmony_ci	0x2b, 0x00,
918c2ecf20Sopenharmony_ci	0x2c, 0x00,
928c2ecf20Sopenharmony_ci	0x2d, 0x00,
938c2ecf20Sopenharmony_ci	0x2e, 0x00,
948c2ecf20Sopenharmony_ci	0x2f, 0x00,
958c2ecf20Sopenharmony_ci	0x30, 0x00,
968c2ecf20Sopenharmony_ci	0x31, 0x00,
978c2ecf20Sopenharmony_ci	0x32, 0x00,
988c2ecf20Sopenharmony_ci	0x33, 0x00,
998c2ecf20Sopenharmony_ci	0x34, 0x00,
1008c2ecf20Sopenharmony_ci	0x35, 0x00,
1018c2ecf20Sopenharmony_ci	0x36, 0x00,
1028c2ecf20Sopenharmony_ci	0x37, 0x00,
1038c2ecf20Sopenharmony_ci	0xff, 0xff, /* Terminator (reg 0xff does not exist) */
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int write_reg(struct i2c_client *client, u8 reg, u8 value, u8 channel)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int write_regs(struct i2c_client *client, const u8 *regs, u8 channel)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	int ret;
1148c2ecf20Sopenharmony_ci	int i;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	for (i = 0; regs[i] != 0xff; i += 2) {
1178c2ecf20Sopenharmony_ci		ret = i2c_smbus_write_byte_data(client,
1188c2ecf20Sopenharmony_ci				regs[i] | (channel << 6), regs[i + 1]);
1198c2ecf20Sopenharmony_ci		if (ret < 0)
1208c2ecf20Sopenharmony_ci			return ret;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci	return 0;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int read_reg(struct i2c_client *client, u8 reg, u8 channel)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	return i2c_smbus_read_byte_data(client, (reg) | (channel << 6));
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic inline struct tw2804 *to_state(struct v4l2_subdev *sd)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	return container_of(sd, struct tw2804, sd);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline struct tw2804 *to_state_from_ctrl(struct v4l2_ctrl *ctrl)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return container_of(ctrl->handler, struct tw2804, hdl);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic int tw2804_log_status(struct v4l2_subdev *sd)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct tw2804 *state = to_state(sd);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	v4l2_info(sd, "Standard: %s\n",
1458c2ecf20Sopenharmony_ci			state->norm & V4L2_STD_525_60 ? "60 Hz" : "50 Hz");
1468c2ecf20Sopenharmony_ci	v4l2_info(sd, "Channel: %d\n", state->channel);
1478c2ecf20Sopenharmony_ci	v4l2_info(sd, "Input: %d\n", state->input);
1488c2ecf20Sopenharmony_ci	return v4l2_ctrl_subdev_log_status(sd);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/*
1528c2ecf20Sopenharmony_ci * These volatile controls are needed because all four channels share
1538c2ecf20Sopenharmony_ci * these controls. So a change made to them through one channel would
1548c2ecf20Sopenharmony_ci * require another channel to be updated.
1558c2ecf20Sopenharmony_ci *
1568c2ecf20Sopenharmony_ci * Normally this would have been done in a different way, but since the one
1578c2ecf20Sopenharmony_ci * board that uses this driver sees this single chip as if it was on four
1588c2ecf20Sopenharmony_ci * different i2c adapters (each adapter belonging to a separate instance of
1598c2ecf20Sopenharmony_ci * the same USB driver) there is no reliable method that I have found to let
1608c2ecf20Sopenharmony_ci * the instances know about each other.
1618c2ecf20Sopenharmony_ci *
1628c2ecf20Sopenharmony_ci * So implementing these global registers as volatile is the best we can do.
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistatic int tw2804_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct tw2804 *state = to_state_from_ctrl(ctrl);
1678c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	switch (ctrl->id) {
1708c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN:
1718c2ecf20Sopenharmony_ci		ctrl->val = read_reg(client, TW2804_REG_GAIN, 0);
1728c2ecf20Sopenharmony_ci		return 0;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	case V4L2_CID_CHROMA_GAIN:
1758c2ecf20Sopenharmony_ci		ctrl->val = read_reg(client, TW2804_REG_CHROMA_GAIN, 0);
1768c2ecf20Sopenharmony_ci		return 0;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	case V4L2_CID_BLUE_BALANCE:
1798c2ecf20Sopenharmony_ci		ctrl->val = read_reg(client, TW2804_REG_BLUE_BALANCE, 0);
1808c2ecf20Sopenharmony_ci		return 0;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	case V4L2_CID_RED_BALANCE:
1838c2ecf20Sopenharmony_ci		ctrl->val = read_reg(client, TW2804_REG_RED_BALANCE, 0);
1848c2ecf20Sopenharmony_ci		return 0;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci	return 0;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int tw2804_s_ctrl(struct v4l2_ctrl *ctrl)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct tw2804 *state = to_state_from_ctrl(ctrl);
1928c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
1938c2ecf20Sopenharmony_ci	int addr;
1948c2ecf20Sopenharmony_ci	int reg;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	switch (ctrl->id) {
1978c2ecf20Sopenharmony_ci	case V4L2_CID_AUTOGAIN:
1988c2ecf20Sopenharmony_ci		addr = TW2804_REG_AUTOGAIN;
1998c2ecf20Sopenharmony_ci		reg = read_reg(client, addr, state->channel);
2008c2ecf20Sopenharmony_ci		if (reg < 0)
2018c2ecf20Sopenharmony_ci			return reg;
2028c2ecf20Sopenharmony_ci		if (ctrl->val == 0)
2038c2ecf20Sopenharmony_ci			reg &= ~(1 << 7);
2048c2ecf20Sopenharmony_ci		else
2058c2ecf20Sopenharmony_ci			reg |= 1 << 7;
2068c2ecf20Sopenharmony_ci		return write_reg(client, addr, reg, state->channel);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	case V4L2_CID_COLOR_KILLER:
2098c2ecf20Sopenharmony_ci		addr = TW2804_REG_COLOR_KILLER;
2108c2ecf20Sopenharmony_ci		reg = read_reg(client, addr, state->channel);
2118c2ecf20Sopenharmony_ci		if (reg < 0)
2128c2ecf20Sopenharmony_ci			return reg;
2138c2ecf20Sopenharmony_ci		reg = (reg & ~(0x03)) | (ctrl->val == 0 ? 0x02 : 0x03);
2148c2ecf20Sopenharmony_ci		return write_reg(client, addr, reg, state->channel);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN:
2178c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_GAIN, ctrl->val, 0);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	case V4L2_CID_CHROMA_GAIN:
2208c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_CHROMA_GAIN, ctrl->val, 0);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	case V4L2_CID_BLUE_BALANCE:
2238c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_BLUE_BALANCE, ctrl->val, 0);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	case V4L2_CID_RED_BALANCE:
2268c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_RED_BALANCE, ctrl->val, 0);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
2298c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_BRIGHTNESS,
2308c2ecf20Sopenharmony_ci				ctrl->val, state->channel);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
2338c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_CONTRAST,
2348c2ecf20Sopenharmony_ci				ctrl->val, state->channel);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
2378c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_SATURATION,
2388c2ecf20Sopenharmony_ci				ctrl->val, state->channel);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	case V4L2_CID_HUE:
2418c2ecf20Sopenharmony_ci		return write_reg(client, TW2804_REG_HUE,
2428c2ecf20Sopenharmony_ci				ctrl->val, state->channel);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	default:
2458c2ecf20Sopenharmony_ci		break;
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci	return -EINVAL;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic int tw2804_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct tw2804 *dec = to_state(sd);
2538c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
2548c2ecf20Sopenharmony_ci	bool is_60hz = norm & V4L2_STD_525_60;
2558c2ecf20Sopenharmony_ci	u8 regs[] = {
2568c2ecf20Sopenharmony_ci		0x01, is_60hz ? 0xc4 : 0x84,
2578c2ecf20Sopenharmony_ci		0x09, is_60hz ? 0x07 : 0x04,
2588c2ecf20Sopenharmony_ci		0x0a, is_60hz ? 0xf0 : 0x20,
2598c2ecf20Sopenharmony_ci		0x0b, is_60hz ? 0x07 : 0x04,
2608c2ecf20Sopenharmony_ci		0x0c, is_60hz ? 0xf0 : 0x20,
2618c2ecf20Sopenharmony_ci		0x0d, is_60hz ? 0x40 : 0x4a,
2628c2ecf20Sopenharmony_ci		0x16, is_60hz ? 0x00 : 0x40,
2638c2ecf20Sopenharmony_ci		0x17, is_60hz ? 0x00 : 0x40,
2648c2ecf20Sopenharmony_ci		0x20, is_60hz ? 0x07 : 0x0f,
2658c2ecf20Sopenharmony_ci		0x21, is_60hz ? 0x07 : 0x0f,
2668c2ecf20Sopenharmony_ci		0xff, 0xff,
2678c2ecf20Sopenharmony_ci	};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	write_regs(client, regs, dec->channel);
2708c2ecf20Sopenharmony_ci	dec->norm = norm;
2718c2ecf20Sopenharmony_ci	return 0;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic int tw2804_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
2758c2ecf20Sopenharmony_ci	u32 config)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct tw2804 *dec = to_state(sd);
2788c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
2798c2ecf20Sopenharmony_ci	int reg;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (config && config - 1 != dec->channel) {
2828c2ecf20Sopenharmony_ci		if (config > 4) {
2838c2ecf20Sopenharmony_ci			dev_err(&client->dev,
2848c2ecf20Sopenharmony_ci				"channel %d is not between 1 and 4!\n", config);
2858c2ecf20Sopenharmony_ci			return -EINVAL;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci		dec->channel = config - 1;
2888c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "initializing TW2804 channel %d\n",
2898c2ecf20Sopenharmony_ci			dec->channel);
2908c2ecf20Sopenharmony_ci		if (dec->channel == 0 &&
2918c2ecf20Sopenharmony_ci				write_regs(client, global_registers, 0) < 0) {
2928c2ecf20Sopenharmony_ci			dev_err(&client->dev,
2938c2ecf20Sopenharmony_ci				"error initializing TW2804 global registers\n");
2948c2ecf20Sopenharmony_ci			return -EIO;
2958c2ecf20Sopenharmony_ci		}
2968c2ecf20Sopenharmony_ci		if (write_regs(client, channel_registers, dec->channel) < 0) {
2978c2ecf20Sopenharmony_ci			dev_err(&client->dev,
2988c2ecf20Sopenharmony_ci				"error initializing TW2804 channel %d\n",
2998c2ecf20Sopenharmony_ci				dec->channel);
3008c2ecf20Sopenharmony_ci			return -EIO;
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	if (input > 1)
3058c2ecf20Sopenharmony_ci		return -EINVAL;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if (input == dec->input)
3088c2ecf20Sopenharmony_ci		return 0;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	reg = read_reg(client, 0x22, dec->channel);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (reg >= 0) {
3138c2ecf20Sopenharmony_ci		if (input == 0)
3148c2ecf20Sopenharmony_ci			reg &= ~(1 << 2);
3158c2ecf20Sopenharmony_ci		else
3168c2ecf20Sopenharmony_ci			reg |= 1 << 2;
3178c2ecf20Sopenharmony_ci		reg = write_reg(client, 0x22, reg, dec->channel);
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (reg >= 0)
3218c2ecf20Sopenharmony_ci		dec->input = input;
3228c2ecf20Sopenharmony_ci	else
3238c2ecf20Sopenharmony_ci		return reg;
3248c2ecf20Sopenharmony_ci	return 0;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops tw2804_ctrl_ops = {
3288c2ecf20Sopenharmony_ci	.g_volatile_ctrl = tw2804_g_volatile_ctrl,
3298c2ecf20Sopenharmony_ci	.s_ctrl = tw2804_s_ctrl,
3308c2ecf20Sopenharmony_ci};
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops tw2804_video_ops = {
3338c2ecf20Sopenharmony_ci	.s_std = tw2804_s_std,
3348c2ecf20Sopenharmony_ci	.s_routing = tw2804_s_video_routing,
3358c2ecf20Sopenharmony_ci};
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops tw2804_core_ops = {
3388c2ecf20Sopenharmony_ci	.log_status = tw2804_log_status,
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops tw2804_ops = {
3428c2ecf20Sopenharmony_ci	.core = &tw2804_core_ops,
3438c2ecf20Sopenharmony_ci	.video = &tw2804_video_ops,
3448c2ecf20Sopenharmony_ci};
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic int tw2804_probe(struct i2c_client *client,
3478c2ecf20Sopenharmony_ci			    const struct i2c_device_id *id)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
3508c2ecf20Sopenharmony_ci	struct tw2804 *state;
3518c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
3528c2ecf20Sopenharmony_ci	struct v4l2_ctrl *ctrl;
3538c2ecf20Sopenharmony_ci	int err;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
3568c2ecf20Sopenharmony_ci		return -ENODEV;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
3598c2ecf20Sopenharmony_ci	if (state == NULL)
3608c2ecf20Sopenharmony_ci		return -ENOMEM;
3618c2ecf20Sopenharmony_ci	sd = &state->sd;
3628c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &tw2804_ops);
3638c2ecf20Sopenharmony_ci	state->channel = -1;
3648c2ecf20Sopenharmony_ci	state->norm = V4L2_STD_NTSC;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&state->hdl, 10);
3678c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3688c2ecf20Sopenharmony_ci				V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
3698c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3708c2ecf20Sopenharmony_ci				V4L2_CID_CONTRAST, 0, 255, 1, 128);
3718c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3728c2ecf20Sopenharmony_ci				V4L2_CID_SATURATION, 0, 255, 1, 128);
3738c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3748c2ecf20Sopenharmony_ci				V4L2_CID_HUE, 0, 255, 1, 128);
3758c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3768c2ecf20Sopenharmony_ci				V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
3778c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3788c2ecf20Sopenharmony_ci				V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
3798c2ecf20Sopenharmony_ci	ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3808c2ecf20Sopenharmony_ci				V4L2_CID_GAIN, 0, 255, 1, 128);
3818c2ecf20Sopenharmony_ci	if (ctrl)
3828c2ecf20Sopenharmony_ci		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
3838c2ecf20Sopenharmony_ci	ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3848c2ecf20Sopenharmony_ci				V4L2_CID_CHROMA_GAIN, 0, 255, 1, 128);
3858c2ecf20Sopenharmony_ci	if (ctrl)
3868c2ecf20Sopenharmony_ci		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
3878c2ecf20Sopenharmony_ci	ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3888c2ecf20Sopenharmony_ci				V4L2_CID_BLUE_BALANCE, 0, 255, 1, 122);
3898c2ecf20Sopenharmony_ci	if (ctrl)
3908c2ecf20Sopenharmony_ci		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
3918c2ecf20Sopenharmony_ci	ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
3928c2ecf20Sopenharmony_ci				V4L2_CID_RED_BALANCE, 0, 255, 1, 122);
3938c2ecf20Sopenharmony_ci	if (ctrl)
3948c2ecf20Sopenharmony_ci		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
3958c2ecf20Sopenharmony_ci	sd->ctrl_handler = &state->hdl;
3968c2ecf20Sopenharmony_ci	err = state->hdl.error;
3978c2ecf20Sopenharmony_ci	if (err) {
3988c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(&state->hdl);
3998c2ecf20Sopenharmony_ci		return err;
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	v4l_info(client, "chip found @ 0x%02x (%s)\n",
4038c2ecf20Sopenharmony_ci			client->addr << 1, client->adapter->name);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	return 0;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic int tw2804_remove(struct i2c_client *client)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
4118c2ecf20Sopenharmony_ci	struct tw2804 *state = to_state(sd);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
4148c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&state->hdl);
4158c2ecf20Sopenharmony_ci	return 0;
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic const struct i2c_device_id tw2804_id[] = {
4198c2ecf20Sopenharmony_ci	{ "tw2804", 0 },
4208c2ecf20Sopenharmony_ci	{ }
4218c2ecf20Sopenharmony_ci};
4228c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tw2804_id);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic struct i2c_driver tw2804_driver = {
4258c2ecf20Sopenharmony_ci	.driver = {
4268c2ecf20Sopenharmony_ci		.name	= "tw2804",
4278c2ecf20Sopenharmony_ci	},
4288c2ecf20Sopenharmony_ci	.probe		= tw2804_probe,
4298c2ecf20Sopenharmony_ci	.remove		= tw2804_remove,
4308c2ecf20Sopenharmony_ci	.id_table	= tw2804_id,
4318c2ecf20Sopenharmony_ci};
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cimodule_i2c_driver(tw2804_driver);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TW2804/TW2802 V4L2 i2c driver");
4378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Micronas USA Inc");
438