1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ALSA SoC Voice Codec Interface for TI DAVINCI processor
4 *
5 * Copyright (C) 2010 Texas Instruments.
6 *
7 * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
8 */
9
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/mfd/davinci_voicecodec.h>
17
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/initval.h>
22 #include <sound/soc.h>
23 #include <sound/dmaengine_pcm.h>
24
25 #include "edma-pcm.h"
26 #include "davinci-i2s.h"
27
28 #define MOD_REG_BIT(val, mask, set) do { \
29 if (set) { \
30 val |= mask; \
31 } else { \
32 val &= ~mask; \
33 } \
34 } while (0)
35
36 struct davinci_vcif_dev {
37 struct davinci_vc *davinci_vc;
38 struct snd_dmaengine_dai_dma_data dma_data[2];
39 int dma_request[2];
40 };
41
davinci_vcif_start(struct snd_pcm_substream * substream)42 static void davinci_vcif_start(struct snd_pcm_substream *substream)
43 {
44 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
45 struct davinci_vcif_dev *davinci_vcif_dev =
46 snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
47 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
48 u32 w;
49
50 /* Start the sample generator and enable transmitter/receiver */
51 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
52
53 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
54 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 0);
55 else
56 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 0);
57
58 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
59 }
60
davinci_vcif_stop(struct snd_pcm_substream * substream)61 static void davinci_vcif_stop(struct snd_pcm_substream *substream)
62 {
63 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
64 struct davinci_vcif_dev *davinci_vcif_dev =
65 snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
66 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
67 u32 w;
68
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
76 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
77 }
78
davinci_vcif_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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
87 /* Restart the codec before setup */
88 davinci_vcif_stop(substream);
89 davinci_vcif_start(substream);
90
91 /* General line settings */
92 writel(DAVINCI_VC_CTRL_MASK, davinci_vc->base + DAVINCI_VC_CTRL);
93
94 writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTCLR);
95
96 writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTEN);
97
98 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
99
100 /* Determine xfer data type */
101 switch (params_format(params)) {
102 case SNDRV_PCM_FORMAT_U8:
103 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
104 DAVINCI_VC_CTRL_RD_UNSIGNED |
105 DAVINCI_VC_CTRL_WD_BITS_8 |
106 DAVINCI_VC_CTRL_WD_UNSIGNED, 1);
107 break;
108 case SNDRV_PCM_FORMAT_S8:
109 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 |
110 DAVINCI_VC_CTRL_WD_BITS_8, 1);
111
112 MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_UNSIGNED |
113 DAVINCI_VC_CTRL_WD_UNSIGNED, 0);
114 break;
115 case SNDRV_PCM_FORMAT_S16_LE:
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, 0);
120 break;
121 default:
122 printk(KERN_WARNING "davinci-vcif: unsupported PCM format");
123 return -EINVAL;
124 }
125
126 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
127
128 return 0;
129 }
130
davinci_vcif_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)131 static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd,
132 struct snd_soc_dai *dai)
133 {
134 int ret = 0;
135
136 switch (cmd) {
137 case SNDRV_PCM_TRIGGER_START:
138 case SNDRV_PCM_TRIGGER_RESUME:
139 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
140 davinci_vcif_start(substream);
141 break;
142 case SNDRV_PCM_TRIGGER_STOP:
143 case SNDRV_PCM_TRIGGER_SUSPEND:
144 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
145 davinci_vcif_stop(substream);
146 break;
147 default:
148 ret = -EINVAL;
149 }
150
151 return ret;
152 }
153
154 #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000
155
156 static const struct snd_soc_dai_ops davinci_vcif_dai_ops = {
157 .trigger = davinci_vcif_trigger,
158 .hw_params = davinci_vcif_hw_params,
159 };
160
davinci_vcif_dai_probe(struct snd_soc_dai * dai)161 static int davinci_vcif_dai_probe(struct snd_soc_dai *dai)
162 {
163 struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai);
164
165 dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
166 dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
167
168 return 0;
169 }
170
171 static struct snd_soc_dai_driver davinci_vcif_dai = {
172 .probe = davinci_vcif_dai_probe,
173 .playback = {
174 .channels_min = 1,
175 .channels_max = 2,
176 .rates = DAVINCI_VCIF_RATES,
177 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
178 .capture = {
179 .channels_min = 1,
180 .channels_max = 2,
181 .rates = DAVINCI_VCIF_RATES,
182 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
183 .ops = &davinci_vcif_dai_ops,
184
185 };
186
187 static const struct snd_soc_component_driver davinci_vcif_component = {
188 .name = "davinci-vcif",
189 };
190
davinci_vcif_probe(struct platform_device * pdev)191 static int davinci_vcif_probe(struct platform_device *pdev)
192 {
193 struct davinci_vc *davinci_vc = pdev->dev.platform_data;
194 struct davinci_vcif_dev *davinci_vcif_dev;
195 int ret;
196
197 davinci_vcif_dev = devm_kzalloc(&pdev->dev,
198 sizeof(struct davinci_vcif_dev),
199 GFP_KERNEL);
200 if (!davinci_vcif_dev)
201 return -ENOMEM;
202
203 /* DMA tx params */
204 davinci_vcif_dev->davinci_vc = davinci_vc;
205 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data =
206 &davinci_vc->davinci_vcif.dma_tx_channel;
207 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
208 davinci_vc->davinci_vcif.dma_tx_addr;
209
210 /* DMA rx params */
211 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].filter_data =
212 &davinci_vc->davinci_vcif.dma_rx_channel;
213 davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
214 davinci_vc->davinci_vcif.dma_rx_addr;
215
216 dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
217
218 ret = devm_snd_soc_register_component(&pdev->dev,
219 &davinci_vcif_component,
220 &davinci_vcif_dai, 1);
221 if (ret != 0) {
222 dev_err(&pdev->dev, "could not register dai\n");
223 return ret;
224 }
225
226 ret = edma_pcm_platform_register(&pdev->dev);
227 if (ret) {
228 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
229 return ret;
230 }
231
232 return 0;
233 }
234
235 static struct platform_driver davinci_vcif_driver = {
236 .probe = davinci_vcif_probe,
237 .driver = {
238 .name = "davinci-vcif",
239 },
240 };
241
242 module_platform_driver(davinci_vcif_driver);
243
244 MODULE_AUTHOR("Miguel Aguilar");
245 MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
246 MODULE_LICENSE("GPL");
247