Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2005-2006 Micronas USA Inc.
  4 */
  5
  6#include <linux/module.h>
  7#include <linux/init.h>
  8#include <linux/i2c.h>
  9#include <linux/videodev2.h>
 10#include <linux/ioctl.h>
 11#include <media/v4l2-device.h>
 12#include <media/v4l2-ctrls.h>
 13#include <linux/slab.h>
 14
 15MODULE_DESCRIPTION("TW9903 I2C subdev driver");
 16MODULE_LICENSE("GPL v2");
 17
 18/*
 19 * This driver is based on the wis-tw9903.c source that was in
 20 * drivers/staging/media/go7007. That source had commented out code for
 21 * saturation and scaling (neither seemed to work). If anyone ever gets
 22 * hardware to test this driver, then that code might be useful to look at.
 23 * You need to get the kernel sources of, say, kernel 3.8 where that
 24 * wis-tw9903 driver is still present.
 25 */
 26
 27struct tw9903 {
 28	struct v4l2_subdev sd;
 29	struct v4l2_ctrl_handler hdl;
 30	v4l2_std_id norm;
 31};
 32
 33static inline struct tw9903 *to_state(struct v4l2_subdev *sd)
 34{
 35	return container_of(sd, struct tw9903, sd);
 36}
 37
 38static const u8 initial_registers[] = {
 39	0x02, 0x44, /* input 1, composite */
 40	0x03, 0x92, /* correct digital format */
 41	0x04, 0x00,
 42	0x05, 0x80, /* or 0x00 for PAL */
 43	0x06, 0x40, /* second internal current reference */
 44	0x07, 0x02, /* window */
 45	0x08, 0x14, /* window */
 46	0x09, 0xf0, /* window */
 47	0x0a, 0x81, /* window */
 48	0x0b, 0xd0, /* window */
 49	0x0c, 0x8c,
 50	0x0d, 0x00, /* scaling */
 51	0x0e, 0x11, /* scaling */
 52	0x0f, 0x00, /* scaling */
 53	0x10, 0x00, /* brightness */
 54	0x11, 0x60, /* contrast */
 55	0x12, 0x01, /* sharpness */
 56	0x13, 0x7f, /* U gain */
 57	0x14, 0x5a, /* V gain */
 58	0x15, 0x00, /* hue */
 59	0x16, 0xc3, /* sharpness */
 60	0x18, 0x00,
 61	0x19, 0x58, /* vbi */
 62	0x1a, 0x80,
 63	0x1c, 0x0f, /* video norm */
 64	0x1d, 0x7f, /* video norm */
 65	0x20, 0xa0, /* clamping gain (working 0x50) */
 66	0x21, 0x22,
 67	0x22, 0xf0,
 68	0x23, 0xfe,
 69	0x24, 0x3c,
 70	0x25, 0x38,
 71	0x26, 0x44,
 72	0x27, 0x20,
 73	0x28, 0x00,
 74	0x29, 0x15,
 75	0x2a, 0xa0,
 76	0x2b, 0x44,
 77	0x2c, 0x37,
 78	0x2d, 0x00,
 79	0x2e, 0xa5, /* burst PLL control (working: a9) */
 80	0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
 81	0x31, 0x00,
 82	0x33, 0x22,
 83	0x34, 0x11,
 84	0x35, 0x35,
 85	0x3b, 0x05,
 86	0x06, 0xc0, /* reset device */
 87	0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
 88};
 89
 90static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
 91{
 92	struct i2c_client *client = v4l2_get_subdevdata(sd);
 93
 94	return i2c_smbus_write_byte_data(client, reg, value);
 95}
 96
 97static int write_regs(struct v4l2_subdev *sd, const u8 *regs)
 98{
 99	int i;
100
101	for (i = 0; regs[i] != 0x00; i += 2)
102		if (write_reg(sd, regs[i], regs[i + 1]) < 0)
103			return -1;
104	return 0;
105}
106
107static int tw9903_s_video_routing(struct v4l2_subdev *sd, u32 input,
108				      u32 output, u32 config)
109{
110	write_reg(sd, 0x02, 0x40 | (input << 1));
111	return 0;
112}
113
114static int tw9903_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
115{
116	struct tw9903 *dec = to_state(sd);
117	bool is_60hz = norm & V4L2_STD_525_60;
118	static const u8 config_60hz[] = {
119		0x05, 0x80,
120		0x07, 0x02,
121		0x08, 0x14,
122		0x09, 0xf0,
123		0,    0,
124	};
125	static const u8 config_50hz[] = {
126		0x05, 0x00,
127		0x07, 0x12,
128		0x08, 0x18,
129		0x09, 0x20,
130		0,    0,
131	};
132
133	write_regs(sd, is_60hz ? config_60hz : config_50hz);
134	dec->norm = norm;
135	return 0;
136}
137
138
139static int tw9903_s_ctrl(struct v4l2_ctrl *ctrl)
140{
141	struct tw9903 *dec = container_of(ctrl->handler, struct tw9903, hdl);
142	struct v4l2_subdev *sd = &dec->sd;
143
144	switch (ctrl->id) {
145	case V4L2_CID_BRIGHTNESS:
146		write_reg(sd, 0x10, ctrl->val);
147		break;
148	case V4L2_CID_CONTRAST:
149		write_reg(sd, 0x11, ctrl->val);
150		break;
151	case V4L2_CID_HUE:
152		write_reg(sd, 0x15, ctrl->val);
153		break;
154	default:
155		return -EINVAL;
156	}
157	return 0;
158}
159
160static int tw9903_log_status(struct v4l2_subdev *sd)
161{
162	struct tw9903 *dec = to_state(sd);
163	bool is_60hz = dec->norm & V4L2_STD_525_60;
164
165	v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
166	v4l2_ctrl_subdev_log_status(sd);
167	return 0;
168}
169
170/* --------------------------------------------------------------------------*/
171
172static const struct v4l2_ctrl_ops tw9903_ctrl_ops = {
173	.s_ctrl = tw9903_s_ctrl,
174};
175
176static const struct v4l2_subdev_core_ops tw9903_core_ops = {
177	.log_status = tw9903_log_status,
178};
179
180static const struct v4l2_subdev_video_ops tw9903_video_ops = {
181	.s_std = tw9903_s_std,
182	.s_routing = tw9903_s_video_routing,
183};
184
185static const struct v4l2_subdev_ops tw9903_ops = {
186	.core = &tw9903_core_ops,
187	.video = &tw9903_video_ops,
188};
189
190/* --------------------------------------------------------------------------*/
191
192static int tw9903_probe(struct i2c_client *client)
193{
194	struct tw9903 *dec;
195	struct v4l2_subdev *sd;
196	struct v4l2_ctrl_handler *hdl;
197
198	/* Check if the adapter supports the needed features */
199	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
200		return -EIO;
201
202	v4l_info(client, "chip found @ 0x%02x (%s)\n",
203			client->addr << 1, client->adapter->name);
204
205	dec = devm_kzalloc(&client->dev, sizeof(*dec), GFP_KERNEL);
206	if (dec == NULL)
207		return -ENOMEM;
208	sd = &dec->sd;
209	v4l2_i2c_subdev_init(sd, client, &tw9903_ops);
210	hdl = &dec->hdl;
211	v4l2_ctrl_handler_init(hdl, 4);
212	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
213		V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
214	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
215		V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
216	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
217		V4L2_CID_HUE, -128, 127, 1, 0);
218	sd->ctrl_handler = hdl;
219	if (hdl->error) {
220		int err = hdl->error;
221
222		v4l2_ctrl_handler_free(hdl);
223		return err;
224	}
225
226	/* Initialize tw9903 */
227	dec->norm = V4L2_STD_NTSC;
228
229	if (write_regs(sd, initial_registers) < 0) {
230		v4l2_err(client, "error initializing TW9903\n");
231		return -EINVAL;
232	}
233
234	return 0;
235}
236
237static void tw9903_remove(struct i2c_client *client)
238{
239	struct v4l2_subdev *sd = i2c_get_clientdata(client);
240
241	v4l2_device_unregister_subdev(sd);
242	v4l2_ctrl_handler_free(&to_state(sd)->hdl);
243}
244
245/* ----------------------------------------------------------------------- */
246
247static const struct i2c_device_id tw9903_id[] = {
248	{ "tw9903", 0 },
249	{ }
250};
251MODULE_DEVICE_TABLE(i2c, tw9903_id);
252
253static struct i2c_driver tw9903_driver = {
254	.driver = {
255		.name	= "tw9903",
256	},
257	.probe = tw9903_probe,
258	.remove = tw9903_remove,
259	.id_table = tw9903_id,
260};
261module_i2c_driver(tw9903_driver);
  1/*
  2 * Copyright (C) 2005-2006 Micronas USA Inc.
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License (Version 2) as
  6 * published by the Free Software Foundation.
  7 *
  8 * This program is distributed in the hope that it will be useful,
  9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11 * GNU General Public License for more details.
 12 *
 13 * You should have received a copy of the GNU General Public License
 14 * along with this program; if not, write to the Free Software Foundation,
 15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 16 */
 17
 18#include <linux/module.h>
 19#include <linux/init.h>
 20#include <linux/i2c.h>
 21#include <linux/videodev2.h>
 22#include <linux/ioctl.h>
 23#include <media/v4l2-device.h>
 24#include <media/v4l2-ctrls.h>
 25#include <linux/slab.h>
 26
 27MODULE_DESCRIPTION("TW9903 I2C subdev driver");
 28MODULE_LICENSE("GPL v2");
 29
 30/*
 31 * This driver is based on the wis-tw9903.c source that was in
 32 * drivers/staging/media/go7007. That source had commented out code for
 33 * saturation and scaling (neither seemed to work). If anyone ever gets
 34 * hardware to test this driver, then that code might be useful to look at.
 35 * You need to get the kernel sources of, say, kernel 3.8 where that
 36 * wis-tw9903 driver is still present.
 37 */
 38
 39struct tw9903 {
 40	struct v4l2_subdev sd;
 41	struct v4l2_ctrl_handler hdl;
 42	v4l2_std_id norm;
 43};
 44
 45static inline struct tw9903 *to_state(struct v4l2_subdev *sd)
 46{
 47	return container_of(sd, struct tw9903, sd);
 48}
 49
 50static const u8 initial_registers[] = {
 51	0x02, 0x44, /* input 1, composite */
 52	0x03, 0x92, /* correct digital format */
 53	0x04, 0x00,
 54	0x05, 0x80, /* or 0x00 for PAL */
 55	0x06, 0x40, /* second internal current reference */
 56	0x07, 0x02, /* window */
 57	0x08, 0x14, /* window */
 58	0x09, 0xf0, /* window */
 59	0x0a, 0x81, /* window */
 60	0x0b, 0xd0, /* window */
 61	0x0c, 0x8c,
 62	0x0d, 0x00, /* scaling */
 63	0x0e, 0x11, /* scaling */
 64	0x0f, 0x00, /* scaling */
 65	0x10, 0x00, /* brightness */
 66	0x11, 0x60, /* contrast */
 67	0x12, 0x01, /* sharpness */
 68	0x13, 0x7f, /* U gain */
 69	0x14, 0x5a, /* V gain */
 70	0x15, 0x00, /* hue */
 71	0x16, 0xc3, /* sharpness */
 72	0x18, 0x00,
 73	0x19, 0x58, /* vbi */
 74	0x1a, 0x80,
 75	0x1c, 0x0f, /* video norm */
 76	0x1d, 0x7f, /* video norm */
 77	0x20, 0xa0, /* clamping gain (working 0x50) */
 78	0x21, 0x22,
 79	0x22, 0xf0,
 80	0x23, 0xfe,
 81	0x24, 0x3c,
 82	0x25, 0x38,
 83	0x26, 0x44,
 84	0x27, 0x20,
 85	0x28, 0x00,
 86	0x29, 0x15,
 87	0x2a, 0xa0,
 88	0x2b, 0x44,
 89	0x2c, 0x37,
 90	0x2d, 0x00,
 91	0x2e, 0xa5, /* burst PLL control (working: a9) */
 92	0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
 93	0x31, 0x00,
 94	0x33, 0x22,
 95	0x34, 0x11,
 96	0x35, 0x35,
 97	0x3b, 0x05,
 98	0x06, 0xc0, /* reset device */
 99	0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
100};
101
102static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
103{
104	struct i2c_client *client = v4l2_get_subdevdata(sd);
105
106	return i2c_smbus_write_byte_data(client, reg, value);
107}
108
109static int write_regs(struct v4l2_subdev *sd, const u8 *regs)
110{
111	int i;
112
113	for (i = 0; regs[i] != 0x00; i += 2)
114		if (write_reg(sd, regs[i], regs[i + 1]) < 0)
115			return -1;
116	return 0;
117}
118
119static int tw9903_s_video_routing(struct v4l2_subdev *sd, u32 input,
120				      u32 output, u32 config)
121{
122	write_reg(sd, 0x02, 0x40 | (input << 1));
123	return 0;
124}
125
126static int tw9903_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
127{
128	struct tw9903 *dec = to_state(sd);
129	bool is_60hz = norm & V4L2_STD_525_60;
130	static const u8 config_60hz[] = {
131		0x05, 0x80,
132		0x07, 0x02,
133		0x08, 0x14,
134		0x09, 0xf0,
135		0,    0,
136	};
137	static const u8 config_50hz[] = {
138		0x05, 0x00,
139		0x07, 0x12,
140		0x08, 0x18,
141		0x09, 0x20,
142		0,    0,
143	};
144
145	write_regs(sd, is_60hz ? config_60hz : config_50hz);
146	dec->norm = norm;
147	return 0;
148}
149
150
151static int tw9903_s_ctrl(struct v4l2_ctrl *ctrl)
152{
153	struct tw9903 *dec = container_of(ctrl->handler, struct tw9903, hdl);
154	struct v4l2_subdev *sd = &dec->sd;
155
156	switch (ctrl->id) {
157	case V4L2_CID_BRIGHTNESS:
158		write_reg(sd, 0x10, ctrl->val);
159		break;
160	case V4L2_CID_CONTRAST:
161		write_reg(sd, 0x11, ctrl->val);
162		break;
163	case V4L2_CID_HUE:
164		write_reg(sd, 0x15, ctrl->val);
165		break;
166	default:
167		return -EINVAL;
168	}
169	return 0;
170}
171
172static int tw9903_log_status(struct v4l2_subdev *sd)
173{
174	struct tw9903 *dec = to_state(sd);
175	bool is_60hz = dec->norm & V4L2_STD_525_60;
176
177	v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
178	v4l2_ctrl_subdev_log_status(sd);
179	return 0;
180}
181
182/* --------------------------------------------------------------------------*/
183
184static const struct v4l2_ctrl_ops tw9903_ctrl_ops = {
185	.s_ctrl = tw9903_s_ctrl,
186};
187
188static const struct v4l2_subdev_core_ops tw9903_core_ops = {
189	.log_status = tw9903_log_status,
190};
191
192static const struct v4l2_subdev_video_ops tw9903_video_ops = {
193	.s_std = tw9903_s_std,
194	.s_routing = tw9903_s_video_routing,
195};
196
197static const struct v4l2_subdev_ops tw9903_ops = {
198	.core = &tw9903_core_ops,
199	.video = &tw9903_video_ops,
200};
201
202/* --------------------------------------------------------------------------*/
203
204static int tw9903_probe(struct i2c_client *client,
205			     const struct i2c_device_id *id)
206{
207	struct tw9903 *dec;
208	struct v4l2_subdev *sd;
209	struct v4l2_ctrl_handler *hdl;
210
211	/* Check if the adapter supports the needed features */
212	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
213		return -EIO;
214
215	v4l_info(client, "chip found @ 0x%02x (%s)\n",
216			client->addr << 1, client->adapter->name);
217
218	dec = devm_kzalloc(&client->dev, sizeof(*dec), GFP_KERNEL);
219	if (dec == NULL)
220		return -ENOMEM;
221	sd = &dec->sd;
222	v4l2_i2c_subdev_init(sd, client, &tw9903_ops);
223	hdl = &dec->hdl;
224	v4l2_ctrl_handler_init(hdl, 4);
225	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
226		V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
227	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
228		V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
229	v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
230		V4L2_CID_HUE, -128, 127, 1, 0);
231	sd->ctrl_handler = hdl;
232	if (hdl->error) {
233		int err = hdl->error;
234
235		v4l2_ctrl_handler_free(hdl);
236		return err;
237	}
238
239	/* Initialize tw9903 */
240	dec->norm = V4L2_STD_NTSC;
241
242	if (write_regs(sd, initial_registers) < 0) {
243		v4l2_err(client, "error initializing TW9903\n");
244		return -EINVAL;
245	}
246
247	return 0;
248}
249
250static int tw9903_remove(struct i2c_client *client)
251{
252	struct v4l2_subdev *sd = i2c_get_clientdata(client);
253
254	v4l2_device_unregister_subdev(sd);
255	v4l2_ctrl_handler_free(&to_state(sd)->hdl);
256	return 0;
257}
258
259/* ----------------------------------------------------------------------- */
260
261static const struct i2c_device_id tw9903_id[] = {
262	{ "tw9903", 0 },
263	{ }
264};
265MODULE_DEVICE_TABLE(i2c, tw9903_id);
266
267static struct i2c_driver tw9903_driver = {
268	.driver = {
269		.name	= "tw9903",
270	},
271	.probe = tw9903_probe,
272	.remove = tw9903_remove,
273	.id_table = tw9903_id,
274};
275module_i2c_driver(tw9903_driver);