aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2011-05-22 04:01:22 -0400
committerTakashi Iwai <tiwai@suse.de>2011-05-22 04:01:22 -0400
commitb759b3ac9aee3afb01c21b603970ebb200c8048e (patch)
tree8a7bc11819410c47d248a3fc27579190853179df /sound
parente28fb9c603f321e0bb4c6c43d58b31de052d6de6 (diff)
parentf428c94c842343b8580751cf637794050ca12d77 (diff)
Merge branch 'topic/lola' into for-linus
Diffstat (limited to 'sound')
-rw-r--r--sound/pci/Kconfig9
-rw-r--r--sound/pci/Makefile1
-rw-r--r--sound/pci/lola/Makefile4
-rw-r--r--sound/pci/lola/lola.c791
-rw-r--r--sound/pci/lola/lola.h527
-rw-r--r--sound/pci/lola/lola_clock.c323
-rw-r--r--sound/pci/lola/lola_mixer.c839
-rw-r--r--sound/pci/lola/lola_pcm.c706
-rw-r--r--sound/pci/lola/lola_proc.c222
9 files changed, 3422 insertions, 0 deletions
diff --git a/sound/pci/Kconfig b/sound/pci/Kconfig
index 389cd7931668..8d2856fb4d97 100644
--- a/sound/pci/Kconfig
+++ b/sound/pci/Kconfig
@@ -658,6 +658,15 @@ config SND_KORG1212
658 To compile this driver as a module, choose M here: the module 658 To compile this driver as a module, choose M here: the module
659 will be called snd-korg1212. 659 will be called snd-korg1212.
660 660
661config SND_LOLA
662 tristate "Digigram Lola"
663 select SND_PCM
664 help
665 Say Y to include support for Digigram Lola boards.
666
667 To compile this driver as a module, choose M here: the module
668 will be called snd-lola.
669
661config SND_LX6464ES 670config SND_LX6464ES
662 tristate "Digigram LX6464ES" 671 tristate "Digigram LX6464ES"
663 select SND_PCM 672 select SND_PCM
diff --git a/sound/pci/Makefile b/sound/pci/Makefile
index 9cf4348ec137..54fe325e3aa5 100644
--- a/sound/pci/Makefile
+++ b/sound/pci/Makefile
@@ -64,6 +64,7 @@ obj-$(CONFIG_SND) += \
64 ca0106/ \ 64 ca0106/ \
65 cs46xx/ \ 65 cs46xx/ \
66 cs5535audio/ \ 66 cs5535audio/ \
67 lola/ \
67 lx6464es/ \ 68 lx6464es/ \
68 echoaudio/ \ 69 echoaudio/ \
69 emu10k1/ \ 70 emu10k1/ \
diff --git a/sound/pci/lola/Makefile b/sound/pci/lola/Makefile
new file mode 100644
index 000000000000..8178a2a59d00
--- /dev/null
+++ b/sound/pci/lola/Makefile
@@ -0,0 +1,4 @@
1snd-lola-y := lola.o lola_pcm.o lola_clock.o lola_mixer.o
2snd-lola-$(CONFIG_SND_DEBUG) += lola_proc.o
3
4obj-$(CONFIG_SND_LOLA) += snd-lola.o
diff --git a/sound/pci/lola/lola.c b/sound/pci/lola/lola.c
new file mode 100644
index 000000000000..34b24286d279
--- /dev/null
+++ b/sound/pci/lola/lola.c
@@ -0,0 +1,791 @@
1/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/moduleparam.h>
24#include <linux/dma-mapping.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/slab.h>
28#include <linux/pci.h>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/pcm.h>
32#include <sound/initval.h>
33#include "lola.h"
34
35/* Standard options */
36static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
37static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
38static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
39
40module_param_array(index, int, NULL, 0444);
41MODULE_PARM_DESC(index, "Index value for Digigram Lola driver.");
42module_param_array(id, charp, NULL, 0444);
43MODULE_PARM_DESC(id, "ID string for Digigram Lola driver.");
44module_param_array(enable, bool, NULL, 0444);
45MODULE_PARM_DESC(enable, "Enable Digigram Lola driver.");
46
47/* Lola-specific options */
48
49/* for instance use always max granularity which is compatible
50 * with all sample rates
51 */
52static int granularity[SNDRV_CARDS] = {
53 [0 ... (SNDRV_CARDS - 1)] = LOLA_GRANULARITY_MAX
54};
55
56/* below a sample_rate of 16kHz the analogue audio quality is NOT excellent */
57static int sample_rate_min[SNDRV_CARDS] = {
58 [0 ... (SNDRV_CARDS - 1) ] = 16000
59};
60
61module_param_array(granularity, int, NULL, 0444);
62MODULE_PARM_DESC(granularity, "Granularity value");
63module_param_array(sample_rate_min, int, NULL, 0444);
64MODULE_PARM_DESC(sample_rate_min, "Minimal sample rate");
65
66/*
67 */
68
69MODULE_LICENSE("GPL");
70MODULE_SUPPORTED_DEVICE("{{Digigram, Lola}}");
71MODULE_DESCRIPTION("Digigram Lola driver");
72MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
73
74#ifdef CONFIG_SND_DEBUG_VERBOSE
75static int debug;
76module_param(debug, int, 0644);
77#define verbose_debug(fmt, args...) \
78 do { if (debug > 1) printk(KERN_DEBUG SFX fmt, ##args); } while (0)
79#else
80#define verbose_debug(fmt, args...)
81#endif
82
83/*
84 * pseudo-codec read/write via CORB/RIRB
85 */
86
87static int corb_send_verb(struct lola *chip, unsigned int nid,
88 unsigned int verb, unsigned int data,
89 unsigned int extdata)
90{
91 unsigned long flags;
92 int ret = -EIO;
93
94 chip->last_cmd_nid = nid;
95 chip->last_verb = verb;
96 chip->last_data = data;
97 chip->last_extdata = extdata;
98 data |= (nid << 20) | (verb << 8);
99
100 spin_lock_irqsave(&chip->reg_lock, flags);
101 if (chip->rirb.cmds < LOLA_CORB_ENTRIES - 1) {
102 unsigned int wp = chip->corb.wp + 1;
103 wp %= LOLA_CORB_ENTRIES;
104 chip->corb.wp = wp;
105 chip->corb.buf[wp * 2] = cpu_to_le32(data);
106 chip->corb.buf[wp * 2 + 1] = cpu_to_le32(extdata);
107 lola_writew(chip, BAR0, CORBWP, wp);
108 chip->rirb.cmds++;
109 smp_wmb();
110 ret = 0;
111 }
112 spin_unlock_irqrestore(&chip->reg_lock, flags);
113 return ret;
114}
115
116static void lola_queue_unsol_event(struct lola *chip, unsigned int res,
117 unsigned int res_ex)
118{
119 lola_update_ext_clock_freq(chip, res);
120}
121
122/* retrieve RIRB entry - called from interrupt handler */
123static void lola_update_rirb(struct lola *chip)
124{
125 unsigned int rp, wp;
126 u32 res, res_ex;
127
128 wp = lola_readw(chip, BAR0, RIRBWP);
129 if (wp == chip->rirb.wp)
130 return;
131 chip->rirb.wp = wp;
132
133 while (chip->rirb.rp != wp) {
134 chip->rirb.rp++;
135 chip->rirb.rp %= LOLA_CORB_ENTRIES;
136
137 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
138 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
139 res = le32_to_cpu(chip->rirb.buf[rp]);
140 if (res_ex & LOLA_RIRB_EX_UNSOL_EV)
141 lola_queue_unsol_event(chip, res, res_ex);
142 else if (chip->rirb.cmds) {
143 chip->res = res;
144 chip->res_ex = res_ex;
145 smp_wmb();
146 chip->rirb.cmds--;
147 }
148 }
149}
150
151static int rirb_get_response(struct lola *chip, unsigned int *val,
152 unsigned int *extval)
153{
154 unsigned long timeout;
155
156 again:
157 timeout = jiffies + msecs_to_jiffies(1000);
158 for (;;) {
159 if (chip->polling_mode) {
160 spin_lock_irq(&chip->reg_lock);
161 lola_update_rirb(chip);
162 spin_unlock_irq(&chip->reg_lock);
163 }
164 if (!chip->rirb.cmds) {
165 *val = chip->res;
166 if (extval)
167 *extval = chip->res_ex;
168 verbose_debug("get_response: %x, %x\n",
169 chip->res, chip->res_ex);
170 if (chip->res_ex & LOLA_RIRB_EX_ERROR) {
171 printk(KERN_WARNING SFX "RIRB ERROR: "
172 "NID=%x, verb=%x, data=%x, ext=%x\n",
173 chip->last_cmd_nid,
174 chip->last_verb, chip->last_data,
175 chip->last_extdata);
176 return -EIO;
177 }
178 return 0;
179 }
180 if (time_after(jiffies, timeout))
181 break;
182 udelay(20);
183 cond_resched();
184 }
185 printk(KERN_WARNING SFX "RIRB response error\n");
186 if (!chip->polling_mode) {
187 printk(KERN_WARNING SFX "switching to polling mode\n");
188 chip->polling_mode = 1;
189 goto again;
190 }
191 return -EIO;
192}
193
194/* aynchronous write of a codec verb with data */
195int lola_codec_write(struct lola *chip, unsigned int nid, unsigned int verb,
196 unsigned int data, unsigned int extdata)
197{
198 verbose_debug("codec_write NID=%x, verb=%x, data=%x, ext=%x\n",
199 nid, verb, data, extdata);
200 return corb_send_verb(chip, nid, verb, data, extdata);
201}
202
203/* write a codec verb with data and read the returned status */
204int lola_codec_read(struct lola *chip, unsigned int nid, unsigned int verb,
205 unsigned int data, unsigned int extdata,
206 unsigned int *val, unsigned int *extval)
207{
208 int err;
209
210 verbose_debug("codec_read NID=%x, verb=%x, data=%x, ext=%x\n",
211 nid, verb, data, extdata);
212 err = corb_send_verb(chip, nid, verb, data, extdata);
213 if (err < 0)
214 return err;
215 err = rirb_get_response(chip, val, extval);
216 return err;
217}
218
219/* flush all pending codec writes */
220int lola_codec_flush(struct lola *chip)
221{
222 unsigned int tmp;
223 return rirb_get_response(chip, &tmp, NULL);
224}
225
226/*
227 * interrupt handler
228 */
229static irqreturn_t lola_interrupt(int irq, void *dev_id)
230{
231 struct lola *chip = dev_id;
232 unsigned int notify_ins, notify_outs, error_ins, error_outs;
233 int handled = 0;
234 int i;
235
236 notify_ins = notify_outs = error_ins = error_outs = 0;
237 spin_lock(&chip->reg_lock);
238 for (;;) {
239 unsigned int status, in_sts, out_sts;
240 unsigned int reg;
241
242 status = lola_readl(chip, BAR1, DINTSTS);
243 if (!status || status == -1)
244 break;
245
246 in_sts = lola_readl(chip, BAR1, DIINTSTS);
247 out_sts = lola_readl(chip, BAR1, DOINTSTS);
248
249 /* clear Input Interrupts */
250 for (i = 0; in_sts && i < chip->pcm[CAPT].num_streams; i++) {
251 if (!(in_sts & (1 << i)))
252 continue;
253 in_sts &= ~(1 << i);
254 reg = lola_dsd_read(chip, i, STS);
255 if (reg & LOLA_DSD_STS_DESE) /* error */
256 error_ins |= (1 << i);
257 if (reg & LOLA_DSD_STS_BCIS) /* notify */
258 notify_ins |= (1 << i);
259 /* clear */
260 lola_dsd_write(chip, i, STS, reg);
261 }
262
263 /* clear Output Interrupts */
264 for (i = 0; out_sts && i < chip->pcm[PLAY].num_streams; i++) {
265 if (!(out_sts & (1 << i)))
266 continue;
267 out_sts &= ~(1 << i);
268 reg = lola_dsd_read(chip, i + MAX_STREAM_IN_COUNT, STS);
269 if (reg & LOLA_DSD_STS_DESE) /* error */
270 error_outs |= (1 << i);
271 if (reg & LOLA_DSD_STS_BCIS) /* notify */
272 notify_outs |= (1 << i);
273 lola_dsd_write(chip, i + MAX_STREAM_IN_COUNT, STS, reg);
274 }
275
276 if (status & LOLA_DINT_CTRL) {
277 unsigned char rbsts; /* ring status is byte access */
278 rbsts = lola_readb(chip, BAR0, RIRBSTS);
279 rbsts &= LOLA_RIRB_INT_MASK;
280 if (rbsts)
281 lola_writeb(chip, BAR0, RIRBSTS, rbsts);
282 rbsts = lola_readb(chip, BAR0, CORBSTS);
283 rbsts &= LOLA_CORB_INT_MASK;
284 if (rbsts)
285 lola_writeb(chip, BAR0, CORBSTS, rbsts);
286
287 lola_update_rirb(chip);
288 }
289
290 if (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)) {
291 /* clear global fifo error interrupt */
292 lola_writel(chip, BAR1, DINTSTS,
293 (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)));
294 }
295 handled = 1;
296 }
297 spin_unlock(&chip->reg_lock);
298
299 lola_pcm_update(chip, &chip->pcm[CAPT], notify_ins);
300 lola_pcm_update(chip, &chip->pcm[PLAY], notify_outs);
301
302 return IRQ_RETVAL(handled);
303}
304
305
306/*
307 * controller
308 */
309static int reset_controller(struct lola *chip)
310{
311 unsigned int gctl = lola_readl(chip, BAR0, GCTL);
312 unsigned long end_time;
313
314 if (gctl) {
315 /* to be sure */
316 lola_writel(chip, BAR1, BOARD_MODE, 0);
317 return 0;
318 }
319
320 chip->cold_reset = 1;
321 lola_writel(chip, BAR0, GCTL, LOLA_GCTL_RESET);
322 end_time = jiffies + msecs_to_jiffies(200);
323 do {
324 msleep(1);
325 gctl = lola_readl(chip, BAR0, GCTL);
326 if (gctl)
327 break;
328 } while (time_before(jiffies, end_time));
329 if (!gctl) {
330 printk(KERN_ERR SFX "cannot reset controller\n");
331 return -EIO;
332 }
333 return 0;
334}
335
336static void lola_irq_enable(struct lola *chip)
337{
338 unsigned int val;
339
340 /* enalbe all I/O streams */
341 val = (1 << chip->pcm[PLAY].num_streams) - 1;
342 lola_writel(chip, BAR1, DOINTCTL, val);
343 val = (1 << chip->pcm[CAPT].num_streams) - 1;
344 lola_writel(chip, BAR1, DIINTCTL, val);
345
346 /* enable global irqs */
347 val = LOLA_DINT_GLOBAL | LOLA_DINT_CTRL | LOLA_DINT_FIFOERR |
348 LOLA_DINT_MUERR;
349 lola_writel(chip, BAR1, DINTCTL, val);
350}
351
352static void lola_irq_disable(struct lola *chip)
353{
354 lola_writel(chip, BAR1, DINTCTL, 0);
355 lola_writel(chip, BAR1, DIINTCTL, 0);
356 lola_writel(chip, BAR1, DOINTCTL, 0);
357}
358
359static int setup_corb_rirb(struct lola *chip)
360{
361 int err;
362 unsigned char tmp;
363 unsigned long end_time;
364
365 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
366 snd_dma_pci_data(chip->pci),
367 PAGE_SIZE, &chip->rb);
368 if (err < 0)
369 return err;
370
371 chip->corb.addr = chip->rb.addr;
372 chip->corb.buf = (u32 *)chip->rb.area;
373 chip->rirb.addr = chip->rb.addr + 2048;
374 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
375
376 /* disable ringbuffer DMAs */
377 lola_writeb(chip, BAR0, RIRBCTL, 0);
378 lola_writeb(chip, BAR0, CORBCTL, 0);
379
380 end_time = jiffies + msecs_to_jiffies(200);
381 do {
382 if (!lola_readb(chip, BAR0, RIRBCTL) &&
383 !lola_readb(chip, BAR0, CORBCTL))
384 break;
385 msleep(1);
386 } while (time_before(jiffies, end_time));
387
388 /* CORB set up */
389 lola_writel(chip, BAR0, CORBLBASE, (u32)chip->corb.addr);
390 lola_writel(chip, BAR0, CORBUBASE, upper_32_bits(chip->corb.addr));
391 /* set the corb size to 256 entries */
392 lola_writeb(chip, BAR0, CORBSIZE, 0x02);
393 /* set the corb write pointer to 0 */
394 lola_writew(chip, BAR0, CORBWP, 0);
395 /* reset the corb hw read pointer */
396 lola_writew(chip, BAR0, CORBRP, LOLA_RBRWP_CLR);
397 /* enable corb dma */
398 lola_writeb(chip, BAR0, CORBCTL, LOLA_RBCTL_DMA_EN);
399 /* clear flags if set */
400 tmp = lola_readb(chip, BAR0, CORBSTS) & LOLA_CORB_INT_MASK;
401 if (tmp)
402 lola_writeb(chip, BAR0, CORBSTS, tmp);
403 chip->corb.wp = 0;
404
405 /* RIRB set up */
406 lola_writel(chip, BAR0, RIRBLBASE, (u32)chip->rirb.addr);
407 lola_writel(chip, BAR0, RIRBUBASE, upper_32_bits(chip->rirb.addr));
408 /* set the rirb size to 256 entries */
409 lola_writeb(chip, BAR0, RIRBSIZE, 0x02);
410 /* reset the rirb hw write pointer */
411 lola_writew(chip, BAR0, RIRBWP, LOLA_RBRWP_CLR);
412 /* set N=1, get RIRB response interrupt for new entry */
413 lola_writew(chip, BAR0, RINTCNT, 1);
414 /* enable rirb dma and response irq */
415 lola_writeb(chip, BAR0, RIRBCTL, LOLA_RBCTL_DMA_EN | LOLA_RBCTL_IRQ_EN);
416 /* clear flags if set */
417 tmp = lola_readb(chip, BAR0, RIRBSTS) & LOLA_RIRB_INT_MASK;
418 if (tmp)
419 lola_writeb(chip, BAR0, RIRBSTS, tmp);
420 chip->rirb.rp = chip->rirb.cmds = 0;
421
422 return 0;
423}
424
425static void stop_corb_rirb(struct lola *chip)
426{
427 /* disable ringbuffer DMAs */
428 lola_writeb(chip, BAR0, RIRBCTL, 0);
429 lola_writeb(chip, BAR0, CORBCTL, 0);
430}
431
432static void lola_reset_setups(struct lola *chip)
433{
434 /* update the granularity */
435 lola_set_granularity(chip, chip->granularity, true);
436 /* update the sample clock */
437 lola_set_clock_index(chip, chip->clock.cur_index);
438 /* enable unsolicited events of the clock widget */
439 lola_enable_clock_events(chip);
440 /* update the analog gains */
441 lola_setup_all_analog_gains(chip, CAPT, false); /* input, update */
442 /* update SRC configuration if applicable */
443 lola_set_src_config(chip, chip->input_src_mask, false);
444 /* update the analog outputs */
445 lola_setup_all_analog_gains(chip, PLAY, false); /* output, update */
446}
447
448static int lola_parse_tree(struct lola *chip)
449{
450 unsigned int val;
451 int nid, err;
452
453 err = lola_read_param(chip, 0, LOLA_PAR_VENDOR_ID, &val);
454 if (err < 0) {
455 printk(KERN_ERR SFX "Can't read VENDOR_ID\n");
456 return err;
457 }
458 val >>= 16;
459 if (val != 0x1369) {
460 printk(KERN_ERR SFX "Unknown codec vendor 0x%x\n", val);
461 return -EINVAL;
462 }
463
464 err = lola_read_param(chip, 1, LOLA_PAR_FUNCTION_TYPE, &val);
465 if (err < 0) {
466 printk(KERN_ERR SFX "Can't read FUNCTION_TYPE for 0x%x\n", nid);
467 return err;
468 }
469 if (val != 1) {
470 printk(KERN_ERR SFX "Unknown function type %d\n", val);
471 return -EINVAL;
472 }
473
474 err = lola_read_param(chip, 1, LOLA_PAR_SPECIFIC_CAPS, &val);
475 if (err < 0) {
476 printk(KERN_ERR SFX "Can't read SPECCAPS\n");
477 return err;
478 }
479 chip->lola_caps = val;
480 chip->pin[CAPT].num_pins = LOLA_AFG_INPUT_PIN_COUNT(chip->lola_caps);
481 chip->pin[PLAY].num_pins = LOLA_AFG_OUTPUT_PIN_COUNT(chip->lola_caps);
482 snd_printdd(SFX "speccaps=0x%x, pins in=%d, out=%d\n",
483 chip->lola_caps,
484 chip->pin[CAPT].num_pins, chip->pin[PLAY].num_pins);
485
486 if (chip->pin[CAPT].num_pins > MAX_AUDIO_INOUT_COUNT ||
487 chip->pin[PLAY].num_pins > MAX_AUDIO_INOUT_COUNT) {
488 printk(KERN_ERR SFX "Invalid Lola-spec caps 0x%x\n", val);
489 return -EINVAL;
490 }
491
492 nid = 0x02;
493 err = lola_init_pcm(chip, CAPT, &nid);
494 if (err < 0)
495 return err;
496 err = lola_init_pcm(chip, PLAY, &nid);
497 if (err < 0)
498 return err;
499
500 err = lola_init_pins(chip, CAPT, &nid);
501 if (err < 0)
502 return err;
503 err = lola_init_pins(chip, PLAY, &nid);
504 if (err < 0)
505 return err;
506
507 if (LOLA_AFG_CLOCK_WIDGET_PRESENT(chip->lola_caps)) {
508 err = lola_init_clock_widget(chip, nid);
509 if (err < 0)
510 return err;
511 nid++;
512 }
513 if (LOLA_AFG_MIXER_WIDGET_PRESENT(chip->lola_caps)) {
514 err = lola_init_mixer_widget(chip, nid);
515 if (err < 0)
516 return err;
517 nid++;
518 }
519
520 /* enable unsolicited events of the clock widget */
521 err = lola_enable_clock_events(chip);
522 if (err < 0)
523 return err;
524
525 /* if last ResetController was not a ColdReset, we don't know
526 * the state of the card; initialize here again
527 */
528 if (!chip->cold_reset) {
529 lola_reset_setups(chip);
530 chip->cold_reset = 1;
531 } else {
532 /* set the granularity if it is not the default */
533 if (chip->granularity != LOLA_GRANULARITY_MIN)
534 lola_set_granularity(chip, chip->granularity, true);
535 }
536
537 return 0;
538}
539
540static void lola_stop_hw(struct lola *chip)
541{
542 stop_corb_rirb(chip);
543 lola_irq_disable(chip);
544}
545
546static void lola_free(struct lola *chip)
547{
548 if (chip->initialized)
549 lola_stop_hw(chip);
550 lola_free_pcm(chip);
551 lola_free_mixer(chip);
552 if (chip->irq >= 0)
553 free_irq(chip->irq, (void *)chip);
554 if (chip->bar[0].remap_addr)
555 iounmap(chip->bar[0].remap_addr);
556 if (chip->bar[1].remap_addr)
557 iounmap(chip->bar[1].remap_addr);
558 if (chip->rb.area)
559 snd_dma_free_pages(&chip->rb);
560 pci_release_regions(chip->pci);
561 pci_disable_device(chip->pci);
562 kfree(chip);
563}
564
565static int lola_dev_free(struct snd_device *device)
566{
567 lola_free(device->device_data);
568 return 0;
569}
570
571static int __devinit lola_create(struct snd_card *card, struct pci_dev *pci,
572 int dev, struct lola **rchip)
573{
574 struct lola *chip;
575 int err;
576 unsigned int dever;
577 static struct snd_device_ops ops = {
578 .dev_free = lola_dev_free,
579 };
580
581 *rchip = NULL;
582
583 err = pci_enable_device(pci);
584 if (err < 0)
585 return err;
586
587 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
588 if (!chip) {
589 snd_printk(KERN_ERR SFX "cannot allocate chip\n");
590 pci_disable_device(pci);
591 return -ENOMEM;
592 }
593
594 spin_lock_init(&chip->reg_lock);
595 mutex_init(&chip->open_mutex);
596 chip->card = card;
597 chip->pci = pci;
598 chip->irq = -1;
599
600 chip->granularity = granularity[dev];
601 switch (chip->granularity) {
602 case 8:
603 chip->sample_rate_max = 48000;
604 break;
605 case 16:
606 chip->sample_rate_max = 96000;
607 break;
608 case 32:
609 chip->sample_rate_max = 192000;
610 break;
611 default:
612 snd_printk(KERN_WARNING SFX
613 "Invalid granularity %d, reset to %d\n",
614 chip->granularity, LOLA_GRANULARITY_MAX);
615 chip->granularity = LOLA_GRANULARITY_MAX;
616 chip->sample_rate_max = 192000;
617 break;
618 }
619 chip->sample_rate_min = sample_rate_min[dev];
620 if (chip->sample_rate_min > chip->sample_rate_max) {
621 snd_printk(KERN_WARNING SFX
622 "Invalid sample_rate_min %d, reset to 16000\n",
623 chip->sample_rate_min);
624 chip->sample_rate_min = 16000;
625 }
626
627 err = pci_request_regions(pci, DRVNAME);
628 if (err < 0) {
629 kfree(chip);
630 pci_disable_device(pci);
631 return err;
632 }
633
634 chip->bar[0].addr = pci_resource_start(pci, 0);
635 chip->bar[0].remap_addr = pci_ioremap_bar(pci, 0);
636 chip->bar[1].addr = pci_resource_start(pci, 2);
637 chip->bar[1].remap_addr = pci_ioremap_bar(pci, 2);
638 if (!chip->bar[0].remap_addr || !chip->bar[1].remap_addr) {
639 snd_printk(KERN_ERR SFX "ioremap error\n");
640 err = -ENXIO;
641 goto errout;
642 }
643
644 pci_set_master(pci);
645
646 err = reset_controller(chip);
647 if (err < 0)
648 goto errout;
649
650 if (request_irq(pci->irq, lola_interrupt, IRQF_SHARED,
651 DRVNAME, chip)) {
652 printk(KERN_ERR SFX "unable to grab IRQ %d\n", pci->irq);
653 err = -EBUSY;
654 goto errout;
655 }
656 chip->irq = pci->irq;
657 synchronize_irq(chip->irq);
658
659 dever = lola_readl(chip, BAR1, DEVER);
660 chip->pcm[CAPT].num_streams = (dever >> 0) & 0x3ff;
661 chip->pcm[PLAY].num_streams = (dever >> 10) & 0x3ff;
662 chip->version = (dever >> 24) & 0xff;
663 snd_printdd(SFX "streams in=%d, out=%d, version=0x%x\n",
664 chip->pcm[CAPT].num_streams, chip->pcm[PLAY].num_streams,
665 chip->version);
666
667 /* Test LOLA_BAR1_DEVER */
668 if (chip->pcm[CAPT].num_streams > MAX_STREAM_IN_COUNT ||
669 chip->pcm[PLAY].num_streams > MAX_STREAM_OUT_COUNT ||
670 (!chip->pcm[CAPT].num_streams &&
671 !chip->pcm[PLAY].num_streams)) {
672 printk(KERN_ERR SFX "invalid DEVER = %x\n", dever);
673 err = -EINVAL;
674 goto errout;
675 }
676
677 err = setup_corb_rirb(chip);
678 if (err < 0)
679 goto errout;
680
681 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
682 if (err < 0) {
683 snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
684 goto errout;
685 }
686
687 strcpy(card->driver, "Lola");
688 strlcpy(card->shortname, "Digigram Lola", sizeof(card->shortname));
689 snprintf(card->longname, sizeof(card->longname),
690 "%s at 0x%lx irq %i",
691 card->shortname, chip->bar[0].addr, chip->irq);
692 strcpy(card->mixername, card->shortname);
693
694 lola_irq_enable(chip);
695
696 chip->initialized = 1;
697 *rchip = chip;
698 return 0;
699
700 errout:
701 lola_free(chip);
702 return err;
703}
704
705static int __devinit lola_probe(struct pci_dev *pci,
706 const struct pci_device_id *pci_id)
707{
708 static int dev;
709 struct snd_card *card;
710 struct lola *chip;
711 int err;
712
713 if (dev >= SNDRV_CARDS)
714 return -ENODEV;
715 if (!enable[dev]) {
716 dev++;
717 return -ENOENT;
718 }
719
720 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
721 if (err < 0) {
722 snd_printk(KERN_ERR SFX "Error creating card!\n");
723 return err;
724 }
725
726 snd_card_set_dev(card, &pci->dev);
727
728 err = lola_create(card, pci, dev, &chip);
729 if (err < 0)
730 goto out_free;
731 card->private_data = chip;
732
733 err = lola_parse_tree(chip);
734 if (err < 0)
735 goto out_free;
736
737 err = lola_create_pcm(chip);
738 if (err < 0)
739 goto out_free;
740
741 err = lola_create_mixer(chip);
742 if (err < 0)
743 goto out_free;
744
745 lola_proc_debug_new(chip);
746
747 err = snd_card_register(card);
748 if (err < 0)
749 goto out_free;
750
751 pci_set_drvdata(pci, card);
752 dev++;
753 return err;
754out_free:
755 snd_card_free(card);
756 return err;
757}
758
759static void __devexit lola_remove(struct pci_dev *pci)
760{
761 snd_card_free(pci_get_drvdata(pci));
762 pci_set_drvdata(pci, NULL);
763}
764
765/* PCI IDs */
766static DEFINE_PCI_DEVICE_TABLE(lola_ids) = {
767 { PCI_VDEVICE(DIGIGRAM, 0x0001) },
768 { 0, }
769};
770MODULE_DEVICE_TABLE(pci, lola_ids);
771
772/* pci_driver definition */
773static struct pci_driver driver = {
774 .name = DRVNAME,
775 .id_table = lola_ids,
776 .probe = lola_probe,
777 .remove = __devexit_p(lola_remove),
778};
779
780static int __init alsa_card_lola_init(void)
781{
782 return pci_register_driver(&driver);
783}
784
785static void __exit alsa_card_lola_exit(void)
786{
787 pci_unregister_driver(&driver);
788}
789
790module_init(alsa_card_lola_init)
791module_exit(alsa_card_lola_exit)
diff --git a/sound/pci/lola/lola.h b/sound/pci/lola/lola.h
new file mode 100644
index 000000000000..d5708e29b16d
--- /dev/null
+++ b/sound/pci/lola/lola.h
@@ -0,0 +1,527 @@
1/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#ifndef _LOLA_H
22#define _LOLA_H
23
24#define DRVNAME "snd-lola"
25#define SFX DRVNAME ": "
26
27/*
28 * Lola HD Audio Registers BAR0
29 */
30#define LOLA_BAR0_GCAP 0x00
31#define LOLA_BAR0_VMIN 0x02
32#define LOLA_BAR0_VMAJ 0x03
33#define LOLA_BAR0_OUTPAY 0x04
34#define LOLA_BAR0_INPAY 0x06
35#define LOLA_BAR0_GCTL 0x08
36#define LOLA_BAR0_WAKEEN 0x0c
37#define LOLA_BAR0_STATESTS 0x0e
38#define LOLA_BAR0_GSTS 0x10
39#define LOLA_BAR0_OUTSTRMPAY 0x18
40#define LOLA_BAR0_INSTRMPAY 0x1a
41#define LOLA_BAR0_INTCTL 0x20
42#define LOLA_BAR0_INTSTS 0x24
43#define LOLA_BAR0_WALCLK 0x30
44#define LOLA_BAR0_SSYNC 0x38
45
46#define LOLA_BAR0_CORBLBASE 0x40
47#define LOLA_BAR0_CORBUBASE 0x44
48#define LOLA_BAR0_CORBWP 0x48 /* no ULONG access */
49#define LOLA_BAR0_CORBRP 0x4a /* no ULONG access */
50#define LOLA_BAR0_CORBCTL 0x4c /* no ULONG access */
51#define LOLA_BAR0_CORBSTS 0x4d /* UCHAR access only */
52#define LOLA_BAR0_CORBSIZE 0x4e /* no ULONG access */
53
54#define LOLA_BAR0_RIRBLBASE 0x50
55#define LOLA_BAR0_RIRBUBASE 0x54
56#define LOLA_BAR0_RIRBWP 0x58
57#define LOLA_BAR0_RINTCNT 0x5a /* no ULONG access */
58#define LOLA_BAR0_RIRBCTL 0x5c
59#define LOLA_BAR0_RIRBSTS 0x5d /* UCHAR access only */
60#define LOLA_BAR0_RIRBSIZE 0x5e /* no ULONG access */
61
62#define LOLA_BAR0_ICW 0x60
63#define LOLA_BAR0_IRR 0x64
64#define LOLA_BAR0_ICS 0x68
65#define LOLA_BAR0_DPLBASE 0x70
66#define LOLA_BAR0_DPUBASE 0x74
67
68/* stream register offsets from stream base 0x80 */
69#define LOLA_BAR0_SD0_OFFSET 0x80
70#define LOLA_REG0_SD_CTL 0x00
71#define LOLA_REG0_SD_STS 0x03
72#define LOLA_REG0_SD_LPIB 0x04
73#define LOLA_REG0_SD_CBL 0x08
74#define LOLA_REG0_SD_LVI 0x0c
75#define LOLA_REG0_SD_FIFOW 0x0e
76#define LOLA_REG0_SD_FIFOSIZE 0x10
77#define LOLA_REG0_SD_FORMAT 0x12
78#define LOLA_REG0_SD_BDLPL 0x18
79#define LOLA_REG0_SD_BDLPU 0x1c
80
81/*
82 * Lola Digigram Registers BAR1
83 */
84#define LOLA_BAR1_FPGAVER 0x00
85#define LOLA_BAR1_DEVER 0x04
86#define LOLA_BAR1_UCBMV 0x08
87#define LOLA_BAR1_JTAG 0x0c
88#define LOLA_BAR1_UARTRX 0x10
89#define LOLA_BAR1_UARTTX 0x14
90#define LOLA_BAR1_UARTCR 0x18
91#define LOLA_BAR1_NVRAMVER 0x1c
92#define LOLA_BAR1_CTRLSPI 0x20
93#define LOLA_BAR1_DSPI 0x24
94#define LOLA_BAR1_AISPI 0x28
95#define LOLA_BAR1_GRAN 0x2c
96
97#define LOLA_BAR1_DINTCTL 0x80
98#define LOLA_BAR1_DIINTCTL 0x84
99#define LOLA_BAR1_DOINTCTL 0x88
100#define LOLA_BAR1_LRC 0x90
101#define LOLA_BAR1_DINTSTS 0x94
102#define LOLA_BAR1_DIINTSTS 0x98
103#define LOLA_BAR1_DOINTSTS 0x9c
104
105#define LOLA_BAR1_DSD0_OFFSET 0xa0
106#define LOLA_BAR1_DSD_SIZE 0x18
107
108#define LOLA_BAR1_DSDnSTS 0x00
109#define LOLA_BAR1_DSDnLPIB 0x04
110#define LOLA_BAR1_DSDnCTL 0x08
111#define LOLA_BAR1_DSDnLVI 0x0c
112#define LOLA_BAR1_DSDnBDPL 0x10
113#define LOLA_BAR1_DSDnBDPU 0x14
114
115#define LOLA_BAR1_SSYNC 0x03e8
116
117#define LOLA_BAR1_BOARD_CTRL 0x0f00
118#define LOLA_BAR1_BOARD_MODE 0x0f02
119
120#define LOLA_BAR1_SOURCE_GAIN_ENABLE 0x1000
121#define LOLA_BAR1_DEST00_MIX_GAIN_ENABLE 0x1004
122#define LOLA_BAR1_DEST31_MIX_GAIN_ENABLE 0x1080
123#define LOLA_BAR1_SOURCE00_01_GAIN 0x1084
124#define LOLA_BAR1_SOURCE30_31_GAIN 0x10c0
125#define LOLA_BAR1_SOURCE_GAIN(src) \
126 (LOLA_BAR1_SOURCE00_01_GAIN + (src) * 2)
127#define LOLA_BAR1_DEST00_MIX00_01_GAIN 0x10c4
128#define LOLA_BAR1_DEST00_MIX30_31_GAIN 0x1100
129#define LOLA_BAR1_DEST01_MIX00_01_GAIN 0x1104
130#define LOLA_BAR1_DEST01_MIX30_31_GAIN 0x1140
131#define LOLA_BAR1_DEST31_MIX00_01_GAIN 0x1884
132#define LOLA_BAR1_DEST31_MIX30_31_GAIN 0x18c0
133#define LOLA_BAR1_MIX_GAIN(dest, mix) \
134 (LOLA_BAR1_DEST00_MIX00_01_GAIN + (dest) * 0x40 + (mix) * 2)
135#define LOLA_BAR1_ANALOG_CLIP_IN 0x18c4
136#define LOLA_BAR1_PEAKMETERS_SOURCE00_01 0x18c8
137#define LOLA_BAR1_PEAKMETERS_SOURCE30_31 0x1904
138#define LOLA_BAR1_PEAKMETERS_SOURCE(src) \
139 (LOLA_BAR1_PEAKMETERS_SOURCE00_01 + (src) * 2)
140#define LOLA_BAR1_PEAKMETERS_DEST00_01 0x1908
141#define LOLA_BAR1_PEAKMETERS_DEST30_31 0x1944
142#define LOLA_BAR1_PEAKMETERS_DEST(dest) \
143 (LOLA_BAR1_PEAKMETERS_DEST00_01 + (dest) * 2)
144#define LOLA_BAR1_PEAKMETERS_AGC00_01 0x1948
145#define LOLA_BAR1_PEAKMETERS_AGC14_15 0x1964
146#define LOLA_BAR1_PEAKMETERS_AGC(x) \
147 (LOLA_BAR1_PEAKMETERS_AGC00_01 + (x) * 2)
148
149/* GCTL reset bit */
150#define LOLA_GCTL_RESET (1 << 0)
151/* GCTL unsolicited response enable bit */
152#define LOLA_GCTL_UREN (1 << 8)
153
154/* CORB/RIRB control, read/write pointer */
155#define LOLA_RBCTL_DMA_EN 0x02 /* enable DMA */
156#define LOLA_RBCTL_IRQ_EN 0x01 /* enable IRQ */
157#define LOLA_RBRWP_CLR 0x8000 /* read/write pointer clear */
158
159#define LOLA_RIRB_EX_UNSOL_EV 0x40000000
160#define LOLA_RIRB_EX_ERROR 0x80000000
161
162/* CORB int mask: CMEI[0] */
163#define LOLA_CORB_INT_CMEI 0x01
164#define LOLA_CORB_INT_MASK LOLA_CORB_INT_CMEI
165
166/* RIRB int mask: overrun[2], response[0] */
167#define LOLA_RIRB_INT_RESPONSE 0x01
168#define LOLA_RIRB_INT_OVERRUN 0x04
169#define LOLA_RIRB_INT_MASK (LOLA_RIRB_INT_RESPONSE | LOLA_RIRB_INT_OVERRUN)
170
171/* DINTCTL and DINTSTS */
172#define LOLA_DINT_GLOBAL 0x80000000 /* global interrupt enable bit */
173#define LOLA_DINT_CTRL 0x40000000 /* controller interrupt enable bit */
174#define LOLA_DINT_FIFOERR 0x20000000 /* global fifo error enable bit */
175#define LOLA_DINT_MUERR 0x10000000 /* global microcontroller underrun error */
176
177/* DSDnCTL bits */
178#define LOLA_DSD_CTL_SRST 0x01 /* stream reset bit */
179#define LOLA_DSD_CTL_SRUN 0x02 /* stream DMA start bit */
180#define LOLA_DSD_CTL_IOCE 0x04 /* interrupt on completion enable */
181#define LOLA_DSD_CTL_DEIE 0x10 /* descriptor error interrupt enable */
182#define LOLA_DSD_CTL_VLRCV 0x20 /* valid LRCountValue information in bits 8..31 */
183#define LOLA_LRC_MASK 0xffffff00
184
185/* DSDnSTS */
186#define LOLA_DSD_STS_BCIS 0x04 /* buffer completion interrupt status */
187#define LOLA_DSD_STS_DESE 0x10 /* descriptor error interrupt */
188#define LOLA_DSD_STS_FIFORDY 0x20 /* fifo ready */
189
190#define LOLA_CORB_ENTRIES 256
191
192#define MAX_STREAM_IN_COUNT 16
193#define MAX_STREAM_OUT_COUNT 16
194#define MAX_STREAM_COUNT 16
195#define MAX_PINS MAX_STREAM_COUNT
196#define MAX_STREAM_BUFFER_COUNT 16
197#define MAX_AUDIO_INOUT_COUNT 16
198
199#define LOLA_CLOCK_TYPE_INTERNAL 0
200#define LOLA_CLOCK_TYPE_AES 1
201#define LOLA_CLOCK_TYPE_AES_SYNC 2
202#define LOLA_CLOCK_TYPE_WORDCLOCK 3
203#define LOLA_CLOCK_TYPE_ETHERSOUND 4
204#define LOLA_CLOCK_TYPE_VIDEO 5
205
206#define LOLA_CLOCK_FORMAT_NONE 0
207#define LOLA_CLOCK_FORMAT_NTSC 1
208#define LOLA_CLOCK_FORMAT_PAL 2
209
210#define MAX_SAMPLE_CLOCK_COUNT 48
211
212/* parameters used with mixer widget's mixer capabilities */
213#define LOLA_PEAK_METER_CAN_AGC_MASK 1
214#define LOLA_PEAK_METER_CAN_ANALOG_CLIP_MASK 2
215
216struct lola_bar {
217 unsigned long addr;
218 void __iomem *remap_addr;
219};
220
221/* CORB/RIRB */
222struct lola_rb {
223 u32 *buf; /* CORB/RIRB buffer, 8 byte per each entry */
224 dma_addr_t addr; /* physical address of CORB/RIRB buffer */
225 unsigned short rp, wp; /* read/write pointers */
226 int cmds; /* number of pending requests */
227};
228
229/* Pin widget setup */
230struct lola_pin {
231 unsigned int nid;
232 bool is_analog;
233 unsigned int amp_mute;
234 unsigned int amp_step_size;
235 unsigned int amp_num_steps;
236 unsigned int amp_offset;
237 unsigned int max_level;
238 unsigned int config_default_reg;
239 unsigned int fixed_gain_list_len;
240 unsigned int cur_gain_step;
241};
242
243struct lola_pin_array {
244 unsigned int num_pins;
245 unsigned int num_analog_pins;
246 struct lola_pin pins[MAX_PINS];
247};
248
249/* Clock widget setup */
250struct lola_sample_clock {
251 unsigned int type;
252 unsigned int format;
253 unsigned int freq;
254};
255
256struct lola_clock_widget {
257 unsigned int nid;
258 unsigned int items;
259 unsigned int cur_index;
260 unsigned int cur_freq;
261 bool cur_valid;
262 struct lola_sample_clock sample_clock[MAX_SAMPLE_CLOCK_COUNT];
263 unsigned int idx_lookup[MAX_SAMPLE_CLOCK_COUNT];
264};
265
266#define LOLA_MIXER_DIM 32
267struct lola_mixer_array {
268 u32 src_gain_enable;
269 u32 dest_mix_gain_enable[LOLA_MIXER_DIM];
270 u16 src_gain[LOLA_MIXER_DIM];
271 u16 dest_mix_gain[LOLA_MIXER_DIM][LOLA_MIXER_DIM];
272};
273
274/* Mixer widget setup */
275struct lola_mixer_widget {
276 unsigned int nid;
277 unsigned int caps;
278 struct lola_mixer_array __user *array;
279 struct lola_mixer_array *array_saved;
280 unsigned int src_stream_outs;
281 unsigned int src_phys_ins;
282 unsigned int dest_stream_ins;
283 unsigned int dest_phys_outs;
284 unsigned int src_stream_out_ofs;
285 unsigned int dest_phys_out_ofs;
286 unsigned int src_mask;
287 unsigned int dest_mask;
288};
289
290/* Audio stream */
291struct lola_stream {
292 unsigned int nid; /* audio widget NID */
293 unsigned int index; /* array index */
294 unsigned int dsd; /* DSD index */
295 bool can_float;
296 struct snd_pcm_substream *substream; /* assigned PCM substream */
297 struct lola_stream *master; /* master stream (for multi-channel) */
298
299 /* buffer setup */
300 unsigned int bufsize;
301 unsigned int period_bytes;
302 unsigned int frags;
303
304 /* format + channel setup */
305 unsigned int format_verb;
306
307 /* flags */
308 unsigned int opened:1;
309 unsigned int prepared:1;
310 unsigned int paused:1;
311 unsigned int running:1;
312};
313
314#define PLAY SNDRV_PCM_STREAM_PLAYBACK
315#define CAPT SNDRV_PCM_STREAM_CAPTURE
316
317struct lola_pcm {
318 unsigned int num_streams;
319 struct snd_dma_buffer bdl; /* BDL buffer */
320 struct lola_stream streams[MAX_STREAM_COUNT];
321};
322
323/* card instance */
324struct lola {
325 struct snd_card *card;
326 struct pci_dev *pci;
327
328 /* pci resources */
329 struct lola_bar bar[2];
330 int irq;
331
332 /* locks */
333 spinlock_t reg_lock;
334 struct mutex open_mutex;
335
336 /* CORB/RIRB */
337 struct lola_rb corb;
338 struct lola_rb rirb;
339 unsigned int res, res_ex; /* last read values */
340 /* last command (for debugging) */
341 unsigned int last_cmd_nid, last_verb, last_data, last_extdata;
342
343 /* CORB/RIRB buffers */
344 struct snd_dma_buffer rb;
345
346 /* unsolicited events */
347 unsigned int last_unsol_res;
348
349 /* streams */
350 struct lola_pcm pcm[2];
351
352 /* input src */
353 unsigned int input_src_caps_mask;
354 unsigned int input_src_mask;
355
356 /* pins */
357 struct lola_pin_array pin[2];
358
359 /* clock */
360 struct lola_clock_widget clock;
361 int ref_count_rate;
362 unsigned int sample_rate;
363
364 /* mixer */
365 struct lola_mixer_widget mixer;
366
367 /* hw info */
368 unsigned int version;
369 unsigned int lola_caps;
370
371 /* parameters */
372 unsigned int granularity;
373 unsigned int sample_rate_min;
374 unsigned int sample_rate_max;
375
376 /* flags */
377 unsigned int initialized:1;
378 unsigned int cold_reset:1;
379 unsigned int polling_mode:1;
380
381 /* for debugging */
382 unsigned int debug_res;
383 unsigned int debug_res_ex;
384};
385
386#define BAR0 0
387#define BAR1 1
388
389/* Helper macros */
390#define lola_readl(chip, idx, name) \
391 readl((chip)->bar[idx].remap_addr + LOLA_##idx##_##name)
392#define lola_readw(chip, idx, name) \
393 readw((chip)->bar[idx].remap_addr + LOLA_##idx##_##name)
394#define lola_readb(chip, idx, name) \
395 readb((chip)->bar[idx].remap_addr + LOLA_##idx##_##name)
396#define lola_writel(chip, idx, name, val) \
397 writel((val), (chip)->bar[idx].remap_addr + LOLA_##idx##_##name)
398#define lola_writew(chip, idx, name, val) \
399 writew((val), (chip)->bar[idx].remap_addr + LOLA_##idx##_##name)
400#define lola_writeb(chip, idx, name, val) \
401 writeb((val), (chip)->bar[idx].remap_addr + LOLA_##idx##_##name)
402
403#define lola_dsd_read(chip, dsd, name) \
404 readl((chip)->bar[BAR1].remap_addr + LOLA_BAR1_DSD0_OFFSET + \
405 (LOLA_BAR1_DSD_SIZE * (dsd)) + LOLA_BAR1_DSDn##name)
406#define lola_dsd_write(chip, dsd, name, val) \
407 writel((val), (chip)->bar[BAR1].remap_addr + LOLA_BAR1_DSD0_OFFSET + \
408 (LOLA_BAR1_DSD_SIZE * (dsd)) + LOLA_BAR1_DSDn##name)
409
410/* GET verbs HDAudio */
411#define LOLA_VERB_GET_STREAM_FORMAT 0xa00
412#define LOLA_VERB_GET_AMP_GAIN_MUTE 0xb00
413#define LOLA_VERB_PARAMETERS 0xf00
414#define LOLA_VERB_GET_POWER_STATE 0xf05
415#define LOLA_VERB_GET_CONV 0xf06
416#define LOLA_VERB_GET_UNSOLICITED_RESPONSE 0xf08
417#define LOLA_VERB_GET_DIGI_CONVERT_1 0xf0d
418#define LOLA_VERB_GET_CONFIG_DEFAULT 0xf1c
419#define LOLA_VERB_GET_SUBSYSTEM_ID 0xf20
420/* GET verbs Digigram */
421#define LOLA_VERB_GET_FIXED_GAIN 0xfc0
422#define LOLA_VERB_GET_GAIN_SELECT 0xfc1
423#define LOLA_VERB_GET_MAX_LEVEL 0xfc2
424#define LOLA_VERB_GET_CLOCK_LIST 0xfc3
425#define LOLA_VERB_GET_CLOCK_SELECT 0xfc4
426#define LOLA_VERB_GET_CLOCK_STATUS 0xfc5
427
428/* SET verbs HDAudio */
429#define LOLA_VERB_SET_STREAM_FORMAT 0x200
430#define LOLA_VERB_SET_AMP_GAIN_MUTE 0x300
431#define LOLA_VERB_SET_POWER_STATE 0x705
432#define LOLA_VERB_SET_CHANNEL_STREAMID 0x706
433#define LOLA_VERB_SET_UNSOLICITED_ENABLE 0x708
434#define LOLA_VERB_SET_DIGI_CONVERT_1 0x70d
435/* SET verbs Digigram */
436#define LOLA_VERB_SET_GAIN_SELECT 0xf81
437#define LOLA_VERB_SET_CLOCK_SELECT 0xf84
438#define LOLA_VERB_SET_GRANULARITY_STEPS 0xf86
439#define LOLA_VERB_SET_SOURCE_GAIN 0xf87
440#define LOLA_VERB_SET_MIX_GAIN 0xf88
441#define LOLA_VERB_SET_DESTINATION_GAIN 0xf89
442#define LOLA_VERB_SET_SRC 0xf8a
443
444/* Parameter IDs used with LOLA_VERB_PARAMETERS */
445#define LOLA_PAR_VENDOR_ID 0x00
446#define LOLA_PAR_FUNCTION_TYPE 0x05
447#define LOLA_PAR_AUDIO_WIDGET_CAP 0x09
448#define LOLA_PAR_PCM 0x0a
449#define LOLA_PAR_STREAM_FORMATS 0x0b
450#define LOLA_PAR_PIN_CAP 0x0c
451#define LOLA_PAR_AMP_IN_CAP 0x0d
452#define LOLA_PAR_CONNLIST_LEN 0x0e
453#define LOLA_PAR_POWER_STATE 0x0f
454#define LOLA_PAR_GPIO_CAP 0x11
455#define LOLA_PAR_AMP_OUT_CAP 0x12
456#define LOLA_PAR_SPECIFIC_CAPS 0x80
457#define LOLA_PAR_FIXED_GAIN_LIST 0x81
458
459/* extract results of LOLA_PAR_SPECIFIC_CAPS */
460#define LOLA_AFG_MIXER_WIDGET_PRESENT(res) ((res & (1 << 21)) != 0)
461#define LOLA_AFG_CLOCK_WIDGET_PRESENT(res) ((res & (1 << 20)) != 0)
462#define LOLA_AFG_INPUT_PIN_COUNT(res) ((res >> 10) & 0x2ff)
463#define LOLA_AFG_OUTPUT_PIN_COUNT(res) ((res) & 0x2ff)
464
465/* extract results of LOLA_PAR_AMP_IN_CAP / LOLA_PAR_AMP_OUT_CAP */
466#define LOLA_AMP_MUTE_CAPABLE(res) ((res & (1 << 31)) != 0)
467#define LOLA_AMP_STEP_SIZE(res) ((res >> 24) & 0x7f)
468#define LOLA_AMP_NUM_STEPS(res) ((res >> 12) & 0x3ff)
469#define LOLA_AMP_OFFSET(res) ((res) & 0x3ff)
470
471#define LOLA_GRANULARITY_MIN 8
472#define LOLA_GRANULARITY_MAX 32
473#define LOLA_GRANULARITY_STEP 8
474
475/* parameters used with unsolicited command/response */
476#define LOLA_UNSOLICITED_TAG_MASK 0x3f
477#define LOLA_UNSOLICITED_TAG 0x1a
478#define LOLA_UNSOLICITED_ENABLE 0x80
479#define LOLA_UNSOL_RESP_TAG_OFFSET 26
480
481/* count values in the Vendor Specific Mixer Widget's Audio Widget Capabilities */
482#define LOLA_MIXER_SRC_INPUT_PLAY_SEPARATION(res) ((res >> 2) & 0x1f)
483#define LOLA_MIXER_DEST_REC_OUTPUT_SEPATATION(res) ((res >> 7) & 0x1f)
484
485int lola_codec_write(struct lola *chip, unsigned int nid, unsigned int verb,
486 unsigned int data, unsigned int extdata);
487int lola_codec_read(struct lola *chip, unsigned int nid, unsigned int verb,
488 unsigned int data, unsigned int extdata,
489 unsigned int *val, unsigned int *extval);
490int lola_codec_flush(struct lola *chip);
491#define lola_read_param(chip, nid, param, val) \
492 lola_codec_read(chip, nid, LOLA_VERB_PARAMETERS, param, 0, val, NULL)
493
494/* PCM */
495int lola_create_pcm(struct lola *chip);
496void lola_free_pcm(struct lola *chip);
497int lola_init_pcm(struct lola *chip, int dir, int *nidp);
498void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits);
499
500/* clock */
501int lola_init_clock_widget(struct lola *chip, int nid);
502int lola_set_granularity(struct lola *chip, unsigned int val, bool force);
503int lola_enable_clock_events(struct lola *chip);
504int lola_set_clock_index(struct lola *chip, unsigned int idx);
505int lola_set_clock(struct lola *chip, int idx);
506int lola_set_sample_rate(struct lola *chip, int rate);
507bool lola_update_ext_clock_freq(struct lola *chip, unsigned int val);
508unsigned int lola_sample_rate_convert(unsigned int coded);
509
510/* mixer */
511int lola_init_pins(struct lola *chip, int dir, int *nidp);
512int lola_init_mixer_widget(struct lola *chip, int nid);
513void lola_free_mixer(struct lola *chip);
514int lola_create_mixer(struct lola *chip);
515int lola_setup_all_analog_gains(struct lola *chip, int dir, bool mute);
516void lola_save_mixer(struct lola *chip);
517void lola_restore_mixer(struct lola *chip);
518int lola_set_src_config(struct lola *chip, unsigned int src_mask, bool update);
519
520/* proc */
521#ifdef CONFIG_SND_DEBUG
522void lola_proc_debug_new(struct lola *chip);
523#else
524#define lola_proc_debug_new(chip)
525#endif
526
527#endif /* _LOLA_H */
diff --git a/sound/pci/lola/lola_clock.c b/sound/pci/lola/lola_clock.c
new file mode 100644
index 000000000000..72f8ef0ac865
--- /dev/null
+++ b/sound/pci/lola/lola_clock.c
@@ -0,0 +1,323 @@
1/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include "lola.h"
27
28unsigned int lola_sample_rate_convert(unsigned int coded)
29{
30 unsigned int freq;
31
32 /* base frequency */
33 switch (coded & 0x3) {
34 case 0: freq = 48000; break;
35 case 1: freq = 44100; break;
36 case 2: freq = 32000; break;
37 default: return 0; /* error */
38 }
39
40 /* multiplier / devisor */
41 switch (coded & 0x1c) {
42 case (0 << 2): break;
43 case (4 << 2): break;
44 case (1 << 2): freq *= 2; break;
45 case (2 << 2): freq *= 4; break;
46 case (5 << 2): freq /= 2; break;
47 case (6 << 2): freq /= 4; break;
48 default: return 0; /* error */
49 }
50
51 /* ajustement */
52 switch (coded & 0x60) {
53 case (0 << 5): break;
54 case (1 << 5): freq = (freq * 999) / 1000; break;
55 case (2 << 5): freq = (freq * 1001) / 1000; break;
56 default: return 0; /* error */
57 }
58 return freq;
59}
60
61/*
62 * Granualrity
63 */
64
65#define LOLA_MAXFREQ_AT_GRANULARITY_MIN 48000
66#define LOLA_MAXFREQ_AT_GRANULARITY_BELOW_MAX 96000
67
68static bool check_gran_clock_compatibility(struct lola *chip,
69 unsigned int val,
70 unsigned int freq)
71{
72 if (!chip->granularity)
73 return true;
74
75 if (val < LOLA_GRANULARITY_MIN || val > LOLA_GRANULARITY_MAX ||
76 (val % LOLA_GRANULARITY_STEP) != 0)
77 return false;
78
79 if (val == LOLA_GRANULARITY_MIN) {
80 if (freq > LOLA_MAXFREQ_AT_GRANULARITY_MIN)
81 return false;
82 } else if (val < LOLA_GRANULARITY_MAX) {
83 if (freq > LOLA_MAXFREQ_AT_GRANULARITY_BELOW_MAX)
84 return false;
85 }
86 return true;
87}
88
89int lola_set_granularity(struct lola *chip, unsigned int val, bool force)
90{
91 int err;
92
93 if (!force) {
94 if (val == chip->granularity)
95 return 0;
96#if 0
97 /* change Gran only if there are no streams allocated ! */
98 if (chip->audio_in_alloc_mask || chip->audio_out_alloc_mask)
99 return -EBUSY;
100#endif
101 if (!check_gran_clock_compatibility(chip, val,
102 chip->clock.cur_freq))
103 return -EINVAL;
104 }
105
106 chip->granularity = val;
107 val /= LOLA_GRANULARITY_STEP;
108
109 /* audio function group */
110 err = lola_codec_write(chip, 1, LOLA_VERB_SET_GRANULARITY_STEPS,
111 val, 0);
112 if (err < 0)
113 return err;
114 /* this can be a very slow function !!! */
115 usleep_range(400 * val, 20000);
116 return lola_codec_flush(chip);
117}
118
119/*
120 * Clock widget handling
121 */
122
123int __devinit lola_init_clock_widget(struct lola *chip, int nid)
124{
125 unsigned int val;
126 int i, j, nitems, nb_verbs, idx, idx_list;
127 int err;
128
129 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
130 if (err < 0) {
131 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
132 return err;
133 }
134
135 if ((val & 0xfff00000) != 0x01f00000) { /* test SubType and Type */
136 snd_printdd("No valid clock widget\n");
137 return 0;
138 }
139
140 chip->clock.nid = nid;
141 chip->clock.items = val & 0xff;
142 snd_printdd("clock_list nid=%x, entries=%d\n", nid,
143 chip->clock.items);
144 if (chip->clock.items > MAX_SAMPLE_CLOCK_COUNT) {
145 printk(KERN_ERR SFX "CLOCK_LIST too big: %d\n",
146 chip->clock.items);
147 return -EINVAL;
148 }
149
150 nitems = chip->clock.items;
151 nb_verbs = (nitems + 3) / 4;
152 idx = 0;
153 idx_list = 0;
154 for (i = 0; i < nb_verbs; i++) {
155 unsigned int res_ex;
156 unsigned short items[4];
157
158 err = lola_codec_read(chip, nid, LOLA_VERB_GET_CLOCK_LIST,
159 idx, 0, &val, &res_ex);
160 if (err < 0) {
161 printk(KERN_ERR SFX "Can't read CLOCK_LIST\n");
162 return -EINVAL;
163 }
164
165 items[0] = val & 0xfff;
166 items[1] = (val >> 16) & 0xfff;
167 items[2] = res_ex & 0xfff;
168 items[3] = (res_ex >> 16) & 0xfff;
169
170 for (j = 0; j < 4; j++) {
171 unsigned char type = items[j] >> 8;
172 unsigned int freq = items[j] & 0xff;
173 int format = LOLA_CLOCK_FORMAT_NONE;
174 bool add_clock = true;
175 if (type == LOLA_CLOCK_TYPE_INTERNAL) {
176 freq = lola_sample_rate_convert(freq);
177 if (freq < chip->sample_rate_min)
178 add_clock = false;
179 else if (freq == 48000) {
180 chip->clock.cur_index = idx_list;
181 chip->clock.cur_freq = 48000;
182 chip->clock.cur_valid = true;
183 }
184 } else if (type == LOLA_CLOCK_TYPE_VIDEO) {
185 freq = lola_sample_rate_convert(freq);
186 if (freq < chip->sample_rate_min)
187 add_clock = false;
188 /* video clock has a format (0:NTSC, 1:PAL)*/
189 if (items[j] & 0x80)
190 format = LOLA_CLOCK_FORMAT_NTSC;
191 else
192 format = LOLA_CLOCK_FORMAT_PAL;
193 }
194 if (add_clock) {
195 struct lola_sample_clock *sc;
196 sc = &chip->clock.sample_clock[idx_list];
197 sc->type = type;
198 sc->format = format;
199 sc->freq = freq;
200 /* keep the index used with the board */
201 chip->clock.idx_lookup[idx_list] = idx;
202 idx_list++;
203 } else {
204 chip->clock.items--;
205 }
206 if (++idx >= nitems)
207 break;
208 }
209 }
210 return 0;
211}
212
213/* enable unsolicited events of the clock widget */
214int lola_enable_clock_events(struct lola *chip)
215{
216 unsigned int res;
217 int err;
218
219 err = lola_codec_read(chip, chip->clock.nid,
220 LOLA_VERB_SET_UNSOLICITED_ENABLE,
221 LOLA_UNSOLICITED_ENABLE | LOLA_UNSOLICITED_TAG,
222 0, &res, NULL);
223 if (err < 0)
224 return err;
225 if (res) {
226 printk(KERN_WARNING SFX "error in enable_clock_events %d\n",
227 res);
228 return -EINVAL;
229 }
230 return 0;
231}
232
233int lola_set_clock_index(struct lola *chip, unsigned int idx)
234{
235 unsigned int res;
236 int err;
237
238 err = lola_codec_read(chip, chip->clock.nid,
239 LOLA_VERB_SET_CLOCK_SELECT,
240 chip->clock.idx_lookup[idx],
241 0, &res, NULL);
242 if (err < 0)
243 return err;
244 if (res) {
245 printk(KERN_WARNING SFX "error in set_clock %d\n", res);
246 return -EINVAL;
247 }
248 return 0;
249}
250
251bool lola_update_ext_clock_freq(struct lola *chip, unsigned int val)
252{
253 unsigned int tag;
254
255 /* the current EXTERNAL clock information gets updated by interrupt
256 * with an unsolicited response
257 */
258 if (!val)
259 return false;
260 tag = (val >> LOLA_UNSOL_RESP_TAG_OFFSET) & LOLA_UNSOLICITED_TAG_MASK;
261 if (tag != LOLA_UNSOLICITED_TAG)
262 return false;
263
264 /* only for current = external clocks */
265 if (chip->clock.sample_clock[chip->clock.cur_index].type !=
266 LOLA_CLOCK_TYPE_INTERNAL) {
267 chip->clock.cur_freq = lola_sample_rate_convert(val & 0x7f);
268 chip->clock.cur_valid = (val & 0x100) != 0;
269 }
270 return true;
271}
272
273int lola_set_clock(struct lola *chip, int idx)
274{
275 int freq = 0;
276 bool valid = false;
277
278 if (idx == chip->clock.cur_index) {
279 /* current clock is allowed */
280 freq = chip->clock.cur_freq;
281 valid = chip->clock.cur_valid;
282 } else if (chip->clock.sample_clock[idx].type ==
283 LOLA_CLOCK_TYPE_INTERNAL) {
284 /* internal clocks allowed */
285 freq = chip->clock.sample_clock[idx].freq;
286 valid = true;
287 }
288
289 if (!freq || !valid)
290 return -EINVAL;
291
292 if (!check_gran_clock_compatibility(chip, chip->granularity, freq))
293 return -EINVAL;
294
295 if (idx != chip->clock.cur_index) {
296 int err = lola_set_clock_index(chip, idx);
297 if (err < 0)
298 return err;
299 /* update new settings */
300 chip->clock.cur_index = idx;
301 chip->clock.cur_freq = freq;
302 chip->clock.cur_valid = true;
303 }
304 return 0;
305}
306
307int lola_set_sample_rate(struct lola *chip, int rate)
308{
309 int i;
310
311 if (chip->clock.cur_freq == rate && chip->clock.cur_valid)
312 return 0;
313 /* search for new dwClockIndex */
314 for (i = 0; i < chip->clock.items; i++) {
315 if (chip->clock.sample_clock[i].type == LOLA_CLOCK_TYPE_INTERNAL &&
316 chip->clock.sample_clock[i].freq == rate)
317 break;
318 }
319 if (i >= chip->clock.items)
320 return -EINVAL;
321 return lola_set_clock(chip, i);
322}
323
diff --git a/sound/pci/lola/lola_mixer.c b/sound/pci/lola/lola_mixer.c
new file mode 100644
index 000000000000..5d518f1a712c
--- /dev/null
+++ b/sound/pci/lola/lola_mixer.c
@@ -0,0 +1,839 @@
1/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/vmalloc.h>
24#include <linux/io.h>
25#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/pcm.h>
28#include <sound/tlv.h>
29#include "lola.h"
30
31static int __devinit lola_init_pin(struct lola *chip, struct lola_pin *pin,
32 int dir, int nid)
33{
34 unsigned int val;
35 int err;
36
37 pin->nid = nid;
38 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
39 if (err < 0) {
40 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
41 return err;
42 }
43 val &= 0x00f00fff; /* test TYPE and bits 0..11 */
44 if (val == 0x00400200) /* Type = 4, Digital = 1 */
45 pin->is_analog = false;
46 else if (val == 0x0040000a && dir == CAPT) /* Dig=0, InAmp/ovrd */
47 pin->is_analog = true;
48 else if (val == 0x0040000c && dir == PLAY) /* Dig=0, OutAmp/ovrd */
49 pin->is_analog = true;
50 else {
51 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n", val, nid);
52 return -EINVAL;
53 }
54
55 /* analog parameters only following, so continue in case of Digital pin
56 */
57 if (!pin->is_analog)
58 return 0;
59
60 if (dir == PLAY)
61 err = lola_read_param(chip, nid, LOLA_PAR_AMP_OUT_CAP, &val);
62 else
63 err = lola_read_param(chip, nid, LOLA_PAR_AMP_IN_CAP, &val);
64 if (err < 0) {
65 printk(KERN_ERR SFX "Can't read AMP-caps for 0x%x\n", nid);
66 return err;
67 }
68
69 pin->amp_mute = LOLA_AMP_MUTE_CAPABLE(val);
70 pin->amp_step_size = LOLA_AMP_STEP_SIZE(val);
71 pin->amp_num_steps = LOLA_AMP_NUM_STEPS(val);
72 if (pin->amp_num_steps) {
73 /* zero as mute state */
74 pin->amp_num_steps++;
75 pin->amp_step_size++;
76 }
77 pin->amp_offset = LOLA_AMP_OFFSET(val);
78
79 err = lola_codec_read(chip, nid, LOLA_VERB_GET_MAX_LEVEL, 0, 0, &val,
80 NULL);
81 if (err < 0) {
82 printk(KERN_ERR SFX "Can't get MAX_LEVEL 0x%x\n", nid);
83 return err;
84 }
85 pin->max_level = val & 0x3ff; /* 10 bits */
86
87 pin->config_default_reg = 0;
88 pin->fixed_gain_list_len = 0;
89 pin->cur_gain_step = 0;
90
91 return 0;
92}
93
94int __devinit lola_init_pins(struct lola *chip, int dir, int *nidp)
95{
96 int i, err, nid;
97 nid = *nidp;
98 for (i = 0; i < chip->pin[dir].num_pins; i++, nid++) {
99 err = lola_init_pin(chip, &chip->pin[dir].pins[i], dir, nid);
100 if (err < 0)
101 return err;
102 if (chip->pin[dir].pins[i].is_analog)
103 chip->pin[dir].num_analog_pins++;
104 }
105 *nidp = nid;
106 return 0;
107}
108
109void lola_free_mixer(struct lola *chip)
110{
111 if (chip->mixer.array_saved)
112 vfree(chip->mixer.array_saved);
113}
114
115int __devinit lola_init_mixer_widget(struct lola *chip, int nid)
116{
117 unsigned int val;
118 int err;
119
120 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
121 if (err < 0) {
122 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
123 return err;
124 }
125
126 if ((val & 0xfff00000) != 0x02f00000) { /* test SubType and Type */
127 snd_printdd("No valid mixer widget\n");
128 return 0;
129 }
130
131 chip->mixer.nid = nid;
132 chip->mixer.caps = val;
133 chip->mixer.array = (struct lola_mixer_array __iomem *)
134 (chip->bar[BAR1].remap_addr + LOLA_BAR1_SOURCE_GAIN_ENABLE);
135
136 /* reserve memory to copy mixer data for sleep mode transitions */
137 chip->mixer.array_saved = vmalloc(sizeof(struct lola_mixer_array));
138
139 /* mixer matrix sources are physical input data and play streams */
140 chip->mixer.src_stream_outs = chip->pcm[PLAY].num_streams;
141 chip->mixer.src_phys_ins = chip->pin[CAPT].num_pins;
142
143 /* mixer matrix destinations are record streams and physical output */
144 chip->mixer.dest_stream_ins = chip->pcm[CAPT].num_streams;
145 chip->mixer.dest_phys_outs = chip->pin[PLAY].num_pins;
146
147 /* mixer matrix can have unused areas between PhysIn and
148 * Play or Record and PhysOut zones
149 */
150 chip->mixer.src_stream_out_ofs = chip->mixer.src_phys_ins +
151 LOLA_MIXER_SRC_INPUT_PLAY_SEPARATION(val);
152 chip->mixer.dest_phys_out_ofs = chip->mixer.dest_stream_ins +
153 LOLA_MIXER_DEST_REC_OUTPUT_SEPATATION(val);
154
155 /* example : MixerMatrix of LoLa881
156 * 0-------8------16-------8------16
157 * | | | | |
158 * | INPUT | | INPUT | |
159 * | -> |unused | -> |unused |
160 * | RECORD| | OUTPUT| |
161 * | | | | |
162 * 8--------------------------------
163 * | | | | |
164 * | | | | |
165 * |unused |unused |unused |unused |
166 * | | | | |
167 * | | | | |
168 * 16-------------------------------
169 * | | | | |
170 * | PLAY | | PLAY | |
171 * | -> |unused | -> |unused |
172 * | RECORD| | OUTPUT| |
173 * | | | | |
174 * 8--------------------------------
175 * | | | | |
176 * | | | | |
177 * |unused |unused |unused |unused |
178 * | | | | |
179 * | | | | |
180 * 16-------------------------------
181 */
182 if (chip->mixer.src_stream_out_ofs > MAX_AUDIO_INOUT_COUNT ||
183 chip->mixer.dest_phys_out_ofs > MAX_STREAM_IN_COUNT) {
184 printk(KERN_ERR SFX "Invalid mixer widget size\n");
185 return -EINVAL;
186 }
187
188 chip->mixer.src_mask = ((1U << chip->mixer.src_phys_ins) - 1) |
189 (((1U << chip->mixer.src_stream_outs) - 1)
190 << chip->mixer.src_stream_out_ofs);
191 chip->mixer.dest_mask = ((1U << chip->mixer.dest_stream_ins) - 1) |
192 (((1U << chip->mixer.dest_phys_outs) - 1)
193 << chip->mixer.dest_phys_out_ofs);
194
195 return 0;
196}
197
198static int lola_mixer_set_src_gain(struct lola *chip, unsigned int id,
199 unsigned short gain, bool on)
200{
201 unsigned int oldval, val;
202
203 if (!(chip->mixer.src_mask & (1 << id)))
204 return -EINVAL;
205 writew(gain, &chip->mixer.array->src_gain[id]);
206 oldval = val = readl(&chip->mixer.array->src_gain_enable);
207 if (on)
208 val |= (1 << id);
209 else
210 val &= ~(1 << id);
211 writel(val, &chip->mixer.array->src_gain_enable);
212 lola_codec_flush(chip);
213 /* inform micro-controller about the new source gain */
214 return lola_codec_write(chip, chip->mixer.nid,
215 LOLA_VERB_SET_SOURCE_GAIN, id, 0);
216}
217
218#if 0 /* not used */
219static int lola_mixer_set_src_gains(struct lola *chip, unsigned int mask,
220 unsigned short *gains)
221{
222 int i;
223
224 if ((chip->mixer.src_mask & mask) != mask)
225 return -EINVAL;
226 for (i = 0; i < LOLA_MIXER_DIM; i++) {
227 if (mask & (1 << i)) {
228 writew(*gains, &chip->mixer.array->src_gain[i]);
229 gains++;
230 }
231 }
232 writel(mask, &chip->mixer.array->src_gain_enable);
233 lola_codec_flush(chip);
234 if (chip->mixer.caps & LOLA_PEAK_METER_CAN_AGC_MASK) {
235 /* update for all srcs at once */
236 return lola_codec_write(chip, chip->mixer.nid,
237 LOLA_VERB_SET_SOURCE_GAIN, 0x80, 0);
238 }
239 /* update manually */
240 for (i = 0; i < LOLA_MIXER_DIM; i++) {
241 if (mask & (1 << i)) {
242 lola_codec_write(chip, chip->mixer.nid,
243 LOLA_VERB_SET_SOURCE_GAIN, i, 0);
244 }
245 }
246 return 0;
247}
248#endif /* not used */
249
250static int lola_mixer_set_mapping_gain(struct lola *chip,
251 unsigned int src, unsigned int dest,
252 unsigned short gain, bool on)
253{
254 unsigned int val;
255
256 if (!(chip->mixer.src_mask & (1 << src)) ||
257 !(chip->mixer.dest_mask & (1 << dest)))
258 return -EINVAL;
259 if (on)
260 writew(gain, &chip->mixer.array->dest_mix_gain[dest][src]);
261 val = readl(&chip->mixer.array->dest_mix_gain_enable[dest]);
262 if (on)
263 val |= (1 << src);
264 else
265 val &= ~(1 << src);
266 writel(val, &chip->mixer.array->dest_mix_gain_enable[dest]);
267 lola_codec_flush(chip);
268 return lola_codec_write(chip, chip->mixer.nid, LOLA_VERB_SET_MIX_GAIN,
269 src, dest);
270}
271
272static int lola_mixer_set_dest_gains(struct lola *chip, unsigned int id,
273 unsigned int mask, unsigned short *gains)
274{
275 int i;
276
277 if (!(chip->mixer.dest_mask & (1 << id)) ||
278 (chip->mixer.src_mask & mask) != mask)
279 return -EINVAL;
280 for (i = 0; i < LOLA_MIXER_DIM; i++) {
281 if (mask & (1 << i)) {
282 writew(*gains, &chip->mixer.array->dest_mix_gain[id][i]);
283 gains++;
284 }
285 }
286 writel(mask, &chip->mixer.array->dest_mix_gain_enable[id]);
287 lola_codec_flush(chip);
288 /* update for all dests at once */
289 return lola_codec_write(chip, chip->mixer.nid,
290 LOLA_VERB_SET_DESTINATION_GAIN, id, 0);
291}
292
293/*
294 */
295
296static int set_analog_volume(struct lola *chip, int dir,
297 unsigned int idx, unsigned int val,
298 bool external_call);
299
300int lola_setup_all_analog_gains(struct lola *chip, int dir, bool mute)
301{
302 struct lola_pin *pin;
303 int idx, max_idx;
304
305 pin = chip->pin[dir].pins;
306 max_idx = chip->pin[dir].num_pins;
307 for (idx = 0; idx < max_idx; idx++) {
308 if (pin[idx].is_analog) {
309 unsigned int val = mute ? 0 : pin[idx].cur_gain_step;
310 /* set volume and do not save the value */
311 set_analog_volume(chip, dir, idx, val, false);
312 }
313 }
314 return lola_codec_flush(chip);
315}
316
317void lola_save_mixer(struct lola *chip)
318{
319 /* mute analog output */
320 if (chip->mixer.array_saved) {
321 /* store contents of mixer array */
322 memcpy_fromio(chip->mixer.array_saved, chip->mixer.array,
323 sizeof(*chip->mixer.array));
324 }
325 lola_setup_all_analog_gains(chip, PLAY, true); /* output mute */
326}
327
328void lola_restore_mixer(struct lola *chip)
329{
330 int i;
331
332 /*lola_reset_setups(chip);*/
333 if (chip->mixer.array_saved) {
334 /* restore contents of mixer array */
335 memcpy_toio(chip->mixer.array, chip->mixer.array_saved,
336 sizeof(*chip->mixer.array));
337 /* inform micro-controller about all restored values
338 * and ignore return values
339 */
340 for (i = 0; i < chip->mixer.src_phys_ins; i++)
341 lola_codec_write(chip, chip->mixer.nid,
342 LOLA_VERB_SET_SOURCE_GAIN,
343 i, 0);
344 for (i = 0; i < chip->mixer.src_stream_outs; i++)
345 lola_codec_write(chip, chip->mixer.nid,
346 LOLA_VERB_SET_SOURCE_GAIN,
347 chip->mixer.src_stream_out_ofs + i, 0);
348 for (i = 0; i < chip->mixer.dest_stream_ins; i++)
349 lola_codec_write(chip, chip->mixer.nid,
350 LOLA_VERB_SET_DESTINATION_GAIN,
351 i, 0);
352 for (i = 0; i < chip->mixer.dest_phys_outs; i++)
353 lola_codec_write(chip, chip->mixer.nid,
354 LOLA_VERB_SET_DESTINATION_GAIN,
355 chip->mixer.dest_phys_out_ofs + i, 0);
356 lola_codec_flush(chip);
357 }
358}
359
360/*
361 */
362
363static int set_analog_volume(struct lola *chip, int dir,
364 unsigned int idx, unsigned int val,
365 bool external_call)
366{
367 struct lola_pin *pin;
368 int err;
369
370 if (idx >= chip->pin[dir].num_pins)
371 return -EINVAL;
372 pin = &chip->pin[dir].pins[idx];
373 if (!pin->is_analog || pin->amp_num_steps <= val)
374 return -EINVAL;
375 if (external_call && pin->cur_gain_step == val)
376 return 0;
377 if (external_call)
378 lola_codec_flush(chip);
379 err = lola_codec_write(chip, pin->nid,
380 LOLA_VERB_SET_AMP_GAIN_MUTE, val, 0);
381 if (err < 0)
382 return err;
383 if (external_call)
384 pin->cur_gain_step = val;
385 return 0;
386}
387
388int lola_set_src_config(struct lola *chip, unsigned int src_mask, bool update)
389{
390 int ret = 0;
391 int success = 0;
392 int n, err;
393
394 /* SRC can be activated and the dwInputSRCMask is valid? */
395 if ((chip->input_src_caps_mask & src_mask) != src_mask)
396 return -EINVAL;
397 /* handle all even Inputs - SRC is a stereo setting !!! */
398 for (n = 0; n < chip->pin[CAPT].num_pins; n += 2) {
399 unsigned int mask = 3U << n; /* handle the stereo case */
400 unsigned int new_src, src_state;
401 if (!(chip->input_src_caps_mask & mask))
402 continue;
403 /* if one IO needs SRC, both stereo IO will get SRC */
404 new_src = (src_mask & mask) != 0;
405 if (update) {
406 src_state = (chip->input_src_mask & mask) != 0;
407 if (src_state == new_src)
408 continue; /* nothing to change for this IO */
409 }
410 err = lola_codec_write(chip, chip->pcm[CAPT].streams[n].nid,
411 LOLA_VERB_SET_SRC, new_src, 0);
412 if (!err)
413 success++;
414 else
415 ret = err;
416 }
417 if (success)
418 ret = lola_codec_flush(chip);
419 if (!ret)
420 chip->input_src_mask = src_mask;
421 return ret;
422}
423
424/*
425 */
426static int init_mixer_values(struct lola *chip)
427{
428 int i;
429
430 /* all src on */
431 lola_set_src_config(chip, (1 << chip->pin[CAPT].num_pins) - 1, false);
432
433 /* clear all matrix */
434 memset_io(chip->mixer.array, 0, sizeof(*chip->mixer.array));
435 /* set src gain to 0dB */
436 for (i = 0; i < chip->mixer.src_phys_ins; i++)
437 lola_mixer_set_src_gain(chip, i, 336, true); /* 0dB */
438 for (i = 0; i < chip->mixer.src_stream_outs; i++)
439 lola_mixer_set_src_gain(chip,
440 i + chip->mixer.src_stream_out_ofs,
441 336, true); /* 0dB */
442 /* set 1:1 dest gain */
443 for (i = 0; i < chip->mixer.dest_stream_ins; i++) {
444 int src = i % chip->mixer.src_phys_ins;
445 lola_mixer_set_mapping_gain(chip, src, i, 336, true);
446 }
447 for (i = 0; i < chip->mixer.src_stream_outs; i++) {
448 int src = chip->mixer.src_stream_out_ofs + i;
449 int dst = chip->mixer.dest_phys_out_ofs +
450 i % chip->mixer.dest_phys_outs;
451 lola_mixer_set_mapping_gain(chip, src, dst, 336, true);
452 }
453 return 0;
454}
455
456/*
457 * analog mixer control element
458 */
459static int lola_analog_vol_info(struct snd_kcontrol *kcontrol,
460 struct snd_ctl_elem_info *uinfo)
461{
462 struct lola *chip = snd_kcontrol_chip(kcontrol);
463 int dir = kcontrol->private_value;
464
465 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
466 uinfo->count = chip->pin[dir].num_pins;
467 uinfo->value.integer.min = 0;
468 uinfo->value.integer.max = chip->pin[dir].pins[0].amp_num_steps;
469 return 0;
470}
471
472static int lola_analog_vol_get(struct snd_kcontrol *kcontrol,
473 struct snd_ctl_elem_value *ucontrol)
474{
475 struct lola *chip = snd_kcontrol_chip(kcontrol);
476 int dir = kcontrol->private_value;
477 int i;
478
479 for (i = 0; i < chip->pin[dir].num_pins; i++)
480 ucontrol->value.integer.value[i] =
481 chip->pin[dir].pins[i].cur_gain_step;
482 return 0;
483}
484
485static int lola_analog_vol_put(struct snd_kcontrol *kcontrol,
486 struct snd_ctl_elem_value *ucontrol)
487{
488 struct lola *chip = snd_kcontrol_chip(kcontrol);
489 int dir = kcontrol->private_value;
490 int i, err;
491
492 for (i = 0; i < chip->pin[dir].num_pins; i++) {
493 err = set_analog_volume(chip, dir, i,
494 ucontrol->value.integer.value[i],
495 true);
496 if (err < 0)
497 return err;
498 }
499 return 0;
500}
501
502static int lola_analog_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
503 unsigned int size, unsigned int __user *tlv)
504{
505 struct lola *chip = snd_kcontrol_chip(kcontrol);
506 int dir = kcontrol->private_value;
507 unsigned int val1, val2;
508 struct lola_pin *pin;
509
510 if (size < 4 * sizeof(unsigned int))
511 return -ENOMEM;
512 pin = &chip->pin[dir].pins[0];
513
514 val2 = pin->amp_step_size * 25;
515 val1 = -1 * (int)pin->amp_offset * (int)val2;
516#ifdef TLV_DB_SCALE_MUTE
517 val2 |= TLV_DB_SCALE_MUTE;
518#endif
519 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, tlv))
520 return -EFAULT;
521 if (put_user(2 * sizeof(unsigned int), tlv + 1))
522 return -EFAULT;
523 if (put_user(val1, tlv + 2))
524 return -EFAULT;
525 if (put_user(val2, tlv + 3))
526 return -EFAULT;
527 return 0;
528}
529
530static struct snd_kcontrol_new lola_analog_mixer __devinitdata = {
531 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
532 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
533 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
534 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
535 .info = lola_analog_vol_info,
536 .get = lola_analog_vol_get,
537 .put = lola_analog_vol_put,
538 .tlv.c = lola_analog_vol_tlv,
539};
540
541static int __devinit create_analog_mixer(struct lola *chip, int dir, char *name)
542{
543 if (!chip->pin[dir].num_pins)
544 return 0;
545 /* no analog volumes on digital only adapters */
546 if (chip->pin[dir].num_pins != chip->pin[dir].num_analog_pins)
547 return 0;
548 lola_analog_mixer.name = name;
549 lola_analog_mixer.private_value = dir;
550 return snd_ctl_add(chip->card,
551 snd_ctl_new1(&lola_analog_mixer, chip));
552}
553
554/*
555 * Hardware sample rate converter on digital input
556 */
557static int lola_input_src_info(struct snd_kcontrol *kcontrol,
558 struct snd_ctl_elem_info *uinfo)
559{
560 struct lola *chip = snd_kcontrol_chip(kcontrol);
561
562 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
563 uinfo->count = chip->pin[CAPT].num_pins;
564 uinfo->value.integer.min = 0;
565 uinfo->value.integer.max = 1;
566 return 0;
567}
568
569static int lola_input_src_get(struct snd_kcontrol *kcontrol,
570 struct snd_ctl_elem_value *ucontrol)
571{
572 struct lola *chip = snd_kcontrol_chip(kcontrol);
573 int i;
574
575 for (i = 0; i < chip->pin[CAPT].num_pins; i++)
576 ucontrol->value.integer.value[i] =
577 !!(chip->input_src_mask & (1 << i));
578 return 0;
579}
580
581static int lola_input_src_put(struct snd_kcontrol *kcontrol,
582 struct snd_ctl_elem_value *ucontrol)
583{
584 struct lola *chip = snd_kcontrol_chip(kcontrol);
585 int i;
586 unsigned int mask;
587
588 mask = 0;
589 for (i = 0; i < chip->pin[CAPT].num_pins; i++)
590 if (ucontrol->value.integer.value[i])
591 mask |= 1 << i;
592 return lola_set_src_config(chip, mask, true);
593}
594
595static struct snd_kcontrol_new lola_input_src_mixer __devinitdata = {
596 .name = "Digital SRC Capture Switch",
597 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
598 .info = lola_input_src_info,
599 .get = lola_input_src_get,
600 .put = lola_input_src_put,
601};
602
603/*
604 * Lola16161 or Lola881 can have Hardware sample rate converters
605 * on its digital input pins
606 */
607static int __devinit create_input_src_mixer(struct lola *chip)
608{
609 if (!chip->input_src_caps_mask)
610 return 0;
611
612 return snd_ctl_add(chip->card,
613 snd_ctl_new1(&lola_input_src_mixer, chip));
614}
615
616/*
617 * src gain mixer
618 */
619static int lola_src_gain_info(struct snd_kcontrol *kcontrol,
620 struct snd_ctl_elem_info *uinfo)
621{
622 unsigned int count = (kcontrol->private_value >> 8) & 0xff;
623
624 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
625 uinfo->count = count;
626 uinfo->value.integer.min = 0;
627 uinfo->value.integer.max = 409;
628 return 0;
629}
630
631static int lola_src_gain_get(struct snd_kcontrol *kcontrol,
632 struct snd_ctl_elem_value *ucontrol)
633{
634 struct lola *chip = snd_kcontrol_chip(kcontrol);
635 unsigned int ofs = kcontrol->private_value & 0xff;
636 unsigned int count = (kcontrol->private_value >> 8) & 0xff;
637 unsigned int mask, i;
638
639 mask = readl(&chip->mixer.array->src_gain_enable);
640 for (i = 0; i < count; i++) {
641 unsigned int idx = ofs + i;
642 unsigned short val;
643 if (!(chip->mixer.src_mask & (1 << idx)))
644 return -EINVAL;
645 if (mask & (1 << idx))
646 val = readw(&chip->mixer.array->src_gain[idx]) + 1;
647 else
648 val = 0;
649 ucontrol->value.integer.value[i] = val;
650 }
651 return 0;
652}
653
654static int lola_src_gain_put(struct snd_kcontrol *kcontrol,
655 struct snd_ctl_elem_value *ucontrol)
656{
657 struct lola *chip = snd_kcontrol_chip(kcontrol);
658 unsigned int ofs = kcontrol->private_value & 0xff;
659 unsigned int count = (kcontrol->private_value >> 8) & 0xff;
660 int i, err;
661
662 for (i = 0; i < count; i++) {
663 unsigned int idx = ofs + i;
664 unsigned short val = ucontrol->value.integer.value[i];
665 if (val)
666 val--;
667 err = lola_mixer_set_src_gain(chip, idx, val, !!val);
668 if (err < 0)
669 return err;
670 }
671 return 0;
672}
673
674/* raw value: 0 = -84dB, 336 = 0dB, 408=18dB, incremented 1 for mute */
675static const DECLARE_TLV_DB_SCALE(lola_src_gain_tlv, -8425, 25, 1);
676
677static struct snd_kcontrol_new lola_src_gain_mixer __devinitdata = {
678 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
679 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
680 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
681 .info = lola_src_gain_info,
682 .get = lola_src_gain_get,
683 .put = lola_src_gain_put,
684 .tlv.p = lola_src_gain_tlv,
685};
686
687static int __devinit create_src_gain_mixer(struct lola *chip,
688 int num, int ofs, char *name)
689{
690 lola_src_gain_mixer.name = name;
691 lola_src_gain_mixer.private_value = ofs + (num << 8);
692 return snd_ctl_add(chip->card,
693 snd_ctl_new1(&lola_src_gain_mixer, chip));
694}
695
696/*
697 * destination gain (matrix-like) mixer
698 */
699static int lola_dest_gain_info(struct snd_kcontrol *kcontrol,
700 struct snd_ctl_elem_info *uinfo)
701{
702 unsigned int src_num = (kcontrol->private_value >> 8) & 0xff;
703
704 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
705 uinfo->count = src_num;
706 uinfo->value.integer.min = 0;
707 uinfo->value.integer.max = 433;
708 return 0;
709}
710
711static int lola_dest_gain_get(struct snd_kcontrol *kcontrol,
712 struct snd_ctl_elem_value *ucontrol)
713{
714 struct lola *chip = snd_kcontrol_chip(kcontrol);
715 unsigned int src_ofs = kcontrol->private_value & 0xff;
716 unsigned int src_num = (kcontrol->private_value >> 8) & 0xff;
717 unsigned int dst_ofs = (kcontrol->private_value >> 16) & 0xff;
718 unsigned int dst, mask, i;
719
720 dst = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + dst_ofs;
721 mask = readl(&chip->mixer.array->dest_mix_gain_enable[dst]);
722 for (i = 0; i < src_num; i++) {
723 unsigned int src = src_ofs + i;
724 unsigned short val;
725 if (!(chip->mixer.src_mask & (1 << src)))
726 return -EINVAL;
727 if (mask & (1 << dst))
728 val = readw(&chip->mixer.array->dest_mix_gain[dst][src]) + 1;
729 else
730 val = 0;
731 ucontrol->value.integer.value[i] = val;
732 }
733 return 0;
734}
735
736static int lola_dest_gain_put(struct snd_kcontrol *kcontrol,
737 struct snd_ctl_elem_value *ucontrol)
738{
739 struct lola *chip = snd_kcontrol_chip(kcontrol);
740 unsigned int src_ofs = kcontrol->private_value & 0xff;
741 unsigned int src_num = (kcontrol->private_value >> 8) & 0xff;
742 unsigned int dst_ofs = (kcontrol->private_value >> 16) & 0xff;
743 unsigned int dst, mask;
744 unsigned short gains[MAX_STREAM_COUNT];
745 int i, num;
746
747 mask = 0;
748 num = 0;
749 for (i = 0; i < src_num; i++) {
750 unsigned short val = ucontrol->value.integer.value[i];
751 if (val) {
752 gains[num++] = val - 1;
753 mask |= 1 << i;
754 }
755 }
756 mask <<= src_ofs;
757 dst = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + dst_ofs;
758 return lola_mixer_set_dest_gains(chip, dst, mask, gains);
759}
760
761static const DECLARE_TLV_DB_SCALE(lola_dest_gain_tlv, -8425, 25, 1);
762
763static struct snd_kcontrol_new lola_dest_gain_mixer __devinitdata = {
764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
765 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
766 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
767 .info = lola_dest_gain_info,
768 .get = lola_dest_gain_get,
769 .put = lola_dest_gain_put,
770 .tlv.p = lola_dest_gain_tlv,
771};
772
773static int __devinit create_dest_gain_mixer(struct lola *chip,
774 int src_num, int src_ofs,
775 int num, int ofs, char *name)
776{
777 lola_dest_gain_mixer.count = num;
778 lola_dest_gain_mixer.name = name;
779 lola_dest_gain_mixer.private_value =
780 src_ofs + (src_num << 8) + (ofs << 16) + (num << 24);
781 return snd_ctl_add(chip->card,
782 snd_ctl_new1(&lola_dest_gain_mixer, chip));
783}
784
785/*
786 */
787int __devinit lola_create_mixer(struct lola *chip)
788{
789 int err;
790
791 err = create_analog_mixer(chip, PLAY, "Analog Playback Volume");
792 if (err < 0)
793 return err;
794 err = create_analog_mixer(chip, CAPT, "Analog Capture Volume");
795 if (err < 0)
796 return err;
797 err = create_input_src_mixer(chip);
798 if (err < 0)
799 return err;
800 err = create_src_gain_mixer(chip, chip->mixer.src_phys_ins, 0,
801 "Line Source Gain Volume");
802 if (err < 0)
803 return err;
804 err = create_src_gain_mixer(chip, chip->mixer.src_stream_outs,
805 chip->mixer.src_stream_out_ofs,
806 "Stream Source Gain Volume");
807 if (err < 0)
808 return err;
809 err = create_dest_gain_mixer(chip,
810 chip->mixer.src_phys_ins, 0,
811 chip->mixer.dest_stream_ins, 0,
812 "Line Capture Volume");
813 if (err < 0)
814 return err;
815 err = create_dest_gain_mixer(chip,
816 chip->mixer.src_stream_outs,
817 chip->mixer.src_stream_out_ofs,
818 chip->mixer.dest_stream_ins, 0,
819 "Stream-Loopback Capture Volume");
820 if (err < 0)
821 return err;
822 err = create_dest_gain_mixer(chip,
823 chip->mixer.src_phys_ins, 0,
824 chip->mixer.dest_phys_outs,
825 chip->mixer.dest_phys_out_ofs,
826 "Line-Loopback Playback Volume");
827 if (err < 0)
828 return err;
829 err = create_dest_gain_mixer(chip,
830 chip->mixer.src_stream_outs,
831 chip->mixer.src_stream_out_ofs,
832 chip->mixer.dest_phys_outs,
833 chip->mixer.dest_phys_out_ofs,
834 "Stream Playback Volume");
835 if (err < 0)
836 return err;
837
838 return init_mixer_values(chip);
839}
diff --git a/sound/pci/lola/lola_pcm.c b/sound/pci/lola/lola_pcm.c
new file mode 100644
index 000000000000..c44db68eecb5
--- /dev/null
+++ b/sound/pci/lola/lola_pcm.c
@@ -0,0 +1,706 @@
1/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/dma-mapping.h>
24#include <linux/pci.h>
25#include <linux/delay.h>
26#include <sound/core.h>
27#include <sound/pcm.h>
28#include "lola.h"
29
30#define LOLA_MAX_BDL_ENTRIES 8
31#define LOLA_MAX_BUF_SIZE (1024*1024*1024)
32#define LOLA_BDL_ENTRY_SIZE (16 * 16)
33
34static struct lola_pcm *lola_get_pcm(struct snd_pcm_substream *substream)
35{
36 struct lola *chip = snd_pcm_substream_chip(substream);
37 return &chip->pcm[substream->stream];
38}
39
40static struct lola_stream *lola_get_stream(struct snd_pcm_substream *substream)
41{
42 struct lola_pcm *pcm = lola_get_pcm(substream);
43 unsigned int idx = substream->number;
44 return &pcm->streams[idx];
45}
46
47static unsigned int lola_get_lrc(struct lola *chip)
48{
49 return lola_readl(chip, BAR1, LRC);
50}
51
52static unsigned int lola_get_tstamp(struct lola *chip, bool quick_no_sync)
53{
54 unsigned int tstamp = lola_get_lrc(chip) >> 8;
55 if (chip->granularity) {
56 unsigned int wait_banks = quick_no_sync ? 0 : 8;
57 tstamp += (wait_banks + 1) * chip->granularity - 1;
58 tstamp -= tstamp % chip->granularity;
59 }
60 return tstamp << 8;
61}
62
63/* clear any pending interrupt status */
64static void lola_stream_clear_pending_irq(struct lola *chip,
65 struct lola_stream *str)
66{
67 unsigned int val = lola_dsd_read(chip, str->dsd, STS);
68 val &= LOLA_DSD_STS_DESE | LOLA_DSD_STS_BCIS;
69 if (val)
70 lola_dsd_write(chip, str->dsd, STS, val);
71}
72
73static void lola_stream_start(struct lola *chip, struct lola_stream *str,
74 unsigned int tstamp)
75{
76 lola_stream_clear_pending_irq(chip, str);
77 lola_dsd_write(chip, str->dsd, CTL,
78 LOLA_DSD_CTL_SRUN |
79 LOLA_DSD_CTL_IOCE |
80 LOLA_DSD_CTL_DEIE |
81 LOLA_DSD_CTL_VLRCV |
82 tstamp);
83}
84
85static void lola_stream_stop(struct lola *chip, struct lola_stream *str,
86 unsigned int tstamp)
87{
88 lola_dsd_write(chip, str->dsd, CTL,
89 LOLA_DSD_CTL_IOCE |
90 LOLA_DSD_CTL_DEIE |
91 LOLA_DSD_CTL_VLRCV |
92 tstamp);
93 lola_stream_clear_pending_irq(chip, str);
94}
95
96static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
97{
98 unsigned long end_time = jiffies + msecs_to_jiffies(200);
99 while (time_before(jiffies, end_time)) {
100 unsigned int val;
101 val = lola_dsd_read(chip, str->dsd, CTL);
102 if (!(val & LOLA_DSD_CTL_SRST))
103 return;
104 msleep(1);
105 }
106 printk(KERN_WARNING SFX "SRST not clear (stream %d)\n", str->dsd);
107}
108
109static int lola_stream_wait_for_fifo(struct lola *chip,
110 struct lola_stream *str,
111 bool ready)
112{
113 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
114 unsigned long end_time = jiffies + msecs_to_jiffies(200);
115 while (time_before(jiffies, end_time)) {
116 unsigned int reg = lola_dsd_read(chip, str->dsd, STS);
117 if ((reg & LOLA_DSD_STS_FIFORDY) == val)
118 return 0;
119 msleep(1);
120 }
121 printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
122 return -EIO;
123}
124
125/* sync for FIFO ready/empty for all linked streams;
126 * clear paused flag when FIFO gets ready again
127 */
128static int lola_sync_wait_for_fifo(struct lola *chip,
129 struct snd_pcm_substream *substream,
130 bool ready)
131{
132 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
133 unsigned long end_time = jiffies + msecs_to_jiffies(200);
134 struct snd_pcm_substream *s;
135 int pending = 0;
136
137 while (time_before(jiffies, end_time)) {
138 pending = 0;
139 snd_pcm_group_for_each_entry(s, substream) {
140 struct lola_stream *str;
141 if (s->pcm->card != substream->pcm->card)
142 continue;
143 str = lola_get_stream(s);
144 if (str->prepared && str->paused) {
145 unsigned int reg;
146 reg = lola_dsd_read(chip, str->dsd, STS);
147 if ((reg & LOLA_DSD_STS_FIFORDY) != val) {
148 pending = str->dsd + 1;
149 break;
150 }
151 if (ready)
152 str->paused = 0;
153 }
154 }
155 if (!pending)
156 return 0;
157 msleep(1);
158 }
159 printk(KERN_WARNING SFX "FIFO not ready (pending %d)\n", pending - 1);
160 return -EIO;
161}
162
163/* finish pause - prepare for a new resume */
164static void lola_sync_pause(struct lola *chip,
165 struct snd_pcm_substream *substream)
166{
167 struct snd_pcm_substream *s;
168
169 lola_sync_wait_for_fifo(chip, substream, false);
170 snd_pcm_group_for_each_entry(s, substream) {
171 struct lola_stream *str;
172 if (s->pcm->card != substream->pcm->card)
173 continue;
174 str = lola_get_stream(s);
175 if (str->paused && str->prepared)
176 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN |
177 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
178 }
179 lola_sync_wait_for_fifo(chip, substream, true);
180}
181
182static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
183{
184 if (str->prepared) {
185 if (str->paused)
186 lola_sync_pause(chip, str->substream);
187 str->prepared = 0;
188 lola_dsd_write(chip, str->dsd, CTL,
189 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
190 lola_stream_wait_for_fifo(chip, str, false);
191 lola_stream_clear_pending_irq(chip, str);
192 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
193 lola_dsd_write(chip, str->dsd, LVI, 0);
194 lola_dsd_write(chip, str->dsd, BDPU, 0);
195 lola_dsd_write(chip, str->dsd, BDPL, 0);
196 wait_for_srst_clear(chip, str);
197 }
198}
199
200static struct snd_pcm_hardware lola_pcm_hw = {
201 .info = (SNDRV_PCM_INFO_MMAP |
202 SNDRV_PCM_INFO_INTERLEAVED |
203 SNDRV_PCM_INFO_BLOCK_TRANSFER |
204 SNDRV_PCM_INFO_MMAP_VALID |
205 SNDRV_PCM_INFO_PAUSE),
206 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
207 SNDRV_PCM_FMTBIT_S24_LE |
208 SNDRV_PCM_FMTBIT_S32_LE |
209 SNDRV_PCM_FMTBIT_FLOAT_LE),
210 .rates = SNDRV_PCM_RATE_8000_192000,
211 .rate_min = 8000,
212 .rate_max = 192000,
213 .channels_min = 1,
214 .channels_max = 2,
215 .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
216 .period_bytes_min = 128,
217 .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
218 .periods_min = 2,
219 .periods_max = LOLA_MAX_BDL_ENTRIES,
220 .fifo_size = 0,
221};
222
223static int lola_pcm_open(struct snd_pcm_substream *substream)
224{
225 struct lola *chip = snd_pcm_substream_chip(substream);
226 struct lola_pcm *pcm = lola_get_pcm(substream);
227 struct lola_stream *str = lola_get_stream(substream);
228 struct snd_pcm_runtime *runtime = substream->runtime;
229
230 mutex_lock(&chip->open_mutex);
231 if (str->opened) {
232 mutex_unlock(&chip->open_mutex);
233 return -EBUSY;
234 }
235 str->substream = substream;
236 str->master = NULL;
237 str->opened = 1;
238 runtime->hw = lola_pcm_hw;
239 runtime->hw.channels_max = pcm->num_streams - str->index;
240 if (chip->sample_rate) {
241 /* sample rate is locked */
242 runtime->hw.rate_min = chip->sample_rate;
243 runtime->hw.rate_max = chip->sample_rate;
244 } else {
245 runtime->hw.rate_min = chip->sample_rate_min;
246 runtime->hw.rate_max = chip->sample_rate_max;
247 }
248 chip->ref_count_rate++;
249 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
250 /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
251 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
252 chip->granularity);
253 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
254 chip->granularity);
255 mutex_unlock(&chip->open_mutex);
256 return 0;
257}
258
259static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
260 struct lola_stream *str)
261{
262 int i;
263 for (i = str->index + 1; i < pcm->num_streams; i++) {
264 struct lola_stream *s = &pcm->streams[i];
265 if (s->master != str)
266 break;
267 s->master = NULL;
268 s->opened = 0;
269 }
270}
271
272static int lola_pcm_close(struct snd_pcm_substream *substream)
273{
274 struct lola *chip = snd_pcm_substream_chip(substream);
275 struct lola_stream *str = lola_get_stream(substream);
276
277 mutex_lock(&chip->open_mutex);
278 if (str->substream == substream) {
279 str->substream = NULL;
280 str->opened = 0;
281 }
282 if (--chip->ref_count_rate == 0) {
283 /* release sample rate */
284 chip->sample_rate = 0;
285 }
286 mutex_unlock(&chip->open_mutex);
287 return 0;
288}
289
290static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
291 struct snd_pcm_hw_params *hw_params)
292{
293 struct lola_stream *str = lola_get_stream(substream);
294
295 str->bufsize = 0;
296 str->period_bytes = 0;
297 str->format_verb = 0;
298 return snd_pcm_lib_malloc_pages(substream,
299 params_buffer_bytes(hw_params));
300}
301
302static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
303{
304 struct lola *chip = snd_pcm_substream_chip(substream);
305 struct lola_pcm *pcm = lola_get_pcm(substream);
306 struct lola_stream *str = lola_get_stream(substream);
307
308 mutex_lock(&chip->open_mutex);
309 lola_stream_reset(chip, str);
310 lola_cleanup_slave_streams(pcm, str);
311 mutex_unlock(&chip->open_mutex);
312 return snd_pcm_lib_free_pages(substream);
313}
314
315/*
316 * set up a BDL entry
317 */
318static int setup_bdle(struct snd_pcm_substream *substream,
319 struct lola_stream *str, u32 **bdlp,
320 int ofs, int size)
321{
322 u32 *bdl = *bdlp;
323
324 while (size > 0) {
325 dma_addr_t addr;
326 int chunk;
327
328 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
329 return -EINVAL;
330
331 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
332 /* program the address field of the BDL entry */
333 bdl[0] = cpu_to_le32((u32)addr);
334 bdl[1] = cpu_to_le32(upper_32_bits(addr));
335 /* program the size field of the BDL entry */
336 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
337 bdl[2] = cpu_to_le32(chunk);
338 /* program the IOC to enable interrupt
339 * only when the whole fragment is processed
340 */
341 size -= chunk;
342 bdl[3] = size ? 0 : cpu_to_le32(0x01);
343 bdl += 4;
344 str->frags++;
345 ofs += chunk;
346 }
347 *bdlp = bdl;
348 return ofs;
349}
350
351/*
352 * set up BDL entries
353 */
354static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
355 struct snd_pcm_substream *substream,
356 struct lola_stream *str)
357{
358 u32 *bdl;
359 int i, ofs, periods, period_bytes;
360
361 period_bytes = str->period_bytes;
362 periods = str->bufsize / period_bytes;
363
364 /* program the initial BDL entries */
365 bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
366 ofs = 0;
367 str->frags = 0;
368 for (i = 0; i < periods; i++) {
369 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
370 if (ofs < 0)
371 goto error;
372 }
373 return 0;
374
375 error:
376 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
377 str->bufsize, period_bytes);
378 return -EINVAL;
379}
380
381static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
382{
383 unsigned int verb;
384
385 switch (substream->runtime->format) {
386 case SNDRV_PCM_FORMAT_S16_LE:
387 verb = 0x00000000;
388 break;
389 case SNDRV_PCM_FORMAT_S24_LE:
390 verb = 0x00000200;
391 break;
392 case SNDRV_PCM_FORMAT_S32_LE:
393 verb = 0x00000300;
394 break;
395 case SNDRV_PCM_FORMAT_FLOAT_LE:
396 verb = 0x00001300;
397 break;
398 default:
399 return 0;
400 }
401 verb |= substream->runtime->channels;
402 return verb;
403}
404
405static int lola_set_stream_config(struct lola *chip,
406 struct lola_stream *str,
407 int channels)
408{
409 int i, err;
410 unsigned int verb, val;
411
412 /* set format info for all channels
413 * (with only one command for the first channel)
414 */
415 err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
416 str->format_verb, 0, &val, NULL);
417 if (err < 0) {
418 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
419 str->format_verb);
420 return err;
421 }
422
423 /* update stream - channel config */
424 for (i = 0; i < channels; i++) {
425 verb = (str->index << 6) | i;
426 err = lola_codec_read(chip, str[i].nid,
427 LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
428 &val, NULL);
429 if (err < 0) {
430 printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
431 return err;
432 }
433 }
434 return 0;
435}
436
437/*
438 * set up the SD for streaming
439 */
440static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
441 struct lola_stream *str)
442{
443 dma_addr_t bdl;
444
445 if (str->prepared)
446 return -EINVAL;
447
448 /* set up BDL */
449 bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
450 lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
451 lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
452 /* program the stream LVI (last valid index) of the BDL */
453 lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
454 lola_stream_clear_pending_irq(chip, str);
455
456 lola_dsd_write(chip, str->dsd, CTL,
457 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
458
459 str->prepared = 1;
460
461 return lola_stream_wait_for_fifo(chip, str, true);
462}
463
464static int lola_pcm_prepare(struct snd_pcm_substream *substream)
465{
466 struct lola *chip = snd_pcm_substream_chip(substream);
467 struct lola_pcm *pcm = lola_get_pcm(substream);
468 struct lola_stream *str = lola_get_stream(substream);
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 unsigned int bufsize, period_bytes, format_verb;
471 int i, err;
472
473 mutex_lock(&chip->open_mutex);
474 lola_stream_reset(chip, str);
475 lola_cleanup_slave_streams(pcm, str);
476 if (str->index + runtime->channels > pcm->num_streams) {
477 mutex_unlock(&chip->open_mutex);
478 return -EINVAL;
479 }
480 for (i = 1; i < runtime->channels; i++) {
481 str[i].master = str;
482 str[i].opened = 1;
483 }
484 mutex_unlock(&chip->open_mutex);
485
486 bufsize = snd_pcm_lib_buffer_bytes(substream);
487 period_bytes = snd_pcm_lib_period_bytes(substream);
488 format_verb = lola_get_format_verb(substream);
489
490 str->bufsize = bufsize;
491 str->period_bytes = period_bytes;
492 str->format_verb = format_verb;
493
494 err = lola_setup_periods(chip, pcm, substream, str);
495 if (err < 0)
496 return err;
497
498 err = lola_set_sample_rate(chip, runtime->rate);
499 if (err < 0)
500 return err;
501 chip->sample_rate = runtime->rate; /* sample rate gets locked */
502
503 err = lola_set_stream_config(chip, str, runtime->channels);
504 if (err < 0)
505 return err;
506
507 err = lola_setup_controller(chip, pcm, str);
508 if (err < 0) {
509 lola_stream_reset(chip, str);
510 return err;
511 }
512
513 return 0;
514}
515
516static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
517{
518 struct lola *chip = snd_pcm_substream_chip(substream);
519 struct lola_stream *str;
520 struct snd_pcm_substream *s;
521 unsigned int start;
522 unsigned int tstamp;
523 bool sync_streams;
524
525 switch (cmd) {
526 case SNDRV_PCM_TRIGGER_START:
527 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
528 case SNDRV_PCM_TRIGGER_RESUME:
529 start = 1;
530 break;
531 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
532 case SNDRV_PCM_TRIGGER_SUSPEND:
533 case SNDRV_PCM_TRIGGER_STOP:
534 start = 0;
535 break;
536 default:
537 return -EINVAL;
538 }
539
540 /*
541 * sample correct synchronization is only needed starting several
542 * streams. On stop or if only one stream do as quick as possible
543 */
544 sync_streams = (start && snd_pcm_stream_linked(substream));
545 tstamp = lola_get_tstamp(chip, !sync_streams);
546 spin_lock(&chip->reg_lock);
547 snd_pcm_group_for_each_entry(s, substream) {
548 if (s->pcm->card != substream->pcm->card)
549 continue;
550 str = lola_get_stream(s);
551 if (start)
552 lola_stream_start(chip, str, tstamp);
553 else
554 lola_stream_stop(chip, str, tstamp);
555 str->running = start;
556 str->paused = !start;
557 snd_pcm_trigger_done(s, substream);
558 }
559 spin_unlock(&chip->reg_lock);
560 return 0;
561}
562
563static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
564{
565 struct lola *chip = snd_pcm_substream_chip(substream);
566 struct lola_stream *str = lola_get_stream(substream);
567 unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
568
569 if (pos >= str->bufsize)
570 pos = 0;
571 return bytes_to_frames(substream->runtime, pos);
572}
573
574void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
575{
576 int i;
577
578 for (i = 0; bits && i < pcm->num_streams; i++) {
579 if (bits & (1 << i)) {
580 struct lola_stream *str = &pcm->streams[i];
581 if (str->substream && str->running)
582 snd_pcm_period_elapsed(str->substream);
583 bits &= ~(1 << i);
584 }
585 }
586}
587
588static struct snd_pcm_ops lola_pcm_ops = {
589 .open = lola_pcm_open,
590 .close = lola_pcm_close,
591 .ioctl = snd_pcm_lib_ioctl,
592 .hw_params = lola_pcm_hw_params,
593 .hw_free = lola_pcm_hw_free,
594 .prepare = lola_pcm_prepare,
595 .trigger = lola_pcm_trigger,
596 .pointer = lola_pcm_pointer,
597 .page = snd_pcm_sgbuf_ops_page,
598};
599
600int __devinit lola_create_pcm(struct lola *chip)
601{
602 struct snd_pcm *pcm;
603 int i, err;
604
605 for (i = 0; i < 2; i++) {
606 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
607 snd_dma_pci_data(chip->pci),
608 PAGE_SIZE, &chip->pcm[i].bdl);
609 if (err < 0)
610 return err;
611 }
612
613 err = snd_pcm_new(chip->card, "Digigram Lola", 0,
614 chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
615 chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
616 &pcm);
617 if (err < 0)
618 return err;
619 strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
620 pcm->private_data = chip;
621 for (i = 0; i < 2; i++) {
622 if (chip->pcm[i].num_streams)
623 snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
624 }
625 /* buffer pre-allocation */
626 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
627 snd_dma_pci_data(chip->pci),
628 1024 * 64, 32 * 1024 * 1024);
629 return 0;
630}
631
632void lola_free_pcm(struct lola *chip)
633{
634 snd_dma_free_pages(&chip->pcm[0].bdl);
635 snd_dma_free_pages(&chip->pcm[1].bdl);
636}
637
638/*
639 */
640
641static int lola_init_stream(struct lola *chip, struct lola_stream *str,
642 int idx, int nid, int dir)
643{
644 unsigned int val;
645 int err;
646
647 str->nid = nid;
648 str->index = idx;
649 str->dsd = idx;
650 if (dir == PLAY)
651 str->dsd += MAX_STREAM_IN_COUNT;
652 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
653 if (err < 0) {
654 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
655 return err;
656 }
657 if (dir == PLAY) {
658 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
659 if ((val & 0x00f00dff) != 0x00000010) {
660 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
661 val, nid);
662 return -EINVAL;
663 }
664 } else {
665 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
666 * (bug : ignore bit8: Conn list = 0/1)
667 */
668 if ((val & 0x00f00cff) != 0x00100010) {
669 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
670 val, nid);
671 return -EINVAL;
672 }
673 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
674 if ((val & 0x00001200) == 0x00001200)
675 chip->input_src_caps_mask |= (1 << idx);
676 }
677
678 err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
679 if (err < 0) {
680 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
681 return err;
682 }
683 val &= 3;
684 if (val == 3)
685 str->can_float = true;
686 if (!(val & 1)) {
687 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
688 return -EINVAL;
689 }
690 return 0;
691}
692
693int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
694{
695 struct lola_pcm *pcm = &chip->pcm[dir];
696 int i, nid, err;
697
698 nid = *nidp;
699 for (i = 0; i < pcm->num_streams; i++, nid++) {
700 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
701 if (err < 0)
702 return err;
703 }
704 *nidp = nid;
705 return 0;
706}
diff --git a/sound/pci/lola/lola_proc.c b/sound/pci/lola/lola_proc.c
new file mode 100644
index 000000000000..9d7daf897c9d
--- /dev/null
+++ b/sound/pci/lola/lola_proc.c
@@ -0,0 +1,222 @@
1/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/io.h>
24#include <sound/core.h>
25#include <sound/info.h>
26#include <sound/pcm.h>
27#include "lola.h"
28
29static void print_audio_widget(struct snd_info_buffer *buffer,
30 struct lola *chip, int nid, const char *name)
31{
32 unsigned int val;
33
34 lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
35 snd_iprintf(buffer, "Node 0x%02x %s wcaps 0x%x\n", nid, name, val);
36 lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
37 snd_iprintf(buffer, " Formats: 0x%x\n", val);
38}
39
40static void print_pin_widget(struct snd_info_buffer *buffer,
41 struct lola *chip, int nid, unsigned int ampcap,
42 const char *name)
43{
44 unsigned int val;
45
46 lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
47 snd_iprintf(buffer, "Node 0x%02x %s wcaps 0x%x\n", nid, name, val);
48 if (val == 0x00400200)
49 return;
50 lola_read_param(chip, nid, ampcap, &val);
51 snd_iprintf(buffer, " Amp-Caps: 0x%x\n", val);
52 snd_iprintf(buffer, " mute=%d, step-size=%d, steps=%d, ofs=%d\n",
53 LOLA_AMP_MUTE_CAPABLE(val),
54 LOLA_AMP_STEP_SIZE(val),
55 LOLA_AMP_NUM_STEPS(val),
56 LOLA_AMP_OFFSET(val));
57 lola_codec_read(chip, nid, LOLA_VERB_GET_MAX_LEVEL, 0, 0, &val, NULL);
58 snd_iprintf(buffer, " Max-level: 0x%x\n", val);
59}
60
61static void print_clock_widget(struct snd_info_buffer *buffer,
62 struct lola *chip, int nid)
63{
64 int i, j, num_clocks;
65 unsigned int val;
66
67 lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
68 snd_iprintf(buffer, "Node 0x%02x [Clock] wcaps 0x%x\n", nid, val);
69 num_clocks = val & 0xff;
70 for (i = 0; i < num_clocks; i += 4) {
71 unsigned int res_ex;
72 unsigned short items[4];
73 const char *name;
74
75 lola_codec_read(chip, nid, LOLA_VERB_GET_CLOCK_LIST,
76 i, 0, &val, &res_ex);
77 items[0] = val & 0xfff;
78 items[1] = (val >> 16) & 0xfff;
79 items[2] = res_ex & 0xfff;
80 items[3] = (res_ex >> 16) & 0xfff;
81 for (j = 0; j < 4; j++) {
82 unsigned char type = items[j] >> 8;
83 unsigned int freq = items[j] & 0xff;
84 if (i + j >= num_clocks)
85 break;
86 if (type == LOLA_CLOCK_TYPE_INTERNAL) {
87 name = "Internal";
88 freq = lola_sample_rate_convert(freq);
89 } else if (type == LOLA_CLOCK_TYPE_VIDEO) {
90 name = "Video";
91 freq = lola_sample_rate_convert(freq);
92 } else {
93 name = "Other";
94 }
95 snd_iprintf(buffer, " Clock %d: Type %d:%s, freq=%d\n",
96 i + j, type, name, freq);
97 }
98 }
99}
100
101static void print_mixer_widget(struct snd_info_buffer *buffer,
102 struct lola *chip, int nid)
103{
104 unsigned int val;
105
106 lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
107 snd_iprintf(buffer, "Node 0x%02x [Mixer] wcaps 0x%x\n", nid, val);
108}
109
110static void lola_proc_codec_read(struct snd_info_entry *entry,
111 struct snd_info_buffer *buffer)
112{
113 struct lola *chip = entry->private_data;
114 unsigned int val;
115 int i, nid;
116
117 lola_read_param(chip, 0, LOLA_PAR_VENDOR_ID, &val);
118 snd_iprintf(buffer, "Vendor: 0x%08x\n", val);
119 lola_read_param(chip, 1, LOLA_PAR_FUNCTION_TYPE, &val);
120 snd_iprintf(buffer, "Function Type: %d\n", val);
121 lola_read_param(chip, 1, LOLA_PAR_SPECIFIC_CAPS, &val);
122 snd_iprintf(buffer, "Specific-Caps: 0x%08x\n", val);
123 snd_iprintf(buffer, " Pins-In %d, Pins-Out %d\n",
124 chip->pin[CAPT].num_pins, chip->pin[PLAY].num_pins);
125 nid = 2;
126 for (i = 0; i < chip->pcm[CAPT].num_streams; i++, nid++)
127 print_audio_widget(buffer, chip, nid, "[Audio-In]");
128 for (i = 0; i < chip->pcm[PLAY].num_streams; i++, nid++)
129 print_audio_widget(buffer, chip, nid, "[Audio-Out]");
130 for (i = 0; i < chip->pin[CAPT].num_pins; i++, nid++)
131 print_pin_widget(buffer, chip, nid, LOLA_PAR_AMP_IN_CAP,
132 "[Pin-In]");
133 for (i = 0; i < chip->pin[PLAY].num_pins; i++, nid++)
134 print_pin_widget(buffer, chip, nid, LOLA_PAR_AMP_OUT_CAP,
135 "[Pin-Out]");
136 if (LOLA_AFG_CLOCK_WIDGET_PRESENT(chip->lola_caps)) {
137 print_clock_widget(buffer, chip, nid);
138 nid++;
139 }
140 if (LOLA_AFG_MIXER_WIDGET_PRESENT(chip->lola_caps)) {
141 print_mixer_widget(buffer, chip, nid);
142 nid++;
143 }
144}
145
146/* direct codec access for debugging */
147static void lola_proc_codec_rw_write(struct snd_info_entry *entry,
148 struct snd_info_buffer *buffer)
149{
150 struct lola *chip = entry->private_data;
151 char line[64];
152 unsigned int id, verb, data, extdata;
153 while (!snd_info_get_line(buffer, line, sizeof(line))) {
154 if (sscanf(line, "%i %i %i %i", &id, &verb, &data, &extdata) != 4)
155 continue;
156 lola_codec_read(chip, id, verb, data, extdata,
157 &chip->debug_res,
158 &chip->debug_res_ex);
159 }
160}
161
162static void lola_proc_codec_rw_read(struct snd_info_entry *entry,
163 struct snd_info_buffer *buffer)
164{
165 struct lola *chip = entry->private_data;
166 snd_iprintf(buffer, "0x%x 0x%x\n", chip->debug_res, chip->debug_res_ex);
167}
168
169/*
170 * dump some registers
171 */
172static void lola_proc_regs_read(struct snd_info_entry *entry,
173 struct snd_info_buffer *buffer)
174{
175 struct lola *chip = entry->private_data;
176 int i;
177
178 for (i = 0; i < 0x40; i += 4) {
179 snd_iprintf(buffer, "BAR0 %02x: %08x\n", i,
180 readl(chip->bar[BAR0].remap_addr + i));
181 }
182 snd_iprintf(buffer, "\n");
183 for (i = 0; i < 0x30; i += 4) {
184 snd_iprintf(buffer, "BAR1 %02x: %08x\n", i,
185 readl(chip->bar[BAR1].remap_addr + i));
186 }
187 snd_iprintf(buffer, "\n");
188 for (i = 0x80; i < 0xa0; i += 4) {
189 snd_iprintf(buffer, "BAR1 %02x: %08x\n", i,
190 readl(chip->bar[BAR1].remap_addr + i));
191 }
192 snd_iprintf(buffer, "\n");
193 for (i = 0; i < 32; i++) {
194 snd_iprintf(buffer, "DSD %02x STS %08x\n", i,
195 lola_dsd_read(chip, i, STS));
196 snd_iprintf(buffer, "DSD %02x LPIB %08x\n", i,
197 lola_dsd_read(chip, i, LPIB));
198 snd_iprintf(buffer, "DSD %02x CTL %08x\n", i,
199 lola_dsd_read(chip, i, CTL));
200 snd_iprintf(buffer, "DSD %02x LVIL %08x\n", i,
201 lola_dsd_read(chip, i, LVI));
202 snd_iprintf(buffer, "DSD %02x BDPL %08x\n", i,
203 lola_dsd_read(chip, i, BDPL));
204 snd_iprintf(buffer, "DSD %02x BDPU %08x\n", i,
205 lola_dsd_read(chip, i, BDPU));
206 }
207}
208
209void __devinit lola_proc_debug_new(struct lola *chip)
210{
211 struct snd_info_entry *entry;
212
213 if (!snd_card_proc_new(chip->card, "codec", &entry))
214 snd_info_set_text_ops(entry, chip, lola_proc_codec_read);
215 if (!snd_card_proc_new(chip->card, "codec_rw", &entry)) {
216 snd_info_set_text_ops(entry, chip, lola_proc_codec_rw_read);
217 entry->mode |= S_IWUSR;
218 entry->c.text.write = lola_proc_codec_rw_write;
219 }
220 if (!snd_card_proc_new(chip->card, "regs", &entry))
221 snd_info_set_text_ops(entry, chip, lola_proc_regs_read);
222}