ad1889.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 *a lot* 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 <linux/module.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 bool 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. __func__);
  225. return -EIO;
  226. }
  227. ad1889_debug("[%s] ready after %d ms\n", __func__, 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. if (snd_BUG_ON(ptr >= chip->wave.size))
  454. return 0;
  455. return bytes_to_frames(ss->runtime, ptr);
  456. }
  457. /* Called in atomic context with IRQ disabled */
  458. static snd_pcm_uframes_t
  459. snd_ad1889_capture_pointer(struct snd_pcm_substream *ss)
  460. {
  461. size_t ptr = 0;
  462. struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
  463. if (unlikely(!(chip->ramc.reg & AD_DS_RAMC_ADEN)))
  464. return 0;
  465. ptr = ad1889_readl(chip, AD_DMA_ADCCA);
  466. ptr -= chip->ramc.addr;
  467. if (snd_BUG_ON(ptr >= chip->ramc.size))
  468. return 0;
  469. return bytes_to_frames(ss->runtime, ptr);
  470. }
  471. static struct snd_pcm_ops snd_ad1889_playback_ops = {
  472. .open = snd_ad1889_playback_open,
  473. .close = snd_ad1889_playback_close,
  474. .ioctl = snd_pcm_lib_ioctl,
  475. .hw_params = snd_ad1889_hw_params,
  476. .hw_free = snd_ad1889_hw_free,
  477. .prepare = snd_ad1889_playback_prepare,
  478. .trigger = snd_ad1889_playback_trigger,
  479. .pointer = snd_ad1889_playback_pointer,
  480. };
  481. static struct snd_pcm_ops snd_ad1889_capture_ops = {
  482. .open = snd_ad1889_capture_open,
  483. .close = snd_ad1889_capture_close,
  484. .ioctl = snd_pcm_lib_ioctl,
  485. .hw_params = snd_ad1889_hw_params,
  486. .hw_free = snd_ad1889_hw_free,
  487. .prepare = snd_ad1889_capture_prepare,
  488. .trigger = snd_ad1889_capture_trigger,
  489. .pointer = snd_ad1889_capture_pointer,
  490. };
  491. static irqreturn_t
  492. snd_ad1889_interrupt(int irq, void *dev_id)
  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. if (chip->irq >= 0)
  693. free_irq(chip->irq, chip);
  694. skip_hw:
  695. if (chip->iobase)
  696. iounmap(chip->iobase);
  697. pci_release_regions(chip->pci);
  698. pci_disable_device(chip->pci);
  699. kfree(chip);
  700. return 0;
  701. }
  702. static int
  703. snd_ad1889_dev_free(struct snd_device *device)
  704. {
  705. struct snd_ad1889 *chip = device->device_data;
  706. return snd_ad1889_free(chip);
  707. }
  708. static int __devinit
  709. snd_ad1889_init(struct snd_ad1889 *chip)
  710. {
  711. ad1889_writew(chip, AD_DS_CCS, AD_DS_CCS_CLKEN); /* turn on clock */
  712. ad1889_readw(chip, AD_DS_CCS); /* flush posted write */
  713. mdelay(10);
  714. /* enable Master and Target abort interrupts */
  715. ad1889_writel(chip, AD_DMA_DISR, AD_DMA_DISR_PMAE | AD_DMA_DISR_PTAE);
  716. return 0;
  717. }
  718. static int __devinit
  719. snd_ad1889_create(struct snd_card *card,
  720. struct pci_dev *pci,
  721. struct snd_ad1889 **rchip)
  722. {
  723. int err;
  724. struct snd_ad1889 *chip;
  725. static struct snd_device_ops ops = {
  726. .dev_free = snd_ad1889_dev_free,
  727. };
  728. *rchip = NULL;
  729. if ((err = pci_enable_device(pci)) < 0)
  730. return err;
  731. /* check PCI availability (32bit DMA) */
  732. if (pci_set_dma_mask(pci, DMA_BIT_MASK(32)) < 0 ||
  733. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32)) < 0) {
  734. printk(KERN_ERR PFX "error setting 32-bit DMA mask.\n");
  735. pci_disable_device(pci);
  736. return -ENXIO;
  737. }
  738. /* allocate chip specific data with zero-filled memory */
  739. if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
  740. pci_disable_device(pci);
  741. return -ENOMEM;
  742. }
  743. chip->card = card;
  744. card->private_data = chip;
  745. chip->pci = pci;
  746. chip->irq = -1;
  747. /* (1) PCI resource allocation */
  748. if ((err = pci_request_regions(pci, card->driver)) < 0)
  749. goto free_and_ret;
  750. chip->bar = pci_resource_start(pci, 0);
  751. chip->iobase = pci_ioremap_bar(pci, 0);
  752. if (chip->iobase == NULL) {
  753. printk(KERN_ERR PFX "unable to reserve region.\n");
  754. err = -EBUSY;
  755. goto free_and_ret;
  756. }
  757. pci_set_master(pci);
  758. spin_lock_init(&chip->lock); /* only now can we call ad1889_free */
  759. if (request_irq(pci->irq, snd_ad1889_interrupt,
  760. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  761. printk(KERN_ERR PFX "cannot obtain IRQ %d\n", pci->irq);
  762. snd_ad1889_free(chip);
  763. return -EBUSY;
  764. }
  765. chip->irq = pci->irq;
  766. synchronize_irq(chip->irq);
  767. /* (2) initialization of the chip hardware */
  768. if ((err = snd_ad1889_init(chip)) < 0) {
  769. snd_ad1889_free(chip);
  770. return err;
  771. }
  772. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  773. snd_ad1889_free(chip);
  774. return err;
  775. }
  776. snd_card_set_dev(card, &pci->dev);
  777. *rchip = chip;
  778. return 0;
  779. free_and_ret:
  780. kfree(chip);
  781. pci_disable_device(pci);
  782. return err;
  783. }
  784. static int __devinit
  785. snd_ad1889_probe(struct pci_dev *pci,
  786. const struct pci_device_id *pci_id)
  787. {
  788. int err;
  789. static int devno;
  790. struct snd_card *card;
  791. struct snd_ad1889 *chip;
  792. /* (1) */
  793. if (devno >= SNDRV_CARDS)
  794. return -ENODEV;
  795. if (!enable[devno]) {
  796. devno++;
  797. return -ENOENT;
  798. }
  799. /* (2) */
  800. err = snd_card_create(index[devno], id[devno], THIS_MODULE, 0, &card);
  801. /* XXX REVISIT: we can probably allocate chip in this call */
  802. if (err < 0)
  803. return err;
  804. strcpy(card->driver, "AD1889");
  805. strcpy(card->shortname, "Analog Devices AD1889");
  806. /* (3) */
  807. err = snd_ad1889_create(card, pci, &chip);
  808. if (err < 0)
  809. goto free_and_ret;
  810. /* (4) */
  811. sprintf(card->longname, "%s at 0x%lx irq %i",
  812. card->shortname, chip->bar, chip->irq);
  813. /* (5) */
  814. /* register AC97 mixer */
  815. err = snd_ad1889_ac97_init(chip, ac97_quirk[devno]);
  816. if (err < 0)
  817. goto free_and_ret;
  818. err = snd_ad1889_pcm_init(chip, 0, NULL);
  819. if (err < 0)
  820. goto free_and_ret;
  821. /* register proc interface */
  822. snd_ad1889_proc_init(chip);
  823. /* (6) */
  824. err = snd_card_register(card);
  825. if (err < 0)
  826. goto free_and_ret;
  827. /* (7) */
  828. pci_set_drvdata(pci, card);
  829. devno++;
  830. return 0;
  831. free_and_ret:
  832. snd_card_free(card);
  833. return err;
  834. }
  835. static void __devexit
  836. snd_ad1889_remove(struct pci_dev *pci)
  837. {
  838. snd_card_free(pci_get_drvdata(pci));
  839. pci_set_drvdata(pci, NULL);
  840. }
  841. static DEFINE_PCI_DEVICE_TABLE(snd_ad1889_ids) = {
  842. { PCI_DEVICE(PCI_VENDOR_ID_ANALOG_DEVICES, PCI_DEVICE_ID_AD1889JS) },
  843. { 0, },
  844. };
  845. MODULE_DEVICE_TABLE(pci, snd_ad1889_ids);
  846. static struct pci_driver ad1889_pci_driver = {
  847. .name = KBUILD_MODNAME,
  848. .id_table = snd_ad1889_ids,
  849. .probe = snd_ad1889_probe,
  850. .remove = __devexit_p(snd_ad1889_remove),
  851. };
  852. static int __init
  853. alsa_ad1889_init(void)
  854. {
  855. return pci_register_driver(&ad1889_pci_driver);
  856. }
  857. static void __exit
  858. alsa_ad1889_fini(void)
  859. {
  860. pci_unregister_driver(&ad1889_pci_driver);
  861. }
  862. module_init(alsa_ad1889_init);
  863. module_exit(alsa_ad1889_fini);