blob: 96f3e906dfefe6718693ab73d3b0b1680732a0f7 [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 Chiou49ef7922014-05-20 15:01:53 +080014
15#include "rl6231.h"
16
17/**
18 * rl6231_calc_dmic_clk - Calculate the parameter of dmic.
19 *
20 * @rate: base clock rate.
21 *
22 * Choose dmic clock between 1MHz and 3MHz.
23 * It is better for clock to approximate 3MHz.
24 */
25int rl6231_calc_dmic_clk(int rate)
26{
27 int div[] = {2, 3, 4, 6, 8, 12}, idx = -EINVAL;
28 int i, red, bound, temp;
29
30 red = 3000000 * 12;
31 for (i = 0; i < ARRAY_SIZE(div); i++) {
32 bound = div[i] * 3000000;
33 if (rate > bound)
34 continue;
35 temp = bound - rate;
36 if (temp < red) {
37 red = temp;
38 idx = i;
39 }
40 }
41
42 return idx;
43}
44EXPORT_SYMBOL_GPL(rl6231_calc_dmic_clk);
45
Bard Liao213213d2015-07-22 13:09:15 +080046struct pll_calc_map {
47 unsigned int pll_in;
48 unsigned int pll_out;
49 int k;
50 int n;
51 int m;
52 bool m_bp;
53};
54
55static const struct pll_calc_map pll_preset_table[] = {
56 {19200000, 24576000, 3, 30, 3, false},
57};
58
Oder Chiou71c7a2d2014-05-20 15:01:54 +080059/**
60 * rl6231_pll_calc - Calcualte PLL M/N/K code.
61 * @freq_in: external clock provided to codec.
62 * @freq_out: target clock which codec works on.
63 * @pll_code: Pointer to structure with M, N, K and bypass flag.
64 *
65 * Calcualte M/N/K code to configure PLL for codec.
66 *
67 * Returns 0 for success or negative error code.
68 */
69int rl6231_pll_calc(const unsigned int freq_in,
70 const unsigned int freq_out, struct rl6231_pll_code *pll_code)
71{
72 int max_n = RL6231_PLL_N_MAX, max_m = RL6231_PLL_M_MAX;
Bard Liao213213d2015-07-22 13:09:15 +080073 int i, k, red, n_t, pll_out, in_t, out_t;
Oder Chiou71c7a2d2014-05-20 15:01:54 +080074 int n = 0, m = 0, m_t = 0;
75 int red_t = abs(freq_out - freq_in);
76 bool bypass = false;
77
78 if (RL6231_PLL_INP_MAX < freq_in || RL6231_PLL_INP_MIN > freq_in)
79 return -EINVAL;
80
Bard Liao213213d2015-07-22 13:09:15 +080081 for (i = 0; i < ARRAY_SIZE(pll_preset_table); i++) {
82 if (freq_in == pll_preset_table[i].pll_in &&
83 freq_out == pll_preset_table[i].pll_out) {
84 k = pll_preset_table[i].k;
85 m = pll_preset_table[i].m;
86 n = pll_preset_table[i].n;
87 bypass = pll_preset_table[i].m_bp;
88 pr_debug("Use preset PLL parameter table\n");
89 goto code_find;
90 }
91 }
92
Oder Chiou71c7a2d2014-05-20 15:01:54 +080093 k = 100000000 / freq_out - 2;
94 if (k > RL6231_PLL_K_MAX)
95 k = RL6231_PLL_K_MAX;
96 for (n_t = 0; n_t <= max_n; n_t++) {
97 in_t = freq_in / (k + 2);
98 pll_out = freq_out / (n_t + 2);
99 if (in_t < 0)
100 continue;
101 if (in_t == pll_out) {
102 bypass = true;
103 n = n_t;
104 goto code_find;
105 }
106 red = abs(in_t - pll_out);
107 if (red < red_t) {
108 bypass = true;
109 n = n_t;
110 m = m_t;
111 if (red == 0)
112 goto code_find;
113 red_t = red;
114 }
115 for (m_t = 0; m_t <= max_m; m_t++) {
116 out_t = in_t / (m_t + 2);
117 red = abs(out_t - pll_out);
118 if (red < red_t) {
119 bypass = false;
120 n = n_t;
121 m = m_t;
122 if (red == 0)
123 goto code_find;
124 red_t = red;
125 }
126 }
127 }
128 pr_debug("Only get approximation about PLL\n");
129
130code_find:
131
132 pll_code->m_bp = bypass;
133 pll_code->m_code = m;
134 pll_code->n_code = n;
135 pll_code->k_code = k;
136 return 0;
137}
138EXPORT_SYMBOL_GPL(rl6231_pll_calc);
139
Oder Chioud92950e2014-05-20 15:01:55 +0800140int rl6231_get_clk_info(int sclk, int rate)
141{
142 int i, pd[] = {1, 2, 3, 4, 6, 8, 12, 16};
143
144 if (sclk <= 0 || rate <= 0)
145 return -EINVAL;
146
147 rate = rate << 8;
148 for (i = 0; i < ARRAY_SIZE(pd); i++)
149 if (sclk == rate * pd[i])
150 return i;
151
152 return -EINVAL;
153}
154EXPORT_SYMBOL_GPL(rl6231_get_clk_info);
155
Oder Chiou49ef7922014-05-20 15:01:53 +0800156MODULE_DESCRIPTION("RL6231 class device shared support");
157MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
158MODULE_LICENSE("GPL v2");