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