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