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 timeout)
313 {
314 	DECLARE_WAITQUEUE(wait, current);
315 
316 	add_wait_queue(&state->wq, &wait);
317 	if (!kthread_should_stop()) {
318 		if (timeout < 0) {
319 			set_current_state(TASK_INTERRUPTIBLE);
320 			schedule();
321 		} else {
322 			schedule_timeout_interruptible
323 						(msecs_to_jiffies(timeout));
324 		}
325 	}
326 
327 	remove_wait_queue(&state->wq, &wait);
328 	try_to_freeze();
329 	return state->restart;
330 }
331 
332 /* ------------------------------------------------------------------------ */
333 
334 static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
335 {
336 	struct msp_state *state = ctrl_to_state(ctrl);
337 	struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
338 	int val = ctrl->val;
339 
340 	switch (ctrl->id) {
341 	case V4L2_CID_AUDIO_VOLUME: {
342 		/* audio volume cluster */
343 		int reallymuted = state->muted->val | state->scan_in_progress;
344 
345 		if (!reallymuted)
346 			val = (val * 0x7f / 65535) << 8;
347 
348 		dev_dbg_lvl(&client->dev, 1, msp_debug, "mute=%s scanning=%s volume=%d\n",
349 				state->muted->val ? "on" : "off",
350 				state->scan_in_progress ? "yes" : "no",
351 				state->volume->val);
352 
353 		msp_write_dsp(client, 0x0000, val);
354 		msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
355 		if (state->has_scart2_out_volume)
356 			msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
357 		if (state->has_headphones)
358 			msp_write_dsp(client, 0x0006, val);
359 		break;
360 	}
361 
362 	case V4L2_CID_AUDIO_BASS:
363 		val = ((val - 32768) * 0x60 / 65535) << 8;
364 		msp_write_dsp(client, 0x0002, val);
365 		if (state->has_headphones)
366 			msp_write_dsp(client, 0x0031, val);
367 		break;
368 
369 	case V4L2_CID_AUDIO_TREBLE:
370 		val = ((val - 32768) * 0x60 / 65535) << 8;
371 		msp_write_dsp(client, 0x0003, val);
372 		if (state->has_headphones)
373 			msp_write_dsp(client, 0x0032, val);
374 		break;
375 
376 	case V4L2_CID_AUDIO_LOUDNESS:
377 		val = val ? ((5 * 4) << 8) : 0;
378 		msp_write_dsp(client, 0x0004, val);
379 		if (state->has_headphones)
380 			msp_write_dsp(client, 0x0033, val);
381 		break;
382 
383 	case V4L2_CID_AUDIO_BALANCE:
384 		val = (u8)((val / 256) - 128);
385 		msp_write_dsp(client, 0x0001, val << 8);
386 		if (state->has_headphones)
387 			msp_write_dsp(client, 0x0030, val << 8);
388 		break;
389 
390 	default:
391 		return -EINVAL;
392 	}
393 	return 0;
394 }
395 
396 void msp_update_volume(struct msp_state *state)
397 {
398 	/* Force an update of the volume/mute cluster */
399 	v4l2_ctrl_lock(state->volume);
400 	state->volume->val = state->volume->cur.val;
401 	state->muted->val = state->muted->cur.val;
402 	msp_s_ctrl(state->volume);
403 	v4l2_ctrl_unlock(state->volume);
404 }
405 
406 /* --- v4l2 ioctls --- */
407 static int msp_s_radio(struct v4l2_subdev *sd)
408 {
409 	struct msp_state *state = to_state(sd);
410 	struct i2c_client *client = v4l2_get_subdevdata(sd);
411 
412 	if (state->radio)
413 		return 0;
414 	state->radio = 1;
415 	dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to radio mode\n");
416 	state->watch_stereo = 0;
417 	switch (state->opmode) {
418 	case OPMODE_MANUAL:
419 		/* set msp3400 to FM radio mode */
420 		msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
421 		msp3400c_set_carrier(client, MSP_CARRIER(10.7),
422 				MSP_CARRIER(10.7));
423 		msp_update_volume(state);
424 		break;
425 	case OPMODE_AUTODETECT:
426 	case OPMODE_AUTOSELECT:
427 		/* the thread will do for us */
428 		msp_wake_thread(client);
429 		break;
430 	}
431 	return 0;
432 }
433 
434 static int msp_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
435 {
436 	struct i2c_client *client = v4l2_get_subdevdata(sd);
437 
438 	/* new channel -- kick audio carrier scan */
439 	msp_wake_thread(client);
440 	return 0;
441 }
442 
443 static int msp_querystd(struct v4l2_subdev *sd, v4l2_std_id *id)
444 {
445 	struct msp_state *state = to_state(sd);
446 	struct i2c_client *client = v4l2_get_subdevdata(sd);
447 
448 	*id &= state->detected_std;
449 
450 	dev_dbg_lvl(&client->dev, 2, msp_debug,
451 		"detected standard: %s(0x%08Lx)\n",
452 		msp_standard_std_name(state->std), state->detected_std);
453 
454 	return 0;
455 }
456 
457 static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
458 {
459 	struct msp_state *state = to_state(sd);
460 	struct i2c_client *client = v4l2_get_subdevdata(sd);
461 	int update = state->radio || state->v4l2_std != id;
462 
463 	state->v4l2_std = id;
464 	state->radio = 0;
465 	if (update)
466 		msp_wake_thread(client);
467 	return 0;
468 }
469 
470 static int msp_s_routing(struct v4l2_subdev *sd,
471 			 u32 input, u32 output, u32 config)
472 {
473 	struct msp_state *state = to_state(sd);
474 	struct i2c_client *client = v4l2_get_subdevdata(sd);
475 	int tuner = (input >> 3) & 1;
476 	int sc_in = input & 0x7;
477 	int sc1_out = output & 0xf;
478 	int sc2_out = (output >> 4) & 0xf;
479 	u16 val, reg;
480 	int i;
481 	int extern_input = 1;
482 
483 	if (state->route_in == input && state->route_out == output)
484 		return 0;
485 	state->route_in = input;
486 	state->route_out = output;
487 	/* check if the tuner input is used */
488 	for (i = 0; i < 5; i++) {
489 		if (((input >> (4 + i * 4)) & 0xf) == 0)
490 			extern_input = 0;
491 	}
492 	state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
493 	state->rxsubchans = V4L2_TUNER_SUB_STEREO;
494 	msp_set_scart(client, sc_in, 0);
495 	msp_set_scart(client, sc1_out, 1);
496 	msp_set_scart(client, sc2_out, 2);
497 	msp_set_audmode(client);
498 	reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
499 	val = msp_read_dem(client, reg);
500 	msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
501 	/* wake thread when a new input is chosen */
502 	msp_wake_thread(client);
503 	return 0;
504 }
505 
506 static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
507 {
508 	struct msp_state *state = to_state(sd);
509 	struct i2c_client *client = v4l2_get_subdevdata(sd);
510 
511 	if (vt->type != V4L2_TUNER_ANALOG_TV)
512 		return 0;
513 	if (!state->radio) {
514 		if (state->opmode == OPMODE_AUTOSELECT)
515 			msp_detect_stereo(client);
516 		vt->rxsubchans = state->rxsubchans;
517 	}
518 	vt->audmode = state->audmode;
519 	vt->capability |= V4L2_TUNER_CAP_STEREO |
520 		V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
521 	return 0;
522 }
523 
524 static int msp_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
525 {
526 	struct msp_state *state = to_state(sd);
527 	struct i2c_client *client = v4l2_get_subdevdata(sd);
528 
529 	if (state->radio)  /* TODO: add mono/stereo support for radio */
530 		return 0;
531 	if (state->audmode == vt->audmode)
532 		return 0;
533 	state->audmode = vt->audmode;
534 	/* only set audmode */
535 	msp_set_audmode(client);
536 	return 0;
537 }
538 
539 static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
540 {
541 	struct msp_state *state = to_state(sd);
542 	struct i2c_client *client = v4l2_get_subdevdata(sd);
543 
544 	dev_dbg_lvl(&client->dev, 1, msp_debug, "Setting I2S speed to %d\n", freq);
545 
546 	switch (freq) {
547 		case 1024000:
548 			state->i2s_mode = 0;
549 			break;
550 		case 2048000:
551 			state->i2s_mode = 1;
552 			break;
553 		default:
554 			return -EINVAL;
555 	}
556 	return 0;
557 }
558 
559 static int msp_log_status(struct v4l2_subdev *sd)
560 {
561 	struct msp_state *state = to_state(sd);
562 	struct i2c_client *client = v4l2_get_subdevdata(sd);
563 	const char *p;
564 	char prefix[V4L2_SUBDEV_NAME_SIZE + 20];
565 
566 	if (state->opmode == OPMODE_AUTOSELECT)
567 		msp_detect_stereo(client);
568 	dev_info(&client->dev, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
569 			client->name, state->rev1, state->rev2);
570 	snprintf(prefix, sizeof(prefix), "%s: Audio:    ", sd->name);
571 	v4l2_ctrl_handler_log_status(&state->hdl, prefix);
572 	switch (state->mode) {
573 		case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
574 		case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
575 		case MSP_MODE_FM_TERRA: p = "Terrestrial FM-mono/stereo"; break;
576 		case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
577 		case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
578 		case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
579 		case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
580 		case MSP_MODE_BTSC: p = "BTSC"; break;
581 		case MSP_MODE_EXTERN: p = "External input"; break;
582 		default: p = "unknown"; break;
583 	}
584 	if (state->mode == MSP_MODE_EXTERN) {
585 		dev_info(&client->dev, "Mode:     %s\n", p);
586 	} else if (state->opmode == OPMODE_MANUAL) {
587 		dev_info(&client->dev, "Mode:     %s (%s%s)\n", p,
588 				(state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
589 				(state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
590 	} else {
591 		if (state->opmode == OPMODE_AUTODETECT)
592 			dev_info(&client->dev, "Mode:     %s\n", p);
593 		dev_info(&client->dev, "Standard: %s (%s%s)\n",
594 				msp_standard_std_name(state->std),
595 				(state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
596 				(state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
597 	}
598 	dev_info(&client->dev, "Audmode:  0x%04x\n", state->audmode);
599 	dev_info(&client->dev, "Routing:  0x%08x (input) 0x%08x (output)\n",
600 			state->route_in, state->route_out);
601 	dev_info(&client->dev, "ACB:      0x%04x\n", state->acb);
602 	return 0;
603 }
604 
605 #ifdef CONFIG_PM_SLEEP
606 static int msp_suspend(struct device *dev)
607 {
608 	struct i2c_client *client = to_i2c_client(dev);
609 	dev_dbg_lvl(&client->dev, 1, msp_debug, "suspend\n");
610 	msp_reset(client);
611 	return 0;
612 }
613 
614 static int msp_resume(struct device *dev)
615 {
616 	struct i2c_client *client = to_i2c_client(dev);
617 	dev_dbg_lvl(&client->dev, 1, msp_debug, "resume\n");
618 	msp_wake_thread(client);
619 	return 0;
620 }
621 #endif
622 
623 /* ----------------------------------------------------------------------- */
624 
625 static const struct v4l2_ctrl_ops msp_ctrl_ops = {
626 	.s_ctrl = msp_s_ctrl,
627 };
628 
629 static const struct v4l2_subdev_core_ops msp_core_ops = {
630 	.log_status = msp_log_status,
631 };
632 
633 static const struct v4l2_subdev_video_ops msp_video_ops = {
634 	.s_std = msp_s_std,
635 	.querystd = msp_querystd,
636 };
637 
638 static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
639 	.s_frequency = msp_s_frequency,
640 	.g_tuner = msp_g_tuner,
641 	.s_tuner = msp_s_tuner,
642 	.s_radio = msp_s_radio,
643 };
644 
645 static const struct v4l2_subdev_audio_ops msp_audio_ops = {
646 	.s_routing = msp_s_routing,
647 	.s_i2s_clock_freq = msp_s_i2s_clock_freq,
648 };
649 
650 static const struct v4l2_subdev_ops msp_ops = {
651 	.core = &msp_core_ops,
652 	.video = &msp_video_ops,
653 	.tuner = &msp_tuner_ops,
654 	.audio = &msp_audio_ops,
655 };
656 
657 /* ----------------------------------------------------------------------- */
658 
659 
660 static const char * const opmode_str[] = {
661 	[OPMODE_MANUAL] = "manual",
662 	[OPMODE_AUTODETECT] = "autodetect",
663 	[OPMODE_AUTOSELECT] = "autodetect and autoselect",
664 };
665 
666 static int msp_probe(struct i2c_client *client)
667 {
668 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
669 	struct msp_state *state;
670 	struct v4l2_subdev *sd;
671 	struct v4l2_ctrl_handler *hdl;
672 	int (*thread_func)(void *data) = NULL;
673 	int msp_hard;
674 	int msp_family;
675 	int msp_revision;
676 	int msp_product, msp_prod_hi, msp_prod_lo;
677 	int msp_rom;
678 #if defined(CONFIG_MEDIA_CONTROLLER)
679 	int ret;
680 #endif
681 
682 	if (!id)
683 		strscpy(client->name, "msp3400", sizeof(client->name));
684 
685 	if (msp_reset(client) == -1) {
686 		dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
687 		return -ENODEV;
688 	}
689 
690 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
691 	if (!state)
692 		return -ENOMEM;
693 
694 	sd = &state->sd;
695 	v4l2_i2c_subdev_init(sd, client, &msp_ops);
696 
697 #if defined(CONFIG_MEDIA_CONTROLLER)
698 	state->pads[MSP3400_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
699 	state->pads[MSP3400_PAD_IF_INPUT].sig_type = PAD_SIGNAL_AUDIO;
700 	state->pads[MSP3400_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
701 	state->pads[MSP3400_PAD_OUT].sig_type = PAD_SIGNAL_AUDIO;
702 
703 	sd->entity.function = MEDIA_ENT_F_IF_AUD_DECODER;
704 
705 	ret = media_entity_pads_init(&sd->entity, 2, state->pads);
706 	if (ret < 0)
707 		return ret;
708 #endif
709 
710 	state->v4l2_std = V4L2_STD_NTSC;
711 	state->detected_std = V4L2_STD_ALL;
712 	state->audmode = V4L2_TUNER_MODE_STEREO;
713 	state->input = -1;
714 	state->i2s_mode = 0;
715 	init_waitqueue_head(&state->wq);
716 	/* These are the reset input/output positions */
717 	state->route_in = MSP_INPUT_DEFAULT;
718 	state->route_out = MSP_OUTPUT_DEFAULT;
719 
720 	state->rev1 = msp_read_dsp(client, 0x1e);
721 	if (state->rev1 != -1)
722 		state->rev2 = msp_read_dsp(client, 0x1f);
723 	dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
724 			state->rev1, state->rev2);
725 	if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
726 		dev_dbg_lvl(&client->dev, 1, msp_debug,
727 				"not an msp3400 (cannot read chip version)\n");
728 		return -ENODEV;
729 	}
730 
731 	msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
732 	msp_product = (state->rev2 >> 8) & 0xff;
733 	msp_prod_hi = msp_product / 10;
734 	msp_prod_lo = msp_product % 10;
735 	msp_revision = (state->rev1 & 0x0f) + '@';
736 	msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
737 	msp_rom = state->rev2 & 0x1f;
738 	/* Rev B=2, C=3, D=4, G=7 */
739 	state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
740 			msp_revision - '@';
741 
742 	/* Has NICAM support: all mspx41x and mspx45x products have NICAM */
743 	state->has_nicam =
744 		msp_prod_hi == 1 || msp_prod_hi == 5;
745 	/* Has radio support: was added with revision G */
746 	state->has_radio =
747 		msp_revision >= 'G';
748 	/* Has headphones output: not for stripped down products */
749 	state->has_headphones =
750 		msp_prod_lo < 5;
751 	/* Has scart2 input: not in stripped down products of the '3' family */
752 	state->has_scart2 =
753 		msp_family >= 4 || msp_prod_lo < 7;
754 	/* Has scart3 input: not in stripped down products of the '3' family */
755 	state->has_scart3 =
756 		msp_family >= 4 || msp_prod_lo < 5;
757 	/* Has scart4 input: not in pre D revisions, not in stripped D revs */
758 	state->has_scart4 =
759 		msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
760 	/* Has scart2 output: not in stripped down products of
761 	 * the '3' family */
762 	state->has_scart2_out =
763 		msp_family >= 4 || msp_prod_lo < 5;
764 	/* Has scart2 a volume control? Not in pre-D revisions. */
765 	state->has_scart2_out_volume =
766 		msp_revision > 'C' && state->has_scart2_out;
767 	/* Has a configurable i2s out? */
768 	state->has_i2s_conf =
769 		msp_revision >= 'G' && msp_prod_lo < 7;
770 	/* Has subwoofer output: not in pre-D revs and not in stripped down
771 	 * products */
772 	state->has_subwoofer =
773 		msp_revision >= 'D' && msp_prod_lo < 5;
774 	/* Has soundprocessing (bass/treble/balance/loudness/equalizer):
775 	 *  not in stripped down products */
776 	state->has_sound_processing =
777 		msp_prod_lo < 7;
778 	/* Has Virtual Dolby Surround: only in msp34x1 */
779 	state->has_virtual_dolby_surround =
780 		msp_revision == 'G' && msp_prod_lo == 1;
781 	/* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
782 	state->has_dolby_pro_logic =
783 		msp_revision == 'G' && msp_prod_lo == 2;
784 	/* The msp343xG supports BTSC only and cannot do Automatic Standard
785 	 * Detection. */
786 	state->force_btsc =
787 		msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
788 
789 	state->opmode = opmode;
790 	if (state->opmode < OPMODE_MANUAL
791 	    || state->opmode > OPMODE_AUTOSELECT) {
792 		/* MSP revision G and up have both autodetect and autoselect */
793 		if (msp_revision >= 'G')
794 			state->opmode = OPMODE_AUTOSELECT;
795 		/* MSP revision D and up have autodetect */
796 		else if (msp_revision >= 'D')
797 			state->opmode = OPMODE_AUTODETECT;
798 		else
799 			state->opmode = OPMODE_MANUAL;
800 	}
801 
802 	hdl = &state->hdl;
803 	v4l2_ctrl_handler_init(hdl, 6);
804 	if (state->has_sound_processing) {
805 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
806 			V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
807 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
808 			V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
809 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
810 			V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
811 	}
812 	state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
813 			V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
814 	v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
815 			V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
816 	state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
817 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
818 	sd->ctrl_handler = hdl;
819 	if (hdl->error) {
820 		int err = hdl->error;
821 
822 		v4l2_ctrl_handler_free(hdl);
823 		return err;
824 	}
825 
826 	v4l2_ctrl_cluster(2, &state->volume);
827 	v4l2_ctrl_handler_setup(hdl);
828 
829 	dev_info(&client->dev,
830 		 "MSP%d4%02d%c-%c%d found on %s: supports %s%s%s, mode is %s\n",
831 		 msp_family, msp_product,
832 		 msp_revision, msp_hard, msp_rom,
833 		 client->adapter->name,
834 		 (state->has_nicam) ? "nicam" : "",
835 		 (state->has_nicam && state->has_radio) ? " and " : "",
836 		 (state->has_radio) ? "radio" : "",
837 		 opmode_str[state->opmode]);
838 
839 	/* version-specific initialization */
840 	switch (state->opmode) {
841 	case OPMODE_MANUAL:
842 		thread_func = msp3400c_thread;
843 		break;
844 	case OPMODE_AUTODETECT:
845 		thread_func = msp3410d_thread;
846 		break;
847 	case OPMODE_AUTOSELECT:
848 		thread_func = msp34xxg_thread;
849 		break;
850 	}
851 
852 	/* startup control thread if needed */
853 	if (thread_func) {
854 		state->kthread = kthread_run(thread_func, client, "msp34xx");
855 
856 		if (IS_ERR(state->kthread))
857 			dev_warn(&client->dev, "kernel_thread() failed\n");
858 		msp_wake_thread(client);
859 	}
860 	return 0;
861 }
862 
863 static void msp_remove(struct i2c_client *client)
864 {
865 	struct msp_state *state = to_state(i2c_get_clientdata(client));
866 
867 	v4l2_device_unregister_subdev(&state->sd);
868 	/* shutdown control thread */
869 	if (state->kthread) {
870 		state->restart = 1;
871 		kthread_stop(state->kthread);
872 	}
873 	msp_reset(client);
874 
875 	v4l2_ctrl_handler_free(&state->hdl);
876 }
877 
878 /* ----------------------------------------------------------------------- */
879 
880 static const struct dev_pm_ops msp3400_pm_ops = {
881 	SET_SYSTEM_SLEEP_PM_OPS(msp_suspend, msp_resume)
882 };
883 
884 static const struct i2c_device_id msp_id[] = {
885 	{ "msp3400", 0 },
886 	{ }
887 };
888 MODULE_DEVICE_TABLE(i2c, msp_id);
889 
890 static struct i2c_driver msp_driver = {
891 	.driver = {
892 		.name	= "msp3400",
893 		.pm	= &msp3400_pm_ops,
894 	},
895 	.probe		= msp_probe,
896 	.remove		= msp_remove,
897 	.id_table	= msp_id,
898 };
899 
900 module_i2c_driver(msp_driver);
901