vserver 1.9.3
[linux-2.6.git] / sound / pci / bt87x.c
1 /*
2  * bt87x.c - Brooktree Bt878/Bt879 driver for ALSA
3  *
4  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  *
6  * based on btaudio.c by Gerd Knorr <kraxel@bytesex.org>
7  *
8  *
9  *  This driver is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This driver is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/moduleparam.h>
30 #include <asm/io.h>
31 #include <asm/bitops.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/control.h>
36 #include <sound/initval.h>
37
38 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
39 MODULE_DESCRIPTION("Brooktree Bt87x audio driver");
40 MODULE_LICENSE("GPL");
41 MODULE_SUPPORTED_DEVICE("{{Brooktree,Bt878},"
42                 "{Brooktree,Bt879}}");
43
44 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
45 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
46 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
47 static int digital_rate[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 }; /* digital input rate */
48 static int boot_devs;
49
50 module_param_array(index, int, boot_devs, 0444);
51 MODULE_PARM_DESC(index, "Index value for Bt87x soundcard");
52 module_param_array(id, charp, boot_devs, 0444);
53 MODULE_PARM_DESC(id, "ID string for Bt87x soundcard");
54 module_param_array(enable, bool, boot_devs, 0444);
55 MODULE_PARM_DESC(enable, "Enable Bt87x soundcard");
56 module_param_array(digital_rate, int, boot_devs, 0444);
57 MODULE_PARM_DESC(digital_rate, "Digital input rate for Bt87x soundcard");
58
59
60 #ifndef PCI_VENDOR_ID_BROOKTREE
61 #define PCI_VENDOR_ID_BROOKTREE 0x109e
62 #endif
63 #ifndef PCI_DEVICE_ID_BROOKTREE_878
64 #define PCI_DEVICE_ID_BROOKTREE_878 0x0878
65 #endif
66 #ifndef PCI_DEVICE_ID_BROOKTREE_879
67 #define PCI_DEVICE_ID_BROOKTREE_879 0x0879
68 #endif
69
70 /* register offsets */
71 #define REG_INT_STAT            0x100   /* interrupt status */
72 #define REG_INT_MASK            0x104   /* interrupt mask */
73 #define REG_GPIO_DMA_CTL        0x10c   /* audio control */
74 #define REG_PACKET_LEN          0x110   /* audio packet lengths */
75 #define REG_RISC_STRT_ADD       0x114   /* RISC program start address */
76 #define REG_RISC_COUNT          0x120   /* RISC program counter */
77
78 /* interrupt bits */
79 #define INT_OFLOW       (1 <<  3)       /* audio A/D overflow */
80 #define INT_RISCI       (1 << 11)       /* RISC instruction IRQ bit set */
81 #define INT_FBUS        (1 << 12)       /* FIFO overrun due to bus access latency */
82 #define INT_FTRGT       (1 << 13)       /* FIFO overrun due to target latency */
83 #define INT_FDSR        (1 << 14)       /* FIFO data stream resynchronization */
84 #define INT_PPERR       (1 << 15)       /* PCI parity error */
85 #define INT_RIPERR      (1 << 16)       /* RISC instruction parity error */
86 #define INT_PABORT      (1 << 17)       /* PCI master or target abort */
87 #define INT_OCERR       (1 << 18)       /* invalid opcode */
88 #define INT_SCERR       (1 << 19)       /* sync counter overflow */
89 #define INT_RISC_EN     (1 << 27)       /* DMA controller running */
90 #define INT_RISCS_SHIFT       28        /* RISC status bits */
91
92 /* audio control bits */
93 #define CTL_FIFO_ENABLE         (1 <<  0)       /* enable audio data FIFO */
94 #define CTL_RISC_ENABLE         (1 <<  1)       /* enable audio DMA controller */
95 #define CTL_PKTP_4              (0 <<  2)       /* packet mode FIFO trigger point - 4 DWORDs */
96 #define CTL_PKTP_8              (1 <<  2)       /* 8 DWORDs */
97 #define CTL_PKTP_16             (2 <<  2)       /* 16 DWORDs */
98 #define CTL_ACAP_EN             (1 <<  4)       /* enable audio capture */
99 #define CTL_DA_APP              (1 <<  5)       /* GPIO input */
100 #define CTL_DA_IOM_AFE          (0 <<  6)       /* audio A/D input */
101 #define CTL_DA_IOM_DA           (1 <<  6)       /* digital audio input */
102 #define CTL_DA_SDR_SHIFT               8        /* DDF first stage decimation rate */
103 #define CTL_DA_SDR_MASK         (0xf<< 8)
104 #define CTL_DA_LMT              (1 << 12)       /* limit audio data values */
105 #define CTL_DA_ES2              (1 << 13)       /* enable DDF stage 2 */
106 #define CTL_DA_SBR              (1 << 14)       /* samples rounded to 8 bits */
107 #define CTL_DA_DPM              (1 << 15)       /* data packet mode */
108 #define CTL_DA_LRD_SHIFT              16        /* ALRCK delay */
109 #define CTL_DA_MLB              (1 << 21)       /* MSB/LSB format */
110 #define CTL_DA_LRI              (1 << 22)       /* left/right indication */
111 #define CTL_DA_SCE              (1 << 23)       /* sample clock edge */
112 #define CTL_A_SEL_STV           (0 << 24)       /* TV tuner audio input */
113 #define CTL_A_SEL_SFM           (1 << 24)       /* FM audio input */
114 #define CTL_A_SEL_SML           (2 << 24)       /* mic/line audio input */
115 #define CTL_A_SEL_SMXC          (3 << 24)       /* MUX bypass */
116 #define CTL_A_SEL_SHIFT               24
117 #define CTL_A_SEL_MASK          (3 << 24)
118 #define CTL_A_PWRDN             (1 << 26)       /* analog audio power-down */
119 #define CTL_A_G2X               (1 << 27)       /* audio gain boost */
120 #define CTL_A_GAIN_SHIFT              28        /* audio input gain */
121 #define CTL_A_GAIN_MASK         (0xf<<28)
122
123 /* RISC instruction opcodes */
124 #define RISC_WRITE      (0x1 << 28)     /* write FIFO data to memory at address */
125 #define RISC_WRITEC     (0x5 << 28)     /* write FIFO data to memory at current address */
126 #define RISC_SKIP       (0x2 << 28)     /* skip FIFO data */
127 #define RISC_JUMP       (0x7 << 28)     /* jump to address */
128 #define RISC_SYNC       (0x8 << 28)     /* synchronize with FIFO */
129
130 /* RISC instruction bits */
131 #define RISC_BYTES_ENABLE       (0xf << 12)     /* byte enable bits */
132 #define RISC_RESYNC             (  1 << 15)     /* disable FDSR errors */
133 #define RISC_SET_STATUS_SHIFT           16      /* set status bits */
134 #define RISC_RESET_STATUS_SHIFT         20      /* clear status bits */
135 #define RISC_IRQ                (  1 << 24)     /* interrupt */
136 #define RISC_EOL                (  1 << 26)     /* end of line */
137 #define RISC_SOL                (  1 << 27)     /* start of line */
138
139 /* SYNC status bits values */
140 #define RISC_SYNC_FM1   0x6
141 #define RISC_SYNC_VRO   0xc
142
143 #define ERROR_INTERRUPTS (INT_FBUS | INT_FTRGT | INT_PPERR | \
144                           INT_RIPERR | INT_PABORT | INT_OCERR)
145 #define MY_INTERRUPTS (INT_RISCI | ERROR_INTERRUPTS)
146
147 /* SYNC, one WRITE per line, one extra WRITE per page boundary, SYNC, JUMP */
148 #define MAX_RISC_SIZE ((1 + 255 + (PAGE_ALIGN(255 * 4092) / PAGE_SIZE - 1) + 1 + 1) * 8)
149
150 typedef struct snd_bt87x bt87x_t;
151 struct snd_bt87x {
152         snd_card_t *card;
153         struct pci_dev *pci;
154
155         void *mmio;
156         int irq;
157
158         int dig_rate;
159
160         spinlock_t reg_lock;
161         long opened;
162         snd_pcm_substream_t *substream;
163
164         struct snd_dma_buffer dma_risc;
165         unsigned int line_bytes;
166         unsigned int lines;
167
168         u32 reg_control;
169         int current_line;
170 };
171
172 enum { DEVICE_DIGITAL, DEVICE_ANALOG };
173
174 static inline u32 snd_bt87x_readl(bt87x_t *chip, u32 reg)
175 {
176         return readl(chip->mmio + reg);
177 }
178
179 static inline void snd_bt87x_writel(bt87x_t *chip, u32 reg, u32 value)
180 {
181         writel(value, chip->mmio + reg);
182 }
183
184 static int snd_bt87x_create_risc(bt87x_t *chip, snd_pcm_substream_t *substream,
185                                  unsigned int periods, unsigned int period_bytes)
186 {
187         struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
188         unsigned int i, offset;
189         u32 *risc;
190
191         if (chip->dma_risc.area == NULL) {
192                 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
193                                         PAGE_ALIGN(MAX_RISC_SIZE), &chip->dma_risc) < 0)
194                         return -ENOMEM;
195         }
196         risc = (u32 *)chip->dma_risc.area;
197         offset = 0;
198         *risc++ = cpu_to_le32(RISC_SYNC | RISC_SYNC_FM1);
199         *risc++ = cpu_to_le32(0);
200         for (i = 0; i < periods; ++i) {
201                 u32 rest;
202
203                 rest = period_bytes;
204                 do {
205                         u32 cmd, len;
206
207                         len = PAGE_SIZE - (offset % PAGE_SIZE);
208                         if (len > rest)
209                                 len = rest;
210                         cmd = RISC_WRITE | len;
211                         if (rest == period_bytes) {
212                                 u32 block = i * 16 / periods;
213                                 cmd |= RISC_SOL;
214                                 cmd |= block << RISC_SET_STATUS_SHIFT;
215                                 cmd |= (~block & 0xf) << RISC_RESET_STATUS_SHIFT;
216                         }
217                         if (len == rest)
218                                 cmd |= RISC_EOL | RISC_IRQ;
219                         *risc++ = cpu_to_le32(cmd);
220                         *risc++ = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, offset));
221                         offset += len;
222                         rest -= len;
223                 } while (rest > 0);
224         }
225         *risc++ = cpu_to_le32(RISC_SYNC | RISC_SYNC_VRO);
226         *risc++ = cpu_to_le32(0);
227         *risc++ = cpu_to_le32(RISC_JUMP);
228         *risc++ = cpu_to_le32(chip->dma_risc.addr);
229         chip->line_bytes = period_bytes;
230         chip->lines = periods;
231         return 0;
232 }
233
234 static void snd_bt87x_free_risc(bt87x_t *chip)
235 {
236         if (chip->dma_risc.area) {
237                 snd_dma_free_pages(&chip->dma_risc);
238                 chip->dma_risc.area = NULL;
239         }
240 }
241
242 static irqreturn_t snd_bt87x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
243 {
244         bt87x_t *chip = dev_id;
245         unsigned int status;
246
247         status = snd_bt87x_readl(chip, REG_INT_STAT);
248         if (!(status & MY_INTERRUPTS))
249                 return IRQ_NONE;
250         snd_bt87x_writel(chip, REG_INT_STAT, status & MY_INTERRUPTS);
251
252         if (status & ERROR_INTERRUPTS) {
253                 if (status & (INT_FBUS | INT_FTRGT))
254                         snd_printk(KERN_WARNING "FIFO overrun, status %#08x\n", status);
255                 if (status & INT_OCERR)
256                         snd_printk(KERN_ERR "internal RISC error, status %#08x\n", status);
257                 if (status & (INT_PPERR | INT_RIPERR | INT_PABORT)) {
258                         u16 pci_status;
259                         pci_read_config_word(chip->pci, PCI_STATUS, &pci_status);
260                         pci_write_config_word(chip->pci, PCI_STATUS, pci_status &
261                                               (PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
262                                                PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
263                                                PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY));
264                         snd_printk(KERN_ERR "Aieee - PCI error! status %#08x, PCI status %#04x\n",
265                                    status, pci_status);
266                 }
267         }
268         if (status & INT_RISCI) {
269                 int current_block, irq_block;
270
271                 /* assume that exactly one line has been recorded */
272                 chip->current_line = (chip->current_line + 1) % chip->lines;
273                 /* but check if some interrupts have been skipped */
274                 current_block = chip->current_line * 16 / chip->lines;
275                 irq_block = status >> INT_RISCS_SHIFT;
276                 if (current_block != irq_block)
277                         chip->current_line = (irq_block * chip->lines + 15) / 16;
278
279                 snd_pcm_period_elapsed(chip->substream);
280         }
281         return IRQ_HANDLED;
282 }
283
284 static snd_pcm_hardware_t snd_bt87x_digital_hw = {
285         .info = SNDRV_PCM_INFO_MMAP |
286                 SNDRV_PCM_INFO_INTERLEAVED |
287                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
288                 SNDRV_PCM_INFO_MMAP_VALID,
289         .formats = SNDRV_PCM_FMTBIT_S16_LE,
290         .rates = 0, /* set at runtime */
291         .channels_min = 2,
292         .channels_max = 2,
293         .buffer_bytes_max = 255 * 4092,
294         .period_bytes_min = 32,
295         .period_bytes_max = 4092,
296         .periods_min = 2,
297         .periods_max = 255,
298 };
299
300 static snd_pcm_hardware_t snd_bt87x_analog_hw = {
301         .info = SNDRV_PCM_INFO_MMAP |
302                 SNDRV_PCM_INFO_INTERLEAVED |
303                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
304                 SNDRV_PCM_INFO_MMAP_VALID,
305         .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
306         .rates = SNDRV_PCM_RATE_KNOT,
307         .rate_min = 119466,
308         .rate_max = 448000,
309         .channels_min = 1,
310         .channels_max = 1,
311         .buffer_bytes_max = 255 * 4092,
312         .period_bytes_min = 32,
313         .period_bytes_max = 4092,
314         .periods_min = 2,
315         .periods_max = 255,
316 };
317
318 static int snd_bt87x_set_digital_hw(bt87x_t *chip, snd_pcm_runtime_t *runtime)
319 {
320         static struct {
321                 int rate;
322                 unsigned int bit;
323         } ratebits[] = {
324                 {8000, SNDRV_PCM_RATE_8000},
325                 {11025, SNDRV_PCM_RATE_11025},
326                 {16000, SNDRV_PCM_RATE_16000},
327                 {22050, SNDRV_PCM_RATE_22050},
328                 {32000, SNDRV_PCM_RATE_32000},
329                 {44100, SNDRV_PCM_RATE_44100},
330                 {48000, SNDRV_PCM_RATE_48000}
331         };
332         int i;
333
334         chip->reg_control |= CTL_DA_IOM_DA;
335         runtime->hw = snd_bt87x_digital_hw;
336         runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
337         for (i = 0; i < ARRAY_SIZE(ratebits); ++i)
338                 if (chip->dig_rate == ratebits[i].rate) {
339                         runtime->hw.rates = ratebits[i].bit;
340                         break;
341                 }
342         runtime->hw.rate_min = chip->dig_rate;
343         runtime->hw.rate_max = chip->dig_rate;
344         return 0;
345 }
346
347 static int snd_bt87x_set_analog_hw(bt87x_t *chip, snd_pcm_runtime_t *runtime)
348 {
349         static ratnum_t analog_clock = {
350                 .num = 1792000,
351                 .den_min = 4,
352                 .den_max = 15,
353                 .den_step = 1
354         };
355         static snd_pcm_hw_constraint_ratnums_t constraint_rates = {
356                 .nrats = 1,
357                 .rats = &analog_clock
358         };
359
360         chip->reg_control &= ~CTL_DA_IOM_DA;
361         runtime->hw = snd_bt87x_analog_hw;
362         return snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
363                                              &constraint_rates);
364 }
365
366 static int snd_bt87x_pcm_open(snd_pcm_substream_t *substream)
367 {
368         bt87x_t *chip = snd_pcm_substream_chip(substream);
369         snd_pcm_runtime_t *runtime = substream->runtime;
370         int err;
371
372         if (test_and_set_bit(0, &chip->opened))
373                 return -EBUSY;
374
375         if (substream->pcm->device == DEVICE_DIGITAL)
376                 err = snd_bt87x_set_digital_hw(chip, runtime);
377         else
378                 err = snd_bt87x_set_analog_hw(chip, runtime);
379         if (err < 0)
380                 goto _error;
381
382         err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
383         if (err < 0)
384                 goto _error;
385
386         chip->substream = substream;
387         return 0;
388
389 _error:
390         clear_bit(0, &chip->opened);
391         smp_mb__after_clear_bit();
392         return err;
393 }
394
395 static int snd_bt87x_close(snd_pcm_substream_t *substream)
396 {
397         bt87x_t *chip = snd_pcm_substream_chip(substream);
398
399         chip->substream = NULL;
400         clear_bit(0, &chip->opened);
401         smp_mb__after_clear_bit();
402         return 0;
403 }
404
405 static int snd_bt87x_hw_params(snd_pcm_substream_t *substream,
406                                snd_pcm_hw_params_t *hw_params)
407 {
408         bt87x_t *chip = snd_pcm_substream_chip(substream);
409         int err;
410
411         err = snd_pcm_lib_malloc_pages(substream,
412                                        params_buffer_bytes(hw_params));
413         if (err < 0)
414                 return err;
415         return snd_bt87x_create_risc(chip, substream,
416                                      params_periods(hw_params),
417                                      params_period_bytes(hw_params));
418 }
419
420 static int snd_bt87x_hw_free(snd_pcm_substream_t *substream)
421 {
422         bt87x_t *chip = snd_pcm_substream_chip(substream);
423
424         snd_bt87x_free_risc(chip);
425         snd_pcm_lib_free_pages(substream);
426         return 0;
427 }
428
429 static int snd_bt87x_prepare(snd_pcm_substream_t *substream)
430 {
431         bt87x_t *chip = snd_pcm_substream_chip(substream);
432         snd_pcm_runtime_t *runtime = substream->runtime;
433         int decimation;
434
435         spin_lock_irq(&chip->reg_lock);
436         chip->reg_control &= ~(CTL_DA_SDR_MASK | CTL_DA_SBR);
437         decimation = (1792000 + runtime->rate / 4) / runtime->rate;
438         chip->reg_control |= decimation << CTL_DA_SDR_SHIFT;
439         if (runtime->format == SNDRV_PCM_FORMAT_S8)
440                 chip->reg_control |= CTL_DA_SBR;
441         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
442         spin_unlock_irq(&chip->reg_lock);
443         return 0;
444 }
445
446 static int snd_bt87x_start(bt87x_t *chip)
447 {
448         spin_lock(&chip->reg_lock);
449         chip->current_line = 0;
450         chip->reg_control |= CTL_FIFO_ENABLE | CTL_RISC_ENABLE | CTL_ACAP_EN;
451         snd_bt87x_writel(chip, REG_RISC_STRT_ADD, chip->dma_risc.addr);
452         snd_bt87x_writel(chip, REG_PACKET_LEN,
453                          chip->line_bytes | (chip->lines << 16));
454         snd_bt87x_writel(chip, REG_INT_MASK, MY_INTERRUPTS);
455         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
456         spin_unlock(&chip->reg_lock);
457         return 0;
458 }
459
460 static int snd_bt87x_stop(bt87x_t *chip)
461 {
462         spin_lock(&chip->reg_lock);
463         chip->reg_control &= ~(CTL_FIFO_ENABLE | CTL_RISC_ENABLE | CTL_ACAP_EN);
464         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
465         snd_bt87x_writel(chip, REG_INT_MASK, 0);
466         snd_bt87x_writel(chip, REG_INT_STAT, MY_INTERRUPTS);
467         spin_unlock(&chip->reg_lock);
468         return 0;
469 }
470
471 static int snd_bt87x_trigger(snd_pcm_substream_t *substream, int cmd)
472 {
473         bt87x_t *chip = snd_pcm_substream_chip(substream);
474
475         switch (cmd) {
476         case SNDRV_PCM_TRIGGER_START:
477                 return snd_bt87x_start(chip);
478         case SNDRV_PCM_TRIGGER_STOP:
479                 return snd_bt87x_stop(chip);
480         default:
481                 return -EINVAL;
482         }
483 }
484
485 static snd_pcm_uframes_t snd_bt87x_pointer(snd_pcm_substream_t *substream)
486 {
487         bt87x_t *chip = snd_pcm_substream_chip(substream);
488         snd_pcm_runtime_t *runtime = substream->runtime;
489
490         return (snd_pcm_uframes_t)bytes_to_frames(runtime, chip->current_line * chip->line_bytes);
491 }
492
493 static snd_pcm_ops_t snd_bt87x_pcm_ops = {
494         .open = snd_bt87x_pcm_open,
495         .close = snd_bt87x_close,
496         .ioctl = snd_pcm_lib_ioctl,
497         .hw_params = snd_bt87x_hw_params,
498         .hw_free = snd_bt87x_hw_free,
499         .prepare = snd_bt87x_prepare,
500         .trigger = snd_bt87x_trigger,
501         .pointer = snd_bt87x_pointer,
502         .page = snd_pcm_sgbuf_ops_page,
503 };
504
505 static int snd_bt87x_capture_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *info)
506 {
507         info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
508         info->count = 1;
509         info->value.integer.min = 0;
510         info->value.integer.max = 15;
511         return 0;
512 }
513
514 static int snd_bt87x_capture_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
515 {
516         bt87x_t *chip = snd_kcontrol_chip(kcontrol);
517
518         value->value.integer.value[0] = (chip->reg_control & CTL_A_GAIN_MASK) >> CTL_A_GAIN_SHIFT;
519         return 0;
520 }
521
522 static int snd_bt87x_capture_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
523 {
524         bt87x_t *chip = snd_kcontrol_chip(kcontrol);
525         u32 old_control;
526         int changed;
527
528         spin_lock_irq(&chip->reg_lock);
529         old_control = chip->reg_control;
530         chip->reg_control = (chip->reg_control & ~CTL_A_GAIN_MASK)
531                 | (value->value.integer.value[0] << CTL_A_GAIN_SHIFT);
532         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
533         changed = old_control != chip->reg_control;
534         spin_unlock_irq(&chip->reg_lock);
535         return changed;
536 }
537
538 static snd_kcontrol_new_t snd_bt87x_capture_volume = {
539         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
540         .name = "Capture Volume",
541         .info = snd_bt87x_capture_volume_info,
542         .get = snd_bt87x_capture_volume_get,
543         .put = snd_bt87x_capture_volume_put,
544 };
545
546 static int snd_bt87x_capture_boost_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *info)
547 {
548         info->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
549         info->count = 1;
550         info->value.integer.min = 0;
551         info->value.integer.max = 1;
552         return 0;
553 }
554
555 static int snd_bt87x_capture_boost_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
556 {
557         bt87x_t *chip = snd_kcontrol_chip(kcontrol);
558
559         value->value.integer.value[0] = !! (chip->reg_control & CTL_A_G2X);
560         return 0;
561 }
562
563 static int snd_bt87x_capture_boost_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
564 {
565         bt87x_t *chip = snd_kcontrol_chip(kcontrol);
566         u32 old_control;
567         int changed;
568
569         spin_lock_irq(&chip->reg_lock);
570         old_control = chip->reg_control;
571         chip->reg_control = (chip->reg_control & ~CTL_A_G2X)
572                 | (value->value.integer.value[0] ? CTL_A_G2X : 0);
573         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
574         changed = chip->reg_control != old_control;
575         spin_unlock_irq(&chip->reg_lock);
576         return changed;
577 }
578
579 static snd_kcontrol_new_t snd_bt87x_capture_boost = {
580         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
581         .name = "Capture Boost",
582         .info = snd_bt87x_capture_boost_info,
583         .get = snd_bt87x_capture_boost_get,
584         .put = snd_bt87x_capture_boost_put,
585 };
586
587 static int snd_bt87x_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *info)
588 {
589         static char *texts[3] = {"TV Tuner", "FM", "Mic/Line"};
590
591         info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
592         info->count = 1;
593         info->value.enumerated.items = 3;
594         if (info->value.enumerated.item > 2)
595                 info->value.enumerated.item = 2;
596         strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
597         return 0;
598 }
599
600 static int snd_bt87x_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
601 {
602         bt87x_t *chip = snd_kcontrol_chip(kcontrol);
603
604         value->value.enumerated.item[0] = (chip->reg_control & CTL_A_SEL_MASK) >> CTL_A_SEL_SHIFT;
605         return 0;
606 }
607
608 static int snd_bt87x_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
609 {
610         bt87x_t *chip = snd_kcontrol_chip(kcontrol);
611         u32 old_control;
612         int changed;
613
614         spin_lock_irq(&chip->reg_lock);
615         old_control = chip->reg_control;
616         chip->reg_control = (chip->reg_control & ~CTL_A_SEL_MASK)
617                 | (value->value.enumerated.item[0] << CTL_A_SEL_SHIFT);
618         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
619         changed = chip->reg_control != old_control;
620         spin_unlock_irq(&chip->reg_lock);
621         return changed;
622 }
623
624 static snd_kcontrol_new_t snd_bt87x_capture_source = {
625         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
626         .name = "Capture Source",
627         .info = snd_bt87x_capture_source_info,
628         .get = snd_bt87x_capture_source_get,
629         .put = snd_bt87x_capture_source_put,
630 };
631
632 static int snd_bt87x_free(bt87x_t *chip)
633 {
634         if (chip->mmio) {
635                 snd_bt87x_stop(chip);
636                 if (chip->irq >= 0)
637                         synchronize_irq(chip->irq);
638
639                 iounmap(chip->mmio);
640         }
641         if (chip->irq >= 0)
642                 free_irq(chip->irq, chip);
643         pci_release_regions(chip->pci);
644         kfree(chip);
645         return 0;
646 }
647
648 static int snd_bt87x_dev_free(snd_device_t *device)
649 {
650         bt87x_t *chip = device->device_data;
651         return snd_bt87x_free(chip);
652 }
653
654 static int __devinit snd_bt87x_pcm(bt87x_t *chip, int device, char *name)
655 {
656         int err;
657         snd_pcm_t *pcm;
658
659         err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
660         if (err < 0)
661                 return err;
662         pcm->private_data = chip;
663         strcpy(pcm->name, name);
664         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_bt87x_pcm_ops);
665         return snd_pcm_lib_preallocate_pages_for_all(pcm,
666                                                      SNDRV_DMA_TYPE_DEV_SG,
667                                                      snd_dma_pci_data(chip->pci),
668                                                         128 * 1024,
669                                                         (255 * 4092 + 1023) & ~1023);
670 }
671
672 static int __devinit snd_bt87x_create(snd_card_t *card,
673                                       struct pci_dev *pci,
674                                       bt87x_t **rchip)
675 {
676         bt87x_t *chip;
677         int err;
678         static snd_device_ops_t ops = {
679                 .dev_free = snd_bt87x_dev_free
680         };
681
682         *rchip = NULL;
683
684         err = pci_enable_device(pci);
685         if (err < 0)
686                 return err;
687
688         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
689         if (!chip)
690                 return -ENOMEM;
691         chip->card = card;
692         chip->pci = pci;
693         chip->irq = -1;
694         spin_lock_init(&chip->reg_lock);
695
696         if ((err = pci_request_regions(pci, "Bt87x audio")) < 0) {
697                 kfree(chip);
698                 return err;
699         }
700         chip->mmio = ioremap_nocache(pci_resource_start(pci, 0),
701                                      pci_resource_len(pci, 0));
702         if (!chip->mmio) {
703                 snd_bt87x_free(chip);
704                 snd_printk(KERN_ERR "cannot remap io memory\n");
705                 return -ENOMEM;
706         }
707
708         chip->reg_control = CTL_DA_ES2 | CTL_PKTP_16 | (15 << CTL_DA_SDR_SHIFT);
709         snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
710         snd_bt87x_writel(chip, REG_INT_MASK, 0);
711         snd_bt87x_writel(chip, REG_INT_STAT, MY_INTERRUPTS);
712
713         if (request_irq(pci->irq, snd_bt87x_interrupt, SA_INTERRUPT | SA_SHIRQ,
714                         "Bt87x audio", chip)) {
715                 snd_bt87x_free(chip);
716                 snd_printk(KERN_ERR "cannot grab irq\n");
717                 return -EBUSY;
718         }
719         chip->irq = pci->irq;
720         pci_set_master(pci);
721         synchronize_irq(chip->irq);
722
723         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
724         if (err < 0) {
725                 snd_bt87x_free(chip);
726                 return err;
727         }
728         snd_card_set_dev(card, &pci->dev);
729         *rchip = chip;
730         return 0;
731 }
732
733 static int __devinit snd_bt87x_probe(struct pci_dev *pci,
734                                      const struct pci_device_id *pci_id)
735 {
736         static int dev;
737         snd_card_t *card;
738         bt87x_t *chip;
739         int err;
740
741         if (dev >= SNDRV_CARDS)
742                 return -ENODEV;
743         if (!enable[dev]) {
744                 ++dev;
745                 return -ENOENT;
746         }
747
748         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
749         if (!card)
750                 return -ENOMEM;
751
752         err = snd_bt87x_create(card, pci, &chip);
753         if (err < 0)
754                 goto _error;
755
756         if (digital_rate[dev] > 0)
757                 chip->dig_rate = digital_rate[dev];
758         else
759                 chip->dig_rate = (int)pci_id->driver_data;
760
761         err = snd_bt87x_pcm(chip, DEVICE_DIGITAL, "Bt87x Digital");
762         if (err < 0)
763                 goto _error;
764         err = snd_bt87x_pcm(chip, DEVICE_ANALOG, "Bt87x Analog");
765         if (err < 0)
766                 goto _error;
767
768         err = snd_ctl_add(card, snd_ctl_new1(&snd_bt87x_capture_volume, chip));
769         if (err < 0)
770                 goto _error;
771         err = snd_ctl_add(card, snd_ctl_new1(&snd_bt87x_capture_boost, chip));
772         if (err < 0)
773                 goto _error;
774         err = snd_ctl_add(card, snd_ctl_new1(&snd_bt87x_capture_source, chip));
775         if (err < 0)
776                 goto _error;
777
778         strcpy(card->driver, "Bt87x");
779         sprintf(card->shortname, "Brooktree Bt%x", pci->device);
780         sprintf(card->longname, "%s at %#lx, irq %i",
781                 card->shortname, pci_resource_start(pci, 0), chip->irq);
782         strcpy(card->mixername, "Bt87x");
783
784         err = snd_card_register(card);
785         if (err < 0)
786                 goto _error;
787
788         pci_set_drvdata(pci, card);
789         ++dev;
790         return 0;
791
792 _error:
793         snd_card_free(card);
794         return err;
795 }
796
797 static void __devexit snd_bt87x_remove(struct pci_dev *pci)
798 {
799         snd_card_free(pci_get_drvdata(pci));
800         pci_set_drvdata(pci, NULL);
801 }
802
803 #define BT_DEVICE(chip, subvend, subdev, rate) \
804         { .vendor = PCI_VENDOR_ID_BROOKTREE, \
805           .device = PCI_DEVICE_ID_BROOKTREE_##chip, \
806           .subvendor = subvend, .subdevice = subdev, \
807           .driver_data = rate }
808
809 /* driver_data is the default digital_rate value for that device */
810 static struct pci_device_id snd_bt87x_ids[] = {
811         BT_DEVICE(878, 0x0070, 0xff01, 44100), /* Osprey 200 */
812
813         /* default entries for 32kHz and generic Bt87x cards */
814         BT_DEVICE(878, PCI_ANY_ID, PCI_ANY_ID, 32000),
815         BT_DEVICE(879, PCI_ANY_ID, PCI_ANY_ID, 32000),
816         { }
817 };
818 MODULE_DEVICE_TABLE(pci, snd_bt87x_ids);
819
820 static struct pci_driver driver = {
821         .name = "Bt87x",
822         .id_table = snd_bt87x_ids,
823         .probe = snd_bt87x_probe,
824         .remove = __devexit_p(snd_bt87x_remove),
825 };
826
827 static int __init alsa_card_bt87x_init(void)
828 {
829         return pci_module_init(&driver);
830 }
831
832 static void __exit alsa_card_bt87x_exit(void)
833 {
834         pci_unregister_driver(&driver);
835 }
836
837 module_init(alsa_card_bt87x_init)
838 module_exit(alsa_card_bt87x_exit)