cs5535audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/module.h>
  29. #include <linux/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 const struct ac97_quirk ac97_quirks[] = {
  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 bool 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 const 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. dev_err(cs5535au->card->dev,
  79. "Failure writing to cs5535 codec\n");
  80. }
  81. static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
  82. unsigned short reg)
  83. {
  84. unsigned int regdata;
  85. unsigned int timeout;
  86. unsigned int val;
  87. regdata = ((unsigned int) reg) << 24;
  88. regdata |= ACC_CODEC_CNTL_RD_CMD;
  89. regdata |= CMD_NEW;
  90. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  91. wait_till_cmd_acked(cs5535au, 50);
  92. timeout = 50;
  93. do {
  94. val = cs_readl(cs5535au, ACC_CODEC_STATUS);
  95. if ((val & STS_NEW) && reg == (val >> 24))
  96. break;
  97. udelay(1);
  98. } while (--timeout);
  99. if (!timeout)
  100. dev_err(cs5535au->card->dev,
  101. "Failure reading codec reg 0x%x, Last value=0x%x\n",
  102. reg, val);
  103. return (unsigned short) val;
  104. }
  105. static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
  106. unsigned short reg, unsigned short val)
  107. {
  108. unsigned int regdata;
  109. regdata = ((unsigned int) reg) << 24;
  110. regdata |= val;
  111. regdata &= CMD_MASK;
  112. regdata |= CMD_NEW;
  113. regdata &= ACC_CODEC_CNTL_WR_CMD;
  114. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  115. wait_till_cmd_acked(cs5535au, 50);
  116. }
  117. static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
  118. unsigned short reg, unsigned short val)
  119. {
  120. struct cs5535audio *cs5535au = ac97->private_data;
  121. snd_cs5535audio_codec_write(cs5535au, reg, val);
  122. }
  123. static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
  124. unsigned short reg)
  125. {
  126. struct cs5535audio *cs5535au = ac97->private_data;
  127. return snd_cs5535audio_codec_read(cs5535au, reg);
  128. }
  129. static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
  130. {
  131. struct snd_card *card = cs5535au->card;
  132. struct snd_ac97_bus *pbus;
  133. struct snd_ac97_template ac97;
  134. int err;
  135. static struct snd_ac97_bus_ops ops = {
  136. .write = snd_cs5535audio_ac97_codec_write,
  137. .read = snd_cs5535audio_ac97_codec_read,
  138. };
  139. if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
  140. return err;
  141. memset(&ac97, 0, sizeof(ac97));
  142. ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM
  143. | AC97_SCAP_POWER_SAVE;
  144. ac97.private_data = cs5535au;
  145. ac97.pci = cs5535au->pci;
  146. /* set any OLPC-specific scaps */
  147. olpc_prequirks(card, &ac97);
  148. if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
  149. dev_err(card->dev, "mixer failed\n");
  150. return err;
  151. }
  152. snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
  153. err = olpc_quirks(card, cs5535au->ac97);
  154. if (err < 0) {
  155. dev_err(card->dev, "olpc quirks failed\n");
  156. return err;
  157. }
  158. return 0;
  159. }
  160. static void process_bm0_irq(struct cs5535audio *cs5535au)
  161. {
  162. u8 bm_stat;
  163. spin_lock(&cs5535au->reg_lock);
  164. bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
  165. spin_unlock(&cs5535au->reg_lock);
  166. if (bm_stat & EOP) {
  167. struct cs5535audio_dma *dma;
  168. dma = cs5535au->playback_substream->runtime->private_data;
  169. snd_pcm_period_elapsed(cs5535au->playback_substream);
  170. } else {
  171. dev_err(cs5535au->card->dev,
  172. "unexpected bm0 irq src, bm_stat=%x\n",
  173. bm_stat);
  174. }
  175. }
  176. static void process_bm1_irq(struct cs5535audio *cs5535au)
  177. {
  178. u8 bm_stat;
  179. spin_lock(&cs5535au->reg_lock);
  180. bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
  181. spin_unlock(&cs5535au->reg_lock);
  182. if (bm_stat & EOP) {
  183. struct cs5535audio_dma *dma;
  184. dma = cs5535au->capture_substream->runtime->private_data;
  185. snd_pcm_period_elapsed(cs5535au->capture_substream);
  186. }
  187. }
  188. static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
  189. {
  190. u16 acc_irq_stat;
  191. unsigned char count;
  192. struct cs5535audio *cs5535au = dev_id;
  193. if (cs5535au == NULL)
  194. return IRQ_NONE;
  195. acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
  196. if (!acc_irq_stat)
  197. return IRQ_NONE;
  198. for (count = 0; count < 4; count++) {
  199. if (acc_irq_stat & (1 << count)) {
  200. switch (count) {
  201. case IRQ_STS:
  202. cs_readl(cs5535au, ACC_GPIO_STATUS);
  203. break;
  204. case WU_IRQ_STS:
  205. cs_readl(cs5535au, ACC_GPIO_STATUS);
  206. break;
  207. case BM0_IRQ_STS:
  208. process_bm0_irq(cs5535au);
  209. break;
  210. case BM1_IRQ_STS:
  211. process_bm1_irq(cs5535au);
  212. break;
  213. default:
  214. dev_err(cs5535au->card->dev,
  215. "Unexpected irq src: 0x%x\n",
  216. acc_irq_stat);
  217. break;
  218. }
  219. }
  220. }
  221. return IRQ_HANDLED;
  222. }
  223. static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
  224. {
  225. synchronize_irq(cs5535au->irq);
  226. pci_set_power_state(cs5535au->pci, PCI_D3hot);
  227. if (cs5535au->irq >= 0)
  228. free_irq(cs5535au->irq, cs5535au);
  229. pci_release_regions(cs5535au->pci);
  230. pci_disable_device(cs5535au->pci);
  231. kfree(cs5535au);
  232. return 0;
  233. }
  234. static int snd_cs5535audio_dev_free(struct snd_device *device)
  235. {
  236. struct cs5535audio *cs5535au = device->device_data;
  237. return snd_cs5535audio_free(cs5535au);
  238. }
  239. static int snd_cs5535audio_create(struct snd_card *card,
  240. struct pci_dev *pci,
  241. struct cs5535audio **rcs5535au)
  242. {
  243. struct cs5535audio *cs5535au;
  244. int err;
  245. static struct snd_device_ops ops = {
  246. .dev_free = snd_cs5535audio_dev_free,
  247. };
  248. *rcs5535au = NULL;
  249. if ((err = pci_enable_device(pci)) < 0)
  250. return err;
  251. if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0 ||
  252. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
  253. dev_warn(card->dev, "unable to get 32bit dma\n");
  254. err = -ENXIO;
  255. goto pcifail;
  256. }
  257. cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
  258. if (cs5535au == NULL) {
  259. err = -ENOMEM;
  260. goto pcifail;
  261. }
  262. spin_lock_init(&cs5535au->reg_lock);
  263. cs5535au->card = card;
  264. cs5535au->pci = pci;
  265. cs5535au->irq = -1;
  266. if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
  267. kfree(cs5535au);
  268. goto pcifail;
  269. }
  270. cs5535au->port = pci_resource_start(pci, 0);
  271. if (request_irq(pci->irq, snd_cs5535audio_interrupt,
  272. IRQF_SHARED, KBUILD_MODNAME, cs5535au)) {
  273. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  274. err = -EBUSY;
  275. goto sndfail;
  276. }
  277. cs5535au->irq = pci->irq;
  278. pci_set_master(pci);
  279. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  280. cs5535au, &ops)) < 0)
  281. goto sndfail;
  282. *rcs5535au = cs5535au;
  283. return 0;
  284. sndfail: /* leave the device alive, just kill the snd */
  285. snd_cs5535audio_free(cs5535au);
  286. return err;
  287. pcifail:
  288. pci_disable_device(pci);
  289. return err;
  290. }
  291. static int snd_cs5535audio_probe(struct pci_dev *pci,
  292. const struct pci_device_id *pci_id)
  293. {
  294. static int dev;
  295. struct snd_card *card;
  296. struct cs5535audio *cs5535au;
  297. int err;
  298. if (dev >= SNDRV_CARDS)
  299. return -ENODEV;
  300. if (!enable[dev]) {
  301. dev++;
  302. return -ENOENT;
  303. }
  304. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  305. 0, &card);
  306. if (err < 0)
  307. return err;
  308. if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
  309. goto probefail_out;
  310. card->private_data = cs5535au;
  311. if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
  312. goto probefail_out;
  313. if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
  314. goto probefail_out;
  315. strcpy(card->driver, DRIVER_NAME);
  316. strcpy(card->shortname, "CS5535 Audio");
  317. sprintf(card->longname, "%s %s at 0x%lx, irq %i",
  318. card->shortname, card->driver,
  319. cs5535au->port, cs5535au->irq);
  320. if ((err = snd_card_register(card)) < 0)
  321. goto probefail_out;
  322. pci_set_drvdata(pci, card);
  323. dev++;
  324. return 0;
  325. probefail_out:
  326. snd_card_free(card);
  327. return err;
  328. }
  329. static void snd_cs5535audio_remove(struct pci_dev *pci)
  330. {
  331. olpc_quirks_cleanup();
  332. snd_card_free(pci_get_drvdata(pci));
  333. }
  334. static struct pci_driver cs5535audio_driver = {
  335. .name = KBUILD_MODNAME,
  336. .id_table = snd_cs5535audio_ids,
  337. .probe = snd_cs5535audio_probe,
  338. .remove = snd_cs5535audio_remove,
  339. #ifdef CONFIG_PM_SLEEP
  340. .driver = {
  341. .pm = &snd_cs5535audio_pm,
  342. },
  343. #endif
  344. };
  345. module_pci_driver(cs5535audio_driver);
  346. MODULE_AUTHOR("Jaya Kumar");
  347. MODULE_LICENSE("GPL");
  348. MODULE_DESCRIPTION("CS5535 Audio");
  349. MODULE_SUPPORTED_DEVICE("CS5535 Audio");