OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [media/] [video/] [tda7432.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.