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