rl6231.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  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. #include <linux/module.h>
  13. #include <linux/regmap.h>
  14. #include "rl6231.h"
  15. /**
  16. * rl6231_get_pre_div - Return the value of pre divider.
  17. *
  18. * @map: map for setting.
  19. * @reg: register.
  20. * @sft: shift.
  21. *
  22. * Return the value of pre divider from given register value.
  23. * Return negative error code for unexpected register value.
  24. */
  25. int rl6231_get_pre_div(struct regmap *map, unsigned int reg, int sft)
  26. {
  27. int pd, val;
  28. regmap_read(map, reg, &val);
  29. val = (val >> sft) & 0x7;
  30. switch (val) {
  31. case 0:
  32. case 1:
  33. case 2:
  34. case 3:
  35. pd = val + 1;
  36. break;
  37. case 4:
  38. pd = 6;
  39. break;
  40. case 5:
  41. pd = 8;
  42. break;
  43. case 6:
  44. pd = 12;
  45. break;
  46. case 7:
  47. pd = 16;
  48. break;
  49. default:
  50. pd = -EINVAL;
  51. break;
  52. }
  53. return pd;
  54. }
  55. EXPORT_SYMBOL_GPL(rl6231_get_pre_div);
  56. /**
  57. * rl6231_calc_dmic_clk - Calculate the frequency divider parameter of dmic.
  58. *
  59. * @rate: base clock rate.
  60. *
  61. * Choose divider parameter that gives the highest possible DMIC frequency in
  62. * 1MHz - 3MHz range.
  63. */
  64. int rl6231_calc_dmic_clk(int rate)
  65. {
  66. int div[] = {2, 3, 4, 6, 8, 12};
  67. int i;
  68. if (rate < 1000000 * div[0]) {
  69. pr_warn("Base clock rate %d is too low\n", rate);
  70. return -EINVAL;
  71. }
  72. for (i = 0; i < ARRAY_SIZE(div); i++) {
  73. if ((div[i] % 3) == 0)
  74. continue;
  75. /* find divider that gives DMIC frequency below 3.072MHz */
  76. if (3072000 * div[i] >= rate)
  77. return i;
  78. }
  79. pr_warn("Base clock rate %d is too high\n", rate);
  80. return -EINVAL;
  81. }
  82. EXPORT_SYMBOL_GPL(rl6231_calc_dmic_clk);
  83. struct pll_calc_map {
  84. unsigned int pll_in;
  85. unsigned int pll_out;
  86. int k;
  87. int n;
  88. int m;
  89. bool m_bp;
  90. };
  91. static const struct pll_calc_map pll_preset_table[] = {
  92. {19200000, 24576000, 3, 30, 3, false},
  93. };
  94. /**
  95. * rl6231_pll_calc - Calcualte PLL M/N/K code.
  96. * @freq_in: external clock provided to codec.
  97. * @freq_out: target clock which codec works on.
  98. * @pll_code: Pointer to structure with M, N, K and bypass flag.
  99. *
  100. * Calcualte M/N/K code to configure PLL for codec.
  101. *
  102. * Returns 0 for success or negative error code.
  103. */
  104. int rl6231_pll_calc(const unsigned int freq_in,
  105. const unsigned int freq_out, struct rl6231_pll_code *pll_code)
  106. {
  107. int max_n = RL6231_PLL_N_MAX, max_m = RL6231_PLL_M_MAX;
  108. int i, k, red, n_t, pll_out, in_t, out_t;
  109. int n = 0, m = 0, m_t = 0;
  110. int red_t = abs(freq_out - freq_in);
  111. bool bypass = false;
  112. if (RL6231_PLL_INP_MAX < freq_in || RL6231_PLL_INP_MIN > freq_in)
  113. return -EINVAL;
  114. for (i = 0; i < ARRAY_SIZE(pll_preset_table); i++) {
  115. if (freq_in == pll_preset_table[i].pll_in &&
  116. freq_out == pll_preset_table[i].pll_out) {
  117. k = pll_preset_table[i].k;
  118. m = pll_preset_table[i].m;
  119. n = pll_preset_table[i].n;
  120. bypass = pll_preset_table[i].m_bp;
  121. pr_debug("Use preset PLL parameter table\n");
  122. goto code_find;
  123. }
  124. }
  125. k = 100000000 / freq_out - 2;
  126. if (k > RL6231_PLL_K_MAX)
  127. k = RL6231_PLL_K_MAX;
  128. for (n_t = 0; n_t <= max_n; n_t++) {
  129. in_t = freq_in / (k + 2);
  130. pll_out = freq_out / (n_t + 2);
  131. if (in_t < 0)
  132. continue;
  133. if (in_t == pll_out) {
  134. bypass = true;
  135. n = n_t;
  136. goto code_find;
  137. }
  138. red = abs(in_t - pll_out);
  139. if (red < red_t) {
  140. bypass = true;
  141. n = n_t;
  142. m = m_t;
  143. if (red == 0)
  144. goto code_find;
  145. red_t = red;
  146. }
  147. for (m_t = 0; m_t <= max_m; m_t++) {
  148. out_t = in_t / (m_t + 2);
  149. red = abs(out_t - pll_out);
  150. if (red < red_t) {
  151. bypass = false;
  152. n = n_t;
  153. m = m_t;
  154. if (red == 0)
  155. goto code_find;
  156. red_t = red;
  157. }
  158. }
  159. }
  160. pr_debug("Only get approximation about PLL\n");
  161. code_find:
  162. pll_code->m_bp = bypass;
  163. pll_code->m_code = m;
  164. pll_code->n_code = n;
  165. pll_code->k_code = k;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(rl6231_pll_calc);
  169. int rl6231_get_clk_info(int sclk, int rate)
  170. {
  171. int i, pd[] = {1, 2, 3, 4, 6, 8, 12, 16};
  172. if (sclk <= 0 || rate <= 0)
  173. return -EINVAL;
  174. rate = rate << 8;
  175. for (i = 0; i < ARRAY_SIZE(pd); i++)
  176. if (sclk == rate * pd[i])
  177. return i;
  178. return -EINVAL;
  179. }
  180. EXPORT_SYMBOL_GPL(rl6231_get_clk_info);
  181. MODULE_DESCRIPTION("RL6231 class device shared support");
  182. MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
  183. MODULE_LICENSE("GPL v2");