cs5535audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Driver for audio on multifunction CS5535/6 companion device
  3. * Copyright (C) Jaya Kumar
  4. *
  5. * Based on Jaroslav Kysela and Takashi Iwai's examples.
  6. * This work was sponsored by CIS(M) Sdn Bhd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/moduleparam.h>
  29. #include <asm/io.h>
  30. #include <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/pcm.h>
  33. #include <sound/rawmidi.h>
  34. #include <sound/ac97_codec.h>
  35. #include <sound/initval.h>
  36. #include <sound/asoundef.h>
  37. #include "cs5535audio.h"
  38. #define DRIVER_NAME "cs5535audio"
  39. static char *ac97_quirk;
  40. module_param(ac97_quirk, charp, 0444);
  41. MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
  42. static struct ac97_quirk ac97_quirks[] __devinitdata = {
  43. #if 0 /* Not yet confirmed if all 5536 boards are HP only */
  44. {
  45. .subvendor = PCI_VENDOR_ID_AMD,
  46. .subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
  47. .name = "AMD RDK",
  48. .type = AC97_TUNE_HP_ONLY
  49. },
  50. #endif
  51. {}
  52. };
  53. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  54. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  55. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  56. module_param_array(index, int, NULL, 0444);
  57. MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
  58. module_param_array(id, charp, NULL, 0444);
  59. MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
  60. module_param_array(enable, bool, NULL, 0444);
  61. MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);
  62. static struct pci_device_id snd_cs5535audio_ids[] = {
  63. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
  64. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
  65. {}
  66. };
  67. MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
  68. static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
  69. {
  70. unsigned int tmp;
  71. do {
  72. tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
  73. if (!(tmp & CMD_NEW))
  74. break;
  75. udelay(1);
  76. } while (--timeout);
  77. if (!timeout)
  78. snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
  79. }
  80. static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
  81. unsigned short reg)
  82. {
  83. unsigned int regdata;
  84. unsigned int timeout;
  85. unsigned int val;
  86. regdata = ((unsigned int) reg) << 24;
  87. regdata |= ACC_CODEC_CNTL_RD_CMD;
  88. regdata |= CMD_NEW;
  89. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  90. wait_till_cmd_acked(cs5535au, 50);
  91. timeout = 50;
  92. do {
  93. val = cs_readl(cs5535au, ACC_CODEC_STATUS);
  94. if ((val & STS_NEW) && reg == (val >> 24))
  95. break;
  96. udelay(1);
  97. } while (--timeout);
  98. if (!timeout)
  99. snd_printk(KERN_ERR "Failure reading codec reg 0x%x,"
  100. "Last value=0x%x\n", reg, val);
  101. return (unsigned short) val;
  102. }
  103. static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
  104. unsigned short reg, unsigned short val)
  105. {
  106. unsigned int regdata;
  107. regdata = ((unsigned int) reg) << 24;
  108. regdata |= val;
  109. regdata &= CMD_MASK;
  110. regdata |= CMD_NEW;
  111. regdata &= ACC_CODEC_CNTL_WR_CMD;
  112. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  113. wait_till_cmd_acked(cs5535au, 50);
  114. }
  115. static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
  116. unsigned short reg, unsigned short val)
  117. {
  118. struct cs5535audio *cs5535au = ac97->private_data;
  119. snd_cs5535audio_codec_write(cs5535au, reg, val);
  120. }
  121. static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
  122. unsigned short reg)
  123. {
  124. struct cs5535audio *cs5535au = ac97->private_data;
  125. return snd_cs5535audio_codec_read(cs5535au, reg);
  126. }
  127. static int __devinit snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
  128. {
  129. struct snd_card *card = cs5535au->card;
  130. struct snd_ac97_bus *pbus;
  131. struct snd_ac97_template ac97;
  132. int err;
  133. static struct snd_ac97_bus_ops ops = {
  134. .write = snd_cs5535audio_ac97_codec_write,
  135. .read = snd_cs5535audio_ac97_codec_read,
  136. };
  137. if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
  138. return err;
  139. memset(&ac97, 0, sizeof(ac97));
  140. ac97.scaps = AC97_SCAP_AUDIO|AC97_SCAP_SKIP_MODEM;
  141. ac97.private_data = cs5535au;
  142. ac97.pci = cs5535au->pci;
  143. if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
  144. snd_printk(KERN_ERR "mixer failed\n");
  145. return err;
  146. }
  147. snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
  148. return 0;
  149. }
  150. static void process_bm0_irq(struct cs5535audio *cs5535au)
  151. {
  152. u8 bm_stat;
  153. spin_lock(&cs5535au->reg_lock);
  154. bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
  155. spin_unlock(&cs5535au->reg_lock);
  156. if (bm_stat & EOP) {
  157. struct cs5535audio_dma *dma;
  158. dma = cs5535au->playback_substream->runtime->private_data;
  159. snd_pcm_period_elapsed(cs5535au->playback_substream);
  160. } else {
  161. snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",
  162. bm_stat);
  163. }
  164. }
  165. static void process_bm1_irq(struct cs5535audio *cs5535au)
  166. {
  167. u8 bm_stat;
  168. spin_lock(&cs5535au->reg_lock);
  169. bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
  170. spin_unlock(&cs5535au->reg_lock);
  171. if (bm_stat & EOP) {
  172. struct cs5535audio_dma *dma;
  173. dma = cs5535au->capture_substream->runtime->private_data;
  174. snd_pcm_period_elapsed(cs5535au->capture_substream);
  175. }
  176. }
  177. static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
  178. {
  179. u16 acc_irq_stat;
  180. unsigned char count;
  181. struct cs5535audio *cs5535au = dev_id;
  182. if (cs5535au == NULL)
  183. return IRQ_NONE;
  184. acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
  185. if (!acc_irq_stat)
  186. return IRQ_NONE;
  187. for (count = 0; count < 4; count++) {
  188. if (acc_irq_stat & (1 << count)) {
  189. switch (count) {
  190. case IRQ_STS:
  191. cs_readl(cs5535au, ACC_GPIO_STATUS);
  192. break;
  193. case WU_IRQ_STS:
  194. cs_readl(cs5535au, ACC_GPIO_STATUS);
  195. break;
  196. case BM0_IRQ_STS:
  197. process_bm0_irq(cs5535au);
  198. break;
  199. case BM1_IRQ_STS:
  200. process_bm1_irq(cs5535au);
  201. break;
  202. default:
  203. snd_printk(KERN_ERR "Unexpected irq src: "
  204. "0x%x\n", acc_irq_stat);
  205. break;
  206. }
  207. }
  208. }
  209. return IRQ_HANDLED;
  210. }
  211. static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
  212. {
  213. synchronize_irq(cs5535au->irq);
  214. pci_set_power_state(cs5535au->pci, 3);
  215. if (cs5535au->irq >= 0)
  216. free_irq(cs5535au->irq, cs5535au);
  217. pci_release_regions(cs5535au->pci);
  218. pci_disable_device(cs5535au->pci);
  219. kfree(cs5535au);
  220. return 0;
  221. }
  222. static int snd_cs5535audio_dev_free(struct snd_device *device)
  223. {
  224. struct cs5535audio *cs5535au = device->device_data;
  225. return snd_cs5535audio_free(cs5535au);
  226. }
  227. static int __devinit snd_cs5535audio_create(struct snd_card *card,
  228. struct pci_dev *pci,
  229. struct cs5535audio **rcs5535au)
  230. {
  231. struct cs5535audio *cs5535au;
  232. int err;
  233. static struct snd_device_ops ops = {
  234. .dev_free = snd_cs5535audio_dev_free,
  235. };
  236. *rcs5535au = NULL;
  237. if ((err = pci_enable_device(pci)) < 0)
  238. return err;
  239. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  240. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  241. printk(KERN_WARNING "unable to get 32bit dma\n");
  242. err = -ENXIO;
  243. goto pcifail;
  244. }
  245. cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
  246. if (cs5535au == NULL) {
  247. err = -ENOMEM;
  248. goto pcifail;
  249. }
  250. spin_lock_init(&cs5535au->reg_lock);
  251. cs5535au->card = card;
  252. cs5535au->pci = pci;
  253. cs5535au->irq = -1;
  254. if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
  255. kfree(cs5535au);
  256. goto pcifail;
  257. }
  258. cs5535au->port = pci_resource_start(pci, 0);
  259. if (request_irq(pci->irq, snd_cs5535audio_interrupt,
  260. IRQF_SHARED, "CS5535 Audio", cs5535au)) {
  261. snd_printk("unable to grab IRQ %d\n", pci->irq);
  262. err = -EBUSY;
  263. goto sndfail;
  264. }
  265. cs5535au->irq = pci->irq;
  266. pci_set_master(pci);
  267. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  268. cs5535au, &ops)) < 0)
  269. goto sndfail;
  270. snd_card_set_dev(card, &pci->dev);
  271. *rcs5535au = cs5535au;
  272. return 0;
  273. sndfail: /* leave the device alive, just kill the snd */
  274. snd_cs5535audio_free(cs5535au);
  275. return err;
  276. pcifail:
  277. pci_disable_device(pci);
  278. return err;
  279. }
  280. static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
  281. const struct pci_device_id *pci_id)
  282. {
  283. static int dev;
  284. struct snd_card *card;
  285. struct cs5535audio *cs5535au;
  286. int err;
  287. if (dev >= SNDRV_CARDS)
  288. return -ENODEV;
  289. if (!enable[dev]) {
  290. dev++;
  291. return -ENOENT;
  292. }
  293. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  294. if (card == NULL)
  295. return -ENOMEM;
  296. if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
  297. goto probefail_out;
  298. card->private_data = cs5535au;
  299. if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
  300. goto probefail_out;
  301. if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
  302. goto probefail_out;
  303. strcpy(card->driver, DRIVER_NAME);
  304. strcpy(card->shortname, "CS5535 Audio");
  305. sprintf(card->longname, "%s %s at 0x%lx, irq %i",
  306. card->shortname, card->driver,
  307. cs5535au->port, cs5535au->irq);
  308. if ((err = snd_card_register(card)) < 0)
  309. goto probefail_out;
  310. pci_set_drvdata(pci, card);
  311. dev++;
  312. return 0;
  313. probefail_out:
  314. snd_card_free(card);
  315. return err;
  316. }
  317. static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)
  318. {
  319. snd_card_free(pci_get_drvdata(pci));
  320. pci_set_drvdata(pci, NULL);
  321. }
  322. static struct pci_driver driver = {
  323. .name = DRIVER_NAME,
  324. .id_table = snd_cs5535audio_ids,
  325. .probe = snd_cs5535audio_probe,
  326. .remove = __devexit_p(snd_cs5535audio_remove),
  327. #ifdef CONFIG_PM
  328. .suspend = snd_cs5535audio_suspend,
  329. .resume = snd_cs5535audio_resume,
  330. #endif
  331. };
  332. static int __init alsa_card_cs5535audio_init(void)
  333. {
  334. return pci_register_driver(&driver);
  335. }
  336. static void __exit alsa_card_cs5535audio_exit(void)
  337. {
  338. pci_unregister_driver(&driver);
  339. }
  340. module_init(alsa_card_cs5535audio_init)
  341. module_exit(alsa_card_cs5535audio_exit)
  342. MODULE_AUTHOR("Jaya Kumar");
  343. MODULE_LICENSE("GPL");
  344. MODULE_DESCRIPTION("CS5535 Audio");
  345. MODULE_SUPPORTED_DEVICE("CS5535 Audio");