psc-ac97.c 12 KB

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