aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/media/video/msp3400.c1406
1 files changed, 709 insertions, 697 deletions
diff --git a/drivers/media/video/msp3400.c b/drivers/media/video/msp3400.c
index 11235c1ac5c6..39058d3b2e61 100644
--- a/drivers/media/video/msp3400.c
+++ b/drivers/media/video/msp3400.c
@@ -59,21 +59,60 @@
59 59
60/* ---------------------------------------------------------------------- */ 60/* ---------------------------------------------------------------------- */
61 61
62#define I2C_MSP3400C 0x80 62MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
63#define I2C_MSP3400C_ALT 0x88 63MODULE_AUTHOR("Gerd Knorr");
64MODULE_LICENSE("GPL");
64 65
65#define I2C_MSP3400C_DEM 0x10 66#define OPMODE_AUTO -1
66#define I2C_MSP3400C_DFP 0x12 67#define OPMODE_MANUAL 0
68#define OPMODE_AUTODETECT 1 /* use autodetect (>= msp3410 only) */
69#define OPMODE_AUTOSELECT 2 /* use autodetect & autoselect (>= msp34xxG) */
67 70
68/* Addresses to scan */ 71/* module parameters */
69static unsigned short normal_i2c[] = { 72static int opmode = OPMODE_AUTO;
70 I2C_MSP3400C >> 1, 73static int debug = 0; /* debug output */
71 I2C_MSP3400C_ALT >> 1, 74static int once = 0; /* no continous stereo monitoring */
72 I2C_CLIENT_END 75static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
73}; 76 the autoscan seems work well only with FM... */
74I2C_CLIENT_INSMOD; 77static int standard = 1; /* Override auto detect of audio standard, if needed. */
78static int dolby = 0;
79
80static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
81 (msp34xxg only) 0x00a0-0x03c0 */
75 82
76#define msp3400_dbg(fmt, arg...) \ 83/* read-only */
84module_param(opmode, int, 0444);
85
86/* read-write */
87module_param(once, int, 0644);
88module_param(debug, int, 0644);
89module_param(stereo_threshold, int, 0644);
90module_param(standard, int, 0644);
91module_param(amsound, int, 0644);
92module_param(dolby, int, 0644);
93
94MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
95MODULE_PARM_DESC(once, "No continuous stereo monitoring");
96MODULE_PARM_DESC(debug, "Enable debug messages");
97MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
98MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
99MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
100MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
101
102/* ---------------------------------------------------------------------- */
103
104#define msp3400_err(fmt, arg...) do { \
105 printk(KERN_ERR "%s %d-%04x: " fmt, client->driver->driver.name, \
106 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
107#define msp3400_warn(fmt, arg...) do { \
108 printk(KERN_WARNING "%s %d-%04x: " fmt, client->driver->driver.name, \
109 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
110#define msp3400_info(fmt, arg...) do { \
111 printk(KERN_INFO "%s %d-%04x: " fmt, client->driver->driver.name, \
112 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
113
114/* level 1 debug. */
115#define msp_dbg1(fmt, arg...) \
77 do { \ 116 do { \
78 if (debug) \ 117 if (debug) \
79 printk(KERN_INFO "%s debug %d-%04x: " fmt, \ 118 printk(KERN_INFO "%s debug %d-%04x: " fmt, \
@@ -81,8 +120,8 @@ I2C_CLIENT_INSMOD;
81 i2c_adapter_id(client->adapter), client->addr , ## arg); \ 120 i2c_adapter_id(client->adapter), client->addr , ## arg); \
82 } while (0) 121 } while (0)
83 122
84/* Medium volume debug. */ 123/* level 2 debug. */
85#define msp3400_dbg_mediumvol(fmt, arg...) \ 124#define msp_dbg2(fmt, arg...) \
86 do { \ 125 do { \
87 if (debug >= 2) \ 126 if (debug >= 2) \
88 printk(KERN_INFO "%s debug %d-%04x: " fmt, \ 127 printk(KERN_INFO "%s debug %d-%04x: " fmt, \
@@ -90,8 +129,8 @@ I2C_CLIENT_INSMOD;
90 i2c_adapter_id(client->adapter), client->addr , ## arg); \ 129 i2c_adapter_id(client->adapter), client->addr , ## arg); \
91 } while (0) 130 } while (0)
92 131
93/* High volume debug. Use with care. */ 132/* level 3 debug. Use with care. */
94#define msp3400_dbg_highvol(fmt, arg...) \ 133#define msp_dbg3(fmt, arg...) \
95 do { \ 134 do { \
96 if (debug >= 16) \ 135 if (debug >= 16) \
97 printk(KERN_INFO "%s debug %d-%04x: " fmt, \ 136 printk(KERN_INFO "%s debug %d-%04x: " fmt, \
@@ -99,45 +138,33 @@ I2C_CLIENT_INSMOD;
99 i2c_adapter_id(client->adapter), client->addr , ## arg); \ 138 i2c_adapter_id(client->adapter), client->addr , ## arg); \
100 } while (0) 139 } while (0)
101 140
102#define msp3400_err(fmt, arg...) do { \ 141/* control subaddress */
103 printk(KERN_ERR "%s %d-%04x: " fmt, client->driver->driver.name, \ 142#define I2C_MSP_CONTROL 0x00
104 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) 143/* demodulator unit subaddress */
105#define msp3400_warn(fmt, arg...) do { \ 144#define I2C_MSP_DEM 0x10
106 printk(KERN_WARNING "%s %d-%04x: " fmt, client->driver->driver.name, \ 145/* DSP unit subaddress */
107 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) 146#define I2C_MSP_DSP 0x12
108#define msp3400_info(fmt, arg...) do { \ 147
109 printk(KERN_INFO "%s %d-%04x: " fmt, client->driver->driver.name, \ 148/* Addresses to scan */
110 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0) 149static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
150>>>>>>> remote
111 151
112#define OPMODE_AUTO -1
113#define OPMODE_MANUAL 0
114#define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
115#define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
116 152
117/* insmod parameters */ 153I2C_CLIENT_INSMOD;
118static int opmode = OPMODE_AUTO;
119static int debug = 0; /* debug output */
120static int once = 0; /* no continous stereo monitoring */
121static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
122 the autoscan seems work well only with FM... */
123static int standard = 1; /* Override auto detect of audio standard, if needed. */
124static int dolby = 0;
125 154
126static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
127 (msp34xxg only) 0x00a0-0x03c0 */
128#define DFP_COUNT 0x41 155#define DFP_COUNT 0x41
129static const int bl_dfp[] = { 156static const int bl_dfp[] = {
130 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a, 157 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
131 0x0b, 0x0d, 0x0e, 0x10 158 0x0b, 0x0d, 0x0e, 0x10
132}; 159};
133 160
134#define IS_MSP34XX_G(msp) ((msp)->opmode==2) 161#define HAVE_NICAM(state) (((state->rev2>>8) & 0xff) != 00)
162#define HAVE_RADIO(state) ((state->rev1 & 0x0f) >= 'G'-'@')
135 163
136struct msp3400c { 164struct msp_state {
137 int rev1,rev2; 165 int rev1, rev2;
138 166
139 int opmode; 167 int opmode;
140 int nicam;
141 int mode; 168 int mode;
142 int norm; 169 int norm;
143 int stereo; 170 int stereo;
@@ -167,49 +194,19 @@ struct msp3400c {
167 int watch_stereo:1; 194 int watch_stereo:1;
168}; 195};
169 196
170#define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
171#define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
172#define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
173#define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
174
175#define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */ 197#define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
176 198
177/* ---------------------------------------------------------------------- */
178
179/* read-only */
180module_param(opmode, int, 0444);
181
182/* read-write */
183module_param(once, int, 0644);
184module_param(debug, int, 0644);
185module_param(stereo_threshold, int, 0644);
186module_param(standard, int, 0644);
187module_param(amsound, int, 0644);
188module_param(dolby, int, 0644);
189
190MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
191MODULE_PARM_DESC(once, "No continuous stereo monitoring");
192MODULE_PARM_DESC(debug, "Enable debug messages");
193MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
194MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
195MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
196MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
197
198
199MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
200MODULE_AUTHOR("Gerd Knorr");
201MODULE_LICENSE("GPL");
202 199
203/* ----------------------------------------------------------------------- */ 200/* ----------------------------------------------------------------------- */
204/* functions for talking to the MSP3400C Sound processor */ 201/* functions for talking to the MSP3400C Sound processor */
205 202
206static int msp3400c_reset(struct i2c_client *client) 203static int msp_reset(struct i2c_client *client)
207{ 204{
208 /* reset and read revision code */ 205 /* reset and read revision code */
209 static char reset_off[3] = { 0x00, 0x80, 0x00 }; 206 static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
210 static char reset_on[3] = { 0x00, 0x00, 0x00 }; 207 static u8 reset_on[3] = { I2C_MSP_CONTROL, 0x00, 0x00 };
211 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e }; 208 static u8 write[3] = { I2C_MSP_DSP + 1, 0x00, 0x1e };
212 char read[2]; 209 u8 read[2];
213 struct i2c_msg reset[2] = { 210 struct i2c_msg reset[2] = {
214 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off }, 211 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
215 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on }, 212 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
@@ -219,54 +216,62 @@ static int msp3400c_reset(struct i2c_client *client)
219 { client->addr, I2C_M_RD, 2, read }, 216 { client->addr, I2C_M_RD, 2, read },
220 }; 217 };
221 218
222 msp3400_dbg_highvol("msp3400c_reset\n"); 219 msp_dbg3("msp_reset\n");
223 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) || 220 if (1 != i2c_transfer(client->adapter, &reset[0], 1) ||
224 (1 != i2c_transfer(client->adapter,&reset[1],1)) || 221 1 != i2c_transfer(client->adapter, &reset[1], 1) ||
225 (2 != i2c_transfer(client->adapter,test,2)) ) { 222 2 != i2c_transfer(client->adapter, test, 2)) {
226 msp3400_err("chip reset failed\n"); 223 msp_err("chip reset failed\n");
227 return -1; 224 return -1;
228 } 225 }
229 return 0; 226 return 0;
230} 227}
231 228
232static int msp3400c_read(struct i2c_client *client, int dev, int addr) 229static int msp_read(struct i2c_client *client, int dev, int addr)
233{ 230{
234 int err,retval; 231 int err, retval;
235 232 u8 write[3];
236 unsigned char write[3]; 233 u8 read[2];
237 unsigned char read[2];
238 struct i2c_msg msgs[2] = { 234 struct i2c_msg msgs[2] = {
239 { client->addr, 0, 3, write }, 235 { client->addr, 0, 3, write },
240 { client->addr, I2C_M_RD, 2, read } 236 { client->addr, I2C_M_RD, 2, read }
241 }; 237 };
242 238
243 write[0] = dev+1; 239 write[0] = dev + 1;
244 write[1] = addr >> 8; 240 write[1] = addr >> 8;
245 write[2] = addr & 0xff; 241 write[2] = addr & 0xff;
246 242
247 for (err = 0; err < 3;) { 243 for (err = 0; err < 3; err++) {
248 if (2 == i2c_transfer(client->adapter,msgs,2)) 244 if (2 == i2c_transfer(client->adapter, msgs, 2))
249 break; 245 break;
250 err++; 246 msp_warn("I/O error #%d (read 0x%02x/0x%02x)\n", err,
251 msp3400_warn("I/O error #%d (read 0x%02x/0x%02x)\n", err,
252 dev, addr); 247 dev, addr);
253 current->state = TASK_INTERRUPTIBLE; 248 current->state = TASK_INTERRUPTIBLE;
254 schedule_timeout(msecs_to_jiffies(10)); 249 schedule_timeout(msecs_to_jiffies(10));
255 } 250 }
256 if (3 == err) { 251 if (3 == err) {
257 msp3400_warn("giving up, resetting chip. Sound will go off, sorry folks :-|\n"); 252 msp_warn("giving up, resetting chip. Sound will go off, sorry folks :-|\n");
258 msp3400c_reset(client); 253 msp_reset(client);
259 return -1; 254 return -1;
260 } 255 }
261 retval = read[0] << 8 | read[1]; 256 retval = read[0] << 8 | read[1];
262 msp3400_dbg_highvol("msp3400c_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval); 257 msp_dbg3("msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
263 return retval; 258 return retval;
264} 259}
265 260
266static int msp3400c_write(struct i2c_client *client, int dev, int addr, int val) 261static inline int msp_read_dem(struct i2c_client *client, int addr)
262{
263 return msp_read(client, I2C_MSP_DEM, addr);
264}
265
266static inline int msp_read_dsp(struct i2c_client *client, int addr)
267{
268 return msp_read(client, I2C_MSP_DSP, addr);
269}
270
271static int msp_write(struct i2c_client *client, int dev, int addr, int val)
267{ 272{
268 int err; 273 int err;
269 unsigned char buffer[5]; 274 u8 buffer[5];
270 275
271 buffer[0] = dev; 276 buffer[0] = dev;
272 buffer[1] = addr >> 8; 277 buffer[1] = addr >> 8;
@@ -274,29 +279,38 @@ static int msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
274 buffer[3] = val >> 8; 279 buffer[3] = val >> 8;
275 buffer[4] = val & 0xff; 280 buffer[4] = val & 0xff;
276 281
277 msp3400_dbg_highvol("msp3400c_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val); 282 msp_dbg3("msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
278 for (err = 0; err < 3;) { 283 for (err = 0; err < 3; err++) {
279 if (5 == i2c_master_send(client, buffer, 5)) 284 if (5 == i2c_master_send(client, buffer, 5))
280 break; 285 break;
281 err++; 286 msp_warn("I/O error #%d (write 0x%02x/0x%02x)\n", err,
282 msp3400_warn("I/O error #%d (write 0x%02x/0x%02x)\n", err,
283 dev, addr); 287 dev, addr);
284 current->state = TASK_INTERRUPTIBLE; 288 current->state = TASK_INTERRUPTIBLE;
285 schedule_timeout(msecs_to_jiffies(10)); 289 schedule_timeout(msecs_to_jiffies(10));
286 } 290 }
287 if (3 == err) { 291 if (3 == err) {
288 msp3400_warn("giving up, reseting chip. Sound will go off, sorry folks :-|\n"); 292 msp_warn("giving up, resetting chip. Sound will go off, sorry folks :-|\n");
289 msp3400c_reset(client); 293 msp_reset(client);
290 return -1; 294 return -1;
291 } 295 }
292 return 0; 296 return 0;
293} 297}
294 298
299static inline int msp_write_dem(struct i2c_client *client, int addr, int val)
300{
301 return msp_write(client, I2C_MSP_DEM, addr, val);
302}
303
304static inline int msp_write_dsp(struct i2c_client *client, int addr, int val)
305{
306 return msp_write(client, I2C_MSP_DSP, addr, val);
307}
308
295/* ------------------------------------------------------------------------ */ 309/* ------------------------------------------------------------------------ */
296 310
297/* This macro is allowed for *constants* only, gcc must calculate it 311/* This macro is allowed for *constants* only, gcc must calculate it
298 at compile time. Remember -- no floats in kernel mode */ 312 at compile time. Remember -- no floats in kernel mode */
299#define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24))) 313#define MSP_CARRIER(freq) ((int)((float)(freq / 18.432) * (1 << 24)))
300 314
301#define MSP_MODE_AM_DETECT 0 315#define MSP_MODE_AM_DETECT 0
302#define MSP_MODE_FM_RADIO 2 316#define MSP_MODE_FM_RADIO 2
@@ -433,41 +447,41 @@ static char *scart_names[] = {
433 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute" 447 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
434}; 448};
435 449
436static void msp3400c_set_scart(struct i2c_client *client, int in, int out) 450static void msp_set_scart(struct i2c_client *client, int in, int out)
437{ 451{
438 struct msp3400c *msp = i2c_get_clientdata(client); 452 struct msp_state *state = i2c_get_clientdata(client);
439 453
440 msp->in_scart=in; 454 state->in_scart=in;
441 455
442 if (in >= 1 && in <= 8 && out >= 0 && out <= 2) { 456 if (in >= 1 && in <= 8 && out >= 0 && out <= 2) {
443 if (-1 == scarts[out][in]) 457 if (-1 == scarts[out][in])
444 return; 458 return;
445 459
446 msp->acb &= ~scarts[out][SCART_MASK]; 460 state->acb &= ~scarts[out][SCART_MASK];
447 msp->acb |= scarts[out][in]; 461 state->acb |= scarts[out][in];
448 } else 462 } else
449 msp->acb = 0xf60; /* Mute Input and SCART 1 Output */ 463 state->acb = 0xf60; /* Mute Input and SCART 1 Output */
450 464
451 msp3400_dbg("scart switch: %s => %d (ACB=0x%04x)\n", 465 msp_dbg1("scart switch: %s => %d (ACB=0x%04x)\n",
452 scart_names[in], out, msp->acb); 466 scart_names[in], out, state->acb);
453 msp3400c_write(client,I2C_MSP3400C_DFP, 0x13, msp->acb); 467 msp_write_dsp(client, 0x13, state->acb);
454 468
455 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */ 469 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
456 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); 470 msp_write_dem(client, 0x40, state->i2s_mode);
457} 471}
458 472
459/* ------------------------------------------------------------------------ */ 473/* ------------------------------------------------------------------------ */
460 474
461static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2) 475static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
462{ 476{
463 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff); 477 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
464 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12); 478 msp_write_dem(client, 0x009b, cdo1 >> 12);
465 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff); 479 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
466 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12); 480 msp_write_dem(client, 0x00ab, cdo2 >> 12);
467 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/ 481 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
468} 482}
469 483
470static void msp3400c_setvolume(struct i2c_client *client, 484static void msp_set_volume(struct i2c_client *client,
471 int muted, int left, int right) 485 int muted, int left, int right)
472 { 486 {
473 int vol = 0, val = 0, balance = 0; 487 int vol = 0, val = 0, balance = 0;
@@ -480,91 +494,91 @@ static void msp3400c_setvolume(struct i2c_client *client,
480 balance = ((right - left) * 127) / vol; 494 balance = ((right - left) * 127) / vol;
481 } 495 }
482 496
483 msp3400_dbg("setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n", 497 msp_dbg1("setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
484 muted ? "on" : "off", left, right, val >> 8, balance); 498 muted ? "on" : "off", left, right, val >> 8, balance);
485 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */ 499 msp_write_dsp(client, 0x0000, val); /* loudspeaker */
486 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */ 500 msp_write_dsp(client, 0x0006, val); /* headphones */
487 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, 501 msp_write_dsp(client, 0x0007,
488 muted ? 0x1 : (val | 0x1)); 502 muted ? 0x1 : (val | 0x1));
489 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0001, balance << 8); 503 msp_write_dsp(client, 0x0001, balance << 8);
490} 504}
491 505
492static void msp3400c_setbass(struct i2c_client *client, int bass) 506static void msp_set_bass(struct i2c_client *client, int bass)
493{ 507{
494 int val = ((bass-32768) * 0x60 / 65535) << 8; 508 int val = ((bass-32768) * 0x60 / 65535) << 8;
495 509
496 msp3400_dbg("setbass: %d 0x%02x\n", bass, val >> 8); 510 msp_dbg1("setbass: %d 0x%02x\n", bass, val >> 8);
497 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */ 511 msp_write_dsp(client, 0x0002, val); /* loudspeaker */
498} 512}
499 513
500static void msp3400c_settreble(struct i2c_client *client, int treble) 514static void msp_set_treble(struct i2c_client *client, int treble)
501{ 515{
502 int val = ((treble-32768) * 0x60 / 65535) << 8; 516 int val = ((treble-32768) * 0x60 / 65535) << 8;
503 517
504 msp3400_dbg("settreble: %d 0x%02x\n",treble, val>>8); 518 msp_dbg1("settreble: %d 0x%02x\n",treble, val>>8);
505 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */ 519 msp_write_dsp(client, 0x0003, val); /* loudspeaker */
506} 520}
507 521
508static void msp3400c_setmode(struct i2c_client *client, int type) 522static void msp3400c_setmode(struct i2c_client *client, int type)
509{ 523{
510 struct msp3400c *msp = i2c_get_clientdata(client); 524 struct msp_state *state = i2c_get_clientdata(client);
511 int i; 525 int i;
512 526
513 msp3400_dbg("setmode: %d\n",type); 527 msp_dbg1("setmode: %d\n",type);
514 msp->mode = type; 528 state->mode = type;
515 msp->audmode = V4L2_TUNER_MODE_MONO; 529 state->audmode = V4L2_TUNER_MODE_MONO;
516 msp->rxsubchans = V4L2_TUNER_SUB_MONO; 530 state->rxsubchans = V4L2_TUNER_SUB_MONO;
517 531
518 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */ 532 msp_write_dem(client, 0x00bb, /* ad_cv */
519 msp_init_data[type].ad_cv); 533 msp_init_data[type].ad_cv);
520 534
521 for (i = 5; i >= 0; i--) /* fir 1 */ 535 for (i = 5; i >= 0; i--) /* fir 1 */
522 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001, 536 msp_write_dem(client, 0x0001,
523 msp_init_data[type].fir1[i]); 537 msp_init_data[type].fir1[i]);
524 538
525 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */ 539 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
526 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040); 540 msp_write_dem(client, 0x0005, 0x0040);
527 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000); 541 msp_write_dem(client, 0x0005, 0x0000);
528 for (i = 5; i >= 0; i--) 542 for (i = 5; i >= 0; i--)
529 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 543 msp_write_dem(client, 0x0005,
530 msp_init_data[type].fir2[i]); 544 msp_init_data[type].fir2[i]);
531 545
532 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */ 546 msp_write_dem(client, 0x0083, /* MODE_REG */
533 msp_init_data[type].mode_reg); 547 msp_init_data[type].mode_reg);
534 548
535 msp3400c_setcarrier(client, msp_init_data[type].cdo1, 549 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
536 msp_init_data[type].cdo2); 550 msp_init_data[type].cdo2);
537 551
538 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/ 552 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
539 553
540 if (dolby) { 554 if (dolby) {
541 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008, 555 msp_write_dsp(client, 0x0008,
542 0x0520); /* I2S1 */ 556 0x0520); /* I2S1 */
543 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009, 557 msp_write_dsp(client, 0x0009,
544 0x0620); /* I2S2 */ 558 0x0620); /* I2S2 */
545 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b, 559 msp_write_dsp(client, 0x000b,
546 msp_init_data[type].dfp_src); 560 msp_init_data[type].dfp_src);
547 } else { 561 } else {
548 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008, 562 msp_write_dsp(client, 0x0008,
549 msp_init_data[type].dfp_src); 563 msp_init_data[type].dfp_src);
550 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009, 564 msp_write_dsp(client, 0x0009,
551 msp_init_data[type].dfp_src); 565 msp_init_data[type].dfp_src);
552 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b, 566 msp_write_dsp(client, 0x000b,
553 msp_init_data[type].dfp_src); 567 msp_init_data[type].dfp_src);
554 } 568 }
555 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a, 569 msp_write_dsp(client, 0x000a,
556 msp_init_data[type].dfp_src); 570 msp_init_data[type].dfp_src);
557 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 571 msp_write_dsp(client, 0x000e,
558 msp_init_data[type].dfp_matrix); 572 msp_init_data[type].dfp_matrix);
559 573
560 if (HAVE_NICAM(msp)) { 574 if (HAVE_NICAM(state)) {
561 /* nicam prescale */ 575 /* nicam prescale */
562 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */ 576 msp_write_dsp(client, 0x0010, 0x5a00); /* was: 0x3000 */
563 } 577 }
564} 578}
565 579
566/* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */ 580/* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
567static int best_video_sound(int rxsubchans) 581static int msp3400c_best_video_sound(int rxsubchans)
568{ 582{
569 if (rxsubchans & V4L2_TUNER_SUB_STEREO) 583 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
570 return V4L2_TUNER_MODE_STEREO; 584 return V4L2_TUNER_MODE_STEREO;
@@ -581,38 +595,37 @@ static void msp3400c_setstereo(struct i2c_client *client, int mode)
581 static char *strmode[] = { "0", "mono", "stereo", "3", 595 static char *strmode[] = { "0", "mono", "stereo", "3",
582 "lang1", "5", "6", "7", "lang2" 596 "lang1", "5", "6", "7", "lang2"
583 }; 597 };
584 struct msp3400c *msp = i2c_get_clientdata(client); 598 struct msp_state *state = i2c_get_clientdata(client);
585 int nicam = 0; /* channel source: FM/AM or nicam */ 599 int nicam = 0; /* channel source: FM/AM or nicam */
586 int src = 0; 600 int src = 0;
587 601
588 if (IS_MSP34XX_G(msp)) { 602 if (state->opmode == OPMODE_AUTOSELECT) {
589 /* this method would break everything, let's make sure 603 /* this method would break everything, let's make sure
590 * it's never called 604 * it's never called
591 */ 605 */
592 msp3400_dbg 606 msp_dbg1("setstereo called with mode=%d instead of set_source (ignored)\n",
593 ("DEBUG WARNING setstereo called with mode=%d instead of set_source (ignored)\n",
594 mode); 607 mode);
595 return; 608 return;
596 } 609 }
597 610
598 /* switch demodulator */ 611 /* switch demodulator */
599 switch (msp->mode) { 612 switch (state->mode) {
600 case MSP_MODE_FM_TERRA: 613 case MSP_MODE_FM_TERRA:
601 msp3400_dbg("FM setstereo: %s\n", strmode[mode]); 614 msp_dbg1("FM setstereo: %s\n", strmode[mode]);
602 msp3400c_setcarrier(client,msp->second,msp->main); 615 msp3400c_setcarrier(client,state->second,state->main);
603 switch (mode) { 616 switch (mode) {
604 case V4L2_TUNER_MODE_STEREO: 617 case V4L2_TUNER_MODE_STEREO:
605 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001); 618 msp_write_dsp(client, 0x000e, 0x3001);
606 break; 619 break;
607 case V4L2_TUNER_MODE_MONO: 620 case V4L2_TUNER_MODE_MONO:
608 case V4L2_TUNER_MODE_LANG1: 621 case V4L2_TUNER_MODE_LANG1:
609 case V4L2_TUNER_MODE_LANG2: 622 case V4L2_TUNER_MODE_LANG2:
610 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000); 623 msp_write_dsp(client, 0x000e, 0x3000);
611 break; 624 break;
612 } 625 }
613 break; 626 break;
614 case MSP_MODE_FM_SAT: 627 case MSP_MODE_FM_SAT:
615 msp3400_dbg("SAT setstereo: %s\n", strmode[mode]); 628 msp_dbg1("SAT setstereo: %s\n", strmode[mode]);
616 switch (mode) { 629 switch (mode) {
617 case V4L2_TUNER_MODE_MONO: 630 case V4L2_TUNER_MODE_MONO:
618 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5)); 631 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
@@ -631,38 +644,38 @@ static void msp3400c_setstereo(struct i2c_client *client, int mode)
631 case MSP_MODE_FM_NICAM1: 644 case MSP_MODE_FM_NICAM1:
632 case MSP_MODE_FM_NICAM2: 645 case MSP_MODE_FM_NICAM2:
633 case MSP_MODE_AM_NICAM: 646 case MSP_MODE_AM_NICAM:
634 msp3400_dbg("NICAM setstereo: %s\n",strmode[mode]); 647 msp_dbg1("NICAM setstereo: %s\n",strmode[mode]);
635 msp3400c_setcarrier(client,msp->second,msp->main); 648 msp3400c_setcarrier(client,state->second,state->main);
636 if (msp->nicam_on) 649 if (state->nicam_on)
637 nicam=0x0100; 650 nicam=0x0100;
638 break; 651 break;
639 case MSP_MODE_BTSC: 652 case MSP_MODE_BTSC:
640 msp3400_dbg("BTSC setstereo: %s\n",strmode[mode]); 653 msp_dbg1("BTSC setstereo: %s\n",strmode[mode]);
641 nicam=0x0300; 654 nicam=0x0300;
642 break; 655 break;
643 case MSP_MODE_EXTERN: 656 case MSP_MODE_EXTERN:
644 msp3400_dbg("extern setstereo: %s\n",strmode[mode]); 657 msp_dbg1("extern setstereo: %s\n",strmode[mode]);
645 nicam = 0x0200; 658 nicam = 0x0200;
646 break; 659 break;
647 case MSP_MODE_FM_RADIO: 660 case MSP_MODE_FM_RADIO:
648 msp3400_dbg("FM-Radio setstereo: %s\n",strmode[mode]); 661 msp_dbg1("FM-Radio setstereo: %s\n",strmode[mode]);
649 break; 662 break;
650 default: 663 default:
651 msp3400_dbg("mono setstereo\n"); 664 msp_dbg1("mono setstereo\n");
652 return; 665 return;
653 } 666 }
654 667
655 /* switch audio */ 668 /* switch audio */
656 switch (best_video_sound(mode)) { 669 switch (msp3400c_best_video_sound(mode)) {
657 case V4L2_TUNER_MODE_STEREO: 670 case V4L2_TUNER_MODE_STEREO:
658 src = 0x0020 | nicam; 671 src = 0x0020 | nicam;
659 break; 672 break;
660 case V4L2_TUNER_MODE_MONO: 673 case V4L2_TUNER_MODE_MONO:
661 if (msp->mode == MSP_MODE_AM_NICAM) { 674 if (state->mode == MSP_MODE_AM_NICAM) {
662 msp3400_dbg("switching to AM mono\n"); 675 msp_dbg1("switching to AM mono\n");
663 /* AM mono decoding is handled by tuner, not MSP chip */ 676 /* AM mono decoding is handled by tuner, not MSP chip */
664 /* SCART switching control register */ 677 /* SCART switching control register */
665 msp3400c_set_scart(client,SCART_MONO,0); 678 msp_set_scart(client,SCART_MONO,0);
666 src = 0x0200; 679 src = 0x0200;
667 break; 680 break;
668 } 681 }
@@ -673,67 +686,67 @@ static void msp3400c_setstereo(struct i2c_client *client, int mode)
673 src = 0x0010 | nicam; 686 src = 0x0010 | nicam;
674 break; 687 break;
675 } 688 }
676 msp3400_dbg("setstereo final source/matrix = 0x%x\n", src); 689 msp_dbg1("setstereo final source/matrix = 0x%x\n", src);
677 690
678 if (dolby) { 691 if (dolby) {
679 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520); 692 msp_write_dsp(client, 0x0008,0x0520);
680 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620); 693 msp_write_dsp(client, 0x0009,0x0620);
681 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src); 694 msp_write_dsp(client, 0x000a,src);
682 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src); 695 msp_write_dsp(client, 0x000b,src);
683 } else { 696 } else {
684 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src); 697 msp_write_dsp(client, 0x0008,src);
685 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src); 698 msp_write_dsp(client, 0x0009,src);
686 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src); 699 msp_write_dsp(client, 0x000a,src);
687 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src); 700 msp_write_dsp(client, 0x000b,src);
688 } 701 }
689} 702}
690 703
691static void 704static void
692msp3400c_print_mode(struct i2c_client *client) 705msp3400c_print_mode(struct i2c_client *client)
693{ 706{
694 struct msp3400c *msp = i2c_get_clientdata(client); 707 struct msp_state *state = i2c_get_clientdata(client);
695 708
696 if (msp->main == msp->second) { 709 if (state->main == state->second) {
697 msp3400_dbg("mono sound carrier: %d.%03d MHz\n", 710 msp_dbg1("mono sound carrier: %d.%03d MHz\n",
698 msp->main/910000,(msp->main/910)%1000); 711 state->main/910000,(state->main/910)%1000);
699 } else { 712 } else {
700 msp3400_dbg("main sound carrier: %d.%03d MHz\n", 713 msp_dbg1("main sound carrier: %d.%03d MHz\n",
701 msp->main/910000,(msp->main/910)%1000); 714 state->main/910000,(state->main/910)%1000);
702 } 715 }
703 if (msp->mode == MSP_MODE_FM_NICAM1 || msp->mode == MSP_MODE_FM_NICAM2) 716 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
704 msp3400_dbg("NICAM/FM carrier : %d.%03d MHz\n", 717 msp_dbg1("NICAM/FM carrier : %d.%03d MHz\n",
705 msp->second/910000,(msp->second/910)%1000); 718 state->second/910000,(state->second/910)%1000);
706 if (msp->mode == MSP_MODE_AM_NICAM) 719 if (state->mode == MSP_MODE_AM_NICAM)
707 msp3400_dbg("NICAM/AM carrier : %d.%03d MHz\n", 720 msp_dbg1("NICAM/AM carrier : %d.%03d MHz\n",
708 msp->second/910000,(msp->second/910)%1000); 721 state->second/910000,(state->second/910)%1000);
709 if (msp->mode == MSP_MODE_FM_TERRA && 722 if (state->mode == MSP_MODE_FM_TERRA &&
710 msp->main != msp->second) { 723 state->main != state->second) {
711 msp3400_dbg("FM-stereo carrier : %d.%03d MHz\n", 724 msp_dbg1("FM-stereo carrier : %d.%03d MHz\n",
712 msp->second/910000,(msp->second/910)%1000); 725 state->second/910000,(state->second/910)%1000);
713 } 726 }
714} 727}
715 728
716static void msp3400c_restore_dfp(struct i2c_client *client) 729static void msp3400c_restore_dfp(struct i2c_client *client)
717{ 730{
718 struct msp3400c *msp = i2c_get_clientdata(client); 731 struct msp_state *state = i2c_get_clientdata(client);
719 int i; 732 int i;
720 733
721 for (i = 0; i < DFP_COUNT; i++) { 734 for (i = 0; i < DFP_COUNT; i++) {
722 if (-1 == msp->dfp_regs[i]) 735 if (-1 == state->dfp_regs[i])
723 continue; 736 continue;
724 msp3400c_write(client, I2C_MSP3400C_DFP, i, msp->dfp_regs[i]); 737 msp_write_dsp(client, i, state->dfp_regs[i]);
725 } 738 }
726} 739}
727 740
728/* if the dfp_regs is set, set what's in there. Otherwise, set the default value */ 741/* if the dfp_regs is set, set what's in there. Otherwise, set the default value */
729static int msp3400c_write_dfp_with_default(struct i2c_client *client, 742static int msp_write_dfp_with_default(struct i2c_client *client,
730 int addr, int default_value) 743 int addr, int default_value)
731{ 744{
732 struct msp3400c *msp = i2c_get_clientdata(client); 745 struct msp_state *state = i2c_get_clientdata(client);
733 int value = default_value; 746 int value = default_value;
734 if (addr < DFP_COUNT && -1 != msp->dfp_regs[addr]) 747 if (addr < DFP_COUNT && -1 != state->dfp_regs[addr])
735 value = msp->dfp_regs[addr]; 748 value = state->dfp_regs[addr];
736 return msp3400c_write(client, I2C_MSP3400C_DFP, addr, value); 749 return msp_write_dsp(client, addr, value);
737} 750}
738 751
739/* ----------------------------------------------------------------------- */ 752/* ----------------------------------------------------------------------- */
@@ -753,18 +766,18 @@ struct REGISTER_DUMP d1[] = {
753 766
754static int autodetect_stereo(struct i2c_client *client) 767static int autodetect_stereo(struct i2c_client *client)
755{ 768{
756 struct msp3400c *msp = i2c_get_clientdata(client); 769 struct msp_state *state = i2c_get_clientdata(client);
757 int val; 770 int val;
758 int rxsubchans = msp->rxsubchans; 771 int rxsubchans = state->rxsubchans;
759 int newnicam = msp->nicam_on; 772 int newnicam = state->nicam_on;
760 int update = 0; 773 int update = 0;
761 774
762 switch (msp->mode) { 775 switch (state->mode) {
763 case MSP_MODE_FM_TERRA: 776 case MSP_MODE_FM_TERRA:
764 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18); 777 val = msp_read_dsp(client, 0x18);
765 if (val > 32767) 778 if (val > 32767)
766 val -= 65536; 779 val -= 65536;
767 msp3400_dbg("stereo detect register: %d\n",val); 780 msp_dbg2("stereo detect register: %d\n",val);
768 if (val > 4096) { 781 if (val > 4096) {
769 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO; 782 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
770 } else if (val < -4096) { 783 } else if (val < -4096) {
@@ -777,8 +790,8 @@ static int autodetect_stereo(struct i2c_client *client)
777 case MSP_MODE_FM_NICAM1: 790 case MSP_MODE_FM_NICAM1:
778 case MSP_MODE_FM_NICAM2: 791 case MSP_MODE_FM_NICAM2:
779 case MSP_MODE_AM_NICAM: 792 case MSP_MODE_AM_NICAM:
780 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23); 793 val = msp_read_dem(client, 0x23);
781 msp3400_dbg("nicam sync=%d, mode=%d\n", 794 msp_dbg2("nicam sync=%d, mode=%d\n",
782 val & 1, (val & 0x1e) >> 1); 795 val & 1, (val & 0x1e) >> 1);
783 796
784 if (val & 1) { 797 if (val & 1) {
@@ -810,8 +823,8 @@ static int autodetect_stereo(struct i2c_client *client)
810 } 823 }
811 break; 824 break;
812 case MSP_MODE_BTSC: 825 case MSP_MODE_BTSC:
813 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200); 826 val = msp_read_dem(client, 0x200);
814 msp3400_dbg("status=0x%x (pri=%s, sec=%s, %s%s%s)\n", 827 msp_dbg2("status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
815 val, 828 val,
816 (val & 0x0002) ? "no" : "yes", 829 (val & 0x0002) ? "no" : "yes",
817 (val & 0x0004) ? "no" : "yes", 830 (val & 0x0004) ? "no" : "yes",
@@ -823,17 +836,17 @@ static int autodetect_stereo(struct i2c_client *client)
823 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1; 836 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
824 break; 837 break;
825 } 838 }
826 if (rxsubchans != msp->rxsubchans) { 839 if (rxsubchans != state->rxsubchans) {
827 update = 1; 840 update = 1;
828 msp3400_dbg("watch: rxsubchans %d => %d\n", 841 msp_dbg1("watch: rxsubchans %d => %d\n",
829 msp->rxsubchans,rxsubchans); 842 state->rxsubchans,rxsubchans);
830 msp->rxsubchans = rxsubchans; 843 state->rxsubchans = rxsubchans;
831 } 844 }
832 if (newnicam != msp->nicam_on) { 845 if (newnicam != state->nicam_on) {
833 update = 1; 846 update = 1;
834 msp3400_dbg("watch: nicam %d => %d\n", 847 msp_dbg1("watch: nicam %d => %d\n",
835 msp->nicam_on,newnicam); 848 state->nicam_on,newnicam);
836 msp->nicam_on = newnicam; 849 state->nicam_on = newnicam;
837 } 850 }
838 return update; 851 return update;
839} 852}
@@ -843,11 +856,11 @@ static int autodetect_stereo(struct i2c_client *client)
843 * in the ioctl while doing the sound carrier & stereo detect 856 * in the ioctl while doing the sound carrier & stereo detect
844 */ 857 */
845 858
846static int msp34xx_sleep(struct msp3400c *msp, int timeout) 859static int msp_sleep(struct msp_state *state, int timeout)
847{ 860{
848 DECLARE_WAITQUEUE(wait, current); 861 DECLARE_WAITQUEUE(wait, current);
849 862
850 add_wait_queue(&msp->wq, &wait); 863 add_wait_queue(&state->wq, &wait);
851 if (!kthread_should_stop()) { 864 if (!kthread_should_stop()) {
852 if (timeout < 0) { 865 if (timeout < 0) {
853 set_current_state(TASK_INTERRUPTIBLE); 866 set_current_state(TASK_INTERRUPTIBLE);
@@ -858,90 +871,90 @@ static int msp34xx_sleep(struct msp3400c *msp, int timeout)
858 } 871 }
859 } 872 }
860 873
861 remove_wait_queue(&msp->wq, &wait); 874 remove_wait_queue(&state->wq, &wait);
862 try_to_freeze(); 875 try_to_freeze();
863 return msp->restart; 876 return state->restart;
864} 877}
865 878
866/* stereo/multilang monitoring */ 879/* stereo/multilang monitoring */
867static void watch_stereo(struct i2c_client *client) 880static void watch_stereo(struct i2c_client *client)
868{ 881{
869 struct msp3400c *msp = i2c_get_clientdata(client); 882 struct msp_state *state = i2c_get_clientdata(client);
870 883
871 if (autodetect_stereo(client)) { 884 if (autodetect_stereo(client)) {
872 if (msp->stereo & V4L2_TUNER_MODE_STEREO) 885 if (state->stereo & V4L2_TUNER_MODE_STEREO)
873 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO); 886 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
874 else if (msp->stereo & VIDEO_SOUND_LANG1) 887 else if (state->stereo & VIDEO_SOUND_LANG1)
875 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1); 888 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
876 else 889 else
877 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); 890 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
878 } 891 }
879 892
880 if (once) 893 if (once)
881 msp->watch_stereo = 0; 894 state->watch_stereo = 0;
882} 895}
883 896
884 897
885static int msp3400c_thread(void *data) 898static int msp3400c_thread(void *data)
886{ 899{
887 struct i2c_client *client = data; 900 struct i2c_client *client = data;
888 struct msp3400c *msp = i2c_get_clientdata(client); 901 struct msp_state *state = i2c_get_clientdata(client);
889 struct CARRIER_DETECT *cd; 902 struct CARRIER_DETECT *cd;
890 int count, max1,max2,val1,val2, val,this; 903 int count, max1,max2,val1,val2, val,this;
891 904
892 905
893 msp3400_info("msp3400 daemon started\n"); 906 msp_info("msp3400 daemon started\n");
894 for (;;) { 907 for (;;) {
895 msp3400_dbg_mediumvol("msp3400 thread: sleep\n"); 908 msp_dbg2("msp3400 thread: sleep\n");
896 msp34xx_sleep(msp,-1); 909 msp_sleep(state, -1);
897 msp3400_dbg_mediumvol("msp3400 thread: wakeup\n"); 910 msp_dbg2("msp3400 thread: wakeup\n");
898 911
899 restart: 912 restart:
900 msp3400_dbg("thread: restart scan\n"); 913 msp_dbg1("thread: restart scan\n");
901 msp->restart = 0; 914 state->restart = 0;
902 if (kthread_should_stop()) 915 if (kthread_should_stop())
903 break; 916 break;
904 917
905 if (VIDEO_MODE_RADIO == msp->norm || 918 if (VIDEO_MODE_RADIO == state->norm ||
906 MSP_MODE_EXTERN == msp->mode) { 919 MSP_MODE_EXTERN == state->mode) {
907 /* no carrier scan, just unmute */ 920 /* no carrier scan, just unmute */
908 msp3400_info("thread: no carrier scan\n"); 921 msp_info("thread: no carrier scan\n");
909 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 922 msp_set_volume(client, state->muted, state->left, state->right);
910 continue; 923 continue;
911 } 924 }
912 925
913 /* mute */ 926 /* mute */
914 msp3400c_setvolume(client, msp->muted, 0, 0); 927 msp_set_volume(client, state->muted, 0, 0);
915 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ ); 928 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
916 val1 = val2 = 0; 929 val1 = val2 = 0;
917 max1 = max2 = -1; 930 max1 = max2 = -1;
918 msp->watch_stereo = 0; 931 state->watch_stereo = 0;
919 932
920 /* some time for the tuner to sync */ 933 /* some time for the tuner to sync */
921 if (msp34xx_sleep(msp,200)) 934 if (msp_sleep(state,200))
922 goto restart; 935 goto restart;
923 936
924 /* carrier detect pass #1 -- main carrier */ 937 /* carrier detect pass #1 -- main carrier */
925 cd = carrier_detect_main; 938 cd = carrier_detect_main;
926 count = CARRIER_COUNT(carrier_detect_main); 939 count = CARRIER_COUNT(carrier_detect_main);
927 940
928 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) { 941 if (amsound && (state->norm == VIDEO_MODE_SECAM)) {
929 /* autodetect doesn't work well with AM ... */ 942 /* autodetect doesn't work well with AM ... */
930 max1 = 3; 943 max1 = 3;
931 count = 0; 944 count = 0;
932 msp3400_dbg("AM sound override\n"); 945 msp_dbg1("AM sound override\n");
933 } 946 }
934 947
935 for (this = 0; this < count; this++) { 948 for (this = 0; this < count; this++) {
936 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo); 949 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
937 if (msp34xx_sleep(msp,100)) 950 if (msp_sleep(state,100))
938 goto restart; 951 goto restart;
939 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b); 952 val = msp_read_dsp(client, 0x1b);
940 if (val > 32767) 953 if (val > 32767)
941 val -= 65536; 954 val -= 65536;
942 if (val1 < val) 955 if (val1 < val)
943 val1 = val, max1 = this; 956 val1 = val, max1 = this;
944 msp3400_dbg("carrier1 val: %5d / %s\n", val,cd[this].name); 957 msp_dbg1("carrier1 val: %5d / %s\n", val,cd[this].name);
945 } 958 }
946 959
947 /* carrier detect pass #2 -- second (stereo) carrier */ 960 /* carrier detect pass #2 -- second (stereo) carrier */
@@ -962,7 +975,7 @@ static int msp3400c_thread(void *data)
962 break; 975 break;
963 } 976 }
964 977
965 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) { 978 if (amsound && (state->norm == VIDEO_MODE_SECAM)) {
966 /* autodetect doesn't work well with AM ... */ 979 /* autodetect doesn't work well with AM ... */
967 cd = NULL; 980 cd = NULL;
968 count = 0; 981 count = 0;
@@ -970,72 +983,72 @@ static int msp3400c_thread(void *data)
970 } 983 }
971 for (this = 0; this < count; this++) { 984 for (this = 0; this < count; this++) {
972 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo); 985 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
973 if (msp34xx_sleep(msp,100)) 986 if (msp_sleep(state,100))
974 goto restart; 987 goto restart;
975 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b); 988 val = msp_read_dsp(client, 0x1b);
976 if (val > 32767) 989 if (val > 32767)
977 val -= 65536; 990 val -= 65536;
978 if (val2 < val) 991 if (val2 < val)
979 val2 = val, max2 = this; 992 val2 = val, max2 = this;
980 msp3400_dbg("carrier2 val: %5d / %s\n", val,cd[this].name); 993 msp_dbg1("carrier2 val: %5d / %s\n", val,cd[this].name);
981 } 994 }
982 995
983 /* programm the msp3400 according to the results */ 996 /* program the msp3400 according to the results */
984 msp->main = carrier_detect_main[max1].cdo; 997 state->main = carrier_detect_main[max1].cdo;
985 switch (max1) { 998 switch (max1) {
986 case 1: /* 5.5 */ 999 case 1: /* 5.5 */
987 if (max2 == 0) { 1000 if (max2 == 0) {
988 /* B/G FM-stereo */ 1001 /* B/G FM-stereo */
989 msp->second = carrier_detect_55[max2].cdo; 1002 state->second = carrier_detect_55[max2].cdo;
990 msp3400c_setmode(client, MSP_MODE_FM_TERRA); 1003 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
991 msp->nicam_on = 0; 1004 state->nicam_on = 0;
992 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); 1005 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
993 msp->watch_stereo = 1; 1006 state->watch_stereo = 1;
994 } else if (max2 == 1 && HAVE_NICAM(msp)) { 1007 } else if (max2 == 1 && HAVE_NICAM(state)) {
995 /* B/G NICAM */ 1008 /* B/G NICAM */
996 msp->second = carrier_detect_55[max2].cdo; 1009 state->second = carrier_detect_55[max2].cdo;
997 msp3400c_setmode(client, MSP_MODE_FM_NICAM1); 1010 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
998 msp->nicam_on = 1; 1011 state->nicam_on = 1;
999 msp3400c_setcarrier(client, msp->second, msp->main); 1012 msp3400c_setcarrier(client, state->second, state->main);
1000 msp->watch_stereo = 1; 1013 state->watch_stereo = 1;
1001 } else { 1014 } else {
1002 goto no_second; 1015 goto no_second;
1003 } 1016 }
1004 break; 1017 break;
1005 case 2: /* 6.0 */ 1018 case 2: /* 6.0 */
1006 /* PAL I NICAM */ 1019 /* PAL I NICAM */
1007 msp->second = MSP_CARRIER(6.552); 1020 state->second = MSP_CARRIER(6.552);
1008 msp3400c_setmode(client, MSP_MODE_FM_NICAM2); 1021 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
1009 msp->nicam_on = 1; 1022 state->nicam_on = 1;
1010 msp3400c_setcarrier(client, msp->second, msp->main); 1023 msp3400c_setcarrier(client, state->second, state->main);
1011 msp->watch_stereo = 1; 1024 state->watch_stereo = 1;
1012 break; 1025 break;
1013 case 3: /* 6.5 */ 1026 case 3: /* 6.5 */
1014 if (max2 == 1 || max2 == 2) { 1027 if (max2 == 1 || max2 == 2) {
1015 /* D/K FM-stereo */ 1028 /* D/K FM-stereo */
1016 msp->second = carrier_detect_65[max2].cdo; 1029 state->second = carrier_detect_65[max2].cdo;
1017 msp3400c_setmode(client, MSP_MODE_FM_TERRA); 1030 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
1018 msp->nicam_on = 0; 1031 state->nicam_on = 0;
1019 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); 1032 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1020 msp->watch_stereo = 1; 1033 state->watch_stereo = 1;
1021 } else if (max2 == 0 && 1034 } else if (max2 == 0 &&
1022 msp->norm == VIDEO_MODE_SECAM) { 1035 state->norm == VIDEO_MODE_SECAM) {
1023 /* L NICAM or AM-mono */ 1036 /* L NICAM or AM-mono */
1024 msp->second = carrier_detect_65[max2].cdo; 1037 state->second = carrier_detect_65[max2].cdo;
1025 msp3400c_setmode(client, MSP_MODE_AM_NICAM); 1038 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
1026 msp->nicam_on = 0; 1039 state->nicam_on = 0;
1027 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); 1040 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1028 msp3400c_setcarrier(client, msp->second, msp->main); 1041 msp3400c_setcarrier(client, state->second, state->main);
1029 /* volume prescale for SCART (AM mono input) */ 1042 /* volume prescale for SCART (AM mono input) */
1030 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900); 1043 msp_write_dsp(client, 0x000d, 0x1900);
1031 msp->watch_stereo = 1; 1044 state->watch_stereo = 1;
1032 } else if (max2 == 0 && HAVE_NICAM(msp)) { 1045 } else if (max2 == 0 && HAVE_NICAM(state)) {
1033 /* D/K NICAM */ 1046 /* D/K NICAM */
1034 msp->second = carrier_detect_65[max2].cdo; 1047 state->second = carrier_detect_65[max2].cdo;
1035 msp3400c_setmode(client, MSP_MODE_FM_NICAM1); 1048 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
1036 msp->nicam_on = 1; 1049 state->nicam_on = 1;
1037 msp3400c_setcarrier(client, msp->second, msp->main); 1050 msp3400c_setcarrier(client, state->second, state->main);
1038 msp->watch_stereo = 1; 1051 state->watch_stereo = 1;
1039 } else { 1052 } else {
1040 goto no_second; 1053 goto no_second;
1041 } 1054 }
@@ -1043,30 +1056,30 @@ static int msp3400c_thread(void *data)
1043 case 0: /* 4.5 */ 1056 case 0: /* 4.5 */
1044 default: 1057 default:
1045 no_second: 1058 no_second:
1046 msp->second = carrier_detect_main[max1].cdo; 1059 state->second = carrier_detect_main[max1].cdo;
1047 msp3400c_setmode(client, MSP_MODE_FM_TERRA); 1060 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
1048 msp->nicam_on = 0; 1061 state->nicam_on = 0;
1049 msp3400c_setcarrier(client, msp->second, msp->main); 1062 msp3400c_setcarrier(client, state->second, state->main);
1050 msp->rxsubchans = V4L2_TUNER_SUB_MONO; 1063 state->rxsubchans = V4L2_TUNER_SUB_MONO;
1051 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO); 1064 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1052 break; 1065 break;
1053 } 1066 }
1054 1067
1055 /* unmute */ 1068 /* unmute */
1056 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1069 msp_set_volume(client, state->muted, state->left, state->right);
1057 msp3400c_restore_dfp(client); 1070 msp3400c_restore_dfp(client);
1058 1071
1059 if (debug) 1072 if (debug)
1060 msp3400c_print_mode(client); 1073 msp3400c_print_mode(client);
1061 1074
1062 /* monitor tv audio mode */ 1075 /* monitor tv audio mode */
1063 while (msp->watch_stereo) { 1076 while (state->watch_stereo) {
1064 if (msp34xx_sleep(msp,5000)) 1077 if (msp_sleep(state,5000))
1065 goto restart; 1078 goto restart;
1066 watch_stereo(client); 1079 watch_stereo(client);
1067 } 1080 }
1068 } 1081 }
1069 msp3400_dbg("thread: exit\n"); 1082 msp_dbg1("thread: exit\n");
1070 return 0; 1083 return 0;
1071} 1084}
1072 1085
@@ -1079,8 +1092,8 @@ static struct MODES {
1079 int main, second; 1092 int main, second;
1080 char *name; 1093 char *name;
1081} modelist[] = { 1094} modelist[] = {
1082 { 0x0000, 0, 0, "ERROR" }, 1095 { 0x0000, 0, 0, "could not detect sound standard" },
1083 { 0x0001, 0, 0, "autodetect start" }, 1096 { 0x0001, 0, 0, "autodetect started" },
1084 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" }, 1097 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
1085 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" }, 1098 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
1086 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" }, 1099 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
@@ -1101,7 +1114,7 @@ static struct MODES {
1101 { -1, 0, 0, NULL }, /* EOF */ 1114 { -1, 0, 0, NULL }, /* EOF */
1102}; 1115};
1103 1116
1104static inline const char *msp34xx_standard_mode_name(int mode) 1117static inline const char *msp_standard_mode_name(int mode)
1105{ 1118{
1106 int i; 1119 int i;
1107 for (i = 0; modelist[i].name != NULL; i++) 1120 for (i = 0; modelist[i].name != NULL; i++)
@@ -1110,11 +1123,11 @@ static inline const char *msp34xx_standard_mode_name(int mode)
1110 return "unknown"; 1123 return "unknown";
1111} 1124}
1112 1125
1113static int msp34xx_modus(struct i2c_client *client, int norm) 1126static int msp_modus(struct i2c_client *client, int norm)
1114{ 1127{
1115 switch (norm) { 1128 switch (norm) {
1116 case VIDEO_MODE_PAL: 1129 case VIDEO_MODE_PAL:
1117 msp3400_dbg("video mode selected to PAL\n"); 1130 msp_dbg1("video mode selected to PAL\n");
1118 1131
1119#if 1 1132#if 1
1120 /* experimental: not sure this works with all chip versions */ 1133 /* experimental: not sure this works with all chip versions */
@@ -1124,23 +1137,23 @@ static int msp34xx_modus(struct i2c_client *client, int norm)
1124 return 0x1003; 1137 return 0x1003;
1125#endif 1138#endif
1126 case VIDEO_MODE_NTSC: /* BTSC */ 1139 case VIDEO_MODE_NTSC: /* BTSC */
1127 msp3400_dbg("video mode selected to NTSC\n"); 1140 msp_dbg1("video mode selected to NTSC\n");
1128 return 0x2003; 1141 return 0x2003;
1129 case VIDEO_MODE_SECAM: 1142 case VIDEO_MODE_SECAM:
1130 msp3400_dbg("video mode selected to SECAM\n"); 1143 msp_dbg1("video mode selected to SECAM\n");
1131 return 0x0003; 1144 return 0x0003;
1132 case VIDEO_MODE_RADIO: 1145 case VIDEO_MODE_RADIO:
1133 msp3400_dbg("video mode selected to Radio\n"); 1146 msp_dbg1("video mode selected to Radio\n");
1134 return 0x0003; 1147 return 0x0003;
1135 case VIDEO_MODE_AUTO: 1148 case VIDEO_MODE_AUTO:
1136 msp3400_dbg("video mode selected to Auto\n"); 1149 msp_dbg1("video mode selected to Auto\n");
1137 return 0x2003; 1150 return 0x2003;
1138 default: 1151 default:
1139 return 0x0003; 1152 return 0x0003;
1140 } 1153 }
1141} 1154}
1142 1155
1143static int msp34xx_standard(int norm) 1156static int msp_standard(int norm)
1144{ 1157{
1145 switch (norm) { 1158 switch (norm) {
1146 case VIDEO_MODE_PAL: 1159 case VIDEO_MODE_PAL:
@@ -1159,46 +1172,46 @@ static int msp34xx_standard(int norm)
1159static int msp3410d_thread(void *data) 1172static int msp3410d_thread(void *data)
1160{ 1173{
1161 struct i2c_client *client = data; 1174 struct i2c_client *client = data;
1162 struct msp3400c *msp = i2c_get_clientdata(client); 1175 struct msp_state *state = i2c_get_clientdata(client);
1163 int mode,val,i,std; 1176 int mode,val,i,std;
1164 1177
1165 msp3400_info("msp3410 daemon started\n"); 1178 msp_info("msp3410 daemon started\n");
1166 1179
1167 for (;;) { 1180 for (;;) {
1168 msp3400_dbg_mediumvol("msp3410 thread: sleep\n"); 1181 msp_dbg2("msp3410 thread: sleep\n");
1169 msp34xx_sleep(msp,-1); 1182 msp_sleep(state,-1);
1170 msp3400_dbg_mediumvol("msp3410 thread: wakeup\n"); 1183 msp_dbg2("msp3410 thread: wakeup\n");
1171 1184
1172 restart: 1185 restart:
1173 msp3400_dbg("thread: restart scan\n"); 1186 msp_dbg1("thread: restart scan\n");
1174 msp->restart = 0; 1187 state->restart = 0;
1175 if (kthread_should_stop()) 1188 if (kthread_should_stop())
1176 break; 1189 break;
1177 1190
1178 if (msp->mode == MSP_MODE_EXTERN) { 1191 if (state->mode == MSP_MODE_EXTERN) {
1179 /* no carrier scan needed, just unmute */ 1192 /* no carrier scan needed, just unmute */
1180 msp3400_dbg("thread: no carrier scan\n"); 1193 msp_dbg1("thread: no carrier scan\n");
1181 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1194 msp_set_volume(client, state->muted, state->left, state->right);
1182 continue; 1195 continue;
1183 } 1196 }
1184 1197
1185 /* put into sane state (and mute) */ 1198 /* put into sane state (and mute) */
1186 msp3400c_reset(client); 1199 msp_reset(client);
1187 1200
1188 /* some time for the tuner to sync */ 1201 /* some time for the tuner to sync */
1189 if (msp34xx_sleep(msp,200)) 1202 if (msp_sleep(state,200))
1190 goto restart; 1203 goto restart;
1191 1204
1192 /* start autodetect */ 1205 /* start autodetect */
1193 mode = msp34xx_modus(client, msp->norm); 1206 mode = msp_modus(client, state->norm);
1194 std = msp34xx_standard(msp->norm); 1207 std = msp_standard(state->norm);
1195 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode); 1208 msp_write_dem(client, 0x30, mode);
1196 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std); 1209 msp_write_dem(client, 0x20, std);
1197 msp->watch_stereo = 0; 1210 state->watch_stereo = 0;
1198 1211
1199 if (debug) 1212 if (debug)
1200 msp3400_dbg("setting mode: %s (0x%04x)\n", 1213 msp_dbg1("setting mode: %s (0x%04x)\n",
1201 msp34xx_standard_mode_name(std) ,std); 1214 msp_standard_mode_name(std) ,std);
1202 1215
1203 if (std != 1) { 1216 if (std != 1) {
1204 /* programmed some specific mode */ 1217 /* programmed some specific mode */
@@ -1206,171 +1219,164 @@ static int msp3410d_thread(void *data)
1206 } else { 1219 } else {
1207 /* triggered autodetect */ 1220 /* triggered autodetect */
1208 for (;;) { 1221 for (;;) {
1209 if (msp34xx_sleep(msp,100)) 1222 if (msp_sleep(state,100))
1210 goto restart; 1223 goto restart;
1211 1224
1212 /* check results */ 1225 /* check results */
1213 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e); 1226 val = msp_read_dem(client, 0x7e);
1214 if (val < 0x07ff) 1227 if (val < 0x07ff)
1215 break; 1228 break;
1216 msp3400_dbg("detection still in progress\n"); 1229 msp_dbg1("detection still in progress\n");
1217 } 1230 }
1218 } 1231 }
1219 for (i = 0; modelist[i].name != NULL; i++) 1232 for (i = 0; modelist[i].name != NULL; i++)
1220 if (modelist[i].retval == val) 1233 if (modelist[i].retval == val)
1221 break; 1234 break;
1222 msp3400_dbg("current mode: %s (0x%04x)\n", 1235 msp_dbg1("current mode: %s (0x%04x)\n",
1223 modelist[i].name ? modelist[i].name : "unknown", 1236 modelist[i].name ? modelist[i].name : "unknown",
1224 val); 1237 val);
1225 msp->main = modelist[i].main; 1238 state->main = modelist[i].main;
1226 msp->second = modelist[i].second; 1239 state->second = modelist[i].second;
1227 1240
1228 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) { 1241 if (amsound && (state->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1229 /* autodetection has failed, let backup */ 1242 /* autodetection has failed, let backup */
1230 msp3400_dbg("autodetection failed," 1243 msp_dbg1("autodetection failed,"
1231 " switching to backup mode: %s (0x%04x)\n", 1244 " switching to backup mode: %s (0x%04x)\n",
1232 modelist[8].name ? modelist[8].name : "unknown",val); 1245 modelist[8].name ? modelist[8].name : "unknown",val);
1233 val = 0x0009; 1246 val = 0x0009;
1234 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val); 1247 msp_write_dem(client, 0x20, val);
1235 } 1248 }
1236 1249
1237 /* set various prescales */ 1250 /* set various prescales */
1238 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */ 1251 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
1239 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */ 1252 msp_write_dsp(client, 0x0e, 0x2403); /* FM */
1240 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */ 1253 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
1241 1254
1242 /* set stereo */ 1255 /* set stereo */
1243 switch (val) { 1256 switch (val) {
1244 case 0x0008: /* B/G NICAM */ 1257 case 0x0008: /* B/G NICAM */
1245 case 0x000a: /* I NICAM */ 1258 case 0x000a: /* I NICAM */
1246 if (val == 0x0008) 1259 if (val == 0x0008)
1247 msp->mode = MSP_MODE_FM_NICAM1; 1260 state->mode = MSP_MODE_FM_NICAM1;
1248 else 1261 else
1249 msp->mode = MSP_MODE_FM_NICAM2; 1262 state->mode = MSP_MODE_FM_NICAM2;
1250 /* just turn on stereo */ 1263 /* just turn on stereo */
1251 msp->rxsubchans = V4L2_TUNER_SUB_STEREO; 1264 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
1252 msp->nicam_on = 1; 1265 state->nicam_on = 1;
1253 msp->watch_stereo = 1; 1266 state->watch_stereo = 1;
1254 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO); 1267 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1255 break; 1268 break;
1256 case 0x0009: 1269 case 0x0009:
1257 msp->mode = MSP_MODE_AM_NICAM; 1270 state->mode = MSP_MODE_AM_NICAM;
1258 msp->rxsubchans = V4L2_TUNER_SUB_MONO; 1271 state->rxsubchans = V4L2_TUNER_SUB_MONO;
1259 msp->nicam_on = 1; 1272 state->nicam_on = 1;
1260 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO); 1273 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
1261 msp->watch_stereo = 1; 1274 state->watch_stereo = 1;
1262 break; 1275 break;
1263 case 0x0020: /* BTSC */ 1276 case 0x0020: /* BTSC */
1264 /* just turn on stereo */ 1277 /* just turn on stereo */
1265 msp->mode = MSP_MODE_BTSC; 1278 state->mode = MSP_MODE_BTSC;
1266 msp->rxsubchans = V4L2_TUNER_SUB_STEREO; 1279 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
1267 msp->nicam_on = 0; 1280 state->nicam_on = 0;
1268 msp->watch_stereo = 1; 1281 state->watch_stereo = 1;
1269 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO); 1282 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1270 break; 1283 break;
1271 case 0x0040: /* FM radio */ 1284 case 0x0040: /* FM radio */
1272 msp->mode = MSP_MODE_FM_RADIO; 1285 state->mode = MSP_MODE_FM_RADIO;
1273 msp->rxsubchans = V4L2_TUNER_SUB_STEREO; 1286 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
1274 msp->audmode = V4L2_TUNER_MODE_STEREO; 1287 state->audmode = V4L2_TUNER_MODE_STEREO;
1275 msp->nicam_on = 0; 1288 state->nicam_on = 0;
1276 msp->watch_stereo = 0; 1289 state->watch_stereo = 0;
1277 /* not needed in theory if HAVE_RADIO(), but 1290 /* not needed in theory if HAVE_RADIO(), but
1278 short programming enables carrier mute */ 1291 short programming enables carrier mute */
1279 msp3400c_setmode(client,MSP_MODE_FM_RADIO); 1292 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1280 msp3400c_setcarrier(client, MSP_CARRIER(10.7), 1293 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1281 MSP_CARRIER(10.7)); 1294 MSP_CARRIER(10.7));
1282 /* scart routing */ 1295 /* scart routing */
1283 msp3400c_set_scart(client,SCART_IN2,0); 1296 msp_set_scart(client,SCART_IN2,0);
1284 /* msp34xx does radio decoding */ 1297 /* msp34xx does radio decoding */
1285 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020); 1298 msp_write_dsp(client, 0x08, 0x0020);
1286 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020); 1299 msp_write_dsp(client, 0x09, 0x0020);
1287 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020); 1300 msp_write_dsp(client, 0x0b, 0x0020);
1288 break; 1301 break;
1289 case 0x0003: 1302 case 0x0003:
1290 case 0x0004: 1303 case 0x0004:
1291 case 0x0005: 1304 case 0x0005:
1292 msp->mode = MSP_MODE_FM_TERRA; 1305 state->mode = MSP_MODE_FM_TERRA;
1293 msp->rxsubchans = V4L2_TUNER_SUB_MONO; 1306 state->rxsubchans = V4L2_TUNER_SUB_MONO;
1294 msp->audmode = V4L2_TUNER_MODE_MONO; 1307 state->audmode = V4L2_TUNER_MODE_MONO;
1295 msp->nicam_on = 0; 1308 state->nicam_on = 0;
1296 msp->watch_stereo = 1; 1309 state->watch_stereo = 1;
1297 break; 1310 break;
1298 } 1311 }
1299 1312
1300 /* unmute, restore misc registers */ 1313 /* unmute, restore misc registers */
1301 msp3400c_setbass(client, msp->bass); 1314 msp_set_bass(client, state->bass);
1302 msp3400c_settreble(client, msp->treble); 1315 msp_set_treble(client, state->treble);
1303 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1316 msp_set_volume(client, state->muted, state->left, state->right);
1304 msp3400c_write(client, I2C_MSP3400C_DFP, 0x13, msp->acb); 1317 msp_write_dsp(client, 0x13, state->acb);
1305 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); 1318 msp_write_dem(client, 0x40, state->i2s_mode);
1306 msp3400c_restore_dfp(client); 1319 msp3400c_restore_dfp(client);
1307 1320
1308 /* monitor tv audio mode */ 1321 /* monitor tv audio mode */
1309 while (msp->watch_stereo) { 1322 while (state->watch_stereo) {
1310 if (msp34xx_sleep(msp,5000)) 1323 if (msp_sleep(state,5000))
1311 goto restart; 1324 goto restart;
1312 watch_stereo(client); 1325 watch_stereo(client);
1313 } 1326 }
1314 } 1327 }
1315 msp3400_dbg("thread: exit\n"); 1328 msp_dbg1("thread: exit\n");
1316 return 0; 1329 return 0;
1317} 1330}
1318 1331
1319/* ----------------------------------------------------------------------- */ 1332/* ----------------------------------------------------------------------- */
1320/* msp34xxG + (simpler no-thread) */ 1333/* msp34xxG + (autoselect no-thread) */
1321/* this one uses both automatic standard detection and automatic sound */ 1334/* this one uses both automatic standard detection and automatic sound */
1322/* select which are available in the newer G versions */ 1335/* select which are available in the newer G versions */
1323/* struct msp: only norm, acb and source are really used in this mode */ 1336/* struct msp: only norm, acb and source are really used in this mode */
1324 1337
1325static void msp34xxg_set_source(struct i2c_client *client, int source); 1338static void msp34xxg_set_source(struct i2c_client *client, int source);
1326 1339
1327/* (re-)initialize the msp34xxg, according to the current norm in msp->norm 1340/* (re-)initialize the msp34xxg, according to the current norm in state->norm
1328 * return 0 if it worked, -1 if it failed 1341 * return 0 if it worked, -1 if it failed
1329 */ 1342 */
1330static int msp34xxg_reset(struct i2c_client *client) 1343static int msp34xxg_reset(struct i2c_client *client)
1331{ 1344{
1332 struct msp3400c *msp = i2c_get_clientdata(client); 1345 struct msp_state *state = i2c_get_clientdata(client);
1333 int modus,std; 1346 int modus,std;
1334 1347
1335 if (msp3400c_reset(client)) 1348 if (msp_reset(client))
1336 return -1; 1349 return -1;
1337 1350
1338 /* make sure that input/output is muted (paranoid mode) */ 1351 /* make sure that input/output is muted (paranoid mode) */
1339 if (msp3400c_write(client, 1352 if (msp_write_dsp(client,
1340 I2C_MSP3400C_DFP,
1341 0x13, /* ACB */ 1353 0x13, /* ACB */
1342 0x0f20 /* mute DSP input, mute SCART 1 */)) 1354 0x0f20 /* mute DSP input, mute SCART 1 */))
1343 return -1; 1355 return -1;
1344 1356
1345 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); 1357 msp_write_dem(client, 0x40, state->i2s_mode);
1346 1358
1347 /* step-by-step initialisation, as described in the manual */ 1359 /* step-by-step initialisation, as described in the manual */
1348 modus = msp34xx_modus(client, msp->norm); 1360 modus = msp_modus(client, state->norm);
1349 std = msp34xx_standard(msp->norm); 1361 std = msp_standard(state->norm);
1350 modus &= ~0x03; /* STATUS_CHANGE=0 */ 1362 modus &= ~0x03; /* STATUS_CHANGE=0 */
1351 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */ 1363 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1352 if (msp3400c_write(client, 1364 if (msp_write_dem(client, 0x30/*MODUS*/, modus))
1353 I2C_MSP3400C_DEM,
1354 0x30/*MODUS*/,
1355 modus))
1356 return -1; 1365 return -1;
1357 if (msp3400c_write(client, 1366 if (msp_write_dem(client, 0x20/*standard*/, std))
1358 I2C_MSP3400C_DEM,
1359 0x20/*standard*/,
1360 std))
1361 return -1; 1367 return -1;
1362 1368
1363 /* write the dfps that may have an influence on 1369 /* write the dfps that may have an influence on
1364 standard/audio autodetection right now */ 1370 standard/audio autodetection right now */
1365 msp34xxg_set_source(client, msp->source); 1371 msp34xxg_set_source(client, state->source);
1366 1372
1367 if (msp3400c_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */ 1373 if (msp_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */
1368 0x3000 1374 0x3000
1369 /* default: [15:8] 75khz deviation */ 1375 /* default: [15:8] 75khz deviation */
1370 )) 1376 ))
1371 return -1; 1377 return -1;
1372 1378
1373 if (msp3400c_write_dfp_with_default(client, 0x10, /* NICAM Prescale */ 1379 if (msp_write_dfp_with_default(client, 0x10, /* NICAM Prescale */
1374 0x5a00 1380 0x5a00
1375 /* default: 9db gain (as recommended) */ 1381 /* default: 9db gain (as recommended) */
1376 )) 1382 ))
@@ -1382,20 +1388,20 @@ static int msp34xxg_reset(struct i2c_client *client)
1382static int msp34xxg_thread(void *data) 1388static int msp34xxg_thread(void *data)
1383{ 1389{
1384 struct i2c_client *client = data; 1390 struct i2c_client *client = data;
1385 struct msp3400c *msp = i2c_get_clientdata(client); 1391 struct msp_state *state = i2c_get_clientdata(client);
1386 int val, std, i; 1392 int val, std, i;
1387 1393
1388 msp3400_info("msp34xxg daemon started\n"); 1394 msp_info("msp34xxg daemon started\n");
1389 1395
1390 msp->source = 1; /* default */ 1396 state->source = 1; /* default */
1391 for (;;) { 1397 for (;;) {
1392 msp3400_dbg_mediumvol("msp34xxg thread: sleep\n"); 1398 msp_dbg2("msp34xxg thread: sleep\n");
1393 msp34xx_sleep(msp,-1); 1399 msp_sleep(state,-1);
1394 msp3400_dbg_mediumvol("msp34xxg thread: wakeup\n"); 1400 msp_dbg2("msp34xxg thread: wakeup\n");
1395 1401
1396 restart: 1402 restart:
1397 msp3400_dbg("thread: restart scan\n"); 1403 msp_dbg1("thread: restart scan\n");
1398 msp->restart = 0; 1404 state->restart = 0;
1399 if (kthread_should_stop()) 1405 if (kthread_should_stop())
1400 break; 1406 break;
1401 1407
@@ -1406,45 +1412,43 @@ static int msp34xxg_thread(void *data)
1406 goto unmute; 1412 goto unmute;
1407 1413
1408 /* watch autodetect */ 1414 /* watch autodetect */
1409 msp3400_dbg("triggered autodetect, waiting for result\n"); 1415 msp_dbg1("triggered autodetect, waiting for result\n");
1410 for (i = 0; i < 10; i++) { 1416 for (i = 0; i < 10; i++) {
1411 if (msp34xx_sleep(msp,100)) 1417 if (msp_sleep(state,100))
1412 goto restart; 1418 goto restart;
1413 1419
1414 /* check results */ 1420 /* check results */
1415 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e); 1421 val = msp_read_dem(client, 0x7e);
1416 if (val < 0x07ff) { 1422 if (val < 0x07ff) {
1417 std = val; 1423 std = val;
1418 break; 1424 break;
1419 } 1425 }
1420 msp3400_dbg("detection still in progress\n"); 1426 msp_dbg1("detection still in progress\n");
1421 } 1427 }
1422 if (0x01 == std) { 1428 if (0x01 == std) {
1423 msp3400_dbg("detection still in progress after 10 tries. giving up.\n"); 1429 msp_dbg1("detection still in progress after 10 tries. giving up.\n");
1424 continue; 1430 continue;
1425 } 1431 }
1426 1432
1427 unmute: 1433 unmute:
1428 msp3400_dbg("current mode: %s (0x%04x)\n", 1434 state->mode = std;
1429 msp34xx_standard_mode_name(std), std); 1435 msp_dbg1("current mode: %s (0x%04x)\n",
1436 msp_standard_mode_name(std), std);
1430 1437
1431 /* unmute: dispatch sound to scart output, set scart volume */ 1438 /* unmute: dispatch sound to scart output, set scart volume */
1432 msp3400_dbg("unmute\n"); 1439 msp_dbg1("unmute\n");
1433 1440
1434 msp3400c_setbass(client, msp->bass); 1441 msp_set_bass(client, state->bass);
1435 msp3400c_settreble(client, msp->treble); 1442 msp_set_treble(client, state->treble);
1436 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1443 msp_set_volume(client, state->muted, state->left, state->right);
1437 1444
1438 /* restore ACB */ 1445 /* restore ACB */
1439 if (msp3400c_write(client, 1446 if (msp_write_dsp(client, 0x13, state->acb))
1440 I2C_MSP3400C_DFP,
1441 0x13, /* ACB */
1442 msp->acb))
1443 return -1; 1447 return -1;
1444 1448
1445 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode); 1449 msp_write_dem(client, 0x40, state->i2s_mode);
1446 } 1450 }
1447 msp3400_dbg("thread: exit\n"); 1451 msp_dbg1("thread: exit\n");
1448 return 0; 1452 return 0;
1449} 1453}
1450 1454
@@ -1456,24 +1460,21 @@ static int msp34xxg_thread(void *data)
1456 */ 1460 */
1457static void msp34xxg_set_source(struct i2c_client *client, int source) 1461static void msp34xxg_set_source(struct i2c_client *client, int source)
1458{ 1462{
1459 struct msp3400c *msp = i2c_get_clientdata(client); 1463 struct msp_state *state = i2c_get_clientdata(client);
1460 1464
1461 /* fix matrix mode to stereo and let the msp choose what 1465 /* fix matrix mode to stereo and let the msp choose what
1462 * to output according to 'source', as recommended 1466 * to output according to 'source', as recommended
1463 * for MONO (source==0) downmixing set bit[7:0] to 0x30 1467 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1464 */ 1468 */
1465 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20); 1469 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1466 msp3400_dbg("set source to %d (0x%x)\n", source, value); 1470 msp_dbg1("set source to %d (0x%x)\n", source, value);
1467 msp3400c_write(client, 1471 msp_write_dsp(client,
1468 I2C_MSP3400C_DFP,
1469 0x08, /* Loudspeaker Output */ 1472 0x08, /* Loudspeaker Output */
1470 value); 1473 value);
1471 msp3400c_write(client, 1474 msp_write_dsp(client,
1472 I2C_MSP3400C_DFP,
1473 0x0a, /* SCART1 DA Output */ 1475 0x0a, /* SCART1 DA Output */
1474 value); 1476 value);
1475 msp3400c_write(client, 1477 msp_write_dsp(client,
1476 I2C_MSP3400C_DFP,
1477 0x0c, /* Quasi-peak detector */ 1478 0x0c, /* Quasi-peak detector */
1478 value); 1479 value);
1479 /* 1480 /*
@@ -1483,30 +1484,28 @@ static void msp34xxg_set_source(struct i2c_client *client, int source)
1483 * this needs tuning. (recommended range 0x00a0-0x03c0) 1484 * this needs tuning. (recommended range 0x00a0-0x03c0)
1484 * 0x7f0 = forced mono mode 1485 * 0x7f0 = forced mono mode
1485 */ 1486 */
1486 msp3400c_write(client, 1487 msp_write_dem(client,
1487 I2C_MSP3400C_DEM,
1488 0x22, /* a2 threshold for stereo/bilingual */ 1488 0x22, /* a2 threshold for stereo/bilingual */
1489 stereo_threshold); 1489 stereo_threshold);
1490 msp->source=source; 1490 state->source=source;
1491} 1491}
1492 1492
1493static void msp34xxg_detect_stereo(struct i2c_client *client) 1493static void msp34xxg_detect_stereo(struct i2c_client *client)
1494{ 1494{
1495 struct msp3400c *msp = i2c_get_clientdata(client); 1495 struct msp_state *state = i2c_get_clientdata(client);
1496 1496
1497 int status = msp3400c_read(client, 1497 int status = msp_read_dem(client,
1498 I2C_MSP3400C_DEM,
1499 0x0200 /* STATUS */); 1498 0x0200 /* STATUS */);
1500 int is_bilingual = status&0x100; 1499 int is_bilingual = status&0x100;
1501 int is_stereo = status&0x40; 1500 int is_stereo = status&0x40;
1502 1501
1503 msp->rxsubchans = 0; 1502 state->rxsubchans = 0;
1504 if (is_stereo) 1503 if (is_stereo)
1505 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO; 1504 state->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1506 else 1505 else
1507 msp->rxsubchans |= V4L2_TUNER_SUB_MONO; 1506 state->rxsubchans |= V4L2_TUNER_SUB_MONO;
1508 if (is_bilingual) { 1507 if (is_bilingual) {
1509 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2; 1508 state->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1510 /* I'm supposed to check whether it's SAP or not 1509 /* I'm supposed to check whether it's SAP or not
1511 * and set only LANG2/SAP in this case. Yet, the MSP 1510 * and set only LANG2/SAP in this case. Yet, the MSP
1512 * does a lot of work to hide this and handle everything 1511 * does a lot of work to hide this and handle everything
@@ -1514,13 +1513,13 @@ static void msp34xxg_detect_stereo(struct i2c_client *client)
1514 * this is a problem, I'll handle SAP just like lang1/lang2. 1513 * this is a problem, I'll handle SAP just like lang1/lang2.
1515 */ 1514 */
1516 } 1515 }
1517 msp3400_dbg("status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n", 1516 msp_dbg1("status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1518 status, is_stereo, is_bilingual, msp->rxsubchans); 1517 status, is_stereo, is_bilingual, state->rxsubchans);
1519} 1518}
1520 1519
1521static void msp34xxg_set_audmode(struct i2c_client *client, int audmode) 1520static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1522{ 1521{
1523 struct msp3400c *msp = i2c_get_clientdata(client); 1522 struct msp_state *state = i2c_get_clientdata(client);
1524 int source; 1523 int source;
1525 1524
1526 switch (audmode) { 1525 switch (audmode) {
@@ -1542,7 +1541,7 @@ static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1542 source = 1; 1541 source = 1;
1543 break; 1542 break;
1544 } 1543 }
1545 msp->audmode = audmode; 1544 state->audmode = audmode;
1546 msp34xxg_set_source(client, source); 1545 msp34xxg_set_source(client, source);
1547} 1546}
1548 1547
@@ -1551,14 +1550,14 @@ static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1551 1550
1552static void msp_wake_thread(struct i2c_client *client) 1551static void msp_wake_thread(struct i2c_client *client)
1553{ 1552{
1554 struct msp3400c *msp = i2c_get_clientdata(client); 1553 struct msp_state *state = i2c_get_clientdata(client);
1555 1554
1556 if (NULL == msp->kthread) 1555 if (NULL == state->kthread)
1557 return; 1556 return;
1558 msp3400c_setvolume(client,msp->muted,0,0); 1557 msp_set_volume(client,state->muted,0,0);
1559 msp->watch_stereo = 0; 1558 state->watch_stereo = 0;
1560 msp->restart = 1; 1559 state->restart = 1;
1561 wake_up_interruptible(&msp->wq); 1560 wake_up_interruptible(&state->wq);
1562} 1561}
1563 1562
1564/* ----------------------------------------------------------------------- */ 1563/* ----------------------------------------------------------------------- */
@@ -1591,20 +1590,20 @@ static int mode_v4l1_to_v4l2(int mode)
1591 1590
1592static void msp_any_detect_stereo(struct i2c_client *client) 1591static void msp_any_detect_stereo(struct i2c_client *client)
1593{ 1592{
1594 struct msp3400c *msp = i2c_get_clientdata(client); 1593 struct msp_state *state = i2c_get_clientdata(client);
1595 1594
1596 switch (msp->opmode) { 1595 switch (state->opmode) {
1597 case OPMODE_MANUAL: 1596 case OPMODE_MANUAL:
1598 case OPMODE_SIMPLE: 1597 case OPMODE_AUTODETECT:
1599 autodetect_stereo(client); 1598 autodetect_stereo(client);
1600 break; 1599 break;
1601 case OPMODE_SIMPLER: 1600 case OPMODE_AUTOSELECT:
1602 msp34xxg_detect_stereo(client); 1601 msp34xxg_detect_stereo(client);
1603 break; 1602 break;
1604 } 1603 }
1605} 1604}
1606 1605
1607static struct v4l2_queryctrl msp34xx_qctrl[] = { 1606static struct v4l2_queryctrl msp_qctrl[] = {
1608 { 1607 {
1609 .id = V4L2_CID_AUDIO_VOLUME, 1608 .id = V4L2_CID_AUDIO_VOLUME,
1610 .name = "Volume", 1609 .name = "Volume",
@@ -1645,15 +1644,15 @@ static struct v4l2_queryctrl msp34xx_qctrl[] = {
1645 1644
1646static void msp_any_set_audmode(struct i2c_client *client, int audmode) 1645static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1647{ 1646{
1648 struct msp3400c *msp = i2c_get_clientdata(client); 1647 struct msp_state *state = i2c_get_clientdata(client);
1649 1648
1650 switch (msp->opmode) { 1649 switch (state->opmode) {
1651 case OPMODE_MANUAL: 1650 case OPMODE_MANUAL:
1652 case OPMODE_SIMPLE: 1651 case OPMODE_AUTODETECT:
1653 msp->watch_stereo = 0; 1652 state->watch_stereo = 0;
1654 msp3400c_setstereo(client, audmode); 1653 msp3400c_setstereo(client, audmode);
1655 break; 1654 break;
1656 case OPMODE_SIMPLER: 1655 case OPMODE_AUTOSELECT:
1657 msp34xxg_set_audmode(client, audmode); 1656 msp34xxg_set_audmode(client, audmode);
1658 break; 1657 break;
1659 } 1658 }
@@ -1661,32 +1660,32 @@ static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1661 1660
1662static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl) 1661static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
1663{ 1662{
1664 struct msp3400c *msp = i2c_get_clientdata(client); 1663 struct msp_state *state = i2c_get_clientdata(client);
1665 1664
1666 switch (ctrl->id) { 1665 switch (ctrl->id) {
1667 case V4L2_CID_AUDIO_MUTE: 1666 case V4L2_CID_AUDIO_MUTE:
1668 ctrl->value = msp->muted; 1667 ctrl->value = state->muted;
1669 return 0; 1668 return 0;
1670 case V4L2_CID_AUDIO_BALANCE: 1669 case V4L2_CID_AUDIO_BALANCE:
1671 { 1670 {
1672 int volume = max(msp->left, msp->right); 1671 int volume = max(state->left, state->right);
1673 1672
1674 ctrl->value = (32768 * min(msp->left, msp->right)) / 1673 ctrl->value = (32768 * min(state->left, state->right)) /
1675 (volume ? volume : 1); 1674 (volume ? volume : 1);
1676 ctrl->value = (msp->left < msp->right) ? 1675 ctrl->value = (state->left < state->right) ?
1677 (65535 - ctrl->value) : ctrl->value; 1676 (65535 - ctrl->value) : ctrl->value;
1678 if (0 == volume) 1677 if (0 == volume)
1679 ctrl->value = 32768; 1678 ctrl->value = 32768;
1680 return 0; 1679 return 0;
1681 } 1680 }
1682 case V4L2_CID_AUDIO_BASS: 1681 case V4L2_CID_AUDIO_BASS:
1683 ctrl->value = msp->bass; 1682 ctrl->value = state->bass;
1684 return 0; 1683 return 0;
1685 case V4L2_CID_AUDIO_TREBLE: 1684 case V4L2_CID_AUDIO_TREBLE:
1686 ctrl->value = msp->treble; 1685 ctrl->value = state->treble;
1687 return 0; 1686 return 0;
1688 case V4L2_CID_AUDIO_VOLUME: 1687 case V4L2_CID_AUDIO_VOLUME:
1689 ctrl->value = max(msp->left, msp->right); 1688 ctrl->value = max(state->left, state->right);
1690 return 0; 1689 return 0;
1691 default: 1690 default:
1692 return -EINVAL; 1691 return -EINVAL;
@@ -1695,124 +1694,126 @@ static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
1695 1694
1696static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl) 1695static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
1697{ 1696{
1698 struct msp3400c *msp = i2c_get_clientdata(client); 1697 struct msp_state *state = i2c_get_clientdata(client);
1699 int set_volume=0, balance, volume; 1698 int set_volume = 0, balance, volume;
1700 1699
1701 switch (ctrl->id) { 1700 switch (ctrl->id) {
1702 case V4L2_CID_AUDIO_MUTE: 1701 case V4L2_CID_AUDIO_MUTE:
1703 if (ctrl->value>=0 && ctrl->value<2) 1702 if (ctrl->value < 0 || ctrl->value >= 2)
1704 msp->muted = ctrl->value;
1705 else
1706 return -ERANGE; 1703 return -ERANGE;
1707 1704 state->muted = ctrl->value;
1708 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1705 msp_set_volume(client, state->muted, state->left, state->right);
1709 return 0; 1706 return 0;
1707
1710 case V4L2_CID_AUDIO_BALANCE: 1708 case V4L2_CID_AUDIO_BALANCE:
1711 balance=ctrl->value; 1709 balance = ctrl->value;
1712 volume = max(msp->left, msp->right); 1710 volume = max(state->left, state->right);
1713 set_volume=1; 1711 set_volume = 1;
1714 break; 1712 break;
1713
1715 case V4L2_CID_AUDIO_BASS: 1714 case V4L2_CID_AUDIO_BASS:
1716 msp->bass=ctrl->value; 1715 state->bass = ctrl->value;
1717 msp3400c_setbass(client, msp->bass); 1716 msp_set_bass(client, state->bass);
1718 return 0; 1717 return 0;
1718
1719 case V4L2_CID_AUDIO_TREBLE: 1719 case V4L2_CID_AUDIO_TREBLE:
1720 msp->treble=ctrl->value; 1720 state->treble = ctrl->value;
1721 msp3400c_settreble(client, msp->treble); 1721 msp_set_treble(client, state->treble);
1722 return 0; 1722 return 0;
1723
1723 case V4L2_CID_AUDIO_VOLUME: 1724 case V4L2_CID_AUDIO_VOLUME:
1724 volume = max(msp->left, msp->right); 1725 volume = max(state->left, state->right);
1725 1726
1726 balance = (32768 * min(msp->left, msp->right)) / 1727 balance = (32768 * min(state->left, state->right)) /
1727 (volume ? volume : 1); 1728 (volume ? volume : 1);
1728 balance = (msp->left < msp->right) ? 1729 balance = (state->left < state->right) ?
1729 (65535 - balance) : balance; 1730 (65535 - balance) : balance;
1730 if (0 == volume) 1731 if (volume == 0)
1731 balance = 32768; 1732 balance = 32768;
1732 1733
1733 volume=ctrl->value; 1734 volume = ctrl->value;
1734 set_volume=1; 1735 set_volume = 1;
1735 break; 1736 break;
1737
1736 default: 1738 default:
1737 return -EINVAL; 1739 return -EINVAL;
1738 } 1740 }
1739 1741
1740 if (set_volume) { 1742 if (set_volume) {
1741 msp->left = (min(65536 - balance, 32768) * volume) / 32768; 1743 state->left = (min(65536 - balance, 32768) * volume) / 32768;
1742 msp->right = (min(balance, 32768) * volume) / 32768; 1744 state->right = (min(balance, 32768) * volume) / 32768;
1743 1745
1744 msp3400_dbg("volume=%d, balance=%d, left=%d, right=%d", 1746 msp_dbg2("volume=%d, balance=%d, left=%d, right=%d",
1745 volume,balance,msp->left,msp->right); 1747 volume, balance, state->left, state->right);
1746 1748
1747 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1749 msp_set_volume(client, state->muted, state->left, state->right);
1748 } 1750 }
1749 return 0; 1751 return 0;
1750} 1752}
1751 1753
1752static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg) 1754static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1753{ 1755{
1754 struct msp3400c *msp = i2c_get_clientdata(client); 1756 struct msp_state *state = i2c_get_clientdata(client);
1755 __u16 *sarg = arg; 1757 u16 *sarg = arg;
1756 int scart = 0; 1758 int scart = 0;
1757 1759
1758 switch (cmd) { 1760 switch (cmd) {
1759
1760 case AUDC_SET_INPUT: 1761 case AUDC_SET_INPUT:
1761 msp3400_dbg("AUDC_SET_INPUT(%d)\n",*sarg); 1762 msp_dbg1("AUDC_SET_INPUT(%d)\n",*sarg);
1762 1763
1763 if (*sarg == msp->input) 1764 if (*sarg == state->input)
1764 break; 1765 break;
1765 msp->input = *sarg; 1766 state->input = *sarg;
1766 switch (*sarg) { 1767 switch (*sarg) {
1767 case AUDIO_RADIO: 1768 case AUDIO_RADIO:
1768 /* Hauppauge uses IN2 for the radio */ 1769 /* Hauppauge uses IN2 for the radio */
1769 msp->mode = MSP_MODE_FM_RADIO; 1770 state->mode = MSP_MODE_FM_RADIO;
1770 scart = SCART_IN2; 1771 scart = SCART_IN2;
1771 break; 1772 break;
1772 case AUDIO_EXTERN_1: 1773 case AUDIO_EXTERN_1:
1773 /* IN1 is often used for external input ... */ 1774 /* IN1 is often used for external input ... */
1774 msp->mode = MSP_MODE_EXTERN; 1775 state->mode = MSP_MODE_EXTERN;
1775 scart = SCART_IN1; 1776 scart = SCART_IN1;
1776 break; 1777 break;
1777 case AUDIO_EXTERN_2: 1778 case AUDIO_EXTERN_2:
1778 /* ... sometimes it is IN2 through ;) */ 1779 /* ... sometimes it is IN2 through ;) */
1779 msp->mode = MSP_MODE_EXTERN; 1780 state->mode = MSP_MODE_EXTERN;
1780 scart = SCART_IN2; 1781 scart = SCART_IN2;
1781 break; 1782 break;
1782 case AUDIO_TUNER: 1783 case AUDIO_TUNER:
1783 msp->mode = -1; 1784 state->mode = -1;
1784 break; 1785 break;
1785 default: 1786 default:
1786 if (*sarg & AUDIO_MUTE) 1787 if (*sarg & AUDIO_MUTE)
1787 msp3400c_set_scart(client,SCART_MUTE,0); 1788 msp_set_scart(client, SCART_MUTE, 0);
1788 break; 1789 break;
1789 } 1790 }
1790 if (scart) { 1791 if (scart) {
1791 msp->rxsubchans = V4L2_TUNER_SUB_STEREO; 1792 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
1792 msp->audmode = V4L2_TUNER_MODE_STEREO; 1793 state->audmode = V4L2_TUNER_MODE_STEREO;
1793 msp3400c_set_scart(client,scart,0); 1794 msp_set_scart(client, scart, 0);
1794 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900); 1795 msp_write_dsp(client, 0x000d, 0x1900);
1795 if (msp->opmode != OPMODE_SIMPLER) 1796 if (state->opmode != OPMODE_AUTOSELECT)
1796 msp3400c_setstereo(client, msp->audmode); 1797 msp3400c_setstereo(client, state->audmode);
1797 } 1798 }
1798 msp_wake_thread(client); 1799 msp_wake_thread(client);
1799 break; 1800 break;
1800 1801
1801 case AUDC_SET_RADIO: 1802 case AUDC_SET_RADIO:
1802 msp3400_dbg("AUDC_SET_RADIO\n"); 1803 msp_dbg1("AUDC_SET_RADIO\n");
1803 msp->norm = VIDEO_MODE_RADIO; 1804 state->norm = VIDEO_MODE_RADIO;
1804 msp3400_dbg("switching to radio mode\n"); 1805 msp_dbg1("switching to radio mode\n");
1805 msp->watch_stereo = 0; 1806 state->watch_stereo = 0;
1806 switch (msp->opmode) { 1807 switch (state->opmode) {
1807 case OPMODE_MANUAL: 1808 case OPMODE_MANUAL:
1808 /* set msp3400 to FM radio mode */ 1809 /* set msp3400 to FM radio mode */
1809 msp3400c_setmode(client,MSP_MODE_FM_RADIO); 1810 msp3400c_setmode(client, MSP_MODE_FM_RADIO);
1810 msp3400c_setcarrier(client, MSP_CARRIER(10.7), 1811 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1811 MSP_CARRIER(10.7)); 1812 MSP_CARRIER(10.7));
1812 msp3400c_setvolume(client, msp->muted, msp->left, msp->right); 1813 msp_set_volume(client, state->muted, state->left, state->right);
1813 break; 1814 break;
1814 case OPMODE_SIMPLE: 1815 case OPMODE_AUTODETECT:
1815 case OPMODE_SIMPLER: 1816 case OPMODE_AUTOSELECT:
1816 /* the thread will do for us */ 1817 /* the thread will do for us */
1817 msp_wake_thread(client); 1818 msp_wake_thread(client);
1818 break; 1819 break;
@@ -1829,17 +1830,18 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1829 for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++) 1830 for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++)
1830 if (r->reg == bl_dfp[i]) 1831 if (r->reg == bl_dfp[i])
1831 return -EINVAL; 1832 return -EINVAL;
1832 msp->dfp_regs[r->reg] = r->value; 1833 state->dfp_regs[r->reg] = r->value;
1833 msp3400c_write(client, I2C_MSP3400C_DFP, r->reg, r->value); 1834 msp_write_dsp(client, r->reg, r->value);
1834 return 0; 1835 return 0;
1835 } 1836 }
1837
1836 case MSP_GET_DFPREG: 1838 case MSP_GET_DFPREG:
1837 { 1839 {
1838 struct msp_dfpreg *r = arg; 1840 struct msp_dfpreg *r = arg;
1839 1841
1840 if (r->reg < 0 || r->reg >= DFP_COUNT) 1842 if (r->reg < 0 || r->reg >= DFP_COUNT)
1841 return -EINVAL; 1843 return -EINVAL;
1842 r->value = msp3400c_read(client, I2C_MSP3400C_DFP, r->reg); 1844 r->value = msp_read_dsp(client, r->reg);
1843 return 0; 1845 return 0;
1844 } 1846 }
1845 1847
@@ -1850,62 +1852,50 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1850 { 1852 {
1851 struct video_audio *va = arg; 1853 struct video_audio *va = arg;
1852 1854
1853 msp3400_dbg("VIDIOCGAUDIO\n"); 1855 msp_dbg1("VIDIOCGAUDIO\n");
1854 va->flags |= VIDEO_AUDIO_VOLUME | 1856 va->flags |= VIDEO_AUDIO_VOLUME |
1855 VIDEO_AUDIO_BASS | 1857 VIDEO_AUDIO_BASS |
1856 VIDEO_AUDIO_TREBLE | 1858 VIDEO_AUDIO_TREBLE |
1857 VIDEO_AUDIO_MUTABLE; 1859 VIDEO_AUDIO_MUTABLE;
1858 if (msp->muted) 1860 if (state->muted)
1859 va->flags |= VIDEO_AUDIO_MUTE; 1861 va->flags |= VIDEO_AUDIO_MUTE;
1860 1862
1861 if (msp->muted) 1863 if (state->muted)
1862 va->flags |= VIDEO_AUDIO_MUTE; 1864 va->flags |= VIDEO_AUDIO_MUTE;
1863 va->volume = max(msp->left, msp->right); 1865 va->volume = max(state->left, state->right);
1864 va->balance = (32768 * min(msp->left, msp->right)) / 1866 va->balance = (32768 * min(state->left, state->right)) /
1865 (va->volume ? va->volume : 1); 1867 (va->volume ? va->volume : 1);
1866 va->balance = (msp->left < msp->right) ? 1868 va->balance = (state->left < state->right) ?
1867 (65535 - va->balance) : va->balance; 1869 (65535 - va->balance) : va->balance;
1868 if (0 == va->volume) 1870 if (0 == va->volume)
1869 va->balance = 32768; 1871 va->balance = 32768;
1870 va->bass = msp->bass; 1872 va->bass = state->bass;
1871 va->treble = msp->treble; 1873 va->treble = state->treble;
1872 1874
1873 msp_any_detect_stereo(client); 1875 msp_any_detect_stereo(client);
1874 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans); 1876 va->mode = mode_v4l2_to_v4l1(state->rxsubchans);
1875 break; 1877 break;
1876 } 1878 }
1879
1877 case VIDIOCSAUDIO: 1880 case VIDIOCSAUDIO:
1878 { 1881 {
1879 struct video_audio *va = arg; 1882 struct video_audio *va = arg;
1880 1883
1881 msp3400_dbg("VIDIOCSAUDIO\n"); 1884 msp_dbg1("VIDIOCSAUDIO\n");
1882 msp->muted = (va->flags & VIDEO_AUDIO_MUTE); 1885 state->muted = (va->flags & VIDEO_AUDIO_MUTE);
1883 msp->left = (min(65536 - va->balance, 32768) * 1886 state->left = (min(65536 - va->balance, 32768) *
1884 va->volume) / 32768; 1887 va->volume) / 32768;
1885 msp->right = (min((int)va->balance, 32768) * va->volume) / 32768; 1888 state->right = (min((int)va->balance, 32768) * va->volume) / 32768;
1886 msp->bass = va->bass; 1889 state->bass = va->bass;
1887 msp->treble = va->treble; 1890 state->treble = va->treble;
1888 msp3400_dbg("VIDIOCSAUDIO setting va->volume to %d\n", 1891 msp_dbg1("vol %d, bal %d, flags %x, left %d, right %d, bass %d, treble %d, mode %x\n",
1889 va->volume); 1892 va->volume, va->balance, va->flags, state->left,
1890 msp3400_dbg("VIDIOCSAUDIO setting va->balance to %d\n", 1893 state->right, state->bass, state->treble, state->mode);
1891 va->balance); 1894 msp_set_volume(client, state->muted, state->left, state->right);
1892 msp3400_dbg("VIDIOCSAUDIO setting va->flags to %d\n", 1895 msp_set_bass(client, state->bass);
1893 va->flags); 1896 msp_set_treble(client, state->treble);
1894 msp3400_dbg("VIDIOCSAUDIO setting msp->left to %d\n", 1897
1895 msp->left); 1898 if (va->mode != 0 && state->norm != VIDEO_MODE_RADIO)
1896 msp3400_dbg("VIDIOCSAUDIO setting msp->right to %d\n",
1897 msp->right);
1898 msp3400_dbg("VIDIOCSAUDIO setting msp->bass to %d\n",
1899 msp->bass);
1900 msp3400_dbg("VIDIOCSAUDIO setting msp->treble to %d\n",
1901 msp->treble);
1902 msp3400_dbg("VIDIOCSAUDIO setting msp->mode to %d\n",
1903 msp->mode);
1904 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1905 msp3400c_setbass(client, msp->bass);
1906 msp3400c_settreble(client, msp->treble);
1907
1908 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1909 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode)); 1899 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1910 break; 1900 break;
1911 } 1901 }
@@ -1914,8 +1904,8 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1914 { 1904 {
1915 struct video_channel *vc = arg; 1905 struct video_channel *vc = arg;
1916 1906
1917 msp3400_dbg("VIDIOCSCHAN (norm=%d)\n",vc->norm); 1907 msp_dbg1("VIDIOCSCHAN (norm=%d)\n",vc->norm);
1918 msp->norm = vc->norm; 1908 state->norm = vc->norm;
1919 msp_wake_thread(client); 1909 msp_wake_thread(client);
1920 break; 1910 break;
1921 } 1911 }
@@ -1924,7 +1914,7 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1924 case VIDIOC_S_FREQUENCY: 1914 case VIDIOC_S_FREQUENCY:
1925 { 1915 {
1926 /* new channel -- kick audio carrier scan */ 1916 /* new channel -- kick audio carrier scan */
1927 msp3400_dbg("VIDIOCSFREQ\n"); 1917 msp_dbg1("VIDIOCSFREQ\n");
1928 msp_wake_thread(client); 1918 msp_wake_thread(client);
1929 break; 1919 break;
1930 } 1920 }
@@ -1934,8 +1924,8 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1934 { 1924 {
1935 struct msp_matrix *mspm = arg; 1925 struct msp_matrix *mspm = arg;
1936 1926
1937 msp3400_dbg("MSP_SET_MATRIX\n"); 1927 msp_dbg1("MSP_SET_MATRIX\n");
1938 msp3400c_set_scart(client, mspm->input, mspm->output); 1928 msp_set_scart(client, mspm->input, mspm->output);
1939 break; 1929 break;
1940 } 1930 }
1941 1931
@@ -1946,11 +1936,11 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1946 1936
1947 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/ 1937 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
1948 if (*id & V4L2_STD_PAL) { 1938 if (*id & V4L2_STD_PAL) {
1949 msp->norm=VIDEO_MODE_PAL; 1939 state->norm=VIDEO_MODE_PAL;
1950 } else if (*id & V4L2_STD_SECAM) { 1940 } else if (*id & V4L2_STD_SECAM) {
1951 msp->norm=VIDEO_MODE_SECAM; 1941 state->norm=VIDEO_MODE_SECAM;
1952 } else { 1942 } else {
1953 msp->norm=VIDEO_MODE_NTSC; 1943 state->norm=VIDEO_MODE_NTSC;
1954 } 1944 }
1955 1945
1956 msp_wake_thread(client); 1946 msp_wake_thread(client);
@@ -2008,12 +1998,13 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
2008 } 1998 }
2009 1999
2010 msp_any_detect_stereo(client); 2000 msp_any_detect_stereo(client);
2011 if (msp->audmode == V4L2_TUNER_MODE_STEREO) { 2001 if (state->audmode == V4L2_TUNER_MODE_STEREO) {
2012 a->capability=V4L2_AUDCAP_STEREO; 2002 a->capability=V4L2_AUDCAP_STEREO;
2013 } 2003 }
2014 2004
2015 break; 2005 break;
2016 } 2006 }
2007
2017 case VIDIOC_S_AUDIO: 2008 case VIDIOC_S_AUDIO:
2018 { 2009 {
2019 struct v4l2_audio *sarg = arg; 2010 struct v4l2_audio *sarg = arg;
@@ -2021,50 +2012,52 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
2021 switch (sarg->index) { 2012 switch (sarg->index) {
2022 case AUDIO_RADIO: 2013 case AUDIO_RADIO:
2023 /* Hauppauge uses IN2 for the radio */ 2014 /* Hauppauge uses IN2 for the radio */
2024 msp->mode = MSP_MODE_FM_RADIO; 2015 state->mode = MSP_MODE_FM_RADIO;
2025 scart = SCART_IN2; 2016 scart = SCART_IN2;
2026 break; 2017 break;
2027 case AUDIO_EXTERN_1: 2018 case AUDIO_EXTERN_1:
2028 /* IN1 is often used for external input ... */ 2019 /* IN1 is often used for external input ... */
2029 msp->mode = MSP_MODE_EXTERN; 2020 state->mode = MSP_MODE_EXTERN;
2030 scart = SCART_IN1; 2021 scart = SCART_IN1;
2031 break; 2022 break;
2032 case AUDIO_EXTERN_2: 2023 case AUDIO_EXTERN_2:
2033 /* ... sometimes it is IN2 through ;) */ 2024 /* ... sometimes it is IN2 through ;) */
2034 msp->mode = MSP_MODE_EXTERN; 2025 state->mode = MSP_MODE_EXTERN;
2035 scart = SCART_IN2; 2026 scart = SCART_IN2;
2036 break; 2027 break;
2037 case AUDIO_TUNER: 2028 case AUDIO_TUNER:
2038 msp->mode = -1; 2029 state->mode = -1;
2039 break; 2030 break;
2040 } 2031 }
2041 if (scart) { 2032 if (scart) {
2042 msp->rxsubchans = V4L2_TUNER_SUB_STEREO; 2033 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
2043 msp->audmode = V4L2_TUNER_MODE_STEREO; 2034 state->audmode = V4L2_TUNER_MODE_STEREO;
2044 msp3400c_set_scart(client,scart,0); 2035 msp_set_scart(client,scart,0);
2045 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900); 2036 msp_write_dsp(client,0x000d,0x1900);
2046 } 2037 }
2047 if (sarg->capability==V4L2_AUDCAP_STEREO) { 2038 if (sarg->capability==V4L2_AUDCAP_STEREO) {
2048 msp->audmode = V4L2_TUNER_MODE_STEREO; 2039 state->audmode = V4L2_TUNER_MODE_STEREO;
2049 } else { 2040 } else {
2050 msp->audmode &= ~V4L2_TUNER_MODE_STEREO; 2041 state->audmode &= ~V4L2_TUNER_MODE_STEREO;
2051 } 2042 }
2052 msp_any_set_audmode(client, msp->audmode); 2043 msp_any_set_audmode(client, state->audmode);
2053 msp_wake_thread(client); 2044 msp_wake_thread(client);
2054 break; 2045 break;
2055 } 2046 }
2047
2056 case VIDIOC_G_TUNER: 2048 case VIDIOC_G_TUNER:
2057 { 2049 {
2058 struct v4l2_tuner *vt = arg; 2050 struct v4l2_tuner *vt = arg;
2059 2051
2060 msp_any_detect_stereo(client); 2052 msp_any_detect_stereo(client);
2061 vt->audmode = msp->audmode; 2053 vt->audmode = state->audmode;
2062 vt->rxsubchans = msp->rxsubchans; 2054 vt->rxsubchans = state->rxsubchans;
2063 vt->capability = V4L2_TUNER_CAP_STEREO | 2055 vt->capability = V4L2_TUNER_CAP_STEREO |
2064 V4L2_TUNER_CAP_LANG1| 2056 V4L2_TUNER_CAP_LANG1|
2065 V4L2_TUNER_CAP_LANG2; 2057 V4L2_TUNER_CAP_LANG2;
2066 break; 2058 break;
2067 } 2059 }
2060
2068 case VIDIOC_S_TUNER: 2061 case VIDIOC_S_TUNER:
2069 { 2062 {
2070 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg; 2063 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
@@ -2098,6 +2091,7 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
2098 break; 2091 break;
2099 2092
2100 } 2093 }
2094
2101 case VIDIOC_S_AUDOUT: 2095 case VIDIOC_S_AUDOUT:
2102 { 2096 {
2103 struct v4l2_audioout *a=(struct v4l2_audioout *)arg; 2097 struct v4l2_audioout *a=(struct v4l2_audioout *)arg;
@@ -2105,23 +2099,24 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
2105 if (a->index<0||a->index>2) 2099 if (a->index<0||a->index>2)
2106 return -EINVAL; 2100 return -EINVAL;
2107 2101
2108 msp3400_dbg("Setting audio out on msp34xx to input %i\n",a->index); 2102 msp_dbg1("Setting audio out on msp34xx to input %i\n",a->index);
2109 msp3400c_set_scart(client,msp->in_scart,a->index+1); 2103 msp_set_scart(client,state->in_scart,a->index+1);
2110 2104
2111 break; 2105 break;
2112 } 2106 }
2107
2113 case VIDIOC_INT_I2S_CLOCK_FREQ: 2108 case VIDIOC_INT_I2S_CLOCK_FREQ:
2114 { 2109 {
2115 u32 *a=(u32 *)arg; 2110 u32 *a=(u32 *)arg;
2116 2111
2117 msp3400_dbg("Setting I2S speed to %d\n",*a); 2112 msp_dbg1("Setting I2S speed to %d\n",*a);
2118 2113
2119 switch (*a) { 2114 switch (*a) {
2120 case 1024000: 2115 case 1024000:
2121 msp->i2s_mode=0; 2116 state->i2s_mode=0;
2122 break; 2117 break;
2123 case 2048000: 2118 case 2048000:
2124 msp->i2s_mode=1; 2119 state->i2s_mode=1;
2125 break; 2120 break;
2126 default: 2121 default:
2127 return -EINVAL; 2122 return -EINVAL;
@@ -2134,33 +2129,49 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
2134 struct v4l2_queryctrl *qc = arg; 2129 struct v4l2_queryctrl *qc = arg;
2135 int i; 2130 int i;
2136 2131
2137 msp3400_dbg("VIDIOC_QUERYCTRL\n"); 2132 msp_dbg1("VIDIOC_QUERYCTRL\n");
2138 2133
2139 for (i = 0; i < ARRAY_SIZE(msp34xx_qctrl); i++) 2134 for (i = 0; i < ARRAY_SIZE(msp_qctrl); i++)
2140 if (qc->id && qc->id == msp34xx_qctrl[i].id) { 2135 if (qc->id && qc->id == msp_qctrl[i].id) {
2141 memcpy(qc, &(msp34xx_qctrl[i]), 2136 memcpy(qc, &(msp_qctrl[i]),
2142 sizeof(*qc)); 2137 sizeof(*qc));
2143 return 0; 2138 return 0;
2144 } 2139 }
2145 2140
2146 return -EINVAL; 2141 return -EINVAL;
2147 } 2142 }
2143
2148 case VIDIOC_G_CTRL: 2144 case VIDIOC_G_CTRL:
2149 { 2145 {
2150 struct v4l2_control *ctrl = arg; 2146 struct v4l2_control *ctrl = arg;
2151 msp3400_dbg("VIDIOC_G_CTRL\n"); 2147 msp_dbg1("VIDIOC_G_CTRL\n");
2152 2148
2153 return msp_get_ctrl(client, ctrl); 2149 return msp_get_ctrl(client, ctrl);
2154 } 2150 }
2151
2155 case VIDIOC_S_CTRL: 2152 case VIDIOC_S_CTRL:
2156 { 2153 {
2157 struct v4l2_control *ctrl = arg; 2154 struct v4l2_control *ctrl = arg;
2158 2155
2159 msp3400_dbg("VIDIOC_S_CTRL\n"); 2156 msp_dbg1("VIDIOC_S_CTRL\n");
2160 2157
2161 return msp_set_ctrl(client, ctrl); 2158 return msp_set_ctrl(client, ctrl);
2162 } 2159 }
2163 2160
2161 case VIDIOC_LOG_STATUS:
2162 msp_any_detect_stereo(client);
2163 msp_info("%s rev1 = 0x%04x rev2 = 0x%04x\n",
2164 client->name, state->rev1, state->rev2);
2165 msp_info("Volume: left %d right %d bass %d treble %d%s\n",
2166 state->left, state->right,
2167 state->bass, state->treble,
2168 state->muted ? " (muted)" : "");
2169 msp_info("Mode: %s (%s%s)\n", msp_standard_mode_name(state->mode),
2170 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
2171 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
2172 msp_info("ACB: 0x%04x\n", state->acb);
2173 break;
2174
2164 default: 2175 default:
2165 /* nothing */ 2176 /* nothing */
2166 break; 2177 break;
@@ -2172,8 +2183,8 @@ static int msp_suspend(struct device * dev, pm_message_t state)
2172{ 2183{
2173 struct i2c_client *client = container_of(dev, struct i2c_client, dev); 2184 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
2174 2185
2175 msp3400_dbg("suspend\n"); 2186 msp_dbg1("suspend\n");
2176 msp3400c_reset(client); 2187 msp_reset(client);
2177 return 0; 2188 return 0;
2178} 2189}
2179 2190
@@ -2181,133 +2192,113 @@ static int msp_resume(struct device * dev)
2181{ 2192{
2182 struct i2c_client *client = container_of(dev, struct i2c_client, dev); 2193 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
2183 2194
2184 msp3400_dbg("resume\n"); 2195 msp_dbg1("resume\n");
2185 msp_wake_thread(client); 2196 msp_wake_thread(client);
2186 return 0; 2197 return 0;
2187} 2198}
2188 2199
2189/* ----------------------------------------------------------------------- */ 2200/* ----------------------------------------------------------------------- */
2190 2201
2191static int msp_probe(struct i2c_adapter *adap); 2202static struct i2c_driver i2c_driver;
2192static int msp_detach(struct i2c_client *client);
2193
2194static struct i2c_driver driver = {
2195 .owner = THIS_MODULE,
2196 .name = "msp3400",
2197 .id = I2C_DRIVERID_MSP3400,
2198 .flags = I2C_DF_NOTIFY,
2199 .attach_adapter = msp_probe,
2200 .detach_client = msp_detach,
2201 .command = msp_command,
2202 .driver = {
2203 .suspend = msp_suspend,
2204 .resume = msp_resume,
2205 },
2206};
2207
2208static struct i2c_client client_template =
2209{
2210 .name = "(unset)",
2211 .flags = I2C_CLIENT_ALLOW_USE,
2212 .driver = &driver,
2213};
2214 2203
2215static int msp_attach(struct i2c_adapter *adap, int addr, int kind) 2204static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
2216{ 2205{
2217 struct msp3400c *msp; 2206 struct i2c_client *client;
2218 struct i2c_client *client = &client_template; 2207 struct msp_state *state;
2219 int (*thread_func)(void *data) = NULL; 2208 int (*thread_func)(void *data) = NULL;
2220 int i; 2209 int i;
2221 2210
2222 client_template.adapter = adap; 2211 client = kmalloc(sizeof(*client), GFP_KERNEL);
2223 client_template.addr = addr; 2212 if (client == NULL)
2224 2213 return -ENOMEM;
2225 if (-1 == msp3400c_reset(&client_template)) { 2214 memset(client, 0, sizeof(*client));
2226 msp3400_dbg("no chip found\n"); 2215 client->addr = address;
2216 client->adapter = adapter;
2217 client->driver = &i2c_driver;
2218 client->flags = I2C_CLIENT_ALLOW_USE;
2219 snprintf(client->name, sizeof(client->name) - 1, "msp3400");
2220
2221 if (msp_reset(client) == -1) {
2222 msp_dbg1("msp3400 not found\n");
2223 kfree(client);
2227 return -1; 2224 return -1;
2228 } 2225 }
2229 2226
2230 if (NULL == (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL))) 2227 state = kmalloc(sizeof(*state), GFP_KERNEL);
2231 return -ENOMEM; 2228 if (state == NULL) {
2232 memcpy(client,&client_template,sizeof(struct i2c_client));
2233 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
2234 kfree(client); 2229 kfree(client);
2235 return -ENOMEM; 2230 return -ENOMEM;
2236 } 2231 }
2237 2232 i2c_set_clientdata(client, state);
2238 memset(msp,0,sizeof(struct msp3400c)); 2233
2239 msp->norm = VIDEO_MODE_NTSC; 2234 memset(state, 0, sizeof(*state));
2240 msp->left = 58880; /* 0db gain */ 2235 state->norm = VIDEO_MODE_NTSC;
2241 msp->right = 58880; /* 0db gain */ 2236 state->left = 58880; /* 0db gain */
2242 msp->bass = 32768; 2237 state->right = 58880; /* 0db gain */
2243 msp->treble = 32768; 2238 state->bass = 32768;
2244 msp->input = -1; 2239 state->treble = 32768;
2245 msp->muted = 0; 2240 state->input = -1;
2246 msp->i2s_mode = 0; 2241 state->muted = 0;
2242 state->i2s_mode = 0;
2247 for (i = 0; i < DFP_COUNT; i++) 2243 for (i = 0; i < DFP_COUNT; i++)
2248 msp->dfp_regs[i] = -1; 2244 state->dfp_regs[i] = -1;
2249 2245 init_waitqueue_head(&state->wq);
2250 i2c_set_clientdata(client, msp); 2246
2251 init_waitqueue_head(&msp->wq); 2247 state->rev1 = msp_read_dsp(client, 0x1e);
2252 2248 if (state->rev1 != -1)
2253 if (-1 == msp3400c_reset(client)) { 2249 state->rev2 = msp_read_dsp(client, 0x1f);
2254 kfree(msp); 2250 msp_dbg1("rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
2251 if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
2252 msp_dbg1("error while reading chip version\n");
2253 kfree(state);
2255 kfree(client); 2254 kfree(client);
2256 msp3400_dbg("no chip found\n");
2257 return -1; 2255 return -1;
2258 } 2256 }
2259 2257
2260 msp->rev1 = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1e); 2258 msp_set_volume(client, state->muted, state->left, state->right);
2261 if (-1 != msp->rev1)
2262 msp->rev2 = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1f);
2263 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
2264 kfree(msp);
2265 kfree(client);
2266 msp3400_dbg("error while reading chip version\n");
2267 return -1;
2268 }
2269 msp3400_dbg("rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2);
2270
2271 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
2272 2259
2273 snprintf(client->name, sizeof(client->name), "MSP%c4%02d%c-%c%d", 2260 snprintf(client->name, sizeof(client->name), "MSP%c4%02d%c-%c%d",
2274 ((msp->rev1>>4)&0x0f) + '3', 2261 ((state->rev1 >> 4) & 0x0f) + '3',
2275 (msp->rev2>>8)&0xff, (msp->rev1&0x0f)+'@', 2262 (state->rev2 >> 8) & 0xff,
2276 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f); 2263 (state->rev1 & 0x0f) + '@',
2277 2264 ((state->rev1 >> 8) & 0xff) + '@',
2278 msp->opmode = opmode; 2265 state->rev2 & 0x1f);
2279 if (OPMODE_AUTO == msp->opmode) { 2266
2280 if (HAVE_SIMPLER(msp)) 2267 state->opmode = opmode;
2281 msp->opmode = OPMODE_SIMPLER; 2268 if (state->opmode == OPMODE_AUTO) {
2282 else if (HAVE_SIMPLE(msp)) 2269 /* MSP revision G and up have both autodetect and autoselect */
2283 msp->opmode = OPMODE_SIMPLE; 2270 if ((state->rev1 & 0x0f) >= 'G'-'@')
2271 state->opmode = OPMODE_AUTOSELECT;
2272 /* MSP revision D and up have autodetect */
2273 else if ((state->rev1 & 0x0f) >= 'D'-'@')
2274 state->opmode = OPMODE_AUTODETECT;
2284 else 2275 else
2285 msp->opmode = OPMODE_MANUAL; 2276 state->opmode = OPMODE_MANUAL;
2286 } 2277 }
2287 2278
2288 /* hello world :-) */ 2279 /* hello world :-) */
2289 msp3400_info("chip=%s", client->name); 2280 msp_info("%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
2290 if (HAVE_NICAM(msp)) 2281 msp_info("%s ", client->name);
2291 printk(" +nicam"); 2282 if (HAVE_NICAM(state) && HAVE_RADIO(state))
2292 if (HAVE_SIMPLE(msp)) 2283 printk("supports nicam and radio, ");
2293 printk(" +simple"); 2284 else if (HAVE_NICAM(state))
2294 if (HAVE_SIMPLER(msp)) 2285 printk("supports nicam, ");
2295 printk(" +simpler"); 2286 else if (HAVE_RADIO(state))
2296 if (HAVE_RADIO(msp)) 2287 printk("supports radio, ");
2297 printk(" +radio"); 2288 printk("mode is ");
2298 2289
2299 /* version-specific initialization */ 2290 /* version-specific initialization */
2300 switch (msp->opmode) { 2291 switch (state->opmode) {
2301 case OPMODE_MANUAL: 2292 case OPMODE_MANUAL:
2302 printk(" mode=manual"); 2293 printk("manual");
2303 thread_func = msp3400c_thread; 2294 thread_func = msp3400c_thread;
2304 break; 2295 break;
2305 case OPMODE_SIMPLE: 2296 case OPMODE_AUTODETECT:
2306 printk(" mode=simple"); 2297 printk("autodetect");
2307 thread_func = msp3410d_thread; 2298 thread_func = msp3410d_thread;
2308 break; 2299 break;
2309 case OPMODE_SIMPLER: 2300 case OPMODE_AUTOSELECT:
2310 printk(" mode=simpler"); 2301 printk("autodetect and autoselect");
2311 thread_func = msp34xxg_thread; 2302 thread_func = msp34xxg_thread;
2312 break; 2303 break;
2313 } 2304 }
@@ -2315,10 +2306,10 @@ static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
2315 2306
2316 /* startup control thread if needed */ 2307 /* startup control thread if needed */
2317 if (thread_func) { 2308 if (thread_func) {
2318 msp->kthread = kthread_run(thread_func, client, "msp34xx"); 2309 state->kthread = kthread_run(thread_func, client, "msp34xx");
2319 2310
2320 if (NULL == msp->kthread) 2311 if (NULL == state->kthread)
2321 msp3400_warn("kernel_thread() failed\n"); 2312 msp_warn("kernel_thread() failed\n");
2322 msp_wake_thread(client); 2313 msp_wake_thread(client);
2323 } 2314 }
2324 2315
@@ -2328,39 +2319,60 @@ static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
2328 return 0; 2319 return 0;
2329} 2320}
2330 2321
2322static int msp_probe(struct i2c_adapter *adapter)
2323{
2324 if (adapter->class & I2C_CLASS_TV_ANALOG)
2325 return i2c_probe(adapter, &addr_data, msp_attach);
2326 return 0;
2327}
2328
2331static int msp_detach(struct i2c_client *client) 2329static int msp_detach(struct i2c_client *client)
2332{ 2330{
2333 struct msp3400c *msp = i2c_get_clientdata(client); 2331 struct msp_state *state = i2c_get_clientdata(client);
2332 int err;
2334 2333
2335 /* shutdown control thread */ 2334 /* shutdown control thread */
2336 if (msp->kthread) { 2335 if (state->kthread) {
2337 msp->restart = 1; 2336 state->restart = 1;
2338 kthread_stop(msp->kthread); 2337 kthread_stop(state->kthread);
2339 } 2338 }
2340 msp3400c_reset(client); 2339 msp_reset(client);
2341 2340
2342 i2c_detach_client(client); 2341 err = i2c_detach_client(client);
2342 if (err) {
2343 return err;
2344 }
2343 2345
2344 kfree(msp); 2346 kfree(state);
2345 kfree(client); 2347 kfree(client);
2346 return 0; 2348 return 0;
2347} 2349}
2348 2350
2349static int msp_probe(struct i2c_adapter *adap) 2351/* ----------------------------------------------------------------------- */
2350{ 2352
2351 if (adap->class & I2C_CLASS_TV_ANALOG) 2353/* i2c implementation */
2352 return i2c_probe(adap, &addr_data, msp_attach); 2354static struct i2c_driver i2c_driver = {
2353 return 0; 2355 .name = "msp3400",
2354} 2356 .id = I2C_DRIVERID_MSP3400,
2357 .flags = I2C_DF_NOTIFY,
2358 .attach_adapter = msp_probe,
2359 .detach_client = msp_detach,
2360 .command = msp_command,
2361 .driver = {
2362 .suspend = msp_suspend,
2363 .resume = msp_resume,
2364 },
2365 .owner = THIS_MODULE,
2366};
2355 2367
2356static int __init msp3400_init_module(void) 2368static int __init msp3400_init_module(void)
2357{ 2369{
2358 return i2c_add_driver(&driver); 2370 return i2c_add_driver(&i2c_driver);
2359} 2371}
2360 2372
2361static void __exit msp3400_cleanup_module(void) 2373static void __exit msp3400_cleanup_module(void)
2362{ 2374{
2363 i2c_del_driver(&driver); 2375 i2c_del_driver(&i2c_driver);
2364} 2376}
2365 2377
2366module_init(msp3400_init_module); 2378module_init(msp3400_init_module);