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