ad1889.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /* Analog Devices 1889 audio driver
  2. *
  3. * This is a driver for the AD1889 PCI audio chipset found
  4. * on the HP PA-RISC [BCJ]-xxx0 workstations.
  5. *
  6. * Copyright (C) 2004-2005, Kyle McMartin <kyle@parisc-linux.org>
  7. * Copyright (C) 2005, Thibaut Varene <varenet@parisc-linux.org>
  8. * Based on the OSS AD1889 driver by Randolph Chung <tausq@debian.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * TODO:
  24. * Do we need to take care of CCS register?
  25. * Maybe we could use finer grained locking (separate locks for pb/cap)?
  26. * Wishlist:
  27. * Control Interface (mixer) support
  28. * Better AC97 support (VSR...)?
  29. * PM support
  30. * MIDI support
  31. * Game Port support
  32. * SG DMA support (this will need *alot* of work)
  33. */
  34. #include <linux/init.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/slab.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/compiler.h>
  40. #include <linux/delay.h>
  41. #include <sound/driver.h>
  42. #include <sound/core.h>
  43. #include <sound/pcm.h>
  44. #include <sound/initval.h>
  45. #include <sound/ac97_codec.h>
  46. #include <asm/io.h>
  47. #include "ad1889.h"
  48. #include "ac97/ac97_id.h"
  49. #define AD1889_DRVVER "Version: 1.7"
  50. MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>, Thibaut Varene <t-bone@parisc-linux.org>");
  51. MODULE_DESCRIPTION("Analog Devices AD1889 ALSA sound driver");
  52. MODULE_LICENSE("GPL");
  53. MODULE_SUPPORTED_DEVICE("{{Analog Devices,AD1889}}");
  54. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  55. module_param_array(index, int, NULL, 0444);
  56. MODULE_PARM_DESC(index, "Index value for the AD1889 soundcard.");
  57. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  58. module_param_array(id, charp, NULL, 0444);
  59. MODULE_PARM_DESC(id, "ID string for the AD1889 soundcard.");
  60. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  61. module_param_array(enable, bool, NULL, 0444);
  62. MODULE_PARM_DESC(enable, "Enable AD1889 soundcard.");
  63. static char *ac97_quirk[SNDRV_CARDS];
  64. module_param_array(ac97_quirk, charp, NULL, 0444);
  65. MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  66. #define DEVNAME "ad1889"
  67. #define PFX DEVNAME ": "
  68. /* let's use the global sound debug interfaces */
  69. #define ad1889_debug(fmt, arg...) snd_printd(KERN_DEBUG fmt, ## arg)
  70. /* keep track of some hw registers */
  71. struct ad1889_register_state {
  72. u16 reg; /* reg setup */
  73. u32 addr; /* dma base address */
  74. unsigned long size; /* DMA buffer size */
  75. };
  76. struct snd_ad1889 {
  77. struct snd_card *card;
  78. struct pci_dev *pci;
  79. int irq;
  80. unsigned long bar;
  81. void __iomem *iobase;
  82. struct snd_ac97 *ac97;
  83. struct snd_ac97_bus *ac97_bus;
  84. struct snd_pcm *pcm;
  85. struct snd_info_entry *proc;
  86. struct snd_pcm_substream *psubs;
  87. struct snd_pcm_substream *csubs;
  88. /* playback register state */
  89. struct ad1889_register_state wave;
  90. struct ad1889_register_state ramc;
  91. spinlock_t lock;
  92. };
  93. static inline u16
  94. ad1889_readw(struct snd_ad1889 *chip, unsigned reg)
  95. {
  96. return readw(chip->iobase + reg);
  97. }
  98. static inline void
  99. ad1889_writew(struct snd_ad1889 *chip, unsigned reg, u16 val)
  100. {
  101. writew(val, chip->iobase + reg);
  102. }
  103. static inline u32
  104. ad1889_readl(struct snd_ad1889 *chip, unsigned reg)
  105. {
  106. return readl(chip->iobase + reg);
  107. }
  108. static inline void
  109. ad1889_writel(struct snd_ad1889 *chip, unsigned reg, u32 val)
  110. {
  111. writel(val, chip->iobase + reg);
  112. }
  113. static inline void
  114. ad1889_unmute(struct snd_ad1889 *chip)
  115. {
  116. u16 st;
  117. st = ad1889_readw(chip, AD_DS_WADA) &
  118. ~(AD_DS_WADA_RWAM | AD_DS_WADA_LWAM);
  119. ad1889_writew(chip, AD_DS_WADA, st);
  120. ad1889_readw(chip, AD_DS_WADA);
  121. }
  122. static inline void
  123. ad1889_mute(struct snd_ad1889 *chip)
  124. {
  125. u16 st;
  126. st = ad1889_readw(chip, AD_DS_WADA) | AD_DS_WADA_RWAM | AD_DS_WADA_LWAM;
  127. ad1889_writew(chip, AD_DS_WADA, st);
  128. ad1889_readw(chip, AD_DS_WADA);
  129. }
  130. static inline void
  131. ad1889_load_adc_buffer_address(struct snd_ad1889 *chip, u32 address)
  132. {
  133. ad1889_writel(chip, AD_DMA_ADCBA, address);
  134. ad1889_writel(chip, AD_DMA_ADCCA, address);
  135. }
  136. static inline void
  137. ad1889_load_adc_buffer_count(struct snd_ad1889 *chip, u32 count)
  138. {
  139. ad1889_writel(chip, AD_DMA_ADCBC, count);
  140. ad1889_writel(chip, AD_DMA_ADCCC, count);
  141. }
  142. static inline void
  143. ad1889_load_adc_interrupt_count(struct snd_ad1889 *chip, u32 count)
  144. {
  145. ad1889_writel(chip, AD_DMA_ADCIB, count);
  146. ad1889_writel(chip, AD_DMA_ADCIC, count);
  147. }
  148. static inline void
  149. ad1889_load_wave_buffer_address(struct snd_ad1889 *chip, u32 address)
  150. {
  151. ad1889_writel(chip, AD_DMA_WAVBA, address);
  152. ad1889_writel(chip, AD_DMA_WAVCA, address);
  153. }
  154. static inline void
  155. ad1889_load_wave_buffer_count(struct snd_ad1889 *chip, u32 count)
  156. {
  157. ad1889_writel(chip, AD_DMA_WAVBC, count);
  158. ad1889_writel(chip, AD_DMA_WAVCC, count);
  159. }
  160. static inline void
  161. ad1889_load_wave_interrupt_count(struct snd_ad1889 *chip, u32 count)
  162. {
  163. ad1889_writel(chip, AD_DMA_WAVIB, count);
  164. ad1889_writel(chip, AD_DMA_WAVIC, count);
  165. }
  166. static void
  167. ad1889_channel_reset(struct snd_ad1889 *chip, unsigned int channel)
  168. {
  169. u16 reg;
  170. if (channel & AD_CHAN_WAV) {
  171. /* Disable wave channel */
  172. reg = ad1889_readw(chip, AD_DS_WSMC) & ~AD_DS_WSMC_WAEN;
  173. ad1889_writew(chip, AD_DS_WSMC, reg);
  174. chip->wave.reg = reg;
  175. /* disable IRQs */
  176. reg = ad1889_readw(chip, AD_DMA_WAV);
  177. reg &= AD_DMA_IM_DIS;
  178. reg &= ~AD_DMA_LOOP;
  179. ad1889_writew(chip, AD_DMA_WAV, reg);
  180. /* clear IRQ and address counters and pointers */
  181. ad1889_load_wave_buffer_address(chip, 0x0);
  182. ad1889_load_wave_buffer_count(chip, 0x0);
  183. ad1889_load_wave_interrupt_count(chip, 0x0);
  184. /* flush */
  185. ad1889_readw(chip, AD_DMA_WAV);
  186. }
  187. if (channel & AD_CHAN_ADC) {
  188. /* Disable ADC channel */
  189. reg = ad1889_readw(chip, AD_DS_RAMC) & ~AD_DS_RAMC_ADEN;
  190. ad1889_writew(chip, AD_DS_RAMC, reg);
  191. chip->ramc.reg = reg;
  192. reg = ad1889_readw(chip, AD_DMA_ADC);
  193. reg &= AD_DMA_IM_DIS;
  194. reg &= ~AD_DMA_LOOP;
  195. ad1889_writew(chip, AD_DMA_ADC, reg);
  196. ad1889_load_adc_buffer_address(chip, 0x0);
  197. ad1889_load_adc_buffer_count(chip, 0x0);
  198. ad1889_load_adc_interrupt_count(chip, 0x0);
  199. /* flush */
  200. ad1889_readw(chip, AD_DMA_ADC);
  201. }
  202. }
  203. static u16
  204. snd_ad1889_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  205. {
  206. struct snd_ad1889 *chip = ac97->private_data;
  207. return ad1889_readw(chip, AD_AC97_BASE + reg);
  208. }
  209. static void
  210. snd_ad1889_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
  211. {
  212. struct snd_ad1889 *chip = ac97->private_data;
  213. ad1889_writew(chip, AD_AC97_BASE + reg, val);
  214. }
  215. static int
  216. snd_ad1889_ac97_ready(struct snd_ad1889 *chip)
  217. {
  218. int retry = 400; /* average needs 352 msec */
  219. while (!(ad1889_readw(chip, AD_AC97_ACIC) & AD_AC97_ACIC_ACRDY)
  220. && --retry)
  221. mdelay(1);
  222. if (!retry) {
  223. snd_printk(KERN_ERR PFX "[%s] Link is not ready.\n",
  224. __FUNCTION__);
  225. return -EIO;
  226. }
  227. ad1889_debug("[%s] ready after %d ms\n", __FUNCTION__, 400 - retry);
  228. return 0;
  229. }
  230. static int
  231. snd_ad1889_hw_params(struct snd_pcm_substream *substream,
  232. struct snd_pcm_hw_params *hw_params)
  233. {
  234. return snd_pcm_lib_malloc_pages(substream,
  235. params_buffer_bytes(hw_params));
  236. }
  237. static int
  238. snd_ad1889_hw_free(struct snd_pcm_substream *substream)
  239. {
  240. return snd_pcm_lib_free_pages(substream);
  241. }
  242. static struct snd_pcm_hardware snd_ad1889_playback_hw = {
  243. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  244. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BLOCK_TRANSFER,
  245. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  246. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  247. .rate_min = 8000, /* docs say 7000, but we're lazy */
  248. .rate_max = 48000,
  249. .channels_min = 1,
  250. .channels_max = 2,
  251. .buffer_bytes_max = BUFFER_BYTES_MAX,
  252. .period_bytes_min = PERIOD_BYTES_MIN,
  253. .period_bytes_max = PERIOD_BYTES_MAX,
  254. .periods_min = PERIODS_MIN,
  255. .periods_max = PERIODS_MAX,
  256. /*.fifo_size = 0,*/
  257. };
  258. static struct snd_pcm_hardware snd_ad1889_capture_hw = {
  259. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  260. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BLOCK_TRANSFER,
  261. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  262. .rates = SNDRV_PCM_RATE_48000,
  263. .rate_min = 48000, /* docs say we could to VSR, but we're lazy */
  264. .rate_max = 48000,
  265. .channels_min = 1,
  266. .channels_max = 2,
  267. .buffer_bytes_max = BUFFER_BYTES_MAX,
  268. .period_bytes_min = PERIOD_BYTES_MIN,
  269. .period_bytes_max = PERIOD_BYTES_MAX,
  270. .periods_min = PERIODS_MIN,
  271. .periods_max = PERIODS_MAX,
  272. /*.fifo_size = 0,*/
  273. };
  274. static int
  275. snd_ad1889_playback_open(struct snd_pcm_substream *ss)
  276. {
  277. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  278. struct snd_pcm_runtime *rt = ss->runtime;
  279. chip->psubs = ss;
  280. rt->hw = snd_ad1889_playback_hw;
  281. return 0;
  282. }
  283. static int
  284. snd_ad1889_capture_open(struct snd_pcm_substream *ss)
  285. {
  286. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  287. struct snd_pcm_runtime *rt = ss->runtime;
  288. chip->csubs = ss;
  289. rt->hw = snd_ad1889_capture_hw;
  290. return 0;
  291. }
  292. static int
  293. snd_ad1889_playback_close(struct snd_pcm_substream *ss)
  294. {
  295. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  296. chip->psubs = NULL;
  297. return 0;
  298. }
  299. static int
  300. snd_ad1889_capture_close(struct snd_pcm_substream *ss)
  301. {
  302. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  303. chip->csubs = NULL;
  304. return 0;
  305. }
  306. static int
  307. snd_ad1889_playback_prepare(struct snd_pcm_substream *ss)
  308. {
  309. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  310. struct snd_pcm_runtime *rt = ss->runtime;
  311. unsigned int size = snd_pcm_lib_buffer_bytes(ss);
  312. unsigned int count = snd_pcm_lib_period_bytes(ss);
  313. u16 reg;
  314. ad1889_channel_reset(chip, AD_CHAN_WAV);
  315. reg = ad1889_readw(chip, AD_DS_WSMC);
  316. /* Mask out 16-bit / Stereo */
  317. reg &= ~(AD_DS_WSMC_WA16 | AD_DS_WSMC_WAST);
  318. if (snd_pcm_format_width(rt->format) == 16)
  319. reg |= AD_DS_WSMC_WA16;
  320. if (rt->channels > 1)
  321. reg |= AD_DS_WSMC_WAST;
  322. /* let's make sure we don't clobber ourselves */
  323. spin_lock_irq(&chip->lock);
  324. chip->wave.size = size;
  325. chip->wave.reg = reg;
  326. chip->wave.addr = rt->dma_addr;
  327. ad1889_writew(chip, AD_DS_WSMC, chip->wave.reg);
  328. /* Set sample rates on the codec */
  329. ad1889_writew(chip, AD_DS_WAS, rt->rate);
  330. /* Set up DMA */
  331. ad1889_load_wave_buffer_address(chip, chip->wave.addr);
  332. ad1889_load_wave_buffer_count(chip, size);
  333. ad1889_load_wave_interrupt_count(chip, count);
  334. /* writes flush */
  335. ad1889_readw(chip, AD_DS_WSMC);
  336. spin_unlock_irq(&chip->lock);
  337. ad1889_debug("prepare playback: addr = 0x%x, count = %u, "
  338. "size = %u, reg = 0x%x, rate = %u\n", chip->wave.addr,
  339. count, size, reg, rt->rate);
  340. return 0;
  341. }
  342. static int
  343. snd_ad1889_capture_prepare(struct snd_pcm_substream *ss)
  344. {
  345. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  346. struct snd_pcm_runtime *rt = ss->runtime;
  347. unsigned int size = snd_pcm_lib_buffer_bytes(ss);
  348. unsigned int count = snd_pcm_lib_period_bytes(ss);
  349. u16 reg;
  350. ad1889_channel_reset(chip, AD_CHAN_ADC);
  351. reg = ad1889_readw(chip, AD_DS_RAMC);
  352. /* Mask out 16-bit / Stereo */
  353. reg &= ~(AD_DS_RAMC_AD16 | AD_DS_RAMC_ADST);
  354. if (snd_pcm_format_width(rt->format) == 16)
  355. reg |= AD_DS_RAMC_AD16;
  356. if (rt->channels > 1)
  357. reg |= AD_DS_RAMC_ADST;
  358. /* let's make sure we don't clobber ourselves */
  359. spin_lock_irq(&chip->lock);
  360. chip->ramc.size = size;
  361. chip->ramc.reg = reg;
  362. chip->ramc.addr = rt->dma_addr;
  363. ad1889_writew(chip, AD_DS_RAMC, chip->ramc.reg);
  364. /* Set up DMA */
  365. ad1889_load_adc_buffer_address(chip, chip->ramc.addr);
  366. ad1889_load_adc_buffer_count(chip, size);
  367. ad1889_load_adc_interrupt_count(chip, count);
  368. /* writes flush */
  369. ad1889_readw(chip, AD_DS_RAMC);
  370. spin_unlock_irq(&chip->lock);
  371. ad1889_debug("prepare capture: addr = 0x%x, count = %u, "
  372. "size = %u, reg = 0x%x, rate = %u\n", chip->ramc.addr,
  373. count, size, reg, rt->rate);
  374. return 0;
  375. }
  376. /* this is called in atomic context with IRQ disabled.
  377. Must be as fast as possible and not sleep.
  378. DMA should be *triggered* by this call.
  379. The WSMC "WAEN" bit triggers DMA Wave On/Off */
  380. static int
  381. snd_ad1889_playback_trigger(struct snd_pcm_substream *ss, int cmd)
  382. {
  383. u16 wsmc;
  384. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  385. wsmc = ad1889_readw(chip, AD_DS_WSMC);
  386. switch (cmd) {
  387. case SNDRV_PCM_TRIGGER_START:
  388. /* enable DMA loop & interrupts */
  389. ad1889_writew(chip, AD_DMA_WAV, AD_DMA_LOOP | AD_DMA_IM_CNT);
  390. wsmc |= AD_DS_WSMC_WAEN;
  391. /* 1 to clear CHSS bit */
  392. ad1889_writel(chip, AD_DMA_CHSS, AD_DMA_CHSS_WAVS);
  393. ad1889_unmute(chip);
  394. break;
  395. case SNDRV_PCM_TRIGGER_STOP:
  396. ad1889_mute(chip);
  397. wsmc &= ~AD_DS_WSMC_WAEN;
  398. break;
  399. default:
  400. snd_BUG();
  401. return -EINVAL;
  402. }
  403. chip->wave.reg = wsmc;
  404. ad1889_writew(chip, AD_DS_WSMC, wsmc);
  405. ad1889_readw(chip, AD_DS_WSMC); /* flush */
  406. /* reset the chip when STOP - will disable IRQs */
  407. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  408. ad1889_channel_reset(chip, AD_CHAN_WAV);
  409. return 0;
  410. }
  411. /* this is called in atomic context with IRQ disabled.
  412. Must be as fast as possible and not sleep.
  413. DMA should be *triggered* by this call.
  414. The RAMC "ADEN" bit triggers DMA ADC On/Off */
  415. static int
  416. snd_ad1889_capture_trigger(struct snd_pcm_substream *ss, int cmd)
  417. {
  418. u16 ramc;
  419. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  420. ramc = ad1889_readw(chip, AD_DS_RAMC);
  421. switch (cmd) {
  422. case SNDRV_PCM_TRIGGER_START:
  423. /* enable DMA loop & interrupts */
  424. ad1889_writew(chip, AD_DMA_ADC, AD_DMA_LOOP | AD_DMA_IM_CNT);
  425. ramc |= AD_DS_RAMC_ADEN;
  426. /* 1 to clear CHSS bit */
  427. ad1889_writel(chip, AD_DMA_CHSS, AD_DMA_CHSS_ADCS);
  428. break;
  429. case SNDRV_PCM_TRIGGER_STOP:
  430. ramc &= ~AD_DS_RAMC_ADEN;
  431. break;
  432. default:
  433. return -EINVAL;
  434. }
  435. chip->ramc.reg = ramc;
  436. ad1889_writew(chip, AD_DS_RAMC, ramc);
  437. ad1889_readw(chip, AD_DS_RAMC); /* flush */
  438. /* reset the chip when STOP - will disable IRQs */
  439. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  440. ad1889_channel_reset(chip, AD_CHAN_ADC);
  441. return 0;
  442. }
  443. /* Called in atomic context with IRQ disabled */
  444. static snd_pcm_uframes_t
  445. snd_ad1889_playback_pointer(struct snd_pcm_substream *ss)
  446. {
  447. size_t ptr = 0;
  448. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  449. if (unlikely(!(chip->wave.reg & AD_DS_WSMC_WAEN)))
  450. return 0;
  451. ptr = ad1889_readl(chip, AD_DMA_WAVCA);
  452. ptr -= chip->wave.addr;
  453. snd_assert((ptr >= 0) && (ptr < chip->wave.size), return 0);
  454. return bytes_to_frames(ss->runtime, ptr);
  455. }
  456. /* Called in atomic context with IRQ disabled */
  457. static snd_pcm_uframes_t
  458. snd_ad1889_capture_pointer(struct snd_pcm_substream *ss)
  459. {
  460. size_t ptr = 0;
  461. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  462. if (unlikely(!(chip->ramc.reg & AD_DS_RAMC_ADEN)))
  463. return 0;
  464. ptr = ad1889_readl(chip, AD_DMA_ADCCA);
  465. ptr -= chip->ramc.addr;
  466. snd_assert((ptr >= 0) && (ptr < chip->ramc.size), return 0);
  467. return bytes_to_frames(ss->runtime, ptr);
  468. }
  469. static struct snd_pcm_ops snd_ad1889_playback_ops = {
  470. .open = snd_ad1889_playback_open,
  471. .close = snd_ad1889_playback_close,
  472. .ioctl = snd_pcm_lib_ioctl,
  473. .hw_params = snd_ad1889_hw_params,
  474. .hw_free = snd_ad1889_hw_free,
  475. .prepare = snd_ad1889_playback_prepare,
  476. .trigger = snd_ad1889_playback_trigger,
  477. .pointer = snd_ad1889_playback_pointer,
  478. };
  479. static struct snd_pcm_ops snd_ad1889_capture_ops = {
  480. .open = snd_ad1889_capture_open,
  481. .close = snd_ad1889_capture_close,
  482. .ioctl = snd_pcm_lib_ioctl,
  483. .hw_params = snd_ad1889_hw_params,
  484. .hw_free = snd_ad1889_hw_free,
  485. .prepare = snd_ad1889_capture_prepare,
  486. .trigger = snd_ad1889_capture_trigger,
  487. .pointer = snd_ad1889_capture_pointer,
  488. };
  489. static irqreturn_t
  490. snd_ad1889_interrupt(int irq,
  491. void *dev_id,
  492. struct pt_regs *regs)
  493. {
  494. unsigned long st;
  495. struct snd_ad1889 *chip = dev_id;
  496. st = ad1889_readl(chip, AD_DMA_DISR);
  497. /* clear ISR */
  498. ad1889_writel(chip, AD_DMA_DISR, st);
  499. st &= AD_INTR_MASK;
  500. if (unlikely(!st))
  501. return IRQ_NONE;
  502. if (st & (AD_DMA_DISR_PMAI|AD_DMA_DISR_PTAI))
  503. ad1889_debug("Unexpected master or target abort interrupt!\n");
  504. if ((st & AD_DMA_DISR_WAVI) && chip->psubs)
  505. snd_pcm_period_elapsed(chip->psubs);
  506. if ((st & AD_DMA_DISR_ADCI) && chip->csubs)
  507. snd_pcm_period_elapsed(chip->csubs);
  508. return IRQ_HANDLED;
  509. }
  510. static int __devinit
  511. snd_ad1889_pcm_init(struct snd_ad1889 *chip, int device, struct snd_pcm **rpcm)
  512. {
  513. int err;
  514. struct snd_pcm *pcm;
  515. if (rpcm)
  516. *rpcm = NULL;
  517. err = snd_pcm_new(chip->card, chip->card->driver, device, 1, 1, &pcm);
  518. if (err < 0)
  519. return err;
  520. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  521. &snd_ad1889_playback_ops);
  522. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  523. &snd_ad1889_capture_ops);
  524. pcm->private_data = chip;
  525. pcm->info_flags = 0;
  526. strcpy(pcm->name, chip->card->shortname);
  527. chip->pcm = pcm;
  528. chip->psubs = NULL;
  529. chip->csubs = NULL;
  530. err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  531. snd_dma_pci_data(chip->pci),
  532. BUFFER_BYTES_MAX / 2,
  533. BUFFER_BYTES_MAX);
  534. if (err < 0) {
  535. snd_printk(KERN_ERR PFX "buffer allocation error: %d\n", err);
  536. return err;
  537. }
  538. if (rpcm)
  539. *rpcm = pcm;
  540. return 0;
  541. }
  542. static void
  543. snd_ad1889_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  544. {
  545. struct snd_ad1889 *chip = entry->private_data;
  546. u16 reg;
  547. int tmp;
  548. reg = ad1889_readw(chip, AD_DS_WSMC);
  549. snd_iprintf(buffer, "Wave output: %s\n",
  550. (reg & AD_DS_WSMC_WAEN) ? "enabled" : "disabled");
  551. snd_iprintf(buffer, "Wave Channels: %s\n",
  552. (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
  553. snd_iprintf(buffer, "Wave Quality: %d-bit linear\n",
  554. (reg & AD_DS_WSMC_WA16) ? 16 : 8);
  555. /* WARQ is at offset 12 */
  556. tmp = (reg & AD_DS_WSMC_WARQ) ?
  557. (((reg & AD_DS_WSMC_WARQ >> 12) & 0x01) ? 12 : 18) : 4;
  558. tmp /= (reg & AD_DS_WSMC_WAST) ? 2 : 1;
  559. snd_iprintf(buffer, "Wave FIFO: %d %s words\n\n", tmp,
  560. (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
  561. snd_iprintf(buffer, "Synthesis output: %s\n",
  562. reg & AD_DS_WSMC_SYEN ? "enabled" : "disabled");
  563. /* SYRQ is at offset 4 */
  564. tmp = (reg & AD_DS_WSMC_SYRQ) ?
  565. (((reg & AD_DS_WSMC_SYRQ >> 4) & 0x01) ? 12 : 18) : 4;
  566. tmp /= (reg & AD_DS_WSMC_WAST) ? 2 : 1;
  567. snd_iprintf(buffer, "Synthesis FIFO: %d %s words\n\n", tmp,
  568. (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
  569. reg = ad1889_readw(chip, AD_DS_RAMC);
  570. snd_iprintf(buffer, "ADC input: %s\n",
  571. (reg & AD_DS_RAMC_ADEN) ? "enabled" : "disabled");
  572. snd_iprintf(buffer, "ADC Channels: %s\n",
  573. (reg & AD_DS_RAMC_ADST) ? "stereo" : "mono");
  574. snd_iprintf(buffer, "ADC Quality: %d-bit linear\n",
  575. (reg & AD_DS_RAMC_AD16) ? 16 : 8);
  576. /* ACRQ is at offset 4 */
  577. tmp = (reg & AD_DS_RAMC_ACRQ) ?
  578. (((reg & AD_DS_RAMC_ACRQ >> 4) & 0x01) ? 12 : 18) : 4;
  579. tmp /= (reg & AD_DS_RAMC_ADST) ? 2 : 1;
  580. snd_iprintf(buffer, "ADC FIFO: %d %s words\n\n", tmp,
  581. (reg & AD_DS_RAMC_ADST) ? "stereo" : "mono");
  582. snd_iprintf(buffer, "Resampler input: %s\n",
  583. reg & AD_DS_RAMC_REEN ? "enabled" : "disabled");
  584. /* RERQ is at offset 12 */
  585. tmp = (reg & AD_DS_RAMC_RERQ) ?
  586. (((reg & AD_DS_RAMC_RERQ >> 12) & 0x01) ? 12 : 18) : 4;
  587. tmp /= (reg & AD_DS_RAMC_ADST) ? 2 : 1;
  588. snd_iprintf(buffer, "Resampler FIFO: %d %s words\n\n", tmp,
  589. (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
  590. /* doc says LSB represents -1.5dB, but the max value (-94.5dB)
  591. suggests that LSB is -3dB, which is more coherent with the logarithmic
  592. nature of the dB scale */
  593. reg = ad1889_readw(chip, AD_DS_WADA);
  594. snd_iprintf(buffer, "Left: %s, -%d dB\n",
  595. (reg & AD_DS_WADA_LWAM) ? "mute" : "unmute",
  596. ((reg & AD_DS_WADA_LWAA) >> 8) * 3);
  597. reg = ad1889_readw(chip, AD_DS_WADA);
  598. snd_iprintf(buffer, "Right: %s, -%d dB\n",
  599. (reg & AD_DS_WADA_RWAM) ? "mute" : "unmute",
  600. ((reg & AD_DS_WADA_RWAA) >> 8) * 3);
  601. reg = ad1889_readw(chip, AD_DS_WAS);
  602. snd_iprintf(buffer, "Wave samplerate: %u Hz\n", reg);
  603. reg = ad1889_readw(chip, AD_DS_RES);
  604. snd_iprintf(buffer, "Resampler samplerate: %u Hz\n", reg);
  605. }
  606. static void __devinit
  607. snd_ad1889_proc_init(struct snd_ad1889 *chip)
  608. {
  609. struct snd_info_entry *entry;
  610. if (!snd_card_proc_new(chip->card, chip->card->driver, &entry))
  611. snd_info_set_text_ops(entry, chip, snd_ad1889_proc_read);
  612. }
  613. static struct ac97_quirk ac97_quirks[] = {
  614. {
  615. .subvendor = 0x11d4, /* AD */
  616. .subdevice = 0x1889, /* AD1889 */
  617. .codec_id = AC97_ID_AD1819,
  618. .name = "AD1889",
  619. .type = AC97_TUNE_HP_ONLY
  620. },
  621. { } /* terminator */
  622. };
  623. static void __devinit
  624. snd_ad1889_ac97_xinit(struct snd_ad1889 *chip)
  625. {
  626. u16 reg;
  627. reg = ad1889_readw(chip, AD_AC97_ACIC);
  628. reg |= AD_AC97_ACIC_ACRD; /* Reset Disable */
  629. ad1889_writew(chip, AD_AC97_ACIC, reg);
  630. ad1889_readw(chip, AD_AC97_ACIC); /* flush posted write */
  631. udelay(10);
  632. /* Interface Enable */
  633. reg |= AD_AC97_ACIC_ACIE;
  634. ad1889_writew(chip, AD_AC97_ACIC, reg);
  635. snd_ad1889_ac97_ready(chip);
  636. /* Audio Stream Output | Variable Sample Rate Mode */
  637. reg = ad1889_readw(chip, AD_AC97_ACIC);
  638. reg |= AD_AC97_ACIC_ASOE | AD_AC97_ACIC_VSRM;
  639. ad1889_writew(chip, AD_AC97_ACIC, reg);
  640. ad1889_readw(chip, AD_AC97_ACIC); /* flush posted write */
  641. }
  642. static void
  643. snd_ad1889_ac97_bus_free(struct snd_ac97_bus *bus)
  644. {
  645. struct snd_ad1889 *chip = bus->private_data;
  646. chip->ac97_bus = NULL;
  647. }
  648. static void
  649. snd_ad1889_ac97_free(struct snd_ac97 *ac97)
  650. {
  651. struct snd_ad1889 *chip = ac97->private_data;
  652. chip->ac97 = NULL;
  653. }
  654. static int __devinit
  655. snd_ad1889_ac97_init(struct snd_ad1889 *chip, const char *quirk_override)
  656. {
  657. int err;
  658. struct snd_ac97_template ac97;
  659. static struct snd_ac97_bus_ops ops = {
  660. .write = snd_ad1889_ac97_write,
  661. .read = snd_ad1889_ac97_read,
  662. };
  663. /* doing that here, it works. */
  664. snd_ad1889_ac97_xinit(chip);
  665. err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
  666. if (err < 0)
  667. return err;
  668. chip->ac97_bus->private_free = snd_ad1889_ac97_bus_free;
  669. memset(&ac97, 0, sizeof(ac97));
  670. ac97.private_data = chip;
  671. ac97.private_free = snd_ad1889_ac97_free;
  672. ac97.pci = chip->pci;
  673. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
  674. if (err < 0)
  675. return err;
  676. snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
  677. return 0;
  678. }
  679. static int
  680. snd_ad1889_free(struct snd_ad1889 *chip)
  681. {
  682. if (chip->irq < 0)
  683. goto skip_hw;
  684. spin_lock_irq(&chip->lock);
  685. ad1889_mute(chip);
  686. /* Turn off interrupt on count and zero DMA registers */
  687. ad1889_channel_reset(chip, AD_CHAN_WAV | AD_CHAN_ADC);
  688. /* clear DISR. If we don't, we'd better jump off the Eiffel Tower */
  689. ad1889_writel(chip, AD_DMA_DISR, AD_DMA_DISR_PTAI | AD_DMA_DISR_PMAI);
  690. ad1889_readl(chip, AD_DMA_DISR); /* flush, dammit! */
  691. spin_unlock_irq(&chip->lock);
  692. synchronize_irq(chip->irq);
  693. if (chip->irq >= 0)
  694. free_irq(chip->irq, (void*)chip);
  695. skip_hw:
  696. if (chip->iobase)
  697. iounmap(chip->iobase);
  698. pci_release_regions(chip->pci);
  699. pci_disable_device(chip->pci);
  700. kfree(chip);
  701. return 0;
  702. }
  703. static int
  704. snd_ad1889_dev_free(struct snd_device *device)
  705. {
  706. struct snd_ad1889 *chip = device->device_data;
  707. return snd_ad1889_free(chip);
  708. }
  709. static int __devinit
  710. snd_ad1889_init(struct snd_ad1889 *chip)
  711. {
  712. ad1889_writew(chip, AD_DS_CCS, AD_DS_CCS_CLKEN); /* turn on clock */
  713. ad1889_readw(chip, AD_DS_CCS); /* flush posted write */
  714. mdelay(10);
  715. /* enable Master and Target abort interrupts */
  716. ad1889_writel(chip, AD_DMA_DISR, AD_DMA_DISR_PMAE | AD_DMA_DISR_PTAE);
  717. return 0;
  718. }
  719. static int __devinit
  720. snd_ad1889_create(struct snd_card *card,
  721. struct pci_dev *pci,
  722. struct snd_ad1889 **rchip)
  723. {
  724. int err;
  725. struct snd_ad1889 *chip;
  726. static struct snd_device_ops ops = {
  727. .dev_free = snd_ad1889_dev_free,
  728. };
  729. *rchip = NULL;
  730. if ((err = pci_enable_device(pci)) < 0)
  731. return err;
  732. /* check PCI availability (32bit DMA) */
  733. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  734. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  735. printk(KERN_ERR PFX "error setting 32-bit DMA mask.\n");
  736. pci_disable_device(pci);
  737. return -ENXIO;
  738. }
  739. /* allocate chip specific data with zero-filled memory */
  740. if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
  741. pci_disable_device(pci);
  742. return -ENOMEM;
  743. }
  744. chip->card = card;
  745. card->private_data = chip;
  746. chip->pci = pci;
  747. chip->irq = -1;
  748. /* (1) PCI resource allocation */
  749. if ((err = pci_request_regions(pci, card->driver)) < 0)
  750. goto free_and_ret;
  751. chip->bar = pci_resource_start(pci, 0);
  752. chip->iobase = ioremap_nocache(chip->bar, pci_resource_len(pci, 0));
  753. if (chip->iobase == NULL) {
  754. printk(KERN_ERR PFX "unable to reserve region.\n");
  755. err = -EBUSY;
  756. goto free_and_ret;
  757. }
  758. pci_set_master(pci);
  759. spin_lock_init(&chip->lock); /* only now can we call ad1889_free */
  760. if (request_irq(pci->irq, snd_ad1889_interrupt,
  761. IRQF_DISABLED|IRQF_SHARED, card->driver, (void*)chip)) {
  762. printk(KERN_ERR PFX "cannot obtain IRQ %d\n", pci->irq);
  763. snd_ad1889_free(chip);
  764. return -EBUSY;
  765. }
  766. chip->irq = pci->irq;
  767. synchronize_irq(chip->irq);
  768. /* (2) initialization of the chip hardware */
  769. if ((err = snd_ad1889_init(chip)) < 0) {
  770. snd_ad1889_free(chip);
  771. return err;
  772. }
  773. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  774. snd_ad1889_free(chip);
  775. return err;
  776. }
  777. snd_card_set_dev(card, &pci->dev);
  778. *rchip = chip;
  779. return 0;
  780. free_and_ret:
  781. kfree(chip);
  782. pci_disable_device(pci);
  783. return err;
  784. }
  785. static int __devinit
  786. snd_ad1889_probe(struct pci_dev *pci,
  787. const struct pci_device_id *pci_id)
  788. {
  789. int err;
  790. static int devno;
  791. struct snd_card *card;
  792. struct snd_ad1889 *chip;
  793. /* (1) */
  794. if (devno >= SNDRV_CARDS)
  795. return -ENODEV;
  796. if (!enable[devno]) {
  797. devno++;
  798. return -ENOENT;
  799. }
  800. /* (2) */
  801. card = snd_card_new(index[devno], id[devno], THIS_MODULE, 0);
  802. /* XXX REVISIT: we can probably allocate chip in this call */
  803. if (card == NULL)
  804. return -ENOMEM;
  805. strcpy(card->driver, "AD1889");
  806. strcpy(card->shortname, "Analog Devices AD1889");
  807. /* (3) */
  808. err = snd_ad1889_create(card, pci, &chip);
  809. if (err < 0)
  810. goto free_and_ret;
  811. /* (4) */
  812. sprintf(card->longname, "%s at 0x%lx irq %i",
  813. card->shortname, chip->bar, chip->irq);
  814. /* (5) */
  815. /* register AC97 mixer */
  816. err = snd_ad1889_ac97_init(chip, ac97_quirk[devno]);
  817. if (err < 0)
  818. goto free_and_ret;
  819. err = snd_ad1889_pcm_init(chip, 0, NULL);
  820. if (err < 0)
  821. goto free_and_ret;
  822. /* register proc interface */
  823. snd_ad1889_proc_init(chip);
  824. /* (6) */
  825. err = snd_card_register(card);
  826. if (err < 0)
  827. goto free_and_ret;
  828. /* (7) */
  829. pci_set_drvdata(pci, card);
  830. devno++;
  831. return 0;
  832. free_and_ret:
  833. snd_card_free(card);
  834. return err;
  835. }
  836. static void __devexit
  837. snd_ad1889_remove(struct pci_dev *pci)
  838. {
  839. snd_card_free(pci_get_drvdata(pci));
  840. pci_set_drvdata(pci, NULL);
  841. }
  842. static struct pci_device_id snd_ad1889_ids[] = {
  843. { PCI_DEVICE(PCI_VENDOR_ID_ANALOG_DEVICES, PCI_DEVICE_ID_AD1889JS) },
  844. { 0, },
  845. };
  846. MODULE_DEVICE_TABLE(pci, snd_ad1889_ids);
  847. static struct pci_driver ad1889_pci = {
  848. .name = "AD1889 Audio",
  849. .id_table = snd_ad1889_ids,
  850. .probe = snd_ad1889_probe,
  851. .remove = __devexit_p(snd_ad1889_remove),
  852. };
  853. static int __init
  854. alsa_ad1889_init(void)
  855. {
  856. return pci_register_driver(&ad1889_pci);
  857. }
  858. static void __exit
  859. alsa_ad1889_fini(void)
  860. {
  861. pci_unregister_driver(&ad1889_pci);
  862. }
  863. module_init(alsa_ad1889_init);
  864. module_exit(alsa_ad1889_fini);