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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [media/] [video/] [tda9875.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 TDA9875 chip
3
 * (The TDA9875 is used on the Diamond DTV2000 french version
4
 * Other cards probably use these chips as well.)
5
 * This driver will not complain if used with any
6
 * other i2c device with the same address.
7
 *
8
 * Copyright (c) 2000 Guillaume Delvit based on Gerd Knorr source and
9
 * Eric Sandeen
10
 * This code is placed under the terms of the GNU General Public License
11
 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
12
 * Which was based on tda8425.c by Greg Alexander (c) 1998
13
 *
14
 * OPTIONS:
15
 * debug   - set to 1 if you'd like to see debug messages
16
 *
17
 *  Revision: 0.1 - original version
18
 */
19
 
20
#include <linux/module.h>
21
#include <linux/kernel.h>
22
#include <linux/sched.h>
23
#include <linux/string.h>
24
#include <linux/timer.h>
25
#include <linux/delay.h>
26
#include <linux/errno.h>
27
#include <linux/slab.h>
28
#include <linux/videodev.h>
29
#include <linux/i2c.h>
30
#include <linux/i2c-algo-bit.h>
31
#include <linux/init.h>
32
 
33
#include "bttv.h"
34
#include "audiochip.h"
35
#include "id.h"
36
#include "i2c-compat.h"
37
 
38
MODULE_PARM(debug,"i");
39
MODULE_LICENSE("GPL");
40
 
41
static int debug = 0;    /* insmod parameter */
42
 
43
/* Addresses to scan */
44
static unsigned short normal_i2c[] =  {
45
    I2C_TDA9875 >> 1,
46
    I2C_CLIENT_END
47
};
48
static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
49
I2C_CLIENT_INSMOD;
50
 
51
/* This is a superset of the TDA9875 */
52
struct tda9875 {
53
        int mode;
54
        int rvol, lvol;
55
        int bass, treble;
56
        struct i2c_client c;
57
};
58
 
59
static struct i2c_driver driver;
60
static struct i2c_client client_template;
61
 
62
#define dprintk  if (debug) printk
63
 
64
/* The TDA9875 is made by Philips Semiconductor
65
 * http://www.semiconductors.philips.com
66
 * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
67
 *
68
 */
69
 
70
                /* subaddresses for TDA9875 */
71
#define TDA9875_MUT         0x12  /*General mute  (value --> 0b11001100*/
72
#define TDA9875_CFG         0x01  /* Config register (value --> 0b00000000 */
73
#define TDA9875_DACOS       0x13  /*DAC i/o select (ADC) 0b0000100*/
74
#define TDA9875_LOSR        0x16  /*Line output select regirter 0b0100 0001*/
75
 
76
#define TDA9875_CH1V        0x0c  /*Channel 1 volume (mute)*/
77
#define TDA9875_CH2V        0x0d  /*Channel 2 volume (mute)*/
78
#define TDA9875_SC1         0x14  /*SCART 1 in (mono)*/
79
#define TDA9875_SC2         0x15  /*SCART 2 in (mono)*/
80
 
81
#define TDA9875_ADCIS       0x17  /*ADC input select (mono) 0b0110 000*/
82
#define TDA9875_AER         0x19  /*Audio effect (AVL+Pseudo) 0b0000 0110*/
83
#define TDA9875_MCS         0x18  /*Main channel select (DAC) 0b0000100*/
84
#define TDA9875_MVL         0x1a  /* Main volume gauche */
85
#define TDA9875_MVR         0x1b  /* Main volume droite */
86
#define TDA9875_MBA         0x1d  /* Main Basse */
87
#define TDA9875_MTR         0x1e  /* Main treble */
88
#define TDA9875_ACS         0x1f  /* Auxilary channel select (FM) 0b0000000*/
89
#define TDA9875_AVL         0x20  /* Auxilary volume gauche */
90
#define TDA9875_AVR         0x21  /* Auxilary volume droite */
91
#define TDA9875_ABA         0x22  /* Auxilary Basse */
92
#define TDA9875_ATR         0x23  /* Auxilary treble */
93
 
94
#define TDA9875_MSR         0x02  /* Monitor select register */
95
#define TDA9875_C1MSB       0x03  /* Carrier 1 (FM) frequency register MSB */
96
#define TDA9875_C1MIB       0x04  /* Carrier 1 (FM) frequency register (16-8]b */
97
#define TDA9875_C1LSB       0x05  /* Carrier 1 (FM) frequency register LSB */
98
#define TDA9875_C2MSB       0x06  /* Carrier 2 (nicam) frequency register MSB */
99
#define TDA9875_C2MIB       0x07  /* Carrier 2 (nicam) frequency register (16-8]b */
100
#define TDA9875_C2LSB       0x08  /* Carrier 2 (nicam) frequency register LSB */
101
#define TDA9875_DCR         0x09  /* Demodulateur configuration regirter*/
102
#define TDA9875_DEEM        0x0a  /* FM de-emphasis regirter*/
103
#define TDA9875_FMAT        0x0b  /* FM Matrix regirter*/
104
 
105
/* values */
106
#define TDA9875_MUTE_ON     0xff /* general mute */
107
#define TDA9875_MUTE_OFF    0xcc /* general no mute */
108
 
109
 
110
 
111
/* Begin code */
112
 
113
static int tda9875_write(struct i2c_client *client, int subaddr, unsigned char val)
114
{
115
        unsigned char buffer[2];
116
        dprintk("In tda9875_write\n");
117
        dprintk("Writing %d 0x%x\n", subaddr, val);
118
        buffer[0] = subaddr;
119
        buffer[1] = val;
120
        if (2 != i2c_master_send(client,buffer,2)) {
121
                printk(KERN_WARNING "tda9875: I/O error, trying (write %d 0x%x)\n",
122
                       subaddr, val);
123
                return -1;
124
        }
125
        return 0;
126
}
127
 
128
#if 0
129
static int tda9875_read(struct i2c_client *client)
130
{
131
        unsigned char buffer;
132
        dprintk("In tda9875_read\n");
133
        if (1 != i2c_master_recv(client,&buffer,1)) {
134
                printk(KERN_WARNING "tda9875: I/O error, trying (read)\n");
135
                return -1;
136
        }
137
        dprintk("Read 0x%02x\n", buffer);
138
        return buffer;
139
}
140
#endif
141
 
142
static int i2c_read_register(struct i2c_adapter *adap, int addr, int reg)
143
{
144
        unsigned char write[1];
145
        unsigned char read[1];
146
        struct i2c_msg msgs[2] = {
147
                { addr, 0,        1, write },
148
                { addr, I2C_M_RD, 1, read  }
149
        };
150
        write[0] = reg;
151
 
152
        if (2 != i2c_transfer(adap,msgs,2)) {
153
                printk(KERN_WARNING "tda9875: I/O error (read2)\n");
154
                return -1;
155
        }
156
        dprintk("tda9875: chip_read2: reg%d=0x%x\n",reg,read[0]);
157
        return read[0];
158
}
159
 
160
static void tda9875_set(struct i2c_client *client)
161
{
162
        struct tda9875 *tda = i2c_get_clientdata(client);
163
        unsigned char a;
164
 
165
        dprintk(KERN_DEBUG "tda9875_set(%04x,%04x,%04x,%04x)\n",
166
                tda->lvol,tda->rvol,tda->bass,tda->treble);
167
 
168
 
169
        a = tda->lvol & 0xff;
170
        tda9875_write(client, TDA9875_MVL, a);
171
        a =tda->rvol & 0xff;
172
        tda9875_write(client, TDA9875_MVR, a);
173
        a =tda->bass & 0xff;
174
        tda9875_write(client, TDA9875_MBA, a);
175
        a =tda->treble  & 0xff;
176
        tda9875_write(client, TDA9875_MTR, a);
177
}
178
 
179
static void do_tda9875_init(struct i2c_client *client)
180
{
181
        struct tda9875 *t = i2c_get_clientdata(client);
182
        dprintk("In tda9875_init\n");
183
        tda9875_write(client, TDA9875_CFG, 0xd0 ); /*reg de config 0 (reset)*/
184
        tda9875_write(client, TDA9875_MSR, 0x03 );    /* Monitor 0b00000XXX*/
185
        tda9875_write(client, TDA9875_C1MSB, 0x00 );  /*Car1(FM) MSB XMHz*/
186
        tda9875_write(client, TDA9875_C1MIB, 0x00 );  /*Car1(FM) MIB XMHz*/
187
        tda9875_write(client, TDA9875_C1LSB, 0x00 );  /*Car1(FM) LSB XMHz*/
188
        tda9875_write(client, TDA9875_C2MSB, 0x00 );  /*Car2(NICAM) MSB XMHz*/
189
        tda9875_write(client, TDA9875_C2MIB, 0x00 );  /*Car2(NICAM) MIB XMHz*/
190
        tda9875_write(client, TDA9875_C2LSB, 0x00 );  /*Car2(NICAM) LSB XMHz*/
191
        tda9875_write(client, TDA9875_DCR, 0x00 );    /*Demod config 0x00*/
192
        tda9875_write(client, TDA9875_DEEM, 0x44 );   /*DE-Emph 0b0100 0100*/
193
        tda9875_write(client, TDA9875_FMAT, 0x00 );   /*FM Matrix reg 0x00*/
194
        tda9875_write(client, TDA9875_SC1, 0x00 );    /* SCART 1 (SC1)*/
195
        tda9875_write(client, TDA9875_SC2, 0x01 );    /* SCART 2 (sc2)*/
196
 
197
        tda9875_write(client, TDA9875_CH1V, 0x10 );  /* Channel volume 1 mute*/
198
        tda9875_write(client, TDA9875_CH2V, 0x10 );  /* Channel volume 2 mute */
199
        tda9875_write(client, TDA9875_DACOS, 0x02 ); /* sig DAC i/o(in:nicam)*/
200
        tda9875_write(client, TDA9875_ADCIS, 0x6f ); /* sig ADC input(in:mono)*/
201
        tda9875_write(client, TDA9875_LOSR, 0x00 );  /* line out (in:mono)*/
202
        tda9875_write(client, TDA9875_AER, 0x00 );   /*06 Effect (AVL+PSEUDO) */
203
        tda9875_write(client, TDA9875_MCS, 0x44 );   /* Main ch select (DAC) */
204
        tda9875_write(client, TDA9875_MVL, 0x03 );   /* Vol Main left 10dB */
205
        tda9875_write(client, TDA9875_MVR, 0x03 );   /* Vol Main right 10dB*/
206
        tda9875_write(client, TDA9875_MBA, 0x00 );   /* Main Bass Main 0dB*/
207
        tda9875_write(client, TDA9875_MTR, 0x00 );   /* Main Treble Main 0dB*/
208
        tda9875_write(client, TDA9875_ACS, 0x44 );   /* Aux chan select (dac)*/
209
        tda9875_write(client, TDA9875_AVL, 0x00 );   /* Vol Aux left 0dB*/
210
        tda9875_write(client, TDA9875_AVR, 0x00 );   /* Vol Aux right 0dB*/
211
        tda9875_write(client, TDA9875_ABA, 0x00 );   /* Aux Bass Main 0dB*/
212
        tda9875_write(client, TDA9875_ATR, 0x00 );   /* Aux Aigus Main 0dB*/
213
 
214
        tda9875_write(client, TDA9875_MUT, 0xcc );   /* General mute  */
215
 
216
        t->mode=AUDIO_UNMUTE;
217
        t->lvol=t->rvol =0;      /* 0dB */
218
        t->bass=0;                       /* 0dB */
219
        t->treble=0;             /* 0dB */
220
        tda9875_set(client);
221
 
222
}
223
 
224
 
225
/* *********************** *
226
 * i2c interface functions *
227
 * *********************** */
228
 
229
static int tda9875_checkit(struct i2c_adapter *adap, int addr)
230
{
231
        int dic,rev;
232
 
233
        dic=i2c_read_register(adap,addr,254);
234
        rev=i2c_read_register(adap,addr,255);
235
 
236
        if(dic==0 || dic==2) { // tda9875 and tda9875A
237
                printk("tda9875: TDA9875%s Rev.%d detected at 0x%x\n",
238
                dic==0?"":"A", rev,addr<<1);
239
                return 1;
240
        }
241
        printk("tda9875: no such chip at 0x%x (dic=0x%x rev=0x%x)\n",addr<<1,dic,rev);
242
        return(0);
243
}
244
 
245
static int tda9875_attach(struct i2c_adapter *adap, int addr,
246
                          unsigned short flags, int kind)
247
{
248
        struct tda9875 *t;
249
        struct i2c_client *client;
250
        dprintk("In tda9875_attach\n");
251
 
252
        t = kmalloc(sizeof *t,GFP_KERNEL);
253
        if (!t)
254
                return -ENOMEM;
255
        memset(t,0,sizeof *t);
256
 
257
        client = &t->c;
258
        memcpy(client,&client_template,sizeof(struct i2c_client));
259
        client->adapter = adap;
260
        client->addr = addr;
261
        i2c_set_clientdata(client, t);
262
 
263
        if(!tda9875_checkit(adap,addr)) {
264
                kfree(t);
265
                return 1;
266
        }
267
 
268
        do_tda9875_init(client);
269
        MOD_INC_USE_COUNT;
270
        printk(KERN_INFO "tda9875: init\n");
271
 
272
        i2c_attach_client(client);
273
        return 0;
274
}
275
 
276
static int tda9875_probe(struct i2c_adapter *adap)
277
{
278
#ifdef I2C_ADAP_CLASS_TV_ANALOG
279
        if (adap->class & I2C_ADAP_CLASS_TV_ANALOG)
280
                return i2c_probe(adap, &addr_data, tda9875_attach);
281
#else
282
        if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
283
                return i2c_probe(adap, &addr_data, tda9875_attach);
284
#endif
285
        return 0;
286
}
287
 
288
static int tda9875_detach(struct i2c_client *client)
289
{
290
        struct tda9875 *t  = i2c_get_clientdata(client);
291
 
292
        do_tda9875_init(client);
293
        i2c_detach_client(client);
294
 
295
        kfree(t);
296
        MOD_DEC_USE_COUNT;
297
        return 0;
298
}
299
 
300
static int tda9875_command(struct i2c_client *client,
301
                                unsigned int cmd, void *arg)
302
{
303
        struct tda9875 *t = i2c_get_clientdata(client);
304
 
305
        dprintk("In tda9875_command...\n");
306
 
307
        switch (cmd) {
308
        /* --- v4l ioctls --- */
309
        /* take care: bttv does userspace copying, we'll get a
310
           kernel pointer here... */
311
        case VIDIOCGAUDIO:
312
        {
313
                struct video_audio *va = arg;
314
                int left,right;
315
 
316
                dprintk("VIDIOCGAUDIO\n");
317
 
318
                va->flags |= VIDEO_AUDIO_VOLUME |
319
                        VIDEO_AUDIO_BASS |
320
                        VIDEO_AUDIO_TREBLE;
321
 
322
                /* min is -84 max is 24 */
323
                left = (t->lvol+84)*606;
324
                right = (t->rvol+84)*606;
325
                va->volume=max(left,right);
326
                va->balance=(32768*min(left,right))/
327
                        (va->volume ? va->volume : 1);
328
                va->balance=(left<right)?
329
                        (65535-va->balance) : va->balance;
330
                va->bass = (t->bass+12)*2427;    /* min -12 max +15 */
331
                va->treble = (t->treble+12)*2730;/* min -12 max +12 */
332
                va->mode |= VIDEO_SOUND_MONO;
333
 
334
                break; /* VIDIOCGAUDIO case */
335
        }
336
 
337
        case VIDIOCSAUDIO:
338
        {
339
                struct video_audio *va = arg;
340
                int left,right;
341
 
342
                dprintk("VIDEOCSAUDIO...\n");
343
                left = (min(65536 - va->balance,32768) *
344
                        va->volume) / 32768;
345
                right = (min(va->balance,(__u16)32768) *
346
                         va->volume) / 32768;
347
                t->lvol = ((left/606)-84) & 0xff;
348
                if (t->lvol > 24)
349
                 t->lvol = 24;
350
                if (t->lvol < -84)
351
                 t->lvol = -84 & 0xff;
352
 
353
                t->rvol = ((right/606)-84) & 0xff;
354
                if (t->rvol > 24)
355
                 t->rvol = 24;
356
                if (t->rvol < -84)
357
                 t->rvol = -84 & 0xff;
358
 
359
                t->bass = ((va->bass/2400)-12) & 0xff;
360
                if (t->bass > 15)
361
                 t->bass = 15;
362
                if (t->bass < -12)
363
                 t->bass = -12 & 0xff;
364
 
365
                t->treble = ((va->treble/2700)-12) & 0xff;
366
                if (t->treble > 12)
367
                 t->treble = 12;
368
                if (t->treble < -12)
369
                 t->treble = -12 & 0xff;
370
 
371
 
372
 
373
//printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble);
374
 
375
 
376
                tda9875_set(client);
377
 
378
                break;
379
 
380
        } /* end of VIDEOCSAUDIO case */
381
 
382
        default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
383
 
384
                /* nothing */
385
                dprintk("Default\n");
386
 
387
        } /* end of (cmd) switch */
388
 
389
        return 0;
390
}
391
 
392
 
393
static struct i2c_driver driver = {
394
        .name           = "i2c tda9875 driver",
395
        .id             = I2C_DRIVERID_TDA9875,
396
        .flags          = I2C_DF_NOTIFY,
397
        .attach_adapter = tda9875_probe,
398
        .detach_client  = tda9875_detach,
399
        .command        = tda9875_command,
400
};
401
 
402
static struct i2c_client client_template =
403
{
404
        I2C_DEVNAME("tda9875"),
405
        .id        = -1,
406
        .driver    = &driver,
407
};
408
 
409
static int tda9875_init(void)
410
{
411
        i2c_add_driver(&driver);
412
        return 0;
413
}
414
 
415
static void tda9875_fini(void)
416
{
417
        i2c_del_driver(&driver);
418
}
419
 
420
module_init(tda9875_init);
421
module_exit(tda9875_fini);
422
 
423
/*
424
 * Local variables:
425
 * c-basic-offset: 8
426
 * End:
427
 */
428
 

powered by: WebSVN 2.1.0

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