File: /usr/src/linux/drivers/media/video/tda9875.c

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