blob: 3235a15db59f3dbc2007a1a8b1bb2f731753c76f [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>
29#include <media/audiochip.h>
30#include <linux/kthread.h>
31#include <linux/suspend.h>
32#include "msp3400.h"
33
34/* this one uses the automatic sound standard detection of newer msp34xx
35 chip versions */
36static struct {
37 int retval;
38 int main, second;
39 char *name;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -020040} msp_stdlist[] = {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020041 { 0x0000, 0, 0, "could not detect sound standard" },
42 { 0x0001, 0, 0, "autodetect start" },
43 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
44 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
45 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
46 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
47 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
48 { 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)" },
53 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
54 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
55 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
56 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
57 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
58 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
59 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
60 { -1, 0, 0, NULL }, /* EOF */
61};
62
63static struct msp3400c_init_data_dem {
64 int fir1[6];
65 int fir2[6];
66 int cdo1;
67 int cdo2;
68 int ad_cv;
69 int mode_reg;
70 int dsp_src;
71 int dsp_matrix;
72} msp3400c_init_data[] = {
73 { /* AM (for carrier detect / msp3400) */
74 {75, 19, 36, 35, 39, 40},
75 {75, 19, 36, 35, 39, 40},
76 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
77 0x00d0, 0x0500, 0x0020, 0x3000
78 },{ /* AM (for carrier detect / msp3410) */
79 {-1, -1, -8, 2, 59, 126},
80 {-1, -1, -8, 2, 59, 126},
81 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
82 0x00d0, 0x0100, 0x0020, 0x3000
83 },{ /* FM Radio */
84 {-8, -8, 4, 6, 78, 107},
85 {-8, -8, 4, 6, 78, 107},
86 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
87 0x00d0, 0x0480, 0x0020, 0x3000
88 },{ /* Terrestial FM-mono + FM-stereo */
89 {3, 18, 27, 48, 66, 72},
90 {3, 18, 27, 48, 66, 72},
91 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
92 0x00d0, 0x0480, 0x0030, 0x3000
93 },{ /* Sat FM-mono */
94 { 1, 9, 14, 24, 33, 37},
95 { 3, 18, 27, 48, 66, 72},
96 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
97 0x00c6, 0x0480, 0x0000, 0x3000
98 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
99 {-2, -8, -10, 10, 50, 86},
100 {3, 18, 27, 48, 66, 72},
101 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
102 0x00d0, 0x0040, 0x0120, 0x3000
103 },{ /* NICAM/FM -- I (6.0/6.552) */
104 {2, 4, -6, -4, 40, 94},
105 {3, 18, 27, 48, 66, 72},
106 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
107 0x00d0, 0x0040, 0x0120, 0x3000
108 },{ /* NICAM/AM -- L (6.5/5.85) */
109 {-2, -8, -10, 10, 50, 86},
110 {-4, -12, -9, 23, 79, 126},
111 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
112 0x00c6, 0x0140, 0x0120, 0x7c03
113 },
114};
115
116struct msp3400c_carrier_detect {
117 int cdo;
118 char *name;
119};
120
121static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
122 /* main carrier */
123 { MSP_CARRIER(4.5), "4.5 NTSC" },
124 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
125 { MSP_CARRIER(6.0), "6.0 PAL I" },
126 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
127};
128
129static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
130 /* PAL B/G */
131 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
132 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
133};
134
135static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
136 /* PAL SAT / SECAM */
137 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
138 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
139 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
140 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
141 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
142 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
143};
144
145/* ------------------------------------------------------------------------ */
146
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200147const char *msp_standard_std_name(int std)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200148{
149 int i;
150
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200151 for (i = 0; msp_stdlist[i].name != NULL; i++)
152 if (msp_stdlist[i].retval == std)
153 return msp_stdlist[i].name;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200154 return "unknown";
155}
156
157void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
158{
159 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
160 msp_write_dem(client, 0x009b, cdo1 >> 12);
161 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
162 msp_write_dem(client, 0x00ab, cdo2 >> 12);
163 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
164}
165
166void msp3400c_setmode(struct i2c_client *client, int type)
167{
168 struct msp_state *state = i2c_get_clientdata(client);
169 int i;
170
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200171 v4l_dbg(1, msp_debug, client, "setmode: %d\n", type);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200172 state->mode = type;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200173 state->rxsubchans = V4L2_TUNER_SUB_MONO;
174
175 msp_write_dem(client, 0x00bb, msp3400c_init_data[type].ad_cv);
176
177 for (i = 5; i >= 0; i--) /* fir 1 */
178 msp_write_dem(client, 0x0001, msp3400c_init_data[type].fir1[i]);
179
180 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
181 msp_write_dem(client, 0x0005, 0x0040);
182 msp_write_dem(client, 0x0005, 0x0000);
183 for (i = 5; i >= 0; i--)
184 msp_write_dem(client, 0x0005, msp3400c_init_data[type].fir2[i]);
185
186 msp_write_dem(client, 0x0083, msp3400c_init_data[type].mode_reg);
187
188 msp3400c_setcarrier(client, msp3400c_init_data[type].cdo1,
189 msp3400c_init_data[type].cdo2);
190
191 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
192
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200193 if (msp_dolby) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200194 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
195 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
196 msp_write_dsp(client, 0x000b, msp3400c_init_data[type].dsp_src);
197 } else {
198 msp_write_dsp(client, 0x0008, msp3400c_init_data[type].dsp_src);
199 msp_write_dsp(client, 0x0009, msp3400c_init_data[type].dsp_src);
200 msp_write_dsp(client, 0x000b, msp3400c_init_data[type].dsp_src);
201 }
202 msp_write_dsp(client, 0x000a, msp3400c_init_data[type].dsp_src);
203 msp_write_dsp(client, 0x000e, msp3400c_init_data[type].dsp_matrix);
204
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200205 if (state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200206 /* nicam prescale */
207 msp_write_dsp(client, 0x0010, 0x5a00); /* was: 0x3000 */
208 }
209}
210
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200211/* turn on/off nicam + stereo */
Hans Verkuil53a72402006-01-23 09:46:02 -0200212void msp3400c_setstereo(struct i2c_client *client, int audmode)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200213{
Hans Verkuild312a462006-01-09 18:21:36 -0200214 static char *strmode[] = { "mono", "stereo", "lang2", "lang1" };
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200215 struct msp_state *state = i2c_get_clientdata(client);
216 int nicam = 0; /* channel source: FM/AM or nicam */
217 int src = 0;
218
219 if (state->opmode == OPMODE_AUTOSELECT) {
220 /* this method would break everything, let's make sure
221 * it's never called
222 */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200223 v4l_dbg(1, msp_debug, client, "setstereo called with mode=%d instead of set_source (ignored)\n",
Hans Verkuil53a72402006-01-23 09:46:02 -0200224 audmode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200225 return;
226 }
227
228 /* switch demodulator */
229 switch (state->mode) {
230 case MSP_MODE_FM_TERRA:
Hans Verkuil53a72402006-01-23 09:46:02 -0200231 v4l_dbg(1, msp_debug, client, "FM setstereo: %s\n", strmode[audmode]);
Hans Verkuild312a462006-01-09 18:21:36 -0200232 msp3400c_setcarrier(client, state->second, state->main);
Hans Verkuil53a72402006-01-23 09:46:02 -0200233 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200234 case V4L2_TUNER_MODE_STEREO:
235 msp_write_dsp(client, 0x000e, 0x3001);
236 break;
237 case V4L2_TUNER_MODE_MONO:
238 case V4L2_TUNER_MODE_LANG1:
239 case V4L2_TUNER_MODE_LANG2:
240 msp_write_dsp(client, 0x000e, 0x3000);
241 break;
242 }
243 break;
244 case MSP_MODE_FM_SAT:
Hans Verkuil53a72402006-01-23 09:46:02 -0200245 v4l_dbg(1, msp_debug, client, "SAT setstereo: %s\n", strmode[audmode]);
246 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200247 case V4L2_TUNER_MODE_MONO:
248 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
249 break;
250 case V4L2_TUNER_MODE_STEREO:
251 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
252 break;
253 case V4L2_TUNER_MODE_LANG1:
254 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
255 break;
256 case V4L2_TUNER_MODE_LANG2:
257 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
258 break;
259 }
260 break;
261 case MSP_MODE_FM_NICAM1:
262 case MSP_MODE_FM_NICAM2:
263 case MSP_MODE_AM_NICAM:
Hans Verkuil53a72402006-01-23 09:46:02 -0200264 v4l_dbg(1, msp_debug, client, "NICAM setstereo: %s\n",strmode[audmode]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200265 msp3400c_setcarrier(client,state->second,state->main);
266 if (state->nicam_on)
267 nicam=0x0100;
268 break;
269 case MSP_MODE_BTSC:
Hans Verkuil53a72402006-01-23 09:46:02 -0200270 v4l_dbg(1, msp_debug, client, "BTSC setstereo: %s\n",strmode[audmode]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200271 nicam=0x0300;
272 break;
273 case MSP_MODE_EXTERN:
Hans Verkuil53a72402006-01-23 09:46:02 -0200274 v4l_dbg(1, msp_debug, client, "extern setstereo: %s\n",strmode[audmode]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200275 nicam = 0x0200;
276 break;
277 case MSP_MODE_FM_RADIO:
Hans Verkuil53a72402006-01-23 09:46:02 -0200278 v4l_dbg(1, msp_debug, client, "FM-Radio setstereo: %s\n",strmode[audmode]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200279 break;
280 default:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200281 v4l_dbg(1, msp_debug, client, "mono setstereo\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200282 return;
283 }
284
285 /* switch audio */
Hans Verkuil53a72402006-01-23 09:46:02 -0200286 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200287 case V4L2_TUNER_MODE_STEREO:
288 src = 0x0020 | nicam;
289 break;
290 case V4L2_TUNER_MODE_MONO:
291 if (state->mode == MSP_MODE_AM_NICAM) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200292 v4l_dbg(1, msp_debug, client, "switching to AM mono\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200293 /* AM mono decoding is handled by tuner, not MSP chip */
294 /* SCART switching control register */
295 msp_set_scart(client, SCART_MONO, 0);
296 src = 0x0200;
297 break;
298 }
299 case V4L2_TUNER_MODE_LANG1:
300 src = 0x0000 | nicam;
301 break;
302 case V4L2_TUNER_MODE_LANG2:
303 src = 0x0010 | nicam;
304 break;
305 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200306 v4l_dbg(1, msp_debug, client, "setstereo final source/matrix = 0x%x\n", src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200307
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200308 if (msp_dolby) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200309 msp_write_dsp(client, 0x0008, 0x0520);
310 msp_write_dsp(client, 0x0009, 0x0620);
311 msp_write_dsp(client, 0x000a, src);
312 msp_write_dsp(client, 0x000b, src);
313 } else {
314 msp_write_dsp(client, 0x0008, src);
315 msp_write_dsp(client, 0x0009, src);
316 msp_write_dsp(client, 0x000a, src);
317 msp_write_dsp(client, 0x000b, src);
Hans Verkuild312a462006-01-09 18:21:36 -0200318 msp_write_dsp(client, 0x000c, src);
319 if (state->has_scart23_in_scart2_out)
320 msp_write_dsp(client, 0x0041, src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200321 }
322}
323
324static void msp3400c_print_mode(struct i2c_client *client)
325{
326 struct msp_state *state = i2c_get_clientdata(client);
327
328 if (state->main == state->second) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200329 v4l_dbg(1, msp_debug, client, "mono sound carrier: %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200330 state->main / 910000, (state->main / 910) % 1000);
331 } else {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200332 v4l_dbg(1, msp_debug, client, "main sound carrier: %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200333 state->main / 910000, (state->main / 910) % 1000);
334 }
335 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200336 v4l_dbg(1, msp_debug, client, "NICAM/FM carrier : %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200337 state->second / 910000, (state->second/910) % 1000);
338 if (state->mode == MSP_MODE_AM_NICAM)
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200339 v4l_dbg(1, msp_debug, client, "NICAM/AM carrier : %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200340 state->second / 910000, (state->second / 910) % 1000);
341 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200342 v4l_dbg(1, msp_debug, client, "FM-stereo carrier : %d.%03d MHz\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200343 state->second / 910000, (state->second / 910) % 1000);
344 }
345}
346
347/* ----------------------------------------------------------------------- */
348
349int autodetect_stereo(struct i2c_client *client)
350{
351 struct msp_state *state = i2c_get_clientdata(client);
352 int val;
353 int rxsubchans = state->rxsubchans;
354 int newnicam = state->nicam_on;
355 int update = 0;
356
357 switch (state->mode) {
358 case MSP_MODE_FM_TERRA:
359 val = msp_read_dsp(client, 0x18);
360 if (val > 32767)
361 val -= 65536;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200362 v4l_dbg(2, msp_debug, client, "stereo detect register: %d\n", val);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200363 if (val > 4096) {
364 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
365 } else if (val < -4096) {
366 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
367 } else {
368 rxsubchans = V4L2_TUNER_SUB_MONO;
369 }
370 newnicam = 0;
371 break;
372 case MSP_MODE_FM_NICAM1:
373 case MSP_MODE_FM_NICAM2:
374 case MSP_MODE_AM_NICAM:
375 val = msp_read_dem(client, 0x23);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200376 v4l_dbg(2, msp_debug, client, "nicam sync=%d, mode=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200377 val & 1, (val & 0x1e) >> 1);
378
379 if (val & 1) {
380 /* nicam synced */
381 switch ((val & 0x1e) >> 1) {
382 case 0:
383 case 8:
384 rxsubchans = V4L2_TUNER_SUB_STEREO;
385 break;
386 case 1:
387 case 9:
388 rxsubchans = V4L2_TUNER_SUB_MONO
389 | V4L2_TUNER_SUB_LANG1;
390 break;
391 case 2:
392 case 10:
393 rxsubchans = V4L2_TUNER_SUB_MONO
394 | V4L2_TUNER_SUB_LANG1
395 | V4L2_TUNER_SUB_LANG2;
396 break;
397 default:
398 rxsubchans = V4L2_TUNER_SUB_MONO;
399 break;
400 }
401 newnicam = 1;
402 } else {
403 newnicam = 0;
404 rxsubchans = V4L2_TUNER_SUB_MONO;
405 }
406 break;
407 case MSP_MODE_BTSC:
408 val = msp_read_dem(client, 0x200);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200409 v4l_dbg(2, msp_debug, client, "status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200410 val,
411 (val & 0x0002) ? "no" : "yes",
412 (val & 0x0004) ? "no" : "yes",
413 (val & 0x0040) ? "stereo" : "mono",
414 (val & 0x0080) ? ", nicam 2nd mono" : "",
415 (val & 0x0100) ? ", bilingual/SAP" : "");
416 rxsubchans = V4L2_TUNER_SUB_MONO;
417 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
418 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
419 break;
420 }
421 if (rxsubchans != state->rxsubchans) {
422 update = 1;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200423 v4l_dbg(1, msp_debug, client, "watch: rxsubchans %d => %d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200424 state->rxsubchans,rxsubchans);
425 state->rxsubchans = rxsubchans;
426 }
427 if (newnicam != state->nicam_on) {
428 update = 1;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200429 v4l_dbg(1, msp_debug, client, "watch: nicam %d => %d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200430 state->nicam_on,newnicam);
431 state->nicam_on = newnicam;
432 }
433 return update;
434}
435
436/*
437 * A kernel thread for msp3400 control -- we don't want to block the
438 * in the ioctl while doing the sound carrier & stereo detect
439 */
440/* stereo/multilang monitoring */
441static void watch_stereo(struct i2c_client *client)
442{
443 struct msp_state *state = i2c_get_clientdata(client);
444
445 if (autodetect_stereo(client)) {
Hans Verkuild312a462006-01-09 18:21:36 -0200446 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200447 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
Hans Verkuild312a462006-01-09 18:21:36 -0200448 else if (state->rxsubchans & V4L2_TUNER_SUB_LANG1)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200449 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
450 else
451 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
452 }
453
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200454 if (msp_once)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200455 state->watch_stereo = 0;
456}
457
458int msp3400c_thread(void *data)
459{
460 struct i2c_client *client = data;
461 struct msp_state *state = i2c_get_clientdata(client);
462 struct msp3400c_carrier_detect *cd;
463 int count, max1,max2,val1,val2, val,this;
464
465
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200466 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200467 for (;;) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200468 v4l_dbg(2, msp_debug, client, "msp3400 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200469 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200470 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200471
472 restart:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200473 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200474 state->restart = 0;
475 if (kthread_should_stop())
476 break;
477
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200478 if (state->radio || MSP_MODE_EXTERN == state->mode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200479 /* no carrier scan, just unmute */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200480 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200481 msp_set_audio(client);
482 continue;
483 }
484
485 /* mute */
486 msp_set_mute(client);
487 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
488 val1 = val2 = 0;
489 max1 = max2 = -1;
490 state->watch_stereo = 0;
491
492 /* some time for the tuner to sync */
493 if (msp_sleep(state,200))
494 goto restart;
495
496 /* carrier detect pass #1 -- main carrier */
497 cd = msp3400c_carrier_detect_main;
498 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
499
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200500 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200501 /* autodetect doesn't work well with AM ... */
502 max1 = 3;
503 count = 0;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200504 v4l_dbg(1, msp_debug, client, "AM sound override\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200505 }
506
507 for (this = 0; this < count; this++) {
508 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
509 if (msp_sleep(state,100))
510 goto restart;
511 val = msp_read_dsp(client, 0x1b);
512 if (val > 32767)
513 val -= 65536;
514 if (val1 < val)
515 val1 = val, max1 = this;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200516 v4l_dbg(1, msp_debug, client, "carrier1 val: %5d / %s\n", val,cd[this].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200517 }
518
519 /* carrier detect pass #2 -- second (stereo) carrier */
520 switch (max1) {
521 case 1: /* 5.5 */
522 cd = msp3400c_carrier_detect_55;
523 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
524 break;
525 case 3: /* 6.5 */
526 cd = msp3400c_carrier_detect_65;
527 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
528 break;
529 case 0: /* 4.5 */
530 case 2: /* 6.0 */
531 default:
532 cd = NULL;
533 count = 0;
534 break;
535 }
536
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200537 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200538 /* autodetect doesn't work well with AM ... */
539 cd = NULL;
540 count = 0;
541 max2 = 0;
542 }
543 for (this = 0; this < count; this++) {
544 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
545 if (msp_sleep(state,100))
546 goto restart;
547 val = msp_read_dsp(client, 0x1b);
548 if (val > 32767)
549 val -= 65536;
550 if (val2 < val)
551 val2 = val, max2 = this;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200552 v4l_dbg(1, msp_debug, client, "carrier2 val: %5d / %s\n", val,cd[this].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200553 }
554
555 /* program the msp3400 according to the results */
556 state->main = msp3400c_carrier_detect_main[max1].cdo;
557 switch (max1) {
558 case 1: /* 5.5 */
559 if (max2 == 0) {
560 /* B/G FM-stereo */
561 state->second = msp3400c_carrier_detect_55[max2].cdo;
562 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
563 state->nicam_on = 0;
564 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
565 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200566 } else if (max2 == 1 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200567 /* B/G NICAM */
568 state->second = msp3400c_carrier_detect_55[max2].cdo;
569 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
570 state->nicam_on = 1;
571 msp3400c_setcarrier(client, state->second, state->main);
572 state->watch_stereo = 1;
573 } else {
574 goto no_second;
575 }
576 break;
577 case 2: /* 6.0 */
578 /* PAL I NICAM */
579 state->second = MSP_CARRIER(6.552);
580 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
581 state->nicam_on = 1;
582 msp3400c_setcarrier(client, state->second, state->main);
583 state->watch_stereo = 1;
584 break;
585 case 3: /* 6.5 */
586 if (max2 == 1 || max2 == 2) {
587 /* D/K FM-stereo */
588 state->second = msp3400c_carrier_detect_65[max2].cdo;
589 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
590 state->nicam_on = 0;
591 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
592 state->watch_stereo = 1;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200593 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200594 /* L NICAM or AM-mono */
595 state->second = msp3400c_carrier_detect_65[max2].cdo;
596 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
597 state->nicam_on = 0;
598 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
599 msp3400c_setcarrier(client, state->second, state->main);
600 /* volume prescale for SCART (AM mono input) */
601 msp_write_dsp(client, 0x000d, 0x1900);
602 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200603 } else if (max2 == 0 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200604 /* D/K NICAM */
605 state->second = msp3400c_carrier_detect_65[max2].cdo;
606 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
607 state->nicam_on = 1;
608 msp3400c_setcarrier(client, state->second, state->main);
609 state->watch_stereo = 1;
610 } else {
611 goto no_second;
612 }
613 break;
614 case 0: /* 4.5 */
615 default:
616 no_second:
617 state->second = msp3400c_carrier_detect_main[max1].cdo;
618 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
619 state->nicam_on = 0;
620 msp3400c_setcarrier(client, state->second, state->main);
621 state->rxsubchans = V4L2_TUNER_SUB_MONO;
622 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
623 break;
624 }
625
626 /* unmute */
627 msp_set_audio(client);
628
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200629 if (msp_debug)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200630 msp3400c_print_mode(client);
631
632 /* monitor tv audio mode */
633 while (state->watch_stereo) {
634 if (msp_sleep(state,5000))
635 goto restart;
636 watch_stereo(client);
637 }
638 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200639 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200640 return 0;
641}
642
643
644int msp3410d_thread(void *data)
645{
646 struct i2c_client *client = data;
647 struct msp_state *state = i2c_get_clientdata(client);
Hans Verkuild312a462006-01-09 18:21:36 -0200648 int val, i, std;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200649
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200650 v4l_dbg(1, msp_debug, client, "msp3410 daemon started\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200651
652 for (;;) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200653 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200654 msp_sleep(state,-1);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200655 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200656
657 restart:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200658 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200659 state->restart = 0;
660 if (kthread_should_stop())
661 break;
662
663 if (state->mode == MSP_MODE_EXTERN) {
664 /* no carrier scan needed, just unmute */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200665 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200666 msp_set_audio(client);
667 continue;
668 }
669
670 /* put into sane state (and mute) */
671 msp_reset(client);
672
673 /* some time for the tuner to sync */
674 if (msp_sleep(state,200))
675 goto restart;
676
677 /* start autodetect */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200678 if (state->radio)
679 std = 0x40;
Hans Verkuild312a462006-01-09 18:21:36 -0200680 else
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200681 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200682 state->watch_stereo = 0;
683
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200684 if (msp_debug)
685 v4l_dbg(1, msp_debug, client, "setting standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200686 msp_standard_std_name(std), std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200687
688 if (std != 1) {
689 /* programmed some specific mode */
690 val = std;
691 } else {
692 /* triggered autodetect */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200693 msp_write_dem(client, 0x20, std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200694 for (;;) {
Hans Verkuild312a462006-01-09 18:21:36 -0200695 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200696 goto restart;
697
698 /* check results */
699 val = msp_read_dem(client, 0x7e);
700 if (val < 0x07ff)
701 break;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200702 v4l_dbg(1, msp_debug, client, "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200703 }
704 }
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200705 for (i = 0; msp_stdlist[i].name != NULL; i++)
706 if (msp_stdlist[i].retval == val)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200707 break;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200708 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200709 msp_standard_std_name(val), val);
710 state->main = msp_stdlist[i].main;
711 state->second = msp_stdlist[i].second;
712 state->std = val;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200713
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200714 if (msp_amsound && !state->radio && (state->v4l2_std & V4L2_STD_SECAM) &&
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200715 (val != 0x0009)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200716 /* autodetection has failed, let backup */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200717 v4l_dbg(1, msp_debug, client, "autodetection failed,"
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200718 " switching to backup standard: %s (0x%04x)\n",
719 msp_stdlist[8].name ? msp_stdlist[8].name : "unknown",val);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200720 val = 0x0009;
721 msp_write_dem(client, 0x20, val);
722 }
723
724 /* set various prescales */
725 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
726 msp_write_dsp(client, 0x0e, 0x2403); /* FM */
727 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
728
729 /* set stereo */
730 switch (val) {
731 case 0x0008: /* B/G NICAM */
732 case 0x000a: /* I NICAM */
733 if (val == 0x0008)
734 state->mode = MSP_MODE_FM_NICAM1;
735 else
736 state->mode = MSP_MODE_FM_NICAM2;
737 /* just turn on stereo */
738 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
739 state->nicam_on = 1;
740 state->watch_stereo = 1;
741 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
742 break;
743 case 0x0009:
744 state->mode = MSP_MODE_AM_NICAM;
745 state->rxsubchans = V4L2_TUNER_SUB_MONO;
746 state->nicam_on = 1;
747 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
748 state->watch_stereo = 1;
749 break;
750 case 0x0020: /* BTSC */
751 /* just turn on stereo */
752 state->mode = MSP_MODE_BTSC;
753 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
754 state->nicam_on = 0;
755 state->watch_stereo = 1;
756 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
757 break;
758 case 0x0040: /* FM radio */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200759 state->mode = MSP_MODE_FM_RADIO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200760 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200761 state->nicam_on = 0;
762 state->watch_stereo = 0;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200763 /* not needed in theory if we have radio, but
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200764 short programming enables carrier mute */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200765 msp3400c_setmode(client, MSP_MODE_FM_RADIO);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200766 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
767 MSP_CARRIER(10.7));
768 /* scart routing */
769 msp_set_scart(client,SCART_IN2,0);
770 /* msp34xx does radio decoding */
771 msp_write_dsp(client, 0x08, 0x0020);
772 msp_write_dsp(client, 0x09, 0x0020);
773 msp_write_dsp(client, 0x0b, 0x0020);
774 break;
775 case 0x0003:
776 case 0x0004:
777 case 0x0005:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200778 state->mode = MSP_MODE_FM_TERRA;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200779 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200780 state->nicam_on = 0;
781 state->watch_stereo = 1;
782 break;
783 }
784
785 /* unmute, restore misc registers */
786 msp_set_audio(client);
787 msp_write_dsp(client, 0x13, state->acb);
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200788 if (state->has_i2s_conf)
789 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200790
791 /* monitor tv audio mode */
792 while (state->watch_stereo) {
793 if (msp_sleep(state,5000))
794 goto restart;
795 watch_stereo(client);
796 }
797 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200798 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200799 return 0;
800}
801
802/* ----------------------------------------------------------------------- */
803
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200804/* msp34xxG + (autoselect no-thread) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200805/* this one uses both automatic standard detection and automatic sound */
806/* select which are available in the newer G versions */
807/* struct msp: only norm, acb and source are really used in this mode */
808
809/* set the same 'source' for the loudspeaker, scart and quasi-peak detector
810 * the value for source is the same as bit 15:8 of DSP registers 0x08,
811 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
812 *
813 * this function replaces msp3400c_setstereo
814 */
815static void msp34xxg_set_source(struct i2c_client *client, int source)
816{
817 struct msp_state *state = i2c_get_clientdata(client);
818
819 /* fix matrix mode to stereo and let the msp choose what
820 * to output according to 'source', as recommended
821 * for MONO (source==0) downmixing set bit[7:0] to 0x30
822 */
823 int value = (source & 0x07) << 8 | (source == 0 ? 0x30 : 0x20);
824
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200825 v4l_dbg(1, msp_debug, client, "set source to %d (0x%x)\n", source, value);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200826 /* Loudspeaker Output */
827 msp_write_dsp(client, 0x08, value);
828 /* SCART1 DA Output */
829 msp_write_dsp(client, 0x0a, value);
830 /* Quasi-peak detector */
831 msp_write_dsp(client, 0x0c, value);
832 /*
833 * set identification threshold. Personally, I
834 * I set it to a higher value that the default
835 * of 0x190 to ignore noisy stereo signals.
836 * this needs tuning. (recommended range 0x00a0-0x03c0)
837 * 0x7f0 = forced mono mode
838 */
839 /* a2 threshold for stereo/bilingual */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200840 msp_write_dem(client, 0x22, msp_stereo_thresh);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200841 state->source = source;
842}
843
844/* (re-)initialize the msp34xxg, according to the current norm in state->norm
845 * return 0 if it worked, -1 if it failed
846 */
847static int msp34xxg_reset(struct i2c_client *client)
848{
849 struct msp_state *state = i2c_get_clientdata(client);
850 int modus, std;
851
852 if (msp_reset(client))
853 return -1;
854
855 /* make sure that input/output is muted (paranoid mode) */
856 /* ACB, mute DSP input, mute SCART 1 */
857 if (msp_write_dsp(client, 0x13, 0x0f20))
858 return -1;
859
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200860 if (state->has_i2s_conf)
861 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200862
863 /* step-by-step initialisation, as described in the manual */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200864 modus = msp_modus(client);
865 if (state->radio)
866 std = 0x40;
867 else
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200868 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200869 modus &= ~0x03; /* STATUS_CHANGE = 0 */
870 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION = 1 */
871 if (msp_write_dem(client, 0x30, modus))
872 return -1;
873 if (msp_write_dem(client, 0x20, std))
874 return -1;
875
876 /* write the dsps that may have an influence on
877 standard/audio autodetection right now */
878 msp34xxg_set_source(client, state->source);
879
880 /* AM/FM Prescale [15:8] 75khz deviation */
881 if (msp_write_dsp(client, 0x0e, 0x3000))
882 return -1;
883
884 /* NICAM Prescale 9db gain (as recommended) */
885 if (msp_write_dsp(client, 0x10, 0x5a00))
886 return -1;
887
888 return 0;
889}
890
891int msp34xxg_thread(void *data)
892{
893 struct i2c_client *client = data;
894 struct msp_state *state = i2c_get_clientdata(client);
895 int val, std, i;
896
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200897 v4l_dbg(1, msp_debug, client, "msp34xxg daemon started\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200898
899 state->source = 1; /* default */
900 for (;;) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200901 v4l_dbg(2, msp_debug, client, "msp34xxg thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200902 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200903 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200904
905 restart:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200906 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200907 state->restart = 0;
908 if (kthread_should_stop())
909 break;
910
911 /* setup the chip*/
912 msp34xxg_reset(client);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200913 std = msp_standard;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200914 if (std != 0x01)
915 goto unmute;
916
917 /* watch autodetect */
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200918 v4l_dbg(1, msp_debug, client, "triggered autodetect, waiting for result\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200919 for (i = 0; i < 10; i++) {
920 if (msp_sleep(state, 100))
921 goto restart;
922
923 /* check results */
924 val = msp_read_dem(client, 0x7e);
925 if (val < 0x07ff) {
926 std = val;
927 break;
928 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200929 v4l_dbg(2, msp_debug, client, "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200930 }
931 if (std == 1) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200932 v4l_dbg(1, msp_debug, client, "detection still in progress after 10 tries. giving up.\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200933 continue;
934 }
935
936 unmute:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200937 state->std = std;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200938 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200939 msp_standard_std_name(std), 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
948 msp_write_dem(client, 0x40, state->i2s_mode);
949 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200950 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200951 return 0;
952}
953
954void msp34xxg_detect_stereo(struct i2c_client *client)
955{
956 struct msp_state *state = i2c_get_clientdata(client);
957
958 int status = msp_read_dem(client, 0x0200);
959 int is_bilingual = status & 0x100;
960 int is_stereo = status & 0x40;
961
962 state->rxsubchans = 0;
963 if (is_stereo)
964 state->rxsubchans |= V4L2_TUNER_SUB_STEREO;
965 else
966 state->rxsubchans |= V4L2_TUNER_SUB_MONO;
967 if (is_bilingual) {
968 state->rxsubchans |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
969 /* I'm supposed to check whether it's SAP or not
970 * and set only LANG2/SAP in this case. Yet, the MSP
971 * does a lot of work to hide this and handle everything
972 * the same way. I don't want to work around it so unless
973 * this is a problem, I'll handle SAP just like lang1/lang2.
974 */
975 }
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200976 v4l_dbg(1, msp_debug, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200977 status, is_stereo, is_bilingual, state->rxsubchans);
978}
979
980void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
981{
982 struct msp_state *state = i2c_get_clientdata(client);
983 int source;
984
985 switch (audmode) {
986 case V4L2_TUNER_MODE_MONO:
987 source = 0; /* mono only */
988 break;
989 case V4L2_TUNER_MODE_STEREO:
990 source = 1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
991 /* problem: that could also mean 2 (scart input) */
992 break;
993 case V4L2_TUNER_MODE_LANG1:
994 source = 3; /* stereo or A */
995 break;
996 case V4L2_TUNER_MODE_LANG2:
997 source = 4; /* stereo or B */
998 break;
999 default:
1000 audmode = 0;
1001 source = 1;
1002 break;
1003 }
1004 state->audmode = audmode;
1005 msp34xxg_set_source(client, source);
1006}
1007