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