1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * tas5720.c - ALSA SoC Texas Instruments TAS5720 Mono Audio Amplifier 4 * 5 * Copyright (C)2015-2016 Texas Instruments Incorporated - https://www.ti.com 6 * 7 * Author: Andreas Dannenberg <dannenberg@ti.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/errno.h> 12 #include <linux/device.h> 13 #include <linux/i2c.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/regmap.h> 16 #include <linux/slab.h> 17 #include <linux/regulator/consumer.h> 18 #include <linux/delay.h> 19 20 #include <sound/pcm.h> 21 #include <sound/pcm_params.h> 22 #include <sound/soc.h> 23 #include <sound/soc-dapm.h> 24 #include <sound/tlv.h> 25 26 #include "tas5720.h" 27 28 /* Define how often to check (and clear) the fault status register (in ms) */ 29 #define TAS5720_FAULT_CHECK_INTERVAL 200 30 31 enum tas572x_type { 32 TAS5720, 33 TAS5722, 34 }; 35 36 static const char * const tas5720_supply_names[] = { 37 "dvdd", /* Digital power supply. Connect to 3.3-V supply. */ 38 "pvdd", /* Class-D amp and analog power supply (connected). */ 39 }; 40 41 #define TAS5720_NUM_SUPPLIES ARRAY_SIZE(tas5720_supply_names) 42 43 struct tas5720_data { 44 struct snd_soc_component *component; 45 struct regmap *regmap; 46 struct i2c_client *tas5720_client; 47 enum tas572x_type devtype; 48 struct regulator_bulk_data supplies[TAS5720_NUM_SUPPLIES]; 49 struct delayed_work fault_check_work; 50 unsigned int last_fault; 51 }; 52 53 static int tas5720_hw_params(struct snd_pcm_substream *substream, 54 struct snd_pcm_hw_params *params, 55 struct snd_soc_dai *dai) 56 { 57 struct snd_soc_component *component = dai->component; 58 unsigned int rate = params_rate(params); 59 bool ssz_ds; 60 int ret; 61 62 switch (rate) { 63 case 44100: 64 case 48000: 65 ssz_ds = false; 66 break; 67 case 88200: 68 case 96000: 69 ssz_ds = true; 70 break; 71 default: 72 dev_err(component->dev, "unsupported sample rate: %u\n", rate); 73 return -EINVAL; 74 } 75 76 ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL1_REG, 77 TAS5720_SSZ_DS, ssz_ds); 78 if (ret < 0) { 79 dev_err(component->dev, "error setting sample rate: %d\n", ret); 80 return ret; 81 } 82 83 return 0; 84 } 85 86 static int tas5720_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 87 { 88 struct snd_soc_component *component = dai->component; 89 u8 serial_format; 90 int ret; 91 92 if ((fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) { 93 dev_vdbg(component->dev, "DAI clocking invalid\n"); 94 return -EINVAL; 95 } 96 97 switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK | 98 SND_SOC_DAIFMT_INV_MASK)) { 99 case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF): 100 /* 1st data bit occur one BCLK cycle after the frame sync */ 101 serial_format = TAS5720_SAIF_I2S; 102 break; 103 case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF): 104 /* 105 * Note that although the TAS5720 does not have a dedicated DSP 106 * mode it doesn't care about the LRCLK duty cycle during TDM 107 * operation. Therefore we can use the device's I2S mode with 108 * its delaying of the 1st data bit to receive DSP_A formatted 109 * data. See device datasheet for additional details. 110 */ 111 serial_format = TAS5720_SAIF_I2S; 112 break; 113 case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF): 114 /* 115 * Similar to DSP_A, we can use the fact that the TAS5720 does 116 * not care about the LRCLK duty cycle during TDM to receive 117 * DSP_B formatted data in LEFTJ mode (no delaying of the 1st 118 * data bit). 119 */ 120 serial_format = TAS5720_SAIF_LEFTJ; 121 break; 122 case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF): 123 /* No delay after the frame sync */ 124 serial_format = TAS5720_SAIF_LEFTJ; 125 break; 126 default: 127 dev_vdbg(component->dev, "DAI Format is not found\n"); 128 return -EINVAL; 129 } 130 131 ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL1_REG, 132 TAS5720_SAIF_FORMAT_MASK, 133 serial_format); 134 if (ret < 0) { 135 dev_err(component->dev, "error setting SAIF format: %d\n", ret); 136 return ret; 137 } 138 139 return 0; 140 } 141 142 static int tas5720_set_dai_tdm_slot(struct snd_soc_dai *dai, 143 unsigned int tx_mask, unsigned int rx_mask, 144 int slots, int slot_width) 145 { 146 struct snd_soc_component *component = dai->component; 147 struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component); 148 unsigned int first_slot; 149 int ret; 150 151 if (!tx_mask) { 152 dev_err(component->dev, "tx masks must not be 0\n"); 153 return -EINVAL; 154 } 155 156 /* 157 * Determine the first slot that is being requested. We will only 158 * use the first slot that is found since the TAS5720 is a mono 159 * amplifier. 160 */ 161 first_slot = __ffs(tx_mask); 162 163 if (first_slot > 7) { 164 dev_err(component->dev, "slot selection out of bounds (%u)\n", 165 first_slot); 166 return -EINVAL; 167 } 168 169 /* Enable manual TDM slot selection (instead of I2C ID based) */ 170 ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL1_REG, 171 TAS5720_TDM_CFG_SRC, TAS5720_TDM_CFG_SRC); 172 if (ret < 0) 173 goto error_snd_soc_component_update_bits; 174 175 /* Configure the TDM slot to process audio from */ 176 ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL2_REG, 177 TAS5720_TDM_SLOT_SEL_MASK, first_slot); 178 if (ret < 0) 179 goto error_snd_soc_component_update_bits; 180 181 /* Configure TDM slot width. This is only applicable to TAS5722. */ 182 switch (tas5720->devtype) { 183 case TAS5722: 184 ret = snd_soc_component_update_bits(component, TAS5722_DIGITAL_CTRL2_REG, 185 TAS5722_TDM_SLOT_16B, 186 slot_width == 16 ? 187 TAS5722_TDM_SLOT_16B : 0); 188 if (ret < 0) 189 goto error_snd_soc_component_update_bits; 190 break; 191 default: 192 break; 193 } 194 195 return 0; 196 197 error_snd_soc_component_update_bits: 198 dev_err(component->dev, "error configuring TDM mode: %d\n", ret); 199 return ret; 200 } 201 202 static int tas5720_mute_soc_component(struct snd_soc_component *component, int mute) 203 { 204 int ret; 205 206 ret = snd_soc_component_update_bits(component, TAS5720_DIGITAL_CTRL2_REG, 207 TAS5720_MUTE, mute ? TAS5720_MUTE : 0); 208 if (ret < 0) { 209 dev_err(component->dev, "error (un-)muting device: %d\n", ret); 210 return ret; 211 } 212 213 return 0; 214 } 215 216 static int tas5720_mute(struct snd_soc_dai *dai, int mute, int direction) 217 { 218 return tas5720_mute_soc_component(dai->component, mute); 219 } 220 221 static void tas5720_fault_check_work(struct work_struct *work) 222 { 223 struct tas5720_data *tas5720 = container_of(work, struct tas5720_data, 224 fault_check_work.work); 225 struct device *dev = tas5720->component->dev; 226 unsigned int curr_fault; 227 int ret; 228 229 ret = regmap_read(tas5720->regmap, TAS5720_FAULT_REG, &curr_fault); 230 if (ret < 0) { 231 dev_err(dev, "failed to read FAULT register: %d\n", ret); 232 goto out; 233 } 234 235 /* Check/handle all errors except SAIF clock errors */ 236 curr_fault &= TAS5720_OCE | TAS5720_DCE | TAS5720_OTE; 237 238 /* 239 * Only flag errors once for a given occurrence. This is needed as 240 * the TAS5720 will take time clearing the fault condition internally 241 * during which we don't want to bombard the system with the same 242 * error message over and over. 243 */ 244 if ((curr_fault & TAS5720_OCE) && !(tas5720->last_fault & TAS5720_OCE)) 245 dev_crit(dev, "experienced an over current hardware fault\n"); 246 247 if ((curr_fault & TAS5720_DCE) && !(tas5720->last_fault & TAS5720_DCE)) 248 dev_crit(dev, "experienced a DC detection fault\n"); 249 250 if ((curr_fault & TAS5720_OTE) && !(tas5720->last_fault & TAS5720_OTE)) 251 dev_crit(dev, "experienced an over temperature fault\n"); 252 253 /* Store current fault value so we can detect any changes next time */ 254 tas5720->last_fault = curr_fault; 255 256 if (!curr_fault) 257 goto out; 258 259 /* 260 * Periodically toggle SDZ (shutdown bit) H->L->H to clear any latching 261 * faults as long as a fault condition persists. Always going through 262 * the full sequence no matter the first return value to minimizes 263 * chances for the device to end up in shutdown mode. 264 */ 265 ret = regmap_write_bits(tas5720->regmap, TAS5720_POWER_CTRL_REG, 266 TAS5720_SDZ, 0); 267 if (ret < 0) 268 dev_err(dev, "failed to write POWER_CTRL register: %d\n", ret); 269 270 ret = regmap_write_bits(tas5720->regmap, TAS5720_POWER_CTRL_REG, 271 TAS5720_SDZ, TAS5720_SDZ); 272 if (ret < 0) 273 dev_err(dev, "failed to write POWER_CTRL register: %d\n", ret); 274 275 out: 276 /* Schedule the next fault check at the specified interval */ 277 schedule_delayed_work(&tas5720->fault_check_work, 278 msecs_to_jiffies(TAS5720_FAULT_CHECK_INTERVAL)); 279 } 280 281 static int tas5720_codec_probe(struct snd_soc_component *component) 282 { 283 struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component); 284 unsigned int device_id, expected_device_id; 285 int ret; 286 287 tas5720->component = component; 288 289 ret = regulator_bulk_enable(ARRAY_SIZE(tas5720->supplies), 290 tas5720->supplies); 291 if (ret != 0) { 292 dev_err(component->dev, "failed to enable supplies: %d\n", ret); 293 return ret; 294 } 295 296 /* 297 * Take a liberal approach to checking the device ID to allow the 298 * driver to be used even if the device ID does not match, however 299 * issue a warning if there is a mismatch. 300 */ 301 ret = regmap_read(tas5720->regmap, TAS5720_DEVICE_ID_REG, &device_id); 302 if (ret < 0) { 303 dev_err(component->dev, "failed to read device ID register: %d\n", 304 ret); 305 goto probe_fail; 306 } 307 308 switch (tas5720->devtype) { 309 case TAS5720: 310 expected_device_id = TAS5720_DEVICE_ID; 311 break; 312 case TAS5722: 313 expected_device_id = TAS5722_DEVICE_ID; 314 break; 315 default: 316 dev_err(component->dev, "unexpected private driver data\n"); 317 return -EINVAL; 318 } 319 320 if (device_id != expected_device_id) 321 dev_warn(component->dev, "wrong device ID. expected: %u read: %u\n", 322 expected_device_id, device_id); 323 324 /* Set device to mute */ 325 ret = tas5720_mute_soc_component(component, 1); 326 if (ret < 0) 327 goto error_snd_soc_component_update_bits; 328 329 /* 330 * Enter shutdown mode - our default when not playing audio - to 331 * minimize current consumption. On the TAS5720 there is no real down 332 * side doing so as all device registers are preserved and the wakeup 333 * of the codec is rather quick which we do using a dapm widget. 334 */ 335 ret = snd_soc_component_update_bits(component, TAS5720_POWER_CTRL_REG, 336 TAS5720_SDZ, 0); 337 if (ret < 0) 338 goto error_snd_soc_component_update_bits; 339 340 INIT_DELAYED_WORK(&tas5720->fault_check_work, tas5720_fault_check_work); 341 342 return 0; 343 344 error_snd_soc_component_update_bits: 345 dev_err(component->dev, "error configuring device registers: %d\n", ret); 346 347 probe_fail: 348 regulator_bulk_disable(ARRAY_SIZE(tas5720->supplies), 349 tas5720->supplies); 350 return ret; 351 } 352 353 static void tas5720_codec_remove(struct snd_soc_component *component) 354 { 355 struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component); 356 int ret; 357 358 cancel_delayed_work_sync(&tas5720->fault_check_work); 359 360 ret = regulator_bulk_disable(ARRAY_SIZE(tas5720->supplies), 361 tas5720->supplies); 362 if (ret < 0) 363 dev_err(component->dev, "failed to disable supplies: %d\n", ret); 364 }; 365 366 static int tas5720_dac_event(struct snd_soc_dapm_widget *w, 367 struct snd_kcontrol *kcontrol, int event) 368 { 369 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 370 struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component); 371 int ret; 372 373 if (event & SND_SOC_DAPM_POST_PMU) { 374 /* Take TAS5720 out of shutdown mode */ 375 ret = snd_soc_component_update_bits(component, TAS5720_POWER_CTRL_REG, 376 TAS5720_SDZ, TAS5720_SDZ); 377 if (ret < 0) { 378 dev_err(component->dev, "error waking component: %d\n", ret); 379 return ret; 380 } 381 382 /* 383 * Observe codec shutdown-to-active time. The datasheet only 384 * lists a nominal value however just use-it as-is without 385 * additional padding to minimize the delay introduced in 386 * starting to play audio (actually there is other setup done 387 * by the ASoC framework that will provide additional delays, 388 * so we should always be safe). 389 */ 390 msleep(25); 391 392 /* Turn on TAS5720 periodic fault checking/handling */ 393 tas5720->last_fault = 0; 394 schedule_delayed_work(&tas5720->fault_check_work, 395 msecs_to_jiffies(TAS5720_FAULT_CHECK_INTERVAL)); 396 } else if (event & SND_SOC_DAPM_PRE_PMD) { 397 /* Disable TAS5720 periodic fault checking/handling */ 398 cancel_delayed_work_sync(&tas5720->fault_check_work); 399 400 /* Place TAS5720 in shutdown mode to minimize current draw */ 401 ret = snd_soc_component_update_bits(component, TAS5720_POWER_CTRL_REG, 402 TAS5720_SDZ, 0); 403 if (ret < 0) { 404 dev_err(component->dev, "error shutting down component: %d\n", 405 ret); 406 return ret; 407 } 408 } 409 410 return 0; 411 } 412 413 #ifdef CONFIG_PM 414 static int tas5720_suspend(struct snd_soc_component *component) 415 { 416 struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component); 417 int ret; 418 419 regcache_cache_only(tas5720->regmap, true); 420 regcache_mark_dirty(tas5720->regmap); 421 422 ret = regulator_bulk_disable(ARRAY_SIZE(tas5720->supplies), 423 tas5720->supplies); 424 if (ret < 0) 425 dev_err(component->dev, "failed to disable supplies: %d\n", ret); 426 427 return ret; 428 } 429 430 static int tas5720_resume(struct snd_soc_component *component) 431 { 432 struct tas5720_data *tas5720 = snd_soc_component_get_drvdata(component); 433 int ret; 434 435 ret = regulator_bulk_enable(ARRAY_SIZE(tas5720->supplies), 436 tas5720->supplies); 437 if (ret < 0) { 438 dev_err(component->dev, "failed to enable supplies: %d\n", ret); 439 return ret; 440 } 441 442 regcache_cache_only(tas5720->regmap, false); 443 444 ret = regcache_sync(tas5720->regmap); 445 if (ret < 0) { 446 dev_err(component->dev, "failed to sync regcache: %d\n", ret); 447 return ret; 448 } 449 450 return 0; 451 } 452 #else 453 #define tas5720_suspend NULL 454 #define tas5720_resume NULL 455 #endif 456 457 static bool tas5720_is_volatile_reg(struct device *dev, unsigned int reg) 458 { 459 switch (reg) { 460 case TAS5720_DEVICE_ID_REG: 461 case TAS5720_FAULT_REG: 462 return true; 463 default: 464 return false; 465 } 466 } 467 468 static const struct regmap_config tas5720_regmap_config = { 469 .reg_bits = 8, 470 .val_bits = 8, 471 472 .max_register = TAS5720_MAX_REG, 473 .cache_type = REGCACHE_RBTREE, 474 .volatile_reg = tas5720_is_volatile_reg, 475 }; 476 477 static const struct regmap_config tas5722_regmap_config = { 478 .reg_bits = 8, 479 .val_bits = 8, 480 481 .max_register = TAS5722_MAX_REG, 482 .cache_type = REGCACHE_RBTREE, 483 .volatile_reg = tas5720_is_volatile_reg, 484 }; 485 486 /* 487 * DAC analog gain. There are four discrete values to select from, ranging 488 * from 19.2 dB to 26.3dB. 489 */ 490 static const DECLARE_TLV_DB_RANGE(dac_analog_tlv, 491 0x0, 0x0, TLV_DB_SCALE_ITEM(1920, 0, 0), 492 0x1, 0x1, TLV_DB_SCALE_ITEM(2070, 0, 0), 493 0x2, 0x2, TLV_DB_SCALE_ITEM(2350, 0, 0), 494 0x3, 0x3, TLV_DB_SCALE_ITEM(2630, 0, 0), 495 ); 496 497 /* 498 * DAC digital volumes. From -103.5 to 24 dB in 0.5 dB or 0.25 dB steps 499 * depending on the device. Note that setting the gain below -100 dB 500 * (register value <0x7) is effectively a MUTE as per device datasheet. 501 * 502 * Note that for the TAS5722 the digital volume controls are actually split 503 * over two registers, so we need custom getters/setters for access. 504 */ 505 static DECLARE_TLV_DB_SCALE(tas5720_dac_tlv, -10350, 50, 0); 506 static DECLARE_TLV_DB_SCALE(tas5722_dac_tlv, -10350, 25, 0); 507 508 static int tas5722_volume_get(struct snd_kcontrol *kcontrol, 509 struct snd_ctl_elem_value *ucontrol) 510 { 511 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 512 unsigned int val; 513 514 val = snd_soc_component_read(component, TAS5720_VOLUME_CTRL_REG); 515 ucontrol->value.integer.value[0] = val << 1; 516 517 val = snd_soc_component_read(component, TAS5722_DIGITAL_CTRL2_REG); 518 ucontrol->value.integer.value[0] |= val & TAS5722_VOL_CONTROL_LSB; 519 520 return 0; 521 } 522 523 static int tas5722_volume_set(struct snd_kcontrol *kcontrol, 524 struct snd_ctl_elem_value *ucontrol) 525 { 526 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 527 unsigned int sel = ucontrol->value.integer.value[0]; 528 529 snd_soc_component_write(component, TAS5720_VOLUME_CTRL_REG, sel >> 1); 530 snd_soc_component_update_bits(component, TAS5722_DIGITAL_CTRL2_REG, 531 TAS5722_VOL_CONTROL_LSB, sel); 532 533 return 0; 534 } 535 536 static const struct snd_kcontrol_new tas5720_snd_controls[] = { 537 SOC_SINGLE_TLV("Speaker Driver Playback Volume", 538 TAS5720_VOLUME_CTRL_REG, 0, 0xff, 0, tas5720_dac_tlv), 539 SOC_SINGLE_TLV("Speaker Driver Analog Gain", TAS5720_ANALOG_CTRL_REG, 540 TAS5720_ANALOG_GAIN_SHIFT, 3, 0, dac_analog_tlv), 541 }; 542 543 static const struct snd_kcontrol_new tas5722_snd_controls[] = { 544 SOC_SINGLE_EXT_TLV("Speaker Driver Playback Volume", 545 0, 0, 511, 0, 546 tas5722_volume_get, tas5722_volume_set, 547 tas5722_dac_tlv), 548 SOC_SINGLE_TLV("Speaker Driver Analog Gain", TAS5720_ANALOG_CTRL_REG, 549 TAS5720_ANALOG_GAIN_SHIFT, 3, 0, dac_analog_tlv), 550 }; 551 552 static const struct snd_soc_dapm_widget tas5720_dapm_widgets[] = { 553 SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0), 554 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas5720_dac_event, 555 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 556 SND_SOC_DAPM_OUTPUT("OUT") 557 }; 558 559 static const struct snd_soc_dapm_route tas5720_audio_map[] = { 560 { "DAC", NULL, "DAC IN" }, 561 { "OUT", NULL, "DAC" }, 562 }; 563 564 static const struct snd_soc_component_driver soc_component_dev_tas5720 = { 565 .probe = tas5720_codec_probe, 566 .remove = tas5720_codec_remove, 567 .suspend = tas5720_suspend, 568 .resume = tas5720_resume, 569 .controls = tas5720_snd_controls, 570 .num_controls = ARRAY_SIZE(tas5720_snd_controls), 571 .dapm_widgets = tas5720_dapm_widgets, 572 .num_dapm_widgets = ARRAY_SIZE(tas5720_dapm_widgets), 573 .dapm_routes = tas5720_audio_map, 574 .num_dapm_routes = ARRAY_SIZE(tas5720_audio_map), 575 .idle_bias_on = 1, 576 .use_pmdown_time = 1, 577 .endianness = 1, 578 }; 579 580 static const struct snd_soc_component_driver soc_component_dev_tas5722 = { 581 .probe = tas5720_codec_probe, 582 .remove = tas5720_codec_remove, 583 .suspend = tas5720_suspend, 584 .resume = tas5720_resume, 585 .controls = tas5722_snd_controls, 586 .num_controls = ARRAY_SIZE(tas5722_snd_controls), 587 .dapm_widgets = tas5720_dapm_widgets, 588 .num_dapm_widgets = ARRAY_SIZE(tas5720_dapm_widgets), 589 .dapm_routes = tas5720_audio_map, 590 .num_dapm_routes = ARRAY_SIZE(tas5720_audio_map), 591 .idle_bias_on = 1, 592 .use_pmdown_time = 1, 593 .endianness = 1, 594 }; 595 596 /* PCM rates supported by the TAS5720 driver */ 597 #define TAS5720_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ 598 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) 599 600 /* Formats supported by TAS5720 driver */ 601 #define TAS5720_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE |\ 602 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE) 603 604 static const struct snd_soc_dai_ops tas5720_speaker_dai_ops = { 605 .hw_params = tas5720_hw_params, 606 .set_fmt = tas5720_set_dai_fmt, 607 .set_tdm_slot = tas5720_set_dai_tdm_slot, 608 .mute_stream = tas5720_mute, 609 .no_capture_mute = 1, 610 }; 611 612 /* 613 * TAS5720 DAI structure 614 * 615 * Note that were are advertising .playback.channels_max = 2 despite this being 616 * a mono amplifier. The reason for that is that some serial ports such as TI's 617 * McASP module have a minimum number of channels (2) that they can output. 618 * Advertising more channels than we have will allow us to interface with such 619 * a serial port without really any negative side effects as the TAS5720 will 620 * simply ignore any extra channel(s) asides from the one channel that is 621 * configured to be played back. 622 */ 623 static struct snd_soc_dai_driver tas5720_dai[] = { 624 { 625 .name = "tas5720-amplifier", 626 .playback = { 627 .stream_name = "Playback", 628 .channels_min = 1, 629 .channels_max = 2, 630 .rates = TAS5720_RATES, 631 .formats = TAS5720_FORMATS, 632 }, 633 .ops = &tas5720_speaker_dai_ops, 634 }, 635 }; 636 637 static const struct i2c_device_id tas5720_id[] = { 638 { "tas5720", TAS5720 }, 639 { "tas5722", TAS5722 }, 640 { } 641 }; 642 MODULE_DEVICE_TABLE(i2c, tas5720_id); 643 644 static int tas5720_probe(struct i2c_client *client) 645 { 646 struct device *dev = &client->dev; 647 struct tas5720_data *data; 648 const struct regmap_config *regmap_config; 649 const struct i2c_device_id *id; 650 int ret; 651 int i; 652 653 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 654 if (!data) 655 return -ENOMEM; 656 657 id = i2c_match_id(tas5720_id, client); 658 data->tas5720_client = client; 659 data->devtype = id->driver_data; 660 661 switch (id->driver_data) { 662 case TAS5720: 663 regmap_config = &tas5720_regmap_config; 664 break; 665 case TAS5722: 666 regmap_config = &tas5722_regmap_config; 667 break; 668 default: 669 dev_err(dev, "unexpected private driver data\n"); 670 return -EINVAL; 671 } 672 data->regmap = devm_regmap_init_i2c(client, regmap_config); 673 if (IS_ERR(data->regmap)) { 674 ret = PTR_ERR(data->regmap); 675 dev_err(dev, "failed to allocate register map: %d\n", ret); 676 return ret; 677 } 678 679 for (i = 0; i < ARRAY_SIZE(data->supplies); i++) 680 data->supplies[i].supply = tas5720_supply_names[i]; 681 682 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies), 683 data->supplies); 684 if (ret != 0) { 685 dev_err(dev, "failed to request supplies: %d\n", ret); 686 return ret; 687 } 688 689 dev_set_drvdata(dev, data); 690 691 switch (id->driver_data) { 692 case TAS5720: 693 ret = devm_snd_soc_register_component(&client->dev, 694 &soc_component_dev_tas5720, 695 tas5720_dai, 696 ARRAY_SIZE(tas5720_dai)); 697 break; 698 case TAS5722: 699 ret = devm_snd_soc_register_component(&client->dev, 700 &soc_component_dev_tas5722, 701 tas5720_dai, 702 ARRAY_SIZE(tas5720_dai)); 703 break; 704 default: 705 dev_err(dev, "unexpected private driver data\n"); 706 return -EINVAL; 707 } 708 if (ret < 0) { 709 dev_err(dev, "failed to register component: %d\n", ret); 710 return ret; 711 } 712 713 return 0; 714 } 715 716 #if IS_ENABLED(CONFIG_OF) 717 static const struct of_device_id tas5720_of_match[] = { 718 { .compatible = "ti,tas5720", }, 719 { .compatible = "ti,tas5722", }, 720 { }, 721 }; 722 MODULE_DEVICE_TABLE(of, tas5720_of_match); 723 #endif 724 725 static struct i2c_driver tas5720_i2c_driver = { 726 .driver = { 727 .name = "tas5720", 728 .of_match_table = of_match_ptr(tas5720_of_match), 729 }, 730 .probe_new = tas5720_probe, 731 .id_table = tas5720_id, 732 }; 733 734 module_i2c_driver(tas5720_i2c_driver); 735 736 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>"); 737 MODULE_DESCRIPTION("TAS5720 Audio amplifier driver"); 738 MODULE_LICENSE("GPL"); 739