cs5530.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. snd_cs5530_free(chip);
  128. return -EBUSY;
  129. }
  130. map = readw(mem + 0x18);
  131. iounmap(mem);
  132. /* Map bits
  133. 0:1 * 0x20 + 0x200 = sb base
  134. 2 sb enable
  135. 3 adlib enable
  136. 5 MPU enable 0x330
  137. 6 MPU enable 0x300
  138. The other bits may be used internally so must be masked */
  139. sb_base = 0x220 + 0x20 * (map & 3);
  140. if (map & (1<<2))
  141. printk(KERN_INFO "CS5530: XpressAudio at 0x%lx\n", sb_base);
  142. else {
  143. printk(KERN_ERR "Could not find XpressAudio!\n");
  144. snd_cs5530_free(chip);
  145. return -ENODEV;
  146. }
  147. if (map & (1<<5))
  148. printk(KERN_INFO "CS5530: MPU at 0x300\n");
  149. else if (map & (1<<6))
  150. printk(KERN_INFO "CS5530: MPU at 0x330\n");
  151. irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F;
  152. dma8 = snd_cs5530_mixer_read(sb_base, 0x81);
  153. if (dma8 & 0x20)
  154. dma16 = 5;
  155. else if (dma8 & 0x40)
  156. dma16 = 6;
  157. else if (dma8 & 0x80)
  158. dma16 = 7;
  159. else {
  160. printk(KERN_ERR "CS5530: No 16bit DMA enabled\n");
  161. snd_cs5530_free(chip);
  162. return -ENODEV;
  163. }
  164. if (dma8 & 0x01)
  165. dma8 = 0;
  166. else if (dma8 & 02)
  167. dma8 = 1;
  168. else if (dma8 & 0x08)
  169. dma8 = 3;
  170. else {
  171. printk(KERN_ERR "CS5530: No 8bit DMA enabled\n");
  172. snd_cs5530_free(chip);
  173. return -ENODEV;
  174. }
  175. if (irq & 1)
  176. irq = 9;
  177. else if (irq & 2)
  178. irq = 5;
  179. else if (irq & 4)
  180. irq = 7;
  181. else if (irq & 8)
  182. irq = 10;
  183. else {
  184. printk(KERN_ERR "CS5530: SoundBlaster IRQ not set\n");
  185. snd_cs5530_free(chip);
  186. return -ENODEV;
  187. }
  188. printk(KERN_INFO "CS5530: IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8,
  189. dma16);
  190. err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8,
  191. dma16, SB_HW_CS5530, &chip->sb);
  192. if (err < 0) {
  193. printk(KERN_ERR "CS5530: Could not create SoundBlaster\n");
  194. snd_cs5530_free(chip);
  195. return err;
  196. }
  197. err = snd_sb16dsp_pcm(chip->sb, 0, &chip->sb->pcm);
  198. if (err < 0) {
  199. printk(KERN_ERR "CS5530: Could not create PCM\n");
  200. snd_cs5530_free(chip);
  201. return err;
  202. }
  203. err = snd_sbmixer_new(chip->sb);
  204. if (err < 0) {
  205. printk(KERN_ERR "CS5530: Could not create Mixer\n");
  206. snd_cs5530_free(chip);
  207. return err;
  208. }
  209. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  210. if (err < 0) {
  211. snd_cs5530_free(chip);
  212. return err;
  213. }
  214. snd_card_set_dev(card, &pci->dev);
  215. *rchip = chip;
  216. return 0;
  217. }
  218. static int __devinit snd_cs5530_probe(struct pci_dev *pci,
  219. const struct pci_device_id *pci_id)
  220. {
  221. static int dev;
  222. struct snd_card *card;
  223. struct snd_cs5530 *chip = NULL;
  224. int err;
  225. if (dev >= SNDRV_CARDS)
  226. return -ENODEV;
  227. if (!enable[dev]) {
  228. dev++;
  229. return -ENOENT;
  230. }
  231. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  232. if (err < 0)
  233. return err;
  234. err = snd_cs5530_create(card, pci, &chip);
  235. if (err < 0) {
  236. snd_card_free(card);
  237. return err;
  238. }
  239. strcpy(card->driver, "CS5530");
  240. strcpy(card->shortname, "CS5530 Audio");
  241. sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base);
  242. err = snd_card_register(card);
  243. if (err < 0) {
  244. snd_card_free(card);
  245. return err;
  246. }
  247. pci_set_drvdata(pci, card);
  248. dev++;
  249. return 0;
  250. }
  251. static struct pci_driver cs5530_driver = {
  252. .name = KBUILD_MODNAME,
  253. .id_table = snd_cs5530_ids,
  254. .probe = snd_cs5530_probe,
  255. .remove = __devexit_p(snd_cs5530_remove),
  256. };
  257. module_pci_driver(cs5530_driver);