1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // MediaTek ALSA SoC Audio Control
4 //
5 // Copyright (c) 2022 MediaTek Inc.
6 // Author: Jiaxin Yu <jiaxin.yu@mediatek.com>
7 
8 #include "mt8186-afe-common.h"
9 
10 enum {
11 	MTK_AFE_RATE_8K = 0,
12 	MTK_AFE_RATE_11K,
13 	MTK_AFE_RATE_12K,
14 	MTK_AFE_RATE_384K,
15 	MTK_AFE_RATE_16K,
16 	MTK_AFE_RATE_22K,
17 	MTK_AFE_RATE_24K,
18 	MTK_AFE_RATE_352K,
19 	MTK_AFE_RATE_32K,
20 	MTK_AFE_RATE_44K,
21 	MTK_AFE_RATE_48K,
22 	MTK_AFE_RATE_88K,
23 	MTK_AFE_RATE_96K,
24 	MTK_AFE_RATE_176K,
25 	MTK_AFE_RATE_192K,
26 	MTK_AFE_RATE_260K,
27 };
28 
29 enum {
30 	MTK_AFE_PCM_RATE_8K = 0,
31 	MTK_AFE_PCM_RATE_16K,
32 	MTK_AFE_PCM_RATE_32K,
33 	MTK_AFE_PCM_RATE_48K,
34 };
35 
36 enum {
37 	MTK_AFE_TDM_RATE_8K = 0,
38 	MTK_AFE_TDM_RATE_12K,
39 	MTK_AFE_TDM_RATE_16K,
40 	MTK_AFE_TDM_RATE_24K,
41 	MTK_AFE_TDM_RATE_32K,
42 	MTK_AFE_TDM_RATE_48K,
43 	MTK_AFE_TDM_RATE_64K,
44 	MTK_AFE_TDM_RATE_96K,
45 	MTK_AFE_TDM_RATE_128K,
46 	MTK_AFE_TDM_RATE_192K,
47 	MTK_AFE_TDM_RATE_256K,
48 	MTK_AFE_TDM_RATE_384K,
49 	MTK_AFE_TDM_RATE_11K,
50 	MTK_AFE_TDM_RATE_22K,
51 	MTK_AFE_TDM_RATE_44K,
52 	MTK_AFE_TDM_RATE_88K,
53 	MTK_AFE_TDM_RATE_176K,
54 	MTK_AFE_TDM_RATE_352K,
55 };
56 
57 enum {
58 	MTK_AFE_TDM_RELATCH_RATE_8K = 0,
59 	MTK_AFE_TDM_RELATCH_RATE_11K,
60 	MTK_AFE_TDM_RELATCH_RATE_12K,
61 	MTK_AFE_TDM_RELATCH_RATE_16K,
62 	MTK_AFE_TDM_RELATCH_RATE_22K,
63 	MTK_AFE_TDM_RELATCH_RATE_24K,
64 	MTK_AFE_TDM_RELATCH_RATE_32K,
65 	MTK_AFE_TDM_RELATCH_RATE_44K,
66 	MTK_AFE_TDM_RELATCH_RATE_48K,
67 	MTK_AFE_TDM_RELATCH_RATE_88K,
68 	MTK_AFE_TDM_RELATCH_RATE_96K,
69 	MTK_AFE_TDM_RELATCH_RATE_176K,
70 	MTK_AFE_TDM_RELATCH_RATE_192K,
71 	MTK_AFE_TDM_RELATCH_RATE_352K,
72 	MTK_AFE_TDM_RELATCH_RATE_384K,
73 };
74 
mt8186_general_rate_transform(struct device * dev,unsigned int rate)75 unsigned int mt8186_general_rate_transform(struct device *dev, unsigned int rate)
76 {
77 	switch (rate) {
78 	case 8000:
79 		return MTK_AFE_RATE_8K;
80 	case 11025:
81 		return MTK_AFE_RATE_11K;
82 	case 12000:
83 		return MTK_AFE_RATE_12K;
84 	case 16000:
85 		return MTK_AFE_RATE_16K;
86 	case 22050:
87 		return MTK_AFE_RATE_22K;
88 	case 24000:
89 		return MTK_AFE_RATE_24K;
90 	case 32000:
91 		return MTK_AFE_RATE_32K;
92 	case 44100:
93 		return MTK_AFE_RATE_44K;
94 	case 48000:
95 		return MTK_AFE_RATE_48K;
96 	case 88200:
97 		return MTK_AFE_RATE_88K;
98 	case 96000:
99 		return MTK_AFE_RATE_96K;
100 	case 176400:
101 		return MTK_AFE_RATE_176K;
102 	case 192000:
103 		return MTK_AFE_RATE_192K;
104 	case 260000:
105 		return MTK_AFE_RATE_260K;
106 	case 352800:
107 		return MTK_AFE_RATE_352K;
108 	case 384000:
109 		return MTK_AFE_RATE_384K;
110 	default:
111 		dev_err(dev, "%s(), rate %u invalid, use %d!!!\n",
112 			__func__, rate, MTK_AFE_RATE_48K);
113 	}
114 
115 	return MTK_AFE_RATE_48K;
116 }
117 
tdm_rate_transform(struct device * dev,unsigned int rate)118 static unsigned int tdm_rate_transform(struct device *dev, unsigned int rate)
119 {
120 	switch (rate) {
121 	case 8000:
122 		return MTK_AFE_TDM_RATE_8K;
123 	case 11025:
124 		return MTK_AFE_TDM_RATE_11K;
125 	case 12000:
126 		return MTK_AFE_TDM_RATE_12K;
127 	case 16000:
128 		return MTK_AFE_TDM_RATE_16K;
129 	case 22050:
130 		return MTK_AFE_TDM_RATE_22K;
131 	case 24000:
132 		return MTK_AFE_TDM_RATE_24K;
133 	case 32000:
134 		return MTK_AFE_TDM_RATE_32K;
135 	case 44100:
136 		return MTK_AFE_TDM_RATE_44K;
137 	case 48000:
138 		return MTK_AFE_TDM_RATE_48K;
139 	case 64000:
140 		return MTK_AFE_TDM_RATE_64K;
141 	case 88200:
142 		return MTK_AFE_TDM_RATE_88K;
143 	case 96000:
144 		return MTK_AFE_TDM_RATE_96K;
145 	case 128000:
146 		return MTK_AFE_TDM_RATE_128K;
147 	case 176400:
148 		return MTK_AFE_TDM_RATE_176K;
149 	case 192000:
150 		return MTK_AFE_TDM_RATE_192K;
151 	case 256000:
152 		return MTK_AFE_TDM_RATE_256K;
153 	case 352800:
154 		return MTK_AFE_TDM_RATE_352K;
155 	case 384000:
156 		return MTK_AFE_TDM_RATE_384K;
157 	default:
158 		dev_err(dev, "%s(), rate %u invalid, use %d!!!\n",
159 			__func__, rate, MTK_AFE_TDM_RATE_48K);
160 	}
161 
162 	return MTK_AFE_TDM_RATE_48K;
163 }
164 
pcm_rate_transform(struct device * dev,unsigned int rate)165 static unsigned int pcm_rate_transform(struct device *dev, unsigned int rate)
166 {
167 	switch (rate) {
168 	case 8000:
169 		return MTK_AFE_PCM_RATE_8K;
170 	case 16000:
171 		return MTK_AFE_PCM_RATE_16K;
172 	case 32000:
173 		return MTK_AFE_PCM_RATE_32K;
174 	case 48000:
175 		return MTK_AFE_PCM_RATE_48K;
176 	default:
177 		dev_err(dev, "%s(), rate %u invalid, use %d!!!\n",
178 			__func__, rate, MTK_AFE_PCM_RATE_48K);
179 	}
180 
181 	return MTK_AFE_PCM_RATE_48K;
182 }
183 
mt8186_tdm_relatch_rate_transform(struct device * dev,unsigned int rate)184 unsigned int mt8186_tdm_relatch_rate_transform(struct device *dev, unsigned int rate)
185 {
186 	switch (rate) {
187 	case 8000:
188 		return MTK_AFE_TDM_RELATCH_RATE_8K;
189 	case 11025:
190 		return MTK_AFE_TDM_RELATCH_RATE_11K;
191 	case 12000:
192 		return MTK_AFE_TDM_RELATCH_RATE_12K;
193 	case 16000:
194 		return MTK_AFE_TDM_RELATCH_RATE_16K;
195 	case 22050:
196 		return MTK_AFE_TDM_RELATCH_RATE_22K;
197 	case 24000:
198 		return MTK_AFE_TDM_RELATCH_RATE_24K;
199 	case 32000:
200 		return MTK_AFE_TDM_RELATCH_RATE_32K;
201 	case 44100:
202 		return MTK_AFE_TDM_RELATCH_RATE_44K;
203 	case 48000:
204 		return MTK_AFE_TDM_RELATCH_RATE_48K;
205 	case 88200:
206 		return MTK_AFE_TDM_RELATCH_RATE_88K;
207 	case 96000:
208 		return MTK_AFE_TDM_RELATCH_RATE_96K;
209 	case 176400:
210 		return MTK_AFE_TDM_RELATCH_RATE_176K;
211 	case 192000:
212 		return MTK_AFE_TDM_RELATCH_RATE_192K;
213 	case 352800:
214 		return MTK_AFE_TDM_RELATCH_RATE_352K;
215 	case 384000:
216 		return MTK_AFE_TDM_RELATCH_RATE_384K;
217 	default:
218 		dev_err(dev, "%s(), rate %u invalid, use %d!!!\n",
219 			__func__, rate, MTK_AFE_TDM_RELATCH_RATE_48K);
220 	}
221 
222 	return MTK_AFE_TDM_RELATCH_RATE_48K;
223 }
224 
mt8186_rate_transform(struct device * dev,unsigned int rate,int aud_blk)225 unsigned int mt8186_rate_transform(struct device *dev, unsigned int rate, int aud_blk)
226 {
227 	switch (aud_blk) {
228 	case MT8186_DAI_PCM:
229 		return pcm_rate_transform(dev, rate);
230 	case MT8186_DAI_TDM_IN:
231 		return tdm_rate_transform(dev, rate);
232 	default:
233 		return mt8186_general_rate_transform(dev, rate);
234 	}
235 }
236 
mt8186_dai_set_priv(struct mtk_base_afe * afe,int id,int priv_size,const void * priv_data)237 int mt8186_dai_set_priv(struct mtk_base_afe *afe, int id, int priv_size, const void *priv_data)
238 {
239 	struct mt8186_afe_private *afe_priv = afe->platform_priv;
240 	void *temp_data;
241 
242 	temp_data = devm_kzalloc(afe->dev,
243 				 priv_size,
244 				 GFP_KERNEL);
245 	if (!temp_data)
246 		return -ENOMEM;
247 
248 	if (priv_data)
249 		memcpy(temp_data, priv_data, priv_size);
250 
251 	afe_priv->dai_priv[id] = temp_data;
252 
253 	return 0;
254 }
255