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