cs5530.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * cs5530.c - Initialisation code for Cyrix/NatSemi VSA1 softaudio
  3. *
  4. * (C) Copyright 2007 Ash Willis <ashwillis@programmer.net>
  5. * (C) Copyright 2003 Red Hat Inc <alan@lxorguk.ukuu.org.uk>
  6. *
  7. * This driver was ported (shamelessly ripped ;) from oss/kahlua.c but I did
  8. * mess with it a bit. The chip seems to have to have trouble with full duplex
  9. * mode. If we're recording in 8bit 8000kHz, say, and we then attempt to
  10. * simultaneously play back audio at 16bit 44100kHz, the device actually plays
  11. * back in the same format in which it is capturing. By forcing the chip to
  12. * always play/capture in 16/44100, we can let alsa-lib convert the samples and
  13. * that way we can hack up some full duplex audio.
  14. *
  15. * XpressAudio(tm) is used on the Cyrix MediaGX (now NatSemi Geode) systems.
  16. * The older version (VSA1) provides fairly good soundblaster emulation
  17. * although there are a couple of bugs: large DMA buffers break record,
  18. * and the MPU event handling seems suspect. VSA2 allows the native driver
  19. * to control the AC97 audio engine directly and requires a different driver.
  20. *
  21. * Thanks to National Semiconductor for providing the needed information
  22. * on the XpressAudio(tm) internals.
  23. *
  24. * This program is free software; you can redistribute it and/or modify it
  25. * under the terms of the GNU General Public License as published by the
  26. * Free Software Foundation; either version 2, or (at your option) any
  27. * later version.
  28. *
  29. * This program is distributed in the hope that it will be useful, but
  30. * WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. * General Public License for more details.
  33. *
  34. * TO DO:
  35. * Investigate whether we can portably support Cognac (5520) in the
  36. * same manner.
  37. */
  38. #include <linux/delay.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/pci.h>
  41. #include <linux/slab.h>
  42. #include <sound/core.h>
  43. #include <sound/sb.h>
  44. #include <sound/initval.h>
  45. MODULE_AUTHOR("Ash Willis");
  46. MODULE_DESCRIPTION("CS5530 Audio");
  47. MODULE_LICENSE("GPL");
  48. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  49. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  50. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  51. struct snd_cs5530 {
  52. struct snd_card *card;
  53. struct pci_dev *pci;
  54. struct snd_sb *sb;
  55. unsigned long pci_base;
  56. };
  57. static DEFINE_PCI_DEVICE_TABLE(snd_cs5530_ids) = {
  58. {PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_AUDIO, PCI_ANY_ID,
  59. PCI_ANY_ID, 0, 0},
  60. {0,}
  61. };
  62. MODULE_DEVICE_TABLE(pci, snd_cs5530_ids);
  63. static int snd_cs5530_free(struct snd_cs5530 *chip)
  64. {
  65. pci_release_regions(chip->pci);
  66. pci_disable_device(chip->pci);
  67. kfree(chip);
  68. return 0;
  69. }
  70. static int snd_cs5530_dev_free(struct snd_device *device)
  71. {
  72. struct snd_cs5530 *chip = device->device_data;
  73. return snd_cs5530_free(chip);
  74. }
  75. static void __devexit snd_cs5530_remove(struct pci_dev *pci)
  76. {
  77. snd_card_free(pci_get_drvdata(pci));
  78. pci_set_drvdata(pci, NULL);
  79. }
  80. static u8 __devinit snd_cs5530_mixer_read(unsigned long io, u8 reg)
  81. {
  82. outb(reg, io + 4);
  83. udelay(20);
  84. reg = inb(io + 5);
  85. udelay(20);
  86. return reg;
  87. }
  88. static int __devinit snd_cs5530_create(struct snd_card *card,
  89. struct pci_dev *pci,
  90. struct snd_cs5530 **rchip)
  91. {
  92. struct snd_cs5530 *chip;
  93. unsigned long sb_base;
  94. u8 irq, dma8, dma16 = 0;
  95. u16 map;
  96. void __iomem *mem;
  97. int err;
  98. static struct snd_device_ops ops = {
  99. .dev_free = snd_cs5530_dev_free,
  100. };
  101. *rchip = NULL;
  102. err = pci_enable_device(pci);
  103. if (err < 0)
  104. return err;
  105. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  106. if (chip == NULL) {
  107. pci_disable_device(pci);
  108. return -ENOMEM;
  109. }
  110. chip->card = card;
  111. chip->pci = pci;
  112. err = pci_request_regions(pci, "CS5530");
  113. if (err < 0) {
  114. kfree(chip);
  115. pci_disable_device(pci);
  116. return err;
  117. }
  118. chip->pci_base = pci_resource_start(pci, 0);
  119. mem = pci_ioremap_bar(pci, 0);
  120. if (mem == NULL) {
  121. kfree(chip);
  122. pci_disable_device(pci);
  123. return -EBUSY;
  124. }
  125. map = readw(mem + 0x18);
  126. iounmap(mem);
  127. /* Map bits
  128. 0:1 * 0x20 + 0x200 = sb base
  129. 2 sb enable
  130. 3 adlib enable
  131. 5 MPU enable 0x330
  132. 6 MPU enable 0x300
  133. The other bits may be used internally so must be masked */
  134. sb_base = 0x220 + 0x20 * (map & 3);
  135. if (map & (1<<2))
  136. printk(KERN_INFO "CS5530: XpressAudio at 0x%lx\n", sb_base);
  137. else {
  138. printk(KERN_ERR "Could not find XpressAudio!\n");
  139. snd_cs5530_free(chip);
  140. return -ENODEV;
  141. }
  142. if (map & (1<<5))
  143. printk(KERN_INFO "CS5530: MPU at 0x300\n");
  144. else if (map & (1<<6))
  145. printk(KERN_INFO "CS5530: MPU at 0x330\n");
  146. irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F;
  147. dma8 = snd_cs5530_mixer_read(sb_base, 0x81);
  148. if (dma8 & 0x20)
  149. dma16 = 5;
  150. else if (dma8 & 0x40)
  151. dma16 = 6;
  152. else if (dma8 & 0x80)
  153. dma16 = 7;
  154. else {
  155. printk(KERN_ERR "CS5530: No 16bit DMA enabled\n");
  156. snd_cs5530_free(chip);
  157. return -ENODEV;
  158. }
  159. if (dma8 & 0x01)
  160. dma8 = 0;
  161. else if (dma8 & 02)
  162. dma8 = 1;
  163. else if (dma8 & 0x08)
  164. dma8 = 3;
  165. else {
  166. printk(KERN_ERR "CS5530: No 8bit DMA enabled\n");
  167. snd_cs5530_free(chip);
  168. return -ENODEV;
  169. }
  170. if (irq & 1)
  171. irq = 9;
  172. else if (irq & 2)
  173. irq = 5;
  174. else if (irq & 4)
  175. irq = 7;
  176. else if (irq & 8)
  177. irq = 10;
  178. else {
  179. printk(KERN_ERR "CS5530: SoundBlaster IRQ not set\n");
  180. snd_cs5530_free(chip);
  181. return -ENODEV;
  182. }
  183. printk(KERN_INFO "CS5530: IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8,
  184. dma16);
  185. err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8,
  186. dma16, SB_HW_CS5530, &chip->sb);
  187. if (err < 0) {
  188. printk(KERN_ERR "CS5530: Could not create SoundBlaster\n");
  189. snd_cs5530_free(chip);
  190. return err;
  191. }
  192. err = snd_sb16dsp_pcm(chip->sb, 0, &chip->sb->pcm);
  193. if (err < 0) {
  194. printk(KERN_ERR "CS5530: Could not create PCM\n");
  195. snd_cs5530_free(chip);
  196. return err;
  197. }
  198. err = snd_sbmixer_new(chip->sb);
  199. if (err < 0) {
  200. printk(KERN_ERR "CS5530: Could not create Mixer\n");
  201. snd_cs5530_free(chip);
  202. return err;
  203. }
  204. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  205. if (err < 0) {
  206. snd_cs5530_free(chip);
  207. return err;
  208. }
  209. snd_card_set_dev(card, &pci->dev);
  210. *rchip = chip;
  211. return 0;
  212. }
  213. static int __devinit snd_cs5530_probe(struct pci_dev *pci,
  214. const struct pci_device_id *pci_id)
  215. {
  216. static int dev;
  217. struct snd_card *card;
  218. struct snd_cs5530 *chip = NULL;
  219. int err;
  220. if (dev >= SNDRV_CARDS)
  221. return -ENODEV;
  222. if (!enable[dev]) {
  223. dev++;
  224. return -ENOENT;
  225. }
  226. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  227. if (err < 0)
  228. return err;
  229. err = snd_cs5530_create(card, pci, &chip);
  230. if (err < 0) {
  231. snd_card_free(card);
  232. return err;
  233. }
  234. strcpy(card->driver, "CS5530");
  235. strcpy(card->shortname, "CS5530 Audio");
  236. sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base);
  237. err = snd_card_register(card);
  238. if (err < 0) {
  239. snd_card_free(card);
  240. return err;
  241. }
  242. pci_set_drvdata(pci, card);
  243. dev++;
  244. return 0;
  245. }
  246. static struct pci_driver driver = {
  247. .name = "CS5530_Audio",
  248. .id_table = snd_cs5530_ids,
  249. .probe = snd_cs5530_probe,
  250. .remove = __devexit_p(snd_cs5530_remove),
  251. };
  252. static int __init alsa_card_cs5530_init(void)
  253. {
  254. return pci_register_driver(&driver);
  255. }
  256. static void __exit alsa_card_cs5530_exit(void)
  257. {
  258. pci_unregister_driver(&driver);
  259. }
  260. module_init(alsa_card_cs5530_init)
  261. module_exit(alsa_card_cs5530_exit)