blob: 11fc593ed908a25db615c5ecec979eb31f3e0ac5 [file] [log] [blame]
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001/*
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.
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020015 */
16
17
18#include <linux/kernel.h>
19#include <linux/module.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020020#include <linux/i2c.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070021#include <linux/freezer.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020022#include <linux/videodev2.h>
23#include <media/v4l2-common.h>
Mauro Carvalho Chehabd647f0b2015-11-13 19:40:07 -020024#include <media/drv-intf/msp3400.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020025#include <linux/kthread.h>
26#include <linux/suspend.h>
Hans Verkuil49965a82006-03-19 08:45:38 -030027#include "msp3400-driver.h"
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020028
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;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -030035 v4l2_std_id std;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -020036} msp_stdlist[] = {
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -030037 { 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 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020078};
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
Hans Verkuild52c7382007-12-12 08:25:18 -030095 }, { /* AM (for carrier detect / msp3410) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020096 {-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
Hans Verkuild52c7382007-12-12 08:25:18 -0300100 }, { /* FM Radio */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200101 {-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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 }, { /* Terrestrial FM-mono + FM-stereo */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200106 {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
Hans Verkuild52c7382007-12-12 08:25:18 -0300110 }, { /* Sat FM-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200111 { 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
Hans Verkuild52c7382007-12-12 08:25:18 -0300115 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200116 {-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
Hans Verkuild52c7382007-12-12 08:25:18 -0300120 }, { /* NICAM/FM -- I (6.0/6.552) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200121 {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
Hans Verkuild52c7382007-12-12 08:25:18 -0300125 }, { /* NICAM/AM -- L (6.5/5.85) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200126 {-2, -8, -10, 10, 50, 86},
127 {-4, -12, -9, 23, 79, 126},
128 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200129 0x00c6, 0x0140, 0x0120, 0x7c00
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200130 },
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
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200164const char *msp_standard_std_name(int std)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200165{
166 int i;
167
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200168 for (i = 0; msp_stdlist[i].name != NULL; i++)
169 if (msp_stdlist[i].retval == std)
170 return msp_stdlist[i].name;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200171 return "unknown";
172}
173
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300174static 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
Adrian Bunk044f3242006-03-08 10:50:07 -0300184static void msp_set_source(struct i2c_client *client, u16 src)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200185{
Hans Verkuil76efd622008-11-24 18:16:46 -0300186 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200187
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);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300198 if (state->has_scart2_out)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200199 msp_write_dsp(client, 0x0041, src);
200}
201
202void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200203{
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);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200208 msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200209}
210
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200211void msp3400c_set_mode(struct i2c_client *client, int mode)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200212{
Hans Verkuil76efd622008-11-24 18:16:46 -0300213 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200214 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
Hans Verkuil5325b422009-04-02 11:26:22 -0300215 int tuner = (state->route_in >> 3) & 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200216 int i;
217
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200218 dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200219 state->mode = mode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200220 state->rxsubchans = V4L2_TUNER_SUB_MONO;
221
Hans Verkuil2474ed42006-03-19 12:35:57 -0300222 msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200223
224 for (i = 5; i >= 0; i--) /* fir 1 */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200225 msp_write_dem(client, 0x0001, data->fir1[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200226
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--)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200231 msp_write_dem(client, 0x0005, data->fir2[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200232
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200233 msp_write_dem(client, 0x0083, data->mode_reg);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200234
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200235 msp3400c_set_carrier(client, data->cdo1, data->cdo2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200236
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200237 msp_set_source(client, data->dsp_src);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300238 /* set prescales */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200239
Hans Verkuilde533cc2006-03-19 08:21:56 -0300240 /* 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);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200245}
246
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200247/* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
248 nor do they support stereo BTSC. */
Adrian Bunk044f3242006-03-08 10:50:07 -0300249static void msp3400c_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200250{
Hans Verkuild52c7382007-12-12 08:25:18 -0300251 static char *strmode[] = {
252 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
253 };
Hans Verkuil76efd622008-11-24 18:16:46 -0300254 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil301e22d2006-03-18 17:15:00 -0300255 char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200256 strmode[state->audmode] : "unknown";
257 int src = 0; /* channel source: FM/AM, nicam or SCART */
Hans Verkuil70615612006-03-29 14:31:44 -0300258 int audmode = state->audmode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200259
260 if (state->opmode == OPMODE_AUTOSELECT) {
261 /* this method would break everything, let's make sure
262 * it's never called
263 */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200264 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200265 "set_audmode called with mode=%d instead of set_source (ignored)\n",
266 state->audmode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200267 return;
268 }
269
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300270 /* 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
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300274 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;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300288 }
Hans Verkuil70615612006-03-29 14:31:44 -0300289
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200290 /* switch demodulator */
291 switch (state->mode) {
292 case MSP_MODE_FM_TERRA:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200293 dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr);
Hans Verkuil70615612006-03-29 14:31:44 -0300294 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200295 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:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300301 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200302 msp_write_dsp(client, 0x000e, 0x3000);
303 break;
304 }
305 break;
306 case MSP_MODE_FM_SAT:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200307 dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr);
Hans Verkuil70615612006-03-29 14:31:44 -0300308 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200309 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200310 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200311 break;
312 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300313 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200314 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200315 break;
316 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200317 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200318 break;
319 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200320 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200321 break;
322 }
323 break;
324 case MSP_MODE_FM_NICAM1:
325 case MSP_MODE_FM_NICAM2:
326 case MSP_MODE_AM_NICAM:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200327 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300328 "NICAM set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200329 if (state->nicam_on)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200330 src = 0x0100; /* NICAM */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200331 break;
332 case MSP_MODE_BTSC:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200333 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300334 "BTSC set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200335 break;
336 case MSP_MODE_EXTERN:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200337 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300338 "extern set_audmode: %s\n", modestr);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200339 src = 0x0200; /* SCART */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200340 break;
341 case MSP_MODE_FM_RADIO:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200342 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300343 "FM-Radio set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200344 break;
345 default:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200346 dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200347 return;
348 }
349
350 /* switch audio */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200351 dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode);
Hans Verkuil70615612006-03-29 14:31:44 -0300352 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200353 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300354 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200355 src |= 0x0020;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200356 break;
357 case V4L2_TUNER_MODE_MONO:
358 if (state->mode == MSP_MODE_AM_NICAM) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200359 dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200360 /* 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 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200366 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
367 src = 0x0030;
368 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200369 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200370 break;
371 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200372 src |= 0x0010;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200373 break;
374 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200375 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300376 "set_audmode final source/matrix = 0x%x\n", src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200377
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200378 msp_set_source(client, src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200379}
380
381static void msp3400c_print_mode(struct i2c_client *client)
382{
Hans Verkuil76efd622008-11-24 18:16:46 -0300383 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200384
Hans Verkuild52c7382007-12-12 08:25:18 -0300385 if (state->main == state->second)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200386 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300387 "mono sound carrier: %d.%03d MHz\n",
388 state->main / 910000, (state->main / 910) % 1000);
389 else
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200390 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300391 "main sound carrier: %d.%03d MHz\n",
392 state->main / 910000, (state->main / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200393 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200394 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300395 "NICAM/FM carrier : %d.%03d MHz\n",
396 state->second / 910000, (state->second/910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200397 if (state->mode == MSP_MODE_AM_NICAM)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200398 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300399 "NICAM/AM carrier : %d.%03d MHz\n",
400 state->second / 910000, (state->second / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200401 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200402 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300403 "FM-stereo carrier : %d.%03d MHz\n",
404 state->second / 910000, (state->second / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200405 }
406}
407
408/* ----------------------------------------------------------------------- */
409
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200410static int msp3400c_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200411{
Hans Verkuil76efd622008-11-24 18:16:46 -0300412 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200413 int val;
414 int rxsubchans = state->rxsubchans;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200415 int newnicam = state->nicam_on;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200416 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;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200423 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300424 "stereo detect register: %d\n", val);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300425 if (val > 8192) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200426 rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200427 } 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);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200438 dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200439 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:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200450 rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200451 break;
452 case 2:
453 case 10:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200454 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200455 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;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200466 }
467 if (rxsubchans != state->rxsubchans) {
468 update = 1;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200469 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300470 "watch: rxsubchans %02x => %02x\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200471 state->rxsubchans, rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200472 state->rxsubchans = rxsubchans;
473 }
474 if (newnicam != state->nicam_on) {
475 update = 1;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200476 dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200477 state->nicam_on, newnicam);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200478 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{
Hans Verkuil76efd622008-11-24 18:16:46 -0300490 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200491
Hans Verkuild52c7382007-12-12 08:25:18 -0300492 if (msp_detect_stereo(client))
Hans Verkuilde533cc2006-03-19 08:21:56 -0300493 msp_set_audmode(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200494
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200495 if (msp_once)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200496 state->watch_stereo = 0;
497}
498
499int msp3400c_thread(void *data)
500{
501 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300502 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200503 struct msp3400c_carrier_detect *cd;
Hans Verkuild52c7382007-12-12 08:25:18 -0300504 int count, max1, max2, val1, val2, val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200505
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200506 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n");
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300507 state->detected_std = V4L2_STD_ALL;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700508 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200509 for (;;) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200510 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200511 msp_sleep(state, -1);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200512 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200513
Hans Verkuild52c7382007-12-12 08:25:18 -0300514restart:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200515 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200516 state->restart = 0;
517 if (kthread_should_stop())
518 break;
519
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200520 if (state->radio || MSP_MODE_EXTERN == state->mode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200521 /* no carrier scan, just unmute */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200522 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300523 "thread: no carrier scan\n");
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300524 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300525 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200526 continue;
527 }
528
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300529 /* mute audio */
530 state->scan_in_progress = 1;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300531 msp_update_volume(state);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300532
Hans Verkuilde533cc2006-03-19 08:21:56 -0300533 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200534 val1 = val2 = 0;
535 max1 = max2 = -1;
536 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200537 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200538
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300539 /* wait for tuner to settle down after a channel change */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200540 if (msp_sleep(state, 200))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200541 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
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200547 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200548 /* autodetect doesn't work well with AM ... */
549 max1 = 3;
550 count = 0;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200551 dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200552 }
553
Hans Verkuild52c7382007-12-12 08:25:18 -0300554 for (i = 0; i < count; i++) {
555 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
556 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200557 goto restart;
558 val = msp_read_dsp(client, 0x1b);
559 if (val > 32767)
560 val -= 65536;
561 if (val1 < val)
Hans Verkuild52c7382007-12-12 08:25:18 -0300562 val1 = val, max1 = i;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200563 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300564 "carrier1 val: %5d / %s\n", val, cd[i].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200565 }
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
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200585 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200586 /* autodetect doesn't work well with AM ... */
587 cd = NULL;
588 count = 0;
589 max2 = 0;
590 }
Hans Verkuild52c7382007-12-12 08:25:18 -0300591 for (i = 0; i < count; i++) {
592 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
593 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200594 goto restart;
595 val = msp_read_dsp(client, 0x1b);
596 if (val > 32767)
597 val -= 65536;
598 if (val2 < val)
Hans Verkuild52c7382007-12-12 08:25:18 -0300599 val2 = val, max2 = i;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200600 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300601 "carrier2 val: %5d / %s\n", val, cd[i].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200602 }
603
604 /* program the msp3400 according to the results */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200605 state->main = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200606 switch (max1) {
607 case 1: /* 5.5 */
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300608 state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200609 if (max2 == 0) {
610 /* B/G FM-stereo */
611 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200612 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200613 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200614 } else if (max2 == 1 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200615 /* B/G NICAM */
616 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200617 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200618 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200619 state->watch_stereo = 1;
620 } else {
621 goto no_second;
622 }
623 break;
624 case 2: /* 6.0 */
625 /* PAL I NICAM */
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300626 state->detected_std = V4L2_STD_PAL_I;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200627 state->second = MSP_CARRIER(6.552);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200628 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200629 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200630 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;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200636 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200637 state->watch_stereo = 1;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300638 state->detected_std = V4L2_STD_DK;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200639 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200640 /* L NICAM or AM-mono */
641 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200642 msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200643 state->watch_stereo = 1;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300644 state->detected_std = V4L2_STD_L;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200645 } else if (max2 == 0 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200646 /* D/K NICAM */
647 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200648 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200649 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200650 state->watch_stereo = 1;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300651 state->detected_std = V4L2_STD_DK;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200652 } else {
653 goto no_second;
654 }
655 break;
656 case 0: /* 4.5 */
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300657 state->detected_std = V4L2_STD_MN;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200658 default:
Hans Verkuild52c7382007-12-12 08:25:18 -0300659no_second:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200660 state->second = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200661 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200662 break;
663 }
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300664 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200665
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300666 /* unmute */
667 state->scan_in_progress = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200668 msp3400c_set_audmode(client);
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300669 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200670
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200671 if (msp_debug)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200672 msp3400c_print_mode(client);
673
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200674 /* monitor tv audio mode, the first time don't wait
675 so long to get a quick stereo/bilingual result */
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300676 count = 3;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200677 while (state->watch_stereo) {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300678 if (msp_sleep(state, count ? 1000 : 5000))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200679 goto restart;
Hans Verkuild52c7382007-12-12 08:25:18 -0300680 if (count)
681 count--;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300682 watch_stereo(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200683 }
684 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200685 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200686 return 0;
687}
688
689
690int msp3410d_thread(void *data)
691{
692 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300693 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300694 int val, i, std, count;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200695
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200696 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300697 state->detected_std = V4L2_STD_ALL;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700698 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200699 for (;;) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200700 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
Hans Verkuild52c7382007-12-12 08:25:18 -0300701 msp_sleep(state, -1);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200702 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200703
Hans Verkuild52c7382007-12-12 08:25:18 -0300704restart:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200705 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200706 state->restart = 0;
707 if (kthread_should_stop())
708 break;
709
710 if (state->mode == MSP_MODE_EXTERN) {
711 /* no carrier scan needed, just unmute */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200712 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300713 "thread: no carrier scan\n");
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300714 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300715 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200716 continue;
717 }
718
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300719 /* mute audio */
720 state->scan_in_progress = 1;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300721 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200722
Hans Verkuilde533cc2006-03-19 08:21:56 -0300723 /* start autodetect. Note: autodetect is not supported for
Hans Verkuild52c7382007-12-12 08:25:18 -0300724 NTSC-M and radio, hence we force the standard in those
725 cases. */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200726 if (state->radio)
727 std = 0x40;
Hans Verkuild312a462006-01-09 18:21:36 -0200728 else
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200729 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200730 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200731 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200732
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300733 /* wait for tuner to settle down after a channel change */
734 if (msp_sleep(state, 200))
735 goto restart;
736
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200737 if (msp_debug)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200738 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300739 "setting standard: %s (0x%04x)\n",
740 msp_standard_std_name(std), std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200741
742 if (std != 1) {
743 /* programmed some specific mode */
744 val = std;
745 } else {
746 /* triggered autodetect */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200747 msp_write_dem(client, 0x20, std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200748 for (;;) {
Hans Verkuild312a462006-01-09 18:21:36 -0200749 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200750 goto restart;
751
752 /* check results */
753 val = msp_read_dem(client, 0x7e);
754 if (val < 0x07ff)
755 break;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200756 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300757 "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200758 }
759 }
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200760 for (i = 0; msp_stdlist[i].name != NULL; i++)
761 if (msp_stdlist[i].retval == val)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200762 break;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200763 dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200764 msp_standard_std_name(val), val);
765 state->main = msp_stdlist[i].main;
766 state->second = msp_stdlist[i].second;
767 state->std = val;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300768 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200769
Hans Verkuild52c7382007-12-12 08:25:18 -0300770 if (msp_amsound && !state->radio &&
771 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200772 /* autodetection has failed, let backup */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200773 dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
Hans Verkuild52c7382007-12-12 08:25:18 -0300774 msp_stdlist[8].name ?
775 msp_stdlist[8].name : "unknown", val);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300776 state->std = val = 0x0009;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200777 msp_write_dem(client, 0x20, val);
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300778 } else {
779 state->detected_std = msp_standard_std(state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200780 }
781
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200782 /* set stereo */
783 switch (val) {
784 case 0x0008: /* B/G NICAM */
785 case 0x000a: /* I NICAM */
Hans Verkuilde533cc2006-03-19 08:21:56 -0300786 case 0x000b: /* D/K NICAM */
787 if (val == 0x000a)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200788 state->mode = MSP_MODE_FM_NICAM2;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300789 else
790 state->mode = MSP_MODE_FM_NICAM1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200791 /* just turn on stereo */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200792 state->nicam_on = 1;
793 state->watch_stereo = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200794 break;
795 case 0x0009:
796 state->mode = MSP_MODE_AM_NICAM;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200797 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200798 state->watch_stereo = 1;
799 break;
800 case 0x0020: /* BTSC */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200801 /* The pre-'G' models only have BTSC-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200802 state->mode = MSP_MODE_BTSC;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200803 break;
804 case 0x0040: /* FM radio */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200805 state->mode = MSP_MODE_FM_RADIO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200806 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200807 /* not needed in theory if we have radio, but
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200808 short programming enables carrier mute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200809 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
810 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200811 MSP_CARRIER(10.7));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200812 break;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300813 case 0x0002:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200814 case 0x0003:
815 case 0x0004:
816 case 0x0005:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200817 state->mode = MSP_MODE_FM_TERRA;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200818 state->watch_stereo = 1;
819 break;
820 }
821
Hans Verkuilde533cc2006-03-19 08:21:56 -0300822 /* set various prescales */
823 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
824 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
825 if (state->has_nicam)
826 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
827
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200828 if (state->has_i2s_conf)
829 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200830
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300831 /* unmute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200832 msp3400c_set_audmode(client);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300833 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300834 msp_update_volume(state);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200835
836 /* monitor tv audio mode, the first time don't wait
837 so long to get a quick stereo/bilingual result */
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300838 count = 3;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200839 while (state->watch_stereo) {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300840 if (msp_sleep(state, count ? 1000 : 5000))
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200841 goto restart;
Hans Verkuild52c7382007-12-12 08:25:18 -0300842 if (count)
843 count--;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300844 watch_stereo(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200845 }
846 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200847 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200848 return 0;
849}
850
851/* ----------------------------------------------------------------------- */
852
Hans Verkuilde533cc2006-03-19 08:21:56 -0300853/* msp34xxG + (autoselect no-thread)
854 * this one uses both automatic standard detection and automatic sound
855 * select which are available in the newer G versions
856 * struct msp: only norm, acb and source are really used in this mode
857 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200858
Hans Verkuil9a80a932006-03-19 07:25:42 -0300859static int msp34xxg_modus(struct i2c_client *client)
860{
Hans Verkuil76efd622008-11-24 18:16:46 -0300861 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil9a80a932006-03-19 07:25:42 -0300862
863 if (state->radio) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200864 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300865 return 0x0001;
866 }
Hans Verkuil9a80a932006-03-19 07:25:42 -0300867 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200868 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300869 return 0x4001;
870 }
871 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200872 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300873 return 0x0001;
874 }
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300875 if (state->v4l2_std == V4L2_STD_SECAM_L) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200876 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300877 return 0x6001;
878 }
Hans Verkuil9a80a932006-03-19 07:25:42 -0300879 if (state->v4l2_std & V4L2_STD_MN) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200880 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300881 return 0x2001;
882 }
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300883 return 0x7001;
Hans Verkuil9a80a932006-03-19 07:25:42 -0300884}
885
Hans Verkuil2474ed42006-03-19 12:35:57 -0300886static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
887 {
Hans Verkuil76efd622008-11-24 18:16:46 -0300888 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil2474ed42006-03-19 12:35:57 -0300889 int source, matrix;
890
891 switch (state->audmode) {
892 case V4L2_TUNER_MODE_MONO:
893 source = 0; /* mono only */
894 matrix = 0x30;
895 break;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300896 case V4L2_TUNER_MODE_LANG2:
897 source = 4; /* stereo or B */
898 matrix = 0x10;
899 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -0300900 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil2474ed42006-03-19 12:35:57 -0300901 source = 1; /* stereo or A|B */
902 matrix = 0x20;
903 break;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300904 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300905 source = 3; /* stereo or A */
906 matrix = 0x00;
907 break;
Hans Verkuilb331def2006-12-18 13:18:28 -0300908 case V4L2_TUNER_MODE_STEREO:
909 default:
910 source = 3; /* stereo or A */
911 matrix = 0x20;
912 break;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300913 }
914
Hans Verkuil07151722006-04-01 18:03:23 -0300915 if (in == MSP_DSP_IN_TUNER)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300916 source = (source << 8) | 0x20;
917 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
918 instead of 11, 12, 13. So we add one for that msp version. */
Hans Verkuil07151722006-04-01 18:03:23 -0300919 else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300920 source = ((in + 1) << 8) | matrix;
921 else
922 source = (in << 8) | matrix;
923
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200924 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300925 "set source to %d (0x%x) for output %02x\n", in, source, reg);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300926 msp_write_dsp(client, reg, source);
927}
928
929static void msp34xxg_set_sources(struct i2c_client *client)
930{
Hans Verkuil76efd622008-11-24 18:16:46 -0300931 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil5325b422009-04-02 11:26:22 -0300932 u32 in = state->route_in;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300933
934 msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
935 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
936 msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
937 msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
938 msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300939 if (state->has_scart2_out)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300940 msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
941 msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
942}
943
Hans Verkuilde533cc2006-03-19 08:21:56 -0300944/* (re-)initialize the msp34xxg */
945static void msp34xxg_reset(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200946{
Hans Verkuil76efd622008-11-24 18:16:46 -0300947 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil5325b422009-04-02 11:26:22 -0300948 int tuner = (state->route_in >> 3) & 1;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300949 int modus;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200950
Hans Verkuilde533cc2006-03-19 08:21:56 -0300951 /* initialize std to 1 (autodetect) to signal that no standard is
952 selected yet. */
953 state->std = 1;
954
955 msp_reset(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200956
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200957 if (state->has_i2s_conf)
958 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200959
960 /* step-by-step initialisation, as described in the manual */
Hans Verkuil9a80a932006-03-19 07:25:42 -0300961 modus = msp34xxg_modus(client);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300962 modus |= tuner ? 0x100 : 0;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300963 msp_write_dem(client, 0x30, modus);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200964
965 /* write the dsps that may have an influence on
966 standard/audio autodetection right now */
Hans Verkuil2474ed42006-03-19 12:35:57 -0300967 msp34xxg_set_sources(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200968
Hans Verkuilde533cc2006-03-19 08:21:56 -0300969 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
970 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
971 if (state->has_nicam)
972 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200973
Hans Verkuilde533cc2006-03-19 08:21:56 -0300974 /* set identification threshold. Personally, I
975 * I set it to a higher value than the default
976 * of 0x190 to ignore noisy stereo signals.
977 * this needs tuning. (recommended range 0x00a0-0x03c0)
978 * 0x7f0 = forced mono mode
979 *
980 * a2 threshold for stereo/bilingual.
981 * Note: this register is part of the Manual/Compatibility mode.
982 * It is supported by all 'G'-family chips.
983 */
984 msp_write_dem(client, 0x22, msp_stereo_thresh);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200985}
986
987int msp34xxg_thread(void *data)
988{
989 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300990 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilde533cc2006-03-19 08:21:56 -0300991 int val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200992
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200993 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300994 state->detected_std = V4L2_STD_ALL;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700995 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200996 for (;;) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200997 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200998 msp_sleep(state, -1);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200999 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001000
Hans Verkuild52c7382007-12-12 08:25:18 -03001001restart:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001002 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001003 state->restart = 0;
1004 if (kthread_should_stop())
1005 break;
1006
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001007 if (state->mode == MSP_MODE_EXTERN) {
1008 /* no carrier scan needed, just unmute */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001009 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001010 "thread: no carrier scan\n");
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001011 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -03001012 msp_update_volume(state);
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001013 continue;
1014 }
1015
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001016 /* setup the chip*/
1017 msp34xxg_reset(client);
Hans Verkuilde98cda2006-10-01 17:56:32 -03001018 state->std = state->radio ? 0x40 :
1019 (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
Hans Verkuilde533cc2006-03-19 08:21:56 -03001020 msp_write_dem(client, 0x20, state->std);
Hans Verkuilde98cda2006-10-01 17:56:32 -03001021 /* start autodetect */
Hans Verkuile335fad2006-08-08 09:10:15 -03001022 if (state->std != 1)
1023 goto unmute;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001024
1025 /* watch autodetect */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001026 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001027 "started autodetect, waiting for result\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001028 for (i = 0; i < 10; i++) {
1029 if (msp_sleep(state, 100))
1030 goto restart;
1031
1032 /* check results */
1033 val = msp_read_dem(client, 0x7e);
1034 if (val < 0x07ff) {
Hans Verkuilde533cc2006-03-19 08:21:56 -03001035 state->std = val;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001036 break;
1037 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001038 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001039 "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001040 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001041 if (state->std == 1) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001042 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001043 "detection still in progress after 10 tries. giving up.\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001044 continue;
1045 }
1046
Hans Verkuild52c7382007-12-12 08:25:18 -03001047unmute:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001048 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001049 "detected standard: %s (0x%04x)\n",
Hans Verkuilde533cc2006-03-19 08:21:56 -03001050 msp_standard_std_name(state->std), state->std);
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -03001051 state->detected_std = msp_standard_std(state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001052
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001053 if (state->std == 9) {
1054 /* AM NICAM mode */
1055 msp_write_dsp(client, 0x0e, 0x7c00);
1056 }
1057
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001058 /* unmute: dispatch sound to scart output, set scart volume */
Hans Verkuilebc3bba2010-05-24 10:01:58 -03001059 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001060
1061 /* restore ACB */
1062 if (msp_write_dsp(client, 0x13, state->acb))
1063 return -1;
1064
Hans Verkuilde533cc2006-03-19 08:21:56 -03001065 /* the periodic stereo/SAP check is only relevant for
1066 the 0x20 standard (BTSC) */
1067 if (state->std != 0x20)
1068 continue;
1069
1070 state->watch_stereo = 1;
1071
1072 /* monitor tv audio mode, the first time don't wait
1073 in order to get a quick stereo/SAP update */
1074 watch_stereo(client);
1075 while (state->watch_stereo) {
1076 watch_stereo(client);
1077 if (msp_sleep(state, 5000))
1078 goto restart;
1079 }
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001080 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001081 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001082 return 0;
1083}
1084
Hans Verkuilde533cc2006-03-19 08:21:56 -03001085static int msp34xxg_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001086{
Hans Verkuil76efd622008-11-24 18:16:46 -03001087 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001088 int status = msp_read_dem(client, 0x0200);
1089 int is_bilingual = status & 0x100;
1090 int is_stereo = status & 0x40;
Hans Verkuilde533cc2006-03-19 08:21:56 -03001091 int oldrx = state->rxsubchans;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001092
Hans Verkuilb331def2006-12-18 13:18:28 -03001093 if (state->mode == MSP_MODE_EXTERN)
1094 return 0;
1095
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001096 state->rxsubchans = 0;
1097 if (is_stereo)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001098 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001099 else
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001100 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001101 if (is_bilingual) {
Hans Verkuilde533cc2006-03-19 08:21:56 -03001102 if (state->std == 0x20)
1103 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1104 else
Hans Verkuild52c7382007-12-12 08:25:18 -03001105 state->rxsubchans =
1106 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001107 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001108 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001109 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001110 status, is_stereo, is_bilingual, state->rxsubchans);
Hans Verkuilde533cc2006-03-19 08:21:56 -03001111 return (oldrx != state->rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001112}
1113
Adrian Bunk044f3242006-03-08 10:50:07 -03001114static void msp34xxg_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001115{
Hans Verkuil76efd622008-11-24 18:16:46 -03001116 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001117
Hans Verkuilde533cc2006-03-19 08:21:56 -03001118 if (state->std == 0x20) {
1119 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
Mauro Carvalho Chehabcc336682006-03-29 16:29:00 -03001120 (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
Hans Verkuilde533cc2006-03-19 08:21:56 -03001121 state->audmode == V4L2_TUNER_MODE_LANG2)) {
1122 msp_write_dem(client, 0x20, 0x21);
1123 } else {
1124 msp_write_dem(client, 0x20, 0x20);
1125 }
1126 }
1127
Hans Verkuil2474ed42006-03-19 12:35:57 -03001128 msp34xxg_set_sources(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001129}
1130
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001131void msp_set_audmode(struct i2c_client *client)
1132{
Hans Verkuil76efd622008-11-24 18:16:46 -03001133 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001134
1135 switch (state->opmode) {
1136 case OPMODE_MANUAL:
1137 case OPMODE_AUTODETECT:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001138 msp3400c_set_audmode(client);
1139 break;
1140 case OPMODE_AUTOSELECT:
1141 msp34xxg_set_audmode(client);
1142 break;
1143 }
1144}
1145
Hans Verkuilde533cc2006-03-19 08:21:56 -03001146int msp_detect_stereo(struct i2c_client *client)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001147{
Hans Verkuil76efd622008-11-24 18:16:46 -03001148 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001149
1150 switch (state->opmode) {
1151 case OPMODE_MANUAL:
1152 case OPMODE_AUTODETECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001153 return msp3400c_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001154 case OPMODE_AUTOSELECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001155 return msp34xxg_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001156 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001157 return 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001158}
1159