1 // SPDX-License-Identifier: GPL-2.0
3 * mtk-afe-platform-driver.c -- Mediatek afe platform driver
5 * Copyright (c) 2016 MediaTek Inc.
6 * Author: Garlic Tseng <garlic.tseng@mediatek.com>
9 #include <linux/module.h>
10 #include <linux/dma-mapping.h>
11 #include <sound/soc.h>
13 #include "mtk-afe-platform-driver.h"
14 #include "mtk-base-afe.h"
16 int mtk_afe_combine_sub_dai(struct mtk_base_afe *afe)
18 struct mtk_base_afe_dai *dai;
19 size_t num_dai_drivers = 0, dai_idx = 0;
21 /* calcualte total dai driver size */
22 list_for_each_entry(dai, &afe->sub_dais, list) {
23 num_dai_drivers += dai->num_dai_drivers;
26 dev_info(afe->dev, "%s(), num of dai %zd\n", __func__, num_dai_drivers);
28 /* combine sub_dais */
29 afe->num_dai_drivers = num_dai_drivers;
30 afe->dai_drivers = devm_kcalloc(afe->dev,
32 sizeof(struct snd_soc_dai_driver),
34 if (!afe->dai_drivers)
37 list_for_each_entry(dai, &afe->sub_dais, list) {
39 memcpy(&afe->dai_drivers[dai_idx],
41 dai->num_dai_drivers *
42 sizeof(struct snd_soc_dai_driver));
43 dai_idx += dai->num_dai_drivers;
47 EXPORT_SYMBOL_GPL(mtk_afe_combine_sub_dai);
49 int mtk_afe_add_sub_dai_control(struct snd_soc_component *component)
51 struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
52 struct mtk_base_afe_dai *dai;
54 list_for_each_entry(dai, &afe->sub_dais, list) {
56 snd_soc_add_component_controls(component,
60 if (dai->dapm_widgets)
61 snd_soc_dapm_new_controls(&component->dapm,
63 dai->num_dapm_widgets);
65 /* add routes after all widgets are added */
66 list_for_each_entry(dai, &afe->sub_dais, list) {
68 snd_soc_dapm_add_routes(&component->dapm,
70 dai->num_dapm_routes);
73 snd_soc_dapm_new_widgets(component->dapm.card);
78 EXPORT_SYMBOL_GPL(mtk_afe_add_sub_dai_control);
80 snd_pcm_uframes_t mtk_afe_pcm_pointer(struct snd_soc_component *component,
81 struct snd_pcm_substream *substream)
83 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
84 struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
85 struct mtk_base_afe_memif *memif = &afe->memif[snd_soc_rtd_to_cpu(rtd, 0)->id];
86 const struct mtk_base_memif_data *memif_data = memif->data;
87 struct regmap *regmap = afe->regmap;
88 struct device *dev = afe->dev;
89 int reg_ofs_base = memif_data->reg_ofs_base;
90 int reg_ofs_cur = memif_data->reg_ofs_cur;
91 unsigned int hw_ptr = 0, hw_base = 0;
92 int ret, pcm_ptr_bytes;
94 ret = regmap_read(regmap, reg_ofs_cur, &hw_ptr);
95 if (ret || hw_ptr == 0) {
96 dev_err(dev, "%s hw_ptr err\n", __func__);
98 goto POINTER_RETURN_FRAMES;
101 ret = regmap_read(regmap, reg_ofs_base, &hw_base);
102 if (ret || hw_base == 0) {
103 dev_err(dev, "%s hw_ptr err\n", __func__);
105 goto POINTER_RETURN_FRAMES;
108 pcm_ptr_bytes = hw_ptr - hw_base;
110 POINTER_RETURN_FRAMES:
111 return bytes_to_frames(substream->runtime, pcm_ptr_bytes);
113 EXPORT_SYMBOL_GPL(mtk_afe_pcm_pointer);
115 int mtk_afe_pcm_new(struct snd_soc_component *component,
116 struct snd_soc_pcm_runtime *rtd)
119 struct snd_pcm *pcm = rtd->pcm;
120 struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
122 size = afe->mtk_afe_hardware->buffer_bytes_max;
123 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
124 afe->dev, size, size);
127 EXPORT_SYMBOL_GPL(mtk_afe_pcm_new);
129 const struct snd_soc_component_driver mtk_afe_pcm_platform = {
130 .name = AFE_PCM_NAME,
131 .pointer = mtk_afe_pcm_pointer,
132 .pcm_construct = mtk_afe_pcm_new,
134 EXPORT_SYMBOL_GPL(mtk_afe_pcm_platform);
136 MODULE_DESCRIPTION("Mediatek simple platform driver");
137 MODULE_AUTHOR("Garlic Tseng <garlic.tseng@mediatek.com>");
138 MODULE_LICENSE("GPL v2");