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