mpc5200_psc_ac97.c 8.4 KB

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