davinci-vcif.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <sound/dmaengine_pcm.h>
  35. #include "edma-pcm.h"
  36. #include "davinci-i2s.h"
  37. #define MOD_REG_BIT(val, mask, set) do { \
  38. if (set) { \
  39. val |= mask; \
  40. } else { \
  41. val &= ~mask; \
  42. } \
  43. } while (0)
  44. struct davinci_vcif_dev {
  45. struct davinci_vc *davinci_vc;
  46. struct snd_dmaengine_dai_dma_data dma_data[2];
  47. int dma_request[2];
  48. };
  49. static void davinci_vcif_start(struct snd_pcm_substream *substream)
  50. {
  51. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  52. struct davinci_vcif_dev *davinci_vcif_dev =
  53. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  54. struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
  55. u32 w;
  56. /* Start the sample generator and enable transmitter/receiver */
  57. w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
  58. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  59. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 0);
  60. else
  61. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 0);
  62. writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
  63. }
  64. static void davinci_vcif_stop(struct snd_pcm_substream *substream)
  65. {
  66. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  67. struct davinci_vcif_dev *davinci_vcif_dev =
  68. snd_soc_dai_get_drvdata(rtd->cpu_dai);
  69. struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
  70. u32 w;
  71. /* Reset transmitter/receiver and sample rate/frame sync generators */
  72. w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
  73. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  74. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 1);
  75. else
  76. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 1);
  77. writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
  78. }
  79. static int davinci_vcif_hw_params(struct snd_pcm_substream *substream,
  80. struct snd_pcm_hw_params *params,
  81. struct snd_soc_dai *dai)
  82. {
  83. struct davinci_vcif_dev *davinci_vcif_dev = snd_soc_dai_get_drvdata(dai);
  84. struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
  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. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
  98. DAVINCI_VC_CTRL_RD_UNSIGNED |
  99. DAVINCI_VC_CTRL_WD_BITS_8 |
  100. DAVINCI_VC_CTRL_WD_UNSIGNED, 1);
  101. break;
  102. case SNDRV_PCM_FORMAT_S8:
  103. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
  104. DAVINCI_VC_CTRL_WD_BITS_8, 1);
  105. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_UNSIGNED |
  106. DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
  107. break;
  108. case SNDRV_PCM_FORMAT_S16_LE:
  109. MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
  110. DAVINCI_VC_CTRL_RD_UNSIGNED |
  111. DAVINCI_VC_CTRL_WD_BITS_8 |
  112. DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
  113. break;
  114. default:
  115. printk(KERN_WARNING "davinci-vcif: unsupported PCM format");
  116. return -EINVAL;
  117. }
  118. writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
  119. return 0;
  120. }
  121. static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd,
  122. struct snd_soc_dai *dai)
  123. {
  124. int ret = 0;
  125. switch (cmd) {
  126. case SNDRV_PCM_TRIGGER_START:
  127. case SNDRV_PCM_TRIGGER_RESUME:
  128. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  129. davinci_vcif_start(substream);
  130. break;
  131. case SNDRV_PCM_TRIGGER_STOP:
  132. case SNDRV_PCM_TRIGGER_SUSPEND:
  133. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  134. davinci_vcif_stop(substream);
  135. break;
  136. default:
  137. ret = -EINVAL;
  138. }
  139. return ret;
  140. }
  141. #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000
  142. static const struct snd_soc_dai_ops davinci_vcif_dai_ops = {
  143. .trigger = davinci_vcif_trigger,
  144. .hw_params = davinci_vcif_hw_params,
  145. };
  146. static int davinci_vcif_dai_probe(struct snd_soc_dai *dai)
  147. {
  148. struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai);
  149. dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  150. dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  151. return 0;
  152. }
  153. static struct snd_soc_dai_driver davinci_vcif_dai = {
  154. .probe = davinci_vcif_dai_probe,
  155. .playback = {
  156. .channels_min = 1,
  157. .channels_max = 2,
  158. .rates = DAVINCI_VCIF_RATES,
  159. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  160. .capture = {
  161. .channels_min = 1,
  162. .channels_max = 2,
  163. .rates = DAVINCI_VCIF_RATES,
  164. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  165. .ops = &davinci_vcif_dai_ops,
  166. };
  167. static const struct snd_soc_component_driver davinci_vcif_component = {
  168. .name = "davinci-vcif",
  169. };
  170. static int davinci_vcif_probe(struct platform_device *pdev)
  171. {
  172. struct davinci_vc *davinci_vc = pdev->dev.platform_data;
  173. struct davinci_vcif_dev *davinci_vcif_dev;
  174. int ret;
  175. davinci_vcif_dev = devm_kzalloc(&pdev->dev,
  176. sizeof(struct davinci_vcif_dev),
  177. GFP_KERNEL);
  178. if (!davinci_vcif_dev) {
  179. dev_dbg(&pdev->dev,
  180. "could not allocate memory for private data\n");
  181. return -ENOMEM;
  182. }
  183. /* DMA tx params */
  184. davinci_vcif_dev->davinci_vc = davinci_vc;
  185. davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data =
  186. &davinci_vc->davinci_vcif.dma_tx_channel;
  187. davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  188. davinci_vc->davinci_vcif.dma_tx_addr;
  189. /* DMA rx params */
  190. davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].filter_data =
  191. &davinci_vc->davinci_vcif.dma_rx_channel;
  192. davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  193. davinci_vc->davinci_vcif.dma_rx_addr;
  194. dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
  195. ret = devm_snd_soc_register_component(&pdev->dev,
  196. &davinci_vcif_component,
  197. &davinci_vcif_dai, 1);
  198. if (ret != 0) {
  199. dev_err(&pdev->dev, "could not register dai\n");
  200. return ret;
  201. }
  202. ret = edma_pcm_platform_register(&pdev->dev);
  203. if (ret) {
  204. dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
  205. return ret;
  206. }
  207. return 0;
  208. }
  209. static struct platform_driver davinci_vcif_driver = {
  210. .probe = davinci_vcif_probe,
  211. .driver = {
  212. .name = "davinci-vcif",
  213. },
  214. };
  215. module_platform_driver(davinci_vcif_driver);
  216. MODULE_AUTHOR("Miguel Aguilar");
  217. MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
  218. MODULE_LICENSE("GPL");