mpc5200_psc_i2s.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Freescale MPC5200 PSC in I2S mode
  3. * ALSA SoC Digital Audio Interface (DAI) driver
  4. *
  5. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  6. * Copyright (C) 2009 Jon Smirl, Digispeaker
  7. */
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/of_platform.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/soc.h>
  14. #include <asm/mpc52xx_psc.h>
  15. #include "mpc5200_psc_i2s.h"
  16. #include "mpc5200_dma.h"
  17. /**
  18. * PSC_I2S_RATES: sample rates supported by the I2S
  19. *
  20. * This driver currently only supports the PSC running in I2S slave mode,
  21. * which means the codec determines the sample rate. Therefore, we tell
  22. * ALSA that we support all rates and let the codec driver decide what rates
  23. * are really supported.
  24. */
  25. #define PSC_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
  26. SNDRV_PCM_RATE_CONTINUOUS)
  27. /**
  28. * PSC_I2S_FORMATS: audio formats supported by the PSC I2S mode
  29. */
  30. #define PSC_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
  31. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
  32. static int psc_i2s_hw_params(struct snd_pcm_substream *substream,
  33. struct snd_pcm_hw_params *params,
  34. struct snd_soc_dai *dai)
  35. {
  36. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  37. struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
  38. u32 mode;
  39. dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
  40. " periods=%i buffer_size=%i buffer_bytes=%i\n",
  41. __func__, substream, params_period_size(params),
  42. params_period_bytes(params), params_periods(params),
  43. params_buffer_size(params), params_buffer_bytes(params));
  44. switch (params_format(params)) {
  45. case SNDRV_PCM_FORMAT_S8:
  46. mode = MPC52xx_PSC_SICR_SIM_CODEC_8;
  47. break;
  48. case SNDRV_PCM_FORMAT_S16_BE:
  49. mode = MPC52xx_PSC_SICR_SIM_CODEC_16;
  50. break;
  51. case SNDRV_PCM_FORMAT_S24_BE:
  52. mode = MPC52xx_PSC_SICR_SIM_CODEC_24;
  53. break;
  54. case SNDRV_PCM_FORMAT_S32_BE:
  55. mode = MPC52xx_PSC_SICR_SIM_CODEC_32;
  56. break;
  57. default:
  58. dev_dbg(psc_dma->dev, "invalid format\n");
  59. return -EINVAL;
  60. }
  61. out_be32(&psc_dma->psc_regs->sicr, psc_dma->sicr | mode);
  62. return 0;
  63. }
  64. /**
  65. * psc_i2s_set_sysclk: set the clock frequency and direction
  66. *
  67. * This function is called by the machine driver to tell us what the clock
  68. * frequency and direction are.
  69. *
  70. * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
  71. * and we don't care about the frequency. Return an error if the direction
  72. * is not SND_SOC_CLOCK_IN.
  73. *
  74. * @clk_id: reserved, should be zero
  75. * @freq: the frequency of the given clock ID, currently ignored
  76. * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
  77. */
  78. static int psc_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
  79. int clk_id, unsigned int freq, int dir)
  80. {
  81. struct psc_dma *psc_dma = cpu_dai->private_data;
  82. dev_dbg(psc_dma->dev, "psc_i2s_set_sysclk(cpu_dai=%p, dir=%i)\n",
  83. cpu_dai, dir);
  84. return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
  85. }
  86. /**
  87. * psc_i2s_set_fmt: set the serial format.
  88. *
  89. * This function is called by the machine driver to tell us what serial
  90. * format to use.
  91. *
  92. * This driver only supports I2S mode. Return an error if the format is
  93. * not SND_SOC_DAIFMT_I2S.
  94. *
  95. * @format: one of SND_SOC_DAIFMT_xxx
  96. */
  97. static int psc_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
  98. {
  99. struct psc_dma *psc_dma = cpu_dai->private_data;
  100. dev_dbg(psc_dma->dev, "psc_i2s_set_fmt(cpu_dai=%p, format=%i)\n",
  101. cpu_dai, format);
  102. return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
  103. }
  104. /* ---------------------------------------------------------------------
  105. * ALSA SoC Bindings
  106. *
  107. * - Digital Audio Interface (DAI) template
  108. * - create/destroy dai hooks
  109. */
  110. /**
  111. * psc_i2s_dai_template: template CPU Digital Audio Interface
  112. */
  113. static struct snd_soc_dai_ops psc_i2s_dai_ops = {
  114. .hw_params = psc_i2s_hw_params,
  115. .set_sysclk = psc_i2s_set_sysclk,
  116. .set_fmt = psc_i2s_set_fmt,
  117. };
  118. struct snd_soc_dai psc_i2s_dai[] = {{
  119. .name = "I2S",
  120. .playback = {
  121. .channels_min = 2,
  122. .channels_max = 2,
  123. .rates = PSC_I2S_RATES,
  124. .formats = PSC_I2S_FORMATS,
  125. },
  126. .capture = {
  127. .channels_min = 2,
  128. .channels_max = 2,
  129. .rates = PSC_I2S_RATES,
  130. .formats = PSC_I2S_FORMATS,
  131. },
  132. .ops = &psc_i2s_dai_ops,
  133. } };
  134. EXPORT_SYMBOL_GPL(psc_i2s_dai);
  135. /* ---------------------------------------------------------------------
  136. * OF platform bus binding code:
  137. * - Probe/remove operations
  138. * - OF device match table
  139. */
  140. static int __devinit psc_i2s_of_probe(struct of_device *op,
  141. const struct of_device_id *match)
  142. {
  143. int rc;
  144. struct psc_dma *psc_dma;
  145. struct mpc52xx_psc __iomem *regs;
  146. rc = mpc5200_audio_dma_create(op);
  147. if (rc != 0)
  148. return rc;
  149. rc = snd_soc_register_dais(psc_i2s_dai, ARRAY_SIZE(psc_i2s_dai));
  150. if (rc != 0) {
  151. pr_err("Failed to register DAI\n");
  152. return 0;
  153. }
  154. psc_dma = dev_get_drvdata(&op->dev);
  155. regs = psc_dma->psc_regs;
  156. /* Configure the serial interface mode; defaulting to CODEC8 mode */
  157. psc_dma->sicr = MPC52xx_PSC_SICR_DTS1 | MPC52xx_PSC_SICR_I2S |
  158. MPC52xx_PSC_SICR_CLKPOL;
  159. out_be32(&psc_dma->psc_regs->sicr,
  160. psc_dma->sicr | MPC52xx_PSC_SICR_SIM_CODEC_8);
  161. /* Check for the codec handle. If it is not present then we
  162. * are done */
  163. if (!of_get_property(op->node, "codec-handle", NULL))
  164. return 0;
  165. /* Due to errata in the dma mode; need to line up enabling
  166. * the transmitter with a transition on the frame sync
  167. * line */
  168. /* first make sure it is low */
  169. while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) != 0)
  170. ;
  171. /* then wait for the transition to high */
  172. while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) == 0)
  173. ;
  174. /* Finally, enable the PSC.
  175. * Receiver must always be enabled; even when we only want
  176. * transmit. (see 15.3.2.3 of MPC5200B User's Guide) */
  177. /* Go */
  178. out_8(&psc_dma->psc_regs->command,
  179. MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  180. return 0;
  181. }
  182. static int __devexit psc_i2s_of_remove(struct of_device *op)
  183. {
  184. return mpc5200_audio_dma_destroy(op);
  185. }
  186. /* Match table for of_platform binding */
  187. static struct of_device_id psc_i2s_match[] __devinitdata = {
  188. { .compatible = "fsl,mpc5200-psc-i2s", },
  189. { .compatible = "fsl,mpc5200b-psc-i2s", },
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(of, psc_i2s_match);
  193. static struct of_platform_driver psc_i2s_driver = {
  194. .match_table = psc_i2s_match,
  195. .probe = psc_i2s_of_probe,
  196. .remove = __devexit_p(psc_i2s_of_remove),
  197. .driver = {
  198. .name = "mpc5200-psc-i2s",
  199. .owner = THIS_MODULE,
  200. },
  201. };
  202. /* ---------------------------------------------------------------------
  203. * Module setup and teardown; simply register the of_platform driver
  204. * for the PSC in I2S mode.
  205. */
  206. static int __init psc_i2s_init(void)
  207. {
  208. return of_register_platform_driver(&psc_i2s_driver);
  209. }
  210. module_init(psc_i2s_init);
  211. static void __exit psc_i2s_exit(void)
  212. {
  213. of_unregister_platform_driver(&psc_i2s_driver);
  214. }
  215. module_exit(psc_i2s_exit);
  216. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  217. MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
  218. MODULE_LICENSE("GPL");