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 
msp_reset(struct i2c_client * client)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 
msp_read(struct i2c_client * client,int dev,int addr)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 
msp_read_dem(struct i2c_client * client,int addr)186 int msp_read_dem(struct i2c_client *client, int addr)
187 {
188 	return msp_read(client, I2C_MSP_DEM, addr);
189 }
190 
msp_read_dsp(struct i2c_client * client,int addr)191 int msp_read_dsp(struct i2c_client *client, int addr)
192 {
193 	return msp_read(client, I2C_MSP_DSP, addr);
194 }
195 
msp_write(struct i2c_client * client,int dev,int addr,int val)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 
msp_write_dem(struct i2c_client * client,int addr,int val)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 
msp_write_dsp(struct i2c_client * client,int addr,int val)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 
msp_set_scart(struct i2c_client * client,int in,int out)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 
msp_wake_thread(struct i2c_client * client)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 
msp_sleep(struct msp_state * state,int timeout)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 
msp_s_ctrl(struct v4l2_ctrl * ctrl)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 
msp_update_volume(struct msp_state * state)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 --- */
msp_s_radio(struct v4l2_subdev * sd)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 
msp_s_frequency(struct v4l2_subdev * sd,const struct v4l2_frequency * freq)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 
msp_querystd(struct v4l2_subdev * sd,v4l2_std_id * id)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 
msp_s_std(struct v4l2_subdev * sd,v4l2_std_id id)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 
msp_s_routing(struct v4l2_subdev * sd,u32 input,u32 output,u32 config)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 
msp_g_tuner(struct v4l2_subdev * sd,struct v4l2_tuner * vt)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 
msp_s_tuner(struct v4l2_subdev * sd,const struct v4l2_tuner * vt)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 
msp_s_i2s_clock_freq(struct v4l2_subdev * sd,u32 freq)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 
msp_log_status(struct v4l2_subdev * sd)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
msp_suspend(struct device * dev)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 
msp_resume(struct device * dev)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 
msp_probe(struct i2c_client * client,const struct i2c_device_id * id)666 static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
667 {
668 	struct msp_state *state;
669 	struct v4l2_subdev *sd;
670 	struct v4l2_ctrl_handler *hdl;
671 	int (*thread_func)(void *data) = NULL;
672 	int msp_hard;
673 	int msp_family;
674 	int msp_revision;
675 	int msp_product, msp_prod_hi, msp_prod_lo;
676 	int msp_rom;
677 #if defined(CONFIG_MEDIA_CONTROLLER)
678 	int ret;
679 #endif
680 
681 	if (!id)
682 		strscpy(client->name, "msp3400", sizeof(client->name));
683 
684 	if (msp_reset(client) == -1) {
685 		dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
686 		return -ENODEV;
687 	}
688 
689 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
690 	if (!state)
691 		return -ENOMEM;
692 
693 	sd = &state->sd;
694 	v4l2_i2c_subdev_init(sd, client, &msp_ops);
695 
696 #if defined(CONFIG_MEDIA_CONTROLLER)
697 	state->pads[MSP3400_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
698 	state->pads[MSP3400_PAD_IF_INPUT].sig_type = PAD_SIGNAL_AUDIO;
699 	state->pads[MSP3400_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
700 	state->pads[MSP3400_PAD_OUT].sig_type = PAD_SIGNAL_AUDIO;
701 
702 	sd->entity.function = MEDIA_ENT_F_IF_AUD_DECODER;
703 
704 	ret = media_entity_pads_init(&sd->entity, 2, state->pads);
705 	if (ret < 0)
706 		return ret;
707 #endif
708 
709 	state->v4l2_std = V4L2_STD_NTSC;
710 	state->detected_std = V4L2_STD_ALL;
711 	state->audmode = V4L2_TUNER_MODE_STEREO;
712 	state->input = -1;
713 	state->i2s_mode = 0;
714 	init_waitqueue_head(&state->wq);
715 	/* These are the reset input/output positions */
716 	state->route_in = MSP_INPUT_DEFAULT;
717 	state->route_out = MSP_OUTPUT_DEFAULT;
718 
719 	state->rev1 = msp_read_dsp(client, 0x1e);
720 	if (state->rev1 != -1)
721 		state->rev2 = msp_read_dsp(client, 0x1f);
722 	dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
723 			state->rev1, state->rev2);
724 	if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
725 		dev_dbg_lvl(&client->dev, 1, msp_debug,
726 				"not an msp3400 (cannot read chip version)\n");
727 		return -ENODEV;
728 	}
729 
730 	msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
731 	msp_product = (state->rev2 >> 8) & 0xff;
732 	msp_prod_hi = msp_product / 10;
733 	msp_prod_lo = msp_product % 10;
734 	msp_revision = (state->rev1 & 0x0f) + '@';
735 	msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
736 	msp_rom = state->rev2 & 0x1f;
737 	/* Rev B=2, C=3, D=4, G=7 */
738 	state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
739 			msp_revision - '@';
740 
741 	/* Has NICAM support: all mspx41x and mspx45x products have NICAM */
742 	state->has_nicam =
743 		msp_prod_hi == 1 || msp_prod_hi == 5;
744 	/* Has radio support: was added with revision G */
745 	state->has_radio =
746 		msp_revision >= 'G';
747 	/* Has headphones output: not for stripped down products */
748 	state->has_headphones =
749 		msp_prod_lo < 5;
750 	/* Has scart2 input: not in stripped down products of the '3' family */
751 	state->has_scart2 =
752 		msp_family >= 4 || msp_prod_lo < 7;
753 	/* Has scart3 input: not in stripped down products of the '3' family */
754 	state->has_scart3 =
755 		msp_family >= 4 || msp_prod_lo < 5;
756 	/* Has scart4 input: not in pre D revisions, not in stripped D revs */
757 	state->has_scart4 =
758 		msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
759 	/* Has scart2 output: not in stripped down products of
760 	 * the '3' family */
761 	state->has_scart2_out =
762 		msp_family >= 4 || msp_prod_lo < 5;
763 	/* Has scart2 a volume control? Not in pre-D revisions. */
764 	state->has_scart2_out_volume =
765 		msp_revision > 'C' && state->has_scart2_out;
766 	/* Has a configurable i2s out? */
767 	state->has_i2s_conf =
768 		msp_revision >= 'G' && msp_prod_lo < 7;
769 	/* Has subwoofer output: not in pre-D revs and not in stripped down
770 	 * products */
771 	state->has_subwoofer =
772 		msp_revision >= 'D' && msp_prod_lo < 5;
773 	/* Has soundprocessing (bass/treble/balance/loudness/equalizer):
774 	 *  not in stripped down products */
775 	state->has_sound_processing =
776 		msp_prod_lo < 7;
777 	/* Has Virtual Dolby Surround: only in msp34x1 */
778 	state->has_virtual_dolby_surround =
779 		msp_revision == 'G' && msp_prod_lo == 1;
780 	/* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
781 	state->has_dolby_pro_logic =
782 		msp_revision == 'G' && msp_prod_lo == 2;
783 	/* The msp343xG supports BTSC only and cannot do Automatic Standard
784 	 * Detection. */
785 	state->force_btsc =
786 		msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
787 
788 	state->opmode = opmode;
789 	if (state->opmode < OPMODE_MANUAL
790 	    || state->opmode > OPMODE_AUTOSELECT) {
791 		/* MSP revision G and up have both autodetect and autoselect */
792 		if (msp_revision >= 'G')
793 			state->opmode = OPMODE_AUTOSELECT;
794 		/* MSP revision D and up have autodetect */
795 		else if (msp_revision >= 'D')
796 			state->opmode = OPMODE_AUTODETECT;
797 		else
798 			state->opmode = OPMODE_MANUAL;
799 	}
800 
801 	hdl = &state->hdl;
802 	v4l2_ctrl_handler_init(hdl, 6);
803 	if (state->has_sound_processing) {
804 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
805 			V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
806 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
807 			V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
808 		v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
809 			V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
810 	}
811 	state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
812 			V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
813 	v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
814 			V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
815 	state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
816 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
817 	sd->ctrl_handler = hdl;
818 	if (hdl->error) {
819 		int err = hdl->error;
820 
821 		v4l2_ctrl_handler_free(hdl);
822 		return err;
823 	}
824 
825 	v4l2_ctrl_cluster(2, &state->volume);
826 	v4l2_ctrl_handler_setup(hdl);
827 
828 	dev_info(&client->dev,
829 		 "MSP%d4%02d%c-%c%d found on %s: supports %s%s%s, mode is %s\n",
830 		 msp_family, msp_product,
831 		 msp_revision, msp_hard, msp_rom,
832 		 client->adapter->name,
833 		 (state->has_nicam) ? "nicam" : "",
834 		 (state->has_nicam && state->has_radio) ? " and " : "",
835 		 (state->has_radio) ? "radio" : "",
836 		 opmode_str[state->opmode]);
837 
838 	/* version-specific initialization */
839 	switch (state->opmode) {
840 	case OPMODE_MANUAL:
841 		thread_func = msp3400c_thread;
842 		break;
843 	case OPMODE_AUTODETECT:
844 		thread_func = msp3410d_thread;
845 		break;
846 	case OPMODE_AUTOSELECT:
847 		thread_func = msp34xxg_thread;
848 		break;
849 	}
850 
851 	/* startup control thread if needed */
852 	if (thread_func) {
853 		state->kthread = kthread_run(thread_func, client, "msp34xx");
854 
855 		if (IS_ERR(state->kthread))
856 			dev_warn(&client->dev, "kernel_thread() failed\n");
857 		msp_wake_thread(client);
858 	}
859 	return 0;
860 }
861 
msp_remove(struct i2c_client * client)862 static int msp_remove(struct i2c_client *client)
863 {
864 	struct msp_state *state = to_state(i2c_get_clientdata(client));
865 
866 	v4l2_device_unregister_subdev(&state->sd);
867 	/* shutdown control thread */
868 	if (state->kthread) {
869 		state->restart = 1;
870 		kthread_stop(state->kthread);
871 	}
872 	msp_reset(client);
873 
874 	v4l2_ctrl_handler_free(&state->hdl);
875 	return 0;
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