davinci-vcif.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * ALSA SoC Voice Codec Interface for TI DAVINCI processor
  3. *
  4. * Copyright (C) 2010 Texas Instruments.
  5. *
  6. * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/io.h>
  28. #include <linux/mfd/davinci_voicecodec.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/initval.h>
  33. #include <sound/soc.h>
  34. #include "davinci-pcm.h"
  35. #include "davinci-i2s.h"
  36. #define MOD_REG_BIT(val, mask, set) do { \
  37. if (set) { \
  38. val |= mask; \
  39. } else { \
  40. val &= ~mask; \
  41. } \
  42. } while (0)
  43. struct davinci_vcif_dev {
  44. struct davinci_vc *davinci_vc;
  45. struct davinci_pcm_dma_params dma_params[2];
  46. };
  47. static void davinci_vcif_start(struct snd_pcm_substream *substream)
  48. {
  49. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  50. struct davinci_vcif_dev *davinci_vcif_dev =
  51. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  52. struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
  53. u32 w;
  54. /* Start the sample generator and enable transmitter/receiver */
  55. w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
  56. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  57. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 0);
  58. else
  59. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 0);
  60. writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
  61. }
  62. static void davinci_vcif_stop(struct snd_pcm_substream *substream)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. struct davinci_vcif_dev *davinci_vcif_dev =
  66. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  67. struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
  68. u32 w;
  69. /* Reset transmitter/receiver and sample rate/frame sync generators */
  70. w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
  71. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  72. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 1);
  73. else
  74. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 1);
  75. writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
  76. }
  77. static int davinci_vcif_hw_params(struct snd_pcm_substream *substream,
  78. struct snd_pcm_hw_params *params,
  79. struct snd_soc_dai *dai)
  80. {
  81. struct davinci_vcif_dev *davinci_vcif_dev = snd_soc_dai_get_drvdata(dai);
  82. struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
  83. struct davinci_pcm_dma_params *dma_params =
  84. &davinci_vcif_dev->dma_params[substream->stream];
  85. u32 w;
  86. /* Restart the codec before setup */
  87. davinci_vcif_stop(substream);
  88. davinci_vcif_start(substream);
  89. /* General line settings */
  90. writel(DAVINCI_VC_CTRL_MASK, davinci_vc->base + DAVINCI_VC_CTRL);
  91. writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTCLR);
  92. writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTEN);
  93. w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
  94. /* Determine xfer data type */
  95. switch (params_format(params)) {
  96. case SNDRV_PCM_FORMAT_U8:
  97. dma_params->data_type = 0;
  98. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
  99. DAVINCI_VC_CTRL_RD_UNSIGNED |
  100. DAVINCI_VC_CTRL_WD_BITS_8 |
  101. DAVINCI_VC_CTRL_WD_UNSIGNED, 1);
  102. break;
  103. case SNDRV_PCM_FORMAT_S8:
  104. dma_params->data_type = 1;
  105. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
  106. DAVINCI_VC_CTRL_WD_BITS_8, 1);
  107. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_UNSIGNED |
  108. DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
  109. break;
  110. case SNDRV_PCM_FORMAT_S16_LE:
  111. dma_params->data_type = 2;
  112. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
  113. DAVINCI_VC_CTRL_RD_UNSIGNED |
  114. DAVINCI_VC_CTRL_WD_BITS_8 |
  115. DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
  116. break;
  117. default:
  118. printk(KERN_WARNING "davinci-vcif: unsupported PCM format");
  119. return -EINVAL;
  120. }
  121. dma_params->acnt = dma_params->data_type;
  122. writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
  123. return 0;
  124. }
  125. static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd,
  126. struct snd_soc_dai *dai)
  127. {
  128. int ret = 0;
  129. switch (cmd) {
  130. case SNDRV_PCM_TRIGGER_START:
  131. case SNDRV_PCM_TRIGGER_RESUME:
  132. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  133. davinci_vcif_start(substream);
  134. break;
  135. case SNDRV_PCM_TRIGGER_STOP:
  136. case SNDRV_PCM_TRIGGER_SUSPEND:
  137. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  138. davinci_vcif_stop(substream);
  139. break;
  140. default:
  141. ret = -EINVAL;
  142. }
  143. return ret;
  144. }
  145. static int davinci_vcif_startup(struct snd_pcm_substream *substream,
  146. struct snd_soc_dai *dai)
  147. {
  148. struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai);
  149. snd_soc_dai_set_dma_data(dai, substream, dev->dma_params);
  150. return 0;
  151. }
  152. #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000
  153. static struct snd_soc_dai_ops davinci_vcif_dai_ops = {
  154. .startup = davinci_vcif_startup,
  155. .trigger = davinci_vcif_trigger,
  156. .hw_params = davinci_vcif_hw_params,
  157. };
  158. static struct snd_soc_dai_driver davinci_vcif_dai = {
  159. .playback = {
  160. .channels_min = 1,
  161. .channels_max = 2,
  162. .rates = DAVINCI_VCIF_RATES,
  163. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  164. .capture = {
  165. .channels_min = 1,
  166. .channels_max = 2,
  167. .rates = DAVINCI_VCIF_RATES,
  168. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  169. .ops = &davinci_vcif_dai_ops,
  170. };
  171. static int davinci_vcif_probe(struct platform_device *pdev)
  172. {
  173. struct davinci_vc *davinci_vc = pdev->dev.platform_data;
  174. struct davinci_vcif_dev *davinci_vcif_dev;
  175. int ret;
  176. davinci_vcif_dev = kzalloc(sizeof(struct davinci_vcif_dev), GFP_KERNEL);
  177. if (!davinci_vcif_dev) {
  178. dev_dbg(&pdev->dev,
  179. "could not allocate memory for private data\n");
  180. return -ENOMEM;
  181. }
  182. /* DMA tx params */
  183. davinci_vcif_dev->davinci_vc = davinci_vc;
  184. davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel =
  185. davinci_vc->davinci_vcif.dma_tx_channel;
  186. davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
  187. davinci_vc->davinci_vcif.dma_tx_addr;
  188. /* DMA rx params */
  189. davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel =
  190. davinci_vc->davinci_vcif.dma_rx_channel;
  191. davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
  192. davinci_vc->davinci_vcif.dma_rx_addr;
  193. dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
  194. ret = snd_soc_register_dai(&pdev->dev, &davinci_vcif_dai);
  195. if (ret != 0) {
  196. dev_err(&pdev->dev, "could not register dai\n");
  197. goto fail;
  198. }
  199. return 0;
  200. fail:
  201. kfree(davinci_vcif_dev);
  202. return ret;
  203. }
  204. static int davinci_vcif_remove(struct platform_device *pdev)
  205. {
  206. struct davinci_vcif_dev *davinci_vcif_dev = dev_get_drvdata(&pdev->dev);
  207. snd_soc_unregister_dai(&pdev->dev);
  208. kfree(davinci_vcif_dev);
  209. return 0;
  210. }
  211. static struct platform_driver davinci_vcif_driver = {
  212. .probe = davinci_vcif_probe,
  213. .remove = davinci_vcif_remove,
  214. .driver = {
  215. .name = "davinci-vcif",
  216. .owner = THIS_MODULE,
  217. },
  218. };
  219. static int __init davinci_vcif_init(void)
  220. {
  221. return platform_driver_probe(&davinci_vcif_driver, davinci_vcif_probe);
  222. }
  223. module_init(davinci_vcif_init);
  224. static void __exit davinci_vcif_exit(void)
  225. {
  226. platform_driver_unregister(&davinci_vcif_driver);
  227. }
  228. module_exit(davinci_vcif_exit);
  229. MODULE_AUTHOR("Miguel Aguilar");
  230. MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
  231. MODULE_LICENSE("GPL");