bt87x.c 28 KB

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