1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // MediaTek ALSA SoC Audio Control
4 //
5 // Copyright (c) 2020 MediaTek Inc.
6 // Author: Shane Chien <shane.chien@mediatek.com>
7 //
8 
9 #include "mt8192-afe-common.h"
10 
11 enum {
12 	MTK_AFE_RATE_8K = 0,
13 	MTK_AFE_RATE_11K = 1,
14 	MTK_AFE_RATE_12K = 2,
15 	MTK_AFE_RATE_384K = 3,
16 	MTK_AFE_RATE_16K = 4,
17 	MTK_AFE_RATE_22K = 5,
18 	MTK_AFE_RATE_24K = 6,
19 	MTK_AFE_RATE_352K = 7,
20 	MTK_AFE_RATE_32K = 8,
21 	MTK_AFE_RATE_44K = 9,
22 	MTK_AFE_RATE_48K = 10,
23 	MTK_AFE_RATE_88K = 11,
24 	MTK_AFE_RATE_96K = 12,
25 	MTK_AFE_RATE_176K = 13,
26 	MTK_AFE_RATE_192K = 14,
27 	MTK_AFE_RATE_260K = 15,
28 };
29 
30 enum {
31 	MTK_AFE_DAI_MEMIF_RATE_8K = 0,
32 	MTK_AFE_DAI_MEMIF_RATE_16K = 1,
33 	MTK_AFE_DAI_MEMIF_RATE_32K = 2,
34 	MTK_AFE_DAI_MEMIF_RATE_48K = 3,
35 };
36 
37 enum {
38 	MTK_AFE_PCM_RATE_8K = 0,
39 	MTK_AFE_PCM_RATE_16K = 1,
40 	MTK_AFE_PCM_RATE_32K = 2,
41 	MTK_AFE_PCM_RATE_48K = 3,
42 };
43 
44 unsigned int mt8192_general_rate_transform(struct device *dev,
45 					   unsigned int rate)
46 {
47 	switch (rate) {
48 	case 8000:
49 		return MTK_AFE_RATE_8K;
50 	case 11025:
51 		return MTK_AFE_RATE_11K;
52 	case 12000:
53 		return MTK_AFE_RATE_12K;
54 	case 16000:
55 		return MTK_AFE_RATE_16K;
56 	case 22050:
57 		return MTK_AFE_RATE_22K;
58 	case 24000:
59 		return MTK_AFE_RATE_24K;
60 	case 32000:
61 		return MTK_AFE_RATE_32K;
62 	case 44100:
63 		return MTK_AFE_RATE_44K;
64 	case 48000:
65 		return MTK_AFE_RATE_48K;
66 	case 88200:
67 		return MTK_AFE_RATE_88K;
68 	case 96000:
69 		return MTK_AFE_RATE_96K;
70 	case 176400:
71 		return MTK_AFE_RATE_176K;
72 	case 192000:
73 		return MTK_AFE_RATE_192K;
74 	case 260000:
75 		return MTK_AFE_RATE_260K;
76 	case 352800:
77 		return MTK_AFE_RATE_352K;
78 	case 384000:
79 		return MTK_AFE_RATE_384K;
80 	default:
81 		dev_warn(dev, "%s(), rate %u invalid, use %d!!!\n",
82 			 __func__,
83 			 rate, MTK_AFE_RATE_48K);
84 		return MTK_AFE_RATE_48K;
85 	}
86 }
87 
88 static unsigned int dai_memif_rate_transform(struct device *dev,
89 					     unsigned int rate)
90 {
91 	switch (rate) {
92 	case 8000:
93 		return MTK_AFE_DAI_MEMIF_RATE_8K;
94 	case 16000:
95 		return MTK_AFE_DAI_MEMIF_RATE_16K;
96 	case 32000:
97 		return MTK_AFE_DAI_MEMIF_RATE_32K;
98 	case 48000:
99 		return MTK_AFE_DAI_MEMIF_RATE_48K;
100 	default:
101 		dev_warn(dev, "%s(), rate %u invalid, use %d!!!\n",
102 			 __func__,
103 			 rate, MTK_AFE_DAI_MEMIF_RATE_16K);
104 		return MTK_AFE_DAI_MEMIF_RATE_16K;
105 	}
106 }
107 
108 static unsigned int pcm_rate_transform(struct device *dev,
109 				       unsigned int rate)
110 {
111 	switch (rate) {
112 	case 8000:
113 		return MTK_AFE_PCM_RATE_8K;
114 	case 16000:
115 		return MTK_AFE_PCM_RATE_16K;
116 	case 32000:
117 		return MTK_AFE_PCM_RATE_32K;
118 	case 48000:
119 		return MTK_AFE_PCM_RATE_48K;
120 	default:
121 		dev_warn(dev, "%s(), rate %u invalid, use %d!!!\n",
122 			 __func__,
123 			 rate, MTK_AFE_PCM_RATE_32K);
124 		return MTK_AFE_PCM_RATE_32K;
125 	}
126 }
127 
128 unsigned int mt8192_rate_transform(struct device *dev,
129 				   unsigned int rate, int aud_blk)
130 {
131 	switch (aud_blk) {
132 	case MT8192_MEMIF_DAI:
133 	case MT8192_MEMIF_MOD_DAI:
134 		return dai_memif_rate_transform(dev, rate);
135 	case MT8192_DAI_PCM_1:
136 	case MT8192_DAI_PCM_2:
137 		return pcm_rate_transform(dev, rate);
138 	default:
139 		return mt8192_general_rate_transform(dev, rate);
140 	}
141 }
142 
143 int mt8192_dai_set_priv(struct mtk_base_afe *afe, int id,
144 			int priv_size, const void *priv_data)
145 {
146 	struct mt8192_afe_private *afe_priv = afe->platform_priv;
147 	void *temp_data;
148 
149 	temp_data = devm_kzalloc(afe->dev,
150 				 priv_size,
151 				 GFP_KERNEL);
152 	if (!temp_data)
153 		return -ENOMEM;
154 
155 	if (priv_data)
156 		memcpy(temp_data, priv_data, priv_size);
157 
158 	afe_priv->dai_priv[id] = temp_data;
159 
160 	return 0;
161 }
162