1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // ALSA SoC Texas Instruments TAS2770 20-W Digital Input Mono Class-D 4 // Audio Amplifier with Speaker I/V Sense 5 // 6 // Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/ 7 // Author: Tracy Yi <tracy-yi@ti.com> 8 // Frank Shi <shifu0704@thundersoft.com> 9 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <linux/err.h> 13 #include <linux/init.h> 14 #include <linux/delay.h> 15 #include <linux/hwmon.h> 16 #include <linux/pm.h> 17 #include <linux/i2c.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/regulator/consumer.h> 20 #include <linux/firmware.h> 21 #include <linux/regmap.h> 22 #include <linux/of.h> 23 #include <linux/slab.h> 24 #include <sound/soc.h> 25 #include <sound/pcm.h> 26 #include <sound/pcm_params.h> 27 #include <sound/initval.h> 28 #include <sound/tlv.h> 29 30 #include "tas2770.h" 31 32 #define TAS2770_MDELAY 0xFFFFFFFE 33 34 static void tas2770_reset(struct tas2770_priv *tas2770) 35 { 36 if (tas2770->reset_gpio) { 37 gpiod_set_value_cansleep(tas2770->reset_gpio, 0); 38 msleep(20); 39 gpiod_set_value_cansleep(tas2770->reset_gpio, 1); 40 usleep_range(1000, 2000); 41 } 42 43 snd_soc_component_write(tas2770->component, TAS2770_SW_RST, 44 TAS2770_RST); 45 usleep_range(1000, 2000); 46 } 47 48 static int tas2770_update_pwr_ctrl(struct tas2770_priv *tas2770) 49 { 50 struct snd_soc_component *component = tas2770->component; 51 unsigned int val; 52 int ret; 53 54 if (tas2770->dac_powered) 55 val = tas2770->unmuted ? 56 TAS2770_PWR_CTRL_ACTIVE : TAS2770_PWR_CTRL_MUTE; 57 else 58 val = TAS2770_PWR_CTRL_SHUTDOWN; 59 60 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL, 61 TAS2770_PWR_CTRL_MASK, val); 62 if (ret < 0) 63 return ret; 64 65 return 0; 66 } 67 68 #ifdef CONFIG_PM 69 static int tas2770_codec_suspend(struct snd_soc_component *component) 70 { 71 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component); 72 int ret = 0; 73 74 regcache_cache_only(tas2770->regmap, true); 75 regcache_mark_dirty(tas2770->regmap); 76 77 if (tas2770->sdz_gpio) { 78 gpiod_set_value_cansleep(tas2770->sdz_gpio, 0); 79 } else { 80 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL, 81 TAS2770_PWR_CTRL_MASK, 82 TAS2770_PWR_CTRL_SHUTDOWN); 83 if (ret < 0) { 84 regcache_cache_only(tas2770->regmap, false); 85 regcache_sync(tas2770->regmap); 86 return ret; 87 } 88 89 ret = 0; 90 } 91 92 return ret; 93 } 94 95 static int tas2770_codec_resume(struct snd_soc_component *component) 96 { 97 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component); 98 int ret; 99 100 if (tas2770->sdz_gpio) { 101 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1); 102 usleep_range(1000, 2000); 103 } else { 104 ret = tas2770_update_pwr_ctrl(tas2770); 105 if (ret < 0) 106 return ret; 107 } 108 109 regcache_cache_only(tas2770->regmap, false); 110 111 return regcache_sync(tas2770->regmap); 112 } 113 #else 114 #define tas2770_codec_suspend NULL 115 #define tas2770_codec_resume NULL 116 #endif 117 118 static const char * const tas2770_ASI1_src[] = { 119 "I2C offset", "Left", "Right", "LeftRightDiv2", 120 }; 121 122 static SOC_ENUM_SINGLE_DECL( 123 tas2770_ASI1_src_enum, TAS2770_TDM_CFG_REG2, 124 4, tas2770_ASI1_src); 125 126 static const struct snd_kcontrol_new tas2770_asi1_mux = 127 SOC_DAPM_ENUM("ASI1 Source", tas2770_ASI1_src_enum); 128 129 static int tas2770_dac_event(struct snd_soc_dapm_widget *w, 130 struct snd_kcontrol *kcontrol, int event) 131 { 132 struct snd_soc_component *component = 133 snd_soc_dapm_to_component(w->dapm); 134 struct tas2770_priv *tas2770 = 135 snd_soc_component_get_drvdata(component); 136 int ret; 137 138 switch (event) { 139 case SND_SOC_DAPM_POST_PMU: 140 tas2770->dac_powered = 1; 141 ret = tas2770_update_pwr_ctrl(tas2770); 142 break; 143 case SND_SOC_DAPM_PRE_PMD: 144 tas2770->dac_powered = 0; 145 ret = tas2770_update_pwr_ctrl(tas2770); 146 break; 147 default: 148 dev_err(tas2770->dev, "Not supported evevt\n"); 149 return -EINVAL; 150 } 151 152 return ret; 153 } 154 155 static const struct snd_kcontrol_new isense_switch = 156 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 3, 1, 1); 157 static const struct snd_kcontrol_new vsense_switch = 158 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 2, 1, 1); 159 160 static int sense_event(struct snd_soc_dapm_widget *w, 161 struct snd_kcontrol *kcontrol, int event) 162 { 163 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 164 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component); 165 166 /* 167 * Powering up ISENSE/VSENSE requires a trip through the shutdown state. 168 * Do that here to ensure that our changes are applied properly, otherwise 169 * we might end up with non-functional IVSENSE if playback started earlier, 170 * which would break software speaker protection. 171 */ 172 switch (event) { 173 case SND_SOC_DAPM_PRE_REG: 174 return snd_soc_component_update_bits(component, TAS2770_PWR_CTRL, 175 TAS2770_PWR_CTRL_MASK, 176 TAS2770_PWR_CTRL_SHUTDOWN); 177 case SND_SOC_DAPM_POST_REG: 178 return tas2770_update_pwr_ctrl(tas2770); 179 default: 180 return 0; 181 } 182 } 183 184 static const struct snd_soc_dapm_widget tas2770_dapm_widgets[] = { 185 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0), 186 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2770_asi1_mux), 187 SND_SOC_DAPM_SWITCH_E("ISENSE", TAS2770_PWR_CTRL, 3, 1, &isense_switch, 188 sense_event, SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG), 189 SND_SOC_DAPM_SWITCH_E("VSENSE", TAS2770_PWR_CTRL, 2, 1, &vsense_switch, 190 sense_event, SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG), 191 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2770_dac_event, 192 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 193 SND_SOC_DAPM_OUTPUT("OUT"), 194 SND_SOC_DAPM_SIGGEN("VMON"), 195 SND_SOC_DAPM_SIGGEN("IMON") 196 }; 197 198 static const struct snd_soc_dapm_route tas2770_audio_map[] = { 199 {"ASI1 Sel", "I2C offset", "ASI1"}, 200 {"ASI1 Sel", "Left", "ASI1"}, 201 {"ASI1 Sel", "Right", "ASI1"}, 202 {"ASI1 Sel", "LeftRightDiv2", "ASI1"}, 203 {"DAC", NULL, "ASI1 Sel"}, 204 {"OUT", NULL, "DAC"}, 205 {"ISENSE", "Switch", "IMON"}, 206 {"VSENSE", "Switch", "VMON"}, 207 }; 208 209 static int tas2770_mute(struct snd_soc_dai *dai, int mute, int direction) 210 { 211 struct snd_soc_component *component = dai->component; 212 struct tas2770_priv *tas2770 = 213 snd_soc_component_get_drvdata(component); 214 215 tas2770->unmuted = !mute; 216 return tas2770_update_pwr_ctrl(tas2770); 217 } 218 219 static int tas2770_set_ivsense_transmit(struct tas2770_priv *tas2770, 220 int i_slot, int v_slot) 221 { 222 struct snd_soc_component *component = tas2770->component; 223 int ret; 224 225 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG5, 226 TAS2770_TDM_CFG_REG5_VSNS_MASK | 227 TAS2770_TDM_CFG_REG5_50_MASK, 228 TAS2770_TDM_CFG_REG5_VSNS_ENABLE | 229 v_slot); 230 if (ret < 0) 231 return ret; 232 233 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG6, 234 TAS2770_TDM_CFG_REG6_ISNS_MASK | 235 TAS2770_TDM_CFG_REG6_50_MASK, 236 TAS2770_TDM_CFG_REG6_ISNS_ENABLE | 237 i_slot); 238 if (ret < 0) 239 return ret; 240 241 return 0; 242 } 243 244 static int tas2770_set_pdm_transmit(struct tas2770_priv *tas2770, int slot) 245 { 246 struct snd_soc_component *component = tas2770->component; 247 int ret; 248 249 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG7, 250 TAS2770_TDM_CFG_REG7_PDM_MASK | 251 TAS2770_TDM_CFG_REG7_50_MASK, 252 TAS2770_TDM_CFG_REG7_PDM_ENABLE | 253 slot); 254 return ret; 255 } 256 257 static int tas2770_set_bitwidth(struct tas2770_priv *tas2770, int bitwidth) 258 { 259 int ret; 260 struct snd_soc_component *component = tas2770->component; 261 262 switch (bitwidth) { 263 case SNDRV_PCM_FORMAT_S16_LE: 264 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2, 265 TAS2770_TDM_CFG_REG2_RXW_MASK, 266 TAS2770_TDM_CFG_REG2_RXW_16BITS); 267 break; 268 case SNDRV_PCM_FORMAT_S24_LE: 269 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2, 270 TAS2770_TDM_CFG_REG2_RXW_MASK, 271 TAS2770_TDM_CFG_REG2_RXW_24BITS); 272 break; 273 case SNDRV_PCM_FORMAT_S32_LE: 274 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2, 275 TAS2770_TDM_CFG_REG2_RXW_MASK, 276 TAS2770_TDM_CFG_REG2_RXW_32BITS); 277 break; 278 279 default: 280 return -EINVAL; 281 } 282 283 if (ret < 0) 284 return ret; 285 286 return 0; 287 } 288 289 static int tas2770_set_samplerate(struct tas2770_priv *tas2770, int samplerate) 290 { 291 struct snd_soc_component *component = tas2770->component; 292 int ramp_rate_val; 293 int ret; 294 295 switch (samplerate) { 296 case 48000: 297 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ | 298 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ; 299 break; 300 case 44100: 301 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ | 302 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ; 303 break; 304 case 96000: 305 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ | 306 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ; 307 break; 308 case 88200: 309 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ | 310 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ; 311 break; 312 case 192000: 313 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ | 314 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ; 315 break; 316 case 176400: 317 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ | 318 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ; 319 break; 320 default: 321 return -EINVAL; 322 } 323 324 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0, 325 TAS2770_TDM_CFG_REG0_SMP_MASK | 326 TAS2770_TDM_CFG_REG0_31_MASK, 327 ramp_rate_val); 328 if (ret < 0) 329 return ret; 330 331 return 0; 332 } 333 334 static int tas2770_hw_params(struct snd_pcm_substream *substream, 335 struct snd_pcm_hw_params *params, 336 struct snd_soc_dai *dai) 337 { 338 struct snd_soc_component *component = dai->component; 339 struct tas2770_priv *tas2770 = 340 snd_soc_component_get_drvdata(component); 341 int ret; 342 343 ret = tas2770_set_bitwidth(tas2770, params_format(params)); 344 if (ret) 345 return ret; 346 347 return tas2770_set_samplerate(tas2770, params_rate(params)); 348 } 349 350 static int tas2770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 351 { 352 struct snd_soc_component *component = dai->component; 353 struct tas2770_priv *tas2770 = 354 snd_soc_component_get_drvdata(component); 355 u8 tdm_rx_start_slot = 0, invert_fpol = 0, fpol_preinv = 0, asi_cfg_1 = 0; 356 int ret; 357 358 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 359 case SND_SOC_DAIFMT_CBC_CFC: 360 break; 361 default: 362 dev_err(tas2770->dev, "ASI invalid DAI clocking\n"); 363 return -EINVAL; 364 } 365 366 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 367 case SND_SOC_DAIFMT_NB_IF: 368 invert_fpol = 1; 369 fallthrough; 370 case SND_SOC_DAIFMT_NB_NF: 371 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_RSING; 372 break; 373 case SND_SOC_DAIFMT_IB_IF: 374 invert_fpol = 1; 375 fallthrough; 376 case SND_SOC_DAIFMT_IB_NF: 377 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_FALING; 378 break; 379 default: 380 dev_err(tas2770->dev, "ASI format Inverse is not found\n"); 381 return -EINVAL; 382 } 383 384 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1, 385 TAS2770_TDM_CFG_REG1_RX_MASK, 386 asi_cfg_1); 387 if (ret < 0) 388 return ret; 389 390 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 391 case SND_SOC_DAIFMT_I2S: 392 tdm_rx_start_slot = 1; 393 fpol_preinv = 0; 394 break; 395 case SND_SOC_DAIFMT_DSP_A: 396 tdm_rx_start_slot = 0; 397 fpol_preinv = 1; 398 break; 399 case SND_SOC_DAIFMT_DSP_B: 400 tdm_rx_start_slot = 1; 401 fpol_preinv = 1; 402 break; 403 case SND_SOC_DAIFMT_LEFT_J: 404 tdm_rx_start_slot = 0; 405 fpol_preinv = 1; 406 break; 407 default: 408 dev_err(tas2770->dev, 409 "DAI Format is not found, fmt=0x%x\n", fmt); 410 return -EINVAL; 411 } 412 413 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1, 414 TAS2770_TDM_CFG_REG1_MASK, 415 (tdm_rx_start_slot << TAS2770_TDM_CFG_REG1_51_SHIFT)); 416 if (ret < 0) 417 return ret; 418 419 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0, 420 TAS2770_TDM_CFG_REG0_FPOL_MASK, 421 (fpol_preinv ^ invert_fpol) 422 ? TAS2770_TDM_CFG_REG0_FPOL_RSING 423 : TAS2770_TDM_CFG_REG0_FPOL_FALING); 424 if (ret < 0) 425 return ret; 426 427 return 0; 428 } 429 430 static int tas2770_set_dai_tdm_slot(struct snd_soc_dai *dai, 431 unsigned int tx_mask, 432 unsigned int rx_mask, 433 int slots, int slot_width) 434 { 435 struct snd_soc_component *component = dai->component; 436 int left_slot, right_slot; 437 int ret; 438 439 if (tx_mask == 0 || rx_mask != 0) 440 return -EINVAL; 441 442 left_slot = __ffs(tx_mask); 443 tx_mask &= ~(1 << left_slot); 444 if (tx_mask == 0) { 445 right_slot = left_slot; 446 } else { 447 right_slot = __ffs(tx_mask); 448 tx_mask &= ~(1 << right_slot); 449 } 450 451 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots) 452 return -EINVAL; 453 454 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3, 455 TAS2770_TDM_CFG_REG3_30_MASK, 456 (left_slot << TAS2770_TDM_CFG_REG3_30_SHIFT)); 457 if (ret < 0) 458 return ret; 459 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3, 460 TAS2770_TDM_CFG_REG3_RXS_MASK, 461 (right_slot << TAS2770_TDM_CFG_REG3_RXS_SHIFT)); 462 if (ret < 0) 463 return ret; 464 465 switch (slot_width) { 466 case 16: 467 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2, 468 TAS2770_TDM_CFG_REG2_RXS_MASK, 469 TAS2770_TDM_CFG_REG2_RXS_16BITS); 470 break; 471 case 24: 472 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2, 473 TAS2770_TDM_CFG_REG2_RXS_MASK, 474 TAS2770_TDM_CFG_REG2_RXS_24BITS); 475 break; 476 case 32: 477 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2, 478 TAS2770_TDM_CFG_REG2_RXS_MASK, 479 TAS2770_TDM_CFG_REG2_RXS_32BITS); 480 break; 481 case 0: 482 /* Do not change slot width */ 483 ret = 0; 484 break; 485 default: 486 ret = -EINVAL; 487 } 488 489 if (ret < 0) 490 return ret; 491 492 return 0; 493 } 494 495 static int tas2770_set_dai_tdm_idle(struct snd_soc_dai *dai, 496 unsigned int tx_mask, 497 unsigned int rx_mask, 498 int tx_mode, int rx_mode) 499 { 500 struct snd_soc_component *component = dai->component; 501 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component); 502 int ret; 503 504 /* We don't support setting anything for SDIN */ 505 if (rx_mode) 506 return -EOPNOTSUPP; 507 508 if (tas2770->idle_tx_mode == tx_mode) 509 return 0; 510 511 switch (tx_mode) { 512 case SND_SOC_DAI_TDM_IDLE_PULLDOWN: 513 ret = snd_soc_component_update_bits(component, TAS2770_DIN_PD, 514 TAS2770_DIN_PD_SDOUT, 515 TAS2770_DIN_PD_SDOUT); 516 if (ret) 517 return ret; 518 519 break; 520 case SND_SOC_DAI_TDM_IDLE_ZERO: 521 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG4, 522 TAS2770_TDM_CFG_REG4_TX_KEEPER, 523 TAS2770_TDM_CFG_REG4_TX_KEEPER); 524 if (ret) 525 return ret; 526 527 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG4, 528 TAS2770_TDM_CFG_REG4_TX_FILL, 0); 529 if (ret) 530 return ret; 531 532 break; 533 case SND_SOC_DAI_TDM_IDLE_HIZ: 534 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG4, 535 TAS2770_TDM_CFG_REG4_TX_KEEPER, 536 TAS2770_TDM_CFG_REG4_TX_KEEPER); 537 if (ret) 538 return ret; 539 540 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG4, 541 TAS2770_TDM_CFG_REG4_TX_FILL, 542 TAS2770_TDM_CFG_REG4_TX_FILL); 543 if (ret) 544 return ret; 545 546 break; 547 case SND_SOC_DAI_TDM_IDLE_OFF: 548 ret = snd_soc_component_update_bits(component, TAS2770_DIN_PD, 549 TAS2770_DIN_PD_SDOUT, 0); 550 if (ret) 551 return ret; 552 553 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG4, 554 TAS2770_TDM_CFG_REG4_TX_KEEPER, 0); 555 if (ret) 556 return ret; 557 558 break; 559 560 default: 561 return -EOPNOTSUPP; 562 } 563 564 tas2770->idle_tx_mode = tx_mode; 565 566 return 0; 567 } 568 569 static const struct snd_soc_dai_ops tas2770_dai_ops = { 570 .mute_stream = tas2770_mute, 571 .hw_params = tas2770_hw_params, 572 .set_fmt = tas2770_set_fmt, 573 .set_tdm_slot = tas2770_set_dai_tdm_slot, 574 .set_tdm_idle = tas2770_set_dai_tdm_idle, 575 .no_capture_mute = 1, 576 }; 577 578 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 579 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 580 581 #define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ 582 SNDRV_PCM_RATE_96000 |\ 583 SNDRV_PCM_RATE_192000\ 584 ) 585 586 static struct snd_soc_dai_driver tas2770_dai_driver[] = { 587 { 588 .name = "tas2770 ASI1", 589 .id = 0, 590 .playback = { 591 .stream_name = "ASI1 Playback", 592 .channels_min = 1, 593 .channels_max = 2, 594 .rates = TAS2770_RATES, 595 .formats = TAS2770_FORMATS, 596 }, 597 .capture = { 598 .stream_name = "ASI1 Capture", 599 .channels_min = 0, 600 .channels_max = 2, 601 .rates = TAS2770_RATES, 602 .formats = TAS2770_FORMATS, 603 }, 604 .ops = &tas2770_dai_ops, 605 .symmetric_rate = 1, 606 }, 607 }; 608 609 static int tas2770_read_die_temp(struct tas2770_priv *tas2770, long *result) 610 { 611 int ret = 0; 612 int reading, msb, lsb; 613 614 ret = regmap_read(tas2770->regmap, TAS2770_TEMP_MSB, &msb); 615 if (ret) 616 return ret; 617 618 ret = regmap_read(tas2770->regmap, TAS2770_TEMP_LSB, &lsb); 619 if (ret) 620 return ret; 621 622 reading = (msb << 4) | (lsb >> 4); 623 624 /* 625 * As per datasheet: divide register by 16 and subtract 93 to get 626 * degrees Celsius. hwmon requires millidegrees. Let's avoid rounding 627 * errors by subtracting 93 * 16 and scaling before dividing. 628 * 629 * NOTE: The ADC registers are initialised to 0 on reset. This means 630 * that the temperature will read -93 *C until the chip is brought out 631 * of software shutdown (e.g. the PCM it's attached to is opened). The 632 * ADC is also shut down in software shutdown/low-power mode, so the 633 * value read back from its registers will be the last value sampled 634 * before entering software shutdown. 635 */ 636 if (reading == 0) 637 return -ENODATA; 638 639 *result = (reading - (93 * 16)) * 1000 / 16; 640 return 0; 641 } 642 643 static int tas2770_hwmon_is_fault(struct tas2770_priv *tas2770, long *result) 644 { 645 int ret; 646 long temp; 647 648 ret = tas2770_read_die_temp(tas2770, &temp); 649 if (ret == -ENODATA) { 650 *result = true; 651 return 0; 652 } 653 654 return ret; 655 } 656 657 static umode_t tas2770_hwmon_is_visible(const void *data, 658 enum hwmon_sensor_types type, u32 attr, 659 int channel) 660 { 661 if (type != hwmon_temp) 662 return 0; 663 664 switch (attr) { 665 case hwmon_temp_input: 666 case hwmon_temp_fault: 667 return 0444; 668 default: 669 break; 670 } 671 672 return 0; 673 } 674 675 static int tas2770_hwmon_read(struct device *dev, 676 enum hwmon_sensor_types type, 677 u32 attr, int channel, long *val) 678 { 679 struct tas2770_priv *tas2770 = dev_get_drvdata(dev); 680 int ret; 681 682 switch (attr) { 683 case hwmon_temp_input: 684 ret = tas2770_read_die_temp(tas2770, val); 685 break; 686 case hwmon_temp_fault: 687 ret = tas2770_hwmon_is_fault(tas2770, val); 688 break; 689 default: 690 ret = -EOPNOTSUPP; 691 break; 692 } 693 694 return ret; 695 } 696 697 static const struct hwmon_channel_info *const tas2770_hwmon_info[] = { 698 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_FAULT), 699 NULL 700 }; 701 702 static const struct hwmon_ops tas2770_hwmon_ops = { 703 .is_visible = tas2770_hwmon_is_visible, 704 .read = tas2770_hwmon_read, 705 }; 706 707 static const struct hwmon_chip_info tas2770_hwmon_chip_info = { 708 .ops = &tas2770_hwmon_ops, 709 .info = tas2770_hwmon_info, 710 }; 711 712 static const struct regmap_config tas2770_i2c_regmap; 713 714 static int tas2770_codec_probe(struct snd_soc_component *component) 715 { 716 struct tas2770_priv *tas2770 = 717 snd_soc_component_get_drvdata(component); 718 int ret; 719 720 tas2770->component = component; 721 722 if (tas2770->sdz_gpio) { 723 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1); 724 usleep_range(1000, 2000); 725 } 726 727 tas2770_reset(tas2770); 728 regmap_reinit_cache(tas2770->regmap, &tas2770_i2c_regmap); 729 730 if (tas2770->i_sense_slot != -1 && tas2770->v_sense_slot != -1) { 731 ret = tas2770_set_ivsense_transmit(tas2770, tas2770->i_sense_slot, 732 tas2770->v_sense_slot); 733 734 if (ret < 0) 735 return ret; 736 } 737 738 if (tas2770->pdm_slot != -1) { 739 ret = tas2770_set_pdm_transmit(tas2770, tas2770->pdm_slot); 740 741 if (ret < 0) 742 return ret; 743 } 744 745 return 0; 746 } 747 748 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0); 749 static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -10050, 50, 0); 750 751 static const struct snd_kcontrol_new tas2770_snd_controls[] = { 752 SOC_SINGLE_TLV("Speaker Playback Volume", TAS2770_PLAY_CFG_REG2, 753 0, TAS2770_PLAY_CFG_REG2_VMAX, 1, tas2770_playback_volume), 754 SOC_SINGLE_TLV("Amp Gain Volume", TAS2770_PLAY_CFG_REG0, 0, 0x14, 0, 755 tas2770_digital_tlv), 756 }; 757 758 static const struct snd_soc_component_driver soc_component_driver_tas2770 = { 759 .probe = tas2770_codec_probe, 760 .suspend = tas2770_codec_suspend, 761 .resume = tas2770_codec_resume, 762 .controls = tas2770_snd_controls, 763 .num_controls = ARRAY_SIZE(tas2770_snd_controls), 764 .dapm_widgets = tas2770_dapm_widgets, 765 .num_dapm_widgets = ARRAY_SIZE(tas2770_dapm_widgets), 766 .dapm_routes = tas2770_audio_map, 767 .num_dapm_routes = ARRAY_SIZE(tas2770_audio_map), 768 .idle_bias_on = 1, 769 .endianness = 1, 770 }; 771 772 static int tas2770_register_codec(struct tas2770_priv *tas2770) 773 { 774 return devm_snd_soc_register_component(tas2770->dev, 775 &soc_component_driver_tas2770, 776 tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver)); 777 } 778 779 static const struct reg_default tas2770_reg_defaults[] = { 780 { TAS2770_PAGE, 0x00 }, 781 { TAS2770_SW_RST, 0x00 }, 782 { TAS2770_PWR_CTRL, 0x0e }, 783 { TAS2770_PLAY_CFG_REG0, 0x10 }, 784 { TAS2770_PLAY_CFG_REG1, 0x01 }, 785 { TAS2770_PLAY_CFG_REG2, 0x00 }, 786 { TAS2770_MSC_CFG_REG0, 0x07 }, 787 { TAS2770_TDM_CFG_REG1, 0x02 }, 788 { TAS2770_TDM_CFG_REG2, 0x0a }, 789 { TAS2770_TDM_CFG_REG3, 0x10 }, 790 { TAS2770_INT_MASK_REG0, 0xfc }, 791 { TAS2770_INT_MASK_REG1, 0xb1 }, 792 { TAS2770_INT_CFG, 0x05 }, 793 { TAS2770_MISC_IRQ, 0x81 }, 794 { TAS2770_CLK_CGF, 0x0c }, 795 796 }; 797 798 static bool tas2770_volatile(struct device *dev, unsigned int reg) 799 { 800 switch (reg) { 801 case TAS2770_PAGE: /* regmap implementation requires this */ 802 case TAS2770_SW_RST: /* always clears after write */ 803 case TAS2770_BO_PRV_REG0:/* has a self clearing bit */ 804 case TAS2770_LVE_INT_REG0: 805 case TAS2770_LVE_INT_REG1: 806 case TAS2770_LAT_INT_REG0:/* Sticky interrupt flags */ 807 case TAS2770_LAT_INT_REG1:/* Sticky interrupt flags */ 808 case TAS2770_VBAT_MSB: 809 case TAS2770_VBAT_LSB: 810 case TAS2770_TEMP_MSB: 811 case TAS2770_TEMP_LSB: 812 return true; 813 } 814 815 return false; 816 } 817 818 static bool tas2770_writeable(struct device *dev, unsigned int reg) 819 { 820 switch (reg) { 821 case TAS2770_LVE_INT_REG0: 822 case TAS2770_LVE_INT_REG1: 823 case TAS2770_LAT_INT_REG0: 824 case TAS2770_LAT_INT_REG1: 825 case TAS2770_VBAT_MSB: 826 case TAS2770_VBAT_LSB: 827 case TAS2770_TEMP_MSB: 828 case TAS2770_TEMP_LSB: 829 case TAS2770_TDM_CLK_DETC: 830 case TAS2770_REV_AND_GPID: 831 return false; 832 } 833 834 return true; 835 } 836 837 static const struct regmap_range_cfg tas2770_regmap_ranges[] = { 838 { 839 .range_min = 0, 840 .range_max = 1 * 128, 841 .selector_reg = TAS2770_PAGE, 842 .selector_mask = 0xff, 843 .selector_shift = 0, 844 .window_start = 0, 845 .window_len = 128, 846 }, 847 }; 848 849 static const struct regmap_config tas2770_i2c_regmap = { 850 .reg_bits = 8, 851 .val_bits = 8, 852 .writeable_reg = tas2770_writeable, 853 .volatile_reg = tas2770_volatile, 854 .reg_defaults = tas2770_reg_defaults, 855 .num_reg_defaults = ARRAY_SIZE(tas2770_reg_defaults), 856 .cache_type = REGCACHE_RBTREE, 857 .ranges = tas2770_regmap_ranges, 858 .num_ranges = ARRAY_SIZE(tas2770_regmap_ranges), 859 .max_register = 1 * 128, 860 }; 861 862 static int tas2770_parse_dt(struct device *dev, struct tas2770_priv *tas2770) 863 { 864 int rc = 0; 865 866 rc = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no", 867 &tas2770->i_sense_slot); 868 if (rc) { 869 dev_info(tas2770->dev, "Property %s is missing setting default slot\n", 870 "ti,imon-slot-no"); 871 872 tas2770->i_sense_slot = -1; 873 } 874 875 rc = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no", 876 &tas2770->v_sense_slot); 877 if (rc) { 878 dev_info(tas2770->dev, "Property %s is missing setting default slot\n", 879 "ti,vmon-slot-no"); 880 881 tas2770->v_sense_slot = -1; 882 } 883 884 rc = fwnode_property_read_u32(dev->fwnode, "ti,pdm-slot-no", 885 &tas2770->pdm_slot); 886 if (rc) 887 tas2770->pdm_slot = -1; 888 889 tas2770->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH); 890 if (IS_ERR(tas2770->sdz_gpio)) { 891 if (PTR_ERR(tas2770->sdz_gpio) == -EPROBE_DEFER) 892 return -EPROBE_DEFER; 893 894 tas2770->sdz_gpio = NULL; 895 } 896 897 return 0; 898 } 899 900 static int tas2770_i2c_probe(struct i2c_client *client) 901 { 902 struct tas2770_priv *tas2770; 903 int result; 904 905 tas2770 = devm_kzalloc(&client->dev, sizeof(struct tas2770_priv), 906 GFP_KERNEL); 907 if (!tas2770) 908 return -ENOMEM; 909 910 tas2770->dev = &client->dev; 911 i2c_set_clientdata(client, tas2770); 912 dev_set_drvdata(&client->dev, tas2770); 913 914 tas2770->regmap = devm_regmap_init_i2c(client, &tas2770_i2c_regmap); 915 if (IS_ERR(tas2770->regmap)) { 916 result = PTR_ERR(tas2770->regmap); 917 dev_err(&client->dev, "Failed to allocate register map: %d\n", 918 result); 919 return result; 920 } 921 922 if (client->dev.of_node) { 923 result = tas2770_parse_dt(&client->dev, tas2770); 924 if (result) { 925 dev_err(tas2770->dev, "%s: Failed to parse devicetree\n", 926 __func__); 927 return result; 928 } 929 } 930 931 tas2770->reset_gpio = devm_gpiod_get_optional(tas2770->dev, "reset", 932 GPIOD_OUT_HIGH); 933 if (IS_ERR(tas2770->reset_gpio)) { 934 if (PTR_ERR(tas2770->reset_gpio) == -EPROBE_DEFER) { 935 tas2770->reset_gpio = NULL; 936 return -EPROBE_DEFER; 937 } 938 } 939 940 if (IS_REACHABLE(CONFIG_HWMON)) { 941 struct device *hwmon; 942 943 hwmon = devm_hwmon_device_register_with_info(&client->dev, "tas2770", 944 tas2770, 945 &tas2770_hwmon_chip_info, 946 NULL); 947 if (IS_ERR(hwmon)) { 948 return dev_err_probe(&client->dev, PTR_ERR(hwmon), 949 "Failed to register temp sensor\n"); 950 } 951 } 952 953 result = tas2770_register_codec(tas2770); 954 if (result) 955 dev_err(tas2770->dev, "Register codec failed.\n"); 956 957 return result; 958 } 959 960 static const struct i2c_device_id tas2770_i2c_id[] = { 961 { "tas2770"}, 962 { } 963 }; 964 MODULE_DEVICE_TABLE(i2c, tas2770_i2c_id); 965 966 #if defined(CONFIG_OF) 967 static const struct of_device_id tas2770_of_match[] = { 968 { .compatible = "ti,tas2770" }, 969 {}, 970 }; 971 MODULE_DEVICE_TABLE(of, tas2770_of_match); 972 #endif 973 974 static struct i2c_driver tas2770_i2c_driver = { 975 .driver = { 976 .name = "tas2770", 977 .of_match_table = of_match_ptr(tas2770_of_match), 978 }, 979 .probe = tas2770_i2c_probe, 980 .id_table = tas2770_i2c_id, 981 }; 982 module_i2c_driver(tas2770_i2c_driver); 983 984 MODULE_AUTHOR("Shi Fu <shifu0704@thundersoft.com>"); 985 MODULE_DESCRIPTION("TAS2770 I2C Smart Amplifier driver"); 986 MODULE_LICENSE("GPL v2"); 987