bt87x.c 30 KB

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