psc-ac97.c 11 KB

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