msp3400-driver.c 30.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * Programming the mspx4xx sound processor family
 *
 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
 *
 * what works and what doesn't:
 *
 *  AM-Mono
 *      Support for Hauppauge cards added (decoding handled by tuner) added by
 *      Frederic Crozat <fcrozat@mail.dotcom.fr>
 *
 *  FM-Mono
 *      should work. The stereo modes are backward compatible to FM-mono,
 *      therefore FM-Mono should be allways available.
 *
 *  FM-Stereo (B/G, used in germany)
 *      should work, with autodetect
 *
 *  FM-Stereo (satellite)
 *      should work, no autodetect (i.e. default is mono, but you can
 *      switch to stereo -- untested)
 *
 *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
 *      should work, with autodetect. Support for NICAM was added by
 *      Pekka Pietikainen <pp@netppl.fi>
 *
 * TODO:
 *   - better SAT support
 *
 * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
 *         using soundcore instead of OSS
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/videodev.h>
#include <linux/videodev2.h>
#include <media/v4l2-common.h>
56
#include <media/tvaudio.h>
57 58 59 60 61 62 63 64 65 66 67 68
#include <linux/kthread.h>
#include <linux/suspend.h>
#include "msp3400.h"

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

MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
MODULE_AUTHOR("Gerd Knorr");
MODULE_LICENSE("GPL");

/* module parameters */
static int opmode   = OPMODE_AUTO;
69 70 71 72
int msp_debug;		 /* msp_debug output */
int msp_once;		 /* no continous stereo monitoring */
int msp_amsound;	 /* hard-wire AM sound at 6.5 Hz (france),
			    the autoscan seems work well only with FM... */
M
 
Mauro Carvalho Chehab 已提交
73
int msp_standard = 1;    /* Override auto detect of audio msp_standard, if needed. */
74
int msp_dolby;
75

M
 
Mauro Carvalho Chehab 已提交
76
int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
77 78 79 80 81 82
					(msp34xxg only) 0x00a0-0x03c0 */

/* read-only */
module_param(opmode,           int, 0444);

/* read-write */
M
 
Mauro Carvalho Chehab 已提交
83 84 85 86 87 88
module_param_named(once,msp_once,                      bool, 0644);
module_param_named(debug,msp_debug,                    int,  0644);
module_param_named(stereo_threshold,msp_stereo_thresh, int,  0644);
module_param_named(standard,msp_standard,              int,  0644);
module_param_named(amsound,msp_amsound,                bool, 0644);
module_param_named(dolby,msp_dolby,                    bool, 0644);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
MODULE_PARM_DESC(once, "No continuous stereo monitoring");
MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
MODULE_PARM_DESC(dolby, "Activates Dolby processsing");

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

/* control subaddress */
#define I2C_MSP_CONTROL 0x00
/* demodulator unit subaddress */
#define I2C_MSP_DEM     0x10
/* DSP unit subaddress */
#define I2C_MSP_DSP     0x12

/* Addresses to scan */
static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
I2C_CLIENT_INSMOD;

/* ----------------------------------------------------------------------- */
/* functions for talking to the MSP3400C Sound processor                   */

int msp_reset(struct i2c_client *client)
{
	/* reset and read revision code */
	static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
	static u8 reset_on[3]  = { I2C_MSP_CONTROL, 0x00, 0x00 };
	static u8 write[3]     = { I2C_MSP_DSP + 1, 0x00, 0x1e };
	u8 read[2];
	struct i2c_msg reset[2] = {
		{ client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
		{ client->addr, I2C_M_IGNORE_NAK, 3, reset_on  },
	};
	struct i2c_msg test[2] = {
		{ client->addr, 0,        3, write },
		{ client->addr, I2C_M_RD, 2, read  },
	};

M
 
Mauro Carvalho Chehab 已提交
130
	v4l_dbg(3, msp_debug, client, "msp_reset\n");
131 132 133
	if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
	    i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
	    i2c_transfer(client->adapter, test, 2) != 2) {
134
		v4l_err(client, "chip reset failed\n");
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		return -1;
	}
	return 0;
}

static int msp_read(struct i2c_client *client, int dev, int addr)
{
	int err, retval;
	u8 write[3];
	u8 read[2];
	struct i2c_msg msgs[2] = {
		{ client->addr, 0,        3, write },
		{ client->addr, I2C_M_RD, 2, read  }
	};

	write[0] = dev + 1;
	write[1] = addr >> 8;
	write[2] = addr & 0xff;

	for (err = 0; err < 3; err++) {
		if (i2c_transfer(client->adapter, msgs, 2) == 2)
			break;
157
		v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
158 159 160 161 162
		       dev, addr);
		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(msecs_to_jiffies(10));
	}
	if (err == 3) {
163
		v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
164 165 166 167
		msp_reset(client);
		return -1;
	}
	retval = read[0] << 8 | read[1];
M
 
Mauro Carvalho Chehab 已提交
168
	v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	return retval;
}

int msp_read_dem(struct i2c_client *client, int addr)
{
	return msp_read(client, I2C_MSP_DEM, addr);
}

int msp_read_dsp(struct i2c_client *client, int addr)
{
	return msp_read(client, I2C_MSP_DSP, addr);
}

static int msp_write(struct i2c_client *client, int dev, int addr, int val)
{
	int err;
	u8 buffer[5];

	buffer[0] = dev;
	buffer[1] = addr >> 8;
	buffer[2] = addr &  0xff;
	buffer[3] = val  >> 8;
	buffer[4] = val  &  0xff;

M
 
Mauro Carvalho Chehab 已提交
193
	v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
194 195 196
	for (err = 0; err < 3; err++) {
		if (i2c_master_send(client, buffer, 5) == 5)
			break;
197
		v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
198 199 200 201 202
		       dev, addr);
		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(msecs_to_jiffies(10));
	}
	if (err == 3) {
203
		v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
		msp_reset(client);
		return -1;
	}
	return 0;
}

int msp_write_dem(struct i2c_client *client, int addr, int val)
{
	return msp_write(client, I2C_MSP_DEM, addr, val);
}

int msp_write_dsp(struct i2c_client *client, int addr, int val)
{
	return msp_write(client, I2C_MSP_DSP, addr, val);
}

/* ----------------------------------------------------------------------- *
 * bits  9  8  5 - SCART DSP input Select:
 *       0  0  0 - SCART 1 to DSP input (reset position)
 *       0  1  0 - MONO to DSP input
 *       1  0  0 - SCART 2 to DSP input
 *       1  1  1 - Mute DSP input
 *
 * bits 11 10  6 - SCART 1 Output Select:
 *       0  0  0 - undefined (reset position)
 *       0  1  0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
 *       1  0  0 - MONO input to SCART 1 Output
 *       1  1  0 - SCART 1 DA to SCART 1 Output
 *       0  0  1 - SCART 2 DA to SCART 1 Output
 *       0  1  1 - SCART 1 Input to SCART 1 Output
 *       1  1  1 - Mute SCART 1 Output
 *
 * bits 13 12  7 - SCART 2 Output Select (for devices with 2 Output SCART):
 *       0  0  0 - SCART 1 DA to SCART 2 Output (reset position)
 *       0  1  0 - SCART 1 Input to SCART 2 Output
 *       1  0  0 - MONO input to SCART 2 Output
 *       0  0  1 - SCART 2 DA to SCART 2 Output
 *       0  1  1 - SCART 2 Input to SCART 2 Output
 *       1  1  0 - Mute SCART 2 Output
 *
 * Bits 4 to 0 should be zero.
 * ----------------------------------------------------------------------- */

static int scarts[3][9] = {
	/* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
	/* SCART DSP Input select */
	{ 0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
	/* SCART1 Output select */
	{ 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
	/* SCART2 Output select */
	{ 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
};

static char *scart_names[] = {
	"mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
};

void msp_set_scart(struct i2c_client *client, int in, int out)
{
	struct msp_state *state = i2c_get_clientdata(client);

	state->in_scart=in;

	if (in >= 1 && in <= 8 && out >= 0 && out <= 2) {
		if (-1 == scarts[out][in])
			return;

		state->acb &= ~scarts[out][SCART_MASK];
		state->acb |=  scarts[out][in];
	} else
		state->acb = 0xf60; /* Mute Input and SCART 1 Output */

M
 
Mauro Carvalho Chehab 已提交
276
	v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
277 278 279 280 281 282 283 284 285
						scart_names[in], out, state->acb);
	msp_write_dsp(client, 0x13, state->acb);

	/* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
	msp_write_dem(client, 0x40, state->i2s_mode);
}

void msp_set_mute(struct i2c_client *client)
{
286 287
	struct msp_state *state = i2c_get_clientdata(client);

M
 
Mauro Carvalho Chehab 已提交
288
	v4l_dbg(1, msp_debug, client, "mute audio\n");
289 290 291 292 293 294
	msp_write_dsp(client, 0x0000, 0);
	msp_write_dsp(client, 0x0007, 1);
	if (state->has_scart2_out_volume)
		msp_write_dsp(client, 0x0040, 1);
	if (state->has_headphones)
		msp_write_dsp(client, 0x0006, 0);
295 296 297 298 299
}

void msp_set_audio(struct i2c_client *client)
{
	struct msp_state *state = i2c_get_clientdata(client);
300 301
	int bal = 0, bass, treble, loudness;
	int val = 0;
302 303 304

	if (!state->muted)
		val = (state->volume * 0x7f / 65535) << 8;
305

M
 
Mauro Carvalho Chehab 已提交
306
	v4l_dbg(1, msp_debug, client, "mute=%s volume=%d\n",
307 308 309 310 311 312 313 314 315 316 317
		state->muted ? "on" : "off", state->volume);

	msp_write_dsp(client, 0x0000, val);
	msp_write_dsp(client, 0x0007, state->muted ? 0x1 : (val | 0x1));
	if (state->has_scart2_out_volume)
		msp_write_dsp(client, 0x0040, state->muted ? 0x1 : (val | 0x1));
	if (state->has_headphones)
		msp_write_dsp(client, 0x0006, val);
	if (!state->has_sound_processing)
		return;

318
	if (val)
319
		bal = (u8)((state->balance / 256) - 128);
320 321
	bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
	treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
322
	loudness = state->loudness ? ((5 * 4) << 8) : 0;
323

M
 
Mauro Carvalho Chehab 已提交
324
	v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
325
		state->balance, state->bass, state->treble, state->loudness);
326 327

	msp_write_dsp(client, 0x0001, bal << 8);
328 329 330 331 332 333 334 335 336
	msp_write_dsp(client, 0x0002, bass);
	msp_write_dsp(client, 0x0003, treble);
	msp_write_dsp(client, 0x0004, loudness);
	if (!state->has_headphones)
		return;
	msp_write_dsp(client, 0x0030, bal << 8);
	msp_write_dsp(client, 0x0031, bass);
	msp_write_dsp(client, 0x0032, treble);
	msp_write_dsp(client, 0x0033, loudness);
337 338
}

339
int msp_modus(struct i2c_client *client)
340
{
341 342 343
	struct msp_state *state = i2c_get_clientdata(client);

	if (state->radio) {
M
 
Mauro Carvalho Chehab 已提交
344
		v4l_dbg(1, msp_debug, client, "video mode selected to Radio\n");
345 346 347
		return 0x0003;
	}

348
	if (state->v4l2_std & V4L2_STD_PAL) {
M
 
Mauro Carvalho Chehab 已提交
349
		v4l_dbg(1, msp_debug, client, "video mode selected to PAL\n");
350 351 352 353 354 355 356 357

#if 1
		/* experimental: not sure this works with all chip versions */
		return 0x7003;
#else
		/* previous value, try this if it breaks ... */
		return 0x1003;
#endif
358
	}
359
	if (state->v4l2_std & V4L2_STD_NTSC) {
M
 
Mauro Carvalho Chehab 已提交
360
		v4l_dbg(1, msp_debug, client, "video mode selected to NTSC\n");
361
		return 0x2003;
362
	}
363
	if (state->v4l2_std & V4L2_STD_SECAM) {
M
 
Mauro Carvalho Chehab 已提交
364
		v4l_dbg(1, msp_debug, client, "video mode selected to SECAM\n");
365 366
		return 0x0003;
	}
367
	return 0x0003;
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
}

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


static void msp_wake_thread(struct i2c_client *client)
{
	struct msp_state *state = i2c_get_clientdata(client);

	if (NULL == state->kthread)
		return;
	msp_set_mute(client);
	state->watch_stereo = 0;
	state->restart = 1;
	wake_up_interruptible(&state->wq);
}

int msp_sleep(struct msp_state *state, int timeout)
{
	DECLARE_WAITQUEUE(wait, current);

	add_wait_queue(&state->wq, &wait);
	if (!kthread_should_stop()) {
		if (timeout < 0) {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule();
		} else {
			schedule_timeout_interruptible
						(msecs_to_jiffies(timeout));
		}
	}

	remove_wait_queue(&state->wq, &wait);
	try_to_freeze();
	return state->restart;
}

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

static int msp_mode_v4l2_to_v4l1(int rxsubchans)
{
	int mode = 0;

	if (rxsubchans & V4L2_TUNER_SUB_STEREO)
		mode |= VIDEO_SOUND_STEREO;
	if (rxsubchans & V4L2_TUNER_SUB_LANG2)
414
		mode |= VIDEO_SOUND_LANG2 | VIDEO_SOUND_STEREO;
415
	if (rxsubchans & V4L2_TUNER_SUB_LANG1)
416
		mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_STEREO;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	if (mode == 0)
		mode |= VIDEO_SOUND_MONO;
	return mode;
}

static int msp_mode_v4l1_to_v4l2(int mode)
{
	if (mode & VIDEO_SOUND_STEREO)
		return V4L2_TUNER_MODE_STEREO;
	if (mode & VIDEO_SOUND_LANG2)
		return V4L2_TUNER_MODE_LANG2;
	if (mode & VIDEO_SOUND_LANG1)
		return V4L2_TUNER_MODE_LANG1;
	return V4L2_TUNER_MODE_MONO;
}

433
static struct v4l2_queryctrl msp_qctrl_std[] = {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	{
		.id            = V4L2_CID_AUDIO_VOLUME,
		.name          = "Volume",
		.minimum       = 0,
		.maximum       = 65535,
		.step          = 65535/100,
		.default_value = 58880,
		.flags         = 0,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_AUDIO_MUTE,
		.name          = "Mute",
		.minimum       = 0,
		.maximum       = 1,
		.step          = 1,
		.default_value = 1,
		.flags         = 0,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
452 453 454 455 456 457 458 459 460 461 462 463 464
	},
};

static struct v4l2_queryctrl msp_qctrl_sound_processing[] = {
	{
		.id            = V4L2_CID_AUDIO_BALANCE,
		.name          = "Balance",
		.minimum       = 0,
		.maximum       = 65535,
		.step          = 65535/100,
		.default_value = 32768,
		.flags         = 0,
		.type          = V4L2_CTRL_TYPE_INTEGER,
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	},{
		.id            = V4L2_CID_AUDIO_BASS,
		.name          = "Bass",
		.minimum       = 0,
		.maximum       = 65535,
		.step          = 65535/100,
		.default_value = 32768,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_AUDIO_TREBLE,
		.name          = "Treble",
		.minimum       = 0,
		.maximum       = 65535,
		.step          = 65535/100,
		.default_value = 32768,
		.type          = V4L2_CTRL_TYPE_INTEGER,
481 482 483 484 485 486 487 488 489
	},{
		.id            = V4L2_CID_AUDIO_LOUDNESS,
		.name          = "Loudness",
		.minimum       = 0,
		.maximum       = 1,
		.step          = 1,
		.default_value = 1,
		.flags         = 0,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
490 491 492 493 494 495 496 497 498
	},
};


static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
{
	struct msp_state *state = i2c_get_clientdata(client);

	switch (ctrl->id) {
499 500 501 502
	case V4L2_CID_AUDIO_VOLUME:
		ctrl->value = state->volume;
		break;

503 504 505 506 507
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = state->muted;
		break;

	case V4L2_CID_AUDIO_BALANCE:
508 509
		if (!state->has_sound_processing)
			return -EINVAL;
510 511 512 513
		ctrl->value = state->balance;
		break;

	case V4L2_CID_AUDIO_BASS:
514 515
		if (!state->has_sound_processing)
			return -EINVAL;
516 517 518 519
		ctrl->value = state->bass;
		break;

	case V4L2_CID_AUDIO_TREBLE:
520 521
		if (!state->has_sound_processing)
			return -EINVAL;
522 523 524
		ctrl->value = state->treble;
		break;

525 526 527 528
	case V4L2_CID_AUDIO_LOUDNESS:
		if (!state->has_sound_processing)
			return -EINVAL;
		ctrl->value = state->loudness;
529 530 531 532 533 534 535 536 537 538 539 540 541
		break;

	default:
		return -EINVAL;
	}
	return 0;
}

static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
{
	struct msp_state *state = i2c_get_clientdata(client);

	switch (ctrl->id) {
542 543 544 545 546 547
	case V4L2_CID_AUDIO_VOLUME:
		state->volume = ctrl->value;
		if (state->volume == 0)
			state->balance = 32768;
		break;

548 549 550 551 552 553 554
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->value < 0 || ctrl->value >= 2)
			return -ERANGE;
		state->muted = ctrl->value;
		break;

	case V4L2_CID_AUDIO_BASS:
555 556
		if (!state->has_sound_processing)
			return -EINVAL;
557 558 559 560
		state->bass = ctrl->value;
		break;

	case V4L2_CID_AUDIO_TREBLE:
561 562
		if (!state->has_sound_processing)
			return -EINVAL;
563 564 565
		state->treble = ctrl->value;
		break;

566 567 568 569
	case V4L2_CID_AUDIO_LOUDNESS:
		if (!state->has_sound_processing)
			return -EINVAL;
		state->loudness = ctrl->value;
570 571
		break;

572 573 574 575
	case V4L2_CID_AUDIO_BALANCE:
		if (!state->has_sound_processing)
			return -EINVAL;
		state->balance = ctrl->value;
576 577 578 579 580 581 582 583 584 585 586 587 588 589
		break;

	default:
		return -EINVAL;
	}
	msp_set_audio(client);
	return 0;
}

static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
{
	struct msp_state *state = i2c_get_clientdata(client);
	int scart = 0;

M
 
Mauro Carvalho Chehab 已提交
590
	if (msp_debug >= 2)
591 592 593 594
		v4l_i2c_print_ioctl(client, cmd);

	switch (cmd) {
	case AUDC_SET_RADIO:
595 596
		if (state->radio)
			return 0;
597
		state->radio = 1;
M
 
Mauro Carvalho Chehab 已提交
598
		v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
599 600 601 602
		state->watch_stereo = 0;
		switch (state->opmode) {
		case OPMODE_MANUAL:
			/* set msp3400 to FM radio mode */
603 604
			msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
			msp3400c_set_carrier(client, MSP_CARRIER(10.7),
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
					    MSP_CARRIER(10.7));
			msp_set_audio(client);
			break;
		case OPMODE_AUTODETECT:
		case OPMODE_AUTOSELECT:
			/* the thread will do for us */
			msp_wake_thread(client);
			break;
		}
		break;

	/* --- v4l ioctls --- */
	/* take care: bttv does userspace copying, we'll get a
	   kernel pointer here... */
	case VIDIOCGAUDIO:
	{
		struct video_audio *va = arg;

623 624 625 626 627
		va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
		if (state->has_sound_processing)
			va->flags |= VIDEO_AUDIO_BALANCE |
				VIDEO_AUDIO_BASS |
				VIDEO_AUDIO_TREBLE;
628 629 630 631 632 633 634
		if (state->muted)
			va->flags |= VIDEO_AUDIO_MUTE;
		va->volume = state->volume;
		va->balance = state->volume ? state->balance : 32768;
		va->bass = state->bass;
		va->treble = state->treble;

635 636
		if (state->radio)
			break;
637
		if (state->opmode == OPMODE_AUTOSELECT)
638
			msp_detect_stereo(client);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans);
		break;
	}

	case VIDIOCSAUDIO:
	{
		struct video_audio *va = arg;

		state->muted = (va->flags & VIDEO_AUDIO_MUTE);
		state->volume = va->volume;
		state->balance = va->balance;
		state->bass = va->bass;
		state->treble = va->treble;
		msp_set_audio(client);

654 655 656
		if (va->mode != 0 && state->radio == 0) {
			state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
		}
657 658 659 660 661 662
		break;
	}

	case VIDIOCSCHAN:
	{
		struct video_channel *vc = arg;
663 664
		int update = 0;
		v4l2_std_id std;
665

666 667
		if (state->radio)
			update = 1;
668 669
		state->radio = 0;
		if (vc->norm == VIDEO_MODE_PAL)
670
			std = V4L2_STD_PAL;
671
		else if (vc->norm == VIDEO_MODE_SECAM)
672
			std = V4L2_STD_SECAM;
673
		else
674
			std = V4L2_STD_NTSC;
675 676
		if (std != state->v4l2_std) {
			state->v4l2_std = std;
677 678 679 680
			update = 1;
		}
		if (update)
			msp_wake_thread(client);
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		break;
	}

	case VIDIOCSFREQ:
	case VIDIOC_S_FREQUENCY:
	{
		/* new channel -- kick audio carrier scan */
		msp_wake_thread(client);
		break;
	}

	/* msp34xx specific */
	case MSP_SET_MATRIX:
	{
		struct msp_matrix *mspm = arg;

		msp_set_scart(client, mspm->input, mspm->output);
		break;
	}

	/* --- v4l2 ioctls --- */
	case VIDIOC_S_STD:
	{
		v4l2_std_id *id = arg;
705
		int update = state->radio || state->v4l2_std != *id;
706

707
		state->v4l2_std = *id;
708
		state->radio = 0;
709 710
		if (update)
			msp_wake_thread(client);
711 712 713 714 715 716 717 718
		return 0;
	}

	case VIDIOC_S_AUDIO:
	{
		struct v4l2_audio *sarg = arg;

		switch (sarg->index) {
719
		case TVAUDIO_INPUT_RADIO:
720 721 722 723
			/* Hauppauge uses IN2 for the radio */
			state->mode = MSP_MODE_FM_RADIO;
			scart       = SCART_IN2;
			break;
724
		case TVAUDIO_INPUT_EXTERN:
725 726 727 728
			/* IN1 is often used for external input ... */
			state->mode = MSP_MODE_EXTERN;
			scart       = SCART_IN1;
			break;
729
		case TVAUDIO_INPUT_INTERN:
730 731 732 733
			/* ... sometimes it is IN2 through ;) */
			state->mode = MSP_MODE_EXTERN;
			scart       = SCART_IN2;
			break;
734
		case TVAUDIO_INPUT_TUNER:
735 736 737 738 739 740 741 742
			state->mode = -1;
			break;
		}
		if (scart) {
			state->rxsubchans = V4L2_TUNER_SUB_STEREO;
			msp_set_scart(client, scart, 0);
			msp_write_dsp(client, 0x000d, 0x1900);
		}
743
		msp_set_audmode(client);
744 745 746 747 748 749 750 751
		msp_wake_thread(client);
		break;
	}

	case VIDIOC_G_TUNER:
	{
		struct v4l2_tuner *vt = arg;

752 753
		if (state->radio)
			break;
754
		if (state->opmode == OPMODE_AUTOSELECT)
755
			msp_detect_stereo(client);
756 757 758 759 760 761 762 763 764 765 766
		vt->audmode    = state->audmode;
		vt->rxsubchans = state->rxsubchans;
		vt->capability = V4L2_TUNER_CAP_STEREO |
			V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
		break;
	}

	case VIDIOC_S_TUNER:
	{
		struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;

767
		if (state->radio)  /* TODO: add mono/stereo support for radio */
768
			break;
769
		state->audmode = vt->audmode;
770
		/* only set audmode */
771
		msp_set_audmode(client);
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
		break;
	}

	case VIDIOC_G_AUDOUT:
	{
		struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
		int idx = a->index;

		memset(a, 0, sizeof(*a));

		switch (idx) {
		case 0:
			strcpy(a->name, "Scart1 Out");
			break;
		case 1:
			strcpy(a->name, "Scart2 Out");
			break;
		case 2:
			strcpy(a->name, "I2S Out");
			break;
		default:
			return -EINVAL;
		}
		break;
	}

	case VIDIOC_S_AUDOUT:
	{
		struct v4l2_audioout *a = (struct v4l2_audioout *)arg;

		if (a->index < 0 || a->index > 2)
			return -EINVAL;

M
 
Mauro Carvalho Chehab 已提交
805
		v4l_dbg(1, msp_debug, client, "Setting audio out on msp34xx to input %i\n", a->index);
806 807 808 809 810 811 812 813 814
		msp_set_scart(client, state->in_scart, a->index + 1);

		break;
	}

	case VIDIOC_INT_I2S_CLOCK_FREQ:
	{
		u32 *a = (u32 *)arg;

M
 
Mauro Carvalho Chehab 已提交
815
		v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

		switch (*a) {
			case 1024000:
				state->i2s_mode = 0;
				break;
			case 2048000:
				state->i2s_mode = 1;
				break;
			default:
				return -EINVAL;
		}
		break;
	}

	case VIDIOC_QUERYCTRL:
	{
		struct v4l2_queryctrl *qc = arg;
		int i;

835 836 837 838 839 840 841 842 843 844
		for (i = 0; i < ARRAY_SIZE(msp_qctrl_std); i++)
			if (qc->id && qc->id == msp_qctrl_std[i].id) {
				memcpy(qc, &msp_qctrl_std[i], sizeof(*qc));
				return 0;
			}
		if (!state->has_sound_processing)
			return -EINVAL;
		for (i = 0; i < ARRAY_SIZE(msp_qctrl_sound_processing); i++)
			if (qc->id && qc->id == msp_qctrl_sound_processing[i].id) {
				memcpy(qc, &msp_qctrl_sound_processing[i], sizeof(*qc));
845 846 847 848 849 850 851 852 853 854 855 856
				return 0;
			}
		return -EINVAL;
	}

	case VIDIOC_G_CTRL:
		return msp_get_ctrl(client, arg);

	case VIDIOC_S_CTRL:
		return msp_set_ctrl(client, arg);

	case VIDIOC_LOG_STATUS:
857 858 859
	{
		const char *p;

860
		if (state->opmode == OPMODE_AUTOSELECT)
861
			msp_detect_stereo(client);
862
		v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
863
				client->name, state->rev1, state->rev2);
864
		v4l_info(client, "Audio:    volume %d%s\n",
865 866
				state->volume, state->muted ? " (muted)" : "");
		if (state->has_sound_processing) {
867
			v4l_info(client, "Audio:    balance %d bass %d treble %d loudness %s\n",
868 869 870
					state->balance, state->bass, state->treble,
					state->loudness ? "on" : "off");
		}
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
		switch (state->mode) {
		case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
		case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
		case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono + FM-stereo"; break;
		case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
		case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
		case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
		case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
		case MSP_MODE_BTSC: p = "BTSC"; break;
		case MSP_MODE_EXTERN: p = "External input"; break;
		default: p = "unknown"; break;
		}
		if (state->opmode == OPMODE_MANUAL) {
			v4l_info(client, "Mode:     %s (%s%s)\n", p,
				(state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
				(state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
		} else {
			v4l_info(client, "Mode:     %s\n", p);
			v4l_info(client, "Standard: %s (%s%s)\n",
				msp_standard_std_name(state->std),
				(state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
				(state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
		}
		v4l_info(client, "ACB:      0x%04x\n", state->acb);
895
		break;
896
	}
897 898

	default:
899 900
		/* unknown */
		return -EINVAL;
901 902 903 904 905 906 907 908
	}
	return 0;
}

static int msp_suspend(struct device * dev, pm_message_t state)
{
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);

M
 
Mauro Carvalho Chehab 已提交
909
	v4l_dbg(1, msp_debug, client, "suspend\n");
910 911 912 913 914 915 916 917
	msp_reset(client);
	return 0;
}

static int msp_resume(struct device * dev)
{
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);

M
 
Mauro Carvalho Chehab 已提交
918
	v4l_dbg(1, msp_debug, client, "resume\n");
919 920 921 922 923 924 925 926 927 928 929 930 931
	msp_wake_thread(client);
	return 0;
}

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

static struct i2c_driver i2c_driver;

static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
{
	struct i2c_client *client;
	struct msp_state *state;
	int (*thread_func)(void *data) = NULL;
932 933 934 935 936
	int msp_hard;
	int msp_family;
	int msp_revision;
	int msp_product, msp_prod_hi, msp_prod_lo;
	int msp_rom;
937 938 939 940 941 942 943 944 945 946 947

	client = kmalloc(sizeof(*client), GFP_KERNEL);
	if (client == NULL)
		return -ENOMEM;
	memset(client, 0, sizeof(*client));
	client->addr = address;
	client->adapter = adapter;
	client->driver = &i2c_driver;
	snprintf(client->name, sizeof(client->name) - 1, "msp3400");

	if (msp_reset(client) == -1) {
M
 
Mauro Carvalho Chehab 已提交
948
		v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
949 950 951 952 953 954 955 956 957 958 959 960
		kfree(client);
		return -1;
	}

	state = kmalloc(sizeof(*state), GFP_KERNEL);
	if (state == NULL) {
		kfree(client);
		return -ENOMEM;
	}
	i2c_set_clientdata(client, state);

	memset(state, 0, sizeof(*state));
961
	state->v4l2_std = V4L2_STD_NTSC;
962
	state->audmode = V4L2_TUNER_MODE_LANG1;
963 964 965 966
	state->volume = 58880;	/* 0db gain */
	state->balance = 32768;	/* 0db gain */
	state->bass = 32768;
	state->treble = 32768;
967
	state->loudness = 0;
968 969 970 971 972 973 974 975
	state->input = -1;
	state->muted = 0;
	state->i2s_mode = 0;
	init_waitqueue_head(&state->wq);

	state->rev1 = msp_read_dsp(client, 0x1e);
	if (state->rev1 != -1)
		state->rev2 = msp_read_dsp(client, 0x1f);
M
 
Mauro Carvalho Chehab 已提交
976
	v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
977
	if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
M
 
Mauro Carvalho Chehab 已提交
978
		v4l_dbg(1, msp_debug, client, "not an msp3400 (cannot read chip version)\n");
979 980 981 982 983 984 985
		kfree(state);
		kfree(client);
		return -1;
	}

	msp_set_audio(client);

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
	msp_product = (state->rev2 >> 8) & 0xff;
	msp_prod_hi = msp_product / 10;
	msp_prod_lo = msp_product % 10;
	msp_revision = (state->rev1 & 0x0f) + '@';
	msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
	msp_rom = state->rev2 & 0x1f;
	snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
			msp_family, msp_product,
			msp_revision, msp_hard, msp_rom);

	/* Has NICAM support: all mspx41x and mspx45x products have NICAM */
	state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
	/* Has radio support: was added with revision G */
	state->has_radio = msp_revision >= 'G';
	/* Has headphones output: not for stripped down products */
	state->has_headphones = msp_prod_lo < 5;
	/* Has scart4 input: not in pre D revisions, not in stripped D revs */
	state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
	/* Has scart2 and scart3 inputs and scart2 output: not in stripped
	   down products of the '3' family */
	state->has_scart23_in_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
1008 1009
	/* Has scart2 a volume control? Not in pre-D revisions. */
	state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart23_in_scart2_out;
1010 1011
	/* Has a configurable i2s out? */
	state->has_i2s_conf = msp_revision >= 'G' && msp_prod_lo < 7;
1012 1013 1014 1015 1016 1017 1018 1019 1020
	/* Has subwoofer output: not in pre-D revs and not in stripped down products */
	state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
	/* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
	   stripped down products */
	state->has_sound_processing = msp_prod_lo < 7;
	/* Has Virtual Dolby Surround: only in msp34x1 */
	state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
	/* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
	state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
1021 1022 1023 1024

	state->opmode = opmode;
	if (state->opmode == OPMODE_AUTO) {
		/* MSP revision G and up have both autodetect and autoselect */
1025
		if (msp_revision >= 'G')
1026 1027
			state->opmode = OPMODE_AUTOSELECT;
		/* MSP revision D and up have autodetect */
1028
		else if (msp_revision >= 'D')
1029 1030 1031 1032 1033 1034
			state->opmode = OPMODE_AUTODETECT;
		else
			state->opmode = OPMODE_MANUAL;
	}

	/* hello world :-) */
1035 1036
	v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
	v4l_info(client, "%s ", client->name);
1037
	if (state->has_nicam && state->has_radio)
1038
		printk("supports nicam and radio, ");
1039
	else if (state->has_nicam)
1040
		printk("supports nicam, ");
1041
	else if (state->has_radio)
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		printk("supports radio, ");
	printk("mode is ");

	/* version-specific initialization */
	switch (state->opmode) {
	case OPMODE_MANUAL:
		printk("manual");
		thread_func = msp3400c_thread;
		break;
	case OPMODE_AUTODETECT:
		printk("autodetect");
		thread_func = msp3410d_thread;
		break;
	case OPMODE_AUTOSELECT:
		printk("autodetect and autoselect");
		thread_func = msp34xxg_thread;
		break;
	}
	printk("\n");

	/* startup control thread if needed */
	if (thread_func) {
		state->kthread = kthread_run(thread_func, client, "msp34xx");

		if (state->kthread == NULL)
1067
			v4l_warn(client, "kernel_thread() failed\n");
1068 1069 1070 1071 1072 1073 1074 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 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		msp_wake_thread(client);
	}

	/* done */
	i2c_attach_client(client);

	return 0;
}

static int msp_probe(struct i2c_adapter *adapter)
{
	if (adapter->class & I2C_CLASS_TV_ANALOG)
		return i2c_probe(adapter, &addr_data, msp_attach);
	return 0;
}

static int msp_detach(struct i2c_client *client)
{
	struct msp_state *state = i2c_get_clientdata(client);
	int err;

	/* shutdown control thread */
	if (state->kthread) {
		state->restart = 1;
		kthread_stop(state->kthread);
	}
	msp_reset(client);

	err = i2c_detach_client(client);
	if (err) {
		return err;
	}

	kfree(state);
	kfree(client);
	return 0;
}

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

/* i2c implementation */
static struct i2c_driver i2c_driver = {
	.id             = I2C_DRIVERID_MSP3400,
	.attach_adapter = msp_probe,
	.detach_client  = msp_detach,
	.command        = msp_command,
	.driver = {
		.name    = "msp3400",
		.suspend = msp_suspend,
		.resume  = msp_resume,
	},
};

static int __init msp3400_init_module(void)
{
	return i2c_add_driver(&i2c_driver);
}

static void __exit msp3400_cleanup_module(void)
{
	i2c_del_driver(&i2c_driver);
}

module_init(msp3400_init_module);
module_exit(msp3400_cleanup_module);

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-basic-offset: 8
 * End:
 */