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