Merge tag 'v4.10-rockchip-dts64-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / sound / soc / davinci / davinci-vcif.c
blobc77d9218795adba25d2ae54665a8e21b769d6ee7
1 /*
2 * ALSA SoC Voice Codec Interface for TI DAVINCI processor
4 * Copyright (C) 2010 Texas Instruments.
6 * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
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.
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.
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
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/io.h>
29 #include <linux/mfd/davinci_voicecodec.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/initval.h>
35 #include <sound/soc.h>
36 #include <sound/dmaengine_pcm.h>
38 #include "edma-pcm.h"
39 #include "davinci-i2s.h"
41 #define MOD_REG_BIT(val, mask, set) do { \
42 if (set) { \
43 val |= mask; \
44 } else { \
45 val &= ~mask; \
46 } \
47 } while (0)
49 struct davinci_vcif_dev {
50 struct davinci_vc *davinci_vc;
51 struct snd_dmaengine_dai_dma_data dma_data[2];
52 int dma_request[2];
55 static void davinci_vcif_start(struct snd_pcm_substream *substream)
57 struct snd_soc_pcm_runtime *rtd = substream->private_data;
58 struct davinci_vcif_dev *davinci_vcif_dev =
59 snd_soc_dai_get_drvdata(rtd->cpu_dai);
60 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
61 u32 w;
63 /* Start the sample generator and enable transmitter/receiver */
64 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
66 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
67 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 0);
68 else
69 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 0);
71 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
74 static void davinci_vcif_stop(struct snd_pcm_substream *substream)
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
77 struct davinci_vcif_dev *davinci_vcif_dev =
78 snd_soc_dai_get_drvdata(rtd->cpu_dai);
79 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
80 u32 w;
82 /* Reset transmitter/receiver and sample rate/frame sync generators */
83 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
84 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
85 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 1);
86 else
87 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 1);
89 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
92 static int davinci_vcif_hw_params(struct snd_pcm_substream *substream,
93 struct snd_pcm_hw_params *params,
94 struct snd_soc_dai *dai)
96 struct davinci_vcif_dev *davinci_vcif_dev = snd_soc_dai_get_drvdata(dai);
97 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
98 u32 w;
100 /* Restart the codec before setup */
101 davinci_vcif_stop(substream);
102 davinci_vcif_start(substream);
104 /* General line settings */
105 writel(DAVINCI_VC_CTRL_MASK, davinci_vc->base + DAVINCI_VC_CTRL);
107 writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTCLR);
109 writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTEN);
111 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
113 /* Determine xfer data type */
114 switch (params_format(params)) {
115 case SNDRV_PCM_FORMAT_U8:
116 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
117 DAVINCI_VC_CTRL_RD_UNSIGNED |
118 DAVINCI_VC_CTRL_WD_BITS_8 |
119 DAVINCI_VC_CTRL_WD_UNSIGNED, 1);
120 break;
121 case SNDRV_PCM_FORMAT_S8:
122 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
123 DAVINCI_VC_CTRL_WD_BITS_8, 1);
125 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_UNSIGNED |
126 DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
127 break;
128 case SNDRV_PCM_FORMAT_S16_LE:
129 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
130 DAVINCI_VC_CTRL_RD_UNSIGNED |
131 DAVINCI_VC_CTRL_WD_BITS_8 |
132 DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
133 break;
134 default:
135 printk(KERN_WARNING "davinci-vcif: unsupported PCM format");
136 return -EINVAL;
139 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
141 return 0;
144 static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd,
145 struct snd_soc_dai *dai)
147 int ret = 0;
149 switch (cmd) {
150 case SNDRV_PCM_TRIGGER_START:
151 case SNDRV_PCM_TRIGGER_RESUME:
152 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
153 davinci_vcif_start(substream);
154 break;
155 case SNDRV_PCM_TRIGGER_STOP:
156 case SNDRV_PCM_TRIGGER_SUSPEND:
157 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
158 davinci_vcif_stop(substream);
159 break;
160 default:
161 ret = -EINVAL;
164 return ret;
167 #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000
169 static const struct snd_soc_dai_ops davinci_vcif_dai_ops = {
170 .trigger = davinci_vcif_trigger,
171 .hw_params = davinci_vcif_hw_params,
174 static int davinci_vcif_dai_probe(struct snd_soc_dai *dai)
176 struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai);
178 dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
179 dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
181 return 0;
184 static struct snd_soc_dai_driver davinci_vcif_dai = {
185 .probe = davinci_vcif_dai_probe,
186 .playback = {
187 .channels_min = 1,
188 .channels_max = 2,
189 .rates = DAVINCI_VCIF_RATES,
190 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
191 .capture = {
192 .channels_min = 1,
193 .channels_max = 2,
194 .rates = DAVINCI_VCIF_RATES,
195 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
196 .ops = &davinci_vcif_dai_ops,
200 static const struct snd_soc_component_driver davinci_vcif_component = {
201 .name = "davinci-vcif",
204 static int davinci_vcif_probe(struct platform_device *pdev)
206 struct davinci_vc *davinci_vc = pdev->dev.platform_data;
207 struct davinci_vcif_dev *davinci_vcif_dev;
208 int ret;
210 davinci_vcif_dev = devm_kzalloc(&pdev->dev,
211 sizeof(struct davinci_vcif_dev),
212 GFP_KERNEL);
213 if (!davinci_vcif_dev) {
214 dev_dbg(&pdev->dev,
215 "could not allocate memory for private data\n");
216 return -ENOMEM;
219 /* DMA tx params */
220 davinci_vcif_dev->davinci_vc = davinci_vc;
221 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data =
222 &davinci_vc->davinci_vcif.dma_tx_channel;
223 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
224 davinci_vc->davinci_vcif.dma_tx_addr;
226 /* DMA rx params */
227 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].filter_data =
228 &davinci_vc->davinci_vcif.dma_rx_channel;
229 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
230 davinci_vc->davinci_vcif.dma_rx_addr;
232 dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
234 ret = devm_snd_soc_register_component(&pdev->dev,
235 &davinci_vcif_component,
236 &davinci_vcif_dai, 1);
237 if (ret != 0) {
238 dev_err(&pdev->dev, "could not register dai\n");
239 return ret;
242 ret = edma_pcm_platform_register(&pdev->dev);
243 if (ret) {
244 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
245 return ret;
248 return 0;
251 static struct platform_driver davinci_vcif_driver = {
252 .probe = davinci_vcif_probe,
253 .driver = {
254 .name = "davinci-vcif",
258 module_platform_driver(davinci_vcif_driver);
260 MODULE_AUTHOR("Miguel Aguilar");
261 MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
262 MODULE_LICENSE("GPL");