Commit | Line | Data |
---|---|---|
a000e9a0 HV |
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> | |
a000e9a0 HV |
20 | #include <linux/i2c.h> |
21 | #include <linux/videodev2.h> | |
22 | #include <linux/ioctl.h> | |
23 | #include <linux/slab.h> | |
24 | #include <media/v4l2-device.h> | |
25 | #include <media/v4l2-ctrls.h> | |
26 | ||
27 | MODULE_DESCRIPTION("TW9906 I2C subdev driver"); | |
28 | MODULE_LICENSE("GPL v2"); | |
29 | ||
30 | struct tw9906 { | |
31 | struct v4l2_subdev sd; | |
32 | struct v4l2_ctrl_handler hdl; | |
33 | v4l2_std_id norm; | |
34 | }; | |
35 | ||
36 | static inline struct tw9906 *to_state(struct v4l2_subdev *sd) | |
37 | { | |
38 | return container_of(sd, struct tw9906, sd); | |
39 | } | |
40 | ||
41 | static const u8 initial_registers[] = { | |
42 | 0x02, 0x40, /* input 0, composite */ | |
43 | 0x03, 0xa2, /* correct digital format */ | |
44 | 0x05, 0x81, /* or 0x01 for PAL */ | |
45 | 0x07, 0x02, /* window */ | |
46 | 0x08, 0x14, /* window */ | |
47 | 0x09, 0xf0, /* window */ | |
48 | 0x0a, 0x10, /* window */ | |
49 | 0x0b, 0xd0, /* window */ | |
50 | 0x0d, 0x00, /* scaling */ | |
51 | 0x0e, 0x11, /* scaling */ | |
52 | 0x0f, 0x00, /* scaling */ | |
53 | 0x10, 0x00, /* brightness */ | |
54 | 0x11, 0x60, /* contrast */ | |
55 | 0x12, 0x11, /* sharpness */ | |
56 | 0x13, 0x7e, /* U gain */ | |
57 | 0x14, 0x7e, /* V gain */ | |
58 | 0x15, 0x00, /* hue */ | |
59 | 0x19, 0x57, /* vbi */ | |
60 | 0x1a, 0x0f, | |
61 | 0x1b, 0x40, | |
62 | 0x29, 0x03, | |
63 | 0x55, 0x00, | |
64 | 0x6b, 0x26, | |
65 | 0x6c, 0x36, | |
66 | 0x6d, 0xf0, | |
67 | 0x6e, 0x41, | |
68 | 0x6f, 0x13, | |
69 | 0xad, 0x70, | |
70 | 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */ | |
71 | }; | |
72 | ||
73 | static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value) | |
74 | { | |
75 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
76 | ||
77 | return i2c_smbus_write_byte_data(client, reg, value); | |
78 | } | |
79 | ||
80 | static int write_regs(struct v4l2_subdev *sd, const u8 *regs) | |
81 | { | |
82 | int i; | |
83 | ||
84 | for (i = 0; regs[i] != 0x00; i += 2) | |
85 | if (write_reg(sd, regs[i], regs[i + 1]) < 0) | |
86 | return -1; | |
87 | return 0; | |
88 | } | |
89 | ||
90 | static int tw9906_s_video_routing(struct v4l2_subdev *sd, u32 input, | |
91 | u32 output, u32 config) | |
92 | { | |
93 | write_reg(sd, 0x02, 0x40 | (input << 1)); | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int tw9906_s_std(struct v4l2_subdev *sd, v4l2_std_id norm) | |
98 | { | |
99 | struct tw9906 *dec = to_state(sd); | |
100 | bool is_60hz = norm & V4L2_STD_525_60; | |
101 | u8 regs[] = { | |
102 | 0x05, is_60hz ? 0x81 : 0x01, | |
103 | 0x07, is_60hz ? 0x02 : 0x12, | |
104 | 0x08, is_60hz ? 0x14 : 0x18, | |
105 | 0x09, is_60hz ? 0xf0 : 0x20, | |
106 | 0, 0, | |
107 | }; | |
108 | ||
109 | write_regs(sd, regs); | |
110 | dec->norm = norm; | |
111 | return 0; | |
112 | } | |
113 | ||
114 | static int tw9906_s_ctrl(struct v4l2_ctrl *ctrl) | |
115 | { | |
116 | struct tw9906 *dec = container_of(ctrl->handler, struct tw9906, hdl); | |
117 | struct v4l2_subdev *sd = &dec->sd; | |
118 | ||
119 | switch (ctrl->id) { | |
120 | case V4L2_CID_BRIGHTNESS: | |
121 | write_reg(sd, 0x10, ctrl->val); | |
122 | break; | |
123 | case V4L2_CID_CONTRAST: | |
124 | write_reg(sd, 0x11, ctrl->val); | |
125 | break; | |
126 | case V4L2_CID_HUE: | |
127 | write_reg(sd, 0x15, ctrl->val); | |
128 | break; | |
129 | default: | |
130 | return -EINVAL; | |
131 | } | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static int tw9906_log_status(struct v4l2_subdev *sd) | |
136 | { | |
137 | struct tw9906 *dec = to_state(sd); | |
138 | bool is_60hz = dec->norm & V4L2_STD_525_60; | |
139 | ||
140 | v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50); | |
141 | v4l2_ctrl_subdev_log_status(sd); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | /* --------------------------------------------------------------------------*/ | |
146 | ||
147 | static const struct v4l2_ctrl_ops tw9906_ctrl_ops = { | |
148 | .s_ctrl = tw9906_s_ctrl, | |
149 | }; | |
150 | ||
151 | static const struct v4l2_subdev_core_ops tw9906_core_ops = { | |
152 | .log_status = tw9906_log_status, | |
153 | .s_std = tw9906_s_std, | |
154 | }; | |
155 | ||
156 | static const struct v4l2_subdev_video_ops tw9906_video_ops = { | |
157 | .s_routing = tw9906_s_video_routing, | |
158 | }; | |
159 | ||
160 | static const struct v4l2_subdev_ops tw9906_ops = { | |
161 | .core = &tw9906_core_ops, | |
162 | .video = &tw9906_video_ops, | |
163 | }; | |
164 | ||
165 | static int tw9906_probe(struct i2c_client *client, | |
166 | const struct i2c_device_id *id) | |
167 | { | |
168 | struct tw9906 *dec; | |
169 | struct v4l2_subdev *sd; | |
170 | struct v4l2_ctrl_handler *hdl; | |
171 | ||
172 | /* Check if the adapter supports the needed features */ | |
173 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
174 | return -EIO; | |
175 | ||
176 | v4l_info(client, "chip found @ 0x%02x (%s)\n", | |
177 | client->addr << 1, client->adapter->name); | |
178 | ||
179 | dec = kzalloc(sizeof(struct tw9906), GFP_KERNEL); | |
180 | if (dec == NULL) | |
181 | return -ENOMEM; | |
182 | sd = &dec->sd; | |
183 | v4l2_i2c_subdev_init(sd, client, &tw9906_ops); | |
184 | hdl = &dec->hdl; | |
185 | v4l2_ctrl_handler_init(hdl, 4); | |
186 | v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops, | |
187 | V4L2_CID_BRIGHTNESS, -128, 127, 1, 0); | |
188 | v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops, | |
189 | V4L2_CID_CONTRAST, 0, 255, 1, 0x60); | |
190 | v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops, | |
191 | V4L2_CID_HUE, -128, 127, 1, 0); | |
192 | sd->ctrl_handler = hdl; | |
193 | if (hdl->error) { | |
194 | int err = hdl->error; | |
195 | ||
196 | v4l2_ctrl_handler_free(hdl); | |
197 | kfree(dec); | |
198 | return err; | |
199 | } | |
200 | ||
201 | /* Initialize tw9906 */ | |
202 | dec->norm = V4L2_STD_NTSC; | |
203 | ||
204 | if (write_regs(sd, initial_registers) < 0) { | |
205 | v4l2_err(client, "error initializing TW9906\n"); | |
206 | kfree(dec); | |
207 | return -EINVAL; | |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | static int tw9906_remove(struct i2c_client *client) | |
214 | { | |
215 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
216 | ||
217 | v4l2_device_unregister_subdev(sd); | |
218 | v4l2_ctrl_handler_free(&to_state(sd)->hdl); | |
219 | kfree(to_state(sd)); | |
220 | return 0; | |
221 | } | |
222 | ||
223 | /* ----------------------------------------------------------------------- */ | |
224 | ||
225 | static const struct i2c_device_id tw9906_id[] = { | |
226 | { "tw9906", 0 }, | |
227 | { } | |
228 | }; | |
229 | MODULE_DEVICE_TABLE(i2c, tw9906_id); | |
230 | ||
231 | static struct i2c_driver tw9906_driver = { | |
232 | .driver = { | |
233 | .owner = THIS_MODULE, | |
234 | .name = "tw9906", | |
235 | }, | |
236 | .probe = tw9906_probe, | |
237 | .remove = tw9906_remove, | |
238 | .id_table = tw9906_id, | |
239 | }; | |
240 | module_i2c_driver(tw9906_driver); |