psc-ac97.c 12 KB

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