aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/rt5645.c
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2014-06-03 05:51:14 -0400
committerTakashi Iwai <tiwai@suse.de>2014-06-03 05:51:14 -0400
commit8743dcd6639c28204ac03fb3e9db7932e3d85418 (patch)
tree53b80846cf6731ddb196d522ed2bbf0f76da8344 /sound/soc/codecs/rt5645.c
parentefd4b76ef789541e7046e873b3546209352cdb59 (diff)
parente1d4d3c854f25cff6c6c139588570e124d5e8fa4 (diff)
Merge tag 'asoc-v3.16-2' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound into for-next
ASoC: Final updates for v3.16 A few more updates from the last week of development, nothing too exciting. Highlights include: - GPIO descriptor support for jacks - More updates and fixes to the Freescale SSI, Intel and rsnd drivers. - New drivers for Analog Devices ADAU1361, ADAU1381, ADAU1761 and ADAU1781, and Realtek RT5677.
Diffstat (limited to 'sound/soc/codecs/rt5645.c')
-rw-r--r--sound/soc/codecs/rt5645.c111
1 files changed, 7 insertions, 104 deletions
diff --git a/sound/soc/codecs/rt5645.c b/sound/soc/codecs/rt5645.c
index ab97d722e15d..02147be2b302 100644
--- a/sound/soc/codecs/rt5645.c
+++ b/sound/soc/codecs/rt5645.c
@@ -26,6 +26,7 @@
26#include <sound/initval.h> 26#include <sound/initval.h>
27#include <sound/tlv.h> 27#include <sound/tlv.h>
28 28
29#include "rl6231.h"
29#include "rt5645.h" 30#include "rt5645.h"
30 31
31#define RT5645_DEVICE_ID 0x6308 32#define RT5645_DEVICE_ID 0x6308
@@ -519,30 +520,15 @@ static const struct snd_kcontrol_new rt5645_snd_controls[] = {
519 * @kcontrol: The kcontrol of this widget. 520 * @kcontrol: The kcontrol of this widget.
520 * @event: Event id. 521 * @event: Event id.
521 * 522 *
522 * Choose dmic clock between 1MHz and 3MHz.
523 * It is better for clock to approximate 3MHz.
524 */ 523 */
525static int set_dmic_clk(struct snd_soc_dapm_widget *w, 524static int set_dmic_clk(struct snd_soc_dapm_widget *w,
526 struct snd_kcontrol *kcontrol, int event) 525 struct snd_kcontrol *kcontrol, int event)
527{ 526{
528 struct snd_soc_codec *codec = w->codec; 527 struct snd_soc_codec *codec = w->codec;
529 struct rt5645_priv *rt5645 = snd_soc_codec_get_drvdata(codec); 528 struct rt5645_priv *rt5645 = snd_soc_codec_get_drvdata(codec);
530 int div[] = {2, 3, 4, 6, 8, 12}; 529 int idx = -EINVAL;
531 int idx = -EINVAL, i; 530
532 int rate, red, bound, temp; 531 idx = rl6231_calc_dmic_clk(rt5645->sysclk);
533
534 rate = rt5645->sysclk;
535 red = 3000000 * 12;
536 for (i = 0; i < ARRAY_SIZE(div); i++) {
537 bound = div[i] * 3000000;
538 if (rate > bound)
539 continue;
540 temp = bound - rate;
541 if (temp < red) {
542 red = temp;
543 idx = i;
544 }
545 }
546 532
547 if (idx < 0) 533 if (idx < 0)
548 dev_err(codec->dev, "Failed to set DMIC clock\n"); 534 dev_err(codec->dev, "Failed to set DMIC clock\n");
@@ -1800,21 +1786,6 @@ static const struct snd_soc_dapm_route rt5645_dapm_routes[] = {
1800 { "SPOR", NULL, "SPK amp" }, 1786 { "SPOR", NULL, "SPK amp" },
1801}; 1787};
1802 1788
1803static int get_clk_info(int sclk, int rate)
1804{
1805 int i, pd[] = {1, 2, 3, 4, 6, 8, 12, 16};
1806
1807 if (sclk <= 0 || rate <= 0)
1808 return -EINVAL;
1809
1810 rate = rate << 8;
1811 for (i = 0; i < ARRAY_SIZE(pd); i++)
1812 if (sclk == rate * pd[i])
1813 return i;
1814
1815 return -EINVAL;
1816}
1817
1818static int rt5645_hw_params(struct snd_pcm_substream *substream, 1789static int rt5645_hw_params(struct snd_pcm_substream *substream,
1819 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 1790 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1820{ 1791{
@@ -1824,7 +1795,7 @@ static int rt5645_hw_params(struct snd_pcm_substream *substream,
1824 int pre_div, bclk_ms, frame_size; 1795 int pre_div, bclk_ms, frame_size;
1825 1796
1826 rt5645->lrck[dai->id] = params_rate(params); 1797 rt5645->lrck[dai->id] = params_rate(params);
1827 pre_div = get_clk_info(rt5645->sysclk, rt5645->lrck[dai->id]); 1798 pre_div = rl6231_get_clk_info(rt5645->sysclk, rt5645->lrck[dai->id]);
1828 if (pre_div < 0) { 1799 if (pre_div < 0) {
1829 dev_err(codec->dev, "Unsupported clock setting\n"); 1800 dev_err(codec->dev, "Unsupported clock setting\n");
1830 return -EINVAL; 1801 return -EINVAL;
@@ -1978,80 +1949,12 @@ static int rt5645_set_dai_sysclk(struct snd_soc_dai *dai,
1978 return 0; 1949 return 0;
1979} 1950}
1980 1951
1981/**
1982 * rt5645_pll_calc - Calcualte PLL M/N/K code.
1983 * @freq_in: external clock provided to codec.
1984 * @freq_out: target clock which codec works on.
1985 * @pll_code: Pointer to structure with M, N, K and bypass flag.
1986 *
1987 * Calcualte M/N/K code to configure PLL for codec. And K is assigned to 2
1988 * which make calculation more efficiently.
1989 *
1990 * Returns 0 for success or negative error code.
1991 */
1992static int rt5645_pll_calc(const unsigned int freq_in,
1993 const unsigned int freq_out, struct rt5645_pll_code *pll_code)
1994{
1995 int max_n = RT5645_PLL_N_MAX, max_m = RT5645_PLL_M_MAX;
1996 int k, n = 0, m = 0, red, n_t, m_t, pll_out, in_t, out_t;
1997 int red_t = abs(freq_out - freq_in);
1998 bool bypass = false;
1999
2000 if (RT5645_PLL_INP_MAX < freq_in || RT5645_PLL_INP_MIN > freq_in)
2001 return -EINVAL;
2002
2003 k = 100000000 / freq_out - 2;
2004 if (k > RT5645_PLL_K_MAX)
2005 k = RT5645_PLL_K_MAX;
2006 for (n_t = 0; n_t <= max_n; n_t++) {
2007 in_t = freq_in / (k + 2);
2008 pll_out = freq_out / (n_t + 2);
2009 if (in_t < 0)
2010 continue;
2011 if (in_t == pll_out) {
2012 bypass = true;
2013 n = n_t;
2014 goto code_find;
2015 }
2016 red = abs(in_t - pll_out);
2017 if (red < red_t) {
2018 bypass = true;
2019 n = n_t;
2020 m = m_t;
2021 if (red == 0)
2022 goto code_find;
2023 red_t = red;
2024 }
2025 for (m_t = 0; m_t <= max_m; m_t++) {
2026 out_t = in_t / (m_t + 2);
2027 red = abs(out_t - pll_out);
2028 if (red < red_t) {
2029 bypass = false;
2030 n = n_t;
2031 m = m_t;
2032 if (red == 0)
2033 goto code_find;
2034 red_t = red;
2035 }
2036 }
2037 }
2038 pr_debug("Only get approximation about PLL\n");
2039
2040code_find:
2041
2042 pll_code->m_bp = bypass;
2043 pll_code->m_code = m;
2044 pll_code->n_code = n;
2045 pll_code->k_code = k;
2046 return 0;
2047}
2048
2049static int rt5645_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source, 1952static int rt5645_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2050 unsigned int freq_in, unsigned int freq_out) 1953 unsigned int freq_in, unsigned int freq_out)
2051{ 1954{
2052 struct snd_soc_codec *codec = dai->codec; 1955 struct snd_soc_codec *codec = dai->codec;
2053 struct rt5645_priv *rt5645 = snd_soc_codec_get_drvdata(codec); 1956 struct rt5645_priv *rt5645 = snd_soc_codec_get_drvdata(codec);
2054 struct rt5645_pll_code pll_code; 1957 struct rl6231_pll_code pll_code;
2055 int ret; 1958 int ret;
2056 1959
2057 if (source == rt5645->pll_src && freq_in == rt5645->pll_in && 1960 if (source == rt5645->pll_src && freq_in == rt5645->pll_in &&
@@ -2094,7 +1997,7 @@ static int rt5645_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2094 return -EINVAL; 1997 return -EINVAL;
2095 } 1998 }
2096 1999
2097 ret = rt5645_pll_calc(freq_in, freq_out, &pll_code); 2000 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
2098 if (ret < 0) { 2001 if (ret < 0) {
2099 dev_err(codec->dev, "Unsupport input clock %d\n", freq_in); 2002 dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
2100 return ret; 2003 return ret;