mpc5200_psc_ac97.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * linux/sound/mpc5200-ac97.c -- AC97 support for the Freescale MPC52xx chip.
  3. *
  4. * Copyright (C) 2009 Jon Smirl, Digispeaker
  5. * Author: Jon Smirl <jonsmirl@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. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_platform.h>
  14. #include <sound/pcm.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/soc.h>
  17. #include <asm/time.h>
  18. #include <asm/delay.h>
  19. #include <asm/mpc52xx_psc.h>
  20. #include "mpc5200_dma.h"
  21. #include "mpc5200_psc_ac97.h"
  22. #define DRV_NAME "mpc5200-psc-ac97"
  23. /* ALSA only supports a single AC97 device so static is recommend here */
  24. static struct psc_dma *psc_dma;
  25. static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  26. {
  27. int status;
  28. unsigned int val;
  29. mutex_lock(&psc_dma->mutex);
  30. /* Wait for command send status zero = ready */
  31. status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
  32. MPC52xx_PSC_SR_CMDSEND), 100, 0);
  33. if (status == 0) {
  34. pr_err("timeout on ac97 bus (rdy)\n");
  35. mutex_unlock(&psc_dma->mutex);
  36. return -ENODEV;
  37. }
  38. /* Force clear the data valid bit */
  39. in_be32(&psc_dma->psc_regs->ac97_data);
  40. /* Send the read */
  41. out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));
  42. /* Wait for the answer */
  43. status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
  44. MPC52xx_PSC_SR_DATA_VAL), 100, 0);
  45. if (status == 0) {
  46. pr_err("timeout on ac97 read (val) %x\n",
  47. in_be16(&psc_dma->psc_regs->sr_csr.status));
  48. mutex_unlock(&psc_dma->mutex);
  49. return -ENODEV;
  50. }
  51. /* Get the data */
  52. val = in_be32(&psc_dma->psc_regs->ac97_data);
  53. if (((val >> 24) & 0x7f) != reg) {
  54. pr_err("reg echo error on ac97 read\n");
  55. mutex_unlock(&psc_dma->mutex);
  56. return -ENODEV;
  57. }
  58. val = (val >> 8) & 0xffff;
  59. mutex_unlock(&psc_dma->mutex);
  60. return (unsigned short) val;
  61. }
  62. static void psc_ac97_write(struct snd_ac97 *ac97,
  63. unsigned short reg, unsigned short val)
  64. {
  65. int status;
  66. mutex_lock(&psc_dma->mutex);
  67. /* Wait for command status zero = ready */
  68. status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
  69. MPC52xx_PSC_SR_CMDSEND), 100, 0);
  70. if (status == 0) {
  71. pr_err("timeout on ac97 bus (write)\n");
  72. goto out;
  73. }
  74. /* Write data */
  75. out_be32(&psc_dma->psc_regs->ac97_cmd,
  76. ((reg & 0x7f) << 24) | (val << 8));
  77. out:
  78. mutex_unlock(&psc_dma->mutex);
  79. }
  80. static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
  81. {
  82. struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
  83. out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
  84. udelay(3);
  85. out_be32(&regs->sicr, psc_dma->sicr);
  86. }
  87. static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
  88. {
  89. struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
  90. /* Do a cold reset */
  91. out_8(&regs->op1, MPC52xx_PSC_OP_RES);
  92. udelay(10);
  93. out_8(&regs->op0, MPC52xx_PSC_OP_RES);
  94. udelay(50);
  95. psc_ac97_warm_reset(ac97);
  96. }
  97. struct snd_ac97_bus_ops soc_ac97_ops = {
  98. .read = psc_ac97_read,
  99. .write = psc_ac97_write,
  100. .reset = psc_ac97_cold_reset,
  101. .warm_reset = psc_ac97_warm_reset,
  102. };
  103. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  104. static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
  105. struct snd_pcm_hw_params *params,
  106. struct snd_soc_dai *cpu_dai)
  107. {
  108. struct psc_dma *psc_dma = cpu_dai->private_data;
  109. dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
  110. " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
  111. " rate=%i format=%i\n",
  112. __func__, substream, params_period_size(params),
  113. params_period_bytes(params), params_periods(params),
  114. params_buffer_size(params), params_buffer_bytes(params),
  115. params_channels(params), params_rate(params),
  116. params_format(params));
  117. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
  118. if (params_channels(params) == 1)
  119. psc_dma->slots |= 0x00000100;
  120. else
  121. psc_dma->slots |= 0x00000300;
  122. } else {
  123. if (params_channels(params) == 1)
  124. psc_dma->slots |= 0x01000000;
  125. else
  126. psc_dma->slots |= 0x03000000;
  127. }
  128. out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
  129. return 0;
  130. }
  131. static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
  132. struct snd_pcm_hw_params *params,
  133. struct snd_soc_dai *cpu_dai)
  134. {
  135. struct psc_dma *psc_dma = cpu_dai->private_data;
  136. if (params_channels(params) == 1)
  137. out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
  138. else
  139. out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
  140. return 0;
  141. }
  142. static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
  143. struct snd_soc_dai *dai)
  144. {
  145. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  146. struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
  147. switch (cmd) {
  148. case SNDRV_PCM_TRIGGER_STOP:
  149. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  150. psc_dma->slots &= 0xFFFF0000;
  151. else
  152. psc_dma->slots &= 0x0000FFFF;
  153. out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
  154. break;
  155. }
  156. return 0;
  157. }
  158. static int psc_ac97_probe(struct platform_device *pdev,
  159. struct snd_soc_dai *cpu_dai)
  160. {
  161. struct psc_dma *psc_dma = cpu_dai->private_data;
  162. struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
  163. /* Go */
  164. out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  165. return 0;
  166. }
  167. /* ---------------------------------------------------------------------
  168. * ALSA SoC Bindings
  169. *
  170. * - Digital Audio Interface (DAI) template
  171. * - create/destroy dai hooks
  172. */
  173. /**
  174. * psc_ac97_dai_template: template CPU Digital Audio Interface
  175. */
  176. static struct snd_soc_dai_ops psc_ac97_analog_ops = {
  177. .hw_params = psc_ac97_hw_analog_params,
  178. .trigger = psc_ac97_trigger,
  179. };
  180. static struct snd_soc_dai_ops psc_ac97_digital_ops = {
  181. .hw_params = psc_ac97_hw_digital_params,
  182. };
  183. struct snd_soc_dai psc_ac97_dai[] = {
  184. {
  185. .name = "AC97",
  186. .ac97_control = 1,
  187. .probe = psc_ac97_probe,
  188. .playback = {
  189. .channels_min = 1,
  190. .channels_max = 6,
  191. .rates = SNDRV_PCM_RATE_8000_48000,
  192. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  193. },
  194. .capture = {
  195. .channels_min = 1,
  196. .channels_max = 2,
  197. .rates = SNDRV_PCM_RATE_8000_48000,
  198. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  199. },
  200. .ops = &psc_ac97_analog_ops,
  201. },
  202. {
  203. .name = "SPDIF",
  204. .ac97_control = 1,
  205. .playback = {
  206. .channels_min = 1,
  207. .channels_max = 2,
  208. .rates = SNDRV_PCM_RATE_32000 | \
  209. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  210. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  211. },
  212. .ops = &psc_ac97_digital_ops,
  213. } };
  214. EXPORT_SYMBOL_GPL(psc_ac97_dai);
  215. /* ---------------------------------------------------------------------
  216. * OF platform bus binding code:
  217. * - Probe/remove operations
  218. * - OF device match table
  219. */
  220. static int __devinit psc_ac97_of_probe(struct of_device *op,
  221. const struct of_device_id *match)
  222. {
  223. int rc, i;
  224. struct snd_ac97 ac97;
  225. struct mpc52xx_psc __iomem *regs;
  226. rc = mpc5200_audio_dma_create(op);
  227. if (rc != 0)
  228. return rc;
  229. for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
  230. psc_ac97_dai[i].dev = &op->dev;
  231. rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
  232. if (rc != 0) {
  233. dev_err(&op->dev, "Failed to register DAI\n");
  234. return rc;
  235. }
  236. psc_dma = dev_get_drvdata(&op->dev);
  237. regs = psc_dma->psc_regs;
  238. ac97.private_data = psc_dma;
  239. for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
  240. psc_ac97_dai[i].private_data = psc_dma;
  241. psc_dma->imr = 0;
  242. out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
  243. /* Configure the serial interface mode to AC97 */
  244. psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
  245. out_be32(&regs->sicr, psc_dma->sicr);
  246. /* No slots active */
  247. out_be32(&regs->ac97_slots, 0x00000000);
  248. return 0;
  249. }
  250. static int __devexit psc_ac97_of_remove(struct of_device *op)
  251. {
  252. return mpc5200_audio_dma_destroy(op);
  253. }
  254. /* Match table for of_platform binding */
  255. static struct of_device_id psc_ac97_match[] __devinitdata = {
  256. { .compatible = "fsl,mpc5200-psc-ac97", },
  257. { .compatible = "fsl,mpc5200b-psc-ac97", },
  258. {}
  259. };
  260. MODULE_DEVICE_TABLE(of, psc_ac97_match);
  261. static struct of_platform_driver psc_ac97_driver = {
  262. .match_table = psc_ac97_match,
  263. .probe = psc_ac97_of_probe,
  264. .remove = __devexit_p(psc_ac97_of_remove),
  265. .driver = {
  266. .name = "mpc5200-psc-ac97",
  267. .owner = THIS_MODULE,
  268. },
  269. };
  270. /* ---------------------------------------------------------------------
  271. * Module setup and teardown; simply register the of_platform driver
  272. * for the PSC in AC97 mode.
  273. */
  274. static int __init psc_ac97_init(void)
  275. {
  276. return of_register_platform_driver(&psc_ac97_driver);
  277. }
  278. module_init(psc_ac97_init);
  279. static void __exit psc_ac97_exit(void)
  280. {
  281. of_unregister_platform_driver(&psc_ac97_driver);
  282. }
  283. module_exit(psc_ac97_exit);
  284. MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
  285. MODULE_DESCRIPTION("mpc5200 AC97 module");
  286. MODULE_LICENSE("GPL");