ASoC: amd: Interrupt handler changes for ACP3x DMA 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         unsigned int i2s_irq;
31         void __iomem *acp3x_base;
32         struct snd_pcm_substream *play_stream;
33         struct snd_pcm_substream *capture_stream;
34 };
35
36 static int acp3x_power_on(void __iomem *acp3x_base, bool on)
37 {
38         u16 val, mask;
39         u32 timeout;
40
41         if (on == true) {
42                 val = 1;
43                 mask = ACP3x_POWER_ON;
44         } else {
45                 val = 0;
46                 mask = ACP3x_POWER_OFF;
47         }
48
49         rv_writel(val, acp3x_base + mmACP_PGFSM_CONTROL);
50         timeout = 0;
51         while (true) {
52                 val = rv_readl(acp3x_base + mmACP_PGFSM_STATUS);
53                 if ((val & ACP3x_POWER_OFF_IN_PROGRESS) == mask)
54                         break;
55                 if (timeout > 100) {
56                         pr_err("ACP3x power state change failure\n");
57                         return -ENODEV;
58                 }
59                 timeout++;
60                 cpu_relax();
61         }
62         return 0;
63 }
64
65 static int acp3x_reset(void __iomem *acp3x_base)
66 {
67         u32 val, timeout;
68
69         rv_writel(1, acp3x_base + mmACP_SOFT_RESET);
70         timeout = 0;
71         while (true) {
72                 val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
73                 if ((val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK) ||
74                      timeout > 100) {
75                         if (val & ACP3x_SOFT_RESET__SoftResetAudDone_MASK)
76                                 break;
77                         return -ENODEV;
78                 }
79                 timeout++;
80                 cpu_relax();
81         }
82
83         rv_writel(0, acp3x_base + mmACP_SOFT_RESET);
84         timeout = 0;
85         while (true) {
86                 val = rv_readl(acp3x_base + mmACP_SOFT_RESET);
87                 if (!val || timeout > 100) {
88                         if (!val)
89                                 break;
90                         return -ENODEV;
91                 }
92                 timeout++;
93                 cpu_relax();
94         }
95         return 0;
96 }
97
98 static int acp3x_init(void __iomem *acp3x_base)
99 {
100         int ret;
101
102         /* power on */
103         ret = acp3x_power_on(acp3x_base, true);
104         if (ret) {
105                 pr_err("ACP3x power on failed\n");
106                 return ret;
107         }
108         /* Reset */
109         ret = acp3x_reset(acp3x_base);
110         if (ret) {
111                 pr_err("ACP3x reset failed\n");
112                 return ret;
113         }
114         return 0;
115 }
116
117 static int acp3x_deinit(void __iomem *acp3x_base)
118 {
119         int ret;
120
121         /* Reset */
122         ret = acp3x_reset(acp3x_base);
123         if (ret) {
124                 pr_err("ACP3x reset failed\n");
125                 return ret;
126         }
127         /* power off */
128         ret = acp3x_power_on(acp3x_base, false);
129         if (ret) {
130                 pr_err("ACP3x power off failed\n");
131                 return ret;
132         }
133         return 0;
134 }
135
136 static irqreturn_t i2s_irq_handler(int irq, void *dev_id)
137 {
138         u16 play_flag, cap_flag;
139         u32 val;
140         struct i2s_dev_data *rv_i2s_data = dev_id;
141
142         if (!rv_i2s_data)
143                 return IRQ_NONE;
144
145         play_flag = 0;
146         cap_flag = 0;
147         val = rv_readl(rv_i2s_data->acp3x_base + mmACP_EXTERNAL_INTR_STAT);
148         if ((val & BIT(BT_TX_THRESHOLD)) && rv_i2s_data->play_stream) {
149                 rv_writel(BIT(BT_TX_THRESHOLD), rv_i2s_data->acp3x_base +
150                           mmACP_EXTERNAL_INTR_STAT);
151                 snd_pcm_period_elapsed(rv_i2s_data->play_stream);
152                 play_flag = 1;
153         }
154
155         if ((val & BIT(BT_RX_THRESHOLD)) && rv_i2s_data->capture_stream) {
156                 rv_writel(BIT(BT_RX_THRESHOLD), rv_i2s_data->acp3x_base +
157                           mmACP_EXTERNAL_INTR_STAT);
158                 snd_pcm_period_elapsed(rv_i2s_data->capture_stream);
159                 cap_flag = 1;
160         }
161
162         if (play_flag | cap_flag)
163                 return IRQ_HANDLED;
164         else
165                 return IRQ_NONE;
166 }
167
168 static struct snd_pcm_ops acp3x_dma_ops = {
169         .open = NULL,
170         .close = NULL,
171         .ioctl = NULL,
172         .hw_params = NULL,
173         .hw_free = NULL,
174         .pointer = NULL,
175         .mmap = NULL,
176 };
177
178 struct snd_soc_dai_ops acp3x_dai_i2s_ops = {
179         .hw_params = NULL,
180         .trigger   = NULL,
181         .set_fmt = NULL,
182 };
183
184 static struct snd_soc_dai_driver acp3x_i2s_dai_driver = {
185         .playback = {
186                 .rates = SNDRV_PCM_RATE_8000_96000,
187                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
188                                         SNDRV_PCM_FMTBIT_U8 |
189                                         SNDRV_PCM_FMTBIT_S24_LE |
190                                         SNDRV_PCM_FMTBIT_S32_LE,
191                 .channels_min = 2,
192                 .channels_max = 8,
193
194                 .rate_min = 8000,
195                 .rate_max = 96000,
196         },
197         .capture = {
198                 .rates = SNDRV_PCM_RATE_8000_48000,
199                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
200                                         SNDRV_PCM_FMTBIT_U8 |
201                                         SNDRV_PCM_FMTBIT_S24_LE |
202                                         SNDRV_PCM_FMTBIT_S32_LE,
203                 .channels_min = 2,
204                 .channels_max = 2,
205                 .rate_min = 8000,
206                 .rate_max = 48000,
207         },
208         .ops = &acp3x_dai_i2s_ops,
209 };
210
211 static const struct snd_soc_component_driver acp3x_i2s_component = {
212         .name           = DRV_NAME,
213         .ops            = &acp3x_dma_ops,
214         .pcm_new        = acp3x_dma_new,
215 };
216
217 static int acp3x_audio_probe(struct platform_device *pdev)
218 {
219         int status;
220         struct resource *res;
221         struct i2s_dev_data *adata;
222         unsigned int irqflags;
223
224         if (!pdev->dev.platform_data) {
225                 dev_err(&pdev->dev, "platform_data not retrieved\n");
226                 return -ENODEV;
227         }
228         irqflags = *((unsigned int *)(pdev->dev.platform_data));
229
230         adata = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dev_data),
231                              GFP_KERNEL);
232         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
233         if (!res) {
234                 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
235                         return -ENODEV;
236         }
237
238         adata->acp3x_base = devm_ioremap(&pdev->dev, res->start,
239                                          resource_size(res));
240
241         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
242         if (!res) {
243                 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
244                 return -ENODEV;
245         }
246
247         adata->i2s_irq = res->start;
248         adata->play_stream = NULL;
249         adata->capture_stream = NULL;
250
251         dev_set_drvdata(&pdev->dev, adata);
252         /* Initialize ACP */
253         status = acp3x_init(adata->acp3x_base);
254         if (status)
255                 return -ENODEV;
256         status = devm_snd_soc_register_component(&pdev->dev,
257                                                  &acp3x_i2s_component,
258                                                  &acp3x_i2s_dai_driver, 1);
259         if (status) {
260                 dev_err(&pdev->dev, "Fail to register acp i2s dai\n");
261                 goto dev_err;
262         }
263         status = devm_request_irq(&pdev->dev, adata->i2s_irq, i2s_irq_handler,
264                                   irqflags, "ACP3x_I2S_IRQ", adata);
265         if (status) {
266                 dev_err(&pdev->dev, "ACP3x I2S IRQ request failed\n");
267                 goto dev_err;
268         }
269
270         return 0;
271 dev_err:
272         status = acp3x_deinit(adata->acp3x_base);
273         if (status)
274                 dev_err(&pdev->dev, "ACP de-init failed\n");
275         else
276                 dev_info(&pdev->dev, "ACP de-initialized\n");
277         /*ignore device status and return driver probe error*/
278         return -ENODEV;
279 }
280
281 static int acp3x_audio_remove(struct platform_device *pdev)
282 {
283         int ret;
284         struct i2s_dev_data *adata = dev_get_drvdata(&pdev->dev);
285
286         ret = acp3x_deinit(adata->acp3x_base);
287         if (ret)
288                 dev_err(&pdev->dev, "ACP de-init failed\n");
289         else
290                 dev_info(&pdev->dev, "ACP de-initialized\n");
291
292         return 0;
293 }
294
295 static struct platform_driver acp3x_dma_driver = {
296         .probe = acp3x_audio_probe,
297         .remove = acp3x_audio_remove,
298         .driver = {
299                 .name = "acp3x_rv_i2s",
300         },
301 };
302
303 module_platform_driver(acp3x_dma_driver);
304
305 MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
306 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
307 MODULE_DESCRIPTION("AMD ACP 3.x PCM Driver");
308 MODULE_LICENSE("GPL v2");
309 MODULE_ALIAS("platform:" DRV_NAME);