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