Original kernel 2.4.37.5
[tomato.git] / release / src / linux / linux / drivers / media / video / tda9875.c
blob57a30ea4cef0c7cee07de5544684a46624b6f08d
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.
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
14 * OPTIONS:
15 * debug - set to 1 if you'd like to see debug messages
17 * Revision: 0.1 - original version
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>
33 #include "bttv.h"
34 #include "audiochip.h"
35 #include "id.h"
36 #include "i2c-compat.h"
38 MODULE_PARM(debug,"i");
39 MODULE_LICENSE("GPL");
41 static int debug = 0; /* insmod parameter */
43 /* Addresses to scan */
44 static unsigned short normal_i2c[] = {
45 I2C_TDA9875 >> 1,
46 I2C_CLIENT_END
48 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
49 I2C_CLIENT_INSMOD;
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;
59 static struct i2c_driver driver;
60 static struct i2c_client client_template;
62 #define dprintk if (debug) printk
64 /* The TDA9875 is made by Philips Semiconductor
65 * http://www.semiconductors.philips.com
66 * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
68 */
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*/
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)*/
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 */
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*/
105 /* values */
106 #define TDA9875_MUTE_ON 0xff /* general mute */
107 #define TDA9875_MUTE_OFF 0xcc /* general no mute */
111 /* Begin code */
113 static int tda9875_write(struct i2c_client *client, int subaddr, unsigned char val)
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;
125 return 0;
128 #if 0
129 static int tda9875_read(struct i2c_client *client)
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;
137 dprintk("Read 0x%02x\n", buffer);
138 return buffer;
140 #endif
142 static int i2c_read_register(struct i2c_adapter *adap, int addr, int reg)
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 }
150 write[0] = reg;
152 if (2 != i2c_transfer(adap,msgs,2)) {
153 printk(KERN_WARNING "tda9875: I/O error (read2)\n");
154 return -1;
156 dprintk("tda9875: chip_read2: reg%d=0x%x\n",reg,read[0]);
157 return read[0];
160 static void tda9875_set(struct i2c_client *client)
162 struct tda9875 *tda = i2c_get_clientdata(client);
163 unsigned char a;
165 dprintk(KERN_DEBUG "tda9875_set(%04x,%04x,%04x,%04x)\n",
166 tda->lvol,tda->rvol,tda->bass,tda->treble);
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);
179 static void do_tda9875_init(struct i2c_client *client)
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)*/
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*/
214 tda9875_write(client, TDA9875_MUT, 0xcc ); /* General mute */
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);
225 /* *********************** *
226 * i2c interface functions *
227 * *********************** */
229 static int tda9875_checkit(struct i2c_adapter *adap, int addr)
231 int dic,rev;
233 dic=i2c_read_register(adap,addr,254);
234 rev=i2c_read_register(adap,addr,255);
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;
241 printk("tda9875: no such chip at 0x%x (dic=0x%x rev=0x%x)\n",addr<<1,dic,rev);
242 return(0);
245 static int tda9875_attach(struct i2c_adapter *adap, int addr,
246 unsigned short flags, int kind)
248 struct tda9875 *t;
249 struct i2c_client *client;
250 dprintk("In tda9875_attach\n");
252 t = kmalloc(sizeof *t,GFP_KERNEL);
253 if (!t)
254 return -ENOMEM;
255 memset(t,0,sizeof *t);
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);
263 if(!tda9875_checkit(adap,addr)) {
264 kfree(t);
265 return 1;
268 do_tda9875_init(client);
269 MOD_INC_USE_COUNT;
270 printk(KERN_INFO "tda9875: init\n");
272 i2c_attach_client(client);
273 return 0;
276 static int tda9875_probe(struct i2c_adapter *adap)
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;
288 static int tda9875_detach(struct i2c_client *client)
290 struct tda9875 *t = i2c_get_clientdata(client);
292 do_tda9875_init(client);
293 i2c_detach_client(client);
295 kfree(t);
296 MOD_DEC_USE_COUNT;
297 return 0;
300 static int tda9875_command(struct i2c_client *client,
301 unsigned int cmd, void *arg)
303 struct tda9875 *t = i2c_get_clientdata(client);
305 dprintk("In tda9875_command...\n");
307 switch (cmd) {
308 /* --- v4l ioctls --- */
309 /* take care: bttv does userspace copying, we'll get a
310 kernel pointer here... */
311 case VIDIOCGAUDIO:
313 struct video_audio *va = arg;
314 int left,right;
316 dprintk("VIDIOCGAUDIO\n");
318 va->flags |= VIDEO_AUDIO_VOLUME |
319 VIDEO_AUDIO_BASS |
320 VIDEO_AUDIO_TREBLE;
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;
334 break; /* VIDIOCGAUDIO case */
337 case VIDIOCSAUDIO:
339 struct video_audio *va = arg;
340 int left,right;
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;
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;
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;
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;
373 //printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble);
376 tda9875_set(client);
378 break;
380 } /* end of VIDEOCSAUDIO case */
382 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
384 /* nothing */
385 dprintk("Default\n");
387 } /* end of (cmd) switch */
389 return 0;
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,
402 static struct i2c_client client_template =
404 I2C_DEVNAME("tda9875"),
405 .id = -1,
406 .driver = &driver,
409 static int tda9875_init(void)
411 i2c_add_driver(&driver);
412 return 0;
415 static void tda9875_fini(void)
417 i2c_del_driver(&driver);
420 module_init(tda9875_init);
421 module_exit(tda9875_fini);
424 * Local variables:
425 * c-basic-offset: 8
426 * End: