blob: d3b0d1c18efdb4dac8d1142c63284c7d0aeb59eb [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02002/*
3 * Programming the mspx4xx sound processor family
4 *
5 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02006 */
7
8
9#include <linux/kernel.h>
10#include <linux/module.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020011#include <linux/i2c.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070012#include <linux/freezer.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020013#include <linux/videodev2.h>
14#include <media/v4l2-common.h>
Mauro Carvalho Chehabd647f0b2015-11-13 19:40:07 -020015#include <media/drv-intf/msp3400.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020016#include <linux/kthread.h>
17#include <linux/suspend.h>
Hans Verkuil49965a82006-03-19 08:45:38 -030018#include "msp3400-driver.h"
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020019
20/* this one uses the automatic sound standard detection of newer msp34xx
21 chip versions */
22static struct {
23 int retval;
24 int main, second;
25 char *name;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -030026 v4l2_std_id std;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -020027} msp_stdlist[] = {
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -030028 { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL },
29 { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL },
30 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
31 "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN },
32 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
33 "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG },
34 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
35 "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK },
36 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
37 "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK },
38 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
39 "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK },
40 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
41 "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK },
42 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
43 "5.5/5.85 B/G NICAM FM", V4L2_STD_BG },
44 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
45 "6.5/5.85 L NICAM AM", V4L2_STD_L },
46 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
47 "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I },
48 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
49 "6.5/5.85 D/K NICAM FM", V4L2_STD_DK },
50 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
51 "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK },
52 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
53 "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK },
54 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
55 "4.5 M BTSC-Stereo", V4L2_STD_MTS },
56 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
57 "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS },
58 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
59 "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP },
60 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
61 "10.7 FM-Stereo Radio", V4L2_STD_ALL },
62 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
63 "6.5 SAT-Mono", V4L2_STD_ALL },
64 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
65 "7.02/7.20 SAT-Stereo", V4L2_STD_ALL },
66 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
67 "7.2 SAT ADR", V4L2_STD_ALL },
68 { -1, 0, 0, NULL, 0 }, /* EOF */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020069};
70
71static struct msp3400c_init_data_dem {
72 int fir1[6];
73 int fir2[6];
74 int cdo1;
75 int cdo2;
76 int ad_cv;
77 int mode_reg;
78 int dsp_src;
79 int dsp_matrix;
80} msp3400c_init_data[] = {
81 { /* AM (for carrier detect / msp3400) */
82 {75, 19, 36, 35, 39, 40},
83 {75, 19, 36, 35, 39, 40},
84 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
85 0x00d0, 0x0500, 0x0020, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -030086 }, { /* AM (for carrier detect / msp3410) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020087 {-1, -1, -8, 2, 59, 126},
88 {-1, -1, -8, 2, 59, 126},
89 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
90 0x00d0, 0x0100, 0x0020, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -030091 }, { /* FM Radio */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020092 {-8, -8, 4, 6, 78, 107},
93 {-8, -8, 4, 6, 78, 107},
94 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
95 0x00d0, 0x0480, 0x0020, 0x3000
Lucas De Marchi25985ed2011-03-30 22:57:33 -030096 }, { /* Terrestrial FM-mono + FM-stereo */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020097 {3, 18, 27, 48, 66, 72},
98 {3, 18, 27, 48, 66, 72},
99 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
100 0x00d0, 0x0480, 0x0030, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300101 }, { /* Sat FM-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200102 { 1, 9, 14, 24, 33, 37},
103 { 3, 18, 27, 48, 66, 72},
104 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
105 0x00c6, 0x0480, 0x0000, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300106 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200107 {-2, -8, -10, 10, 50, 86},
108 {3, 18, 27, 48, 66, 72},
109 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
110 0x00d0, 0x0040, 0x0120, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300111 }, { /* NICAM/FM -- I (6.0/6.552) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200112 {2, 4, -6, -4, 40, 94},
113 {3, 18, 27, 48, 66, 72},
114 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
115 0x00d0, 0x0040, 0x0120, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300116 }, { /* NICAM/AM -- L (6.5/5.85) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200117 {-2, -8, -10, 10, 50, 86},
118 {-4, -12, -9, 23, 79, 126},
119 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200120 0x00c6, 0x0140, 0x0120, 0x7c00
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200121 },
122};
123
124struct msp3400c_carrier_detect {
125 int cdo;
126 char *name;
127};
128
129static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
130 /* main carrier */
131 { MSP_CARRIER(4.5), "4.5 NTSC" },
132 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
133 { MSP_CARRIER(6.0), "6.0 PAL I" },
134 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
135};
136
137static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
138 /* PAL B/G */
139 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
140 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
141};
142
143static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
144 /* PAL SAT / SECAM */
145 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
146 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
147 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
148 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
149 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
150 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
151};
152
153/* ------------------------------------------------------------------------ */
154
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200155const char *msp_standard_std_name(int std)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200156{
157 int i;
158
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200159 for (i = 0; msp_stdlist[i].name != NULL; i++)
160 if (msp_stdlist[i].retval == std)
161 return msp_stdlist[i].name;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200162 return "unknown";
163}
164
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300165static v4l2_std_id msp_standard_std(int std)
166{
167 int i;
168
169 for (i = 0; msp_stdlist[i].name != NULL; i++)
170 if (msp_stdlist[i].retval == std)
171 return msp_stdlist[i].std;
172 return V4L2_STD_ALL;
173}
174
Adrian Bunk044f3242006-03-08 10:50:07 -0300175static void msp_set_source(struct i2c_client *client, u16 src)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200176{
Hans Verkuil76efd622008-11-24 18:16:46 -0300177 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200178
179 if (msp_dolby) {
180 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
181 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
182 } else {
183 msp_write_dsp(client, 0x0008, src);
184 msp_write_dsp(client, 0x0009, src);
185 }
186 msp_write_dsp(client, 0x000a, src);
187 msp_write_dsp(client, 0x000b, src);
188 msp_write_dsp(client, 0x000c, src);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300189 if (state->has_scart2_out)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200190 msp_write_dsp(client, 0x0041, src);
191}
192
193void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200194{
195 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
196 msp_write_dem(client, 0x009b, cdo1 >> 12);
197 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
198 msp_write_dem(client, 0x00ab, cdo2 >> 12);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200199 msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200200}
201
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200202void msp3400c_set_mode(struct i2c_client *client, int mode)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200203{
Hans Verkuil76efd622008-11-24 18:16:46 -0300204 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200205 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
Hans Verkuil5325b422009-04-02 11:26:22 -0300206 int tuner = (state->route_in >> 3) & 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200207 int i;
208
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200209 dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200210 state->mode = mode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200211 state->rxsubchans = V4L2_TUNER_SUB_MONO;
212
Hans Verkuil2474ed42006-03-19 12:35:57 -0300213 msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200214
215 for (i = 5; i >= 0; i--) /* fir 1 */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200216 msp_write_dem(client, 0x0001, data->fir1[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200217
218 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
219 msp_write_dem(client, 0x0005, 0x0040);
220 msp_write_dem(client, 0x0005, 0x0000);
221 for (i = 5; i >= 0; i--)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200222 msp_write_dem(client, 0x0005, data->fir2[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200223
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200224 msp_write_dem(client, 0x0083, data->mode_reg);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200225
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200226 msp3400c_set_carrier(client, data->cdo1, data->cdo2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200227
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200228 msp_set_source(client, data->dsp_src);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300229 /* set prescales */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200230
Hans Verkuilde533cc2006-03-19 08:21:56 -0300231 /* volume prescale for SCART (AM mono input) */
232 msp_write_dsp(client, 0x000d, 0x1900);
233 msp_write_dsp(client, 0x000e, data->dsp_matrix);
234 if (state->has_nicam) /* nicam prescale */
235 msp_write_dsp(client, 0x0010, 0x5a00);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200236}
237
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200238/* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
239 nor do they support stereo BTSC. */
Adrian Bunk044f3242006-03-08 10:50:07 -0300240static void msp3400c_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200241{
Hans Verkuild52c7382007-12-12 08:25:18 -0300242 static char *strmode[] = {
243 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
244 };
Hans Verkuil76efd622008-11-24 18:16:46 -0300245 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil301e22d2006-03-18 17:15:00 -0300246 char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200247 strmode[state->audmode] : "unknown";
248 int src = 0; /* channel source: FM/AM, nicam or SCART */
Hans Verkuil70615612006-03-29 14:31:44 -0300249 int audmode = state->audmode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200250
251 if (state->opmode == OPMODE_AUTOSELECT) {
252 /* this method would break everything, let's make sure
253 * it's never called
254 */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200255 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200256 "set_audmode called with mode=%d instead of set_source (ignored)\n",
257 state->audmode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200258 return;
259 }
260
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300261 /* Note: for the C and D revs no NTSC stereo + SAP is possible as
262 the hardware does not support SAP. So the rxsubchans combination
263 of STEREO | LANG2 does not occur. */
264
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300265 if (state->mode != MSP_MODE_EXTERN) {
266 /* switch to mono if only mono is available */
267 if (state->rxsubchans == V4L2_TUNER_SUB_MONO)
268 audmode = V4L2_TUNER_MODE_MONO;
269 /* if bilingual */
270 else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) {
271 /* and mono or stereo, then fallback to lang1 */
272 if (audmode == V4L2_TUNER_MODE_MONO ||
273 audmode == V4L2_TUNER_MODE_STEREO)
274 audmode = V4L2_TUNER_MODE_LANG1;
275 }
276 /* if stereo, and audmode is not mono, then switch to stereo */
277 else if (audmode != V4L2_TUNER_MODE_MONO)
278 audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300279 }
Hans Verkuil70615612006-03-29 14:31:44 -0300280
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200281 /* switch demodulator */
282 switch (state->mode) {
283 case MSP_MODE_FM_TERRA:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200284 dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr);
Hans Verkuil70615612006-03-29 14:31:44 -0300285 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200286 case V4L2_TUNER_MODE_STEREO:
287 msp_write_dsp(client, 0x000e, 0x3001);
288 break;
289 case V4L2_TUNER_MODE_MONO:
290 case V4L2_TUNER_MODE_LANG1:
291 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300292 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200293 msp_write_dsp(client, 0x000e, 0x3000);
294 break;
295 }
296 break;
297 case MSP_MODE_FM_SAT:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200298 dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr);
Hans Verkuil70615612006-03-29 14:31:44 -0300299 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200300 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200301 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200302 break;
303 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300304 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200305 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200306 break;
307 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200308 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200309 break;
310 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200311 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200312 break;
313 }
314 break;
315 case MSP_MODE_FM_NICAM1:
316 case MSP_MODE_FM_NICAM2:
317 case MSP_MODE_AM_NICAM:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200318 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300319 "NICAM set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200320 if (state->nicam_on)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200321 src = 0x0100; /* NICAM */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200322 break;
323 case MSP_MODE_BTSC:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200324 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300325 "BTSC set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200326 break;
327 case MSP_MODE_EXTERN:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200328 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300329 "extern set_audmode: %s\n", modestr);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200330 src = 0x0200; /* SCART */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200331 break;
332 case MSP_MODE_FM_RADIO:
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 "FM-Radio set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200335 break;
336 default:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200337 dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200338 return;
339 }
340
341 /* switch audio */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200342 dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode);
Hans Verkuil70615612006-03-29 14:31:44 -0300343 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200344 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300345 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200346 src |= 0x0020;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200347 break;
348 case V4L2_TUNER_MODE_MONO:
349 if (state->mode == MSP_MODE_AM_NICAM) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200350 dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200351 /* AM mono decoding is handled by tuner, not MSP chip */
352 /* SCART switching control register */
353 msp_set_scart(client, SCART_MONO, 0);
354 src = 0x0200;
355 break;
356 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200357 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
358 src = 0x0030;
359 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200360 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200361 break;
362 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200363 src |= 0x0010;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200364 break;
365 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200366 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300367 "set_audmode final source/matrix = 0x%x\n", src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200368
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200369 msp_set_source(client, src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200370}
371
372static void msp3400c_print_mode(struct i2c_client *client)
373{
Hans Verkuil76efd622008-11-24 18:16:46 -0300374 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200375
Hans Verkuild52c7382007-12-12 08:25:18 -0300376 if (state->main == state->second)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200377 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300378 "mono sound carrier: %d.%03d MHz\n",
379 state->main / 910000, (state->main / 910) % 1000);
380 else
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200381 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300382 "main sound carrier: %d.%03d MHz\n",
383 state->main / 910000, (state->main / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200384 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200385 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300386 "NICAM/FM carrier : %d.%03d MHz\n",
387 state->second / 910000, (state->second/910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200388 if (state->mode == MSP_MODE_AM_NICAM)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200389 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300390 "NICAM/AM carrier : %d.%03d MHz\n",
391 state->second / 910000, (state->second / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200392 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200393 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300394 "FM-stereo carrier : %d.%03d MHz\n",
395 state->second / 910000, (state->second / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200396 }
397}
398
399/* ----------------------------------------------------------------------- */
400
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200401static int msp3400c_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200402{
Hans Verkuil76efd622008-11-24 18:16:46 -0300403 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200404 int val;
405 int rxsubchans = state->rxsubchans;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200406 int newnicam = state->nicam_on;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200407 int update = 0;
408
409 switch (state->mode) {
410 case MSP_MODE_FM_TERRA:
411 val = msp_read_dsp(client, 0x18);
412 if (val > 32767)
413 val -= 65536;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200414 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300415 "stereo detect register: %d\n", val);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300416 if (val > 8192) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200417 rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200418 } else if (val < -4096) {
419 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
420 } else {
421 rxsubchans = V4L2_TUNER_SUB_MONO;
422 }
423 newnicam = 0;
424 break;
425 case MSP_MODE_FM_NICAM1:
426 case MSP_MODE_FM_NICAM2:
427 case MSP_MODE_AM_NICAM:
428 val = msp_read_dem(client, 0x23);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200429 dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200430 val & 1, (val & 0x1e) >> 1);
431
432 if (val & 1) {
433 /* nicam synced */
434 switch ((val & 0x1e) >> 1) {
435 case 0:
436 case 8:
437 rxsubchans = V4L2_TUNER_SUB_STEREO;
438 break;
439 case 1:
440 case 9:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200441 rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200442 break;
443 case 2:
444 case 10:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200445 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200446 break;
447 default:
448 rxsubchans = V4L2_TUNER_SUB_MONO;
449 break;
450 }
451 newnicam = 1;
452 } else {
453 newnicam = 0;
454 rxsubchans = V4L2_TUNER_SUB_MONO;
455 }
456 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200457 }
458 if (rxsubchans != state->rxsubchans) {
459 update = 1;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200460 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300461 "watch: rxsubchans %02x => %02x\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200462 state->rxsubchans, rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200463 state->rxsubchans = rxsubchans;
464 }
465 if (newnicam != state->nicam_on) {
466 update = 1;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200467 dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200468 state->nicam_on, newnicam);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200469 state->nicam_on = newnicam;
470 }
471 return update;
472}
473
474/*
475 * A kernel thread for msp3400 control -- we don't want to block the
476 * in the ioctl while doing the sound carrier & stereo detect
477 */
478/* stereo/multilang monitoring */
479static void watch_stereo(struct i2c_client *client)
480{
Hans Verkuil76efd622008-11-24 18:16:46 -0300481 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200482
Hans Verkuild52c7382007-12-12 08:25:18 -0300483 if (msp_detect_stereo(client))
Hans Verkuilde533cc2006-03-19 08:21:56 -0300484 msp_set_audmode(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200485
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200486 if (msp_once)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200487 state->watch_stereo = 0;
488}
489
490int msp3400c_thread(void *data)
491{
492 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300493 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200494 struct msp3400c_carrier_detect *cd;
Hans Verkuild52c7382007-12-12 08:25:18 -0300495 int count, max1, max2, val1, val2, val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200496
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200497 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n");
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300498 state->detected_std = V4L2_STD_ALL;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700499 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200500 for (;;) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200501 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200502 msp_sleep(state, -1);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200503 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200504
Hans Verkuild52c7382007-12-12 08:25:18 -0300505restart:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200506 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200507 state->restart = 0;
508 if (kthread_should_stop())
509 break;
510
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200511 if (state->radio || MSP_MODE_EXTERN == state->mode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200512 /* no carrier scan, just unmute */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200513 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300514 "thread: no carrier scan\n");
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300515 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300516 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200517 continue;
518 }
519
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300520 /* mute audio */
521 state->scan_in_progress = 1;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300522 msp_update_volume(state);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300523
Hans Verkuilde533cc2006-03-19 08:21:56 -0300524 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200525 val1 = val2 = 0;
526 max1 = max2 = -1;
527 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200528 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200529
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300530 /* wait for tuner to settle down after a channel change */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200531 if (msp_sleep(state, 200))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200532 goto restart;
533
534 /* carrier detect pass #1 -- main carrier */
535 cd = msp3400c_carrier_detect_main;
536 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
537
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200538 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200539 /* autodetect doesn't work well with AM ... */
540 max1 = 3;
541 count = 0;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200542 dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200543 }
544
Hans Verkuild52c7382007-12-12 08:25:18 -0300545 for (i = 0; i < count; i++) {
546 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
547 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200548 goto restart;
549 val = msp_read_dsp(client, 0x1b);
550 if (val > 32767)
551 val -= 65536;
552 if (val1 < val)
Hans Verkuild52c7382007-12-12 08:25:18 -0300553 val1 = val, max1 = i;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200554 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300555 "carrier1 val: %5d / %s\n", val, cd[i].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200556 }
557
558 /* carrier detect pass #2 -- second (stereo) carrier */
559 switch (max1) {
560 case 1: /* 5.5 */
561 cd = msp3400c_carrier_detect_55;
562 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
563 break;
564 case 3: /* 6.5 */
565 cd = msp3400c_carrier_detect_65;
566 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
567 break;
568 case 0: /* 4.5 */
569 case 2: /* 6.0 */
570 default:
571 cd = NULL;
572 count = 0;
573 break;
574 }
575
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200576 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200577 /* autodetect doesn't work well with AM ... */
578 cd = NULL;
579 count = 0;
580 max2 = 0;
581 }
Hans Verkuild52c7382007-12-12 08:25:18 -0300582 for (i = 0; i < count; i++) {
583 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
584 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200585 goto restart;
586 val = msp_read_dsp(client, 0x1b);
587 if (val > 32767)
588 val -= 65536;
589 if (val2 < val)
Hans Verkuild52c7382007-12-12 08:25:18 -0300590 val2 = val, max2 = i;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200591 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300592 "carrier2 val: %5d / %s\n", val, cd[i].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200593 }
594
595 /* program the msp3400 according to the results */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200596 state->main = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200597 switch (max1) {
598 case 1: /* 5.5 */
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300599 state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200600 if (max2 == 0) {
601 /* B/G FM-stereo */
602 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200603 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200604 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200605 } else if (max2 == 1 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200606 /* B/G NICAM */
607 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200608 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200609 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200610 state->watch_stereo = 1;
611 } else {
612 goto no_second;
613 }
614 break;
615 case 2: /* 6.0 */
616 /* PAL I NICAM */
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300617 state->detected_std = V4L2_STD_PAL_I;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200618 state->second = MSP_CARRIER(6.552);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200619 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200620 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200621 state->watch_stereo = 1;
622 break;
623 case 3: /* 6.5 */
624 if (max2 == 1 || max2 == 2) {
625 /* D/K FM-stereo */
626 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200627 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200628 state->watch_stereo = 1;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300629 state->detected_std = V4L2_STD_DK;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200630 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200631 /* L NICAM or AM-mono */
632 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200633 msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200634 state->watch_stereo = 1;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300635 state->detected_std = V4L2_STD_L;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200636 } else if (max2 == 0 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200637 /* D/K NICAM */
638 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200639 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200640 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200641 state->watch_stereo = 1;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300642 state->detected_std = V4L2_STD_DK;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200643 } else {
644 goto no_second;
645 }
646 break;
647 case 0: /* 4.5 */
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300648 state->detected_std = V4L2_STD_MN;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -0300649 /* fall-through */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200650 default:
Hans Verkuild52c7382007-12-12 08:25:18 -0300651no_second:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200652 state->second = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200653 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200654 break;
655 }
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300656 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200657
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300658 /* unmute */
659 state->scan_in_progress = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200660 msp3400c_set_audmode(client);
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300661 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200662
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200663 if (msp_debug)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200664 msp3400c_print_mode(client);
665
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200666 /* monitor tv audio mode, the first time don't wait
667 so long to get a quick stereo/bilingual result */
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300668 count = 3;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200669 while (state->watch_stereo) {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300670 if (msp_sleep(state, count ? 1000 : 5000))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200671 goto restart;
Hans Verkuild52c7382007-12-12 08:25:18 -0300672 if (count)
673 count--;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300674 watch_stereo(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200675 }
676 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200677 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200678 return 0;
679}
680
681
682int msp3410d_thread(void *data)
683{
684 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300685 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300686 int val, i, std, count;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200687
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200688 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300689 state->detected_std = V4L2_STD_ALL;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700690 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200691 for (;;) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200692 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
Hans Verkuild52c7382007-12-12 08:25:18 -0300693 msp_sleep(state, -1);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200694 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200695
Hans Verkuild52c7382007-12-12 08:25:18 -0300696restart:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200697 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200698 state->restart = 0;
699 if (kthread_should_stop())
700 break;
701
702 if (state->mode == MSP_MODE_EXTERN) {
703 /* no carrier scan needed, just unmute */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200704 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300705 "thread: no carrier scan\n");
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300706 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300707 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200708 continue;
709 }
710
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300711 /* mute audio */
712 state->scan_in_progress = 1;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300713 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200714
Hans Verkuilde533cc2006-03-19 08:21:56 -0300715 /* start autodetect. Note: autodetect is not supported for
Hans Verkuild52c7382007-12-12 08:25:18 -0300716 NTSC-M and radio, hence we force the standard in those
717 cases. */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200718 if (state->radio)
719 std = 0x40;
Hans Verkuild312a462006-01-09 18:21:36 -0200720 else
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200721 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200722 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200723 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200724
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300725 /* wait for tuner to settle down after a channel change */
726 if (msp_sleep(state, 200))
727 goto restart;
728
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200729 if (msp_debug)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200730 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300731 "setting standard: %s (0x%04x)\n",
732 msp_standard_std_name(std), std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200733
734 if (std != 1) {
735 /* programmed some specific mode */
736 val = std;
737 } else {
738 /* triggered autodetect */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200739 msp_write_dem(client, 0x20, std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200740 for (;;) {
Hans Verkuild312a462006-01-09 18:21:36 -0200741 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200742 goto restart;
743
744 /* check results */
745 val = msp_read_dem(client, 0x7e);
746 if (val < 0x07ff)
747 break;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200748 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300749 "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200750 }
751 }
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200752 for (i = 0; msp_stdlist[i].name != NULL; i++)
753 if (msp_stdlist[i].retval == val)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200754 break;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200755 dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200756 msp_standard_std_name(val), val);
757 state->main = msp_stdlist[i].main;
758 state->second = msp_stdlist[i].second;
759 state->std = val;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300760 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200761
Hans Verkuild52c7382007-12-12 08:25:18 -0300762 if (msp_amsound && !state->radio &&
763 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200764 /* autodetection has failed, let backup */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200765 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 -0300766 msp_stdlist[8].name ?
767 msp_stdlist[8].name : "unknown", val);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300768 state->std = val = 0x0009;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200769 msp_write_dem(client, 0x20, val);
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300770 } else {
771 state->detected_std = msp_standard_std(state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200772 }
773
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200774 /* set stereo */
775 switch (val) {
776 case 0x0008: /* B/G NICAM */
777 case 0x000a: /* I NICAM */
Hans Verkuilde533cc2006-03-19 08:21:56 -0300778 case 0x000b: /* D/K NICAM */
779 if (val == 0x000a)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200780 state->mode = MSP_MODE_FM_NICAM2;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300781 else
782 state->mode = MSP_MODE_FM_NICAM1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200783 /* just turn on stereo */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200784 state->nicam_on = 1;
785 state->watch_stereo = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200786 break;
787 case 0x0009:
788 state->mode = MSP_MODE_AM_NICAM;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200789 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200790 state->watch_stereo = 1;
791 break;
792 case 0x0020: /* BTSC */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200793 /* The pre-'G' models only have BTSC-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200794 state->mode = MSP_MODE_BTSC;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200795 break;
796 case 0x0040: /* FM radio */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200797 state->mode = MSP_MODE_FM_RADIO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200798 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200799 /* not needed in theory if we have radio, but
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200800 short programming enables carrier mute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200801 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
802 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200803 MSP_CARRIER(10.7));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200804 break;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300805 case 0x0002:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200806 case 0x0003:
807 case 0x0004:
808 case 0x0005:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200809 state->mode = MSP_MODE_FM_TERRA;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200810 state->watch_stereo = 1;
811 break;
812 }
813
Hans Verkuilde533cc2006-03-19 08:21:56 -0300814 /* set various prescales */
815 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
816 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
817 if (state->has_nicam)
818 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
819
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200820 if (state->has_i2s_conf)
821 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200822
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300823 /* unmute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200824 msp3400c_set_audmode(client);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300825 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300826 msp_update_volume(state);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200827
828 /* monitor tv audio mode, the first time don't wait
829 so long to get a quick stereo/bilingual result */
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300830 count = 3;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200831 while (state->watch_stereo) {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300832 if (msp_sleep(state, count ? 1000 : 5000))
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200833 goto restart;
Hans Verkuild52c7382007-12-12 08:25:18 -0300834 if (count)
835 count--;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300836 watch_stereo(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200837 }
838 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200839 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200840 return 0;
841}
842
843/* ----------------------------------------------------------------------- */
844
Hans Verkuilde533cc2006-03-19 08:21:56 -0300845/* msp34xxG + (autoselect no-thread)
846 * this one uses both automatic standard detection and automatic sound
847 * select which are available in the newer G versions
848 * struct msp: only norm, acb and source are really used in this mode
849 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200850
Hans Verkuil9a80a932006-03-19 07:25:42 -0300851static int msp34xxg_modus(struct i2c_client *client)
852{
Hans Verkuil76efd622008-11-24 18:16:46 -0300853 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil9a80a932006-03-19 07:25:42 -0300854
855 if (state->radio) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200856 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300857 return 0x0001;
858 }
Hans Verkuil9a80a932006-03-19 07:25:42 -0300859 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200860 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300861 return 0x4001;
862 }
863 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200864 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300865 return 0x0001;
866 }
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300867 if (state->v4l2_std == V4L2_STD_SECAM_L) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200868 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300869 return 0x6001;
870 }
Hans Verkuil9a80a932006-03-19 07:25:42 -0300871 if (state->v4l2_std & V4L2_STD_MN) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200872 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
Hans Verkuil9a80a932006-03-19 07:25:42 -0300873 return 0x2001;
874 }
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300875 return 0x7001;
Hans Verkuil9a80a932006-03-19 07:25:42 -0300876}
877
Hans Verkuil2474ed42006-03-19 12:35:57 -0300878static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
Joe Perches447a5642018-03-21 15:09:32 -0700879{
Hans Verkuil76efd622008-11-24 18:16:46 -0300880 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil2474ed42006-03-19 12:35:57 -0300881 int source, matrix;
882
883 switch (state->audmode) {
884 case V4L2_TUNER_MODE_MONO:
885 source = 0; /* mono only */
886 matrix = 0x30;
887 break;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300888 case V4L2_TUNER_MODE_LANG2:
889 source = 4; /* stereo or B */
890 matrix = 0x10;
891 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -0300892 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil2474ed42006-03-19 12:35:57 -0300893 source = 1; /* stereo or A|B */
894 matrix = 0x20;
895 break;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300896 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300897 source = 3; /* stereo or A */
898 matrix = 0x00;
899 break;
Hans Verkuilb331def2006-12-18 13:18:28 -0300900 case V4L2_TUNER_MODE_STEREO:
901 default:
902 source = 3; /* stereo or A */
903 matrix = 0x20;
904 break;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300905 }
906
Hans Verkuil07151722006-04-01 18:03:23 -0300907 if (in == MSP_DSP_IN_TUNER)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300908 source = (source << 8) | 0x20;
909 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
910 instead of 11, 12, 13. So we add one for that msp version. */
Hans Verkuil07151722006-04-01 18:03:23 -0300911 else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300912 source = ((in + 1) << 8) | matrix;
913 else
914 source = (in << 8) | matrix;
915
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200916 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300917 "set source to %d (0x%x) for output %02x\n", in, source, reg);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300918 msp_write_dsp(client, reg, source);
919}
920
921static void msp34xxg_set_sources(struct i2c_client *client)
922{
Hans Verkuil76efd622008-11-24 18:16:46 -0300923 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil5325b422009-04-02 11:26:22 -0300924 u32 in = state->route_in;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300925
926 msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
927 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
928 msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
929 msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
930 msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300931 if (state->has_scart2_out)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300932 msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
933 msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
934}
935
Hans Verkuilde533cc2006-03-19 08:21:56 -0300936/* (re-)initialize the msp34xxg */
937static void msp34xxg_reset(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200938{
Hans Verkuil76efd622008-11-24 18:16:46 -0300939 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil5325b422009-04-02 11:26:22 -0300940 int tuner = (state->route_in >> 3) & 1;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300941 int modus;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200942
Hans Verkuilde533cc2006-03-19 08:21:56 -0300943 /* initialize std to 1 (autodetect) to signal that no standard is
944 selected yet. */
945 state->std = 1;
946
947 msp_reset(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200948
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200949 if (state->has_i2s_conf)
950 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200951
952 /* step-by-step initialisation, as described in the manual */
Hans Verkuil9a80a932006-03-19 07:25:42 -0300953 modus = msp34xxg_modus(client);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300954 modus |= tuner ? 0x100 : 0;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300955 msp_write_dem(client, 0x30, modus);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200956
957 /* write the dsps that may have an influence on
958 standard/audio autodetection right now */
Hans Verkuil2474ed42006-03-19 12:35:57 -0300959 msp34xxg_set_sources(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200960
Hans Verkuilde533cc2006-03-19 08:21:56 -0300961 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
962 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
963 if (state->has_nicam)
964 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200965
Hans Verkuilde533cc2006-03-19 08:21:56 -0300966 /* set identification threshold. Personally, I
967 * I set it to a higher value than the default
968 * of 0x190 to ignore noisy stereo signals.
969 * this needs tuning. (recommended range 0x00a0-0x03c0)
970 * 0x7f0 = forced mono mode
971 *
972 * a2 threshold for stereo/bilingual.
973 * Note: this register is part of the Manual/Compatibility mode.
974 * It is supported by all 'G'-family chips.
975 */
976 msp_write_dem(client, 0x22, msp_stereo_thresh);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200977}
978
979int msp34xxg_thread(void *data)
980{
981 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300982 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilde533cc2006-03-19 08:21:56 -0300983 int val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200984
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200985 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300986 state->detected_std = V4L2_STD_ALL;
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700987 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200988 for (;;) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200989 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200990 msp_sleep(state, -1);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200991 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200992
Hans Verkuild52c7382007-12-12 08:25:18 -0300993restart:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200994 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200995 state->restart = 0;
996 if (kthread_should_stop())
997 break;
998
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300999 if (state->mode == MSP_MODE_EXTERN) {
1000 /* no carrier scan needed, just unmute */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001001 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001002 "thread: no carrier scan\n");
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001003 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -03001004 msp_update_volume(state);
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001005 continue;
1006 }
1007
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001008 /* setup the chip*/
1009 msp34xxg_reset(client);
Hans Verkuilde98cda2006-10-01 17:56:32 -03001010 state->std = state->radio ? 0x40 :
1011 (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
Hans Verkuilde533cc2006-03-19 08:21:56 -03001012 msp_write_dem(client, 0x20, state->std);
Hans Verkuilde98cda2006-10-01 17:56:32 -03001013 /* start autodetect */
Hans Verkuile335fad2006-08-08 09:10:15 -03001014 if (state->std != 1)
1015 goto unmute;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001016
1017 /* watch autodetect */
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001018 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001019 "started autodetect, waiting for result\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001020 for (i = 0; i < 10; i++) {
1021 if (msp_sleep(state, 100))
1022 goto restart;
1023
1024 /* check results */
1025 val = msp_read_dem(client, 0x7e);
1026 if (val < 0x07ff) {
Hans Verkuilde533cc2006-03-19 08:21:56 -03001027 state->std = val;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001028 break;
1029 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001030 dev_dbg_lvl(&client->dev, 2, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001031 "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001032 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001033 if (state->std == 1) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001034 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001035 "detection still in progress after 10 tries. giving up.\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001036 continue;
1037 }
1038
Hans Verkuild52c7382007-12-12 08:25:18 -03001039unmute:
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001040 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001041 "detected standard: %s (0x%04x)\n",
Hans Verkuilde533cc2006-03-19 08:21:56 -03001042 msp_standard_std_name(state->std), state->std);
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -03001043 state->detected_std = msp_standard_std(state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001044
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001045 if (state->std == 9) {
1046 /* AM NICAM mode */
1047 msp_write_dsp(client, 0x0e, 0x7c00);
1048 }
1049
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001050 /* unmute: dispatch sound to scart output, set scart volume */
Hans Verkuilebc3bba2010-05-24 10:01:58 -03001051 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001052
1053 /* restore ACB */
1054 if (msp_write_dsp(client, 0x13, state->acb))
1055 return -1;
1056
Hans Verkuilde533cc2006-03-19 08:21:56 -03001057 /* the periodic stereo/SAP check is only relevant for
1058 the 0x20 standard (BTSC) */
1059 if (state->std != 0x20)
1060 continue;
1061
1062 state->watch_stereo = 1;
1063
1064 /* monitor tv audio mode, the first time don't wait
1065 in order to get a quick stereo/SAP update */
1066 watch_stereo(client);
1067 while (state->watch_stereo) {
1068 watch_stereo(client);
1069 if (msp_sleep(state, 5000))
1070 goto restart;
1071 }
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001072 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001073 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001074 return 0;
1075}
1076
Hans Verkuilde533cc2006-03-19 08:21:56 -03001077static int msp34xxg_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001078{
Hans Verkuil76efd622008-11-24 18:16:46 -03001079 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001080 int status = msp_read_dem(client, 0x0200);
1081 int is_bilingual = status & 0x100;
1082 int is_stereo = status & 0x40;
Hans Verkuilde533cc2006-03-19 08:21:56 -03001083 int oldrx = state->rxsubchans;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001084
Hans Verkuilb331def2006-12-18 13:18:28 -03001085 if (state->mode == MSP_MODE_EXTERN)
1086 return 0;
1087
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001088 state->rxsubchans = 0;
1089 if (is_stereo)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001090 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001091 else
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001092 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001093 if (is_bilingual) {
Hans Verkuilde533cc2006-03-19 08:21:56 -03001094 if (state->std == 0x20)
1095 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1096 else
Hans Verkuild52c7382007-12-12 08:25:18 -03001097 state->rxsubchans =
1098 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001099 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -02001100 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -03001101 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001102 status, is_stereo, is_bilingual, state->rxsubchans);
Hans Verkuilde533cc2006-03-19 08:21:56 -03001103 return (oldrx != state->rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001104}
1105
Adrian Bunk044f3242006-03-08 10:50:07 -03001106static void msp34xxg_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001107{
Hans Verkuil76efd622008-11-24 18:16:46 -03001108 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001109
Hans Verkuilde533cc2006-03-19 08:21:56 -03001110 if (state->std == 0x20) {
1111 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
Mauro Carvalho Chehabcc336682006-03-29 16:29:00 -03001112 (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
Hans Verkuilde533cc2006-03-19 08:21:56 -03001113 state->audmode == V4L2_TUNER_MODE_LANG2)) {
1114 msp_write_dem(client, 0x20, 0x21);
1115 } else {
1116 msp_write_dem(client, 0x20, 0x20);
1117 }
1118 }
1119
Hans Verkuil2474ed42006-03-19 12:35:57 -03001120 msp34xxg_set_sources(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001121}
1122
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001123void msp_set_audmode(struct i2c_client *client)
1124{
Hans Verkuil76efd622008-11-24 18:16:46 -03001125 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001126
1127 switch (state->opmode) {
1128 case OPMODE_MANUAL:
1129 case OPMODE_AUTODETECT:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001130 msp3400c_set_audmode(client);
1131 break;
1132 case OPMODE_AUTOSELECT:
1133 msp34xxg_set_audmode(client);
1134 break;
1135 }
1136}
1137
Hans Verkuilde533cc2006-03-19 08:21:56 -03001138int msp_detect_stereo(struct i2c_client *client)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001139{
Hans Verkuil76efd622008-11-24 18:16:46 -03001140 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001141
1142 switch (state->opmode) {
1143 case OPMODE_MANUAL:
1144 case OPMODE_AUTODETECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001145 return msp3400c_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001146 case OPMODE_AUTOSELECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001147 return msp34xxg_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001148 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001149 return 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001150}
1151