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