1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/delay.h>
31#include <linux/errno.h>
32#include <linux/slab.h>
33#include <linux/videodev2.h>
34#include <linux/i2c.h>
35
36#include <media/v4l2-device.h>
37#include <media/v4l2-ioctl.h>
38#include <media/v4l2-ctrls.h>
39#include <media/i2c-addr.h>
40
41#ifndef VIDEO_AUDIO_BALANCE
42# define VIDEO_AUDIO_BALANCE 32
43#endif
44
45MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
46MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
47MODULE_LICENSE("GPL");
48
49static int maxvol;
50static int loudness;
51static int debug;
52module_param(debug, int, S_IRUGO | S_IWUSR);
53MODULE_PARM_DESC(debug, "Set debugging level from 0 to 3. Default is off(0).");
54module_param(loudness, int, S_IRUGO);
55MODULE_PARM_DESC(loudness, "Turn loudness on(1) else off(0). Default is off(0).");
56module_param(maxvol, int, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(maxvol, "Set maximium volume to +20dB(0) else +0dB(1). Default is +20dB(0).");
58
59
60
61
62struct tda7432 {
63 struct v4l2_subdev sd;
64 struct v4l2_ctrl_handler hdl;
65 struct {
66
67 struct v4l2_ctrl *bass;
68 struct v4l2_ctrl *treble;
69 };
70 struct {
71
72 struct v4l2_ctrl *mute;
73 struct v4l2_ctrl *balance;
74 };
75};
76
77static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
78{
79 return container_of(sd, struct tda7432, sd);
80}
81
82static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
83{
84 return &container_of(ctrl->handler, struct tda7432, hdl)->sd;
85}
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102#define TDA7432_IN 0x00
103#define TDA7432_VL 0x01
104#define TDA7432_TN 0x02
105#define TDA7432_LF 0x03
106#define TDA7432_LR 0x04
107#define TDA7432_RF 0x05
108#define TDA7432_RR 0x06
109#define TDA7432_LD 0x07
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131#define TDA7432_STEREO_IN 0
132#define TDA7432_MONO_IN 2
133#define TDA7432_BASS_SYM 1 << 3
134#define TDA7432_BASS_NORM 1 << 4
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149#define TDA7432_VOL_0DB 0x20
150#define TDA7432_LD_ON 1 << 7
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174#define TDA7432_TREBLE_0DB 0xf
175#define TDA7432_TREBLE 7
176#define TDA7432_TREBLE_GAIN 1 << 3
177#define TDA7432_BASS_0DB 0xf
178#define TDA7432_BASS 7 << 4
179#define TDA7432_BASS_GAIN 1 << 7
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198#define TDA7432_ATTEN_0DB 0x00
199#define TDA7432_MUTE 0x1 << 5
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
221{
222 struct i2c_client *client = v4l2_get_subdevdata(sd);
223 unsigned char buffer[2];
224
225 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
226 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
227 buffer[0] = subaddr;
228 buffer[1] = val;
229 if (2 != i2c_master_send(client, buffer, 2)) {
230 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
231 subaddr, val);
232 return -1;
233 }
234 return 0;
235}
236
237static int tda7432_set(struct v4l2_subdev *sd)
238{
239 struct i2c_client *client = v4l2_get_subdevdata(sd);
240 unsigned char buf[16];
241
242 buf[0] = TDA7432_IN;
243 buf[1] = TDA7432_STEREO_IN |
244 TDA7432_BASS_SYM |
245 TDA7432_BASS_NORM;
246 buf[2] = 0x3b;
247 if (loudness)
248 buf[2] |= TDA7432_LD_ON;
249 buf[3] = TDA7432_TREBLE_0DB | (TDA7432_BASS_0DB << 4);
250 buf[4] = TDA7432_ATTEN_0DB;
251 buf[5] = TDA7432_ATTEN_0DB;
252 buf[6] = TDA7432_ATTEN_0DB;
253 buf[7] = TDA7432_ATTEN_0DB;
254 buf[8] = loudness;
255 if (9 != i2c_master_send(client, buf, 9)) {
256 v4l2_err(sd, "I/O error, trying tda7432_set\n");
257 return -1;
258 }
259
260 return 0;
261}
262
263static int tda7432_log_status(struct v4l2_subdev *sd)
264{
265 struct tda7432 *state = to_state(sd);
266
267 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
268 return 0;
269}
270
271static int tda7432_s_ctrl(struct v4l2_ctrl *ctrl)
272{
273 struct v4l2_subdev *sd = to_sd(ctrl);
274 struct tda7432 *t = to_state(sd);
275 u8 bass, treble, volume;
276 u8 lf, lr, rf, rr;
277
278 switch (ctrl->id) {
279 case V4L2_CID_AUDIO_MUTE:
280 if (t->balance->val < 0) {
281
282 rr = rf = -t->balance->val;
283 lr = lf = TDA7432_ATTEN_0DB;
284 } else if (t->balance->val > 0) {
285
286 rr = rf = TDA7432_ATTEN_0DB;
287 lr = lf = t->balance->val;
288 } else {
289
290 rr = rf = TDA7432_ATTEN_0DB;
291 lr = lf = TDA7432_ATTEN_0DB;
292 }
293 if (t->mute->val) {
294 lf |= TDA7432_MUTE;
295 lr |= TDA7432_MUTE;
296 lf |= TDA7432_MUTE;
297 rr |= TDA7432_MUTE;
298 }
299
300 tda7432_write(sd, TDA7432_LF, lf);
301 tda7432_write(sd, TDA7432_LR, lr);
302 tda7432_write(sd, TDA7432_RF, rf);
303 tda7432_write(sd, TDA7432_RR, rr);
304 return 0;
305 case V4L2_CID_AUDIO_VOLUME:
306 volume = 0x6f - ctrl->val;
307 if (loudness)
308 volume |= TDA7432_LD_ON;
309
310 tda7432_write(sd, TDA7432_VL, volume);
311 return 0;
312 case V4L2_CID_AUDIO_BASS:
313 bass = t->bass->val;
314 treble = t->treble->val;
315 if (bass >= 0x8)
316 bass = 14 - (bass - 8);
317 if (treble >= 0x8)
318 treble = 14 - (treble - 8);
319
320 tda7432_write(sd, TDA7432_TN, 0x10 | (bass << 4) | treble);
321 return 0;
322 }
323 return -EINVAL;
324}
325
326
327
328static const struct v4l2_ctrl_ops tda7432_ctrl_ops = {
329 .s_ctrl = tda7432_s_ctrl,
330};
331
332static const struct v4l2_subdev_core_ops tda7432_core_ops = {
333 .log_status = tda7432_log_status,
334 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
335 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
336 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
337 .g_ctrl = v4l2_subdev_g_ctrl,
338 .s_ctrl = v4l2_subdev_s_ctrl,
339 .queryctrl = v4l2_subdev_queryctrl,
340 .querymenu = v4l2_subdev_querymenu,
341};
342
343static const struct v4l2_subdev_ops tda7432_ops = {
344 .core = &tda7432_core_ops,
345};
346
347
348
349
350
351
352
353static int tda7432_probe(struct i2c_client *client,
354 const struct i2c_device_id *id)
355{
356 struct tda7432 *t;
357 struct v4l2_subdev *sd;
358
359 v4l_info(client, "chip found @ 0x%02x (%s)\n",
360 client->addr << 1, client->adapter->name);
361
362 t = kzalloc(sizeof(*t), GFP_KERNEL);
363 if (!t)
364 return -ENOMEM;
365 sd = &t->sd;
366 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
367 v4l2_ctrl_handler_init(&t->hdl, 5);
368 v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
369 V4L2_CID_AUDIO_VOLUME, 0, maxvol ? 0x68 : 0x4f, 1, maxvol ? 0x5d : 0x47);
370 t->mute = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
371 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
372 t->balance = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
373 V4L2_CID_AUDIO_BALANCE, -31, 31, 1, 0);
374 t->bass = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
375 V4L2_CID_AUDIO_BASS, 0, 14, 1, 7);
376 t->treble = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
377 V4L2_CID_AUDIO_TREBLE, 0, 14, 1, 7);
378 sd->ctrl_handler = &t->hdl;
379 if (t->hdl.error) {
380 int err = t->hdl.error;
381
382 v4l2_ctrl_handler_free(&t->hdl);
383 kfree(t);
384 return err;
385 }
386 v4l2_ctrl_cluster(2, &t->bass);
387 v4l2_ctrl_cluster(2, &t->mute);
388 v4l2_ctrl_handler_setup(&t->hdl);
389 if (loudness < 0 || loudness > 15) {
390 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
391 if (loudness < 0)
392 loudness = 0;
393 if (loudness > 15)
394 loudness = 15;
395 }
396
397 tda7432_set(sd);
398 return 0;
399}
400
401static int tda7432_remove(struct i2c_client *client)
402{
403 struct v4l2_subdev *sd = i2c_get_clientdata(client);
404 struct tda7432 *t = to_state(sd);
405
406 tda7432_set(sd);
407 v4l2_device_unregister_subdev(sd);
408 v4l2_ctrl_handler_free(&t->hdl);
409 kfree(t);
410 return 0;
411}
412
413static const struct i2c_device_id tda7432_id[] = {
414 { "tda7432", 0 },
415 { }
416};
417MODULE_DEVICE_TABLE(i2c, tda7432_id);
418
419static struct i2c_driver tda7432_driver = {
420 .driver = {
421 .owner = THIS_MODULE,
422 .name = "tda7432",
423 },
424 .probe = tda7432_probe,
425 .remove = tda7432_remove,
426 .id_table = tda7432_id,
427};
428
429module_i2c_driver(tda7432_driver);
430