| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
// |
| 3 |
// rt700.c -- rt700 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 "rt700.h" |
| 31 |
|
| 32 |
static int rt700_index_write(struct regmap *regmap, |
| 33 |
unsigned int reg, unsigned int value) |
| 34 |
{ |
| 35 |
int ret; |
| 36 |
unsigned int addr = (RT700_PRIV_INDEX_W_H << 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 rt700_index_read(struct regmap *regmap, |
| 47 |
unsigned int reg, unsigned int *value) |
| 48 |
{ |
| 49 |
int ret; |
| 50 |
unsigned int addr = (RT700_PRIV_INDEX_W_H << 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 unsigned int rt700_button_detect(struct rt700_priv *rt700) |
| 62 |
{ |
| 63 |
unsigned int btn_type = 0, val80, val81; |
| 64 |
int ret; |
| 65 |
|
| 66 |
ret = rt700_index_read(rt700->regmap, RT700_IRQ_FLAG_TABLE1, &val80); |
| 67 |
if (ret < 0) |
| 68 |
goto read_error; |
| 69 |
ret = rt700_index_read(rt700->regmap, RT700_IRQ_FLAG_TABLE2, &val81); |
| 70 |
if (ret < 0) |
| 71 |
goto read_error; |
| 72 |
|
| 73 |
val80 &= 0x0381; |
| 74 |
val81 &= 0xff00; |
| 75 |
|
| 76 |
switch (val80) { |
| 77 |
case 0x0200: |
| 78 |
case 0x0100: |
| 79 |
case 0x0080: |
| 80 |
btn_type |= SND_JACK_BTN_0; |
| 81 |
break; |
| 82 |
case 0x0001: |
| 83 |
btn_type |= SND_JACK_BTN_3; |
| 84 |
break; |
| 85 |
} |
| 86 |
switch (val81) { |
| 87 |
case 0x8000: |
| 88 |
case 0x4000: |
| 89 |
case 0x2000: |
| 90 |
btn_type |= SND_JACK_BTN_1; |
| 91 |
break; |
| 92 |
case 0x1000: |
| 93 |
case 0x0800: |
| 94 |
case 0x0400: |
| 95 |
btn_type |= SND_JACK_BTN_2; |
| 96 |
break; |
| 97 |
case 0x0200: |
| 98 |
case 0x0100: |
| 99 |
btn_type |= SND_JACK_BTN_3; |
| 100 |
break; |
| 101 |
} |
| 102 |
read_error: |
| 103 |
return btn_type; |
| 104 |
} |
| 105 |
|
| 106 |
static int rt700_headset_detect(struct rt700_priv *rt700) |
| 107 |
{ |
| 108 |
unsigned int buf, loop = 0; |
| 109 |
int ret; |
| 110 |
unsigned int jack_status = 0, reg; |
| 111 |
|
| 112 |
ret = rt700_index_read(rt700->regmap, |
| 113 |
RT700_COMBO_JACK_AUTO_CTL2, &buf); |
| 114 |
if (ret < 0) |
| 115 |
goto io_error; |
| 116 |
|
| 117 |
while (loop < 500 && |
| 118 |
(buf & RT700_COMBOJACK_AUTO_DET_STATUS) == 0) { |
| 119 |
loop++; |
| 120 |
|
| 121 |
usleep_range(9000, 10000); |
| 122 |
ret = rt700_index_read(rt700->regmap, |
| 123 |
RT700_COMBO_JACK_AUTO_CTL2, &buf); |
| 124 |
if (ret < 0) |
| 125 |
goto io_error; |
| 126 |
|
| 127 |
reg = RT700_VERB_GET_PIN_SENSE | RT700_HP_OUT; |
| 128 |
ret = regmap_read(rt700->regmap, reg, &jack_status); |
| 129 |
if ((jack_status & (1 << 31)) == 0) |
| 130 |
goto remove_error; |
| 131 |
} |
| 132 |
|
| 133 |
if (loop >= 500) |
| 134 |
goto to_error; |
| 135 |
|
| 136 |
if (buf & RT700_COMBOJACK_AUTO_DET_TRS) |
| 137 |
rt700->jack_type = SND_JACK_HEADPHONE; |
| 138 |
else if ((buf & RT700_COMBOJACK_AUTO_DET_CTIA) || |
| 139 |
(buf & RT700_COMBOJACK_AUTO_DET_OMTP)) |
| 140 |
rt700->jack_type = SND_JACK_HEADSET; |
| 141 |
|
| 142 |
return 0; |
| 143 |
|
| 144 |
to_error: |
| 145 |
ret = -ETIMEDOUT; |
| 146 |
pr_err_ratelimited("Time-out error in %s\n", __func__); |
| 147 |
return ret; |
| 148 |
io_error: |
| 149 |
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); |
| 150 |
return ret; |
| 151 |
remove_error: |
| 152 |
pr_err_ratelimited("Jack removal in %s\n", __func__); |
| 153 |
return -ENODEV; |
| 154 |
} |
| 155 |
|
| 156 |
static void rt700_jack_detect_handler(struct work_struct *work) |
| 157 |
{ |
| 158 |
struct rt700_priv *rt700 = |
| 159 |
container_of(work, struct rt700_priv, jack_detect_work.work); |
| 160 |
int btn_type = 0, ret; |
| 161 |
unsigned int jack_status = 0, reg; |
| 162 |
|
| 163 |
if (!rt700->hs_jack) |
| 164 |
return; |
| 165 |
|
| 166 |
if (!snd_soc_card_is_instantiated(rt700->component->card)) |
| 167 |
return; |
| 168 |
|
| 169 |
reg = RT700_VERB_GET_PIN_SENSE | RT700_HP_OUT; |
| 170 |
ret = regmap_read(rt700->regmap, reg, &jack_status); |
| 171 |
if (ret < 0) |
| 172 |
goto io_error; |
| 173 |
|
| 174 |
/* pin attached */ |
| 175 |
if (jack_status & (1 << 31)) { |
| 176 |
/* jack in */ |
| 177 |
if (rt700->jack_type == 0) { |
| 178 |
ret = rt700_headset_detect(rt700); |
| 179 |
if (ret < 0) |
| 180 |
return; |
| 181 |
if (rt700->jack_type == SND_JACK_HEADSET) |
| 182 |
btn_type = rt700_button_detect(rt700); |
| 183 |
} else if (rt700->jack_type == SND_JACK_HEADSET) { |
| 184 |
/* jack is already in, report button event */ |
| 185 |
btn_type = rt700_button_detect(rt700); |
| 186 |
} |
| 187 |
} else { |
| 188 |
/* jack out */ |
| 189 |
rt700->jack_type = 0; |
| 190 |
} |
| 191 |
|
| 192 |
dev_dbg(&rt700->slave->dev, |
| 193 |
"in %s, jack_type=0x%x\n", __func__, rt700->jack_type); |
| 194 |
dev_dbg(&rt700->slave->dev, |
| 195 |
"in %s, btn_type=0x%x\n", __func__, btn_type); |
| 196 |
|
| 197 |
snd_soc_jack_report(rt700->hs_jack, rt700->jack_type | btn_type, |
| 198 |
SND_JACK_HEADSET | |
| 199 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 200 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 201 |
|
| 202 |
if (btn_type) { |
| 203 |
/* button released */ |
| 204 |
snd_soc_jack_report(rt700->hs_jack, rt700->jack_type, |
| 205 |
SND_JACK_HEADSET | |
| 206 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 207 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 208 |
|
| 209 |
mod_delayed_work(system_power_efficient_wq, |
| 210 |
&rt700->jack_btn_check_work, msecs_to_jiffies(200)); |
| 211 |
} |
| 212 |
|
| 213 |
return; |
| 214 |
|
| 215 |
io_error: |
| 216 |
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); |
| 217 |
} |
| 218 |
|
| 219 |
static void rt700_btn_check_handler(struct work_struct *work) |
| 220 |
{ |
| 221 |
struct rt700_priv *rt700 = container_of(work, struct rt700_priv, |
| 222 |
jack_btn_check_work.work); |
| 223 |
int btn_type = 0, ret; |
| 224 |
unsigned int jack_status = 0, reg; |
| 225 |
|
| 226 |
reg = RT700_VERB_GET_PIN_SENSE | RT700_HP_OUT; |
| 227 |
ret = regmap_read(rt700->regmap, reg, &jack_status); |
| 228 |
if (ret < 0) |
| 229 |
goto io_error; |
| 230 |
|
| 231 |
/* pin attached */ |
| 232 |
if (jack_status & (1 << 31)) { |
| 233 |
if (rt700->jack_type == SND_JACK_HEADSET) { |
| 234 |
/* jack is already in, report button event */ |
| 235 |
btn_type = rt700_button_detect(rt700); |
| 236 |
} |
| 237 |
} else { |
| 238 |
rt700->jack_type = 0; |
| 239 |
} |
| 240 |
|
| 241 |
/* cbj comparator */ |
| 242 |
ret = rt700_index_read(rt700->regmap, RT700_COMBO_JACK_AUTO_CTL2, ®); |
| 243 |
if (ret < 0) |
| 244 |
goto io_error; |
| 245 |
|
| 246 |
if ((reg & 0xf0) == 0xf0) |
| 247 |
btn_type = 0; |
| 248 |
|
| 249 |
dev_dbg(&rt700->slave->dev, |
| 250 |
"%s, btn_type=0x%x\n", __func__, btn_type); |
| 251 |
snd_soc_jack_report(rt700->hs_jack, rt700->jack_type | btn_type, |
| 252 |
SND_JACK_HEADSET | |
| 253 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 254 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 255 |
|
| 256 |
if (btn_type) { |
| 257 |
/* button released */ |
| 258 |
snd_soc_jack_report(rt700->hs_jack, rt700->jack_type, |
| 259 |
SND_JACK_HEADSET | |
| 260 |
SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 261 |
SND_JACK_BTN_2 | SND_JACK_BTN_3); |
| 262 |
|
| 263 |
mod_delayed_work(system_power_efficient_wq, |
| 264 |
&rt700->jack_btn_check_work, msecs_to_jiffies(200)); |
| 265 |
} |
| 266 |
|
| 267 |
return; |
| 268 |
|
| 269 |
io_error: |
| 270 |
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); |
| 271 |
} |
| 272 |
|
| 273 |
static void rt700_jack_init(struct rt700_priv *rt700) |
| 274 |
{ |
| 275 |
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(rt700->component); |
| 276 |
|
| 277 |
/* power on */ |
| 278 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 279 |
regmap_write(rt700->regmap, |
| 280 |
RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 281 |
|
| 282 |
if (rt700->hs_jack) { |
| 283 |
/* Enable Jack Detection */ |
| 284 |
regmap_write(rt700->regmap, |
| 285 |
RT700_SET_MIC2_UNSOLICITED_ENABLE, 0x82); |
| 286 |
regmap_write(rt700->regmap, |
| 287 |
RT700_SET_HP_UNSOLICITED_ENABLE, 0x81); |
| 288 |
regmap_write(rt700->regmap, |
| 289 |
RT700_SET_INLINE_UNSOLICITED_ENABLE, 0x83); |
| 290 |
rt700_index_write(rt700->regmap, 0x10, 0x2420); |
| 291 |
rt700_index_write(rt700->regmap, 0x19, 0x2e11); |
| 292 |
|
| 293 |
dev_dbg(&rt700->slave->dev, "in %s enable\n", __func__); |
| 294 |
|
| 295 |
mod_delayed_work(system_power_efficient_wq, |
| 296 |
&rt700->jack_detect_work, msecs_to_jiffies(250)); |
| 297 |
} else { |
| 298 |
regmap_write(rt700->regmap, |
| 299 |
RT700_SET_MIC2_UNSOLICITED_ENABLE, 0x00); |
| 300 |
regmap_write(rt700->regmap, |
| 301 |
RT700_SET_HP_UNSOLICITED_ENABLE, 0x00); |
| 302 |
regmap_write(rt700->regmap, |
| 303 |
RT700_SET_INLINE_UNSOLICITED_ENABLE, 0x00); |
| 304 |
|
| 305 |
dev_dbg(&rt700->slave->dev, "in %s disable\n", __func__); |
| 306 |
} |
| 307 |
|
| 308 |
/* power off */ |
| 309 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 310 |
regmap_write(rt700->regmap, |
| 311 |
RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 312 |
} |
| 313 |
|
| 314 |
static int rt700_set_jack_detect(struct snd_soc_component *component, |
| 315 |
struct snd_soc_jack *hs_jack, void *data) |
| 316 |
{ |
| 317 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 318 |
int ret; |
| 319 |
|
| 320 |
rt700->hs_jack = hs_jack; |
| 321 |
|
| 322 |
/* we can only resume if the device was initialized at least once */ |
| 323 |
if (!rt700->first_hw_init) |
| 324 |
return 0; |
| 325 |
|
| 326 |
ret = pm_runtime_resume_and_get(component->dev); |
| 327 |
if (ret < 0) { |
| 328 |
if (ret != -EACCES) { |
| 329 |
dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret); |
| 330 |
return ret; |
| 331 |
} |
| 332 |
|
| 333 |
/* pm_runtime not enabled yet */ |
| 334 |
dev_dbg(component->dev, "%s: skipping jack init for now\n", __func__); |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
rt700_jack_init(rt700); |
| 339 |
|
| 340 |
pm_runtime_put_autosuspend(component->dev); |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
static void rt700_get_gain(struct rt700_priv *rt700, unsigned int addr_h, |
| 346 |
unsigned int addr_l, unsigned int val_h, |
| 347 |
unsigned int *r_val, unsigned int *l_val) |
| 348 |
{ |
| 349 |
/* R Channel */ |
| 350 |
*r_val = (val_h << 8); |
| 351 |
regmap_read(rt700->regmap, addr_l, r_val); |
| 352 |
|
| 353 |
/* L Channel */ |
| 354 |
val_h |= 0x20; |
| 355 |
*l_val = (val_h << 8); |
| 356 |
regmap_read(rt700->regmap, addr_h, l_val); |
| 357 |
} |
| 358 |
|
| 359 |
/* For Verb-Set Amplifier Gain (Verb ID = 3h) */ |
| 360 |
static int rt700_set_amp_gain_put(struct snd_kcontrol *kcontrol, |
| 361 |
struct snd_ctl_elem_value *ucontrol) |
| 362 |
{ |
| 363 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 364 |
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); |
| 365 |
struct soc_mixer_control *mc = |
| 366 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 367 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 368 |
unsigned int addr_h, addr_l, val_h, val_ll, val_lr; |
| 369 |
unsigned int read_ll, read_rl; |
| 370 |
int i; |
| 371 |
|
| 372 |
/* Can't use update bit function, so read the original value first */ |
| 373 |
addr_h = mc->reg; |
| 374 |
addr_l = mc->rreg; |
| 375 |
if (mc->shift == RT700_DIR_OUT_SFT) /* output */ |
| 376 |
val_h = 0x80; |
| 377 |
else /* input */ |
| 378 |
val_h = 0x0; |
| 379 |
|
| 380 |
rt700_get_gain(rt700, addr_h, addr_l, val_h, &read_rl, &read_ll); |
| 381 |
|
| 382 |
/* L Channel */ |
| 383 |
if (mc->invert) { |
| 384 |
/* for mute */ |
| 385 |
val_ll = (mc->max - ucontrol->value.integer.value[0]) << 7; |
| 386 |
/* keep gain */ |
| 387 |
read_ll = read_ll & 0x7f; |
| 388 |
val_ll |= read_ll; |
| 389 |
} else { |
| 390 |
/* for gain */ |
| 391 |
val_ll = ((ucontrol->value.integer.value[0]) & 0x7f); |
| 392 |
if (val_ll > mc->max) |
| 393 |
val_ll = mc->max; |
| 394 |
/* keep mute status */ |
| 395 |
read_ll = read_ll & 0x80; |
| 396 |
val_ll |= read_ll; |
| 397 |
} |
| 398 |
|
| 399 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 400 |
regmap_write(rt700->regmap, |
| 401 |
RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 402 |
|
| 403 |
/* R Channel */ |
| 404 |
if (mc->invert) { |
| 405 |
/* for mute */ |
| 406 |
val_lr = (mc->max - ucontrol->value.integer.value[1]) << 7; |
| 407 |
/* keep gain */ |
| 408 |
read_rl = read_rl & 0x7f; |
| 409 |
val_lr |= read_rl; |
| 410 |
} else { |
| 411 |
/* for gain */ |
| 412 |
val_lr = ((ucontrol->value.integer.value[1]) & 0x7f); |
| 413 |
if (val_lr > mc->max) |
| 414 |
val_lr = mc->max; |
| 415 |
/* keep mute status */ |
| 416 |
read_rl = read_rl & 0x80; |
| 417 |
val_lr |= read_rl; |
| 418 |
} |
| 419 |
|
| 420 |
for (i = 0; i < 3; i++) { /* retry 3 times at most */ |
| 421 |
if (val_ll == val_lr) { |
| 422 |
/* Set both L/R channels at the same time */ |
| 423 |
val_h = (1 << mc->shift) | (3 << 4); |
| 424 |
regmap_write(rt700->regmap, |
| 425 |
addr_h, (val_h << 8 | val_ll)); |
| 426 |
regmap_write(rt700->regmap, |
| 427 |
addr_l, (val_h << 8 | val_ll)); |
| 428 |
} else { |
| 429 |
/* Lch*/ |
| 430 |
val_h = (1 << mc->shift) | (1 << 5); |
| 431 |
regmap_write(rt700->regmap, |
| 432 |
addr_h, (val_h << 8 | val_ll)); |
| 433 |
|
| 434 |
/* Rch */ |
| 435 |
val_h = (1 << mc->shift) | (1 << 4); |
| 436 |
regmap_write(rt700->regmap, |
| 437 |
addr_l, (val_h << 8 | val_lr)); |
| 438 |
} |
| 439 |
/* check result */ |
| 440 |
if (mc->shift == RT700_DIR_OUT_SFT) /* output */ |
| 441 |
val_h = 0x80; |
| 442 |
else /* input */ |
| 443 |
val_h = 0x0; |
| 444 |
|
| 445 |
rt700_get_gain(rt700, addr_h, addr_l, val_h, |
| 446 |
&read_rl, &read_ll); |
| 447 |
if (read_rl == val_lr && read_ll == val_ll) |
| 448 |
break; |
| 449 |
} |
| 450 |
|
| 451 |
if (snd_soc_dapm_get_bias_level(dapm) <= SND_SOC_BIAS_STANDBY) |
| 452 |
regmap_write(rt700->regmap, |
| 453 |
RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
static int rt700_set_amp_gain_get(struct snd_kcontrol *kcontrol, |
| 458 |
struct snd_ctl_elem_value *ucontrol) |
| 459 |
{ |
| 460 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 461 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 462 |
struct soc_mixer_control *mc = |
| 463 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 464 |
unsigned int addr_h, addr_l, val_h; |
| 465 |
unsigned int read_ll, read_rl; |
| 466 |
|
| 467 |
addr_h = mc->reg; |
| 468 |
addr_l = mc->rreg; |
| 469 |
if (mc->shift == RT700_DIR_OUT_SFT) /* output */ |
| 470 |
val_h = 0x80; |
| 471 |
else /* input */ |
| 472 |
val_h = 0x0; |
| 473 |
|
| 474 |
rt700_get_gain(rt700, addr_h, addr_l, val_h, &read_rl, &read_ll); |
| 475 |
|
| 476 |
if (mc->invert) { |
| 477 |
/* for mute status */ |
| 478 |
read_ll = !((read_ll & 0x80) >> RT700_MUTE_SFT); |
| 479 |
read_rl = !((read_rl & 0x80) >> RT700_MUTE_SFT); |
| 480 |
} else { |
| 481 |
/* for gain */ |
| 482 |
read_ll = read_ll & 0x7f; |
| 483 |
read_rl = read_rl & 0x7f; |
| 484 |
} |
| 485 |
ucontrol->value.integer.value[0] = read_ll; |
| 486 |
ucontrol->value.integer.value[1] = read_rl; |
| 487 |
|
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); |
| 492 |
static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); |
| 493 |
static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); |
| 494 |
|
| 495 |
static const struct snd_kcontrol_new rt700_snd_controls[] = { |
| 496 |
SOC_DOUBLE_R_EXT_TLV("DAC Front Playback Volume", |
| 497 |
RT700_SET_GAIN_DAC1_H, RT700_SET_GAIN_DAC1_L, |
| 498 |
RT700_DIR_OUT_SFT, 0x57, 0, |
| 499 |
rt700_set_amp_gain_get, rt700_set_amp_gain_put, out_vol_tlv), |
| 500 |
SOC_DOUBLE_R_EXT("ADC 08 Capture Switch", |
| 501 |
RT700_SET_GAIN_ADC2_H, RT700_SET_GAIN_ADC2_L, |
| 502 |
RT700_DIR_IN_SFT, 1, 1, |
| 503 |
rt700_set_amp_gain_get, rt700_set_amp_gain_put), |
| 504 |
SOC_DOUBLE_R_EXT("ADC 09 Capture Switch", |
| 505 |
RT700_SET_GAIN_ADC1_H, RT700_SET_GAIN_ADC1_L, |
| 506 |
RT700_DIR_IN_SFT, 1, 1, |
| 507 |
rt700_set_amp_gain_get, rt700_set_amp_gain_put), |
| 508 |
SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume", |
| 509 |
RT700_SET_GAIN_ADC2_H, RT700_SET_GAIN_ADC2_L, |
| 510 |
RT700_DIR_IN_SFT, 0x3f, 0, |
| 511 |
rt700_set_amp_gain_get, rt700_set_amp_gain_put, in_vol_tlv), |
| 512 |
SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume", |
| 513 |
RT700_SET_GAIN_ADC1_H, RT700_SET_GAIN_ADC1_L, |
| 514 |
RT700_DIR_IN_SFT, 0x3f, 0, |
| 515 |
rt700_set_amp_gain_get, rt700_set_amp_gain_put, in_vol_tlv), |
| 516 |
SOC_DOUBLE_R_EXT_TLV("AMIC Volume", |
| 517 |
RT700_SET_GAIN_AMIC_H, RT700_SET_GAIN_AMIC_L, |
| 518 |
RT700_DIR_IN_SFT, 3, 0, |
| 519 |
rt700_set_amp_gain_get, rt700_set_amp_gain_put, mic_vol_tlv), |
| 520 |
}; |
| 521 |
|
| 522 |
static int rt700_mux_get(struct snd_kcontrol *kcontrol, |
| 523 |
struct snd_ctl_elem_value *ucontrol) |
| 524 |
{ |
| 525 |
struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol); |
| 526 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 527 |
unsigned int reg, val = 0, nid; |
| 528 |
int ret; |
| 529 |
|
| 530 |
if (strstr(ucontrol->id.name, "HPO Mux")) |
| 531 |
nid = RT700_HP_OUT; |
| 532 |
else if (strstr(ucontrol->id.name, "ADC 22 Mux")) |
| 533 |
nid = RT700_MIXER_IN1; |
| 534 |
else if (strstr(ucontrol->id.name, "ADC 23 Mux")) |
| 535 |
nid = RT700_MIXER_IN2; |
| 536 |
else |
| 537 |
return -EINVAL; |
| 538 |
|
| 539 |
/* vid = 0xf01 */ |
| 540 |
reg = RT700_VERB_SET_CONNECT_SEL | nid; |
| 541 |
ret = regmap_read(rt700->regmap, reg, &val); |
| 542 |
if (ret < 0) |
| 543 |
return ret; |
| 544 |
|
| 545 |
ucontrol->value.enumerated.item[0] = val; |
| 546 |
|
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
static int rt700_mux_put(struct snd_kcontrol *kcontrol, |
| 551 |
struct snd_ctl_elem_value *ucontrol) |
| 552 |
{ |
| 553 |
struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol); |
| 554 |
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol); |
| 555 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 556 |
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 557 |
unsigned int *item = ucontrol->value.enumerated.item; |
| 558 |
unsigned int val, val2 = 0, change, reg, nid; |
| 559 |
int ret; |
| 560 |
|
| 561 |
if (item[0] >= e->items) |
| 562 |
return -EINVAL; |
| 563 |
|
| 564 |
if (strstr(ucontrol->id.name, "HPO Mux")) |
| 565 |
nid = RT700_HP_OUT; |
| 566 |
else if (strstr(ucontrol->id.name, "ADC 22 Mux")) |
| 567 |
nid = RT700_MIXER_IN1; |
| 568 |
else if (strstr(ucontrol->id.name, "ADC 23 Mux")) |
| 569 |
nid = RT700_MIXER_IN2; |
| 570 |
else |
| 571 |
return -EINVAL; |
| 572 |
|
| 573 |
/* Verb ID = 0x701h */ |
| 574 |
val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; |
| 575 |
|
| 576 |
reg = RT700_VERB_SET_CONNECT_SEL | nid; |
| 577 |
ret = regmap_read(rt700->regmap, reg, &val2); |
| 578 |
if (ret < 0) |
| 579 |
return ret; |
| 580 |
|
| 581 |
if (val == val2) |
| 582 |
change = 0; |
| 583 |
else |
| 584 |
change = 1; |
| 585 |
|
| 586 |
if (change) { |
| 587 |
reg = RT700_VERB_SET_CONNECT_SEL | nid; |
| 588 |
regmap_write(rt700->regmap, reg, val); |
| 589 |
} |
| 590 |
|
| 591 |
snd_soc_dapm_mux_update_power(dapm, kcontrol, |
| 592 |
item[0], e, NULL); |
| 593 |
|
| 594 |
return change; |
| 595 |
} |
| 596 |
|
| 597 |
static const char * const adc_mux_text[] = { |
| 598 |
"MIC2", |
| 599 |
"LINE1", |
| 600 |
"LINE2", |
| 601 |
"DMIC", |
| 602 |
}; |
| 603 |
|
| 604 |
static SOC_ENUM_SINGLE_DECL( |
| 605 |
rt700_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); |
| 606 |
|
| 607 |
static SOC_ENUM_SINGLE_DECL( |
| 608 |
rt700_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); |
| 609 |
|
| 610 |
static const struct snd_kcontrol_new rt700_adc22_mux = |
| 611 |
SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt700_adc22_enum, |
| 612 |
rt700_mux_get, rt700_mux_put); |
| 613 |
|
| 614 |
static const struct snd_kcontrol_new rt700_adc23_mux = |
| 615 |
SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt700_adc23_enum, |
| 616 |
rt700_mux_get, rt700_mux_put); |
| 617 |
|
| 618 |
static const char * const out_mux_text[] = { |
| 619 |
"Front", |
| 620 |
"Surround", |
| 621 |
}; |
| 622 |
|
| 623 |
static SOC_ENUM_SINGLE_DECL( |
| 624 |
rt700_hp_enum, SND_SOC_NOPM, 0, out_mux_text); |
| 625 |
|
| 626 |
static const struct snd_kcontrol_new rt700_hp_mux = |
| 627 |
SOC_DAPM_ENUM_EXT("HP Mux", rt700_hp_enum, |
| 628 |
rt700_mux_get, rt700_mux_put); |
| 629 |
|
| 630 |
static int rt700_dac_front_event(struct snd_soc_dapm_widget *w, |
| 631 |
struct snd_kcontrol *kcontrol, int event) |
| 632 |
{ |
| 633 |
struct snd_soc_component *component = |
| 634 |
snd_soc_dapm_to_component(w->dapm); |
| 635 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 636 |
|
| 637 |
switch (event) { |
| 638 |
case SND_SOC_DAPM_POST_PMU: |
| 639 |
regmap_write(rt700->regmap, |
| 640 |
RT700_SET_STREAMID_DAC1, 0x10); |
| 641 |
break; |
| 642 |
case SND_SOC_DAPM_PRE_PMD: |
| 643 |
regmap_write(rt700->regmap, |
| 644 |
RT700_SET_STREAMID_DAC1, 0x00); |
| 645 |
break; |
| 646 |
} |
| 647 |
return 0; |
| 648 |
} |
| 649 |
|
| 650 |
static int rt700_dac_surround_event(struct snd_soc_dapm_widget *w, |
| 651 |
struct snd_kcontrol *kcontrol, int event) |
| 652 |
{ |
| 653 |
struct snd_soc_component *component = |
| 654 |
snd_soc_dapm_to_component(w->dapm); |
| 655 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 656 |
|
| 657 |
switch (event) { |
| 658 |
case SND_SOC_DAPM_POST_PMU: |
| 659 |
regmap_write(rt700->regmap, |
| 660 |
RT700_SET_STREAMID_DAC2, 0x10); |
| 661 |
break; |
| 662 |
case SND_SOC_DAPM_PRE_PMD: |
| 663 |
regmap_write(rt700->regmap, |
| 664 |
RT700_SET_STREAMID_DAC2, 0x00); |
| 665 |
break; |
| 666 |
} |
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
static int rt700_adc_09_event(struct snd_soc_dapm_widget *w, |
| 671 |
struct snd_kcontrol *kcontrol, int event) |
| 672 |
{ |
| 673 |
struct snd_soc_component *component = |
| 674 |
snd_soc_dapm_to_component(w->dapm); |
| 675 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 676 |
|
| 677 |
switch (event) { |
| 678 |
case SND_SOC_DAPM_POST_PMU: |
| 679 |
regmap_write(rt700->regmap, |
| 680 |
RT700_SET_STREAMID_ADC1, 0x10); |
| 681 |
break; |
| 682 |
case SND_SOC_DAPM_PRE_PMD: |
| 683 |
regmap_write(rt700->regmap, |
| 684 |
RT700_SET_STREAMID_ADC1, 0x00); |
| 685 |
break; |
| 686 |
} |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
static int rt700_adc_08_event(struct snd_soc_dapm_widget *w, |
| 691 |
struct snd_kcontrol *kcontrol, int event) |
| 692 |
{ |
| 693 |
struct snd_soc_component *component = |
| 694 |
snd_soc_dapm_to_component(w->dapm); |
| 695 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 696 |
|
| 697 |
switch (event) { |
| 698 |
case SND_SOC_DAPM_POST_PMU: |
| 699 |
regmap_write(rt700->regmap, |
| 700 |
RT700_SET_STREAMID_ADC2, 0x10); |
| 701 |
break; |
| 702 |
case SND_SOC_DAPM_PRE_PMD: |
| 703 |
regmap_write(rt700->regmap, |
| 704 |
RT700_SET_STREAMID_ADC2, 0x00); |
| 705 |
break; |
| 706 |
} |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
static int rt700_hpo_mux_event(struct snd_soc_dapm_widget *w, |
| 711 |
struct snd_kcontrol *kcontrol, int event) |
| 712 |
{ |
| 713 |
struct snd_soc_component *component = |
| 714 |
snd_soc_dapm_to_component(w->dapm); |
| 715 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 716 |
unsigned int val_h = (1 << RT700_DIR_OUT_SFT) | (0x3 << 4); |
| 717 |
unsigned int val_l; |
| 718 |
|
| 719 |
switch (event) { |
| 720 |
case SND_SOC_DAPM_POST_PMU: |
| 721 |
val_l = 0x00; |
| 722 |
regmap_write(rt700->regmap, |
| 723 |
RT700_SET_GAIN_HP_H, (val_h << 8 | val_l)); |
| 724 |
break; |
| 725 |
case SND_SOC_DAPM_PRE_PMD: |
| 726 |
val_l = (1 << RT700_MUTE_SFT); |
| 727 |
regmap_write(rt700->regmap, |
| 728 |
RT700_SET_GAIN_HP_H, (val_h << 8 | val_l)); |
| 729 |
usleep_range(50000, 55000); |
| 730 |
break; |
| 731 |
} |
| 732 |
return 0; |
| 733 |
} |
| 734 |
|
| 735 |
static int rt700_spk_pga_event(struct snd_soc_dapm_widget *w, |
| 736 |
struct snd_kcontrol *kcontrol, int event) |
| 737 |
{ |
| 738 |
struct snd_soc_component *component = |
| 739 |
snd_soc_dapm_to_component(w->dapm); |
| 740 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 741 |
unsigned int val_h = (1 << RT700_DIR_OUT_SFT) | (0x3 << 4); |
| 742 |
unsigned int val_l; |
| 743 |
|
| 744 |
switch (event) { |
| 745 |
case SND_SOC_DAPM_POST_PMU: |
| 746 |
val_l = 0x00; |
| 747 |
regmap_write(rt700->regmap, |
| 748 |
RT700_SET_GAIN_SPK_H, (val_h << 8 | val_l)); |
| 749 |
break; |
| 750 |
case SND_SOC_DAPM_PRE_PMD: |
| 751 |
val_l = (1 << RT700_MUTE_SFT); |
| 752 |
regmap_write(rt700->regmap, |
| 753 |
RT700_SET_GAIN_SPK_H, (val_h << 8 | val_l)); |
| 754 |
break; |
| 755 |
} |
| 756 |
return 0; |
| 757 |
} |
| 758 |
|
| 759 |
static const struct snd_soc_dapm_widget rt700_dapm_widgets[] = { |
| 760 |
SND_SOC_DAPM_OUTPUT("HP"), |
| 761 |
SND_SOC_DAPM_OUTPUT("SPK"), |
| 762 |
SND_SOC_DAPM_INPUT("DMIC1"), |
| 763 |
SND_SOC_DAPM_INPUT("DMIC2"), |
| 764 |
SND_SOC_DAPM_INPUT("MIC2"), |
| 765 |
SND_SOC_DAPM_INPUT("LINE1"), |
| 766 |
SND_SOC_DAPM_INPUT("LINE2"), |
| 767 |
SND_SOC_DAPM_DAC_E("DAC Front", NULL, SND_SOC_NOPM, 0, 0, |
| 768 |
rt700_dac_front_event, |
| 769 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 770 |
SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0, |
| 771 |
rt700_dac_surround_event, |
| 772 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 773 |
SND_SOC_DAPM_MUX_E("HPO Mux", SND_SOC_NOPM, 0, 0, &rt700_hp_mux, |
| 774 |
rt700_hpo_mux_event, |
| 775 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 776 |
SND_SOC_DAPM_PGA_E("SPK PGA", SND_SOC_NOPM, 0, 0, NULL, 0, |
| 777 |
rt700_spk_pga_event, |
| 778 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 779 |
SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0, |
| 780 |
rt700_adc_09_event, |
| 781 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 782 |
SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0, |
| 783 |
rt700_adc_08_event, |
| 784 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 785 |
SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, |
| 786 |
&rt700_adc22_mux), |
| 787 |
SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, |
| 788 |
&rt700_adc23_mux), |
| 789 |
SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 790 |
SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 791 |
SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 792 |
SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 793 |
}; |
| 794 |
|
| 795 |
static const struct snd_soc_dapm_route rt700_audio_map[] = { |
| 796 |
{"DAC Front", NULL, "DP1RX"}, |
| 797 |
{"DAC Surround", NULL, "DP3RX"}, |
| 798 |
{"DP2TX", NULL, "ADC 09"}, |
| 799 |
{"DP4TX", NULL, "ADC 08"}, |
| 800 |
{"ADC 09", NULL, "ADC 22 Mux"}, |
| 801 |
{"ADC 08", NULL, "ADC 23 Mux"}, |
| 802 |
{"ADC 22 Mux", "DMIC", "DMIC1"}, |
| 803 |
{"ADC 22 Mux", "LINE1", "LINE1"}, |
| 804 |
{"ADC 22 Mux", "LINE2", "LINE2"}, |
| 805 |
{"ADC 22 Mux", "MIC2", "MIC2"}, |
| 806 |
{"ADC 23 Mux", "DMIC", "DMIC2"}, |
| 807 |
{"ADC 23 Mux", "LINE1", "LINE1"}, |
| 808 |
{"ADC 23 Mux", "LINE2", "LINE2"}, |
| 809 |
{"ADC 23 Mux", "MIC2", "MIC2"}, |
| 810 |
{"HPO Mux", "Front", "DAC Front"}, |
| 811 |
{"HPO Mux", "Surround", "DAC Surround"}, |
| 812 |
{"HP", NULL, "HPO Mux"}, |
| 813 |
{"SPK PGA", NULL, "DAC Front"}, |
| 814 |
{"SPK", NULL, "SPK PGA"}, |
| 815 |
}; |
| 816 |
|
| 817 |
static int rt700_probe(struct snd_soc_component *component) |
| 818 |
{ |
| 819 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 820 |
int ret; |
| 821 |
|
| 822 |
rt700->component = component; |
| 823 |
|
| 824 |
if (!rt700->first_hw_init) |
| 825 |
return 0; |
| 826 |
|
| 827 |
ret = pm_runtime_resume(component->dev); |
| 828 |
if (ret < 0 && ret != -EACCES) |
| 829 |
return ret; |
| 830 |
|
| 831 |
return 0; |
| 832 |
} |
| 833 |
|
| 834 |
static int rt700_set_bias_level(struct snd_soc_component *component, |
| 835 |
enum snd_soc_bias_level level) |
| 836 |
{ |
| 837 |
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); |
| 838 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 839 |
|
| 840 |
switch (level) { |
| 841 |
case SND_SOC_BIAS_PREPARE: |
| 842 |
if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_STANDBY) { |
| 843 |
regmap_write(rt700->regmap, |
| 844 |
RT700_SET_AUDIO_POWER_STATE, |
| 845 |
AC_PWRST_D0); |
| 846 |
} |
| 847 |
break; |
| 848 |
|
| 849 |
case SND_SOC_BIAS_STANDBY: |
| 850 |
regmap_write(rt700->regmap, |
| 851 |
RT700_SET_AUDIO_POWER_STATE, |
| 852 |
AC_PWRST_D3); |
| 853 |
break; |
| 854 |
|
| 855 |
default: |
| 856 |
break; |
| 857 |
} |
| 858 |
|
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
static const struct snd_soc_component_driver soc_codec_dev_rt700 = { |
| 863 |
.probe = rt700_probe, |
| 864 |
.set_bias_level = rt700_set_bias_level, |
| 865 |
.controls = rt700_snd_controls, |
| 866 |
.num_controls = ARRAY_SIZE(rt700_snd_controls), |
| 867 |
.dapm_widgets = rt700_dapm_widgets, |
| 868 |
.num_dapm_widgets = ARRAY_SIZE(rt700_dapm_widgets), |
| 869 |
.dapm_routes = rt700_audio_map, |
| 870 |
.num_dapm_routes = ARRAY_SIZE(rt700_audio_map), |
| 871 |
.set_jack = rt700_set_jack_detect, |
| 872 |
.endianness = 1, |
| 873 |
}; |
| 874 |
|
| 875 |
static int rt700_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, |
| 876 |
int direction) |
| 877 |
{ |
| 878 |
snd_soc_dai_dma_data_set(dai, direction, sdw_stream); |
| 879 |
|
| 880 |
return 0; |
| 881 |
} |
| 882 |
|
| 883 |
static void rt700_shutdown(struct snd_pcm_substream *substream, |
| 884 |
struct snd_soc_dai *dai) |
| 885 |
{ |
| 886 |
snd_soc_dai_set_dma_data(dai, substream, NULL); |
| 887 |
} |
| 888 |
|
| 889 |
static int rt700_pcm_hw_params(struct snd_pcm_substream *substream, |
| 890 |
struct snd_pcm_hw_params *params, |
| 891 |
struct snd_soc_dai *dai) |
| 892 |
{ |
| 893 |
struct snd_soc_component *component = dai->component; |
| 894 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 895 |
struct sdw_stream_config stream_config = {0}; |
| 896 |
struct sdw_port_config port_config = {0}; |
| 897 |
struct sdw_stream_runtime *sdw_stream; |
| 898 |
int retval; |
| 899 |
unsigned int val = 0; |
| 900 |
|
| 901 |
dev_dbg(dai->dev, "%s %s", __func__, dai->name); |
| 902 |
sdw_stream = snd_soc_dai_get_dma_data(dai, substream); |
| 903 |
|
| 904 |
if (!sdw_stream) |
| 905 |
return -EINVAL; |
| 906 |
|
| 907 |
if (!rt700->slave) |
| 908 |
return -EINVAL; |
| 909 |
|
| 910 |
/* SoundWire specific configuration */ |
| 911 |
snd_sdw_params_to_config(substream, params, &stream_config, &port_config); |
| 912 |
|
| 913 |
/* This code assumes port 1 for playback and port 2 for capture */ |
| 914 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 915 |
port_config.num = 1; |
| 916 |
else |
| 917 |
port_config.num = 2; |
| 918 |
|
| 919 |
switch (dai->id) { |
| 920 |
case RT700_AIF1: |
| 921 |
break; |
| 922 |
case RT700_AIF2: |
| 923 |
port_config.num += 2; |
| 924 |
break; |
| 925 |
default: |
| 926 |
dev_err(component->dev, "%s: Invalid DAI id %d\n", __func__, dai->id); |
| 927 |
return -EINVAL; |
| 928 |
} |
| 929 |
|
| 930 |
retval = sdw_stream_add_slave(rt700->slave, &stream_config, |
| 931 |
&port_config, 1, sdw_stream); |
| 932 |
if (retval) { |
| 933 |
dev_err(dai->dev, "%s: Unable to configure port\n", __func__); |
| 934 |
return retval; |
| 935 |
} |
| 936 |
|
| 937 |
if (params_channels(params) <= 16) { |
| 938 |
/* bit 3:0 Number of Channel */ |
| 939 |
val |= (params_channels(params) - 1); |
| 940 |
} else { |
| 941 |
dev_err(component->dev, "%s: Unsupported channels %d\n", |
| 942 |
__func__, params_channels(params)); |
| 943 |
return -EINVAL; |
| 944 |
} |
| 945 |
|
| 946 |
switch (params_width(params)) { |
| 947 |
/* bit 6:4 Bits per Sample */ |
| 948 |
case 8: |
| 949 |
break; |
| 950 |
case 16: |
| 951 |
val |= (0x1 << 4); |
| 952 |
break; |
| 953 |
case 20: |
| 954 |
val |= (0x2 << 4); |
| 955 |
break; |
| 956 |
case 24: |
| 957 |
val |= (0x3 << 4); |
| 958 |
break; |
| 959 |
case 32: |
| 960 |
val |= (0x4 << 4); |
| 961 |
break; |
| 962 |
default: |
| 963 |
return -EINVAL; |
| 964 |
} |
| 965 |
|
| 966 |
/* 48Khz */ |
| 967 |
regmap_write(rt700->regmap, RT700_DAC_FORMAT_H, val); |
| 968 |
regmap_write(rt700->regmap, RT700_ADC_FORMAT_H, val); |
| 969 |
|
| 970 |
return retval; |
| 971 |
} |
| 972 |
|
| 973 |
static int rt700_pcm_hw_free(struct snd_pcm_substream *substream, |
| 974 |
struct snd_soc_dai *dai) |
| 975 |
{ |
| 976 |
struct snd_soc_component *component = dai->component; |
| 977 |
struct rt700_priv *rt700 = snd_soc_component_get_drvdata(component); |
| 978 |
struct sdw_stream_runtime *sdw_stream = |
| 979 |
snd_soc_dai_get_dma_data(dai, substream); |
| 980 |
|
| 981 |
if (!rt700->slave) |
| 982 |
return -EINVAL; |
| 983 |
|
| 984 |
sdw_stream_remove_slave(rt700->slave, sdw_stream); |
| 985 |
return 0; |
| 986 |
} |
| 987 |
|
| 988 |
#define RT700_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) |
| 989 |
#define RT700_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 990 |
SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8) |
| 991 |
|
| 992 |
static const struct snd_soc_dai_ops rt700_ops = { |
| 993 |
.hw_params = rt700_pcm_hw_params, |
| 994 |
.hw_free = rt700_pcm_hw_free, |
| 995 |
.set_stream = rt700_set_sdw_stream, |
| 996 |
.shutdown = rt700_shutdown, |
| 997 |
}; |
| 998 |
|
| 999 |
static struct snd_soc_dai_driver rt700_dai[] = { |
| 1000 |
{ |
| 1001 |
.name = "rt700-aif1", |
| 1002 |
.id = RT700_AIF1, |
| 1003 |
.playback = { |
| 1004 |
.stream_name = "DP1 Playback", |
| 1005 |
.channels_min = 1, |
| 1006 |
.channels_max = 2, |
| 1007 |
.rates = RT700_STEREO_RATES, |
| 1008 |
.formats = RT700_FORMATS, |
| 1009 |
}, |
| 1010 |
.capture = { |
| 1011 |
.stream_name = "DP2 Capture", |
| 1012 |
.channels_min = 1, |
| 1013 |
.channels_max = 2, |
| 1014 |
.rates = RT700_STEREO_RATES, |
| 1015 |
.formats = RT700_FORMATS, |
| 1016 |
}, |
| 1017 |
.ops = &rt700_ops, |
| 1018 |
}, |
| 1019 |
{ |
| 1020 |
.name = "rt700-aif2", |
| 1021 |
.id = RT700_AIF2, |
| 1022 |
.playback = { |
| 1023 |
.stream_name = "DP3 Playback", |
| 1024 |
.channels_min = 1, |
| 1025 |
.channels_max = 2, |
| 1026 |
.rates = RT700_STEREO_RATES, |
| 1027 |
.formats = RT700_FORMATS, |
| 1028 |
}, |
| 1029 |
.capture = { |
| 1030 |
.stream_name = "DP4 Capture", |
| 1031 |
.channels_min = 1, |
| 1032 |
.channels_max = 2, |
| 1033 |
.rates = RT700_STEREO_RATES, |
| 1034 |
.formats = RT700_FORMATS, |
| 1035 |
}, |
| 1036 |
.ops = &rt700_ops, |
| 1037 |
}, |
| 1038 |
}; |
| 1039 |
|
| 1040 |
/* Bus clock frequency */ |
| 1041 |
#define RT700_CLK_FREQ_9600000HZ 9600000 |
| 1042 |
#define RT700_CLK_FREQ_12000000HZ 12000000 |
| 1043 |
#define RT700_CLK_FREQ_6000000HZ 6000000 |
| 1044 |
#define RT700_CLK_FREQ_4800000HZ 4800000 |
| 1045 |
#define RT700_CLK_FREQ_2400000HZ 2400000 |
| 1046 |
#define RT700_CLK_FREQ_12288000HZ 12288000 |
| 1047 |
|
| 1048 |
int rt700_clock_config(struct device *dev) |
| 1049 |
{ |
| 1050 |
struct rt700_priv *rt700 = dev_get_drvdata(dev); |
| 1051 |
unsigned int clk_freq, value; |
| 1052 |
|
| 1053 |
clk_freq = (rt700->params.curr_dr_freq >> 1); |
| 1054 |
|
| 1055 |
switch (clk_freq) { |
| 1056 |
case RT700_CLK_FREQ_12000000HZ: |
| 1057 |
value = 0x0; |
| 1058 |
break; |
| 1059 |
case RT700_CLK_FREQ_6000000HZ: |
| 1060 |
value = 0x1; |
| 1061 |
break; |
| 1062 |
case RT700_CLK_FREQ_9600000HZ: |
| 1063 |
value = 0x2; |
| 1064 |
break; |
| 1065 |
case RT700_CLK_FREQ_4800000HZ: |
| 1066 |
value = 0x3; |
| 1067 |
break; |
| 1068 |
case RT700_CLK_FREQ_2400000HZ: |
| 1069 |
value = 0x4; |
| 1070 |
break; |
| 1071 |
case RT700_CLK_FREQ_12288000HZ: |
| 1072 |
value = 0x5; |
| 1073 |
break; |
| 1074 |
default: |
| 1075 |
return -EINVAL; |
| 1076 |
} |
| 1077 |
|
| 1078 |
regmap_write(rt700->regmap, 0xe0, value); |
| 1079 |
regmap_write(rt700->regmap, 0xf0, value); |
| 1080 |
|
| 1081 |
dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq); |
| 1082 |
|
| 1083 |
return 0; |
| 1084 |
} |
| 1085 |
|
| 1086 |
int rt700_init(struct device *dev, struct regmap *sdw_regmap, |
| 1087 |
struct regmap *regmap, struct sdw_slave *slave) |
| 1088 |
|
| 1089 |
{ |
| 1090 |
struct rt700_priv *rt700; |
| 1091 |
int ret; |
| 1092 |
|
| 1093 |
rt700 = devm_kzalloc(dev, sizeof(*rt700), GFP_KERNEL); |
| 1094 |
if (!rt700) |
| 1095 |
return -ENOMEM; |
| 1096 |
|
| 1097 |
dev_set_drvdata(dev, rt700); |
| 1098 |
rt700->slave = slave; |
| 1099 |
rt700->sdw_regmap = sdw_regmap; |
| 1100 |
rt700->regmap = regmap; |
| 1101 |
|
| 1102 |
regcache_cache_only(rt700->regmap, true); |
| 1103 |
|
| 1104 |
mutex_init(&rt700->disable_irq_lock); |
| 1105 |
|
| 1106 |
INIT_DELAYED_WORK(&rt700->jack_detect_work, |
| 1107 |
rt700_jack_detect_handler); |
| 1108 |
INIT_DELAYED_WORK(&rt700->jack_btn_check_work, |
| 1109 |
rt700_btn_check_handler); |
| 1110 |
|
| 1111 |
/* |
| 1112 |
* Mark hw_init to false |
| 1113 |
* HW init will be performed when device reports present |
| 1114 |
*/ |
| 1115 |
rt700->hw_init = false; |
| 1116 |
rt700->first_hw_init = false; |
| 1117 |
|
| 1118 |
ret = devm_snd_soc_register_component(dev, |
| 1119 |
&soc_codec_dev_rt700, |
| 1120 |
rt700_dai, |
| 1121 |
ARRAY_SIZE(rt700_dai)); |
| 1122 |
if (ret < 0) |
| 1123 |
return ret; |
| 1124 |
|
| 1125 |
/* set autosuspend parameters */ |
| 1126 |
pm_runtime_set_autosuspend_delay(dev, 3000); |
| 1127 |
pm_runtime_use_autosuspend(dev); |
| 1128 |
|
| 1129 |
/* make sure the device does not suspend immediately */ |
| 1130 |
pm_runtime_mark_last_busy(dev); |
| 1131 |
|
| 1132 |
pm_runtime_enable(dev); |
| 1133 |
|
| 1134 |
/* important note: the device is NOT tagged as 'active' and will remain |
| 1135 |
* 'suspended' until the hardware is enumerated/initialized. This is required |
| 1136 |
* to make sure the ASoC framework use of pm_runtime_get_sync() does not silently |
| 1137 |
* fail with -EACCESS because of race conditions between card creation and enumeration |
| 1138 |
*/ |
| 1139 |
dev_dbg(&slave->dev, "%s\n", __func__); |
| 1140 |
|
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
|
| 1144 |
int rt700_io_init(struct device *dev, struct sdw_slave *slave) |
| 1145 |
{ |
| 1146 |
struct rt700_priv *rt700 = dev_get_drvdata(dev); |
| 1147 |
|
| 1148 |
rt700->disable_irq = false; |
| 1149 |
|
| 1150 |
if (rt700->hw_init) |
| 1151 |
return 0; |
| 1152 |
|
| 1153 |
regcache_cache_only(rt700->regmap, false); |
| 1154 |
if (rt700->first_hw_init) |
| 1155 |
regcache_cache_bypass(rt700->regmap, true); |
| 1156 |
|
| 1157 |
/* |
| 1158 |
* PM runtime is only enabled when a Slave reports as Attached |
| 1159 |
*/ |
| 1160 |
if (!rt700->first_hw_init) |
| 1161 |
/* PM runtime status is marked as 'active' only when a Slave reports as Attached */ |
| 1162 |
pm_runtime_set_active(&slave->dev); |
| 1163 |
|
| 1164 |
pm_runtime_get_noresume(&slave->dev); |
| 1165 |
|
| 1166 |
/* reset */ |
| 1167 |
regmap_write(rt700->regmap, 0xff01, 0x0000); |
| 1168 |
regmap_write(rt700->regmap, 0x7520, 0x001a); |
| 1169 |
regmap_write(rt700->regmap, 0x7420, 0xc003); |
| 1170 |
|
| 1171 |
/* power on */ |
| 1172 |
regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D0); |
| 1173 |
/* Set Pin Widget */ |
| 1174 |
regmap_write(rt700->regmap, RT700_SET_PIN_HP, 0x40); |
| 1175 |
regmap_write(rt700->regmap, RT700_SET_PIN_SPK, 0x40); |
| 1176 |
regmap_write(rt700->regmap, RT700_SET_EAPD_SPK, RT700_EAPD_HIGH); |
| 1177 |
regmap_write(rt700->regmap, RT700_SET_PIN_DMIC1, 0x20); |
| 1178 |
regmap_write(rt700->regmap, RT700_SET_PIN_DMIC2, 0x20); |
| 1179 |
regmap_write(rt700->regmap, RT700_SET_PIN_MIC2, 0x20); |
| 1180 |
|
| 1181 |
/* Set Configuration Default */ |
| 1182 |
regmap_write(rt700->regmap, 0x4f12, 0x91); |
| 1183 |
regmap_write(rt700->regmap, 0x4e12, 0xd6); |
| 1184 |
regmap_write(rt700->regmap, 0x4d12, 0x11); |
| 1185 |
regmap_write(rt700->regmap, 0x4c12, 0x20); |
| 1186 |
regmap_write(rt700->regmap, 0x4f13, 0x91); |
| 1187 |
regmap_write(rt700->regmap, 0x4e13, 0xd6); |
| 1188 |
regmap_write(rt700->regmap, 0x4d13, 0x11); |
| 1189 |
regmap_write(rt700->regmap, 0x4c13, 0x21); |
| 1190 |
|
| 1191 |
regmap_write(rt700->regmap, 0x4f19, 0x02); |
| 1192 |
regmap_write(rt700->regmap, 0x4e19, 0xa1); |
| 1193 |
regmap_write(rt700->regmap, 0x4d19, 0x90); |
| 1194 |
regmap_write(rt700->regmap, 0x4c19, 0x80); |
| 1195 |
|
| 1196 |
/* Enable Line2 */ |
| 1197 |
regmap_write(rt700->regmap, 0x371b, 0x40); |
| 1198 |
regmap_write(rt700->regmap, 0x731b, 0xb0); |
| 1199 |
regmap_write(rt700->regmap, 0x839b, 0x00); |
| 1200 |
|
| 1201 |
/* Set index */ |
| 1202 |
rt700_index_write(rt700->regmap, 0x4a, 0x201b); |
| 1203 |
rt700_index_write(rt700->regmap, 0x45, 0x5089); |
| 1204 |
rt700_index_write(rt700->regmap, 0x6b, 0x5064); |
| 1205 |
rt700_index_write(rt700->regmap, 0x48, 0xd249); |
| 1206 |
|
| 1207 |
/* Finish Initial Settings, set power to D3 */ |
| 1208 |
regmap_write(rt700->regmap, RT700_SET_AUDIO_POWER_STATE, AC_PWRST_D3); |
| 1209 |
|
| 1210 |
/* |
| 1211 |
* if set_jack callback occurred early than io_init, |
| 1212 |
* we set up the jack detection function now |
| 1213 |
*/ |
| 1214 |
if (rt700->hs_jack) |
| 1215 |
rt700_jack_init(rt700); |
| 1216 |
|
| 1217 |
if (rt700->first_hw_init) { |
| 1218 |
regcache_cache_bypass(rt700->regmap, false); |
| 1219 |
regcache_mark_dirty(rt700->regmap); |
| 1220 |
} else |
| 1221 |
rt700->first_hw_init = true; |
| 1222 |
|
| 1223 |
/* Mark Slave initialization complete */ |
| 1224 |
rt700->hw_init = true; |
| 1225 |
|
| 1226 |
pm_runtime_put_autosuspend(&slave->dev); |
| 1227 |
|
| 1228 |
dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); |
| 1229 |
|
| 1230 |
return 0; |
| 1231 |
} |
| 1232 |
|
| 1233 |
MODULE_DESCRIPTION("ASoC RT700 driver SDW"); |
| 1234 |
MODULE_AUTHOR("Shuming Fan <[email protected]>"); |
| 1235 |
MODULE_LICENSE("GPL v2"); |