xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/msp3400-kthreads.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 
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/i2c.h>
12*4882a593Smuzhiyun #include <linux/freezer.h>
13*4882a593Smuzhiyun #include <linux/videodev2.h>
14*4882a593Smuzhiyun #include <media/v4l2-common.h>
15*4882a593Smuzhiyun #include <media/drv-intf/msp3400.h>
16*4882a593Smuzhiyun #include <linux/kthread.h>
17*4882a593Smuzhiyun #include <linux/suspend.h>
18*4882a593Smuzhiyun #include "msp3400-driver.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* this one uses the automatic sound standard detection of newer msp34xx
21*4882a593Smuzhiyun    chip versions */
22*4882a593Smuzhiyun static struct {
23*4882a593Smuzhiyun 	int retval;
24*4882a593Smuzhiyun 	int main, second;
25*4882a593Smuzhiyun 	char *name;
26*4882a593Smuzhiyun 	v4l2_std_id std;
27*4882a593Smuzhiyun } msp_stdlist[] = {
28*4882a593Smuzhiyun 	{ 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL },
29*4882a593Smuzhiyun 	{ 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL },
30*4882a593Smuzhiyun 	{ 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
31*4882a593Smuzhiyun 	  "4.5/4.72  M Dual FM-Stereo", V4L2_STD_MN },
32*4882a593Smuzhiyun 	{ 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
33*4882a593Smuzhiyun 	  "5.5/5.74  B/G Dual FM-Stereo", V4L2_STD_BG },
34*4882a593Smuzhiyun 	{ 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
35*4882a593Smuzhiyun 	  "6.5/6.25  D/K1 Dual FM-Stereo", V4L2_STD_DK },
36*4882a593Smuzhiyun 	{ 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
37*4882a593Smuzhiyun 	  "6.5/6.74  D/K2 Dual FM-Stereo", V4L2_STD_DK },
38*4882a593Smuzhiyun 	{ 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
39*4882a593Smuzhiyun 	  "6.5  D/K FM-Mono (HDEV3)", V4L2_STD_DK },
40*4882a593Smuzhiyun 	{ 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
41*4882a593Smuzhiyun 	  "6.5/5.74  D/K3 Dual FM-Stereo", V4L2_STD_DK },
42*4882a593Smuzhiyun 	{ 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
43*4882a593Smuzhiyun 	  "5.5/5.85  B/G NICAM FM", V4L2_STD_BG },
44*4882a593Smuzhiyun 	{ 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
45*4882a593Smuzhiyun 	  "6.5/5.85  L NICAM AM", V4L2_STD_L },
46*4882a593Smuzhiyun 	{ 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
47*4882a593Smuzhiyun 	  "6.0/6.55  I NICAM FM", V4L2_STD_PAL_I },
48*4882a593Smuzhiyun 	{ 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
49*4882a593Smuzhiyun 	  "6.5/5.85  D/K NICAM FM", V4L2_STD_DK },
50*4882a593Smuzhiyun 	{ 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
51*4882a593Smuzhiyun 	  "6.5/5.85  D/K NICAM FM (HDEV2)", V4L2_STD_DK },
52*4882a593Smuzhiyun 	{ 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
53*4882a593Smuzhiyun 	  "6.5/5.85  D/K NICAM FM (HDEV3)", V4L2_STD_DK },
54*4882a593Smuzhiyun 	{ 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
55*4882a593Smuzhiyun 	  "4.5  M BTSC-Stereo", V4L2_STD_MTS },
56*4882a593Smuzhiyun 	{ 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
57*4882a593Smuzhiyun 	  "4.5  M BTSC-Mono + SAP", V4L2_STD_MTS },
58*4882a593Smuzhiyun 	{ 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
59*4882a593Smuzhiyun 	  "4.5  M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP },
60*4882a593Smuzhiyun 	{ 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
61*4882a593Smuzhiyun 	  "10.7  FM-Stereo Radio", V4L2_STD_ALL },
62*4882a593Smuzhiyun 	{ 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
63*4882a593Smuzhiyun 	  "6.5  SAT-Mono", V4L2_STD_ALL },
64*4882a593Smuzhiyun 	{ 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
65*4882a593Smuzhiyun 	  "7.02/7.20  SAT-Stereo", V4L2_STD_ALL },
66*4882a593Smuzhiyun 	{ 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
67*4882a593Smuzhiyun 	  "7.2  SAT ADR", V4L2_STD_ALL },
68*4882a593Smuzhiyun 	{     -1, 0, 0, NULL, 0 }, /* EOF */
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun static struct msp3400c_init_data_dem {
72*4882a593Smuzhiyun 	int fir1[6];
73*4882a593Smuzhiyun 	int fir2[6];
74*4882a593Smuzhiyun 	int cdo1;
75*4882a593Smuzhiyun 	int cdo2;
76*4882a593Smuzhiyun 	int ad_cv;
77*4882a593Smuzhiyun 	int mode_reg;
78*4882a593Smuzhiyun 	int dsp_src;
79*4882a593Smuzhiyun 	int dsp_matrix;
80*4882a593Smuzhiyun } msp3400c_init_data[] = {
81*4882a593Smuzhiyun 	{	/* AM (for carrier detect / msp3400) */
82*4882a593Smuzhiyun 		{75, 19, 36, 35, 39, 40},
83*4882a593Smuzhiyun 		{75, 19, 36, 35, 39, 40},
84*4882a593Smuzhiyun 		MSP_CARRIER(5.5), MSP_CARRIER(5.5),
85*4882a593Smuzhiyun 		0x00d0, 0x0500, 0x0020, 0x3000
86*4882a593Smuzhiyun 	}, {	/* AM (for carrier detect / msp3410) */
87*4882a593Smuzhiyun 		{-1, -1, -8, 2, 59, 126},
88*4882a593Smuzhiyun 		{-1, -1, -8, 2, 59, 126},
89*4882a593Smuzhiyun 		MSP_CARRIER(5.5), MSP_CARRIER(5.5),
90*4882a593Smuzhiyun 		0x00d0, 0x0100, 0x0020, 0x3000
91*4882a593Smuzhiyun 	}, {	/* FM Radio */
92*4882a593Smuzhiyun 		{-8, -8, 4, 6, 78, 107},
93*4882a593Smuzhiyun 		{-8, -8, 4, 6, 78, 107},
94*4882a593Smuzhiyun 		MSP_CARRIER(10.7), MSP_CARRIER(10.7),
95*4882a593Smuzhiyun 		0x00d0, 0x0480, 0x0020, 0x3000
96*4882a593Smuzhiyun 	}, {	/* Terrestrial FM-mono + FM-stereo */
97*4882a593Smuzhiyun 		{3, 18, 27, 48, 66, 72},
98*4882a593Smuzhiyun 		{3, 18, 27, 48, 66, 72},
99*4882a593Smuzhiyun 		MSP_CARRIER(5.5), MSP_CARRIER(5.5),
100*4882a593Smuzhiyun 		0x00d0, 0x0480, 0x0030, 0x3000
101*4882a593Smuzhiyun 	}, {	/* Sat FM-mono */
102*4882a593Smuzhiyun 		{ 1, 9, 14, 24, 33, 37},
103*4882a593Smuzhiyun 		{ 3, 18, 27, 48, 66, 72},
104*4882a593Smuzhiyun 		MSP_CARRIER(6.5), MSP_CARRIER(6.5),
105*4882a593Smuzhiyun 		0x00c6, 0x0480, 0x0000, 0x3000
106*4882a593Smuzhiyun 	}, {	/* NICAM/FM --  B/G (5.5/5.85), D/K (6.5/5.85) */
107*4882a593Smuzhiyun 		{-2, -8, -10, 10, 50, 86},
108*4882a593Smuzhiyun 		{3, 18, 27, 48, 66, 72},
109*4882a593Smuzhiyun 		MSP_CARRIER(5.5), MSP_CARRIER(5.5),
110*4882a593Smuzhiyun 		0x00d0, 0x0040, 0x0120, 0x3000
111*4882a593Smuzhiyun 	}, {	/* NICAM/FM -- I (6.0/6.552) */
112*4882a593Smuzhiyun 		{2, 4, -6, -4, 40, 94},
113*4882a593Smuzhiyun 		{3, 18, 27, 48, 66, 72},
114*4882a593Smuzhiyun 		MSP_CARRIER(6.0), MSP_CARRIER(6.0),
115*4882a593Smuzhiyun 		0x00d0, 0x0040, 0x0120, 0x3000
116*4882a593Smuzhiyun 	}, {	/* NICAM/AM -- L (6.5/5.85) */
117*4882a593Smuzhiyun 		{-2, -8, -10, 10, 50, 86},
118*4882a593Smuzhiyun 		{-4, -12, -9, 23, 79, 126},
119*4882a593Smuzhiyun 		MSP_CARRIER(6.5), MSP_CARRIER(6.5),
120*4882a593Smuzhiyun 		0x00c6, 0x0140, 0x0120, 0x7c00
121*4882a593Smuzhiyun 	},
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun struct msp3400c_carrier_detect {
125*4882a593Smuzhiyun 	int   cdo;
126*4882a593Smuzhiyun 	char *name;
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
130*4882a593Smuzhiyun 	/* main carrier */
131*4882a593Smuzhiyun 	{ MSP_CARRIER(4.5),        "4.5   NTSC"                   },
132*4882a593Smuzhiyun 	{ MSP_CARRIER(5.5),        "5.5   PAL B/G"                },
133*4882a593Smuzhiyun 	{ MSP_CARRIER(6.0),        "6.0   PAL I"                  },
134*4882a593Smuzhiyun 	{ MSP_CARRIER(6.5),        "6.5   PAL D/K + SAT + SECAM"  }
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
138*4882a593Smuzhiyun 	/* PAL B/G */
139*4882a593Smuzhiyun 	{ MSP_CARRIER(5.7421875),  "5.742 PAL B/G FM-stereo"     },
140*4882a593Smuzhiyun 	{ MSP_CARRIER(5.85),       "5.85  PAL B/G NICAM"         }
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
144*4882a593Smuzhiyun 	/* PAL SAT / SECAM */
145*4882a593Smuzhiyun 	{ MSP_CARRIER(5.85),       "5.85  PAL D/K + SECAM NICAM" },
146*4882a593Smuzhiyun 	{ MSP_CARRIER(6.2578125),  "6.25  PAL D/K1 FM-stereo" },
147*4882a593Smuzhiyun 	{ MSP_CARRIER(6.7421875),  "6.74  PAL D/K2 FM-stereo" },
148*4882a593Smuzhiyun 	{ MSP_CARRIER(7.02),       "7.02  PAL SAT FM-stereo s/b" },
149*4882a593Smuzhiyun 	{ MSP_CARRIER(7.20),       "7.20  PAL SAT FM-stereo s"   },
150*4882a593Smuzhiyun 	{ MSP_CARRIER(7.38),       "7.38  PAL SAT FM-stereo b"   },
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /* ------------------------------------------------------------------------ */
154*4882a593Smuzhiyun 
msp_standard_std_name(int std)155*4882a593Smuzhiyun const char *msp_standard_std_name(int std)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	int i;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	for (i = 0; msp_stdlist[i].name != NULL; i++)
160*4882a593Smuzhiyun 		if (msp_stdlist[i].retval == std)
161*4882a593Smuzhiyun 			return msp_stdlist[i].name;
162*4882a593Smuzhiyun 	return "unknown";
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
msp_standard_std(int std)165*4882a593Smuzhiyun static v4l2_std_id msp_standard_std(int std)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	int i;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	for (i = 0; msp_stdlist[i].name != NULL; i++)
170*4882a593Smuzhiyun 		if (msp_stdlist[i].retval == std)
171*4882a593Smuzhiyun 			return msp_stdlist[i].std;
172*4882a593Smuzhiyun 	return V4L2_STD_ALL;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
msp_set_source(struct i2c_client * client,u16 src)175*4882a593Smuzhiyun static void msp_set_source(struct i2c_client *client, u16 src)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (msp_dolby) {
180*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
181*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
182*4882a593Smuzhiyun 	} else {
183*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0008, src);
184*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0009, src);
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 	msp_write_dsp(client, 0x000a, src);
187*4882a593Smuzhiyun 	msp_write_dsp(client, 0x000b, src);
188*4882a593Smuzhiyun 	msp_write_dsp(client, 0x000c, src);
189*4882a593Smuzhiyun 	if (state->has_scart2_out)
190*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0041, src);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
msp3400c_set_carrier(struct i2c_client * client,int cdo1,int cdo2)193*4882a593Smuzhiyun void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	msp_write_dem(client, 0x0093, cdo1 & 0xfff);
196*4882a593Smuzhiyun 	msp_write_dem(client, 0x009b, cdo1 >> 12);
197*4882a593Smuzhiyun 	msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
198*4882a593Smuzhiyun 	msp_write_dem(client, 0x00ab, cdo2 >> 12);
199*4882a593Smuzhiyun 	msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
msp3400c_set_mode(struct i2c_client * client,int mode)202*4882a593Smuzhiyun void msp3400c_set_mode(struct i2c_client *client, int mode)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
205*4882a593Smuzhiyun 	struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
206*4882a593Smuzhiyun 	int tuner = (state->route_in >> 3) & 1;
207*4882a593Smuzhiyun 	int i;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode);
210*4882a593Smuzhiyun 	state->mode = mode;
211*4882a593Smuzhiyun 	state->rxsubchans = V4L2_TUNER_SUB_MONO;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	for (i = 5; i >= 0; i--)               /* fir 1 */
216*4882a593Smuzhiyun 		msp_write_dem(client, 0x0001, data->fir1[i]);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
219*4882a593Smuzhiyun 	msp_write_dem(client, 0x0005, 0x0040);
220*4882a593Smuzhiyun 	msp_write_dem(client, 0x0005, 0x0000);
221*4882a593Smuzhiyun 	for (i = 5; i >= 0; i--)
222*4882a593Smuzhiyun 		msp_write_dem(client, 0x0005, data->fir2[i]);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	msp_write_dem(client, 0x0083, data->mode_reg);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	msp3400c_set_carrier(client, data->cdo1, data->cdo2);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	msp_set_source(client, data->dsp_src);
229*4882a593Smuzhiyun 	/* set prescales */
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* volume prescale for SCART (AM mono input) */
232*4882a593Smuzhiyun 	msp_write_dsp(client, 0x000d, 0x1900);
233*4882a593Smuzhiyun 	msp_write_dsp(client, 0x000e, data->dsp_matrix);
234*4882a593Smuzhiyun 	if (state->has_nicam) /* nicam prescale */
235*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0010, 0x5a00);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
239*4882a593Smuzhiyun    nor do they support stereo BTSC. */
msp3400c_set_audmode(struct i2c_client * client)240*4882a593Smuzhiyun static void msp3400c_set_audmode(struct i2c_client *client)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	static char *strmode[] = {
243*4882a593Smuzhiyun 		"mono", "stereo", "lang2", "lang1", "lang1+lang2"
244*4882a593Smuzhiyun 	};
245*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
246*4882a593Smuzhiyun 	char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
247*4882a593Smuzhiyun 		strmode[state->audmode] : "unknown";
248*4882a593Smuzhiyun 	int src = 0;	/* channel source: FM/AM, nicam or SCART */
249*4882a593Smuzhiyun 	int audmode = state->audmode;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	if (state->opmode == OPMODE_AUTOSELECT) {
252*4882a593Smuzhiyun 		/* this method would break everything, let's make sure
253*4882a593Smuzhiyun 		 * it's never called
254*4882a593Smuzhiyun 		 */
255*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
256*4882a593Smuzhiyun 			"set_audmode called with mode=%d instead of set_source (ignored)\n",
257*4882a593Smuzhiyun 			state->audmode);
258*4882a593Smuzhiyun 		return;
259*4882a593Smuzhiyun 	}
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* Note: for the C and D revs no NTSC stereo + SAP is possible as
262*4882a593Smuzhiyun 	   the hardware does not support SAP. So the rxsubchans combination
263*4882a593Smuzhiyun 	   of STEREO | LANG2 does not occur. */
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	if (state->mode != MSP_MODE_EXTERN) {
266*4882a593Smuzhiyun 		/* switch to mono if only mono is available */
267*4882a593Smuzhiyun 		if (state->rxsubchans == V4L2_TUNER_SUB_MONO)
268*4882a593Smuzhiyun 			audmode = V4L2_TUNER_MODE_MONO;
269*4882a593Smuzhiyun 		/* if bilingual */
270*4882a593Smuzhiyun 		else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) {
271*4882a593Smuzhiyun 			/* and mono or stereo, then fallback to lang1 */
272*4882a593Smuzhiyun 			if (audmode == V4L2_TUNER_MODE_MONO ||
273*4882a593Smuzhiyun 			    audmode == V4L2_TUNER_MODE_STEREO)
274*4882a593Smuzhiyun 				audmode = V4L2_TUNER_MODE_LANG1;
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 		/* if stereo, and audmode is not mono, then switch to stereo */
277*4882a593Smuzhiyun 		else if (audmode != V4L2_TUNER_MODE_MONO)
278*4882a593Smuzhiyun 			audmode = V4L2_TUNER_MODE_STEREO;
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* switch demodulator */
282*4882a593Smuzhiyun 	switch (state->mode) {
283*4882a593Smuzhiyun 	case MSP_MODE_FM_TERRA:
284*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr);
285*4882a593Smuzhiyun 		switch (audmode) {
286*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_STEREO:
287*4882a593Smuzhiyun 			msp_write_dsp(client, 0x000e, 0x3001);
288*4882a593Smuzhiyun 			break;
289*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_MONO:
290*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_LANG1:
291*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_LANG2:
292*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_LANG1_LANG2:
293*4882a593Smuzhiyun 			msp_write_dsp(client, 0x000e, 0x3000);
294*4882a593Smuzhiyun 			break;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 		break;
297*4882a593Smuzhiyun 	case MSP_MODE_FM_SAT:
298*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr);
299*4882a593Smuzhiyun 		switch (audmode) {
300*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_MONO:
301*4882a593Smuzhiyun 			msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
302*4882a593Smuzhiyun 			break;
303*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_STEREO:
304*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_LANG1_LANG2:
305*4882a593Smuzhiyun 			msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
306*4882a593Smuzhiyun 			break;
307*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_LANG1:
308*4882a593Smuzhiyun 			msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
309*4882a593Smuzhiyun 			break;
310*4882a593Smuzhiyun 		case V4L2_TUNER_MODE_LANG2:
311*4882a593Smuzhiyun 			msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
312*4882a593Smuzhiyun 			break;
313*4882a593Smuzhiyun 		}
314*4882a593Smuzhiyun 		break;
315*4882a593Smuzhiyun 	case MSP_MODE_FM_NICAM1:
316*4882a593Smuzhiyun 	case MSP_MODE_FM_NICAM2:
317*4882a593Smuzhiyun 	case MSP_MODE_AM_NICAM:
318*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
319*4882a593Smuzhiyun 			"NICAM set_audmode: %s\n", modestr);
320*4882a593Smuzhiyun 		if (state->nicam_on)
321*4882a593Smuzhiyun 			src = 0x0100;  /* NICAM */
322*4882a593Smuzhiyun 		break;
323*4882a593Smuzhiyun 	case MSP_MODE_BTSC:
324*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
325*4882a593Smuzhiyun 			"BTSC set_audmode: %s\n", modestr);
326*4882a593Smuzhiyun 		break;
327*4882a593Smuzhiyun 	case MSP_MODE_EXTERN:
328*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
329*4882a593Smuzhiyun 			"extern set_audmode: %s\n", modestr);
330*4882a593Smuzhiyun 		src = 0x0200;  /* SCART */
331*4882a593Smuzhiyun 		break;
332*4882a593Smuzhiyun 	case MSP_MODE_FM_RADIO:
333*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
334*4882a593Smuzhiyun 			"FM-Radio set_audmode: %s\n", modestr);
335*4882a593Smuzhiyun 		break;
336*4882a593Smuzhiyun 	default:
337*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n");
338*4882a593Smuzhiyun 		return;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/* switch audio */
342*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode);
343*4882a593Smuzhiyun 	switch (audmode) {
344*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_STEREO:
345*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_LANG1_LANG2:
346*4882a593Smuzhiyun 		src |= 0x0020;
347*4882a593Smuzhiyun 		break;
348*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_MONO:
349*4882a593Smuzhiyun 		if (state->mode == MSP_MODE_AM_NICAM) {
350*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n");
351*4882a593Smuzhiyun 			/* AM mono decoding is handled by tuner, not MSP chip */
352*4882a593Smuzhiyun 			/* SCART switching control register */
353*4882a593Smuzhiyun 			msp_set_scart(client, SCART_MONO, 0);
354*4882a593Smuzhiyun 			src = 0x0200;
355*4882a593Smuzhiyun 			break;
356*4882a593Smuzhiyun 		}
357*4882a593Smuzhiyun 		if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
358*4882a593Smuzhiyun 			src = 0x0030;
359*4882a593Smuzhiyun 		break;
360*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_LANG1:
361*4882a593Smuzhiyun 		break;
362*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_LANG2:
363*4882a593Smuzhiyun 		src |= 0x0010;
364*4882a593Smuzhiyun 		break;
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug,
367*4882a593Smuzhiyun 		"set_audmode final source/matrix = 0x%x\n", src);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	msp_set_source(client, src);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
msp3400c_print_mode(struct i2c_client * client)372*4882a593Smuzhiyun static void msp3400c_print_mode(struct i2c_client *client)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (state->main == state->second)
377*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
378*4882a593Smuzhiyun 			"mono sound carrier: %d.%03d MHz\n",
379*4882a593Smuzhiyun 			state->main / 910000, (state->main / 910) % 1000);
380*4882a593Smuzhiyun 	else
381*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
382*4882a593Smuzhiyun 			"main sound carrier: %d.%03d MHz\n",
383*4882a593Smuzhiyun 			state->main / 910000, (state->main / 910) % 1000);
384*4882a593Smuzhiyun 	if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
385*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
386*4882a593Smuzhiyun 			"NICAM/FM carrier  : %d.%03d MHz\n",
387*4882a593Smuzhiyun 			state->second / 910000, (state->second/910) % 1000);
388*4882a593Smuzhiyun 	if (state->mode == MSP_MODE_AM_NICAM)
389*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
390*4882a593Smuzhiyun 			"NICAM/AM carrier  : %d.%03d MHz\n",
391*4882a593Smuzhiyun 			state->second / 910000, (state->second / 910) % 1000);
392*4882a593Smuzhiyun 	if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
393*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
394*4882a593Smuzhiyun 			"FM-stereo carrier : %d.%03d MHz\n",
395*4882a593Smuzhiyun 			state->second / 910000, (state->second / 910) % 1000);
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun /* ----------------------------------------------------------------------- */
400*4882a593Smuzhiyun 
msp3400c_detect_stereo(struct i2c_client * client)401*4882a593Smuzhiyun static int msp3400c_detect_stereo(struct i2c_client *client)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
404*4882a593Smuzhiyun 	int val;
405*4882a593Smuzhiyun 	int rxsubchans = state->rxsubchans;
406*4882a593Smuzhiyun 	int newnicam = state->nicam_on;
407*4882a593Smuzhiyun 	int update = 0;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	switch (state->mode) {
410*4882a593Smuzhiyun 	case MSP_MODE_FM_TERRA:
411*4882a593Smuzhiyun 		val = msp_read_dsp(client, 0x18);
412*4882a593Smuzhiyun 		if (val > 32767)
413*4882a593Smuzhiyun 			val -= 65536;
414*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug,
415*4882a593Smuzhiyun 			"stereo detect register: %d\n", val);
416*4882a593Smuzhiyun 		if (val > 8192) {
417*4882a593Smuzhiyun 			rxsubchans = V4L2_TUNER_SUB_STEREO;
418*4882a593Smuzhiyun 		} else if (val < -4096) {
419*4882a593Smuzhiyun 			rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
420*4882a593Smuzhiyun 		} else {
421*4882a593Smuzhiyun 			rxsubchans = V4L2_TUNER_SUB_MONO;
422*4882a593Smuzhiyun 		}
423*4882a593Smuzhiyun 		newnicam = 0;
424*4882a593Smuzhiyun 		break;
425*4882a593Smuzhiyun 	case MSP_MODE_FM_NICAM1:
426*4882a593Smuzhiyun 	case MSP_MODE_FM_NICAM2:
427*4882a593Smuzhiyun 	case MSP_MODE_AM_NICAM:
428*4882a593Smuzhiyun 		val = msp_read_dem(client, 0x23);
429*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n",
430*4882a593Smuzhiyun 			val & 1, (val & 0x1e) >> 1);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 		if (val & 1) {
433*4882a593Smuzhiyun 			/* nicam synced */
434*4882a593Smuzhiyun 			switch ((val & 0x1e) >> 1)  {
435*4882a593Smuzhiyun 			case 0:
436*4882a593Smuzhiyun 			case 8:
437*4882a593Smuzhiyun 				rxsubchans = V4L2_TUNER_SUB_STEREO;
438*4882a593Smuzhiyun 				break;
439*4882a593Smuzhiyun 			case 1:
440*4882a593Smuzhiyun 			case 9:
441*4882a593Smuzhiyun 				rxsubchans = V4L2_TUNER_SUB_MONO;
442*4882a593Smuzhiyun 				break;
443*4882a593Smuzhiyun 			case 2:
444*4882a593Smuzhiyun 			case 10:
445*4882a593Smuzhiyun 				rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
446*4882a593Smuzhiyun 				break;
447*4882a593Smuzhiyun 			default:
448*4882a593Smuzhiyun 				rxsubchans = V4L2_TUNER_SUB_MONO;
449*4882a593Smuzhiyun 				break;
450*4882a593Smuzhiyun 			}
451*4882a593Smuzhiyun 			newnicam = 1;
452*4882a593Smuzhiyun 		} else {
453*4882a593Smuzhiyun 			newnicam = 0;
454*4882a593Smuzhiyun 			rxsubchans = V4L2_TUNER_SUB_MONO;
455*4882a593Smuzhiyun 		}
456*4882a593Smuzhiyun 		break;
457*4882a593Smuzhiyun 	}
458*4882a593Smuzhiyun 	if (rxsubchans != state->rxsubchans) {
459*4882a593Smuzhiyun 		update = 1;
460*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
461*4882a593Smuzhiyun 			"watch: rxsubchans %02x => %02x\n",
462*4882a593Smuzhiyun 			state->rxsubchans, rxsubchans);
463*4882a593Smuzhiyun 		state->rxsubchans = rxsubchans;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 	if (newnicam != state->nicam_on) {
466*4882a593Smuzhiyun 		update = 1;
467*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n",
468*4882a593Smuzhiyun 			state->nicam_on, newnicam);
469*4882a593Smuzhiyun 		state->nicam_on = newnicam;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 	return update;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun  * A kernel thread for msp3400 control -- we don't want to block the
476*4882a593Smuzhiyun  * in the ioctl while doing the sound carrier & stereo detect
477*4882a593Smuzhiyun  */
478*4882a593Smuzhiyun /* stereo/multilang monitoring */
watch_stereo(struct i2c_client * client)479*4882a593Smuzhiyun static void watch_stereo(struct i2c_client *client)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (msp_detect_stereo(client))
484*4882a593Smuzhiyun 		msp_set_audmode(client);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (msp_once)
487*4882a593Smuzhiyun 		state->watch_stereo = 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
msp3400c_thread(void * data)490*4882a593Smuzhiyun int msp3400c_thread(void *data)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun 	struct i2c_client *client = data;
493*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
494*4882a593Smuzhiyun 	struct msp3400c_carrier_detect *cd;
495*4882a593Smuzhiyun 	int count, max1, max2, val1, val2, val, i;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n");
498*4882a593Smuzhiyun 	state->detected_std = V4L2_STD_ALL;
499*4882a593Smuzhiyun 	set_freezable();
500*4882a593Smuzhiyun 	for (;;) {
501*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n");
502*4882a593Smuzhiyun 		msp_sleep(state, -1);
503*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n");
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun restart:
506*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
507*4882a593Smuzhiyun 		state->restart = 0;
508*4882a593Smuzhiyun 		if (kthread_should_stop())
509*4882a593Smuzhiyun 			break;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 		if (state->radio || MSP_MODE_EXTERN == state->mode) {
512*4882a593Smuzhiyun 			/* no carrier scan, just unmute */
513*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug,
514*4882a593Smuzhiyun 				"thread: no carrier scan\n");
515*4882a593Smuzhiyun 			state->scan_in_progress = 0;
516*4882a593Smuzhiyun 			msp_update_volume(state);
517*4882a593Smuzhiyun 			continue;
518*4882a593Smuzhiyun 		}
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 		/* mute audio */
521*4882a593Smuzhiyun 		state->scan_in_progress = 1;
522*4882a593Smuzhiyun 		msp_update_volume(state);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 		msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
525*4882a593Smuzhiyun 		val1 = val2 = 0;
526*4882a593Smuzhiyun 		max1 = max2 = -1;
527*4882a593Smuzhiyun 		state->watch_stereo = 0;
528*4882a593Smuzhiyun 		state->nicam_on = 0;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		/* wait for tuner to settle down after a channel change */
531*4882a593Smuzhiyun 		if (msp_sleep(state, 200))
532*4882a593Smuzhiyun 			goto restart;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 		/* carrier detect pass #1 -- main carrier */
535*4882a593Smuzhiyun 		cd = msp3400c_carrier_detect_main;
536*4882a593Smuzhiyun 		count = ARRAY_SIZE(msp3400c_carrier_detect_main);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 		if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
539*4882a593Smuzhiyun 			/* autodetect doesn't work well with AM ... */
540*4882a593Smuzhiyun 			max1 = 3;
541*4882a593Smuzhiyun 			count = 0;
542*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n");
543*4882a593Smuzhiyun 		}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 		for (i = 0; i < count; i++) {
546*4882a593Smuzhiyun 			msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
547*4882a593Smuzhiyun 			if (msp_sleep(state, 100))
548*4882a593Smuzhiyun 				goto restart;
549*4882a593Smuzhiyun 			val = msp_read_dsp(client, 0x1b);
550*4882a593Smuzhiyun 			if (val > 32767)
551*4882a593Smuzhiyun 				val -= 65536;
552*4882a593Smuzhiyun 			if (val1 < val)
553*4882a593Smuzhiyun 				val1 = val, max1 = i;
554*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug,
555*4882a593Smuzhiyun 				"carrier1 val: %5d / %s\n", val, cd[i].name);
556*4882a593Smuzhiyun 		}
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 		/* carrier detect pass #2 -- second (stereo) carrier */
559*4882a593Smuzhiyun 		switch (max1) {
560*4882a593Smuzhiyun 		case 1: /* 5.5 */
561*4882a593Smuzhiyun 			cd = msp3400c_carrier_detect_55;
562*4882a593Smuzhiyun 			count = ARRAY_SIZE(msp3400c_carrier_detect_55);
563*4882a593Smuzhiyun 			break;
564*4882a593Smuzhiyun 		case 3: /* 6.5 */
565*4882a593Smuzhiyun 			cd = msp3400c_carrier_detect_65;
566*4882a593Smuzhiyun 			count = ARRAY_SIZE(msp3400c_carrier_detect_65);
567*4882a593Smuzhiyun 			break;
568*4882a593Smuzhiyun 		case 0: /* 4.5 */
569*4882a593Smuzhiyun 		case 2: /* 6.0 */
570*4882a593Smuzhiyun 		default:
571*4882a593Smuzhiyun 			cd = NULL;
572*4882a593Smuzhiyun 			count = 0;
573*4882a593Smuzhiyun 			break;
574*4882a593Smuzhiyun 		}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 		if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
577*4882a593Smuzhiyun 			/* autodetect doesn't work well with AM ... */
578*4882a593Smuzhiyun 			cd = NULL;
579*4882a593Smuzhiyun 			count = 0;
580*4882a593Smuzhiyun 			max2 = 0;
581*4882a593Smuzhiyun 		}
582*4882a593Smuzhiyun 		for (i = 0; i < count; i++) {
583*4882a593Smuzhiyun 			msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
584*4882a593Smuzhiyun 			if (msp_sleep(state, 100))
585*4882a593Smuzhiyun 				goto restart;
586*4882a593Smuzhiyun 			val = msp_read_dsp(client, 0x1b);
587*4882a593Smuzhiyun 			if (val > 32767)
588*4882a593Smuzhiyun 				val -= 65536;
589*4882a593Smuzhiyun 			if (val2 < val)
590*4882a593Smuzhiyun 				val2 = val, max2 = i;
591*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug,
592*4882a593Smuzhiyun 				"carrier2 val: %5d / %s\n", val, cd[i].name);
593*4882a593Smuzhiyun 		}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 		/* program the msp3400 according to the results */
596*4882a593Smuzhiyun 		state->main = msp3400c_carrier_detect_main[max1].cdo;
597*4882a593Smuzhiyun 		switch (max1) {
598*4882a593Smuzhiyun 		case 1: /* 5.5 */
599*4882a593Smuzhiyun 			state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H;
600*4882a593Smuzhiyun 			if (max2 == 0) {
601*4882a593Smuzhiyun 				/* B/G FM-stereo */
602*4882a593Smuzhiyun 				state->second = msp3400c_carrier_detect_55[max2].cdo;
603*4882a593Smuzhiyun 				msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
604*4882a593Smuzhiyun 				state->watch_stereo = 1;
605*4882a593Smuzhiyun 			} else if (max2 == 1 && state->has_nicam) {
606*4882a593Smuzhiyun 				/* B/G NICAM */
607*4882a593Smuzhiyun 				state->second = msp3400c_carrier_detect_55[max2].cdo;
608*4882a593Smuzhiyun 				msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
609*4882a593Smuzhiyun 				state->nicam_on = 1;
610*4882a593Smuzhiyun 				state->watch_stereo = 1;
611*4882a593Smuzhiyun 			} else {
612*4882a593Smuzhiyun 				goto no_second;
613*4882a593Smuzhiyun 			}
614*4882a593Smuzhiyun 			break;
615*4882a593Smuzhiyun 		case 2: /* 6.0 */
616*4882a593Smuzhiyun 			/* PAL I NICAM */
617*4882a593Smuzhiyun 			state->detected_std = V4L2_STD_PAL_I;
618*4882a593Smuzhiyun 			state->second = MSP_CARRIER(6.552);
619*4882a593Smuzhiyun 			msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
620*4882a593Smuzhiyun 			state->nicam_on = 1;
621*4882a593Smuzhiyun 			state->watch_stereo = 1;
622*4882a593Smuzhiyun 			break;
623*4882a593Smuzhiyun 		case 3: /* 6.5 */
624*4882a593Smuzhiyun 			if (max2 == 1 || max2 == 2) {
625*4882a593Smuzhiyun 				/* D/K FM-stereo */
626*4882a593Smuzhiyun 				state->second = msp3400c_carrier_detect_65[max2].cdo;
627*4882a593Smuzhiyun 				msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
628*4882a593Smuzhiyun 				state->watch_stereo = 1;
629*4882a593Smuzhiyun 				state->detected_std = V4L2_STD_DK;
630*4882a593Smuzhiyun 			} else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
631*4882a593Smuzhiyun 				/* L NICAM or AM-mono */
632*4882a593Smuzhiyun 				state->second = msp3400c_carrier_detect_65[max2].cdo;
633*4882a593Smuzhiyun 				msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
634*4882a593Smuzhiyun 				state->watch_stereo = 1;
635*4882a593Smuzhiyun 				state->detected_std = V4L2_STD_L;
636*4882a593Smuzhiyun 			} else if (max2 == 0 && state->has_nicam) {
637*4882a593Smuzhiyun 				/* D/K NICAM */
638*4882a593Smuzhiyun 				state->second = msp3400c_carrier_detect_65[max2].cdo;
639*4882a593Smuzhiyun 				msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
640*4882a593Smuzhiyun 				state->nicam_on = 1;
641*4882a593Smuzhiyun 				state->watch_stereo = 1;
642*4882a593Smuzhiyun 				state->detected_std = V4L2_STD_DK;
643*4882a593Smuzhiyun 			} else {
644*4882a593Smuzhiyun 				goto no_second;
645*4882a593Smuzhiyun 			}
646*4882a593Smuzhiyun 			break;
647*4882a593Smuzhiyun 		case 0: /* 4.5 */
648*4882a593Smuzhiyun 			state->detected_std = V4L2_STD_MN;
649*4882a593Smuzhiyun 			fallthrough;
650*4882a593Smuzhiyun 		default:
651*4882a593Smuzhiyun no_second:
652*4882a593Smuzhiyun 			state->second = msp3400c_carrier_detect_main[max1].cdo;
653*4882a593Smuzhiyun 			msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
654*4882a593Smuzhiyun 			break;
655*4882a593Smuzhiyun 		}
656*4882a593Smuzhiyun 		msp3400c_set_carrier(client, state->second, state->main);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 		/* unmute */
659*4882a593Smuzhiyun 		state->scan_in_progress = 0;
660*4882a593Smuzhiyun 		msp3400c_set_audmode(client);
661*4882a593Smuzhiyun 		msp_update_volume(state);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 		if (msp_debug)
664*4882a593Smuzhiyun 			msp3400c_print_mode(client);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 		/* monitor tv audio mode, the first time don't wait
667*4882a593Smuzhiyun 		   so long to get a quick stereo/bilingual result */
668*4882a593Smuzhiyun 		count = 3;
669*4882a593Smuzhiyun 		while (state->watch_stereo) {
670*4882a593Smuzhiyun 			if (msp_sleep(state, count ? 1000 : 5000))
671*4882a593Smuzhiyun 				goto restart;
672*4882a593Smuzhiyun 			if (count)
673*4882a593Smuzhiyun 				count--;
674*4882a593Smuzhiyun 			watch_stereo(client);
675*4882a593Smuzhiyun 		}
676*4882a593Smuzhiyun 	}
677*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
678*4882a593Smuzhiyun 	return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 
msp3410d_thread(void * data)682*4882a593Smuzhiyun int msp3410d_thread(void *data)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	struct i2c_client *client = data;
685*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
686*4882a593Smuzhiyun 	int val, i, std, count;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
689*4882a593Smuzhiyun 	state->detected_std = V4L2_STD_ALL;
690*4882a593Smuzhiyun 	set_freezable();
691*4882a593Smuzhiyun 	for (;;) {
692*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
693*4882a593Smuzhiyun 		msp_sleep(state, -1);
694*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun restart:
697*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
698*4882a593Smuzhiyun 		state->restart = 0;
699*4882a593Smuzhiyun 		if (kthread_should_stop())
700*4882a593Smuzhiyun 			break;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 		if (state->mode == MSP_MODE_EXTERN) {
703*4882a593Smuzhiyun 			/* no carrier scan needed, just unmute */
704*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug,
705*4882a593Smuzhiyun 				"thread: no carrier scan\n");
706*4882a593Smuzhiyun 			state->scan_in_progress = 0;
707*4882a593Smuzhiyun 			msp_update_volume(state);
708*4882a593Smuzhiyun 			continue;
709*4882a593Smuzhiyun 		}
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		/* mute audio */
712*4882a593Smuzhiyun 		state->scan_in_progress = 1;
713*4882a593Smuzhiyun 		msp_update_volume(state);
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 		/* start autodetect. Note: autodetect is not supported for
716*4882a593Smuzhiyun 		   NTSC-M and radio, hence we force the standard in those
717*4882a593Smuzhiyun 		   cases. */
718*4882a593Smuzhiyun 		if (state->radio)
719*4882a593Smuzhiyun 			std = 0x40;
720*4882a593Smuzhiyun 		else
721*4882a593Smuzhiyun 			std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
722*4882a593Smuzhiyun 		state->watch_stereo = 0;
723*4882a593Smuzhiyun 		state->nicam_on = 0;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 		/* wait for tuner to settle down after a channel change */
726*4882a593Smuzhiyun 		if (msp_sleep(state, 200))
727*4882a593Smuzhiyun 			goto restart;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 		if (msp_debug)
730*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 2, msp_debug,
731*4882a593Smuzhiyun 				"setting standard: %s (0x%04x)\n",
732*4882a593Smuzhiyun 				msp_standard_std_name(std), std);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 		if (std != 1) {
735*4882a593Smuzhiyun 			/* programmed some specific mode */
736*4882a593Smuzhiyun 			val = std;
737*4882a593Smuzhiyun 		} else {
738*4882a593Smuzhiyun 			/* triggered autodetect */
739*4882a593Smuzhiyun 			msp_write_dem(client, 0x20, std);
740*4882a593Smuzhiyun 			for (;;) {
741*4882a593Smuzhiyun 				if (msp_sleep(state, 100))
742*4882a593Smuzhiyun 					goto restart;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 				/* check results */
745*4882a593Smuzhiyun 				val = msp_read_dem(client, 0x7e);
746*4882a593Smuzhiyun 				if (val < 0x07ff)
747*4882a593Smuzhiyun 					break;
748*4882a593Smuzhiyun 				dev_dbg_lvl(&client->dev, 2, msp_debug,
749*4882a593Smuzhiyun 					"detection still in progress\n");
750*4882a593Smuzhiyun 			}
751*4882a593Smuzhiyun 		}
752*4882a593Smuzhiyun 		for (i = 0; msp_stdlist[i].name != NULL; i++)
753*4882a593Smuzhiyun 			if (msp_stdlist[i].retval == val)
754*4882a593Smuzhiyun 				break;
755*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
756*4882a593Smuzhiyun 			msp_standard_std_name(val), val);
757*4882a593Smuzhiyun 		state->main   = msp_stdlist[i].main;
758*4882a593Smuzhiyun 		state->second = msp_stdlist[i].second;
759*4882a593Smuzhiyun 		state->std = val;
760*4882a593Smuzhiyun 		state->rxsubchans = V4L2_TUNER_SUB_MONO;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 		if (msp_amsound && !state->radio &&
763*4882a593Smuzhiyun 		    (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
764*4882a593Smuzhiyun 			/* autodetection has failed, let backup */
765*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
766*4882a593Smuzhiyun 				msp_stdlist[8].name ?
767*4882a593Smuzhiyun 					msp_stdlist[8].name : "unknown", val);
768*4882a593Smuzhiyun 			state->std = val = 0x0009;
769*4882a593Smuzhiyun 			msp_write_dem(client, 0x20, val);
770*4882a593Smuzhiyun 		} else {
771*4882a593Smuzhiyun 			state->detected_std = msp_standard_std(state->std);
772*4882a593Smuzhiyun 		}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		/* set stereo */
775*4882a593Smuzhiyun 		switch (val) {
776*4882a593Smuzhiyun 		case 0x0008: /* B/G NICAM */
777*4882a593Smuzhiyun 		case 0x000a: /* I NICAM */
778*4882a593Smuzhiyun 		case 0x000b: /* D/K NICAM */
779*4882a593Smuzhiyun 			if (val == 0x000a)
780*4882a593Smuzhiyun 				state->mode = MSP_MODE_FM_NICAM2;
781*4882a593Smuzhiyun 			else
782*4882a593Smuzhiyun 				state->mode = MSP_MODE_FM_NICAM1;
783*4882a593Smuzhiyun 			/* just turn on stereo */
784*4882a593Smuzhiyun 			state->nicam_on = 1;
785*4882a593Smuzhiyun 			state->watch_stereo = 1;
786*4882a593Smuzhiyun 			break;
787*4882a593Smuzhiyun 		case 0x0009:
788*4882a593Smuzhiyun 			state->mode = MSP_MODE_AM_NICAM;
789*4882a593Smuzhiyun 			state->nicam_on = 1;
790*4882a593Smuzhiyun 			state->watch_stereo = 1;
791*4882a593Smuzhiyun 			break;
792*4882a593Smuzhiyun 		case 0x0020: /* BTSC */
793*4882a593Smuzhiyun 			/* The pre-'G' models only have BTSC-mono */
794*4882a593Smuzhiyun 			state->mode = MSP_MODE_BTSC;
795*4882a593Smuzhiyun 			break;
796*4882a593Smuzhiyun 		case 0x0040: /* FM radio */
797*4882a593Smuzhiyun 			state->mode = MSP_MODE_FM_RADIO;
798*4882a593Smuzhiyun 			state->rxsubchans = V4L2_TUNER_SUB_STEREO;
799*4882a593Smuzhiyun 			/* not needed in theory if we have radio, but
800*4882a593Smuzhiyun 			   short programming enables carrier mute */
801*4882a593Smuzhiyun 			msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
802*4882a593Smuzhiyun 			msp3400c_set_carrier(client, MSP_CARRIER(10.7),
803*4882a593Smuzhiyun 					    MSP_CARRIER(10.7));
804*4882a593Smuzhiyun 			break;
805*4882a593Smuzhiyun 		case 0x0002:
806*4882a593Smuzhiyun 		case 0x0003:
807*4882a593Smuzhiyun 		case 0x0004:
808*4882a593Smuzhiyun 		case 0x0005:
809*4882a593Smuzhiyun 			state->mode = MSP_MODE_FM_TERRA;
810*4882a593Smuzhiyun 			state->watch_stereo = 1;
811*4882a593Smuzhiyun 			break;
812*4882a593Smuzhiyun 		}
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 		/* set various prescales */
815*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0d, 0x1900); /* scart */
816*4882a593Smuzhiyun 		msp_write_dsp(client, 0x0e, 0x3000); /* FM */
817*4882a593Smuzhiyun 		if (state->has_nicam)
818*4882a593Smuzhiyun 			msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 		if (state->has_i2s_conf)
821*4882a593Smuzhiyun 			msp_write_dem(client, 0x40, state->i2s_mode);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 		/* unmute */
824*4882a593Smuzhiyun 		msp3400c_set_audmode(client);
825*4882a593Smuzhiyun 		state->scan_in_progress = 0;
826*4882a593Smuzhiyun 		msp_update_volume(state);
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 		/* monitor tv audio mode, the first time don't wait
829*4882a593Smuzhiyun 		   so long to get a quick stereo/bilingual result */
830*4882a593Smuzhiyun 		count = 3;
831*4882a593Smuzhiyun 		while (state->watch_stereo) {
832*4882a593Smuzhiyun 			if (msp_sleep(state, count ? 1000 : 5000))
833*4882a593Smuzhiyun 				goto restart;
834*4882a593Smuzhiyun 			if (count)
835*4882a593Smuzhiyun 				count--;
836*4882a593Smuzhiyun 			watch_stereo(client);
837*4882a593Smuzhiyun 		}
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
840*4882a593Smuzhiyun 	return 0;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun /* ----------------------------------------------------------------------- */
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun /* msp34xxG + (autoselect no-thread)
846*4882a593Smuzhiyun  * this one uses both automatic standard detection and automatic sound
847*4882a593Smuzhiyun  * select which are available in the newer G versions
848*4882a593Smuzhiyun  * struct msp: only norm, acb and source are really used in this mode
849*4882a593Smuzhiyun  */
850*4882a593Smuzhiyun 
msp34xxg_modus(struct i2c_client * client)851*4882a593Smuzhiyun static int msp34xxg_modus(struct i2c_client *client)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	if (state->radio) {
856*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
857*4882a593Smuzhiyun 		return 0x0001;
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 	if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
860*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
861*4882a593Smuzhiyun 		return 0x4001;
862*4882a593Smuzhiyun 	}
863*4882a593Smuzhiyun 	if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
864*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
865*4882a593Smuzhiyun 		return 0x0001;
866*4882a593Smuzhiyun 	}
867*4882a593Smuzhiyun 	if (state->v4l2_std == V4L2_STD_SECAM_L) {
868*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
869*4882a593Smuzhiyun 		return 0x6001;
870*4882a593Smuzhiyun 	}
871*4882a593Smuzhiyun 	if (state->v4l2_std & V4L2_STD_MN) {
872*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
873*4882a593Smuzhiyun 		return 0x2001;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 	return 0x7001;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun 
msp34xxg_set_source(struct i2c_client * client,u16 reg,int in)878*4882a593Smuzhiyun static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
881*4882a593Smuzhiyun 	int source, matrix;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	switch (state->audmode) {
884*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_MONO:
885*4882a593Smuzhiyun 		source = 0; /* mono only */
886*4882a593Smuzhiyun 		matrix = 0x30;
887*4882a593Smuzhiyun 		break;
888*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_LANG2:
889*4882a593Smuzhiyun 		source = 4; /* stereo or B */
890*4882a593Smuzhiyun 		matrix = 0x10;
891*4882a593Smuzhiyun 		break;
892*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_LANG1_LANG2:
893*4882a593Smuzhiyun 		source = 1; /* stereo or A|B */
894*4882a593Smuzhiyun 		matrix = 0x20;
895*4882a593Smuzhiyun 		break;
896*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_LANG1:
897*4882a593Smuzhiyun 		source = 3; /* stereo or A */
898*4882a593Smuzhiyun 		matrix = 0x00;
899*4882a593Smuzhiyun 		break;
900*4882a593Smuzhiyun 	case V4L2_TUNER_MODE_STEREO:
901*4882a593Smuzhiyun 	default:
902*4882a593Smuzhiyun 		source = 3; /* stereo or A */
903*4882a593Smuzhiyun 		matrix = 0x20;
904*4882a593Smuzhiyun 		break;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	if (in == MSP_DSP_IN_TUNER)
908*4882a593Smuzhiyun 		source = (source << 8) | 0x20;
909*4882a593Smuzhiyun 	/* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
910*4882a593Smuzhiyun 	   instead of 11, 12, 13. So we add one for that msp version. */
911*4882a593Smuzhiyun 	else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
912*4882a593Smuzhiyun 		source = ((in + 1) << 8) | matrix;
913*4882a593Smuzhiyun 	else
914*4882a593Smuzhiyun 		source = (in << 8) | matrix;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug,
917*4882a593Smuzhiyun 		"set source to %d (0x%x) for output %02x\n", in, source, reg);
918*4882a593Smuzhiyun 	msp_write_dsp(client, reg, source);
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun 
msp34xxg_set_sources(struct i2c_client * client)921*4882a593Smuzhiyun static void msp34xxg_set_sources(struct i2c_client *client)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
924*4882a593Smuzhiyun 	u32 in = state->route_in;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
927*4882a593Smuzhiyun 	/* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
928*4882a593Smuzhiyun 	msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
929*4882a593Smuzhiyun 	msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
930*4882a593Smuzhiyun 	msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
931*4882a593Smuzhiyun 	if (state->has_scart2_out)
932*4882a593Smuzhiyun 		msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
933*4882a593Smuzhiyun 	msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun /* (re-)initialize the msp34xxg */
msp34xxg_reset(struct i2c_client * client)937*4882a593Smuzhiyun static void msp34xxg_reset(struct i2c_client *client)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
940*4882a593Smuzhiyun 	int tuner = (state->route_in >> 3) & 1;
941*4882a593Smuzhiyun 	int modus;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	/* initialize std to 1 (autodetect) to signal that no standard is
944*4882a593Smuzhiyun 	   selected yet. */
945*4882a593Smuzhiyun 	state->std = 1;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	msp_reset(client);
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	if (state->has_i2s_conf)
950*4882a593Smuzhiyun 		msp_write_dem(client, 0x40, state->i2s_mode);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	/* step-by-step initialisation, as described in the manual */
953*4882a593Smuzhiyun 	modus = msp34xxg_modus(client);
954*4882a593Smuzhiyun 	modus |= tuner ? 0x100 : 0;
955*4882a593Smuzhiyun 	msp_write_dem(client, 0x30, modus);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	/* write the dsps that may have an influence on
958*4882a593Smuzhiyun 	   standard/audio autodetection right now */
959*4882a593Smuzhiyun 	msp34xxg_set_sources(client);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	msp_write_dsp(client, 0x0d, 0x1900); /* scart */
962*4882a593Smuzhiyun 	msp_write_dsp(client, 0x0e, 0x3000); /* FM */
963*4882a593Smuzhiyun 	if (state->has_nicam)
964*4882a593Smuzhiyun 		msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	/* set identification threshold. Personally, I
967*4882a593Smuzhiyun 	 * I set it to a higher value than the default
968*4882a593Smuzhiyun 	 * of 0x190 to ignore noisy stereo signals.
969*4882a593Smuzhiyun 	 * this needs tuning. (recommended range 0x00a0-0x03c0)
970*4882a593Smuzhiyun 	 * 0x7f0 = forced mono mode
971*4882a593Smuzhiyun 	 *
972*4882a593Smuzhiyun 	 * a2 threshold for stereo/bilingual.
973*4882a593Smuzhiyun 	 * Note: this register is part of the Manual/Compatibility mode.
974*4882a593Smuzhiyun 	 * It is supported by all 'G'-family chips.
975*4882a593Smuzhiyun 	 */
976*4882a593Smuzhiyun 	msp_write_dem(client, 0x22, msp_stereo_thresh);
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun 
msp34xxg_thread(void * data)979*4882a593Smuzhiyun int msp34xxg_thread(void *data)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun 	struct i2c_client *client = data;
982*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
983*4882a593Smuzhiyun 	int val, i;
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
986*4882a593Smuzhiyun 	state->detected_std = V4L2_STD_ALL;
987*4882a593Smuzhiyun 	set_freezable();
988*4882a593Smuzhiyun 	for (;;) {
989*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
990*4882a593Smuzhiyun 		msp_sleep(state, -1);
991*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun restart:
994*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
995*4882a593Smuzhiyun 		state->restart = 0;
996*4882a593Smuzhiyun 		if (kthread_should_stop())
997*4882a593Smuzhiyun 			break;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 		if (state->mode == MSP_MODE_EXTERN) {
1000*4882a593Smuzhiyun 			/* no carrier scan needed, just unmute */
1001*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug,
1002*4882a593Smuzhiyun 				"thread: no carrier scan\n");
1003*4882a593Smuzhiyun 			state->scan_in_progress = 0;
1004*4882a593Smuzhiyun 			msp_update_volume(state);
1005*4882a593Smuzhiyun 			continue;
1006*4882a593Smuzhiyun 		}
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 		/* setup the chip*/
1009*4882a593Smuzhiyun 		msp34xxg_reset(client);
1010*4882a593Smuzhiyun 		state->std = state->radio ? 0x40 :
1011*4882a593Smuzhiyun 			(state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
1012*4882a593Smuzhiyun 		msp_write_dem(client, 0x20, state->std);
1013*4882a593Smuzhiyun 		/* start autodetect */
1014*4882a593Smuzhiyun 		if (state->std != 1)
1015*4882a593Smuzhiyun 			goto unmute;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 		/* watch autodetect */
1018*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
1019*4882a593Smuzhiyun 			"started autodetect, waiting for result\n");
1020*4882a593Smuzhiyun 		for (i = 0; i < 10; i++) {
1021*4882a593Smuzhiyun 			if (msp_sleep(state, 100))
1022*4882a593Smuzhiyun 				goto restart;
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 			/* check results */
1025*4882a593Smuzhiyun 			val = msp_read_dem(client, 0x7e);
1026*4882a593Smuzhiyun 			if (val < 0x07ff) {
1027*4882a593Smuzhiyun 				state->std = val;
1028*4882a593Smuzhiyun 				break;
1029*4882a593Smuzhiyun 			}
1030*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 2, msp_debug,
1031*4882a593Smuzhiyun 				"detection still in progress\n");
1032*4882a593Smuzhiyun 		}
1033*4882a593Smuzhiyun 		if (state->std == 1) {
1034*4882a593Smuzhiyun 			dev_dbg_lvl(&client->dev, 1, msp_debug,
1035*4882a593Smuzhiyun 				"detection still in progress after 10 tries. giving up.\n");
1036*4882a593Smuzhiyun 			continue;
1037*4882a593Smuzhiyun 		}
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun unmute:
1040*4882a593Smuzhiyun 		dev_dbg_lvl(&client->dev, 1, msp_debug,
1041*4882a593Smuzhiyun 			"detected standard: %s (0x%04x)\n",
1042*4882a593Smuzhiyun 			msp_standard_std_name(state->std), state->std);
1043*4882a593Smuzhiyun 		state->detected_std = msp_standard_std(state->std);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 		if (state->std == 9) {
1046*4882a593Smuzhiyun 			/* AM NICAM mode */
1047*4882a593Smuzhiyun 			msp_write_dsp(client, 0x0e, 0x7c00);
1048*4882a593Smuzhiyun 		}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 		/* unmute: dispatch sound to scart output, set scart volume */
1051*4882a593Smuzhiyun 		msp_update_volume(state);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 		/* restore ACB */
1054*4882a593Smuzhiyun 		if (msp_write_dsp(client, 0x13, state->acb))
1055*4882a593Smuzhiyun 			return -1;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 		/* the periodic stereo/SAP check is only relevant for
1058*4882a593Smuzhiyun 		   the 0x20 standard (BTSC) */
1059*4882a593Smuzhiyun 		if (state->std != 0x20)
1060*4882a593Smuzhiyun 			continue;
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 		state->watch_stereo = 1;
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 		/* monitor tv audio mode, the first time don't wait
1065*4882a593Smuzhiyun 		   in order to get a quick stereo/SAP update */
1066*4882a593Smuzhiyun 		watch_stereo(client);
1067*4882a593Smuzhiyun 		while (state->watch_stereo) {
1068*4882a593Smuzhiyun 			watch_stereo(client);
1069*4882a593Smuzhiyun 			if (msp_sleep(state, 5000))
1070*4882a593Smuzhiyun 				goto restart;
1071*4882a593Smuzhiyun 		}
1072*4882a593Smuzhiyun 	}
1073*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
1074*4882a593Smuzhiyun 	return 0;
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun 
msp34xxg_detect_stereo(struct i2c_client * client)1077*4882a593Smuzhiyun static int msp34xxg_detect_stereo(struct i2c_client *client)
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
1080*4882a593Smuzhiyun 	int status = msp_read_dem(client, 0x0200);
1081*4882a593Smuzhiyun 	int is_bilingual = status & 0x100;
1082*4882a593Smuzhiyun 	int is_stereo = status & 0x40;
1083*4882a593Smuzhiyun 	int oldrx = state->rxsubchans;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	if (state->mode == MSP_MODE_EXTERN)
1086*4882a593Smuzhiyun 		return 0;
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	state->rxsubchans = 0;
1089*4882a593Smuzhiyun 	if (is_stereo)
1090*4882a593Smuzhiyun 		state->rxsubchans = V4L2_TUNER_SUB_STEREO;
1091*4882a593Smuzhiyun 	else
1092*4882a593Smuzhiyun 		state->rxsubchans = V4L2_TUNER_SUB_MONO;
1093*4882a593Smuzhiyun 	if (is_bilingual) {
1094*4882a593Smuzhiyun 		if (state->std == 0x20)
1095*4882a593Smuzhiyun 			state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1096*4882a593Smuzhiyun 		else
1097*4882a593Smuzhiyun 			state->rxsubchans =
1098*4882a593Smuzhiyun 				V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1099*4882a593Smuzhiyun 	}
1100*4882a593Smuzhiyun 	dev_dbg_lvl(&client->dev, 1, msp_debug,
1101*4882a593Smuzhiyun 		"status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1102*4882a593Smuzhiyun 		status, is_stereo, is_bilingual, state->rxsubchans);
1103*4882a593Smuzhiyun 	return (oldrx != state->rxsubchans);
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
msp34xxg_set_audmode(struct i2c_client * client)1106*4882a593Smuzhiyun static void msp34xxg_set_audmode(struct i2c_client *client)
1107*4882a593Smuzhiyun {
1108*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	if (state->std == 0x20) {
1111*4882a593Smuzhiyun 	       if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
1112*4882a593Smuzhiyun 		   (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
1113*4882a593Smuzhiyun 		    state->audmode == V4L2_TUNER_MODE_LANG2)) {
1114*4882a593Smuzhiyun 			msp_write_dem(client, 0x20, 0x21);
1115*4882a593Smuzhiyun 	       } else {
1116*4882a593Smuzhiyun 			msp_write_dem(client, 0x20, 0x20);
1117*4882a593Smuzhiyun 	       }
1118*4882a593Smuzhiyun 	}
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	msp34xxg_set_sources(client);
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun 
msp_set_audmode(struct i2c_client * client)1123*4882a593Smuzhiyun void msp_set_audmode(struct i2c_client *client)
1124*4882a593Smuzhiyun {
1125*4882a593Smuzhiyun 	struct msp_state *state = to_state(i2c_get_clientdata(client));
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	switch (state->opmode) {
1128*4882a593Smuzhiyun 	case OPMODE_MANUAL:
1129*4882a593Smuzhiyun 	case OPMODE_AUTODETECT:
1130*4882a593Smuzhiyun 		msp3400c_set_audmode(client);
1131*4882a593Smuzhiyun 		break;
1132*4882a593Smuzhiyun 	case OPMODE_AUTOSELECT:
1133*4882a593Smuzhiyun 		msp34xxg_set_audmode(client);
1134*4882a593Smuzhiyun 		break;
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
msp_detect_stereo(struct i2c_client * client)1138*4882a593Smuzhiyun int msp_detect_stereo(struct i2c_client *client)
1139*4882a593Smuzhiyun {
1140*4882a593Smuzhiyun 	struct msp_state *state  = to_state(i2c_get_clientdata(client));
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	switch (state->opmode) {
1143*4882a593Smuzhiyun 	case OPMODE_MANUAL:
1144*4882a593Smuzhiyun 	case OPMODE_AUTODETECT:
1145*4882a593Smuzhiyun 		return msp3400c_detect_stereo(client);
1146*4882a593Smuzhiyun 	case OPMODE_AUTOSELECT:
1147*4882a593Smuzhiyun 		return msp34xxg_detect_stereo(client);
1148*4882a593Smuzhiyun 	}
1149*4882a593Smuzhiyun 	return 0;
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun 
1152