mpc5200_psc_ac97.c 8.7 KB

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