1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Driver for the Texas Instruments TAS2562 CODEC 4 // Copyright (C) 2019 Texas Instruments Inc. 5 6 7 #include <linux/module.h> 8 #include <linux/errno.h> 9 #include <linux/device.h> 10 #include <linux/i2c.h> 11 #include <linux/regmap.h> 12 #include <linux/slab.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/regulator/consumer.h> 15 #include <linux/delay.h> 16 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/soc.h> 20 #include <sound/soc-dapm.h> 21 #include <sound/tlv.h> 22 23 #include "tas2562.h" 24 25 #define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\ 26 SNDRV_PCM_FORMAT_S32_LE) 27 28 /* DVC equation involves floating point math 29 * round(10^(volume in dB/20)*2^30) 30 * so create a lookup table for 2dB step 31 */ 32 static const unsigned int float_vol_db_lookup[] = { 33 0x00000d43, 0x000010b2, 0x00001505, 0x00001a67, 0x00002151, 34 0x000029f1, 0x000034cd, 0x00004279, 0x000053af, 0x0000695b, 35 0x000084a3, 0x0000a6fa, 0x0000d236, 0x000108a4, 0x00014d2a, 36 0x0001a36e, 0x00021008, 0x000298c0, 0x000344df, 0x00041d8f, 37 0x00052e5a, 0x000685c8, 0x00083621, 0x000a566d, 0x000d03a7, 38 0x0010624d, 0x0014a050, 0x0019f786, 0x0020b0bc, 0x0029279d, 39 0x0033cf8d, 0x004139d3, 0x00521d50, 0x00676044, 0x0082248a, 40 0x00a3d70a, 0x00ce4328, 0x0103ab3d, 0x0146e75d, 0x019b8c27, 41 0x02061b89, 0x028c423f, 0x03352529, 0x0409c2b0, 0x05156d68, 42 0x06666666, 0x080e9f96, 0x0a24b062, 0x0cc509ab, 0x10137987, 43 0x143d1362, 0x197a967f, 0x2013739e, 0x28619ae9, 0x32d64617, 44 0x40000000 45 }; 46 47 struct tas2562_data { 48 struct snd_soc_component *component; 49 struct gpio_desc *sdz_gpio; 50 struct regmap *regmap; 51 struct device *dev; 52 struct i2c_client *client; 53 int v_sense_slot; 54 int i_sense_slot; 55 int volume_lvl; 56 int model_id; 57 bool dac_powered; 58 bool unmuted; 59 }; 60 61 enum tas256x_model { 62 TAS2562, 63 TAS2564, 64 TAS2110, 65 }; 66 67 static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate) 68 { 69 int samp_rate; 70 int ramp_rate; 71 72 switch (samplerate) { 73 case 7350: 74 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 75 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ; 76 break; 77 case 8000: 78 ramp_rate = 0; 79 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ; 80 break; 81 case 14700: 82 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 83 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ; 84 break; 85 case 16000: 86 ramp_rate = 0; 87 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ; 88 break; 89 case 22050: 90 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 91 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ; 92 break; 93 case 24000: 94 ramp_rate = 0; 95 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ; 96 break; 97 case 29400: 98 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 99 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ; 100 break; 101 case 32000: 102 ramp_rate = 0; 103 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ; 104 break; 105 case 44100: 106 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 107 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ; 108 break; 109 case 48000: 110 ramp_rate = 0; 111 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ; 112 break; 113 case 88200: 114 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 115 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ; 116 break; 117 case 96000: 118 ramp_rate = 0; 119 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ; 120 break; 121 case 176400: 122 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1; 123 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ; 124 break; 125 case 192000: 126 ramp_rate = 0; 127 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ; 128 break; 129 default: 130 dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n", 131 __func__, samplerate); 132 return -EINVAL; 133 } 134 135 snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0, 136 TAS2562_TDM_CFG0_RAMPRATE_MASK, ramp_rate); 137 snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0, 138 TAS2562_TDM_CFG0_SAMPRATE_MASK, samp_rate); 139 140 return 0; 141 } 142 143 static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai, 144 unsigned int tx_mask, unsigned int rx_mask, 145 int slots, int slot_width) 146 { 147 struct snd_soc_component *component = dai->component; 148 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 149 int left_slot, right_slot; 150 int slots_cfg; 151 int ret; 152 153 if (!tx_mask) { 154 dev_err(component->dev, "tx masks must not be 0\n"); 155 return -EINVAL; 156 } 157 158 if (slots == 1) { 159 if (tx_mask != 1) 160 return -EINVAL; 161 162 left_slot = 0; 163 right_slot = 0; 164 } else { 165 left_slot = __ffs(tx_mask); 166 tx_mask &= ~(1 << left_slot); 167 if (tx_mask == 0) { 168 right_slot = left_slot; 169 } else { 170 right_slot = __ffs(tx_mask); 171 } 172 } 173 174 slots_cfg = (right_slot << TAS2562_RIGHT_SLOT_SHIFT) | left_slot; 175 176 ret = snd_soc_component_write(component, TAS2562_TDM_CFG3, slots_cfg); 177 if (ret < 0) 178 return ret; 179 180 switch (slot_width) { 181 case 16: 182 ret = snd_soc_component_update_bits(component, 183 TAS2562_TDM_CFG2, 184 TAS2562_TDM_CFG2_RXLEN_MASK, 185 TAS2562_TDM_CFG2_RXLEN_16B); 186 break; 187 case 24: 188 ret = snd_soc_component_update_bits(component, 189 TAS2562_TDM_CFG2, 190 TAS2562_TDM_CFG2_RXLEN_MASK, 191 TAS2562_TDM_CFG2_RXLEN_24B); 192 break; 193 case 32: 194 ret = snd_soc_component_update_bits(component, 195 TAS2562_TDM_CFG2, 196 TAS2562_TDM_CFG2_RXLEN_MASK, 197 TAS2562_TDM_CFG2_RXLEN_32B); 198 break; 199 200 case 0: 201 /* Do not change slot width */ 202 break; 203 default: 204 dev_err(tas2562->dev, "slot width not supported"); 205 ret = -EINVAL; 206 } 207 208 if (ret < 0) 209 return ret; 210 211 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG5, 212 TAS2562_TDM_CFG5_VSNS_SLOT_MASK, 213 tas2562->v_sense_slot); 214 if (ret < 0) 215 return ret; 216 217 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG6, 218 TAS2562_TDM_CFG6_ISNS_SLOT_MASK, 219 tas2562->i_sense_slot); 220 if (ret < 0) 221 return ret; 222 223 return 0; 224 } 225 226 static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth) 227 { 228 int ret; 229 int val; 230 int sense_en; 231 232 switch (bitwidth) { 233 case SNDRV_PCM_FORMAT_S16_LE: 234 snd_soc_component_update_bits(tas2562->component, 235 TAS2562_TDM_CFG2, 236 TAS2562_TDM_CFG2_RXWLEN_MASK, 237 TAS2562_TDM_CFG2_RXWLEN_16B); 238 break; 239 case SNDRV_PCM_FORMAT_S24_LE: 240 snd_soc_component_update_bits(tas2562->component, 241 TAS2562_TDM_CFG2, 242 TAS2562_TDM_CFG2_RXWLEN_MASK, 243 TAS2562_TDM_CFG2_RXWLEN_24B); 244 break; 245 case SNDRV_PCM_FORMAT_S32_LE: 246 snd_soc_component_update_bits(tas2562->component, 247 TAS2562_TDM_CFG2, 248 TAS2562_TDM_CFG2_RXWLEN_MASK, 249 TAS2562_TDM_CFG2_RXWLEN_32B); 250 break; 251 252 default: 253 dev_info(tas2562->dev, "Unsupported bitwidth format\n"); 254 return -EINVAL; 255 } 256 257 val = snd_soc_component_read(tas2562->component, TAS2562_PWR_CTRL); 258 if (val < 0) 259 return val; 260 261 if (val & (1 << TAS2562_VSENSE_POWER_EN)) 262 sense_en = 0; 263 else 264 sense_en = TAS2562_TDM_CFG5_VSNS_EN; 265 266 ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG5, 267 TAS2562_TDM_CFG5_VSNS_EN, sense_en); 268 if (ret < 0) 269 return ret; 270 271 if (val & (1 << TAS2562_ISENSE_POWER_EN)) 272 sense_en = 0; 273 else 274 sense_en = TAS2562_TDM_CFG6_ISNS_EN; 275 276 ret = snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG6, 277 TAS2562_TDM_CFG6_ISNS_EN, sense_en); 278 if (ret < 0) 279 return ret; 280 281 return 0; 282 } 283 284 static int tas2562_hw_params(struct snd_pcm_substream *substream, 285 struct snd_pcm_hw_params *params, 286 struct snd_soc_dai *dai) 287 { 288 struct snd_soc_component *component = dai->component; 289 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 290 int ret; 291 292 ret = tas2562_set_bitwidth(tas2562, params_format(params)); 293 if (ret) { 294 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret); 295 return ret; 296 } 297 298 ret = tas2562_set_samplerate(tas2562, params_rate(params)); 299 if (ret) 300 dev_err(tas2562->dev, "set sample rate failed, %d\n", ret); 301 302 return ret; 303 } 304 305 static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 306 { 307 struct snd_soc_component *component = dai->component; 308 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 309 u8 asi_cfg_1 = 0; 310 u8 tdm_rx_start_slot = 0; 311 int ret; 312 313 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 314 case SND_SOC_DAIFMT_NB_NF: 315 asi_cfg_1 = 0; 316 break; 317 case SND_SOC_DAIFMT_IB_NF: 318 asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING; 319 break; 320 default: 321 dev_err(tas2562->dev, "ASI format Inverse is not found\n"); 322 return -EINVAL; 323 } 324 325 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1, 326 TAS2562_TDM_CFG1_RX_EDGE_MASK, 327 asi_cfg_1); 328 if (ret < 0) { 329 dev_err(tas2562->dev, "Failed to set RX edge\n"); 330 return ret; 331 } 332 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 333 case SND_SOC_DAIFMT_LEFT_J: 334 case SND_SOC_DAIFMT_DSP_B: 335 tdm_rx_start_slot = 0; 336 break; 337 case SND_SOC_DAIFMT_I2S: 338 case SND_SOC_DAIFMT_DSP_A: 339 tdm_rx_start_slot = 1; 340 break; 341 default: 342 dev_err(tas2562->dev, 343 "DAI Format is not found, fmt=0x%x\n", fmt); 344 return -EINVAL; 345 } 346 347 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1, 348 TAS2562_RX_OFF_MASK, (tdm_rx_start_slot << 1)); 349 if (ret < 0) 350 return ret; 351 352 return 0; 353 } 354 355 static int tas2562_update_pwr_ctrl(struct tas2562_data *tas2562) 356 { 357 struct snd_soc_component *component = tas2562->component; 358 unsigned int val; 359 int ret; 360 361 if (tas2562->dac_powered) 362 val = tas2562->unmuted ? 363 TAS2562_ACTIVE : TAS2562_MUTE; 364 else 365 val = TAS2562_SHUTDOWN; 366 367 ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL, 368 TAS2562_MODE_MASK, val); 369 if (ret < 0) 370 return ret; 371 372 return 0; 373 } 374 375 static int tas2562_mute(struct snd_soc_dai *dai, int mute, int direction) 376 { 377 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(dai->component); 378 379 tas2562->unmuted = !mute; 380 return tas2562_update_pwr_ctrl(tas2562); 381 } 382 383 static int tas2562_codec_probe(struct snd_soc_component *component) 384 { 385 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 386 387 tas2562->component = component; 388 389 if (tas2562->sdz_gpio) 390 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1); 391 392 return 0; 393 } 394 395 #ifdef CONFIG_PM 396 static int tas2562_suspend(struct snd_soc_component *component) 397 { 398 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 399 400 regcache_cache_only(tas2562->regmap, true); 401 regcache_mark_dirty(tas2562->regmap); 402 403 if (tas2562->sdz_gpio) 404 gpiod_set_value_cansleep(tas2562->sdz_gpio, 0); 405 406 return 0; 407 } 408 409 static int tas2562_resume(struct snd_soc_component *component) 410 { 411 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 412 413 if (tas2562->sdz_gpio) 414 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1); 415 416 regcache_cache_only(tas2562->regmap, false); 417 418 return regcache_sync(tas2562->regmap); 419 } 420 #else 421 #define tas2562_suspend NULL 422 #define tas2562_resume NULL 423 #endif 424 425 static const char * const tas2562_ASI1_src[] = { 426 "I2C offset", "Left", "Right", "LeftRightDiv2", 427 }; 428 429 static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4, 430 tas2562_ASI1_src); 431 432 static const struct snd_kcontrol_new tas2562_asi1_mux = 433 SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum); 434 435 static int tas2562_dac_event(struct snd_soc_dapm_widget *w, 436 struct snd_kcontrol *kcontrol, int event) 437 { 438 struct snd_soc_component *component = 439 snd_soc_dapm_to_component(w->dapm); 440 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 441 int ret = 0; 442 443 switch (event) { 444 case SND_SOC_DAPM_POST_PMU: 445 tas2562->dac_powered = true; 446 ret = tas2562_update_pwr_ctrl(tas2562); 447 break; 448 case SND_SOC_DAPM_PRE_PMD: 449 tas2562->dac_powered = false; 450 ret = tas2562_update_pwr_ctrl(tas2562); 451 break; 452 default: 453 dev_err(tas2562->dev, "Not supported evevt\n"); 454 return -EINVAL; 455 } 456 457 return ret; 458 } 459 460 static int tas2562_volume_control_get(struct snd_kcontrol *kcontrol, 461 struct snd_ctl_elem_value *ucontrol) 462 { 463 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 464 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 465 466 ucontrol->value.integer.value[0] = tas2562->volume_lvl; 467 return 0; 468 } 469 470 static int tas2562_volume_control_put(struct snd_kcontrol *kcontrol, 471 struct snd_ctl_elem_value *ucontrol) 472 { 473 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 474 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component); 475 int ret, index; 476 u32 reg_val; 477 478 if (tas2562->volume_lvl == ucontrol->value.integer.value[0]) 479 return 0; 480 481 index = ucontrol->value.integer.value[0] / 2; 482 if (index < 0 || index >= ARRAY_SIZE(float_vol_db_lookup)) 483 return -EINVAL; 484 485 reg_val = float_vol_db_lookup[index]; 486 487 /* 488 * The device applies the 32-bit coefficient to the playback path on 489 * the write to DVC_CFG4 (the LSB, book 0 page 2 reg 0x0F), so the 490 * bytes must be written MSB first and DVC_CFG4 last. Writing CFG4 491 * first latches a mix of the previous coefficient's upper bytes and 492 * the new LSB instead of the requested value. 493 */ 494 ret = snd_soc_component_write(component, TAS2562_DVC_CFG1, 495 ((reg_val >> 24) & 0xff)); 496 if (ret) 497 return ret; 498 ret = snd_soc_component_write(component, TAS2562_DVC_CFG2, 499 ((reg_val >> 16) & 0xff)); 500 if (ret) 501 return ret; 502 ret = snd_soc_component_write(component, TAS2562_DVC_CFG3, 503 ((reg_val >> 8) & 0xff)); 504 if (ret) 505 return ret; 506 ret = snd_soc_component_write(component, TAS2562_DVC_CFG4, 507 (reg_val & 0xff)); 508 if (ret) 509 return ret; 510 511 tas2562->volume_lvl = ucontrol->value.integer.value[0]; 512 513 return 1; 514 } 515 516 /* Digital Volume Control. From 0 dB to -110 dB in 1 dB steps */ 517 static const DECLARE_TLV_DB_SCALE(dvc_tlv, -11000, 100, 0); 518 519 static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0); 520 521 static const struct snd_kcontrol_new isense_switch = 522 SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN, 523 1, 1); 524 525 static const struct snd_kcontrol_new vsense_switch = 526 SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN, 527 1, 1); 528 529 static const struct snd_kcontrol_new tas2562_snd_controls[] = { 530 SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 1, 0x1c, 0, 531 tas2562_dac_tlv), 532 SOC_SINGLE_EXT_TLV("Digital Volume Control", TAS2562_DVC_CFG1, 0, 110, 0, 533 tas2562_volume_control_get, tas2562_volume_control_put, 534 dvc_tlv), 535 }; 536 537 static const struct snd_soc_dapm_widget tas2110_dapm_widgets[] = { 538 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0), 539 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux), 540 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event, 541 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 542 SND_SOC_DAPM_OUTPUT("OUT"), 543 }; 544 545 static const struct snd_soc_dapm_route tas2110_audio_map[] = { 546 {"ASI1 Sel", "I2C offset", "ASI1"}, 547 {"ASI1 Sel", "Left", "ASI1"}, 548 {"ASI1 Sel", "Right", "ASI1"}, 549 {"ASI1 Sel", "LeftRightDiv2", "ASI1"}, 550 { "DAC", NULL, "ASI1 Sel" }, 551 { "OUT", NULL, "DAC" }, 552 }; 553 554 static const struct snd_soc_component_driver soc_component_dev_tas2110 = { 555 .probe = tas2562_codec_probe, 556 .suspend = tas2562_suspend, 557 .resume = tas2562_resume, 558 .controls = tas2562_snd_controls, 559 .num_controls = ARRAY_SIZE(tas2562_snd_controls), 560 .dapm_widgets = tas2110_dapm_widgets, 561 .num_dapm_widgets = ARRAY_SIZE(tas2110_dapm_widgets), 562 .dapm_routes = tas2110_audio_map, 563 .num_dapm_routes = ARRAY_SIZE(tas2110_audio_map), 564 .idle_bias_on = 1, 565 .use_pmdown_time = 1, 566 .endianness = 1, 567 }; 568 569 static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = { 570 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0), 571 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux), 572 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event, 573 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 574 SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch), 575 SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch), 576 SND_SOC_DAPM_SIGGEN("VMON"), 577 SND_SOC_DAPM_SIGGEN("IMON"), 578 SND_SOC_DAPM_OUTPUT("OUT"), 579 }; 580 581 static const struct snd_soc_dapm_route tas2562_audio_map[] = { 582 {"ASI1 Sel", "I2C offset", "ASI1"}, 583 {"ASI1 Sel", "Left", "ASI1"}, 584 {"ASI1 Sel", "Right", "ASI1"}, 585 {"ASI1 Sel", "LeftRightDiv2", "ASI1"}, 586 { "DAC", NULL, "ASI1 Sel" }, 587 { "OUT", NULL, "DAC" }, 588 {"ISENSE", "Switch", "IMON"}, 589 {"VSENSE", "Switch", "VMON"}, 590 }; 591 592 static const struct snd_soc_component_driver soc_component_dev_tas2562 = { 593 .probe = tas2562_codec_probe, 594 .suspend = tas2562_suspend, 595 .resume = tas2562_resume, 596 .controls = tas2562_snd_controls, 597 .num_controls = ARRAY_SIZE(tas2562_snd_controls), 598 .dapm_widgets = tas2562_dapm_widgets, 599 .num_dapm_widgets = ARRAY_SIZE(tas2562_dapm_widgets), 600 .dapm_routes = tas2562_audio_map, 601 .num_dapm_routes = ARRAY_SIZE(tas2562_audio_map), 602 .idle_bias_on = 1, 603 .use_pmdown_time = 1, 604 .endianness = 1, 605 }; 606 607 static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = { 608 .hw_params = tas2562_hw_params, 609 .set_fmt = tas2562_set_dai_fmt, 610 .set_tdm_slot = tas2562_set_dai_tdm_slot, 611 .mute_stream = tas2562_mute, 612 .no_capture_mute = 1, 613 }; 614 615 static struct snd_soc_dai_driver tas2562_dai[] = { 616 { 617 .name = "tas2562-amplifier", 618 .id = 0, 619 .playback = { 620 .stream_name = "ASI1 Playback", 621 .channels_min = 2, 622 .channels_max = 2, 623 .rates = SNDRV_PCM_RATE_8000_192000, 624 .formats = TAS2562_FORMATS, 625 }, 626 .capture = { 627 .stream_name = "ASI1 Capture", 628 .channels_min = 0, 629 .channels_max = 2, 630 .rates = SNDRV_PCM_RATE_8000_192000, 631 .formats = TAS2562_FORMATS, 632 }, 633 .ops = &tas2562_speaker_dai_ops, 634 }, 635 }; 636 637 static const struct regmap_range_cfg tas2562_ranges[] = { 638 { 639 .range_min = 0, 640 .range_max = 5 * 128, 641 .selector_reg = TAS2562_PAGE_CTRL, 642 .selector_mask = 0xff, 643 .selector_shift = 0, 644 .window_start = 0, 645 .window_len = 128, 646 }, 647 }; 648 649 static const struct reg_default tas2562_reg_defaults[] = { 650 { TAS2562_PAGE_CTRL, 0x00 }, 651 { TAS2562_SW_RESET, 0x00 }, 652 { TAS2562_PWR_CTRL, 0x0e }, 653 { TAS2562_PB_CFG1, 0x20 }, 654 { TAS2562_TDM_CFG0, 0x09 }, 655 { TAS2562_TDM_CFG1, 0x02 }, 656 { TAS2562_DVC_CFG1, 0x40 }, 657 { TAS2562_DVC_CFG2, 0x40 }, 658 { TAS2562_DVC_CFG3, 0x00 }, 659 { TAS2562_DVC_CFG4, 0x00 }, 660 }; 661 662 static const struct regmap_config tas2562_regmap_config = { 663 .reg_bits = 8, 664 .val_bits = 8, 665 666 .max_register = 5 * 128, 667 .cache_type = REGCACHE_RBTREE, 668 .reg_defaults = tas2562_reg_defaults, 669 .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults), 670 .ranges = tas2562_ranges, 671 .num_ranges = ARRAY_SIZE(tas2562_ranges), 672 }; 673 674 static int tas2562_parse_dt(struct tas2562_data *tas2562) 675 { 676 struct device *dev = tas2562->dev; 677 int ret = 0; 678 679 tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH); 680 if (IS_ERR(tas2562->sdz_gpio)) { 681 if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER) 682 return -EPROBE_DEFER; 683 684 tas2562->sdz_gpio = NULL; 685 } 686 687 /* 688 * The shut-down property is deprecated but needs to be checked for 689 * backwards compatibility. 690 */ 691 if (tas2562->sdz_gpio == NULL) { 692 tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down", 693 GPIOD_OUT_HIGH); 694 if (IS_ERR(tas2562->sdz_gpio)) { 695 if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER) 696 return -EPROBE_DEFER; 697 698 tas2562->sdz_gpio = NULL; 699 } 700 } 701 702 if (tas2562->model_id == TAS2110) 703 return ret; 704 705 ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no", 706 &tas2562->i_sense_slot); 707 if (ret) { 708 dev_err(dev, "Property %s is missing setting default slot\n", 709 "ti,imon-slot-no"); 710 tas2562->i_sense_slot = 0; 711 } 712 713 714 ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no", 715 &tas2562->v_sense_slot); 716 if (ret) { 717 dev_info(dev, "Property %s is missing setting default slot\n", 718 "ti,vmon-slot-no"); 719 tas2562->v_sense_slot = 2; 720 } 721 722 if (tas2562->v_sense_slot < tas2562->i_sense_slot) { 723 dev_err(dev, "Vsense slot must be greater than Isense slot\n"); 724 return -EINVAL; 725 } 726 727 return ret; 728 } 729 730 static const struct i2c_device_id tas2562_id[] = { 731 { .name = "tas2562", .driver_data = TAS2562 }, 732 { .name = "tas2564", .driver_data = TAS2564 }, 733 { .name = "tas2110", .driver_data = TAS2110 }, 734 { } 735 }; 736 MODULE_DEVICE_TABLE(i2c, tas2562_id); 737 738 static int tas2562_probe(struct i2c_client *client) 739 { 740 struct device *dev = &client->dev; 741 struct tas2562_data *data; 742 int ret; 743 744 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 745 if (!data) 746 return -ENOMEM; 747 748 data->client = client; 749 data->dev = &client->dev; 750 data->model_id = (uintptr_t)i2c_get_match_data(client); 751 /* Register default is 0x40400000, this is closest */ 752 data->volume_lvl = (ARRAY_SIZE(float_vol_db_lookup) - 1) * 2; 753 754 tas2562_parse_dt(data); 755 756 data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config); 757 if (IS_ERR(data->regmap)) { 758 ret = PTR_ERR(data->regmap); 759 dev_err(dev, "failed to allocate register map: %d\n", ret); 760 return ret; 761 } 762 763 dev_set_drvdata(&client->dev, data); 764 765 if (data->model_id == TAS2110) 766 return devm_snd_soc_register_component(dev, 767 &soc_component_dev_tas2110, 768 tas2562_dai, 769 ARRAY_SIZE(tas2562_dai)); 770 771 return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562, 772 tas2562_dai, 773 ARRAY_SIZE(tas2562_dai)); 774 775 } 776 777 #ifdef CONFIG_OF 778 static const struct of_device_id tas2562_of_match[] = { 779 { .compatible = "ti,tas2562", }, 780 { .compatible = "ti,tas2564", }, 781 { .compatible = "ti,tas2110", }, 782 { }, 783 }; 784 MODULE_DEVICE_TABLE(of, tas2562_of_match); 785 #endif 786 787 static struct i2c_driver tas2562_i2c_driver = { 788 .driver = { 789 .name = "tas2562", 790 .of_match_table = of_match_ptr(tas2562_of_match), 791 }, 792 .probe = tas2562_probe, 793 .id_table = tas2562_id, 794 }; 795 796 module_i2c_driver(tas2562_i2c_driver); 797 798 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); 799 MODULE_DESCRIPTION("TAS2562 Audio amplifier driver"); 800 MODULE_LICENSE("GPL"); 801