.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * rt5651.c -- RT5651 ALSA SoC audio codec driver |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright 2014 Realtek Semiconductor Corp. |
---|
5 | 6 | * Author: Bard Liao <bardliao@realtek.com> |
---|
6 | | - * |
---|
7 | | - * This program is free software; you can redistribute it and/or modify |
---|
8 | | - * it under the terms of the GNU General Public License version 2 as |
---|
9 | | - * published by the Free Software Foundation. |
---|
10 | 7 | */ |
---|
11 | 8 | |
---|
12 | 9 | #include <linux/module.h> |
---|
13 | | -#include <linux/moduleparam.h> |
---|
14 | 10 | #include <linux/init.h> |
---|
15 | 11 | #include <linux/delay.h> |
---|
16 | 12 | #include <linux/pm.h> |
---|
| 13 | +#include <linux/gpio/consumer.h> |
---|
17 | 14 | #include <linux/i2c.h> |
---|
18 | 15 | #include <linux/regmap.h> |
---|
19 | 16 | #include <linux/platform_device.h> |
---|
.. | .. |
---|
316 | 313 | regmap_write(rt5651->regmap, 0x83, 0x1000); |
---|
317 | 314 | regmap_write(rt5651->regmap, 0x84, 0x7000); |
---|
318 | 315 | snd_soc_component_update_bits(component, 0x64, 0x0200, 0x0200); |
---|
319 | | - snd_soc_component_update_bits(component, RT5651_D_MISC, 0xc00, |
---|
320 | | - 0xc00); |
---|
| 316 | + snd_soc_component_update_bits(component, RT5651_D_MISC, 0xc00, 0xc00); |
---|
321 | 317 | } else { |
---|
322 | 318 | regmap_write(rt5651->regmap, 0x83, 0x0); |
---|
323 | 319 | regmap_write(rt5651->regmap, 0x84, 0x0); |
---|
.. | .. |
---|
793 | 789 | RT5651_HP_CP_PD | RT5651_HP_SG_EN); |
---|
794 | 790 | regmap_update_bits(rt5651->regmap, RT5651_PR_BASE + |
---|
795 | 791 | RT5651_CHPUMP_INT_REG1, 0x0700, 0x0400); |
---|
796 | | - rt5651->hp_mute = 0; |
---|
| 792 | + rt5651->hp_mute = false; |
---|
797 | 793 | break; |
---|
798 | 794 | |
---|
799 | 795 | case SND_SOC_DAPM_PRE_PMD: |
---|
800 | | - rt5651->hp_mute = 1; |
---|
| 796 | + rt5651->hp_mute = true; |
---|
801 | 797 | usleep_range(70000, 75000); |
---|
802 | 798 | break; |
---|
803 | 799 | |
---|
.. | .. |
---|
1566 | 1562 | if (SND_SOC_BIAS_STANDBY == snd_soc_component_get_bias_level(component)) { |
---|
1567 | 1563 | if (!IS_ERR(rt5651->mclk)) |
---|
1568 | 1564 | clk_prepare_enable(rt5651->mclk); |
---|
1569 | | - if (snd_soc_component_read32(component, RT5651_PLL_MODE_1) & 0x9200) |
---|
| 1565 | + if (snd_soc_component_read(component, RT5651_PLL_MODE_1) & 0x9200) |
---|
1570 | 1566 | snd_soc_component_update_bits(component, RT5651_D_MISC, |
---|
1571 | 1567 | 0xc00, 0xc00); |
---|
1572 | 1568 | } |
---|
.. | .. |
---|
1663 | 1659 | { |
---|
1664 | 1660 | int val; |
---|
1665 | 1661 | |
---|
1666 | | - val = snd_soc_component_read32(component, RT5651_IRQ_CTRL2); |
---|
| 1662 | + val = snd_soc_component_read(component, RT5651_IRQ_CTRL2); |
---|
1667 | 1663 | dev_dbg(component->dev, "irq ctrl2 %#04x\n", val); |
---|
1668 | 1664 | |
---|
1669 | 1665 | return (val & RT5651_MB1_OC_CLR); |
---|
.. | .. |
---|
1674 | 1670 | struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component); |
---|
1675 | 1671 | int val; |
---|
1676 | 1672 | |
---|
1677 | | - val = snd_soc_component_read32(component, RT5651_INT_IRQ_ST); |
---|
| 1673 | + if (rt5651->gpiod_hp_det) { |
---|
| 1674 | + val = gpiod_get_value_cansleep(rt5651->gpiod_hp_det); |
---|
| 1675 | + dev_dbg(component->dev, "jack-detect gpio %d\n", val); |
---|
| 1676 | + return val; |
---|
| 1677 | + } |
---|
| 1678 | + |
---|
| 1679 | + val = snd_soc_component_read(component, RT5651_INT_IRQ_ST); |
---|
1678 | 1680 | dev_dbg(component->dev, "irq status %#04x\n", val); |
---|
1679 | 1681 | |
---|
1680 | 1682 | switch (rt5651->jd_src) { |
---|
.. | .. |
---|
1691 | 1693 | break; |
---|
1692 | 1694 | } |
---|
1693 | 1695 | |
---|
1694 | | - return val == 0; |
---|
| 1696 | + if (rt5651->jd_active_high) |
---|
| 1697 | + return val != 0; |
---|
| 1698 | + else |
---|
| 1699 | + return val == 0; |
---|
1695 | 1700 | } |
---|
1696 | 1701 | |
---|
1697 | 1702 | /* Jack detect and button-press timings */ |
---|
.. | .. |
---|
1814 | 1819 | return SND_JACK_HEADPHONE; |
---|
1815 | 1820 | } |
---|
1816 | 1821 | |
---|
| 1822 | +static bool rt5651_support_button_press(struct rt5651_priv *rt5651) |
---|
| 1823 | +{ |
---|
| 1824 | + if (!rt5651->hp_jack) |
---|
| 1825 | + return false; |
---|
| 1826 | + |
---|
| 1827 | + /* Button press support only works with internal jack-detection */ |
---|
| 1828 | + return (rt5651->hp_jack->status & SND_JACK_MICROPHONE) && |
---|
| 1829 | + rt5651->gpiod_hp_det == NULL; |
---|
| 1830 | +} |
---|
| 1831 | + |
---|
1817 | 1832 | static void rt5651_jack_detect_work(struct work_struct *work) |
---|
1818 | 1833 | { |
---|
1819 | 1834 | struct rt5651_priv *rt5651 = |
---|
.. | .. |
---|
1838 | 1853 | WARN_ON(rt5651->ovcd_irq_enabled); |
---|
1839 | 1854 | rt5651_enable_micbias1_for_ovcd(component); |
---|
1840 | 1855 | report = rt5651_detect_headset(component); |
---|
1841 | | - if (report == SND_JACK_HEADSET) { |
---|
| 1856 | + dev_dbg(component->dev, "detect report %#02x\n", report); |
---|
| 1857 | + snd_soc_jack_report(rt5651->hp_jack, report, SND_JACK_HEADSET); |
---|
| 1858 | + if (rt5651_support_button_press(rt5651)) { |
---|
1842 | 1859 | /* Enable ovcd IRQ for button press detect. */ |
---|
1843 | 1860 | rt5651_enable_micbias1_ovcd_irq(component); |
---|
1844 | 1861 | } else { |
---|
1845 | 1862 | /* No more need for overcurrent detect. */ |
---|
1846 | 1863 | rt5651_disable_micbias1_for_ovcd(component); |
---|
1847 | 1864 | } |
---|
1848 | | - dev_dbg(component->dev, "detect report %#02x\n", report); |
---|
1849 | | - snd_soc_jack_report(rt5651->hp_jack, report, SND_JACK_HEADSET); |
---|
1850 | 1865 | } else if (rt5651->ovcd_irq_enabled && rt5651_micbias1_ovcd(component)) { |
---|
1851 | 1866 | dev_dbg(component->dev, "OVCD IRQ\n"); |
---|
1852 | 1867 | |
---|
.. | .. |
---|
1890 | 1905 | } |
---|
1891 | 1906 | |
---|
1892 | 1907 | static void rt5651_enable_jack_detect(struct snd_soc_component *component, |
---|
1893 | | - struct snd_soc_jack *hp_jack) |
---|
| 1908 | + struct snd_soc_jack *hp_jack, |
---|
| 1909 | + struct gpio_desc *gpiod_hp_det) |
---|
1894 | 1910 | { |
---|
1895 | 1911 | struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component); |
---|
1896 | | - |
---|
1897 | | - /* IRQ output on GPIO1 */ |
---|
1898 | | - snd_soc_component_update_bits(component, RT5651_GPIO_CTRL1, |
---|
1899 | | - RT5651_GP1_PIN_MASK, RT5651_GP1_PIN_IRQ); |
---|
| 1912 | + bool using_internal_jack_detect = true; |
---|
1900 | 1913 | |
---|
1901 | 1914 | /* Select jack detect source */ |
---|
1902 | 1915 | switch (rt5651->jd_src) { |
---|
| 1916 | + case RT5651_JD_NULL: |
---|
| 1917 | + rt5651->gpiod_hp_det = gpiod_hp_det; |
---|
| 1918 | + if (!rt5651->gpiod_hp_det) |
---|
| 1919 | + return; /* No jack detect */ |
---|
| 1920 | + using_internal_jack_detect = false; |
---|
| 1921 | + break; |
---|
1903 | 1922 | case RT5651_JD1_1: |
---|
1904 | 1923 | snd_soc_component_update_bits(component, RT5651_JD_CTRL2, |
---|
1905 | 1924 | RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD1_1); |
---|
1906 | | - snd_soc_component_update_bits(component, RT5651_IRQ_CTRL1, |
---|
1907 | | - RT5651_JD1_1_IRQ_EN, RT5651_JD1_1_IRQ_EN); |
---|
| 1925 | + /* active-low is normal, set inv flag for active-high */ |
---|
| 1926 | + if (rt5651->jd_active_high) |
---|
| 1927 | + snd_soc_component_update_bits(component, |
---|
| 1928 | + RT5651_IRQ_CTRL1, |
---|
| 1929 | + RT5651_JD1_1_IRQ_EN | RT5651_JD1_1_INV, |
---|
| 1930 | + RT5651_JD1_1_IRQ_EN | RT5651_JD1_1_INV); |
---|
| 1931 | + else |
---|
| 1932 | + snd_soc_component_update_bits(component, |
---|
| 1933 | + RT5651_IRQ_CTRL1, |
---|
| 1934 | + RT5651_JD1_1_IRQ_EN | RT5651_JD1_1_INV, |
---|
| 1935 | + RT5651_JD1_1_IRQ_EN); |
---|
1908 | 1936 | break; |
---|
1909 | 1937 | case RT5651_JD1_2: |
---|
1910 | 1938 | snd_soc_component_update_bits(component, RT5651_JD_CTRL2, |
---|
1911 | 1939 | RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD1_2); |
---|
1912 | | - snd_soc_component_update_bits(component, RT5651_IRQ_CTRL1, |
---|
1913 | | - RT5651_JD1_2_IRQ_EN, RT5651_JD1_2_IRQ_EN); |
---|
| 1940 | + /* active-low is normal, set inv flag for active-high */ |
---|
| 1941 | + if (rt5651->jd_active_high) |
---|
| 1942 | + snd_soc_component_update_bits(component, |
---|
| 1943 | + RT5651_IRQ_CTRL1, |
---|
| 1944 | + RT5651_JD1_2_IRQ_EN | RT5651_JD1_2_INV, |
---|
| 1945 | + RT5651_JD1_2_IRQ_EN | RT5651_JD1_2_INV); |
---|
| 1946 | + else |
---|
| 1947 | + snd_soc_component_update_bits(component, |
---|
| 1948 | + RT5651_IRQ_CTRL1, |
---|
| 1949 | + RT5651_JD1_2_IRQ_EN | RT5651_JD1_2_INV, |
---|
| 1950 | + RT5651_JD1_2_IRQ_EN); |
---|
1914 | 1951 | break; |
---|
1915 | 1952 | case RT5651_JD2: |
---|
1916 | 1953 | snd_soc_component_update_bits(component, RT5651_JD_CTRL2, |
---|
1917 | 1954 | RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD2); |
---|
1918 | | - snd_soc_component_update_bits(component, RT5651_IRQ_CTRL1, |
---|
1919 | | - RT5651_JD2_IRQ_EN, RT5651_JD2_IRQ_EN); |
---|
| 1955 | + /* active-low is normal, set inv flag for active-high */ |
---|
| 1956 | + if (rt5651->jd_active_high) |
---|
| 1957 | + snd_soc_component_update_bits(component, |
---|
| 1958 | + RT5651_IRQ_CTRL1, |
---|
| 1959 | + RT5651_JD2_IRQ_EN | RT5651_JD2_INV, |
---|
| 1960 | + RT5651_JD2_IRQ_EN | RT5651_JD2_INV); |
---|
| 1961 | + else |
---|
| 1962 | + snd_soc_component_update_bits(component, |
---|
| 1963 | + RT5651_IRQ_CTRL1, |
---|
| 1964 | + RT5651_JD2_IRQ_EN | RT5651_JD2_INV, |
---|
| 1965 | + RT5651_JD2_IRQ_EN); |
---|
1920 | 1966 | break; |
---|
1921 | | - case RT5651_JD_NULL: |
---|
1922 | | - return; |
---|
1923 | 1967 | default: |
---|
1924 | 1968 | dev_err(component->dev, "Currently only JD1_1 / JD1_2 / JD2 are supported\n"); |
---|
1925 | 1969 | return; |
---|
1926 | 1970 | } |
---|
1927 | 1971 | |
---|
1928 | | - /* Enable jack detect power */ |
---|
1929 | | - snd_soc_component_update_bits(component, RT5651_PWR_ANLG2, |
---|
1930 | | - RT5651_PWR_JD_M, RT5651_PWR_JD_M); |
---|
| 1972 | + if (using_internal_jack_detect) { |
---|
| 1973 | + /* IRQ output on GPIO1 */ |
---|
| 1974 | + snd_soc_component_update_bits(component, RT5651_GPIO_CTRL1, |
---|
| 1975 | + RT5651_GP1_PIN_MASK, RT5651_GP1_PIN_IRQ); |
---|
| 1976 | + |
---|
| 1977 | + /* Enable jack detect power */ |
---|
| 1978 | + snd_soc_component_update_bits(component, RT5651_PWR_ANLG2, |
---|
| 1979 | + RT5651_PWR_JD_M, RT5651_PWR_JD_M); |
---|
| 1980 | + } |
---|
1931 | 1981 | |
---|
1932 | 1982 | /* Set OVCD threshold current and scale-factor */ |
---|
1933 | 1983 | snd_soc_component_write(component, RT5651_PR_BASE + RT5651_BIAS_CUR4, |
---|
.. | .. |
---|
1956 | 2006 | RT5651_MB1_OC_STKY_MASK, RT5651_MB1_OC_STKY_EN); |
---|
1957 | 2007 | |
---|
1958 | 2008 | rt5651->hp_jack = hp_jack; |
---|
1959 | | - if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) { |
---|
| 2009 | + if (rt5651_support_button_press(rt5651)) { |
---|
1960 | 2010 | rt5651_enable_micbias1_for_ovcd(component); |
---|
1961 | 2011 | rt5651_enable_micbias1_ovcd_irq(component); |
---|
1962 | 2012 | } |
---|
.. | .. |
---|
1973 | 2023 | disable_irq(rt5651->irq); |
---|
1974 | 2024 | rt5651_cancel_work(rt5651); |
---|
1975 | 2025 | |
---|
1976 | | - if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) { |
---|
| 2026 | + if (rt5651_support_button_press(rt5651)) { |
---|
1977 | 2027 | rt5651_disable_micbias1_ovcd_irq(component); |
---|
1978 | 2028 | rt5651_disable_micbias1_for_ovcd(component); |
---|
1979 | 2029 | snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0); |
---|
.. | .. |
---|
1986 | 2036 | struct snd_soc_jack *jack, void *data) |
---|
1987 | 2037 | { |
---|
1988 | 2038 | if (jack) |
---|
1989 | | - rt5651_enable_jack_detect(component, jack); |
---|
| 2039 | + rt5651_enable_jack_detect(component, jack, data); |
---|
1990 | 2040 | else |
---|
1991 | 2041 | rt5651_disable_jack_detect(component); |
---|
1992 | 2042 | |
---|
.. | .. |
---|
2016 | 2066 | if (device_property_read_u32(component->dev, |
---|
2017 | 2067 | "realtek,jack-detect-source", &val) == 0) |
---|
2018 | 2068 | rt5651->jd_src = val; |
---|
| 2069 | + |
---|
| 2070 | + if (device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted")) |
---|
| 2071 | + rt5651->jd_active_high = true; |
---|
2019 | 2072 | |
---|
2020 | 2073 | /* |
---|
2021 | 2074 | * Testing on various boards has shown that good defaults for the OVCD |
---|
.. | .. |
---|
2080 | 2133 | gpiod_set_value(rt5651->gpiod_spk_ctl, enable); |
---|
2081 | 2134 | } |
---|
2082 | 2135 | |
---|
2083 | | -static int rt5651_mute(struct snd_soc_dai *dai, int mute) |
---|
| 2136 | +static int rt5651_mute(struct snd_soc_dai *dai, int mute, int stream) |
---|
2084 | 2137 | { |
---|
2085 | 2138 | struct snd_soc_component *component = dai->component; |
---|
2086 | 2139 | struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component); |
---|
.. | .. |
---|
2125 | 2178 | .set_fmt = rt5651_set_dai_fmt, |
---|
2126 | 2179 | .set_sysclk = rt5651_set_dai_sysclk, |
---|
2127 | 2180 | .set_pll = rt5651_set_dai_pll, |
---|
2128 | | - .digital_mute = rt5651_mute, |
---|
| 2181 | + .mute_stream = rt5651_mute, |
---|
| 2182 | + .no_capture_mute = 1, |
---|
2129 | 2183 | }; |
---|
2130 | 2184 | |
---|
2131 | 2185 | static struct snd_soc_dai_driver rt5651_dai[] = { |
---|
.. | .. |
---|
2200 | 2254 | .num_reg_defaults = ARRAY_SIZE(rt5651_reg), |
---|
2201 | 2255 | .ranges = rt5651_ranges, |
---|
2202 | 2256 | .num_ranges = ARRAY_SIZE(rt5651_ranges), |
---|
2203 | | - .use_single_rw = true, |
---|
| 2257 | + .use_single_read = true, |
---|
| 2258 | + .use_single_write = true, |
---|
2204 | 2259 | }; |
---|
2205 | 2260 | |
---|
2206 | 2261 | #if defined(CONFIG_OF) |
---|
.. | .. |
---|
2214 | 2269 | #ifdef CONFIG_ACPI |
---|
2215 | 2270 | static const struct acpi_device_id rt5651_acpi_match[] = { |
---|
2216 | 2271 | { "10EC5651", 0 }, |
---|
| 2272 | + { "10EC5640", 0 }, |
---|
2217 | 2273 | { }, |
---|
2218 | 2274 | }; |
---|
2219 | 2275 | MODULE_DEVICE_TABLE(acpi, rt5651_acpi_match); |
---|
.. | .. |
---|
2234 | 2290 | { |
---|
2235 | 2291 | struct rt5651_priv *rt5651; |
---|
2236 | 2292 | int ret; |
---|
| 2293 | + int err; |
---|
2237 | 2294 | |
---|
2238 | 2295 | rt5651 = devm_kzalloc(&i2c->dev, sizeof(*rt5651), |
---|
2239 | 2296 | GFP_KERNEL); |
---|
.. | .. |
---|
2250 | 2307 | return ret; |
---|
2251 | 2308 | } |
---|
2252 | 2309 | |
---|
2253 | | - regmap_read(rt5651->regmap, RT5651_DEVICE_ID, &ret); |
---|
| 2310 | + err = regmap_read(rt5651->regmap, RT5651_DEVICE_ID, &ret); |
---|
| 2311 | + if (err) |
---|
| 2312 | + return err; |
---|
| 2313 | + |
---|
2254 | 2314 | if (ret != RT5651_DEVICE_ID_VALUE) { |
---|
2255 | 2315 | dev_err(&i2c->dev, |
---|
2256 | 2316 | "Device with ID register %#x is not rt5651\n", ret); |
---|
.. | .. |
---|
2265 | 2325 | dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret); |
---|
2266 | 2326 | |
---|
2267 | 2327 | rt5651->irq = i2c->irq; |
---|
2268 | | - rt5651->hp_mute = 1; |
---|
| 2328 | + rt5651->hp_mute = true; |
---|
2269 | 2329 | |
---|
2270 | 2330 | INIT_DELAYED_WORK(&rt5651->bp_work, rt5651_button_press_work); |
---|
2271 | 2331 | INIT_WORK(&rt5651->jack_detect_work, rt5651_jack_detect_work); |
---|