V4L/DVB (9963): tlv320aic23b: convert to v4l2_subdev.
[deliverable/linux.git] / drivers / media / video / tda7432.c
CommitLineData
1da177e4
LT
1/*
2 * For the STS-Thompson TDA7432 audio processor chip
3 *
4 * Handles audio functions: volume, balance, tone, loudness
5 * This driver will not complain if used with any
6 * other i2c device with the same address.
7 *
8 * Muting and tone control by Jonathan Isom <jisom@ematic.com>
9 *
10 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
7a00d45c 11 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
12 * This code is placed under the terms of the GNU General Public License
13 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
14 * Which was based on tda8425.c by Greg Alexander (c) 1998
15 *
16 * OPTIONS:
17 * debug - set to 1 if you'd like to see debug messages
18 * set to 2 if you'd like to be inundated with debug messages
19 *
20 * loudness - set between 0 and 15 for varying degrees of loudness effect
21 *
22 * maxvol - set maximium volume to +20db (1), default is 0db(0)
23 *
24 *
25 * Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
26 * store if muted so we can return it
27 * change balance only if flaged to
28 * Revision: 0.6 - added tone controls
29 * Revision: 0.5 - Fixed odd balance problem
30 * Revision: 0.4 - added muting
31 * Revision: 0.3 - Fixed silly reversed volume controls. :)
32 * Revision: 0.2 - Cleaned up #defines
33 * fixed volume control
34 * Added I2C_DRIVERID_TDA7432
35 * added loudness insmod control
36 * Revision: 0.1 - initial version
37 */
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/kernel.h>
1da177e4
LT
42#include <linux/string.h>
43#include <linux/timer.h>
44#include <linux/delay.h>
45#include <linux/errno.h>
46#include <linux/slab.h>
33b687cf 47#include <linux/videodev2.h>
1da177e4 48#include <linux/i2c.h>
1da177e4 49
0680481c 50#include <media/v4l2-common.h>
35ea11ff 51#include <media/v4l2-ioctl.h>
fa3fcceb 52#include <media/i2c-addr.h>
1da177e4
LT
53
54#ifndef VIDEO_AUDIO_BALANCE
55# define VIDEO_AUDIO_BALANCE 32
56#endif
57
58MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
59MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
60MODULE_LICENSE("GPL");
61
62static int maxvol;
63static int loudness; /* disable loudness by default */
64static int debug; /* insmod parameter */
65module_param(debug, int, S_IRUGO | S_IWUSR);
66module_param(loudness, int, S_IRUGO);
67MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
68module_param(maxvol, int, S_IRUGO | S_IWUSR);
69
70
71/* Address to scan (I2C address of this chip) */
72static unsigned short normal_i2c[] = {
09df1c16 73 I2C_ADDR_TDA7432 >> 1,
1da177e4
LT
74 I2C_CLIENT_END,
75};
f87086e3 76
1da177e4
LT
77I2C_CLIENT_INSMOD;
78
79/* Structure of address and subaddresses for the tda7432 */
80
81struct tda7432 {
82 int addr;
83 int input;
84 int volume;
85 int muted;
86 int bass, treble;
87 int lf, lr, rf, rr;
88 int loud;
89 struct i2c_client c;
90};
91static struct i2c_driver driver;
92static struct i2c_client client_template;
93
1da177e4
LT
94/* The TDA7432 is made by STS-Thompson
95 * http://www.st.com
96 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
97 *
98 * TDA7432: I2C-bus controlled basic audio processor
99 *
100 * The TDA7432 controls basic audio functions like volume, balance,
101 * and tone control (including loudness). It also has four channel
102 * output (for front and rear). Since most vidcap cards probably
103 * don't have 4 channel output, this driver will set front & rear
104 * together (no independent control).
105 */
106
107 /* Subaddresses for TDA7432 */
108
109#define TDA7432_IN 0x00 /* Input select */
110#define TDA7432_VL 0x01 /* Volume */
111#define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
112#define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
113#define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
114#define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
115#define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
116#define TDA7432_LD 0x07 /* Loudness */
117
118
119 /* Masks for bits in TDA7432 subaddresses */
120
121/* Many of these not used - just for documentation */
122
123/* Subaddress 0x00 - Input selection and bass control */
124
125/* Bits 0,1,2 control input:
126 * 0x00 - Stereo input
127 * 0x02 - Mono input
128 * 0x03 - Mute (Using Attenuators Plays better with modules)
129 * Mono probably isn't used - I'm guessing only the stereo
130 * input is connected on most cards, so we'll set it to stereo.
131 *
132 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
133 * Bit 4 controls bass range: 0/1 is extended/standard bass range
134 *
135 * Highest 3 bits not used
136 */
137
138#define TDA7432_STEREO_IN 0
139#define TDA7432_MONO_IN 2 /* Probably won't be used */
140#define TDA7432_BASS_SYM 1 << 3
141#define TDA7432_BASS_NORM 1 << 4
142
143/* Subaddress 0x01 - Volume */
144
145/* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
146 * Recommended maximum is +20 dB
147 *
148 * +32dB: 0x00
149 * +20dB: 0x0c
150 * 0dB: 0x20
151 * -79dB: 0x6f
152 *
153 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
154 */
155
156#define TDA7432_VOL_0DB 0x20
157#define TDA7432_LD_ON 1 << 7
158
159
160/* Subaddress 0x02 - Tone control */
161
162/* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
163 * 0x0 is 14dB, 0x7 is 0dB
164 *
165 * Bit 3 controls treble attenuation/gain (sign)
166 * 1 = gain (+)
167 * 0 = attenuation (-)
168 *
169 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
170 * (This is only true for normal base range, set in 0x00)
171 * 0x0 << 4 is 14dB, 0x7 is 0dB
172 *
173 * Bit 7 controls bass attenuation/gain (sign)
174 * 1 << 7 = gain (+)
175 * 0 << 7 = attenuation (-)
176 *
177 * Example:
178 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
179 */
180
181#define TDA7432_TREBLE_0DB 0xf
182#define TDA7432_TREBLE 7
183#define TDA7432_TREBLE_GAIN 1 << 3
184#define TDA7432_BASS_0DB 0xf
185#define TDA7432_BASS 7 << 4
186#define TDA7432_BASS_GAIN 1 << 7
187
188
189/* Subaddress 0x03 - Left Front attenuation */
190/* Subaddress 0x04 - Left Rear attenuation */
191/* Subaddress 0x05 - Right Front attenuation */
192/* Subaddress 0x06 - Right Rear attenuation */
193
194/* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
195 * in 1.5dB steps.
196 *
197 * 0x00 is 0dB
198 * 0x1f is -37.5dB
199 *
200 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
201 * We'll use the mute on the input, though (above)
202 * Bits 6,7 unused
203 */
204
205#define TDA7432_ATTEN_0DB 0x00
206#define TDA7432_MUTE 0x1 << 5
207
208
209/* Subaddress 0x07 - Loudness Control */
210
211/* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
212 * when bit 4 is NOT set
213 *
214 * 0x0 is 0dB
215 * 0xf is -15dB
216 *
217 * If bit 4 is set, then there is a flat attenuation according to
218 * the lower 4 bits, as above.
219 *
220 * Bits 5,6,7 unused
221 */
222
223
224
225/* Begin code */
226
227static int tda7432_write(struct i2c_client *client, int subaddr, int val)
228{
229 unsigned char buffer[2];
f167cb4e
MCC
230 v4l_dbg(2, debug,client,"In tda7432_write\n");
231 v4l_dbg(1, debug,client,"Writing %d 0x%x\n", subaddr, val);
1da177e4
LT
232 buffer[0] = subaddr;
233 buffer[1] = val;
234 if (2 != i2c_master_send(client,buffer,2)) {
0680481c 235 v4l_err(client,"I/O error, trying (write %d 0x%x)\n",
1da177e4
LT
236 subaddr, val);
237 return -1;
238 }
239 return 0;
240}
241
242/* I don't think we ever actually _read_ the chip... */
1da177e4
LT
243
244static int tda7432_set(struct i2c_client *client)
245{
246 struct tda7432 *t = i2c_get_clientdata(client);
247 unsigned char buf[16];
f167cb4e 248 v4l_dbg(2, debug,client,"In tda7432_set\n");
1da177e4 249
f167cb4e 250 v4l_dbg(1, debug,client,
1da177e4
LT
251 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
252 t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
253 buf[0] = TDA7432_IN;
254 buf[1] = t->input;
255 buf[2] = t->volume;
256 buf[3] = t->bass;
257 buf[4] = t->treble;
258 buf[5] = t->lf;
259 buf[6] = t->lr;
260 buf[7] = t->rf;
261 buf[8] = t->rr;
262 buf[9] = t->loud;
263 if (10 != i2c_master_send(client,buf,10)) {
0680481c 264 v4l_err(client,"I/O error, trying tda7432_set\n");
1da177e4
LT
265 return -1;
266 }
267
268 return 0;
269}
270
271static void do_tda7432_init(struct i2c_client *client)
272{
273 struct tda7432 *t = i2c_get_clientdata(client);
f167cb4e 274 v4l_dbg(2, debug,client,"In tda7432_init\n");
1da177e4
LT
275
276 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
277 TDA7432_BASS_SYM | /* Symmetric bass cut */
278 TDA7432_BASS_NORM; /* Normal bass range */
279 t->volume = 0x3b ; /* -27dB Volume */
280 if (loudness) /* Turn loudness on? */
281 t->volume |= TDA7432_LD_ON;
7a00d45c 282 t->muted = 1;
1da177e4
LT
283 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
284 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
285 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
286 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
287 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
288 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
289 t->loud = loudness; /* insmod parameter */
290
291 tda7432_set(client);
292}
293
294/* *********************** *
295 * i2c interface functions *
296 * *********************** */
297
298static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
299{
300 struct tda7432 *t;
301 struct i2c_client *client;
1da177e4 302
7408187d 303 t = kzalloc(sizeof *t,GFP_KERNEL);
1da177e4
LT
304 if (!t)
305 return -ENOMEM;
1da177e4
LT
306
307 client = &t->c;
4ac97914
MCC
308 memcpy(client,&client_template,sizeof(struct i2c_client));
309 client->adapter = adap;
310 client->addr = addr;
1da177e4
LT
311 i2c_set_clientdata(client, t);
312
313 do_tda7432_init(client);
1da177e4 314 i2c_attach_client(client);
0680481c
MCC
315
316 v4l_info(client, "chip found @ 0x%x (%s)\n", addr << 1, adap->name);
1da177e4
LT
317 return 0;
318}
319
320static int tda7432_probe(struct i2c_adapter *adap)
321{
1da177e4
LT
322 if (adap->class & I2C_CLASS_TV_ANALOG)
323 return i2c_probe(adap, &addr_data, tda7432_attach);
1da177e4
LT
324 return 0;
325}
326
327static int tda7432_detach(struct i2c_client *client)
328{
329 struct tda7432 *t = i2c_get_clientdata(client);
330
331 do_tda7432_init(client);
332 i2c_detach_client(client);
333
334 kfree(t);
335 return 0;
336}
337
7a00d45c
MCC
338static int tda7432_get_ctrl(struct i2c_client *client,
339 struct v4l2_control *ctrl)
1da177e4
LT
340{
341 struct tda7432 *t = i2c_get_clientdata(client);
1da177e4 342
7a00d45c
MCC
343 switch (ctrl->id) {
344 case V4L2_CID_AUDIO_MUTE:
345 ctrl->value=t->muted;
346 return 0;
347 case V4L2_CID_AUDIO_VOLUME:
1da177e4 348 if (!maxvol){ /* max +20db */
7a00d45c 349 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
1da177e4 350 } else { /* max 0db */
7a00d45c 351 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
1da177e4 352 }
7a00d45c
MCC
353 return 0;
354 case V4L2_CID_AUDIO_BALANCE:
355 {
1da177e4
LT
356 if ( (t->lf) < (t->rf) )
357 /* right is attenuated, balance shifted left */
7a00d45c 358 ctrl->value = (32768 - 1057*(t->rf));
1da177e4
LT
359 else
360 /* left is attenuated, balance shifted right */
7a00d45c
MCC
361 ctrl->value = (32768 + 1057*(t->lf));
362 return 0;
363 }
364 case V4L2_CID_AUDIO_BASS:
365 {
1da177e4 366 /* Bass/treble 4 bits each */
7a00d45c
MCC
367 int bass=t->bass;
368 if(bass >= 0x8)
369 bass = ~(bass - 0x8) & 0xf;
370 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
371 return 0;
1da177e4 372 }
7a00d45c 373 case V4L2_CID_AUDIO_TREBLE:
1da177e4 374 {
7a00d45c
MCC
375 int treble=t->treble;
376 if(treble >= 0x8)
377 treble = ~(treble - 0x8) & 0xf;
378 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
379 return 0;
380 }
381 }
382 return -EINVAL;
383}
1da177e4 384
7a00d45c
MCC
385static int tda7432_set_ctrl(struct i2c_client *client,
386 struct v4l2_control *ctrl)
387{
388 struct tda7432 *t = i2c_get_clientdata(client);
1da177e4 389
7a00d45c
MCC
390 switch (ctrl->id) {
391 case V4L2_CID_AUDIO_MUTE:
392 t->muted=ctrl->value;
393 break;
394 case V4L2_CID_AUDIO_VOLUME:
395 if(!maxvol){ /* max +20db */
396 t->volume = 0x6f - ((ctrl->value)/630);
397 } else { /* max 0db */
398 t->volume = 0x6f - ((ctrl->value)/829);
399 }
1da177e4
LT
400 if (loudness) /* Turn on the loudness bit */
401 t->volume |= TDA7432_LD_ON;
402
7a00d45c
MCC
403 tda7432_write(client,TDA7432_VL, t->volume);
404 return 0;
405 case V4L2_CID_AUDIO_BALANCE:
406 if (ctrl->value < 32768) {
1da177e4 407 /* shifted to left, attenuate right */
7a00d45c 408 t->rr = (32768 - ctrl->value)/1057;
1da177e4
LT
409 t->rf = t->rr;
410 t->lr = TDA7432_ATTEN_0DB;
411 t->lf = TDA7432_ATTEN_0DB;
7a00d45c 412 } else if(ctrl->value > 32769) {
1da177e4 413 /* shifted to right, attenuate left */
7a00d45c 414 t->lf = (ctrl->value - 32768)/1057;
1da177e4
LT
415 t->lr = t->lf;
416 t->rr = TDA7432_ATTEN_0DB;
417 t->rf = TDA7432_ATTEN_0DB;
7a00d45c 418 } else {
1da177e4
LT
419 /* centered */
420 t->rr = TDA7432_ATTEN_0DB;
421 t->rf = TDA7432_ATTEN_0DB;
422 t->lf = TDA7432_ATTEN_0DB;
423 t->lr = TDA7432_ATTEN_0DB;
424 }
7a00d45c
MCC
425 break;
426 case V4L2_CID_AUDIO_BASS:
427 t->bass = ctrl->value >> 12;
428 if(t->bass>= 0x8)
429 t->bass = (~t->bass & 0xf) + 0x8 ;
430
431 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
432 return 0;
433 case V4L2_CID_AUDIO_TREBLE:
434 t->treble= ctrl->value >> 12;
435 if(t->treble>= 0x8)
436 t->treble = (~t->treble & 0xf) + 0x8 ;
437
438 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
439 return 0;
440 default:
441 return -EINVAL;
442 }
1da177e4 443
7a00d45c
MCC
444 /* Used for both mute and balance changes */
445 if (t->muted)
446 {
447 /* Mute & update balance*/
448 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
449 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
450 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
451 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
452 } else {
453 tda7432_write(client,TDA7432_LF, t->lf);
454 tda7432_write(client,TDA7432_LR, t->lr);
455 tda7432_write(client,TDA7432_RF, t->rf);
456 tda7432_write(client,TDA7432_RR, t->rr);
457 }
458 return 0;
459}
1da177e4 460
7a00d45c
MCC
461static int tda7432_command(struct i2c_client *client,
462 unsigned int cmd, void *arg)
463{
464 v4l_dbg(2, debug,client,"In tda7432_command\n");
465 if (debug>1)
466 v4l_i2c_print_ioctl(client,cmd);
467
468 switch (cmd) {
469 /* --- v4l ioctls --- */
470 /* take care: bttv does userspace copying, we'll get a
471 kernel pointer here... */
472 case VIDIOC_QUERYCTRL:
473 {
474 struct v4l2_queryctrl *qc = arg;
475
476 switch (qc->id) {
477 case V4L2_CID_AUDIO_MUTE:
478 case V4L2_CID_AUDIO_VOLUME:
479 case V4L2_CID_AUDIO_BALANCE:
480 case V4L2_CID_AUDIO_BASS:
481 case V4L2_CID_AUDIO_TREBLE:
482 default:
483 return -EINVAL;
484 }
485 return v4l2_ctrl_query_fill_std(qc);
486 }
487 case VIDIOC_S_CTRL:
488 return tda7432_set_ctrl(client, arg);
1da177e4 489
7a00d45c
MCC
490 case VIDIOC_G_CTRL:
491 return tda7432_get_ctrl(client, arg);
1da177e4 492
1da177e4
LT
493 } /* end of (cmd) switch */
494
495 return 0;
496}
497
498static struct i2c_driver driver = {
604f28e2 499 .driver = {
cab462f7 500 .name = "tda7432",
604f28e2 501 },
1da177e4 502 .id = I2C_DRIVERID_TDA7432,
1da177e4 503 .attach_adapter = tda7432_probe,
4ac97914
MCC
504 .detach_client = tda7432_detach,
505 .command = tda7432_command,
1da177e4
LT
506};
507
508static struct i2c_client client_template =
509{
fae91e72 510 .name = "tda7432",
1da177e4
LT
511 .driver = &driver,
512};
513
514static int __init tda7432_init(void)
515{
516 if ( (loudness < 0) || (loudness > 15) ) {
0680481c 517 printk(KERN_ERR "loudness parameter must be between 0 and 15\n");
1da177e4
LT
518 return -EINVAL;
519 }
520
521 return i2c_add_driver(&driver);
522}
523
524static void __exit tda7432_fini(void)
525{
526 i2c_del_driver(&driver);
527}
528
529module_init(tda7432_init);
530module_exit(tda7432_fini);
531
532/*
533 * Local variables:
534 * c-basic-offset: 8
535 * End:
536 */
This page took 0.501527 seconds and 5 git commands to generate.