blob: b376fcdee6523e1292182b37c2e9f37e42fe81be [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
Hans Verkuild52c7382007-12-12 08:25:18 -030018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020020 */
21
22
23#include <linux/kernel.h>
24#include <linux/module.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020025#include <linux/i2c.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070026#include <linux/freezer.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020027#include <linux/videodev2.h>
28#include <media/v4l2-common.h>
Hans Verkuil49965a82006-03-19 08:45:38 -030029#include <media/msp3400.h>
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020030#include <linux/kthread.h>
31#include <linux/suspend.h>
Hans Verkuil49965a82006-03-19 08:45:38 -030032#include "msp3400-driver.h"
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020033
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)" },
Hans Verkuilde533cc2006-03-19 08:21:56 -030048 { 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 -020049 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
50 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
51 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
52 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
53 { 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 -030054 { 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 -020055 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
56 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
57 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
58 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
59 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
60 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
61 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
62 { -1, 0, 0, NULL }, /* EOF */
63};
64
65static struct msp3400c_init_data_dem {
66 int fir1[6];
67 int fir2[6];
68 int cdo1;
69 int cdo2;
70 int ad_cv;
71 int mode_reg;
72 int dsp_src;
73 int dsp_matrix;
74} msp3400c_init_data[] = {
75 { /* AM (for carrier detect / msp3400) */
76 {75, 19, 36, 35, 39, 40},
77 {75, 19, 36, 35, 39, 40},
78 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
79 0x00d0, 0x0500, 0x0020, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -030080 }, { /* AM (for carrier detect / msp3410) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020081 {-1, -1, -8, 2, 59, 126},
82 {-1, -1, -8, 2, 59, 126},
83 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
84 0x00d0, 0x0100, 0x0020, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -030085 }, { /* FM Radio */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020086 {-8, -8, 4, 6, 78, 107},
87 {-8, -8, 4, 6, 78, 107},
88 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
89 0x00d0, 0x0480, 0x0020, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -030090 }, { /* Terrestial FM-mono + FM-stereo */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020091 {3, 18, 27, 48, 66, 72},
92 {3, 18, 27, 48, 66, 72},
93 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
94 0x00d0, 0x0480, 0x0030, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -030095 }, { /* Sat FM-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -020096 { 1, 9, 14, 24, 33, 37},
97 { 3, 18, 27, 48, 66, 72},
98 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
99 0x00c6, 0x0480, 0x0000, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300100 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200101 {-2, -8, -10, 10, 50, 86},
102 {3, 18, 27, 48, 66, 72},
103 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
104 0x00d0, 0x0040, 0x0120, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300105 }, { /* NICAM/FM -- I (6.0/6.552) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200106 {2, 4, -6, -4, 40, 94},
107 {3, 18, 27, 48, 66, 72},
108 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
109 0x00d0, 0x0040, 0x0120, 0x3000
Hans Verkuild52c7382007-12-12 08:25:18 -0300110 }, { /* NICAM/AM -- L (6.5/5.85) */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200111 {-2, -8, -10, 10, 50, 86},
112 {-4, -12, -9, 23, 79, 126},
113 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200114 0x00c6, 0x0140, 0x0120, 0x7c00
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200115 },
116};
117
118struct msp3400c_carrier_detect {
119 int cdo;
120 char *name;
121};
122
123static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
124 /* main carrier */
125 { MSP_CARRIER(4.5), "4.5 NTSC" },
126 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
127 { MSP_CARRIER(6.0), "6.0 PAL I" },
128 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
129};
130
131static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
132 /* PAL B/G */
133 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
134 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
135};
136
137static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
138 /* PAL SAT / SECAM */
139 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
140 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
141 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
142 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
143 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
144 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
145};
146
147/* ------------------------------------------------------------------------ */
148
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200149const char *msp_standard_std_name(int std)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200150{
151 int i;
152
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200153 for (i = 0; msp_stdlist[i].name != NULL; i++)
154 if (msp_stdlist[i].retval == std)
155 return msp_stdlist[i].name;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200156 return "unknown";
157}
158
Adrian Bunk044f3242006-03-08 10:50:07 -0300159static void msp_set_source(struct i2c_client *client, u16 src)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200160{
Hans Verkuil76efd622008-11-24 18:16:46 -0300161 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200162
163 if (msp_dolby) {
164 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
165 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
166 } else {
167 msp_write_dsp(client, 0x0008, src);
168 msp_write_dsp(client, 0x0009, src);
169 }
170 msp_write_dsp(client, 0x000a, src);
171 msp_write_dsp(client, 0x000b, src);
172 msp_write_dsp(client, 0x000c, src);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300173 if (state->has_scart2_out)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200174 msp_write_dsp(client, 0x0041, src);
175}
176
177void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200178{
179 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
180 msp_write_dem(client, 0x009b, cdo1 >> 12);
181 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
182 msp_write_dem(client, 0x00ab, cdo2 >> 12);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200183 msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200184}
185
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200186void msp3400c_set_mode(struct i2c_client *client, int mode)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200187{
Hans Verkuil76efd622008-11-24 18:16:46 -0300188 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200189 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
Hans Verkuil5325b422009-04-02 11:26:22 -0300190 int tuner = (state->route_in >> 3) & 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200191 int i;
192
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200193 v4l_dbg(1, msp_debug, client, "set_mode: %d\n", mode);
194 state->mode = mode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200195 state->rxsubchans = V4L2_TUNER_SUB_MONO;
196
Hans Verkuil2474ed42006-03-19 12:35:57 -0300197 msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200198
199 for (i = 5; i >= 0; i--) /* fir 1 */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200200 msp_write_dem(client, 0x0001, data->fir1[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200201
202 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
203 msp_write_dem(client, 0x0005, 0x0040);
204 msp_write_dem(client, 0x0005, 0x0000);
205 for (i = 5; i >= 0; i--)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200206 msp_write_dem(client, 0x0005, data->fir2[i]);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200207
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200208 msp_write_dem(client, 0x0083, data->mode_reg);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200209
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200210 msp3400c_set_carrier(client, data->cdo1, data->cdo2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200211
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200212 msp_set_source(client, data->dsp_src);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300213 /* set prescales */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200214
Hans Verkuilde533cc2006-03-19 08:21:56 -0300215 /* volume prescale for SCART (AM mono input) */
216 msp_write_dsp(client, 0x000d, 0x1900);
217 msp_write_dsp(client, 0x000e, data->dsp_matrix);
218 if (state->has_nicam) /* nicam prescale */
219 msp_write_dsp(client, 0x0010, 0x5a00);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200220}
221
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200222/* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
223 nor do they support stereo BTSC. */
Adrian Bunk044f3242006-03-08 10:50:07 -0300224static void msp3400c_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200225{
Hans Verkuild52c7382007-12-12 08:25:18 -0300226 static char *strmode[] = {
227 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
228 };
Hans Verkuil76efd622008-11-24 18:16:46 -0300229 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil301e22d2006-03-18 17:15:00 -0300230 char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200231 strmode[state->audmode] : "unknown";
232 int src = 0; /* channel source: FM/AM, nicam or SCART */
Hans Verkuil70615612006-03-29 14:31:44 -0300233 int audmode = state->audmode;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200234
235 if (state->opmode == OPMODE_AUTOSELECT) {
236 /* this method would break everything, let's make sure
237 * it's never called
238 */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200239 v4l_dbg(1, msp_debug, client,
240 "set_audmode called with mode=%d instead of set_source (ignored)\n",
241 state->audmode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200242 return;
243 }
244
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300245 /* Note: for the C and D revs no NTSC stereo + SAP is possible as
246 the hardware does not support SAP. So the rxsubchans combination
247 of STEREO | LANG2 does not occur. */
248
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300249 if (state->mode != MSP_MODE_EXTERN) {
250 /* switch to mono if only mono is available */
251 if (state->rxsubchans == V4L2_TUNER_SUB_MONO)
252 audmode = V4L2_TUNER_MODE_MONO;
253 /* if bilingual */
254 else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) {
255 /* and mono or stereo, then fallback to lang1 */
256 if (audmode == V4L2_TUNER_MODE_MONO ||
257 audmode == V4L2_TUNER_MODE_STEREO)
258 audmode = V4L2_TUNER_MODE_LANG1;
259 }
260 /* if stereo, and audmode is not mono, then switch to stereo */
261 else if (audmode != V4L2_TUNER_MODE_MONO)
262 audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300263 }
Hans Verkuil70615612006-03-29 14:31:44 -0300264
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200265 /* switch demodulator */
266 switch (state->mode) {
267 case MSP_MODE_FM_TERRA:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200268 v4l_dbg(1, msp_debug, client, "FM set_audmode: %s\n", modestr);
Hans Verkuil70615612006-03-29 14:31:44 -0300269 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200270 case V4L2_TUNER_MODE_STEREO:
271 msp_write_dsp(client, 0x000e, 0x3001);
272 break;
273 case V4L2_TUNER_MODE_MONO:
274 case V4L2_TUNER_MODE_LANG1:
275 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300276 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200277 msp_write_dsp(client, 0x000e, 0x3000);
278 break;
279 }
280 break;
281 case MSP_MODE_FM_SAT:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200282 v4l_dbg(1, msp_debug, client, "SAT set_audmode: %s\n", modestr);
Hans Verkuil70615612006-03-29 14:31:44 -0300283 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200284 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200285 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200286 break;
287 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300288 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200289 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200290 break;
291 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200292 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200293 break;
294 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200295 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200296 break;
297 }
298 break;
299 case MSP_MODE_FM_NICAM1:
300 case MSP_MODE_FM_NICAM2:
301 case MSP_MODE_AM_NICAM:
Hans Verkuild52c7382007-12-12 08:25:18 -0300302 v4l_dbg(1, msp_debug, client,
303 "NICAM set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200304 if (state->nicam_on)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200305 src = 0x0100; /* NICAM */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200306 break;
307 case MSP_MODE_BTSC:
Hans Verkuild52c7382007-12-12 08:25:18 -0300308 v4l_dbg(1, msp_debug, client,
309 "BTSC set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200310 break;
311 case MSP_MODE_EXTERN:
Hans Verkuild52c7382007-12-12 08:25:18 -0300312 v4l_dbg(1, msp_debug, client,
313 "extern set_audmode: %s\n", modestr);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200314 src = 0x0200; /* SCART */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200315 break;
316 case MSP_MODE_FM_RADIO:
Hans Verkuild52c7382007-12-12 08:25:18 -0300317 v4l_dbg(1, msp_debug, client,
318 "FM-Radio set_audmode: %s\n", modestr);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200319 break;
320 default:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200321 v4l_dbg(1, msp_debug, client, "mono set_audmode\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200322 return;
323 }
324
325 /* switch audio */
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300326 v4l_dbg(1, msp_debug, client, "set audmode %d\n", audmode);
Hans Verkuil70615612006-03-29 14:31:44 -0300327 switch (audmode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200328 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil301e22d2006-03-18 17:15:00 -0300329 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200330 src |= 0x0020;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200331 break;
332 case V4L2_TUNER_MODE_MONO:
333 if (state->mode == MSP_MODE_AM_NICAM) {
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200334 v4l_dbg(1, msp_debug, client, "switching to AM mono\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200335 /* AM mono decoding is handled by tuner, not MSP chip */
336 /* SCART switching control register */
337 msp_set_scart(client, SCART_MONO, 0);
338 src = 0x0200;
339 break;
340 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200341 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
342 src = 0x0030;
343 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200344 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200345 break;
346 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200347 src |= 0x0010;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200348 break;
349 }
Hans Verkuild52c7382007-12-12 08:25:18 -0300350 v4l_dbg(1, msp_debug, client,
351 "set_audmode final source/matrix = 0x%x\n", src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200352
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200353 msp_set_source(client, src);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200354}
355
356static void msp3400c_print_mode(struct i2c_client *client)
357{
Hans Verkuil76efd622008-11-24 18:16:46 -0300358 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200359
Hans Verkuild52c7382007-12-12 08:25:18 -0300360 if (state->main == state->second)
361 v4l_dbg(1, msp_debug, client,
362 "mono sound carrier: %d.%03d MHz\n",
363 state->main / 910000, (state->main / 910) % 1000);
364 else
365 v4l_dbg(1, msp_debug, client,
366 "main sound carrier: %d.%03d MHz\n",
367 state->main / 910000, (state->main / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200368 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
Hans Verkuild52c7382007-12-12 08:25:18 -0300369 v4l_dbg(1, msp_debug, client,
370 "NICAM/FM carrier : %d.%03d MHz\n",
371 state->second / 910000, (state->second/910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200372 if (state->mode == MSP_MODE_AM_NICAM)
Hans Verkuild52c7382007-12-12 08:25:18 -0300373 v4l_dbg(1, msp_debug, client,
374 "NICAM/AM carrier : %d.%03d MHz\n",
375 state->second / 910000, (state->second / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200376 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
Hans Verkuild52c7382007-12-12 08:25:18 -0300377 v4l_dbg(1, msp_debug, client,
378 "FM-stereo carrier : %d.%03d MHz\n",
379 state->second / 910000, (state->second / 910) % 1000);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200380 }
381}
382
383/* ----------------------------------------------------------------------- */
384
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200385static int msp3400c_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200386{
Hans Verkuil76efd622008-11-24 18:16:46 -0300387 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200388 int val;
389 int rxsubchans = state->rxsubchans;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200390 int newnicam = state->nicam_on;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200391 int update = 0;
392
393 switch (state->mode) {
394 case MSP_MODE_FM_TERRA:
395 val = msp_read_dsp(client, 0x18);
396 if (val > 32767)
397 val -= 65536;
Hans Verkuild52c7382007-12-12 08:25:18 -0300398 v4l_dbg(2, msp_debug, client,
399 "stereo detect register: %d\n", val);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300400 if (val > 8192) {
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200401 rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200402 } else if (val < -4096) {
403 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
404 } else {
405 rxsubchans = V4L2_TUNER_SUB_MONO;
406 }
407 newnicam = 0;
408 break;
409 case MSP_MODE_FM_NICAM1:
410 case MSP_MODE_FM_NICAM2:
411 case MSP_MODE_AM_NICAM:
412 val = msp_read_dem(client, 0x23);
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200413 v4l_dbg(2, msp_debug, client, "nicam sync=%d, mode=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200414 val & 1, (val & 0x1e) >> 1);
415
416 if (val & 1) {
417 /* nicam synced */
418 switch ((val & 0x1e) >> 1) {
419 case 0:
420 case 8:
421 rxsubchans = V4L2_TUNER_SUB_STEREO;
422 break;
423 case 1:
424 case 9:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200425 rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200426 break;
427 case 2:
428 case 10:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200429 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200430 break;
431 default:
432 rxsubchans = V4L2_TUNER_SUB_MONO;
433 break;
434 }
435 newnicam = 1;
436 } else {
437 newnicam = 0;
438 rxsubchans = V4L2_TUNER_SUB_MONO;
439 }
440 break;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200441 }
442 if (rxsubchans != state->rxsubchans) {
443 update = 1;
Hans Verkuild52c7382007-12-12 08:25:18 -0300444 v4l_dbg(1, msp_debug, client,
445 "watch: rxsubchans %02x => %02x\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200446 state->rxsubchans, rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200447 state->rxsubchans = rxsubchans;
448 }
449 if (newnicam != state->nicam_on) {
450 update = 1;
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200451 v4l_dbg(1, msp_debug, client, "watch: nicam %d => %d\n",
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200452 state->nicam_on, newnicam);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200453 state->nicam_on = newnicam;
454 }
455 return update;
456}
457
458/*
459 * A kernel thread for msp3400 control -- we don't want to block the
460 * in the ioctl while doing the sound carrier & stereo detect
461 */
462/* stereo/multilang monitoring */
463static void watch_stereo(struct i2c_client *client)
464{
Hans Verkuil76efd622008-11-24 18:16:46 -0300465 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200466
Hans Verkuild52c7382007-12-12 08:25:18 -0300467 if (msp_detect_stereo(client))
Hans Verkuilde533cc2006-03-19 08:21:56 -0300468 msp_set_audmode(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200469
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200470 if (msp_once)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200471 state->watch_stereo = 0;
472}
473
474int msp3400c_thread(void *data)
475{
476 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300477 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200478 struct msp3400c_carrier_detect *cd;
Hans Verkuild52c7382007-12-12 08:25:18 -0300479 int count, max1, max2, val1, val2, val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200480
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200481 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700482 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200483 for (;;) {
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200484 v4l_dbg(2, msp_debug, client, "msp3400 thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200485 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200486 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200487
Hans Verkuild52c7382007-12-12 08:25:18 -0300488restart:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200489 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200490 state->restart = 0;
491 if (kthread_should_stop())
492 break;
493
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200494 if (state->radio || MSP_MODE_EXTERN == state->mode) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200495 /* no carrier scan, just unmute */
Hans Verkuild52c7382007-12-12 08:25:18 -0300496 v4l_dbg(1, msp_debug, client,
497 "thread: no carrier scan\n");
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300498 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300499 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200500 continue;
501 }
502
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300503 /* mute audio */
504 state->scan_in_progress = 1;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300505 msp_update_volume(state);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300506
Hans Verkuilde533cc2006-03-19 08:21:56 -0300507 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200508 val1 = val2 = 0;
509 max1 = max2 = -1;
510 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200511 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200512
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300513 /* wait for tuner to settle down after a channel change */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200514 if (msp_sleep(state, 200))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200515 goto restart;
516
517 /* carrier detect pass #1 -- main carrier */
518 cd = msp3400c_carrier_detect_main;
519 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
520
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200521 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200522 /* autodetect doesn't work well with AM ... */
523 max1 = 3;
524 count = 0;
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200525 v4l_dbg(1, msp_debug, client, "AM sound override\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200526 }
527
Hans Verkuild52c7382007-12-12 08:25:18 -0300528 for (i = 0; i < count; i++) {
529 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
530 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200531 goto restart;
532 val = msp_read_dsp(client, 0x1b);
533 if (val > 32767)
534 val -= 65536;
535 if (val1 < val)
Hans Verkuild52c7382007-12-12 08:25:18 -0300536 val1 = val, max1 = i;
537 v4l_dbg(1, msp_debug, client,
538 "carrier1 val: %5d / %s\n", val, cd[i].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200539 }
540
541 /* carrier detect pass #2 -- second (stereo) carrier */
542 switch (max1) {
543 case 1: /* 5.5 */
544 cd = msp3400c_carrier_detect_55;
545 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
546 break;
547 case 3: /* 6.5 */
548 cd = msp3400c_carrier_detect_65;
549 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
550 break;
551 case 0: /* 4.5 */
552 case 2: /* 6.0 */
553 default:
554 cd = NULL;
555 count = 0;
556 break;
557 }
558
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200559 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200560 /* autodetect doesn't work well with AM ... */
561 cd = NULL;
562 count = 0;
563 max2 = 0;
564 }
Hans Verkuild52c7382007-12-12 08:25:18 -0300565 for (i = 0; i < count; i++) {
566 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
567 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200568 goto restart;
569 val = msp_read_dsp(client, 0x1b);
570 if (val > 32767)
571 val -= 65536;
572 if (val2 < val)
Hans Verkuild52c7382007-12-12 08:25:18 -0300573 val2 = val, max2 = i;
574 v4l_dbg(1, msp_debug, client,
575 "carrier2 val: %5d / %s\n", val, cd[i].name);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200576 }
577
578 /* program the msp3400 according to the results */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200579 state->main = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200580 switch (max1) {
581 case 1: /* 5.5 */
582 if (max2 == 0) {
583 /* B/G FM-stereo */
584 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200585 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200586 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200587 } else if (max2 == 1 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200588 /* B/G NICAM */
589 state->second = msp3400c_carrier_detect_55[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200590 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200591 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200592 state->watch_stereo = 1;
593 } else {
594 goto no_second;
595 }
596 break;
597 case 2: /* 6.0 */
598 /* PAL I NICAM */
599 state->second = MSP_CARRIER(6.552);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200600 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200601 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200602 state->watch_stereo = 1;
603 break;
604 case 3: /* 6.5 */
605 if (max2 == 1 || max2 == 2) {
606 /* D/K FM-stereo */
607 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200608 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200609 state->watch_stereo = 1;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200610 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200611 /* L NICAM or AM-mono */
612 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200613 msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200614 state->watch_stereo = 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200615 } else if (max2 == 0 && state->has_nicam) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200616 /* D/K NICAM */
617 state->second = msp3400c_carrier_detect_65[max2].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200618 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200619 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200620 state->watch_stereo = 1;
621 } else {
622 goto no_second;
623 }
624 break;
625 case 0: /* 4.5 */
626 default:
Hans Verkuild52c7382007-12-12 08:25:18 -0300627no_second:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200628 state->second = msp3400c_carrier_detect_main[max1].cdo;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200629 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200630 break;
631 }
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300632 msp3400c_set_carrier(client, state->second, state->main);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200633
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300634 /* unmute */
635 state->scan_in_progress = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200636 msp3400c_set_audmode(client);
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300637 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200638
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200639 if (msp_debug)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200640 msp3400c_print_mode(client);
641
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200642 /* monitor tv audio mode, the first time don't wait
643 so long to get a quick stereo/bilingual result */
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300644 count = 3;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200645 while (state->watch_stereo) {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300646 if (msp_sleep(state, count ? 1000 : 5000))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200647 goto restart;
Hans Verkuild52c7382007-12-12 08:25:18 -0300648 if (count)
649 count--;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300650 watch_stereo(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200651 }
652 }
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200653 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200654 return 0;
655}
656
657
658int msp3410d_thread(void *data)
659{
660 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300661 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300662 int val, i, std, count;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200663
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200664 v4l_dbg(1, msp_debug, client, "msp3410 daemon started\n");
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700665 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200666 for (;;) {
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200667 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
Hans Verkuild52c7382007-12-12 08:25:18 -0300668 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200669 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200670
Hans Verkuild52c7382007-12-12 08:25:18 -0300671restart:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200672 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200673 state->restart = 0;
674 if (kthread_should_stop())
675 break;
676
677 if (state->mode == MSP_MODE_EXTERN) {
678 /* no carrier scan needed, just unmute */
Hans Verkuild52c7382007-12-12 08:25:18 -0300679 v4l_dbg(1, msp_debug, client,
680 "thread: no carrier scan\n");
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300681 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300682 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200683 continue;
684 }
685
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300686 /* mute audio */
687 state->scan_in_progress = 1;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300688 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200689
Hans Verkuilde533cc2006-03-19 08:21:56 -0300690 /* start autodetect. Note: autodetect is not supported for
Hans Verkuild52c7382007-12-12 08:25:18 -0300691 NTSC-M and radio, hence we force the standard in those
692 cases. */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200693 if (state->radio)
694 std = 0x40;
Hans Verkuild312a462006-01-09 18:21:36 -0200695 else
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200696 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200697 state->watch_stereo = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200698 state->nicam_on = 0;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200699
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300700 /* wait for tuner to settle down after a channel change */
701 if (msp_sleep(state, 200))
702 goto restart;
703
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200704 if (msp_debug)
Hans Verkuild52c7382007-12-12 08:25:18 -0300705 v4l_dbg(2, msp_debug, client,
706 "setting standard: %s (0x%04x)\n",
707 msp_standard_std_name(std), std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200708
709 if (std != 1) {
710 /* programmed some specific mode */
711 val = std;
712 } else {
713 /* triggered autodetect */
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200714 msp_write_dem(client, 0x20, std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200715 for (;;) {
Hans Verkuild312a462006-01-09 18:21:36 -0200716 if (msp_sleep(state, 100))
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200717 goto restart;
718
719 /* check results */
720 val = msp_read_dem(client, 0x7e);
721 if (val < 0x07ff)
722 break;
Hans Verkuild52c7382007-12-12 08:25:18 -0300723 v4l_dbg(2, msp_debug, client,
724 "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200725 }
726 }
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200727 for (i = 0; msp_stdlist[i].name != NULL; i++)
728 if (msp_stdlist[i].retval == val)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200729 break;
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200730 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200731 msp_standard_std_name(val), val);
732 state->main = msp_stdlist[i].main;
733 state->second = msp_stdlist[i].second;
734 state->std = val;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300735 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200736
Hans Verkuild52c7382007-12-12 08:25:18 -0300737 if (msp_amsound && !state->radio &&
738 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200739 /* autodetection has failed, let backup */
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200740 v4l_dbg(1, msp_debug, client, "autodetection failed,"
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200741 " switching to backup standard: %s (0x%04x)\n",
Hans Verkuild52c7382007-12-12 08:25:18 -0300742 msp_stdlist[8].name ?
743 msp_stdlist[8].name : "unknown", val);
Hans Verkuilde533cc2006-03-19 08:21:56 -0300744 state->std = val = 0x0009;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200745 msp_write_dem(client, 0x20, val);
746 }
747
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200748 /* set stereo */
749 switch (val) {
750 case 0x0008: /* B/G NICAM */
751 case 0x000a: /* I NICAM */
Hans Verkuilde533cc2006-03-19 08:21:56 -0300752 case 0x000b: /* D/K NICAM */
753 if (val == 0x000a)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200754 state->mode = MSP_MODE_FM_NICAM2;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300755 else
756 state->mode = MSP_MODE_FM_NICAM1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200757 /* just turn on stereo */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200758 state->nicam_on = 1;
759 state->watch_stereo = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200760 break;
761 case 0x0009:
762 state->mode = MSP_MODE_AM_NICAM;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200763 state->nicam_on = 1;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200764 state->watch_stereo = 1;
765 break;
766 case 0x0020: /* BTSC */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200767 /* The pre-'G' models only have BTSC-mono */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200768 state->mode = MSP_MODE_BTSC;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200769 break;
770 case 0x0040: /* FM radio */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200771 state->mode = MSP_MODE_FM_RADIO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200772 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200773 /* not needed in theory if we have radio, but
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200774 short programming enables carrier mute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200775 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
776 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200777 MSP_CARRIER(10.7));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200778 break;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300779 case 0x0002:
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200780 case 0x0003:
781 case 0x0004:
782 case 0x0005:
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200783 state->mode = MSP_MODE_FM_TERRA;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200784 state->watch_stereo = 1;
785 break;
786 }
787
Hans Verkuilde533cc2006-03-19 08:21:56 -0300788 /* set various prescales */
789 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
790 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
791 if (state->has_nicam)
792 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
793
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200794 if (state->has_i2s_conf)
795 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200796
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300797 /* unmute */
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200798 msp3400c_set_audmode(client);
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300799 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300800 msp_update_volume(state);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200801
802 /* monitor tv audio mode, the first time don't wait
803 so long to get a quick stereo/bilingual result */
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300804 count = 3;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200805 while (state->watch_stereo) {
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300806 if (msp_sleep(state, count ? 1000 : 5000))
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200807 goto restart;
Hans Verkuild52c7382007-12-12 08:25:18 -0300808 if (count)
809 count--;
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300810 watch_stereo(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200811 }
812 }
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200813 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200814 return 0;
815}
816
817/* ----------------------------------------------------------------------- */
818
Hans Verkuilde533cc2006-03-19 08:21:56 -0300819/* msp34xxG + (autoselect no-thread)
820 * this one uses both automatic standard detection and automatic sound
821 * select which are available in the newer G versions
822 * struct msp: only norm, acb and source are really used in this mode
823 */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200824
Hans Verkuil9a80a932006-03-19 07:25:42 -0300825static int msp34xxg_modus(struct i2c_client *client)
826{
Hans Verkuil76efd622008-11-24 18:16:46 -0300827 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil9a80a932006-03-19 07:25:42 -0300828
829 if (state->radio) {
830 v4l_dbg(1, msp_debug, client, "selected radio modus\n");
831 return 0x0001;
832 }
Hans Verkuil9a80a932006-03-19 07:25:42 -0300833 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
834 v4l_dbg(1, msp_debug, client, "selected M (EIA-J) modus\n");
835 return 0x4001;
836 }
837 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
838 v4l_dbg(1, msp_debug, client, "selected M (A2) modus\n");
839 return 0x0001;
840 }
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300841 if (state->v4l2_std == V4L2_STD_SECAM_L) {
842 v4l_dbg(1, msp_debug, client, "selected SECAM-L modus\n");
843 return 0x6001;
844 }
Hans Verkuil9a80a932006-03-19 07:25:42 -0300845 if (state->v4l2_std & V4L2_STD_MN) {
846 v4l_dbg(1, msp_debug, client, "selected M (BTSC) modus\n");
847 return 0x2001;
848 }
Hans Verkuil2fd3c142008-04-22 14:45:51 -0300849 return 0x7001;
Hans Verkuil9a80a932006-03-19 07:25:42 -0300850}
851
Hans Verkuil2474ed42006-03-19 12:35:57 -0300852static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
853 {
Hans Verkuil76efd622008-11-24 18:16:46 -0300854 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil2474ed42006-03-19 12:35:57 -0300855 int source, matrix;
856
857 switch (state->audmode) {
858 case V4L2_TUNER_MODE_MONO:
859 source = 0; /* mono only */
860 matrix = 0x30;
861 break;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300862 case V4L2_TUNER_MODE_LANG2:
863 source = 4; /* stereo or B */
864 matrix = 0x10;
865 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -0300866 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil2474ed42006-03-19 12:35:57 -0300867 source = 1; /* stereo or A|B */
868 matrix = 0x20;
869 break;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300870 case V4L2_TUNER_MODE_LANG1:
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300871 source = 3; /* stereo or A */
872 matrix = 0x00;
873 break;
Hans Verkuilb331def2006-12-18 13:18:28 -0300874 case V4L2_TUNER_MODE_STEREO:
875 default:
876 source = 3; /* stereo or A */
877 matrix = 0x20;
878 break;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300879 }
880
Hans Verkuil07151722006-04-01 18:03:23 -0300881 if (in == MSP_DSP_IN_TUNER)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300882 source = (source << 8) | 0x20;
883 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
884 instead of 11, 12, 13. So we add one for that msp version. */
Hans Verkuil07151722006-04-01 18:03:23 -0300885 else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300886 source = ((in + 1) << 8) | matrix;
887 else
888 source = (in << 8) | matrix;
889
Hans Verkuild52c7382007-12-12 08:25:18 -0300890 v4l_dbg(1, msp_debug, client,
891 "set source to %d (0x%x) for output %02x\n", in, source, reg);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300892 msp_write_dsp(client, reg, source);
893}
894
895static void msp34xxg_set_sources(struct i2c_client *client)
896{
Hans Verkuil76efd622008-11-24 18:16:46 -0300897 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil5325b422009-04-02 11:26:22 -0300898 u32 in = state->route_in;
Hans Verkuil2474ed42006-03-19 12:35:57 -0300899
900 msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
901 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
902 msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
903 msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
904 msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300905 if (state->has_scart2_out)
Hans Verkuil2474ed42006-03-19 12:35:57 -0300906 msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
907 msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
908}
909
Hans Verkuilde533cc2006-03-19 08:21:56 -0300910/* (re-)initialize the msp34xxg */
911static void msp34xxg_reset(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200912{
Hans Verkuil76efd622008-11-24 18:16:46 -0300913 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil5325b422009-04-02 11:26:22 -0300914 int tuner = (state->route_in >> 3) & 1;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300915 int modus;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200916
Hans Verkuilde533cc2006-03-19 08:21:56 -0300917 /* initialize std to 1 (autodetect) to signal that no standard is
918 selected yet. */
919 state->std = 1;
920
921 msp_reset(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200922
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200923 if (state->has_i2s_conf)
924 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200925
926 /* step-by-step initialisation, as described in the manual */
Hans Verkuil9a80a932006-03-19 07:25:42 -0300927 modus = msp34xxg_modus(client);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300928 modus |= tuner ? 0x100 : 0;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300929 msp_write_dem(client, 0x30, modus);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200930
931 /* write the dsps that may have an influence on
932 standard/audio autodetection right now */
Hans Verkuil2474ed42006-03-19 12:35:57 -0300933 msp34xxg_set_sources(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200934
Hans Verkuilde533cc2006-03-19 08:21:56 -0300935 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
936 msp_write_dsp(client, 0x0e, 0x3000); /* FM */
937 if (state->has_nicam)
938 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200939
Hans Verkuilde533cc2006-03-19 08:21:56 -0300940 /* set identification threshold. Personally, I
941 * I set it to a higher value than the default
942 * of 0x190 to ignore noisy stereo signals.
943 * this needs tuning. (recommended range 0x00a0-0x03c0)
944 * 0x7f0 = forced mono mode
945 *
946 * a2 threshold for stereo/bilingual.
947 * Note: this register is part of the Manual/Compatibility mode.
948 * It is supported by all 'G'-family chips.
949 */
950 msp_write_dem(client, 0x22, msp_stereo_thresh);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200951}
952
953int msp34xxg_thread(void *data)
954{
955 struct i2c_client *client = data;
Hans Verkuil76efd622008-11-24 18:16:46 -0300956 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilde533cc2006-03-19 08:21:56 -0300957 int val, i;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200958
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200959 v4l_dbg(1, msp_debug, client, "msp34xxg daemon started\n");
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700960 set_freezable();
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200961 for (;;) {
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200962 v4l_dbg(2, msp_debug, client, "msp34xxg thread: sleep\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200963 msp_sleep(state, -1);
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200964 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200965
Hans Verkuild52c7382007-12-12 08:25:18 -0300966restart:
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -0200967 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200968 state->restart = 0;
969 if (kthread_should_stop())
970 break;
971
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300972 if (state->mode == MSP_MODE_EXTERN) {
973 /* no carrier scan needed, just unmute */
Hans Verkuild52c7382007-12-12 08:25:18 -0300974 v4l_dbg(1, msp_debug, client,
975 "thread: no carrier scan\n");
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300976 state->scan_in_progress = 0;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300977 msp_update_volume(state);
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300978 continue;
979 }
980
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200981 /* setup the chip*/
982 msp34xxg_reset(client);
Hans Verkuilde98cda2006-10-01 17:56:32 -0300983 state->std = state->radio ? 0x40 :
984 (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
Hans Verkuilde533cc2006-03-19 08:21:56 -0300985 msp_write_dem(client, 0x20, state->std);
Hans Verkuilde98cda2006-10-01 17:56:32 -0300986 /* start autodetect */
Hans Verkuile335fad2006-08-08 09:10:15 -0300987 if (state->std != 1)
988 goto unmute;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200989
990 /* watch autodetect */
Hans Verkuild52c7382007-12-12 08:25:18 -0300991 v4l_dbg(1, msp_debug, client,
992 "started autodetect, waiting for result\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -0200993 for (i = 0; i < 10; i++) {
994 if (msp_sleep(state, 100))
995 goto restart;
996
997 /* check results */
998 val = msp_read_dem(client, 0x7e);
999 if (val < 0x07ff) {
Hans Verkuilde533cc2006-03-19 08:21:56 -03001000 state->std = val;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001001 break;
1002 }
Hans Verkuild52c7382007-12-12 08:25:18 -03001003 v4l_dbg(2, msp_debug, client,
1004 "detection still in progress\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001005 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001006 if (state->std == 1) {
Hans Verkuild52c7382007-12-12 08:25:18 -03001007 v4l_dbg(1, msp_debug, client,
1008 "detection still in progress after 10 tries. giving up.\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001009 continue;
1010 }
1011
Hans Verkuild52c7382007-12-12 08:25:18 -03001012unmute:
1013 v4l_dbg(1, msp_debug, client,
1014 "detected standard: %s (0x%04x)\n",
Hans Verkuilde533cc2006-03-19 08:21:56 -03001015 msp_standard_std_name(state->std), state->std);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001016
Hans Verkuil2eb606d2006-04-02 08:21:02 -03001017 if (state->std == 9) {
1018 /* AM NICAM mode */
1019 msp_write_dsp(client, 0x0e, 0x7c00);
1020 }
1021
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001022 /* unmute: dispatch sound to scart output, set scart volume */
Hans Verkuilebc3bba2010-05-24 10:01:58 -03001023 msp_update_volume(state);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001024
1025 /* restore ACB */
1026 if (msp_write_dsp(client, 0x13, state->acb))
1027 return -1;
1028
Hans Verkuilde533cc2006-03-19 08:21:56 -03001029 /* the periodic stereo/SAP check is only relevant for
1030 the 0x20 standard (BTSC) */
1031 if (state->std != 0x20)
1032 continue;
1033
1034 state->watch_stereo = 1;
1035
1036 /* monitor tv audio mode, the first time don't wait
1037 in order to get a quick stereo/SAP update */
1038 watch_stereo(client);
1039 while (state->watch_stereo) {
1040 watch_stereo(client);
1041 if (msp_sleep(state, 5000))
1042 goto restart;
1043 }
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001044 }
Mauro Carvalho Chehabf167cb42006-01-11 19:41:49 -02001045 v4l_dbg(1, msp_debug, client, "thread: exit\n");
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001046 return 0;
1047}
1048
Hans Verkuilde533cc2006-03-19 08:21:56 -03001049static int msp34xxg_detect_stereo(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001050{
Hans Verkuil76efd622008-11-24 18:16:46 -03001051 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001052 int status = msp_read_dem(client, 0x0200);
1053 int is_bilingual = status & 0x100;
1054 int is_stereo = status & 0x40;
Hans Verkuilde533cc2006-03-19 08:21:56 -03001055 int oldrx = state->rxsubchans;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001056
Hans Verkuilb331def2006-12-18 13:18:28 -03001057 if (state->mode == MSP_MODE_EXTERN)
1058 return 0;
1059
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001060 state->rxsubchans = 0;
1061 if (is_stereo)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001062 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001063 else
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001064 state->rxsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001065 if (is_bilingual) {
Hans Verkuilde533cc2006-03-19 08:21:56 -03001066 if (state->std == 0x20)
1067 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1068 else
Hans Verkuild52c7382007-12-12 08:25:18 -03001069 state->rxsubchans =
1070 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001071 }
Hans Verkuild52c7382007-12-12 08:25:18 -03001072 v4l_dbg(1, msp_debug, client,
1073 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001074 status, is_stereo, is_bilingual, state->rxsubchans);
Hans Verkuilde533cc2006-03-19 08:21:56 -03001075 return (oldrx != state->rxsubchans);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001076}
1077
Adrian Bunk044f3242006-03-08 10:50:07 -03001078static void msp34xxg_set_audmode(struct i2c_client *client)
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001079{
Hans Verkuil76efd622008-11-24 18:16:46 -03001080 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001081
Hans Verkuilde533cc2006-03-19 08:21:56 -03001082 if (state->std == 0x20) {
1083 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
Mauro Carvalho Chehabcc336682006-03-29 16:29:00 -03001084 (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
Hans Verkuilde533cc2006-03-19 08:21:56 -03001085 state->audmode == V4L2_TUNER_MODE_LANG2)) {
1086 msp_write_dem(client, 0x20, 0x21);
1087 } else {
1088 msp_write_dem(client, 0x20, 0x20);
1089 }
1090 }
1091
Hans Verkuil2474ed42006-03-19 12:35:57 -03001092 msp34xxg_set_sources(client);
Hans Verkuil7e8b09e2006-01-09 15:32:40 -02001093}
1094
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001095void msp_set_audmode(struct i2c_client *client)
1096{
Hans Verkuil76efd622008-11-24 18:16:46 -03001097 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001098
1099 switch (state->opmode) {
1100 case OPMODE_MANUAL:
1101 case OPMODE_AUTODETECT:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001102 msp3400c_set_audmode(client);
1103 break;
1104 case OPMODE_AUTOSELECT:
1105 msp34xxg_set_audmode(client);
1106 break;
1107 }
1108}
1109
Hans Verkuilde533cc2006-03-19 08:21:56 -03001110int msp_detect_stereo(struct i2c_client *client)
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001111{
Hans Verkuil76efd622008-11-24 18:16:46 -03001112 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001113
1114 switch (state->opmode) {
1115 case OPMODE_MANUAL:
1116 case OPMODE_AUTODETECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001117 return msp3400c_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001118 case OPMODE_AUTOSELECT:
Hans Verkuilde533cc2006-03-19 08:21:56 -03001119 return msp34xxg_detect_stereo(client);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001120 }
Hans Verkuilde533cc2006-03-19 08:21:56 -03001121 return 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001122}
1123