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 62 if (snd_soc_component_read(rt5682->component, RT5682_AJD1_CTRL) 63 & RT5682_JDH_RS_MASK) { 64 /* jack out */ 65 rt5682->jack_type = rt5682_headset_detect(rt5682->component, 0); 66 67 snd_soc_jack_report(rt5682->hs_jack, rt5682->jack_type, 68 SND_JACK_HEADSET | 69 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 70 SND_JACK_BTN_2 | SND_JACK_BTN_3); 71 } else { 72 schedule_delayed_work(&rt5682->jd_check_work, 500); 73 } 74 } 75 76 static irqreturn_t rt5682_irq(int irq, void *data) 77 { 78 struct rt5682_priv *rt5682 = data; 79 80 mod_delayed_work(system_power_efficient_wq, 81 &rt5682->jack_detect_work, msecs_to_jiffies(250)); 82 83 return IRQ_HANDLED; 84 } 85 86 static struct snd_soc_dai_driver rt5682_dai[] = { 87 { 88 .name = "rt5682-aif1", 89 .id = RT5682_AIF1, 90 .playback = { 91 .stream_name = "AIF1 Playback", 92 .channels_min = 1, 93 .channels_max = 2, 94 .rates = RT5682_STEREO_RATES, 95 .formats = RT5682_FORMATS, 96 }, 97 .capture = { 98 .stream_name = "AIF1 Capture", 99 .channels_min = 1, 100 .channels_max = 2, 101 .rates = RT5682_STEREO_RATES, 102 .formats = RT5682_FORMATS, 103 }, 104 .ops = &rt5682_aif1_dai_ops, 105 }, 106 { 107 .name = "rt5682-aif2", 108 .id = RT5682_AIF2, 109 .capture = { 110 .stream_name = "AIF2 Capture", 111 .channels_min = 1, 112 .channels_max = 2, 113 .rates = RT5682_STEREO_RATES, 114 .formats = RT5682_FORMATS, 115 }, 116 .ops = &rt5682_aif2_dai_ops, 117 }, 118 }; 119 120 static int rt5682_i2c_probe(struct i2c_client *i2c, 121 const struct i2c_device_id *id) 122 { 123 struct rt5682_platform_data *pdata = dev_get_platdata(&i2c->dev); 124 struct rt5682_priv *rt5682; 125 int i, ret; 126 unsigned int val; 127 128 rt5682 = devm_kzalloc(&i2c->dev, sizeof(struct rt5682_priv), 129 GFP_KERNEL); 130 if (!rt5682) 131 return -ENOMEM; 132 133 i2c_set_clientdata(i2c, rt5682); 134 135 rt5682->pdata = i2s_default_platform_data; 136 137 if (pdata) 138 rt5682->pdata = *pdata; 139 else 140 rt5682_parse_dt(rt5682, &i2c->dev); 141 142 rt5682->regmap = devm_regmap_init_i2c(i2c, &rt5682_regmap); 143 if (IS_ERR(rt5682->regmap)) { 144 ret = PTR_ERR(rt5682->regmap); 145 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", 146 ret); 147 return ret; 148 } 149 150 for (i = 0; i < ARRAY_SIZE(rt5682->supplies); i++) 151 rt5682->supplies[i].supply = rt5682_supply_names[i]; 152 153 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(rt5682->supplies), 154 rt5682->supplies); 155 if (ret) { 156 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret); 157 return ret; 158 } 159 160 ret = regulator_bulk_enable(ARRAY_SIZE(rt5682->supplies), 161 rt5682->supplies); 162 if (ret) { 163 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); 164 return ret; 165 } 166 167 if (gpio_is_valid(rt5682->pdata.ldo1_en)) { 168 if (devm_gpio_request_one(&i2c->dev, rt5682->pdata.ldo1_en, 169 GPIOF_OUT_INIT_HIGH, "rt5682")) 170 dev_err(&i2c->dev, "Fail gpio_request gpio_ldo\n"); 171 } 172 173 /* Sleep for 300 ms miniumum */ 174 usleep_range(300000, 350000); 175 176 regmap_write(rt5682->regmap, RT5682_I2C_MODE, 0x1); 177 usleep_range(10000, 15000); 178 179 regmap_read(rt5682->regmap, RT5682_DEVICE_ID, &val); 180 if (val != DEVICE_ID) { 181 dev_err(&i2c->dev, 182 "Device with ID register %x is not rt5682\n", val); 183 return -ENODEV; 184 } 185 186 mutex_init(&rt5682->calibrate_mutex); 187 rt5682_calibrate(rt5682); 188 189 rt5682_apply_patch_list(rt5682, &i2c->dev); 190 191 regmap_write(rt5682->regmap, RT5682_DEPOP_1, 0x0000); 192 193 /* DMIC pin*/ 194 if (rt5682->pdata.dmic1_data_pin != RT5682_DMIC1_NULL) { 195 switch (rt5682->pdata.dmic1_data_pin) { 196 case RT5682_DMIC1_DATA_GPIO2: /* share with LRCK2 */ 197 regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, 198 RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO2); 199 regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, 200 RT5682_GP2_PIN_MASK, RT5682_GP2_PIN_DMIC_SDA); 201 break; 202 203 case RT5682_DMIC1_DATA_GPIO5: /* share with DACDAT1 */ 204 regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, 205 RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO5); 206 regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, 207 RT5682_GP5_PIN_MASK, RT5682_GP5_PIN_DMIC_SDA); 208 break; 209 210 default: 211 dev_warn(&i2c->dev, "invalid DMIC_DAT pin\n"); 212 break; 213 } 214 215 switch (rt5682->pdata.dmic1_clk_pin) { 216 case RT5682_DMIC1_CLK_GPIO1: /* share with IRQ */ 217 regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, 218 RT5682_GP1_PIN_MASK, RT5682_GP1_PIN_DMIC_CLK); 219 break; 220 221 case RT5682_DMIC1_CLK_GPIO3: /* share with BCLK2 */ 222 regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, 223 RT5682_GP3_PIN_MASK, RT5682_GP3_PIN_DMIC_CLK); 224 break; 225 226 default: 227 dev_warn(&i2c->dev, "invalid DMIC_CLK pin\n"); 228 break; 229 } 230 } 231 232 regmap_update_bits(rt5682->regmap, RT5682_PWR_ANLG_1, 233 RT5682_LDO1_DVO_MASK | RT5682_HP_DRIVER_MASK, 234 RT5682_LDO1_DVO_12 | RT5682_HP_DRIVER_5X); 235 regmap_write(rt5682->regmap, RT5682_MICBIAS_2, 0x0080); 236 regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, 237 RT5682_GP4_PIN_MASK | RT5682_GP5_PIN_MASK, 238 RT5682_GP4_PIN_ADCDAT1 | RT5682_GP5_PIN_DACDAT1); 239 regmap_write(rt5682->regmap, RT5682_TEST_MODE_CTRL_1, 0x0000); 240 regmap_update_bits(rt5682->regmap, RT5682_BIAS_CUR_CTRL_8, 241 RT5682_HPA_CP_BIAS_CTRL_MASK, RT5682_HPA_CP_BIAS_3UA); 242 regmap_update_bits(rt5682->regmap, RT5682_CHARGE_PUMP_1, 243 RT5682_CP_CLK_HP_MASK, RT5682_CP_CLK_HP_300KHZ); 244 regmap_update_bits(rt5682->regmap, RT5682_HP_CHARGE_PUMP_1, 245 RT5682_PM_HP_MASK, RT5682_PM_HP_HV); 246 regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, 247 RT5682_FIFO_CLK_DIV_MASK, RT5682_FIFO_CLK_DIV_2); 248 249 INIT_DELAYED_WORK(&rt5682->jack_detect_work, 250 rt5682_jack_detect_handler); 251 INIT_DELAYED_WORK(&rt5682->jd_check_work, 252 rt5682_jd_check_handler); 253 254 if (i2c->irq) { 255 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, 256 rt5682_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING 257 | IRQF_ONESHOT, "rt5682", rt5682); 258 if (ret) 259 dev_err(&i2c->dev, "Failed to reguest IRQ: %d\n", ret); 260 } 261 262 return devm_snd_soc_register_component(&i2c->dev, 263 &rt5682_soc_component_dev, 264 rt5682_dai, ARRAY_SIZE(rt5682_dai)); 265 } 266 267 static void rt5682_i2c_shutdown(struct i2c_client *client) 268 { 269 struct rt5682_priv *rt5682 = i2c_get_clientdata(client); 270 271 rt5682_reset(rt5682); 272 } 273 274 static const struct of_device_id rt5682_of_match[] = { 275 {.compatible = "realtek,rt5682i"}, 276 {}, 277 }; 278 MODULE_DEVICE_TABLE(of, rt5682_of_match); 279 280 static const struct acpi_device_id rt5682_acpi_match[] = { 281 {"10EC5682", 0,}, 282 {}, 283 }; 284 MODULE_DEVICE_TABLE(acpi, rt5682_acpi_match); 285 286 static const struct i2c_device_id rt5682_i2c_id[] = { 287 {"rt5682", 0}, 288 {} 289 }; 290 MODULE_DEVICE_TABLE(i2c, rt5682_i2c_id); 291 292 static struct i2c_driver rt5682_i2c_driver = { 293 .driver = { 294 .name = "rt5682", 295 .of_match_table = rt5682_of_match, 296 .acpi_match_table = rt5682_acpi_match, 297 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 298 }, 299 .probe = rt5682_i2c_probe, 300 .shutdown = rt5682_i2c_shutdown, 301 .id_table = rt5682_i2c_id, 302 }; 303 module_i2c_driver(rt5682_i2c_driver); 304 305 MODULE_DESCRIPTION("ASoC RT5682 driver"); 306 MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>"); 307 MODULE_LICENSE("GPL v2"); 308