xref: /linux/drivers/media/i2c/msp3400-driver.c (revision 5ce42b5de461c3154f61a023b191dd6b77ee66c0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Programming the mspx4xx sound processor family
4  *
5  * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
6  *
7  * what works and what doesn't:
8  *
9  *  AM-Mono
10  *      Support for Hauppauge cards added (decoding handled by tuner) added by
11  *      Frederic Crozat <fcrozat@mail.dotcom.fr>
12  *
13  *  FM-Mono
14  *      should work. The stereo modes are backward compatible to FM-mono,
15  *      therefore FM-Mono should be always available.
16  *
17  *  FM-Stereo (B/G, used in germany)
18  *      should work, with autodetect
19  *
20  *  FM-Stereo (satellite)
21  *      should work, no autodetect (i.e. default is mono, but you can
22  *      switch to stereo -- untested)
23  *
24  *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
25  *      should work, with autodetect. Support for NICAM was added by
26  *      Pekka Pietikainen <pp@netppl.fi>
27  *
28  * TODO:
29  *   - better SAT support
30  *
31  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
32  *         using soundcore instead of OSS
33  */
34 
35 
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/i2c.h>
40 #include <linux/kthread.h>
41 #include <linux/freezer.h>
42 #include <linux/videodev2.h>
43 #include <media/v4l2-device.h>
44 #include <media/v4l2-ioctl.h>
45 #include <media/drv-intf/msp3400.h>
46 #include <media/i2c/tvaudio.h>
47 #include "msp3400-driver.h"
48 
49 /* ---------------------------------------------------------------------- */
50 
51 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
52 MODULE_AUTHOR("Gerd Knorr");
53 MODULE_LICENSE("GPL");
54 
55 /* module parameters */
56 static int opmode   = OPMODE_AUTO;
57 int msp_debug;		 /* msp_debug output */
58 bool msp_once;		 /* no continuous stereo monitoring */
59 bool msp_amsound;	 /* hard-wire AM sound at 6.5 Hz (france),
60 			    the autoscan seems work well only with FM... */
61 int msp_standard = 1;    /* Override auto detect of audio msp_standard,
62 			    if needed. */
63 bool msp_dolby;
64 
65 int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
66 					(msp34xxg only) 0x00a0-0x03c0 */
67 
68 /* read-only */
69 module_param(opmode,           int, 0444);
70 
71 /* read-write */
72 module_param_named(once, msp_once,                      bool, 0644);
73 module_param_named(debug, msp_debug,                    int,  0644);
74 module_param_named(stereo_threshold, msp_stereo_thresh, int,  0644);
75 module_param_named(standard, msp_standard,              int,  0644);
76 module_param_named(amsound, msp_amsound,                bool, 0644);
77 module_param_named(dolby, msp_dolby,                    bool, 0644);
78 
79 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
80 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
81 MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
82 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
83 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
84 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
85 MODULE_PARM_DESC(dolby, "Activates Dolby processing");
86 
87 /* ---------------------------------------------------------------------- */
88 
89 /* control subaddress */
90 #define I2C_MSP_CONTROL 0x00
91 /* demodulator unit subaddress */
92 #define I2C_MSP_DEM     0x10
93 /* DSP unit subaddress */
94 #define I2C_MSP_DSP     0x12
95 
96 
97 /* ----------------------------------------------------------------------- */
98 /* functions for talking to the MSP3400C Sound processor                   */
99 
100 int msp_reset(struct i2c_client *client)
101 {
102 	/* reset and read revision code */
103 	static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
104 	static u8 reset_on[3]  = { I2C_MSP_CONTROL, 0x00, 0x00 };
105 	static u8 write[3]     = { I2C_MSP_DSP + 1, 0x00, 0x1e };
106 	u8 read[2];
107 	struct i2c_msg reset[2] = {
108 		{
109 			.addr = client->addr,
110 			.flags = I2C_M_IGNORE_NAK,
111 			.len = 3,
112 			.buf = reset_off
113 		},
114 		{
115 			.addr = client->addr,
116 			.flags = I2C_M_IGNORE_NAK,
117 			.len = 3,
118 			.buf = reset_on
119 		},
120 	};
121 	struct i2c_msg test[2] = {
122 		{
123 			.addr = client->addr,
124 			.len = 3,
125 			.buf = write
126 		},
127 		{
128 			.addr = client->addr,
129 			.flags = I2C_M_RD,
130 			.len = 2,
131 			.buf = read
132 		},
133 	};
134 
135 	dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_reset\n");
136 	if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
137 	    i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
138 	    i2c_transfer(client->adapter, test, 2) != 2) {
139 		dev_err(&client->dev, "chip reset failed\n");
140 		return -1;
141 	}
142 	return 0;
143 }
144 
145 static int msp_read(struct i2c_client *client, int dev, int addr)
146 {
147 	int err, retval;
148 	u8 write[3];
149 	u8 read[2];
150 	struct i2c_msg msgs[2] = {
151 		{
152 			.addr = client->addr,
153 			.len = 3,
154 			.buf = write
155 		},
156 		{
157 			.addr = client->addr,
158 			.flags = I2C_M_RD,
159 			.len = 2,
160 			.buf = read
161 		}
162 	};
163 
164 	write[0] = dev + 1;
165 	write[1] = addr >> 8;
166 	write[2] = addr & 0xff;
167 
168 	for (err = 0; err < 3; err++) {
169 		if (i2c_transfer(client->adapter, msgs, 2) == 2)
170 			break;
171 		dev_warn(&client->dev, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
172 		       dev, addr);
173 		schedule_timeout_interruptible(msecs_to_jiffies(10));
174 	}
175 	if (err == 3) {
176 		dev_warn(&client->dev, "resetting chip, sound will go off.\n");
177 		msp_reset(client);
178 		return -1;
179 	}
180 	retval = read[0] << 8 | read[1];
181 	dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_read(0x%x, 0x%x): 0x%x\n",
182 			dev, addr, retval);
183 	return retval;
184 }
185 
186 int msp_read_dem(struct i2c_client *client, int addr)
187 {
188 	return msp_read(client, I2C_MSP_DEM, addr);
189 }
190 
191 int msp_read_dsp(struct i2c_client *client, int addr)
192 {
193 	return msp_read(client, I2C_MSP_DSP, addr);
194 }
195 
196 static int msp_write(struct i2c_client *client, int dev, int addr, int val)
197 {
198 	int err;
199 	u8 buffer[5];
200 
201 	buffer[0] = dev;
202 	buffer[1] = addr >> 8;
203 	buffer[2] = addr &  0xff;
204 	buffer[3] = val  >> 8;
205 	buffer[4] = val  &  0xff;
206 
207 	dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_write(0x%x, 0x%x, 0x%x)\n",
208 			dev, addr, val);
209 	for (err = 0; err < 3; err++) {
210 		if (i2c_master_send(client, buffer, 5) == 5)
211 			break;
212 		dev_warn(&client->dev, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
213 		       dev, addr);
214 		schedule_timeout_interruptible(msecs_to_jiffies(10));
215 	}
216 	if (err == 3) {
217 		dev_warn(&client->dev, "resetting chip, sound will go off.\n");
218 		msp_reset(client);
219 		return -1;
220 	}
221 	return 0;
222 }
223 
224 int msp_write_dem(struct i2c_client *client, int addr, int val)
225 {
226 	return msp_write(client, I2C_MSP_DEM, addr, val);
227 }
228 
229 int msp_write_dsp(struct i2c_client *client, int addr, int val)
230 {
231 	return msp_write(client, I2C_MSP_DSP, addr, val);
232 }
233 
234 /* ----------------------------------------------------------------------- *
235  * bits  9  8  5 - SCART DSP input Select:
236  *       0  0  0 - SCART 1 to DSP input (reset position)
237  *       0  1  0 - MONO to DSP input
238  *       1  0  0 - SCART 2 to DSP input
239  *       1  1  1 - Mute DSP input
240  *
241  * bits 11 10  6 - SCART 1 Output Select:
242  *       0  0  0 - undefined (reset position)
243  *       0  1  0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
244  *       1  0  0 - MONO input to SCART 1 Output
245  *       1  1  0 - SCART 1 DA to SCART 1 Output
246  *       0  0  1 - SCART 2 DA to SCART 1 Output
247  *       0  1  1 - SCART 1 Input to SCART 1 Output
248  *       1  1  1 - Mute SCART 1 Output
249  *
250  * bits 13 12  7 - SCART 2 Output Select (for devices with 2 Output SCART):
251  *       0  0  0 - SCART 1 DA to SCART 2 Output (reset position)
252  *       0  1  0 - SCART 1 Input to SCART 2 Output
253  *       1  0  0 - MONO input to SCART 2 Output
254  *       0  0  1 - SCART 2 DA to SCART 2 Output
255  *       0  1  1 - SCART 2 Input to SCART 2 Output
256  *       1  1  0 - Mute SCART 2 Output
257  *
258  * Bits 4 to 0 should be zero.
259  * ----------------------------------------------------------------------- */
260 
261 static int scarts[3][9] = {
262 	/* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
263 	/* SCART DSP Input select */
264 	{ 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
265 	/* SCART1 Output select */
266 	{ 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
267 	/* SCART2 Output select */
268 	{ 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
269 };
270 
271 static char *scart_names[] = {
272 	"in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
273 };
274 
275 void msp_set_scart(struct i2c_client *client, int in, int out)
276 {
277 	struct msp_state *state = to_state(i2c_get_clientdata(client));
278 
279 	state->in_scart = in;
280 
281 	if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
282 		if (-1 == scarts[out][in + 1])
283 			return;
284 
285 		state->acb &= ~scarts[out][0];
286 		state->acb |=  scarts[out][in + 1];
287 	} else
288 		state->acb = 0xf60; /* Mute Input and SCART 1 Output */
289 
290 	dev_dbg_lvl(&client->dev, 1, msp_debug, "scart switch: %s => %d (ACB=0x%04x)\n",
291 					scart_names[in], out, state->acb);
292 	msp_write_dsp(client, 0x13, state->acb);
293 
294 	/* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
295 	if (state->has_i2s_conf)
296 		msp_write_dem(client, 0x40, state->i2s_mode);
297 }
298 
299 /* ------------------------------------------------------------------------ */
300 
301 static void msp_wake_thread(struct i2c_client *client)
302 {
303 	struct msp_state *state = to_state(i2c_get_clientdata(client));
304 
305 	if (NULL == state->kthread)
306 		return;
307 	state->watch_stereo = 0;
308 	state->restart = 1;
309 	wake_up_interruptible(&state->wq);
310 }
311 
312 int msp_sleep(struct msp_state *state, int msec)
313 {
314 	long timeout;
315 
316 	timeout = msec < 0 ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(msec);
317 
318 	wait_event_freezable_timeout(state->wq, kthread_should_stop() ||
319 				     state->restart, timeout);
320 
321 	return state->restart;
322 }
323 
324 /* ------------------------------------------------------------------------ */
325 
326 static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
327 {
328 	struct msp_state *state = ctrl_to_state(ctrl);
329 	struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
330 	int val = ctrl->val;
331 
332 	switch (ctrl->id) {
333 	case V4L2_CID_AUDIO_VOLUME: {
334 		/* audio volume cluster */
335 		int reallymuted = state->muted->val | state->scan_in_progress;
336 
337 		if (!reallymuted)
338 			val = (val * 0x7f / 65535) << 8;
339 
340 		dev_dbg_lvl(&client->dev, 1, msp_debug, "mute=%s scanning=%s volume=%d\n",
341 				state->muted->val ? "on" : "off",
342 				state->scan_in_progress ? "yes" : "no",
343 				state->volume->val);
344 
345 		msp_write_dsp(client, 0x0000, val);
346 		msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
347 		if (state->has_scart2_out_volume)
348 			msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
349 		if (state->has_headphones)
350 			msp_write_dsp(client, 0x0006, val);
351 		break;
352 	}
353 
354 	case V4L2_CID_AUDIO_BASS:
355 		val = ((val - 32768) * 0x60 / 65535) << 8;
356 		msp_write_dsp(client, 0x0002, val);
357 		if (state->has_headphones)
358 			msp_write_dsp(client, 0x0031, val);
359 		break;
360 
361 	case V4L2_CID_AUDIO_TREBLE:
362 		val = ((val - 32768) * 0x60 / 65535) << 8;
363 		msp_write_dsp(client, 0x0003, val);
364 		if (state->has_headphones)
365 			msp_write_dsp(client, 0x0032, val);
366 		break;
367 
368 	case V4L2_CID_AUDIO_LOUDNESS:
369 		val = val ? ((5 * 4) << 8) : 0;
370 		msp_write_dsp(client, 0x0004, val);
371 		if (state->has_headphones)
372 			msp_write_dsp(client, 0x0033, val);
373 		break;
374 
375 	case V4L2_CID_AUDIO_BALANCE:
376 		val = (u8)((val / 256) - 128);
377 		msp_write_dsp(client, 0x0001, val << 8);
378 		if (state->has_headphones)
379 			msp_write_dsp(client, 0x0030, val << 8);
380 		break;
381 
382 	default:
383 		return -EINVAL;
384 	}
385 	return 0;
386 }
387 
388 void msp_update_volume(struct msp_state *state)
389 {
390 	/* Force an update of the volume/mute cluster */
391 	v4l2_ctrl_lock(state->volume);
392 	state->volume->val = state->volume->cur.val;
393 	state->muted->val = state->muted->cur.val;
394 	msp_s_ctrl(state->volume);
395 	v4l2_ctrl_unlock(state->volume);
396 }
397 
398 /* --- v4l2 ioctls --- */
399 static int msp_s_radio(struct v4l2_subdev *sd)
400 {
401 	struct msp_state *state = to_state(sd);
402 	struct i2c_client *client = v4l2_get_subdevdata(sd);
403 
404 	if (state->radio)
405 		return 0;
406 	state->radio = 1;
407 	dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to radio mode\n");
408 	state->watch_stereo = 0;
409 	switch (state->opmode) {
410 	case OPMODE_MANUAL:
411 		/* set msp3400 to FM radio mode */
412 		msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
413 		msp3400c_set_carrier(client, MSP_CARRIER(10.7),
414 				MSP_CARRIER(10.7));
415 		msp_update_volume(state);
416 		break;
417 	case OPMODE_AUTODETECT:
418 	case OPMODE_AUTOSELECT:
419 		/* the thread will do for us */
420 		msp_wake_thread(client);
421 		break;
422 	}
423 	return 0;
424 }
425 
426 static int msp_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
427 {
428 	struct i2c_client *client = v4l2_get_subdevdata(sd);
429 
430 	/* new channel -- kick audio carrier scan */
431 	msp_wake_thread(client);
432 	return 0;
433 }
434 
435 static int msp_querystd(struct v4l2_subdev *sd, v4l2_std_id *id)
436 {
437 	struct msp_state *state = to_state(sd);
438 	struct i2c_client *client = v4l2_get_subdevdata(sd);
439 
440 	*id &= state->detected_std;
441 
442 	dev_dbg_lvl(&client->dev, 2, msp_debug,
443 		"detected standard: %s(0x%08Lx)\n",
444 		msp_standard_std_name(state->std), state->detected_std);
445 
446 	return 0;
447 }
448 
449 static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
450 {
451 	struct msp_state *state = to_state(sd);
452 	struct i2c_client *client = v4l2_get_subdevdata(sd);
453 	int update = state->radio || state->v4l2_std != id;
454 
455 	state->v4l2_std = id;
456 	state->radio = 0;
457 	if (update)
458 		msp_wake_thread(client);
459 	return 0;
460 }
461 
462 static int msp_s_routing(struct v4l2_subdev *sd,
463 			 u32 input, u32 output, u32 config)
464 {
465 	struct msp_state *state = to_state(sd);
466 	struct i2c_client *client = v4l2_get_subdevdata(sd);
467 	int tuner = (input >> 3) & 1;
468 	int sc_in = input & 0x7;
469 	int sc1_out = output & 0xf;
470 	int sc2_out = (output >> 4) & 0xf;
471 	u16 val, reg;
472 	int i;
473 	int extern_input = 1;
474 
475 	if (state->route_in == input && state->route_out == output)
476 		return 0;
477 	state->route_in = input;
478 	state->route_out = output;
479 	/* check if the tuner input is used */
480 	for (i = 0; i < 5; i++) {
481 		if (((input >> (4 + i * 4)) & 0xf) == 0)
482 			extern_input = 0;
483 	}
484 	state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
485 	state->rxsubchans = V4L2_TUNER_SUB_STEREO;
486 	msp_set_scart(client, sc_in, 0);
487 	msp_set_scart(client, sc1_out, 1);
488 	msp_set_scart(client, sc2_out, 2);
489 	msp_set_audmode(client);
490 	reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
491 	val = msp_read_dem(client, reg);
492 	msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
493 	/* wake thread when a new input is chosen */
494 	msp_wake_thread(client);
495 	return 0;
496 }
497 
498 static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
499 {
500 	struct msp_state *state = to_state(sd);
501 	struct i2c_client *client = v4l2_get_subdevdata(sd);
502 
503 	if (vt->type != V4L2_TUNER_ANALOG_TV)
504 		return 0;
505 	if (!state->radio) {
506 		if (state->opmode == OPMODE_AUTOSELECT)
507 			msp_detect_stereo(client);
508 		vt->rxsubchans = state->rxsubchans;
509 	}
510 	vt->audmode = state->audmode;
511 	vt->capability |= V4L2_TUNER_CAP_STEREO |
512 		V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
513 	return 0;
514 }
515 
516 static int msp_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
517 {
518 	struct msp_state *state = to_state(sd);
519 	struct i2c_client *client = v4l2_get_subdevdata(sd);
520 
521 	if (state->radio)  /* TODO: add mono/stereo support for radio */
522 		return 0;
523 	if (state->audmode == vt->audmode)
524 		return 0;
525 	state->audmode = vt->audmode;
526 	/* only set audmode */
527 	msp_set_audmode(client);
528 	return 0;
529 }
530 
531 static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
532 {
533 	struct msp_state *state = to_state(sd);
534 	struct i2c_client *client = v4l2_get_subdevdata(sd);
535 
536 	dev_dbg_lvl(&client->dev, 1, msp_debug, "Setting I2S speed to %d\n", freq);
537 
538 	switch (freq) {
539 		case 1024000:
540 			state->i2s_mode = 0;
541 			break;
542 		case 2048000:
543 			state->i2s_mode = 1;
544 			break;
545 		default:
546 			return -EINVAL;
547 	}
548 	return 0;
549 }
550 
551 static int msp_log_status(struct v4l2_subdev *sd)
552 {
553 	struct msp_state *state = to_state(sd);
554 	struct i2c_client *client = v4l2_get_subdevdata(sd);
555 	const char *p;
556 	char prefix[sizeof(sd->name) + 20];
557 
558 	if (state->opmode == OPMODE_AUTOSELECT)
559 		msp_detect_stereo(client);
560 	dev_info(&client->dev, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
561 			client->name, state->rev1, state->rev2);
562 	snprintf(prefix, sizeof(prefix), "%s: Audio:    ", sd->name);
563 	v4l2_ctrl_handler_log_status(&state->hdl, prefix);
564 	switch (state->mode) {
565 		case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
566 		case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
567 		case MSP_MODE_FM_TERRA: p = "Terrestrial FM-mono/stereo"; break;
568 		case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
569 		case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
570 		case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
571 		case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
572 		case MSP_MODE_BTSC: p = "BTSC"; break;
573 		case MSP_MODE_EXTERN: p = "External input"; break;
574 		default: p = "unknown"; break;
575 	}
576 	if (state->mode == MSP_MODE_EXTERN) {
577 		dev_info(&client->dev, "Mode:     %s\n", p);
578 	} else if (state->opmode == OPMODE_MANUAL) {
579 		dev_info(&client->dev, "Mode:     %s (%s%s)\n", p,
580 				(state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
581 				(state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
582 	} else {
583 		if (state->opmode == OPMODE_AUTODETECT)
584 			dev_info(&client->dev, "Mode:     %s\n", p);
585 		dev_info(&client->dev, "Standard: %s (%s%s)\n",
586 				msp_standard_std_name(state->std),
587 				(state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
588 				(state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
589 	}
590 	dev_info(&client->dev, "Audmode:  0x%04x\n", state->audmode);
591 	dev_info(&client->dev, "Routing:  0x%08x (input) 0x%08x (output)\n",
592 			state->route_in, state->route_out);
593 	dev_info(&client->dev, "ACB:      0x%04x\n", state->acb);
594 	return 0;
595 }
596 
597 #ifdef CONFIG_PM_SLEEP
598 static int msp_suspend(struct device *dev)
599 {
600 	struct i2c_client *client = to_i2c_client(dev);
601 	dev_dbg_lvl(&client->dev, 1, msp_debug, "suspend\n");
602 	msp_reset(client);
603 	return 0;
604 }
605 
606 static int msp_resume(struct device *dev)
607 {
608 	struct i2c_client *client = to_i2c_client(dev);
609 	dev_dbg_lvl(&client->dev, 1, msp_debug, "resume\n");
610 	msp_wake_thread(client);
611 	return 0;
612 }
613 #endif
614 
615 /* ----------------------------------------------------------------------- */
616 
617 static const struct v4l2_ctrl_ops msp_ctrl_ops = {
618 	.s_ctrl = msp_s_ctrl,
619 };
620 
621 static const struct v4l2_subdev_core_ops msp_core_ops = {
622 	.log_status = msp_log_status,
623 };
624 
625 static const struct v4l2_subdev_video_ops msp_video_ops = {
626 	.s_std = msp_s_std,
627 	.querystd = msp_querystd,
628 };
629 
630 static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
631 	.s_frequency = msp_s_frequency,
632 	.g_tuner = msp_g_tuner,
633 	.s_tuner = msp_s_tuner,
634 	.s_radio = msp_s_radio,
635 };
636 
637 static const struct v4l2_subdev_audio_ops msp_audio_ops = {
638 	.s_routing = msp_s_routing,
639 	.s_i2s_clock_freq = msp_s_i2s_clock_freq,
640 };
641 
642 static const struct v4l2_subdev_ops msp_ops = {
643 	.core = &msp_core_ops,
644 	.video = &msp_video_ops,
645 	.tuner = &msp_tuner_ops,
646 	.audio = &msp_audio_ops,
647 };
648 
649 /* ----------------------------------------------------------------------- */
650 
651 
652 static const char * const opmode_str[] = {
653 	[OPMODE_MANUAL] = "manual",
654 	[OPMODE_AUTODETECT] = "autodetect",
655 	[OPMODE_AUTOSELECT] = "autodetect and autoselect",
656 };
657 
658 static int msp_probe(struct i2c_client *client)
659 {
660 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
661 	struct msp_state *state;
662 	struct v4l2_subdev *sd;
663 	struct v4l2_ctrl_handler *hdl;
664 	int (*thread_func)(void *data) = NULL;
665 	int msp_hard;
666 	int msp_family;
667 	int msp_revision;
668 	int msp_product, msp_prod_hi, msp_prod_lo;
669 	int msp_rom;
670 #if defined(CONFIG_MEDIA_CONTROLLER)
671 	int ret;
672 #endif
673 
674 	if (!id)
675 		strscpy(client->name, "msp3400", sizeof(client->name));
676 
677 	if (msp_reset(client) == -1) {
678 		dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
679 		return -ENODEV;
680 	}
681 
682 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
683 	if (!state)
684 		return -ENOMEM;
685 
686 	sd = &state->sd;
687 	v4l2_i2c_subdev_init(sd, client, &msp_ops);
688 
689 #if defined(CONFIG_MEDIA_CONTROLLER)
690 	state->pads[MSP3400_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
691 	state->pads[MSP3400_PAD_IF_INPUT].sig_type = PAD_SIGNAL_AUDIO;
692 	state->pads[MSP3400_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
693 	state->pads[MSP3400_PAD_OUT].sig_type = PAD_SIGNAL_AUDIO;
694 
695 	sd->entity.function = MEDIA_ENT_F_IF_AUD_DECODER;
696 
697 	ret = media_entity_pads_init(&sd->entity, 2, state->pads);
698 	if (ret < 0)
699 		return ret;
700 #endif
701 
702 	state->v4l2_std = V4L2_STD_NTSC;
703 	state->detected_std = V4L2_STD_ALL;
704 	state->audmode = V4L2_TUNER_MODE_STEREO;
705 	state->input = -1;
706 	state->i2s_mode = 0;
707 	init_waitqueue_head(&state->wq);
708 	/* These are the reset input/output positions */
709 	state->route_in = MSP_INPUT_DEFAULT;
710 	state->route_out = MSP_OUTPUT_DEFAULT;
711 
712 	state->rev1 = msp_read_dsp(client, 0x1e);
713 	if (state->rev1 != -1)
714 		state->rev2 = msp_read_dsp(client, 0x1f);
715 	dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
716 			state->rev1, state->rev2);
717 	if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
718 		dev_dbg_lvl(&client->dev, 1, msp_debug,
719 				"not an msp3400 (cannot read chip version)\n");
720 		return -ENODEV;
721 	}
722 
723 	msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
724 	msp_product = (state->rev2 >> 8) & 0xff;
725 	msp_prod_hi = msp_product / 10;
726 	msp_prod_lo = msp_product % 10;
727 	msp_revision = (state->rev1 & 0x0f) + '@';
728 	msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
729 	msp_rom = state->rev2 & 0x1f;
730 	/* Rev B=2, C=3, D=4, G=7 */
731 	state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
732 			msp_revision - '@';
733 
734 	/* Has NICAM support: all mspx41x and mspx45x products have NICAM */
735 	state->has_nicam =
736 		msp_prod_hi == 1 || msp_prod_hi == 5;
737 	/* Has radio support: was added with revision G */
738 	state->has_radio =
739 		msp_revision >= 'G';
740 	/* Has headphones output: not for stripped down products */
741 	state->has_headphones =
742 		msp_prod_lo < 5;
743 	/* Has scart2 input: not in stripped down products of the '3' family */
744 	state->has_scart2 =
745 		msp_family >= 4 || msp_prod_lo < 7;
746 	/* Has scart3 input: not in stripped down products of the '3' family */
747 	state->has_scart3 =
748 		msp_family >= 4 || msp_prod_lo < 5;
749 	/* Has scart4 input: not in pre D revisions, not in stripped D revs */
750 	state->has_scart4 =
751 		msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
752 	/* Has scart2 output: not in stripped down products of
753 	 * the '3' family */
754 	state->has_scart2_out =
755 		msp_family >= 4 || msp_prod_lo < 5;
756 	/* Has scart2 a volume control? Not in pre-D revisions. */
757 	state->has_scart2_out_volume =
758 		msp_revision > 'C' && state->has_scart2_out;
759 	/* Has a configurable i2s out? */
760 	state->has_i2s_conf =
761 		msp_revision >= 'G' && msp_prod_lo < 7;
762 	/* Has subwoofer output: not in pre-D revs and not in stripped down
763 	 * products */
764 	state->has_subwoofer =
765 		msp_revision >= 'D' && msp_prod_lo < 5;
766 	/* Has soundprocessing (bass/treble/balance/loudness/equalizer):
767 	 *  not in stripped down products */
768 	state->has_sound_processing =
769 		msp_prod_lo < 7;
770 	/* Has Virtual Dolby Surround: only in msp34x1 */
771 	state->has_virtual_dolby_surround =
772 		msp_revision == 'G' && msp_prod_lo == 1;
773 	/* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
774 	state->has_dolby_pro_logic =
775 		msp_revision == 'G' && msp_prod_lo == 2;
776 	/* The msp343xG supports BTSC only and cannot do Automatic Standard
777 	 * Detection. */
778 	state->force_btsc =
779 		msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
780 
781 	state->opmode = opmode;
782 	if (state->opmode < OPMODE_MANUAL
783 	    || state->opmode > OPMODE_AUTOSELECT) {
784 		/* MSP revision G and up have both autodetect and autoselect */
785 		if (msp_revision >= 'G')
786 			state->opmode = OPMODE_AUTOSELECT;
787 		/* MSP revision D and up have autodetect */
788 		else if (msp_revision >= 'D')
789 			state->opmode = OPMODE_AUTODETECT;
790 		else
791 			state->opmode = OPMODE_MANUAL;
792 	}
793 
794 	hdl = &state->hdl;
795 	v4l2_ctrl_handler_init(hdl, 6);
796 	if (state->has_sound_processing) {
797 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
798 			V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
799 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
800 			V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
801 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
802 			V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
803 	}
804 	state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
805 			V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
806 	v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
807 			V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
808 	state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
809 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
810 	sd->ctrl_handler = hdl;
811 	if (hdl->error) {
812 		int err = hdl->error;
813 
814 		v4l2_ctrl_handler_free(hdl);
815 		return err;
816 	}
817 
818 	v4l2_ctrl_cluster(2, &state->volume);
819 	v4l2_ctrl_handler_setup(hdl);
820 
821 	dev_info(&client->dev,
822 		 "MSP%d4%02d%c-%c%d found on %s: supports %s%s%s, mode is %s\n",
823 		 msp_family, msp_product,
824 		 msp_revision, msp_hard, msp_rom,
825 		 client->adapter->name,
826 		 (state->has_nicam) ? "nicam" : "",
827 		 (state->has_nicam && state->has_radio) ? " and " : "",
828 		 (state->has_radio) ? "radio" : "",
829 		 opmode_str[state->opmode]);
830 
831 	/* version-specific initialization */
832 	switch (state->opmode) {
833 	case OPMODE_MANUAL:
834 		thread_func = msp3400c_thread;
835 		break;
836 	case OPMODE_AUTODETECT:
837 		thread_func = msp3410d_thread;
838 		break;
839 	case OPMODE_AUTOSELECT:
840 		thread_func = msp34xxg_thread;
841 		break;
842 	}
843 
844 	/* startup control thread if needed */
845 	if (thread_func) {
846 		state->kthread = kthread_run(thread_func, client, "msp34xx");
847 
848 		if (IS_ERR(state->kthread))
849 			dev_warn(&client->dev, "kernel_thread() failed\n");
850 		msp_wake_thread(client);
851 	}
852 	return 0;
853 }
854 
855 static void msp_remove(struct i2c_client *client)
856 {
857 	struct msp_state *state = to_state(i2c_get_clientdata(client));
858 
859 	v4l2_device_unregister_subdev(&state->sd);
860 	/* shutdown control thread */
861 	if (state->kthread) {
862 		state->restart = 1;
863 		kthread_stop(state->kthread);
864 	}
865 	msp_reset(client);
866 
867 	v4l2_ctrl_handler_free(&state->hdl);
868 }
869 
870 /* ----------------------------------------------------------------------- */
871 
872 static const struct dev_pm_ops msp3400_pm_ops = {
873 	SET_SYSTEM_SLEEP_PM_OPS(msp_suspend, msp_resume)
874 };
875 
876 static const struct i2c_device_id msp_id[] = {
877 	{ "msp3400" },
878 	{ }
879 };
880 MODULE_DEVICE_TABLE(i2c, msp_id);
881 
882 static struct i2c_driver msp_driver = {
883 	.driver = {
884 		.name	= "msp3400",
885 		.pm	= &msp3400_pm_ops,
886 	},
887 	.probe		= msp_probe,
888 	.remove		= msp_remove,
889 	.id_table	= msp_id,
890 };
891 
892 module_i2c_driver(msp_driver);
893