| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * rl6231.c - RL6231 class device shared support |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright 2014 Realtek Semiconductor Corp. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * 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 | 8 | */ |
|---|
| 12 | 9 | |
|---|
| 13 | 10 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 83 | 80 | for (i = 0; i < ARRAY_SIZE(div); i++) { |
|---|
| 84 | 81 | if ((div[i] % 3) == 0) |
|---|
| 85 | 82 | continue; |
|---|
| 86 | | - /* find divider that gives DMIC frequency below 3.072MHz */ |
|---|
| 87 | | - if (3072000 * div[i] >= rate) |
|---|
| 83 | + /* find divider that gives DMIC frequency below 1.536MHz */ |
|---|
| 84 | + if (1536000 * div[i] >= rate) |
|---|
| 88 | 85 | return i; |
|---|
| 89 | 86 | } |
|---|
| 90 | 87 | |
|---|
| .. | .. |
|---|
| 100 | 97 | int n; |
|---|
| 101 | 98 | int m; |
|---|
| 102 | 99 | bool m_bp; |
|---|
| 100 | + bool k_bp; |
|---|
| 103 | 101 | }; |
|---|
| 104 | 102 | |
|---|
| 105 | 103 | static const struct pll_calc_map pll_preset_table[] = { |
|---|
| 106 | | - {19200000, 4096000, 23, 14, 1, false}, |
|---|
| 107 | | - {19200000, 24576000, 3, 30, 3, false}, |
|---|
| 104 | + {19200000, 4096000, 23, 14, 1, false, false}, |
|---|
| 105 | + {19200000, 24576000, 3, 30, 3, false, false}, |
|---|
| 106 | + {48000000, 3840000, 23, 2, 0, false, false}, |
|---|
| 107 | + {3840000, 24576000, 3, 30, 0, true, false}, |
|---|
| 108 | + {3840000, 22579200, 3, 5, 0, true, false}, |
|---|
| 108 | 109 | }; |
|---|
| 109 | 110 | |
|---|
| 110 | 111 | static unsigned int find_best_div(unsigned int in, |
|---|
| .. | .. |
|---|
| 130 | 131 | * rl6231_pll_calc - Calcualte PLL M/N/K code. |
|---|
| 131 | 132 | * @freq_in: external clock provided to codec. |
|---|
| 132 | 133 | * @freq_out: target clock which codec works on. |
|---|
| 133 | | - * @pll_code: Pointer to structure with M, N, K and bypass flag. |
|---|
| 134 | + * @pll_code: Pointer to structure with M, N, K, m_bypass and k_bypass flag. |
|---|
| 134 | 135 | * |
|---|
| 135 | 136 | * Calcualte M/N/K code to configure PLL for codec. |
|---|
| 136 | 137 | * |
|---|
| .. | .. |
|---|
| 145 | 146 | unsigned int red, pll_out, in_t, out_t, div, div_t; |
|---|
| 146 | 147 | unsigned int red_t = abs(freq_out - freq_in); |
|---|
| 147 | 148 | unsigned int f_in, f_out, f_max; |
|---|
| 148 | | - bool bypass = false; |
|---|
| 149 | + bool m_bypass = false, k_bypass = false; |
|---|
| 149 | 150 | |
|---|
| 150 | 151 | if (RL6231_PLL_INP_MAX < freq_in || RL6231_PLL_INP_MIN > freq_in) |
|---|
| 151 | 152 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 156 | 157 | k = pll_preset_table[i].k; |
|---|
| 157 | 158 | m = pll_preset_table[i].m; |
|---|
| 158 | 159 | n = pll_preset_table[i].n; |
|---|
| 159 | | - bypass = pll_preset_table[i].m_bp; |
|---|
| 160 | + m_bypass = pll_preset_table[i].m_bp; |
|---|
| 161 | + k_bypass = pll_preset_table[i].k_bp; |
|---|
| 160 | 162 | pr_debug("Use preset PLL parameter table\n"); |
|---|
| 161 | 163 | goto code_find; |
|---|
| 162 | 164 | } |
|---|
| .. | .. |
|---|
| 174 | 176 | f_in = freq_in / div; |
|---|
| 175 | 177 | f_out = freq_out / div; |
|---|
| 176 | 178 | k = min_k; |
|---|
| 179 | + if (min_k < -1) |
|---|
| 180 | + min_k = -1; |
|---|
| 177 | 181 | for (k_t = min_k; k_t <= max_k; k_t++) { |
|---|
| 178 | 182 | for (n_t = 0; n_t <= max_n; n_t++) { |
|---|
| 179 | 183 | in_t = f_in * (n_t + 2); |
|---|
| 180 | 184 | pll_out = f_out * (k_t + 2); |
|---|
| 181 | 185 | if (in_t == pll_out) { |
|---|
| 182 | | - bypass = true; |
|---|
| 186 | + m_bypass = true; |
|---|
| 183 | 187 | n = n_t; |
|---|
| 184 | 188 | k = k_t; |
|---|
| 185 | 189 | goto code_find; |
|---|
| .. | .. |
|---|
| 187 | 191 | out_t = in_t / (k_t + 2); |
|---|
| 188 | 192 | red = abs(f_out - out_t); |
|---|
| 189 | 193 | if (red < red_t) { |
|---|
| 190 | | - bypass = true; |
|---|
| 194 | + m_bypass = true; |
|---|
| 191 | 195 | n = n_t; |
|---|
| 192 | 196 | m = 0; |
|---|
| 193 | 197 | k = k_t; |
|---|
| .. | .. |
|---|
| 199 | 203 | out_t = in_t / ((m_t + 2) * (k_t + 2)); |
|---|
| 200 | 204 | red = abs(f_out - out_t); |
|---|
| 201 | 205 | if (red < red_t) { |
|---|
| 202 | | - bypass = false; |
|---|
| 206 | + m_bypass = false; |
|---|
| 203 | 207 | n = n_t; |
|---|
| 204 | 208 | m = m_t; |
|---|
| 205 | 209 | k = k_t; |
|---|
| .. | .. |
|---|
| 213 | 217 | pr_debug("Only get approximation about PLL\n"); |
|---|
| 214 | 218 | |
|---|
| 215 | 219 | code_find: |
|---|
| 220 | + if (k == -1) { |
|---|
| 221 | + k_bypass = true; |
|---|
| 222 | + k = 0; |
|---|
| 223 | + } |
|---|
| 216 | 224 | |
|---|
| 217 | | - pll_code->m_bp = bypass; |
|---|
| 225 | + pll_code->m_bp = m_bypass; |
|---|
| 226 | + pll_code->k_bp = k_bypass; |
|---|
| 218 | 227 | pll_code->m_code = m; |
|---|
| 219 | 228 | pll_code->n_code = n; |
|---|
| 220 | 229 | pll_code->k_code = k; |
|---|