blob: bc7eb23e615fe8f5f112842dd2c11e8235df85b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Philips UDA1341 mixer device driver
3 * Copyright (c) 2002 Tomas Kasparek <tomas.kasparek@seznam.cz>
4 *
5 * Portions are Copyright (C) 2000 Lernout & Hauspie Speech Products, N.V.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License.
9 *
10 * History:
11 *
12 * 2002-03-13 Tomas Kasparek initial release - based on uda1341.c from OSS
13 * 2002-03-28 Tomas Kasparek basic mixer is working (volume, bass, treble)
14 * 2002-03-30 Tomas Kasparek proc filesystem support, complete mixer and DSP
15 * features support
16 * 2002-04-12 Tomas Kasparek proc interface update, code cleanup
17 * 2002-05-12 Tomas Kasparek another code cleanup
18 */
19
Takashi Iwai0948e3c2005-11-17 10:25:22 +010020/* $Id: uda1341.c,v 1.17 2005/11/17 10:25:22 tiwai Exp $ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <sound/driver.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/types.h>
26#include <linux/slab.h>
27#include <linux/errno.h>
28#include <linux/ioctl.h>
29
30#include <asm/uaccess.h>
31
32#include <sound/core.h>
33#include <sound/control.h>
34#include <sound/initval.h>
35#include <sound/info.h>
36
37#include <linux/l3/l3.h>
38
39#include <sound/uda1341.h>
40
41/* {{{ HW regs definition */
42
43#define STAT0 0x00
44#define STAT1 0x80
45#define STAT_MASK 0x80
46
47#define DATA0_0 0x00
48#define DATA0_1 0x40
49#define DATA0_2 0x80
50#define DATA_MASK 0xc0
51
52#define IS_DATA0(x) ((x) >= data0_0 && (x) <= data0_2)
53#define IS_DATA1(x) ((x) == data1)
54#define IS_STATUS(x) ((x) == stat0 || (x) == stat1)
55#define IS_EXTEND(x) ((x) >= ext0 && (x) <= ext6)
56
57/* }}} */
58
Takashi Iwai0948e3c2005-11-17 10:25:22 +010059
60static const char *peak_names[] = {
61 "before",
62 "after",
63};
64
65static const char *filter_names[] = {
66 "flat",
67 "min",
68 "min",
69 "max",
70};
71
72static const char *mixer_names[] = {
73 "double differential",
74 "input channel 1 (line in)",
75 "input channel 2 (microphone)",
76 "digital mixer",
77};
78
79static const char *deemp_names[] = {
80 "none",
81 "32 kHz",
82 "44.1 kHz",
83 "48 kHz",
84};
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086enum uda1341_regs_names {
87 stat0,
88 stat1,
89 data0_0,
90 data0_1,
91 data0_2,
92 data1,
93 ext0,
94 ext1,
95 ext2,
96 empty,
97 ext4,
98 ext5,
99 ext6,
100 uda1341_reg_last,
101};
102
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100103static const char *uda1341_reg_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 "stat 0 ",
105 "stat 1 ",
106 "data 00",
107 "data 01",
108 "data 02",
109 "data 1 ",
110 "ext 0",
111 "ext 1",
112 "ext 2",
113 "empty",
114 "ext 4",
115 "ext 5",
116 "ext 6",
117};
118
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100119static const int uda1341_enum_items[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 2, //peak - before/after
122 4, //deemp - none/32/44.1/48
123 0,
124 4, //filter - flat/min/min/max
125 0, 0, 0,
126 4, //mixer - differ/line/mic/mixer
127 0, 0, 0, 0, 0,
128};
129
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100130static const char ** uda1341_enum_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
132 peak_names, //peak - before/after
133 deemp_names, //deemp - none/32/44.1/48
134 NULL,
135 filter_names, //filter - flat/min/min/max
136 NULL, NULL, NULL,
137 mixer_names, //mixer - differ/line/mic/mixer
138 NULL, NULL, NULL, NULL, NULL,
139};
140
141typedef int uda1341_cfg[CMD_LAST];
142
143typedef struct uda1341 uda1341_t;
144
145struct uda1341 {
146 int (*write) (struct l3_client *uda1341, unsigned short reg, unsigned short val);
147 int (*read) (struct l3_client *uda1341, unsigned short reg);
148 unsigned char regs[uda1341_reg_last];
149 int active;
150 spinlock_t reg_lock;
151 snd_card_t *card;
152 uda1341_cfg cfg;
153#ifdef CONFIG_PM
154 unsigned char suspend_regs[uda1341_reg_last];
155 uda1341_cfg suspend_cfg;
156#endif
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* transfer 8bit integer into string with binary representation */
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100160static void int2str_bin8(uint8_t val, char *buf)
161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 const int size = sizeof(val) * 8;
163 int i;
164
165 for (i= 0; i < size; i++){
166 *(buf++) = (val >> (size - 1)) ? '1' : '0';
167 val <<= 1;
168 }
169 *buf = '\0'; //end the string with zero
170}
171
172/* {{{ HW manipulation routines */
173
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100174static int snd_uda1341_codec_write(struct l3_client *clnt, unsigned short reg, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct uda1341 *uda = clnt->driver_data;
177 unsigned char buf[2] = { 0xc0, 0xe0 }; // for EXT addressing
178 int err = 0;
179
180 uda->regs[reg] = val;
181
182 if (uda->active) {
183 if (IS_DATA0(reg)) {
184 err = l3_write(clnt, UDA1341_DATA0, (const unsigned char *)&val, 1);
185 } else if (IS_DATA1(reg)) {
186 err = l3_write(clnt, UDA1341_DATA1, (const unsigned char *)&val, 1);
187 } else if (IS_STATUS(reg)) {
188 err = l3_write(clnt, UDA1341_STATUS, (const unsigned char *)&val, 1);
189 } else if (IS_EXTEND(reg)) {
190 buf[0] |= (reg - ext0) & 0x7; //EXT address
191 buf[1] |= val; //EXT data
192 err = l3_write(clnt, UDA1341_DATA0, (const unsigned char *)buf, 2);
193 }
194 } else
195 printk(KERN_ERR "UDA1341 codec not active!\n");
196 return err;
197}
198
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100199static int snd_uda1341_codec_read(struct l3_client *clnt, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 unsigned char val;
202 int err;
203
204 err = l3_read(clnt, reg, &val, 1);
205 if (err == 1)
206 // use just 6bits - the rest is address of the reg
207 return val & 63;
208 return err < 0 ? err : -EIO;
209}
210
211static inline int snd_uda1341_valid_reg(struct l3_client *clnt, unsigned short reg)
212{
213 return reg < uda1341_reg_last;
214}
215
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100216static int snd_uda1341_update_bits(struct l3_client *clnt, unsigned short reg,
217 unsigned short mask, unsigned short shift,
218 unsigned short value, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int change;
221 unsigned short old, new;
222 struct uda1341 *uda = clnt->driver_data;
223
224#if 0
225 printk(KERN_DEBUG "update_bits: reg: %s mask: %d shift: %d val: %d\n",
226 uda1341_reg_names[reg], mask, shift, value);
227#endif
228
229 if (!snd_uda1341_valid_reg(clnt, reg))
230 return -EINVAL;
231 spin_lock(&uda->reg_lock);
232 old = uda->regs[reg];
233 new = (old & ~(mask << shift)) | (value << shift);
234 change = old != new;
235 if (change) {
236 if (flush) uda->write(clnt, reg, new);
237 uda->regs[reg] = new;
238 }
239 spin_unlock(&uda->reg_lock);
240 return change;
241}
242
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100243static int snd_uda1341_cfg_write(struct l3_client *clnt, unsigned short what,
244 unsigned short value, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct uda1341 *uda = clnt->driver_data;
247 int ret = 0;
248#ifdef CONFIG_PM
249 int reg;
250#endif
251
252#if 0
253 printk(KERN_DEBUG "cfg_write what: %d value: %d\n", what, value);
254#endif
255
256 uda->cfg[what] = value;
257
258 switch(what) {
259 case CMD_RESET:
260 ret = snd_uda1341_update_bits(clnt, data0_2, 1, 2, 1, flush); // MUTE
261 ret = snd_uda1341_update_bits(clnt, stat0, 1, 6, 1, flush); // RESET
262 ret = snd_uda1341_update_bits(clnt, stat0, 1, 6, 0, flush); // RESTORE
263 uda->cfg[CMD_RESET]=0;
264 break;
265 case CMD_FS:
266 ret = snd_uda1341_update_bits(clnt, stat0, 3, 4, value, flush);
267 break;
268 case CMD_FORMAT:
269 ret = snd_uda1341_update_bits(clnt, stat0, 7, 1, value, flush);
270 break;
271 case CMD_OGAIN:
272 ret = snd_uda1341_update_bits(clnt, stat1, 1, 6, value, flush);
273 break;
274 case CMD_IGAIN:
275 ret = snd_uda1341_update_bits(clnt, stat1, 1, 5, value, flush);
276 break;
277 case CMD_DAC:
278 ret = snd_uda1341_update_bits(clnt, stat1, 1, 0, value, flush);
279 break;
280 case CMD_ADC:
281 ret = snd_uda1341_update_bits(clnt, stat1, 1, 1, value, flush);
282 break;
283 case CMD_VOLUME:
284 ret = snd_uda1341_update_bits(clnt, data0_0, 63, 0, value, flush);
285 break;
286 case CMD_BASS:
287 ret = snd_uda1341_update_bits(clnt, data0_1, 15, 2, value, flush);
288 break;
289 case CMD_TREBBLE:
290 ret = snd_uda1341_update_bits(clnt, data0_1, 3, 0, value, flush);
291 break;
292 case CMD_PEAK:
293 ret = snd_uda1341_update_bits(clnt, data0_2, 1, 5, value, flush);
294 break;
295 case CMD_DEEMP:
296 ret = snd_uda1341_update_bits(clnt, data0_2, 3, 3, value, flush);
297 break;
298 case CMD_MUTE:
299 ret = snd_uda1341_update_bits(clnt, data0_2, 1, 2, value, flush);
300 break;
301 case CMD_FILTER:
302 ret = snd_uda1341_update_bits(clnt, data0_2, 3, 0, value, flush);
303 break;
304 case CMD_CH1:
305 ret = snd_uda1341_update_bits(clnt, ext0, 31, 0, value, flush);
306 break;
307 case CMD_CH2:
308 ret = snd_uda1341_update_bits(clnt, ext1, 31, 0, value, flush);
309 break;
310 case CMD_MIC:
311 ret = snd_uda1341_update_bits(clnt, ext2, 7, 2, value, flush);
312 break;
313 case CMD_MIXER:
314 ret = snd_uda1341_update_bits(clnt, ext2, 3, 0, value, flush);
315 break;
316 case CMD_AGC:
317 ret = snd_uda1341_update_bits(clnt, ext4, 1, 4, value, flush);
318 break;
319 case CMD_IG:
320 ret = snd_uda1341_update_bits(clnt, ext4, 3, 0, value & 0x3, flush);
321 ret = snd_uda1341_update_bits(clnt, ext5, 31, 0, value >> 2, flush);
322 break;
323 case CMD_AGC_TIME:
324 ret = snd_uda1341_update_bits(clnt, ext6, 7, 2, value, flush);
325 break;
326 case CMD_AGC_LEVEL:
327 ret = snd_uda1341_update_bits(clnt, ext6, 3, 0, value, flush);
328 break;
329#ifdef CONFIG_PM
330 case CMD_SUSPEND:
331 for (reg = stat0; reg < uda1341_reg_last; reg++)
332 uda->suspend_regs[reg] = uda->regs[reg];
333 for (reg = 0; reg < CMD_LAST; reg++)
334 uda->suspend_cfg[reg] = uda->cfg[reg];
335 break;
336 case CMD_RESUME:
337 for (reg = stat0; reg < uda1341_reg_last; reg++)
338 snd_uda1341_codec_write(clnt, reg, uda->suspend_regs[reg]);
339 for (reg = 0; reg < CMD_LAST; reg++)
340 uda->cfg[reg] = uda->suspend_cfg[reg];
341 break;
342#endif
343 default:
344 ret = -EINVAL;
345 break;
346 }
347
348 if (!uda->active)
349 printk(KERN_ERR "UDA1341 codec not active!\n");
350 return ret;
351}
352
353/* }}} */
354
355/* {{{ Proc interface */
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100356#ifdef CONFIG_PROC_FS
357
358static const char *format_names[] = {
359 "I2S-bus",
360 "LSB 16bits",
361 "LSB 18bits",
362 "LSB 20bits",
363 "MSB",
364 "in LSB 16bits/out MSB",
365 "in LSB 18bits/out MSB",
366 "in LSB 20bits/out MSB",
367};
368
369static const char *fs_names[] = {
370 "512*fs",
371 "384*fs",
372 "256*fs",
373 "Unused - bad value!",
374};
375
376static const char* bass_values[][16] = {
377 {"0 dB", "0 dB", "0 dB", "0 dB", "0 dB", "0 dB", "0 dB", "0 dB", "0 dB", "0 dB", "0 dB",
378 "0 dB", "0 dB", "0 dB", "0 dB", "undefined", }, //flat
379 {"0 dB", "2 dB", "4 dB", "6 dB", "8 dB", "10 dB", "12 dB", "14 dB", "16 dB", "18 dB", "18 dB",
380 "18 dB", "18 dB", "18 dB", "18 dB", "undefined",}, // min
381 {"0 dB", "2 dB", "4 dB", "6 dB", "8 dB", "10 dB", "12 dB", "14 dB", "16 dB", "18 dB", "18 dB",
382 "18 dB", "18 dB", "18 dB", "18 dB", "undefined",}, // min
383 {"0 dB", "2 dB", "4 dB", "6 dB", "8 dB", "10 dB", "12 dB", "14 dB", "16 dB", "18 dB", "20 dB",
384 "22 dB", "24 dB", "24 dB", "24 dB", "undefined",}, // max
385};
386
387static const char *mic_sens_value[] = {
388 "-3 dB", "0 dB", "3 dB", "9 dB", "15 dB", "21 dB", "27 dB", "not used",
389};
390
391static const unsigned short AGC_atime[] = {
392 11, 16, 11, 16, 21, 11, 16, 21,
393};
394
395static const unsigned short AGC_dtime[] = {
396 100, 100, 200, 200, 200, 400, 400, 400,
397};
398
399static const char *AGC_level[] = {
400 "-9.0", "-11.5", "-15.0", "-17.5",
401};
402
403static const char *ig_small_value[] = {
404 "-3.0", "-2.5", "-2.0", "-1.5", "-1.0", "-0.5",
405};
406
407/*
408 * this was computed as peak_value[i] = pow((63-i)*1.42,1.013)
409 *
410 * UDA1341 datasheet on page 21: Peak value (dB) = (Peak level - 63.5)*5*log2
411 * There is an table with these values [level]=value: [3]=-90.31, [7]=-84.29
412 * [61]=-2.78, [62] = -1.48, [63] = 0.0
413 * I tried to compute it, but using but even using logarithm with base either 10 or 2
414 * i was'n able to get values in the table from the formula. So I constructed another
415 * formula (see above) to interpolate the values as good as possible. If there is some
416 * mistake, please contact me on tomas.kasparek@seznam.cz. Thanks.
417 * UDA1341TS datasheet is available at:
418 * http://www-us9.semiconductors.com/acrobat/datasheets/UDA1341TS_3.pdf
419 */
420static const char *peak_value[] = {
421 "-INF dB", "N.A.", "N.A", "90.31 dB", "N.A.", "N.A.", "N.A.", "-84.29 dB",
422 "-82.65 dB", "-81.13 dB", "-79.61 dB", "-78.09 dB", "-76.57 dB", "-75.05 dB", "-73.53 dB",
423 "-72.01 dB", "-70.49 dB", "-68.97 dB", "-67.45 dB", "-65.93 dB", "-64.41 dB", "-62.90 dB",
424 "-61.38 dB", "-59.86 dB", "-58.35 dB", "-56.83 dB", "-55.32 dB", "-53.80 dB", "-52.29 dB",
425 "-50.78 dB", "-49.26 dB", "-47.75 dB", "-46.24 dB", "-44.73 dB", "-43.22 dB", "-41.71 dB",
426 "-40.20 dB", "-38.69 dB", "-37.19 dB", "-35.68 dB", "-34.17 dB", "-32.67 dB", "-31.17 dB",
427 "-29.66 dB", "-28.16 dB", "-26.66 dB", "-25.16 dB", "-23.66 dB", "-22.16 dB", "-20.67 dB",
428 "-19.17 dB", "-17.68 dB", "-16.19 dB", "-14.70 dB", "-13.21 dB", "-11.72 dB", "-10.24 dB",
429 "-8.76 dB", "-7.28 dB", "-5.81 dB", "-4.34 dB", "-2.88 dB", "-1.43 dB", "0.00 dB",
430};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432static void snd_uda1341_proc_read(snd_info_entry_t *entry,
433 snd_info_buffer_t * buffer)
434{
435 struct l3_client *clnt = entry->private_data;
436 struct uda1341 *uda = clnt->driver_data;
437 int peak;
438
439 peak = snd_uda1341_codec_read(clnt, UDA1341_DATA1);
440 if (peak < 0)
441 peak = 0;
442
443 snd_iprintf(buffer, "%s\n\n", uda->card->longname);
444
445 // for information about computed values see UDA1341TS datasheet pages 15 - 21
446 snd_iprintf(buffer, "DAC power : %s\n", uda->cfg[CMD_DAC] ? "on" : "off");
447 snd_iprintf(buffer, "ADC power : %s\n", uda->cfg[CMD_ADC] ? "on" : "off");
448 snd_iprintf(buffer, "Clock frequency : %s\n", fs_names[uda->cfg[CMD_FS]]);
449 snd_iprintf(buffer, "Data format : %s\n\n", format_names[uda->cfg[CMD_FORMAT]]);
450
451 snd_iprintf(buffer, "Filter mode : %s\n", filter_names[uda->cfg[CMD_FILTER]]);
452 snd_iprintf(buffer, "Mixer mode : %s\n", mixer_names[uda->cfg[CMD_MIXER]]);
453 snd_iprintf(buffer, "De-emphasis : %s\n", deemp_names[uda->cfg[CMD_DEEMP]]);
454 snd_iprintf(buffer, "Peak detection pos. : %s\n", uda->cfg[CMD_PEAK] ? "after" : "before");
455 snd_iprintf(buffer, "Peak value : %s\n\n", peak_value[peak]);
456
457 snd_iprintf(buffer, "Automatic Gain Ctrl : %s\n", uda->cfg[CMD_AGC] ? "on" : "off");
458 snd_iprintf(buffer, "AGC attack time : %d ms\n", AGC_atime[uda->cfg[CMD_AGC_TIME]]);
459 snd_iprintf(buffer, "AGC decay time : %d ms\n", AGC_dtime[uda->cfg[CMD_AGC_TIME]]);
460 snd_iprintf(buffer, "AGC output level : %s dB\n\n", AGC_level[uda->cfg[CMD_AGC_LEVEL]]);
461
462 snd_iprintf(buffer, "Mute : %s\n", uda->cfg[CMD_MUTE] ? "on" : "off");
463
464 if (uda->cfg[CMD_VOLUME] == 0)
465 snd_iprintf(buffer, "Volume : 0 dB\n");
466 else if (uda->cfg[CMD_VOLUME] < 62)
467 snd_iprintf(buffer, "Volume : %d dB\n", -1*uda->cfg[CMD_VOLUME] +1);
468 else
469 snd_iprintf(buffer, "Volume : -INF dB\n");
470 snd_iprintf(buffer, "Bass : %s\n", bass_values[uda->cfg[CMD_FILTER]][uda->cfg[CMD_BASS]]);
471 snd_iprintf(buffer, "Trebble : %d dB\n", uda->cfg[CMD_FILTER] ? 2*uda->cfg[CMD_TREBBLE] : 0);
472 snd_iprintf(buffer, "Input Gain (6dB) : %s\n", uda->cfg[CMD_IGAIN] ? "on" : "off");
473 snd_iprintf(buffer, "Output Gain (6dB) : %s\n", uda->cfg[CMD_OGAIN] ? "on" : "off");
474 snd_iprintf(buffer, "Mic sensitivity : %s\n", mic_sens_value[uda->cfg[CMD_MIC]]);
475
476
477 if(uda->cfg[CMD_CH1] < 31)
478 snd_iprintf(buffer, "Mixer gain channel 1: -%d.%c dB\n",
479 ((uda->cfg[CMD_CH1] >> 1) * 3) + (uda->cfg[CMD_CH1] & 1),
480 uda->cfg[CMD_CH1] & 1 ? '5' : '0');
481 else
482 snd_iprintf(buffer, "Mixer gain channel 1: -INF dB\n");
483 if(uda->cfg[CMD_CH2] < 31)
484 snd_iprintf(buffer, "Mixer gain channel 2: -%d.%c dB\n",
485 ((uda->cfg[CMD_CH2] >> 1) * 3) + (uda->cfg[CMD_CH2] & 1),
486 uda->cfg[CMD_CH2] & 1 ? '5' : '0');
487 else
488 snd_iprintf(buffer, "Mixer gain channel 2: -INF dB\n");
489
490 if(uda->cfg[CMD_IG] > 5)
491 snd_iprintf(buffer, "Input Amp. Gain ch 2: %d.%c dB\n",
492 (uda->cfg[CMD_IG] >> 1) -3, uda->cfg[CMD_IG] & 1 ? '5' : '0');
493 else
494 snd_iprintf(buffer, "Input Amp. Gain ch 2: %s dB\n", ig_small_value[uda->cfg[CMD_IG]]);
495}
496
497static void snd_uda1341_proc_regs_read(snd_info_entry_t *entry,
498 snd_info_buffer_t * buffer)
499{
500 struct l3_client *clnt = entry->private_data;
501 struct uda1341 *uda = clnt->driver_data;
502 int reg;
503 char buf[12];
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 for (reg = 0; reg < uda1341_reg_last; reg ++) {
506 if (reg == empty)
507 continue;
508 int2str_bin8(uda->regs[reg], buf);
509 snd_iprintf(buffer, "%s = %s\n", uda1341_reg_names[reg], buf);
510 }
511
512 int2str_bin8(snd_uda1341_codec_read(clnt, UDA1341_DATA1), buf);
513 snd_iprintf(buffer, "DATA1 = %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100515#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517static void __devinit snd_uda1341_proc_init(snd_card_t *card, struct l3_client *clnt)
518{
519 snd_info_entry_t *entry;
520
521 if (! snd_card_proc_new(card, "uda1341", &entry))
522 snd_info_set_text_ops(entry, clnt, 1024, snd_uda1341_proc_read);
523 if (! snd_card_proc_new(card, "uda1341-regs", &entry))
524 snd_info_set_text_ops(entry, clnt, 1024, snd_uda1341_proc_regs_read);
525}
526
527/* }}} */
528
529/* {{{ Mixer controls setting */
530
531/* {{{ UDA1341 single functions */
532
533#define UDA1341_SINGLE(xname, where, reg, shift, mask, invert) \
534{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_uda1341_info_single, \
535 .get = snd_uda1341_get_single, .put = snd_uda1341_put_single, \
536 .private_value = where | (reg << 5) | (shift << 9) | (mask << 12) | (invert << 18) \
537}
538
539static int snd_uda1341_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
540{
541 int mask = (kcontrol->private_value >> 12) & 63;
542
543 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
544 uinfo->count = 1;
545 uinfo->value.integer.min = 0;
546 uinfo->value.integer.max = mask;
547 return 0;
548}
549
550static int snd_uda1341_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
551{
552 struct l3_client *clnt = snd_kcontrol_chip(kcontrol);
553 uda1341_t *uda = clnt->driver_data;
554 int where = kcontrol->private_value & 31;
555 int mask = (kcontrol->private_value >> 12) & 63;
556 int invert = (kcontrol->private_value >> 18) & 1;
557
558 ucontrol->value.integer.value[0] = uda->cfg[where];
559 if (invert)
560 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
561
562 return 0;
563}
564
565static int snd_uda1341_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
566{
567 struct l3_client *clnt = snd_kcontrol_chip(kcontrol);
568 uda1341_t *uda = clnt->driver_data;
569 int where = kcontrol->private_value & 31;
570 int reg = (kcontrol->private_value >> 5) & 15;
571 int shift = (kcontrol->private_value >> 9) & 7;
572 int mask = (kcontrol->private_value >> 12) & 63;
573 int invert = (kcontrol->private_value >> 18) & 1;
574 unsigned short val;
575
576 val = (ucontrol->value.integer.value[0] & mask);
577 if (invert)
578 val = mask - val;
579
580 uda->cfg[where] = val;
581 return snd_uda1341_update_bits(clnt, reg, mask, shift, val, FLUSH);
582}
583
584/* }}} */
585
586/* {{{ UDA1341 enum functions */
587
588#define UDA1341_ENUM(xname, where, reg, shift, mask, invert) \
589{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_uda1341_info_enum, \
590 .get = snd_uda1341_get_enum, .put = snd_uda1341_put_enum, \
591 .private_value = where | (reg << 5) | (shift << 9) | (mask << 12) | (invert << 18) \
592}
593
594static int snd_uda1341_info_enum(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
595{
596 int where = kcontrol->private_value & 31;
597 const char **texts;
598
599 // this register we don't handle this way
600 if (!uda1341_enum_items[where])
601 return -EINVAL;
602
603 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
604 uinfo->count = 1;
605 uinfo->value.enumerated.items = uda1341_enum_items[where];
606
607 if (uinfo->value.enumerated.item >= uda1341_enum_items[where])
608 uinfo->value.enumerated.item = uda1341_enum_items[where] - 1;
609
610 texts = uda1341_enum_names[where];
611 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
612 return 0;
613}
614
615static int snd_uda1341_get_enum(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
616{
617 struct l3_client *clnt = snd_kcontrol_chip(kcontrol);
618 uda1341_t *uda = clnt->driver_data;
619 int where = kcontrol->private_value & 31;
620
621 ucontrol->value.enumerated.item[0] = uda->cfg[where];
622 return 0;
623}
624
625static int snd_uda1341_put_enum(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
626{
627 struct l3_client *clnt = snd_kcontrol_chip(kcontrol);
628 uda1341_t *uda = clnt->driver_data;
629 int where = kcontrol->private_value & 31;
630 int reg = (kcontrol->private_value >> 5) & 15;
631 int shift = (kcontrol->private_value >> 9) & 7;
632 int mask = (kcontrol->private_value >> 12) & 63;
633
634 uda->cfg[where] = (ucontrol->value.enumerated.item[0] & mask);
635
636 return snd_uda1341_update_bits(clnt, reg, mask, shift, uda->cfg[where], FLUSH);
637}
638
639/* }}} */
640
641/* {{{ UDA1341 2regs functions */
642
643#define UDA1341_2REGS(xname, where, reg_1, reg_2, shift_1, shift_2, mask_1, mask_2, invert) \
644{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .info = snd_uda1341_info_2regs, \
645 .get = snd_uda1341_get_2regs, .put = snd_uda1341_put_2regs, \
646 .private_value = where | (reg_1 << 5) | (reg_2 << 9) | (shift_1 << 13) | (shift_2 << 16) | \
647 (mask_1 << 19) | (mask_2 << 25) | (invert << 31) \
648}
649
650
651static int snd_uda1341_info_2regs(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
652{
653 int mask_1 = (kcontrol->private_value >> 19) & 63;
654 int mask_2 = (kcontrol->private_value >> 25) & 63;
655 int mask;
656
657 mask = (mask_2 + 1) * (mask_1 + 1) - 1;
658 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
659 uinfo->count = 1;
660 uinfo->value.integer.min = 0;
661 uinfo->value.integer.max = mask;
662 return 0;
663}
664
665static int snd_uda1341_get_2regs(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
666{
667 struct l3_client *clnt = snd_kcontrol_chip(kcontrol);
668 uda1341_t *uda = clnt->driver_data;
669 int where = kcontrol->private_value & 31;
670 int mask_1 = (kcontrol->private_value >> 19) & 63;
671 int mask_2 = (kcontrol->private_value >> 25) & 63;
672 int invert = (kcontrol->private_value >> 31) & 1;
673 int mask;
674
675 mask = (mask_2 + 1) * (mask_1 + 1) - 1;
676
677 ucontrol->value.integer.value[0] = uda->cfg[where];
678 if (invert)
679 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
680 return 0;
681}
682
683static int snd_uda1341_put_2regs(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
684{
685 struct l3_client *clnt = snd_kcontrol_chip(kcontrol);
686 uda1341_t *uda = clnt->driver_data;
687 int where = kcontrol->private_value & 31;
688 int reg_1 = (kcontrol->private_value >> 5) & 15;
689 int reg_2 = (kcontrol->private_value >> 9) & 15;
690 int shift_1 = (kcontrol->private_value >> 13) & 7;
691 int shift_2 = (kcontrol->private_value >> 16) & 7;
692 int mask_1 = (kcontrol->private_value >> 19) & 63;
693 int mask_2 = (kcontrol->private_value >> 25) & 63;
694 int invert = (kcontrol->private_value >> 31) & 1;
695 int mask;
696 unsigned short val1, val2, val;
697
698 val = ucontrol->value.integer.value[0];
699
700 mask = (mask_2 + 1) * (mask_1 + 1) - 1;
701
702 val1 = val & mask_1;
703 val2 = (val / (mask_1 + 1)) & mask_2;
704
705 if (invert) {
706 val1 = mask_1 - val1;
707 val2 = mask_2 - val2;
708 }
709
710 uda->cfg[where] = invert ? mask - val : val;
711
712 //FIXME - return value
713 snd_uda1341_update_bits(clnt, reg_1, mask_1, shift_1, val1, FLUSH);
714 return snd_uda1341_update_bits(clnt, reg_2, mask_2, shift_2, val2, FLUSH);
715}
716
717/* }}} */
718
719static snd_kcontrol_new_t snd_uda1341_controls[] = {
720 UDA1341_SINGLE("Master Playback Switch", CMD_MUTE, data0_2, 2, 1, 1),
721 UDA1341_SINGLE("Master Playback Volume", CMD_VOLUME, data0_0, 0, 63, 1),
722
723 UDA1341_SINGLE("Bass Playback Volume", CMD_BASS, data0_1, 2, 15, 0),
724 UDA1341_SINGLE("Treble Playback Volume", CMD_TREBBLE, data0_1, 0, 3, 0),
725
726 UDA1341_SINGLE("Input Gain Switch", CMD_IGAIN, stat1, 5, 1, 0),
727 UDA1341_SINGLE("Output Gain Switch", CMD_OGAIN, stat1, 6, 1, 0),
728
729 UDA1341_SINGLE("Mixer Gain Channel 1 Volume", CMD_CH1, ext0, 0, 31, 1),
730 UDA1341_SINGLE("Mixer Gain Channel 2 Volume", CMD_CH2, ext1, 0, 31, 1),
731
732 UDA1341_SINGLE("Mic Sensitivity Volume", CMD_MIC, ext2, 2, 7, 0),
733
734 UDA1341_SINGLE("AGC Output Level", CMD_AGC_LEVEL, ext6, 0, 3, 0),
735 UDA1341_SINGLE("AGC Time Constant", CMD_AGC_TIME, ext6, 2, 7, 0),
736 UDA1341_SINGLE("AGC Time Constant Switch", CMD_AGC, ext4, 4, 1, 0),
737
738 UDA1341_SINGLE("DAC Power", CMD_DAC, stat1, 0, 1, 0),
739 UDA1341_SINGLE("ADC Power", CMD_ADC, stat1, 1, 1, 0),
740
741 UDA1341_ENUM("Peak detection", CMD_PEAK, data0_2, 5, 1, 0),
742 UDA1341_ENUM("De-emphasis", CMD_DEEMP, data0_2, 3, 3, 0),
743 UDA1341_ENUM("Mixer mode", CMD_MIXER, ext2, 0, 3, 0),
744 UDA1341_ENUM("Filter mode", CMD_FILTER, data0_2, 0, 3, 0),
745
746 UDA1341_2REGS("Gain Input Amplifier Gain (channel 2)", CMD_IG, ext4, ext5, 0, 0, 3, 31, 0),
747};
748
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100749static void uda1341_free(struct l3_client *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100751 l3_detach_client(clnt); // calls kfree for driver_data (uda1341_t)
752 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static int uda1341_dev_free(snd_device_t *device)
756{
757 struct l3_client *clnt = device->device_data;
758 uda1341_free(clnt);
759 return 0;
760}
761
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100762int __init snd_chip_uda1341_mixer_new(snd_card_t *card, struct l3_client **clntp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 static snd_device_ops_t ops = {
765 .dev_free = uda1341_dev_free,
766 };
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100767 struct l3_client *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int idx, err;
769
770 snd_assert(card != NULL, return -EINVAL);
771
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100772 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
773 if (clnt == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -ENOMEM;
775
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100776 if ((err = l3_attach_client(clnt, "l3-bit-sa1100-gpio", UDA1341_ALSA_NAME))) {
777 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return err;
779 }
780
781 for (idx = 0; idx < ARRAY_SIZE(snd_uda1341_controls); idx++) {
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100782 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_uda1341_controls[idx], clnt))) < 0) {
783 uda1341_free(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return err;
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100788 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, clnt, &ops)) < 0) {
789 uda1341_free(clnt);
790 return err;
791 }
792
793 *clntp = clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 strcpy(card->mixername, "UDA1341TS Mixer");
795 ((uda1341_t *)uda1341->driver_data)->card = card;
796
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100797 snd_uda1341_proc_init(card, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 return 0;
800}
801
802/* }}} */
803
804/* {{{ L3 operations */
805
806static int uda1341_attach(struct l3_client *clnt)
807{
808 struct uda1341 *uda;
809
Takashi Iwai561b2202005-09-09 14:22:34 +0200810 uda = kzalloc(sizeof(*uda), 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!uda)
812 return -ENOMEM;
813
814 /* init fixed parts of my copy of registers */
815 uda->regs[stat0] = STAT0;
816 uda->regs[stat1] = STAT1;
817
818 uda->regs[data0_0] = DATA0_0;
819 uda->regs[data0_1] = DATA0_1;
820 uda->regs[data0_2] = DATA0_2;
821
822 uda->write = snd_uda1341_codec_write;
823 uda->read = snd_uda1341_codec_read;
824
825 spin_lock_init(&uda->reg_lock);
826
827 clnt->driver_data = uda;
828 return 0;
829}
830
831static void uda1341_detach(struct l3_client *clnt)
832{
833 kfree(clnt->driver_data);
834}
835
836static int
837uda1341_command(struct l3_client *clnt, int cmd, void *arg)
838{
839 if (cmd != CMD_READ_REG)
840 return snd_uda1341_cfg_write(clnt, cmd, (int) arg, FLUSH);
841
842 return snd_uda1341_codec_read(clnt, (int) arg);
843}
844
845static int uda1341_open(struct l3_client *clnt)
846{
847 struct uda1341 *uda = clnt->driver_data;
848
849 uda->active = 1;
850
851 /* init default configuration */
852 snd_uda1341_cfg_write(clnt, CMD_RESET, 0, REGS_ONLY);
853 snd_uda1341_cfg_write(clnt, CMD_FS, F256, FLUSH); // unknown state after reset
854 snd_uda1341_cfg_write(clnt, CMD_FORMAT, LSB16, FLUSH); // unknown state after reset
855 snd_uda1341_cfg_write(clnt, CMD_OGAIN, ON, FLUSH); // default off after reset
856 snd_uda1341_cfg_write(clnt, CMD_IGAIN, ON, FLUSH); // default off after reset
857 snd_uda1341_cfg_write(clnt, CMD_DAC, ON, FLUSH); // ??? default value after reset
858 snd_uda1341_cfg_write(clnt, CMD_ADC, ON, FLUSH); // ??? default value after reset
859 snd_uda1341_cfg_write(clnt, CMD_VOLUME, 20, FLUSH); // default 0dB after reset
860 snd_uda1341_cfg_write(clnt, CMD_BASS, 0, REGS_ONLY); // default value after reset
861 snd_uda1341_cfg_write(clnt, CMD_TREBBLE, 0, REGS_ONLY); // default value after reset
862 snd_uda1341_cfg_write(clnt, CMD_PEAK, AFTER, REGS_ONLY);// default value after reset
863 snd_uda1341_cfg_write(clnt, CMD_DEEMP, NONE, REGS_ONLY);// default value after reset
864 //at this moment should be QMUTED by h3600_audio_init
865 snd_uda1341_cfg_write(clnt, CMD_MUTE, OFF, REGS_ONLY); // default value after reset
866 snd_uda1341_cfg_write(clnt, CMD_FILTER, MAX, FLUSH); // defaul flat after reset
867 snd_uda1341_cfg_write(clnt, CMD_CH1, 31, FLUSH); // default value after reset
868 snd_uda1341_cfg_write(clnt, CMD_CH2, 4, FLUSH); // default value after reset
869 snd_uda1341_cfg_write(clnt, CMD_MIC, 4, FLUSH); // default 0dB after reset
870 snd_uda1341_cfg_write(clnt, CMD_MIXER, MIXER, FLUSH); // default doub.dif.mode
871 snd_uda1341_cfg_write(clnt, CMD_AGC, OFF, FLUSH); // default value after reset
872 snd_uda1341_cfg_write(clnt, CMD_IG, 0, FLUSH); // unknown state after reset
873 snd_uda1341_cfg_write(clnt, CMD_AGC_TIME, 0, FLUSH); // default value after reset
874 snd_uda1341_cfg_write(clnt, CMD_AGC_LEVEL, 0, FLUSH); // default value after reset
875
876 return 0;
877}
878
879static void uda1341_close(struct l3_client *clnt)
880{
881 struct uda1341 *uda = clnt->driver_data;
882
883 uda->active = 0;
884}
885
886/* }}} */
887
888/* {{{ Module and L3 initialization */
889
890static struct l3_ops uda1341_ops = {
891 .open = uda1341_open,
892 .command = uda1341_command,
893 .close = uda1341_close,
894};
895
896static struct l3_driver uda1341_driver = {
897 .name = UDA1341_ALSA_NAME,
898 .attach_client = uda1341_attach,
899 .detach_client = uda1341_detach,
900 .ops = &uda1341_ops,
901 .owner = THIS_MODULE,
902};
903
904static int __init uda1341_init(void)
905{
906 return l3_add_driver(&uda1341_driver);
907}
908
909static void __exit uda1341_exit(void)
910{
911 l3_del_driver(&uda1341_driver);
912}
913
914module_init(uda1341_init);
915module_exit(uda1341_exit);
916
917MODULE_AUTHOR("Tomas Kasparek <tomas.kasparek@seznam.cz>");
918MODULE_LICENSE("GPL");
919MODULE_DESCRIPTION("Philips UDA1341 CODEC driver for ALSA");
920MODULE_SUPPORTED_DEVICE("{{UDA1341,UDA1341TS}}");
921
922EXPORT_SYMBOL(snd_chip_uda1341_mixer_new);
923
924/* }}} */
925
926/*
927 * Local variables:
928 * indent-tabs-mode: t
929 * End:
930 */