diff options
-rw-r--r-- | Documentation/devicetree/bindings/sound/max98090.txt | 2 | ||||
-rw-r--r-- | sound/soc/codecs/max98090.c | 189 | ||||
-rw-r--r-- | sound/soc/codecs/max98090.h | 8 |
3 files changed, 163 insertions, 36 deletions
diff --git a/Documentation/devicetree/bindings/sound/max98090.txt b/Documentation/devicetree/bindings/sound/max98090.txt index c454e67f54bb..aa802a274520 100644 --- a/Documentation/devicetree/bindings/sound/max98090.txt +++ b/Documentation/devicetree/bindings/sound/max98090.txt | |||
@@ -16,6 +16,8 @@ Optional properties: | |||
16 | 16 | ||
17 | - clock-names: Should be "mclk" | 17 | - clock-names: Should be "mclk" |
18 | 18 | ||
19 | - maxim,dmic-freq: Frequency at which to clock DMIC | ||
20 | |||
19 | Pins on the device (for linking into audio routes): | 21 | Pins on the device (for linking into audio routes): |
20 | 22 | ||
21 | * MIC1 | 23 | * MIC1 |
diff --git a/sound/soc/codecs/max98090.c b/sound/soc/codecs/max98090.c index 1229554f1464..a65861cf0a44 100644 --- a/sound/soc/codecs/max98090.c +++ b/sound/soc/codecs/max98090.c | |||
@@ -1826,27 +1826,155 @@ static int max98090_set_bias_level(struct snd_soc_codec *codec, | |||
1826 | return 0; | 1826 | return 0; |
1827 | } | 1827 | } |
1828 | 1828 | ||
1829 | static const int comp_pclk_rates[] = { | 1829 | static const int dmic_divisors[] = { 2, 3, 4, 5, 6, 8 }; |
1830 | 11289600, 12288000, 12000000, 13000000, 19200000 | ||
1831 | }; | ||
1832 | |||
1833 | static const int dmic_micclk[] = { | ||
1834 | 2, 2, 2, 2, 4, 2 | ||
1835 | }; | ||
1836 | 1830 | ||
1837 | static const int comp_lrclk_rates[] = { | 1831 | static const int comp_lrclk_rates[] = { |
1838 | 8000, 16000, 32000, 44100, 48000, 96000 | 1832 | 8000, 16000, 32000, 44100, 48000, 96000 |
1839 | }; | 1833 | }; |
1840 | 1834 | ||
1841 | static const int dmic_comp[6][6] = { | 1835 | struct dmic_table { |
1842 | {7, 8, 3, 3, 3, 3}, | 1836 | int pclk; |
1843 | {7, 8, 3, 3, 3, 3}, | 1837 | struct { |
1844 | {7, 8, 3, 3, 3, 3}, | 1838 | int freq; |
1845 | {7, 8, 3, 1, 1, 1}, | 1839 | int comp[6]; /* One each for 8, 16, 32, 44.1, 48, and 96 kHz */ |
1846 | {7, 8, 3, 1, 2, 2}, | 1840 | } settings[6]; /* One for each dmic divisor. */ |
1847 | {7, 8, 3, 3, 3, 3} | ||
1848 | }; | 1841 | }; |
1849 | 1842 | ||
1843 | static const struct dmic_table dmic_table[] = { /* One for each pclk freq. */ | ||
1844 | { | ||
1845 | .pclk = 11289600, | ||
1846 | .settings = { | ||
1847 | { .freq = 2, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1848 | { .freq = 1, .comp = { 7, 8, 2, 2, 2, 2 } }, | ||
1849 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1850 | { .freq = 0, .comp = { 7, 8, 6, 6, 6, 6 } }, | ||
1851 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1852 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1853 | }, | ||
1854 | }, | ||
1855 | { | ||
1856 | .pclk = 12000000, | ||
1857 | .settings = { | ||
1858 | { .freq = 2, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1859 | { .freq = 1, .comp = { 7, 8, 2, 2, 2, 2 } }, | ||
1860 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1861 | { .freq = 0, .comp = { 7, 8, 5, 5, 6, 6 } }, | ||
1862 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1863 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1864 | } | ||
1865 | }, | ||
1866 | { | ||
1867 | .pclk = 12288000, | ||
1868 | .settings = { | ||
1869 | { .freq = 2, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1870 | { .freq = 1, .comp = { 7, 8, 2, 2, 2, 2 } }, | ||
1871 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1872 | { .freq = 0, .comp = { 7, 8, 6, 6, 6, 6 } }, | ||
1873 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1874 | { .freq = 0, .comp = { 7, 8, 3, 3, 3, 3 } }, | ||
1875 | } | ||
1876 | }, | ||
1877 | { | ||
1878 | .pclk = 13000000, | ||
1879 | .settings = { | ||
1880 | { .freq = 2, .comp = { 7, 8, 1, 1, 1, 1 } }, | ||
1881 | { .freq = 1, .comp = { 7, 8, 0, 0, 0, 0 } }, | ||
1882 | { .freq = 0, .comp = { 7, 8, 1, 1, 1, 1 } }, | ||
1883 | { .freq = 0, .comp = { 7, 8, 4, 4, 5, 5 } }, | ||
1884 | { .freq = 0, .comp = { 7, 8, 1, 1, 1, 1 } }, | ||
1885 | { .freq = 0, .comp = { 7, 8, 1, 1, 1, 1 } }, | ||
1886 | } | ||
1887 | }, | ||
1888 | { | ||
1889 | .pclk = 19200000, | ||
1890 | .settings = { | ||
1891 | { .freq = 2, .comp = { 0, 0, 0, 0, 0, 0 } }, | ||
1892 | { .freq = 1, .comp = { 7, 8, 1, 1, 1, 1 } }, | ||
1893 | { .freq = 0, .comp = { 7, 8, 5, 5, 6, 6 } }, | ||
1894 | { .freq = 0, .comp = { 7, 8, 2, 2, 3, 3 } }, | ||
1895 | { .freq = 0, .comp = { 7, 8, 1, 1, 2, 2 } }, | ||
1896 | { .freq = 0, .comp = { 7, 8, 5, 5, 6, 6 } }, | ||
1897 | } | ||
1898 | }, | ||
1899 | }; | ||
1900 | |||
1901 | static int max98090_find_divisor(int target_freq, int pclk) | ||
1902 | { | ||
1903 | int current_diff = INT_MAX; | ||
1904 | int test_diff = INT_MAX; | ||
1905 | int divisor_index = 0; | ||
1906 | int i; | ||
1907 | |||
1908 | for (i = 0; i < ARRAY_SIZE(dmic_divisors); i++) { | ||
1909 | test_diff = abs(target_freq - (pclk / dmic_divisors[i])); | ||
1910 | if (test_diff < current_diff) { | ||
1911 | current_diff = test_diff; | ||
1912 | divisor_index = i; | ||
1913 | } | ||
1914 | } | ||
1915 | |||
1916 | return divisor_index; | ||
1917 | } | ||
1918 | |||
1919 | static int max98090_find_closest_pclk(int pclk) | ||
1920 | { | ||
1921 | int m1; | ||
1922 | int m2; | ||
1923 | int i; | ||
1924 | |||
1925 | for (i = 0; i < ARRAY_SIZE(dmic_table); i++) { | ||
1926 | if (pclk == dmic_table[i].pclk) | ||
1927 | return i; | ||
1928 | if (pclk < dmic_table[i].pclk) { | ||
1929 | if (i == 0) | ||
1930 | return i; | ||
1931 | m1 = pclk - dmic_table[i-1].pclk; | ||
1932 | m2 = dmic_table[i].pclk - pclk; | ||
1933 | if (m1 < m2) | ||
1934 | return i - 1; | ||
1935 | else | ||
1936 | return i; | ||
1937 | } | ||
1938 | } | ||
1939 | |||
1940 | return -EINVAL; | ||
1941 | } | ||
1942 | |||
1943 | static int max98090_configure_dmic(struct max98090_priv *max98090, | ||
1944 | int target_dmic_clk, int pclk, int fs) | ||
1945 | { | ||
1946 | int micclk_index; | ||
1947 | int pclk_index; | ||
1948 | int dmic_freq; | ||
1949 | int dmic_comp; | ||
1950 | int i; | ||
1951 | |||
1952 | pclk_index = max98090_find_closest_pclk(pclk); | ||
1953 | if (pclk_index < 0) | ||
1954 | return pclk_index; | ||
1955 | |||
1956 | micclk_index = max98090_find_divisor(target_dmic_clk, pclk); | ||
1957 | |||
1958 | for (i = 0; i < ARRAY_SIZE(comp_lrclk_rates) - 1; i++) { | ||
1959 | if (fs <= (comp_lrclk_rates[i] + comp_lrclk_rates[i+1]) / 2) | ||
1960 | break; | ||
1961 | } | ||
1962 | |||
1963 | dmic_freq = dmic_table[pclk_index].settings[micclk_index].freq; | ||
1964 | dmic_comp = dmic_table[pclk_index].settings[micclk_index].comp[i]; | ||
1965 | |||
1966 | regmap_update_bits(max98090->regmap, M98090_REG_DIGITAL_MIC_ENABLE, | ||
1967 | M98090_MICCLK_MASK, | ||
1968 | micclk_index << M98090_MICCLK_SHIFT); | ||
1969 | |||
1970 | regmap_update_bits(max98090->regmap, M98090_REG_DIGITAL_MIC_CONFIG, | ||
1971 | M98090_DMIC_COMP_MASK | M98090_DMIC_FREQ_MASK, | ||
1972 | dmic_comp << M98090_DMIC_COMP_SHIFT | | ||
1973 | dmic_freq << M98090_DMIC_FREQ_SHIFT); | ||
1974 | |||
1975 | return 0; | ||
1976 | } | ||
1977 | |||
1850 | static int max98090_dai_hw_params(struct snd_pcm_substream *substream, | 1978 | static int max98090_dai_hw_params(struct snd_pcm_substream *substream, |
1851 | struct snd_pcm_hw_params *params, | 1979 | struct snd_pcm_hw_params *params, |
1852 | struct snd_soc_dai *dai) | 1980 | struct snd_soc_dai *dai) |
@@ -1854,7 +1982,6 @@ static int max98090_dai_hw_params(struct snd_pcm_substream *substream, | |||
1854 | struct snd_soc_codec *codec = dai->codec; | 1982 | struct snd_soc_codec *codec = dai->codec; |
1855 | struct max98090_priv *max98090 = snd_soc_codec_get_drvdata(codec); | 1983 | struct max98090_priv *max98090 = snd_soc_codec_get_drvdata(codec); |
1856 | struct max98090_cdata *cdata; | 1984 | struct max98090_cdata *cdata; |
1857 | int i, j; | ||
1858 | 1985 | ||
1859 | cdata = &max98090->dai[0]; | 1986 | cdata = &max98090->dai[0]; |
1860 | max98090->bclk = snd_soc_params_to_bclk(params); | 1987 | max98090->bclk = snd_soc_params_to_bclk(params); |
@@ -1893,27 +2020,8 @@ static int max98090_dai_hw_params(struct snd_pcm_substream *substream, | |||
1893 | snd_soc_update_bits(codec, M98090_REG_FILTER_CONFIG, | 2020 | snd_soc_update_bits(codec, M98090_REG_FILTER_CONFIG, |
1894 | M98090_DHF_MASK, M98090_DHF_MASK); | 2021 | M98090_DHF_MASK, M98090_DHF_MASK); |
1895 | 2022 | ||
1896 | /* Check for supported PCLK to LRCLK ratios */ | 2023 | max98090_configure_dmic(max98090, max98090->dmic_freq, max98090->pclk, |
1897 | for (j = 0; j < ARRAY_SIZE(comp_pclk_rates); j++) { | 2024 | max98090->lrclk); |
1898 | if (comp_pclk_rates[j] == max98090->sysclk) { | ||
1899 | break; | ||
1900 | } | ||
1901 | } | ||
1902 | |||
1903 | for (i = 0; i < ARRAY_SIZE(comp_lrclk_rates) - 1; i++) { | ||
1904 | if (max98090->lrclk <= (comp_lrclk_rates[i] + | ||
1905 | comp_lrclk_rates[i + 1]) / 2) { | ||
1906 | break; | ||
1907 | } | ||
1908 | } | ||
1909 | |||
1910 | snd_soc_update_bits(codec, M98090_REG_DIGITAL_MIC_ENABLE, | ||
1911 | M98090_MICCLK_MASK, | ||
1912 | dmic_micclk[j] << M98090_MICCLK_SHIFT); | ||
1913 | |||
1914 | snd_soc_update_bits(codec, M98090_REG_DIGITAL_MIC_CONFIG, | ||
1915 | M98090_DMIC_COMP_MASK, | ||
1916 | dmic_comp[j][i] << M98090_DMIC_COMP_SHIFT); | ||
1917 | 2025 | ||
1918 | return 0; | 2026 | return 0; |
1919 | } | 2027 | } |
@@ -1944,12 +2052,15 @@ static int max98090_dai_set_sysclk(struct snd_soc_dai *dai, | |||
1944 | if ((freq >= 10000000) && (freq <= 20000000)) { | 2052 | if ((freq >= 10000000) && (freq <= 20000000)) { |
1945 | snd_soc_write(codec, M98090_REG_SYSTEM_CLOCK, | 2053 | snd_soc_write(codec, M98090_REG_SYSTEM_CLOCK, |
1946 | M98090_PSCLK_DIV1); | 2054 | M98090_PSCLK_DIV1); |
2055 | max98090->pclk = freq; | ||
1947 | } else if ((freq > 20000000) && (freq <= 40000000)) { | 2056 | } else if ((freq > 20000000) && (freq <= 40000000)) { |
1948 | snd_soc_write(codec, M98090_REG_SYSTEM_CLOCK, | 2057 | snd_soc_write(codec, M98090_REG_SYSTEM_CLOCK, |
1949 | M98090_PSCLK_DIV2); | 2058 | M98090_PSCLK_DIV2); |
2059 | max98090->pclk = freq >> 1; | ||
1950 | } else if ((freq > 40000000) && (freq <= 60000000)) { | 2060 | } else if ((freq > 40000000) && (freq <= 60000000)) { |
1951 | snd_soc_write(codec, M98090_REG_SYSTEM_CLOCK, | 2061 | snd_soc_write(codec, M98090_REG_SYSTEM_CLOCK, |
1952 | M98090_PSCLK_DIV4); | 2062 | M98090_PSCLK_DIV4); |
2063 | max98090->pclk = freq >> 2; | ||
1953 | } else { | 2064 | } else { |
1954 | dev_err(codec->dev, "Invalid master clock frequency\n"); | 2065 | dev_err(codec->dev, "Invalid master clock frequency\n"); |
1955 | return -EINVAL; | 2066 | return -EINVAL; |
@@ -2324,6 +2435,7 @@ static int max98090_probe(struct snd_soc_codec *codec) | |||
2324 | /* Initialize private data */ | 2435 | /* Initialize private data */ |
2325 | 2436 | ||
2326 | max98090->sysclk = (unsigned)-1; | 2437 | max98090->sysclk = (unsigned)-1; |
2438 | max98090->pclk = (unsigned)-1; | ||
2327 | max98090->master = false; | 2439 | max98090->master = false; |
2328 | 2440 | ||
2329 | cdata = &max98090->dai[0]; | 2441 | cdata = &max98090->dai[0]; |
@@ -2463,6 +2575,11 @@ static int max98090_i2c_probe(struct i2c_client *i2c, | |||
2463 | i2c_set_clientdata(i2c, max98090); | 2575 | i2c_set_clientdata(i2c, max98090); |
2464 | max98090->pdata = i2c->dev.platform_data; | 2576 | max98090->pdata = i2c->dev.platform_data; |
2465 | 2577 | ||
2578 | ret = of_property_read_u32(i2c->dev.of_node, "maxim,dmic-freq", | ||
2579 | &max98090->dmic_freq); | ||
2580 | if (ret < 0) | ||
2581 | max98090->dmic_freq = MAX98090_DEFAULT_DMIC_FREQ; | ||
2582 | |||
2466 | max98090->regmap = devm_regmap_init_i2c(i2c, &max98090_regmap); | 2583 | max98090->regmap = devm_regmap_init_i2c(i2c, &max98090_regmap); |
2467 | if (IS_ERR(max98090->regmap)) { | 2584 | if (IS_ERR(max98090->regmap)) { |
2468 | ret = PTR_ERR(max98090->regmap); | 2585 | ret = PTR_ERR(max98090->regmap); |
diff --git a/sound/soc/codecs/max98090.h b/sound/soc/codecs/max98090.h index a5f6bada06da..21ff743f5af2 100644 --- a/sound/soc/codecs/max98090.h +++ b/sound/soc/codecs/max98090.h | |||
@@ -12,6 +12,12 @@ | |||
12 | #define _MAX98090_H | 12 | #define _MAX98090_H |
13 | 13 | ||
14 | /* | 14 | /* |
15 | * The default operating frequency for a DMIC attached to the codec. | ||
16 | * This can be overridden by a device tree property. | ||
17 | */ | ||
18 | #define MAX98090_DEFAULT_DMIC_FREQ 2500000 | ||
19 | |||
20 | /* | ||
15 | * MAX98090 Register Definitions | 21 | * MAX98090 Register Definitions |
16 | */ | 22 | */ |
17 | 23 | ||
@@ -1518,8 +1524,10 @@ struct max98090_priv { | |||
1518 | struct max98090_pdata *pdata; | 1524 | struct max98090_pdata *pdata; |
1519 | struct clk *mclk; | 1525 | struct clk *mclk; |
1520 | unsigned int sysclk; | 1526 | unsigned int sysclk; |
1527 | unsigned int pclk; | ||
1521 | unsigned int bclk; | 1528 | unsigned int bclk; |
1522 | unsigned int lrclk; | 1529 | unsigned int lrclk; |
1530 | u32 dmic_freq; | ||
1523 | struct max98090_cdata dai[1]; | 1531 | struct max98090_cdata dai[1]; |
1524 | int jack_state; | 1532 | int jack_state; |
1525 | struct delayed_work jack_work; | 1533 | struct delayed_work jack_work; |