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/ioctl.h>
27#include <asm/uaccess.h>
28#include <linux/i2c.h>
29#include <linux/i2c-id.h>
30#include <linux/videodev2.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-chip-ident.h>
33#include <media/v4l2-i2c-drv.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 u32 clock_freq;
57 u8 muted;
58 u16 volume;
59 u16 balance;
60 u8 vol_l;
61 u8 vol_r;
62};
63
64static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
65{
66 return container_of(sd, struct wm8739_state, sd);
67}
68
69
70
71static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
72{
73 struct i2c_client *client = v4l2_get_subdevdata(sd);
74 int i;
75
76 if (reg < 0 || reg >= TOT_REGS) {
77 v4l2_err(sd, "Invalid register R%d\n", reg);
78 return -1;
79 }
80
81 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
82
83 for (i = 0; i < 3; i++)
84 if (i2c_smbus_write_byte_data(client,
85 (reg << 1) | (val >> 8), val & 0xff) == 0)
86 return 0;
87 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
88 return -1;
89}
90
91
92static void wm8739_set_audio(struct v4l2_subdev *sd)
93{
94 struct wm8739_state *state = to_state(sd);
95 u16 mute = state->muted ? 0x80 : 0;
96
97
98
99
100 wm8739_write(sd, R0, (state->vol_l & 0x1f) | mute);
101 wm8739_write(sd, R1, (state->vol_r & 0x1f) | mute);
102}
103
104static int wm8739_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
105{
106 struct wm8739_state *state = to_state(sd);
107
108 switch (ctrl->id) {
109 case V4L2_CID_AUDIO_MUTE:
110 ctrl->value = state->muted;
111 break;
112
113 case V4L2_CID_AUDIO_VOLUME:
114 ctrl->value = state->volume;
115 break;
116
117 case V4L2_CID_AUDIO_BALANCE:
118 ctrl->value = state->balance;
119 break;
120
121 default:
122 return -EINVAL;
123 }
124 return 0;
125}
126
127static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
128{
129 struct wm8739_state *state = to_state(sd);
130 unsigned int work_l, work_r;
131
132 switch (ctrl->id) {
133 case V4L2_CID_AUDIO_MUTE:
134 state->muted = ctrl->value;
135 break;
136
137 case V4L2_CID_AUDIO_VOLUME:
138 state->volume = ctrl->value;
139 break;
140
141 case V4L2_CID_AUDIO_BALANCE:
142 state->balance = ctrl->value;
143 break;
144
145 default:
146 return -EINVAL;
147 }
148
149
150 work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768;
151 work_r = (min(state->balance, (u16)32768) * state->volume) / 32768;
152
153 state->vol_l = (long)work_l * 31 / 65535;
154 state->vol_r = (long)work_r * 31 / 65535;
155
156
157 wm8739_set_audio(sd);
158 return 0;
159}
160
161
162
163static struct v4l2_queryctrl wm8739_qctrl[] = {
164 {
165 .id = V4L2_CID_AUDIO_VOLUME,
166 .name = "Volume",
167 .minimum = 0,
168 .maximum = 65535,
169 .step = 65535/100,
170 .default_value = 58880,
171 .flags = 0,
172 .type = V4L2_CTRL_TYPE_INTEGER,
173 }, {
174 .id = V4L2_CID_AUDIO_MUTE,
175 .name = "Mute",
176 .minimum = 0,
177 .maximum = 1,
178 .step = 1,
179 .default_value = 1,
180 .flags = 0,
181 .type = V4L2_CTRL_TYPE_BOOLEAN,
182 }, {
183 .id = V4L2_CID_AUDIO_BALANCE,
184 .name = "Balance",
185 .minimum = 0,
186 .maximum = 65535,
187 .step = 65535/100,
188 .default_value = 32768,
189 .flags = 0,
190 .type = V4L2_CTRL_TYPE_INTEGER,
191 }
192};
193
194
195
196static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
197{
198 struct wm8739_state *state = to_state(sd);
199
200 state->clock_freq = audiofreq;
201
202 wm8739_write(sd, R9, 0x000);
203 switch (audiofreq) {
204 case 44100:
205
206 wm8739_write(sd, R8, 0x020);
207 break;
208 case 48000:
209
210 wm8739_write(sd, R8, 0x000);
211 break;
212 case 32000:
213
214 wm8739_write(sd, R8, 0x018);
215 break;
216 default:
217 break;
218 }
219
220 wm8739_write(sd, R9, 0x001);
221 return 0;
222}
223
224static int wm8739_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
225{
226 int i;
227
228 for (i = 0; i < ARRAY_SIZE(wm8739_qctrl); i++)
229 if (qc->id && qc->id == wm8739_qctrl[i].id) {
230 memcpy(qc, &wm8739_qctrl[i], sizeof(*qc));
231 return 0;
232 }
233 return -EINVAL;
234}
235
236static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
237{
238 struct i2c_client *client = v4l2_get_subdevdata(sd);
239
240 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_WM8739, 0);
241}
242
243static int wm8739_log_status(struct v4l2_subdev *sd)
244{
245 struct wm8739_state *state = to_state(sd);
246
247 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
248 v4l2_info(sd, "Volume L: %02x%s\n", state->vol_l & 0x1f,
249 state->muted ? " (muted)" : "");
250 v4l2_info(sd, "Volume R: %02x%s\n", state->vol_r & 0x1f,
251 state->muted ? " (muted)" : "");
252 return 0;
253}
254
255
256
257static const struct v4l2_subdev_core_ops wm8739_core_ops = {
258 .log_status = wm8739_log_status,
259 .g_chip_ident = wm8739_g_chip_ident,
260 .queryctrl = wm8739_queryctrl,
261 .g_ctrl = wm8739_g_ctrl,
262 .s_ctrl = wm8739_s_ctrl,
263};
264
265static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
266 .s_clock_freq = wm8739_s_clock_freq,
267};
268
269static const struct v4l2_subdev_ops wm8739_ops = {
270 .core = &wm8739_core_ops,
271 .audio = &wm8739_audio_ops,
272};
273
274
275
276
277
278static int wm8739_probe(struct i2c_client *client,
279 const struct i2c_device_id *id)
280{
281 struct wm8739_state *state;
282 struct v4l2_subdev *sd;
283
284
285 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
286 return -EIO;
287
288 v4l_info(client, "chip found @ 0x%x (%s)\n",
289 client->addr << 1, client->adapter->name);
290
291 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
292 if (state == NULL)
293 return -ENOMEM;
294 sd = &state->sd;
295 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
296 state->vol_l = 0x17;
297 state->vol_r = 0x17;
298 state->muted = 0;
299 state->balance = 32768;
300
301 state->volume = ((long)state->vol_l + 1) * 65535 / 31;
302 state->clock_freq = 48000;
303
304
305
306
307 wm8739_write(sd, R15, 0x00);
308
309 wm8739_write(sd, R5, 0x000);
310
311 wm8739_write(sd, R6, 0x000);
312
313
314 wm8739_write(sd, R7, 0x049);
315
316 wm8739_write(sd, R8, 0x000);
317
318 wm8739_write(sd, R9, 0x001);
319
320 wm8739_set_audio(sd);
321 return 0;
322}
323
324static int wm8739_remove(struct i2c_client *client)
325{
326 struct v4l2_subdev *sd = i2c_get_clientdata(client);
327
328 v4l2_device_unregister_subdev(sd);
329 kfree(to_state(sd));
330 return 0;
331}
332
333static const struct i2c_device_id wm8739_id[] = {
334 { "wm8739", 0 },
335 { }
336};
337MODULE_DEVICE_TABLE(i2c, wm8739_id);
338
339static struct v4l2_i2c_driver_data v4l2_i2c_data = {
340 .name = "wm8739",
341 .probe = wm8739_probe,
342 .remove = wm8739_remove,
343 .id_table = wm8739_id,
344};
345