blob: 7a8ce8fb46dc88b4b13116a0875b98a28484af16 [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
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -030010 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This code is placed under the terms of the GNU General Public License
12 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
13 * Which was based on tda8425.c by Greg Alexander (c) 1998
14 *
15 * OPTIONS:
16 * debug - set to 1 if you'd like to see debug messages
17 *
18 * Revision: 0.1 - original version
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030029#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030033#include <media/i2c-addr.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int debug; /* insmod parameter */
36module_param(debug, int, S_IRUGO | S_IWUSR);
37MODULE_LICENSE("GPL");
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* Addresses to scan */
40static unsigned short normal_i2c[] = {
Mauro Carvalho Chehab09df1c12006-03-18 08:31:34 -030041 I2C_ADDR_TDA9875 >> 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 I2C_CLIENT_END
43};
Hans Verkuilf87086e2008-07-18 00:50:58 -030044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045I2C_CLIENT_INSMOD;
46
47/* This is a superset of the TDA9875 */
48struct tda9875 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int rvol, lvol;
50 int bass, treble;
51 struct i2c_client c;
52};
53
54static struct i2c_driver driver;
55static struct i2c_client client_template;
56
57#define dprintk if (debug) printk
58
59/* The TDA9875 is made by Philips Semiconductor
60 * http://www.semiconductors.philips.com
61 * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
62 *
63 */
64
65 /* subaddresses for TDA9875 */
66#define TDA9875_MUT 0x12 /*General mute (value --> 0b11001100*/
67#define TDA9875_CFG 0x01 /* Config register (value --> 0b00000000 */
68#define TDA9875_DACOS 0x13 /*DAC i/o select (ADC) 0b0000100*/
69#define TDA9875_LOSR 0x16 /*Line output select regirter 0b0100 0001*/
70
71#define TDA9875_CH1V 0x0c /*Channel 1 volume (mute)*/
72#define TDA9875_CH2V 0x0d /*Channel 2 volume (mute)*/
73#define TDA9875_SC1 0x14 /*SCART 1 in (mono)*/
74#define TDA9875_SC2 0x15 /*SCART 2 in (mono)*/
75
76#define TDA9875_ADCIS 0x17 /*ADC input select (mono) 0b0110 000*/
77#define TDA9875_AER 0x19 /*Audio effect (AVL+Pseudo) 0b0000 0110*/
78#define TDA9875_MCS 0x18 /*Main channel select (DAC) 0b0000100*/
79#define TDA9875_MVL 0x1a /* Main volume gauche */
80#define TDA9875_MVR 0x1b /* Main volume droite */
81#define TDA9875_MBA 0x1d /* Main Basse */
82#define TDA9875_MTR 0x1e /* Main treble */
83#define TDA9875_ACS 0x1f /* Auxilary channel select (FM) 0b0000000*/
84#define TDA9875_AVL 0x20 /* Auxilary volume gauche */
85#define TDA9875_AVR 0x21 /* Auxilary volume droite */
86#define TDA9875_ABA 0x22 /* Auxilary Basse */
87#define TDA9875_ATR 0x23 /* Auxilary treble */
88
89#define TDA9875_MSR 0x02 /* Monitor select register */
90#define TDA9875_C1MSB 0x03 /* Carrier 1 (FM) frequency register MSB */
91#define TDA9875_C1MIB 0x04 /* Carrier 1 (FM) frequency register (16-8]b */
92#define TDA9875_C1LSB 0x05 /* Carrier 1 (FM) frequency register LSB */
93#define TDA9875_C2MSB 0x06 /* Carrier 2 (nicam) frequency register MSB */
94#define TDA9875_C2MIB 0x07 /* Carrier 2 (nicam) frequency register (16-8]b */
95#define TDA9875_C2LSB 0x08 /* Carrier 2 (nicam) frequency register LSB */
96#define TDA9875_DCR 0x09 /* Demodulateur configuration regirter*/
97#define TDA9875_DEEM 0x0a /* FM de-emphasis regirter*/
98#define TDA9875_FMAT 0x0b /* FM Matrix regirter*/
99
100/* values */
101#define TDA9875_MUTE_ON 0xff /* general mute */
102#define TDA9875_MUTE_OFF 0xcc /* general no mute */
103
104
105
106/* Begin code */
107
108static int tda9875_write(struct i2c_client *client, int subaddr, unsigned char val)
109{
110 unsigned char buffer[2];
111 dprintk("In tda9875_write\n");
112 dprintk("Writing %d 0x%x\n", subaddr, val);
113 buffer[0] = subaddr;
114 buffer[1] = val;
115 if (2 != i2c_master_send(client,buffer,2)) {
116 printk(KERN_WARNING "tda9875: I/O error, trying (write %d 0x%x)\n",
117 subaddr, val);
118 return -1;
119 }
120 return 0;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124static int i2c_read_register(struct i2c_adapter *adap, int addr, int reg)
125{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800126 unsigned char write[1];
127 unsigned char read[1];
128 struct i2c_msg msgs[2] = {
129 { addr, 0, 1, write },
130 { addr, I2C_M_RD, 1, read }
131 };
132 write[0] = reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800134 if (2 != i2c_transfer(adap,msgs,2)) {
135 printk(KERN_WARNING "tda9875: I/O error (read2)\n");
136 return -1;
137 }
138 dprintk("tda9875: chip_read2: reg%d=0x%x\n",reg,read[0]);
139 return read[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142static void tda9875_set(struct i2c_client *client)
143{
144 struct tda9875 *tda = i2c_get_clientdata(client);
145 unsigned char a;
146
147 dprintk(KERN_DEBUG "tda9875_set(%04x,%04x,%04x,%04x)\n",
148 tda->lvol,tda->rvol,tda->bass,tda->treble);
149
150
151 a = tda->lvol & 0xff;
152 tda9875_write(client, TDA9875_MVL, a);
153 a =tda->rvol & 0xff;
154 tda9875_write(client, TDA9875_MVR, a);
155 a =tda->bass & 0xff;
156 tda9875_write(client, TDA9875_MBA, a);
157 a =tda->treble & 0xff;
158 tda9875_write(client, TDA9875_MTR, a);
159}
160
161static void do_tda9875_init(struct i2c_client *client)
162{
163 struct tda9875 *t = i2c_get_clientdata(client);
164 dprintk("In tda9875_init\n");
165 tda9875_write(client, TDA9875_CFG, 0xd0 ); /*reg de config 0 (reset)*/
Trent Piepho657de3c2006-06-20 00:30:57 -0300166 tda9875_write(client, TDA9875_MSR, 0x03 ); /* Monitor 0b00000XXX*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tda9875_write(client, TDA9875_C1MSB, 0x00 ); /*Car1(FM) MSB XMHz*/
168 tda9875_write(client, TDA9875_C1MIB, 0x00 ); /*Car1(FM) MIB XMHz*/
169 tda9875_write(client, TDA9875_C1LSB, 0x00 ); /*Car1(FM) LSB XMHz*/
170 tda9875_write(client, TDA9875_C2MSB, 0x00 ); /*Car2(NICAM) MSB XMHz*/
171 tda9875_write(client, TDA9875_C2MIB, 0x00 ); /*Car2(NICAM) MIB XMHz*/
172 tda9875_write(client, TDA9875_C2LSB, 0x00 ); /*Car2(NICAM) LSB XMHz*/
173 tda9875_write(client, TDA9875_DCR, 0x00 ); /*Demod config 0x00*/
174 tda9875_write(client, TDA9875_DEEM, 0x44 ); /*DE-Emph 0b0100 0100*/
175 tda9875_write(client, TDA9875_FMAT, 0x00 ); /*FM Matrix reg 0x00*/
176 tda9875_write(client, TDA9875_SC1, 0x00 ); /* SCART 1 (SC1)*/
177 tda9875_write(client, TDA9875_SC2, 0x01 ); /* SCART 2 (sc2)*/
178
179 tda9875_write(client, TDA9875_CH1V, 0x10 ); /* Channel volume 1 mute*/
180 tda9875_write(client, TDA9875_CH2V, 0x10 ); /* Channel volume 2 mute */
181 tda9875_write(client, TDA9875_DACOS, 0x02 ); /* sig DAC i/o(in:nicam)*/
182 tda9875_write(client, TDA9875_ADCIS, 0x6f ); /* sig ADC input(in:mono)*/
183 tda9875_write(client, TDA9875_LOSR, 0x00 ); /* line out (in:mono)*/
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800184 tda9875_write(client, TDA9875_AER, 0x00 ); /*06 Effect (AVL+PSEUDO) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 tda9875_write(client, TDA9875_MCS, 0x44 ); /* Main ch select (DAC) */
186 tda9875_write(client, TDA9875_MVL, 0x03 ); /* Vol Main left 10dB */
187 tda9875_write(client, TDA9875_MVR, 0x03 ); /* Vol Main right 10dB*/
188 tda9875_write(client, TDA9875_MBA, 0x00 ); /* Main Bass Main 0dB*/
189 tda9875_write(client, TDA9875_MTR, 0x00 ); /* Main Treble Main 0dB*/
190 tda9875_write(client, TDA9875_ACS, 0x44 ); /* Aux chan select (dac)*/
191 tda9875_write(client, TDA9875_AVL, 0x00 ); /* Vol Aux left 0dB*/
192 tda9875_write(client, TDA9875_AVR, 0x00 ); /* Vol Aux right 0dB*/
193 tda9875_write(client, TDA9875_ABA, 0x00 ); /* Aux Bass Main 0dB*/
194 tda9875_write(client, TDA9875_ATR, 0x00 ); /* Aux Aigus Main 0dB*/
195
196 tda9875_write(client, TDA9875_MUT, 0xcc ); /* General mute */
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 t->lvol=t->rvol =0; /* 0dB */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800199 t->bass=0; /* 0dB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 t->treble=0; /* 0dB */
201 tda9875_set(client);
202
203}
204
205
206/* *********************** *
207 * i2c interface functions *
208 * *********************** */
209
210static int tda9875_checkit(struct i2c_adapter *adap, int addr)
211{
212 int dic,rev;
213
214 dic=i2c_read_register(adap,addr,254);
215 rev=i2c_read_register(adap,addr,255);
216
217 if(dic==0 || dic==2) { // tda9875 and tda9875A
218 printk("tda9875: TDA9875%s Rev.%d detected at 0x%x\n",
219 dic==0?"":"A", rev,addr<<1);
220 return 1;
221 }
222 printk("tda9875: no such chip at 0x%x (dic=0x%x rev=0x%x)\n",addr<<1,dic,rev);
223 return(0);
224}
225
226static int tda9875_attach(struct i2c_adapter *adap, int addr, int kind)
227{
228 struct tda9875 *t;
229 struct i2c_client *client;
230 dprintk("In tda9875_attach\n");
231
Panagiotis Issaris74081872006-01-11 19:40:56 -0200232 t = kzalloc(sizeof *t,GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!t)
234 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 client = &t->c;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800237 memcpy(client,&client_template,sizeof(struct i2c_client));
238 client->adapter = adap;
239 client->addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 i2c_set_clientdata(client, t);
241
242 if(!tda9875_checkit(adap,addr)) {
243 kfree(t);
244 return 1;
245 }
246
247 do_tda9875_init(client);
248 printk(KERN_INFO "tda9875: init\n");
249
250 i2c_attach_client(client);
251 return 0;
252}
253
254static int tda9875_probe(struct i2c_adapter *adap)
255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (adap->class & I2C_CLASS_TV_ANALOG)
257 return i2c_probe(adap, &addr_data, tda9875_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259}
260
261static int tda9875_detach(struct i2c_client *client)
262{
263 struct tda9875 *t = i2c_get_clientdata(client);
264
265 do_tda9875_init(client);
266 i2c_detach_client(client);
267
268 kfree(t);
269 return 0;
270}
271
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300272static int tda9875_get_ctrl(struct i2c_client *client,
273 struct v4l2_control *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct tda9875 *t = i2c_get_clientdata(client);
276
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300277 switch (ctrl->id) {
278 case V4L2_CID_AUDIO_VOLUME:
279 {
280 int left = (t->lvol+84)*606;
281 int right = (t->rvol+84)*606;
282
283 ctrl->value=max(left,right);
284 return 0;
285 }
286 case V4L2_CID_AUDIO_BALANCE:
287 {
288 int left = (t->lvol+84)*606;
289 int right = (t->rvol+84)*606;
290 int volume = max(left,right);
291 int balance = (32768*min(left,right))/
292 (volume ? volume : 1);
293 ctrl->value=(left<right)?
294 (65535-balance) : balance;
295 return 0;
296 }
297 case V4L2_CID_AUDIO_BASS:
298 ctrl->value = (t->bass+12)*2427; /* min -12 max +15 */
299 return 0;
300 case V4L2_CID_AUDIO_TREBLE:
301 ctrl->value = (t->treble+12)*2730;/* min -12 max +12 */
302 return 0;
303 }
304 return -EINVAL;
305}
306
307static int tda9875_set_ctrl(struct i2c_client *client,
308 struct v4l2_control *ctrl)
309{
310 struct tda9875 *t = i2c_get_clientdata(client);
311 int chvol=0, volume, balance, left, right;
312
313 switch (ctrl->id) {
314 case V4L2_CID_AUDIO_VOLUME:
315 left = (t->lvol+84)*606;
316 right = (t->rvol+84)*606;
317
318 volume = max(left,right);
319 balance = (32768*min(left,right))/
320 (volume ? volume : 1);
321 balance =(left<right)?
322 (65535-balance) : balance;
323
324 volume = ctrl->value;
325
326 chvol=1;
327 break;
328 case V4L2_CID_AUDIO_BALANCE:
329 left = (t->lvol+84)*606;
330 right = (t->rvol+84)*606;
331
332 volume=max(left,right);
333
334 balance = ctrl->value;
335
336 chvol=1;
337 break;
338 case V4L2_CID_AUDIO_BASS:
339 t->bass = ((ctrl->value/2400)-12) & 0xff;
340 if (t->bass > 15)
341 t->bass = 15;
342 if (t->bass < -12)
343 t->bass = -12 & 0xff;
344 break;
345 case V4L2_CID_AUDIO_TREBLE:
346 t->treble = ((ctrl->value/2700)-12) & 0xff;
347 if (t->treble > 12)
348 t->treble = 12;
349 if (t->treble < -12)
350 t->treble = -12 & 0xff;
351 break;
352 default:
353 return -EINVAL;
354 }
355
356 if (chvol) {
357 left = (min(65536 - balance,32768) *
358 volume) / 32768;
359 right = (min(balance,32768) *
360 volume) / 32768;
361 t->lvol = ((left/606)-84) & 0xff;
362 if (t->lvol > 24)
363 t->lvol = 24;
364 if (t->lvol < -84)
365 t->lvol = -84 & 0xff;
366
367 t->rvol = ((right/606)-84) & 0xff;
368 if (t->rvol > 24)
369 t->rvol = 24;
370 if (t->rvol < -84)
371 t->rvol = -84 & 0xff;
372 }
373
374//printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble);
375
376 tda9875_set(client);
377
378 return 0;
379}
380
381
382static int tda9875_command(struct i2c_client *client,
383 unsigned int cmd, void *arg)
384{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dprintk("In tda9875_command...\n");
386
387 switch (cmd) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800388 /* --- v4l ioctls --- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* take care: bttv does userspace copying, we'll get a
390 kernel pointer here... */
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300391 case VIDIOC_QUERYCTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 {
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300393 struct v4l2_queryctrl *qc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300395 switch (qc->id) {
396 case V4L2_CID_AUDIO_VOLUME:
397 case V4L2_CID_AUDIO_BASS:
398 case V4L2_CID_AUDIO_TREBLE:
399 default:
400 return -EINVAL;
401 }
402 return v4l2_ctrl_query_fill_std(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300404 case VIDIOC_S_CTRL:
405 return tda9875_set_ctrl(client, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300407 case VIDIOC_G_CTRL:
408 return tda9875_get_ctrl(client, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
411
412 /* nothing */
413 dprintk("Default\n");
414
415 } /* end of (cmd) switch */
416
417 return 0;
418}
419
420
421static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100422 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200423 .name = "tda9875",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100424 },
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800425 .id = I2C_DRIVERID_TDA9875,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .attach_adapter = tda9875_probe,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800427 .detach_client = tda9875_detach,
428 .command = tda9875_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429};
430
431static struct i2c_client client_template =
432{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800433 .name = "tda9875",
434 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435};
436
437static int __init tda9875_init(void)
438{
439 return i2c_add_driver(&driver);
440}
441
442static void __exit tda9875_fini(void)
443{
444 i2c_del_driver(&driver);
445}
446
447module_init(tda9875_init);
448module_exit(tda9875_fini);
449
450/*
451 * Local variables:
452 * c-basic-offset: 8
453 * End:
454 */
455