psc-ac97.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_AC97PCR_TS : PSC_AC97PCR_RS)
  38. #define AC97PCR_STOP(stype) \
  39. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_AC97PCR_TP : PSC_AC97PCR_RP)
  40. #define AC97PCR_CLRFIFO(stype) \
  41. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_AC97PCR_TC : PSC_AC97PCR_RC)
  42. #define AC97STAT_BUSY(stype) \
  43. ((stype) == SNDRV_PCM_STREAM_PLAYBACK ? 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. __raw_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  67. wmb(); /* drain writebuffer */
  68. retry = AC97_RW_RETRIES;
  69. do {
  70. mutex_lock(&pscdata->lock);
  71. __raw_writel(PSC_AC97CDC_RD | PSC_AC97CDC_INDX(reg),
  72. AC97_CDC(pscdata));
  73. wmb(); /* drain writebuffer */
  74. tmo = 20;
  75. do {
  76. udelay(21);
  77. if (__raw_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)
  78. break;
  79. } while (--tmo);
  80. data = __raw_readl(AC97_CDC(pscdata));
  81. __raw_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  82. wmb(); /* drain writebuffer */
  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. __raw_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  96. wmb(); /* drain writebuffer */
  97. retry = AC97_RW_RETRIES;
  98. do {
  99. mutex_lock(&pscdata->lock);
  100. __raw_writel(PSC_AC97CDC_INDX(reg) | (val & 0xffff),
  101. AC97_CDC(pscdata));
  102. wmb(); /* drain writebuffer */
  103. tmo = 20;
  104. do {
  105. udelay(21);
  106. if (__raw_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)
  107. break;
  108. } while (--tmo);
  109. __raw_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  110. wmb(); /* drain writebuffer */
  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. __raw_writel(PSC_AC97RST_SNC, AC97_RST(pscdata));
  119. wmb(); /* drain writebuffer */
  120. msleep(10);
  121. __raw_writel(0, AC97_RST(pscdata));
  122. wmb(); /* drain writebuffer */
  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. __raw_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  130. wmb(); /* drain writebuffer */
  131. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(pscdata));
  132. wmb(); /* drain writebuffer */
  133. /* issue cold reset */
  134. __raw_writel(PSC_AC97RST_RST, AC97_RST(pscdata));
  135. wmb(); /* drain writebuffer */
  136. msleep(500);
  137. __raw_writel(0, AC97_RST(pscdata));
  138. wmb(); /* drain writebuffer */
  139. /* enable PSC */
  140. __raw_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
  141. wmb(); /* drain writebuffer */
  142. /* wait for PSC to indicate it's ready */
  143. i = 1000;
  144. while (!((__raw_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. __raw_writel(pscdata->cfg | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  152. wmb(); /* drain writebuffer */
  153. /* wait for AC97 core to become ready */
  154. i = 1000;
  155. while (!((__raw_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. static struct snd_ac97_bus_ops psc_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. static int au1xpsc_ac97_hw_params(struct snd_pcm_substream *substream,
  168. struct snd_pcm_hw_params *params,
  169. struct snd_soc_dai *dai)
  170. {
  171. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  172. unsigned long r, ro, stat;
  173. int chans, t, stype = substream->stream;
  174. chans = params_channels(params);
  175. r = ro = __raw_readl(AC97_CFG(pscdata));
  176. stat = __raw_readl(AC97_STAT(pscdata));
  177. /* already active? */
  178. if (stat & (PSC_AC97STAT_TB | PSC_AC97STAT_RB)) {
  179. /* reject parameters not currently set up */
  180. if ((PSC_AC97CFG_GET_LEN(r) != params->msbits) ||
  181. (pscdata->rate != params_rate(params)))
  182. return -EINVAL;
  183. } else {
  184. /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
  185. r &= ~PSC_AC97CFG_LEN_MASK;
  186. r |= PSC_AC97CFG_SET_LEN(params->msbits);
  187. /* channels: enable slots for front L/R channel */
  188. if (stype == SNDRV_PCM_STREAM_PLAYBACK) {
  189. r &= ~PSC_AC97CFG_TXSLOT_MASK;
  190. r |= PSC_AC97CFG_TXSLOT_ENA(3);
  191. r |= PSC_AC97CFG_TXSLOT_ENA(4);
  192. } else {
  193. r &= ~PSC_AC97CFG_RXSLOT_MASK;
  194. r |= PSC_AC97CFG_RXSLOT_ENA(3);
  195. r |= PSC_AC97CFG_RXSLOT_ENA(4);
  196. }
  197. /* do we need to poke the hardware? */
  198. if (!(r ^ ro))
  199. goto out;
  200. /* ac97 engine is about to be disabled */
  201. mutex_lock(&pscdata->lock);
  202. /* disable AC97 device controller first... */
  203. __raw_writel(r & ~PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  204. wmb(); /* drain writebuffer */
  205. /* ...wait for it... */
  206. t = 100;
  207. while ((__raw_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR) && --t)
  208. msleep(1);
  209. if (!t)
  210. printk(KERN_ERR "PSC-AC97: can't disable!\n");
  211. /* ...write config... */
  212. __raw_writel(r, AC97_CFG(pscdata));
  213. wmb(); /* drain writebuffer */
  214. /* ...enable the AC97 controller again... */
  215. __raw_writel(r | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  216. wmb(); /* drain writebuffer */
  217. /* ...and wait for ready bit */
  218. t = 100;
  219. while ((!(__raw_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)) && --t)
  220. msleep(1);
  221. if (!t)
  222. printk(KERN_ERR "PSC-AC97: can't enable!\n");
  223. mutex_unlock(&pscdata->lock);
  224. pscdata->cfg = r;
  225. pscdata->rate = params_rate(params);
  226. }
  227. out:
  228. return 0;
  229. }
  230. static int au1xpsc_ac97_trigger(struct snd_pcm_substream *substream,
  231. int cmd, struct snd_soc_dai *dai)
  232. {
  233. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  234. int ret, stype = substream->stream;
  235. ret = 0;
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. case SNDRV_PCM_TRIGGER_RESUME:
  239. __raw_writel(AC97PCR_CLRFIFO(stype), AC97_PCR(pscdata));
  240. wmb(); /* drain writebuffer */
  241. __raw_writel(AC97PCR_START(stype), AC97_PCR(pscdata));
  242. wmb(); /* drain writebuffer */
  243. break;
  244. case SNDRV_PCM_TRIGGER_STOP:
  245. case SNDRV_PCM_TRIGGER_SUSPEND:
  246. __raw_writel(AC97PCR_STOP(stype), AC97_PCR(pscdata));
  247. wmb(); /* drain writebuffer */
  248. while (__raw_readl(AC97_STAT(pscdata)) & AC97STAT_BUSY(stype))
  249. asm volatile ("nop");
  250. __raw_writel(AC97PCR_CLRFIFO(stype), AC97_PCR(pscdata));
  251. wmb(); /* drain writebuffer */
  252. break;
  253. default:
  254. ret = -EINVAL;
  255. }
  256. return ret;
  257. }
  258. static int au1xpsc_ac97_startup(struct snd_pcm_substream *substream,
  259. struct snd_soc_dai *dai)
  260. {
  261. struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
  262. snd_soc_dai_set_dma_data(dai, substream, &pscdata->dmaids[0]);
  263. return 0;
  264. }
  265. static int au1xpsc_ac97_probe(struct snd_soc_dai *dai)
  266. {
  267. return au1xpsc_ac97_workdata ? 0 : -ENODEV;
  268. }
  269. static const struct snd_soc_dai_ops au1xpsc_ac97_dai_ops = {
  270. .startup = au1xpsc_ac97_startup,
  271. .trigger = au1xpsc_ac97_trigger,
  272. .hw_params = au1xpsc_ac97_hw_params,
  273. };
  274. static const struct snd_soc_dai_driver au1xpsc_ac97_dai_template = {
  275. .bus_control = true,
  276. .probe = au1xpsc_ac97_probe,
  277. .playback = {
  278. .rates = AC97_RATES,
  279. .formats = AC97_FMTS,
  280. .channels_min = 2,
  281. .channels_max = 2,
  282. },
  283. .capture = {
  284. .rates = AC97_RATES,
  285. .formats = AC97_FMTS,
  286. .channels_min = 2,
  287. .channels_max = 2,
  288. },
  289. .ops = &au1xpsc_ac97_dai_ops,
  290. };
  291. static const struct snd_soc_component_driver au1xpsc_ac97_component = {
  292. .name = "au1xpsc-ac97",
  293. };
  294. static int au1xpsc_ac97_drvprobe(struct platform_device *pdev)
  295. {
  296. int ret;
  297. struct resource *iores, *dmares;
  298. unsigned long sel;
  299. struct au1xpsc_audio_data *wd;
  300. wd = devm_kzalloc(&pdev->dev, sizeof(struct au1xpsc_audio_data),
  301. GFP_KERNEL);
  302. if (!wd)
  303. return -ENOMEM;
  304. mutex_init(&wd->lock);
  305. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  306. wd->mmio = devm_ioremap_resource(&pdev->dev, iores);
  307. if (IS_ERR(wd->mmio))
  308. return PTR_ERR(wd->mmio);
  309. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  310. if (!dmares)
  311. return -EBUSY;
  312. wd->dmaids[SNDRV_PCM_STREAM_PLAYBACK] = dmares->start;
  313. dmares = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  314. if (!dmares)
  315. return -EBUSY;
  316. wd->dmaids[SNDRV_PCM_STREAM_CAPTURE] = dmares->start;
  317. /* configuration: max dma trigger threshold, enable ac97 */
  318. wd->cfg = PSC_AC97CFG_RT_FIFO8 | PSC_AC97CFG_TT_FIFO8 |
  319. PSC_AC97CFG_DE_ENABLE;
  320. /* preserve PSC clock source set up by platform */
  321. sel = __raw_readl(PSC_SEL(wd)) & PSC_SEL_CLK_MASK;
  322. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  323. wmb(); /* drain writebuffer */
  324. __raw_writel(0, PSC_SEL(wd));
  325. wmb(); /* drain writebuffer */
  326. __raw_writel(PSC_SEL_PS_AC97MODE | sel, PSC_SEL(wd));
  327. wmb(); /* drain writebuffer */
  328. /* name the DAI like this device instance ("au1xpsc-ac97.PSCINDEX") */
  329. memcpy(&wd->dai_drv, &au1xpsc_ac97_dai_template,
  330. sizeof(struct snd_soc_dai_driver));
  331. wd->dai_drv.name = dev_name(&pdev->dev);
  332. platform_set_drvdata(pdev, wd);
  333. ret = snd_soc_set_ac97_ops(&psc_ac97_ops);
  334. if (ret)
  335. return ret;
  336. ret = snd_soc_register_component(&pdev->dev, &au1xpsc_ac97_component,
  337. &wd->dai_drv, 1);
  338. if (ret)
  339. return ret;
  340. au1xpsc_ac97_workdata = wd;
  341. return 0;
  342. }
  343. static int au1xpsc_ac97_drvremove(struct platform_device *pdev)
  344. {
  345. struct au1xpsc_audio_data *wd = platform_get_drvdata(pdev);
  346. snd_soc_unregister_component(&pdev->dev);
  347. /* disable PSC completely */
  348. __raw_writel(0, AC97_CFG(wd));
  349. wmb(); /* drain writebuffer */
  350. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  351. wmb(); /* drain writebuffer */
  352. au1xpsc_ac97_workdata = NULL; /* MDEV */
  353. return 0;
  354. }
  355. #ifdef CONFIG_PM
  356. static int au1xpsc_ac97_drvsuspend(struct device *dev)
  357. {
  358. struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
  359. /* save interesting registers and disable PSC */
  360. wd->pm[0] = __raw_readl(PSC_SEL(wd));
  361. __raw_writel(0, AC97_CFG(wd));
  362. wmb(); /* drain writebuffer */
  363. __raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
  364. wmb(); /* drain writebuffer */
  365. return 0;
  366. }
  367. static int au1xpsc_ac97_drvresume(struct device *dev)
  368. {
  369. struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
  370. /* restore PSC clock config */
  371. __raw_writel(wd->pm[0] | PSC_SEL_PS_AC97MODE, PSC_SEL(wd));
  372. wmb(); /* drain writebuffer */
  373. /* after this point the ac97 core will cold-reset the codec.
  374. * During cold-reset the PSC is reinitialized and the last
  375. * configuration set up in hw_params() is restored.
  376. */
  377. return 0;
  378. }
  379. static struct dev_pm_ops au1xpscac97_pmops = {
  380. .suspend = au1xpsc_ac97_drvsuspend,
  381. .resume = au1xpsc_ac97_drvresume,
  382. };
  383. #define AU1XPSCAC97_PMOPS &au1xpscac97_pmops
  384. #else
  385. #define AU1XPSCAC97_PMOPS NULL
  386. #endif
  387. static struct platform_driver au1xpsc_ac97_driver = {
  388. .driver = {
  389. .name = "au1xpsc_ac97",
  390. .pm = AU1XPSCAC97_PMOPS,
  391. },
  392. .probe = au1xpsc_ac97_drvprobe,
  393. .remove = au1xpsc_ac97_drvremove,
  394. };
  395. module_platform_driver(au1xpsc_ac97_driver);
  396. MODULE_LICENSE("GPL");
  397. MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
  398. MODULE_AUTHOR("Manuel Lauss");