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