1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
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,
52 0x03, 0x92,
53 0x04, 0x00,
54 0x05, 0x80,
55 0x06, 0x40,
56 0x07, 0x02,
57 0x08, 0x14,
58 0x09, 0xf0,
59 0x0a, 0x81,
60 0x0b, 0xd0,
61 0x0c, 0x8c,
62 0x0d, 0x00,
63 0x0e, 0x11,
64 0x0f, 0x00,
65 0x10, 0x00,
66 0x11, 0x60,
67 0x12, 0x01,
68 0x13, 0x7f,
69 0x14, 0x5a,
70 0x15, 0x00,
71 0x16, 0xc3,
72 0x18, 0x00,
73 0x19, 0x58,
74 0x1a, 0x80,
75 0x1c, 0x0f,
76 0x1d, 0x7f,
77 0x20, 0xa0,
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,
92 0x2f, 0xe0,
93 0x31, 0x00,
94 0x33, 0x22,
95 0x34, 0x11,
96 0x35, 0x35,
97 0x3b, 0x05,
98 0x06, 0xc0,
99 0x00, 0x00,
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 .s_std = tw9903_s_std,
191};
192
193static const struct v4l2_subdev_video_ops tw9903_video_ops = {
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
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 = kzalloc(sizeof(struct tw9903), 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 kfree(dec);
237 return err;
238 }
239
240
241 dec->norm = V4L2_STD_NTSC;
242
243 if (write_regs(sd, initial_registers) < 0) {
244 v4l2_err(client, "error initializing TW9903\n");
245 kfree(dec);
246 return -EINVAL;
247 }
248
249 return 0;
250}
251
252static int tw9903_remove(struct i2c_client *client)
253{
254 struct v4l2_subdev *sd = i2c_get_clientdata(client);
255
256 v4l2_device_unregister_subdev(sd);
257 v4l2_ctrl_handler_free(&to_state(sd)->hdl);
258 kfree(to_state(sd));
259 return 0;
260}
261
262
263
264static const struct i2c_device_id tw9903_id[] = {
265 { "tw9903", 0 },
266 { }
267};
268MODULE_DEVICE_TABLE(i2c, tw9903_id);
269
270static struct i2c_driver tw9903_driver = {
271 .driver = {
272 .owner = THIS_MODULE,
273 .name = "tw9903",
274 },
275 .probe = tw9903_probe,
276 .remove = tw9903_remove,
277 .id_table = tw9903_id,
278};
279module_i2c_driver(tw9903_driver);
280