hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/sound/soc/codecs/rt5663.c
....@@ -1,12 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * rt5663.c -- RT5663 ALSA SoC audio codec driver
34 *
45 * Copyright 2016 Realtek Semiconductor Corp.
56 * Author: Jack Yu <jack.yu@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 #include <linux/module.h>
129 #include <linux/moduleparam.h>
....@@ -17,6 +14,7 @@
1714 #include <linux/platform_device.h>
1815 #include <linux/spi/spi.h>
1916 #include <linux/acpi.h>
17
+#include <linux/regulator/consumer.h>
2018 #include <linux/workqueue.h>
2119 #include <sound/core.h>
2220 #include <sound/pcm.h>
....@@ -33,6 +31,9 @@
3331 #define RT5663_DEVICE_ID_2 0x6451
3432 #define RT5663_DEVICE_ID_1 0x6406
3533
34
+#define RT5663_POWER_ON_DELAY_MS 300
35
+#define RT5663_SUPPLY_CURRENT_UA 500000
36
+
3637 enum {
3738 CODEC_VER_1,
3839 CODEC_VER_0,
....@@ -48,6 +49,11 @@
4849 unsigned int dc_offset_r_manual_mic;
4950 };
5051
52
+static const char *const rt5663_supply_names[] = {
53
+ "avdd",
54
+ "cpvdd",
55
+};
56
+
5157 struct rt5663_priv {
5258 struct snd_soc_component *component;
5359 struct rt5663_platform_data pdata;
....@@ -56,6 +62,7 @@
5662 struct snd_soc_jack *hs_jack;
5763 struct timer_list btn_check_timer;
5864 struct impedance_mapping_table *imp_table;
65
+ struct regulator_bulk_data supplies[ARRAY_SIZE(rt5663_supply_names)];
5966
6067 int codec_ver;
6168 int sysclk;
....@@ -72,6 +79,7 @@
7279 static const struct reg_sequence rt5663_patch_list[] = {
7380 { 0x002a, 0x8020 },
7481 { 0x0086, 0x0028 },
82
+ { 0x0100, 0xa020 },
7583 { 0x0117, 0x0f28 },
7684 { 0x02fb, 0x8089 },
7785 };
....@@ -580,7 +588,7 @@
580588 { 0x00fd, 0x0001 },
581589 { 0x00fe, 0x10ec },
582590 { 0x00ff, 0x6406 },
583
- { 0x0100, 0xa0a0 },
591
+ { 0x0100, 0xa020 },
584592 { 0x0108, 0x4444 },
585593 { 0x0109, 0x4444 },
586594 { 0x010a, 0xaaaa },
....@@ -1474,7 +1482,7 @@
14741482
14751483 while (i < 5) {
14761484 msleep(sleep_time[i]);
1477
- val = snd_soc_component_read32(component, RT5663_CBJ_TYPE_2) & 0x0003;
1485
+ val = snd_soc_component_read(component, RT5663_CBJ_TYPE_2) & 0x0003;
14781486 if (val == 0x1 || val == 0x2 || val == 0x3)
14791487 break;
14801488 dev_dbg(component->dev, "%s: MX-0011 val=%x sleep %d\n",
....@@ -1587,7 +1595,7 @@
15871595 i++;
15881596 }
15891597
1590
- val = snd_soc_component_read32(component, RT5663_EM_JACK_TYPE_2) & 0x0003;
1598
+ val = snd_soc_component_read(component, RT5663_EM_JACK_TYPE_2) & 0x0003;
15911599 dev_dbg(component->dev, "%s val = %d\n", __func__, val);
15921600
15931601 snd_soc_component_update_bits(component, RT5663_HP_CHARGE_PUMP_1,
....@@ -1690,12 +1698,12 @@
16901698 rt5663->imp_table[i].dc_offset_r_manual & 0xffff);
16911699 }
16921700
1693
- reg84 = snd_soc_component_read32(component, RT5663_ASRC_2);
1694
- reg26 = snd_soc_component_read32(component, RT5663_STO1_ADC_MIXER);
1695
- reg2fa = snd_soc_component_read32(component, RT5663_DUMMY_1);
1696
- reg91 = snd_soc_component_read32(component, RT5663_HP_CHARGE_PUMP_1);
1697
- reg10 = snd_soc_component_read32(component, RT5663_RECMIX);
1698
- reg80 = snd_soc_component_read32(component, RT5663_GLB_CLK);
1701
+ reg84 = snd_soc_component_read(component, RT5663_ASRC_2);
1702
+ reg26 = snd_soc_component_read(component, RT5663_STO1_ADC_MIXER);
1703
+ reg2fa = snd_soc_component_read(component, RT5663_DUMMY_1);
1704
+ reg91 = snd_soc_component_read(component, RT5663_HP_CHARGE_PUMP_1);
1705
+ reg10 = snd_soc_component_read(component, RT5663_RECMIX);
1706
+ reg80 = snd_soc_component_read(component, RT5663_GLB_CLK);
16991707
17001708 snd_soc_component_update_bits(component, RT5663_STO_DRE_1, 0x8000, 0);
17011709 snd_soc_component_write(component, RT5663_ASRC_2, 0);
....@@ -1760,11 +1768,11 @@
17601768
17611769 for (i = 0; i < 100; i++) {
17621770 msleep(20);
1763
- if (snd_soc_component_read32(component, RT5663_INT_ST_1) & 0x2)
1771
+ if (snd_soc_component_read(component, RT5663_INT_ST_1) & 0x2)
17641772 break;
17651773 }
17661774
1767
- value = snd_soc_component_read32(component, RT5663_HP_IMP_SEN_4);
1775
+ value = snd_soc_component_read(component, RT5663_HP_IMP_SEN_4);
17681776
17691777 snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x3000, 0);
17701778 snd_soc_component_write(component, RT5663_INT_ST_1, 0);
....@@ -1835,7 +1843,7 @@
18351843 {
18361844 int btn_type, val;
18371845
1838
- val = snd_soc_component_read32(component, RT5663_IL_CMD_5);
1846
+ val = snd_soc_component_read(component, RT5663_IL_CMD_5);
18391847 dev_dbg(component->dev, "%s: val=0x%x\n", __func__, val);
18401848 btn_type = val & 0xfff0;
18411849 snd_soc_component_write(component, RT5663_IL_CMD_5, val);
....@@ -1871,7 +1879,7 @@
18711879 static bool rt5663_check_jd_status(struct snd_soc_component *component)
18721880 {
18731881 struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1874
- int val = snd_soc_component_read32(component, RT5663_INT_ST_1);
1882
+ int val = snd_soc_component_read(component, RT5663_INT_ST_1);
18751883
18761884 dev_dbg(component->dev, "%s val=%x\n", __func__, val);
18771885
....@@ -2064,7 +2072,7 @@
20642072 unsigned int val;
20652073 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
20662074
2067
- val = snd_soc_component_read32(component, RT5663_GLB_CLK);
2075
+ val = snd_soc_component_read(component, RT5663_GLB_CLK);
20682076 val &= RT5663_SCLK_SRC_MASK;
20692077 if (val == RT5663_SCLK_SRC_PLL1)
20702078 return 1;
....@@ -2107,7 +2115,7 @@
21072115 }
21082116 }
21092117
2110
- val = (snd_soc_component_read32(component, reg) >> shift) & 0x7;
2118
+ val = (snd_soc_component_read(component, reg) >> shift) & 0x7;
21112119
21122120 if (val)
21132121 return 1;
....@@ -2122,15 +2130,15 @@
21222130 struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
21232131 int da_asrc_en, ad_asrc_en;
21242132
2125
- da_asrc_en = (snd_soc_component_read32(component, RT5663_ASRC_2) &
2133
+ da_asrc_en = (snd_soc_component_read(component, RT5663_ASRC_2) &
21262134 RT5663_DA_STO1_TRACK_MASK) ? 1 : 0;
21272135 switch (rt5663->codec_ver) {
21282136 case CODEC_VER_1:
2129
- ad_asrc_en = (snd_soc_component_read32(component, RT5663_ASRC_3) &
2137
+ ad_asrc_en = (snd_soc_component_read(component, RT5663_ASRC_3) &
21302138 RT5663_V2_AD_STO1_TRACK_MASK) ? 1 : 0;
21312139 break;
21322140 case CODEC_VER_0:
2133
- ad_asrc_en = (snd_soc_component_read32(component, RT5663_ASRC_2) &
2141
+ ad_asrc_en = (snd_soc_component_read(component, RT5663_ASRC_2) &
21342142 RT5663_AD_STO1_TRACK_MASK) ? 1 : 0;
21352143 break;
21362144 default:
....@@ -2337,6 +2345,8 @@
23372345 0x8000);
23382346 snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x3000,
23392347 0x3000);
2348
+ snd_soc_component_update_bits(component,
2349
+ RT5663_DIG_VOL_ZCD, 0x00c0, 0x0080);
23402350 }
23412351 break;
23422352
....@@ -2351,6 +2361,8 @@
23512361 RT5663_OVCD_HP_MASK, RT5663_OVCD_HP_EN);
23522362 snd_soc_component_update_bits(component,
23532363 RT5663_DACREF_LDO, 0x3e0e, 0);
2364
+ snd_soc_component_update_bits(component,
2365
+ RT5663_DIG_VOL_ZCD, 0x00c0, 0);
23542366 }
23552367 break;
23562368
....@@ -3252,7 +3264,8 @@
32523264 static const struct regmap_config rt5663_v2_regmap = {
32533265 .reg_bits = 16,
32543266 .val_bits = 16,
3255
- .use_single_rw = true,
3267
+ .use_single_read = true,
3268
+ .use_single_write = true,
32563269 .max_register = 0x07fa,
32573270 .volatile_reg = rt5663_v2_volatile_register,
32583271 .readable_reg = rt5663_v2_readable_register,
....@@ -3264,7 +3277,8 @@
32643277 static const struct regmap_config rt5663_regmap = {
32653278 .reg_bits = 16,
32663279 .val_bits = 16,
3267
- .use_single_rw = true,
3280
+ .use_single_read = true,
3281
+ .use_single_write = true,
32683282 .max_register = 0x03f3,
32693283 .volatile_reg = rt5663_volatile_register,
32703284 .readable_reg = rt5663_readable_register,
....@@ -3277,7 +3291,8 @@
32773291 .name = "nocache",
32783292 .reg_bits = 16,
32793293 .val_bits = 16,
3280
- .use_single_rw = true,
3294
+ .use_single_read = true,
3295
+ .use_single_write = true,
32813296 .max_register = 0x03f3,
32823297 .cache_type = REGCACHE_NONE,
32833298 };
....@@ -3463,6 +3478,8 @@
34633478 table_size = sizeof(struct impedance_mapping_table) *
34643479 rt5663->pdata.impedance_sensing_num;
34653480 rt5663->imp_table = devm_kzalloc(dev, table_size, GFP_KERNEL);
3481
+ if (!rt5663->imp_table)
3482
+ return -ENOMEM;
34663483 ret = device_property_read_u32_array(dev,
34673484 "realtek,impedance_sensing_table",
34683485 (u32 *)rt5663->imp_table, table_size);
....@@ -3478,7 +3495,7 @@
34783495 {
34793496 struct rt5663_platform_data *pdata = dev_get_platdata(&i2c->dev);
34803497 struct rt5663_priv *rt5663;
3481
- int ret;
3498
+ int ret, i;
34823499 unsigned int val;
34833500 struct regmap *regmap;
34843501
....@@ -3498,12 +3515,44 @@
34983515 return ret;
34993516 }
35003517
3518
+ for (i = 0; i < ARRAY_SIZE(rt5663->supplies); i++)
3519
+ rt5663->supplies[i].supply = rt5663_supply_names[i];
3520
+
3521
+ ret = devm_regulator_bulk_get(&i2c->dev,
3522
+ ARRAY_SIZE(rt5663->supplies),
3523
+ rt5663->supplies);
3524
+ if (ret) {
3525
+ dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
3526
+ return ret;
3527
+ }
3528
+
3529
+ /* Set load for regulator. */
3530
+ for (i = 0; i < ARRAY_SIZE(rt5663->supplies); i++) {
3531
+ ret = regulator_set_load(rt5663->supplies[i].consumer,
3532
+ RT5663_SUPPLY_CURRENT_UA);
3533
+ if (ret < 0) {
3534
+ dev_err(&i2c->dev,
3535
+ "Failed to set regulator load on %s, ret: %d\n",
3536
+ rt5663->supplies[i].supply, ret);
3537
+ return ret;
3538
+ }
3539
+ }
3540
+
3541
+ ret = regulator_bulk_enable(ARRAY_SIZE(rt5663->supplies),
3542
+ rt5663->supplies);
3543
+
3544
+ if (ret) {
3545
+ dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
3546
+ return ret;
3547
+ }
3548
+ msleep(RT5663_POWER_ON_DELAY_MS);
3549
+
35013550 regmap = devm_regmap_init_i2c(i2c, &temp_regmap);
35023551 if (IS_ERR(regmap)) {
35033552 ret = PTR_ERR(regmap);
35043553 dev_err(&i2c->dev, "Failed to allocate temp register map: %d\n",
35053554 ret);
3506
- return ret;
3555
+ goto err_enable;
35073556 }
35083557
35093558 ret = regmap_read(regmap, RT5663_VENDOR_ID_2, &val);
....@@ -3528,14 +3577,15 @@
35283577 dev_err(&i2c->dev,
35293578 "Device with ID register %#x is not rt5663\n",
35303579 val);
3531
- return -ENODEV;
3580
+ ret = -ENODEV;
3581
+ goto err_enable;
35323582 }
35333583
35343584 if (IS_ERR(rt5663->regmap)) {
35353585 ret = PTR_ERR(rt5663->regmap);
35363586 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
35373587 ret);
3538
- return ret;
3588
+ goto err_enable;
35393589 }
35403590
35413591 /* reset and calibrate */
....@@ -3602,7 +3652,7 @@
36023652 regmap_update_bits(rt5663->regmap, RT5663_PWR_ANLG_1,
36033653 RT5663_LDO1_DVO_MASK | RT5663_AMP_HP_MASK,
36043654 RT5663_LDO1_DVO_0_9V | RT5663_AMP_HP_3X);
3605
- break;
3655
+ break;
36063656 case CODEC_VER_0:
36073657 regmap_update_bits(rt5663->regmap, RT5663_DIG_MISC,
36083658 RT5663_DIG_GATE_CTRL_MASK, RT5663_DIG_GATE_CTRL_EN);
....@@ -3621,7 +3671,7 @@
36213671 regmap_update_bits(rt5663->regmap, RT5663_TDM_2,
36223672 RT5663_DATA_SWAP_ADCDAT1_MASK,
36233673 RT5663_DATA_SWAP_ADCDAT1_LL);
3624
- break;
3674
+ break;
36253675 default:
36263676 dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
36273677 }
....@@ -3633,20 +3683,32 @@
36333683 ret = request_irq(i2c->irq, rt5663_irq,
36343684 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
36353685 | IRQF_ONESHOT, "rt5663", rt5663);
3636
- if (ret)
3686
+ if (ret) {
36373687 dev_err(&i2c->dev, "%s Failed to reguest IRQ: %d\n",
36383688 __func__, ret);
3689
+ goto err_enable;
3690
+ }
36393691 }
36403692
36413693 ret = devm_snd_soc_register_component(&i2c->dev,
36423694 &soc_component_dev_rt5663,
36433695 rt5663_dai, ARRAY_SIZE(rt5663_dai));
36443696
3645
- if (ret) {
3646
- if (i2c->irq)
3647
- free_irq(i2c->irq, rt5663);
3648
- }
3697
+ if (ret)
3698
+ goto err_enable;
36493699
3700
+ return 0;
3701
+
3702
+
3703
+ /*
3704
+ * Error after enabling regulators should goto err_enable
3705
+ * to disable regulators.
3706
+ */
3707
+err_enable:
3708
+ if (i2c->irq)
3709
+ free_irq(i2c->irq, rt5663);
3710
+
3711
+ regulator_bulk_disable(ARRAY_SIZE(rt5663->supplies), rt5663->supplies);
36503712 return ret;
36513713 }
36523714
....@@ -3657,6 +3719,8 @@
36573719 if (i2c->irq)
36583720 free_irq(i2c->irq, rt5663);
36593721
3722
+ regulator_bulk_disable(ARRAY_SIZE(rt5663->supplies), rt5663->supplies);
3723
+
36603724 return 0;
36613725 }
36623726