cs5535audio.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for audio on multifunction CS5535/6 companion device
  4. * Copyright (C) Jaya Kumar
  5. *
  6. * Based on Jaroslav Kysela and Takashi Iwai's examples.
  7. * This work was sponsored by CIS(M) Sdn Bhd.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/pci.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <sound/core.h>
  17. #include <sound/control.h>
  18. #include <sound/pcm.h>
  19. #include <sound/rawmidi.h>
  20. #include <sound/ac97_codec.h>
  21. #include <sound/initval.h>
  22. #include <sound/asoundef.h>
  23. #include "cs5535audio.h"
  24. #define DRIVER_NAME "cs5535audio"
  25. static char *ac97_quirk;
  26. module_param(ac97_quirk, charp, 0444);
  27. MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
  28. static const struct ac97_quirk ac97_quirks[] = {
  29. #if 0 /* Not yet confirmed if all 5536 boards are HP only */
  30. {
  31. .subvendor = PCI_VENDOR_ID_AMD,
  32. .subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
  33. .name = "AMD RDK",
  34. .type = AC97_TUNE_HP_ONLY
  35. },
  36. #endif
  37. {}
  38. };
  39. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  40. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  41. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  42. module_param_array(index, int, NULL, 0444);
  43. MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
  44. module_param_array(id, charp, NULL, 0444);
  45. MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
  46. module_param_array(enable, bool, NULL, 0444);
  47. MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);
  48. static const struct pci_device_id snd_cs5535audio_ids[] = {
  49. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
  50. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
  51. {}
  52. };
  53. MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
  54. static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
  55. {
  56. unsigned int tmp;
  57. do {
  58. tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
  59. if (!(tmp & CMD_NEW))
  60. break;
  61. udelay(1);
  62. } while (--timeout);
  63. if (!timeout)
  64. dev_err(cs5535au->card->dev,
  65. "Failure writing to cs5535 codec\n");
  66. }
  67. static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
  68. unsigned short reg)
  69. {
  70. unsigned int regdata;
  71. unsigned int timeout;
  72. unsigned int val;
  73. regdata = ((unsigned int) reg) << 24;
  74. regdata |= ACC_CODEC_CNTL_RD_CMD;
  75. regdata |= CMD_NEW;
  76. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  77. wait_till_cmd_acked(cs5535au, 50);
  78. timeout = 50;
  79. do {
  80. val = cs_readl(cs5535au, ACC_CODEC_STATUS);
  81. if ((val & STS_NEW) && reg == (val >> 24))
  82. break;
  83. udelay(1);
  84. } while (--timeout);
  85. if (!timeout)
  86. dev_err(cs5535au->card->dev,
  87. "Failure reading codec reg 0x%x, Last value=0x%x\n",
  88. reg, val);
  89. return (unsigned short) val;
  90. }
  91. static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
  92. unsigned short reg, unsigned short val)
  93. {
  94. unsigned int regdata;
  95. regdata = ((unsigned int) reg) << 24;
  96. regdata |= val;
  97. regdata &= CMD_MASK;
  98. regdata |= CMD_NEW;
  99. regdata &= ACC_CODEC_CNTL_WR_CMD;
  100. cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
  101. wait_till_cmd_acked(cs5535au, 50);
  102. }
  103. static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
  104. unsigned short reg, unsigned short val)
  105. {
  106. struct cs5535audio *cs5535au = ac97->private_data;
  107. snd_cs5535audio_codec_write(cs5535au, reg, val);
  108. }
  109. static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
  110. unsigned short reg)
  111. {
  112. struct cs5535audio *cs5535au = ac97->private_data;
  113. return snd_cs5535audio_codec_read(cs5535au, reg);
  114. }
  115. static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
  116. {
  117. struct snd_card *card = cs5535au->card;
  118. struct snd_ac97_bus *pbus;
  119. struct snd_ac97_template ac97;
  120. int err;
  121. static const struct snd_ac97_bus_ops ops = {
  122. .write = snd_cs5535audio_ac97_codec_write,
  123. .read = snd_cs5535audio_ac97_codec_read,
  124. };
  125. err = snd_ac97_bus(card, 0, &ops, NULL, &pbus);
  126. if (err < 0)
  127. return err;
  128. memset(&ac97, 0, sizeof(ac97));
  129. ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM
  130. | AC97_SCAP_POWER_SAVE;
  131. ac97.private_data = cs5535au;
  132. ac97.pci = cs5535au->pci;
  133. /* set any OLPC-specific scaps */
  134. olpc_prequirks(card, &ac97);
  135. err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97);
  136. if (err < 0) {
  137. dev_err(card->dev, "mixer failed\n");
  138. return err;
  139. }
  140. snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
  141. err = olpc_quirks(card, cs5535au->ac97);
  142. if (err < 0) {
  143. dev_err(card->dev, "olpc quirks failed\n");
  144. return err;
  145. }
  146. return 0;
  147. }
  148. static void process_bm0_irq(struct cs5535audio *cs5535au)
  149. {
  150. u8 bm_stat;
  151. spin_lock(&cs5535au->reg_lock);
  152. bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
  153. spin_unlock(&cs5535au->reg_lock);
  154. if (bm_stat & EOP) {
  155. snd_pcm_period_elapsed(cs5535au->playback_substream);
  156. } else {
  157. dev_err(cs5535au->card->dev,
  158. "unexpected bm0 irq src, bm_stat=%x\n",
  159. bm_stat);
  160. }
  161. }
  162. static void process_bm1_irq(struct cs5535audio *cs5535au)
  163. {
  164. u8 bm_stat;
  165. spin_lock(&cs5535au->reg_lock);
  166. bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
  167. spin_unlock(&cs5535au->reg_lock);
  168. if (bm_stat & EOP)
  169. snd_pcm_period_elapsed(cs5535au->capture_substream);
  170. }
  171. static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
  172. {
  173. u16 acc_irq_stat;
  174. unsigned char count;
  175. struct cs5535audio *cs5535au = dev_id;
  176. if (cs5535au == NULL)
  177. return IRQ_NONE;
  178. acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
  179. if (!acc_irq_stat)
  180. return IRQ_NONE;
  181. for (count = 0; count < 4; count++) {
  182. if (acc_irq_stat & (1 << count)) {
  183. switch (count) {
  184. case IRQ_STS:
  185. cs_readl(cs5535au, ACC_GPIO_STATUS);
  186. break;
  187. case WU_IRQ_STS:
  188. cs_readl(cs5535au, ACC_GPIO_STATUS);
  189. break;
  190. case BM0_IRQ_STS:
  191. process_bm0_irq(cs5535au);
  192. break;
  193. case BM1_IRQ_STS:
  194. process_bm1_irq(cs5535au);
  195. break;
  196. default:
  197. dev_err(cs5535au->card->dev,
  198. "Unexpected irq src: 0x%x\n",
  199. acc_irq_stat);
  200. break;
  201. }
  202. }
  203. }
  204. return IRQ_HANDLED;
  205. }
  206. static void snd_cs5535audio_free(struct snd_card *card)
  207. {
  208. olpc_quirks_cleanup();
  209. }
  210. static int snd_cs5535audio_create(struct snd_card *card,
  211. struct pci_dev *pci)
  212. {
  213. struct cs5535audio *cs5535au = card->private_data;
  214. int err;
  215. err = pcim_enable_device(pci);
  216. if (err < 0)
  217. return err;
  218. if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
  219. dev_warn(card->dev, "unable to get 32bit dma\n");
  220. return -ENXIO;
  221. }
  222. spin_lock_init(&cs5535au->reg_lock);
  223. cs5535au->card = card;
  224. cs5535au->pci = pci;
  225. cs5535au->irq = -1;
  226. err = pci_request_regions(pci, "CS5535 Audio");
  227. if (err < 0)
  228. return err;
  229. cs5535au->port = pci_resource_start(pci, 0);
  230. if (devm_request_irq(&pci->dev, pci->irq, snd_cs5535audio_interrupt,
  231. IRQF_SHARED, KBUILD_MODNAME, cs5535au)) {
  232. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  233. return -EBUSY;
  234. }
  235. cs5535au->irq = pci->irq;
  236. card->sync_irq = cs5535au->irq;
  237. pci_set_master(pci);
  238. return 0;
  239. }
  240. static int __snd_cs5535audio_probe(struct pci_dev *pci,
  241. const struct pci_device_id *pci_id)
  242. {
  243. static int dev;
  244. struct snd_card *card;
  245. struct cs5535audio *cs5535au;
  246. int err;
  247. if (dev >= SNDRV_CARDS)
  248. return -ENODEV;
  249. if (!enable[dev]) {
  250. dev++;
  251. return -ENOENT;
  252. }
  253. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  254. sizeof(*cs5535au), &card);
  255. if (err < 0)
  256. return err;
  257. cs5535au = card->private_data;
  258. card->private_free = snd_cs5535audio_free;
  259. err = snd_cs5535audio_create(card, pci);
  260. if (err < 0)
  261. return err;
  262. err = snd_cs5535audio_mixer(cs5535au);
  263. if (err < 0)
  264. return err;
  265. err = snd_cs5535audio_pcm(cs5535au);
  266. if (err < 0)
  267. return err;
  268. strcpy(card->driver, DRIVER_NAME);
  269. strcpy(card->shortname, "CS5535 Audio");
  270. sprintf(card->longname, "%s %s at 0x%lx, irq %i",
  271. card->shortname, card->driver,
  272. cs5535au->port, cs5535au->irq);
  273. err = snd_card_register(card);
  274. if (err < 0)
  275. return err;
  276. pci_set_drvdata(pci, card);
  277. dev++;
  278. return 0;
  279. }
  280. static int snd_cs5535audio_probe(struct pci_dev *pci,
  281. const struct pci_device_id *pci_id)
  282. {
  283. return snd_card_free_on_error(&pci->dev, __snd_cs5535audio_probe(pci, pci_id));
  284. }
  285. static struct pci_driver cs5535audio_driver = {
  286. .name = KBUILD_MODNAME,
  287. .id_table = snd_cs5535audio_ids,
  288. .probe = snd_cs5535audio_probe,
  289. #ifdef CONFIG_PM_SLEEP
  290. .driver = {
  291. .pm = &snd_cs5535audio_pm,
  292. },
  293. #endif
  294. };
  295. module_pci_driver(cs5535audio_driver);
  296. MODULE_AUTHOR("Jaya Kumar");
  297. MODULE_LICENSE("GPL");
  298. MODULE_DESCRIPTION("CS5535 Audio");