treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / ti / davinci-vcif.c
blobc84650e4a7aa7e93528af32ad2a3bd9d4853c335
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ALSA SoC Voice Codec Interface for TI DAVINCI processor
5 * Copyright (C) 2010 Texas Instruments.
7 * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
8 */
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>
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>
25 #include "edma-pcm.h"
26 #include "davinci-i2s.h"
28 #define MOD_REG_BIT(val, mask, set) do { \
29 if (set) { \
30 val |= mask; \
31 } else { \
32 val &= ~mask; \
33 } \
34 } while (0)
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];
42 static void davinci_vcif_start(struct snd_pcm_substream *substream)
44 struct snd_soc_pcm_runtime *rtd = substream->private_data;
45 struct davinci_vcif_dev *davinci_vcif_dev =
46 snd_soc_dai_get_drvdata(rtd->cpu_dai);
47 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
48 u32 w;
50 /* Start the sample generator and enable transmitter/receiver */
51 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
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);
58 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
61 static void davinci_vcif_stop(struct snd_pcm_substream *substream)
63 struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 struct davinci_vcif_dev *davinci_vcif_dev =
65 snd_soc_dai_get_drvdata(rtd->cpu_dai);
66 struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc;
67 u32 w;
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);
76 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
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)
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;
87 /* Restart the codec before setup */
88 davinci_vcif_stop(substream);
89 davinci_vcif_start(substream);
91 /* General line settings */
92 writel(DAVINCI_VC_CTRL_MASK, davinci_vc->base + DAVINCI_VC_CTRL);
94 writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTCLR);
96 writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTEN);
98 w = readl(davinci_vc->base + DAVINCI_VC_CTRL);
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);
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;
126 writel(w, davinci_vc->base + DAVINCI_VC_CTRL);
128 return 0;
131 static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd,
132 struct snd_soc_dai *dai)
134 int ret = 0;
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;
151 return ret;
154 #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000
156 static const struct snd_soc_dai_ops davinci_vcif_dai_ops = {
157 .trigger = davinci_vcif_trigger,
158 .hw_params = davinci_vcif_hw_params,
161 static int davinci_vcif_dai_probe(struct snd_soc_dai *dai)
163 struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai);
165 dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
166 dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
168 return 0;
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,
187 static const struct snd_soc_component_driver davinci_vcif_component = {
188 .name = "davinci-vcif",
191 static int davinci_vcif_probe(struct platform_device *pdev)
193 struct davinci_vc *davinci_vc = pdev->dev.platform_data;
194 struct davinci_vcif_dev *davinci_vcif_dev;
195 int ret;
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;
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;
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;
216 dev_set_drvdata(&pdev->dev, davinci_vcif_dev);
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;
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;
232 return 0;
235 static struct platform_driver davinci_vcif_driver = {
236 .probe = davinci_vcif_probe,
237 .driver = {
238 .name = "davinci-vcif",
242 module_platform_driver(davinci_vcif_driver);
244 MODULE_AUTHOR("Miguel Aguilar");
245 MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface");
246 MODULE_LICENSE("GPL");