]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/soc/mediatek/common/mtk-afe-fe-dai.c
ASoC: mediatek: Fix UBSAN warning.
[linux.git] / sound / soc / mediatek / common / mtk-afe-fe-dai.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mtk-afe-fe-dais.c  --  Mediatek afe fe dai operator
4  *
5  * Copyright (c) 2016 MediaTek Inc.
6  * Author: Garlic Tseng <garlic.tseng@mediatek.com>
7  */
8
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/regmap.h>
12 #include <sound/soc.h>
13 #include "mtk-afe-platform-driver.h"
14 #include "mtk-afe-fe-dai.h"
15 #include "mtk-base-afe.h"
16
17 #define AFE_BASE_END_OFFSET 8
18
19 static int mtk_regmap_update_bits(struct regmap *map, int reg,
20                            unsigned int mask,
21                            unsigned int val)
22 {
23         if (reg < 0)
24                 return 0;
25         return regmap_update_bits(map, reg, mask, val);
26 }
27
28 static int mtk_regmap_write(struct regmap *map, int reg, unsigned int val)
29 {
30         if (reg < 0)
31                 return 0;
32         return regmap_write(map, reg, val);
33 }
34
35 int mtk_afe_fe_startup(struct snd_pcm_substream *substream,
36                        struct snd_soc_dai *dai)
37 {
38         struct snd_soc_pcm_runtime *rtd = substream->private_data;
39         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
40         struct snd_pcm_runtime *runtime = substream->runtime;
41         int memif_num = rtd->cpu_dai->id;
42         struct mtk_base_afe_memif *memif = &afe->memif[memif_num];
43         const struct snd_pcm_hardware *mtk_afe_hardware = afe->mtk_afe_hardware;
44         int ret;
45
46         memif->substream = substream;
47
48         snd_pcm_hw_constraint_step(substream->runtime, 0,
49                                    SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 16);
50         /* enable agent */
51         mtk_regmap_update_bits(afe->regmap, memif->data->agent_disable_reg,
52                                1 << memif->data->agent_disable_shift,
53                                0 << memif->data->agent_disable_shift);
54
55         snd_soc_set_runtime_hwparams(substream, mtk_afe_hardware);
56
57         /*
58          * Capture cannot use ping-pong buffer since hw_ptr at IRQ may be
59          * smaller than period_size due to AFE's internal buffer.
60          * This easily leads to overrun when avail_min is period_size.
61          * One more period can hold the possible unread buffer.
62          */
63         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
64                 int periods_max = mtk_afe_hardware->periods_max;
65
66                 ret = snd_pcm_hw_constraint_minmax(runtime,
67                                                    SNDRV_PCM_HW_PARAM_PERIODS,
68                                                    3, periods_max);
69                 if (ret < 0) {
70                         dev_err(afe->dev, "hw_constraint_minmax failed\n");
71                         return ret;
72                 }
73         }
74
75         ret = snd_pcm_hw_constraint_integer(runtime,
76                                             SNDRV_PCM_HW_PARAM_PERIODS);
77         if (ret < 0)
78                 dev_err(afe->dev, "snd_pcm_hw_constraint_integer failed\n");
79
80         /* dynamic allocate irq to memif */
81         if (memif->irq_usage < 0) {
82                 int irq_id = mtk_dynamic_irq_acquire(afe);
83
84                 if (irq_id != afe->irqs_size) {
85                         /* link */
86                         memif->irq_usage = irq_id;
87                 } else {
88                         dev_err(afe->dev, "%s() error: no more asys irq\n",
89                                 __func__);
90                         ret = -EBUSY;
91                 }
92         }
93         return ret;
94 }
95 EXPORT_SYMBOL_GPL(mtk_afe_fe_startup);
96
97 void mtk_afe_fe_shutdown(struct snd_pcm_substream *substream,
98                          struct snd_soc_dai *dai)
99 {
100         struct snd_soc_pcm_runtime *rtd = substream->private_data;
101         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
102         struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
103         int irq_id;
104
105         irq_id = memif->irq_usage;
106
107         mtk_regmap_update_bits(afe->regmap, memif->data->agent_disable_reg,
108                                1 << memif->data->agent_disable_shift,
109                                1 << memif->data->agent_disable_shift);
110
111         if (!memif->const_irq) {
112                 mtk_dynamic_irq_release(afe, irq_id);
113                 memif->irq_usage = -1;
114                 memif->substream = NULL;
115         }
116 }
117 EXPORT_SYMBOL_GPL(mtk_afe_fe_shutdown);
118
119 int mtk_afe_fe_hw_params(struct snd_pcm_substream *substream,
120                          struct snd_pcm_hw_params *params,
121                          struct snd_soc_dai *dai)
122 {
123         struct snd_soc_pcm_runtime *rtd = substream->private_data;
124         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
125         struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
126         int msb_at_bit33 = 0;
127         int ret, fs = 0;
128
129         ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
130         if (ret < 0)
131                 return ret;
132
133         msb_at_bit33 = upper_32_bits(substream->runtime->dma_addr) ? 1 : 0;
134         memif->phys_buf_addr = lower_32_bits(substream->runtime->dma_addr);
135         memif->buffer_size = substream->runtime->dma_bytes;
136
137         /* start */
138         mtk_regmap_write(afe->regmap, memif->data->reg_ofs_base,
139                          memif->phys_buf_addr);
140         /* end */
141         mtk_regmap_write(afe->regmap,
142                          memif->data->reg_ofs_base + AFE_BASE_END_OFFSET,
143                          memif->phys_buf_addr + memif->buffer_size - 1);
144
145         /* set MSB to 33-bit */
146         mtk_regmap_update_bits(afe->regmap, memif->data->msb_reg,
147                                1 << memif->data->msb_shift,
148                                msb_at_bit33 << memif->data->msb_shift);
149
150         /* set channel */
151         if (memif->data->mono_shift >= 0) {
152                 unsigned int mono = (params_channels(params) == 1) ? 1 : 0;
153
154                 mtk_regmap_update_bits(afe->regmap, memif->data->mono_reg,
155                                        1 << memif->data->mono_shift,
156                                        mono << memif->data->mono_shift);
157         }
158
159         /* set rate */
160         if (memif->data->fs_shift < 0)
161                 return 0;
162
163         fs = afe->memif_fs(substream, params_rate(params));
164
165         if (fs < 0)
166                 return -EINVAL;
167
168         mtk_regmap_update_bits(afe->regmap, memif->data->fs_reg,
169                                memif->data->fs_maskbit << memif->data->fs_shift,
170                                fs << memif->data->fs_shift);
171
172         return 0;
173 }
174 EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_params);
175
176 int mtk_afe_fe_hw_free(struct snd_pcm_substream *substream,
177                        struct snd_soc_dai *dai)
178 {
179         return snd_pcm_lib_free_pages(substream);
180 }
181 EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_free);
182
183 int mtk_afe_fe_trigger(struct snd_pcm_substream *substream, int cmd,
184                        struct snd_soc_dai *dai)
185 {
186         struct snd_soc_pcm_runtime *rtd = substream->private_data;
187         struct snd_pcm_runtime * const runtime = substream->runtime;
188         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
189         struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
190         struct mtk_base_afe_irq *irqs = &afe->irqs[memif->irq_usage];
191         const struct mtk_base_irq_data *irq_data = irqs->irq_data;
192         unsigned int counter = runtime->period_size;
193         int fs;
194
195         dev_dbg(afe->dev, "%s %s cmd=%d\n", __func__, memif->data->name, cmd);
196
197         switch (cmd) {
198         case SNDRV_PCM_TRIGGER_START:
199         case SNDRV_PCM_TRIGGER_RESUME:
200                 mtk_regmap_update_bits(afe->regmap,
201                                        memif->data->enable_reg,
202                                        1 << memif->data->enable_shift,
203                                        1 << memif->data->enable_shift);
204
205                 /* set irq counter */
206                 mtk_regmap_update_bits(afe->regmap, irq_data->irq_cnt_reg,
207                                        irq_data->irq_cnt_maskbit
208                                        << irq_data->irq_cnt_shift,
209                                        counter << irq_data->irq_cnt_shift);
210
211                 /* set irq fs */
212                 fs = afe->irq_fs(substream, runtime->rate);
213
214                 if (fs < 0)
215                         return -EINVAL;
216
217                 mtk_regmap_update_bits(afe->regmap, irq_data->irq_fs_reg,
218                                        irq_data->irq_fs_maskbit
219                                        << irq_data->irq_fs_shift,
220                                        fs << irq_data->irq_fs_shift);
221
222                 /* enable interrupt */
223                 mtk_regmap_update_bits(afe->regmap, irq_data->irq_en_reg,
224                                        1 << irq_data->irq_en_shift,
225                                        1 << irq_data->irq_en_shift);
226
227                 return 0;
228         case SNDRV_PCM_TRIGGER_STOP:
229         case SNDRV_PCM_TRIGGER_SUSPEND:
230                 mtk_regmap_update_bits(afe->regmap, memif->data->enable_reg,
231                                        1 << memif->data->enable_shift, 0);
232                 /* disable interrupt */
233                 mtk_regmap_update_bits(afe->regmap, irq_data->irq_en_reg,
234                                        1 << irq_data->irq_en_shift,
235                                        0 << irq_data->irq_en_shift);
236                 /* and clear pending IRQ */
237                 mtk_regmap_write(afe->regmap, irq_data->irq_clr_reg,
238                                  1 << irq_data->irq_clr_shift);
239                 return 0;
240         default:
241                 return -EINVAL;
242         }
243 }
244 EXPORT_SYMBOL_GPL(mtk_afe_fe_trigger);
245
246 int mtk_afe_fe_prepare(struct snd_pcm_substream *substream,
247                        struct snd_soc_dai *dai)
248 {
249         struct snd_soc_pcm_runtime *rtd  = substream->private_data;
250         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
251         struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
252         int hd_audio = 0;
253
254         /* set hd mode */
255         switch (substream->runtime->format) {
256         case SNDRV_PCM_FORMAT_S16_LE:
257                 hd_audio = 0;
258                 break;
259         case SNDRV_PCM_FORMAT_S32_LE:
260                 hd_audio = 1;
261                 break;
262         case SNDRV_PCM_FORMAT_S24_LE:
263                 hd_audio = 1;
264                 break;
265         default:
266                 dev_err(afe->dev, "%s() error: unsupported format %d\n",
267                         __func__, substream->runtime->format);
268                 break;
269         }
270
271         mtk_regmap_update_bits(afe->regmap, memif->data->hd_reg,
272                                1 << memif->data->hd_shift,
273                                hd_audio << memif->data->hd_shift);
274
275         return 0;
276 }
277 EXPORT_SYMBOL_GPL(mtk_afe_fe_prepare);
278
279 const struct snd_soc_dai_ops mtk_afe_fe_ops = {
280         .startup        = mtk_afe_fe_startup,
281         .shutdown       = mtk_afe_fe_shutdown,
282         .hw_params      = mtk_afe_fe_hw_params,
283         .hw_free        = mtk_afe_fe_hw_free,
284         .prepare        = mtk_afe_fe_prepare,
285         .trigger        = mtk_afe_fe_trigger,
286 };
287 EXPORT_SYMBOL_GPL(mtk_afe_fe_ops);
288
289 static DEFINE_MUTEX(irqs_lock);
290 int mtk_dynamic_irq_acquire(struct mtk_base_afe *afe)
291 {
292         int i;
293
294         mutex_lock(&afe->irq_alloc_lock);
295         for (i = 0; i < afe->irqs_size; ++i) {
296                 if (afe->irqs[i].irq_occupyed == 0) {
297                         afe->irqs[i].irq_occupyed = 1;
298                         mutex_unlock(&afe->irq_alloc_lock);
299                         return i;
300                 }
301         }
302         mutex_unlock(&afe->irq_alloc_lock);
303         return afe->irqs_size;
304 }
305 EXPORT_SYMBOL_GPL(mtk_dynamic_irq_acquire);
306
307 int mtk_dynamic_irq_release(struct mtk_base_afe *afe, int irq_id)
308 {
309         mutex_lock(&afe->irq_alloc_lock);
310         if (irq_id >= 0 && irq_id < afe->irqs_size) {
311                 afe->irqs[irq_id].irq_occupyed = 0;
312                 mutex_unlock(&afe->irq_alloc_lock);
313                 return 0;
314         }
315         mutex_unlock(&afe->irq_alloc_lock);
316         return -EINVAL;
317 }
318 EXPORT_SYMBOL_GPL(mtk_dynamic_irq_release);
319
320 int mtk_afe_dai_suspend(struct snd_soc_dai *dai)
321 {
322         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
323         struct device *dev = afe->dev;
324         struct regmap *regmap = afe->regmap;
325         int i;
326
327         if (pm_runtime_status_suspended(dev) || afe->suspended)
328                 return 0;
329
330         if (!afe->reg_back_up)
331                 afe->reg_back_up =
332                         devm_kcalloc(dev, afe->reg_back_up_list_num,
333                                      sizeof(unsigned int), GFP_KERNEL);
334
335         for (i = 0; i < afe->reg_back_up_list_num; i++)
336                 regmap_read(regmap, afe->reg_back_up_list[i],
337                             &afe->reg_back_up[i]);
338
339         afe->suspended = true;
340         afe->runtime_suspend(dev);
341         return 0;
342 }
343 EXPORT_SYMBOL_GPL(mtk_afe_dai_suspend);
344
345 int mtk_afe_dai_resume(struct snd_soc_dai *dai)
346 {
347         struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
348         struct device *dev = afe->dev;
349         struct regmap *regmap = afe->regmap;
350         int i = 0;
351
352         if (pm_runtime_status_suspended(dev) || !afe->suspended)
353                 return 0;
354
355         afe->runtime_resume(dev);
356
357         if (!afe->reg_back_up)
358                 dev_dbg(dev, "%s no reg_backup\n", __func__);
359
360         for (i = 0; i < afe->reg_back_up_list_num; i++)
361                 mtk_regmap_write(regmap, afe->reg_back_up_list[i],
362                                  afe->reg_back_up[i]);
363
364         afe->suspended = false;
365         return 0;
366 }
367 EXPORT_SYMBOL_GPL(mtk_afe_dai_resume);
368
369 MODULE_DESCRIPTION("Mediatek simple fe dai operator");
370 MODULE_AUTHOR("Garlic Tseng <garlic.tseng@mediatek.com>");
371 MODULE_LICENSE("GPL v2");