Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / sound / soc / davinci / davinci-vcif.c
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
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>
30
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>
37
38 #include "edma-pcm.h"
39 #include "davinci-i2s.h"
40
41 #define MOD_REG_BIT(val, mask, set) do { \
42         if (set) { \
43                 val |= mask; \
44         } else { \
45                 val &= ~mask; \
46         } \
47 } while (0)
48
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];
53 };
54
55 static void davinci_vcif_start(struct snd_pcm_substream *substream)
56 {
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;
62
63         /* Start the sample generator and enable transmitter/receiver */
64         w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
65
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);
70
71         writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
72 }
73
74 static void davinci_vcif_stop(struct snd_pcm_substream *substream)
75 {
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;
81
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);
88
89         writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
90 }
91
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)
95 {
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;
99
100         /* Restart the codec before setup */
101         davinci_vcif_stop(substream);
102         davinci_vcif_start(substream);
103
104         /* General line settings */
105         writel(DAVINCI_VC_CTRL_MASK, davinci_vc->base + DAVINCI_VC_CTRL);
106
107         writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTCLR);
108
109         writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTEN);
110
111         w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
112
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);
124
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;
137         }
138
139         writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
140
141         return 0;
142 }
143
144 static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd,
145                                 struct snd_soc_dai *dai)
146 {
147         int ret = 0;
148
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;
162         }
163
164         return ret;
165 }
166
167 #define DAVINCI_VCIF_RATES      SNDRV_PCM_RATE_8000_48000
168
169 static const struct snd_soc_dai_ops davinci_vcif_dai_ops = {
170         .trigger        = davinci_vcif_trigger,
171         .hw_params      = davinci_vcif_hw_params,
172 };
173
174 static int davinci_vcif_dai_probe(struct snd_soc_dai *dai)
175 {
176         struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai);
177
178         dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
179         dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
180
181         return 0;
182 }
183
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,
197
198 };
199
200 static const struct snd_soc_component_driver davinci_vcif_component = {
201         .name           = "davinci-vcif",
202 };
203
204 static int davinci_vcif_probe(struct platform_device *pdev)
205 {
206         struct davinci_vc *davinci_vc = pdev->dev.platform_data;
207         struct davinci_vcif_dev *davinci_vcif_dev;
208         int ret;
209
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;
217         }
218
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;
225
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;
231
232         dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
233
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;
240         }
241
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;
246         }
247
248         return 0;
249 }
250
251 static struct platform_driver davinci_vcif_driver = {
252         .probe          = davinci_vcif_probe,
253         .driver         = {
254                 .name   = "davinci-vcif",
255         },
256 };
257
258 module_platform_driver(davinci_vcif_driver);
259
260 MODULE_AUTHOR("Miguel Aguilar");
261 MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
262 MODULE_LICENSE("GPL");