File: /usr/src/linux/drivers/media/video/tvaudio.c
1 /*
2 * experimental driver for simple i2c audio chips.
3 *
4 * Copyright (c) 2000 Gerd Knorr
5 * based on code by:
6 * Eric Sandeen (eric_sandeen@bigfoot.com)
7 * Steve VanDeBogart (vandebo@uclink.berkeley.edu)
8 * Greg Alexander (galexand@acm.org)
9 *
10 * This code is placed under the terms of the GNU General Public License
11 *
12 * OPTIONS:
13 * debug - set to 1 if you'd like to see debug messages
14 *
15 */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/timer.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/videodev.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c-algo-bit.h>
29 #include <linux/init.h>
30 #include <linux/smp_lock.h>
31
32 #include "audiochip.h"
33 #include "tvaudio.h"
34 #include "id.h"
35
36
37 /* ---------------------------------------------------------------------- */
38 /* insmod args */
39
40 MODULE_PARM(debug,"i");
41 static int debug = 0; /* insmod parameter */
42
43 #define dprintk if (debug) printk
44
45
46 /* ---------------------------------------------------------------------- */
47 /* our structs */
48
49 #define MAXREGS 64
50
51 struct CHIPSTATE;
52 typedef int (*getvalue)(int);
53 typedef int (*checkit)(struct CHIPSTATE*);
54 typedef int (*getmode)(struct CHIPSTATE*);
55 typedef void (*setmode)(struct CHIPSTATE*, int mode);
56 typedef void (*checkmode)(struct CHIPSTATE*);
57
58 /* i2c command */
59 typedef struct AUDIOCMD {
60 int count; /* # of bytes to send */
61 unsigned char bytes[MAXREGS+1]; /* addr, data, data, ... */
62 } audiocmd;
63
64 /* chip description */
65 struct CHIPDESC {
66 char *name; /* chip name */
67 int id; /* ID */
68 int addr_lo, addr_hi; /* i2c address range */
69 int registers; /* # of registers */
70
71 int *insmodopt;
72 checkit checkit;
73 int flags;
74 #define CHIP_HAS_VOLUME 1
75 #define CHIP_HAS_BASSTREBLE 2
76 #define CHIP_HAS_INPUTSEL 4
77
78 /* various i2c command sequences */
79 audiocmd init;
80
81 /* which register has which value */
82 int leftreg,rightreg,treblereg,bassreg;
83
84 /* initialize with (defaults to 65535/65535/32768/32768 */
85 int leftinit,rightinit,trebleinit,bassinit;
86
87 /* functions to convert the values (v4l -> chip) */
88 getvalue volfunc,treblefunc,bassfunc;
89
90 /* get/set mode */
91 getmode getmode;
92 setmode setmode;
93
94 /* check / autoswitch audio after channel switches */
95 checkmode checkmode;
96
97 /* input switch register + values for v4l inputs */
98 int inputreg;
99 int inputmap[8];
100 int inputmute;
101 int inputmask;
102 };
103 static struct CHIPDESC chiplist[];
104
105 /* current state of the chip */
106 struct CHIPSTATE {
107 struct i2c_client c;
108
109 /* index into CHIPDESC array */
110 int type;
111
112 /* shadow register set */
113 audiocmd shadow;
114
115 /* current settings */
116 __u16 left,right,treble,bass,mode;
117 int prevmode;
118 /* thread */
119 struct task_struct *thread;
120 struct semaphore *notify;
121 wait_queue_head_t wq;
122 struct timer_list wt;
123 int done;
124 };
125
126
127 /* ---------------------------------------------------------------------- */
128 /* i2c addresses */
129
130 static unsigned short normal_i2c[] = {
131 I2C_TDA8425 >> 1,
132 I2C_TEA6300 >> 1,
133 I2C_TEA6420 >> 1,
134 I2C_TDA9840 >> 1,
135 I2C_TDA985x_L >> 1,
136 I2C_TDA985x_H >> 1,
137 I2C_PIC16C54 >> 1,
138 I2C_CLIENT_END };
139 static unsigned short normal_i2c_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
140 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
141 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
142 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
143 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
144 static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
145 static struct i2c_client_address_data addr_data = {
146 normal_i2c, normal_i2c_range,
147 probe, probe_range,
148 ignore, ignore_range,
149 force
150 };
151
152 static struct i2c_driver driver;
153 static struct i2c_client client_template;
154
155
156 /* ---------------------------------------------------------------------- */
157 /* i2c I/O functions */
158
159 static int chip_write(struct CHIPSTATE *chip, int subaddr, int val)
160 {
161 unsigned char buffer[2];
162
163 if (-1 == subaddr) {
164 dprintk("%s: chip_write: 0x%x\n", chip->c.name, val);
165 chip->shadow.bytes[1] = val;
166 buffer[0] = val;
167 if (1 != i2c_master_send(&chip->c,buffer,1)) {
168 printk(KERN_WARNING "%s: I/O error (write 0x%x)\n",
169 chip->c.name, val);
170 return -1;
171 }
172 } else {
173 dprintk("%s: chip_write: reg%d=0x%x\n", chip->c.name, subaddr, val);
174 chip->shadow.bytes[subaddr+1] = val;
175 buffer[0] = subaddr;
176 buffer[1] = val;
177 if (2 != i2c_master_send(&chip->c,buffer,2)) {
178 printk(KERN_WARNING "%s: I/O error (write reg%d=0x%x)\n",
179 chip->c.name, subaddr, val);
180 return -1;
181 }
182 }
183 return 0;
184 }
185
186 static int chip_write_masked(struct CHIPSTATE *chip, int subaddr, int val, int mask)
187 {
188 if (mask != 0) {
189 if (-1 == subaddr) {
190 val = (chip->shadow.bytes[1] & ~mask) | (val & mask);
191 } else {
192 val = (chip->shadow.bytes[subaddr+1] & ~mask) | (val & mask);
193 }
194 }
195 return chip_write(chip, subaddr, val);
196 }
197
198 static int chip_read(struct CHIPSTATE *chip)
199 {
200 unsigned char buffer;
201
202 if (1 != i2c_master_recv(&chip->c,&buffer,1)) {
203 printk(KERN_WARNING "%s: I/O error (read)\n",
204 chip->c.name);
205 return -1;
206 }
207 dprintk("%s: chip_read: 0x%x\n",chip->c.name,buffer);
208 return buffer;
209 }
210
211 static int chip_read2(struct CHIPSTATE *chip, int subaddr)
212 {
213 unsigned char write[1];
214 unsigned char read[1];
215 struct i2c_msg msgs[2] = {
216 { chip->c.addr, 0, 1, write },
217 { chip->c.addr, I2C_M_RD, 1, read }
218 };
219 write[1] = subaddr;
220
221 if (2 != i2c_transfer(chip->c.adapter,msgs,2)) {
222 printk(KERN_WARNING "%s: I/O error (read2)\n",
223 chip->c.name);
224 return -1;
225 }
226 dprintk("%s: chip_read2: reg%d=0x%x\n",
227 chip->c.name,subaddr,read[0]);
228 return read[0];
229 }
230
231 static int chip_cmd(struct CHIPSTATE *chip, char *name, audiocmd *cmd)
232 {
233 int i;
234
235 if (0 == cmd->count)
236 return 0;
237
238 /* update our shadow register set; print bytes if (debug > 0) */
239 dprintk("%s: chip_cmd(%s): reg=%d, data:",
240 chip->c.name,name,cmd->bytes[0]);
241 for (i = 1; i < cmd->count; i++) {
242 dprintk(" 0x%x",cmd->bytes[i]);
243 chip->shadow.bytes[i+cmd->bytes[0]] = cmd->bytes[i];
244 }
245 dprintk("\n");
246
247 /* send data to the chip */
248 if (cmd->count != i2c_master_send(&chip->c,cmd->bytes,cmd->count)) {
249 printk(KERN_WARNING "%s: I/O error (%s)\n", chip->c.name, name);
250 return -1;
251 }
252 return 0;
253 }
254
255 /* ---------------------------------------------------------------------- */
256 /* kernel thread for doing i2c stuff asyncronly
257 * right now it is used only to check the audio mode (mono/stereo/whatever)
258 * some time after switching to another TV channel, then turn on stereo
259 * if available, ...
260 */
261
262 static void chip_thread_wake(unsigned long data)
263 {
264 struct CHIPSTATE *chip = (struct CHIPSTATE*)data;
265 wake_up_interruptible(&chip->wq);
266 }
267
268 static int chip_thread(void *data)
269 {
270 struct CHIPSTATE *chip = data;
271 struct CHIPDESC *desc = chiplist + chip->type;
272
273 #ifdef CONFIG_SMP
274 lock_kernel();
275 #endif
276 daemonize();
277 sigfillset(¤t->blocked);
278 strcpy(current->comm,chip->c.name);
279 chip->thread = current;
280 #ifdef CONFIG_SMP
281 unlock_kernel();
282 #endif
283
284 dprintk("%s: thread started\n", chip->c.name);
285 if(chip->notify != NULL)
286 up(chip->notify);
287
288 for (;;) {
289 interruptible_sleep_on(&chip->wq);
290 dprintk("%s: thread wakeup\n", chip->c.name);
291 if (chip->done || signal_pending(current))
292 break;
293
294 if (0 != chip->mode)
295 /* don't do anything if mode != auto */
296 continue;
297
298 /* have a look what's going on */
299 desc->checkmode(chip);
300
301 /* schedule next check */
302 mod_timer(&chip->wt, jiffies+2*HZ);
303 }
304
305 chip->thread = NULL;
306 dprintk("%s: thread exiting\n", chip->c.name);
307 if(chip->notify != NULL)
308 up(chip->notify);
309
310 return 0;
311 }
312
313 void generic_checkmode(struct CHIPSTATE *chip)
314 {
315 struct CHIPDESC *desc = chiplist + chip->type;
316 int mode = desc->getmode(chip);
317
318 if (mode == chip->prevmode)
319 return;
320
321 dprintk("%s: thread checkmode\n", chip->c.name);
322 chip->prevmode = mode;
323
324 if (mode & VIDEO_SOUND_LANG1)
325 desc->setmode(chip,VIDEO_SOUND_LANG1);
326 else if (mode & VIDEO_SOUND_STEREO)
327 desc->setmode(chip,VIDEO_SOUND_STEREO);
328 else
329 desc->setmode(chip,VIDEO_SOUND_MONO);
330 }
331
332 /* ---------------------------------------------------------------------- */
333 /* audio chip descriptions - defines+functions for tda9840 */
334
335 #define TDA9840_SW 0x00
336 #define TDA9840_LVADJ 0x02
337 #define TDA9840_STADJ 0x03
338 #define TDA9840_TEST 0x04
339
340 #define TDA9840_MONO 0x10
341 #define TDA9840_STEREO 0x2a
342 #define TDA9840_DUALA 0x12
343 #define TDA9840_DUALB 0x1e
344 #define TDA9840_DUALAB 0x1a
345 #define TDA9840_DUALBA 0x16
346 #define TDA9840_EXTERNAL 0x7a
347
348 #define TDA9840_DS_DUAL 0x20 /* Dual sound identified */
349 #define TDA9840_ST_STEREO 0x40 /* Stereo sound identified */
350 #define TDA9840_PONRES 0x80 /* Power-on reset detected if = 1 */
351
352 #define TDA9840_TEST_INT1SN 0x1 /* Integration time 0.5s when set */
353 #define TDA9840_TEST_INTFU 0x02 /* Disables integrator function */
354
355 int tda9840_getmode(struct CHIPSTATE *chip)
356 {
357 int val, mode;
358
359 val = chip_read(chip);
360 mode = VIDEO_SOUND_MONO;
361 if (val & TDA9840_DS_DUAL)
362 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
363 if (val & TDA9840_ST_STEREO)
364 mode |= VIDEO_SOUND_STEREO;
365
366 dprintk ("tda9840_getmode(): raw chip read: %d, return: %d\n",
367 val, mode);
368 return mode;
369 }
370
371 void tda9840_setmode(struct CHIPSTATE *chip, int mode)
372 {
373 int update = 1;
374 int t = chip->shadow.bytes[TDA9840_SW + 1] & ~0x7e;
375
376 switch (mode) {
377 case VIDEO_SOUND_MONO:
378 t |= TDA9840_MONO;
379 break;
380 case VIDEO_SOUND_STEREO:
381 t |= TDA9840_STEREO;
382 break;
383 case VIDEO_SOUND_LANG1:
384 t |= TDA9840_DUALA;
385 break;
386 case VIDEO_SOUND_LANG2:
387 t |= TDA9840_DUALB;
388 break;
389 default:
390 update = 0;
391 }
392
393 if (update)
394 chip_write(chip, TDA9840_SW, t);
395 }
396
397 /* ---------------------------------------------------------------------- */
398 /* audio chip descriptions - defines+functions for tda985x */
399
400 /* subaddresses for TDA9855 */
401 #define TDA9855_VR 0x00 /* Volume, right */
402 #define TDA9855_VL 0x01 /* Volume, left */
403 #define TDA9855_BA 0x02 /* Bass */
404 #define TDA9855_TR 0x03 /* Treble */
405 #define TDA9855_SW 0x04 /* Subwoofer - not connected on DTV2000 */
406
407 /* subaddresses for TDA9850 */
408 #define TDA9850_C4 0x04 /* Control 1 for TDA9850 */
409
410 /* subaddesses for both chips */
411 #define TDA985x_C5 0x05 /* Control 2 for TDA9850, Control 1 for TDA9855 */
412 #define TDA985x_C6 0x06 /* Control 3 for TDA9850, Control 2 for TDA9855 */
413 #define TDA985x_C7 0x07 /* Control 4 for TDA9850, Control 3 for TDA9855 */
414 #define TDA985x_A1 0x08 /* Alignment 1 for both chips */
415 #define TDA985x_A2 0x09 /* Alignment 2 for both chips */
416 #define TDA985x_A3 0x0a /* Alignment 3 for both chips */
417
418 /* Masks for bits in TDA9855 subaddresses */
419 /* 0x00 - VR in TDA9855 */
420 /* 0x01 - VL in TDA9855 */
421 /* lower 7 bits control gain from -71dB (0x28) to 16dB (0x7f)
422 * in 1dB steps - mute is 0x27 */
423
424
425 /* 0x02 - BA in TDA9855 */
426 /* lower 5 bits control bass gain from -12dB (0x06) to 16.5dB (0x19)
427 * in .5dB steps - 0 is 0x0E */
428
429
430 /* 0x03 - TR in TDA9855 */
431 /* 4 bits << 1 control treble gain from -12dB (0x3) to 12dB (0xb)
432 * in 3dB steps - 0 is 0x7 */
433
434 /* Masks for bits in both chips' subaddresses */
435 /* 0x04 - SW in TDA9855, C4/Control 1 in TDA9850 */
436 /* Unique to TDA9855: */
437 /* 4 bits << 2 control subwoofer/surround gain from -14db (0x1) to 14db (0xf)
438 * in 3dB steps - mute is 0x0 */
439
440 /* Unique to TDA9850: */
441 /* lower 4 bits control stereo noise threshold, over which stereo turns off
442 * set to values of 0x00 through 0x0f for Ster1 through Ster16 */
443
444
445 /* 0x05 - C5 - Control 1 in TDA9855 , Control 2 in TDA9850*/
446 /* Unique to TDA9855: */
447 #define TDA9855_MUTE 1<<7 /* GMU, Mute at outputs */
448 #define TDA9855_AVL 1<<6 /* AVL, Automatic Volume Level */
449 #define TDA9855_LOUD 1<<5 /* Loudness, 1==off */
450 #define TDA9855_SUR 1<<3 /* Surround / Subwoofer 1==.5(L-R) 0==.5(L+R) */
451 /* Bits 0 to 3 select various combinations
452 * of line in and line out, only the
453 * interesting ones are defined */
454 #define TDA9855_EXT 1<<2 /* Selects inputs LIR and LIL. Pins 41 & 12 */
455 #define TDA9855_INT 0 /* Selects inputs LOR and LOL. (internal) */
456
457 /* Unique to TDA9850: */
458 /* lower 4 bits contol SAP noise threshold, over which SAP turns off
459 * set to values of 0x00 through 0x0f for SAP1 through SAP16 */
460
461
462 /* 0x06 - C6 - Control 2 in TDA9855, Control 3 in TDA9850 */
463 /* Common to TDA9855 and TDA9850: */
464 #define TDA985x_SAP 3<<6 /* Selects SAP output, mute if not received */
465 #define TDA985x_STEREO 1<<6 /* Selects Stereo ouput, mono if not received */
466 #define TDA985x_MONO 0 /* Forces Mono output */
467 #define TDA985x_LMU 1<<3 /* Mute (LOR/LOL for 9855, OUTL/OUTR for 9850) */
468
469 /* Unique to TDA9855: */
470 #define TDA9855_TZCM 1<<5 /* If set, don't mute till zero crossing */
471 #define TDA9855_VZCM 1<<4 /* If set, don't change volume till zero crossing*/
472 #define TDA9855_LINEAR 0 /* Linear Stereo */
473 #define TDA9855_PSEUDO 1 /* Pseudo Stereo */
474 #define TDA9855_SPAT_30 2 /* Spatial Stereo, 30% anti-phase crosstalk */
475 #define TDA9855_SPAT_50 3 /* Spatial Stereo, 52% anti-phase crosstalk */
476 #define TDA9855_E_MONO 7 /* Forced mono - mono select elseware, so useless*/
477
478 /* 0x07 - C7 - Control 3 in TDA9855, Control 4 in TDA9850 */
479 /* Common to both TDA9855 and TDA9850: */
480 /* lower 4 bits control input gain from -3.5dB (0x0) to 4dB (0xF)
481 * in .5dB steps - 0dB is 0x7 */
482
483 /* 0x08, 0x09 - A1 and A2 (read/write) */
484 /* Common to both TDA9855 and TDA9850: */
485 /* lower 5 bites are wideband and spectral expander alignment
486 * from 0x00 to 0x1f - nominal at 0x0f and 0x10 (read/write) */
487 #define TDA985x_STP 1<<5 /* Stereo Pilot/detect (read-only) */
488 #define TDA985x_SAPP 1<<6 /* SAP Pilot/detect (read-only) */
489 #define TDA985x_STS 1<<7 /* Stereo trigger 1= <35mV 0= <30mV (write-only)*/
490
491 /* 0x0a - A3 */
492 /* Common to both TDA9855 and TDA9850: */
493 /* lower 3 bits control timing current for alignment: -30% (0x0), -20% (0x1),
494 * -10% (0x2), nominal (0x3), +10% (0x6), +20% (0x5), +30% (0x4) */
495 #define TDA985x_ADJ 1<<7 /* Stereo adjust on/off (wideband and spectral */
496
497 int tda9855_volume(int val) { return val/0x2e8+0x27; }
498 int tda9855_bass(int val) { return val/0xccc+0x06; }
499 int tda9855_treble(int val) { return (val/0x1c71+0x3)<<1; }
500
501 int tda985x_getmode(struct CHIPSTATE *chip)
502 {
503 int mode;
504
505 mode = ((TDA985x_STP | TDA985x_SAPP) &
506 chip_read(chip)) >> 4;
507 /* Add mono mode regardless of SAP and stereo */
508 /* Allows forced mono */
509 return mode | VIDEO_SOUND_MONO;
510 }
511
512 void tda985x_setmode(struct CHIPSTATE *chip, int mode)
513 {
514 int update = 1;
515 int c6 = chip->shadow.bytes[TDA985x_C6+1] & 0x3f;
516
517 switch (mode) {
518 case VIDEO_SOUND_MONO:
519 c6 |= TDA985x_MONO;
520 break;
521 case VIDEO_SOUND_STEREO:
522 c6 |= TDA985x_STEREO;
523 break;
524 case VIDEO_SOUND_LANG1:
525 c6 |= TDA985x_SAP;
526 break;
527 default:
528 update = 0;
529 }
530 if (update)
531 chip_write(chip,TDA985x_C6,c6);
532 }
533
534
535 /* ---------------------------------------------------------------------- */
536 /* audio chip descriptions - defines+functions for tda9873h */
537
538 /* Subaddresses for TDA9873H */
539
540 #define TDA9873_SW 0x00 /* Switching */
541 #define TDA9873_AD 0x01 /* Adjust */
542 #define TDA9873_PT 0x02 /* Port */
543
544 /* Subaddress 0x00: Switching Data
545 * B7..B0:
546 *
547 * B1, B0: Input source selection
548 * 0, 0 internal
549 * 1, 0 external stereo
550 * 0, 1 external mono
551 */
552 #define TDA9873_INP_MASK 3
553 #define TDA9873_INTERNAL 0
554 #define TDA9873_EXT_STEREO 2
555 #define TDA9873_EXT_MONO 1
556
557 /* B3, B2: output signal select
558 * B4 : transmission mode
559 * 0, 0, 1 Mono
560 * 1, 0, 0 Stereo
561 * 1, 1, 1 Stereo (reversed channel)
562 * 0, 0, 0 Dual AB
563 * 0, 0, 1 Dual AA
564 * 0, 1, 0 Dual BB
565 * 0, 1, 1 Dual BA
566 */
567
568 #define TDA9873_TR_MASK (7 << 2)
569 #define TDA9873_TR_MONO 4
570 #define TDA9873_TR_STEREO 1 << 4
571 #define TDA9873_TR_REVERSE (1 << 3) & (1 << 2)
572 #define TDA9873_TR_DUALA 1 << 2
573 #define TDA9873_TR_DUALB 1 << 3
574
575 /* output level controls
576 * B5: output level switch (0 = reduced gain, 1 = normal gain)
577 * B6: mute (1 = muted)
578 * B7: auto-mute (1 = auto-mute enabled)
579 */
580
581 #define TDA9873_GAIN_NORMAL 1 << 5
582 #define TDA9873_MUTE 1 << 6
583 #define TDA9873_AUTOMUTE 1 << 7
584
585 /* Subaddress 0x01: Adjust/standard */
586
587 /* Lower 4 bits (C3..C0) control stereo adjustment on R channel (-0.6 - +0.7 dB)
588 * Recommended value is +0 dB
589 */
590
591 #define TDA9873_STEREO_ADJ 0x06 /* 0dB gain */
592
593 /* Bits C6..C4 control FM stantard
594 * C6, C5, C4
595 * 0, 0, 0 B/G (PAL FM)
596 * 0, 0, 1 M
597 * 0, 1, 0 D/K(1)
598 * 0, 1, 1 D/K(2)
599 * 1, 0, 0 D/K(3)
600 * 1, 0, 1 I
601 */
602 #define TDA9873_BG 0
603 #define TDA9873_M 1
604 #define TDA9873_DK1 2
605 #define TDA9873_DK2 3
606 #define TDA9873_DK3 4
607 #define TDA9873_I 5
608
609 /* C7 controls identification response time (1=fast/0=normal)
610 */
611 #define TDA9873_IDR_NORM 0
612 #define TDA9873_IDR_FAST 1 << 7
613
614
615 /* Subaddress 0x02: Port data */
616
617 /* E1, E0 free programmable ports P1/P2
618 0, 0 both ports low
619 0, 1 P1 high
620 1, 0 P2 high
621 1, 1 both ports high
622 */
623
624 #define TDA9873_PORTS 3
625
626 /* E2: test port */
627 #define TDA9873_TST_PORT 1 << 2
628
629 /* E5..E3 control mono output channel (together with transmission mode bit B4)
630 *
631 * E5 E4 E3 B4 OUTM
632 * 0 0 0 0 mono
633 * 0 0 1 0 DUAL B
634 * 0 1 0 1 mono (from stereo decoder)
635 */
636 #define TDA9873_MOUT_MONO 0
637 #define TDA9873_MOUT_FMONO 0
638 #define TDA9873_MOUT_DUALA 0
639 #define TDA9873_MOUT_DUALB 1 << 3
640 #define TDA9873_MOUT_ST 1 << 4
641 #define TDA9873_MOUT_EXTM (1 << 4 ) & (1 << 3)
642 #define TDA9873_MOUT_EXTL 1 << 5
643 #define TDA9873_MOUT_EXTR (1 << 5 ) & (1 << 3)
644 #define TDA9873_MOUT_EXTLR (1 << 5 ) & (1 << 4)
645 #define TDA9873_MOUT_MUTE (1 << 5 ) & (1 << 4) & (1 << 3)
646
647 /* Status bits: (chip read) */
648 #define TDA9873_PONR 0 /* Power-on reset detected if = 1 */
649 #define TDA9873_STEREO 2 /* Stereo sound is identified */
650 #define TDA9873_DUAL 4 /* Dual sound is identified */
651
652 int tda9873_getmode(struct CHIPSTATE *chip)
653 {
654 int val,mode;
655
656 val = chip_read(chip);
657 mode = VIDEO_SOUND_MONO;
658 if (val & TDA9873_STEREO)
659 mode |= VIDEO_SOUND_STEREO;
660 if (val & TDA9873_DUAL)
661 mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
662 dprintk ("tda9873_getmode(): raw chip read: %d, return: %d\n",
663 val, mode);
664 return mode;
665 }
666
667 void tda9873_setmode(struct CHIPSTATE *chip, int mode)
668 {
669 int sw_data = chip->shadow.bytes[TDA9873_SW+1] & ~ TDA9873_TR_MASK;
670 /* int adj_data = chip->shadow.bytes[TDA9873_AD+1] ; */
671
672 if ((sw_data & TDA9873_INP_MASK) != TDA9873_INTERNAL) {
673 dprintk("tda9873_setmode(): external input\n");
674 return;
675 }
676
677 dprintk("tda9873_setmode(): chip->shadow.bytes[%d] = %d\n", TDA9873_SW+1, chip->shadow.bytes[TDA9873_SW+1]);
678 dprintk("tda9873_setmode(): sw_data = %d\n", sw_data);
679
680 switch (mode) {
681 case VIDEO_SOUND_MONO:
682 sw_data |= TDA9873_TR_MONO;
683 break;
684 case VIDEO_SOUND_STEREO:
685 sw_data |= TDA9873_TR_STEREO;
686 break;
687 case VIDEO_SOUND_LANG1:
688 sw_data |= TDA9873_TR_DUALA;
689 break;
690 case VIDEO_SOUND_LANG2:
691 sw_data |= TDA9873_TR_DUALB;
692 break;
693 default:
694 chip->mode = 0;
695 return;
696 }
697
698 dprintk("tda9873_setmode(): req. mode %d; chip_write: %d\n",
699 mode, sw_data);
700 }
701
702 int tda9873_checkit(struct CHIPSTATE *chip)
703 {
704 int rc;
705
706 if (-1 == (rc = chip_read2(chip,254)))
707 return 0;
708 return (rc & ~0x1f) == 0x80;
709 }
710
711
712 /* ---------------------------------------------------------------------- */
713 /* audio chip descriptions - defines+functions for tea6420 */
714
715 #define TEA6300_VL 0x00 /* volume left */
716 #define TEA6300_VR 0x01 /* volume right */
717 #define TEA6300_BA 0x02 /* bass */
718 #define TEA6300_TR 0x03 /* treble */
719 #define TEA6300_FA 0x04 /* fader control */
720 #define TEA6300_S 0x05 /* switch register */
721 /* values for those registers: */
722 #define TEA6300_S_SA 0x01 /* stereo A input */
723 #define TEA6300_S_SB 0x02 /* stereo B */
724 #define TEA6300_S_SC 0x04 /* stereo C */
725 #define TEA6300_S_GMU 0x80 /* general mute */
726
727 #define TEA6420_S_SA 0x00 /* stereo A input */
728 #define TEA6420_S_SB 0x01 /* stereo B */
729 #define TEA6420_S_SC 0x02 /* stereo C */
730 #define TEA6420_S_SD 0x03 /* stereo D */
731 #define TEA6420_S_SE 0x04 /* stereo E */
732 #define TEA6420_S_GMU 0x05 /* general mute */
733
734 int tea6300_shift10(int val) { return val >> 10; }
735 int tea6300_shift12(int val) { return val >> 12; }
736
737
738 /* ---------------------------------------------------------------------- */
739 /* audio chip descriptions - defines+functions for tda8425 */
740
741 #define TDA8425_VL 0x00 /* volume left */
742 #define TDA8425_VR 0x01 /* volume right */
743 #define TDA8425_BA 0x02 /* bass */
744 #define TDA8425_TR 0x03 /* treble */
745 #define TDA8425_S1 0x08 /* switch functions */
746 /* values for those registers: */
747 #define TDA8425_S1_OFF 0xEE /* audio off (mute on) */
748 #define TDA8425_S1_ON 0xCE /* audio on (mute off) - "linear stereo" mode */
749
750 int tda8425_shift10(int val) { return val >> 10 | 0xc0; }
751 int tda8425_shift12(int val) { return val >> 12 | 0xf0; }
752
753
754 /* ---------------------------------------------------------------------- */
755 /* audio chip descriptions - defines+functions for pic16c54 (PV951) */
756
757 /* the registers of 16C54, I2C sub address. */
758 #define PIC16C54_REG_KEY_CODE 0x01 /* Not use. */
759 #define PIC16C54_REG_MISC 0x02
760
761 /* bit definition of the RESET register, I2C data. */
762 #define PIC16C54_MISC_RESET_REMOTE_CTL 0x01 /* bit 0, Reset to receive the key */
763 /* code of remote controller */
764 #define PIC16C54_MISC_MTS_MAIN 0x02 /* bit 1 */
765 #define PIC16C54_MISC_MTS_SAP 0x04 /* bit 2 */
766 #define PIC16C54_MISC_MTS_BOTH 0x08 /* bit 3 */
767 #define PIC16C54_MISC_SND_MUTE 0x10 /* bit 4, Mute Audio(Line-in and Tuner) */
768 #define PIC16C54_MISC_SND_NOTMUTE 0x20 /* bit 5 */
769 #define PIC16C54_MISC_SWITCH_TUNER 0x40 /* bit 6 , Switch to Line-in */
770 #define PIC16C54_MISC_SWITCH_LINE 0x80 /* bit 7 , Switch to Tuner */
771
772
773 /* ---------------------------------------------------------------------- */
774 /* audio chip descriptions - struct CHIPDESC */
775
776 /* insmod options to enable/disable individual audio chips */
777 int tda8425 = 1;
778 int tda9840 = 1;
779 int tda9850 = 1;
780 int tda9855 = 1;
781 int tda9873 = 1;
782 int tea6300 = 0;
783 int tea6420 = 1;
784 int pic16c54 = 1;
785 MODULE_PARM(tda8425,"i");
786 MODULE_PARM(tda9840,"i");
787 MODULE_PARM(tda9850,"i");
788 MODULE_PARM(tda9855,"i");
789 MODULE_PARM(tda9873,"i");
790 MODULE_PARM(tea6300,"i");
791 MODULE_PARM(tea6420,"i");
792 MODULE_PARM(pic16c54,"i");
793
794 static struct CHIPDESC chiplist[] = {
795 {
796 name: "tda9840",
797 id: I2C_DRIVERID_TDA9840,
798 insmodopt: &tda9840,
799 addr_lo: I2C_TDA9840 >> 1,
800 addr_hi: I2C_TDA9840 >> 1,
801 registers: 5,
802
803 getmode: tda9840_getmode,
804 setmode: tda9840_setmode,
805 checkmode: generic_checkmode,
806
807 init: { 2, { TDA9840_TEST, TDA9840_TEST_INT1SN
808 /* ,TDA9840_SW, TDA9840_MONO */} }
809 },
810 {
811 name: "tda9873h",
812 id: I2C_DRIVERID_TDA9873,
813 checkit: tda9873_checkit,
814 insmodopt: &tda9873,
815 addr_lo: I2C_TDA985x_L >> 1,
816 addr_hi: I2C_TDA985x_H >> 1,
817 registers: 3,
818 flags: CHIP_HAS_INPUTSEL,
819
820 getmode: tda9873_getmode,
821 setmode: tda9873_setmode,
822 checkmode: generic_checkmode,
823
824 init: { 4, { TDA9873_SW, 0xa4, 0x06, 0x03 } },
825 inputreg: TDA9873_SW,
826 inputmute: TDA9873_MUTE | TDA9873_AUTOMUTE,
827 inputmap: {0xa0, 0xa2, 0xa0, 0xa0, 0xc0},
828 inputmask: TDA9873_INP_MASK | TDA9873_MUTE | TDA9873_AUTOMUTE
829
830 },
831 {
832 name: "tda9850",
833 id: I2C_DRIVERID_TDA9850,
834 insmodopt: &tda9850,
835 addr_lo: I2C_TDA985x_L >> 1,
836 addr_hi: I2C_TDA985x_H >> 1,
837 registers: 11,
838
839 getmode: tda985x_getmode,
840 setmode: tda985x_setmode,
841
842 init: { 8, { TDA9850_C4, 0x08, 0x08, TDA985x_STEREO, 0x07, 0x10, 0x10, 0x03 } }
843 },
844 {
845 name: "tda9855",
846 id: I2C_DRIVERID_TDA9855,
847 insmodopt: &tda9855,
848 addr_lo: I2C_TDA985x_L >> 1,
849 addr_hi: I2C_TDA985x_H >> 1,
850 registers: 11,
851 flags: CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE,
852
853 leftreg: TDA9855_VR,
854 rightreg: TDA9855_VL,
855 bassreg: TDA9855_BA,
856 treblereg: TDA9855_TR,
857 volfunc: tda9855_volume,
858 bassfunc: tda9855_bass,
859 treblefunc: tda9855_treble,
860
861 getmode: tda985x_getmode,
862 setmode: tda985x_setmode,
863
864 init: { 12, { 0, 0x6f, 0x6f, 0x0e, 0x07<<1, 0x8<<2,
865 TDA9855_MUTE | TDA9855_AVL | TDA9855_LOUD | TDA9855_INT,
866 TDA985x_STEREO | TDA9855_LINEAR | TDA9855_TZCM | TDA9855_VZCM,
867 0x07, 0x10, 0x10, 0x03 }}
868 },
869 {
870 name: "tea6300",
871 id: I2C_DRIVERID_TEA6300,
872 insmodopt: &tea6300,
873 addr_lo: I2C_TEA6300 >> 1,
874 addr_hi: I2C_TEA6300 >> 1,
875 registers: 6,
876 flags: CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,
877
878 leftreg: TEA6300_VR,
879 rightreg: TEA6300_VL,
880 bassreg: TEA6300_BA,
881 treblereg: TEA6300_TR,
882 volfunc: tea6300_shift10,
883 bassfunc: tea6300_shift12,
884 treblefunc: tea6300_shift12,
885
886 inputreg: TEA6300_S,
887 inputmap: { TEA6300_S_SA, TEA6300_S_SB, TEA6300_S_SC },
888 inputmute: TEA6300_S_GMU,
889 },
890 {
891 name: "tea6420",
892 id: I2C_DRIVERID_TEA6420,
893 insmodopt: &tea6420,
894 addr_lo: I2C_TEA6420 >> 1,
895 addr_hi: I2C_TEA6420 >> 1,
896 registers: 1,
897 flags: CHIP_HAS_INPUTSEL,
898
899 inputreg: -1,
900 inputmap: { TEA6420_S_SA, TEA6420_S_SB, TEA6420_S_SC },
901 inputmute: TEA6300_S_GMU,
902 },
903 {
904 name: "tda8425",
905 id: I2C_DRIVERID_TDA8425,
906 insmodopt: &tda8425,
907 addr_lo: I2C_TDA8425 >> 1,
908 addr_hi: I2C_TDA8425 >> 1,
909 registers: 9,
910 flags: CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,
911
912 leftreg: TDA8425_VR,
913 rightreg: TDA8425_VL,
914 bassreg: TDA8425_BA,
915 treblereg: TDA8425_TR,
916 volfunc: tda8425_shift10,
917 bassfunc: tda8425_shift12,
918 treblefunc: tda8425_shift12,
919
920 inputreg: TDA8425_S1,
921 inputmap: { TDA8425_S1_ON, TDA8425_S1_ON, TDA8425_S1_ON },
922 inputmute: TDA8425_S1_OFF,
923 },
924 {
925 name: "pic16c54 (PV951)",
926 id: I2C_DRIVERID_PIC16C54_PV951,
927 insmodopt: &pic16c54,
928 addr_lo: I2C_PIC16C54 >> 1,
929 addr_hi: I2C_PIC16C54>> 1,
930 registers: 2,
931 flags: CHIP_HAS_INPUTSEL,
932
933 inputreg: PIC16C54_REG_MISC,
934 inputmap: {PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_TUNER,
935 PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_LINE,
936 PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_LINE,
937 PIC16C54_MISC_SND_MUTE,PIC16C54_MISC_SND_MUTE,
938 PIC16C54_MISC_SND_NOTMUTE},
939 inputmute: PIC16C54_MISC_SND_MUTE,
940 },
941 { name: NULL } /* EOF */
942 };
943
944
945 /* ---------------------------------------------------------------------- */
946 /* i2c registration */
947
948 static int chip_attach(struct i2c_adapter *adap, int addr,
949 unsigned short flags, int kind)
950 {
951 struct CHIPSTATE *chip;
952 struct CHIPDESC *desc;
953
954 chip = kmalloc(sizeof(*chip),GFP_KERNEL);
955 if (!chip)
956 return -ENOMEM;
957 memset(chip,0,sizeof(*chip));
958 memcpy(&chip->c,&client_template,sizeof(struct i2c_client));
959 chip->c.adapter = adap;
960 chip->c.addr = addr;
961 chip->c.data = chip;
962
963 /* find description for the chip */
964 dprintk("tvaudio: chip @ addr=0x%x\n", addr<<1);
965 for (desc = chiplist; desc->name != NULL; desc++) {
966 if (0 == *(desc->insmodopt))
967 continue;
968 if (addr < desc->addr_lo ||
969 addr > desc->addr_hi)
970 continue;
971 if (desc->checkit && !desc->checkit(chip))
972 continue;
973 break;
974 }
975 if (desc->name == NULL) {
976 dprintk("tvaudio: no matching chip description found\n");
977 return -EIO;
978 }
979 dprintk("tvaudio: %s matches:%s%s%s\n",desc->name,
980 (desc->flags & CHIP_HAS_VOLUME) ? " volume" : "",
981 (desc->flags & CHIP_HAS_BASSTREBLE) ? " bass/treble" : "",
982 (desc->flags & CHIP_HAS_INPUTSEL) ? " audiomux" : "");
983
984 /* fill required data structures */
985 strcpy(chip->c.name,desc->name);
986 chip->type = desc-chiplist;
987 chip->shadow.count = desc->registers+1;
988 chip->prevmode = -1;
989 /* register */
990 MOD_INC_USE_COUNT;
991 i2c_attach_client(&chip->c);
992
993 /* initialization */
994 chip_cmd(chip,"init",&desc->init);
995 if (desc->flags & CHIP_HAS_VOLUME) {
996 chip->left = desc->leftinit ? desc->leftinit : 65536;
997 chip->right = desc->rightinit ? desc->rightinit : 65536;
998 chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
999 chip_write(chip,desc->rightreg,desc->volfunc(chip->right));
1000 }
1001 if (desc->flags & CHIP_HAS_BASSTREBLE) {
1002 chip->treble = desc->trebleinit ? desc->trebleinit : 32768;
1003 chip->bass = desc->bassinit ? desc->bassinit : 32768;
1004 chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass));
1005 chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble));
1006 }
1007
1008 if (desc->checkmode) {
1009 /* start async thread */
1010 DECLARE_MUTEX_LOCKED(sem);
1011 chip->notify = &sem;
1012 chip->wt.function = chip_thread_wake;
1013 chip->wt.data = (unsigned long)chip;
1014 init_waitqueue_head(&chip->wq);
1015 kernel_thread(chip_thread,(void *)chip,0);
1016 down(&sem);
1017 chip->notify = NULL;
1018 wake_up_interruptible(&chip->wq);
1019 }
1020 return 0;
1021 }
1022
1023 static int chip_probe(struct i2c_adapter *adap)
1024 {
1025 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
1026 return i2c_probe(adap, &addr_data, chip_attach);
1027 return 0;
1028 }
1029
1030 static int chip_detach(struct i2c_client *client)
1031 {
1032 struct CHIPSTATE *chip = client->data;
1033
1034 del_timer(&chip->wt);
1035 if (NULL != chip->thread) {
1036 /* shutdown async thread */
1037 DECLARE_MUTEX_LOCKED(sem);
1038 chip->notify = &sem;
1039 chip->done = 1;
1040 wake_up_interruptible(&chip->wq);
1041 down(&sem);
1042 chip->notify = NULL;
1043 }
1044
1045 i2c_detach_client(&chip->c);
1046 kfree(chip);
1047 MOD_DEC_USE_COUNT;
1048 return 0;
1049 }
1050
1051 /* ---------------------------------------------------------------------- */
1052 /* video4linux interface */
1053
1054 static int chip_command(struct i2c_client *client,
1055 unsigned int cmd, void *arg)
1056 {
1057 __u16 *sarg = arg;
1058 struct CHIPSTATE *chip = client->data;
1059 struct CHIPDESC *desc = chiplist + chip->type;
1060
1061 dprintk("%s: chip_command 0x%x\n",chip->c.name,cmd);
1062
1063 switch (cmd) {
1064 case AUDC_SET_INPUT:
1065 if (desc->flags & CHIP_HAS_INPUTSEL) {
1066 if (*sarg & 0x80)
1067 chip_write_masked(chip,desc->inputreg,desc->inputmute,desc->inputmask);
1068 else
1069 chip_write_masked(chip,desc->inputreg,desc->inputmap[*sarg],desc->inputmask);
1070 }
1071 break;
1072 /* --- v4l ioctls --- */
1073 /* take care: bttv does userspace copying, we'll get a
1074 kernel pointer here... */
1075 case VIDIOCGAUDIO:
1076 {
1077 struct video_audio *va = arg;
1078
1079 if (desc->flags & CHIP_HAS_VOLUME) {
1080 va->flags |= VIDEO_AUDIO_VOLUME;
1081 va->volume = MAX(chip->left,chip->right);
1082 va->balance = (32768*MIN(chip->left,chip->right))/
1083 (va->volume ? va->volume : 1);
1084 }
1085 if (desc->flags & CHIP_HAS_BASSTREBLE) {
1086 va->flags |= VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
1087 va->bass = chip->bass;
1088 va->treble = chip->treble;
1089 }
1090 if (desc->getmode)
1091 va->mode = desc->getmode(chip);
1092 else
1093 va->mode = VIDEO_SOUND_MONO;
1094 break;
1095 }
1096
1097 case VIDIOCSAUDIO:
1098 {
1099 struct video_audio *va = arg;
1100
1101 if (desc->flags & CHIP_HAS_VOLUME) {
1102 chip->left = (MIN(65536 - va->balance,32768) *
1103 va->volume) / 32768;
1104 chip->right = (MIN(va->balance,32768) *
1105 va->volume) / 32768;
1106 chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
1107 chip_write(chip,desc->rightreg,desc->volfunc(chip->right));
1108 }
1109 if (desc->flags & CHIP_HAS_BASSTREBLE) {
1110 chip->bass = va->bass;
1111 chip->treble = va->treble;
1112 chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass));
1113 chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble));
1114 }
1115 if (desc->setmode && va->mode) {
1116 chip->mode = va->mode;
1117 desc->setmode(chip,va->mode);
1118 }
1119 break;
1120 }
1121 case VIDIOCSFREQ:
1122 {
1123 chip->mode = 0; /* automatic */
1124 if (desc->checkmode) {
1125 desc->setmode(chip,VIDEO_SOUND_MONO);
1126 if (chip->prevmode != VIDEO_SOUND_MONO)
1127 chip->prevmode = -1; /* reset previous mode */
1128 mod_timer(&chip->wt, jiffies+2*HZ);
1129 /* the thread will call checkmode() later */
1130 }
1131 }
1132 }
1133 return 0;
1134 }
1135
1136
1137 static struct i2c_driver driver = {
1138 name: "generic i2c audio driver",
1139 id: I2C_DRIVERID_TVAUDIO, /* FIXME */
1140 flags: I2C_DF_NOTIFY,
1141 attach_adapter: chip_probe,
1142 detach_client: chip_detach,
1143 command: chip_command,
1144 };
1145
1146 static struct i2c_client client_template =
1147 {
1148 name: "(unset)",
1149 driver: &driver,
1150 };
1151
1152 int audiochip_init_module(void)
1153 {
1154 struct CHIPDESC *desc;
1155 printk(KERN_INFO "tvaudio: TV audio decoder + audio/video mux driver\n");
1156 printk(KERN_INFO "tvaudio: known chips: ");
1157 for (desc = chiplist; desc->name != NULL; desc++)
1158 printk("%s%s", (desc == chiplist) ? "" : ",",desc->name);
1159 printk("\n");
1160 i2c_add_driver(&driver);
1161 return 0;
1162 }
1163
1164 void audiochip_cleanup_module(void)
1165 {
1166 i2c_del_driver(&driver);
1167 }
1168
1169 module_init(audiochip_init_module);
1170 module_exit(audiochip_cleanup_module);
1171
1172 /*
1173 * Local variables:
1174 * c-basic-offset: 8
1175 * End:
1176 */
1177