1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mtk-afe-platform-driver.c  --  Mediatek afe platform driver
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/dma-mapping.h>
11 #include <sound/soc.h>
12 
13 #include "mtk-afe-platform-driver.h"
14 #include "mtk-base-afe.h"
15 
16 int mtk_afe_combine_sub_dai(struct mtk_base_afe *afe)
17 {
18 	struct snd_soc_dai_driver *sub_dai_drivers;
19 	size_t num_dai_drivers = 0, dai_idx = 0;
20 	int i;
21 
22 	if (!afe->sub_dais) {
23 		dev_err(afe->dev, "%s(), sub_dais == NULL\n", __func__);
24 		return -EINVAL;
25 	}
26 
27 	/* calcualte total dai driver size */
28 	for (i = 0; i < afe->num_sub_dais; i++) {
29 		if (afe->sub_dais[i].dai_drivers &&
30 		    afe->sub_dais[i].num_dai_drivers != 0)
31 			num_dai_drivers += afe->sub_dais[i].num_dai_drivers;
32 	}
33 
34 	dev_info(afe->dev, "%s(), num of dai %zd\n", __func__, num_dai_drivers);
35 
36 	/* combine sub_dais */
37 	afe->num_dai_drivers = num_dai_drivers;
38 	afe->dai_drivers = devm_kcalloc(afe->dev,
39 					num_dai_drivers,
40 					sizeof(struct snd_soc_dai_driver),
41 					GFP_KERNEL);
42 	if (!afe->dai_drivers)
43 		return -ENOMEM;
44 
45 	for (i = 0; i < afe->num_sub_dais; i++) {
46 		if (afe->sub_dais[i].dai_drivers &&
47 		    afe->sub_dais[i].num_dai_drivers != 0) {
48 			sub_dai_drivers = afe->sub_dais[i].dai_drivers;
49 			/* dai driver */
50 			memcpy(&afe->dai_drivers[dai_idx],
51 			       sub_dai_drivers,
52 			       afe->sub_dais[i].num_dai_drivers *
53 			       sizeof(struct snd_soc_dai_driver));
54 			dai_idx += afe->sub_dais[i].num_dai_drivers;
55 		}
56 	}
57 
58 	return 0;
59 }
60 EXPORT_SYMBOL_GPL(mtk_afe_combine_sub_dai);
61 
62 int mtk_afe_add_sub_dai_control(struct snd_soc_component *component)
63 {
64 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
65 	int i;
66 
67 	if (!afe->sub_dais) {
68 		dev_err(afe->dev, "%s(), sub_dais == NULL\n", __func__);
69 		return -EINVAL;
70 	}
71 
72 	for (i = 0; i < afe->num_sub_dais; i++) {
73 		if (afe->sub_dais[i].controls)
74 			snd_soc_add_component_controls(component,
75 				afe->sub_dais[i].controls,
76 				afe->sub_dais[i].num_controls);
77 
78 		if (afe->sub_dais[i].dapm_widgets)
79 			snd_soc_dapm_new_controls(&component->dapm,
80 				afe->sub_dais[i].dapm_widgets,
81 				afe->sub_dais[i].num_dapm_widgets);
82 
83 		if (afe->sub_dais[i].dapm_routes)
84 			snd_soc_dapm_add_routes(&component->dapm,
85 				afe->sub_dais[i].dapm_routes,
86 				afe->sub_dais[i].num_dapm_routes);
87 	}
88 
89 	snd_soc_dapm_new_widgets(component->dapm.card);
90 
91 	return 0;
92 
93 }
94 EXPORT_SYMBOL_GPL(mtk_afe_add_sub_dai_control);
95 
96 static snd_pcm_uframes_t mtk_afe_pcm_pointer
97 			 (struct snd_pcm_substream *substream)
98 {
99 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
100 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
101 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
102 	struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
103 	const struct mtk_base_memif_data *memif_data = memif->data;
104 	struct regmap *regmap = afe->regmap;
105 	struct device *dev = afe->dev;
106 	int reg_ofs_base = memif_data->reg_ofs_base;
107 	int reg_ofs_cur = memif_data->reg_ofs_cur;
108 	unsigned int hw_ptr = 0, hw_base = 0;
109 	int ret, pcm_ptr_bytes;
110 
111 	ret = regmap_read(regmap, reg_ofs_cur, &hw_ptr);
112 	if (ret || hw_ptr == 0) {
113 		dev_err(dev, "%s hw_ptr err\n", __func__);
114 		pcm_ptr_bytes = 0;
115 		goto POINTER_RETURN_FRAMES;
116 	}
117 
118 	ret = regmap_read(regmap, reg_ofs_base, &hw_base);
119 	if (ret || hw_base == 0) {
120 		dev_err(dev, "%s hw_ptr err\n", __func__);
121 		pcm_ptr_bytes = 0;
122 		goto POINTER_RETURN_FRAMES;
123 	}
124 
125 	pcm_ptr_bytes = hw_ptr - hw_base;
126 
127 POINTER_RETURN_FRAMES:
128 	return bytes_to_frames(substream->runtime, pcm_ptr_bytes);
129 }
130 
131 const struct snd_pcm_ops mtk_afe_pcm_ops = {
132 	.ioctl = snd_pcm_lib_ioctl,
133 	.pointer = mtk_afe_pcm_pointer,
134 };
135 EXPORT_SYMBOL_GPL(mtk_afe_pcm_ops);
136 
137 int mtk_afe_pcm_new(struct snd_soc_pcm_runtime *rtd)
138 {
139 	size_t size;
140 	struct snd_pcm *pcm = rtd->pcm;
141 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
142 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
143 
144 	size = afe->mtk_afe_hardware->buffer_bytes_max;
145 	return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
146 						     afe->dev,
147 						     size, size);
148 }
149 EXPORT_SYMBOL_GPL(mtk_afe_pcm_new);
150 
151 void mtk_afe_pcm_free(struct snd_pcm *pcm)
152 {
153 	snd_pcm_lib_preallocate_free_for_all(pcm);
154 }
155 EXPORT_SYMBOL_GPL(mtk_afe_pcm_free);
156 
157 const struct snd_soc_component_driver mtk_afe_pcm_platform = {
158 	.name = AFE_PCM_NAME,
159 	.ops = &mtk_afe_pcm_ops,
160 	.pcm_new = mtk_afe_pcm_new,
161 	.pcm_free = mtk_afe_pcm_free,
162 };
163 EXPORT_SYMBOL_GPL(mtk_afe_pcm_platform);
164 
165 MODULE_DESCRIPTION("Mediatek simple platform driver");
166 MODULE_AUTHOR("Garlic Tseng <garlic.tseng@mediatek.com>");
167 MODULE_LICENSE("GPL v2");
168 
169