blob: 88b216cc49d774d73e8f0b175eee78ca50185cb6 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/i2c.h>
26#include <linux/videodev.h>
27#include <linux/videodev2.h>
28#include <media/v4l2-common.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020029#include <linux/kthread.h>
30#include <linux/suspend.h>
31#include "msp3400.h"
32
33/* this one uses the automatic sound standard detection of newer msp34xx
34 chip versions */
35static struct {
36 int retval;
37 int main, second;
38 char *name;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -020039} msp_stdlist[] = {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020040 { 0x0000, 0, 0, "could not detect sound standard" },
41 { 0x0001, 0, 0, "autodetect start" },
42 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
43 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
44 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
45 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
46 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
Hans Verkuilde533cc2006-03-19 08:21:56 -030047 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875), "6.5/5.74 D/K3 Dual FM-Stereo" },
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020048 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
49 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
50 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
51 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
52 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
Hans Verkuilde533cc2006-03-19 08:21:56 -030053 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV3)" },
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020054 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
55 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
56 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
57 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
58 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
59 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
60 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
61 { -1, 0, 0, NULL }, /* EOF */
62};
63
64static struct msp3400c_init_data_dem {
65 int fir1[6];
66 int fir2[6];
67 int cdo1;
68 int cdo2;
69 int ad_cv;
70 int mode_reg;
71 int dsp_src;
72 int dsp_matrix;
73} msp3400c_init_data[] = {
74 { /* AM (for carrier detect / msp3400) */
75 {75, 19, 36, 35, 39, 40},
76 {75, 19, 36, 35, 39, 40},
77 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
78 0x00d0, 0x0500, 0x0020, 0x3000
79 },{ /* AM (for carrier detect / msp3410) */
80 {-1, -1, -8, 2, 59, 126},
81 {-1, -1, -8, 2, 59, 126},
82 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
83 0x00d0, 0x0100, 0x0020, 0x3000
84 },{ /* FM Radio */
85 {-8, -8, 4, 6, 78, 107},
86 {-8, -8, 4, 6, 78, 107},
87 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
88 0x00d0, 0x0480, 0x0020, 0x3000
89 },{ /* Terrestial FM-mono + FM-stereo */
90 {3, 18, 27, 48, 66, 72},
91 {3, 18, 27, 48, 66, 72},
92 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
93 0x00d0, 0x0480, 0x0030, 0x3000
94 },{ /* Sat FM-mono */
95 { 1, 9, 14, 24, 33, 37},
96 { 3, 18, 27, 48, 66, 72},
97 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
98 0x00c6, 0x0480, 0x0000, 0x3000
99 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
100 {-2, -8, -10, 10, 50, 86},
101 {3, 18, 27, 48, 66, 72},
102 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
103 0x00d0, 0x0040, 0x0120, 0x3000
104 },{ /* NICAM/FM -- I (6.0/6.552) */
105 {2, 4, -6, -4, 40, 94},
106 {3, 18, 27, 48, 66, 72},
107 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
108 0x00d0, 0x0040, 0x0120, 0x3000
109 },{ /* NICAM/AM -- L (6.5/5.85) */
110 {-2, -8, -10, 10, 50, 86},
111 {-4, -12, -9, 23, 79, 126},
112 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200113 0x00c6, 0x0140, 0x0120, 0x7c00
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200114 },
115};
116
117struct msp3400c_carrier_detect {
118 int cdo;
119 char *name;
120};
121
122static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
123 /* main carrier */
124 { MSP_CARRIER(4.5), "4.5 NTSC" },
125 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
126 { MSP_CARRIER(6.0), "6.0 PAL I" },
127 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
128};
129
130static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
131 /* PAL B/G */
132 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
133 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
134};
135
136static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
137 /* PAL SAT / SECAM */
138 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
139 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
140 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
141 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
142 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
143 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
144};
145
146/* ------------------------------------------------------------------------ */
147
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200148const char *msp_standard_std_name(int std)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200149{
150 int i;
151
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200152 for (i = 0; msp_stdlist[i].name != NULL; i++)
153 if (msp_stdlist[i].retval == std)
154 return msp_stdlist[i].name;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200155 return "unknown";
156}
157
Adrian Bunk044f3242006-03-08 10:50:07 -0300158static void msp_set_source(struct i2c_client *client, u16 src)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200159{
160 struct msp_state *state = i2c_get_clientdata(client);
161
162 if (msp_dolby) {
163 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
164 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
165 } else {
166 msp_write_dsp(client, 0x0008, src);
167 msp_write_dsp(client, 0x0009, src);
168 }
169 msp_write_dsp(client, 0x000a, src);
170 msp_write_dsp(client, 0x000b, src);
171 msp_write_dsp(client, 0x000c, src);
172 if (state->has_scart23_in_scart2_out)
173 msp_write_dsp(client, 0x0041, src);
174}
175
176void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200177{
178 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
179 msp_write_dem(client, 0x009b, cdo1 >> 12);
180 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
181 msp_write_dem(client, 0x00ab, cdo2 >> 12);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200182 msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200183}
184
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200185void msp3400c_set_mode(struct i2c_client *client, int mode)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200186{
187 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200188 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200189 int i;
190
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200191 v4l_dbg(1, msp_debug, client, "set_mode: %d\n", mode);
192 state->mode = mode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200193 state->rxsubchans = V4L2_TUNER_SUB_MONO;
194
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200195 msp_write_dem(client, 0x00bb, data->ad_cv);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200196
197 for (i = 5; i >= 0; i--) /* fir 1 */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200198 msp_write_dem(client, 0x0001, data->fir1[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200199
200 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
201 msp_write_dem(client, 0x0005, 0x0040);
202 msp_write_dem(client, 0x0005, 0x0000);
203 for (i = 5; i >= 0; i--)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200204 msp_write_dem(client, 0x0005, data->fir2[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200205
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200206 msp_write_dem(client, 0x0083, data->mode_reg);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200207
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200208 msp3400c_set_carrier(client, data->cdo1, data->cdo2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200209
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200210 msp_set_source(client, data->dsp_src);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300211 /* set prescales */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200212
Hans Verkuilde533cc2006-03-19 08:21:56 -0300213 /* volume prescale for SCART (AM mono input) */
214 msp_write_dsp(client, 0x000d, 0x1900);
215 msp_write_dsp(client, 0x000e, data->dsp_matrix);
216 if (state->has_nicam) /* nicam prescale */
217 msp_write_dsp(client, 0x0010, 0x5a00);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200218}
219
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200220/* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
221 nor do they support stereo BTSC. */
Adrian Bunk044f3242006-03-08 10:50:07 -0300222static void msp3400c_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200223{
Hans Verkuild312a462006-01-09 18:21:36 -0200224 static char *strmode[] = { "mono", "stereo", "lang2", "lang1" };
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200225 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200226 char *modestr = (state->audmode >= 0 && state->audmode < 4) ?
227 strmode[state->audmode] : "unknown";
228 int src = 0; /* channel source: FM/AM, nicam or SCART */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200229
230 if (state->opmode == OPMODE_AUTOSELECT) {
231 /* this method would break everything, let's make sure
232 * it's never called
233 */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200234 v4l_dbg(1, msp_debug, client,
235 "set_audmode called with mode=%d instead of set_source (ignored)\n",
236 state->audmode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200237 return;
238 }
239
240 /* switch demodulator */
241 switch (state->mode) {
242 case MSP_MODE_FM_TERRA:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200243 v4l_dbg(1, msp_debug, client, "FM set_audmode: %s\n", modestr);
244 switch (state->audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200245 case V4L2_TUNER_MODE_STEREO:
246 msp_write_dsp(client, 0x000e, 0x3001);
247 break;
248 case V4L2_TUNER_MODE_MONO:
249 case V4L2_TUNER_MODE_LANG1:
250 case V4L2_TUNER_MODE_LANG2:
251 msp_write_dsp(client, 0x000e, 0x3000);
252 break;
253 }
254 break;
255 case MSP_MODE_FM_SAT:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200256 v4l_dbg(1, msp_debug, client, "SAT set_audmode: %s\n", modestr);
257 switch (state->audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200258 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200259 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200260 break;
261 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200262 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200263 break;
264 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200265 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200266 break;
267 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200268 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200269 break;
270 }
271 break;
272 case MSP_MODE_FM_NICAM1:
273 case MSP_MODE_FM_NICAM2:
274 case MSP_MODE_AM_NICAM:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200275 v4l_dbg(1, msp_debug, client, "NICAM set_audmode: %s\n",modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200276 if (state->nicam_on)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200277 src = 0x0100; /* NICAM */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200278 break;
279 case MSP_MODE_BTSC:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200280 v4l_dbg(1, msp_debug, client, "BTSC set_audmode: %s\n",modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200281 break;
282 case MSP_MODE_EXTERN:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200283 v4l_dbg(1, msp_debug, client, "extern set_audmode: %s\n",modestr);
284 src = 0x0200; /* SCART */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200285 break;
286 case MSP_MODE_FM_RADIO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200287 v4l_dbg(1, msp_debug, client, "FM-Radio set_audmode: %s\n",modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200288 break;
289 default:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200290 v4l_dbg(1, msp_debug, client, "mono set_audmode\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200291 return;
292 }
293
294 /* switch audio */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200295 switch (state->audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200296 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200297 src |= 0x0020;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200298 break;
299 case V4L2_TUNER_MODE_MONO:
300 if (state->mode == MSP_MODE_AM_NICAM) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200301 v4l_dbg(1, msp_debug, client, "switching to AM mono\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200302 /* AM mono decoding is handled by tuner, not MSP chip */
303 /* SCART switching control register */
304 msp_set_scart(client, SCART_MONO, 0);
305 src = 0x0200;
306 break;
307 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200308 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
309 src = 0x0030;
310 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200311 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200312 /* switch to stereo for stereo transmission, otherwise
313 keep first language */
314 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
315 src |= 0x0020;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200316 break;
317 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200318 src |= 0x0010;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200319 break;
320 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200321 v4l_dbg(1, msp_debug, client, "set_audmode final source/matrix = 0x%x\n", src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200322
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200323 msp_set_source(client, src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200324}
325
326static void msp3400c_print_mode(struct i2c_client *client)
327{
328 struct msp_state *state = i2c_get_clientdata(client);
329
330 if (state->main == state->second) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200331 v4l_dbg(1, msp_debug, client, "mono sound carrier: %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200332 state->main / 910000, (state->main / 910) % 1000);
333 } else {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200334 v4l_dbg(1, msp_debug, client, "main sound carrier: %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200335 state->main / 910000, (state->main / 910) % 1000);
336 }
337 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200338 v4l_dbg(1, msp_debug, client, "NICAM/FM carrier : %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200339 state->second / 910000, (state->second/910) % 1000);
340 if (state->mode == MSP_MODE_AM_NICAM)
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200341 v4l_dbg(1, msp_debug, client, "NICAM/AM carrier : %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200342 state->second / 910000, (state->second / 910) % 1000);
343 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200344 v4l_dbg(1, msp_debug, client, "FM-stereo carrier : %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200345 state->second / 910000, (state->second / 910) % 1000);
346 }
347}
348
349/* ----------------------------------------------------------------------- */
350
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200351static int msp3400c_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200352{
353 struct msp_state *state = i2c_get_clientdata(client);
354 int val;
355 int rxsubchans = state->rxsubchans;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200356 int newnicam = state->nicam_on;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200357 int update = 0;
358
359 switch (state->mode) {
360 case MSP_MODE_FM_TERRA:
361 val = msp_read_dsp(client, 0x18);
362 if (val > 32767)
363 val -= 65536;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200364 v4l_dbg(2, msp_debug, client, "stereo detect register: %d\n", val);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200365 if (val > 4096) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200366 rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200367 } else if (val < -4096) {
368 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
369 } else {
370 rxsubchans = V4L2_TUNER_SUB_MONO;
371 }
372 newnicam = 0;
373 break;
374 case MSP_MODE_FM_NICAM1:
375 case MSP_MODE_FM_NICAM2:
376 case MSP_MODE_AM_NICAM:
377 val = msp_read_dem(client, 0x23);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200378 v4l_dbg(2, msp_debug, client, "nicam sync=%d, mode=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200379 val & 1, (val & 0x1e) >> 1);
380
381 if (val & 1) {
382 /* nicam synced */
383 switch ((val & 0x1e) >> 1) {
384 case 0:
385 case 8:
386 rxsubchans = V4L2_TUNER_SUB_STEREO;
387 break;
388 case 1:
389 case 9:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200390 rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200391 break;
392 case 2:
393 case 10:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200394 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200395 break;
396 default:
397 rxsubchans = V4L2_TUNER_SUB_MONO;
398 break;
399 }
400 newnicam = 1;
401 } else {
402 newnicam = 0;
403 rxsubchans = V4L2_TUNER_SUB_MONO;
404 }
405 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200406 }
407 if (rxsubchans != state->rxsubchans) {
408 update = 1;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200409 v4l_dbg(1, msp_debug, client, "watch: rxsubchans %02x => %02x\n",
410 state->rxsubchans, rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200411 state->rxsubchans = rxsubchans;
412 }
413 if (newnicam != state->nicam_on) {
414 update = 1;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200415 v4l_dbg(1, msp_debug, client, "watch: nicam %d => %d\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200416 state->nicam_on, newnicam);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200417 state->nicam_on = newnicam;
418 }
419 return update;
420}
421
422/*
423 * A kernel thread for msp3400 control -- we don't want to block the
424 * in the ioctl while doing the sound carrier & stereo detect
425 */
426/* stereo/multilang monitoring */
427static void watch_stereo(struct i2c_client *client)
428{
429 struct msp_state *state = i2c_get_clientdata(client);
430
Hans Verkuilde533cc2006-03-19 08:21:56 -0300431 if (msp_detect_stereo(client)) {
432 msp_set_audmode(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200433 }
434
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200435 if (msp_once)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200436 state->watch_stereo = 0;
437}
438
439int msp3400c_thread(void *data)
440{
441 struct i2c_client *client = data;
442 struct msp_state *state = i2c_get_clientdata(client);
443 struct msp3400c_carrier_detect *cd;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200444 int count, max1, max2, val1, val2, val, this;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200445
446
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200447 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200448 for (;;) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200449 v4l_dbg(2, msp_debug, client, "msp3400 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200450 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200451 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200452
453 restart:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200454 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200455 state->restart = 0;
456 if (kthread_should_stop())
457 break;
458
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200459 if (state->radio || MSP_MODE_EXTERN == state->mode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200460 /* no carrier scan, just unmute */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200461 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200462 msp_set_audio(client);
463 continue;
464 }
465
466 /* mute */
467 msp_set_mute(client);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300468 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200469 val1 = val2 = 0;
470 max1 = max2 = -1;
471 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200472 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200473
474 /* some time for the tuner to sync */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200475 if (msp_sleep(state, 200))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200476 goto restart;
477
478 /* carrier detect pass #1 -- main carrier */
479 cd = msp3400c_carrier_detect_main;
480 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
481
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200482 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200483 /* autodetect doesn't work well with AM ... */
484 max1 = 3;
485 count = 0;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200486 v4l_dbg(1, msp_debug, client, "AM sound override\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200487 }
488
489 for (this = 0; this < count; this++) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200490 msp3400c_set_carrier(client, cd[this].cdo, cd[this].cdo);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200491 if (msp_sleep(state,100))
492 goto restart;
493 val = msp_read_dsp(client, 0x1b);
494 if (val > 32767)
495 val -= 65536;
496 if (val1 < val)
497 val1 = val, max1 = this;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200498 v4l_dbg(1, msp_debug, client, "carrier1 val: %5d / %s\n", val,cd[this].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200499 }
500
501 /* carrier detect pass #2 -- second (stereo) carrier */
502 switch (max1) {
503 case 1: /* 5.5 */
504 cd = msp3400c_carrier_detect_55;
505 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
506 break;
507 case 3: /* 6.5 */
508 cd = msp3400c_carrier_detect_65;
509 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
510 break;
511 case 0: /* 4.5 */
512 case 2: /* 6.0 */
513 default:
514 cd = NULL;
515 count = 0;
516 break;
517 }
518
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200519 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200520 /* autodetect doesn't work well with AM ... */
521 cd = NULL;
522 count = 0;
523 max2 = 0;
524 }
525 for (this = 0; this < count; this++) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200526 msp3400c_set_carrier(client, cd[this].cdo, cd[this].cdo);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200527 if (msp_sleep(state,100))
528 goto restart;
529 val = msp_read_dsp(client, 0x1b);
530 if (val > 32767)
531 val -= 65536;
532 if (val2 < val)
533 val2 = val, max2 = this;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200534 v4l_dbg(1, msp_debug, client, "carrier2 val: %5d / %s\n", val,cd[this].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200535 }
536
537 /* program the msp3400 according to the results */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200538 state->main = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200539 switch (max1) {
540 case 1: /* 5.5 */
541 if (max2 == 0) {
542 /* B/G FM-stereo */
543 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200544 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200545 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200546 } else if (max2 == 1 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200547 /* B/G NICAM */
548 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200549 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
550 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200551 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200552 state->watch_stereo = 1;
553 } else {
554 goto no_second;
555 }
556 break;
557 case 2: /* 6.0 */
558 /* PAL I NICAM */
559 state->second = MSP_CARRIER(6.552);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200560 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
561 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200562 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200563 state->watch_stereo = 1;
564 break;
565 case 3: /* 6.5 */
566 if (max2 == 1 || max2 == 2) {
567 /* D/K FM-stereo */
568 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200569 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200570 state->watch_stereo = 1;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200571 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200572 /* L NICAM or AM-mono */
573 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200574 msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
575 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200576 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200577 } else if (max2 == 0 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200578 /* D/K NICAM */
579 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200580 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
581 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200582 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200583 state->watch_stereo = 1;
584 } else {
585 goto no_second;
586 }
587 break;
588 case 0: /* 4.5 */
589 default:
590 no_second:
591 state->second = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200592 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
593 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200594 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200595 break;
596 }
597
598 /* unmute */
599 msp_set_audio(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200600 msp3400c_set_audmode(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200601
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200602 if (msp_debug)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200603 msp3400c_print_mode(client);
604
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200605 /* monitor tv audio mode, the first time don't wait
606 so long to get a quick stereo/bilingual result */
607 if (msp_sleep(state, 1000))
608 goto restart;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200609 while (state->watch_stereo) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200610 if (msp_sleep(state, 5000))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200611 goto restart;
612 watch_stereo(client);
613 }
614 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200615 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200616 return 0;
617}
618
619
620int msp3410d_thread(void *data)
621{
622 struct i2c_client *client = data;
623 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuild312a462006-01-09 18:21:36 -0200624 int val, i, std;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200625
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200626 v4l_dbg(1, msp_debug, client, "msp3410 daemon started\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200627
628 for (;;) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200629 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200630 msp_sleep(state,-1);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200631 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200632
633 restart:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200634 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200635 state->restart = 0;
636 if (kthread_should_stop())
637 break;
638
639 if (state->mode == MSP_MODE_EXTERN) {
640 /* no carrier scan needed, just unmute */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200641 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200642 msp_set_audio(client);
643 continue;
644 }
645
646 /* put into sane state (and mute) */
647 msp_reset(client);
648
649 /* some time for the tuner to sync */
650 if (msp_sleep(state,200))
651 goto restart;
652
Hans Verkuilde533cc2006-03-19 08:21:56 -0300653 /* start autodetect. Note: autodetect is not supported for
654 NTSC-M and radio, hence we force the standard in those cases. */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200655 if (state->radio)
656 std = 0x40;
Hans Verkuild312a462006-01-09 18:21:36 -0200657 else
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200658 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200659 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200660 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200661
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200662 if (msp_debug)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200663 v4l_dbg(2, msp_debug, client, "setting standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200664 msp_standard_std_name(std), std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200665
666 if (std != 1) {
667 /* programmed some specific mode */
668 val = std;
669 } else {
670 /* triggered autodetect */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200671 msp_write_dem(client, 0x20, std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200672 for (;;) {
Hans Verkuild312a462006-01-09 18:21:36 -0200673 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200674 goto restart;
675
676 /* check results */
677 val = msp_read_dem(client, 0x7e);
678 if (val < 0x07ff)
679 break;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200680 v4l_dbg(2, msp_debug, client, "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200681 }
682 }
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200683 for (i = 0; msp_stdlist[i].name != NULL; i++)
684 if (msp_stdlist[i].retval == val)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200685 break;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200686 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200687 msp_standard_std_name(val), val);
688 state->main = msp_stdlist[i].main;
689 state->second = msp_stdlist[i].second;
690 state->std = val;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200691
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200692 if (msp_amsound && !state->radio && (state->v4l2_std & V4L2_STD_SECAM) &&
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200693 (val != 0x0009)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200694 /* autodetection has failed, let backup */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200695 v4l_dbg(1, msp_debug, client, "autodetection failed,"
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200696 " switching to backup standard: %s (0x%04x)\n",
697 msp_stdlist[8].name ? msp_stdlist[8].name : "unknown",val);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300698 state->std = val = 0x0009;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200699 msp_write_dem(client, 0x20, val);
700 }
701
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200702 /* set stereo */
703 switch (val) {
704 case 0x0008: /* B/G NICAM */
705 case 0x000a: /* I NICAM */
Hans Verkuilde533cc2006-03-19 08:21:56 -0300706 case 0x000b: /* D/K NICAM */
707 if (val == 0x000a)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200708 state->mode = MSP_MODE_FM_NICAM2;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300709 else
710 state->mode = MSP_MODE_FM_NICAM1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200711 /* just turn on stereo */
712 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
713 state->nicam_on = 1;
714 state->watch_stereo = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200715 break;
716 case 0x0009:
717 state->mode = MSP_MODE_AM_NICAM;
718 state->rxsubchans = V4L2_TUNER_SUB_MONO;
719 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200720 state->watch_stereo = 1;
721 break;
722 case 0x0020: /* BTSC */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200723 /* The pre-'G' models only have BTSC-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200724 state->mode = MSP_MODE_BTSC;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200725 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200726 break;
727 case 0x0040: /* FM radio */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200728 state->mode = MSP_MODE_FM_RADIO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200729 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200730 /* not needed in theory if we have radio, but
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200731 short programming enables carrier mute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200732 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
733 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200734 MSP_CARRIER(10.7));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200735 /* scart routing (this doesn't belong here I think) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200736 msp_set_scart(client,SCART_IN2,0);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200737 break;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300738 case 0x0002:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200739 case 0x0003:
740 case 0x0004:
741 case 0x0005:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200742 state->mode = MSP_MODE_FM_TERRA;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200743 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200744 state->watch_stereo = 1;
745 break;
746 }
747
Hans Verkuilde533cc2006-03-19 08:21:56 -0300748 /* set various prescales */
749 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
750 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
751 if (state->has_nicam)
752 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
753
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200754 if (state->has_i2s_conf)
755 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200756
Hans Verkuilde533cc2006-03-19 08:21:56 -0300757 /* unmute, restore misc registers */
758 msp_set_audio(client);
759
760 msp_write_dsp(client, 0x13, state->acb);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200761 msp3400c_set_audmode(client);
762
763 /* monitor tv audio mode, the first time don't wait
764 so long to get a quick stereo/bilingual result */
765 if (msp_sleep(state, 1000))
766 goto restart;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200767 while (state->watch_stereo) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200768 watch_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200769 if (msp_sleep(state, 5000))
770 goto restart;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200771 }
772 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200773 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200774 return 0;
775}
776
777/* ----------------------------------------------------------------------- */
778
Hans Verkuilde533cc2006-03-19 08:21:56 -0300779/* msp34xxG + (autoselect no-thread)
780 * this one uses both automatic standard detection and automatic sound
781 * select which are available in the newer G versions
782 * struct msp: only norm, acb and source are really used in this mode
783 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200784
785/* set the same 'source' for the loudspeaker, scart and quasi-peak detector
786 * the value for source is the same as bit 15:8 of DSP registers 0x08,
787 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200788 */
789static void msp34xxg_set_source(struct i2c_client *client, int source)
790{
791 struct msp_state *state = i2c_get_clientdata(client);
792
793 /* fix matrix mode to stereo and let the msp choose what
794 * to output according to 'source', as recommended
795 * for MONO (source==0) downmixing set bit[7:0] to 0x30
796 */
797 int value = (source & 0x07) << 8 | (source == 0 ? 0x30 : 0x20);
798
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200799 v4l_dbg(1, msp_debug, client, "set source to %d (0x%x)\n", source, value);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200800 msp_set_source(client, value);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200801 /*
802 * set identification threshold. Personally, I
803 * I set it to a higher value that the default
804 * of 0x190 to ignore noisy stereo signals.
805 * this needs tuning. (recommended range 0x00a0-0x03c0)
806 * 0x7f0 = forced mono mode
807 */
808 /* a2 threshold for stereo/bilingual */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200809 msp_write_dem(client, 0x22, msp_stereo_thresh);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200810 state->source = source;
811}
812
Hans Verkuil9a80a932006-03-19 07:25:42 -0300813static int msp34xxg_modus(struct i2c_client *client)
814{
815 struct msp_state *state = i2c_get_clientdata(client);
816
817 if (state->radio) {
818 v4l_dbg(1, msp_debug, client, "selected radio modus\n");
819 return 0x0001;
820 }
821
822 if (state->v4l2_std & V4L2_STD_PAL) {
823 v4l_dbg(1, msp_debug, client, "selected PAL modus\n");
824 return 0x7001;
825 }
826 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
827 v4l_dbg(1, msp_debug, client, "selected M (EIA-J) modus\n");
828 return 0x4001;
829 }
830 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
831 v4l_dbg(1, msp_debug, client, "selected M (A2) modus\n");
832 return 0x0001;
833 }
834 if (state->v4l2_std & V4L2_STD_MN) {
835 v4l_dbg(1, msp_debug, client, "selected M (BTSC) modus\n");
836 return 0x2001;
837 }
838 if (state->v4l2_std & V4L2_STD_SECAM) {
839 v4l_dbg(1, msp_debug, client, "selected SECAM modus\n");
840 return 0x6001;
841 }
842 return 0x0001;
843}
844
Hans Verkuilde533cc2006-03-19 08:21:56 -0300845/* (re-)initialize the msp34xxg */
846static void msp34xxg_reset(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200847{
848 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300849 int modus;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200850
Hans Verkuilde533cc2006-03-19 08:21:56 -0300851 /* initialize std to 1 (autodetect) to signal that no standard is
852 selected yet. */
853 state->std = 1;
854
855 msp_reset(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200856
857 /* make sure that input/output is muted (paranoid mode) */
858 /* ACB, mute DSP input, mute SCART 1 */
Hans Verkuilde533cc2006-03-19 08:21:56 -0300859 msp_write_dsp(client, 0x13, 0x0f20);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200860
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200861 if (state->has_i2s_conf)
862 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200863
864 /* step-by-step initialisation, as described in the manual */
Hans Verkuil9a80a932006-03-19 07:25:42 -0300865 modus = msp34xxg_modus(client);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300866 msp_write_dem(client, 0x30, modus);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200867
868 /* write the dsps that may have an influence on
869 standard/audio autodetection right now */
870 msp34xxg_set_source(client, state->source);
871
Hans Verkuilde533cc2006-03-19 08:21:56 -0300872 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
873 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
874 if (state->has_nicam)
875 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200876
Hans Verkuilde533cc2006-03-19 08:21:56 -0300877 /* set identification threshold. Personally, I
878 * I set it to a higher value than the default
879 * of 0x190 to ignore noisy stereo signals.
880 * this needs tuning. (recommended range 0x00a0-0x03c0)
881 * 0x7f0 = forced mono mode
882 *
883 * a2 threshold for stereo/bilingual.
884 * Note: this register is part of the Manual/Compatibility mode.
885 * It is supported by all 'G'-family chips.
886 */
887 msp_write_dem(client, 0x22, msp_stereo_thresh);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200888}
889
890int msp34xxg_thread(void *data)
891{
892 struct i2c_client *client = data;
893 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300894 int val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200895
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200896 v4l_dbg(1, msp_debug, client, "msp34xxg daemon started\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200897
898 state->source = 1; /* default */
899 for (;;) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200900 v4l_dbg(2, msp_debug, client, "msp34xxg thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200901 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200902 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200903
904 restart:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200905 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200906 state->restart = 0;
907 if (kthread_should_stop())
908 break;
909
910 /* setup the chip*/
911 msp34xxg_reset(client);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300912 state->std = state->radio ? 0x40 : msp_standard;
913 if (state->std != 1)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200914 goto unmute;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300915 /* start autodetect */
916 msp_write_dem(client, 0x20, state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200917
918 /* watch autodetect */
Hans Verkuilde533cc2006-03-19 08:21:56 -0300919 v4l_dbg(1, msp_debug, client, "started autodetect, waiting for result\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200920 for (i = 0; i < 10; i++) {
921 if (msp_sleep(state, 100))
922 goto restart;
923
924 /* check results */
925 val = msp_read_dem(client, 0x7e);
926 if (val < 0x07ff) {
Hans Verkuilde533cc2006-03-19 08:21:56 -0300927 state->std = val;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200928 break;
929 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200930 v4l_dbg(2, msp_debug, client, "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200931 }
Hans Verkuilde533cc2006-03-19 08:21:56 -0300932 if (state->std == 1) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200933 v4l_dbg(1, msp_debug, client, "detection still in progress after 10 tries. giving up.\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200934 continue;
935 }
936
937 unmute:
Hans Verkuilde533cc2006-03-19 08:21:56 -0300938 v4l_dbg(1, msp_debug, client, "detected standard: %s (0x%04x)\n",
939 msp_standard_std_name(state->std), state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200940
941 /* unmute: dispatch sound to scart output, set scart volume */
942 msp_set_audio(client);
943
944 /* restore ACB */
945 if (msp_write_dsp(client, 0x13, state->acb))
946 return -1;
947
Hans Verkuilde533cc2006-03-19 08:21:56 -0300948 /* the periodic stereo/SAP check is only relevant for
949 the 0x20 standard (BTSC) */
950 if (state->std != 0x20)
951 continue;
952
953 state->watch_stereo = 1;
954
955 /* monitor tv audio mode, the first time don't wait
956 in order to get a quick stereo/SAP update */
957 watch_stereo(client);
958 while (state->watch_stereo) {
959 watch_stereo(client);
960 if (msp_sleep(state, 5000))
961 goto restart;
962 }
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200963 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200964 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200965 return 0;
966}
967
Hans Verkuilde533cc2006-03-19 08:21:56 -0300968static int msp34xxg_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200969{
970 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200971 int status = msp_read_dem(client, 0x0200);
972 int is_bilingual = status & 0x100;
973 int is_stereo = status & 0x40;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300974 int oldrx = state->rxsubchans;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200975
976 state->rxsubchans = 0;
977 if (is_stereo)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200978 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200979 else
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200980 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200981 if (is_bilingual) {
Hans Verkuilde533cc2006-03-19 08:21:56 -0300982 if (state->std == 0x20)
983 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
984 else
985 state->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200986 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200987 v4l_dbg(1, msp_debug, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200988 status, is_stereo, is_bilingual, state->rxsubchans);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300989 return (oldrx != state->rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200990}
991
Adrian Bunk044f3242006-03-08 10:50:07 -0300992static void msp34xxg_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200993{
994 struct msp_state *state = i2c_get_clientdata(client);
995 int source;
996
Hans Verkuilde533cc2006-03-19 08:21:56 -0300997 if (state->std == 0x20) {
998 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
999 (state->audmode == V4L2_TUNER_MODE_STEREO ||
1000 state->audmode == V4L2_TUNER_MODE_LANG2)) {
1001 msp_write_dem(client, 0x20, 0x21);
1002 } else {
1003 msp_write_dem(client, 0x20, 0x20);
1004 }
1005 }
1006
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001007 switch (state->audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001008 case V4L2_TUNER_MODE_MONO:
1009 source = 0; /* mono only */
1010 break;
1011 case V4L2_TUNER_MODE_STEREO:
1012 source = 1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1013 /* problem: that could also mean 2 (scart input) */
1014 break;
1015 case V4L2_TUNER_MODE_LANG1:
1016 source = 3; /* stereo or A */
1017 break;
1018 case V4L2_TUNER_MODE_LANG2:
1019 source = 4; /* stereo or B */
1020 break;
1021 default:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001022 source = 1;
1023 break;
1024 }
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001025 msp34xxg_set_source(client, source);
1026}
1027
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001028void msp_set_audmode(struct i2c_client *client)
1029{
1030 struct msp_state *state = i2c_get_clientdata(client);
1031
1032 switch (state->opmode) {
1033 case OPMODE_MANUAL:
1034 case OPMODE_AUTODETECT:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001035 msp3400c_set_audmode(client);
1036 break;
1037 case OPMODE_AUTOSELECT:
1038 msp34xxg_set_audmode(client);
1039 break;
1040 }
1041}
1042
Hans Verkuilde533cc2006-03-19 08:21:56 -03001043int msp_detect_stereo(struct i2c_client *client)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001044{
1045 struct msp_state *state = i2c_get_clientdata(client);
1046
1047 switch (state->opmode) {
1048 case OPMODE_MANUAL:
1049 case OPMODE_AUTODETECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001050 return msp3400c_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001051 case OPMODE_AUTOSELECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001052 return msp34xxg_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001053 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001054 return 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001055}
1056