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/i2c-addr.h>
39
40#ifndef VIDEO_AUDIO_BALANCE
41# define VIDEO_AUDIO_BALANCE 32
42#endif
43
44MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
45MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
46MODULE_LICENSE("GPL");
47
48static int maxvol;
49static int loudness;
50static int debug;
51module_param(debug, int, S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug, "Set debugging level from 0 to 3. Default is off(0).");
53module_param(loudness, int, S_IRUGO);
54MODULE_PARM_DESC(loudness, "Turn loudness on(1) else off(0). Default is off(0).");
55module_param(maxvol, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(maxvol, "Set maximium volume to +20dB(0) else +0dB(1). Default is +20dB(0).");
57
58
59
60
61struct tda7432 {
62 struct v4l2_subdev sd;
63 int addr;
64 int input;
65 int volume;
66 int muted;
67 int bass, treble;
68 int lf, lr, rf, rr;
69 int loud;
70};
71
72static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
73{
74 return container_of(sd, struct tda7432, sd);
75}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92#define TDA7432_IN 0x00
93#define TDA7432_VL 0x01
94#define TDA7432_TN 0x02
95#define TDA7432_LF 0x03
96#define TDA7432_LR 0x04
97#define TDA7432_RF 0x05
98#define TDA7432_RR 0x06
99#define TDA7432_LD 0x07
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121#define TDA7432_STEREO_IN 0
122#define TDA7432_MONO_IN 2
123#define TDA7432_BASS_SYM 1 << 3
124#define TDA7432_BASS_NORM 1 << 4
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139#define TDA7432_VOL_0DB 0x20
140#define TDA7432_LD_ON 1 << 7
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164#define TDA7432_TREBLE_0DB 0xf
165#define TDA7432_TREBLE 7
166#define TDA7432_TREBLE_GAIN 1 << 3
167#define TDA7432_BASS_0DB 0xf
168#define TDA7432_BASS 7 << 4
169#define TDA7432_BASS_GAIN 1 << 7
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188#define TDA7432_ATTEN_0DB 0x00
189#define TDA7432_MUTE 0x1 << 5
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
211{
212 struct i2c_client *client = v4l2_get_subdevdata(sd);
213 unsigned char buffer[2];
214
215 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
216 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
217 buffer[0] = subaddr;
218 buffer[1] = val;
219 if (2 != i2c_master_send(client, buffer, 2)) {
220 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
221 subaddr, val);
222 return -1;
223 }
224 return 0;
225}
226
227static int tda7432_set(struct v4l2_subdev *sd)
228{
229 struct i2c_client *client = v4l2_get_subdevdata(sd);
230 struct tda7432 *t = to_state(sd);
231 unsigned char buf[16];
232
233 v4l2_dbg(1, debug, sd,
234 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
235 t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
236 t->rf, t->rr, t->loud);
237 buf[0] = TDA7432_IN;
238 buf[1] = t->input;
239 buf[2] = t->volume;
240 buf[3] = t->bass;
241 buf[4] = t->treble;
242 buf[5] = t->lf;
243 buf[6] = t->lr;
244 buf[7] = t->rf;
245 buf[8] = t->rr;
246 buf[9] = t->loud;
247 if (10 != i2c_master_send(client, buf, 10)) {
248 v4l2_err(sd, "I/O error, trying tda7432_set\n");
249 return -1;
250 }
251
252 return 0;
253}
254
255static void do_tda7432_init(struct v4l2_subdev *sd)
256{
257 struct tda7432 *t = to_state(sd);
258
259 v4l2_dbg(2, debug, sd, "In tda7432_init\n");
260
261 t->input = TDA7432_STEREO_IN |
262 TDA7432_BASS_SYM |
263 TDA7432_BASS_NORM;
264 t->volume = 0x3b ;
265 if (loudness)
266 t->volume |= TDA7432_LD_ON;
267 t->muted = 1;
268 t->treble = TDA7432_TREBLE_0DB;
269 t->bass = TDA7432_BASS_0DB;
270 t->lf = TDA7432_ATTEN_0DB;
271 t->lr = TDA7432_ATTEN_0DB;
272 t->rf = TDA7432_ATTEN_0DB;
273 t->rr = TDA7432_ATTEN_0DB;
274 t->loud = loudness;
275
276 tda7432_set(sd);
277}
278
279static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
280{
281 struct tda7432 *t = to_state(sd);
282
283 switch (ctrl->id) {
284 case V4L2_CID_AUDIO_MUTE:
285 ctrl->value=t->muted;
286 return 0;
287 case V4L2_CID_AUDIO_VOLUME:
288 if (!maxvol){
289 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
290 } else {
291 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
292 }
293 return 0;
294 case V4L2_CID_AUDIO_BALANCE:
295 {
296 if ( (t->lf) < (t->rf) )
297
298 ctrl->value = (32768 - 1057*(t->rf));
299 else
300
301 ctrl->value = (32768 + 1057*(t->lf));
302 return 0;
303 }
304 case V4L2_CID_AUDIO_BASS:
305 {
306
307 int bass=t->bass;
308 if(bass >= 0x8)
309 bass = ~(bass - 0x8) & 0xf;
310 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
311 return 0;
312 }
313 case V4L2_CID_AUDIO_TREBLE:
314 {
315 int treble=t->treble;
316 if(treble >= 0x8)
317 treble = ~(treble - 0x8) & 0xf;
318 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
319 return 0;
320 }
321 }
322 return -EINVAL;
323}
324
325static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
326{
327 struct tda7432 *t = to_state(sd);
328
329 switch (ctrl->id) {
330 case V4L2_CID_AUDIO_MUTE:
331 t->muted=ctrl->value;
332 break;
333 case V4L2_CID_AUDIO_VOLUME:
334 if(!maxvol){
335 t->volume = 0x6f - ((ctrl->value)/630);
336 } else {
337 t->volume = 0x6f - ((ctrl->value)/829);
338 }
339 if (loudness)
340 t->volume |= TDA7432_LD_ON;
341
342 tda7432_write(sd, TDA7432_VL, t->volume);
343 return 0;
344 case V4L2_CID_AUDIO_BALANCE:
345 if (ctrl->value < 32768) {
346
347 t->rr = (32768 - ctrl->value)/1057;
348 t->rf = t->rr;
349 t->lr = TDA7432_ATTEN_0DB;
350 t->lf = TDA7432_ATTEN_0DB;
351 } else if(ctrl->value > 32769) {
352
353 t->lf = (ctrl->value - 32768)/1057;
354 t->lr = t->lf;
355 t->rr = TDA7432_ATTEN_0DB;
356 t->rf = TDA7432_ATTEN_0DB;
357 } else {
358
359 t->rr = TDA7432_ATTEN_0DB;
360 t->rf = TDA7432_ATTEN_0DB;
361 t->lf = TDA7432_ATTEN_0DB;
362 t->lr = TDA7432_ATTEN_0DB;
363 }
364 break;
365 case V4L2_CID_AUDIO_BASS:
366 t->bass = ctrl->value >> 12;
367 if(t->bass>= 0x8)
368 t->bass = (~t->bass & 0xf) + 0x8 ;
369
370 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
371 return 0;
372 case V4L2_CID_AUDIO_TREBLE:
373 t->treble= ctrl->value >> 12;
374 if(t->treble>= 0x8)
375 t->treble = (~t->treble & 0xf) + 0x8 ;
376
377 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
378 return 0;
379 default:
380 return -EINVAL;
381 }
382
383
384 if (t->muted)
385 {
386
387 tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
388 tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
389 tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
390 tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
391 } else {
392 tda7432_write(sd, TDA7432_LF, t->lf);
393 tda7432_write(sd, TDA7432_LR, t->lr);
394 tda7432_write(sd, TDA7432_RF, t->rf);
395 tda7432_write(sd, TDA7432_RR, t->rr);
396 }
397 return 0;
398}
399
400static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
401{
402 switch (qc->id) {
403 case V4L2_CID_AUDIO_VOLUME:
404 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
405 case V4L2_CID_AUDIO_MUTE:
406 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
407 case V4L2_CID_AUDIO_BALANCE:
408 case V4L2_CID_AUDIO_BASS:
409 case V4L2_CID_AUDIO_TREBLE:
410 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
411 }
412 return -EINVAL;
413}
414
415
416
417static const struct v4l2_subdev_core_ops tda7432_core_ops = {
418 .queryctrl = tda7432_queryctrl,
419 .g_ctrl = tda7432_g_ctrl,
420 .s_ctrl = tda7432_s_ctrl,
421};
422
423static const struct v4l2_subdev_ops tda7432_ops = {
424 .core = &tda7432_core_ops,
425};
426
427
428
429
430
431
432
433static int tda7432_probe(struct i2c_client *client,
434 const struct i2c_device_id *id)
435{
436 struct tda7432 *t;
437 struct v4l2_subdev *sd;
438
439 v4l_info(client, "chip found @ 0x%02x (%s)\n",
440 client->addr << 1, client->adapter->name);
441
442 t = kzalloc(sizeof(*t), GFP_KERNEL);
443 if (!t)
444 return -ENOMEM;
445 sd = &t->sd;
446 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
447 if (loudness < 0 || loudness > 15) {
448 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
449 if (loudness < 0)
450 loudness = 0;
451 if (loudness > 15)
452 loudness = 15;
453 }
454
455 do_tda7432_init(sd);
456 return 0;
457}
458
459static int tda7432_remove(struct i2c_client *client)
460{
461 struct v4l2_subdev *sd = i2c_get_clientdata(client);
462
463 do_tda7432_init(sd);
464 v4l2_device_unregister_subdev(sd);
465 kfree(to_state(sd));
466 return 0;
467}
468
469static const struct i2c_device_id tda7432_id[] = {
470 { "tda7432", 0 },
471 { }
472};
473MODULE_DEVICE_TABLE(i2c, tda7432_id);
474
475static struct i2c_driver tda7432_driver = {
476 .driver = {
477 .owner = THIS_MODULE,
478 .name = "tda7432",
479 },
480 .probe = tda7432_probe,
481 .remove = tda7432_remove,
482 .id_table = tda7432_id,
483};
484
485module_i2c_driver(tda7432_driver);
486