cs5530.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 const struct pci_device_id 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 snd_cs5530_remove(struct pci_dev *pci)
  82. {
  83. snd_card_free(pci_get_drvdata(pci));
  84. }
  85. static u8 snd_cs5530_mixer_read(unsigned long io, u8 reg)
  86. {
  87. outb(reg, io + 4);
  88. udelay(20);
  89. reg = inb(io + 5);
  90. udelay(20);
  91. return reg;
  92. }
  93. static int snd_cs5530_create(struct snd_card *card,
  94. struct pci_dev *pci,
  95. struct snd_cs5530 **rchip)
  96. {
  97. struct snd_cs5530 *chip;
  98. unsigned long sb_base;
  99. u8 irq, dma8, dma16 = 0;
  100. u16 map;
  101. void __iomem *mem;
  102. int err;
  103. static struct snd_device_ops ops = {
  104. .dev_free = snd_cs5530_dev_free,
  105. };
  106. *rchip = NULL;
  107. err = pci_enable_device(pci);
  108. if (err < 0)
  109. return err;
  110. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  111. if (chip == NULL) {
  112. pci_disable_device(pci);
  113. return -ENOMEM;
  114. }
  115. chip->card = card;
  116. chip->pci = pci;
  117. err = pci_request_regions(pci, "CS5530");
  118. if (err < 0) {
  119. kfree(chip);
  120. pci_disable_device(pci);
  121. return err;
  122. }
  123. chip->pci_base = pci_resource_start(pci, 0);
  124. mem = pci_ioremap_bar(pci, 0);
  125. if (mem == NULL) {
  126. snd_cs5530_free(chip);
  127. return -EBUSY;
  128. }
  129. map = readw(mem + 0x18);
  130. iounmap(mem);
  131. /* Map bits
  132. 0:1 * 0x20 + 0x200 = sb base
  133. 2 sb enable
  134. 3 adlib enable
  135. 5 MPU enable 0x330
  136. 6 MPU enable 0x300
  137. The other bits may be used internally so must be masked */
  138. sb_base = 0x220 + 0x20 * (map & 3);
  139. if (map & (1<<2))
  140. dev_info(card->dev, "XpressAudio at 0x%lx\n", sb_base);
  141. else {
  142. dev_err(card->dev, "Could not find XpressAudio!\n");
  143. snd_cs5530_free(chip);
  144. return -ENODEV;
  145. }
  146. if (map & (1<<5))
  147. dev_info(card->dev, "MPU at 0x300\n");
  148. else if (map & (1<<6))
  149. dev_info(card->dev, "MPU at 0x330\n");
  150. irq = snd_cs5530_mixer_read(sb_base, 0x80) & 0x0F;
  151. dma8 = snd_cs5530_mixer_read(sb_base, 0x81);
  152. if (dma8 & 0x20)
  153. dma16 = 5;
  154. else if (dma8 & 0x40)
  155. dma16 = 6;
  156. else if (dma8 & 0x80)
  157. dma16 = 7;
  158. else {
  159. dev_err(card->dev, "No 16bit DMA enabled\n");
  160. snd_cs5530_free(chip);
  161. return -ENODEV;
  162. }
  163. if (dma8 & 0x01)
  164. dma8 = 0;
  165. else if (dma8 & 02)
  166. dma8 = 1;
  167. else if (dma8 & 0x08)
  168. dma8 = 3;
  169. else {
  170. dev_err(card->dev, "No 8bit DMA enabled\n");
  171. snd_cs5530_free(chip);
  172. return -ENODEV;
  173. }
  174. if (irq & 1)
  175. irq = 9;
  176. else if (irq & 2)
  177. irq = 5;
  178. else if (irq & 4)
  179. irq = 7;
  180. else if (irq & 8)
  181. irq = 10;
  182. else {
  183. dev_err(card->dev, "SoundBlaster IRQ not set\n");
  184. snd_cs5530_free(chip);
  185. return -ENODEV;
  186. }
  187. dev_info(card->dev, "IRQ: %d DMA8: %d DMA16: %d\n", irq, dma8, dma16);
  188. err = snd_sbdsp_create(card, sb_base, irq, snd_sb16dsp_interrupt, dma8,
  189. dma16, SB_HW_CS5530, &chip->sb);
  190. if (err < 0) {
  191. dev_err(card->dev, "Could not create SoundBlaster\n");
  192. snd_cs5530_free(chip);
  193. return err;
  194. }
  195. err = snd_sb16dsp_pcm(chip->sb, 0);
  196. if (err < 0) {
  197. dev_err(card->dev, "Could not create PCM\n");
  198. snd_cs5530_free(chip);
  199. return err;
  200. }
  201. err = snd_sbmixer_new(chip->sb);
  202. if (err < 0) {
  203. dev_err(card->dev, "Could not create Mixer\n");
  204. snd_cs5530_free(chip);
  205. return err;
  206. }
  207. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  208. if (err < 0) {
  209. snd_cs5530_free(chip);
  210. return err;
  211. }
  212. *rchip = chip;
  213. return 0;
  214. }
  215. static int snd_cs5530_probe(struct pci_dev *pci,
  216. const struct pci_device_id *pci_id)
  217. {
  218. static int dev;
  219. struct snd_card *card;
  220. struct snd_cs5530 *chip = NULL;
  221. int err;
  222. if (dev >= SNDRV_CARDS)
  223. return -ENODEV;
  224. if (!enable[dev]) {
  225. dev++;
  226. return -ENOENT;
  227. }
  228. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  229. 0, &card);
  230. if (err < 0)
  231. return err;
  232. err = snd_cs5530_create(card, pci, &chip);
  233. if (err < 0) {
  234. snd_card_free(card);
  235. return err;
  236. }
  237. strcpy(card->driver, "CS5530");
  238. strcpy(card->shortname, "CS5530 Audio");
  239. sprintf(card->longname, "%s at 0x%lx", card->shortname, chip->pci_base);
  240. err = snd_card_register(card);
  241. if (err < 0) {
  242. snd_card_free(card);
  243. return err;
  244. }
  245. pci_set_drvdata(pci, card);
  246. dev++;
  247. return 0;
  248. }
  249. static struct pci_driver cs5530_driver = {
  250. .name = KBUILD_MODNAME,
  251. .id_table = snd_cs5530_ids,
  252. .probe = snd_cs5530_probe,
  253. .remove = snd_cs5530_remove,
  254. };
  255. module_pci_driver(cs5530_driver);