psc-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 data, retry, tmo;
  55. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  56. au_sync();
  57. retry = AC97_RW_RETRIES;
  58. do {
  59. mutex_lock(&pscdata->lock);
  60. au_writel(PSC_AC97CDC_RD | PSC_AC97CDC_INDX(reg),
  61. AC97_CDC(pscdata));
  62. au_sync();
  63. tmo = 2000;
  64. while ((!(au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD))
  65. && --tmo)
  66. udelay(2);
  67. data = au_readl(AC97_CDC(pscdata)) & 0xffff;
  68. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  69. au_sync();
  70. mutex_unlock(&pscdata->lock);
  71. } while (--retry && !tmo);
  72. return retry ? data : 0xffff;
  73. }
  74. /* AC97 controller writes to codec register */
  75. static void au1xpsc_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  76. unsigned short val)
  77. {
  78. /* FIXME */
  79. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  80. unsigned int tmo, retry;
  81. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  82. au_sync();
  83. retry = AC97_RW_RETRIES;
  84. do {
  85. mutex_lock(&pscdata->lock);
  86. au_writel(PSC_AC97CDC_INDX(reg) | (val & 0xffff),
  87. AC97_CDC(pscdata));
  88. au_sync();
  89. tmo = 2000;
  90. while ((!(au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD))
  91. && --tmo)
  92. udelay(2);
  93. au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
  94. au_sync();
  95. mutex_unlock(&pscdata->lock);
  96. } while (--retry && !tmo);
  97. }
  98. /* AC97 controller asserts a warm reset */
  99. static void au1xpsc_ac97_warm_reset(struct snd_ac97 *ac97)
  100. {
  101. /* FIXME */
  102. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  103. au_writel(PSC_AC97RST_SNC, AC97_RST(pscdata));
  104. au_sync();
  105. msleep(10);
  106. au_writel(0, AC97_RST(pscdata));
  107. au_sync();
  108. }
  109. static void au1xpsc_ac97_cold_reset(struct snd_ac97 *ac97)
  110. {
  111. /* FIXME */
  112. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  113. int i;
  114. /* disable PSC during cold reset */
  115. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  116. au_sync();
  117. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(pscdata));
  118. au_sync();
  119. /* issue cold reset */
  120. au_writel(PSC_AC97RST_RST, AC97_RST(pscdata));
  121. au_sync();
  122. msleep(500);
  123. au_writel(0, AC97_RST(pscdata));
  124. au_sync();
  125. /* enable PSC */
  126. au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
  127. au_sync();
  128. /* wait for PSC to indicate it's ready */
  129. i = 1000;
  130. while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_SR)) && (--i))
  131. msleep(1);
  132. if (i == 0) {
  133. printk(KERN_ERR "au1xpsc-ac97: PSC not ready!\n");
  134. return;
  135. }
  136. /* enable the ac97 function */
  137. au_writel(pscdata->cfg | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  138. au_sync();
  139. /* wait for AC97 core to become ready */
  140. i = 1000;
  141. while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)) && (--i))
  142. msleep(1);
  143. if (i == 0)
  144. printk(KERN_ERR "au1xpsc-ac97: AC97 ctrl not ready\n");
  145. }
  146. /* AC97 controller operations */
  147. struct snd_ac97_bus_ops soc_ac97_ops = {
  148. .read = au1xpsc_ac97_read,
  149. .write = au1xpsc_ac97_write,
  150. .reset = au1xpsc_ac97_cold_reset,
  151. .warm_reset = au1xpsc_ac97_warm_reset,
  152. };
  153. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  154. static int au1xpsc_ac97_hw_params(struct snd_pcm_substream *substream,
  155. struct snd_pcm_hw_params *params,
  156. struct snd_soc_dai *dai)
  157. {
  158. /* FIXME */
  159. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  160. unsigned long r, ro, stat;
  161. int chans, stype = SUBSTREAM_TYPE(substream);
  162. chans = params_channels(params);
  163. r = ro = au_readl(AC97_CFG(pscdata));
  164. stat = au_readl(AC97_STAT(pscdata));
  165. /* already active? */
  166. if (stat & (PSC_AC97STAT_TB | PSC_AC97STAT_RB)) {
  167. /* reject parameters not currently set up */
  168. if ((PSC_AC97CFG_GET_LEN(r) != params->msbits) ||
  169. (pscdata->rate != params_rate(params)))
  170. return -EINVAL;
  171. } else {
  172. /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
  173. r &= ~PSC_AC97CFG_LEN_MASK;
  174. r |= PSC_AC97CFG_SET_LEN(params->msbits);
  175. /* channels: enable slots for front L/R channel */
  176. if (stype == PCM_TX) {
  177. r &= ~PSC_AC97CFG_TXSLOT_MASK;
  178. r |= PSC_AC97CFG_TXSLOT_ENA(3);
  179. r |= PSC_AC97CFG_TXSLOT_ENA(4);
  180. } else {
  181. r &= ~PSC_AC97CFG_RXSLOT_MASK;
  182. r |= PSC_AC97CFG_RXSLOT_ENA(3);
  183. r |= PSC_AC97CFG_RXSLOT_ENA(4);
  184. }
  185. /* do we need to poke the hardware? */
  186. if (!(r ^ ro))
  187. goto out;
  188. /* ac97 engine is about to be disabled */
  189. mutex_lock(&pscdata->lock);
  190. /* disable AC97 device controller first... */
  191. au_writel(r & ~PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  192. au_sync();
  193. /* ...wait for it... */
  194. while (au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)
  195. asm volatile ("nop");
  196. /* ...write config... */
  197. au_writel(r, AC97_CFG(pscdata));
  198. au_sync();
  199. /* ...enable the AC97 controller again... */
  200. au_writel(r | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
  201. au_sync();
  202. /* ...and wait for ready bit */
  203. while (!(au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR))
  204. asm volatile ("nop");
  205. mutex_unlock(&pscdata->lock);
  206. pscdata->cfg = r;
  207. pscdata->rate = params_rate(params);
  208. }
  209. out:
  210. return 0;
  211. }
  212. static int au1xpsc_ac97_trigger(struct snd_pcm_substream *substream,
  213. int cmd, struct snd_soc_dai *dai)
  214. {
  215. /* FIXME */
  216. struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
  217. int ret, stype = SUBSTREAM_TYPE(substream);
  218. ret = 0;
  219. switch (cmd) {
  220. case SNDRV_PCM_TRIGGER_START:
  221. case SNDRV_PCM_TRIGGER_RESUME:
  222. au_writel(AC97PCR_CLRFIFO(stype), AC97_PCR(pscdata));
  223. au_sync();
  224. au_writel(AC97PCR_START(stype), AC97_PCR(pscdata));
  225. au_sync();
  226. break;
  227. case SNDRV_PCM_TRIGGER_STOP:
  228. case SNDRV_PCM_TRIGGER_SUSPEND:
  229. au_writel(AC97PCR_STOP(stype), AC97_PCR(pscdata));
  230. au_sync();
  231. while (au_readl(AC97_STAT(pscdata)) & AC97STAT_BUSY(stype))
  232. asm volatile ("nop");
  233. au_writel(AC97PCR_CLRFIFO(stype), AC97_PCR(pscdata));
  234. au_sync();
  235. break;
  236. default:
  237. ret = -EINVAL;
  238. }
  239. return ret;
  240. }
  241. static int au1xpsc_ac97_probe(struct platform_device *pdev,
  242. struct snd_soc_dai *dai)
  243. {
  244. int ret;
  245. struct resource *r;
  246. unsigned long sel;
  247. if (au1xpsc_ac97_workdata)
  248. return -EBUSY;
  249. au1xpsc_ac97_workdata =
  250. kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
  251. if (!au1xpsc_ac97_workdata)
  252. return -ENOMEM;
  253. mutex_init(&au1xpsc_ac97_workdata->lock);
  254. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. if (!r) {
  256. ret = -ENODEV;
  257. goto out0;
  258. }
  259. ret = -EBUSY;
  260. au1xpsc_ac97_workdata->ioarea =
  261. request_mem_region(r->start, r->end - r->start + 1,
  262. "au1xpsc_ac97");
  263. if (!au1xpsc_ac97_workdata->ioarea)
  264. goto out0;
  265. au1xpsc_ac97_workdata->mmio = ioremap(r->start, 0xffff);
  266. if (!au1xpsc_ac97_workdata->mmio)
  267. goto out1;
  268. /* configuration: max dma trigger threshold, enable ac97 */
  269. au1xpsc_ac97_workdata->cfg = PSC_AC97CFG_RT_FIFO8 |
  270. PSC_AC97CFG_TT_FIFO8 |
  271. PSC_AC97CFG_DE_ENABLE;
  272. /* preserve PSC clock source set up by platform (dev.platform_data
  273. * is already occupied by soc layer)
  274. */
  275. sel = au_readl(PSC_SEL(au1xpsc_ac97_workdata)) & PSC_SEL_CLK_MASK;
  276. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  277. au_sync();
  278. au_writel(0, PSC_SEL(au1xpsc_ac97_workdata));
  279. au_sync();
  280. au_writel(PSC_SEL_PS_AC97MODE | sel, PSC_SEL(au1xpsc_ac97_workdata));
  281. au_sync();
  282. /* next up: cold reset. Dont check for PSC-ready now since
  283. * there may not be any codec clock yet.
  284. */
  285. return 0;
  286. out1:
  287. release_resource(au1xpsc_ac97_workdata->ioarea);
  288. kfree(au1xpsc_ac97_workdata->ioarea);
  289. out0:
  290. kfree(au1xpsc_ac97_workdata);
  291. au1xpsc_ac97_workdata = NULL;
  292. return ret;
  293. }
  294. static void au1xpsc_ac97_remove(struct platform_device *pdev,
  295. struct snd_soc_dai *dai)
  296. {
  297. /* disable PSC completely */
  298. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  299. au_sync();
  300. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  301. au_sync();
  302. iounmap(au1xpsc_ac97_workdata->mmio);
  303. release_resource(au1xpsc_ac97_workdata->ioarea);
  304. kfree(au1xpsc_ac97_workdata->ioarea);
  305. kfree(au1xpsc_ac97_workdata);
  306. au1xpsc_ac97_workdata = NULL;
  307. }
  308. static int au1xpsc_ac97_suspend(struct snd_soc_dai *dai)
  309. {
  310. /* save interesting registers and disable PSC */
  311. au1xpsc_ac97_workdata->pm[0] =
  312. au_readl(PSC_SEL(au1xpsc_ac97_workdata));
  313. au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
  314. au_sync();
  315. au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
  316. au_sync();
  317. return 0;
  318. }
  319. static int au1xpsc_ac97_resume(struct snd_soc_dai *dai)
  320. {
  321. /* restore PSC clock config */
  322. au_writel(au1xpsc_ac97_workdata->pm[0] | PSC_SEL_PS_AC97MODE,
  323. PSC_SEL(au1xpsc_ac97_workdata));
  324. au_sync();
  325. /* after this point the ac97 core will cold-reset the codec.
  326. * During cold-reset the PSC is reinitialized and the last
  327. * configuration set up in hw_params() is restored.
  328. */
  329. return 0;
  330. }
  331. static struct snd_soc_dai_ops au1xpsc_ac97_dai_ops = {
  332. .trigger = au1xpsc_ac97_trigger,
  333. .hw_params = au1xpsc_ac97_hw_params,
  334. };
  335. struct snd_soc_dai au1xpsc_ac97_dai = {
  336. .name = "au1xpsc_ac97",
  337. .ac97_control = 1,
  338. .probe = au1xpsc_ac97_probe,
  339. .remove = au1xpsc_ac97_remove,
  340. .suspend = au1xpsc_ac97_suspend,
  341. .resume = au1xpsc_ac97_resume,
  342. .playback = {
  343. .rates = AC97_RATES,
  344. .formats = AC97_FMTS,
  345. .channels_min = 2,
  346. .channels_max = 2,
  347. },
  348. .capture = {
  349. .rates = AC97_RATES,
  350. .formats = AC97_FMTS,
  351. .channels_min = 2,
  352. .channels_max = 2,
  353. },
  354. .ops = &au1xpsc_ac97_dai_ops,
  355. };
  356. EXPORT_SYMBOL_GPL(au1xpsc_ac97_dai);
  357. static int __init au1xpsc_ac97_init(void)
  358. {
  359. au1xpsc_ac97_workdata = NULL;
  360. return snd_soc_register_dai(&au1xpsc_ac97_dai);
  361. }
  362. static void __exit au1xpsc_ac97_exit(void)
  363. {
  364. snd_soc_unregister_dai(&au1xpsc_ac97_dai);
  365. }
  366. module_init(au1xpsc_ac97_init);
  367. module_exit(au1xpsc_ac97_exit);
  368. MODULE_LICENSE("GPL");
  369. MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
  370. MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");