cs5530.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <sound/core.h>
  42. #include <sound/sb.h>
  43. #include <sound/initval.h>
  44. MODULE_AUTHOR("Ash Willis");
  45. MODULE_DESCRIPTION("CS5530 Audio");
  46. MODULE_LICENSE("GPL");
  47. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  48. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  49. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  50. struct snd_cs5530 {
  51. struct snd_card *card;
  52. struct pci_dev *pci;
  53. struct snd_sb *sb;
  54. unsigned long pci_base;
  55. };
  56. static struct pci_device_id snd_cs5530_ids[] = {
  57. {PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_AUDIO, PCI_ANY_ID,
  58. PCI_ANY_ID, 0, 0},
  59. {0,}
  60. };
  61. MODULE_DEVICE_TABLE(pci, snd_cs5530_ids);
  62. static int snd_cs5530_free(struct snd_cs5530 *chip)
  63. {
  64. pci_release_regions(chip->pci);
  65. pci_disable_device(chip->pci);
  66. kfree(chip);
  67. return 0;
  68. }
  69. static int snd_cs5530_dev_free(struct snd_device *device)
  70. {
  71. struct snd_cs5530 *chip = device->device_data;
  72. return snd_cs5530_free(chip);
  73. }
  74. static void __devexit snd_cs5530_remove(struct pci_dev *pci)
  75. {
  76. snd_card_free(pci_get_drvdata(pci));
  77. pci_set_drvdata(pci, NULL);
  78. }
  79. static u8 __devinit snd_cs5530_mixer_read(unsigned long io, u8 reg)
  80. {
  81. outb(reg, io + 4);
  82. udelay(20);
  83. reg = inb(io + 5);
  84. udelay(20);
  85. return reg;
  86. }
  87. static int __devinit snd_cs5530_create(struct snd_card *card,
  88. struct pci_dev *pci,
  89. struct snd_cs5530 **rchip)
  90. {
  91. struct snd_cs5530 *chip;
  92. unsigned long sb_base;
  93. u8 irq, dma8, dma16 = 0;
  94. u16 map;
  95. void __iomem *mem;
  96. int err;
  97. static struct snd_device_ops ops = {
  98. .dev_free = snd_cs5530_dev_free,
  99. };
  100. *rchip = NULL;
  101. err = pci_enable_device(pci);
  102. if (err < 0)
  103. return err;
  104. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  105. if (chip == NULL) {
  106. pci_disable_device(pci);
  107. return -ENOMEM;
  108. }
  109. chip->card = card;
  110. chip->pci = pci;
  111. err = pci_request_regions(pci, "CS5530");
  112. if (err < 0) {
  113. kfree(chip);
  114. pci_disable_device(pci);
  115. return err;
  116. }
  117. chip->pci_base = pci_resource_start(pci, 0);
  118. mem = pci_ioremap_bar(pci, 0);
  119. if (mem == NULL) {
  120. kfree(chip);
  121. pci_disable_device(pci);
  122. return -EBUSY;
  123. }
  124. map = readw(mem + 0x18);
  125. iounmap(mem);
  126. /* Map bits
  127. 0:1 * 0x20 + 0x200 = sb base
  128. 2 sb enable
  129. 3 adlib enable
  130. 5 MPU enable 0x330
  131. 6 MPU enable 0x300
  132. The other bits may be used internally so must be masked */
  133. sb_base = 0x220 + 0x20 * (map & 3);
  134. if (map & (1<<2))
  135. printk(KERN_INFO "CS5530: XpressAudio at 0x%lx\n", sb_base);
  136. else {
  137. printk(KERN_ERR "Could not find XpressAudio!\n");
  138. snd_cs5530_free(chip);
  139. return -ENODEV;
  140. }
  141. if (map & (1<<5))
  142. printk(KERN_INFO "CS5530: MPU at 0x300\n");
  143. else if (map & (1<<6))
  144. printk(KERN_INFO "CS5530: MPU at 0x330\n");
  145. irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F;
  146. dma8 = snd_cs5530_mixer_read(sb_base, 0x81);
  147. if (dma8 & 0x20)
  148. dma16 = 5;
  149. else if (dma8 & 0x40)
  150. dma16 = 6;
  151. else if (dma8 & 0x80)
  152. dma16 = 7;
  153. else {
  154. printk(KERN_ERR "CS5530: No 16bit DMA enabled\n");
  155. snd_cs5530_free(chip);
  156. return -ENODEV;
  157. }
  158. if (dma8 & 0x01)
  159. dma8 = 0;
  160. else if (dma8 & 02)
  161. dma8 = 1;
  162. else if (dma8 & 0x08)
  163. dma8 = 3;
  164. else {
  165. printk(KERN_ERR "CS5530: No 8bit DMA enabled\n");
  166. snd_cs5530_free(chip);
  167. return -ENODEV;
  168. }
  169. if (irq & 1)
  170. irq = 9;
  171. else if (irq & 2)
  172. irq = 5;
  173. else if (irq & 4)
  174. irq = 7;
  175. else if (irq & 8)
  176. irq = 10;
  177. else {
  178. printk(KERN_ERR "CS5530: SoundBlaster IRQ not set\n");
  179. snd_cs5530_free(chip);
  180. return -ENODEV;
  181. }
  182. printk(KERN_INFO "CS5530: IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8,
  183. dma16);
  184. err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8,
  185. dma16, SB_HW_CS5530, &chip->sb);
  186. if (err < 0) {
  187. printk(KERN_ERR "CS5530: Could not create SoundBlaster\n");
  188. snd_cs5530_free(chip);
  189. return err;
  190. }
  191. err = snd_sb16dsp_pcm(chip->sb, 0, &chip->sb->pcm);
  192. if (err < 0) {
  193. printk(KERN_ERR "CS5530: Could not create PCM\n");
  194. snd_cs5530_free(chip);
  195. return err;
  196. }
  197. err = snd_sbmixer_new(chip->sb);
  198. if (err < 0) {
  199. printk(KERN_ERR "CS5530: Could not create Mixer\n");
  200. snd_cs5530_free(chip);
  201. return err;
  202. }
  203. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  204. if (err < 0) {
  205. snd_cs5530_free(chip);
  206. return err;
  207. }
  208. snd_card_set_dev(card, &pci->dev);
  209. *rchip = chip;
  210. return 0;
  211. }
  212. static int __devinit snd_cs5530_probe(struct pci_dev *pci,
  213. const struct pci_device_id *pci_id)
  214. {
  215. static int dev;
  216. struct snd_card *card;
  217. struct snd_cs5530 *chip = NULL;
  218. int err;
  219. if (dev >= SNDRV_CARDS)
  220. return -ENODEV;
  221. if (!enable[dev]) {
  222. dev++;
  223. return -ENOENT;
  224. }
  225. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  226. if (err < 0)
  227. return err;
  228. err = snd_cs5530_create(card, pci, &chip);
  229. if (err < 0) {
  230. snd_card_free(card);
  231. return err;
  232. }
  233. strcpy(card->driver, "CS5530");
  234. strcpy(card->shortname, "CS5530 Audio");
  235. sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base);
  236. err = snd_card_register(card);
  237. if (err < 0) {
  238. snd_card_free(card);
  239. return err;
  240. }
  241. pci_set_drvdata(pci, card);
  242. dev++;
  243. return 0;
  244. }
  245. static struct pci_driver driver = {
  246. .name = "CS5530_Audio",
  247. .id_table = snd_cs5530_ids,
  248. .probe = snd_cs5530_probe,
  249. .remove = __devexit_p(snd_cs5530_remove),
  250. };
  251. static int __init alsa_card_cs5530_init(void)
  252. {
  253. return pci_register_driver(&driver);
  254. }
  255. static void __exit alsa_card_cs5530_exit(void)
  256. {
  257. pci_unregister_driver(&driver);
  258. }
  259. module_init(alsa_card_cs5530_init)
  260. module_exit(alsa_card_cs5530_exit)