cs5530.c 7.4 KB

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