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