Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | // |
| 3 | // rt5682.c -- RT5682 ALSA SoC audio component driver |
| 4 | // |
| 5 | // Copyright 2018 Realtek Semiconductor Corp. |
| 6 | // Author: Bard Liao <bardliao@realtek.com> |
| 7 | // |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/moduleparam.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/pm.h> |
| 14 | #include <linux/pm_runtime.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/spi/spi.h> |
| 18 | #include <linux/acpi.h> |
| 19 | #include <linux/gpio.h> |
| 20 | #include <linux/of_gpio.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/jack.h> |
| 26 | #include <sound/soc.h> |
| 27 | #include <sound/soc-dapm.h> |
| 28 | #include <sound/initval.h> |
| 29 | #include <sound/tlv.h> |
| 30 | #include <sound/rt5682.h> |
| 31 | |
| 32 | #include "rl6231.h" |
| 33 | #include "rt5682.h" |
| 34 | |
| 35 | static const struct rt5682_platform_data i2s_default_platform_data = { |
| 36 | .dmic1_data_pin = RT5682_DMIC1_DATA_GPIO2, |
| 37 | .dmic1_clk_pin = RT5682_DMIC1_CLK_GPIO3, |
| 38 | .jd_src = RT5682_JD1, |
| 39 | .btndet_delay = 16, |
| 40 | .dai_clk_names[RT5682_DAI_WCLK_IDX] = "rt5682-dai-wclk", |
| 41 | .dai_clk_names[RT5682_DAI_BCLK_IDX] = "rt5682-dai-bclk", |
| 42 | }; |
| 43 | |
| 44 | static const struct regmap_config rt5682_regmap = { |
| 45 | .reg_bits = 16, |
| 46 | .val_bits = 16, |
| 47 | .max_register = RT5682_I2C_MODE, |
| 48 | .volatile_reg = rt5682_volatile_register, |
| 49 | .readable_reg = rt5682_readable_register, |
| 50 | .cache_type = REGCACHE_RBTREE, |
| 51 | .reg_defaults = rt5682_reg, |
| 52 | .num_reg_defaults = RT5682_REG_NUM, |
| 53 | .use_single_read = true, |
| 54 | .use_single_write = true, |
| 55 | }; |
| 56 | |
| 57 | static void rt5682_jd_check_handler(struct work_struct *work) |
| 58 | { |
| 59 | struct rt5682_priv *rt5682 = container_of(work, struct rt5682_priv, |
| 60 | jd_check_work.work); |
| 61 | |
Peter Ujfalusi | 4045daf | 2022-01-26 12:03:25 +0200 | [diff] [blame] | 62 | if (snd_soc_component_read(rt5682->component, RT5682_AJD1_CTRL) & RT5682_JDH_RS_MASK) |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 63 | /* jack out */ |
Peter Ujfalusi | 4045daf | 2022-01-26 12:03:25 +0200 | [diff] [blame] | 64 | mod_delayed_work(system_power_efficient_wq, |
| 65 | &rt5682->jack_detect_work, 0); |
| 66 | else |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 67 | schedule_delayed_work(&rt5682->jd_check_work, 500); |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static irqreturn_t rt5682_irq(int irq, void *data) |
| 71 | { |
| 72 | struct rt5682_priv *rt5682 = data; |
| 73 | |
| 74 | mod_delayed_work(system_power_efficient_wq, |
Shuming Fan | 5427128 | 2021-03-09 16:58:27 +0800 | [diff] [blame] | 75 | &rt5682->jack_detect_work, msecs_to_jiffies(rt5682->irq_work_delay_time)); |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 76 | |
| 77 | return IRQ_HANDLED; |
| 78 | } |
| 79 | |
| 80 | static struct snd_soc_dai_driver rt5682_dai[] = { |
| 81 | { |
| 82 | .name = "rt5682-aif1", |
| 83 | .id = RT5682_AIF1, |
| 84 | .playback = { |
| 85 | .stream_name = "AIF1 Playback", |
| 86 | .channels_min = 1, |
| 87 | .channels_max = 2, |
| 88 | .rates = RT5682_STEREO_RATES, |
| 89 | .formats = RT5682_FORMATS, |
| 90 | }, |
| 91 | .capture = { |
| 92 | .stream_name = "AIF1 Capture", |
| 93 | .channels_min = 1, |
| 94 | .channels_max = 2, |
| 95 | .rates = RT5682_STEREO_RATES, |
| 96 | .formats = RT5682_FORMATS, |
| 97 | }, |
| 98 | .ops = &rt5682_aif1_dai_ops, |
| 99 | }, |
| 100 | { |
| 101 | .name = "rt5682-aif2", |
| 102 | .id = RT5682_AIF2, |
| 103 | .capture = { |
| 104 | .stream_name = "AIF2 Capture", |
| 105 | .channels_min = 1, |
| 106 | .channels_max = 2, |
| 107 | .rates = RT5682_STEREO_RATES, |
| 108 | .formats = RT5682_FORMATS, |
| 109 | }, |
| 110 | .ops = &rt5682_aif2_dai_ops, |
| 111 | }, |
| 112 | }; |
| 113 | |
Douglas Anderson | 772d445 | 2021-08-11 08:17:56 -0700 | [diff] [blame] | 114 | static void rt5682_i2c_disable_regulators(void *data) |
| 115 | { |
| 116 | struct rt5682_priv *rt5682 = data; |
| 117 | |
| 118 | regulator_bulk_disable(ARRAY_SIZE(rt5682->supplies), rt5682->supplies); |
| 119 | } |
| 120 | |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 121 | static int rt5682_i2c_probe(struct i2c_client *i2c, |
| 122 | const struct i2c_device_id *id) |
| 123 | { |
| 124 | struct rt5682_platform_data *pdata = dev_get_platdata(&i2c->dev); |
| 125 | struct rt5682_priv *rt5682; |
| 126 | int i, ret; |
| 127 | unsigned int val; |
| 128 | |
| 129 | rt5682 = devm_kzalloc(&i2c->dev, sizeof(struct rt5682_priv), |
| 130 | GFP_KERNEL); |
| 131 | if (!rt5682) |
| 132 | return -ENOMEM; |
| 133 | |
| 134 | i2c_set_clientdata(i2c, rt5682); |
| 135 | |
Jack Yu | 57589f8 | 2021-09-29 13:43:44 +0800 | [diff] [blame] | 136 | rt5682->i2c_dev = &i2c->dev; |
| 137 | |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 138 | rt5682->pdata = i2s_default_platform_data; |
| 139 | |
| 140 | if (pdata) |
| 141 | rt5682->pdata = *pdata; |
| 142 | else |
| 143 | rt5682_parse_dt(rt5682, &i2c->dev); |
| 144 | |
| 145 | rt5682->regmap = devm_regmap_init_i2c(i2c, &rt5682_regmap); |
| 146 | if (IS_ERR(rt5682->regmap)) { |
| 147 | ret = PTR_ERR(rt5682->regmap); |
| 148 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
| 149 | ret); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | for (i = 0; i < ARRAY_SIZE(rt5682->supplies); i++) |
| 154 | rt5682->supplies[i].supply = rt5682_supply_names[i]; |
| 155 | |
| 156 | ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(rt5682->supplies), |
| 157 | rt5682->supplies); |
| 158 | if (ret) { |
| 159 | dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret); |
| 160 | return ret; |
| 161 | } |
| 162 | |
Douglas Anderson | 772d445 | 2021-08-11 08:17:56 -0700 | [diff] [blame] | 163 | ret = devm_add_action_or_reset(&i2c->dev, rt5682_i2c_disable_regulators, |
| 164 | rt5682); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 168 | ret = regulator_bulk_enable(ARRAY_SIZE(rt5682->supplies), |
| 169 | rt5682->supplies); |
| 170 | if (ret) { |
| 171 | dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | if (gpio_is_valid(rt5682->pdata.ldo1_en)) { |
| 176 | if (devm_gpio_request_one(&i2c->dev, rt5682->pdata.ldo1_en, |
| 177 | GPIOF_OUT_INIT_HIGH, "rt5682")) |
| 178 | dev_err(&i2c->dev, "Fail gpio_request gpio_ldo\n"); |
| 179 | } |
| 180 | |
| 181 | /* Sleep for 300 ms miniumum */ |
| 182 | usleep_range(300000, 350000); |
| 183 | |
| 184 | regmap_write(rt5682->regmap, RT5682_I2C_MODE, 0x1); |
| 185 | usleep_range(10000, 15000); |
| 186 | |
| 187 | regmap_read(rt5682->regmap, RT5682_DEVICE_ID, &val); |
| 188 | if (val != DEVICE_ID) { |
| 189 | dev_err(&i2c->dev, |
| 190 | "Device with ID register %x is not rt5682\n", val); |
| 191 | return -ENODEV; |
| 192 | } |
| 193 | |
| 194 | mutex_init(&rt5682->calibrate_mutex); |
| 195 | rt5682_calibrate(rt5682); |
| 196 | |
| 197 | rt5682_apply_patch_list(rt5682, &i2c->dev); |
| 198 | |
| 199 | regmap_write(rt5682->regmap, RT5682_DEPOP_1, 0x0000); |
| 200 | |
| 201 | /* DMIC pin*/ |
| 202 | if (rt5682->pdata.dmic1_data_pin != RT5682_DMIC1_NULL) { |
| 203 | switch (rt5682->pdata.dmic1_data_pin) { |
| 204 | case RT5682_DMIC1_DATA_GPIO2: /* share with LRCK2 */ |
| 205 | regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, |
| 206 | RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO2); |
| 207 | regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 208 | RT5682_GP2_PIN_MASK, RT5682_GP2_PIN_DMIC_SDA); |
| 209 | break; |
| 210 | |
| 211 | case RT5682_DMIC1_DATA_GPIO5: /* share with DACDAT1 */ |
| 212 | regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, |
| 213 | RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO5); |
| 214 | regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 215 | RT5682_GP5_PIN_MASK, RT5682_GP5_PIN_DMIC_SDA); |
| 216 | break; |
| 217 | |
| 218 | default: |
| 219 | dev_warn(&i2c->dev, "invalid DMIC_DAT pin\n"); |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | switch (rt5682->pdata.dmic1_clk_pin) { |
| 224 | case RT5682_DMIC1_CLK_GPIO1: /* share with IRQ */ |
| 225 | regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 226 | RT5682_GP1_PIN_MASK, RT5682_GP1_PIN_DMIC_CLK); |
| 227 | break; |
| 228 | |
| 229 | case RT5682_DMIC1_CLK_GPIO3: /* share with BCLK2 */ |
| 230 | regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 231 | RT5682_GP3_PIN_MASK, RT5682_GP3_PIN_DMIC_CLK); |
Oder Chiou | 7416f6b | 2020-11-13 13:53:59 +0800 | [diff] [blame] | 232 | if (rt5682->pdata.dmic_clk_driving_high) |
| 233 | regmap_update_bits(rt5682->regmap, |
| 234 | RT5682_PAD_DRIVING_CTRL, |
| 235 | RT5682_PAD_DRV_GP3_MASK, |
| 236 | 2 << RT5682_PAD_DRV_GP3_SFT); |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | default: |
| 240 | dev_warn(&i2c->dev, "invalid DMIC_CLK pin\n"); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | regmap_update_bits(rt5682->regmap, RT5682_PWR_ANLG_1, |
| 246 | RT5682_LDO1_DVO_MASK | RT5682_HP_DRIVER_MASK, |
| 247 | RT5682_LDO1_DVO_12 | RT5682_HP_DRIVER_5X); |
Shuming Fan | 6301adf | 2020-07-17 15:02:28 +0800 | [diff] [blame] | 248 | regmap_write(rt5682->regmap, RT5682_MICBIAS_2, 0x0080); |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 249 | regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 250 | RT5682_GP4_PIN_MASK | RT5682_GP5_PIN_MASK, |
| 251 | RT5682_GP4_PIN_ADCDAT1 | RT5682_GP5_PIN_DACDAT1); |
| 252 | regmap_write(rt5682->regmap, RT5682_TEST_MODE_CTRL_1, 0x0000); |
| 253 | regmap_update_bits(rt5682->regmap, RT5682_BIAS_CUR_CTRL_8, |
| 254 | RT5682_HPA_CP_BIAS_CTRL_MASK, RT5682_HPA_CP_BIAS_3UA); |
| 255 | regmap_update_bits(rt5682->regmap, RT5682_CHARGE_PUMP_1, |
| 256 | RT5682_CP_CLK_HP_MASK, RT5682_CP_CLK_HP_300KHZ); |
| 257 | regmap_update_bits(rt5682->regmap, RT5682_HP_CHARGE_PUMP_1, |
| 258 | RT5682_PM_HP_MASK, RT5682_PM_HP_HV); |
| 259 | regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, |
| 260 | RT5682_FIFO_CLK_DIV_MASK, RT5682_FIFO_CLK_DIV_2); |
| 261 | |
| 262 | INIT_DELAYED_WORK(&rt5682->jack_detect_work, |
| 263 | rt5682_jack_detect_handler); |
| 264 | INIT_DELAYED_WORK(&rt5682->jd_check_work, |
| 265 | rt5682_jd_check_handler); |
| 266 | |
| 267 | if (i2c->irq) { |
| 268 | ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, |
| 269 | rt5682_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
| 270 | | IRQF_ONESHOT, "rt5682", rt5682); |
| 271 | if (ret) |
| 272 | dev_err(&i2c->dev, "Failed to reguest IRQ: %d\n", ret); |
| 273 | } |
| 274 | |
Jack Yu | 57589f8 | 2021-09-29 13:43:44 +0800 | [diff] [blame] | 275 | #ifdef CONFIG_COMMON_CLK |
| 276 | /* Check if MCLK provided */ |
AngeloGioacchino Del Regno | 709d297 | 2021-10-26 10:10:30 +0200 | [diff] [blame] | 277 | rt5682->mclk = devm_clk_get_optional(&i2c->dev, "mclk"); |
| 278 | if (IS_ERR(rt5682->mclk)) |
| 279 | return PTR_ERR(rt5682->mclk); |
Jack Yu | 57589f8 | 2021-09-29 13:43:44 +0800 | [diff] [blame] | 280 | |
| 281 | /* Register CCF DAI clock control */ |
| 282 | ret = rt5682_register_dai_clks(rt5682); |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | |
| 286 | /* Initial setup for CCF */ |
| 287 | rt5682->lrck[RT5682_AIF1] = 48000; |
| 288 | #endif |
| 289 | |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 290 | return devm_snd_soc_register_component(&i2c->dev, |
| 291 | &rt5682_soc_component_dev, |
| 292 | rt5682_dai, ARRAY_SIZE(rt5682_dai)); |
| 293 | } |
| 294 | |
| 295 | static void rt5682_i2c_shutdown(struct i2c_client *client) |
| 296 | { |
| 297 | struct rt5682_priv *rt5682 = i2c_get_clientdata(client); |
| 298 | |
Stephen Boyd | 47bcb1c | 2021-05-08 00:51:50 -0700 | [diff] [blame] | 299 | disable_irq(client->irq); |
Sathyanarayana Nujella | 45a2702 | 2021-02-05 09:14:28 -0800 | [diff] [blame] | 300 | cancel_delayed_work_sync(&rt5682->jack_detect_work); |
| 301 | cancel_delayed_work_sync(&rt5682->jd_check_work); |
| 302 | |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 303 | rt5682_reset(rt5682); |
| 304 | } |
| 305 | |
Stephen Boyd | 87b42ab | 2021-05-08 00:51:51 -0700 | [diff] [blame] | 306 | static int rt5682_i2c_remove(struct i2c_client *client) |
| 307 | { |
Stephen Boyd | 87b42ab | 2021-05-08 00:51:51 -0700 | [diff] [blame] | 308 | rt5682_i2c_shutdown(client); |
Stephen Boyd | 87b42ab | 2021-05-08 00:51:51 -0700 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 313 | static const struct of_device_id rt5682_of_match[] = { |
| 314 | {.compatible = "realtek,rt5682i"}, |
| 315 | {}, |
| 316 | }; |
| 317 | MODULE_DEVICE_TABLE(of, rt5682_of_match); |
| 318 | |
| 319 | static const struct acpi_device_id rt5682_acpi_match[] = { |
| 320 | {"10EC5682", 0,}, |
| 321 | {}, |
| 322 | }; |
| 323 | MODULE_DEVICE_TABLE(acpi, rt5682_acpi_match); |
| 324 | |
| 325 | static const struct i2c_device_id rt5682_i2c_id[] = { |
| 326 | {"rt5682", 0}, |
| 327 | {} |
| 328 | }; |
| 329 | MODULE_DEVICE_TABLE(i2c, rt5682_i2c_id); |
| 330 | |
| 331 | static struct i2c_driver rt5682_i2c_driver = { |
| 332 | .driver = { |
| 333 | .name = "rt5682", |
| 334 | .of_match_table = rt5682_of_match, |
| 335 | .acpi_match_table = rt5682_acpi_match, |
Douglas Anderson | 160c174 | 2020-08-28 16:20:27 -0700 | [diff] [blame] | 336 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 337 | }, |
| 338 | .probe = rt5682_i2c_probe, |
Stephen Boyd | 87b42ab | 2021-05-08 00:51:51 -0700 | [diff] [blame] | 339 | .remove = rt5682_i2c_remove, |
Arnd Bergmann | a50067d | 2020-05-28 11:17:17 +0200 | [diff] [blame] | 340 | .shutdown = rt5682_i2c_shutdown, |
| 341 | .id_table = rt5682_i2c_id, |
| 342 | }; |
| 343 | module_i2c_driver(rt5682_i2c_driver); |
| 344 | |
| 345 | MODULE_DESCRIPTION("ASoC RT5682 driver"); |
| 346 | MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>"); |
| 347 | MODULE_LICENSE("GPL v2"); |