hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/sound/soc/codecs/rt5651.c
....@@ -1,19 +1,16 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * rt5651.c -- RT5651 ALSA SoC audio codec driver
34 *
45 * Copyright 2014 Realtek Semiconductor Corp.
56 * 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.
107 */
118
129 #include <linux/module.h>
13
-#include <linux/moduleparam.h>
1410 #include <linux/init.h>
1511 #include <linux/delay.h>
1612 #include <linux/pm.h>
13
+#include <linux/gpio/consumer.h>
1714 #include <linux/i2c.h>
1815 #include <linux/regmap.h>
1916 #include <linux/platform_device.h>
....@@ -316,8 +313,7 @@
316313 regmap_write(rt5651->regmap, 0x83, 0x1000);
317314 regmap_write(rt5651->regmap, 0x84, 0x7000);
318315 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);
321317 } else {
322318 regmap_write(rt5651->regmap, 0x83, 0x0);
323319 regmap_write(rt5651->regmap, 0x84, 0x0);
....@@ -793,11 +789,11 @@
793789 RT5651_HP_CP_PD | RT5651_HP_SG_EN);
794790 regmap_update_bits(rt5651->regmap, RT5651_PR_BASE +
795791 RT5651_CHPUMP_INT_REG1, 0x0700, 0x0400);
796
- rt5651->hp_mute = 0;
792
+ rt5651->hp_mute = false;
797793 break;
798794
799795 case SND_SOC_DAPM_PRE_PMD:
800
- rt5651->hp_mute = 1;
796
+ rt5651->hp_mute = true;
801797 usleep_range(70000, 75000);
802798 break;
803799
....@@ -1566,7 +1562,7 @@
15661562 if (SND_SOC_BIAS_STANDBY == snd_soc_component_get_bias_level(component)) {
15671563 if (!IS_ERR(rt5651->mclk))
15681564 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)
15701566 snd_soc_component_update_bits(component, RT5651_D_MISC,
15711567 0xc00, 0xc00);
15721568 }
....@@ -1663,7 +1659,7 @@
16631659 {
16641660 int val;
16651661
1666
- val = snd_soc_component_read32(component, RT5651_IRQ_CTRL2);
1662
+ val = snd_soc_component_read(component, RT5651_IRQ_CTRL2);
16671663 dev_dbg(component->dev, "irq ctrl2 %#04x\n", val);
16681664
16691665 return (val & RT5651_MB1_OC_CLR);
....@@ -1674,7 +1670,13 @@
16741670 struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
16751671 int val;
16761672
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);
16781680 dev_dbg(component->dev, "irq status %#04x\n", val);
16791681
16801682 switch (rt5651->jd_src) {
....@@ -1691,7 +1693,10 @@
16911693 break;
16921694 }
16931695
1694
- return val == 0;
1696
+ if (rt5651->jd_active_high)
1697
+ return val != 0;
1698
+ else
1699
+ return val == 0;
16951700 }
16961701
16971702 /* Jack detect and button-press timings */
....@@ -1814,6 +1819,16 @@
18141819 return SND_JACK_HEADPHONE;
18151820 }
18161821
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
+
18171832 static void rt5651_jack_detect_work(struct work_struct *work)
18181833 {
18191834 struct rt5651_priv *rt5651 =
....@@ -1838,15 +1853,15 @@
18381853 WARN_ON(rt5651->ovcd_irq_enabled);
18391854 rt5651_enable_micbias1_for_ovcd(component);
18401855 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)) {
18421859 /* Enable ovcd IRQ for button press detect. */
18431860 rt5651_enable_micbias1_ovcd_irq(component);
18441861 } else {
18451862 /* No more need for overcurrent detect. */
18461863 rt5651_disable_micbias1_for_ovcd(component);
18471864 }
1848
- dev_dbg(component->dev, "detect report %#02x\n", report);
1849
- snd_soc_jack_report(rt5651->hp_jack, report, SND_JACK_HEADSET);
18501865 } else if (rt5651->ovcd_irq_enabled && rt5651_micbias1_ovcd(component)) {
18511866 dev_dbg(component->dev, "OVCD IRQ\n");
18521867
....@@ -1890,44 +1905,79 @@
18901905 }
18911906
18921907 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)
18941910 {
18951911 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;
19001913
19011914 /* Select jack detect source */
19021915 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;
19031922 case RT5651_JD1_1:
19041923 snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
19051924 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);
19081936 break;
19091937 case RT5651_JD1_2:
19101938 snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
19111939 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);
19141951 break;
19151952 case RT5651_JD2:
19161953 snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
19171954 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);
19201966 break;
1921
- case RT5651_JD_NULL:
1922
- return;
19231967 default:
19241968 dev_err(component->dev, "Currently only JD1_1 / JD1_2 / JD2 are supported\n");
19251969 return;
19261970 }
19271971
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
+ }
19311981
19321982 /* Set OVCD threshold current and scale-factor */
19331983 snd_soc_component_write(component, RT5651_PR_BASE + RT5651_BIAS_CUR4,
....@@ -1956,7 +2006,7 @@
19562006 RT5651_MB1_OC_STKY_MASK, RT5651_MB1_OC_STKY_EN);
19572007
19582008 rt5651->hp_jack = hp_jack;
1959
- if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) {
2009
+ if (rt5651_support_button_press(rt5651)) {
19602010 rt5651_enable_micbias1_for_ovcd(component);
19612011 rt5651_enable_micbias1_ovcd_irq(component);
19622012 }
....@@ -1973,7 +2023,7 @@
19732023 disable_irq(rt5651->irq);
19742024 rt5651_cancel_work(rt5651);
19752025
1976
- if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) {
2026
+ if (rt5651_support_button_press(rt5651)) {
19772027 rt5651_disable_micbias1_ovcd_irq(component);
19782028 rt5651_disable_micbias1_for_ovcd(component);
19792029 snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0);
....@@ -1986,7 +2036,7 @@
19862036 struct snd_soc_jack *jack, void *data)
19872037 {
19882038 if (jack)
1989
- rt5651_enable_jack_detect(component, jack);
2039
+ rt5651_enable_jack_detect(component, jack, data);
19902040 else
19912041 rt5651_disable_jack_detect(component);
19922042
....@@ -2016,6 +2066,9 @@
20162066 if (device_property_read_u32(component->dev,
20172067 "realtek,jack-detect-source", &val) == 0)
20182068 rt5651->jd_src = val;
2069
+
2070
+ if (device_property_read_bool(component->dev, "realtek,jack-detect-not-inverted"))
2071
+ rt5651->jd_active_high = true;
20192072
20202073 /*
20212074 * Testing on various boards has shown that good defaults for the OVCD
....@@ -2080,7 +2133,7 @@
20802133 gpiod_set_value(rt5651->gpiod_spk_ctl, enable);
20812134 }
20822135
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)
20842137 {
20852138 struct snd_soc_component *component = dai->component;
20862139 struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
....@@ -2125,7 +2178,8 @@
21252178 .set_fmt = rt5651_set_dai_fmt,
21262179 .set_sysclk = rt5651_set_dai_sysclk,
21272180 .set_pll = rt5651_set_dai_pll,
2128
- .digital_mute = rt5651_mute,
2181
+ .mute_stream = rt5651_mute,
2182
+ .no_capture_mute = 1,
21292183 };
21302184
21312185 static struct snd_soc_dai_driver rt5651_dai[] = {
....@@ -2200,7 +2254,8 @@
22002254 .num_reg_defaults = ARRAY_SIZE(rt5651_reg),
22012255 .ranges = rt5651_ranges,
22022256 .num_ranges = ARRAY_SIZE(rt5651_ranges),
2203
- .use_single_rw = true,
2257
+ .use_single_read = true,
2258
+ .use_single_write = true,
22042259 };
22052260
22062261 #if defined(CONFIG_OF)
....@@ -2214,6 +2269,7 @@
22142269 #ifdef CONFIG_ACPI
22152270 static const struct acpi_device_id rt5651_acpi_match[] = {
22162271 { "10EC5651", 0 },
2272
+ { "10EC5640", 0 },
22172273 { },
22182274 };
22192275 MODULE_DEVICE_TABLE(acpi, rt5651_acpi_match);
....@@ -2234,6 +2290,7 @@
22342290 {
22352291 struct rt5651_priv *rt5651;
22362292 int ret;
2293
+ int err;
22372294
22382295 rt5651 = devm_kzalloc(&i2c->dev, sizeof(*rt5651),
22392296 GFP_KERNEL);
....@@ -2250,7 +2307,10 @@
22502307 return ret;
22512308 }
22522309
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
+
22542314 if (ret != RT5651_DEVICE_ID_VALUE) {
22552315 dev_err(&i2c->dev,
22562316 "Device with ID register %#x is not rt5651\n", ret);
....@@ -2265,7 +2325,7 @@
22652325 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
22662326
22672327 rt5651->irq = i2c->irq;
2268
- rt5651->hp_mute = 1;
2328
+ rt5651->hp_mute = true;
22692329
22702330 INIT_DELAYED_WORK(&rt5651->bp_work, rt5651_button_press_work);
22712331 INIT_WORK(&rt5651->jack_detect_work, rt5651_jack_detect_work);