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 <linux/slab.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ctrls.h>
26
27MODULE_DESCRIPTION("TW9906 I2C subdev driver");
28MODULE_LICENSE("GPL v2");
29
30struct tw9906 {
31 struct v4l2_subdev sd;
32 struct v4l2_ctrl_handler hdl;
33 v4l2_std_id norm;
34};
35
36static inline struct tw9906 *to_state(struct v4l2_subdev *sd)
37{
38 return container_of(sd, struct tw9906, sd);
39}
40
41static const u8 initial_registers[] = {
42 0x02, 0x40,
43 0x03, 0xa2,
44 0x05, 0x81,
45 0x07, 0x02,
46 0x08, 0x14,
47 0x09, 0xf0,
48 0x0a, 0x10,
49 0x0b, 0xd0,
50 0x0d, 0x00,
51 0x0e, 0x11,
52 0x0f, 0x00,
53 0x10, 0x00,
54 0x11, 0x60,
55 0x12, 0x11,
56 0x13, 0x7e,
57 0x14, 0x7e,
58 0x15, 0x00,
59 0x19, 0x57,
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,
71};
72
73static 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
80static 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
90static 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
97static 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 static const u8 config_60hz[] = {
102 0x05, 0x81,
103 0x07, 0x02,
104 0x08, 0x14,
105 0x09, 0xf0,
106 0, 0,
107 };
108 static const u8 config_50hz[] = {
109 0x05, 0x01,
110 0x07, 0x12,
111 0x08, 0x18,
112 0x09, 0x20,
113 0, 0,
114 };
115
116 write_regs(sd, is_60hz ? config_60hz : config_50hz);
117 dec->norm = norm;
118 return 0;
119}
120
121static int tw9906_s_ctrl(struct v4l2_ctrl *ctrl)
122{
123 struct tw9906 *dec = container_of(ctrl->handler, struct tw9906, hdl);
124 struct v4l2_subdev *sd = &dec->sd;
125
126 switch (ctrl->id) {
127 case V4L2_CID_BRIGHTNESS:
128 write_reg(sd, 0x10, ctrl->val);
129 break;
130 case V4L2_CID_CONTRAST:
131 write_reg(sd, 0x11, ctrl->val);
132 break;
133 case V4L2_CID_HUE:
134 write_reg(sd, 0x15, ctrl->val);
135 break;
136 default:
137 return -EINVAL;
138 }
139 return 0;
140}
141
142static int tw9906_log_status(struct v4l2_subdev *sd)
143{
144 struct tw9906 *dec = to_state(sd);
145 bool is_60hz = dec->norm & V4L2_STD_525_60;
146
147 v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
148 v4l2_ctrl_subdev_log_status(sd);
149 return 0;
150}
151
152
153
154static const struct v4l2_ctrl_ops tw9906_ctrl_ops = {
155 .s_ctrl = tw9906_s_ctrl,
156};
157
158static const struct v4l2_subdev_core_ops tw9906_core_ops = {
159 .log_status = tw9906_log_status,
160};
161
162static const struct v4l2_subdev_video_ops tw9906_video_ops = {
163 .s_std = tw9906_s_std,
164 .s_routing = tw9906_s_video_routing,
165};
166
167static const struct v4l2_subdev_ops tw9906_ops = {
168 .core = &tw9906_core_ops,
169 .video = &tw9906_video_ops,
170};
171
172static int tw9906_probe(struct i2c_client *client,
173 const struct i2c_device_id *id)
174{
175 struct tw9906 *dec;
176 struct v4l2_subdev *sd;
177 struct v4l2_ctrl_handler *hdl;
178
179
180 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
181 return -EIO;
182
183 v4l_info(client, "chip found @ 0x%02x (%s)\n",
184 client->addr << 1, client->adapter->name);
185
186 dec = devm_kzalloc(&client->dev, sizeof(*dec), GFP_KERNEL);
187 if (dec == NULL)
188 return -ENOMEM;
189 sd = &dec->sd;
190 v4l2_i2c_subdev_init(sd, client, &tw9906_ops);
191 hdl = &dec->hdl;
192 v4l2_ctrl_handler_init(hdl, 4);
193 v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops,
194 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
195 v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops,
196 V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
197 v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops,
198 V4L2_CID_HUE, -128, 127, 1, 0);
199 sd->ctrl_handler = hdl;
200 if (hdl->error) {
201 int err = hdl->error;
202
203 v4l2_ctrl_handler_free(hdl);
204 return err;
205 }
206
207
208 dec->norm = V4L2_STD_NTSC;
209
210 if (write_regs(sd, initial_registers) < 0) {
211 v4l2_err(client, "error initializing TW9906\n");
212 return -EINVAL;
213 }
214
215 return 0;
216}
217
218static int tw9906_remove(struct i2c_client *client)
219{
220 struct v4l2_subdev *sd = i2c_get_clientdata(client);
221
222 v4l2_device_unregister_subdev(sd);
223 v4l2_ctrl_handler_free(&to_state(sd)->hdl);
224 return 0;
225}
226
227
228
229static const struct i2c_device_id tw9906_id[] = {
230 { "tw9906", 0 },
231 { }
232};
233MODULE_DEVICE_TABLE(i2c, tw9906_id);
234
235static struct i2c_driver tw9906_driver = {
236 .driver = {
237 .name = "tw9906",
238 },
239 .probe = tw9906_probe,
240 .remove = tw9906_remove,
241 .id_table = tw9906_id,
242};
243module_i2c_driver(tw9906_driver);
244