tvaudio.c 57.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Driver for simple i2c audio chips.
L
Linus Torvalds 已提交
3 4 5 6 7 8 9
 *
 * Copyright (c) 2000 Gerd Knorr
 * based on code by:
 *   Eric Sandeen (eric_sandeen@bigfoot.com)
 *   Steve VanDeBogart (vandebo@uclink.berkeley.edu)
 *   Greg Alexander (galexand@acm.org)
 *
10 11 12 13
 * Copyright(c) 2005-2008 Mauro Carvalho Chehab
 *	- Some cleanups, code fixes, etc
 *	- Convert it to V4L2 API
 *
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * This code is placed under the terms of the GNU General Public License
 *
 * OPTIONS:
 *   debug - set to 1 if you'd like to see debug messages
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
29
#include <linux/videodev2.h>
L
Linus Torvalds 已提交
30 31
#include <linux/i2c.h>
#include <linux/init.h>
32
#include <linux/kthread.h>
33
#include <linux/freezer.h>
L
Linus Torvalds 已提交
34

35
#include <media/tvaudio.h>
36
#include <media/v4l2-device.h>
37
#include <media/v4l2-chip-ident.h>
38
#include <media/v4l2-i2c-drv-legacy.h>
L
Linus Torvalds 已提交
39

40
#include <media/i2c-addr.h>
L
Linus Torvalds 已提交
41 42 43 44

/* ---------------------------------------------------------------------- */
/* insmod args                                                            */

45
static int debug;	/* insmod parameter */
L
Linus Torvalds 已提交
46 47 48 49 50 51 52
module_param(debug, int, 0644);

MODULE_DESCRIPTION("device driver for various i2c TV sound decoder / audiomux chips");
MODULE_AUTHOR("Eric Sandeen, Steve VanDeBogart, Greg Alexander, Gerd Knorr");
MODULE_LICENSE("GPL");

#define UNSET    (-1U)
53

L
Linus Torvalds 已提交
54 55 56
/* ---------------------------------------------------------------------- */
/* our structs                                                            */

57
#define MAXREGS 256
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

struct CHIPSTATE;
typedef int  (*getvalue)(int);
typedef int  (*checkit)(struct CHIPSTATE*);
typedef int  (*initialize)(struct CHIPSTATE*);
typedef int  (*getmode)(struct CHIPSTATE*);
typedef void (*setmode)(struct CHIPSTATE*, int mode);

/* i2c command */
typedef struct AUDIOCMD {
	int             count;             /* # of bytes to send */
	unsigned char   bytes[MAXREGS+1];  /* addr, data, data, ... */
} audiocmd;

/* chip description */
struct CHIPDESC {
	char       *name;             /* chip name         */
	int        addr_lo, addr_hi;  /* i2c address range */
	int        registers;         /* # of registers    */

	int        *insmodopt;
	checkit    checkit;
	initialize initialize;
	int        flags;
#define CHIP_HAS_VOLUME      1
#define CHIP_HAS_BASSTREBLE  2
#define CHIP_HAS_INPUTSEL    4
85
#define CHIP_NEED_CHECKMODE  8
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

	/* various i2c command sequences */
	audiocmd   init;

	/* which register has which value */
	int    leftreg,rightreg,treblereg,bassreg;

	/* initialize with (defaults to 65535/65535/32768/32768 */
	int    leftinit,rightinit,trebleinit,bassinit;

	/* functions to convert the values (v4l -> chip) */
	getvalue volfunc,treblefunc,bassfunc;

	/* get/set mode */
	getmode  getmode;
	setmode  setmode;

	/* input switch register + values for v4l inputs */
	int  inputreg;
105
	int  inputmap[4];
L
Linus Torvalds 已提交
106 107 108 109 110 111
	int  inputmute;
	int  inputmask;
};

/* current state of the chip */
struct CHIPSTATE {
112
	struct v4l2_subdev sd;
L
Linus Torvalds 已提交
113

114 115 116
	/* chip-specific description - should point to
	   an entry at CHIPDESC table */
	struct CHIPDESC *desc;
L
Linus Torvalds 已提交
117 118 119 120 121

	/* shadow register set */
	audiocmd   shadow;

	/* current settings */
122
	__u16 left,right,treble,bass,muted,mode;
L
Linus Torvalds 已提交
123
	int prevmode;
124
	int radio;
125
	int input;
L
Linus Torvalds 已提交
126 127

	/* thread */
128
	struct task_struct   *thread;
L
Linus Torvalds 已提交
129 130
	struct timer_list    wt;
	int                  watch_stereo;
131
	int 		     audmode;
L
Linus Torvalds 已提交
132 133
};

134 135 136 137 138
static inline struct CHIPSTATE *to_state(struct v4l2_subdev *sd)
{
	return container_of(sd, struct CHIPSTATE, sd);
}

L
Linus Torvalds 已提交
139 140 141 142
/* ---------------------------------------------------------------------- */
/* i2c addresses                                                          */

static unsigned short normal_i2c[] = {
143 144 145 146 147 148 149 150
	I2C_ADDR_TDA8425   >> 1,
	I2C_ADDR_TEA6300   >> 1,
	I2C_ADDR_TEA6420   >> 1,
	I2C_ADDR_TDA9840   >> 1,
	I2C_ADDR_TDA985x_L >> 1,
	I2C_ADDR_TDA985x_H >> 1,
	I2C_ADDR_TDA9874   >> 1,
	I2C_ADDR_PIC16C54  >> 1,
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158
	I2C_CLIENT_END };
I2C_CLIENT_INSMOD;

/* ---------------------------------------------------------------------- */
/* i2c I/O functions                                                      */

static int chip_write(struct CHIPSTATE *chip, int subaddr, int val)
{
159 160
	struct v4l2_subdev *sd = &chip->sd;
	struct i2c_client *c = v4l2_get_subdevdata(sd);
L
Linus Torvalds 已提交
161 162
	unsigned char buffer[2];

163
	if (subaddr < 0) {
164
		v4l2_dbg(1, debug, sd, "chip_write: 0x%x\n", val);
L
Linus Torvalds 已提交
165 166
		chip->shadow.bytes[1] = val;
		buffer[0] = val;
167 168
		if (1 != i2c_master_send(c, buffer, 1)) {
			v4l2_warn(sd, "I/O error (write 0x%x)\n", val);
L
Linus Torvalds 已提交
169 170 171
			return -1;
		}
	} else {
172
		if (subaddr + 1 >= ARRAY_SIZE(chip->shadow.bytes)) {
173
			v4l2_info(sd,
174 175 176 177 178
				"Tried to access a non-existent register: %d\n",
				subaddr);
			return -EINVAL;
		}

179 180
		v4l2_dbg(1, debug, sd, "chip_write: reg%d=0x%x\n",
			subaddr, val);
L
Linus Torvalds 已提交
181 182 183
		chip->shadow.bytes[subaddr+1] = val;
		buffer[0] = subaddr;
		buffer[1] = val;
184 185 186
		if (2 != i2c_master_send(c, buffer, 2)) {
			v4l2_warn(sd, "I/O error (write reg%d=0x%x)\n",
				subaddr, val);
L
Linus Torvalds 已提交
187 188 189 190 191 192
			return -1;
		}
	}
	return 0;
}

193 194
static int chip_write_masked(struct CHIPSTATE *chip,
			     int subaddr, int val, int mask)
L
Linus Torvalds 已提交
195
{
196 197
	struct v4l2_subdev *sd = &chip->sd;

L
Linus Torvalds 已提交
198
	if (mask != 0) {
199
		if (subaddr < 0) {
L
Linus Torvalds 已提交
200 201
			val = (chip->shadow.bytes[1] & ~mask) | (val & mask);
		} else {
202
			if (subaddr + 1 >= ARRAY_SIZE(chip->shadow.bytes)) {
203
				v4l2_info(sd,
204 205 206 207 208
					"Tried to access a non-existent register: %d\n",
					subaddr);
				return -EINVAL;
			}

L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216
			val = (chip->shadow.bytes[subaddr+1] & ~mask) | (val & mask);
		}
	}
	return chip_write(chip, subaddr, val);
}

static int chip_read(struct CHIPSTATE *chip)
{
217 218
	struct v4l2_subdev *sd = &chip->sd;
	struct i2c_client *c = v4l2_get_subdevdata(sd);
L
Linus Torvalds 已提交
219 220
	unsigned char buffer;

221 222
	if (1 != i2c_master_recv(c, &buffer, 1)) {
		v4l2_warn(sd, "I/O error (read)\n");
L
Linus Torvalds 已提交
223 224
		return -1;
	}
225
	v4l2_dbg(1, debug, sd, "chip_read: 0x%x\n", buffer);
L
Linus Torvalds 已提交
226 227 228 229 230
	return buffer;
}

static int chip_read2(struct CHIPSTATE *chip, int subaddr)
{
231 232
	struct v4l2_subdev *sd = &chip->sd;
	struct i2c_client *c = v4l2_get_subdevdata(sd);
233 234 235
	unsigned char write[1];
	unsigned char read[1];
	struct i2c_msg msgs[2] = {
236 237
		{ c->addr, 0,        1, write },
		{ c->addr, I2C_M_RD, 1, read  }
238
	};
239

240
	write[0] = subaddr;
L
Linus Torvalds 已提交
241

242 243
	if (2 != i2c_transfer(c->adapter, msgs, 2)) {
		v4l2_warn(sd, "I/O error (read2)\n");
L
Linus Torvalds 已提交
244 245
		return -1;
	}
246 247
	v4l2_dbg(1, debug, sd, "chip_read2: reg%d=0x%x\n",
		subaddr, read[0]);
L
Linus Torvalds 已提交
248 249 250 251 252
	return read[0];
}

static int chip_cmd(struct CHIPSTATE *chip, char *name, audiocmd *cmd)
{
253 254
	struct v4l2_subdev *sd = &chip->sd;
	struct i2c_client *c = v4l2_get_subdevdata(sd);
L
Linus Torvalds 已提交
255 256 257 258 259
	int i;

	if (0 == cmd->count)
		return 0;

260
	if (cmd->count + cmd->bytes[0] - 1 >= ARRAY_SIZE(chip->shadow.bytes)) {
261
		v4l2_info(sd,
262 263 264 265 266 267 268
			 "Tried to access a non-existent register range: %d to %d\n",
			 cmd->bytes[0] + 1, cmd->bytes[0] + cmd->count - 1);
		return -EINVAL;
	}

	/* FIXME: it seems that the shadow bytes are wrong bellow !*/

L
Linus Torvalds 已提交
269
	/* update our shadow register set; print bytes if (debug > 0) */
270 271
	v4l2_dbg(1, debug, sd, "chip_cmd(%s): reg=%d, data:",
		name, cmd->bytes[0]);
L
Linus Torvalds 已提交
272
	for (i = 1; i < cmd->count; i++) {
273
		if (debug)
274
			printk(KERN_CONT " 0x%x", cmd->bytes[i]);
L
Linus Torvalds 已提交
275 276
		chip->shadow.bytes[i+cmd->bytes[0]] = cmd->bytes[i];
	}
277
	if (debug)
278
		printk(KERN_CONT "\n");
L
Linus Torvalds 已提交
279 280

	/* send data to the chip */
281 282
	if (cmd->count != i2c_master_send(c, cmd->bytes, cmd->count)) {
		v4l2_warn(sd, "I/O error (%s)\n", name);
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296
		return -1;
	}
	return 0;
}

/* ---------------------------------------------------------------------- */
/* kernel thread for doing i2c stuff asyncronly
 *   right now it is used only to check the audio mode (mono/stereo/whatever)
 *   some time after switching to another TV channel, then turn on stereo
 *   if available, ...
 */

static void chip_thread_wake(unsigned long data)
{
297
	struct CHIPSTATE *chip = (struct CHIPSTATE*)data;
298
	wake_up_process(chip->thread);
L
Linus Torvalds 已提交
299 300 301 302
}

static int chip_thread(void *data)
{
303
	struct CHIPSTATE *chip = data;
304
	struct CHIPDESC  *desc = chip->desc;
305
	struct v4l2_subdev *sd = &chip->sd;
306
	int mode;
L
Linus Torvalds 已提交
307

308
	v4l2_dbg(1, debug, sd, "thread started\n");
309
	set_freezable();
L
Linus Torvalds 已提交
310
	for (;;) {
311 312
		set_current_state(TASK_INTERRUPTIBLE);
		if (!kthread_should_stop())
L
Linus Torvalds 已提交
313
			schedule();
314
		set_current_state(TASK_RUNNING);
315
		try_to_freeze();
316
		if (kthread_should_stop())
L
Linus Torvalds 已提交
317
			break;
318
		v4l2_dbg(1, debug, sd, "thread wakeup\n");
L
Linus Torvalds 已提交
319 320

		/* don't do anything for radio or if mode != auto */
321
		if (chip->radio || chip->mode != 0)
L
Linus Torvalds 已提交
322 323 324
			continue;

		/* have a look what's going on */
325 326 327 328 329
		mode = desc->getmode(chip);
		if (mode == chip->prevmode)
			continue;

		/* chip detected a new audio mode - set it */
330
		v4l2_dbg(1, debug, sd, "thread checkmode\n");
331 332 333 334 335 336 337 338 339 340 341 342 343

		chip->prevmode = mode;

		if (mode & V4L2_TUNER_MODE_STEREO)
			desc->setmode(chip, V4L2_TUNER_MODE_STEREO);
		if (mode & V4L2_TUNER_MODE_LANG1_LANG2)
			desc->setmode(chip, V4L2_TUNER_MODE_STEREO);
		else if (mode & V4L2_TUNER_MODE_LANG1)
			desc->setmode(chip, V4L2_TUNER_MODE_LANG1);
		else if (mode & V4L2_TUNER_MODE_LANG2)
			desc->setmode(chip, V4L2_TUNER_MODE_LANG2);
		else
			desc->setmode(chip, V4L2_TUNER_MODE_MONO);
L
Linus Torvalds 已提交
344 345

		/* schedule next check */
346
		mod_timer(&chip->wt, jiffies+msecs_to_jiffies(2000));
L
Linus Torvalds 已提交
347 348
	}

349
	v4l2_dbg(1, debug, sd, "thread exiting\n");
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	return 0;
}

/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for tda9840                */

#define TDA9840_SW         0x00
#define TDA9840_LVADJ      0x02
#define TDA9840_STADJ      0x03
#define TDA9840_TEST       0x04

#define TDA9840_MONO       0x10
#define TDA9840_STEREO     0x2a
#define TDA9840_DUALA      0x12
#define TDA9840_DUALB      0x1e
#define TDA9840_DUALAB     0x1a
#define TDA9840_DUALBA     0x16
#define TDA9840_EXTERNAL   0x7a

#define TDA9840_DS_DUAL    0x20 /* Dual sound identified          */
#define TDA9840_ST_STEREO  0x40 /* Stereo sound identified        */
#define TDA9840_PONRES     0x80 /* Power-on reset detected if = 1 */

#define TDA9840_TEST_INT1SN 0x1 /* Integration time 0.5s when set */
#define TDA9840_TEST_INTFU 0x02 /* Disables integrator function */

static int tda9840_getmode(struct CHIPSTATE *chip)
{
378
	struct v4l2_subdev *sd = &chip->sd;
L
Linus Torvalds 已提交
379 380 381
	int val, mode;

	val = chip_read(chip);
382
	mode = V4L2_TUNER_MODE_MONO;
L
Linus Torvalds 已提交
383
	if (val & TDA9840_DS_DUAL)
384
		mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
L
Linus Torvalds 已提交
385
	if (val & TDA9840_ST_STEREO)
386
		mode |= V4L2_TUNER_MODE_STEREO;
L
Linus Torvalds 已提交
387

388
	v4l2_dbg(1, debug, sd, "tda9840_getmode(): raw chip read: %d, return: %d\n",
389
		val, mode);
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397 398
	return mode;
}

static void tda9840_setmode(struct CHIPSTATE *chip, int mode)
{
	int update = 1;
	int t = chip->shadow.bytes[TDA9840_SW + 1] & ~0x7e;

	switch (mode) {
399
	case V4L2_TUNER_MODE_MONO:
L
Linus Torvalds 已提交
400 401
		t |= TDA9840_MONO;
		break;
402
	case V4L2_TUNER_MODE_STEREO:
L
Linus Torvalds 已提交
403 404
		t |= TDA9840_STEREO;
		break;
405
	case V4L2_TUNER_MODE_LANG1:
L
Linus Torvalds 已提交
406 407
		t |= TDA9840_DUALA;
		break;
408
	case V4L2_TUNER_MODE_LANG2:
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418
		t |= TDA9840_DUALB;
		break;
	default:
		update = 0;
	}

	if (update)
		chip_write(chip, TDA9840_SW, t);
}

419 420 421 422 423 424 425 426
static int tda9840_checkit(struct CHIPSTATE *chip)
{
	int rc;
	rc = chip_read(chip);
	/* lower 5 bits should be 0 */
	return ((rc & 0x1f) == 0) ? 1 : 0;
}

L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for tda985x                */

/* subaddresses for TDA9855 */
#define TDA9855_VR	0x00 /* Volume, right */
#define TDA9855_VL	0x01 /* Volume, left */
#define TDA9855_BA	0x02 /* Bass */
#define TDA9855_TR	0x03 /* Treble */
#define TDA9855_SW	0x04 /* Subwoofer - not connected on DTV2000 */

/* subaddresses for TDA9850 */
#define TDA9850_C4	0x04 /* Control 1 for TDA9850 */

/* subaddesses for both chips */
#define TDA985x_C5	0x05 /* Control 2 for TDA9850, Control 1 for TDA9855 */
#define TDA985x_C6	0x06 /* Control 3 for TDA9850, Control 2 for TDA9855 */
#define TDA985x_C7	0x07 /* Control 4 for TDA9850, Control 3 for TDA9855 */
#define TDA985x_A1	0x08 /* Alignment 1 for both chips */
#define TDA985x_A2	0x09 /* Alignment 2 for both chips */
#define TDA985x_A3	0x0a /* Alignment 3 for both chips */

/* Masks for bits in TDA9855 subaddresses */
/* 0x00 - VR in TDA9855 */
/* 0x01 - VL in TDA9855 */
/* lower 7 bits control gain from -71dB (0x28) to 16dB (0x7f)
 * in 1dB steps - mute is 0x27 */


/* 0x02 - BA in TDA9855 */
/* lower 5 bits control bass gain from -12dB (0x06) to 16.5dB (0x19)
 * in .5dB steps - 0 is 0x0E */


/* 0x03 - TR in TDA9855 */
/* 4 bits << 1 control treble gain from -12dB (0x3) to 12dB (0xb)
 * in 3dB steps - 0 is 0x7 */

/* Masks for bits in both chips' subaddresses */
/* 0x04 - SW in TDA9855, C4/Control 1 in TDA9850 */
/* Unique to TDA9855: */
/* 4 bits << 2 control subwoofer/surround gain from -14db (0x1) to 14db (0xf)
 * in 3dB steps - mute is 0x0 */

/* Unique to TDA9850: */
/* lower 4 bits control stereo noise threshold, over which stereo turns off
 * set to values of 0x00 through 0x0f for Ster1 through Ster16 */


/* 0x05 - C5 - Control 1 in TDA9855 , Control 2 in TDA9850*/
/* Unique to TDA9855: */
#define TDA9855_MUTE	1<<7 /* GMU, Mute at outputs */
#define TDA9855_AVL	1<<6 /* AVL, Automatic Volume Level */
#define TDA9855_LOUD	1<<5 /* Loudness, 1==off */
#define TDA9855_SUR	1<<3 /* Surround / Subwoofer 1==.5(L-R) 0==.5(L+R) */
			     /* Bits 0 to 3 select various combinations
482 483
			      * of line in and line out, only the
			      * interesting ones are defined */
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
#define TDA9855_EXT	1<<2 /* Selects inputs LIR and LIL.  Pins 41 & 12 */
#define TDA9855_INT	0    /* Selects inputs LOR and LOL.  (internal) */

/* Unique to TDA9850:  */
/* lower 4 bits contol SAP noise threshold, over which SAP turns off
 * set to values of 0x00 through 0x0f for SAP1 through SAP16 */


/* 0x06 - C6 - Control 2 in TDA9855, Control 3 in TDA9850 */
/* Common to TDA9855 and TDA9850: */
#define TDA985x_SAP	3<<6 /* Selects SAP output, mute if not received */
#define TDA985x_STEREO	1<<6 /* Selects Stereo ouput, mono if not received */
#define TDA985x_MONO	0    /* Forces Mono output */
#define TDA985x_LMU	1<<3 /* Mute (LOR/LOL for 9855, OUTL/OUTR for 9850) */

/* Unique to TDA9855: */
#define TDA9855_TZCM	1<<5 /* If set, don't mute till zero crossing */
#define TDA9855_VZCM	1<<4 /* If set, don't change volume till zero crossing*/
#define TDA9855_LINEAR	0    /* Linear Stereo */
#define TDA9855_PSEUDO	1    /* Pseudo Stereo */
#define TDA9855_SPAT_30	2    /* Spatial Stereo, 30% anti-phase crosstalk */
#define TDA9855_SPAT_50	3    /* Spatial Stereo, 52% anti-phase crosstalk */
#define TDA9855_E_MONO	7    /* Forced mono - mono select elseware, so useless*/

/* 0x07 - C7 - Control 3 in TDA9855, Control 4 in TDA9850 */
/* Common to both TDA9855 and TDA9850: */
/* lower 4 bits control input gain from -3.5dB (0x0) to 4dB (0xF)
 * in .5dB steps -  0dB is 0x7 */

/* 0x08, 0x09 - A1 and A2 (read/write) */
/* Common to both TDA9855 and TDA9850: */
/* lower 5 bites are wideband and spectral expander alignment
 * from 0x00 to 0x1f - nominal at 0x0f and 0x10 (read/write) */
#define TDA985x_STP	1<<5 /* Stereo Pilot/detect (read-only) */
#define TDA985x_SAPP	1<<6 /* SAP Pilot/detect (read-only) */
#define TDA985x_STS	1<<7 /* Stereo trigger 1= <35mV 0= <30mV (write-only)*/

/* 0x0a - A3 */
/* Common to both TDA9855 and TDA9850: */
/* lower 3 bits control timing current for alignment: -30% (0x0), -20% (0x1),
 * -10% (0x2), nominal (0x3), +10% (0x6), +20% (0x5), +30% (0x4) */
#define TDA985x_ADJ	1<<7 /* Stereo adjust on/off (wideband and spectral */

static int tda9855_volume(int val) { return val/0x2e8+0x27; }
static int tda9855_bass(int val)   { return val/0xccc+0x06; }
static int tda9855_treble(int val) { return (val/0x1c71+0x3)<<1; }

static int  tda985x_getmode(struct CHIPSTATE *chip)
{
	int mode;

	mode = ((TDA985x_STP | TDA985x_SAPP) &
		chip_read(chip)) >> 4;
	/* Add mono mode regardless of SAP and stereo */
	/* Allows forced mono */
539
	return mode | V4L2_TUNER_MODE_MONO;
L
Linus Torvalds 已提交
540 541 542 543 544 545 546 547
}

static void tda985x_setmode(struct CHIPSTATE *chip, int mode)
{
	int update = 1;
	int c6 = chip->shadow.bytes[TDA985x_C6+1] & 0x3f;

	switch (mode) {
548
	case V4L2_TUNER_MODE_MONO:
L
Linus Torvalds 已提交
549 550
		c6 |= TDA985x_MONO;
		break;
551
	case V4L2_TUNER_MODE_STEREO:
L
Linus Torvalds 已提交
552 553
		c6 |= TDA985x_STEREO;
		break;
554
	case V4L2_TUNER_MODE_LANG1:
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		c6 |= TDA985x_SAP;
		break;
	default:
		update = 0;
	}
	if (update)
		chip_write(chip,TDA985x_C6,c6);
}


/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for tda9873h               */

/* Subaddresses for TDA9873H */

#define TDA9873_SW	0x00 /* Switching                    */
#define TDA9873_AD	0x01 /* Adjust                       */
#define TDA9873_PT	0x02 /* Port                         */

/* Subaddress 0x00: Switching Data
 * B7..B0:
 *
 * B1, B0: Input source selection
 *  0,  0  internal
 *  1,  0  external stereo
 *  0,  1  external mono
 */
#define TDA9873_INP_MASK    3
#define TDA9873_INTERNAL    0
#define TDA9873_EXT_STEREO  2
#define TDA9873_EXT_MONO    1

/*    B3, B2: output signal select
 * B4    : transmission mode
 *  0, 0, 1   Mono
 *  1, 0, 0   Stereo
 *  1, 1, 1   Stereo (reversed channel)
 *  0, 0, 0   Dual AB
 *  0, 0, 1   Dual AA
 *  0, 1, 0   Dual BB
 *  0, 1, 1   Dual BA
 */

#define TDA9873_TR_MASK     (7 << 2)
#define TDA9873_TR_MONO     4
#define TDA9873_TR_STEREO   1 << 4
#define TDA9873_TR_REVERSE  (1 << 3) & (1 << 2)
#define TDA9873_TR_DUALA    1 << 2
#define TDA9873_TR_DUALB    1 << 3

/* output level controls
 * B5:  output level switch (0 = reduced gain, 1 = normal gain)
 * B6:  mute                (1 = muted)
 * B7:  auto-mute           (1 = auto-mute enabled)
 */

#define TDA9873_GAIN_NORMAL 1 << 5
#define TDA9873_MUTE        1 << 6
#define TDA9873_AUTOMUTE    1 << 7

/* Subaddress 0x01:  Adjust/standard */

/* Lower 4 bits (C3..C0) control stereo adjustment on R channel (-0.6 - +0.7 dB)
 * Recommended value is +0 dB
 */

#define	TDA9873_STEREO_ADJ	0x06 /* 0dB gain */

/* Bits C6..C4 control FM stantard
 * C6, C5, C4
 *  0,  0,  0   B/G (PAL FM)
 *  0,  0,  1   M
 *  0,  1,  0   D/K(1)
 *  0,  1,  1   D/K(2)
 *  1,  0,  0   D/K(3)
 *  1,  0,  1   I
 */
#define TDA9873_BG		0
#define TDA9873_M       1
#define TDA9873_DK1     2
#define TDA9873_DK2     3
#define TDA9873_DK3     4
#define TDA9873_I       5

/* C7 controls identification response time (1=fast/0=normal)
 */
#define TDA9873_IDR_NORM 0
#define TDA9873_IDR_FAST 1 << 7


/* Subaddress 0x02: Port data */

/* E1, E0   free programmable ports P1/P2
    0,  0   both ports low
    0,  1   P1 high
    1,  0   P2 high
    1,  1   both ports high
*/

#define TDA9873_PORTS    3

/* E2: test port */
#define TDA9873_TST_PORT 1 << 2

/* E5..E3 control mono output channel (together with transmission mode bit B4)
 *
 * E5 E4 E3 B4     OUTM
 *  0  0  0  0     mono
 *  0  0  1  0     DUAL B
 *  0  1  0  1     mono (from stereo decoder)
 */
#define TDA9873_MOUT_MONO   0
#define TDA9873_MOUT_FMONO  0
#define TDA9873_MOUT_DUALA  0
#define TDA9873_MOUT_DUALB  1 << 3
#define TDA9873_MOUT_ST     1 << 4
#define TDA9873_MOUT_EXTM   (1 << 4 ) & (1 << 3)
#define TDA9873_MOUT_EXTL   1 << 5
#define TDA9873_MOUT_EXTR   (1 << 5 ) & (1 << 3)
#define TDA9873_MOUT_EXTLR  (1 << 5 ) & (1 << 4)
#define TDA9873_MOUT_MUTE   (1 << 5 ) & (1 << 4) & (1 << 3)

/* Status bits: (chip read) */
#define TDA9873_PONR        0 /* Power-on reset detected if = 1 */
#define TDA9873_STEREO      2 /* Stereo sound is identified     */
#define TDA9873_DUAL        4 /* Dual sound is identified       */

static int tda9873_getmode(struct CHIPSTATE *chip)
{
684
	struct v4l2_subdev *sd = &chip->sd;
L
Linus Torvalds 已提交
685 686 687
	int val,mode;

	val = chip_read(chip);
688
	mode = V4L2_TUNER_MODE_MONO;
L
Linus Torvalds 已提交
689
	if (val & TDA9873_STEREO)
690
		mode |= V4L2_TUNER_MODE_STEREO;
L
Linus Torvalds 已提交
691
	if (val & TDA9873_DUAL)
692
		mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
693
	v4l2_dbg(1, debug, sd, "tda9873_getmode(): raw chip read: %d, return: %d\n",
694
		val, mode);
L
Linus Torvalds 已提交
695 696 697 698 699
	return mode;
}

static void tda9873_setmode(struct CHIPSTATE *chip, int mode)
{
700
	struct v4l2_subdev *sd = &chip->sd;
L
Linus Torvalds 已提交
701 702 703 704
	int sw_data  = chip->shadow.bytes[TDA9873_SW+1] & ~ TDA9873_TR_MASK;
	/*	int adj_data = chip->shadow.bytes[TDA9873_AD+1] ; */

	if ((sw_data & TDA9873_INP_MASK) != TDA9873_INTERNAL) {
705
		v4l2_dbg(1, debug, sd, "tda9873_setmode(): external input\n");
L
Linus Torvalds 已提交
706 707 708
		return;
	}

709 710
	v4l2_dbg(1, debug, sd, "tda9873_setmode(): chip->shadow.bytes[%d] = %d\n", TDA9873_SW+1, chip->shadow.bytes[TDA9873_SW+1]);
	v4l2_dbg(1, debug, sd, "tda9873_setmode(): sw_data  = %d\n", sw_data);
L
Linus Torvalds 已提交
711 712

	switch (mode) {
713
	case V4L2_TUNER_MODE_MONO:
L
Linus Torvalds 已提交
714 715
		sw_data |= TDA9873_TR_MONO;
		break;
716
	case V4L2_TUNER_MODE_STEREO:
L
Linus Torvalds 已提交
717 718
		sw_data |= TDA9873_TR_STEREO;
		break;
719
	case V4L2_TUNER_MODE_LANG1:
L
Linus Torvalds 已提交
720 721
		sw_data |= TDA9873_TR_DUALA;
		break;
722
	case V4L2_TUNER_MODE_LANG2:
L
Linus Torvalds 已提交
723 724 725 726 727 728 729 730
		sw_data |= TDA9873_TR_DUALB;
		break;
	default:
		chip->mode = 0;
		return;
	}

	chip_write(chip, TDA9873_SW, sw_data);
731
	v4l2_dbg(1, debug, sd, "tda9873_setmode(): req. mode %d; chip_write: %d\n",
L
Linus Torvalds 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
		mode, sw_data);
}

static int tda9873_checkit(struct CHIPSTATE *chip)
{
	int rc;

	if (-1 == (rc = chip_read2(chip,254)))
		return 0;
	return (rc & ~0x1f) == 0x80;
}


/* ---------------------------------------------------------------------- */
/* audio chip description - defines+functions for tda9874h and tda9874a   */
/* Dariusz Kowalewski <darekk@automex.pl>                                 */

/* Subaddresses for TDA9874H and TDA9874A (slave rx) */
#define TDA9874A_AGCGR		0x00	/* AGC gain */
#define TDA9874A_GCONR		0x01	/* general config */
#define TDA9874A_MSR		0x02	/* monitor select */
#define TDA9874A_C1FRA		0x03	/* carrier 1 freq. */
#define TDA9874A_C1FRB		0x04	/* carrier 1 freq. */
#define TDA9874A_C1FRC		0x05	/* carrier 1 freq. */
#define TDA9874A_C2FRA		0x06	/* carrier 2 freq. */
#define TDA9874A_C2FRB		0x07	/* carrier 2 freq. */
#define TDA9874A_C2FRC		0x08	/* carrier 2 freq. */
#define TDA9874A_DCR		0x09	/* demodulator config */
#define TDA9874A_FMER		0x0a	/* FM de-emphasis */
#define TDA9874A_FMMR		0x0b	/* FM dematrix */
#define TDA9874A_C1OLAR		0x0c	/* ch.1 output level adj. */
#define TDA9874A_C2OLAR		0x0d	/* ch.2 output level adj. */
#define TDA9874A_NCONR		0x0e	/* NICAM config */
#define TDA9874A_NOLAR		0x0f	/* NICAM output level adj. */
#define TDA9874A_NLELR		0x10	/* NICAM lower error limit */
#define TDA9874A_NUELR		0x11	/* NICAM upper error limit */
#define TDA9874A_AMCONR		0x12	/* audio mute control */
#define TDA9874A_SDACOSR	0x13	/* stereo DAC output select */
#define TDA9874A_AOSR		0x14	/* analog output select */
#define TDA9874A_DAICONR	0x15	/* digital audio interface config */
#define TDA9874A_I2SOSR		0x16	/* I2S-bus output select */
#define TDA9874A_I2SOLAR	0x17	/* I2S-bus output level adj. */
#define TDA9874A_MDACOSR	0x18	/* mono DAC output select (tda9874a) */
#define TDA9874A_ESP		0xFF	/* easy standard progr. (tda9874a) */

/* Subaddresses for TDA9874H and TDA9874A (slave tx) */
#define TDA9874A_DSR		0x00	/* device status */
#define TDA9874A_NSR		0x01	/* NICAM status */
#define TDA9874A_NECR		0x02	/* NICAM error count */
#define TDA9874A_DR1		0x03	/* add. data LSB */
#define TDA9874A_DR2		0x04	/* add. data MSB */
#define TDA9874A_LLRA		0x05	/* monitor level read-out LSB */
#define TDA9874A_LLRB		0x06	/* monitor level read-out MSB */
#define TDA9874A_SIFLR		0x07	/* SIF level */
#define TDA9874A_TR2		252	/* test reg. 2 */
#define TDA9874A_TR1		253	/* test reg. 1 */
#define TDA9874A_DIC		254	/* device id. code */
#define TDA9874A_SIC		255	/* software id. code */


static int tda9874a_mode = 1;		/* 0: A2, 1: NICAM */
static int tda9874a_GCONR = 0xc0;	/* default config. input pin: SIFSEL=0 */
static int tda9874a_NCONR = 0x01;	/* default NICAM config.: AMSEL=0,AMUTE=1 */
static int tda9874a_ESP = 0x07;		/* default standard: NICAM D/K */
static int tda9874a_dic = -1;		/* device id. code */

/* insmod options for tda9874a */
static unsigned int tda9874a_SIF   = UNSET;
static unsigned int tda9874a_AMSEL = UNSET;
static unsigned int tda9874a_STD   = UNSET;
module_param(tda9874a_SIF, int, 0444);
module_param(tda9874a_AMSEL, int, 0444);
module_param(tda9874a_STD, int, 0444);

/*
 * initialization table for tda9874 decoder:
 *  - carrier 1 freq. registers (3 bytes)
 *  - carrier 2 freq. registers (3 bytes)
 *  - demudulator config register
 *  - FM de-emphasis register (slow identification mode)
 * Note: frequency registers must be written in single i2c transfer.
 */
static struct tda9874a_MODES {
	char *name;
	audiocmd cmd;
} tda9874a_modelist[9] = {
818
  {	"A2, B/G", /* default */
L
Linus Torvalds 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831
	{ 9, { TDA9874A_C1FRA, 0x72,0x95,0x55, 0x77,0xA0,0x00, 0x00,0x00 }} },
  {	"A2, M (Korea)",
	{ 9, { TDA9874A_C1FRA, 0x5D,0xC0,0x00, 0x62,0x6A,0xAA, 0x20,0x22 }} },
  {	"A2, D/K (1)",
	{ 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x82,0x60,0x00, 0x00,0x00 }} },
  {	"A2, D/K (2)",
	{ 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x8C,0x75,0x55, 0x00,0x00 }} },
  {	"A2, D/K (3)",
	{ 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x77,0xA0,0x00, 0x00,0x00 }} },
  {	"NICAM, I",
	{ 9, { TDA9874A_C1FRA, 0x7D,0x00,0x00, 0x88,0x8A,0xAA, 0x08,0x33 }} },
  {	"NICAM, B/G",
	{ 9, { TDA9874A_C1FRA, 0x72,0x95,0x55, 0x79,0xEA,0xAA, 0x08,0x33 }} },
832
  {	"NICAM, D/K",
L
Linus Torvalds 已提交
833 834 835 836 837 838 839
	{ 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x79,0xEA,0xAA, 0x08,0x33 }} },
  {	"NICAM, L",
	{ 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x79,0xEA,0xAA, 0x09,0x33 }} }
};

static int tda9874a_setup(struct CHIPSTATE *chip)
{
840 841
	struct v4l2_subdev *sd = &chip->sd;

L
Linus Torvalds 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
	chip_write(chip, TDA9874A_AGCGR, 0x00); /* 0 dB */
	chip_write(chip, TDA9874A_GCONR, tda9874a_GCONR);
	chip_write(chip, TDA9874A_MSR, (tda9874a_mode) ? 0x03:0x02);
	if(tda9874a_dic == 0x11) {
		chip_write(chip, TDA9874A_FMMR, 0x80);
	} else { /* dic == 0x07 */
		chip_cmd(chip,"tda9874_modelist",&tda9874a_modelist[tda9874a_STD].cmd);
		chip_write(chip, TDA9874A_FMMR, 0x00);
	}
	chip_write(chip, TDA9874A_C1OLAR, 0x00); /* 0 dB */
	chip_write(chip, TDA9874A_C2OLAR, 0x00); /* 0 dB */
	chip_write(chip, TDA9874A_NCONR, tda9874a_NCONR);
	chip_write(chip, TDA9874A_NOLAR, 0x00); /* 0 dB */
	/* Note: If signal quality is poor you may want to change NICAM */
	/* error limit registers (NLELR and NUELR) to some greater values. */
	/* Then the sound would remain stereo, but won't be so clear. */
	chip_write(chip, TDA9874A_NLELR, 0x14); /* default */
	chip_write(chip, TDA9874A_NUELR, 0x50); /* default */

	if(tda9874a_dic == 0x11) {
		chip_write(chip, TDA9874A_AMCONR, 0xf9);
		chip_write(chip, TDA9874A_SDACOSR, (tda9874a_mode) ? 0x81:0x80);
		chip_write(chip, TDA9874A_AOSR, 0x80);
		chip_write(chip, TDA9874A_MDACOSR, (tda9874a_mode) ? 0x82:0x80);
		chip_write(chip, TDA9874A_ESP, tda9874a_ESP);
	} else { /* dic == 0x07 */
		chip_write(chip, TDA9874A_AMCONR, 0xfb);
		chip_write(chip, TDA9874A_SDACOSR, (tda9874a_mode) ? 0x81:0x80);
870
		chip_write(chip, TDA9874A_AOSR, 0x00); /* or 0x10 */
L
Linus Torvalds 已提交
871
	}
872
	v4l2_dbg(1, debug, sd, "tda9874a_setup(): %s [0x%02X].\n",
L
Linus Torvalds 已提交
873 874 875 876 877 878
		tda9874a_modelist[tda9874a_STD].name,tda9874a_STD);
	return 1;
}

static int tda9874a_getmode(struct CHIPSTATE *chip)
{
879
	struct v4l2_subdev *sd = &chip->sd;
L
Linus Torvalds 已提交
880 881 882
	int dsr,nsr,mode;
	int necr; /* just for debugging */

883
	mode = V4L2_TUNER_MODE_MONO;
L
Linus Torvalds 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

	if(-1 == (dsr = chip_read2(chip,TDA9874A_DSR)))
		return mode;
	if(-1 == (nsr = chip_read2(chip,TDA9874A_NSR)))
		return mode;
	if(-1 == (necr = chip_read2(chip,TDA9874A_NECR)))
		return mode;

	/* need to store dsr/nsr somewhere */
	chip->shadow.bytes[MAXREGS-2] = dsr;
	chip->shadow.bytes[MAXREGS-1] = nsr;

	if(tda9874a_mode) {
		/* Note: DSR.RSSF and DSR.AMSTAT bits are also checked.
		 * If NICAM auto-muting is enabled, DSR.AMSTAT=1 indicates
		 * that sound has (temporarily) switched from NICAM to
		 * mono FM (or AM) on 1st sound carrier due to high NICAM bit
		 * error count. So in fact there is no stereo in this case :-(
902
		 * But changing the mode to V4L2_TUNER_MODE_MONO would switch
L
Linus Torvalds 已提交
903 904 905
		 * external 4052 multiplexer in audio_hook().
		 */
		if(nsr & 0x02) /* NSR.S/MB=1 */
906
			mode |= V4L2_TUNER_MODE_STEREO;
L
Linus Torvalds 已提交
907
		if(nsr & 0x01) /* NSR.D/SB=1 */
908
			mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
L
Linus Torvalds 已提交
909 910
	} else {
		if(dsr & 0x02) /* DSR.IDSTE=1 */
911
			mode |= V4L2_TUNER_MODE_STEREO;
L
Linus Torvalds 已提交
912
		if(dsr & 0x04) /* DSR.IDDUA=1 */
913
			mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
L
Linus Torvalds 已提交
914 915
	}

916
	v4l2_dbg(1, debug, sd, "tda9874a_getmode(): DSR=0x%X, NSR=0x%X, NECR=0x%X, return: %d.\n",
L
Linus Torvalds 已提交
917 918 919 920 921 922
		 dsr, nsr, necr, mode);
	return mode;
}

static void tda9874a_setmode(struct CHIPSTATE *chip, int mode)
{
923 924
	struct v4l2_subdev *sd = &chip->sd;

L
Linus Torvalds 已提交
925 926
	/* Disable/enable NICAM auto-muting (based on DSR.RSSF status bit). */
	/* If auto-muting is disabled, we can hear a signal of degrading quality. */
927
	if (tda9874a_mode) {
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
		if(chip->shadow.bytes[MAXREGS-2] & 0x20) /* DSR.RSSF=1 */
			tda9874a_NCONR &= 0xfe; /* enable */
		else
			tda9874a_NCONR |= 0x01; /* disable */
		chip_write(chip, TDA9874A_NCONR, tda9874a_NCONR);
	}

	/* Note: TDA9874A supports automatic FM dematrixing (FMMR register)
	 * and has auto-select function for audio output (AOSR register).
	 * Old TDA9874H doesn't support these features.
	 * TDA9874A also has additional mono output pin (OUTM), which
	 * on same (all?) tv-cards is not used, anyway (as well as MONOIN).
	 */
	if(tda9874a_dic == 0x11) {
		int aosr = 0x80;
		int mdacosr = (tda9874a_mode) ? 0x82:0x80;

		switch(mode) {
946 947
		case V4L2_TUNER_MODE_MONO:
		case V4L2_TUNER_MODE_STEREO:
L
Linus Torvalds 已提交
948
			break;
949
		case V4L2_TUNER_MODE_LANG1:
L
Linus Torvalds 已提交
950 951 952
			aosr = 0x80; /* auto-select, dual A/A */
			mdacosr = (tda9874a_mode) ? 0x82:0x80;
			break;
953
		case V4L2_TUNER_MODE_LANG2:
L
Linus Torvalds 已提交
954 955 956 957 958 959 960 961 962 963
			aosr = 0xa0; /* auto-select, dual B/B */
			mdacosr = (tda9874a_mode) ? 0x83:0x81;
			break;
		default:
			chip->mode = 0;
			return;
		}
		chip_write(chip, TDA9874A_AOSR, aosr);
		chip_write(chip, TDA9874A_MDACOSR, mdacosr);

964
		v4l2_dbg(1, debug, sd, "tda9874a_setmode(): req. mode %d; AOSR=0x%X, MDACOSR=0x%X.\n",
L
Linus Torvalds 已提交
965 966 967 968 969 970
			mode, aosr, mdacosr);

	} else { /* dic == 0x07 */
		int fmmr,aosr;

		switch(mode) {
971
		case V4L2_TUNER_MODE_MONO:
L
Linus Torvalds 已提交
972 973 974
			fmmr = 0x00; /* mono */
			aosr = 0x10; /* A/A */
			break;
975
		case V4L2_TUNER_MODE_STEREO:
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983
			if(tda9874a_mode) {
				fmmr = 0x00;
				aosr = 0x00; /* handled by NICAM auto-mute */
			} else {
				fmmr = (tda9874a_ESP == 1) ? 0x05 : 0x04; /* stereo */
				aosr = 0x00;
			}
			break;
984
		case V4L2_TUNER_MODE_LANG1:
L
Linus Torvalds 已提交
985 986 987
			fmmr = 0x02; /* dual */
			aosr = 0x10; /* dual A/A */
			break;
988
		case V4L2_TUNER_MODE_LANG2:
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996 997 998
			fmmr = 0x02; /* dual */
			aosr = 0x20; /* dual B/B */
			break;
		default:
			chip->mode = 0;
			return;
		}
		chip_write(chip, TDA9874A_FMMR, fmmr);
		chip_write(chip, TDA9874A_AOSR, aosr);

999
		v4l2_dbg(1, debug, sd, "tda9874a_setmode(): req. mode %d; FMMR=0x%X, AOSR=0x%X.\n",
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005
			mode, fmmr, aosr);
	}
}

static int tda9874a_checkit(struct CHIPSTATE *chip)
{
1006
	struct v4l2_subdev *sd = &chip->sd;
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013
	int dic,sic;	/* device id. and software id. codes */

	if(-1 == (dic = chip_read2(chip,TDA9874A_DIC)))
		return 0;
	if(-1 == (sic = chip_read2(chip,TDA9874A_SIC)))
		return 0;

1014
	v4l2_dbg(1, debug, sd, "tda9874a_checkit(): DIC=0x%X, SIC=0x%X.\n", dic, sic);
L
Linus Torvalds 已提交
1015 1016

	if((dic == 0x11)||(dic == 0x07)) {
1017
		v4l2_info(sd, "found tda9874%s.\n", (dic == 0x11) ? "a" : "h");
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		tda9874a_dic = dic;	/* remember device id. */
		return 1;
	}
	return 0;	/* not found */
}

static int tda9874a_initialize(struct CHIPSTATE *chip)
{
	if (tda9874a_SIF > 2)
		tda9874a_SIF = 1;
1028
	if (tda9874a_STD >= ARRAY_SIZE(tda9874a_modelist))
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
		tda9874a_STD = 0;
	if(tda9874a_AMSEL > 1)
		tda9874a_AMSEL = 0;

	if(tda9874a_SIF == 1)
		tda9874a_GCONR = 0xc0;	/* sound IF input 1 */
	else
		tda9874a_GCONR = 0xc1;	/* sound IF input 2 */

	tda9874a_ESP = tda9874a_STD;
	tda9874a_mode = (tda9874a_STD < 5) ? 0 : 1;

	if(tda9874a_AMSEL == 0)
		tda9874a_NCONR = 0x01; /* auto-mute: analog mono input */
	else
		tda9874a_NCONR = 0x05; /* auto-mute: 1st carrier FM or AM */

	tda9874a_setup(chip);
	return 0;
}


/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for tea6420                */

#define TEA6300_VL         0x00  /* volume left */
#define TEA6300_VR         0x01  /* volume right */
#define TEA6300_BA         0x02  /* bass */
#define TEA6300_TR         0x03  /* treble */
#define TEA6300_FA         0x04  /* fader control */
#define TEA6300_S          0x05  /* switch register */
1060
				 /* values for those registers: */
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
#define TEA6300_S_SA       0x01  /* stereo A input */
#define TEA6300_S_SB       0x02  /* stereo B */
#define TEA6300_S_SC       0x04  /* stereo C */
#define TEA6300_S_GMU      0x80  /* general mute */

#define TEA6320_V          0x00  /* volume (0-5)/loudness off (6)/zero crossing mute(7) */
#define TEA6320_FFR        0x01  /* fader front right (0-5) */
#define TEA6320_FFL        0x02  /* fader front left (0-5) */
#define TEA6320_FRR        0x03  /* fader rear right (0-5) */
#define TEA6320_FRL        0x04  /* fader rear left (0-5) */
#define TEA6320_BA         0x05  /* bass (0-4) */
#define TEA6320_TR         0x06  /* treble (0-4) */
#define TEA6320_S          0x07  /* switch register */
1074
				 /* values for those registers: */
L
Linus Torvalds 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
#define TEA6320_S_SA       0x07  /* stereo A input */
#define TEA6320_S_SB       0x06  /* stereo B */
#define TEA6320_S_SC       0x05  /* stereo C */
#define TEA6320_S_SD       0x04  /* stereo D */
#define TEA6320_S_GMU      0x80  /* general mute */

#define TEA6420_S_SA       0x00  /* stereo A input */
#define TEA6420_S_SB       0x01  /* stereo B */
#define TEA6420_S_SC       0x02  /* stereo C */
#define TEA6420_S_SD       0x03  /* stereo D */
#define TEA6420_S_SE       0x04  /* stereo E */
#define TEA6420_S_GMU      0x05  /* general mute */

static int tea6300_shift10(int val) { return val >> 10; }
static int tea6300_shift12(int val) { return val >> 12; }

/* Assumes 16bit input (values 0x3f to 0x0c are unique, values less than */
/* 0x0c mirror those immediately higher) */
static int tea6320_volume(int val) { return (val / (65535/(63-12)) + 12) & 0x3f; }
static int tea6320_shift11(int val) { return val >> 11; }
static int tea6320_initialize(struct CHIPSTATE * chip)
{
	chip_write(chip, TEA6320_FFR, 0x3f);
	chip_write(chip, TEA6320_FFL, 0x3f);
	chip_write(chip, TEA6320_FRR, 0x3f);
	chip_write(chip, TEA6320_FRL, 0x3f);

	return 0;
}


/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for tda8425                */

#define TDA8425_VL         0x00  /* volume left */
#define TDA8425_VR         0x01  /* volume right */
#define TDA8425_BA         0x02  /* bass */
#define TDA8425_TR         0x03  /* treble */
#define TDA8425_S1         0x08  /* switch functions */
1114
				 /* values for those registers: */
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
#define TDA8425_S1_OFF     0xEE  /* audio off (mute on) */
#define TDA8425_S1_CH1     0xCE  /* audio channel 1 (mute off) - "linear stereo" mode */
#define TDA8425_S1_CH2     0xCF  /* audio channel 2 (mute off) - "linear stereo" mode */
#define TDA8425_S1_MU      0x20  /* mute bit */
#define TDA8425_S1_STEREO  0x18  /* stereo bits */
#define TDA8425_S1_STEREO_SPATIAL 0x18 /* spatial stereo */
#define TDA8425_S1_STEREO_LINEAR  0x08 /* linear stereo */
#define TDA8425_S1_STEREO_PSEUDO  0x10 /* pseudo stereo */
#define TDA8425_S1_STEREO_MONO    0x00 /* forced mono */
#define TDA8425_S1_ML      0x06        /* language selector */
#define TDA8425_S1_ML_SOUND_A 0x02     /* sound a */
#define TDA8425_S1_ML_SOUND_B 0x04     /* sound b */
#define TDA8425_S1_ML_STEREO  0x06     /* stereo */
#define TDA8425_S1_IS      0x01        /* channel selector */


static int tda8425_shift10(int val) { return (val >> 10) | 0xc0; }
static int tda8425_shift12(int val) { return (val >> 12) | 0xf0; }

static int tda8425_initialize(struct CHIPSTATE *chip)
{
1136
	struct CHIPDESC *desc = chip->desc;
1137
	struct i2c_client *c = v4l2_get_subdevdata(&chip->sd);
1138 1139
	int inputmap[4] = { /* tuner	*/ TDA8425_S1_CH2, /* radio  */ TDA8425_S1_CH1,
			    /* extern	*/ TDA8425_S1_CH1, /* intern */ TDA8425_S1_OFF};
L
Linus Torvalds 已提交
1140

1141 1142
	if (c->adapter->id == I2C_HW_B_RIVA)
		memcpy(desc->inputmap, inputmap, sizeof(inputmap));
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148 1149
	return 0;
}

static void tda8425_setmode(struct CHIPSTATE *chip, int mode)
{
	int s1 = chip->shadow.bytes[TDA8425_S1+1] & 0xe1;

1150
	if (mode & V4L2_TUNER_MODE_LANG1) {
L
Linus Torvalds 已提交
1151 1152 1153
		s1 |= TDA8425_S1_ML_SOUND_A;
		s1 |= TDA8425_S1_STEREO_PSEUDO;

1154
	} else if (mode & V4L2_TUNER_MODE_LANG2) {
L
Linus Torvalds 已提交
1155 1156 1157 1158 1159 1160
		s1 |= TDA8425_S1_ML_SOUND_B;
		s1 |= TDA8425_S1_STEREO_PSEUDO;

	} else {
		s1 |= TDA8425_S1_ML_STEREO;

1161
		if (mode & V4L2_TUNER_MODE_MONO)
L
Linus Torvalds 已提交
1162
			s1 |= TDA8425_S1_STEREO_MONO;
1163
		if (mode & V4L2_TUNER_MODE_STEREO)
L
Linus Torvalds 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
			s1 |= TDA8425_S1_STEREO_SPATIAL;
	}
	chip_write(chip,TDA8425_S1,s1);
}


/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for pic16c54 (PV951)       */

/* the registers of 16C54, I2C sub address. */
#define PIC16C54_REG_KEY_CODE     0x01	       /* Not use. */
#define PIC16C54_REG_MISC         0x02

/* bit definition of the RESET register, I2C data. */
#define PIC16C54_MISC_RESET_REMOTE_CTL 0x01 /* bit 0, Reset to receive the key */
1179
					    /*        code of remote controller */
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
#define PIC16C54_MISC_MTS_MAIN         0x02 /* bit 1 */
#define PIC16C54_MISC_MTS_SAP          0x04 /* bit 2 */
#define PIC16C54_MISC_MTS_BOTH         0x08 /* bit 3 */
#define PIC16C54_MISC_SND_MUTE         0x10 /* bit 4, Mute Audio(Line-in and Tuner) */
#define PIC16C54_MISC_SND_NOTMUTE      0x20 /* bit 5 */
#define PIC16C54_MISC_SWITCH_TUNER     0x40 /* bit 6	, Switch to Line-in */
#define PIC16C54_MISC_SWITCH_LINE      0x80 /* bit 7	, Switch to Tuner */

/* ---------------------------------------------------------------------- */
/* audio chip descriptions - defines+functions for TA8874Z                */

1191
/* write 1st byte */
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200
#define TA8874Z_LED_STE	0x80
#define TA8874Z_LED_BIL	0x40
#define TA8874Z_LED_EXT	0x20
#define TA8874Z_MONO_SET	0x10
#define TA8874Z_MUTE	0x08
#define TA8874Z_F_MONO	0x04
#define TA8874Z_MODE_SUB	0x02
#define TA8874Z_MODE_MAIN	0x01

1201 1202
/* write 2nd byte */
/*#define TA8874Z_TI	0x80  */ /* test mode */
L
Linus Torvalds 已提交
1203 1204 1205
#define TA8874Z_SEPARATION	0x3f
#define TA8874Z_SEPARATION_DEFAULT	0x10

1206
/* read */
L
Linus Torvalds 已提交
1207 1208 1209 1210
#define TA8874Z_B1	0x80
#define TA8874Z_B0	0x40
#define TA8874Z_CHAG_FLAG	0x20

1211 1212 1213 1214 1215 1216
/*
 *        B1 B0
 * mono    L  H
 * stereo  L  L
 * BIL     H  L
 */
L
Linus Torvalds 已提交
1217 1218 1219 1220 1221
static int ta8874z_getmode(struct CHIPSTATE *chip)
{
	int val, mode;

	val = chip_read(chip);
1222
	mode = V4L2_TUNER_MODE_MONO;
L
Linus Torvalds 已提交
1223
	if (val & TA8874Z_B1){
1224
		mode |= V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
L
Linus Torvalds 已提交
1225
	}else if (!(val & TA8874Z_B0)){
1226
		mode |= V4L2_TUNER_MODE_STEREO;
L
Linus Torvalds 已提交
1227
	}
1228
	/* v4l_dbg(1, debug, chip->c, "ta8874z_getmode(): raw chip read: 0x%02x, return: 0x%02x\n", val, mode); */
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	return mode;
}

static audiocmd ta8874z_stereo = { 2, {0, TA8874Z_SEPARATION_DEFAULT}};
static audiocmd ta8874z_mono = {2, { TA8874Z_MONO_SET, TA8874Z_SEPARATION_DEFAULT}};
static audiocmd ta8874z_main = {2, { 0, TA8874Z_SEPARATION_DEFAULT}};
static audiocmd ta8874z_sub = {2, { TA8874Z_MODE_SUB, TA8874Z_SEPARATION_DEFAULT}};

static void ta8874z_setmode(struct CHIPSTATE *chip, int mode)
{
1239
	struct v4l2_subdev *sd = &chip->sd;
L
Linus Torvalds 已提交
1240 1241
	int update = 1;
	audiocmd *t = NULL;
1242 1243

	v4l2_dbg(1, debug, sd, "ta8874z_setmode(): mode: 0x%02x\n", mode);
L
Linus Torvalds 已提交
1244 1245

	switch(mode){
1246
	case V4L2_TUNER_MODE_MONO:
L
Linus Torvalds 已提交
1247 1248
		t = &ta8874z_mono;
		break;
1249
	case V4L2_TUNER_MODE_STEREO:
L
Linus Torvalds 已提交
1250 1251
		t = &ta8874z_stereo;
		break;
1252
	case V4L2_TUNER_MODE_LANG1:
L
Linus Torvalds 已提交
1253 1254
		t = &ta8874z_main;
		break;
1255
	case V4L2_TUNER_MODE_LANG2:
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
		t = &ta8874z_sub;
		break;
	default:
		update = 0;
	}

	if(update)
		chip_cmd(chip, "TA8874Z", t);
}

static int ta8874z_checkit(struct CHIPSTATE *chip)
{
	int rc;
	rc = chip_read(chip);
	return ((rc & 0x1f) == 0x1f) ? 1 : 0;
}

/* ---------------------------------------------------------------------- */
/* audio chip descriptions - struct CHIPDESC                              */

/* insmod options to enable/disable individual audio chips */
A
Adrian Bunk 已提交
1277 1278 1279 1280 1281 1282
static int tda8425  = 1;
static int tda9840  = 1;
static int tda9850  = 1;
static int tda9855  = 1;
static int tda9873  = 1;
static int tda9874a = 1;
1283 1284
static int tea6300;	/* default 0 - address clash with msp34xx */
static int tea6320;	/* default 0 - address clash with msp34xx */
A
Adrian Bunk 已提交
1285 1286
static int tea6420  = 1;
static int pic16c54 = 1;
1287
static int ta8874z;	/* default 0 - address clash with tda9840 */
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

module_param(tda8425, int, 0444);
module_param(tda9840, int, 0444);
module_param(tda9850, int, 0444);
module_param(tda9855, int, 0444);
module_param(tda9873, int, 0444);
module_param(tda9874a, int, 0444);
module_param(tea6300, int, 0444);
module_param(tea6320, int, 0444);
module_param(tea6420, int, 0444);
module_param(pic16c54, int, 0444);
module_param(ta8874z, int, 0444);

static struct CHIPDESC chiplist[] = {
	{
		.name       = "tda9840",
		.insmodopt  = &tda9840,
1305 1306
		.addr_lo    = I2C_ADDR_TDA9840 >> 1,
		.addr_hi    = I2C_ADDR_TDA9840 >> 1,
L
Linus Torvalds 已提交
1307
		.registers  = 5,
1308
		.flags      = CHIP_NEED_CHECKMODE,
L
Linus Torvalds 已提交
1309

1310
		/* callbacks */
1311
		.checkit    = tda9840_checkit,
L
Linus Torvalds 已提交
1312 1313 1314
		.getmode    = tda9840_getmode,
		.setmode    = tda9840_setmode,

1315
		.init       = { 2, { TDA9840_TEST, TDA9840_TEST_INT1SN
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320
				/* ,TDA9840_SW, TDA9840_MONO */} }
	},
	{
		.name       = "tda9873h",
		.insmodopt  = &tda9873,
1321 1322
		.addr_lo    = I2C_ADDR_TDA985x_L >> 1,
		.addr_hi    = I2C_ADDR_TDA985x_H >> 1,
L
Linus Torvalds 已提交
1323
		.registers  = 3,
1324
		.flags      = CHIP_HAS_INPUTSEL | CHIP_NEED_CHECKMODE,
L
Linus Torvalds 已提交
1325

1326 1327
		/* callbacks */
		.checkit    = tda9873_checkit,
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333
		.getmode    = tda9873_getmode,
		.setmode    = tda9873_setmode,

		.init       = { 4, { TDA9873_SW, 0xa4, 0x06, 0x03 } },
		.inputreg   = TDA9873_SW,
		.inputmute  = TDA9873_MUTE | TDA9873_AUTOMUTE,
1334
		.inputmap   = {0xa0, 0xa2, 0xa0, 0xa0},
L
Linus Torvalds 已提交
1335 1336 1337 1338 1339 1340
		.inputmask  = TDA9873_INP_MASK|TDA9873_MUTE|TDA9873_AUTOMUTE,

	},
	{
		.name       = "tda9874h/a",
		.insmodopt  = &tda9874a,
1341 1342
		.addr_lo    = I2C_ADDR_TDA9874 >> 1,
		.addr_hi    = I2C_ADDR_TDA9874 >> 1,
1343
		.flags      = CHIP_NEED_CHECKMODE,
L
Linus Torvalds 已提交
1344

1345 1346 1347
		/* callbacks */
		.initialize = tda9874a_initialize,
		.checkit    = tda9874a_checkit,
L
Linus Torvalds 已提交
1348 1349 1350 1351 1352 1353
		.getmode    = tda9874a_getmode,
		.setmode    = tda9874a_setmode,
	},
	{
		.name       = "tda9850",
		.insmodopt  = &tda9850,
1354 1355
		.addr_lo    = I2C_ADDR_TDA985x_L >> 1,
		.addr_hi    = I2C_ADDR_TDA985x_H >> 1,
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		.registers  = 11,

		.getmode    = tda985x_getmode,
		.setmode    = tda985x_setmode,

		.init       = { 8, { TDA9850_C4, 0x08, 0x08, TDA985x_STEREO, 0x07, 0x10, 0x10, 0x03 } }
	},
	{
		.name       = "tda9855",
		.insmodopt  = &tda9855,
1366 1367
		.addr_lo    = I2C_ADDR_TDA985x_L >> 1,
		.addr_hi    = I2C_ADDR_TDA985x_H >> 1,
L
Linus Torvalds 已提交
1368 1369 1370 1371 1372 1373 1374
		.registers  = 11,
		.flags      = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE,

		.leftreg    = TDA9855_VL,
		.rightreg   = TDA9855_VR,
		.bassreg    = TDA9855_BA,
		.treblereg  = TDA9855_TR,
1375 1376

		/* callbacks */
L
Linus Torvalds 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
		.volfunc    = tda9855_volume,
		.bassfunc   = tda9855_bass,
		.treblefunc = tda9855_treble,
		.getmode    = tda985x_getmode,
		.setmode    = tda985x_setmode,

		.init       = { 12, { 0, 0x6f, 0x6f, 0x0e, 0x07<<1, 0x8<<2,
				    TDA9855_MUTE | TDA9855_AVL | TDA9855_LOUD | TDA9855_INT,
				    TDA985x_STEREO | TDA9855_LINEAR | TDA9855_TZCM | TDA9855_VZCM,
				    0x07, 0x10, 0x10, 0x03 }}
	},
	{
		.name       = "tea6300",
		.insmodopt  = &tea6300,
1391 1392
		.addr_lo    = I2C_ADDR_TEA6300 >> 1,
		.addr_hi    = I2C_ADDR_TEA6300 >> 1,
L
Linus Torvalds 已提交
1393 1394 1395 1396 1397 1398 1399
		.registers  = 6,
		.flags      = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,

		.leftreg    = TEA6300_VR,
		.rightreg   = TEA6300_VL,
		.bassreg    = TEA6300_BA,
		.treblereg  = TEA6300_TR,
1400 1401

		/* callbacks */
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
		.volfunc    = tea6300_shift10,
		.bassfunc   = tea6300_shift12,
		.treblefunc = tea6300_shift12,

		.inputreg   = TEA6300_S,
		.inputmap   = { TEA6300_S_SA, TEA6300_S_SB, TEA6300_S_SC },
		.inputmute  = TEA6300_S_GMU,
	},
	{
		.name       = "tea6320",
		.insmodopt  = &tea6320,
1413 1414
		.addr_lo    = I2C_ADDR_TEA6300 >> 1,
		.addr_hi    = I2C_ADDR_TEA6300 >> 1,
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420 1421
		.registers  = 8,
		.flags      = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,

		.leftreg    = TEA6320_V,
		.rightreg   = TEA6320_V,
		.bassreg    = TEA6320_BA,
		.treblereg  = TEA6320_TR,
1422 1423 1424

		/* callbacks */
		.initialize = tea6320_initialize,
L
Linus Torvalds 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
		.volfunc    = tea6320_volume,
		.bassfunc   = tea6320_shift11,
		.treblefunc = tea6320_shift11,

		.inputreg   = TEA6320_S,
		.inputmap   = { TEA6320_S_SA, TEA6420_S_SB, TEA6300_S_SC, TEA6320_S_SD },
		.inputmute  = TEA6300_S_GMU,
	},
	{
		.name       = "tea6420",
		.insmodopt  = &tea6420,
1436 1437
		.addr_lo    = I2C_ADDR_TEA6420 >> 1,
		.addr_hi    = I2C_ADDR_TEA6420 >> 1,
L
Linus Torvalds 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
		.registers  = 1,
		.flags      = CHIP_HAS_INPUTSEL,

		.inputreg   = -1,
		.inputmap   = { TEA6420_S_SA, TEA6420_S_SB, TEA6420_S_SC },
		.inputmute  = TEA6300_S_GMU,
	},
	{
		.name       = "tda8425",
		.insmodopt  = &tda8425,
1448 1449
		.addr_lo    = I2C_ADDR_TDA8425 >> 1,
		.addr_hi    = I2C_ADDR_TDA8425 >> 1,
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455 1456
		.registers  = 9,
		.flags      = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,

		.leftreg    = TDA8425_VL,
		.rightreg   = TDA8425_VR,
		.bassreg    = TDA8425_BA,
		.treblereg  = TDA8425_TR,
1457 1458 1459

		/* callbacks */
		.initialize = tda8425_initialize,
L
Linus Torvalds 已提交
1460 1461 1462
		.volfunc    = tda8425_shift10,
		.bassfunc   = tda8425_shift12,
		.treblefunc = tda8425_shift12,
1463
		.setmode    = tda8425_setmode,
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472

		.inputreg   = TDA8425_S1,
		.inputmap   = { TDA8425_S1_CH1, TDA8425_S1_CH1, TDA8425_S1_CH1 },
		.inputmute  = TDA8425_S1_OFF,

	},
	{
		.name       = "pic16c54 (PV951)",
		.insmodopt  = &pic16c54,
1473 1474
		.addr_lo    = I2C_ADDR_PIC16C54 >> 1,
		.addr_hi    = I2C_ADDR_PIC16C54>> 1,
L
Linus Torvalds 已提交
1475 1476 1477 1478 1479 1480 1481
		.registers  = 2,
		.flags      = CHIP_HAS_INPUTSEL,

		.inputreg   = PIC16C54_REG_MISC,
		.inputmap   = {PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_TUNER,
			     PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_LINE,
			     PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_LINE,
1482
			     PIC16C54_MISC_SND_MUTE},
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488
		.inputmute  = PIC16C54_MISC_SND_MUTE,
	},
	{
		.name       = "ta8874z",
		.checkit    = ta8874z_checkit,
		.insmodopt  = &ta8874z,
1489 1490
		.addr_lo    = I2C_ADDR_TDA9840 >> 1,
		.addr_hi    = I2C_ADDR_TDA9840 >> 1,
L
Linus Torvalds 已提交
1491
		.registers  = 2,
1492
		.flags      = CHIP_NEED_CHECKMODE,
L
Linus Torvalds 已提交
1493

1494
		/* callbacks */
L
Linus Torvalds 已提交
1495 1496 1497
		.getmode    = ta8874z_getmode,
		.setmode    = ta8874z_setmode,

1498
		.init       = {2, { TA8874Z_MONO_SET, TA8874Z_SEPARATION_DEFAULT}},
L
Linus Torvalds 已提交
1499 1500 1501 1502 1503 1504 1505
	},
	{ .name = NULL } /* EOF */
};


/* ---------------------------------------------------------------------- */

1506
static int tvaudio_g_ctrl(struct v4l2_subdev *sd,
1507 1508
			    struct v4l2_control *ctrl)
{
1509
	struct CHIPSTATE *chip = to_state(sd);
1510
	struct CHIPDESC *desc = chip->desc;
1511 1512 1513

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
1514 1515
		if (!(desc->flags & CHIP_HAS_INPUTSEL))
			break;
1516 1517 1518
		ctrl->value=chip->muted;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
R
Roel Kluin 已提交
1519
		if (!(desc->flags & CHIP_HAS_VOLUME))
1520 1521 1522 1523 1524 1525
			break;
		ctrl->value = max(chip->left,chip->right);
		return 0;
	case V4L2_CID_AUDIO_BALANCE:
	{
		int volume;
R
Roel Kluin 已提交
1526
		if (!(desc->flags & CHIP_HAS_VOLUME))
1527 1528 1529 1530 1531 1532 1533 1534 1535
			break;
		volume = max(chip->left,chip->right);
		if (volume)
			ctrl->value=(32768*min(chip->left,chip->right))/volume;
		else
			ctrl->value=32768;
		return 0;
	}
	case V4L2_CID_AUDIO_BASS:
1536
		if (!(desc->flags & CHIP_HAS_BASSTREBLE))
1537 1538 1539 1540
			break;
		ctrl->value = chip->bass;
		return 0;
	case V4L2_CID_AUDIO_TREBLE:
1541 1542
		if (!(desc->flags & CHIP_HAS_BASSTREBLE))
			break;
1543 1544 1545 1546 1547 1548
		ctrl->value = chip->treble;
		return 0;
	}
	return -EINVAL;
}

1549
static int tvaudio_s_ctrl(struct v4l2_subdev *sd,
1550
			    struct v4l2_control *ctrl)
1551
{
1552
	struct CHIPSTATE *chip = to_state(sd);
1553
	struct CHIPDESC *desc = chip->desc;
1554 1555 1556

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
1557 1558 1559
		if (!(desc->flags & CHIP_HAS_INPUTSEL))
			break;

1560 1561 1562 1563 1564 1565 1566 1567
		if (ctrl->value < 0 || ctrl->value >= 2)
			return -ERANGE;
		chip->muted = ctrl->value;
		if (chip->muted)
			chip_write_masked(chip,desc->inputreg,desc->inputmute,desc->inputmask);
		else
			chip_write_masked(chip,desc->inputreg,
					desc->inputmap[chip->input],desc->inputmask);
1568 1569 1570 1571 1572
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
	{
		int volume,balance;

R
Roel Kluin 已提交
1573
		if (!(desc->flags & CHIP_HAS_VOLUME))
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
			break;

		volume = max(chip->left,chip->right);
		if (volume)
			balance=(32768*min(chip->left,chip->right))/volume;
		else
			balance=32768;

		volume=ctrl->value;
		chip->left = (min(65536 - balance,32768) * volume) / 32768;
		chip->right = (min(balance,volume *(__u16)32768)) / 32768;

		chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
		chip_write(chip,desc->rightreg,desc->volfunc(chip->right));

		return 0;
1590
	}
1591 1592 1593
	case V4L2_CID_AUDIO_BALANCE:
	{
		int volume, balance;
R
Roel Kluin 已提交
1594
		if (!(desc->flags & CHIP_HAS_VOLUME))
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
			break;

		volume = max(chip->left,chip->right);
		balance = ctrl->value;

		chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
		chip_write(chip,desc->rightreg,desc->volfunc(chip->right));

		return 0;
	}
	case V4L2_CID_AUDIO_BASS:
1606
		if (!(desc->flags & CHIP_HAS_BASSTREBLE))
1607 1608 1609 1610 1611 1612
			break;
		chip->bass = ctrl->value;
		chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass));

		return 0;
	case V4L2_CID_AUDIO_TREBLE:
1613 1614
		if (!(desc->flags & CHIP_HAS_BASSTREBLE))
			break;
1615 1616 1617 1618 1619 1620
		chip->treble = ctrl->value;
		chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble));

		return 0;
	}
	return -EINVAL;
1621 1622 1623
}


L
Linus Torvalds 已提交
1624 1625 1626
/* ---------------------------------------------------------------------- */
/* video4linux interface                                                  */

1627
static int tvaudio_s_radio(struct v4l2_subdev *sd)
L
Linus Torvalds 已提交
1628
{
1629
	struct CHIPSTATE *chip = to_state(sd);
L
Linus Torvalds 已提交
1630

1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
	chip->radio = 1;
	chip->watch_stereo = 0;
	/* del_timer(&chip->wt); */
	return 0;
}

static int tvaudio_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
{
	struct CHIPSTATE *chip = to_state(sd);
	struct CHIPDESC *desc = chip->desc;

	switch (qc->id) {
	case V4L2_CID_AUDIO_MUTE:
1644 1645 1646
		if (desc->flags & CHIP_HAS_INPUTSEL)
			return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
		break;
1647
	case V4L2_CID_AUDIO_VOLUME:
1648 1649 1650
		if (desc->flags & CHIP_HAS_VOLUME)
			return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
		break;
1651
	case V4L2_CID_AUDIO_BALANCE:
1652 1653
		if (desc->flags & CHIP_HAS_VOLUME)
			return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
1654 1655 1656
		break;
	case V4L2_CID_AUDIO_BASS:
	case V4L2_CID_AUDIO_TREBLE:
1657 1658
		if (desc->flags & CHIP_HAS_BASSTREBLE)
			return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
1659 1660
		break;
	default:
1661
		break;
1662
	}
1663
	return -EINVAL;
1664 1665 1666 1667 1668 1669
}

static int tvaudio_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *rt)
{
	struct CHIPSTATE *chip = to_state(sd);
	struct CHIPDESC *desc = chip->desc;
L
Linus Torvalds 已提交
1670

1671 1672 1673
	if (!(desc->flags & CHIP_HAS_INPUTSEL))
		return 0;
	if (rt->input >= 4)
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
		return -EINVAL;
	/* There are four inputs: tuner, radio, extern and intern. */
	chip->input = rt->input;
	if (chip->muted)
		return 0;
	chip_write_masked(chip, desc->inputreg,
			desc->inputmap[chip->input], desc->inputmask);
	return 0;
}

static int tvaudio_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
{
	struct CHIPSTATE *chip = to_state(sd);
	struct CHIPDESC *desc = chip->desc;
	int mode = 0;

1690 1691
	if (!desc->setmode)
		return 0;
1692 1693
	if (chip->radio)
		return 0;
1694

1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
	switch (vt->audmode) {
	case V4L2_TUNER_MODE_MONO:
	case V4L2_TUNER_MODE_STEREO:
	case V4L2_TUNER_MODE_LANG1:
	case V4L2_TUNER_MODE_LANG2:
		mode = vt->audmode;
		break;
	case V4L2_TUNER_MODE_LANG1_LANG2:
		mode = V4L2_TUNER_MODE_STEREO;
		break;
	default:
		return -EINVAL;
	}
	chip->audmode = vt->audmode;

1710
	if (mode) {
L
Linus Torvalds 已提交
1711 1712
		chip->watch_stereo = 0;
		/* del_timer(&chip->wt); */
1713 1714
		chip->mode = mode;
		desc->setmode(chip, mode);
L
Linus Torvalds 已提交
1715
	}
1716 1717
	return 0;
}
1718

1719 1720 1721 1722 1723
static int tvaudio_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
{
	struct CHIPSTATE *chip = to_state(sd);
	struct CHIPDESC *desc = chip->desc;
	int mode = V4L2_TUNER_MODE_MONO;
1724

1725 1726
	if (!desc->getmode)
		return 0;
1727 1728
	if (chip->radio)
		return 0;
1729

1730 1731 1732 1733 1734
	vt->audmode = chip->audmode;
	vt->rxsubchans = 0;
	vt->capability = V4L2_TUNER_CAP_STEREO |
		V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;

1735
	mode = desc->getmode(chip);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777

	if (mode & V4L2_TUNER_MODE_MONO)
		vt->rxsubchans |= V4L2_TUNER_SUB_MONO;
	if (mode & V4L2_TUNER_MODE_STEREO)
		vt->rxsubchans |= V4L2_TUNER_SUB_STEREO;
	/* Note: for SAP it should be mono/lang2 or stereo/lang2.
	   When this module is converted fully to v4l2, then this
	   should change for those chips that can detect SAP. */
	if (mode & V4L2_TUNER_MODE_LANG1)
		vt->rxsubchans = V4L2_TUNER_SUB_LANG1 |
			V4L2_TUNER_SUB_LANG2;
	return 0;
}

static int tvaudio_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
{
	struct CHIPSTATE *chip = to_state(sd);

	chip->radio = 0;
	return 0;
}

static int tvaudio_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
{
	struct CHIPSTATE *chip = to_state(sd);
	struct CHIPDESC *desc = chip->desc;

	chip->mode = 0; /* automatic */

	/* For chips that provide getmode and setmode, and doesn't
	   automatically follows the stereo carrier, a kthread is
	   created to set the audio standard. In this case, when then
	   the video channel is changed, tvaudio starts on MONO mode.
	   After waiting for 2 seconds, the kernel thread is called,
	   to follow whatever audio standard is pointed by the
	   audio carrier.
	 */
	if (chip->thread) {
		desc->setmode(chip, V4L2_TUNER_MODE_MONO);
		if (chip->prevmode != V4L2_TUNER_MODE_MONO)
			chip->prevmode = -1; /* reset previous mode */
		mod_timer(&chip->wt, jiffies+msecs_to_jiffies(2000));
1778
	}
1779 1780
	return 0;
}
1781

1782
static int tvaudio_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);

	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVAUDIO, 0);
}

static int tvaudio_command(struct i2c_client *client, unsigned cmd, void *arg)
{
	return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
}

/* ----------------------------------------------------------------------- */

static const struct v4l2_subdev_core_ops tvaudio_core_ops = {
	.g_chip_ident = tvaudio_g_chip_ident,
	.queryctrl = tvaudio_queryctrl,
	.g_ctrl = tvaudio_g_ctrl,
	.s_ctrl = tvaudio_s_ctrl,
};

static const struct v4l2_subdev_tuner_ops tvaudio_tuner_ops = {
	.s_radio = tvaudio_s_radio,
	.s_frequency = tvaudio_s_frequency,
	.s_std = tvaudio_s_std,
	.s_tuner = tvaudio_s_tuner,
	.s_tuner = tvaudio_g_tuner,
};

static const struct v4l2_subdev_audio_ops tvaudio_audio_ops = {
	.s_routing = tvaudio_s_routing,
};

static const struct v4l2_subdev_ops tvaudio_ops = {
	.core = &tvaudio_core_ops,
	.tuner = &tvaudio_tuner_ops,
	.audio = &tvaudio_audio_ops,
};

/* ----------------------------------------------------------------------- */


/* i2c registration                                                       */

static int tvaudio_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	struct CHIPSTATE *chip;
	struct CHIPDESC  *desc;
	struct v4l2_subdev *sd;

	if (debug) {
		printk(KERN_INFO "tvaudio: TV audio decoder + audio/video mux driver\n");
		printk(KERN_INFO "tvaudio: known chips: ");
		for (desc = chiplist; desc->name != NULL; desc++)
			printk("%s%s", (desc == chiplist) ? "" : ", ", desc->name);
		printk("\n");
1838
	}
L
Linus Torvalds 已提交
1839

1840 1841 1842 1843 1844
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;
	sd = &chip->sd;
	v4l2_i2c_subdev_init(sd, client, &tvaudio_ops);
1845

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
	/* find description for the chip */
	v4l2_dbg(1, debug, sd, "chip found @ 0x%x\n", client->addr<<1);
	for (desc = chiplist; desc->name != NULL; desc++) {
		if (0 == *(desc->insmodopt))
			continue;
		if (client->addr < desc->addr_lo ||
		    client->addr > desc->addr_hi)
			continue;
		if (desc->checkit && !desc->checkit(chip))
			continue;
L
Linus Torvalds 已提交
1856 1857
		break;
	}
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
	if (desc->name == NULL) {
		v4l2_dbg(1, debug, sd, "no matching chip description found\n");
		kfree(chip);
		return -EIO;
	}
	v4l2_info(sd, "%s found @ 0x%x (%s)\n", desc->name, client->addr<<1, client->adapter->name);
	if (desc->flags) {
		v4l2_dbg(1, debug, sd, "matches:%s%s%s.\n",
			(desc->flags & CHIP_HAS_VOLUME)     ? " volume"      : "",
			(desc->flags & CHIP_HAS_BASSTREBLE) ? " bass/treble" : "",
			(desc->flags & CHIP_HAS_INPUTSEL)   ? " audiomux"    : "");
	}
1870

1871 1872 1873 1874 1875 1876 1877
	/* fill required data structures */
	if (!id)
		strlcpy(client->name, desc->name, I2C_NAME_SIZE);
	chip->desc = desc;
	chip->shadow.count = desc->registers+1;
	chip->prevmode = -1;
	chip->audmode = V4L2_TUNER_MODE_LANG1;
1878

1879 1880 1881 1882 1883
	/* initialization  */
	if (desc->initialize != NULL)
		desc->initialize(chip);
	else
		chip_cmd(chip, "init", &desc->init);
1884

1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
	if (desc->flags & CHIP_HAS_VOLUME) {
		if (!desc->volfunc) {
			/* This shouldn't be happen. Warn user, but keep working
			   without volume controls
			 */
			v4l2_info(sd, "volume callback undefined!\n");
			desc->flags &= ~CHIP_HAS_VOLUME;
		} else {
			chip->left  = desc->leftinit  ? desc->leftinit  : 65535;
			chip->right = desc->rightinit ? desc->rightinit : 65535;
			chip_write(chip, desc->leftreg,
				   desc->volfunc(chip->left));
			chip_write(chip, desc->rightreg,
				   desc->volfunc(chip->right));
		}
1900
	}
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
	if (desc->flags & CHIP_HAS_BASSTREBLE) {
		if (!desc->bassfunc || !desc->treblefunc) {
			/* This shouldn't be happen. Warn user, but keep working
			   without bass/treble controls
			 */
			v4l2_info(sd, "bass/treble callbacks undefined!\n");
			desc->flags &= ~CHIP_HAS_BASSTREBLE;
		} else {
			chip->treble = desc->trebleinit ?
						desc->trebleinit : 32768;
			chip->bass   = desc->bassinit   ?
						desc->bassinit   : 32768;
			chip_write(chip, desc->bassreg,
				   desc->bassfunc(chip->bass));
			chip_write(chip, desc->treblereg,
				   desc->treblefunc(chip->treble));
L
Linus Torvalds 已提交
1917
		}
1918
	}
1919

1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	chip->thread = NULL;
	if (desc->flags & CHIP_NEED_CHECKMODE) {
		if (!desc->getmode || !desc->setmode) {
			/* This shouldn't be happen. Warn user, but keep working
			   without kthread
			 */
			v4l2_info(sd, "set/get mode callbacks undefined!\n");
			return 0;
		}
		/* start async thread */
		init_timer(&chip->wt);
		chip->wt.function = chip_thread_wake;
		chip->wt.data     = (unsigned long)chip;
		chip->thread = kthread_run(chip_thread, chip, client->name);
		if (IS_ERR(chip->thread)) {
			v4l2_warn(sd, "failed to create kthread\n");
			chip->thread = NULL;
		}
L
Linus Torvalds 已提交
1938 1939 1940 1941
	}
	return 0;
}

1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
static int tvaudio_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct CHIPSTATE *chip = to_state(sd);

	del_timer_sync(&chip->wt);
	if (chip->thread) {
		/* shutdown async thread */
		kthread_stop(chip->thread);
		chip->thread = NULL;
	}

	v4l2_device_unregister_subdev(sd);
	kfree(chip);
	return 0;
}

static int tvaudio_legacy_probe(struct i2c_adapter *adap)
L
Linus Torvalds 已提交
1960
{
1961 1962 1963 1964 1965 1966 1967
	/* don't attach on saa7146 based cards,
	   because dedicated drivers are used */
	if ((adap->id == I2C_HW_SAA7146))
		return 0;
	if (adap->class & I2C_CLASS_TV_ANALOG)
		return 1;
	return 0;
L
Linus Torvalds 已提交
1968 1969
}

1970 1971 1972
/* This driver supports many devices and the idea is to let the driver
   detect which device is present. So rather than listing all supported
   devices here, we pretend to support a single, fake device type. */
1973
static const struct i2c_device_id tvaudio_id[] = {
1974 1975 1976
	{ "tvaudio", 0 },
	{ }
};
1977
MODULE_DEVICE_TABLE(i2c, tvaudio_id);
1978

1979 1980 1981
static struct v4l2_i2c_driver_data v4l2_i2c_data = {
	.name = "tvaudio",
	.driverid = I2C_DRIVERID_TVAUDIO,
1982 1983 1984 1985 1986
	.command = tvaudio_command,
	.probe = tvaudio_probe,
	.remove = tvaudio_remove,
	.legacy_probe = tvaudio_legacy_probe,
	.id_table = tvaudio_id,
1987
};