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

Last change on this file since 424 was 410, checked in by Paul Smedley, 17 years ago

Update ALSA core to 1.0.19 level

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