1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * tas2552.c - ALSA SoC Texas Instruments TAS2552 Mono Audio Amplifier 4 * 5 * Copyright (C) 2014 - 2024 Texas Instruments Incorporated - 6 * https://www.ti.com 7 * 8 * Author: Dan Murphy <dmurphy@ti.com> 9 */ 10 11 #include <linux/module.h> 12 #include <linux/errno.h> 13 #include <linux/device.h> 14 #include <linux/i2c.h> 15 #include <linux/gpio.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/regmap.h> 18 #include <linux/slab.h> 19 20 #include <linux/gpio/consumer.h> 21 #include <linux/regulator/consumer.h> 22 23 #include <sound/pcm.h> 24 #include <sound/pcm_params.h> 25 #include <sound/soc.h> 26 #include <sound/soc-dapm.h> 27 #include <sound/tlv.h> 28 #include <sound/tas2552-plat.h> 29 #include <dt-bindings/sound/tas2552.h> 30 31 #include "tas2552.h" 32 33 static const struct reg_default tas2552_reg_defs[] = { 34 {TAS2552_CFG_1, 0x22}, 35 {TAS2552_CFG_3, 0x80}, 36 {TAS2552_DOUT, 0x00}, 37 {TAS2552_OUTPUT_DATA, 0xc0}, 38 {TAS2552_PDM_CFG, 0x01}, 39 {TAS2552_PGA_GAIN, 0x00}, 40 {TAS2552_BOOST_APT_CTRL, 0x0f}, 41 {TAS2552_RESERVED_0D, 0xbe}, 42 {TAS2552_LIMIT_RATE_HYS, 0x08}, 43 {TAS2552_CFG_2, 0xef}, 44 {TAS2552_SER_CTRL_1, 0x00}, 45 {TAS2552_SER_CTRL_2, 0x00}, 46 {TAS2552_PLL_CTRL_1, 0x10}, 47 {TAS2552_PLL_CTRL_2, 0x00}, 48 {TAS2552_PLL_CTRL_3, 0x00}, 49 {TAS2552_BTIP, 0x8f}, 50 {TAS2552_BTS_CTRL, 0x80}, 51 {TAS2552_LIMIT_RELEASE, 0x04}, 52 {TAS2552_LIMIT_INT_COUNT, 0x00}, 53 {TAS2552_EDGE_RATE_CTRL, 0x40}, 54 {TAS2552_VBAT_DATA, 0x00}, 55 }; 56 57 #define TAS2552_NUM_SUPPLIES 3 58 static const char *tas2552_supply_names[TAS2552_NUM_SUPPLIES] = { 59 "vbat", /* vbat voltage */ 60 "iovdd", /* I/O Voltage */ 61 "avdd", /* Analog DAC Voltage */ 62 }; 63 64 struct tas2552_data { 65 struct snd_soc_component *component; 66 struct regmap *regmap; 67 struct i2c_client *tas2552_client; 68 struct regulator_bulk_data supplies[TAS2552_NUM_SUPPLIES]; 69 struct gpio_desc *enable_gpio; 70 unsigned char regs[TAS2552_VBAT_DATA]; 71 unsigned int pll_clkin; 72 int pll_clk_id; 73 unsigned int pdm_clk; 74 int pdm_clk_id; 75 76 unsigned int dai_fmt; 77 unsigned int tdm_delay; 78 }; 79 80 static int tas2552_post_event(struct snd_soc_dapm_widget *w, 81 struct snd_kcontrol *kcontrol, int event) 82 { 83 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 84 85 switch (event) { 86 case SND_SOC_DAPM_POST_PMU: 87 snd_soc_component_write(component, TAS2552_RESERVED_0D, 0xc0); 88 snd_soc_component_update_bits(component, TAS2552_LIMIT_RATE_HYS, (1 << 5), 89 (1 << 5)); 90 snd_soc_component_update_bits(component, TAS2552_CFG_2, 1, 0); 91 snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_SWS, 0); 92 break; 93 case SND_SOC_DAPM_POST_PMD: 94 snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_SWS, 95 TAS2552_SWS); 96 snd_soc_component_update_bits(component, TAS2552_CFG_2, 1, 1); 97 snd_soc_component_update_bits(component, TAS2552_LIMIT_RATE_HYS, (1 << 5), 0); 98 snd_soc_component_write(component, TAS2552_RESERVED_0D, 0xbe); 99 break; 100 } 101 return 0; 102 } 103 104 /* Input mux controls */ 105 static const char * const tas2552_input_texts[] = { 106 "Digital", "Analog" }; 107 static SOC_ENUM_SINGLE_DECL(tas2552_input_mux_enum, TAS2552_CFG_3, 7, 108 tas2552_input_texts); 109 110 static const struct snd_kcontrol_new tas2552_input_mux_control = 111 SOC_DAPM_ENUM("Route", tas2552_input_mux_enum); 112 113 static const struct snd_soc_dapm_widget tas2552_dapm_widgets[] = 114 { 115 SND_SOC_DAPM_INPUT("IN"), 116 117 /* MUX Controls */ 118 SND_SOC_DAPM_MUX("Input selection", SND_SOC_NOPM, 0, 0, 119 &tas2552_input_mux_control), 120 121 SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0), 122 SND_SOC_DAPM_AIF_OUT("ASI OUT", "DAC Capture", 0, SND_SOC_NOPM, 0, 0), 123 SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0), 124 SND_SOC_DAPM_OUT_DRV("ClassD", TAS2552_CFG_2, 7, 0, NULL, 0), 125 SND_SOC_DAPM_SUPPLY("PLL", TAS2552_CFG_2, 3, 0, NULL, 0), 126 SND_SOC_DAPM_POST("Post Event", tas2552_post_event), 127 128 SND_SOC_DAPM_OUTPUT("OUT"), 129 SND_SOC_DAPM_INPUT("DMIC") 130 }; 131 132 static const struct snd_soc_dapm_route tas2552_audio_map[] = { 133 {"DAC", NULL, "DAC IN"}, 134 {"Input selection", "Digital", "DAC"}, 135 {"Input selection", "Analog", "IN"}, 136 {"ClassD", NULL, "Input selection"}, 137 {"OUT", NULL, "ClassD"}, 138 {"ClassD", NULL, "PLL"}, 139 {"ASI OUT", NULL, "DMIC"} 140 }; 141 142 #ifdef CONFIG_PM 143 static void tas2552_sw_shutdown(struct tas2552_data *tas2552, int sw_shutdown) 144 { 145 u8 cfg1_reg = 0; 146 147 if (!tas2552->component) 148 return; 149 150 if (sw_shutdown) 151 cfg1_reg = TAS2552_SWS; 152 153 snd_soc_component_update_bits(tas2552->component, TAS2552_CFG_1, TAS2552_SWS, 154 cfg1_reg); 155 } 156 #endif 157 158 static int tas2552_setup_pll(struct snd_soc_component *component, 159 struct snd_pcm_hw_params *params) 160 { 161 struct tas2552_data *tas2552 = dev_get_drvdata(component->dev); 162 bool bypass_pll = false; 163 unsigned int pll_clk = params_rate(params) * 512; 164 unsigned int pll_clkin = tas2552->pll_clkin; 165 u8 pll_enable; 166 167 if (!pll_clkin) { 168 if (tas2552->pll_clk_id != TAS2552_PLL_CLKIN_BCLK) 169 return -EINVAL; 170 171 pll_clkin = snd_soc_params_to_bclk(params); 172 pll_clkin += tas2552->tdm_delay; 173 } 174 175 pll_enable = snd_soc_component_read(component, TAS2552_CFG_2) & TAS2552_PLL_ENABLE; 176 snd_soc_component_update_bits(component, TAS2552_CFG_2, TAS2552_PLL_ENABLE, 0); 177 178 if (pll_clkin == pll_clk) 179 bypass_pll = true; 180 181 if (bypass_pll) { 182 /* By pass the PLL configuration */ 183 snd_soc_component_update_bits(component, TAS2552_PLL_CTRL_2, 184 TAS2552_PLL_BYPASS, TAS2552_PLL_BYPASS); 185 } else { 186 /* Fill in the PLL control registers for J & D 187 * pll_clk = (.5 * pll_clkin * J.D) / 2^p 188 * Need to fill in J and D here based on incoming freq 189 */ 190 unsigned int d, q, t; 191 u8 j; 192 u8 pll_sel = (tas2552->pll_clk_id << 3) & TAS2552_PLL_SRC_MASK; 193 u8 p = snd_soc_component_read(component, TAS2552_PLL_CTRL_1); 194 195 p = (p >> 7); 196 197 recalc: 198 t = (pll_clk * 2) << p; 199 j = t / pll_clkin; 200 d = t % pll_clkin; 201 t = pll_clkin / 10000; 202 q = d / (t + 1); 203 d = q + ((9999 - pll_clkin % 10000) * (d / t - q)) / 10000; 204 205 if (d && (pll_clkin < 512000 || pll_clkin > 9200000)) { 206 if (tas2552->pll_clk_id == TAS2552_PLL_CLKIN_BCLK) { 207 pll_clkin = 1800000; 208 pll_sel = (TAS2552_PLL_CLKIN_1_8_FIXED << 3) & 209 TAS2552_PLL_SRC_MASK; 210 } else { 211 pll_clkin = snd_soc_params_to_bclk(params); 212 pll_clkin += tas2552->tdm_delay; 213 pll_sel = (TAS2552_PLL_CLKIN_BCLK << 3) & 214 TAS2552_PLL_SRC_MASK; 215 } 216 goto recalc; 217 } 218 219 snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_PLL_SRC_MASK, 220 pll_sel); 221 222 snd_soc_component_update_bits(component, TAS2552_PLL_CTRL_1, 223 TAS2552_PLL_J_MASK, j); 224 /* Will clear the PLL_BYPASS bit */ 225 snd_soc_component_write(component, TAS2552_PLL_CTRL_2, 226 TAS2552_PLL_D_UPPER(d)); 227 snd_soc_component_write(component, TAS2552_PLL_CTRL_3, 228 TAS2552_PLL_D_LOWER(d)); 229 } 230 231 /* Restore PLL status */ 232 snd_soc_component_update_bits(component, TAS2552_CFG_2, TAS2552_PLL_ENABLE, 233 pll_enable); 234 235 return 0; 236 } 237 238 static int tas2552_hw_params(struct snd_pcm_substream *substream, 239 struct snd_pcm_hw_params *params, 240 struct snd_soc_dai *dai) 241 { 242 struct snd_soc_component *component = dai->component; 243 struct tas2552_data *tas2552 = dev_get_drvdata(component->dev); 244 int cpf; 245 u8 ser_ctrl1_reg, wclk_rate; 246 247 switch (params_width(params)) { 248 case 16: 249 ser_ctrl1_reg = TAS2552_WORDLENGTH_16BIT; 250 cpf = 32 + tas2552->tdm_delay; 251 break; 252 case 20: 253 ser_ctrl1_reg = TAS2552_WORDLENGTH_20BIT; 254 cpf = 64 + tas2552->tdm_delay; 255 break; 256 case 24: 257 ser_ctrl1_reg = TAS2552_WORDLENGTH_24BIT; 258 cpf = 64 + tas2552->tdm_delay; 259 break; 260 case 32: 261 ser_ctrl1_reg = TAS2552_WORDLENGTH_32BIT; 262 cpf = 64 + tas2552->tdm_delay; 263 break; 264 default: 265 dev_err(component->dev, "Not supported sample size: %d\n", 266 params_width(params)); 267 return -EINVAL; 268 } 269 270 if (cpf <= 32) 271 ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_32; 272 else if (cpf <= 64) 273 ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_64; 274 else if (cpf <= 128) 275 ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_128; 276 else 277 ser_ctrl1_reg |= TAS2552_CLKSPERFRAME_256; 278 279 snd_soc_component_update_bits(component, TAS2552_SER_CTRL_1, 280 TAS2552_WORDLENGTH_MASK | TAS2552_CLKSPERFRAME_MASK, 281 ser_ctrl1_reg); 282 283 switch (params_rate(params)) { 284 case 8000: 285 wclk_rate = TAS2552_WCLK_FREQ_8KHZ; 286 break; 287 case 11025: 288 case 12000: 289 wclk_rate = TAS2552_WCLK_FREQ_11_12KHZ; 290 break; 291 case 16000: 292 wclk_rate = TAS2552_WCLK_FREQ_16KHZ; 293 break; 294 case 22050: 295 case 24000: 296 wclk_rate = TAS2552_WCLK_FREQ_22_24KHZ; 297 break; 298 case 32000: 299 wclk_rate = TAS2552_WCLK_FREQ_32KHZ; 300 break; 301 case 44100: 302 case 48000: 303 wclk_rate = TAS2552_WCLK_FREQ_44_48KHZ; 304 break; 305 case 88200: 306 case 96000: 307 wclk_rate = TAS2552_WCLK_FREQ_88_96KHZ; 308 break; 309 case 176400: 310 case 192000: 311 wclk_rate = TAS2552_WCLK_FREQ_176_192KHZ; 312 break; 313 default: 314 dev_err(component->dev, "Not supported sample rate: %d\n", 315 params_rate(params)); 316 return -EINVAL; 317 } 318 319 snd_soc_component_update_bits(component, TAS2552_CFG_3, TAS2552_WCLK_FREQ_MASK, 320 wclk_rate); 321 322 return tas2552_setup_pll(component, params); 323 } 324 325 #define TAS2552_DAI_FMT_MASK (TAS2552_BCLKDIR | \ 326 TAS2552_WCLKDIR | \ 327 TAS2552_DATAFORMAT_MASK) 328 static int tas2552_prepare(struct snd_pcm_substream *substream, 329 struct snd_soc_dai *dai) 330 { 331 struct snd_soc_component *component = dai->component; 332 struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component); 333 int delay = 0; 334 335 /* TDM slot selection only valid in DSP_A/_B mode */ 336 if (tas2552->dai_fmt == SND_SOC_DAIFMT_DSP_A) 337 delay += (tas2552->tdm_delay + 1); 338 else if (tas2552->dai_fmt == SND_SOC_DAIFMT_DSP_B) 339 delay += tas2552->tdm_delay; 340 341 /* Configure data delay */ 342 snd_soc_component_write(component, TAS2552_SER_CTRL_2, delay); 343 344 return 0; 345 } 346 347 static int tas2552_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 348 { 349 struct snd_soc_component *component = dai->component; 350 struct tas2552_data *tas2552 = dev_get_drvdata(component->dev); 351 u8 serial_format; 352 353 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 354 case SND_SOC_DAIFMT_CBC_CFC: 355 serial_format = 0x00; 356 break; 357 case SND_SOC_DAIFMT_CBC_CFP: 358 serial_format = TAS2552_WCLKDIR; 359 break; 360 case SND_SOC_DAIFMT_CBP_CFC: 361 serial_format = TAS2552_BCLKDIR; 362 break; 363 case SND_SOC_DAIFMT_CBP_CFP: 364 serial_format = (TAS2552_BCLKDIR | TAS2552_WCLKDIR); 365 break; 366 default: 367 dev_vdbg(component->dev, "DAI Format master is not found\n"); 368 return -EINVAL; 369 } 370 371 switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK | 372 SND_SOC_DAIFMT_INV_MASK)) { 373 case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF): 374 break; 375 case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF): 376 case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF): 377 serial_format |= TAS2552_DATAFORMAT_DSP; 378 break; 379 case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF): 380 serial_format |= TAS2552_DATAFORMAT_RIGHT_J; 381 break; 382 case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF): 383 serial_format |= TAS2552_DATAFORMAT_LEFT_J; 384 break; 385 default: 386 dev_vdbg(component->dev, "DAI Format is not found\n"); 387 return -EINVAL; 388 } 389 tas2552->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK; 390 391 snd_soc_component_update_bits(component, TAS2552_SER_CTRL_1, TAS2552_DAI_FMT_MASK, 392 serial_format); 393 return 0; 394 } 395 396 static int tas2552_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, 397 unsigned int freq, int dir) 398 { 399 struct snd_soc_component *component = dai->component; 400 struct tas2552_data *tas2552 = dev_get_drvdata(component->dev); 401 u8 reg, mask, val; 402 403 switch (clk_id) { 404 case TAS2552_PLL_CLKIN_MCLK: 405 case TAS2552_PLL_CLKIN_IVCLKIN: 406 if (freq < 512000 || freq > 24576000) { 407 /* out of range PLL_CLKIN, fall back to use BCLK */ 408 dev_warn(component->dev, "Out of range PLL_CLKIN: %u\n", 409 freq); 410 clk_id = TAS2552_PLL_CLKIN_BCLK; 411 freq = 0; 412 } 413 fallthrough; 414 case TAS2552_PLL_CLKIN_BCLK: 415 case TAS2552_PLL_CLKIN_1_8_FIXED: 416 mask = TAS2552_PLL_SRC_MASK; 417 val = (clk_id << 3) & mask; /* bit 4:5 in the register */ 418 reg = TAS2552_CFG_1; 419 tas2552->pll_clk_id = clk_id; 420 tas2552->pll_clkin = freq; 421 break; 422 case TAS2552_PDM_CLK_PLL: 423 case TAS2552_PDM_CLK_IVCLKIN: 424 case TAS2552_PDM_CLK_BCLK: 425 case TAS2552_PDM_CLK_MCLK: 426 mask = TAS2552_PDM_CLK_SEL_MASK; 427 val = (clk_id >> 1) & mask; /* bit 0:1 in the register */ 428 reg = TAS2552_PDM_CFG; 429 tas2552->pdm_clk_id = clk_id; 430 tas2552->pdm_clk = freq; 431 break; 432 default: 433 dev_err(component->dev, "Invalid clk id: %d\n", clk_id); 434 return -EINVAL; 435 } 436 437 snd_soc_component_update_bits(component, reg, mask, val); 438 439 return 0; 440 } 441 442 static int tas2552_set_dai_tdm_slot(struct snd_soc_dai *dai, 443 unsigned int tx_mask, unsigned int rx_mask, 444 int slots, int slot_width) 445 { 446 struct snd_soc_component *component = dai->component; 447 struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component); 448 unsigned int lsb; 449 450 if (unlikely(!tx_mask)) { 451 dev_err(component->dev, "tx masks need to be non 0\n"); 452 return -EINVAL; 453 } 454 455 /* TDM based on DSP mode requires slots to be adjacent */ 456 lsb = __ffs(tx_mask); 457 if ((lsb + 1) != __fls(tx_mask)) { 458 dev_err(component->dev, "Invalid mask, slots must be adjacent\n"); 459 return -EINVAL; 460 } 461 462 tas2552->tdm_delay = lsb * slot_width; 463 464 /* DOUT in high-impedance on inactive bit clocks */ 465 snd_soc_component_update_bits(component, TAS2552_DOUT, 466 TAS2552_SDOUT_TRISTATE, TAS2552_SDOUT_TRISTATE); 467 468 return 0; 469 } 470 471 static int tas2552_mute(struct snd_soc_dai *dai, int mute, int direction) 472 { 473 u8 cfg1_reg = 0; 474 struct snd_soc_component *component = dai->component; 475 476 if (mute) 477 cfg1_reg |= TAS2552_MUTE; 478 479 snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_MUTE, cfg1_reg); 480 481 return 0; 482 } 483 484 #ifdef CONFIG_PM 485 static int tas2552_runtime_suspend(struct device *dev) 486 { 487 struct tas2552_data *tas2552 = dev_get_drvdata(dev); 488 489 tas2552_sw_shutdown(tas2552, 1); 490 491 regcache_cache_only(tas2552->regmap, true); 492 regcache_mark_dirty(tas2552->regmap); 493 494 gpiod_set_value(tas2552->enable_gpio, 0); 495 496 return 0; 497 } 498 499 static int tas2552_runtime_resume(struct device *dev) 500 { 501 struct tas2552_data *tas2552 = dev_get_drvdata(dev); 502 503 gpiod_set_value(tas2552->enable_gpio, 1); 504 505 tas2552_sw_shutdown(tas2552, 0); 506 507 regcache_cache_only(tas2552->regmap, false); 508 regcache_sync(tas2552->regmap); 509 510 return 0; 511 } 512 #endif 513 514 static const struct dev_pm_ops tas2552_pm = { 515 SET_RUNTIME_PM_OPS(tas2552_runtime_suspend, tas2552_runtime_resume, 516 NULL) 517 }; 518 519 static const struct snd_soc_dai_ops tas2552_speaker_dai_ops = { 520 .hw_params = tas2552_hw_params, 521 .prepare = tas2552_prepare, 522 .set_sysclk = tas2552_set_dai_sysclk, 523 .set_fmt = tas2552_set_dai_fmt, 524 .set_tdm_slot = tas2552_set_dai_tdm_slot, 525 .mute_stream = tas2552_mute, 526 .no_capture_mute = 1, 527 }; 528 529 /* Formats supported by TAS2552 driver. */ 530 #define TAS2552_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 531 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 532 533 /* TAS2552 dai structure. */ 534 static struct snd_soc_dai_driver tas2552_dai[] = { 535 { 536 .name = "tas2552-amplifier", 537 .playback = { 538 .stream_name = "Playback", 539 .channels_min = 2, 540 .channels_max = 2, 541 .rates = SNDRV_PCM_RATE_8000_192000, 542 .formats = TAS2552_FORMATS, 543 }, 544 .capture = { 545 .stream_name = "Capture", 546 .channels_min = 2, 547 .channels_max = 2, 548 .rates = SNDRV_PCM_RATE_8000_192000, 549 .formats = TAS2552_FORMATS, 550 }, 551 .ops = &tas2552_speaker_dai_ops, 552 }, 553 }; 554 555 /* 556 * DAC digital volumes. From -7 to 24 dB in 1 dB steps 557 */ 558 static DECLARE_TLV_DB_SCALE(dac_tlv, -700, 100, 0); 559 560 static const char * const tas2552_din_source_select[] = { 561 "Muted", 562 "Left", 563 "Right", 564 "Left + Right average", 565 }; 566 static SOC_ENUM_SINGLE_DECL(tas2552_din_source_enum, 567 TAS2552_CFG_3, 3, 568 tas2552_din_source_select); 569 570 static const struct snd_kcontrol_new tas2552_snd_controls[] = { 571 SOC_SINGLE_TLV("Speaker Driver Playback Volume", 572 TAS2552_PGA_GAIN, 0, 0x1f, 0, dac_tlv), 573 SOC_ENUM("DIN source", tas2552_din_source_enum), 574 }; 575 576 static int tas2552_component_probe(struct snd_soc_component *component) 577 { 578 struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component); 579 int ret; 580 581 tas2552->component = component; 582 583 ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies), 584 tas2552->supplies); 585 586 if (ret != 0) { 587 dev_err(component->dev, "Failed to enable supplies: %d\n", 588 ret); 589 return ret; 590 } 591 592 gpiod_set_value(tas2552->enable_gpio, 1); 593 594 ret = pm_runtime_resume_and_get(component->dev); 595 if (ret < 0) { 596 dev_err(component->dev, "Enabling device failed: %d\n", 597 ret); 598 goto probe_fail; 599 } 600 601 snd_soc_component_update_bits(component, TAS2552_CFG_1, TAS2552_MUTE, TAS2552_MUTE); 602 snd_soc_component_write(component, TAS2552_CFG_3, TAS2552_I2S_OUT_SEL | 603 TAS2552_DIN_SRC_SEL_AVG_L_R); 604 snd_soc_component_write(component, TAS2552_OUTPUT_DATA, 605 TAS2552_PDM_DATA_SEL_V_I | 606 TAS2552_R_DATA_OUT(TAS2552_DATA_OUT_V_DATA)); 607 snd_soc_component_write(component, TAS2552_BOOST_APT_CTRL, TAS2552_APT_DELAY_200 | 608 TAS2552_APT_THRESH_20_17); 609 610 snd_soc_component_write(component, TAS2552_CFG_2, TAS2552_BOOST_EN | TAS2552_APT_EN | 611 TAS2552_LIM_EN); 612 613 return 0; 614 615 probe_fail: 616 pm_runtime_put_noidle(component->dev); 617 gpiod_set_value(tas2552->enable_gpio, 0); 618 619 regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies), 620 tas2552->supplies); 621 return ret; 622 } 623 624 static void tas2552_component_remove(struct snd_soc_component *component) 625 { 626 struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component); 627 628 pm_runtime_put(component->dev); 629 630 gpiod_set_value(tas2552->enable_gpio, 0); 631 }; 632 633 #ifdef CONFIG_PM 634 static int tas2552_suspend(struct snd_soc_component *component) 635 { 636 struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component); 637 int ret; 638 639 ret = regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies), 640 tas2552->supplies); 641 642 if (ret != 0) 643 dev_err(component->dev, "Failed to disable supplies: %d\n", 644 ret); 645 return ret; 646 } 647 648 static int tas2552_resume(struct snd_soc_component *component) 649 { 650 struct tas2552_data *tas2552 = snd_soc_component_get_drvdata(component); 651 int ret; 652 653 ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies), 654 tas2552->supplies); 655 656 if (ret != 0) { 657 dev_err(component->dev, "Failed to enable supplies: %d\n", 658 ret); 659 } 660 661 return ret; 662 } 663 #else 664 #define tas2552_suspend NULL 665 #define tas2552_resume NULL 666 #endif 667 668 static const struct snd_soc_component_driver soc_component_dev_tas2552 = { 669 .probe = tas2552_component_probe, 670 .remove = tas2552_component_remove, 671 .suspend = tas2552_suspend, 672 .resume = tas2552_resume, 673 .controls = tas2552_snd_controls, 674 .num_controls = ARRAY_SIZE(tas2552_snd_controls), 675 .dapm_widgets = tas2552_dapm_widgets, 676 .num_dapm_widgets = ARRAY_SIZE(tas2552_dapm_widgets), 677 .dapm_routes = tas2552_audio_map, 678 .num_dapm_routes = ARRAY_SIZE(tas2552_audio_map), 679 .idle_bias_on = 1, 680 .endianness = 1, 681 }; 682 683 static const struct regmap_config tas2552_regmap_config = { 684 .reg_bits = 8, 685 .val_bits = 8, 686 687 .max_register = TAS2552_MAX_REG, 688 .reg_defaults = tas2552_reg_defs, 689 .num_reg_defaults = ARRAY_SIZE(tas2552_reg_defs), 690 .cache_type = REGCACHE_RBTREE, 691 }; 692 693 static int tas2552_probe(struct i2c_client *client) 694 { 695 struct device *dev; 696 struct tas2552_data *data; 697 int ret; 698 int i; 699 700 dev = &client->dev; 701 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 702 if (data == NULL) 703 return -ENOMEM; 704 705 data->enable_gpio = devm_gpiod_get_optional(dev, "enable", 706 GPIOD_OUT_LOW); 707 if (IS_ERR(data->enable_gpio)) 708 return PTR_ERR(data->enable_gpio); 709 710 data->tas2552_client = client; 711 data->regmap = devm_regmap_init_i2c(client, &tas2552_regmap_config); 712 if (IS_ERR(data->regmap)) { 713 ret = PTR_ERR(data->regmap); 714 dev_err(&client->dev, "Failed to allocate register map: %d\n", 715 ret); 716 return ret; 717 } 718 719 for (i = 0; i < ARRAY_SIZE(data->supplies); i++) 720 data->supplies[i].supply = tas2552_supply_names[i]; 721 722 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies), 723 data->supplies); 724 if (ret != 0) { 725 dev_err(dev, "Failed to request supplies: %d\n", ret); 726 return ret; 727 } 728 729 pm_runtime_set_active(&client->dev); 730 pm_runtime_set_autosuspend_delay(&client->dev, 1000); 731 pm_runtime_use_autosuspend(&client->dev); 732 pm_runtime_enable(&client->dev); 733 pm_runtime_mark_last_busy(&client->dev); 734 pm_runtime_put_sync_autosuspend(&client->dev); 735 736 dev_set_drvdata(&client->dev, data); 737 738 ret = devm_snd_soc_register_component(&client->dev, 739 &soc_component_dev_tas2552, 740 tas2552_dai, ARRAY_SIZE(tas2552_dai)); 741 if (ret < 0) { 742 dev_err(&client->dev, "Failed to register component: %d\n", ret); 743 pm_runtime_get_noresume(&client->dev); 744 } 745 746 return ret; 747 } 748 749 static void tas2552_i2c_remove(struct i2c_client *client) 750 { 751 pm_runtime_disable(&client->dev); 752 } 753 754 static const struct i2c_device_id tas2552_id[] = { 755 { "tas2552" }, 756 { } 757 }; 758 MODULE_DEVICE_TABLE(i2c, tas2552_id); 759 760 #if IS_ENABLED(CONFIG_OF) 761 static const struct of_device_id tas2552_of_match[] = { 762 { .compatible = "ti,tas2552", }, 763 {}, 764 }; 765 MODULE_DEVICE_TABLE(of, tas2552_of_match); 766 #endif 767 768 static struct i2c_driver tas2552_i2c_driver = { 769 .driver = { 770 .name = "tas2552", 771 .of_match_table = of_match_ptr(tas2552_of_match), 772 .pm = &tas2552_pm, 773 }, 774 .probe = tas2552_probe, 775 .remove = tas2552_i2c_remove, 776 .id_table = tas2552_id, 777 }; 778 779 module_i2c_driver(tas2552_i2c_driver); 780 781 MODULE_AUTHOR("Dan Muprhy <dmurphy@ti.com>"); 782 MODULE_DESCRIPTION("TAS2552 Audio amplifier driver"); 783 MODULE_LICENSE("GPL"); 784