| 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 <[email protected]> |
| 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/i2c.h> |
| 15 |
#include <linux/platform_device.h> |
| 16 |
#include <linux/spi/spi.h> |
| 17 |
#include <linux/acpi.h> |
| 18 |
#include <linux/gpio/consumer.h> |
| 19 |
#include <linux/mutex.h> |
| 20 |
#include <sound/core.h> |
| 21 |
#include <sound/pcm.h> |
| 22 |
#include <sound/pcm_params.h> |
| 23 |
#include <sound/jack.h> |
| 24 |
#include <sound/soc.h> |
| 25 |
#include <sound/soc-dapm.h> |
| 26 |
#include <sound/initval.h> |
| 27 |
#include <sound/tlv.h> |
| 28 |
#include <sound/rt5682.h> |
| 29 |
|
| 30 |
#include "rl6231.h" |
| 31 |
#include "rt5682.h" |
| 32 |
|
| 33 |
static const struct rt5682_platform_data i2s_default_platform_data = { |
| 34 |
.dmic1_data_pin = RT5682_DMIC1_DATA_GPIO2, |
| 35 |
.dmic1_clk_pin = RT5682_DMIC1_CLK_GPIO3, |
| 36 |
.jd_src = RT5682_JD1, |
| 37 |
.btndet_delay = 16, |
| 38 |
.dai_clk_names[RT5682_DAI_WCLK_IDX] = "rt5682-dai-wclk", |
| 39 |
.dai_clk_names[RT5682_DAI_BCLK_IDX] = "rt5682-dai-bclk", |
| 40 |
}; |
| 41 |
|
| 42 |
static const struct regmap_config rt5682_regmap = { |
| 43 |
.reg_bits = 16, |
| 44 |
.val_bits = 16, |
| 45 |
.max_register = RT5682_I2C_MODE, |
| 46 |
.volatile_reg = rt5682_volatile_register, |
| 47 |
.readable_reg = rt5682_readable_register, |
| 48 |
.cache_type = REGCACHE_MAPLE, |
| 49 |
.reg_defaults = rt5682_reg, |
| 50 |
.num_reg_defaults = RT5682_REG_NUM, |
| 51 |
.use_single_read = true, |
| 52 |
.use_single_write = true, |
| 53 |
}; |
| 54 |
|
| 55 |
static void rt5682_jd_check_handler(struct work_struct *work) |
| 56 |
{ |
| 57 |
struct rt5682_priv *rt5682 = container_of(work, struct rt5682_priv, |
| 58 |
jd_check_work.work); |
| 59 |
|
| 60 |
if (snd_soc_component_read(rt5682->component, RT5682_AJD1_CTRL) & RT5682_JDH_RS_MASK) |
| 61 |
/* jack out */ |
| 62 |
mod_delayed_work(system_power_efficient_wq, |
| 63 |
&rt5682->jack_detect_work, 0); |
| 64 |
else |
| 65 |
schedule_delayed_work(&rt5682->jd_check_work, 500); |
| 66 |
} |
| 67 |
|
| 68 |
static irqreturn_t rt5682_irq(int irq, void *data) |
| 69 |
{ |
| 70 |
struct rt5682_priv *rt5682 = data; |
| 71 |
|
| 72 |
mod_delayed_work(system_power_efficient_wq, |
| 73 |
&rt5682->jack_detect_work, msecs_to_jiffies(rt5682->irq_work_delay_time)); |
| 74 |
|
| 75 |
return IRQ_HANDLED; |
| 76 |
} |
| 77 |
|
| 78 |
static struct snd_soc_dai_driver rt5682_dai[] = { |
| 79 |
{ |
| 80 |
.name = "rt5682-aif1", |
| 81 |
.id = RT5682_AIF1, |
| 82 |
.playback = { |
| 83 |
.stream_name = "AIF1 Playback", |
| 84 |
.channels_min = 1, |
| 85 |
.channels_max = 2, |
| 86 |
.rates = RT5682_STEREO_RATES, |
| 87 |
.formats = RT5682_FORMATS, |
| 88 |
}, |
| 89 |
.capture = { |
| 90 |
.stream_name = "AIF1 Capture", |
| 91 |
.channels_min = 1, |
| 92 |
.channels_max = 2, |
| 93 |
.rates = RT5682_STEREO_RATES, |
| 94 |
.formats = RT5682_FORMATS, |
| 95 |
}, |
| 96 |
.ops = &rt5682_aif1_dai_ops, |
| 97 |
}, |
| 98 |
{ |
| 99 |
.name = "rt5682-aif2", |
| 100 |
.id = RT5682_AIF2, |
| 101 |
.capture = { |
| 102 |
.stream_name = "AIF2 Capture", |
| 103 |
.channels_min = 1, |
| 104 |
.channels_max = 2, |
| 105 |
.rates = RT5682_STEREO_RATES, |
| 106 |
.formats = RT5682_FORMATS, |
| 107 |
}, |
| 108 |
.ops = &rt5682_aif2_dai_ops, |
| 109 |
}, |
| 110 |
}; |
| 111 |
|
| 112 |
static void rt5682_i2c_disable_regulators(void *data) |
| 113 |
{ |
| 114 |
struct rt5682_priv *rt5682 = data; |
| 115 |
|
| 116 |
regulator_bulk_disable(ARRAY_SIZE(rt5682->supplies), rt5682->supplies); |
| 117 |
} |
| 118 |
|
| 119 |
static int rt5682_i2c_probe(struct i2c_client *i2c) |
| 120 |
{ |
| 121 |
struct rt5682_platform_data *pdata = dev_get_platdata(&i2c->dev); |
| 122 |
struct rt5682_priv *rt5682; |
| 123 |
int i, ret; |
| 124 |
unsigned int val; |
| 125 |
|
| 126 |
rt5682 = devm_kzalloc(&i2c->dev, sizeof(struct rt5682_priv), |
| 127 |
GFP_KERNEL); |
| 128 |
if (!rt5682) |
| 129 |
return -ENOMEM; |
| 130 |
|
| 131 |
i2c_set_clientdata(i2c, rt5682); |
| 132 |
|
| 133 |
rt5682->i2c_dev = &i2c->dev; |
| 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 |
ret = devm_add_action_or_reset(&i2c->dev, rt5682_i2c_disable_regulators, |
| 168 |
rt5682); |
| 169 |
if (ret) |
| 170 |
return ret; |
| 171 |
|
| 172 |
ret = rt5682_get_ldo1(rt5682, &i2c->dev); |
| 173 |
if (ret) |
| 174 |
return ret; |
| 175 |
|
| 176 |
/* Sleep for 300 ms minimum */ |
| 177 |
usleep_range(300000, 350000); |
| 178 |
|
| 179 |
regmap_write(rt5682->regmap, RT5682_I2C_MODE, 0x1); |
| 180 |
usleep_range(10000, 15000); |
| 181 |
|
| 182 |
regmap_read(rt5682->regmap, RT5682_DEVICE_ID, &val); |
| 183 |
if (val != DEVICE_ID) { |
| 184 |
dev_err(&i2c->dev, |
| 185 |
"Device with ID register %x is not rt5682\n", val); |
| 186 |
return -ENODEV; |
| 187 |
} |
| 188 |
|
| 189 |
regmap_read(rt5682->regmap, RT5682_INT_DEVICE_ID, &val); |
| 190 |
if (val == 0x6956) { |
| 191 |
dev_dbg(&i2c->dev, "ALC5682I-VE device\n"); |
| 192 |
rt5682->ve_ic = true; |
| 193 |
} |
| 194 |
|
| 195 |
mutex_init(&rt5682->calibrate_mutex); |
| 196 |
rt5682_calibrate(rt5682); |
| 197 |
|
| 198 |
rt5682_apply_patch_list(rt5682, &i2c->dev); |
| 199 |
|
| 200 |
regmap_write(rt5682->regmap, RT5682_DEPOP_1, 0x0000); |
| 201 |
|
| 202 |
/* DMIC pin*/ |
| 203 |
if (rt5682->pdata.dmic1_data_pin != RT5682_DMIC1_NULL) { |
| 204 |
switch (rt5682->pdata.dmic1_data_pin) { |
| 205 |
case RT5682_DMIC1_DATA_GPIO2: /* share with LRCK2 */ |
| 206 |
regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, |
| 207 |
RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO2); |
| 208 |
regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 209 |
RT5682_GP2_PIN_MASK, RT5682_GP2_PIN_DMIC_SDA); |
| 210 |
break; |
| 211 |
|
| 212 |
case RT5682_DMIC1_DATA_GPIO5: /* share with DACDAT1 */ |
| 213 |
regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, |
| 214 |
RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO5); |
| 215 |
regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 216 |
RT5682_GP5_PIN_MASK, RT5682_GP5_PIN_DMIC_SDA); |
| 217 |
break; |
| 218 |
|
| 219 |
default: |
| 220 |
dev_warn(&i2c->dev, "invalid DMIC_DAT pin\n"); |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
switch (rt5682->pdata.dmic1_clk_pin) { |
| 225 |
case RT5682_DMIC1_CLK_GPIO1: /* share with IRQ */ |
| 226 |
regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 227 |
RT5682_GP1_PIN_MASK, RT5682_GP1_PIN_DMIC_CLK); |
| 228 |
break; |
| 229 |
|
| 230 |
case RT5682_DMIC1_CLK_GPIO3: /* share with BCLK2 */ |
| 231 |
regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 232 |
RT5682_GP3_PIN_MASK, RT5682_GP3_PIN_DMIC_CLK); |
| 233 |
if (rt5682->pdata.dmic_clk_driving_high) |
| 234 |
regmap_update_bits(rt5682->regmap, |
| 235 |
RT5682_PAD_DRIVING_CTRL, |
| 236 |
RT5682_PAD_DRV_GP3_MASK, |
| 237 |
2 << RT5682_PAD_DRV_GP3_SFT); |
| 238 |
break; |
| 239 |
|
| 240 |
default: |
| 241 |
dev_warn(&i2c->dev, "invalid DMIC_CLK pin\n"); |
| 242 |
break; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
regmap_update_bits(rt5682->regmap, RT5682_PWR_ANLG_1, |
| 247 |
RT5682_LDO1_DVO_MASK | RT5682_HP_DRIVER_MASK, |
| 248 |
RT5682_LDO1_DVO_12 | RT5682_HP_DRIVER_5X); |
| 249 |
regmap_write(rt5682->regmap, RT5682_MICBIAS_2, 0x0080); |
| 250 |
regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1, |
| 251 |
RT5682_GP4_PIN_MASK | RT5682_GP5_PIN_MASK, |
| 252 |
RT5682_GP4_PIN_ADCDAT1 | RT5682_GP5_PIN_DACDAT1); |
| 253 |
regmap_write(rt5682->regmap, RT5682_TEST_MODE_CTRL_1, 0x0000); |
| 254 |
regmap_update_bits(rt5682->regmap, RT5682_BIAS_CUR_CTRL_8, |
| 255 |
RT5682_HPA_CP_BIAS_CTRL_MASK, RT5682_HPA_CP_BIAS_3UA); |
| 256 |
regmap_update_bits(rt5682->regmap, RT5682_CHARGE_PUMP_1, |
| 257 |
RT5682_CP_CLK_HP_MASK, RT5682_CP_CLK_HP_300KHZ); |
| 258 |
regmap_update_bits(rt5682->regmap, RT5682_HP_CHARGE_PUMP_1, |
| 259 |
RT5682_PM_HP_MASK, RT5682_PM_HP_HV); |
| 260 |
regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1, |
| 261 |
RT5682_FIFO_CLK_DIV_MASK, RT5682_FIFO_CLK_DIV_2); |
| 262 |
|
| 263 |
INIT_DELAYED_WORK(&rt5682->jack_detect_work, |
| 264 |
rt5682_jack_detect_handler); |
| 265 |
INIT_DELAYED_WORK(&rt5682->jd_check_work, |
| 266 |
rt5682_jd_check_handler); |
| 267 |
|
| 268 |
if (i2c->irq) { |
| 269 |
ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, |
| 270 |
rt5682_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
| 271 |
| IRQF_ONESHOT, "rt5682", rt5682); |
| 272 |
if (!ret) |
| 273 |
rt5682->irq = i2c->irq; |
| 274 |
else |
| 275 |
dev_err(&i2c->dev, "Failed to request IRQ: %d\n", ret); |
| 276 |
} |
| 277 |
|
| 278 |
#ifdef CONFIG_COMMON_CLK |
| 279 |
/* Check if MCLK provided */ |
| 280 |
rt5682->mclk = devm_clk_get_optional(&i2c->dev, "mclk"); |
| 281 |
if (IS_ERR(rt5682->mclk)) |
| 282 |
return PTR_ERR(rt5682->mclk); |
| 283 |
|
| 284 |
/* Register CCF DAI clock control */ |
| 285 |
ret = rt5682_register_dai_clks(rt5682); |
| 286 |
if (ret) |
| 287 |
return ret; |
| 288 |
|
| 289 |
/* Initial setup for CCF */ |
| 290 |
rt5682->lrck[RT5682_AIF1] = 48000; |
| 291 |
#endif |
| 292 |
|
| 293 |
return devm_snd_soc_register_component(&i2c->dev, |
| 294 |
&rt5682_soc_component_dev, |
| 295 |
rt5682_dai, ARRAY_SIZE(rt5682_dai)); |
| 296 |
} |
| 297 |
|
| 298 |
static void rt5682_i2c_shutdown(struct i2c_client *client) |
| 299 |
{ |
| 300 |
struct rt5682_priv *rt5682 = i2c_get_clientdata(client); |
| 301 |
|
| 302 |
disable_irq(client->irq); |
| 303 |
cancel_delayed_work_sync(&rt5682->jack_detect_work); |
| 304 |
cancel_delayed_work_sync(&rt5682->jd_check_work); |
| 305 |
|
| 306 |
rt5682_reset(rt5682); |
| 307 |
} |
| 308 |
|
| 309 |
static void rt5682_i2c_remove(struct i2c_client *client) |
| 310 |
{ |
| 311 |
rt5682_i2c_shutdown(client); |
| 312 |
} |
| 313 |
|
| 314 |
static const struct of_device_id rt5682_of_match[] = { |
| 315 |
{.compatible = "realtek,rt5682i"}, |
| 316 |
{ } |
| 317 |
}; |
| 318 |
MODULE_DEVICE_TABLE(of, rt5682_of_match); |
| 319 |
|
| 320 |
static const struct acpi_device_id rt5682_acpi_match[] = { |
| 321 |
{ "10EC5682" }, |
| 322 |
{ } |
| 323 |
}; |
| 324 |
MODULE_DEVICE_TABLE(acpi, rt5682_acpi_match); |
| 325 |
|
| 326 |
static const struct i2c_device_id rt5682_i2c_id[] = { |
| 327 |
{"rt5682"}, |
| 328 |
{} |
| 329 |
}; |
| 330 |
MODULE_DEVICE_TABLE(i2c, rt5682_i2c_id); |
| 331 |
|
| 332 |
static struct i2c_driver rt5682_i2c_driver = { |
| 333 |
.driver = { |
| 334 |
.name = "rt5682", |
| 335 |
.of_match_table = rt5682_of_match, |
| 336 |
.acpi_match_table = rt5682_acpi_match, |
| 337 |
.probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 338 |
}, |
| 339 |
.probe = rt5682_i2c_probe, |
| 340 |
.remove = rt5682_i2c_remove, |
| 341 |
.shutdown = rt5682_i2c_shutdown, |
| 342 |
.id_table = rt5682_i2c_id, |
| 343 |
}; |
| 344 |
module_i2c_driver(rt5682_i2c_driver); |
| 345 |
|
| 346 |
MODULE_DESCRIPTION("ASoC RT5682 driver"); |
| 347 |
MODULE_AUTHOR("Bard Liao <[email protected]>"); |
| 348 |
MODULE_LICENSE("GPL v2"); |