psc-ac97.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Au12x0/Au1550 PSC ALSA ASoC audio support.
  3. *
  4. * (c) 2007-2009 MSC Vertriebsges.m.b.H.,
  5. * Manuel Lauss <manuel.lauss@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Au1xxx-PSC AC97 glue.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/mutex.h>
  20. #include <linux/suspend.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/initval.h>
  24. #include <sound/soc.h>
  25. #include <asm/mach-au1x00/au1000.h>
  26. #include <asm/mach-au1x00/au1xxx_psc.h>
  27. #include "psc.h"
  28. /* how often to retry failed codec register reads/writes */
  29. #define AC97_RW_RETRIES 5
  30. #define AC97_DIR \
  31. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  32. #define AC97_RATES \
  33. SNDRV_PCM_RATE_8000_48000
  34. #define AC97_FMTS \
  35. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3BE)
  36. #define AC97PCR_START(stype) \
  37. ((stype) == PCM_TX ? PSC_AC97PCR_TS : PSC_AC97PCR_RS)
  38. #define AC97PCR_STOP(stype) \
  39. ((stype) == PCM_TX ? PSC_AC97PCR_TP : PSC_AC97PCR_RP)
  40. #define AC97PCR_CLRFIFO(stype) \
  41. ((stype) == PCM_TX ? PSC_AC97PCR_TC : PSC_AC97PCR_RC)
  42. #define AC97STAT_BUSY(stype) \
  43. ((stype) == PCM_TX ? PSC_AC97STAT_TB : PSC_AC97STAT_RB)
  44. /* instance data. There can be only one, MacLeod!!!! */
  45. static struct au1xpsc_audio_data *au1xpsc_ac97_workdata;
  46. #if 0
  47. /* this could theoretically work, but ac97->bus->card->private_data can be NULL
  48. * when snd_ac97_mixer() is called; I don't know if the rest further down the
  49. * chain are always valid either.
  50. */
  51. static inline struct au1xpsc_audio_data *ac97_to_pscdata(struct snd_ac97 *x)
  52. {
  53. struct snd_soc_card *c = x->bus->card->private_data;
  54. return snd_soc_dai_get_drvdata(c->rtd->cpu_dai);
  55. }
  56. #else
  57. #define ac97_to_pscdata(x) au1xpsc_ac97_workdata
  58. #endif
  59. /* AC97 controller reads codec register */
  60. static unsigned short au1xpsc_ac97_read(struct snd_ac97 *ac97,
  61. unsigned short reg)
  62. {
  63. struct au1xpsc_audio_data *pscdata = ac97_to_pscdata(ac97);
  64. unsigned short retry, tmo;
  65. unsigned long data;
  66. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  67. au_sync();
  68. retry = AC97_RW_RETRIES;
  69. do {
  70. mutex_lock(&pscdata->lock);
  71. au_writel(PSC_AC97CDC_RD | PSC_AC97CDC_INDX(reg),
  72. AC97_CDC(pscdata));
  73. au_sync();
  74. tmo = 20;
  75. do {
  76. udelay(21);
  77. if (au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)
  78. break;
  79. } while (--tmo);
  80. data = au_readl(AC97_CDC(pscdata));
  81. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  82. au_sync();
  83. mutex_unlock(&pscdata->lock);
  84. if (reg != ((data >> 16) & 0x7f))
  85. tmo = 1; /* wrong register, try again */
  86. } while (--retry && !tmo);
  87. return retry ? data & 0xffff : 0xffff;
  88. }
  89. /* AC97 controller writes to codec register */
  90. static void au1xpsc_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  91. unsigned short val)
  92. {
  93. struct au1xpsc_audio_data *pscdata = ac97_to_pscdata(ac97);
  94. unsigned int tmo, retry;
  95. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  96. au_sync();
  97. retry = AC97_RW_RETRIES;
  98. do {
  99. mutex_lock(&pscdata->lock);
  100. au_writel(PSC_AC97CDC_INDX(reg) | (val & 0xffff),
  101. AC97_CDC(pscdata));
  102. au_sync();
  103. tmo = 20;
  104. do {
  105. udelay(21);
  106. if (au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)
  107. break;
  108. } while (--tmo);
  109. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  110. au_sync();
  111. mutex_unlock(&pscdata->lock);
  112. } while (--retry && !tmo);
  113. }
  114. /* AC97 controller asserts a warm reset */
  115. static void au1xpsc_ac97_warm_reset(struct snd_ac97 *ac97)
  116. {
  117. struct au1xpsc_audio_data *pscdata = ac97_to_pscdata(ac97);
  118. au_writel(PSC_AC97RST_SNC, AC97_RST(pscdata));
  119. au_sync();
  120. msleep(10);
  121. au_writel(0, AC97_RST(pscdata));
  122. au_sync();
  123. }
  124. static void au1xpsc_ac97_cold_reset(struct snd_ac97 *ac97)
  125. {
  126. struct au1xpsc_audio_data *pscdata = ac97_to_pscdata(ac97);
  127. int i;
  128. /* disable PSC during cold reset */
  129. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  130. au_sync();
  131. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(pscdata));
  132. au_sync();
  133. /* issue cold reset */
  134. au_writel(PSC_AC97RST_RST, AC97_RST(pscdata));
  135. au_sync();
  136. msleep(500);
  137. au_writel(0, AC97_RST(pscdata));
  138. au_sync();
  139. /* enable PSC */
  140. au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
  141. au_sync();
  142. /* wait for PSC to indicate it's ready */
  143. i = 1000;
  144. while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_SR)) && (--i))
  145. msleep(1);
  146. if (i == 0) {
  147. printk(KERN_ERR "au1xpsc-ac97: PSC not ready!\n");
  148. return;
  149. }
  150. /* enable the ac97 function */
  151. au_writel(pscdata->cfg | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  152. au_sync();
  153. /* wait for AC97 core to become ready */
  154. i = 1000;
  155. while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)) && (--i))
  156. msleep(1);
  157. if (i == 0)
  158. printk(KERN_ERR "au1xpsc-ac97: AC97 ctrl not ready\n");
  159. }
  160. /* AC97 controller operations */
  161. struct snd_ac97_bus_ops soc_ac97_ops = {
  162. .read = au1xpsc_ac97_read,
  163. .write = au1xpsc_ac97_write,
  164. .reset = au1xpsc_ac97_cold_reset,
  165. .warm_reset = au1xpsc_ac97_warm_reset,
  166. };
  167. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  168. static int au1xpsc_ac97_hw_params(struct snd_pcm_substream *substream,
  169. struct snd_pcm_hw_params *params,
  170. struct snd_soc_dai *dai)
  171. {
  172. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  173. unsigned long r, ro, stat;
  174. int chans, t, stype = SUBSTREAM_TYPE(substream);
  175. chans = params_channels(params);
  176. r = ro = au_readl(AC97_CFG(pscdata));
  177. stat = au_readl(AC97_STAT(pscdata));
  178. /* already active? */
  179. if (stat & (PSC_AC97STAT_TB | PSC_AC97STAT_RB)) {
  180. /* reject parameters not currently set up */
  181. if ((PSC_AC97CFG_GET_LEN(r) != params->msbits) ||
  182. (pscdata->rate != params_rate(params)))
  183. return -EINVAL;
  184. } else {
  185. /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
  186. r &= ~PSC_AC97CFG_LEN_MASK;
  187. r |= PSC_AC97CFG_SET_LEN(params->msbits);
  188. /* channels: enable slots for front L/R channel */
  189. if (stype == PCM_TX) {
  190. r &= ~PSC_AC97CFG_TXSLOT_MASK;
  191. r |= PSC_AC97CFG_TXSLOT_ENA(3);
  192. r |= PSC_AC97CFG_TXSLOT_ENA(4);
  193. } else {
  194. r &= ~PSC_AC97CFG_RXSLOT_MASK;
  195. r |= PSC_AC97CFG_RXSLOT_ENA(3);
  196. r |= PSC_AC97CFG_RXSLOT_ENA(4);
  197. }
  198. /* do we need to poke the hardware? */
  199. if (!(r ^ ro))
  200. goto out;
  201. /* ac97 engine is about to be disabled */
  202. mutex_lock(&pscdata->lock);
  203. /* disable AC97 device controller first... */
  204. au_writel(r & ~PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  205. au_sync();
  206. /* ...wait for it... */
  207. t = 100;
  208. while ((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR) && --t)
  209. msleep(1);
  210. if (!t)
  211. printk(KERN_ERR "PSC-AC97: can't disable!\n");
  212. /* ...write config... */
  213. au_writel(r, AC97_CFG(pscdata));
  214. au_sync();
  215. /* ...enable the AC97 controller again... */
  216. au_writel(r | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  217. au_sync();
  218. /* ...and wait for ready bit */
  219. t = 100;
  220. while ((!(au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)) && --t)
  221. msleep(1);
  222. if (!t)
  223. printk(KERN_ERR "PSC-AC97: can't enable!\n");
  224. mutex_unlock(&pscdata->lock);
  225. pscdata->cfg = r;
  226. pscdata->rate = params_rate(params);
  227. }
  228. out:
  229. return 0;
  230. }
  231. static int au1xpsc_ac97_trigger(struct snd_pcm_substream *substream,
  232. int cmd, struct snd_soc_dai *dai)
  233. {
  234. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  235. int ret, stype = SUBSTREAM_TYPE(substream);
  236. ret = 0;
  237. switch (cmd) {
  238. case SNDRV_PCM_TRIGGER_START:
  239. case SNDRV_PCM_TRIGGER_RESUME:
  240. au_writel(AC97PCR_CLRFIFO(stype), AC97_PCR(pscdata));
  241. au_sync();
  242. au_writel(AC97PCR_START(stype), AC97_PCR(pscdata));
  243. au_sync();
  244. break;
  245. case SNDRV_PCM_TRIGGER_STOP:
  246. case SNDRV_PCM_TRIGGER_SUSPEND:
  247. au_writel(AC97PCR_STOP(stype), AC97_PCR(pscdata));
  248. au_sync();
  249. while (au_readl(AC97_STAT(pscdata)) & AC97STAT_BUSY(stype))
  250. asm volatile ("nop");
  251. au_writel(AC97PCR_CLRFIFO(stype), AC97_PCR(pscdata));
  252. au_sync();
  253. break;
  254. default:
  255. ret = -EINVAL;
  256. }
  257. return ret;
  258. }
  259. static int au1xpsc_ac97_probe(struct snd_soc_dai *dai)
  260. {
  261. return au1xpsc_ac97_workdata ? 0 : -ENODEV;
  262. }
  263. static struct snd_soc_dai_ops au1xpsc_ac97_dai_ops = {
  264. .trigger = au1xpsc_ac97_trigger,
  265. .hw_params = au1xpsc_ac97_hw_params,
  266. };
  267. static const struct snd_soc_dai_driver au1xpsc_ac97_dai_template = {
  268. .ac97_control = 1,
  269. .probe = au1xpsc_ac97_probe,
  270. .playback = {
  271. .rates = AC97_RATES,
  272. .formats = AC97_FMTS,
  273. .channels_min = 2,
  274. .channels_max = 2,
  275. },
  276. .capture = {
  277. .rates = AC97_RATES,
  278. .formats = AC97_FMTS,
  279. .channels_min = 2,
  280. .channels_max = 2,
  281. },
  282. .ops = &au1xpsc_ac97_dai_ops,
  283. };
  284. static int __devinit au1xpsc_ac97_drvprobe(struct platform_device *pdev)
  285. {
  286. int ret;
  287. struct resource *r;
  288. unsigned long sel;
  289. struct au1xpsc_audio_data *wd;
  290. wd = kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
  291. if (!wd)
  292. return -ENOMEM;
  293. mutex_init(&wd->lock);
  294. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. if (!r) {
  296. ret = -ENODEV;
  297. goto out0;
  298. }
  299. ret = -EBUSY;
  300. if (!request_mem_region(r->start, resource_size(r), pdev->name))
  301. goto out0;
  302. wd->mmio = ioremap(r->start, resource_size(r));
  303. if (!wd->mmio)
  304. goto out1;
  305. /* configuration: max dma trigger threshold, enable ac97 */
  306. wd->cfg = PSC_AC97CFG_RT_FIFO8 | PSC_AC97CFG_TT_FIFO8 |
  307. PSC_AC97CFG_DE_ENABLE;
  308. /* preserve PSC clock source set up by platform */
  309. sel = au_readl(PSC_SEL(wd)) & PSC_SEL_CLK_MASK;
  310. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  311. au_sync();
  312. au_writel(0, PSC_SEL(wd));
  313. au_sync();
  314. au_writel(PSC_SEL_PS_AC97MODE | sel, PSC_SEL(wd));
  315. au_sync();
  316. /* name the DAI like this device instance ("au1xpsc-ac97.PSCINDEX") */
  317. memcpy(&wd->dai_drv, &au1xpsc_ac97_dai_template,
  318. sizeof(struct snd_soc_dai_driver));
  319. wd->dai_drv.name = dev_name(&pdev->dev);
  320. platform_set_drvdata(pdev, wd);
  321. ret = snd_soc_register_dai(&pdev->dev, &wd->dai_drv);
  322. if (ret)
  323. goto out1;
  324. wd->dmapd = au1xpsc_pcm_add(pdev);
  325. if (wd->dmapd) {
  326. au1xpsc_ac97_workdata = wd;
  327. return 0;
  328. }
  329. snd_soc_unregister_dai(&pdev->dev);
  330. out1:
  331. release_mem_region(r->start, resource_size(r));
  332. out0:
  333. kfree(wd);
  334. return ret;
  335. }
  336. static int __devexit au1xpsc_ac97_drvremove(struct platform_device *pdev)
  337. {
  338. struct au1xpsc_audio_data *wd = platform_get_drvdata(pdev);
  339. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  340. if (wd->dmapd)
  341. au1xpsc_pcm_destroy(wd->dmapd);
  342. snd_soc_unregister_dai(&pdev->dev);
  343. /* disable PSC completely */
  344. au_writel(0, AC97_CFG(wd));
  345. au_sync();
  346. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  347. au_sync();
  348. iounmap(wd->mmio);
  349. release_mem_region(r->start, resource_size(r));
  350. kfree(wd);
  351. au1xpsc_ac97_workdata = NULL; /* MDEV */
  352. return 0;
  353. }
  354. #ifdef CONFIG_PM
  355. static int au1xpsc_ac97_drvsuspend(struct device *dev)
  356. {
  357. struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
  358. /* save interesting registers and disable PSC */
  359. wd->pm[0] = au_readl(PSC_SEL(wd));
  360. au_writel(0, AC97_CFG(wd));
  361. au_sync();
  362. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  363. au_sync();
  364. return 0;
  365. }
  366. static int au1xpsc_ac97_drvresume(struct device *dev)
  367. {
  368. struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
  369. /* restore PSC clock config */
  370. au_writel(wd->pm[0] | PSC_SEL_PS_AC97MODE, PSC_SEL(wd));
  371. au_sync();
  372. /* after this point the ac97 core will cold-reset the codec.
  373. * During cold-reset the PSC is reinitialized and the last
  374. * configuration set up in hw_params() is restored.
  375. */
  376. return 0;
  377. }
  378. static struct dev_pm_ops au1xpscac97_pmops = {
  379. .suspend = au1xpsc_ac97_drvsuspend,
  380. .resume = au1xpsc_ac97_drvresume,
  381. };
  382. #define AU1XPSCAC97_PMOPS &au1xpscac97_pmops
  383. #else
  384. #define AU1XPSCAC97_PMOPS NULL
  385. #endif
  386. static struct platform_driver au1xpsc_ac97_driver = {
  387. .driver = {
  388. .name = "au1xpsc_ac97",
  389. .owner = THIS_MODULE,
  390. .pm = AU1XPSCAC97_PMOPS,
  391. },
  392. .probe = au1xpsc_ac97_drvprobe,
  393. .remove = __devexit_p(au1xpsc_ac97_drvremove),
  394. };
  395. static int __init au1xpsc_ac97_load(void)
  396. {
  397. au1xpsc_ac97_workdata = NULL;
  398. return platform_driver_register(&au1xpsc_ac97_driver);
  399. }
  400. static void __exit au1xpsc_ac97_unload(void)
  401. {
  402. platform_driver_unregister(&au1xpsc_ac97_driver);
  403. }
  404. module_init(au1xpsc_ac97_load);
  405. module_exit(au1xpsc_ac97_unload);
  406. MODULE_LICENSE("GPL");
  407. MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
  408. MODULE_AUTHOR("Manuel Lauss");