xref: /openbmc/linux/sound/soc/codecs/rl6231.c (revision b8aa7dc5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * rl6231.c - RL6231 class device shared support
4  *
5  * Copyright 2014 Realtek Semiconductor Corp.
6  *
7  * Author: Oder Chiou <oder_chiou@realtek.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12 
13 #include <linux/gcd.h>
14 #include "rl6231.h"
15 
16 /**
17  * rl6231_get_pre_div - Return the value of pre divider.
18  *
19  * @map: map for setting.
20  * @reg: register.
21  * @sft: shift.
22  *
23  * Return the value of pre divider from given register value.
24  * Return negative error code for unexpected register value.
25  */
26 int rl6231_get_pre_div(struct regmap *map, unsigned int reg, int sft)
27 {
28 	int pd, val;
29 
30 	regmap_read(map, reg, &val);
31 
32 	val = (val >> sft) & 0x7;
33 
34 	switch (val) {
35 	case 0:
36 	case 1:
37 	case 2:
38 	case 3:
39 		pd = val + 1;
40 		break;
41 	case 4:
42 		pd = 6;
43 		break;
44 	case 5:
45 		pd = 8;
46 		break;
47 	case 6:
48 		pd = 12;
49 		break;
50 	case 7:
51 		pd = 16;
52 		break;
53 	default:
54 		pd = -EINVAL;
55 		break;
56 	}
57 
58 	return pd;
59 }
60 EXPORT_SYMBOL_GPL(rl6231_get_pre_div);
61 
62 /**
63  * rl6231_calc_dmic_clk - Calculate the frequency divider parameter of dmic.
64  *
65  * @rate: base clock rate.
66  *
67  * Choose divider parameter that gives the highest possible DMIC frequency in
68  * 1MHz - 3MHz range.
69  */
70 int rl6231_calc_dmic_clk(int rate)
71 {
72 	static const int div[] = {2, 3, 4, 6, 8, 12};
73 	int i;
74 
75 	if (rate < 1000000 * div[0]) {
76 		pr_warn("Base clock rate %d is too low\n", rate);
77 		return -EINVAL;
78 	}
79 
80 	for (i = 0; i < ARRAY_SIZE(div); i++) {
81 		if ((div[i] % 3) == 0)
82 			continue;
83 		/* find divider that gives DMIC frequency below 1.536MHz */
84 		if (1536000 * div[i] >= rate)
85 			return i;
86 	}
87 
88 	pr_warn("Base clock rate %d is too high\n", rate);
89 	return -EINVAL;
90 }
91 EXPORT_SYMBOL_GPL(rl6231_calc_dmic_clk);
92 
93 struct pll_calc_map {
94 	unsigned int pll_in;
95 	unsigned int pll_out;
96 	int k;
97 	int n;
98 	int m;
99 	bool m_bp;
100 	bool k_bp;
101 };
102 
103 static const struct pll_calc_map pll_preset_table[] = {
104 	{19200000,  4096000,  23, 14, 1, false, false},
105 	{19200000,  24576000,  3, 30, 3, false, false},
106 	{3840000,   24576000,  3, 30, 0, true, false},
107 };
108 
109 static unsigned int find_best_div(unsigned int in,
110 	unsigned int max, unsigned int div)
111 {
112 	unsigned int d;
113 
114 	if (in <= max)
115 		return 1;
116 
117 	d = in / max;
118 	if (in % max)
119 		d++;
120 
121 	while (div % d != 0)
122 		d++;
123 
124 
125 	return d;
126 }
127 
128 /**
129  * rl6231_pll_calc - Calcualte PLL M/N/K code.
130  * @freq_in: external clock provided to codec.
131  * @freq_out: target clock which codec works on.
132  * @pll_code: Pointer to structure with M, N, K, m_bypass and k_bypass flag.
133  *
134  * Calcualte M/N/K code to configure PLL for codec.
135  *
136  * Returns 0 for success or negative error code.
137  */
138 int rl6231_pll_calc(const unsigned int freq_in,
139 	const unsigned int freq_out, struct rl6231_pll_code *pll_code)
140 {
141 	int max_n = RL6231_PLL_N_MAX, max_m = RL6231_PLL_M_MAX;
142 	int i, k, n_t;
143 	int k_t, min_k, max_k, n = 0, m = 0, m_t = 0;
144 	unsigned int red, pll_out, in_t, out_t, div, div_t;
145 	unsigned int red_t = abs(freq_out - freq_in);
146 	unsigned int f_in, f_out, f_max;
147 	bool m_bypass = false, k_bypass = false;
148 
149 	if (RL6231_PLL_INP_MAX < freq_in || RL6231_PLL_INP_MIN > freq_in)
150 		return -EINVAL;
151 
152 	for (i = 0; i < ARRAY_SIZE(pll_preset_table); i++) {
153 		if (freq_in == pll_preset_table[i].pll_in &&
154 			freq_out == pll_preset_table[i].pll_out) {
155 			k = pll_preset_table[i].k;
156 			m = pll_preset_table[i].m;
157 			n = pll_preset_table[i].n;
158 			m_bypass = pll_preset_table[i].m_bp;
159 			k_bypass = pll_preset_table[i].k_bp;
160 			pr_debug("Use preset PLL parameter table\n");
161 			goto code_find;
162 		}
163 	}
164 
165 	min_k = 80000000 / freq_out - 2;
166 	max_k = 150000000 / freq_out - 2;
167 	if (max_k > RL6231_PLL_K_MAX)
168 		max_k = RL6231_PLL_K_MAX;
169 	if (min_k > RL6231_PLL_K_MAX)
170 		min_k = max_k = RL6231_PLL_K_MAX;
171 	div_t = gcd(freq_in, freq_out);
172 	f_max = 0xffffffff / RL6231_PLL_N_MAX;
173 	div = find_best_div(freq_in, f_max, div_t);
174 	f_in = freq_in / div;
175 	f_out = freq_out / div;
176 	k = min_k;
177 	if (min_k < -1)
178 		min_k = -1;
179 	for (k_t = min_k; k_t <= max_k; k_t++) {
180 		for (n_t = 0; n_t <= max_n; n_t++) {
181 			in_t = f_in * (n_t + 2);
182 			pll_out = f_out * (k_t + 2);
183 			if (in_t == pll_out) {
184 				m_bypass = true;
185 				n = n_t;
186 				k = k_t;
187 				goto code_find;
188 			}
189 			out_t = in_t / (k_t + 2);
190 			red = abs(f_out - out_t);
191 			if (red < red_t) {
192 				m_bypass = true;
193 				n = n_t;
194 				m = 0;
195 				k = k_t;
196 				if (red == 0)
197 					goto code_find;
198 				red_t = red;
199 			}
200 			for (m_t = 0; m_t <= max_m; m_t++) {
201 				out_t = in_t / ((m_t + 2) * (k_t + 2));
202 				red = abs(f_out - out_t);
203 				if (red < red_t) {
204 					m_bypass = false;
205 					n = n_t;
206 					m = m_t;
207 					k = k_t;
208 					if (red == 0)
209 						goto code_find;
210 					red_t = red;
211 				}
212 			}
213 		}
214 	}
215 	pr_debug("Only get approximation about PLL\n");
216 
217 code_find:
218 	if (k == -1) {
219 		k_bypass = true;
220 		k = 0;
221 	}
222 
223 	pll_code->m_bp = m_bypass;
224 	pll_code->k_bp = k_bypass;
225 	pll_code->m_code = m;
226 	pll_code->n_code = n;
227 	pll_code->k_code = k;
228 	return 0;
229 }
230 EXPORT_SYMBOL_GPL(rl6231_pll_calc);
231 
232 int rl6231_get_clk_info(int sclk, int rate)
233 {
234 	int i;
235 	static const int pd[] = {1, 2, 3, 4, 6, 8, 12, 16};
236 
237 	if (sclk <= 0 || rate <= 0)
238 		return -EINVAL;
239 
240 	rate = rate << 8;
241 	for (i = 0; i < ARRAY_SIZE(pd); i++)
242 		if (sclk == rate * pd[i])
243 			return i;
244 
245 	return -EINVAL;
246 }
247 EXPORT_SYMBOL_GPL(rl6231_get_clk_info);
248 
249 MODULE_DESCRIPTION("RL6231 class device shared support");
250 MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
251 MODULE_LICENSE("GPL v2");
252