]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * programming the msp34* sound processor family | |
3 | * | |
4 | * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org> | |
5 | * | |
6 | * what works and what doesn't: | |
7 | * | |
8 | * AM-Mono | |
9 | * Support for Hauppauge cards added (decoding handled by tuner) added by | |
10 | * Frederic Crozat <fcrozat@mail.dotcom.fr> | |
11 | * | |
12 | * FM-Mono | |
13 | * should work. The stereo modes are backward compatible to FM-mono, | |
14 | * therefore FM-Mono should be allways available. | |
15 | * | |
16 | * FM-Stereo (B/G, used in germany) | |
17 | * should work, with autodetect | |
18 | * | |
19 | * FM-Stereo (satellite) | |
20 | * should work, no autodetect (i.e. default is mono, but you can | |
21 | * switch to stereo -- untested) | |
22 | * | |
23 | * NICAM (B/G, L , used in UK, Scandinavia, Spain and France) | |
24 | * should work, with autodetect. Support for NICAM was added by | |
25 | * Pekka Pietikainen <pp@netppl.fi> | |
26 | * | |
27 | * | |
28 | * TODO: | |
29 | * - better SAT support | |
30 | * | |
31 | * | |
32 | * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch) | |
33 | * using soundcore instead of OSS | |
34 | * | |
35 | */ | |
36 | ||
37 | #include <linux/config.h> | |
38 | #include <linux/module.h> | |
39 | #include <linux/moduleparam.h> | |
40 | #include <linux/kernel.h> | |
41 | #include <linux/sched.h> | |
42 | #include <linux/string.h> | |
43 | #include <linux/timer.h> | |
44 | #include <linux/delay.h> | |
45 | #include <linux/errno.h> | |
46 | #include <linux/slab.h> | |
47 | #include <linux/i2c.h> | |
48 | #include <linux/videodev.h> | |
49 | #include <linux/init.h> | |
50 | #include <linux/smp_lock.h> | |
51 | #include <linux/kthread.h> | |
52 | #include <linux/suspend.h> | |
53 | #include <asm/semaphore.h> | |
54 | #include <asm/pgtable.h> | |
55 | ||
56 | #include <media/audiochip.h> | |
57 | #include "msp3400.h" | |
58 | ||
59 | #define msp3400_dbg(fmt, arg...) \ | |
60 | do { \ | |
61 | if (debug) \ | |
62 | printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \ | |
63 | i2c_adapter_id(client->adapter), client->addr , ## arg); \ | |
64 | } while (0) | |
65 | ||
66 | /* Medium volume debug. */ | |
67 | #define msp3400_dbg_mediumvol(fmt, arg...) \ | |
68 | do { \ | |
69 | if (debug >= 2) \ | |
70 | printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \ | |
71 | i2c_adapter_id(client->adapter), client->addr , ## arg); \ | |
72 | } while (0) | |
73 | ||
74 | /* High volume debug. Use with care. */ | |
75 | #define msp3400_dbg_highvol(fmt, arg...) \ | |
76 | do { \ | |
77 | if (debug >= 16) \ | |
78 | printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \ | |
79 | i2c_adapter_id(client->adapter), client->addr , ## arg); \ | |
80 | } while (0) | |
81 | ||
82 | #define msp3400_err(fmt, arg...) do { \ | |
83 | printk(KERN_ERR "%s %d-%04x: " fmt, client->driver->name, \ | |
84 | i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) | |
85 | #define msp3400_warn(fmt, arg...) do { \ | |
86 | printk(KERN_WARNING "%s %d-%04x: " fmt, client->driver->name, \ | |
87 | i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) | |
88 | #define msp3400_info(fmt, arg...) do { \ | |
89 | printk(KERN_INFO "%s %d-%04x: " fmt, client->driver->name, \ | |
90 | i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) | |
91 | ||
92 | #define OPMODE_AUTO -1 | |
93 | #define OPMODE_MANUAL 0 | |
94 | #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */ | |
95 | #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */ | |
96 | ||
97 | /* insmod parameters */ | |
98 | static int opmode = OPMODE_AUTO; | |
99 | static int debug = 0; /* debug output */ | |
100 | static int once = 0; /* no continous stereo monitoring */ | |
101 | static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france), | |
102 | the autoscan seems work well only with FM... */ | |
103 | static int standard = 1; /* Override auto detect of audio standard, if needed. */ | |
104 | static int dolby = 0; | |
105 | ||
106 | static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual | |
107 | (msp34xxg only) 0x00a0-0x03c0 */ | |
108 | #define DFP_COUNT 0x41 | |
109 | static const int bl_dfp[] = { | |
110 | 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a, | |
111 | 0x0b, 0x0d, 0x0e, 0x10 | |
112 | }; | |
113 | ||
114 | #define IS_MSP34XX_G(msp) ((msp)->opmode==2) | |
115 | ||
116 | struct msp3400c { | |
117 | int rev1,rev2; | |
118 | ||
119 | int opmode; | |
120 | int nicam; | |
121 | int mode; | |
122 | int norm; | |
123 | int stereo; | |
124 | int nicam_on; | |
125 | int acb; | |
126 | int in_scart; | |
127 | int i2s_mode; | |
128 | int main, second; /* sound carrier */ | |
129 | int input; | |
130 | int source; /* see msp34xxg_set_source */ | |
131 | ||
132 | /* v4l2 */ | |
133 | int audmode; | |
134 | int rxsubchans; | |
135 | ||
136 | int muted; | |
137 | int left, right; /* volume */ | |
138 | int bass, treble; | |
139 | ||
140 | /* shadow register set */ | |
141 | int dfp_regs[DFP_COUNT]; | |
142 | ||
143 | /* thread */ | |
144 | struct task_struct *kthread; | |
145 | wait_queue_head_t wq; | |
146 | int restart:1; | |
147 | int watch_stereo:1; | |
148 | }; | |
149 | ||
150 | #define MIN(a,b) (((a)>(b))?(b):(a)) | |
151 | #define MAX(a,b) (((a)>(b))?(a):(b)) | |
152 | #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00) | |
153 | #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@') | |
154 | #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@') | |
155 | #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@') | |
156 | ||
157 | #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */ | |
158 | ||
159 | /* ---------------------------------------------------------------------- */ | |
160 | ||
161 | /* read-only */ | |
162 | module_param(opmode, int, 0444); | |
163 | ||
164 | /* read-write */ | |
165 | module_param(once, int, 0644); | |
166 | module_param(debug, int, 0644); | |
167 | module_param(stereo_threshold, int, 0644); | |
168 | module_param(standard, int, 0644); | |
169 | module_param(amsound, int, 0644); | |
170 | module_param(dolby, int, 0644); | |
171 | ||
172 | MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler"); | |
173 | MODULE_PARM_DESC(once, "No continuous stereo monitoring"); | |
174 | MODULE_PARM_DESC(debug, "Enable debug messages"); | |
175 | MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo"); | |
176 | MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect"); | |
177 | MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan"); | |
178 | MODULE_PARM_DESC(dolby, "Activates Dolby processsing"); | |
179 | ||
180 | /* ---------------------------------------------------------------------- */ | |
181 | ||
182 | #define I2C_MSP3400C 0x80 | |
183 | #define I2C_MSP3400C_ALT 0x88 | |
184 | ||
185 | #define I2C_MSP3400C_DEM 0x10 | |
186 | #define I2C_MSP3400C_DFP 0x12 | |
187 | ||
188 | /* Addresses to scan */ | |
189 | static unsigned short normal_i2c[] = { | |
190 | I2C_MSP3400C >> 1, | |
191 | I2C_MSP3400C_ALT >> 1, | |
192 | I2C_CLIENT_END | |
193 | }; | |
194 | I2C_CLIENT_INSMOD; | |
195 | ||
196 | MODULE_DESCRIPTION("device driver for msp34xx TV sound processor"); | |
197 | MODULE_AUTHOR("Gerd Knorr"); | |
198 | MODULE_LICENSE("GPL"); | |
199 | ||
200 | /* ----------------------------------------------------------------------- */ | |
201 | /* functions for talking to the MSP3400C Sound processor */ | |
202 | ||
203 | static int msp3400c_reset(struct i2c_client *client) | |
204 | { | |
205 | /* reset and read revision code */ | |
206 | static char reset_off[3] = { 0x00, 0x80, 0x00 }; | |
207 | static char reset_on[3] = { 0x00, 0x00, 0x00 }; | |
208 | static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e }; | |
209 | char read[2]; | |
210 | struct i2c_msg reset[2] = { | |
211 | { client->addr, I2C_M_IGNORE_NAK, 3, reset_off }, | |
212 | { client->addr, I2C_M_IGNORE_NAK, 3, reset_on }, | |
213 | }; | |
214 | struct i2c_msg test[2] = { | |
215 | { client->addr, 0, 3, write }, | |
216 | { client->addr, I2C_M_RD, 2, read }, | |
217 | }; | |
218 | ||
219 | msp3400_dbg_highvol("msp3400c_reset\n"); | |
220 | if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) || | |
221 | (1 != i2c_transfer(client->adapter,&reset[1],1)) || | |
222 | (2 != i2c_transfer(client->adapter,test,2)) ) { | |
223 | msp3400_err("chip reset failed\n"); | |
224 | return -1; | |
225 | } | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int msp3400c_read(struct i2c_client *client, int dev, int addr) | |
230 | { | |
231 | int err,retval; | |
232 | ||
233 | unsigned char write[3]; | |
234 | unsigned char read[2]; | |
235 | struct i2c_msg msgs[2] = { | |
236 | { client->addr, 0, 3, write }, | |
237 | { client->addr, I2C_M_RD, 2, read } | |
238 | }; | |
239 | ||
240 | write[0] = dev+1; | |
241 | write[1] = addr >> 8; | |
242 | write[2] = addr & 0xff; | |
243 | ||
244 | for (err = 0; err < 3;) { | |
245 | if (2 == i2c_transfer(client->adapter,msgs,2)) | |
246 | break; | |
247 | err++; | |
248 | msp3400_warn("I/O error #%d (read 0x%02x/0x%02x)\n", err, | |
249 | dev, addr); | |
250 | current->state = TASK_INTERRUPTIBLE; | |
251 | schedule_timeout(msecs_to_jiffies(10)); | |
252 | } | |
253 | if (3 == err) { | |
254 | msp3400_warn("giving up, resetting chip. Sound will go off, sorry folks :-|\n"); | |
255 | msp3400c_reset(client); | |
256 | return -1; | |
257 | } | |
258 | retval = read[0] << 8 | read[1]; | |
259 | msp3400_dbg_highvol("msp3400c_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval); | |
260 | return retval; | |
261 | } | |
262 | ||
263 | static int msp3400c_write(struct i2c_client *client, int dev, int addr, int val) | |
264 | { | |
265 | int err; | |
266 | unsigned char buffer[5]; | |
267 | ||
268 | buffer[0] = dev; | |
269 | buffer[1] = addr >> 8; | |
270 | buffer[2] = addr & 0xff; | |
271 | buffer[3] = val >> 8; | |
272 | buffer[4] = val & 0xff; | |
273 | ||
274 | msp3400_dbg_highvol("msp3400c_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val); | |
275 | for (err = 0; err < 3;) { | |
276 | if (5 == i2c_master_send(client, buffer, 5)) | |
277 | break; | |
278 | err++; | |
279 | msp3400_warn("I/O error #%d (write 0x%02x/0x%02x)\n", err, | |
280 | dev, addr); | |
281 | current->state = TASK_INTERRUPTIBLE; | |
282 | schedule_timeout(msecs_to_jiffies(10)); | |
283 | } | |
284 | if (3 == err) { | |
285 | msp3400_warn("giving up, reseting chip. Sound will go off, sorry folks :-|\n"); | |
286 | msp3400c_reset(client); | |
287 | return -1; | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
292 | /* ------------------------------------------------------------------------ */ | |
293 | ||
294 | /* This macro is allowed for *constants* only, gcc must calculate it | |
295 | at compile time. Remember -- no floats in kernel mode */ | |
296 | #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24))) | |
297 | ||
298 | #define MSP_MODE_AM_DETECT 0 | |
299 | #define MSP_MODE_FM_RADIO 2 | |
300 | #define MSP_MODE_FM_TERRA 3 | |
301 | #define MSP_MODE_FM_SAT 4 | |
302 | #define MSP_MODE_FM_NICAM1 5 | |
303 | #define MSP_MODE_FM_NICAM2 6 | |
304 | #define MSP_MODE_AM_NICAM 7 | |
305 | #define MSP_MODE_BTSC 8 | |
306 | #define MSP_MODE_EXTERN 9 | |
307 | ||
308 | static struct MSP_INIT_DATA_DEM { | |
309 | int fir1[6]; | |
310 | int fir2[6]; | |
311 | int cdo1; | |
312 | int cdo2; | |
313 | int ad_cv; | |
314 | int mode_reg; | |
315 | int dfp_src; | |
316 | int dfp_matrix; | |
317 | } msp_init_data[] = { | |
318 | { /* AM (for carrier detect / msp3400) */ | |
319 | {75, 19, 36, 35, 39, 40}, | |
320 | {75, 19, 36, 35, 39, 40}, | |
321 | MSP_CARRIER(5.5), MSP_CARRIER(5.5), | |
322 | 0x00d0, 0x0500, 0x0020, 0x3000 | |
323 | },{ /* AM (for carrier detect / msp3410) */ | |
324 | {-1, -1, -8, 2, 59, 126}, | |
325 | {-1, -1, -8, 2, 59, 126}, | |
326 | MSP_CARRIER(5.5), MSP_CARRIER(5.5), | |
327 | 0x00d0, 0x0100, 0x0020, 0x3000 | |
328 | },{ /* FM Radio */ | |
329 | {-8, -8, 4, 6, 78, 107}, | |
330 | {-8, -8, 4, 6, 78, 107}, | |
331 | MSP_CARRIER(10.7), MSP_CARRIER(10.7), | |
332 | 0x00d0, 0x0480, 0x0020, 0x3000 | |
333 | },{ /* Terrestial FM-mono + FM-stereo */ | |
334 | {3, 18, 27, 48, 66, 72}, | |
335 | {3, 18, 27, 48, 66, 72}, | |
336 | MSP_CARRIER(5.5), MSP_CARRIER(5.5), | |
337 | 0x00d0, 0x0480, 0x0030, 0x3000 | |
338 | },{ /* Sat FM-mono */ | |
339 | { 1, 9, 14, 24, 33, 37}, | |
340 | { 3, 18, 27, 48, 66, 72}, | |
341 | MSP_CARRIER(6.5), MSP_CARRIER(6.5), | |
342 | 0x00c6, 0x0480, 0x0000, 0x3000 | |
343 | },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */ | |
344 | {-2, -8, -10, 10, 50, 86}, | |
345 | {3, 18, 27, 48, 66, 72}, | |
346 | MSP_CARRIER(5.5), MSP_CARRIER(5.5), | |
347 | 0x00d0, 0x0040, 0x0120, 0x3000 | |
348 | },{ /* NICAM/FM -- I (6.0/6.552) */ | |
349 | {2, 4, -6, -4, 40, 94}, | |
350 | {3, 18, 27, 48, 66, 72}, | |
351 | MSP_CARRIER(6.0), MSP_CARRIER(6.0), | |
352 | 0x00d0, 0x0040, 0x0120, 0x3000 | |
353 | },{ /* NICAM/AM -- L (6.5/5.85) */ | |
354 | {-2, -8, -10, 10, 50, 86}, | |
355 | {-4, -12, -9, 23, 79, 126}, | |
356 | MSP_CARRIER(6.5), MSP_CARRIER(6.5), | |
357 | 0x00c6, 0x0140, 0x0120, 0x7c03 | |
358 | }, | |
359 | }; | |
360 | ||
361 | struct CARRIER_DETECT { | |
362 | int cdo; | |
363 | char *name; | |
364 | }; | |
365 | ||
366 | static struct CARRIER_DETECT carrier_detect_main[] = { | |
367 | /* main carrier */ | |
368 | { MSP_CARRIER(4.5), "4.5 NTSC" }, | |
369 | { MSP_CARRIER(5.5), "5.5 PAL B/G" }, | |
370 | { MSP_CARRIER(6.0), "6.0 PAL I" }, | |
371 | { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" } | |
372 | }; | |
373 | ||
374 | static struct CARRIER_DETECT carrier_detect_55[] = { | |
375 | /* PAL B/G */ | |
376 | { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" }, | |
377 | { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" } | |
378 | }; | |
379 | ||
380 | static struct CARRIER_DETECT carrier_detect_65[] = { | |
381 | /* PAL SAT / SECAM */ | |
382 | { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" }, | |
383 | { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" }, | |
384 | { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" }, | |
385 | { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" }, | |
386 | { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" }, | |
387 | { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" }, | |
388 | }; | |
389 | ||
390 | #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT)) | |
391 | ||
392 | /* ----------------------------------------------------------------------- * | |
393 | * bits 9 8 5 - SCART DSP input Select: | |
394 | * 0 0 0 - SCART 1 to DSP input (reset position) | |
395 | * 0 1 0 - MONO to DSP input | |
396 | * 1 0 0 - SCART 2 to DSP input | |
397 | * 1 1 1 - Mute DSP input | |
398 | * | |
399 | * bits 11 10 6 - SCART 1 Output Select: | |
400 | * 0 0 0 - undefined (reset position) | |
401 | * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS) | |
402 | * 1 0 0 - MONO input to SCART 1 Output | |
403 | * 1 1 0 - SCART 1 DA to SCART 1 Output | |
404 | * 0 0 1 - SCART 2 DA to SCART 1 Output | |
405 | * 0 1 1 - SCART 1 Input to SCART 1 Output | |
406 | * 1 1 1 - Mute SCART 1 Output | |
407 | * | |
408 | * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART): | |
409 | * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position) | |
410 | * 0 1 0 - SCART 1 Input to SCART 2 Output | |
411 | * 1 0 0 - MONO input to SCART 2 Output | |
412 | * 0 0 1 - SCART 2 DA to SCART 2 Output | |
413 | * 0 1 1 - SCART 2 Input to SCART 2 Output | |
414 | * 1 1 0 - Mute SCART 2 Output | |
415 | * | |
416 | * Bits 4 to 0 should be zero. | |
417 | * ----------------------------------------------------------------------- */ | |
418 | ||
419 | static int scarts[3][9] = { | |
420 | /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */ | |
421 | /* SCART DSP Input select */ | |
422 | { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 }, | |
423 | /* SCART1 Output select */ | |
424 | { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 }, | |
425 | /* SCART2 Output select */ | |
426 | { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 }, | |
427 | }; | |
428 | ||
429 | static char *scart_names[] = { | |
430 | "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute" | |
431 | }; | |
432 | ||
433 | static void msp3400c_set_scart(struct i2c_client *client, int in, int out) | |
434 | { | |
435 | struct msp3400c *msp = i2c_get_clientdata(client); | |
436 | ||
437 | msp->in_scart=in; | |
438 | ||
439 | if (in >= 1 && in <= 8 && out >= 0 && out <= 2) { | |
440 | if (-1 == scarts[out][in]) | |
441 | return; | |
442 | ||
443 | msp->acb &= ~scarts[out][SCART_MASK]; | |
444 | msp->acb |= scarts[out][in]; | |
445 | } else | |
446 | msp->acb = 0xf60; /* Mute Input and SCART 1 Output */ | |
447 | ||
448 | msp3400_dbg("scart switch: %s => %d (ACB=0x%04x)\n", | |
449 | scart_names[in], out, msp->acb); | |
450 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x13, msp->acb); | |
451 | ||
452 | /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */ | |
453 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); | |
454 | } | |
455 | ||
456 | /* ------------------------------------------------------------------------ */ | |
457 | ||
458 | static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2) | |
459 | { | |
460 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff); | |
461 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12); | |
462 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff); | |
463 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12); | |
464 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/ | |
465 | } | |
466 | ||
467 | static void msp3400c_setvolume(struct i2c_client *client, | |
468 | int muted, int left, int right) | |
469 | { | |
470 | int vol = 0, val = 0, balance = 0; | |
471 | ||
472 | if (!muted) { | |
473 | /* 0x7f instead if 0x73 here has sound quality issues, | |
474 | * probably due to overmodulation + clipping ... */ | |
475 | vol = (left > right) ? left : right; | |
476 | val = (vol * 0x73 / 65535) << 8; | |
477 | } | |
478 | if (vol > 0) { | |
479 | balance = ((right - left) * 127) / vol; | |
480 | } | |
481 | ||
482 | msp3400_dbg("setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n", | |
483 | muted ? "on" : "off", left, right, val >> 8, balance); | |
484 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */ | |
485 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */ | |
486 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, | |
487 | muted ? 0x1 : (val | 0x1)); | |
488 | msp3400c_write(client, I2C_MSP3400C_DFP, 0x0001, balance << 8); | |
489 | } | |
490 | ||
491 | static void msp3400c_setbass(struct i2c_client *client, int bass) | |
492 | { | |
493 | int val = ((bass-32768) * 0x60 / 65535) << 8; | |
494 | ||
495 | msp3400_dbg("setbass: %d 0x%02x\n", bass, val >> 8); | |
496 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */ | |
497 | } | |
498 | ||
499 | static void msp3400c_settreble(struct i2c_client *client, int treble) | |
500 | { | |
501 | int val = ((treble-32768) * 0x60 / 65535) << 8; | |
502 | ||
503 | msp3400_dbg("settreble: %d 0x%02x\n",treble, val>>8); | |
504 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */ | |
505 | } | |
506 | ||
507 | static void msp3400c_setmode(struct i2c_client *client, int type) | |
508 | { | |
509 | struct msp3400c *msp = i2c_get_clientdata(client); | |
510 | int i; | |
511 | ||
512 | msp3400_dbg("setmode: %d\n",type); | |
513 | msp->mode = type; | |
514 | msp->audmode = V4L2_TUNER_MODE_MONO; | |
515 | msp->rxsubchans = V4L2_TUNER_SUB_MONO; | |
516 | ||
517 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */ | |
518 | msp_init_data[type].ad_cv); | |
519 | ||
520 | for (i = 5; i >= 0; i--) /* fir 1 */ | |
521 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001, | |
522 | msp_init_data[type].fir1[i]); | |
523 | ||
524 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */ | |
525 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040); | |
526 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000); | |
527 | for (i = 5; i >= 0; i--) | |
528 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, | |
529 | msp_init_data[type].fir2[i]); | |
530 | ||
531 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */ | |
532 | msp_init_data[type].mode_reg); | |
533 | ||
534 | msp3400c_setcarrier(client, msp_init_data[type].cdo1, | |
535 | msp_init_data[type].cdo2); | |
536 | ||
537 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/ | |
538 | ||
539 | if (dolby) { | |
540 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008, | |
541 | 0x0520); /* I2S1 */ | |
542 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009, | |
543 | 0x0620); /* I2S2 */ | |
544 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b, | |
545 | msp_init_data[type].dfp_src); | |
546 | } else { | |
547 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008, | |
548 | msp_init_data[type].dfp_src); | |
549 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009, | |
550 | msp_init_data[type].dfp_src); | |
551 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b, | |
552 | msp_init_data[type].dfp_src); | |
553 | } | |
554 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a, | |
555 | msp_init_data[type].dfp_src); | |
556 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, | |
557 | msp_init_data[type].dfp_matrix); | |
558 | ||
559 | if (HAVE_NICAM(msp)) { | |
560 | /* nicam prescale */ | |
561 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */ | |
562 | } | |
563 | } | |
564 | ||
565 | /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */ | |
566 | static int best_video_sound(int rxsubchans) | |
567 | { | |
568 | if (rxsubchans & V4L2_TUNER_SUB_STEREO) | |
569 | return V4L2_TUNER_MODE_STEREO; | |
570 | if (rxsubchans & V4L2_TUNER_SUB_LANG1) | |
571 | return V4L2_TUNER_MODE_LANG1; | |
572 | if (rxsubchans & V4L2_TUNER_SUB_LANG2) | |
573 | return V4L2_TUNER_MODE_LANG2; | |
574 | return V4L2_TUNER_MODE_MONO; | |
575 | } | |
576 | ||
577 | /* turn on/off nicam + stereo */ | |
578 | static void msp3400c_setstereo(struct i2c_client *client, int mode) | |
579 | { | |
580 | static char *strmode[] = { "0", "mono", "stereo", "3", | |
581 | "lang1", "5", "6", "7", "lang2" | |
582 | }; | |
583 | struct msp3400c *msp = i2c_get_clientdata(client); | |
584 | int nicam = 0; /* channel source: FM/AM or nicam */ | |
585 | int src = 0; | |
586 | ||
587 | if (IS_MSP34XX_G(msp)) { | |
588 | /* this method would break everything, let's make sure | |
589 | * it's never called | |
590 | */ | |
591 | msp3400_dbg | |
592 | ("DEBUG WARNING setstereo called with mode=%d instead of set_source (ignored)\n", | |
593 | mode); | |
594 | return; | |
595 | } | |
596 | ||
597 | /* switch demodulator */ | |
598 | switch (msp->mode) { | |
599 | case MSP_MODE_FM_TERRA: | |
600 | msp3400_dbg("FM setstereo: %s\n", strmode[mode]); | |
601 | msp3400c_setcarrier(client,msp->second,msp->main); | |
602 | switch (mode) { | |
603 | case V4L2_TUNER_MODE_STEREO: | |
604 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001); | |
605 | break; | |
606 | case V4L2_TUNER_MODE_MONO: | |
607 | case V4L2_TUNER_MODE_LANG1: | |
608 | case V4L2_TUNER_MODE_LANG2: | |
609 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000); | |
610 | break; | |
611 | } | |
612 | break; | |
613 | case MSP_MODE_FM_SAT: | |
614 | msp3400_dbg("SAT setstereo: %s\n", strmode[mode]); | |
615 | switch (mode) { | |
616 | case V4L2_TUNER_MODE_MONO: | |
617 | msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5)); | |
618 | break; | |
619 | case V4L2_TUNER_MODE_STEREO: | |
620 | msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02)); | |
621 | break; | |
622 | case V4L2_TUNER_MODE_LANG1: | |
623 | msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02)); | |
624 | break; | |
625 | case V4L2_TUNER_MODE_LANG2: | |
626 | msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02)); | |
627 | break; | |
628 | } | |
629 | break; | |
630 | case MSP_MODE_FM_NICAM1: | |
631 | case MSP_MODE_FM_NICAM2: | |
632 | case MSP_MODE_AM_NICAM: | |
633 | msp3400_dbg("NICAM setstereo: %s\n",strmode[mode]); | |
634 | msp3400c_setcarrier(client,msp->second,msp->main); | |
635 | if (msp->nicam_on) | |
636 | nicam=0x0100; | |
637 | break; | |
638 | case MSP_MODE_BTSC: | |
639 | msp3400_dbg("BTSC setstereo: %s\n",strmode[mode]); | |
640 | nicam=0x0300; | |
641 | break; | |
642 | case MSP_MODE_EXTERN: | |
643 | msp3400_dbg("extern setstereo: %s\n",strmode[mode]); | |
644 | nicam = 0x0200; | |
645 | break; | |
646 | case MSP_MODE_FM_RADIO: | |
647 | msp3400_dbg("FM-Radio setstereo: %s\n",strmode[mode]); | |
648 | break; | |
649 | default: | |
650 | msp3400_dbg("mono setstereo\n"); | |
651 | return; | |
652 | } | |
653 | ||
654 | /* switch audio */ | |
655 | switch (best_video_sound(mode)) { | |
656 | case V4L2_TUNER_MODE_STEREO: | |
657 | src = 0x0020 | nicam; | |
658 | break; | |
659 | case V4L2_TUNER_MODE_MONO: | |
660 | if (msp->mode == MSP_MODE_AM_NICAM) { | |
661 | msp3400_dbg("switching to AM mono\n"); | |
662 | /* AM mono decoding is handled by tuner, not MSP chip */ | |
663 | /* SCART switching control register */ | |
664 | msp3400c_set_scart(client,SCART_MONO,0); | |
665 | src = 0x0200; | |
666 | break; | |
667 | } | |
668 | case V4L2_TUNER_MODE_LANG1: | |
669 | src = 0x0000 | nicam; | |
670 | break; | |
671 | case V4L2_TUNER_MODE_LANG2: | |
672 | src = 0x0010 | nicam; | |
673 | break; | |
674 | } | |
675 | msp3400_dbg("setstereo final source/matrix = 0x%x\n", src); | |
676 | ||
677 | if (dolby) { | |
678 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520); | |
679 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620); | |
680 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src); | |
681 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src); | |
682 | } else { | |
683 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src); | |
684 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src); | |
685 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src); | |
686 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src); | |
687 | } | |
688 | } | |
689 | ||
690 | static void | |
691 | msp3400c_print_mode(struct i2c_client *client) | |
692 | { | |
693 | struct msp3400c *msp = i2c_get_clientdata(client); | |
694 | ||
695 | if (msp->main == msp->second) { | |
696 | msp3400_dbg("mono sound carrier: %d.%03d MHz\n", | |
697 | msp->main/910000,(msp->main/910)%1000); | |
698 | } else { | |
699 | msp3400_dbg("main sound carrier: %d.%03d MHz\n", | |
700 | msp->main/910000,(msp->main/910)%1000); | |
701 | } | |
702 | if (msp->mode == MSP_MODE_FM_NICAM1 || msp->mode == MSP_MODE_FM_NICAM2) | |
703 | msp3400_dbg("NICAM/FM carrier : %d.%03d MHz\n", | |
704 | msp->second/910000,(msp->second/910)%1000); | |
705 | if (msp->mode == MSP_MODE_AM_NICAM) | |
706 | msp3400_dbg("NICAM/AM carrier : %d.%03d MHz\n", | |
707 | msp->second/910000,(msp->second/910)%1000); | |
708 | if (msp->mode == MSP_MODE_FM_TERRA && | |
709 | msp->main != msp->second) { | |
710 | msp3400_dbg("FM-stereo carrier : %d.%03d MHz\n", | |
711 | msp->second/910000,(msp->second/910)%1000); | |
712 | } | |
713 | } | |
714 | ||
715 | #define MSP3400_MAX 4 | |
716 | static struct i2c_client *msps[MSP3400_MAX]; | |
717 | static void msp3400c_restore_dfp(struct i2c_client *client) | |
718 | { | |
719 | struct msp3400c *msp = i2c_get_clientdata(client); | |
720 | int i; | |
721 | ||
722 | for (i = 0; i < DFP_COUNT; i++) { | |
723 | if (-1 == msp->dfp_regs[i]) | |
724 | continue; | |
725 | msp3400c_write(client, I2C_MSP3400C_DFP, i, msp->dfp_regs[i]); | |
726 | } | |
727 | } | |
728 | ||
729 | /* if the dfp_regs is set, set what's in there. Otherwise, set the default value */ | |
730 | static int msp3400c_write_dfp_with_default(struct i2c_client *client, | |
731 | int addr, int default_value) | |
732 | { | |
733 | struct msp3400c *msp = i2c_get_clientdata(client); | |
734 | int value = default_value; | |
735 | if (addr < DFP_COUNT && -1 != msp->dfp_regs[addr]) | |
736 | value = msp->dfp_regs[addr]; | |
737 | return msp3400c_write(client, I2C_MSP3400C_DFP, addr, value); | |
738 | } | |
739 | ||
740 | /* ----------------------------------------------------------------------- */ | |
741 | ||
742 | struct REGISTER_DUMP { | |
743 | int addr; | |
744 | char *name; | |
745 | }; | |
746 | ||
747 | struct REGISTER_DUMP d1[] = { | |
748 | {0x007e, "autodetect"}, | |
749 | {0x0023, "C_AD_BITS "}, | |
750 | {0x0038, "ADD_BITS "}, | |
751 | {0x003e, "CIB_BITS "}, | |
752 | {0x0057, "ERROR_RATE"}, | |
753 | }; | |
754 | ||
755 | static int autodetect_stereo(struct i2c_client *client) | |
756 | { | |
757 | struct msp3400c *msp = i2c_get_clientdata(client); | |
758 | int val; | |
759 | int rxsubchans = msp->rxsubchans; | |
760 | int newnicam = msp->nicam_on; | |
761 | int update = 0; | |
762 | ||
763 | switch (msp->mode) { | |
764 | case MSP_MODE_FM_TERRA: | |
765 | val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18); | |
766 | if (val > 32767) | |
767 | val -= 65536; | |
768 | msp3400_dbg("stereo detect register: %d\n",val); | |
769 | if (val > 4096) { | |
770 | rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO; | |
771 | } else if (val < -4096) { | |
772 | rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; | |
773 | } else { | |
774 | rxsubchans = V4L2_TUNER_SUB_MONO; | |
775 | } | |
776 | newnicam = 0; | |
777 | break; | |
778 | case MSP_MODE_FM_NICAM1: | |
779 | case MSP_MODE_FM_NICAM2: | |
780 | case MSP_MODE_AM_NICAM: | |
781 | val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23); | |
782 | msp3400_dbg("nicam sync=%d, mode=%d\n", | |
783 | val & 1, (val & 0x1e) >> 1); | |
784 | ||
785 | if (val & 1) { | |
786 | /* nicam synced */ | |
787 | switch ((val & 0x1e) >> 1) { | |
788 | case 0: | |
789 | case 8: | |
790 | rxsubchans = V4L2_TUNER_SUB_STEREO; | |
791 | break; | |
792 | case 1: | |
793 | case 9: | |
794 | rxsubchans = V4L2_TUNER_SUB_MONO | |
795 | | V4L2_TUNER_SUB_LANG1; | |
796 | break; | |
797 | case 2: | |
798 | case 10: | |
799 | rxsubchans = V4L2_TUNER_SUB_MONO | |
800 | | V4L2_TUNER_SUB_LANG1 | |
801 | | V4L2_TUNER_SUB_LANG2; | |
802 | break; | |
803 | default: | |
804 | rxsubchans = V4L2_TUNER_SUB_MONO; | |
805 | break; | |
806 | } | |
807 | newnicam=1; | |
808 | } else { | |
809 | newnicam = 0; | |
810 | rxsubchans = V4L2_TUNER_SUB_MONO; | |
811 | } | |
812 | break; | |
813 | case MSP_MODE_BTSC: | |
814 | val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200); | |
815 | msp3400_dbg("status=0x%x (pri=%s, sec=%s, %s%s%s)\n", | |
816 | val, | |
817 | (val & 0x0002) ? "no" : "yes", | |
818 | (val & 0x0004) ? "no" : "yes", | |
819 | (val & 0x0040) ? "stereo" : "mono", | |
820 | (val & 0x0080) ? ", nicam 2nd mono" : "", | |
821 | (val & 0x0100) ? ", bilingual/SAP" : ""); | |
822 | rxsubchans = V4L2_TUNER_SUB_MONO; | |
823 | if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO; | |
824 | if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1; | |
825 | break; | |
826 | } | |
827 | if (rxsubchans != msp->rxsubchans) { | |
828 | update = 1; | |
829 | msp3400_dbg("watch: rxsubchans %d => %d\n", | |
830 | msp->rxsubchans,rxsubchans); | |
831 | msp->rxsubchans = rxsubchans; | |
832 | } | |
833 | if (newnicam != msp->nicam_on) { | |
834 | update = 1; | |
835 | msp3400_dbg("watch: nicam %d => %d\n", | |
836 | msp->nicam_on,newnicam); | |
837 | msp->nicam_on = newnicam; | |
838 | } | |
839 | return update; | |
840 | } | |
841 | ||
842 | /* | |
843 | * A kernel thread for msp3400 control -- we don't want to block the | |
844 | * in the ioctl while doing the sound carrier & stereo detect | |
845 | */ | |
846 | ||
847 | static int msp34xx_sleep(struct msp3400c *msp, int timeout) | |
848 | { | |
849 | DECLARE_WAITQUEUE(wait, current); | |
850 | ||
851 | add_wait_queue(&msp->wq, &wait); | |
852 | if (!kthread_should_stop()) { | |
853 | if (timeout < 0) { | |
854 | set_current_state(TASK_INTERRUPTIBLE); | |
855 | schedule(); | |
856 | } else { | |
857 | schedule_timeout_interruptible | |
858 | (msecs_to_jiffies(timeout)); | |
859 | } | |
860 | } | |
861 | ||
862 | remove_wait_queue(&msp->wq, &wait); | |
863 | try_to_freeze(); | |
864 | return msp->restart; | |
865 | } | |
866 | ||
867 | /* stereo/multilang monitoring */ | |
868 | static void watch_stereo(struct i2c_client *client) | |
869 | { | |
870 | struct msp3400c *msp = i2c_get_clientdata(client); | |
871 | ||
872 | if (autodetect_stereo(client)) { | |
873 | if (msp->stereo & V4L2_TUNER_MODE_STEREO) | |
874 | msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO); | |
875 | else if (msp->stereo & VIDEO_SOUND_LANG1) | |
876 | msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1); | |
877 | else | |
878 | msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); | |
879 | } | |
880 | ||
881 | if (once) | |
882 | msp->watch_stereo = 0; | |
883 | } | |
884 | ||
885 | ||
886 | static int msp3400c_thread(void *data) | |
887 | { | |
888 | struct i2c_client *client = data; | |
889 | struct msp3400c *msp = i2c_get_clientdata(client); | |
890 | struct CARRIER_DETECT *cd; | |
891 | int count, max1,max2,val1,val2, val,this; | |
892 | ||
893 | ||
894 | msp3400_info("msp3400 daemon started\n"); | |
895 | for (;;) { | |
896 | msp3400_dbg_mediumvol("msp3400 thread: sleep\n"); | |
897 | msp34xx_sleep(msp,-1); | |
898 | msp3400_dbg_mediumvol("msp3400 thread: wakeup\n"); | |
899 | ||
900 | restart: | |
901 | msp3400_dbg("thread: restart scan\n"); | |
902 | msp->restart = 0; | |
903 | if (kthread_should_stop()) | |
904 | break; | |
905 | ||
906 | if (VIDEO_MODE_RADIO == msp->norm || | |
907 | MSP_MODE_EXTERN == msp->mode) { | |
908 | /* no carrier scan, just unmute */ | |
909 | msp3400_info("thread: no carrier scan\n"); | |
910 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
911 | continue; | |
912 | } | |
913 | ||
914 | /* mute */ | |
915 | msp3400c_setvolume(client, msp->muted, 0, 0); | |
916 | msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ ); | |
917 | val1 = val2 = 0; | |
918 | max1 = max2 = -1; | |
919 | msp->watch_stereo = 0; | |
920 | ||
921 | /* some time for the tuner to sync */ | |
922 | if (msp34xx_sleep(msp,200)) | |
923 | goto restart; | |
924 | ||
925 | /* carrier detect pass #1 -- main carrier */ | |
926 | cd = carrier_detect_main; | |
927 | count = CARRIER_COUNT(carrier_detect_main); | |
928 | ||
929 | if (amsound && (msp->norm == VIDEO_MODE_SECAM)) { | |
930 | /* autodetect doesn't work well with AM ... */ | |
931 | max1 = 3; | |
932 | count = 0; | |
933 | msp3400_dbg("AM sound override\n"); | |
934 | } | |
935 | ||
936 | for (this = 0; this < count; this++) { | |
937 | msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo); | |
938 | if (msp34xx_sleep(msp,100)) | |
939 | goto restart; | |
940 | val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b); | |
941 | if (val > 32767) | |
942 | val -= 65536; | |
943 | if (val1 < val) | |
944 | val1 = val, max1 = this; | |
945 | msp3400_dbg("carrier1 val: %5d / %s\n", val,cd[this].name); | |
946 | } | |
947 | ||
948 | /* carrier detect pass #2 -- second (stereo) carrier */ | |
949 | switch (max1) { | |
950 | case 1: /* 5.5 */ | |
951 | cd = carrier_detect_55; | |
952 | count = CARRIER_COUNT(carrier_detect_55); | |
953 | break; | |
954 | case 3: /* 6.5 */ | |
955 | cd = carrier_detect_65; | |
956 | count = CARRIER_COUNT(carrier_detect_65); | |
957 | break; | |
958 | case 0: /* 4.5 */ | |
959 | case 2: /* 6.0 */ | |
960 | default: | |
961 | cd = NULL; | |
962 | count = 0; | |
963 | break; | |
964 | } | |
965 | ||
966 | if (amsound && (msp->norm == VIDEO_MODE_SECAM)) { | |
967 | /* autodetect doesn't work well with AM ... */ | |
968 | cd = NULL; | |
969 | count = 0; | |
970 | max2 = 0; | |
971 | } | |
972 | for (this = 0; this < count; this++) { | |
973 | msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo); | |
974 | if (msp34xx_sleep(msp,100)) | |
975 | goto restart; | |
976 | val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b); | |
977 | if (val > 32767) | |
978 | val -= 65536; | |
979 | if (val2 < val) | |
980 | val2 = val, max2 = this; | |
981 | msp3400_dbg("carrier2 val: %5d / %s\n", val,cd[this].name); | |
982 | } | |
983 | ||
984 | /* programm the msp3400 according to the results */ | |
985 | msp->main = carrier_detect_main[max1].cdo; | |
986 | switch (max1) { | |
987 | case 1: /* 5.5 */ | |
988 | if (max2 == 0) { | |
989 | /* B/G FM-stereo */ | |
990 | msp->second = carrier_detect_55[max2].cdo; | |
991 | msp3400c_setmode(client, MSP_MODE_FM_TERRA); | |
992 | msp->nicam_on = 0; | |
993 | msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); | |
994 | msp->watch_stereo = 1; | |
995 | } else if (max2 == 1 && HAVE_NICAM(msp)) { | |
996 | /* B/G NICAM */ | |
997 | msp->second = carrier_detect_55[max2].cdo; | |
998 | msp3400c_setmode(client, MSP_MODE_FM_NICAM1); | |
999 | msp->nicam_on = 1; | |
1000 | msp3400c_setcarrier(client, msp->second, msp->main); | |
1001 | msp->watch_stereo = 1; | |
1002 | } else { | |
1003 | goto no_second; | |
1004 | } | |
1005 | break; | |
1006 | case 2: /* 6.0 */ | |
1007 | /* PAL I NICAM */ | |
1008 | msp->second = MSP_CARRIER(6.552); | |
1009 | msp3400c_setmode(client, MSP_MODE_FM_NICAM2); | |
1010 | msp->nicam_on = 1; | |
1011 | msp3400c_setcarrier(client, msp->second, msp->main); | |
1012 | msp->watch_stereo = 1; | |
1013 | break; | |
1014 | case 3: /* 6.5 */ | |
1015 | if (max2 == 1 || max2 == 2) { | |
1016 | /* D/K FM-stereo */ | |
1017 | msp->second = carrier_detect_65[max2].cdo; | |
1018 | msp3400c_setmode(client, MSP_MODE_FM_TERRA); | |
1019 | msp->nicam_on = 0; | |
1020 | msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); | |
1021 | msp->watch_stereo = 1; | |
1022 | } else if (max2 == 0 && | |
1023 | msp->norm == VIDEO_MODE_SECAM) { | |
1024 | /* L NICAM or AM-mono */ | |
1025 | msp->second = carrier_detect_65[max2].cdo; | |
1026 | msp3400c_setmode(client, MSP_MODE_AM_NICAM); | |
1027 | msp->nicam_on = 0; | |
1028 | msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); | |
1029 | msp3400c_setcarrier(client, msp->second, msp->main); | |
1030 | /* volume prescale for SCART (AM mono input) */ | |
1031 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900); | |
1032 | msp->watch_stereo = 1; | |
1033 | } else if (max2 == 0 && HAVE_NICAM(msp)) { | |
1034 | /* D/K NICAM */ | |
1035 | msp->second = carrier_detect_65[max2].cdo; | |
1036 | msp3400c_setmode(client, MSP_MODE_FM_NICAM1); | |
1037 | msp->nicam_on = 1; | |
1038 | msp3400c_setcarrier(client, msp->second, msp->main); | |
1039 | msp->watch_stereo = 1; | |
1040 | } else { | |
1041 | goto no_second; | |
1042 | } | |
1043 | break; | |
1044 | case 0: /* 4.5 */ | |
1045 | default: | |
1046 | no_second: | |
1047 | msp->second = carrier_detect_main[max1].cdo; | |
1048 | msp3400c_setmode(client, MSP_MODE_FM_TERRA); | |
1049 | msp->nicam_on = 0; | |
1050 | msp3400c_setcarrier(client, msp->second, msp->main); | |
1051 | msp->rxsubchans = V4L2_TUNER_SUB_MONO; | |
1052 | msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); | |
1053 | break; | |
1054 | } | |
1055 | ||
1056 | /* unmute */ | |
1057 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1058 | msp3400c_restore_dfp(client); | |
1059 | ||
1060 | if (debug) | |
1061 | msp3400c_print_mode(client); | |
1062 | ||
1063 | /* monitor tv audio mode */ | |
1064 | while (msp->watch_stereo) { | |
1065 | if (msp34xx_sleep(msp,5000)) | |
1066 | goto restart; | |
1067 | watch_stereo(client); | |
1068 | } | |
1069 | } | |
1070 | msp3400_dbg("thread: exit\n"); | |
1071 | return 0; | |
1072 | } | |
1073 | ||
1074 | /* ----------------------------------------------------------------------- */ | |
1075 | /* this one uses the automatic sound standard detection of newer */ | |
1076 | /* msp34xx chip versions */ | |
1077 | ||
1078 | static struct MODES { | |
1079 | int retval; | |
1080 | int main, second; | |
1081 | char *name; | |
1082 | } modelist[] = { | |
1083 | { 0x0000, 0, 0, "ERROR" }, | |
1084 | { 0x0001, 0, 0, "autodetect start" }, | |
1085 | { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" }, | |
1086 | { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" }, | |
1087 | { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" }, | |
1088 | { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" }, | |
1089 | { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" }, | |
1090 | { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" }, | |
1091 | { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" }, | |
1092 | { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" }, | |
1093 | { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" }, | |
1094 | { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" }, | |
1095 | { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" }, | |
1096 | { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" }, | |
1097 | { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" }, | |
1098 | { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" }, | |
1099 | { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" }, | |
1100 | { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" }, | |
1101 | { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" }, | |
1102 | { -1, 0, 0, NULL }, /* EOF */ | |
1103 | }; | |
1104 | ||
1105 | static inline const char *msp34xx_standard_mode_name(int mode) | |
1106 | { | |
1107 | int i; | |
1108 | for (i = 0; modelist[i].name != NULL; i++) | |
1109 | if (modelist[i].retval == mode) | |
1110 | return modelist[i].name; | |
1111 | return "unknown"; | |
1112 | } | |
1113 | ||
1114 | static int msp34xx_modus(struct i2c_client *client, int norm) | |
1115 | { | |
1116 | switch (norm) { | |
1117 | case VIDEO_MODE_PAL: | |
1118 | msp3400_dbg("video mode selected to PAL\n"); | |
1119 | ||
1120 | #if 1 | |
1121 | /* experimental: not sure this works with all chip versions */ | |
1122 | return 0x7003; | |
1123 | #else | |
1124 | /* previous value, try this if it breaks ... */ | |
1125 | return 0x1003; | |
1126 | #endif | |
1127 | case VIDEO_MODE_NTSC: /* BTSC */ | |
1128 | msp3400_dbg("video mode selected to NTSC\n"); | |
1129 | return 0x2003; | |
1130 | case VIDEO_MODE_SECAM: | |
1131 | msp3400_dbg("video mode selected to SECAM\n"); | |
1132 | return 0x0003; | |
1133 | case VIDEO_MODE_RADIO: | |
1134 | msp3400_dbg("video mode selected to Radio\n"); | |
1135 | return 0x0003; | |
1136 | case VIDEO_MODE_AUTO: | |
1137 | msp3400_dbg("video mode selected to Auto\n"); | |
1138 | return 0x2003; | |
1139 | default: | |
1140 | return 0x0003; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | static int msp34xx_standard(int norm) | |
1145 | { | |
1146 | switch (norm) { | |
1147 | case VIDEO_MODE_PAL: | |
1148 | return 1; | |
1149 | case VIDEO_MODE_NTSC: /* BTSC */ | |
1150 | return 0x0020; | |
1151 | case VIDEO_MODE_SECAM: | |
1152 | return 1; | |
1153 | case VIDEO_MODE_RADIO: | |
1154 | return 0x0040; | |
1155 | default: | |
1156 | return 1; | |
1157 | } | |
1158 | } | |
1159 | ||
1160 | static int msp3410d_thread(void *data) | |
1161 | { | |
1162 | struct i2c_client *client = data; | |
1163 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1164 | int mode,val,i,std; | |
1165 | ||
1166 | msp3400_info("msp3410 daemon started\n"); | |
1167 | ||
1168 | for (;;) { | |
1169 | msp3400_dbg_mediumvol("msp3410 thread: sleep\n"); | |
1170 | msp34xx_sleep(msp,-1); | |
1171 | msp3400_dbg_mediumvol("msp3410 thread: wakeup\n"); | |
1172 | ||
1173 | restart: | |
1174 | msp3400_dbg("thread: restart scan\n"); | |
1175 | msp->restart = 0; | |
1176 | if (kthread_should_stop()) | |
1177 | break; | |
1178 | ||
1179 | if (msp->mode == MSP_MODE_EXTERN) { | |
1180 | /* no carrier scan needed, just unmute */ | |
1181 | msp3400_dbg("thread: no carrier scan\n"); | |
1182 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1183 | continue; | |
1184 | } | |
1185 | ||
1186 | /* put into sane state (and mute) */ | |
1187 | msp3400c_reset(client); | |
1188 | ||
1189 | /* some time for the tuner to sync */ | |
1190 | if (msp34xx_sleep(msp,200)) | |
1191 | goto restart; | |
1192 | ||
1193 | /* start autodetect */ | |
1194 | mode = msp34xx_modus(client, msp->norm); | |
1195 | std = msp34xx_standard(msp->norm); | |
1196 | msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode); | |
1197 | msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std); | |
1198 | msp->watch_stereo = 0; | |
1199 | ||
1200 | if (debug) | |
1201 | msp3400_dbg("setting mode: %s (0x%04x)\n", | |
1202 | msp34xx_standard_mode_name(std) ,std); | |
1203 | ||
1204 | if (std != 1) { | |
1205 | /* programmed some specific mode */ | |
1206 | val = std; | |
1207 | } else { | |
1208 | /* triggered autodetect */ | |
1209 | for (;;) { | |
1210 | if (msp34xx_sleep(msp,100)) | |
1211 | goto restart; | |
1212 | ||
1213 | /* check results */ | |
1214 | val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e); | |
1215 | if (val < 0x07ff) | |
1216 | break; | |
1217 | msp3400_dbg("detection still in progress\n"); | |
1218 | } | |
1219 | } | |
1220 | for (i = 0; modelist[i].name != NULL; i++) | |
1221 | if (modelist[i].retval == val) | |
1222 | break; | |
1223 | msp3400_dbg("current mode: %s (0x%04x)\n", | |
1224 | modelist[i].name ? modelist[i].name : "unknown", | |
1225 | val); | |
1226 | msp->main = modelist[i].main; | |
1227 | msp->second = modelist[i].second; | |
1228 | ||
1229 | if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) { | |
1230 | /* autodetection has failed, let backup */ | |
1231 | msp3400_dbg("autodetection failed," | |
1232 | " switching to backup mode: %s (0x%04x)\n", | |
1233 | modelist[8].name ? modelist[8].name : "unknown",val); | |
1234 | val = 0x0009; | |
1235 | msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val); | |
1236 | } | |
1237 | ||
1238 | /* set various prescales */ | |
1239 | msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */ | |
1240 | msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */ | |
1241 | msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */ | |
1242 | ||
1243 | /* set stereo */ | |
1244 | switch (val) { | |
1245 | case 0x0008: /* B/G NICAM */ | |
1246 | case 0x000a: /* I NICAM */ | |
1247 | if (val == 0x0008) | |
1248 | msp->mode = MSP_MODE_FM_NICAM1; | |
1249 | else | |
1250 | msp->mode = MSP_MODE_FM_NICAM2; | |
1251 | /* just turn on stereo */ | |
1252 | msp->rxsubchans = V4L2_TUNER_SUB_STEREO; | |
1253 | msp->nicam_on = 1; | |
1254 | msp->watch_stereo = 1; | |
1255 | msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO); | |
1256 | break; | |
1257 | case 0x0009: | |
1258 | msp->mode = MSP_MODE_AM_NICAM; | |
1259 | msp->rxsubchans = V4L2_TUNER_SUB_MONO; | |
1260 | msp->nicam_on = 1; | |
1261 | msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO); | |
1262 | msp->watch_stereo = 1; | |
1263 | break; | |
1264 | case 0x0020: /* BTSC */ | |
1265 | /* just turn on stereo */ | |
1266 | msp->mode = MSP_MODE_BTSC; | |
1267 | msp->rxsubchans = V4L2_TUNER_SUB_STEREO; | |
1268 | msp->nicam_on = 0; | |
1269 | msp->watch_stereo = 1; | |
1270 | msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO); | |
1271 | break; | |
1272 | case 0x0040: /* FM radio */ | |
1273 | msp->mode = MSP_MODE_FM_RADIO; | |
1274 | msp->rxsubchans = V4L2_TUNER_SUB_STEREO; | |
1275 | msp->audmode = V4L2_TUNER_MODE_STEREO; | |
1276 | msp->nicam_on = 0; | |
1277 | msp->watch_stereo = 0; | |
1278 | /* not needed in theory if HAVE_RADIO(), but | |
1279 | short programming enables carrier mute */ | |
1280 | msp3400c_setmode(client,MSP_MODE_FM_RADIO); | |
1281 | msp3400c_setcarrier(client, MSP_CARRIER(10.7), | |
1282 | MSP_CARRIER(10.7)); | |
1283 | /* scart routing */ | |
1284 | msp3400c_set_scart(client,SCART_IN2,0); | |
1285 | /* msp34xx does radio decoding */ | |
1286 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020); | |
1287 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020); | |
1288 | msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020); | |
1289 | break; | |
1290 | case 0x0003: | |
1291 | case 0x0004: | |
1292 | case 0x0005: | |
1293 | msp->mode = MSP_MODE_FM_TERRA; | |
1294 | msp->rxsubchans = V4L2_TUNER_SUB_MONO; | |
1295 | msp->audmode = V4L2_TUNER_MODE_MONO; | |
1296 | msp->nicam_on = 0; | |
1297 | msp->watch_stereo = 1; | |
1298 | break; | |
1299 | } | |
1300 | ||
1301 | /* unmute, restore misc registers */ | |
1302 | msp3400c_setbass(client, msp->bass); | |
1303 | msp3400c_settreble(client, msp->treble); | |
1304 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1305 | msp3400c_write(client, I2C_MSP3400C_DFP, 0x13, msp->acb); | |
1306 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); | |
1307 | msp3400c_restore_dfp(client); | |
1308 | ||
1309 | /* monitor tv audio mode */ | |
1310 | while (msp->watch_stereo) { | |
1311 | if (msp34xx_sleep(msp,5000)) | |
1312 | goto restart; | |
1313 | watch_stereo(client); | |
1314 | } | |
1315 | } | |
1316 | msp3400_dbg("thread: exit\n"); | |
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | /* ----------------------------------------------------------------------- */ | |
1321 | /* msp34xxG + (simpler no-thread) */ | |
1322 | /* this one uses both automatic standard detection and automatic sound */ | |
1323 | /* select which are available in the newer G versions */ | |
1324 | /* struct msp: only norm, acb and source are really used in this mode */ | |
1325 | ||
1326 | static void msp34xxg_set_source(struct i2c_client *client, int source); | |
1327 | ||
1328 | /* (re-)initialize the msp34xxg, according to the current norm in msp->norm | |
1329 | * return 0 if it worked, -1 if it failed | |
1330 | */ | |
1331 | static int msp34xxg_reset(struct i2c_client *client) | |
1332 | { | |
1333 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1334 | int modus,std; | |
1335 | ||
1336 | if (msp3400c_reset(client)) | |
1337 | return -1; | |
1338 | ||
1339 | /* make sure that input/output is muted (paranoid mode) */ | |
1340 | if (msp3400c_write(client, | |
1341 | I2C_MSP3400C_DFP, | |
1342 | 0x13, /* ACB */ | |
1343 | 0x0f20 /* mute DSP input, mute SCART 1 */)) | |
1344 | return -1; | |
1345 | ||
1346 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); | |
1347 | ||
1348 | /* step-by-step initialisation, as described in the manual */ | |
1349 | modus = msp34xx_modus(client, msp->norm); | |
1350 | std = msp34xx_standard(msp->norm); | |
1351 | modus &= ~0x03; /* STATUS_CHANGE=0 */ | |
1352 | modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */ | |
1353 | if (msp3400c_write(client, | |
1354 | I2C_MSP3400C_DEM, | |
1355 | 0x30/*MODUS*/, | |
1356 | modus)) | |
1357 | return -1; | |
1358 | if (msp3400c_write(client, | |
1359 | I2C_MSP3400C_DEM, | |
1360 | 0x20/*standard*/, | |
1361 | std)) | |
1362 | return -1; | |
1363 | ||
1364 | /* write the dfps that may have an influence on | |
1365 | standard/audio autodetection right now */ | |
1366 | msp34xxg_set_source(client, msp->source); | |
1367 | ||
1368 | if (msp3400c_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */ | |
1369 | 0x3000 | |
1370 | /* default: [15:8] 75khz deviation */ | |
1371 | )) | |
1372 | return -1; | |
1373 | ||
1374 | if (msp3400c_write_dfp_with_default(client, 0x10, /* NICAM Prescale */ | |
1375 | 0x5a00 | |
1376 | /* default: 9db gain (as recommended) */ | |
1377 | )) | |
1378 | return -1; | |
1379 | ||
1380 | return 0; | |
1381 | } | |
1382 | ||
1383 | static int msp34xxg_thread(void *data) | |
1384 | { | |
1385 | struct i2c_client *client = data; | |
1386 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1387 | int val, std, i; | |
1388 | ||
1389 | msp3400_info("msp34xxg daemon started\n"); | |
1390 | ||
1391 | msp->source = 1; /* default */ | |
1392 | for (;;) { | |
1393 | msp3400_dbg_mediumvol("msp34xxg thread: sleep\n"); | |
1394 | msp34xx_sleep(msp,-1); | |
1395 | msp3400_dbg_mediumvol("msp34xxg thread: wakeup\n"); | |
1396 | ||
1397 | restart: | |
1398 | msp3400_dbg("thread: restart scan\n"); | |
1399 | msp->restart = 0; | |
1400 | if (kthread_should_stop()) | |
1401 | break; | |
1402 | ||
1403 | /* setup the chip*/ | |
1404 | msp34xxg_reset(client); | |
1405 | std = standard; | |
1406 | if (std != 0x01) | |
1407 | goto unmute; | |
1408 | ||
1409 | /* watch autodetect */ | |
1410 | msp3400_dbg("triggered autodetect, waiting for result\n"); | |
1411 | for (i = 0; i < 10; i++) { | |
1412 | if (msp34xx_sleep(msp,100)) | |
1413 | goto restart; | |
1414 | ||
1415 | /* check results */ | |
1416 | val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e); | |
1417 | if (val < 0x07ff) { | |
1418 | std = val; | |
1419 | break; | |
1420 | } | |
1421 | msp3400_dbg("detection still in progress\n"); | |
1422 | } | |
1423 | if (0x01 == std) { | |
1424 | msp3400_dbg("detection still in progress after 10 tries. giving up.\n"); | |
1425 | continue; | |
1426 | } | |
1427 | ||
1428 | unmute: | |
1429 | msp3400_dbg("current mode: %s (0x%04x)\n", | |
1430 | msp34xx_standard_mode_name(std), std); | |
1431 | ||
1432 | /* unmute: dispatch sound to scart output, set scart volume */ | |
1433 | msp3400_dbg("unmute\n"); | |
1434 | ||
1435 | msp3400c_setbass(client, msp->bass); | |
1436 | msp3400c_settreble(client, msp->treble); | |
1437 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1438 | ||
1439 | /* restore ACB */ | |
1440 | if (msp3400c_write(client, | |
1441 | I2C_MSP3400C_DFP, | |
1442 | 0x13, /* ACB */ | |
1443 | msp->acb)) | |
1444 | return -1; | |
1445 | ||
1446 | msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); | |
1447 | } | |
1448 | msp3400_dbg("thread: exit\n"); | |
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | /* set the same 'source' for the loudspeaker, scart and quasi-peak detector | |
1453 | * the value for source is the same as bit 15:8 of DFP registers 0x08, | |
1454 | * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B | |
1455 | * | |
1456 | * this function replaces msp3400c_setstereo | |
1457 | */ | |
1458 | static void msp34xxg_set_source(struct i2c_client *client, int source) | |
1459 | { | |
1460 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1461 | ||
1462 | /* fix matrix mode to stereo and let the msp choose what | |
1463 | * to output according to 'source', as recommended | |
1464 | * for MONO (source==0) downmixing set bit[7:0] to 0x30 | |
1465 | */ | |
1466 | int value = (source&0x07)<<8|(source==0 ? 0x30:0x20); | |
1467 | msp3400_dbg("set source to %d (0x%x)\n", source, value); | |
1468 | msp3400c_write(client, | |
1469 | I2C_MSP3400C_DFP, | |
1470 | 0x08, /* Loudspeaker Output */ | |
1471 | value); | |
1472 | msp3400c_write(client, | |
1473 | I2C_MSP3400C_DFP, | |
1474 | 0x0a, /* SCART1 DA Output */ | |
1475 | value); | |
1476 | msp3400c_write(client, | |
1477 | I2C_MSP3400C_DFP, | |
1478 | 0x0c, /* Quasi-peak detector */ | |
1479 | value); | |
1480 | /* | |
1481 | * set identification threshold. Personally, I | |
1482 | * I set it to a higher value that the default | |
1483 | * of 0x190 to ignore noisy stereo signals. | |
1484 | * this needs tuning. (recommended range 0x00a0-0x03c0) | |
1485 | * 0x7f0 = forced mono mode | |
1486 | */ | |
1487 | msp3400c_write(client, | |
1488 | I2C_MSP3400C_DEM, | |
1489 | 0x22, /* a2 threshold for stereo/bilingual */ | |
1490 | stereo_threshold); | |
1491 | msp->source=source; | |
1492 | } | |
1493 | ||
1494 | static void msp34xxg_detect_stereo(struct i2c_client *client) | |
1495 | { | |
1496 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1497 | ||
1498 | int status = msp3400c_read(client, | |
1499 | I2C_MSP3400C_DEM, | |
1500 | 0x0200 /* STATUS */); | |
1501 | int is_bilingual = status&0x100; | |
1502 | int is_stereo = status&0x40; | |
1503 | ||
1504 | msp->rxsubchans = 0; | |
1505 | if (is_stereo) | |
1506 | msp->rxsubchans |= V4L2_TUNER_SUB_STEREO; | |
1507 | else | |
1508 | msp->rxsubchans |= V4L2_TUNER_SUB_MONO; | |
1509 | if (is_bilingual) { | |
1510 | msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2; | |
1511 | /* I'm supposed to check whether it's SAP or not | |
1512 | * and set only LANG2/SAP in this case. Yet, the MSP | |
1513 | * does a lot of work to hide this and handle everything | |
1514 | * the same way. I don't want to work around it so unless | |
1515 | * this is a problem, I'll handle SAP just like lang1/lang2. | |
1516 | */ | |
1517 | } | |
1518 | msp3400_dbg("status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n", | |
1519 | status, is_stereo, is_bilingual, msp->rxsubchans); | |
1520 | } | |
1521 | ||
1522 | static void msp34xxg_set_audmode(struct i2c_client *client, int audmode) | |
1523 | { | |
1524 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1525 | int source; | |
1526 | ||
1527 | switch (audmode) { | |
1528 | case V4L2_TUNER_MODE_MONO: | |
1529 | source=0; /* mono only */ | |
1530 | break; | |
1531 | case V4L2_TUNER_MODE_STEREO: | |
1532 | source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */ | |
1533 | /* problem: that could also mean 2 (scart input) */ | |
1534 | break; | |
1535 | case V4L2_TUNER_MODE_LANG1: | |
1536 | source=3; /* stereo or A */ | |
1537 | break; | |
1538 | case V4L2_TUNER_MODE_LANG2: | |
1539 | source=4; /* stereo or B */ | |
1540 | break; | |
1541 | default: | |
1542 | audmode = 0; | |
1543 | source = 1; | |
1544 | break; | |
1545 | } | |
1546 | msp->audmode = audmode; | |
1547 | msp34xxg_set_source(client, source); | |
1548 | } | |
1549 | ||
1550 | ||
1551 | /* ----------------------------------------------------------------------- */ | |
1552 | ||
1553 | static int msp_attach(struct i2c_adapter *adap, int addr, int kind); | |
1554 | static int msp_detach(struct i2c_client *client); | |
1555 | static int msp_probe(struct i2c_adapter *adap); | |
1556 | static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg); | |
1557 | ||
1558 | static int msp_suspend(struct device * dev, pm_message_t state); | |
1559 | static int msp_resume(struct device * dev); | |
1560 | ||
1561 | static void msp_wake_thread(struct i2c_client *client); | |
1562 | ||
1563 | static struct i2c_driver driver = { | |
1564 | .owner = THIS_MODULE, | |
1565 | .name = "msp3400", | |
1566 | .id = I2C_DRIVERID_MSP3400, | |
1567 | .attach_adapter = msp_probe, | |
1568 | .detach_client = msp_detach, | |
1569 | .command = msp_command, | |
1570 | .driver = { | |
1571 | .suspend = msp_suspend, | |
1572 | .resume = msp_resume, | |
1573 | }, | |
1574 | }; | |
1575 | ||
1576 | static struct i2c_client client_template = | |
1577 | { | |
1578 | .name = "(unset)", | |
1579 | .flags = I2C_CLIENT_ALLOW_USE, | |
1580 | .driver = &driver, | |
1581 | }; | |
1582 | ||
1583 | static int msp_attach(struct i2c_adapter *adap, int addr, int kind) | |
1584 | { | |
1585 | struct msp3400c *msp; | |
1586 | struct i2c_client *client = &client_template; | |
1587 | int (*thread_func)(void *data) = NULL; | |
1588 | int i; | |
1589 | ||
1590 | client_template.adapter = adap; | |
1591 | client_template.addr = addr; | |
1592 | ||
1593 | if (-1 == msp3400c_reset(&client_template)) { | |
1594 | msp3400_dbg("no chip found\n"); | |
1595 | return -1; | |
1596 | } | |
1597 | ||
1598 | if (NULL == (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL))) | |
1599 | return -ENOMEM; | |
1600 | memcpy(client,&client_template,sizeof(struct i2c_client)); | |
1601 | if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) { | |
1602 | kfree(client); | |
1603 | return -ENOMEM; | |
1604 | } | |
1605 | ||
1606 | memset(msp,0,sizeof(struct msp3400c)); | |
1607 | msp->norm = VIDEO_MODE_NTSC; | |
1608 | msp->left = 58880; /* 0db gain */ | |
1609 | msp->right = 58880; /* 0db gain */ | |
1610 | msp->bass = 32768; | |
1611 | msp->treble = 32768; | |
1612 | msp->input = -1; | |
1613 | msp->muted = 0; | |
1614 | msp->i2s_mode = 0; | |
1615 | for (i = 0; i < DFP_COUNT; i++) | |
1616 | msp->dfp_regs[i] = -1; | |
1617 | ||
1618 | i2c_set_clientdata(client, msp); | |
1619 | init_waitqueue_head(&msp->wq); | |
1620 | ||
1621 | if (-1 == msp3400c_reset(client)) { | |
1622 | kfree(msp); | |
1623 | kfree(client); | |
1624 | msp3400_dbg("no chip found\n"); | |
1625 | return -1; | |
1626 | } | |
1627 | ||
1628 | msp->rev1 = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1e); | |
1629 | if (-1 != msp->rev1) | |
1630 | msp->rev2 = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1f); | |
1631 | if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) { | |
1632 | kfree(msp); | |
1633 | kfree(client); | |
1634 | msp3400_dbg("error while reading chip version\n"); | |
1635 | return -1; | |
1636 | } | |
1637 | msp3400_dbg("rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2); | |
1638 | ||
1639 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1640 | ||
1641 | snprintf(client->name, sizeof(client->name), "MSP%c4%02d%c-%c%d", | |
1642 | ((msp->rev1>>4)&0x0f) + '3', | |
1643 | (msp->rev2>>8)&0xff, (msp->rev1&0x0f)+'@', | |
1644 | ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f); | |
1645 | ||
1646 | msp->opmode = opmode; | |
1647 | if (OPMODE_AUTO == msp->opmode) { | |
1648 | if (HAVE_SIMPLER(msp)) | |
1649 | msp->opmode = OPMODE_SIMPLER; | |
1650 | else if (HAVE_SIMPLE(msp)) | |
1651 | msp->opmode = OPMODE_SIMPLE; | |
1652 | else | |
1653 | msp->opmode = OPMODE_MANUAL; | |
1654 | } | |
1655 | ||
1656 | /* hello world :-) */ | |
1657 | msp3400_info("chip=%s", client->name); | |
1658 | if (HAVE_NICAM(msp)) | |
1659 | printk(" +nicam"); | |
1660 | if (HAVE_SIMPLE(msp)) | |
1661 | printk(" +simple"); | |
1662 | if (HAVE_SIMPLER(msp)) | |
1663 | printk(" +simpler"); | |
1664 | if (HAVE_RADIO(msp)) | |
1665 | printk(" +radio"); | |
1666 | ||
1667 | /* version-specific initialization */ | |
1668 | switch (msp->opmode) { | |
1669 | case OPMODE_MANUAL: | |
1670 | printk(" mode=manual"); | |
1671 | thread_func = msp3400c_thread; | |
1672 | break; | |
1673 | case OPMODE_SIMPLE: | |
1674 | printk(" mode=simple"); | |
1675 | thread_func = msp3410d_thread; | |
1676 | break; | |
1677 | case OPMODE_SIMPLER: | |
1678 | printk(" mode=simpler"); | |
1679 | thread_func = msp34xxg_thread; | |
1680 | break; | |
1681 | } | |
1682 | printk("\n"); | |
1683 | ||
1684 | /* startup control thread if needed */ | |
1685 | if (thread_func) { | |
1686 | msp->kthread = kthread_run(thread_func, client, "msp34xx"); | |
1687 | ||
1688 | if (NULL == msp->kthread) | |
1689 | msp3400_warn("kernel_thread() failed\n"); | |
1690 | msp_wake_thread(client); | |
1691 | } | |
1692 | ||
1693 | /* done */ | |
1694 | i2c_attach_client(client); | |
1695 | ||
1696 | /* update our own array */ | |
1697 | for (i = 0; i < MSP3400_MAX; i++) { | |
1698 | if (NULL == msps[i]) { | |
1699 | msps[i] = client; | |
1700 | break; | |
1701 | } | |
1702 | } | |
1703 | ||
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | static int msp_detach(struct i2c_client *client) | |
1708 | { | |
1709 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1710 | int i; | |
1711 | ||
1712 | /* shutdown control thread */ | |
1713 | if (msp->kthread) { | |
1714 | msp->restart = 1; | |
1715 | kthread_stop(msp->kthread); | |
1716 | } | |
1717 | msp3400c_reset(client); | |
1718 | ||
1719 | /* update our own array */ | |
1720 | for (i = 0; i < MSP3400_MAX; i++) { | |
1721 | if (client == msps[i]) { | |
1722 | msps[i] = NULL; | |
1723 | break; | |
1724 | } | |
1725 | } | |
1726 | ||
1727 | i2c_detach_client(client); | |
1728 | ||
1729 | kfree(msp); | |
1730 | kfree(client); | |
1731 | return 0; | |
1732 | } | |
1733 | ||
1734 | static int msp_probe(struct i2c_adapter *adap) | |
1735 | { | |
1736 | if (adap->class & I2C_CLASS_TV_ANALOG) | |
1737 | return i2c_probe(adap, &addr_data, msp_attach); | |
1738 | return 0; | |
1739 | } | |
1740 | ||
1741 | static void msp_wake_thread(struct i2c_client *client) | |
1742 | { | |
1743 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1744 | ||
1745 | if (NULL == msp->kthread) | |
1746 | return; | |
1747 | msp3400c_setvolume(client,msp->muted,0,0); | |
1748 | msp->watch_stereo = 0; | |
1749 | msp->restart = 1; | |
1750 | wake_up_interruptible(&msp->wq); | |
1751 | } | |
1752 | ||
1753 | /* ----------------------------------------------------------------------- */ | |
1754 | ||
1755 | static int mode_v4l2_to_v4l1(int rxsubchans) | |
1756 | { | |
1757 | int mode = 0; | |
1758 | ||
1759 | if (rxsubchans & V4L2_TUNER_SUB_STEREO) | |
1760 | mode |= VIDEO_SOUND_STEREO; | |
1761 | if (rxsubchans & V4L2_TUNER_SUB_LANG2) | |
1762 | mode |= VIDEO_SOUND_LANG2; | |
1763 | if (rxsubchans & V4L2_TUNER_SUB_LANG1) | |
1764 | mode |= VIDEO_SOUND_LANG1; | |
1765 | if (0 == mode) | |
1766 | mode |= VIDEO_SOUND_MONO; | |
1767 | return mode; | |
1768 | } | |
1769 | ||
1770 | static int mode_v4l1_to_v4l2(int mode) | |
1771 | { | |
1772 | if (mode & VIDEO_SOUND_STEREO) | |
1773 | return V4L2_TUNER_MODE_STEREO; | |
1774 | if (mode & VIDEO_SOUND_LANG2) | |
1775 | return V4L2_TUNER_MODE_LANG2; | |
1776 | if (mode & VIDEO_SOUND_LANG1) | |
1777 | return V4L2_TUNER_MODE_LANG1; | |
1778 | return V4L2_TUNER_MODE_MONO; | |
1779 | } | |
1780 | ||
1781 | static void msp_any_detect_stereo(struct i2c_client *client) | |
1782 | { | |
1783 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1784 | ||
1785 | switch (msp->opmode) { | |
1786 | case OPMODE_MANUAL: | |
1787 | case OPMODE_SIMPLE: | |
1788 | autodetect_stereo(client); | |
1789 | break; | |
1790 | case OPMODE_SIMPLER: | |
1791 | msp34xxg_detect_stereo(client); | |
1792 | break; | |
1793 | } | |
1794 | } | |
1795 | ||
1796 | static void msp_any_set_audmode(struct i2c_client *client, int audmode) | |
1797 | { | |
1798 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1799 | ||
1800 | switch (msp->opmode) { | |
1801 | case OPMODE_MANUAL: | |
1802 | case OPMODE_SIMPLE: | |
1803 | msp->watch_stereo = 0; | |
1804 | msp3400c_setstereo(client, audmode); | |
1805 | break; | |
1806 | case OPMODE_SIMPLER: | |
1807 | msp34xxg_set_audmode(client, audmode); | |
1808 | break; | |
1809 | } | |
1810 | } | |
1811 | ||
1812 | ||
1813 | static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg) | |
1814 | { | |
1815 | struct msp3400c *msp = i2c_get_clientdata(client); | |
1816 | __u16 *sarg = arg; | |
1817 | int scart = 0; | |
1818 | ||
1819 | switch (cmd) { | |
1820 | ||
1821 | case AUDC_SET_INPUT: | |
1822 | msp3400_dbg("AUDC_SET_INPUT(%d)\n",*sarg); | |
1823 | ||
1824 | if (*sarg == msp->input) | |
1825 | break; | |
1826 | msp->input = *sarg; | |
1827 | switch (*sarg) { | |
1828 | case AUDIO_RADIO: | |
1829 | /* Hauppauge uses IN2 for the radio */ | |
1830 | msp->mode = MSP_MODE_FM_RADIO; | |
1831 | scart = SCART_IN2; | |
1832 | break; | |
1833 | case AUDIO_EXTERN_1: | |
1834 | /* IN1 is often used for external input ... */ | |
1835 | msp->mode = MSP_MODE_EXTERN; | |
1836 | scart = SCART_IN1; | |
1837 | break; | |
1838 | case AUDIO_EXTERN_2: | |
1839 | /* ... sometimes it is IN2 through ;) */ | |
1840 | msp->mode = MSP_MODE_EXTERN; | |
1841 | scart = SCART_IN2; | |
1842 | break; | |
1843 | case AUDIO_TUNER: | |
1844 | msp->mode = -1; | |
1845 | break; | |
1846 | default: | |
1847 | if (*sarg & AUDIO_MUTE) | |
1848 | msp3400c_set_scart(client,SCART_MUTE,0); | |
1849 | break; | |
1850 | } | |
1851 | if (scart) { | |
1852 | msp->rxsubchans = V4L2_TUNER_SUB_STEREO; | |
1853 | msp->audmode = V4L2_TUNER_MODE_STEREO; | |
1854 | msp3400c_set_scart(client,scart,0); | |
1855 | msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900); | |
1856 | if (msp->opmode != OPMODE_SIMPLER) | |
1857 | msp3400c_setstereo(client, msp->audmode); | |
1858 | } | |
1859 | msp_wake_thread(client); | |
1860 | break; | |
1861 | ||
1862 | case AUDC_SET_RADIO: | |
1863 | msp3400_dbg("AUDC_SET_RADIO\n"); | |
1864 | msp->norm = VIDEO_MODE_RADIO; | |
1865 | msp3400_dbg("switching to radio mode\n"); | |
1866 | msp->watch_stereo = 0; | |
1867 | switch (msp->opmode) { | |
1868 | case OPMODE_MANUAL: | |
1869 | /* set msp3400 to FM radio mode */ | |
1870 | msp3400c_setmode(client,MSP_MODE_FM_RADIO); | |
1871 | msp3400c_setcarrier(client, MSP_CARRIER(10.7), | |
1872 | MSP_CARRIER(10.7)); | |
1873 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1874 | break; | |
1875 | case OPMODE_SIMPLE: | |
1876 | case OPMODE_SIMPLER: | |
1877 | /* the thread will do for us */ | |
1878 | msp_wake_thread(client); | |
1879 | break; | |
1880 | } | |
1881 | break; | |
1882 | /* work-in-progress: hook to control the DFP registers */ | |
1883 | case MSP_SET_DFPREG: | |
1884 | { | |
1885 | struct msp_dfpreg *r = arg; | |
1886 | int i; | |
1887 | ||
1888 | if (r->reg < 0 || r->reg >= DFP_COUNT) | |
1889 | return -EINVAL; | |
1890 | for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++) | |
1891 | if (r->reg == bl_dfp[i]) | |
1892 | return -EINVAL; | |
1893 | msp->dfp_regs[r->reg] = r->value; | |
1894 | msp3400c_write(client, I2C_MSP3400C_DFP, r->reg, r->value); | |
1895 | return 0; | |
1896 | } | |
1897 | case MSP_GET_DFPREG: | |
1898 | { | |
1899 | struct msp_dfpreg *r = arg; | |
1900 | ||
1901 | if (r->reg < 0 || r->reg >= DFP_COUNT) | |
1902 | return -EINVAL; | |
1903 | r->value = msp3400c_read(client, I2C_MSP3400C_DFP, r->reg); | |
1904 | return 0; | |
1905 | } | |
1906 | ||
1907 | /* --- v4l ioctls --- */ | |
1908 | /* take care: bttv does userspace copying, we'll get a | |
1909 | kernel pointer here... */ | |
1910 | case VIDIOCGAUDIO: | |
1911 | { | |
1912 | struct video_audio *va = arg; | |
1913 | ||
1914 | msp3400_dbg("VIDIOCGAUDIO\n"); | |
1915 | va->flags |= VIDEO_AUDIO_VOLUME | | |
1916 | VIDEO_AUDIO_BASS | | |
1917 | VIDEO_AUDIO_TREBLE | | |
1918 | VIDEO_AUDIO_MUTABLE; | |
1919 | if (msp->muted) | |
1920 | va->flags |= VIDEO_AUDIO_MUTE; | |
1921 | ||
1922 | if (msp->muted) | |
1923 | va->flags |= VIDEO_AUDIO_MUTE; | |
1924 | va->volume = MAX(msp->left, msp->right); | |
1925 | va->balance = (32768 * MIN(msp->left, msp->right)) / | |
1926 | (va->volume ? va->volume : 1); | |
1927 | va->balance = (msp->left < msp->right) ? | |
1928 | (65535 - va->balance) : va->balance; | |
1929 | if (0 == va->volume) | |
1930 | va->balance = 32768; | |
1931 | va->bass = msp->bass; | |
1932 | va->treble = msp->treble; | |
1933 | ||
1934 | msp_any_detect_stereo(client); | |
1935 | va->mode = mode_v4l2_to_v4l1(msp->rxsubchans); | |
1936 | break; | |
1937 | } | |
1938 | case VIDIOCSAUDIO: | |
1939 | { | |
1940 | struct video_audio *va = arg; | |
1941 | ||
1942 | msp3400_dbg("VIDIOCSAUDIO\n"); | |
1943 | msp->muted = (va->flags & VIDEO_AUDIO_MUTE); | |
1944 | msp->left = (MIN(65536 - va->balance, 32768) * | |
1945 | va->volume) / 32768; | |
1946 | msp->right = (MIN(va->balance, 32768) * va->volume) / 32768; | |
1947 | msp->bass = va->bass; | |
1948 | msp->treble = va->treble; | |
1949 | msp3400_dbg("VIDIOCSAUDIO setting va->volume to %d\n", | |
1950 | va->volume); | |
1951 | msp3400_dbg("VIDIOCSAUDIO setting va->balance to %d\n", | |
1952 | va->balance); | |
1953 | msp3400_dbg("VIDIOCSAUDIO setting va->flags to %d\n", | |
1954 | va->flags); | |
1955 | msp3400_dbg("VIDIOCSAUDIO setting msp->left to %d\n", | |
1956 | msp->left); | |
1957 | msp3400_dbg("VIDIOCSAUDIO setting msp->right to %d\n", | |
1958 | msp->right); | |
1959 | msp3400_dbg("VIDIOCSAUDIO setting msp->bass to %d\n", | |
1960 | msp->bass); | |
1961 | msp3400_dbg("VIDIOCSAUDIO setting msp->treble to %d\n", | |
1962 | msp->treble); | |
1963 | msp3400_dbg("VIDIOCSAUDIO setting msp->mode to %d\n", | |
1964 | msp->mode); | |
1965 | msp3400c_setvolume(client, msp->muted, msp->left, msp->right); | |
1966 | msp3400c_setbass(client, msp->bass); | |
1967 | msp3400c_settreble(client, msp->treble); | |
1968 | ||
1969 | if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO) | |
1970 | msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode)); | |
1971 | break; | |
1972 | } | |
1973 | ||
1974 | case VIDIOCSCHAN: | |
1975 | { | |
1976 | struct video_channel *vc = arg; | |
1977 | ||
1978 | msp3400_dbg("VIDIOCSCHAN (norm=%d)\n",vc->norm); | |
1979 | msp->norm = vc->norm; | |
1980 | msp_wake_thread(client); | |
1981 | break; | |
1982 | } | |
1983 | ||
1984 | case VIDIOCSFREQ: | |
1985 | case VIDIOC_S_FREQUENCY: | |
1986 | { | |
1987 | /* new channel -- kick audio carrier scan */ | |
1988 | msp3400_dbg("VIDIOCSFREQ\n"); | |
1989 | msp_wake_thread(client); | |
1990 | break; | |
1991 | } | |
1992 | ||
1993 | /* msp34xx specific */ | |
1994 | case MSP_SET_MATRIX: | |
1995 | { | |
1996 | struct msp_matrix *mspm = arg; | |
1997 | ||
1998 | msp3400_dbg("MSP_SET_MATRIX\n"); | |
1999 | msp3400c_set_scart(client, mspm->input, mspm->output); | |
2000 | break; | |
2001 | } | |
2002 | ||
2003 | /* --- v4l2 ioctls --- */ | |
2004 | case VIDIOC_S_STD: | |
2005 | { | |
2006 | v4l2_std_id *id = arg; | |
2007 | ||
2008 | /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/ | |
2009 | if (*id & V4L2_STD_PAL) { | |
2010 | msp->norm=VIDEO_MODE_PAL; | |
2011 | } else if (*id & V4L2_STD_SECAM) { | |
2012 | msp->norm=VIDEO_MODE_SECAM; | |
2013 | } else { | |
2014 | msp->norm=VIDEO_MODE_NTSC; | |
2015 | } | |
2016 | ||
2017 | msp_wake_thread(client); | |
2018 | return 0; | |
2019 | } | |
2020 | ||
2021 | case VIDIOC_ENUMINPUT: | |
2022 | { | |
2023 | struct v4l2_input *i = arg; | |
2024 | ||
2025 | if (i->index != 0) | |
2026 | return -EINVAL; | |
2027 | ||
2028 | i->type = V4L2_INPUT_TYPE_TUNER; | |
2029 | switch (i->index) { | |
2030 | case AUDIO_RADIO: | |
2031 | strcpy(i->name,"Radio"); | |
2032 | break; | |
2033 | case AUDIO_EXTERN_1: | |
2034 | strcpy(i->name,"Extern 1"); | |
2035 | break; | |
2036 | case AUDIO_EXTERN_2: | |
2037 | strcpy(i->name,"Extern 2"); | |
2038 | break; | |
2039 | case AUDIO_TUNER: | |
2040 | strcpy(i->name,"Television"); | |
2041 | break; | |
2042 | default: | |
2043 | return -EINVAL; | |
2044 | } | |
2045 | return 0; | |
2046 | } | |
2047 | ||
2048 | case VIDIOC_G_AUDIO: | |
2049 | { | |
2050 | struct v4l2_audio *a = arg; | |
2051 | ||
2052 | memset(a,0,sizeof(*a)); | |
2053 | ||
2054 | switch (a->index) { | |
2055 | case AUDIO_RADIO: | |
2056 | strcpy(a->name,"Radio"); | |
2057 | break; | |
2058 | case AUDIO_EXTERN_1: | |
2059 | strcpy(a->name,"Extern 1"); | |
2060 | break; | |
2061 | case AUDIO_EXTERN_2: | |
2062 | strcpy(a->name,"Extern 2"); | |
2063 | break; | |
2064 | case AUDIO_TUNER: | |
2065 | strcpy(a->name,"Television"); | |
2066 | break; | |
2067 | default: | |
2068 | return -EINVAL; | |
2069 | } | |
2070 | ||
2071 | msp_any_detect_stereo(client); | |
2072 | if (msp->audmode == V4L2_TUNER_MODE_STEREO) { | |
2073 | a->capability=V4L2_AUDCAP_STEREO; | |
2074 | } | |
2075 | ||
2076 | break; | |
2077 | } | |
2078 | case VIDIOC_S_AUDIO: | |
2079 | { | |
2080 | struct v4l2_audio *sarg = arg; | |
2081 | ||
2082 | switch (sarg->index) { | |
2083 | case AUDIO_RADIO: | |
2084 | /* Hauppauge uses IN2 for the radio */ | |
2085 | msp->mode = MSP_MODE_FM_RADIO; | |
2086 | scart = SCART_IN2; | |
2087 | break; | |
2088 | case AUDIO_EXTERN_1: | |
2089 | /* IN1 is often used for external input ... */ | |
2090 | msp->mode = MSP_MODE_EXTERN; | |
2091 | scart = SCART_IN1; | |
2092 | break; | |
2093 | case AUDIO_EXTERN_2: | |
2094 | /* ... sometimes it is IN2 through ;) */ | |
2095 | msp->mode = MSP_MODE_EXTERN; | |
2096 | scart = SCART_IN2; | |
2097 | break; | |
2098 | case AUDIO_TUNER: | |
2099 | msp->mode = -1; | |
2100 | break; | |
2101 | } | |
2102 | if (scart) { | |
2103 | msp->rxsubchans = V4L2_TUNER_SUB_STEREO; | |
2104 | msp->audmode = V4L2_TUNER_MODE_STEREO; | |
2105 | msp3400c_set_scart(client,scart,0); | |
2106 | msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900); | |
2107 | } | |
2108 | if (sarg->capability==V4L2_AUDCAP_STEREO) { | |
2109 | msp->audmode = V4L2_TUNER_MODE_STEREO; | |
2110 | } else { | |
2111 | msp->audmode &= ~V4L2_TUNER_MODE_STEREO; | |
2112 | } | |
2113 | msp_any_set_audmode(client, msp->audmode); | |
2114 | msp_wake_thread(client); | |
2115 | break; | |
2116 | } | |
2117 | case VIDIOC_G_TUNER: | |
2118 | { | |
2119 | struct v4l2_tuner *vt = arg; | |
2120 | ||
2121 | msp_any_detect_stereo(client); | |
2122 | vt->audmode = msp->audmode; | |
2123 | vt->rxsubchans = msp->rxsubchans; | |
2124 | vt->capability = V4L2_TUNER_CAP_STEREO | | |
2125 | V4L2_TUNER_CAP_LANG1| | |
2126 | V4L2_TUNER_CAP_LANG2; | |
2127 | break; | |
2128 | } | |
2129 | case VIDIOC_S_TUNER: | |
2130 | { | |
2131 | struct v4l2_tuner *vt=(struct v4l2_tuner *)arg; | |
2132 | ||
2133 | /* only set audmode */ | |
2134 | if (vt->audmode != -1 && vt->audmode != 0) | |
2135 | msp_any_set_audmode(client, vt->audmode); | |
2136 | break; | |
2137 | } | |
2138 | ||
2139 | case VIDIOC_G_AUDOUT: | |
2140 | { | |
2141 | struct v4l2_audioout *a=(struct v4l2_audioout *)arg; | |
2142 | int idx=a->index; | |
2143 | ||
2144 | memset(a,0,sizeof(*a)); | |
2145 | ||
2146 | switch (idx) { | |
2147 | case 0: | |
2148 | strcpy(a->name,"Scart1 Out"); | |
2149 | break; | |
2150 | case 1: | |
2151 | strcpy(a->name,"Scart2 Out"); | |
2152 | break; | |
2153 | case 2: | |
2154 | strcpy(a->name,"I2S Out"); | |
2155 | break; | |
2156 | default: | |
2157 | return -EINVAL; | |
2158 | } | |
2159 | break; | |
2160 | ||
2161 | } | |
2162 | case VIDIOC_S_AUDOUT: | |
2163 | { | |
2164 | struct v4l2_audioout *a=(struct v4l2_audioout *)arg; | |
2165 | ||
2166 | if (a->index<0||a->index>2) | |
2167 | return -EINVAL; | |
2168 | ||
2169 | if (a->index==2) { | |
2170 | if (a->mode == V4L2_AUDMODE_32BITS) | |
2171 | msp->i2s_mode=1; | |
2172 | else | |
2173 | msp->i2s_mode=0; | |
2174 | } | |
2175 | msp3400_dbg("Setting audio out on msp34xx to input %i, mode %i\n", | |
2176 | a->index,msp->i2s_mode); | |
2177 | msp3400c_set_scart(client,msp->in_scart,a->index+1); | |
2178 | ||
2179 | break; | |
2180 | } | |
2181 | ||
2182 | default: | |
2183 | /* nothing */ | |
2184 | break; | |
2185 | } | |
2186 | return 0; | |
2187 | } | |
2188 | ||
2189 | static int msp_suspend(struct device * dev, pm_message_t state) | |
2190 | { | |
2191 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); | |
2192 | ||
2193 | msp3400_dbg("msp34xx: suspend\n"); | |
2194 | msp3400c_reset(client); | |
2195 | return 0; | |
2196 | } | |
2197 | ||
2198 | static int msp_resume(struct device * dev) | |
2199 | { | |
2200 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); | |
2201 | ||
2202 | msp3400_dbg("msp34xx: resume\n"); | |
2203 | msp_wake_thread(client); | |
2204 | return 0; | |
2205 | } | |
2206 | ||
2207 | /* ----------------------------------------------------------------------- */ | |
2208 | ||
2209 | static int __init msp3400_init_module(void) | |
2210 | { | |
2211 | return i2c_add_driver(&driver); | |
2212 | } | |
2213 | ||
2214 | static void __exit msp3400_cleanup_module(void) | |
2215 | { | |
2216 | i2c_del_driver(&driver); | |
2217 | } | |
2218 | ||
2219 | module_init(msp3400_init_module); | |
2220 | module_exit(msp3400_cleanup_module); | |
2221 | ||
2222 | /* | |
2223 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
2224 | * --------------------------------------------------------------------------- | |
2225 | * Local variables: | |
2226 | * c-basic-offset: 8 | |
2227 | * End: | |
2228 | */ |