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