blob: 7ef3b5476bcc2ecc3f291d151e99ce534230183e [file] [log] [blame]
Oder Chiou49ef7922014-05-20 15:01:53 +08001/*
2 * rl6231.c - RL6231 class device shared support
3 *
4 * Copyright 2014 Realtek Semiconductor Corp.
5 *
6 * Author: Oder Chiou <oder_chiou@realtek.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
Oder Chiou00a6d6e52015-08-05 10:03:18 +080014#include <linux/regmap.h>
Oder Chiou49ef7922014-05-20 15:01:53 +080015
Bard Liao2f8aab32017-11-27 20:03:14 +080016#include <linux/gcd.h>
Oder Chiou49ef7922014-05-20 15:01:53 +080017#include "rl6231.h"
18
19/**
Oder Chiou00a6d6e52015-08-05 10:03:18 +080020 * rl6231_get_pre_div - Return the value of pre divider.
21 *
22 * @map: map for setting.
23 * @reg: register.
24 * @sft: shift.
25 *
26 * Return the value of pre divider from given register value.
27 * Return negative error code for unexpected register value.
28 */
29int rl6231_get_pre_div(struct regmap *map, unsigned int reg, int sft)
30{
31 int pd, val;
32
33 regmap_read(map, reg, &val);
34
35 val = (val >> sft) & 0x7;
36
37 switch (val) {
38 case 0:
39 case 1:
40 case 2:
41 case 3:
42 pd = val + 1;
43 break;
44 case 4:
45 pd = 6;
46 break;
47 case 5:
48 pd = 8;
49 break;
50 case 6:
51 pd = 12;
52 break;
53 case 7:
54 pd = 16;
55 break;
56 default:
57 pd = -EINVAL;
58 break;
59 }
60
61 return pd;
62}
63EXPORT_SYMBOL_GPL(rl6231_get_pre_div);
64
65/**
Anatol Pomozovac1125d2015-08-05 14:58:33 -070066 * rl6231_calc_dmic_clk - Calculate the frequency divider parameter of dmic.
Oder Chiou49ef7922014-05-20 15:01:53 +080067 *
68 * @rate: base clock rate.
69 *
Anatol Pomozovac1125d2015-08-05 14:58:33 -070070 * Choose divider parameter that gives the highest possible DMIC frequency in
71 * 1MHz - 3MHz range.
Oder Chiou49ef7922014-05-20 15:01:53 +080072 */
73int rl6231_calc_dmic_clk(int rate)
74{
Colin Ian King57f7fef2017-09-19 22:50:00 +010075 static const int div[] = {2, 3, 4, 6, 8, 12};
Anatol Pomozovac1125d2015-08-05 14:58:33 -070076 int i;
Oder Chiou49ef7922014-05-20 15:01:53 +080077
Anatol Pomozovac1125d2015-08-05 14:58:33 -070078 if (rate < 1000000 * div[0]) {
79 pr_warn("Base clock rate %d is too low\n", rate);
80 return -EINVAL;
Oder Chiou49ef7922014-05-20 15:01:53 +080081 }
82
Anatol Pomozovac1125d2015-08-05 14:58:33 -070083 for (i = 0; i < ARRAY_SIZE(div); i++) {
Bard Liao2f64b6e2015-11-10 14:40:55 +080084 if ((div[i] % 3) == 0)
85 continue;
John Lin7336dce2015-11-16 14:41:07 +080086 /* find divider that gives DMIC frequency below 3.072MHz */
87 if (3072000 * div[i] >= rate)
Anatol Pomozovac1125d2015-08-05 14:58:33 -070088 return i;
89 }
90
91 pr_warn("Base clock rate %d is too high\n", rate);
92 return -EINVAL;
Oder Chiou49ef7922014-05-20 15:01:53 +080093}
94EXPORT_SYMBOL_GPL(rl6231_calc_dmic_clk);
95
Bard Liao213213d2015-07-22 13:09:15 +080096struct pll_calc_map {
97 unsigned int pll_in;
98 unsigned int pll_out;
99 int k;
100 int n;
101 int m;
102 bool m_bp;
103};
104
105static const struct pll_calc_map pll_preset_table[] = {
Bard Liao59b01132016-11-15 15:25:50 +0800106 {19200000, 4096000, 23, 14, 1, false},
Bard Liao213213d2015-07-22 13:09:15 +0800107 {19200000, 24576000, 3, 30, 3, false},
108};
109
Bard Liao2f8aab32017-11-27 20:03:14 +0800110static unsigned int find_best_div(unsigned int in,
111 unsigned int max, unsigned int div)
112{
113 unsigned int d;
114
115 if (in <= max)
116 return 1;
117
118 d = in / max;
119 if (in % max)
120 d++;
121
122 while (div % d != 0)
123 d++;
124
125
126 return d;
127}
128
Oder Chiou71c7a2d2014-05-20 15:01:54 +0800129/**
130 * rl6231_pll_calc - Calcualte PLL M/N/K code.
131 * @freq_in: external clock provided to codec.
132 * @freq_out: target clock which codec works on.
133 * @pll_code: Pointer to structure with M, N, K and bypass flag.
134 *
135 * Calcualte M/N/K code to configure PLL for codec.
136 *
137 * Returns 0 for success or negative error code.
138 */
139int rl6231_pll_calc(const unsigned int freq_in,
140 const unsigned int freq_out, struct rl6231_pll_code *pll_code)
141{
142 int max_n = RL6231_PLL_N_MAX, max_m = RL6231_PLL_M_MAX;
Bard Liao2f8aab32017-11-27 20:03:14 +0800143 int i, k, n_t;
144 int k_t, min_k, max_k, n = 0, m = 0, m_t = 0;
145 unsigned int red, pll_out, in_t, out_t, div, div_t;
146 unsigned int red_t = abs(freq_out - freq_in);
147 unsigned int f_in, f_out, f_max;
Oder Chiou71c7a2d2014-05-20 15:01:54 +0800148 bool bypass = false;
149
150 if (RL6231_PLL_INP_MAX < freq_in || RL6231_PLL_INP_MIN > freq_in)
151 return -EINVAL;
152
Bard Liao213213d2015-07-22 13:09:15 +0800153 for (i = 0; i < ARRAY_SIZE(pll_preset_table); i++) {
154 if (freq_in == pll_preset_table[i].pll_in &&
155 freq_out == pll_preset_table[i].pll_out) {
156 k = pll_preset_table[i].k;
157 m = pll_preset_table[i].m;
158 n = pll_preset_table[i].n;
159 bypass = pll_preset_table[i].m_bp;
160 pr_debug("Use preset PLL parameter table\n");
161 goto code_find;
162 }
163 }
164
Bard Liao2f8aab32017-11-27 20:03:14 +0800165 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 for (k_t = min_k; k_t <= max_k; k_t++) {
178 for (n_t = 0; n_t <= max_n; n_t++) {
179 in_t = f_in * (n_t + 2);
180 pll_out = f_out * (k_t + 2);
Bard Liao2f8aab32017-11-27 20:03:14 +0800181 if (in_t == pll_out) {
182 bypass = true;
Oder Chiou71c7a2d2014-05-20 15:01:54 +0800183 n = n_t;
Bard Liao2f8aab32017-11-27 20:03:14 +0800184 k = k_t;
185 goto code_find;
186 }
187 out_t = in_t / (k_t + 2);
188 red = abs(f_out - out_t);
189 if (red < red_t) {
190 bypass = true;
191 n = n_t;
192 m = 0;
193 k = k_t;
Oder Chiou71c7a2d2014-05-20 15:01:54 +0800194 if (red == 0)
195 goto code_find;
196 red_t = red;
197 }
Bard Liao2f8aab32017-11-27 20:03:14 +0800198 for (m_t = 0; m_t <= max_m; m_t++) {
199 out_t = in_t / ((m_t + 2) * (k_t + 2));
200 red = abs(f_out - out_t);
201 if (red < red_t) {
202 bypass = false;
203 n = n_t;
204 m = m_t;
205 k = k_t;
206 if (red == 0)
207 goto code_find;
208 red_t = red;
209 }
210 }
Oder Chiou71c7a2d2014-05-20 15:01:54 +0800211 }
212 }
213 pr_debug("Only get approximation about PLL\n");
214
215code_find:
216
217 pll_code->m_bp = bypass;
218 pll_code->m_code = m;
219 pll_code->n_code = n;
220 pll_code->k_code = k;
221 return 0;
222}
223EXPORT_SYMBOL_GPL(rl6231_pll_calc);
224
Oder Chioud92950e2014-05-20 15:01:55 +0800225int rl6231_get_clk_info(int sclk, int rate)
226{
Colin Ian King57f7fef2017-09-19 22:50:00 +0100227 int i;
228 static const int pd[] = {1, 2, 3, 4, 6, 8, 12, 16};
Oder Chioud92950e2014-05-20 15:01:55 +0800229
230 if (sclk <= 0 || rate <= 0)
231 return -EINVAL;
232
233 rate = rate << 8;
234 for (i = 0; i < ARRAY_SIZE(pd); i++)
235 if (sclk == rate * pd[i])
236 return i;
237
238 return -EINVAL;
239}
240EXPORT_SYMBOL_GPL(rl6231_get_clk_info);
241
Oder Chiou49ef7922014-05-20 15:01:53 +0800242MODULE_DESCRIPTION("RL6231 class device shared support");
243MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
244MODULE_LICENSE("GPL v2");