mpc5200_psc_ac97.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
  111. dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
  112. " periods=%i buffer_size=%i buffer_bytes=%i channels=%i"
  113. " rate=%i format=%i\n",
  114. __func__, substream, params_period_size(params),
  115. params_period_bytes(params), params_periods(params),
  116. params_buffer_size(params), params_buffer_bytes(params),
  117. params_channels(params), params_rate(params),
  118. params_format(params));
  119. /* Determine the set of enable bits to turn on */
  120. s->ac97_slot_bits = (params_channels(params) == 1) ? 0x100 : 0x300;
  121. if (substream->pstr->stream != SNDRV_PCM_STREAM_CAPTURE)
  122. s->ac97_slot_bits <<= 16;
  123. return 0;
  124. }
  125. static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
  126. struct snd_pcm_hw_params *params,
  127. struct snd_soc_dai *cpu_dai)
  128. {
  129. struct psc_dma *psc_dma = cpu_dai->private_data;
  130. dev_dbg(psc_dma->dev, "%s(substream=%p)\n", __func__, substream);
  131. if (params_channels(params) == 1)
  132. out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
  133. else
  134. out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);
  135. return 0;
  136. }
  137. static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
  138. struct snd_soc_dai *dai)
  139. {
  140. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  141. struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
  142. struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
  143. switch (cmd) {
  144. case SNDRV_PCM_TRIGGER_START:
  145. dev_dbg(psc_dma->dev, "AC97 START: stream=%i\n",
  146. substream->pstr->stream);
  147. /* Set the slot enable bits */
  148. psc_dma->slots |= s->ac97_slot_bits;
  149. out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
  150. break;
  151. case SNDRV_PCM_TRIGGER_STOP:
  152. dev_dbg(psc_dma->dev, "AC97 STOP: stream=%i\n",
  153. substream->pstr->stream);
  154. /* Clear the slot enable bits */
  155. psc_dma->slots &= ~(s->ac97_slot_bits);
  156. out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
  157. break;
  158. }
  159. return 0;
  160. }
  161. static int psc_ac97_probe(struct platform_device *pdev,
  162. struct snd_soc_dai *cpu_dai)
  163. {
  164. struct psc_dma *psc_dma = cpu_dai->private_data;
  165. struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
  166. /* Go */
  167. out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  168. return 0;
  169. }
  170. /* ---------------------------------------------------------------------
  171. * ALSA SoC Bindings
  172. *
  173. * - Digital Audio Interface (DAI) template
  174. * - create/destroy dai hooks
  175. */
  176. /**
  177. * psc_ac97_dai_template: template CPU Digital Audio Interface
  178. */
  179. static struct snd_soc_dai_ops psc_ac97_analog_ops = {
  180. .hw_params = psc_ac97_hw_analog_params,
  181. .trigger = psc_ac97_trigger,
  182. };
  183. static struct snd_soc_dai_ops psc_ac97_digital_ops = {
  184. .hw_params = psc_ac97_hw_digital_params,
  185. };
  186. struct snd_soc_dai psc_ac97_dai[] = {
  187. {
  188. .name = "AC97",
  189. .ac97_control = 1,
  190. .probe = psc_ac97_probe,
  191. .playback = {
  192. .channels_min = 1,
  193. .channels_max = 6,
  194. .rates = SNDRV_PCM_RATE_8000_48000,
  195. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  196. },
  197. .capture = {
  198. .channels_min = 1,
  199. .channels_max = 2,
  200. .rates = SNDRV_PCM_RATE_8000_48000,
  201. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  202. },
  203. .ops = &psc_ac97_analog_ops,
  204. },
  205. {
  206. .name = "SPDIF",
  207. .ac97_control = 1,
  208. .playback = {
  209. .channels_min = 1,
  210. .channels_max = 2,
  211. .rates = SNDRV_PCM_RATE_32000 | \
  212. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  213. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  214. },
  215. .ops = &psc_ac97_digital_ops,
  216. } };
  217. EXPORT_SYMBOL_GPL(psc_ac97_dai);
  218. /* ---------------------------------------------------------------------
  219. * OF platform bus binding code:
  220. * - Probe/remove operations
  221. * - OF device match table
  222. */
  223. static int __devinit psc_ac97_of_probe(struct of_device *op,
  224. const struct of_device_id *match)
  225. {
  226. int rc, i;
  227. struct snd_ac97 ac97;
  228. struct mpc52xx_psc __iomem *regs;
  229. rc = mpc5200_audio_dma_create(op);
  230. if (rc != 0)
  231. return rc;
  232. for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
  233. psc_ac97_dai[i].dev = &op->dev;
  234. rc = snd_soc_register_dais(psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
  235. if (rc != 0) {
  236. dev_err(&op->dev, "Failed to register DAI\n");
  237. return rc;
  238. }
  239. psc_dma = dev_get_drvdata(&op->dev);
  240. regs = psc_dma->psc_regs;
  241. ac97.private_data = psc_dma;
  242. for (i = 0; i < ARRAY_SIZE(psc_ac97_dai); i++)
  243. psc_ac97_dai[i].private_data = psc_dma;
  244. psc_dma->imr = 0;
  245. out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
  246. /* Configure the serial interface mode to AC97 */
  247. psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
  248. out_be32(&regs->sicr, psc_dma->sicr);
  249. /* No slots active */
  250. out_be32(&regs->ac97_slots, 0x00000000);
  251. return 0;
  252. }
  253. static int __devexit psc_ac97_of_remove(struct of_device *op)
  254. {
  255. return mpc5200_audio_dma_destroy(op);
  256. }
  257. /* Match table for of_platform binding */
  258. static struct of_device_id psc_ac97_match[] __devinitdata = {
  259. { .compatible = "fsl,mpc5200-psc-ac97", },
  260. { .compatible = "fsl,mpc5200b-psc-ac97", },
  261. {}
  262. };
  263. MODULE_DEVICE_TABLE(of, psc_ac97_match);
  264. static struct of_platform_driver psc_ac97_driver = {
  265. .match_table = psc_ac97_match,
  266. .probe = psc_ac97_of_probe,
  267. .remove = __devexit_p(psc_ac97_of_remove),
  268. .driver = {
  269. .name = "mpc5200-psc-ac97",
  270. .owner = THIS_MODULE,
  271. },
  272. };
  273. /* ---------------------------------------------------------------------
  274. * Module setup and teardown; simply register the of_platform driver
  275. * for the PSC in AC97 mode.
  276. */
  277. static int __init psc_ac97_init(void)
  278. {
  279. return of_register_platform_driver(&psc_ac97_driver);
  280. }
  281. module_init(psc_ac97_init);
  282. static void __exit psc_ac97_exit(void)
  283. {
  284. of_unregister_platform_driver(&psc_ac97_driver);
  285. }
  286. module_exit(psc_ac97_exit);
  287. MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
  288. MODULE_DESCRIPTION("mpc5200 AC97 module");
  289. MODULE_LICENSE("GPL");