source: GPL/trunk/alsa-kernel/pci/bt87x.c@ 464

Last change on this file since 464 was 464, checked in by Paul Smedley, 16 years ago

Update ALSA kernel to 1.0.21 level

File size: 31.7 KB
Line 
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 <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/slab.h>
28#include <linux/moduleparam.h>
29#include <linux/bitops.h>
30#include <asm/io.h>
31#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/control.h>
35#include <sound/initval.h>
36
37MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
38MODULE_DESCRIPTION("Brooktree Bt87x audio driver");
39MODULE_LICENSE("GPL");
40MODULE_SUPPORTED_DEVICE("{{Brooktree,Bt878},"
41 "{Brooktree,Bt879}}");
42
43#ifndef TARGET_OS2
44static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* Exclude the first card */
45static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
46#else
47static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
49#endif
50static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
51static int digital_rate[SNDRV_CARDS]; /* digital input rate */
52static int load_all; /* allow to load the non-whitelisted cards */
53
54module_param_array(index, int, NULL, 0444);
55MODULE_PARM_DESC(index, "Index value for Bt87x soundcard");
56module_param_array(id, charp, NULL, 0444);
57MODULE_PARM_DESC(id, "ID string for Bt87x soundcard");
58module_param_array(enable, bool, NULL, 0444);
59MODULE_PARM_DESC(enable, "Enable Bt87x soundcard");
60module_param_array(digital_rate, int, NULL, 0444);
61MODULE_PARM_DESC(digital_rate, "Digital input rate for Bt87x soundcard");
62module_param(load_all, bool, 0444);
63MODULE_PARM_DESC(load_all, "Allow to load the non-whitelisted cards");
64
65
66/* register offsets */
67#define REG_INT_STAT 0x100 /* interrupt status */
68#define REG_INT_MASK 0x104 /* interrupt mask */
69#define REG_GPIO_DMA_CTL 0x10c /* audio control */
70#define REG_PACKET_LEN 0x110 /* audio packet lengths */
71#define REG_RISC_STRT_ADD 0x114 /* RISC program start address */
72#define REG_RISC_COUNT 0x120 /* RISC program counter */
73
74/* interrupt bits */
75#define INT_OFLOW (1 << 3) /* audio A/D overflow */
76#define INT_RISCI (1 << 11) /* RISC instruction IRQ bit set */
77#define INT_FBUS (1 << 12) /* FIFO overrun due to bus access latency */
78#define INT_FTRGT (1 << 13) /* FIFO overrun due to target latency */
79#define INT_FDSR (1 << 14) /* FIFO data stream resynchronization */
80#define INT_PPERR (1 << 15) /* PCI parity error */
81#define INT_RIPERR (1 << 16) /* RISC instruction parity error */
82#define INT_PABORT (1 << 17) /* PCI master or target abort */
83#define INT_OCERR (1 << 18) /* invalid opcode */
84#define INT_SCERR (1 << 19) /* sync counter overflow */
85#define INT_RISC_EN (1 << 27) /* DMA controller running */
86#define INT_RISCS_SHIFT 28 /* RISC status bits */
87
88/* audio control bits */
89#define CTL_FIFO_ENABLE (1 << 0) /* enable audio data FIFO */
90#define CTL_RISC_ENABLE (1 << 1) /* enable audio DMA controller */
91#define CTL_PKTP_4 (0 << 2) /* packet mode FIFO trigger point - 4 DWORDs */
92#define CTL_PKTP_8 (1 << 2) /* 8 DWORDs */
93#define CTL_PKTP_16 (2 << 2) /* 16 DWORDs */
94#define CTL_ACAP_EN (1 << 4) /* enable audio capture */
95#define CTL_DA_APP (1 << 5) /* GPIO input */
96#define CTL_DA_IOM_AFE (0 << 6) /* audio A/D input */
97#define CTL_DA_IOM_DA (1 << 6) /* digital audio input */
98#define CTL_DA_SDR_SHIFT 8 /* DDF first stage decimation rate */
99#define CTL_DA_SDR_MASK (0xf<< 8)
100#define CTL_DA_LMT (1 << 12) /* limit audio data values */
101#define CTL_DA_ES2 (1 << 13) /* enable DDF stage 2 */
102#define CTL_DA_SBR (1 << 14) /* samples rounded to 8 bits */
103#define CTL_DA_DPM (1 << 15) /* data packet mode */
104#define CTL_DA_LRD_SHIFT 16 /* ALRCK delay */
105#define CTL_DA_MLB (1 << 21) /* MSB/LSB format */
106#define CTL_DA_LRI (1 << 22) /* left/right indication */
107#define CTL_DA_SCE (1 << 23) /* sample clock edge */
108#define CTL_A_SEL_STV (0 << 24) /* TV tuner audio input */
109#define CTL_A_SEL_SFM (1 << 24) /* FM audio input */
110#define CTL_A_SEL_SML (2 << 24) /* mic/line audio input */
111#define CTL_A_SEL_SMXC (3 << 24) /* MUX bypass */
112#define CTL_A_SEL_SHIFT 24
113#define CTL_A_SEL_MASK (3 << 24)
114#define CTL_A_PWRDN (1 << 26) /* analog audio power-down */
115#define CTL_A_G2X (1 << 27) /* audio gain boost */
116#define CTL_A_GAIN_SHIFT 28 /* audio input gain */
117#define CTL_A_GAIN_MASK (0xf<<28)
118
119/* RISC instruction opcodes */
120#define RISC_WRITE (0x1 << 28) /* write FIFO data to memory at address */
121#define RISC_WRITEC (0x5 << 28) /* write FIFO data to memory at current address */
122#define RISC_SKIP (0x2 << 28) /* skip FIFO data */
123#define RISC_JUMP (0x7 << 28) /* jump to address */
124#define RISC_SYNC (0x8 << 28) /* synchronize with FIFO */
125
126/* RISC instruction bits */
127#define RISC_BYTES_ENABLE (0xf << 12) /* byte enable bits */
128#define RISC_RESYNC ( 1 << 15) /* disable FDSR errors */
129#define RISC_SET_STATUS_SHIFT 16 /* set status bits */
130#define RISC_RESET_STATUS_SHIFT 20 /* clear status bits */
131#define RISC_IRQ ( 1 << 24) /* interrupt */
132#define RISC_EOL ( 1 << 26) /* end of line */
133#define RISC_SOL ( 1 << 27) /* start of line */
134
135/* SYNC status bits values */
136#define RISC_SYNC_FM1 0x6
137#define RISC_SYNC_VRO 0xc
138
139#define ANALOG_CLOCK 1792000
140#ifdef CONFIG_SND_BT87X_OVERCLOCK
141#define CLOCK_DIV_MIN 1
142#else
143#define CLOCK_DIV_MIN 4
144#endif
145#define CLOCK_DIV_MAX 15
146
147#define ERROR_INTERRUPTS (INT_FBUS | INT_FTRGT | INT_PPERR | \
148 INT_RIPERR | INT_PABORT | INT_OCERR)
149#define MY_INTERRUPTS (INT_RISCI | ERROR_INTERRUPTS)
150
151/* SYNC, one WRITE per line, one extra WRITE per page boundary, SYNC, JUMP */
152#define MAX_RISC_SIZE ((1 + 255 + (PAGE_ALIGN(255 * 4092) / PAGE_SIZE - 1) + 1 + 1) * 8)
153
154/* Cards with configuration information */
155enum snd_bt87x_boardid {
156 SND_BT87X_BOARD_UNKNOWN,
157 SND_BT87X_BOARD_GENERIC, /* both an & dig interfaces, 32kHz */
158 SND_BT87X_BOARD_ANALOG, /* board with no external A/D */
159 SND_BT87X_BOARD_OSPREY2x0,
160 SND_BT87X_BOARD_OSPREY440,
161 SND_BT87X_BOARD_AVPHONE98,
162};
163
164/* Card configuration */
165struct snd_bt87x_board {
166 int dig_rate; /* Digital input sampling rate */
167 u32 digital_fmt; /* Register settings for digital input */
168 unsigned no_analog:1; /* No analog input */
169 unsigned no_digital:1; /* No digital input */
170};
171
172static __devinitdata struct snd_bt87x_board snd_bt87x_boards[] = {
173 [SND_BT87X_BOARD_UNKNOWN] = {
174 .dig_rate = 32000, /* just a guess */
175 },
176 [SND_BT87X_BOARD_GENERIC] = {
177 .dig_rate = 32000,
178 },
179 [SND_BT87X_BOARD_ANALOG] = {
180 .no_digital = 1,
181 },
182 [SND_BT87X_BOARD_OSPREY2x0] = {
183 .dig_rate = 44100,
184 .digital_fmt = CTL_DA_LRI | (1 << CTL_DA_LRD_SHIFT),
185 },
186 [SND_BT87X_BOARD_OSPREY440] = {
187 .dig_rate = 32000,
188 .digital_fmt = CTL_DA_LRI | (1 << CTL_DA_LRD_SHIFT),
189 .no_analog = 1,
190 },
191 [SND_BT87X_BOARD_AVPHONE98] = {
192 .dig_rate = 48000,
193 },
194};
195
196struct snd_bt87x {
197 struct snd_card *card;
198 struct pci_dev *pci;
199 struct snd_bt87x_board board;
200
201 void __iomem *mmio;
202 int irq;
203
204 spinlock_t reg_lock;
205 unsigned long opened;
206 struct snd_pcm_substream *substream;
207
208 struct snd_dma_buffer dma_risc;
209 unsigned int line_bytes;
210 unsigned int lines;
211
212 u32 reg_control;
213 u32 interrupt_mask;
214
215 int current_line;
216
217 int pci_parity_errors;
218};
219
220enum { DEVICE_DIGITAL, DEVICE_ANALOG };
221
222static inline u32 snd_bt87x_readl(struct snd_bt87x *chip, u32 reg)
223{
224 return readl(chip->mmio + reg);
225}
226
227static inline void snd_bt87x_writel(struct snd_bt87x *chip, u32 reg, u32 value)
228{
229 writel(value, chip->mmio + reg);
230}
231
232static int snd_bt87x_create_risc(struct snd_bt87x *chip, struct snd_pcm_substream *substream,
233 unsigned int periods, unsigned int period_bytes)
234{
235 unsigned int i, offset;
236 u32 *risc;
237
238 if (chip->dma_risc.area == NULL) {
239 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
240 PAGE_ALIGN(MAX_RISC_SIZE), &chip->dma_risc) < 0)
241 return -ENOMEM;
242 }
243 risc = (u32 *)chip->dma_risc.area;
244 offset = 0;
245 *risc++ = cpu_to_le32(RISC_SYNC | RISC_SYNC_FM1);
246 *risc++ = cpu_to_le32(0);
247 for (i = 0; i < periods; ++i) {
248 u32 rest;
249
250 rest = period_bytes;
251 do {
252 u32 cmd, len;
253 unsigned int addr;
254
255 len = PAGE_SIZE - (offset % PAGE_SIZE);
256 if (len > rest)
257 len = rest;
258 cmd = RISC_WRITE | len;
259 if (rest == period_bytes) {
260 u32 block = i * 16 / periods;
261 cmd |= RISC_SOL;
262 cmd |= block << RISC_SET_STATUS_SHIFT;
263 cmd |= (~block & 0xf) << RISC_RESET_STATUS_SHIFT;
264 }
265 if (len == rest)
266 cmd |= RISC_EOL | RISC_IRQ;
267 *risc++ = cpu_to_le32(cmd);
268 addr = snd_pcm_sgbuf_get_addr(substream, offset);
269 *risc++ = cpu_to_le32(addr);
270 offset += len;
271 rest -= len;
272 } while (rest > 0);
273 }
274 *risc++ = cpu_to_le32(RISC_SYNC | RISC_SYNC_VRO);
275 *risc++ = cpu_to_le32(0);
276 *risc++ = cpu_to_le32(RISC_JUMP);
277 *risc++ = cpu_to_le32(chip->dma_risc.addr);
278 chip->line_bytes = period_bytes;
279 chip->lines = periods;
280 return 0;
281}
282
283static void snd_bt87x_free_risc(struct snd_bt87x *chip)
284{
285 if (chip->dma_risc.area) {
286 snd_dma_free_pages(&chip->dma_risc);
287 chip->dma_risc.area = NULL;
288 }
289}
290
291static void snd_bt87x_pci_error(struct snd_bt87x *chip, unsigned int status)
292{
293 u16 pci_status;
294
295 pci_read_config_word(chip->pci, PCI_STATUS, &pci_status);
296 pci_status &= PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
297 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
298 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY;
299 pci_write_config_word(chip->pci, PCI_STATUS, pci_status);
300 if (pci_status != PCI_STATUS_DETECTED_PARITY)
301 snd_printk(KERN_ERR "Aieee - PCI error! status %#08x, PCI status %#04x\n",
302 status & ERROR_INTERRUPTS, pci_status);
303 else {
304 snd_printk(KERN_ERR "Aieee - PCI parity error detected!\n");
305 /* error 'handling' similar to aic7xxx_pci.c: */
306 chip->pci_parity_errors++;
307 if (chip->pci_parity_errors > 20) {
308 snd_printk(KERN_ERR "Too many PCI parity errors observed.\n");
309 snd_printk(KERN_ERR "Some device on this bus is generating bad parity.\n");
310 snd_printk(KERN_ERR "This is an error *observed by*, not *generated by*, this card.\n");
311 snd_printk(KERN_ERR "PCI parity error checking has been disabled.\n");
312 chip->interrupt_mask &= ~(INT_PPERR | INT_RIPERR);
313 snd_bt87x_writel(chip, REG_INT_MASK, chip->interrupt_mask);
314 }
315 }
316}
317
318static irqreturn_t snd_bt87x_interrupt(int irq, void *dev_id)
319{
320 struct snd_bt87x *chip = dev_id;
321 unsigned int status, irq_status;
322
323 status = snd_bt87x_readl(chip, REG_INT_STAT);
324 irq_status = status & chip->interrupt_mask;
325 if (!irq_status)
326 return IRQ_NONE;
327 snd_bt87x_writel(chip, REG_INT_STAT, irq_status);
328
329 if (irq_status & ERROR_INTERRUPTS) {
330 if (irq_status & (INT_FBUS | INT_FTRGT))
331 snd_printk(KERN_WARNING "FIFO overrun, status %#08x\n", status);
332 if (irq_status & INT_OCERR)
333 snd_printk(KERN_ERR "internal RISC error, status %#08x\n", status);
334 if (irq_status & (INT_PPERR | INT_RIPERR | INT_PABORT))
335 snd_bt87x_pci_error(chip, irq_status);
336 }
337 if ((irq_status & INT_RISCI) && (chip->reg_control & CTL_ACAP_EN)) {
338 int current_block, irq_block;
339
340 /* assume that exactly one line has been recorded */
341 chip->current_line = (chip->current_line + 1) % chip->lines;
342 /* but check if some interrupts have been skipped */
343 current_block = chip->current_line * 16 / chip->lines;
344 irq_block = status >> INT_RISCS_SHIFT;
345 if (current_block != irq_block)
346 chip->current_line = (irq_block * chip->lines + 15) / 16;
347
348 snd_pcm_period_elapsed(chip->substream);
349 }
350 return IRQ_HANDLED;
351}
352
353static struct snd_pcm_hardware snd_bt87x_digital_hw = {
354 .info = SNDRV_PCM_INFO_MMAP |
355 SNDRV_PCM_INFO_INTERLEAVED |
356 SNDRV_PCM_INFO_BLOCK_TRANSFER |
357 SNDRV_PCM_INFO_MMAP_VALID |
358 SNDRV_PCM_INFO_BATCH,
359 .formats = SNDRV_PCM_FMTBIT_S16_LE,
360 .rates = 0, /* set at runtime */
361 .channels_min = 2,
362 .channels_max = 2,
363 .buffer_bytes_max = 255 * 4092,
364 .period_bytes_min = 32,
365 .period_bytes_max = 4092,
366 .periods_min = 2,
367 .periods_max = 255,
368};
369
370static struct snd_pcm_hardware snd_bt87x_analog_hw = {
371 .info = SNDRV_PCM_INFO_MMAP |
372 SNDRV_PCM_INFO_INTERLEAVED |
373 SNDRV_PCM_INFO_BLOCK_TRANSFER |
374 SNDRV_PCM_INFO_MMAP_VALID |
375 SNDRV_PCM_INFO_BATCH,
376 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
377 .rates = SNDRV_PCM_RATE_KNOT,
378 .rate_min = ANALOG_CLOCK / CLOCK_DIV_MAX,
379 .rate_max = ANALOG_CLOCK / CLOCK_DIV_MIN,
380 .channels_min = 1,
381 .channels_max = 1,
382 .buffer_bytes_max = 255 * 4092,
383 .period_bytes_min = 32,
384 .period_bytes_max = 4092,
385 .periods_min = 2,
386 .periods_max = 255,
387};
388
389static int snd_bt87x_set_digital_hw(struct snd_bt87x *chip, struct snd_pcm_runtime *runtime)
390{
391 chip->reg_control |= CTL_DA_IOM_DA | CTL_A_PWRDN;
392 runtime->hw = snd_bt87x_digital_hw;
393 runtime->hw.rates = snd_pcm_rate_to_rate_bit(chip->board.dig_rate);
394 runtime->hw.rate_min = chip->board.dig_rate;
395 runtime->hw.rate_max = chip->board.dig_rate;
396 return 0;
397}
398
399static int snd_bt87x_set_analog_hw(struct snd_bt87x *chip, struct snd_pcm_runtime *runtime)
400{
401 static struct snd_ratnum analog_clock = {
402 .num = ANALOG_CLOCK,
403 .den_min = CLOCK_DIV_MIN,
404 .den_max = CLOCK_DIV_MAX,
405 .den_step = 1
406 };
407 static struct snd_pcm_hw_constraint_ratnums constraint_rates = {
408 .nrats = 1,
409 .rats = &analog_clock
410 };
411
412 chip->reg_control &= ~(CTL_DA_IOM_DA | CTL_A_PWRDN);
413 runtime->hw = snd_bt87x_analog_hw;
414 return snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
415 &constraint_rates);
416}
417
418static int snd_bt87x_pcm_open(struct snd_pcm_substream *substream)
419{
420 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 int err;
423
424 if (test_and_set_bit(0, &chip->opened))
425 return -EBUSY;
426
427 if (substream->pcm->device == DEVICE_DIGITAL)
428 err = snd_bt87x_set_digital_hw(chip, runtime);
429 else
430 err = snd_bt87x_set_analog_hw(chip, runtime);
431 if (err < 0)
432 goto _error;
433
434 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
435 if (err < 0)
436 goto _error;
437
438 chip->substream = substream;
439 return 0;
440
441_error:
442 clear_bit(0, &chip->opened);
443 smp_mb__after_clear_bit();
444 return err;
445}
446
447static int snd_bt87x_close(struct snd_pcm_substream *substream)
448{
449 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
450
451 spin_lock_irq(&chip->reg_lock);
452 chip->reg_control |= CTL_A_PWRDN;
453 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
454 spin_unlock_irq(&chip->reg_lock);
455
456 chip->substream = NULL;
457 clear_bit(0, &chip->opened);
458 smp_mb__after_clear_bit();
459 return 0;
460}
461
462static int snd_bt87x_hw_params(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params *hw_params)
464{
465 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
466 int err;
467
468 err = snd_pcm_lib_malloc_pages(substream,
469 params_buffer_bytes(hw_params));
470 if (err < 0)
471 return err;
472 return snd_bt87x_create_risc(chip, substream,
473 params_periods(hw_params),
474 params_period_bytes(hw_params));
475}
476
477static int snd_bt87x_hw_free(struct snd_pcm_substream *substream)
478{
479 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
480
481 snd_bt87x_free_risc(chip);
482 snd_pcm_lib_free_pages(substream);
483 return 0;
484}
485
486static int snd_bt87x_prepare(struct snd_pcm_substream *substream)
487{
488 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
489 struct snd_pcm_runtime *runtime = substream->runtime;
490 int decimation;
491
492 spin_lock_irq(&chip->reg_lock);
493 chip->reg_control &= ~(CTL_DA_SDR_MASK | CTL_DA_SBR);
494 decimation = (ANALOG_CLOCK + runtime->rate / 4) / runtime->rate;
495 chip->reg_control |= decimation << CTL_DA_SDR_SHIFT;
496 if (runtime->format == SNDRV_PCM_FORMAT_S8)
497 chip->reg_control |= CTL_DA_SBR;
498 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
499 spin_unlock_irq(&chip->reg_lock);
500 return 0;
501}
502
503static int snd_bt87x_start(struct snd_bt87x *chip)
504{
505 spin_lock(&chip->reg_lock);
506 chip->current_line = 0;
507 chip->reg_control |= CTL_FIFO_ENABLE | CTL_RISC_ENABLE | CTL_ACAP_EN;
508 snd_bt87x_writel(chip, REG_RISC_STRT_ADD, chip->dma_risc.addr);
509 snd_bt87x_writel(chip, REG_PACKET_LEN,
510 chip->line_bytes | (chip->lines << 16));
511 snd_bt87x_writel(chip, REG_INT_MASK, chip->interrupt_mask);
512 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
513 spin_unlock(&chip->reg_lock);
514 return 0;
515}
516
517static int snd_bt87x_stop(struct snd_bt87x *chip)
518{
519 spin_lock(&chip->reg_lock);
520 chip->reg_control &= ~(CTL_FIFO_ENABLE | CTL_RISC_ENABLE | CTL_ACAP_EN);
521 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
522 snd_bt87x_writel(chip, REG_INT_MASK, 0);
523 snd_bt87x_writel(chip, REG_INT_STAT, MY_INTERRUPTS);
524 spin_unlock(&chip->reg_lock);
525 return 0;
526}
527
528static int snd_bt87x_trigger(struct snd_pcm_substream *substream, int cmd)
529{
530 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
531
532 switch (cmd) {
533 case SNDRV_PCM_TRIGGER_START:
534 return snd_bt87x_start(chip);
535 case SNDRV_PCM_TRIGGER_STOP:
536 return snd_bt87x_stop(chip);
537 default:
538 return -EINVAL;
539 }
540}
541
542static snd_pcm_uframes_t snd_bt87x_pointer(struct snd_pcm_substream *substream)
543{
544 struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
545 struct snd_pcm_runtime *runtime = substream->runtime;
546
547 return (snd_pcm_uframes_t)bytes_to_frames(runtime, chip->current_line * chip->line_bytes);
548}
549
550static struct snd_pcm_ops snd_bt87x_pcm_ops = {
551 .open = snd_bt87x_pcm_open,
552 .close = snd_bt87x_close,
553 .ioctl = snd_pcm_lib_ioctl,
554 .hw_params = snd_bt87x_hw_params,
555 .hw_free = snd_bt87x_hw_free,
556 .prepare = snd_bt87x_prepare,
557 .trigger = snd_bt87x_trigger,
558 .pointer = snd_bt87x_pointer,
559 .page = snd_pcm_sgbuf_ops_page,
560};
561
562static int snd_bt87x_capture_volume_info(struct snd_kcontrol *kcontrol,
563 struct snd_ctl_elem_info *info)
564{
565 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
566 info->count = 1;
567 info->value.integer.min = 0;
568 info->value.integer.max = 15;
569 return 0;
570}
571
572static int snd_bt87x_capture_volume_get(struct snd_kcontrol *kcontrol,
573 struct snd_ctl_elem_value *value)
574{
575 struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
576
577 value->value.integer.value[0] = (chip->reg_control & CTL_A_GAIN_MASK) >> CTL_A_GAIN_SHIFT;
578 return 0;
579}
580
581static int snd_bt87x_capture_volume_put(struct snd_kcontrol *kcontrol,
582 struct snd_ctl_elem_value *value)
583{
584 struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
585 u32 old_control;
586 int changed;
587
588 spin_lock_irq(&chip->reg_lock);
589 old_control = chip->reg_control;
590 chip->reg_control = (chip->reg_control & ~CTL_A_GAIN_MASK)
591 | (value->value.integer.value[0] << CTL_A_GAIN_SHIFT);
592 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
593 changed = old_control != chip->reg_control;
594 spin_unlock_irq(&chip->reg_lock);
595 return changed;
596}
597
598static struct snd_kcontrol_new snd_bt87x_capture_volume = {
599 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
600 .name = "Capture Volume",
601 .info = snd_bt87x_capture_volume_info,
602 .get = snd_bt87x_capture_volume_get,
603 .put = snd_bt87x_capture_volume_put,
604};
605
606#define snd_bt87x_capture_boost_info snd_ctl_boolean_mono_info
607
608static int snd_bt87x_capture_boost_get(struct snd_kcontrol *kcontrol,
609 struct snd_ctl_elem_value *value)
610{
611 struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
612
613 value->value.integer.value[0] = !! (chip->reg_control & CTL_A_G2X);
614 return 0;
615}
616
617static int snd_bt87x_capture_boost_put(struct snd_kcontrol *kcontrol,
618 struct snd_ctl_elem_value *value)
619{
620 struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
621 u32 old_control;
622 int changed;
623
624 spin_lock_irq(&chip->reg_lock);
625 old_control = chip->reg_control;
626 chip->reg_control = (chip->reg_control & ~CTL_A_G2X)
627 | (value->value.integer.value[0] ? CTL_A_G2X : 0);
628 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
629 changed = chip->reg_control != old_control;
630 spin_unlock_irq(&chip->reg_lock);
631 return changed;
632}
633
634static struct snd_kcontrol_new snd_bt87x_capture_boost = {
635 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
636 .name = "Capture Boost",
637 .info = snd_bt87x_capture_boost_info,
638 .get = snd_bt87x_capture_boost_get,
639 .put = snd_bt87x_capture_boost_put,
640};
641
642static int snd_bt87x_capture_source_info(struct snd_kcontrol *kcontrol,
643 struct snd_ctl_elem_info *info)
644{
645 static char *texts[3] = {"TV Tuner", "FM", "Mic/Line"};
646
647 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
648 info->count = 1;
649 info->value.enumerated.items = 3;
650 if (info->value.enumerated.item > 2)
651 info->value.enumerated.item = 2;
652 strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
653 return 0;
654}
655
656static int snd_bt87x_capture_source_get(struct snd_kcontrol *kcontrol,
657 struct snd_ctl_elem_value *value)
658{
659 struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
660
661 value->value.enumerated.item[0] = (chip->reg_control & CTL_A_SEL_MASK) >> CTL_A_SEL_SHIFT;
662 return 0;
663}
664
665static int snd_bt87x_capture_source_put(struct snd_kcontrol *kcontrol,
666 struct snd_ctl_elem_value *value)
667{
668 struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
669 u32 old_control;
670 int changed;
671
672 spin_lock_irq(&chip->reg_lock);
673 old_control = chip->reg_control;
674 chip->reg_control = (chip->reg_control & ~CTL_A_SEL_MASK)
675 | (value->value.enumerated.item[0] << CTL_A_SEL_SHIFT);
676 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
677 changed = chip->reg_control != old_control;
678 spin_unlock_irq(&chip->reg_lock);
679 return changed;
680}
681
682static struct snd_kcontrol_new snd_bt87x_capture_source = {
683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
684 .name = "Capture Source",
685 .info = snd_bt87x_capture_source_info,
686 .get = snd_bt87x_capture_source_get,
687 .put = snd_bt87x_capture_source_put,
688};
689
690static int snd_bt87x_free(struct snd_bt87x *chip)
691{
692 if (chip->mmio)
693 snd_bt87x_stop(chip);
694 if (chip->irq >= 0)
695 free_irq(chip->irq, chip);
696 if (chip->mmio)
697 iounmap(chip->mmio);
698 pci_release_regions(chip->pci);
699 pci_disable_device(chip->pci);
700 kfree(chip);
701 return 0;
702}
703
704static int snd_bt87x_dev_free(struct snd_device *device)
705{
706 struct snd_bt87x *chip = device->device_data;
707 return snd_bt87x_free(chip);
708}
709
710static int __devinit snd_bt87x_pcm(struct snd_bt87x *chip, int device, char *name)
711{
712 int err;
713 struct snd_pcm *pcm;
714
715 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
716 if (err < 0)
717 return err;
718 pcm->private_data = chip;
719 strcpy(pcm->name, name);
720 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_bt87x_pcm_ops);
721 return snd_pcm_lib_preallocate_pages_for_all(pcm,
722 SNDRV_DMA_TYPE_DEV_SG,
723 snd_dma_pci_data(chip->pci),
724 128 * 1024,
725 ALIGN(255 * 4092, 1024));
726}
727
728static int __devinit snd_bt87x_create(struct snd_card *card,
729 struct pci_dev *pci,
730 struct snd_bt87x **rchip)
731{
732 struct snd_bt87x *chip;
733 int err;
734 static struct snd_device_ops ops = {
735 .dev_free = snd_bt87x_dev_free
736 };
737
738 *rchip = NULL;
739
740 err = pci_enable_device(pci);
741 if (err < 0)
742 return err;
743
744 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
745 if (!chip) {
746 pci_disable_device(pci);
747 return -ENOMEM;
748 }
749 chip->card = card;
750 chip->pci = pci;
751 chip->irq = -1;
752 spin_lock_init(&chip->reg_lock);
753
754 if ((err = pci_request_regions(pci, "Bt87x audio")) < 0) {
755 kfree(chip);
756 pci_disable_device(pci);
757 return err;
758 }
759 chip->mmio = pci_ioremap_bar(pci, 0);
760 if (!chip->mmio) {
761 snd_printk(KERN_ERR "cannot remap io memory\n");
762 err = -ENOMEM;
763 goto fail;
764 }
765
766 chip->reg_control = CTL_A_PWRDN | CTL_DA_ES2 |
767 CTL_PKTP_16 | (15 << CTL_DA_SDR_SHIFT);
768 chip->interrupt_mask = MY_INTERRUPTS;
769 snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
770 snd_bt87x_writel(chip, REG_INT_MASK, 0);
771 snd_bt87x_writel(chip, REG_INT_STAT, MY_INTERRUPTS);
772
773 err = request_irq(pci->irq, snd_bt87x_interrupt, IRQF_SHARED,
774 "Bt87x audio", chip);
775 if (err < 0) {
776 snd_printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
777 goto fail;
778 }
779 chip->irq = pci->irq;
780 pci_set_master(pci);
781 synchronize_irq(chip->irq);
782
783 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
784 if (err < 0)
785 goto fail;
786
787 snd_card_set_dev(card, &pci->dev);
788 *rchip = chip;
789 return 0;
790
791fail:
792 snd_bt87x_free(chip);
793 return err;
794}
795
796#define BT_DEVICE(chip, subvend, subdev, id) \
797 { .vendor = PCI_VENDOR_ID_BROOKTREE, \
798 .device = chip, \
799 .subvendor = subvend, .subdevice = subdev, \
800 .driver_data = SND_BT87X_BOARD_ ## id }
801/* driver_data is the card id for that device */
802
803static struct pci_device_id snd_bt87x_ids[] = {
804 /* Hauppauge WinTV series */
805 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x0070, 0x13eb, GENERIC),
806 /* Hauppauge WinTV series */
807 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_879, 0x0070, 0x13eb, GENERIC),
808 /* Viewcast Osprey 200 */
809 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x0070, 0xff01, OSPREY2x0),
810 /* Viewcast Osprey 440 (rate is configurable via gpio) */
811 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x0070, 0xff07, OSPREY440),
812 /* ATI TV-Wonder */
813 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x1002, 0x0001, GENERIC),
814 /* Leadtek Winfast tv 2000xp delux */
815 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x107d, 0x6606, GENERIC),
816 /* Voodoo TV 200 */
817 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x121a, 0x3000, GENERIC),
818 /* Askey Computer Corp. MagicTView'99 */
819 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x144f, 0x3000, GENERIC),
820 /* AVerMedia Studio No. 103, 203, ...? */
821 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x1461, 0x0003, AVPHONE98),
822 /* Prolink PixelView PV-M4900 */
823 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x1554, 0x4011, GENERIC),
824 /* Pinnacle Studio PCTV rave */
825 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0xbd11, 0x1200, GENERIC),
826 {0}
827};
828MODULE_DEVICE_TABLE(pci, snd_bt87x_ids);
829
830/* cards known not to have audio
831 * (DVB cards use the audio function to transfer MPEG data) */
832static struct {
833 unsigned short subvendor, subdevice;
834} blacklist[] __devinitdata = {
835 {0x0071, 0x0101}, /* Nebula Electronics DigiTV */
836 {0x11bd, 0x001c}, /* Pinnacle PCTV Sat */
837 {0x11bd, 0x0026}, /* Pinnacle PCTV SAT CI */
838 {0x1461, 0x0761}, /* AVermedia AverTV DVB-T */
839 {0x1461, 0x0771}, /* AVermedia DVB-T 771 */
840 {0x1822, 0x0001}, /* Twinhan VisionPlus DVB-T */
841 {0x18ac, 0xd500}, /* DVICO FusionHDTV 5 Lite */
842 {0x18ac, 0xdb10}, /* DVICO FusionHDTV DVB-T Lite */
843 {0x18ac, 0xdb11}, /* Ultraview DVB-T Lite */
844 {0x270f, 0xfc00}, /* Chaintech Digitop DST-1000 DVB-S */
845 {0x7063, 0x2000}, /* pcHDTV HD-2000 TV */
846};
847
848static struct pci_driver driver;
849
850/* return the id of the card, or a negative value if it's blacklisted */
851static int __devinit snd_bt87x_detect_card(struct pci_dev *pci)
852{
853 int i;
854 const struct pci_device_id *supported;
855#ifdef TARGET_OS2
856 u16 subsystem_vendor, subsystem_device;
857#endif
858
859#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 13)
860 supported = pci_match_id(snd_bt87x_ids, pci);
861#else
862 supported = pci_match_device(snd_bt87x_ids, pci);
863#endif
864 if (supported && supported->driver_data > 0)
865 return supported->driver_data;
866
867#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 0)
868 subsystem_vendor = pci->subsystem_vendor;
869 subsystem_device = pci->subsystem_device;
870#else
871 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
872 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
873#endif
874 for (i = 0; i < ARRAY_SIZE(blacklist); ++i)
875#ifndef TARGET_OS2
876 if (blacklist[i].subvendor == pci->subsystem_vendor &&
877 blacklist[i].subdevice == pci->subsystem_device) {
878#else
879 if (blacklist[i].subvendor == subsystem_vendor &&
880 blacklist[i].subdevice == subsystem_device) {
881#endif
882 snd_printdd(KERN_INFO "card %#04x-%#04x:%#04x has no audio\n",
883#ifndef TARGET_OS2
884 pci->device, pci->subsystem_vendor, pci->subsystem_device);
885#else
886 pci->device, subsystem_vendor, subsystem_device);
887#endif
888 return -EBUSY;
889 }
890
891 snd_printk(KERN_INFO "unknown card %#04x-%#04x:%#04x\n",
892#ifndef TARGET_OS2
893 pci->device, pci->subsystem_vendor, pci->subsystem_device);
894#else
895 pci->device, subsystem_vendor, subsystem_device);
896#endif
897 snd_printk(KERN_DEBUG "please mail id, board name, and, "
898 "if it works, the correct digital_rate option to "
899 "<alsa-devel@alsa-project.org>\n");
900 return SND_BT87X_BOARD_UNKNOWN;
901}
902
903static int __devinit snd_bt87x_probe(struct pci_dev *pci,
904 const struct pci_device_id *pci_id)
905{
906 static int dev;
907 struct snd_card *card;
908 struct snd_bt87x *chip;
909 int err;
910 enum snd_bt87x_boardid boardid;
911
912 if (!pci_id->driver_data) {
913 err = snd_bt87x_detect_card(pci);
914 if (err < 0)
915 return -ENODEV;
916 boardid = err;
917 } else
918 boardid = pci_id->driver_data;
919
920 if (dev >= SNDRV_CARDS)
921 return -ENODEV;
922 if (!enable[dev]) {
923 ++dev;
924 return -ENOENT;
925 }
926
927 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
928 if (err < 0)
929 return err;
930
931 err = snd_bt87x_create(card, pci, &chip);
932 if (err < 0)
933 goto _error;
934
935 memcpy(&chip->board, &snd_bt87x_boards[boardid], sizeof(chip->board));
936
937 if (!chip->board.no_digital) {
938 if (digital_rate[dev] > 0)
939 chip->board.dig_rate = digital_rate[dev];
940
941 chip->reg_control |= chip->board.digital_fmt;
942
943 err = snd_bt87x_pcm(chip, DEVICE_DIGITAL, "Bt87x Digital");
944 if (err < 0)
945 goto _error;
946 }
947 if (!chip->board.no_analog) {
948 err = snd_bt87x_pcm(chip, DEVICE_ANALOG, "Bt87x Analog");
949 if (err < 0)
950 goto _error;
951 err = snd_ctl_add(card, snd_ctl_new1(
952 &snd_bt87x_capture_volume, chip));
953 if (err < 0)
954 goto _error;
955 err = snd_ctl_add(card, snd_ctl_new1(
956 &snd_bt87x_capture_boost, chip));
957 if (err < 0)
958 goto _error;
959 err = snd_ctl_add(card, snd_ctl_new1(
960 &snd_bt87x_capture_source, chip));
961 if (err < 0)
962 goto _error;
963 }
964 snd_printk(KERN_INFO "bt87x%d: Using board %d, %sanalog, %sdigital "
965 "(rate %d Hz)\n", dev, boardid,
966 chip->board.no_analog ? "no " : "",
967 chip->board.no_digital ? "no " : "", chip->board.dig_rate);
968
969 strcpy(card->driver, "Bt87x");
970 sprintf(card->shortname, "Brooktree Bt%x", pci->device);
971 sprintf(card->longname, "%s at %#llx, irq %i",
972 card->shortname, (unsigned long long)pci_resource_start(pci, 0),
973 chip->irq);
974 strcpy(card->mixername, "Bt87x");
975
976 err = snd_card_register(card);
977 if (err < 0)
978 goto _error;
979
980 pci_set_drvdata(pci, card);
981 ++dev;
982 return 0;
983
984_error:
985 snd_card_free(card);
986 return err;
987}
988
989static void __devexit snd_bt87x_remove(struct pci_dev *pci)
990{
991 snd_card_free(pci_get_drvdata(pci));
992 pci_set_drvdata(pci, NULL);
993}
994
995/* default entries for all Bt87x cards - it's not exported */
996/* driver_data is set to 0 to call detection */
997static struct pci_device_id snd_bt87x_default_ids[] __devinitdata = {
998 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, PCI_ANY_ID, PCI_ANY_ID, UNKNOWN),
999 BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_879, PCI_ANY_ID, PCI_ANY_ID, UNKNOWN),
1000 {0}
1001};
1002
1003static struct pci_driver driver = {
1004 .name = "Bt87x",
1005 .id_table = snd_bt87x_ids,
1006 .probe = snd_bt87x_probe,
1007 .remove = __devexit_p(snd_bt87x_remove),
1008};
1009
1010static int __init alsa_card_bt87x_init(void)
1011{
1012 if (load_all)
1013 driver.id_table = snd_bt87x_default_ids;
1014 return pci_register_driver(&driver);
1015}
1016
1017static void __exit alsa_card_bt87x_exit(void)
1018{
1019 pci_unregister_driver(&driver);
1020}
1021
1022module_init(alsa_card_bt87x_init)
1023module_exit(alsa_card_bt87x_exit)
Note: See TracBrowser for help on using the repository browser.