cs5535audio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Driver for audio on multifunction CS5535 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 <sound/driver.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/pci.h>
  28. #include <linux/slab.h>
  29. #include <linux/moduleparam.h>
  30. #include <asm/io.h>
  31. #include <sound/core.h>
  32. #include <sound/control.h>
  33. #include <sound/pcm.h>
  34. #include <sound/rawmidi.h>
  35. #include <sound/ac97_codec.h>
  36. #include <sound/initval.h>
  37. #include <sound/asoundef.h>
  38. #include "cs5535audio.h"
  39. #define DRIVER_NAME "cs5535audio"
  40. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  41. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  42. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  43. static struct pci_device_id snd_cs5535audio_ids[] = {
  44. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO, PCI_ANY_ID,
  45. PCI_ANY_ID, 0, 0, 0, },
  46. {}
  47. };
  48. MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
  49. static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
  50. {
  51. unsigned int tmp;
  52. do {
  53. tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
  54. if (!(tmp & CMD_NEW))
  55. break;
  56. msleep(10);
  57. } while (--timeout);
  58. if (!timeout)
  59. snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");
  60. }
  61. static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
  62. unsigned short reg)
  63. {
  64. unsigned int regdata;
  65. unsigned int timeout;
  66. unsigned int val;
  67. regdata = ((unsigned int) reg) << 24;
  68. regdata |= ACC_CODEC_CNTL_RD_CMD;
  69. regdata |= CMD_NEW;
  70. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  71. wait_till_cmd_acked(cs5535au, 500);
  72. timeout = 50;
  73. do {
  74. val = cs_readl(cs5535au, ACC_CODEC_STATUS);
  75. if ((val & STS_NEW) && reg == (val >> 24))
  76. break;
  77. msleep(10);
  78. } while (--timeout);
  79. if (!timeout)
  80. snd_printk(KERN_ERR "Failure reading cs5535 codec\n");
  81. return (unsigned short) val;
  82. }
  83. static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
  84. unsigned short reg, unsigned short val)
  85. {
  86. unsigned int regdata;
  87. regdata = ((unsigned int) reg) << 24;
  88. regdata |= val;
  89. regdata &= CMD_MASK;
  90. regdata |= CMD_NEW;
  91. regdata &= ACC_CODEC_CNTL_WR_CMD;
  92. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  93. wait_till_cmd_acked(cs5535au, 50);
  94. }
  95. static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
  96. unsigned short reg, unsigned short val)
  97. {
  98. struct cs5535audio *cs5535au = ac97->private_data;
  99. snd_cs5535audio_codec_write(cs5535au, reg, val);
  100. }
  101. static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
  102. unsigned short reg)
  103. {
  104. struct cs5535audio *cs5535au = ac97->private_data;
  105. return snd_cs5535audio_codec_read(cs5535au, reg);
  106. }
  107. static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
  108. {
  109. struct snd_card *card = cs5535au->card;
  110. struct snd_ac97_bus *pbus;
  111. struct snd_ac97_template ac97;
  112. int err;
  113. static struct snd_ac97_bus_ops ops = {
  114. .write = snd_cs5535audio_ac97_codec_write,
  115. .read = snd_cs5535audio_ac97_codec_read,
  116. };
  117. if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
  118. return err;
  119. memset(&ac97, 0, sizeof(ac97));
  120. ac97.scaps = AC97_SCAP_AUDIO|AC97_SCAP_SKIP_MODEM;
  121. ac97.private_data = cs5535au;
  122. ac97.pci = cs5535au->pci;
  123. if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
  124. snd_printk(KERN_ERR "mixer failed\n");
  125. return err;
  126. }
  127. return 0;
  128. }
  129. static void process_bm0_irq(struct cs5535audio *cs5535au)
  130. {
  131. u8 bm_stat;
  132. spin_lock(&cs5535au->reg_lock);
  133. bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
  134. spin_unlock(&cs5535au->reg_lock);
  135. if (bm_stat & EOP) {
  136. struct cs5535audio_dma *dma;
  137. dma = cs5535au->playback_substream->runtime->private_data;
  138. snd_pcm_period_elapsed(cs5535au->playback_substream);
  139. } else {
  140. snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",
  141. bm_stat);
  142. }
  143. }
  144. static void process_bm1_irq(struct cs5535audio *cs5535au)
  145. {
  146. u8 bm_stat;
  147. spin_lock(&cs5535au->reg_lock);
  148. bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
  149. spin_unlock(&cs5535au->reg_lock);
  150. if (bm_stat & EOP) {
  151. struct cs5535audio_dma *dma;
  152. dma = cs5535au->capture_substream->runtime->private_data;
  153. snd_pcm_period_elapsed(cs5535au->capture_substream);
  154. }
  155. }
  156. static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id,
  157. struct pt_regs *regs)
  158. {
  159. u16 acc_irq_stat;
  160. u8 bm_stat;
  161. unsigned char count;
  162. struct cs5535audio *cs5535au = dev_id;
  163. if (cs5535au == NULL)
  164. return IRQ_NONE;
  165. acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
  166. if (!acc_irq_stat)
  167. return IRQ_NONE;
  168. for (count = 0; count < 10; count++) {
  169. if (acc_irq_stat & (1 << count)) {
  170. switch (count) {
  171. case IRQ_STS:
  172. cs_readl(cs5535au, ACC_GPIO_STATUS);
  173. break;
  174. case WU_IRQ_STS:
  175. cs_readl(cs5535au, ACC_GPIO_STATUS);
  176. break;
  177. case BM0_IRQ_STS:
  178. process_bm0_irq(cs5535au);
  179. break;
  180. case BM1_IRQ_STS:
  181. process_bm1_irq(cs5535au);
  182. break;
  183. case BM2_IRQ_STS:
  184. bm_stat = cs_readb(cs5535au, ACC_BM2_STATUS);
  185. break;
  186. case BM3_IRQ_STS:
  187. bm_stat = cs_readb(cs5535au, ACC_BM3_STATUS);
  188. break;
  189. case BM4_IRQ_STS:
  190. bm_stat = cs_readb(cs5535au, ACC_BM4_STATUS);
  191. break;
  192. case BM5_IRQ_STS:
  193. bm_stat = cs_readb(cs5535au, ACC_BM5_STATUS);
  194. break;
  195. case BM6_IRQ_STS:
  196. bm_stat = cs_readb(cs5535au, ACC_BM6_STATUS);
  197. break;
  198. case BM7_IRQ_STS:
  199. bm_stat = cs_readb(cs5535au, ACC_BM7_STATUS);
  200. break;
  201. default:
  202. snd_printk(KERN_ERR "Unexpected irq src\n");
  203. break;
  204. }
  205. }
  206. }
  207. return IRQ_HANDLED;
  208. }
  209. static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
  210. {
  211. synchronize_irq(cs5535au->irq);
  212. pci_set_power_state(cs5535au->pci, 3);
  213. if (cs5535au->irq >= 0)
  214. free_irq(cs5535au->irq, cs5535au);
  215. pci_release_regions(cs5535au->pci);
  216. pci_disable_device(cs5535au->pci);
  217. kfree(cs5535au);
  218. return 0;
  219. }
  220. static int snd_cs5535audio_dev_free(struct snd_device *device)
  221. {
  222. struct cs5535audio *cs5535au = device->device_data;
  223. return snd_cs5535audio_free(cs5535au);
  224. }
  225. static int __devinit snd_cs5535audio_create(struct snd_card *card,
  226. struct pci_dev *pci,
  227. struct cs5535audio **rcs5535au)
  228. {
  229. struct cs5535audio *cs5535au;
  230. int err;
  231. static struct snd_device_ops ops = {
  232. .dev_free = snd_cs5535audio_dev_free,
  233. };
  234. *rcs5535au = NULL;
  235. if ((err = pci_enable_device(pci)) < 0)
  236. return err;
  237. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  238. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  239. printk(KERN_WARNING "unable to get 32bit dma\n");
  240. err = -ENXIO;
  241. goto pcifail;
  242. }
  243. cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
  244. if (cs5535au == NULL) {
  245. err = -ENOMEM;
  246. goto pcifail;
  247. }
  248. spin_lock_init(&cs5535au->reg_lock);
  249. cs5535au->card = card;
  250. cs5535au->pci = pci;
  251. cs5535au->irq = -1;
  252. if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
  253. kfree(cs5535au);
  254. goto pcifail;
  255. }
  256. cs5535au->port = pci_resource_start(pci, 0);
  257. if (request_irq(pci->irq, snd_cs5535audio_interrupt,
  258. SA_INTERRUPT|SA_SHIRQ, "CS5535 Audio", cs5535au)) {
  259. snd_printk("unable to grab IRQ %d\n", pci->irq);
  260. err = -EBUSY;
  261. goto sndfail;
  262. }
  263. cs5535au->irq = pci->irq;
  264. pci_set_master(pci);
  265. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  266. cs5535au, &ops)) < 0)
  267. goto sndfail;
  268. snd_card_set_dev(card, &pci->dev);
  269. *rcs5535au = cs5535au;
  270. return 0;
  271. sndfail: /* leave the device alive, just kill the snd */
  272. snd_cs5535audio_free(cs5535au);
  273. return err;
  274. pcifail:
  275. pci_disable_device(pci);
  276. return err;
  277. }
  278. static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,
  279. const struct pci_device_id *pci_id)
  280. {
  281. static int dev;
  282. struct snd_card *card;
  283. struct cs5535audio *cs5535au;
  284. int err;
  285. if (dev >= SNDRV_CARDS)
  286. return -ENODEV;
  287. if (!enable[dev]) {
  288. dev++;
  289. return -ENOENT;
  290. }
  291. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  292. if (card == NULL)
  293. return -ENOMEM;
  294. if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
  295. goto probefail_out;
  296. if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
  297. goto probefail_out;
  298. if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
  299. goto probefail_out;
  300. strcpy(card->driver, DRIVER_NAME);
  301. strcpy(card->shortname, "CS5535 Audio");
  302. sprintf(card->longname, "%s %s at 0x%lx, irq %i",
  303. card->shortname, card->driver,
  304. cs5535au->port, cs5535au->irq);
  305. if ((err = snd_card_register(card)) < 0)
  306. goto probefail_out;
  307. pci_set_drvdata(pci, card);
  308. dev++;
  309. return 0;
  310. probefail_out:
  311. snd_card_free(card);
  312. return err;
  313. }
  314. static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)
  315. {
  316. snd_card_free(pci_get_drvdata(pci));
  317. pci_set_drvdata(pci, NULL);
  318. }
  319. static struct pci_driver driver = {
  320. .name = DRIVER_NAME,
  321. .id_table = snd_cs5535audio_ids,
  322. .probe = snd_cs5535audio_probe,
  323. .remove = __devexit_p(snd_cs5535audio_remove),
  324. };
  325. static int __init alsa_card_cs5535audio_init(void)
  326. {
  327. return pci_register_driver(&driver);
  328. }
  329. static void __exit alsa_card_cs5535audio_exit(void)
  330. {
  331. pci_unregister_driver(&driver);
  332. }
  333. module_init(alsa_card_cs5535audio_init)
  334. module_exit(alsa_card_cs5535audio_exit)
  335. MODULE_AUTHOR("Jaya Kumar");
  336. MODULE_LICENSE("GPL");
  337. MODULE_DESCRIPTION("CS5535 Audio");
  338. MODULE_SUPPORTED_DEVICE("CS5535 Audio");