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