Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / sound / soc / davinci / davinci-vcif.c
blob5415b72393fafac24fd685f0bed9c7740083975e
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 return -ENOMEM;
216 /* DMA tx params */
217 davinci_vcif_dev->davinci_vc = davinci_vc;
218 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data =
219 &davinci_vc->davinci_vcif.dma_tx_channel;
220 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
221 davinci_vc->davinci_vcif.dma_tx_addr;
223 /* DMA rx params */
224 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].filter_data =
225 &davinci_vc->davinci_vcif.dma_rx_channel;
226 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
227 davinci_vc->davinci_vcif.dma_rx_addr;
229 dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
231 ret = devm_snd_soc_register_component(&pdev->dev,
232 &davinci_vcif_component,
233 &davinci_vcif_dai, 1);
234 if (ret != 0) {
235 dev_err(&pdev->dev, "could not register dai\n");
236 return ret;
239 ret = edma_pcm_platform_register(&pdev->dev);
240 if (ret) {
241 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
242 return ret;
245 return 0;
248 static struct platform_driver davinci_vcif_driver = {
249 .probe = davinci_vcif_probe,
250 .driver = {
251 .name = "davinci-vcif",
255 module_platform_driver(davinci_vcif_driver);
257 MODULE_AUTHOR("Miguel Aguilar");
258 MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
259 MODULE_LICENSE("GPL");