blob: f0d0ef5da30c7c3c94a43004ddb53f41c9bd5397 [file] [log] [blame]
Hans Verkuil0890ec12013-03-18 04:10:56 -03001/*
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 u8 regs[] = {
131 0x05, is_60hz ? 0x80 : 0x00,
132 0x07, is_60hz ? 0x02 : 0x12,
133 0x08, is_60hz ? 0x14 : 0x18,
134 0x09, is_60hz ? 0xf0 : 0x20,
135 0, 0,
136 };
137
138 write_regs(sd, regs);
139 dec->norm = norm;
140 return 0;
141}
142
143
144static int tw9903_s_ctrl(struct v4l2_ctrl *ctrl)
145{
146 struct tw9903 *dec = container_of(ctrl->handler, struct tw9903, hdl);
147 struct v4l2_subdev *sd = &dec->sd;
148
149 switch (ctrl->id) {
150 case V4L2_CID_BRIGHTNESS:
151 write_reg(sd, 0x10, ctrl->val);
152 break;
153 case V4L2_CID_CONTRAST:
154 write_reg(sd, 0x11, ctrl->val);
155 break;
156 case V4L2_CID_HUE:
157 write_reg(sd, 0x15, ctrl->val);
158 break;
159 default:
160 return -EINVAL;
161 }
162 return 0;
163}
164
165static int tw9903_log_status(struct v4l2_subdev *sd)
166{
167 struct tw9903 *dec = to_state(sd);
168 bool is_60hz = dec->norm & V4L2_STD_525_60;
169
170 v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
171 v4l2_ctrl_subdev_log_status(sd);
172 return 0;
173}
174
175/* --------------------------------------------------------------------------*/
176
177static const struct v4l2_ctrl_ops tw9903_ctrl_ops = {
178 .s_ctrl = tw9903_s_ctrl,
179};
180
181static const struct v4l2_subdev_core_ops tw9903_core_ops = {
182 .log_status = tw9903_log_status,
183 .s_std = tw9903_s_std,
184};
185
186static const struct v4l2_subdev_video_ops tw9903_video_ops = {
187 .s_routing = tw9903_s_video_routing,
188};
189
190static const struct v4l2_subdev_ops tw9903_ops = {
191 .core = &tw9903_core_ops,
192 .video = &tw9903_video_ops,
193};
194
195/* --------------------------------------------------------------------------*/
196
197static int tw9903_probe(struct i2c_client *client,
198 const struct i2c_device_id *id)
199{
200 struct tw9903 *dec;
201 struct v4l2_subdev *sd;
202 struct v4l2_ctrl_handler *hdl;
203
204 /* Check if the adapter supports the needed features */
205 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
206 return -EIO;
207
208 v4l_info(client, "chip found @ 0x%02x (%s)\n",
209 client->addr << 1, client->adapter->name);
210
211 dec = kzalloc(sizeof(struct tw9903), GFP_KERNEL);
212 if (dec == NULL)
213 return -ENOMEM;
214 sd = &dec->sd;
215 v4l2_i2c_subdev_init(sd, client, &tw9903_ops);
216 hdl = &dec->hdl;
217 v4l2_ctrl_handler_init(hdl, 4);
218 v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
219 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
220 v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
221 V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
222 v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
223 V4L2_CID_HUE, -128, 127, 1, 0);
224 sd->ctrl_handler = hdl;
225 if (hdl->error) {
226 int err = hdl->error;
227
228 v4l2_ctrl_handler_free(hdl);
229 kfree(dec);
230 return err;
231 }
232
233 /* Initialize tw9903 */
234 dec->norm = V4L2_STD_NTSC;
235
236 if (write_regs(sd, initial_registers) < 0) {
237 v4l2_err(client, "error initializing TW9903\n");
238 kfree(dec);
239 return -EINVAL;
240 }
241
242 return 0;
243}
244
245static int tw9903_remove(struct i2c_client *client)
246{
247 struct v4l2_subdev *sd = i2c_get_clientdata(client);
248
249 v4l2_device_unregister_subdev(sd);
250 v4l2_ctrl_handler_free(&to_state(sd)->hdl);
251 kfree(to_state(sd));
252 return 0;
253}
254
255/* ----------------------------------------------------------------------- */
256
257static const struct i2c_device_id tw9903_id[] = {
258 { "tw9903", 0 },
259 { }
260};
261MODULE_DEVICE_TABLE(i2c, tw9903_id);
262
263static struct i2c_driver tw9903_driver = {
264 .driver = {
265 .owner = THIS_MODULE,
266 .name = "tw9903",
267 },
268 .probe = tw9903_probe,
269 .remove = tw9903_remove,
270 .id_table = tw9903_id,
271};
272module_i2c_driver(tw9903_driver);