1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/slab.h>
27#include <linux/ioctl.h>
28#include <asm/uaccess.h>
29#include <linux/i2c.h>
30#include <linux/videodev2.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-chip-ident.h>
33#include <media/v4l2-ctrls.h>
34
35MODULE_DESCRIPTION("wm8739 driver");
36MODULE_AUTHOR("T. Adachi, Hans Verkuil");
37MODULE_LICENSE("GPL");
38
39static int debug;
40
41module_param(debug, int, 0644);
42
43MODULE_PARM_DESC(debug, "Debug level (0-1)");
44
45
46
47
48enum {
49 R0 = 0, R1,
50 R5 = 5, R6, R7, R8, R9, R15 = 15,
51 TOT_REGS
52};
53
54struct wm8739_state {
55 struct v4l2_subdev sd;
56 struct v4l2_ctrl_handler hdl;
57 struct {
58
59 struct v4l2_ctrl *volume;
60 struct v4l2_ctrl *mute;
61 struct v4l2_ctrl *balance;
62 };
63 u32 clock_freq;
64};
65
66static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
67{
68 return container_of(sd, struct wm8739_state, sd);
69}
70
71static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
72{
73 return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
74}
75
76
77
78static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
79{
80 struct i2c_client *client = v4l2_get_subdevdata(sd);
81 int i;
82
83 if (reg < 0 || reg >= TOT_REGS) {
84 v4l2_err(sd, "Invalid register R%d\n", reg);
85 return -1;
86 }
87
88 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
89
90 for (i = 0; i < 3; i++)
91 if (i2c_smbus_write_byte_data(client,
92 (reg << 1) | (val >> 8), val & 0xff) == 0)
93 return 0;
94 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
95 return -1;
96}
97
98static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
99{
100 struct v4l2_subdev *sd = to_sd(ctrl);
101 struct wm8739_state *state = to_state(sd);
102 unsigned int work_l, work_r;
103 u8 vol_l;
104 u8 vol_r;
105 u16 mute;
106
107 switch (ctrl->id) {
108 case V4L2_CID_AUDIO_VOLUME:
109 break;
110
111 default:
112 return -EINVAL;
113 }
114
115
116 work_l = (min(65536 - state->balance->val, 32768) * state->volume->val) / 32768;
117 work_r = (min(state->balance->val, 32768) * state->volume->val) / 32768;
118
119 vol_l = (long)work_l * 31 / 65535;
120 vol_r = (long)work_r * 31 / 65535;
121
122
123 mute = state->mute->val ? 0x80 : 0;
124
125
126
127
128 wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
129 wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
130 return 0;
131}
132
133
134
135static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
136{
137 struct wm8739_state *state = to_state(sd);
138
139 state->clock_freq = audiofreq;
140
141 wm8739_write(sd, R9, 0x000);
142 switch (audiofreq) {
143 case 44100:
144
145 wm8739_write(sd, R8, 0x020);
146 break;
147 case 48000:
148
149 wm8739_write(sd, R8, 0x000);
150 break;
151 case 32000:
152
153 wm8739_write(sd, R8, 0x018);
154 break;
155 default:
156 break;
157 }
158
159 wm8739_write(sd, R9, 0x001);
160 return 0;
161}
162
163static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
164{
165 struct i2c_client *client = v4l2_get_subdevdata(sd);
166
167 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_WM8739, 0);
168}
169
170static int wm8739_log_status(struct v4l2_subdev *sd)
171{
172 struct wm8739_state *state = to_state(sd);
173
174 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
175 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
176 return 0;
177}
178
179
180
181static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
182 .s_ctrl = wm8739_s_ctrl,
183};
184
185static const struct v4l2_subdev_core_ops wm8739_core_ops = {
186 .log_status = wm8739_log_status,
187 .g_chip_ident = wm8739_g_chip_ident,
188 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
189 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
190 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
191 .g_ctrl = v4l2_subdev_g_ctrl,
192 .s_ctrl = v4l2_subdev_s_ctrl,
193 .queryctrl = v4l2_subdev_queryctrl,
194 .querymenu = v4l2_subdev_querymenu,
195};
196
197static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
198 .s_clock_freq = wm8739_s_clock_freq,
199};
200
201static const struct v4l2_subdev_ops wm8739_ops = {
202 .core = &wm8739_core_ops,
203 .audio = &wm8739_audio_ops,
204};
205
206
207
208
209
210static int wm8739_probe(struct i2c_client *client,
211 const struct i2c_device_id *id)
212{
213 struct wm8739_state *state;
214 struct v4l2_subdev *sd;
215
216
217 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
218 return -EIO;
219
220 v4l_info(client, "chip found @ 0x%x (%s)\n",
221 client->addr << 1, client->adapter->name);
222
223 state = kzalloc(sizeof(struct wm8739_state), GFP_KERNEL);
224 if (state == NULL)
225 return -ENOMEM;
226 sd = &state->sd;
227 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
228 v4l2_ctrl_handler_init(&state->hdl, 2);
229 state->volume = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
230 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 50736);
231 state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
232 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
233 state->balance = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
234 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
235 sd->ctrl_handler = &state->hdl;
236 if (state->hdl.error) {
237 int err = state->hdl.error;
238
239 v4l2_ctrl_handler_free(&state->hdl);
240 kfree(state);
241 return err;
242 }
243 v4l2_ctrl_cluster(3, &state->volume);
244
245 state->clock_freq = 48000;
246
247
248
249
250 wm8739_write(sd, R15, 0x00);
251
252 wm8739_write(sd, R5, 0x000);
253
254 wm8739_write(sd, R6, 0x000);
255
256
257 wm8739_write(sd, R7, 0x049);
258
259 wm8739_write(sd, R8, 0x000);
260
261 wm8739_write(sd, R9, 0x001);
262
263 v4l2_ctrl_handler_setup(&state->hdl);
264 return 0;
265}
266
267static int wm8739_remove(struct i2c_client *client)
268{
269 struct v4l2_subdev *sd = i2c_get_clientdata(client);
270 struct wm8739_state *state = to_state(sd);
271
272 v4l2_device_unregister_subdev(sd);
273 v4l2_ctrl_handler_free(&state->hdl);
274 kfree(to_state(sd));
275 return 0;
276}
277
278static const struct i2c_device_id wm8739_id[] = {
279 { "wm8739", 0 },
280 { }
281};
282MODULE_DEVICE_TABLE(i2c, wm8739_id);
283
284static struct i2c_driver wm8739_driver = {
285 .driver = {
286 .owner = THIS_MODULE,
287 .name = "wm8739",
288 },
289 .probe = wm8739_probe,
290 .remove = wm8739_remove,
291 .id_table = wm8739_id,
292};
293
294static __init int init_wm8739(void)
295{
296 return i2c_add_driver(&wm8739_driver);
297}
298
299static __exit void exit_wm8739(void)
300{
301 i2c_del_driver(&wm8739_driver);
302}
303
304module_init(init_wm8739);
305module_exit(exit_wm8739);
306