ad1889.c 26 KB

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