| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
// |
| 3 |
// rt711.c -- rt711 ALSA SoC audio driver |
| 4 |
// |
| 5 |
// Copyright(c) 2019 Realtek Semiconductor Corp. |
| 6 |
// |
| 7 |
// |
| 8 |
|
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/moduleparam.h> |
| 11 |
#include <linux/kernel.h> |
| 12 |
#include <linux/init.h> |
| 13 |
#include <linux/delay.h> |
| 14 |
#include <linux/pm_runtime.h> |
| 15 |
#include <linux/pm.h> |
| 16 |
#include <linux/soundwire/sdw.h> |
| 17 |
#include <linux/regmap.h> |
| 18 |
#include <linux/slab.h> |
| 19 |
#include <sound/core.h> |
| 20 |
#include <sound/pcm.h> |
| 21 |
#include <sound/pcm_params.h> |
| 22 |
#include <sound/sdw.h> |
| 23 |
#include <sound/soc.h> |
| 24 |
#include <sound/soc-dapm.h> |
| 25 |
#include <sound/initval.h> |
| 26 |
#include <sound/tlv.h> |
| 27 |
#include <sound/hda_verbs.h> |
| 28 |
#include <sound/jack.h> |
| 29 |
|
| 30 |
#include "rt711.h" |
| 31 |
|
| 32 |
static int rt711_index_write(struct regmap *regmap, |
| 33 |
unsigned int nid, unsigned int reg, unsigned int value) |
| 34 |
{ |
| 35 |
int ret; |
| 36 |
unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg; |
| 37 |
|
| 38 |
ret = regmap_write(regmap, addr, value); |
| 39 |
if (ret < 0) |
| 40 |
pr_err("%s: Failed to set private value: %06x <= %04x ret=%d\n", |
| 41 |
__func__, addr, value, ret); |
| 42 |
|
| 43 |
return ret; |
| 44 |
} |
| 45 |
|
| 46 |
static int rt711_index_read(struct regmap *regmap, |
| 47 |
unsigned int nid, unsigned int reg, unsigned int *value) |
| 48 |
{ |
| 49 |
int ret; |
| 50 |
unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg; |
| 51 |
|
| 52 |
*value = 0; |
| 53 |
ret = regmap_read(regmap, addr, value); |
| 54 |
if (ret < 0) |
| 55 |
pr_err("%s: Failed to get private value: %06x => %04x ret=%d\n", |
| 56 |
__func__, addr, *value, ret); |
| 57 |
|
| 58 |
return ret; |
| 59 |
} |
| 60 |
|
| 61 |
static int rt711_index_update_bits(struct regmap *regmap, unsigned int nid, |
| 62 |
unsigned int reg, unsigned int mask, unsigned int val) |
| 63 |
{ |
| 64 |
unsigned int tmp, orig; |
| 65 |
int ret; |
| 66 |
|
| 67 |
ret = rt711_index_read(regmap, nid, reg, &orig); |
| 68 |
if (ret < 0) |
| 69 |
return ret; |
| 70 |
|
| 71 |
tmp = orig & ~mask; |
| 72 |
tmp |= val & mask; |
| 73 |
|
| 74 |
return rt711_index_write(regmap, nid, reg, tmp); |
| 75 |
} |
| 76 |
|
| 77 |
static void rt711_reset(struct regmap *regmap) |
| 78 |
{ |
| 79 |
regmap_write(regmap, RT711_FUNC_RESET, 0); |
| 80 |
rt711_index_update_bits(regmap, RT711_VENDOR_REG, |
| 81 |
RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET, |
| 82 |
RT711_HIDDEN_REG_SW_RESET); |
| 83 |
} |
| 84 |
|
| 85 |
static int rt711_calibration(struct rt711_priv *rt711) |
| 86 |
{ |
| 87 |
unsigned int val, loop = 0; |
| 88 |
struct device *dev; |
| 89 |
struct regmap *regmap = rt711->regmap; |
| 90 |
int ret = 0; |
| 91 |
|
| 92 |
mutex_lock(&rt711->calibrate_mutex); |
| 93 |
regmap_write(rt711->regmap, |
| 94 |
RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 95 |
|
| 96 |
dev = regmap_get_device(regmap); |
| 97 |
|
| 98 |
/* Calibration manual mode */ |
| 99 |
rt711_index_update_bits(regmap, RT711_VENDOR_REG, RT711_FSM_CTL, |
| 100 |
0xf, 0x0); |
| 101 |
|
| 102 |
/* trigger */ |
| 103 |
rt711_index_update_bits(regmap, RT711_VENDOR_CALI, |
| 104 |
RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER, |
| 105 |
RT711_DAC_DC_CALI_TRIGGER); |
| 106 |
|
| 107 |
/* wait for calibration process */ |
| 108 |
rt711_index_read(regmap, RT711_VENDOR_CALI, |
| 109 |
RT711_DAC_DC_CALI_CTL1, &val); |
| 110 |
|
| 111 |
while (val & RT711_DAC_DC_CALI_TRIGGER) { |
| 112 |
if (loop >= 500) { |
| 113 |
pr_err("%s, calibration time-out!\n", |
| 114 |
__func__); |
| 115 |
ret = -ETIMEDOUT; |
| 116 |
break; |
| 117 |
} |
| 118 |
loop++; |
| 119 |
|
| 120 |
usleep_range(10000, 11000); |
| 121 |
rt711_index_read(regmap, RT711_VENDOR_CALI, |
| 122 |
RT711_DAC_DC_CALI_CTL1, &val); |
| 123 |
} |
| 124 |
|
| 125 |
/* depop mode */ |
| 126 |
rt711_index_update_bits(regmap, RT711_VENDOR_REG, |
| 127 |
RT711_FSM_CTL, 0xf, RT711_DEPOP_CTL); |
| 128 |
|
| 129 |
regmap_write(rt711->regmap, |
| 130 |
RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 131 |
mutex_unlock(&rt711->calibrate_mutex); |
| 132 |
|
| 133 |
dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret); |
| 134 |
return ret; |
| 135 |
} |
| 136 |
|
| 137 |
static unsigned int rt711_button_detect(struct rt711_priv *rt711) |
| 138 |
{ |
| 139 |
unsigned int btn_type = 0, val80, val81; |
| 140 |
int ret; |
| 141 |
|
| 142 |
ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, |
| 143 |
RT711_IRQ_FLAG_TABLE1, &val80); |
| 144 |
if (ret < 0) |
| 145 |
goto read_error; |
| 146 |
ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, |
| 147 |
RT711_IRQ_FLAG_TABLE2, &val81); |
| 148 |
if (ret < 0) |
| 149 |
goto read_error; |
| 150 |
|
| 151 |
val80 &= 0x0381; |
| 152 |
val81 &= 0xff00; |
| 153 |
|
| 154 |
switch (val80) { |
| 155 |
case 0x0200: |
| 156 |
case 0x0100: |
| 157 |
case 0x0080: |
| 158 |
btn_type |= SND_JACK_BTN_0; |
| 159 |
break; |
| 160 |
case 0x0001: |
| 161 |
btn_type |= SND_JACK_BTN_3; |
| 162 |
break; |
| 163 |
} |
| 164 |
switch (val81) { |
| 165 |
case 0x8000: |
| 166 |
case 0x4000: |
| 167 |
case 0x2000: |
| 168 |
btn_type |= SND_JACK_BTN_1; |
| 169 |
break; |
| 170 |
case 0x1000: |
| 171 |
case 0x0800: |
| 172 |
case 0x0400: |
| 173 |
btn_type |= SND_JACK_BTN_2; |
| 174 |
break; |
| 175 |
case 0x0200: |
| 176 |
case 0x0100: |
| 177 |
btn_type |= SND_JACK_BTN_3; |
| 178 |
break; |
| 179 |
} |
| 180 |
read_error: |
| 181 |
return btn_type; |
| 182 |
} |
| 183 |
|
| 184 |
static int rt711_headset_detect(struct rt711_priv *rt711) |
| 185 |
{ |
| 186 |
unsigned int buf, loop = 0; |
| 187 |
int ret; |
| 188 |
unsigned int jack_status = 0, reg; |
| 189 |
|
| 190 |
ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, |
| 191 |
RT711_COMBO_JACK_AUTO_CTL2, &buf); |
| 192 |
if (ret < 0) |
| 193 |
goto io_error; |
| 194 |
|
| 195 |
while (loop < 500 && |
| 196 |
(buf & RT711_COMBOJACK_AUTO_DET_STATUS) == 0) { |
| 197 |
loop++; |
| 198 |
|
| 199 |
usleep_range(9000, 10000); |
| 200 |
ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, |
| 201 |
RT711_COMBO_JACK_AUTO_CTL2, &buf); |
| 202 |
if (ret < 0) |
| 203 |
goto io_error; |
| 204 |
|
| 205 |
reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; |
| 206 |
ret = regmap_read(rt711->regmap, reg, &jack_status); |
| 207 |
if (ret < 0) |
| 208 |
goto io_error; |
| 209 |
if ((jack_status & (1 << 31)) == 0) |
| 210 |
goto remove_error; |
| 211 |
} |
| 212 |
|
| 213 |
if (loop >= 500) |
| 214 |
goto to_error; |
| 215 |
|
| 216 |
if (buf & RT711_COMBOJACK_AUTO_DET_TRS) |
| 217 |
rt711->jack_type = SND_JACK_HEADPHONE; |
| 218 |
else if ((buf & RT711_COMBOJACK_AUTO_DET_CTIA) || |
| 219 |
(buf & RT711_COMBOJACK_AUTO_DET_OMTP)) |
| 220 |
rt711->jack_type = SND_JACK_HEADSET; |
| 221 |
|
| 222 |
return 0; |
| 223 |
|
| 224 |
to_error: |
| 225 |
ret = -ETIMEDOUT; |
| 226 |
pr_err_ratelimited("Time-out error in %s\n", __func__); |
| 227 |
return ret; |
| 228 |
io_error: |
| 229 |
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); |
| 230 |
return ret; |
| 231 |
remove_error: |
| 232 |
pr_err_ratelimited("Jack removal in %s\n", __func__); |
| 233 |
return -ENODEV; |
| 234 |
} |
| 235 |
|
| 236 |
static void rt711_jack_detect_handler(struct work_struct *work) |
| 237 |
{ |
| 238 |
struct rt711_priv *rt711 = |
| 239 |
container_of(work, struct rt711_priv, jack_detect_work.work); |
| 240 |
int btn_type = 0, ret; |
| 241 |
unsigned int jack_status = 0, reg; |
| 242 |
|
| 243 |
if (!rt711->hs_jack) |
| 244 |
return; |
| 245 |
|
| 246 |
if (!snd_soc_card_is_instantiated(rt711->component->card)) |
| 247 |
return; |
| 248 |
|
| 249 |
if (pm_runtime_status_suspended(rt711->slave->dev.parent)) { |
| 250 |
dev_dbg(&rt711->slave->dev, |
| 251 |
"%s: parent device is pm_runtime_status_suspended, skipping jack detection\n", |
| 252 |
__func__); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; |
| 257 |
ret = regmap_read(rt711->regmap, reg, &jack_status); |
| 258 |
if (ret < 0) |
| 259 |
goto io_error; |
| 260 |
|
| 261 |
/* pin attached */ |
| 262 |
if (jack_status & (1 << 31)) { |
| 263 |
/* jack in */ |
| 264 |
if (rt711->jack_type == 0) { |
| 265 |
ret = rt711_headset_detect(rt711); |
| 266 |
if (ret < 0) |
| 267 |
return; |
| 268 |
if (rt711->jack_type == SND_JACK_HEADSET) |
| 269 |
btn_type = rt711_button_detect(rt711); |
| 270 |
} else if (rt711->jack_type == SND_JACK_HEADSET) { |
| 271 |
/* jack is already in, report button event */ |
| 272 |
btn_type = rt711_button_detect(rt711); |
| 273 |
} |
| 274 |
} else { |
| 275 |
/* jack out */ |
| 276 |
rt711->jack_type = 0; |
| 277 |
} |
| 278 |
|
| 279 |
dev_dbg(&rt711->slave->dev, |
| 280 |
"in %s, jack_type=0x%x\n", __func__, rt711->jack_type); |
| 281 |
dev_dbg(&rt711->slave->dev, |
| 282 |
"in %s, btn_type=0x%x\n", __func__, btn_type); |
| 283 |
|
| 284 |
snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, |
| 285 |
SND_JACK_HEADSET | |
| 286 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 287 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 288 |
|
| 289 |
if (btn_type) { |
| 290 |
/* button released */ |
| 291 |
snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, |
| 292 |
SND_JACK_HEADSET | |
| 293 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 294 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 295 |
|
| 296 |
mod_delayed_work(system_power_efficient_wq, |
| 297 |
&rt711->jack_btn_check_work, msecs_to_jiffies(200)); |
| 298 |
} |
| 299 |
|
| 300 |
return; |
| 301 |
|
| 302 |
io_error: |
| 303 |
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); |
| 304 |
} |
| 305 |
|
| 306 |
static void rt711_btn_check_handler(struct work_struct *work) |
| 307 |
{ |
| 308 |
struct rt711_priv *rt711 = container_of(work, struct rt711_priv, |
| 309 |
jack_btn_check_work.work); |
| 310 |
int btn_type = 0, ret; |
| 311 |
unsigned int jack_status = 0, reg; |
| 312 |
|
| 313 |
reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; |
| 314 |
ret = regmap_read(rt711->regmap, reg, &jack_status); |
| 315 |
if (ret < 0) |
| 316 |
goto io_error; |
| 317 |
|
| 318 |
/* pin attached */ |
| 319 |
if (jack_status & (1 << 31)) { |
| 320 |
if (rt711->jack_type == SND_JACK_HEADSET) { |
| 321 |
/* jack is already in, report button event */ |
| 322 |
btn_type = rt711_button_detect(rt711); |
| 323 |
} |
| 324 |
} else { |
| 325 |
rt711->jack_type = 0; |
| 326 |
} |
| 327 |
|
| 328 |
/* cbj comparator */ |
| 329 |
ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, |
| 330 |
RT711_COMBO_JACK_AUTO_CTL2, ®); |
| 331 |
if (ret < 0) |
| 332 |
goto io_error; |
| 333 |
|
| 334 |
if ((reg & 0xf0) == 0xf0) |
| 335 |
btn_type = 0; |
| 336 |
|
| 337 |
dev_dbg(&rt711->slave->dev, |
| 338 |
"%s, btn_type=0x%x\n", __func__, btn_type); |
| 339 |
snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, |
| 340 |
SND_JACK_HEADSET | |
| 341 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 342 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 343 |
|
| 344 |
if (btn_type) { |
| 345 |
/* button released */ |
| 346 |
snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, |
| 347 |
SND_JACK_HEADSET | |
| 348 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 349 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 350 |
|
| 351 |
mod_delayed_work(system_power_efficient_wq, |
| 352 |
&rt711->jack_btn_check_work, msecs_to_jiffies(200)); |
| 353 |
} |
| 354 |
|
| 355 |
return; |
| 356 |
|
| 357 |
io_error: |
| 358 |
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); |
| 359 |
} |
| 360 |
|
| 361 |
static void rt711_jack_init(struct rt711_priv *rt711) |
| 362 |
{ |
| 363 |
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(rt711->component); |
| 364 |
|
| 365 |
mutex_lock(&rt711->calibrate_mutex); |
| 366 |
/* power on */ |
| 367 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 368 |
regmap_write(rt711->regmap, |
| 369 |
RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 370 |
|
| 371 |
if (rt711->hs_jack) { |
| 372 |
/* unsolicited response & IRQ control */ |
| 373 |
regmap_write(rt711->regmap, |
| 374 |
RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x82); |
| 375 |
regmap_write(rt711->regmap, |
| 376 |
RT711_SET_HP_UNSOLICITED_ENABLE, 0x81); |
| 377 |
regmap_write(rt711->regmap, |
| 378 |
RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x83); |
| 379 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 380 |
0x10, 0x2420); |
| 381 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 382 |
0x19, 0x2e11); |
| 383 |
|
| 384 |
switch (rt711->jd_src) { |
| 385 |
case RT711_JD1: |
| 386 |
/* default settings was already for JD1 */ |
| 387 |
break; |
| 388 |
case RT711_JD2: |
| 389 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 390 |
RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP | |
| 391 |
RT711_HP_JD_SEL_JD2, |
| 392 |
RT711_JD2_2PORT_200K_DECODE_HP | |
| 393 |
RT711_HP_JD_SEL_JD2); |
| 394 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 395 |
RT711_CC_DET1, |
| 396 |
RT711_HP_JD_FINAL_RESULT_CTL_JD12, |
| 397 |
RT711_HP_JD_FINAL_RESULT_CTL_JD12); |
| 398 |
break; |
| 399 |
case RT711_JD2_100K: |
| 400 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 401 |
RT711_JD_CTL2, RT711_JD2_2PORT_100K_DECODE | RT711_JD2_1PORT_TYPE_DECODE | |
| 402 |
RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_TYPE_100K_DECODE, |
| 403 |
RT711_JD2_2PORT_100K_DECODE_HP | RT711_JD2_1PORT_JD_HP | |
| 404 |
RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_JD_RESERVED); |
| 405 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 406 |
RT711_CC_DET1, |
| 407 |
RT711_HP_JD_FINAL_RESULT_CTL_JD12, |
| 408 |
RT711_HP_JD_FINAL_RESULT_CTL_JD12); |
| 409 |
break; |
| 410 |
case RT711_JD2_1P8V_1PORT: |
| 411 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 412 |
RT711_JD_CTL1, RT711_JD2_DIGITAL_JD_MODE_SEL, |
| 413 |
RT711_JD2_1_JD_MODE); |
| 414 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 415 |
RT711_JD_CTL2, RT711_JD2_1PORT_TYPE_DECODE | |
| 416 |
RT711_HP_JD_SEL_JD2, |
| 417 |
RT711_JD2_1PORT_JD_HP | |
| 418 |
RT711_HP_JD_SEL_JD2); |
| 419 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 420 |
RT711_JD_CTL4, RT711_JD2_PAD_PULL_UP_MASK | |
| 421 |
RT711_JD2_MODE_SEL_MASK, |
| 422 |
RT711_JD2_PAD_PULL_UP | |
| 423 |
RT711_JD2_MODE2_1P8V_1PORT); |
| 424 |
rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, |
| 425 |
RT711_CC_DET1, |
| 426 |
RT711_HP_JD_FINAL_RESULT_CTL_JD12, |
| 427 |
RT711_HP_JD_FINAL_RESULT_CTL_JD12); |
| 428 |
break; |
| 429 |
default: |
| 430 |
dev_warn(rt711->component->dev, "%s: Wrong JD source\n", __func__); |
| 431 |
break; |
| 432 |
} |
| 433 |
|
| 434 |
dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__); |
| 435 |
|
| 436 |
mod_delayed_work(system_power_efficient_wq, |
| 437 |
&rt711->jack_detect_work, msecs_to_jiffies(250)); |
| 438 |
} else { |
| 439 |
regmap_write(rt711->regmap, |
| 440 |
RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00); |
| 441 |
regmap_write(rt711->regmap, |
| 442 |
RT711_SET_HP_UNSOLICITED_ENABLE, 0x00); |
| 443 |
regmap_write(rt711->regmap, |
| 444 |
RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00); |
| 445 |
|
| 446 |
dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__); |
| 447 |
} |
| 448 |
|
| 449 |
/* power off */ |
| 450 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 451 |
regmap_write(rt711->regmap, |
| 452 |
RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 453 |
mutex_unlock(&rt711->calibrate_mutex); |
| 454 |
} |
| 455 |
|
| 456 |
static int rt711_set_jack_detect(struct snd_soc_component *component, |
| 457 |
struct snd_soc_jack *hs_jack, void *data) |
| 458 |
{ |
| 459 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 460 |
int ret; |
| 461 |
|
| 462 |
rt711->hs_jack = hs_jack; |
| 463 |
|
| 464 |
/* we can only resume if the device was initialized at least once */ |
| 465 |
if (!rt711->first_hw_init) |
| 466 |
return 0; |
| 467 |
|
| 468 |
ret = pm_runtime_resume_and_get(component->dev); |
| 469 |
if (ret < 0) { |
| 470 |
if (ret != -EACCES) { |
| 471 |
dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret); |
| 472 |
return ret; |
| 473 |
} |
| 474 |
|
| 475 |
/* pm_runtime not enabled yet */ |
| 476 |
dev_dbg(component->dev, "%s: skipping jack init for now\n", __func__); |
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
rt711_jack_init(rt711); |
| 481 |
|
| 482 |
pm_runtime_put_autosuspend(component->dev); |
| 483 |
|
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h, |
| 488 |
unsigned int addr_l, unsigned int val_h, |
| 489 |
unsigned int *r_val, unsigned int *l_val) |
| 490 |
{ |
| 491 |
/* R Channel */ |
| 492 |
*r_val = (val_h << 8); |
| 493 |
regmap_read(rt711->regmap, addr_l, r_val); |
| 494 |
|
| 495 |
/* L Channel */ |
| 496 |
val_h |= 0x20; |
| 497 |
*l_val = (val_h << 8); |
| 498 |
regmap_read(rt711->regmap, addr_h, l_val); |
| 499 |
} |
| 500 |
|
| 501 |
/* For Verb-Set Amplifier Gain (Verb ID = 3h) */ |
| 502 |
static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol, |
| 503 |
struct snd_ctl_elem_value *ucontrol) |
| 504 |
{ |
| 505 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 506 |
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); |
| 507 |
struct soc_mixer_control *mc = |
| 508 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 509 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 510 |
unsigned int addr_h, addr_l, val_h, val_ll, val_lr; |
| 511 |
unsigned int read_ll, read_rl; |
| 512 |
int i; |
| 513 |
|
| 514 |
mutex_lock(&rt711->calibrate_mutex); |
| 515 |
|
| 516 |
/* Can't use update bit function, so read the original value first */ |
| 517 |
addr_h = mc->reg; |
| 518 |
addr_l = mc->rreg; |
| 519 |
if (mc->shift == RT711_DIR_OUT_SFT) /* output */ |
| 520 |
val_h = 0x80; |
| 521 |
else /* input */ |
| 522 |
val_h = 0x0; |
| 523 |
|
| 524 |
rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll); |
| 525 |
|
| 526 |
/* L Channel */ |
| 527 |
if (mc->invert) { |
| 528 |
/* for mute/unmute */ |
| 529 |
val_ll = (mc->max - ucontrol->value.integer.value[0]) |
| 530 |
<< RT711_MUTE_SFT; |
| 531 |
/* keep gain */ |
| 532 |
read_ll = read_ll & 0x7f; |
| 533 |
val_ll |= read_ll; |
| 534 |
} else { |
| 535 |
/* for gain */ |
| 536 |
val_ll = ((ucontrol->value.integer.value[0]) & 0x7f); |
| 537 |
if (val_ll > mc->max) |
| 538 |
val_ll = mc->max; |
| 539 |
/* keep mute status */ |
| 540 |
read_ll = read_ll & (1 << RT711_MUTE_SFT); |
| 541 |
val_ll |= read_ll; |
| 542 |
} |
| 543 |
|
| 544 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 545 |
regmap_write(rt711->regmap, |
| 546 |
RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 547 |
|
| 548 |
/* R Channel */ |
| 549 |
if (mc->invert) { |
| 550 |
/* for mute/unmute */ |
| 551 |
val_lr = (mc->max - ucontrol->value.integer.value[1]) |
| 552 |
<< RT711_MUTE_SFT; |
| 553 |
/* keep gain */ |
| 554 |
read_rl = read_rl & 0x7f; |
| 555 |
val_lr |= read_rl; |
| 556 |
} else { |
| 557 |
/* for gain */ |
| 558 |
val_lr = ((ucontrol->value.integer.value[1]) & 0x7f); |
| 559 |
if (val_lr > mc->max) |
| 560 |
val_lr = mc->max; |
| 561 |
/* keep mute status */ |
| 562 |
read_rl = read_rl & (1 << RT711_MUTE_SFT); |
| 563 |
val_lr |= read_rl; |
| 564 |
} |
| 565 |
|
| 566 |
for (i = 0; i < 3; i++) { /* retry 3 times at most */ |
| 567 |
|
| 568 |
if (val_ll == val_lr) { |
| 569 |
/* Set both L/R channels at the same time */ |
| 570 |
val_h = (1 << mc->shift) | (3 << 4); |
| 571 |
regmap_write(rt711->regmap, |
| 572 |
addr_h, (val_h << 8 | val_ll)); |
| 573 |
regmap_write(rt711->regmap, |
| 574 |
addr_l, (val_h << 8 | val_ll)); |
| 575 |
} else { |
| 576 |
/* Lch*/ |
| 577 |
val_h = (1 << mc->shift) | (1 << 5); |
| 578 |
regmap_write(rt711->regmap, |
| 579 |
addr_h, (val_h << 8 | val_ll)); |
| 580 |
|
| 581 |
/* Rch */ |
| 582 |
val_h = (1 << mc->shift) | (1 << 4); |
| 583 |
regmap_write(rt711->regmap, |
| 584 |
addr_l, (val_h << 8 | val_lr)); |
| 585 |
} |
| 586 |
/* check result */ |
| 587 |
if (mc->shift == RT711_DIR_OUT_SFT) /* output */ |
| 588 |
val_h = 0x80; |
| 589 |
else /* input */ |
| 590 |
val_h = 0x0; |
| 591 |
|
| 592 |
rt711_get_gain(rt711, addr_h, addr_l, val_h, |
| 593 |
&read_rl, &read_ll); |
| 594 |
if (read_rl == val_lr && read_ll == val_ll) |
| 595 |
break; |
| 596 |
} |
| 597 |
|
| 598 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 599 |
regmap_write(rt711->regmap, |
| 600 |
RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 601 |
|
| 602 |
mutex_unlock(&rt711->calibrate_mutex); |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol, |
| 607 |
struct snd_ctl_elem_value *ucontrol) |
| 608 |
{ |
| 609 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 610 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 611 |
struct soc_mixer_control *mc = |
| 612 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 613 |
unsigned int addr_h, addr_l, val_h; |
| 614 |
unsigned int read_ll, read_rl; |
| 615 |
|
| 616 |
/* switch to get command */ |
| 617 |
addr_h = mc->reg; |
| 618 |
addr_l = mc->rreg; |
| 619 |
if (mc->shift == RT711_DIR_OUT_SFT) /* output */ |
| 620 |
val_h = 0x80; |
| 621 |
else /* input */ |
| 622 |
val_h = 0x0; |
| 623 |
|
| 624 |
rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll); |
| 625 |
|
| 626 |
if (mc->invert) { |
| 627 |
/* mute/unmute for switch controls */ |
| 628 |
read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT); |
| 629 |
read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT); |
| 630 |
} else { |
| 631 |
/* for gain volume controls */ |
| 632 |
read_ll = read_ll & 0x7f; |
| 633 |
read_rl = read_rl & 0x7f; |
| 634 |
} |
| 635 |
ucontrol->value.integer.value[0] = read_ll; |
| 636 |
ucontrol->value.integer.value[1] = read_rl; |
| 637 |
|
| 638 |
return 0; |
| 639 |
} |
| 640 |
|
| 641 |
static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); |
| 642 |
static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); |
| 643 |
static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); |
| 644 |
|
| 645 |
static const struct snd_kcontrol_new rt711_snd_controls[] = { |
| 646 |
SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume", |
| 647 |
RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L, |
| 648 |
RT711_DIR_OUT_SFT, 0x57, 0, |
| 649 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv), |
| 650 |
SOC_DOUBLE_R_EXT("ADC 08 Capture Switch", |
| 651 |
RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L, |
| 652 |
RT711_DIR_IN_SFT, 1, 1, |
| 653 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put), |
| 654 |
SOC_DOUBLE_R_EXT("ADC 09 Capture Switch", |
| 655 |
RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L, |
| 656 |
RT711_DIR_IN_SFT, 1, 1, |
| 657 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put), |
| 658 |
SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume", |
| 659 |
RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L, |
| 660 |
RT711_DIR_IN_SFT, 0x3f, 0, |
| 661 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv), |
| 662 |
SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume", |
| 663 |
RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L, |
| 664 |
RT711_DIR_IN_SFT, 0x3f, 0, |
| 665 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv), |
| 666 |
SOC_DOUBLE_R_EXT_TLV("AMIC Volume", |
| 667 |
RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L, |
| 668 |
RT711_DIR_IN_SFT, 3, 0, |
| 669 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), |
| 670 |
SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume", |
| 671 |
RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L, |
| 672 |
RT711_DIR_IN_SFT, 3, 0, |
| 673 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), |
| 674 |
SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume", |
| 675 |
RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L, |
| 676 |
RT711_DIR_IN_SFT, 3, 0, |
| 677 |
rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), |
| 678 |
}; |
| 679 |
|
| 680 |
static int rt711_mux_get(struct snd_kcontrol *kcontrol, |
| 681 |
struct snd_ctl_elem_value *ucontrol) |
| 682 |
{ |
| 683 |
struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol); |
| 684 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 685 |
unsigned int reg, val = 0, nid; |
| 686 |
int ret; |
| 687 |
|
| 688 |
if (strstr(ucontrol->id.name, "ADC 22 Mux")) |
| 689 |
nid = RT711_MIXER_IN1; |
| 690 |
else if (strstr(ucontrol->id.name, "ADC 23 Mux")) |
| 691 |
nid = RT711_MIXER_IN2; |
| 692 |
else |
| 693 |
return -EINVAL; |
| 694 |
|
| 695 |
/* vid = 0xf01 */ |
| 696 |
reg = RT711_VERB_SET_CONNECT_SEL | nid; |
| 697 |
ret = regmap_read(rt711->regmap, reg, &val); |
| 698 |
if (ret < 0) { |
| 699 |
dev_err(component->dev, "%s: sdw read failed: %d\n", |
| 700 |
__func__, ret); |
| 701 |
return ret; |
| 702 |
} |
| 703 |
|
| 704 |
ucontrol->value.enumerated.item[0] = val; |
| 705 |
|
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
static int rt711_mux_put(struct snd_kcontrol *kcontrol, |
| 710 |
struct snd_ctl_elem_value *ucontrol) |
| 711 |
{ |
| 712 |
struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol); |
| 713 |
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol); |
| 714 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 715 |
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 716 |
unsigned int *item = ucontrol->value.enumerated.item; |
| 717 |
unsigned int val, val2 = 0, change, reg, nid; |
| 718 |
int ret; |
| 719 |
|
| 720 |
if (item[0] >= e->items) |
| 721 |
return -EINVAL; |
| 722 |
|
| 723 |
if (strstr(ucontrol->id.name, "ADC 22 Mux")) |
| 724 |
nid = RT711_MIXER_IN1; |
| 725 |
else if (strstr(ucontrol->id.name, "ADC 23 Mux")) |
| 726 |
nid = RT711_MIXER_IN2; |
| 727 |
else |
| 728 |
return -EINVAL; |
| 729 |
|
| 730 |
/* Verb ID = 0x701h */ |
| 731 |
val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; |
| 732 |
|
| 733 |
reg = RT711_VERB_SET_CONNECT_SEL | nid; |
| 734 |
ret = regmap_read(rt711->regmap, reg, &val2); |
| 735 |
if (ret < 0) { |
| 736 |
dev_err(component->dev, "%s: sdw read failed: %d\n", |
| 737 |
__func__, ret); |
| 738 |
return ret; |
| 739 |
} |
| 740 |
|
| 741 |
if (val == val2) |
| 742 |
change = 0; |
| 743 |
else |
| 744 |
change = 1; |
| 745 |
|
| 746 |
if (change) { |
| 747 |
reg = RT711_VERB_SET_CONNECT_SEL | nid; |
| 748 |
regmap_write(rt711->regmap, reg, val); |
| 749 |
} |
| 750 |
|
| 751 |
snd_soc_dapm_mux_update_power(dapm, kcontrol, |
| 752 |
item[0], e, NULL); |
| 753 |
|
| 754 |
return change; |
| 755 |
} |
| 756 |
|
| 757 |
static const char * const adc_mux_text[] = { |
| 758 |
"MIC2", |
| 759 |
"LINE1", |
| 760 |
"LINE2", |
| 761 |
"DMIC", |
| 762 |
}; |
| 763 |
|
| 764 |
static SOC_ENUM_SINGLE_DECL( |
| 765 |
rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); |
| 766 |
|
| 767 |
static SOC_ENUM_SINGLE_DECL( |
| 768 |
rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); |
| 769 |
|
| 770 |
static const struct snd_kcontrol_new rt711_adc22_mux = |
| 771 |
SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum, |
| 772 |
rt711_mux_get, rt711_mux_put); |
| 773 |
|
| 774 |
static const struct snd_kcontrol_new rt711_adc23_mux = |
| 775 |
SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum, |
| 776 |
rt711_mux_get, rt711_mux_put); |
| 777 |
|
| 778 |
static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w, |
| 779 |
struct snd_kcontrol *kcontrol, int event) |
| 780 |
{ |
| 781 |
struct snd_soc_component *component = |
| 782 |
snd_soc_dapm_to_component(w->dapm); |
| 783 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 784 |
unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4); |
| 785 |
unsigned int val_l; |
| 786 |
|
| 787 |
switch (event) { |
| 788 |
case SND_SOC_DAPM_POST_PMU: |
| 789 |
regmap_write(rt711->regmap, |
| 790 |
RT711_SET_STREAMID_DAC2, 0x10); |
| 791 |
|
| 792 |
val_l = 0x00; |
| 793 |
regmap_write(rt711->regmap, |
| 794 |
RT711_SET_GAIN_HP_H, (val_h << 8 | val_l)); |
| 795 |
break; |
| 796 |
case SND_SOC_DAPM_PRE_PMD: |
| 797 |
val_l = (1 << RT711_MUTE_SFT); |
| 798 |
regmap_write(rt711->regmap, |
| 799 |
RT711_SET_GAIN_HP_H, (val_h << 8 | val_l)); |
| 800 |
usleep_range(50000, 55000); |
| 801 |
|
| 802 |
regmap_write(rt711->regmap, |
| 803 |
RT711_SET_STREAMID_DAC2, 0x00); |
| 804 |
break; |
| 805 |
} |
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
static int rt711_adc_09_event(struct snd_soc_dapm_widget *w, |
| 810 |
struct snd_kcontrol *kcontrol, int event) |
| 811 |
{ |
| 812 |
struct snd_soc_component *component = |
| 813 |
snd_soc_dapm_to_component(w->dapm); |
| 814 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 815 |
|
| 816 |
switch (event) { |
| 817 |
case SND_SOC_DAPM_POST_PMU: |
| 818 |
regmap_write(rt711->regmap, |
| 819 |
RT711_SET_STREAMID_ADC1, 0x10); |
| 820 |
break; |
| 821 |
case SND_SOC_DAPM_PRE_PMD: |
| 822 |
regmap_write(rt711->regmap, |
| 823 |
RT711_SET_STREAMID_ADC1, 0x00); |
| 824 |
break; |
| 825 |
} |
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
static int rt711_adc_08_event(struct snd_soc_dapm_widget *w, |
| 830 |
struct snd_kcontrol *kcontrol, int event) |
| 831 |
{ |
| 832 |
struct snd_soc_component *component = |
| 833 |
snd_soc_dapm_to_component(w->dapm); |
| 834 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 835 |
|
| 836 |
switch (event) { |
| 837 |
case SND_SOC_DAPM_POST_PMU: |
| 838 |
regmap_write(rt711->regmap, |
| 839 |
RT711_SET_STREAMID_ADC2, 0x10); |
| 840 |
break; |
| 841 |
case SND_SOC_DAPM_PRE_PMD: |
| 842 |
regmap_write(rt711->regmap, |
| 843 |
RT711_SET_STREAMID_ADC2, 0x00); |
| 844 |
break; |
| 845 |
} |
| 846 |
return 0; |
| 847 |
} |
| 848 |
|
| 849 |
static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = { |
| 850 |
SND_SOC_DAPM_OUTPUT("HP"), |
| 851 |
SND_SOC_DAPM_INPUT("MIC2"), |
| 852 |
SND_SOC_DAPM_INPUT("DMIC1"), |
| 853 |
SND_SOC_DAPM_INPUT("DMIC2"), |
| 854 |
SND_SOC_DAPM_INPUT("LINE1"), |
| 855 |
SND_SOC_DAPM_INPUT("LINE2"), |
| 856 |
|
| 857 |
SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0, |
| 858 |
rt711_dac_surround_event, |
| 859 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 860 |
SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0, |
| 861 |
rt711_adc_09_event, |
| 862 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 863 |
SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0, |
| 864 |
rt711_adc_08_event, |
| 865 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 866 |
SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, |
| 867 |
&rt711_adc22_mux), |
| 868 |
SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, |
| 869 |
&rt711_adc23_mux), |
| 870 |
|
| 871 |
SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 872 |
SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 873 |
SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 874 |
}; |
| 875 |
|
| 876 |
static const struct snd_soc_dapm_route rt711_audio_map[] = { |
| 877 |
{"DAC Surround", NULL, "DP3RX"}, |
| 878 |
{"DP2TX", NULL, "ADC 09"}, |
| 879 |
{"DP4TX", NULL, "ADC 08"}, |
| 880 |
|
| 881 |
{"ADC 09", NULL, "ADC 22 Mux"}, |
| 882 |
{"ADC 08", NULL, "ADC 23 Mux"}, |
| 883 |
{"ADC 22 Mux", "DMIC", "DMIC1"}, |
| 884 |
{"ADC 22 Mux", "LINE1", "LINE1"}, |
| 885 |
{"ADC 22 Mux", "LINE2", "LINE2"}, |
| 886 |
{"ADC 22 Mux", "MIC2", "MIC2"}, |
| 887 |
{"ADC 23 Mux", "DMIC", "DMIC2"}, |
| 888 |
{"ADC 23 Mux", "LINE1", "LINE1"}, |
| 889 |
{"ADC 23 Mux", "LINE2", "LINE2"}, |
| 890 |
{"ADC 23 Mux", "MIC2", "MIC2"}, |
| 891 |
|
| 892 |
{"HP", NULL, "DAC Surround"}, |
| 893 |
}; |
| 894 |
|
| 895 |
static int rt711_set_bias_level(struct snd_soc_component *component, |
| 896 |
enum snd_soc_bias_level level) |
| 897 |
{ |
| 898 |
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); |
| 899 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 900 |
|
| 901 |
switch (level) { |
| 902 |
case SND_SOC_BIAS_PREPARE: |
| 903 |
if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_STANDBY) { |
| 904 |
regmap_write(rt711->regmap, |
| 905 |
RT711_SET_AUDIO_POWER_STATE, |
| 906 |
AC_PWRST_D0); |
| 907 |
} |
| 908 |
break; |
| 909 |
|
| 910 |
case SND_SOC_BIAS_STANDBY: |
| 911 |
mutex_lock(&rt711->calibrate_mutex); |
| 912 |
regmap_write(rt711->regmap, |
| 913 |
RT711_SET_AUDIO_POWER_STATE, |
| 914 |
AC_PWRST_D3); |
| 915 |
mutex_unlock(&rt711->calibrate_mutex); |
| 916 |
break; |
| 917 |
|
| 918 |
default: |
| 919 |
break; |
| 920 |
} |
| 921 |
|
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev) |
| 926 |
{ |
| 927 |
device_property_read_u32(dev, "realtek,jd-src", |
| 928 |
&rt711->jd_src); |
| 929 |
|
| 930 |
return 0; |
| 931 |
} |
| 932 |
|
| 933 |
static int rt711_probe(struct snd_soc_component *component) |
| 934 |
{ |
| 935 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 936 |
int ret; |
| 937 |
|
| 938 |
rt711_parse_dt(rt711, &rt711->slave->dev); |
| 939 |
rt711->component = component; |
| 940 |
|
| 941 |
if (!rt711->first_hw_init) |
| 942 |
return 0; |
| 943 |
|
| 944 |
ret = pm_runtime_resume(component->dev); |
| 945 |
if (ret < 0 && ret != -EACCES) |
| 946 |
return ret; |
| 947 |
|
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
static const struct snd_soc_component_driver soc_codec_dev_rt711 = { |
| 952 |
.probe = rt711_probe, |
| 953 |
.set_bias_level = rt711_set_bias_level, |
| 954 |
.controls = rt711_snd_controls, |
| 955 |
.num_controls = ARRAY_SIZE(rt711_snd_controls), |
| 956 |
.dapm_widgets = rt711_dapm_widgets, |
| 957 |
.num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets), |
| 958 |
.dapm_routes = rt711_audio_map, |
| 959 |
.num_dapm_routes = ARRAY_SIZE(rt711_audio_map), |
| 960 |
.set_jack = rt711_set_jack_detect, |
| 961 |
.endianness = 1, |
| 962 |
}; |
| 963 |
|
| 964 |
static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, |
| 965 |
int direction) |
| 966 |
{ |
| 967 |
snd_soc_dai_dma_data_set(dai, direction, sdw_stream); |
| 968 |
|
| 969 |
return 0; |
| 970 |
} |
| 971 |
|
| 972 |
static void rt711_shutdown(struct snd_pcm_substream *substream, |
| 973 |
struct snd_soc_dai *dai) |
| 974 |
{ |
| 975 |
snd_soc_dai_set_dma_data(dai, substream, NULL); |
| 976 |
} |
| 977 |
|
| 978 |
static int rt711_pcm_hw_params(struct snd_pcm_substream *substream, |
| 979 |
struct snd_pcm_hw_params *params, |
| 980 |
struct snd_soc_dai *dai) |
| 981 |
{ |
| 982 |
struct snd_soc_component *component = dai->component; |
| 983 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 984 |
struct sdw_stream_config stream_config = {0}; |
| 985 |
struct sdw_port_config port_config = {0}; |
| 986 |
struct sdw_stream_runtime *sdw_stream; |
| 987 |
int retval; |
| 988 |
unsigned int val = 0; |
| 989 |
|
| 990 |
dev_dbg(dai->dev, "%s %s", __func__, dai->name); |
| 991 |
sdw_stream = snd_soc_dai_get_dma_data(dai, substream); |
| 992 |
|
| 993 |
if (!sdw_stream) |
| 994 |
return -EINVAL; |
| 995 |
|
| 996 |
if (!rt711->slave) |
| 997 |
return -EINVAL; |
| 998 |
|
| 999 |
/* SoundWire specific configuration */ |
| 1000 |
snd_sdw_params_to_config(substream, params, &stream_config, &port_config); |
| 1001 |
|
| 1002 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1003 |
port_config.num = 3; |
| 1004 |
} else { |
| 1005 |
if (dai->id == RT711_AIF1) |
| 1006 |
port_config.num = 4; |
| 1007 |
else if (dai->id == RT711_AIF2) |
| 1008 |
port_config.num = 2; |
| 1009 |
else |
| 1010 |
return -EINVAL; |
| 1011 |
} |
| 1012 |
|
| 1013 |
retval = sdw_stream_add_slave(rt711->slave, &stream_config, |
| 1014 |
&port_config, 1, sdw_stream); |
| 1015 |
if (retval) { |
| 1016 |
dev_err(dai->dev, "%s: Unable to configure port\n", __func__); |
| 1017 |
return retval; |
| 1018 |
} |
| 1019 |
|
| 1020 |
if (params_channels(params) <= 16) { |
| 1021 |
/* bit 3:0 Number of Channel */ |
| 1022 |
val |= (params_channels(params) - 1); |
| 1023 |
} else { |
| 1024 |
dev_err(component->dev, "%s: Unsupported channels %d\n", |
| 1025 |
__func__, params_channels(params)); |
| 1026 |
return -EINVAL; |
| 1027 |
} |
| 1028 |
|
| 1029 |
switch (params_width(params)) { |
| 1030 |
/* bit 6:4 Bits per Sample */ |
| 1031 |
case 8: |
| 1032 |
break; |
| 1033 |
case 16: |
| 1034 |
val |= (0x1 << 4); |
| 1035 |
break; |
| 1036 |
case 20: |
| 1037 |
val |= (0x2 << 4); |
| 1038 |
break; |
| 1039 |
case 24: |
| 1040 |
val |= (0x3 << 4); |
| 1041 |
break; |
| 1042 |
case 32: |
| 1043 |
val |= (0x4 << 4); |
| 1044 |
break; |
| 1045 |
default: |
| 1046 |
return -EINVAL; |
| 1047 |
} |
| 1048 |
|
| 1049 |
/* 48Khz */ |
| 1050 |
regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val); |
| 1051 |
regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val); |
| 1052 |
regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val); |
| 1053 |
|
| 1054 |
return retval; |
| 1055 |
} |
| 1056 |
|
| 1057 |
static int rt711_pcm_hw_free(struct snd_pcm_substream *substream, |
| 1058 |
struct snd_soc_dai *dai) |
| 1059 |
{ |
| 1060 |
struct snd_soc_component *component = dai->component; |
| 1061 |
struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); |
| 1062 |
struct sdw_stream_runtime *sdw_stream = |
| 1063 |
snd_soc_dai_get_dma_data(dai, substream); |
| 1064 |
|
| 1065 |
if (!rt711->slave) |
| 1066 |
return -EINVAL; |
| 1067 |
|
| 1068 |
sdw_stream_remove_slave(rt711->slave, sdw_stream); |
| 1069 |
return 0; |
| 1070 |
} |
| 1071 |
|
| 1072 |
#define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) |
| 1073 |
#define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 1074 |
SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8) |
| 1075 |
|
| 1076 |
static const struct snd_soc_dai_ops rt711_ops = { |
| 1077 |
.hw_params = rt711_pcm_hw_params, |
| 1078 |
.hw_free = rt711_pcm_hw_free, |
| 1079 |
.set_stream = rt711_set_sdw_stream, |
| 1080 |
.shutdown = rt711_shutdown, |
| 1081 |
}; |
| 1082 |
|
| 1083 |
static struct snd_soc_dai_driver rt711_dai[] = { |
| 1084 |
{ |
| 1085 |
.name = "rt711-aif1", |
| 1086 |
.id = RT711_AIF1, |
| 1087 |
.playback = { |
| 1088 |
.stream_name = "DP3 Playback", |
| 1089 |
.channels_min = 1, |
| 1090 |
.channels_max = 2, |
| 1091 |
.rates = RT711_STEREO_RATES, |
| 1092 |
.formats = RT711_FORMATS, |
| 1093 |
}, |
| 1094 |
.capture = { |
| 1095 |
.stream_name = "DP4 Capture", |
| 1096 |
.channels_min = 1, |
| 1097 |
.channels_max = 2, |
| 1098 |
.rates = RT711_STEREO_RATES, |
| 1099 |
.formats = RT711_FORMATS, |
| 1100 |
}, |
| 1101 |
.ops = &rt711_ops, |
| 1102 |
}, |
| 1103 |
{ |
| 1104 |
.name = "rt711-aif2", |
| 1105 |
.id = RT711_AIF2, |
| 1106 |
.capture = { |
| 1107 |
.stream_name = "DP2 Capture", |
| 1108 |
.channels_min = 1, |
| 1109 |
.channels_max = 2, |
| 1110 |
.rates = RT711_STEREO_RATES, |
| 1111 |
.formats = RT711_FORMATS, |
| 1112 |
}, |
| 1113 |
.ops = &rt711_ops, |
| 1114 |
} |
| 1115 |
}; |
| 1116 |
|
| 1117 |
/* Bus clock frequency */ |
| 1118 |
#define RT711_CLK_FREQ_9600000HZ 9600000 |
| 1119 |
#define RT711_CLK_FREQ_12000000HZ 12000000 |
| 1120 |
#define RT711_CLK_FREQ_6000000HZ 6000000 |
| 1121 |
#define RT711_CLK_FREQ_4800000HZ 4800000 |
| 1122 |
#define RT711_CLK_FREQ_2400000HZ 2400000 |
| 1123 |
#define RT711_CLK_FREQ_12288000HZ 12288000 |
| 1124 |
|
| 1125 |
int rt711_clock_config(struct device *dev) |
| 1126 |
{ |
| 1127 |
struct rt711_priv *rt711 = dev_get_drvdata(dev); |
| 1128 |
unsigned int clk_freq, value; |
| 1129 |
|
| 1130 |
clk_freq = (rt711->params.curr_dr_freq >> 1); |
| 1131 |
|
| 1132 |
switch (clk_freq) { |
| 1133 |
case RT711_CLK_FREQ_12000000HZ: |
| 1134 |
value = 0x0; |
| 1135 |
break; |
| 1136 |
case RT711_CLK_FREQ_6000000HZ: |
| 1137 |
value = 0x1; |
| 1138 |
break; |
| 1139 |
case RT711_CLK_FREQ_9600000HZ: |
| 1140 |
value = 0x2; |
| 1141 |
break; |
| 1142 |
case RT711_CLK_FREQ_4800000HZ: |
| 1143 |
value = 0x3; |
| 1144 |
break; |
| 1145 |
case RT711_CLK_FREQ_2400000HZ: |
| 1146 |
value = 0x4; |
| 1147 |
break; |
| 1148 |
case RT711_CLK_FREQ_12288000HZ: |
| 1149 |
value = 0x5; |
| 1150 |
break; |
| 1151 |
default: |
| 1152 |
return -EINVAL; |
| 1153 |
} |
| 1154 |
|
| 1155 |
regmap_write(rt711->regmap, 0xe0, value); |
| 1156 |
regmap_write(rt711->regmap, 0xf0, value); |
| 1157 |
|
| 1158 |
dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq); |
| 1159 |
|
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
static void rt711_calibration_work(struct work_struct *work) |
| 1164 |
{ |
| 1165 |
struct rt711_priv *rt711 = |
| 1166 |
container_of(work, struct rt711_priv, calibration_work); |
| 1167 |
|
| 1168 |
rt711_calibration(rt711); |
| 1169 |
} |
| 1170 |
|
| 1171 |
int rt711_init(struct device *dev, struct regmap *sdw_regmap, |
| 1172 |
struct regmap *regmap, struct sdw_slave *slave) |
| 1173 |
{ |
| 1174 |
struct rt711_priv *rt711; |
| 1175 |
int ret; |
| 1176 |
|
| 1177 |
rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL); |
| 1178 |
if (!rt711) |
| 1179 |
return -ENOMEM; |
| 1180 |
|
| 1181 |
dev_set_drvdata(dev, rt711); |
| 1182 |
rt711->slave = slave; |
| 1183 |
rt711->sdw_regmap = sdw_regmap; |
| 1184 |
rt711->regmap = regmap; |
| 1185 |
|
| 1186 |
regcache_cache_only(rt711->regmap, true); |
| 1187 |
|
| 1188 |
mutex_init(&rt711->calibrate_mutex); |
| 1189 |
mutex_init(&rt711->disable_irq_lock); |
| 1190 |
|
| 1191 |
INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_jack_detect_handler); |
| 1192 |
INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_btn_check_handler); |
| 1193 |
INIT_WORK(&rt711->calibration_work, rt711_calibration_work); |
| 1194 |
|
| 1195 |
/* |
| 1196 |
* Mark hw_init to false |
| 1197 |
* HW init will be performed when device reports present |
| 1198 |
*/ |
| 1199 |
rt711->hw_init = false; |
| 1200 |
rt711->first_hw_init = false; |
| 1201 |
|
| 1202 |
/* JD source uses JD2 in default */ |
| 1203 |
rt711->jd_src = RT711_JD2; |
| 1204 |
|
| 1205 |
ret = devm_snd_soc_register_component(dev, |
| 1206 |
&soc_codec_dev_rt711, |
| 1207 |
rt711_dai, |
| 1208 |
ARRAY_SIZE(rt711_dai)); |
| 1209 |
if (ret < 0) |
| 1210 |
return ret; |
| 1211 |
|
| 1212 |
/* set autosuspend parameters */ |
| 1213 |
pm_runtime_set_autosuspend_delay(dev, 3000); |
| 1214 |
pm_runtime_use_autosuspend(dev); |
| 1215 |
|
| 1216 |
/* make sure the device does not suspend immediately */ |
| 1217 |
pm_runtime_mark_last_busy(dev); |
| 1218 |
|
| 1219 |
pm_runtime_enable(dev); |
| 1220 |
|
| 1221 |
/* important note: the device is NOT tagged as 'active' and will remain |
| 1222 |
* 'suspended' until the hardware is enumerated/initialized. This is required |
| 1223 |
* to make sure the ASoC framework use of pm_runtime_get_sync() does not silently |
| 1224 |
* fail with -EACCESS because of race conditions between card creation and enumeration |
| 1225 |
*/ |
| 1226 |
|
| 1227 |
dev_dbg(dev, "%s\n", __func__); |
| 1228 |
|
| 1229 |
return ret; |
| 1230 |
} |
| 1231 |
|
| 1232 |
int rt711_io_init(struct device *dev, struct sdw_slave *slave) |
| 1233 |
{ |
| 1234 |
struct rt711_priv *rt711 = dev_get_drvdata(dev); |
| 1235 |
|
| 1236 |
rt711->disable_irq = false; |
| 1237 |
|
| 1238 |
if (rt711->hw_init) |
| 1239 |
return 0; |
| 1240 |
|
| 1241 |
regcache_cache_only(rt711->regmap, false); |
| 1242 |
if (rt711->first_hw_init) |
| 1243 |
regcache_cache_bypass(rt711->regmap, true); |
| 1244 |
|
| 1245 |
/* |
| 1246 |
* PM runtime status is marked as 'active' only when a Slave reports as Attached |
| 1247 |
*/ |
| 1248 |
if (!rt711->first_hw_init) |
| 1249 |
/* update count of parent 'active' children */ |
| 1250 |
pm_runtime_set_active(&slave->dev); |
| 1251 |
|
| 1252 |
pm_runtime_get_noresume(&slave->dev); |
| 1253 |
|
| 1254 |
rt711_reset(rt711->regmap); |
| 1255 |
|
| 1256 |
/* power on */ |
| 1257 |
regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 1258 |
|
| 1259 |
/* Set Pin Widget */ |
| 1260 |
regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25); |
| 1261 |
regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0); |
| 1262 |
regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20); |
| 1263 |
regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20); |
| 1264 |
regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20); |
| 1265 |
regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20); |
| 1266 |
|
| 1267 |
/* Mute HP/ADC1/ADC2 */ |
| 1268 |
regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080); |
| 1269 |
regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080); |
| 1270 |
regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080); |
| 1271 |
regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080); |
| 1272 |
regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080); |
| 1273 |
regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080); |
| 1274 |
|
| 1275 |
/* Set Configuration Default */ |
| 1276 |
regmap_write(rt711->regmap, 0x4f12, 0x91); |
| 1277 |
regmap_write(rt711->regmap, 0x4e12, 0xd6); |
| 1278 |
regmap_write(rt711->regmap, 0x4d12, 0x11); |
| 1279 |
regmap_write(rt711->regmap, 0x4c12, 0x20); |
| 1280 |
regmap_write(rt711->regmap, 0x4f13, 0x91); |
| 1281 |
regmap_write(rt711->regmap, 0x4e13, 0xd6); |
| 1282 |
regmap_write(rt711->regmap, 0x4d13, 0x11); |
| 1283 |
regmap_write(rt711->regmap, 0x4c13, 0x21); |
| 1284 |
regmap_write(rt711->regmap, 0x4c21, 0xf0); |
| 1285 |
regmap_write(rt711->regmap, 0x4d21, 0x11); |
| 1286 |
regmap_write(rt711->regmap, 0x4e21, 0x11); |
| 1287 |
regmap_write(rt711->regmap, 0x4f21, 0x01); |
| 1288 |
|
| 1289 |
/* Data port arrangement */ |
| 1290 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 1291 |
RT711_TX_RX_MUX_CTL, 0x0154); |
| 1292 |
|
| 1293 |
/* Set index */ |
| 1294 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 1295 |
RT711_DIGITAL_MISC_CTRL4, 0x201b); |
| 1296 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 1297 |
RT711_COMBO_JACK_AUTO_CTL1, 0x5089); |
| 1298 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 1299 |
RT711_VREFOUT_CTL, 0x5064); |
| 1300 |
rt711_index_write(rt711->regmap, RT711_VENDOR_REG, |
| 1301 |
RT711_INLINE_CMD_CTL, 0xd249); |
| 1302 |
|
| 1303 |
/* Finish Initial Settings, set power to D3 */ |
| 1304 |
regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 1305 |
|
| 1306 |
if (rt711->first_hw_init) |
| 1307 |
rt711_calibration(rt711); |
| 1308 |
else |
| 1309 |
schedule_work(&rt711->calibration_work); |
| 1310 |
|
| 1311 |
/* |
| 1312 |
* if set_jack callback occurred early than io_init, |
| 1313 |
* we set up the jack detection function now |
| 1314 |
*/ |
| 1315 |
if (rt711->hs_jack) |
| 1316 |
rt711_jack_init(rt711); |
| 1317 |
|
| 1318 |
if (rt711->first_hw_init) { |
| 1319 |
regcache_cache_bypass(rt711->regmap, false); |
| 1320 |
regcache_mark_dirty(rt711->regmap); |
| 1321 |
} else |
| 1322 |
rt711->first_hw_init = true; |
| 1323 |
|
| 1324 |
/* Mark Slave initialization complete */ |
| 1325 |
rt711->hw_init = true; |
| 1326 |
|
| 1327 |
pm_runtime_put_autosuspend(&slave->dev); |
| 1328 |
|
| 1329 |
dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); |
| 1330 |
return 0; |
| 1331 |
} |
| 1332 |
|
| 1333 |
MODULE_DESCRIPTION("ASoC RT711 SDW driver"); |
| 1334 |
MODULE_AUTHOR("Shuming Fan <[email protected]>"); |
| 1335 |
MODULE_LICENSE("GPL"); |