ASoC: amd: add ACP3x PCM platform driver
[sfrench/cifs-2.6.git] / sound / soc / amd / raven / acp3x-pcm-dma.c
1 /*
2  * AMD ALSA SoC PCM Driver
3  *
4  * Copyright 2016 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15
16 #include <linux/platform_device.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/io.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dai.h>
24
25 #include "acp3x.h"
26
27 #define DRV_NAME "acp3x-i2s-audio"
28
29 struct i2s_dev_data {
30         void __iomem *acp3x_base;
31         struct snd_pcm_substream *play_stream;
32         struct snd_pcm_substream *capture_stream;
33 };
34
35 static int acp3x_power_on(void __iomem *acp3x_base, bool on)
36 {
37         u16 val, mask;
38         u32 timeout;
39
40         if (on == true) {
41                 val = 1;
42                 mask = ACP3x_POWER_ON;
43         } else {
44                 val = 0;
45                 mask = ACP3x_POWER_OFF;
46         }
47
48         rv_writel(val, acp3x_base + mmACP_PGFSM_CONTROL);
49         timeout = 0;
50         while (true) {
51                 val = rv_readl(acp3x_base + mmACP_PGFSM_STATUS);
52                 if ((val & ACP3x_POWER_OFF_IN_PROGRESS) == mask)
53                         break;
54                 if (timeout > 100) {
55                         pr_err("ACP3x power state change failure\n");
56                         return -ENODEV;
57                 }
58                 timeout++;
59                 cpu_relax();
60         }
61         return 0;
62 }
63
64 static int acp3x_reset(void __iomem *acp3x_base)
65 {
66         u32 val, timeout;
67
68         rv_writel(1, acp3x_base + mmACP_SOFT_RESET);
69         timeout = 0;
70         while (true) {
71                 val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
72                 if ((val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK) ||
73                      timeout > 100) {
74                         if (val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK)
75                                 break;
76                         return -ENODEV;
77                 }
78                 timeout++;
79                 cpu_relax();
80         }
81
82         rv_writel(0, acp3x_base + mmACP_SOFT_RESET);
83         timeout = 0;
84         while (true) {
85                 val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
86                 if (!val || timeout > 100) {
87                         if (!val)
88                                 break;
89                         return -ENODEV;
90                 }
91                 timeout++;
92                 cpu_relax();
93         }
94         return 0;
95 }
96
97 static int acp3x_init(void __iomem *acp3x_base)
98 {
99         int ret;
100
101         /* power on */
102         ret = acp3x_power_on(acp3x_base, true);
103         if (ret) {
104                 pr_err("ACP3x power on failed\n");
105                 return ret;
106         }
107         /* Reset */
108         ret = acp3x_reset(acp3x_base);
109         if (ret) {
110                 pr_err("ACP3x reset failed\n");
111                 return ret;
112         }
113         return 0;
114 }
115
116 static int acp3x_deinit(void __iomem *acp3x_base)
117 {
118         int ret;
119
120         /* Reset */
121         ret = acp3x_reset(acp3x_base);
122         if (ret) {
123                 pr_err("ACP3x reset failed\n");
124                 return ret;
125         }
126         /* power off */
127         ret = acp3x_power_on(acp3x_base, false);
128         if (ret) {
129                 pr_err("ACP3x power off failed\n");
130                 return ret;
131         }
132         return 0;
133 }
134
135 static struct snd_pcm_ops acp3x_dma_ops = {
136         .open = NULL,
137         .close = NULL,
138         .ioctl = NULL,
139         .hw_params = NULL,
140         .hw_free = NULL,
141         .pointer = NULL,
142         .mmap = NULL,
143 };
144
145 struct snd_soc_dai_ops acp3x_dai_i2s_ops = {
146         .hw_params = NULL,
147         .trigger   = NULL,
148         .set_fmt = NULL,
149 };
150
151 static struct snd_soc_dai_driver acp3x_i2s_dai_driver = {
152         .playback = {
153                 .rates = SNDRV_PCM_RATE_8000_96000,
154                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
155                                         SNDRV_PCM_FMTBIT_U8 |
156                                         SNDRV_PCM_FMTBIT_S24_LE |
157                                         SNDRV_PCM_FMTBIT_S32_LE,
158                 .channels_min = 2,
159                 .channels_max = 8,
160
161                 .rate_min = 8000,
162                 .rate_max = 96000,
163         },
164         .capture = {
165                 .rates = SNDRV_PCM_RATE_8000_48000,
166                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
167                                         SNDRV_PCM_FMTBIT_U8 |
168                                         SNDRV_PCM_FMTBIT_S24_LE |
169                                         SNDRV_PCM_FMTBIT_S32_LE,
170                 .channels_min = 2,
171                 .channels_max = 2,
172                 .rate_min = 8000,
173                 .rate_max = 48000,
174         },
175         .ops = &acp3x_dai_i2s_ops,
176 };
177
178 static const struct snd_soc_component_driver acp3x_i2s_component = {
179         .name           = DRV_NAME,
180         .ops            = &acp3x_dma_ops,
181         .pcm_new        = acp3x_dma_new,
182 };
183
184 static int acp3x_audio_probe(struct platform_device *pdev)
185 {
186         int status;
187         struct resource *res;
188         struct i2s_dev_data *adata;
189         unsigned int irqflags;
190
191         if (!pdev->dev.platform_data) {
192                 dev_err(&pdev->dev, "platform_data not retrieved\n");
193                 return -ENODEV;
194         }
195         irqflags = *((unsigned int *)(pdev->dev.platform_data));
196
197         adata = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dev_data),
198                              GFP_KERNEL);
199         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200         if (!res) {
201                 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
202                         return -ENODEV;
203         }
204
205         adata->acp3x_base = devm_ioremap(&pdev->dev, res->start,
206                                          resource_size(res));
207
208         adata->play_stream = NULL;
209         adata->capture_stream = NULL;
210
211         dev_set_drvdata(&pdev->dev, adata);
212         /* Initialize ACP */
213         status = acp3x_init(adata->acp3x_base);
214         if (status)
215                 return -ENODEV;
216         status = devm_snd_soc_register_component(&pdev->dev,
217                                                  &acp3x_i2s_component,
218                                                  &acp3x_i2s_dai_driver, 1);
219         if (status) {
220                 dev_err(&pdev->dev, "Fail to register acp i2s dai\n");
221                 goto dev_err;
222         }
223
224         return 0;
225 dev_err:
226         status = acp3x_deinit(adata->acp3x_base);
227         if (status)
228                 dev_err(&pdev->dev, "ACP de-init failed\n");
229         else
230                 dev_info(&pdev->dev, "ACP de-initialized\n");
231         /*ignore device status and return driver probe error*/
232         return -ENODEV;
233 }
234
235 static int acp3x_audio_remove(struct platform_device *pdev)
236 {
237         int ret;
238         struct i2s_dev_data *adata = dev_get_drvdata(&pdev->dev);
239
240         ret = acp3x_deinit(adata->acp3x_base);
241         if (ret)
242                 dev_err(&pdev->dev, "ACP de-init failed\n");
243         else
244                 dev_info(&pdev->dev, "ACP de-initialized\n");
245
246         return 0;
247 }
248
249 static struct platform_driver acp3x_dma_driver = {
250         .probe = acp3x_audio_probe,
251         .remove = acp3x_audio_remove,
252         .driver = {
253                 .name = "acp3x_rv_i2s",
254         },
255 };
256
257 module_platform_driver(acp3x_dma_driver);
258
259 MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
260 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
261 MODULE_DESCRIPTION("AMD ACP 3.x PCM Driver");
262 MODULE_LICENSE("GPL v2");
263 MODULE_ALIAS("platform:" DRV_NAME);