1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ALSA SoC Texas Instruments TLV320DAC33 codec driver 4 * 5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com> 6 * 7 * Copyright: (C) 2009 Nokia Corporation 8 */ 9 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <linux/init.h> 13 #include <linux/delay.h> 14 #include <linux/pm.h> 15 #include <linux/i2c.h> 16 #include <linux/interrupt.h> 17 #include <linux/gpio/consumer.h> 18 #include <linux/regulator/consumer.h> 19 #include <linux/slab.h> 20 #include <sound/core.h> 21 #include <sound/pcm.h> 22 #include <sound/pcm_params.h> 23 #include <sound/soc.h> 24 #include <sound/initval.h> 25 #include <sound/tlv.h> 26 27 #include "tlv320dac33.h" 28 29 /* 30 * The internal FIFO is 24576 bytes long 31 * It can be configured to hold 16bit or 24bit samples 32 * In 16bit configuration the FIFO can hold 6144 stereo samples 33 * In 24bit configuration the FIFO can hold 4096 stereo samples 34 */ 35 #define DAC33_FIFO_SIZE_16BIT 6144 36 #define DAC33_FIFO_SIZE_24BIT 4096 37 #define DAC33_MODE7_MARGIN 10 /* Safety margin for FIFO in Mode7 */ 38 39 #define BURST_BASEFREQ_HZ 49152000 40 41 #define SAMPLES_TO_US(rate, samples) \ 42 (1000000000 / (((rate) * 1000) / (samples))) 43 44 #define US_TO_SAMPLES(rate, us) \ 45 ((rate) / (1000000 / ((us) < 1000000 ? (us) : 1000000))) 46 47 #define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \ 48 (((samples)*5000) / (((burstrate)*5000) / ((burstrate) - (playrate)))) 49 50 static void dac33_calculate_times(struct snd_pcm_substream *substream, 51 struct snd_soc_component *component); 52 static int dac33_prepare_chip(struct snd_pcm_substream *substream, 53 struct snd_soc_component *component); 54 55 enum dac33_state { 56 DAC33_IDLE = 0, 57 DAC33_PREFILL, 58 DAC33_PLAYBACK, 59 DAC33_FLUSH, 60 }; 61 62 enum dac33_fifo_modes { 63 DAC33_FIFO_BYPASS = 0, 64 DAC33_FIFO_MODE1, 65 DAC33_FIFO_MODE7, 66 DAC33_FIFO_LAST_MODE, 67 }; 68 69 #define DAC33_NUM_SUPPLIES 3 70 static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = { 71 "AVDD", 72 "DVDD", 73 "IOVDD", 74 }; 75 76 struct tlv320dac33_priv { 77 struct mutex mutex; 78 struct work_struct work; 79 struct snd_soc_component *component; 80 struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES]; 81 struct snd_pcm_substream *substream; 82 struct gpio_desc *reset_gpiod; 83 int chip_power; 84 int irq; 85 unsigned int refclk; 86 87 unsigned int alarm_threshold; /* set to be half of LATENCY_TIME_MS */ 88 enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */ 89 unsigned int fifo_size; /* Size of the FIFO in samples */ 90 unsigned int nsample; /* burst read amount from host */ 91 int mode1_latency; /* latency caused by the i2c writes in 92 * us */ 93 u8 burst_bclkdiv; /* BCLK divider value in burst mode */ 94 unsigned int burst_rate; /* Interface speed in Burst modes */ 95 96 int keep_bclk; /* Keep the BCLK continuously running 97 * in FIFO modes */ 98 spinlock_t lock; 99 unsigned long long t_stamp1; /* Time stamp for FIFO modes to */ 100 unsigned long long t_stamp2; /* calculate the FIFO caused delay */ 101 102 unsigned int mode1_us_burst; /* Time to burst read n number of 103 * samples */ 104 unsigned int mode7_us_to_lthr; /* Time to reach lthr from uthr */ 105 106 unsigned int uthr; 107 108 enum dac33_state state; 109 struct i2c_client *i2c; 110 111 u8 reg_cache[]; 112 }; 113 114 static const u8 dac33_reg[DAC33_CACHEREGNUM] = { 115 0x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */ 116 0x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */ 117 0x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */ 118 0x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */ 119 0x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */ 120 0x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */ 121 0x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */ 122 0x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */ 123 0x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */ 124 0x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */ 125 0x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */ 126 0x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */ 127 0x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */ 128 0x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */ 129 0x00, 0x00, /* 0x38 - 0x39 */ 130 /* Registers 0x3a - 0x3f are reserved */ 131 0x00, 0x00, /* 0x3a - 0x3b */ 132 0x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */ 133 134 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */ 135 0x00, 0x80, /* 0x44 - 0x45 */ 136 /* Registers 0x46 - 0x47 are reserved */ 137 0x80, 0x80, /* 0x46 - 0x47 */ 138 139 0x80, 0x00, 0x00, /* 0x48 - 0x4a */ 140 /* Registers 0x4b - 0x7c are reserved */ 141 0x00, /* 0x4b */ 142 0x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */ 143 0x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */ 144 0x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */ 145 0x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */ 146 0x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */ 147 0x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */ 148 0x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */ 149 0x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */ 150 0x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */ 151 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */ 152 0x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */ 153 0x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */ 154 0x00, /* 0x7c */ 155 156 0xda, 0x33, 0x03, /* 0x7d - 0x7f */ 157 }; 158 159 /* Register read and write */ 160 static inline unsigned int dac33_read_reg_cache(struct snd_soc_component *component, 161 unsigned reg) 162 { 163 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 164 u8 *cache = dac33->reg_cache; 165 if (reg >= DAC33_CACHEREGNUM) 166 return 0; 167 168 return cache[reg]; 169 } 170 171 static inline void dac33_write_reg_cache(struct snd_soc_component *component, 172 u8 reg, u8 value) 173 { 174 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 175 u8 *cache = dac33->reg_cache; 176 if (reg >= DAC33_CACHEREGNUM) 177 return; 178 179 cache[reg] = value; 180 } 181 182 static int dac33_read(struct snd_soc_component *component, unsigned int reg, 183 u8 *value) 184 { 185 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 186 int val, ret = 0; 187 188 *value = reg & 0xff; 189 190 /* If powered off, return the cached value */ 191 if (dac33->chip_power) { 192 val = i2c_smbus_read_byte_data(dac33->i2c, value[0]); 193 if (val < 0) { 194 dev_err(component->dev, "Read failed (%d)\n", val); 195 value[0] = dac33_read_reg_cache(component, reg); 196 ret = val; 197 } else { 198 value[0] = val; 199 dac33_write_reg_cache(component, reg, val); 200 } 201 } else { 202 value[0] = dac33_read_reg_cache(component, reg); 203 } 204 205 return ret; 206 } 207 208 static int dac33_write(struct snd_soc_component *component, unsigned int reg, 209 unsigned int value) 210 { 211 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 212 u8 data[2]; 213 int ret = 0; 214 215 /* 216 * data is 217 * D15..D8 dac33 register offset 218 * D7...D0 register data 219 */ 220 data[0] = reg & 0xff; 221 data[1] = value & 0xff; 222 223 dac33_write_reg_cache(component, data[0], data[1]); 224 if (dac33->chip_power) { 225 ret = i2c_master_send(dac33->i2c, data, 2); 226 if (ret != 2) 227 dev_err(component->dev, "Write failed (%d)\n", ret); 228 else 229 ret = 0; 230 } 231 232 return ret; 233 } 234 235 static int dac33_write_locked(struct snd_soc_component *component, unsigned int reg, 236 unsigned int value) 237 { 238 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 239 int ret; 240 241 mutex_lock(&dac33->mutex); 242 ret = dac33_write(component, reg, value); 243 mutex_unlock(&dac33->mutex); 244 245 return ret; 246 } 247 248 #define DAC33_I2C_ADDR_AUTOINC 0x80 249 static int dac33_write16(struct snd_soc_component *component, unsigned int reg, 250 unsigned int value) 251 { 252 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 253 u8 data[3]; 254 int ret = 0; 255 256 /* 257 * data is 258 * D23..D16 dac33 register offset 259 * D15..D8 register data MSB 260 * D7...D0 register data LSB 261 */ 262 data[0] = reg & 0xff; 263 data[1] = (value >> 8) & 0xff; 264 data[2] = value & 0xff; 265 266 dac33_write_reg_cache(component, data[0], data[1]); 267 dac33_write_reg_cache(component, data[0] + 1, data[2]); 268 269 if (dac33->chip_power) { 270 /* We need to set autoincrement mode for 16 bit writes */ 271 data[0] |= DAC33_I2C_ADDR_AUTOINC; 272 ret = i2c_master_send(dac33->i2c, data, 3); 273 if (ret != 3) 274 dev_err(component->dev, "Write failed (%d)\n", ret); 275 else 276 ret = 0; 277 } 278 279 return ret; 280 } 281 282 static void dac33_init_chip(struct snd_soc_component *component) 283 { 284 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 285 286 if (unlikely(!dac33->chip_power)) 287 return; 288 289 /* A : DAC sample rate Fsref/1.5 */ 290 dac33_write(component, DAC33_DAC_CTRL_A, DAC33_DACRATE(0)); 291 /* B : DAC src=normal, not muted */ 292 dac33_write(component, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT | 293 DAC33_DACSRCL_LEFT); 294 /* C : (defaults) */ 295 dac33_write(component, DAC33_DAC_CTRL_C, 0x00); 296 297 /* 73 : volume soft stepping control, 298 clock source = internal osc (?) */ 299 dac33_write(component, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN); 300 301 /* Restore only selected registers (gains mostly) */ 302 dac33_write(component, DAC33_LDAC_DIG_VOL_CTRL, 303 dac33_read_reg_cache(component, DAC33_LDAC_DIG_VOL_CTRL)); 304 dac33_write(component, DAC33_RDAC_DIG_VOL_CTRL, 305 dac33_read_reg_cache(component, DAC33_RDAC_DIG_VOL_CTRL)); 306 307 dac33_write(component, DAC33_LINEL_TO_LLO_VOL, 308 dac33_read_reg_cache(component, DAC33_LINEL_TO_LLO_VOL)); 309 dac33_write(component, DAC33_LINER_TO_RLO_VOL, 310 dac33_read_reg_cache(component, DAC33_LINER_TO_RLO_VOL)); 311 312 dac33_write(component, DAC33_OUT_AMP_CTRL, 313 dac33_read_reg_cache(component, DAC33_OUT_AMP_CTRL)); 314 315 dac33_write(component, DAC33_LDAC_PWR_CTRL, 316 dac33_read_reg_cache(component, DAC33_LDAC_PWR_CTRL)); 317 dac33_write(component, DAC33_RDAC_PWR_CTRL, 318 dac33_read_reg_cache(component, DAC33_RDAC_PWR_CTRL)); 319 } 320 321 static inline int dac33_read_id(struct snd_soc_component *component) 322 { 323 int i, ret = 0; 324 u8 reg; 325 326 for (i = 0; i < 3; i++) { 327 ret = dac33_read(component, DAC33_DEVICE_ID_MSB + i, ®); 328 if (ret < 0) 329 break; 330 } 331 332 return ret; 333 } 334 335 static inline void dac33_soft_power(struct snd_soc_component *component, int power) 336 { 337 u8 reg; 338 339 reg = dac33_read_reg_cache(component, DAC33_PWR_CTRL); 340 if (power) 341 reg |= DAC33_PDNALLB; 342 else 343 reg &= ~(DAC33_PDNALLB | DAC33_OSCPDNB | 344 DAC33_DACRPDNB | DAC33_DACLPDNB); 345 dac33_write(component, DAC33_PWR_CTRL, reg); 346 } 347 348 static inline void dac33_disable_digital(struct snd_soc_component *component) 349 { 350 u8 reg; 351 352 /* Stop the DAI clock */ 353 reg = dac33_read_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_B); 354 reg &= ~DAC33_BCLKON; 355 dac33_write(component, DAC33_SER_AUDIOIF_CTRL_B, reg); 356 357 /* Power down the Oscillator, and DACs */ 358 reg = dac33_read_reg_cache(component, DAC33_PWR_CTRL); 359 reg &= ~(DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB); 360 dac33_write(component, DAC33_PWR_CTRL, reg); 361 } 362 363 static int dac33_hard_power(struct snd_soc_component *component, int power) 364 { 365 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 366 int ret = 0; 367 368 mutex_lock(&dac33->mutex); 369 370 /* Safety check */ 371 if (unlikely(power == dac33->chip_power)) { 372 dev_dbg(component->dev, "Trying to set the same power state: %s\n", 373 power ? "ON" : "OFF"); 374 goto exit; 375 } 376 377 if (power) { 378 ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies), 379 dac33->supplies); 380 if (ret != 0) { 381 dev_err(component->dev, 382 "Failed to enable supplies: %d\n", ret); 383 goto exit; 384 } 385 386 if (dac33->reset_gpiod) { 387 ret = gpiod_set_value(dac33->reset_gpiod, 1); 388 if (ret < 0) { 389 dev_err(&dac33->i2c->dev, 390 "Failed to set reset GPIO: %d\n", ret); 391 goto exit; 392 } 393 } 394 395 dac33->chip_power = 1; 396 } else { 397 dac33_soft_power(component, 0); 398 if (dac33->reset_gpiod) { 399 ret = gpiod_set_value(dac33->reset_gpiod, 0); 400 if (ret < 0) { 401 dev_err(&dac33->i2c->dev, 402 "Failed to set reset GPIO: %d\n", ret); 403 goto exit; 404 } 405 } 406 407 ret = regulator_bulk_disable(ARRAY_SIZE(dac33->supplies), 408 dac33->supplies); 409 if (ret != 0) { 410 dev_err(component->dev, 411 "Failed to disable supplies: %d\n", ret); 412 goto exit; 413 } 414 415 dac33->chip_power = 0; 416 } 417 418 exit: 419 mutex_unlock(&dac33->mutex); 420 return ret; 421 } 422 423 static int dac33_playback_event(struct snd_soc_dapm_widget *w, 424 struct snd_kcontrol *kcontrol, int event) 425 { 426 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 427 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 428 429 switch (event) { 430 case SND_SOC_DAPM_PRE_PMU: 431 if (likely(dac33->substream)) { 432 dac33_calculate_times(dac33->substream, component); 433 dac33_prepare_chip(dac33->substream, component); 434 } 435 break; 436 case SND_SOC_DAPM_POST_PMD: 437 dac33_disable_digital(component); 438 break; 439 } 440 return 0; 441 } 442 443 static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol, 444 struct snd_ctl_elem_value *ucontrol) 445 { 446 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 447 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 448 449 ucontrol->value.enumerated.item[0] = dac33->fifo_mode; 450 451 return 0; 452 } 453 454 static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol, 455 struct snd_ctl_elem_value *ucontrol) 456 { 457 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 458 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 459 int ret = 0; 460 461 if (dac33->fifo_mode == ucontrol->value.enumerated.item[0]) 462 return 0; 463 /* Do not allow changes while stream is running*/ 464 if (snd_soc_component_active(component)) 465 return -EPERM; 466 467 if (ucontrol->value.enumerated.item[0] >= DAC33_FIFO_LAST_MODE) 468 ret = -EINVAL; 469 else 470 dac33->fifo_mode = ucontrol->value.enumerated.item[0]; 471 472 return ret; 473 } 474 475 /* Codec operation modes */ 476 static const char *dac33_fifo_mode_texts[] = { 477 "Bypass", "Mode 1", "Mode 7" 478 }; 479 480 static SOC_ENUM_SINGLE_EXT_DECL(dac33_fifo_mode_enum, dac33_fifo_mode_texts); 481 482 /* L/R Line Output Gain */ 483 static const char *lr_lineout_gain_texts[] = { 484 "Line -12dB DAC 0dB", "Line -6dB DAC 6dB", 485 "Line 0dB DAC 12dB", "Line 6dB DAC 18dB", 486 }; 487 488 static SOC_ENUM_SINGLE_DECL(l_lineout_gain_enum, 489 DAC33_LDAC_PWR_CTRL, 0, 490 lr_lineout_gain_texts); 491 492 static SOC_ENUM_SINGLE_DECL(r_lineout_gain_enum, 493 DAC33_RDAC_PWR_CTRL, 0, 494 lr_lineout_gain_texts); 495 496 /* 497 * DACL/R digital volume control: 498 * from 0 dB to -63.5 in 0.5 dB steps 499 * Need to be inverted later on: 500 * 0x00 == 0 dB 501 * 0x7f == -63.5 dB 502 */ 503 static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0); 504 505 static const struct snd_kcontrol_new dac33_snd_controls[] = { 506 SOC_DOUBLE_R_TLV("DAC Digital Playback Volume", 507 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 508 0, 0x7f, 1, dac_digivol_tlv), 509 SOC_DOUBLE_R("DAC Digital Playback Switch", 510 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 7, 1, 1), 511 SOC_DOUBLE_R("Line to Line Out Volume", 512 DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1), 513 SOC_ENUM("Left Line Output Gain", l_lineout_gain_enum), 514 SOC_ENUM("Right Line Output Gain", r_lineout_gain_enum), 515 }; 516 517 static const struct snd_kcontrol_new dac33_mode_snd_controls[] = { 518 SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum, 519 dac33_get_fifo_mode, dac33_set_fifo_mode), 520 }; 521 522 /* Analog bypass */ 523 static const struct snd_kcontrol_new dac33_dapm_abypassl_control = 524 SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1); 525 526 static const struct snd_kcontrol_new dac33_dapm_abypassr_control = 527 SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1); 528 529 /* LOP L/R invert selection */ 530 static const char *dac33_lr_lom_texts[] = {"DAC", "LOP"}; 531 532 static SOC_ENUM_SINGLE_DECL(dac33_left_lom_enum, 533 DAC33_OUT_AMP_CTRL, 3, 534 dac33_lr_lom_texts); 535 536 static const struct snd_kcontrol_new dac33_dapm_left_lom_control = 537 SOC_DAPM_ENUM("Route", dac33_left_lom_enum); 538 539 static SOC_ENUM_SINGLE_DECL(dac33_right_lom_enum, 540 DAC33_OUT_AMP_CTRL, 2, 541 dac33_lr_lom_texts); 542 543 static const struct snd_kcontrol_new dac33_dapm_right_lom_control = 544 SOC_DAPM_ENUM("Route", dac33_right_lom_enum); 545 546 static const struct snd_soc_dapm_widget dac33_dapm_widgets[] = { 547 SND_SOC_DAPM_OUTPUT("LEFT_LO"), 548 SND_SOC_DAPM_OUTPUT("RIGHT_LO"), 549 550 SND_SOC_DAPM_INPUT("LINEL"), 551 SND_SOC_DAPM_INPUT("LINER"), 552 553 SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0), 554 SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0), 555 556 /* Analog bypass */ 557 SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM, 0, 0, 558 &dac33_dapm_abypassl_control), 559 SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM, 0, 0, 560 &dac33_dapm_abypassr_control), 561 562 SND_SOC_DAPM_MUX("Left LOM Inverted From", SND_SOC_NOPM, 0, 0, 563 &dac33_dapm_left_lom_control), 564 SND_SOC_DAPM_MUX("Right LOM Inverted From", SND_SOC_NOPM, 0, 0, 565 &dac33_dapm_right_lom_control), 566 /* 567 * For DAPM path, when only the anlog bypass path is enabled, and the 568 * LOP inverted from the corresponding DAC side. 569 * This is needed, so we can attach the DAC power supply in this case. 570 */ 571 SND_SOC_DAPM_PGA("Left Bypass PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 572 SND_SOC_DAPM_PGA("Right Bypass PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 573 574 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amplifier", 575 DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0), 576 SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amplifier", 577 DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0), 578 579 SND_SOC_DAPM_SUPPLY("Left DAC Power", 580 DAC33_LDAC_PWR_CTRL, 2, 0, NULL, 0), 581 SND_SOC_DAPM_SUPPLY("Right DAC Power", 582 DAC33_RDAC_PWR_CTRL, 2, 0, NULL, 0), 583 584 SND_SOC_DAPM_SUPPLY("Codec Power", 585 DAC33_PWR_CTRL, 4, 0, NULL, 0), 586 587 SND_SOC_DAPM_PRE("Pre Playback", dac33_playback_event), 588 SND_SOC_DAPM_POST("Post Playback", dac33_playback_event), 589 }; 590 591 static const struct snd_soc_dapm_route audio_map[] = { 592 /* Analog bypass */ 593 {"Analog Left Bypass", "Switch", "LINEL"}, 594 {"Analog Right Bypass", "Switch", "LINER"}, 595 596 {"Output Left Amplifier", NULL, "DACL"}, 597 {"Output Right Amplifier", NULL, "DACR"}, 598 599 {"Left Bypass PGA", NULL, "Analog Left Bypass"}, 600 {"Right Bypass PGA", NULL, "Analog Right Bypass"}, 601 602 {"Left LOM Inverted From", "DAC", "Left Bypass PGA"}, 603 {"Right LOM Inverted From", "DAC", "Right Bypass PGA"}, 604 {"Left LOM Inverted From", "LOP", "Analog Left Bypass"}, 605 {"Right LOM Inverted From", "LOP", "Analog Right Bypass"}, 606 607 {"Output Left Amplifier", NULL, "Left LOM Inverted From"}, 608 {"Output Right Amplifier", NULL, "Right LOM Inverted From"}, 609 610 {"DACL", NULL, "Left DAC Power"}, 611 {"DACR", NULL, "Right DAC Power"}, 612 613 {"Left Bypass PGA", NULL, "Left DAC Power"}, 614 {"Right Bypass PGA", NULL, "Right DAC Power"}, 615 616 /* output */ 617 {"LEFT_LO", NULL, "Output Left Amplifier"}, 618 {"RIGHT_LO", NULL, "Output Right Amplifier"}, 619 620 {"LEFT_LO", NULL, "Codec Power"}, 621 {"RIGHT_LO", NULL, "Codec Power"}, 622 }; 623 624 static int dac33_set_bias_level(struct snd_soc_component *component, 625 enum snd_soc_bias_level level) 626 { 627 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); 628 int ret; 629 630 switch (level) { 631 case SND_SOC_BIAS_ON: 632 break; 633 case SND_SOC_BIAS_PREPARE: 634 break; 635 case SND_SOC_BIAS_STANDBY: 636 if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF) { 637 /* Coming from OFF, switch on the component */ 638 ret = dac33_hard_power(component, 1); 639 if (ret != 0) 640 return ret; 641 642 dac33_init_chip(component); 643 } 644 break; 645 case SND_SOC_BIAS_OFF: 646 /* Do not power off, when the component is already off */ 647 if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF) 648 return 0; 649 ret = dac33_hard_power(component, 0); 650 if (ret != 0) 651 return ret; 652 break; 653 } 654 655 return 0; 656 } 657 658 static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33) 659 { 660 struct snd_soc_component *component = dac33->component; 661 unsigned int delay; 662 unsigned long flags; 663 664 switch (dac33->fifo_mode) { 665 case DAC33_FIFO_MODE1: 666 dac33_write16(component, DAC33_NSAMPLE_MSB, 667 DAC33_THRREG(dac33->nsample)); 668 669 /* Take the timestamps */ 670 spin_lock_irqsave(&dac33->lock, flags); 671 dac33->t_stamp2 = ktime_to_us(ktime_get()); 672 dac33->t_stamp1 = dac33->t_stamp2; 673 spin_unlock_irqrestore(&dac33->lock, flags); 674 675 dac33_write16(component, DAC33_PREFILL_MSB, 676 DAC33_THRREG(dac33->alarm_threshold)); 677 /* Enable Alarm Threshold IRQ with a delay */ 678 delay = SAMPLES_TO_US(dac33->burst_rate, 679 dac33->alarm_threshold) + 1000; 680 usleep_range(delay, delay + 500); 681 dac33_write(component, DAC33_FIFO_IRQ_MASK, DAC33_MAT); 682 break; 683 case DAC33_FIFO_MODE7: 684 /* Take the timestamp */ 685 spin_lock_irqsave(&dac33->lock, flags); 686 dac33->t_stamp1 = ktime_to_us(ktime_get()); 687 /* Move back the timestamp with drain time */ 688 dac33->t_stamp1 -= dac33->mode7_us_to_lthr; 689 spin_unlock_irqrestore(&dac33->lock, flags); 690 691 dac33_write16(component, DAC33_PREFILL_MSB, 692 DAC33_THRREG(DAC33_MODE7_MARGIN)); 693 694 /* Enable Upper Threshold IRQ */ 695 dac33_write(component, DAC33_FIFO_IRQ_MASK, DAC33_MUT); 696 break; 697 default: 698 dev_warn(component->dev, "Unhandled FIFO mode: %d\n", 699 dac33->fifo_mode); 700 break; 701 } 702 } 703 704 static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33) 705 { 706 struct snd_soc_component *component = dac33->component; 707 unsigned long flags; 708 709 switch (dac33->fifo_mode) { 710 case DAC33_FIFO_MODE1: 711 /* Take the timestamp */ 712 spin_lock_irqsave(&dac33->lock, flags); 713 dac33->t_stamp2 = ktime_to_us(ktime_get()); 714 spin_unlock_irqrestore(&dac33->lock, flags); 715 716 dac33_write16(component, DAC33_NSAMPLE_MSB, 717 DAC33_THRREG(dac33->nsample)); 718 break; 719 case DAC33_FIFO_MODE7: 720 /* At the moment we are not using interrupts in mode7 */ 721 break; 722 default: 723 dev_warn(component->dev, "Unhandled FIFO mode: %d\n", 724 dac33->fifo_mode); 725 break; 726 } 727 } 728 729 static void dac33_work(struct work_struct *work) 730 { 731 struct snd_soc_component *component; 732 struct tlv320dac33_priv *dac33; 733 u8 reg; 734 735 dac33 = container_of(work, struct tlv320dac33_priv, work); 736 component = dac33->component; 737 738 mutex_lock(&dac33->mutex); 739 switch (dac33->state) { 740 case DAC33_PREFILL: 741 dac33->state = DAC33_PLAYBACK; 742 dac33_prefill_handler(dac33); 743 break; 744 case DAC33_PLAYBACK: 745 dac33_playback_handler(dac33); 746 break; 747 case DAC33_IDLE: 748 break; 749 case DAC33_FLUSH: 750 dac33->state = DAC33_IDLE; 751 /* Mask all interrupts from dac33 */ 752 dac33_write(component, DAC33_FIFO_IRQ_MASK, 0); 753 754 /* flush fifo */ 755 reg = dac33_read_reg_cache(component, DAC33_FIFO_CTRL_A); 756 reg |= DAC33_FIFOFLUSH; 757 dac33_write(component, DAC33_FIFO_CTRL_A, reg); 758 break; 759 } 760 mutex_unlock(&dac33->mutex); 761 } 762 763 static irqreturn_t dac33_interrupt_handler(int irq, void *dev) 764 { 765 struct snd_soc_component *component = dev; 766 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 767 unsigned long flags; 768 769 spin_lock_irqsave(&dac33->lock, flags); 770 dac33->t_stamp1 = ktime_to_us(ktime_get()); 771 spin_unlock_irqrestore(&dac33->lock, flags); 772 773 /* Do not schedule the workqueue in Mode7 */ 774 if (dac33->fifo_mode != DAC33_FIFO_MODE7) 775 schedule_work(&dac33->work); 776 777 return IRQ_HANDLED; 778 } 779 780 static void dac33_oscwait(struct snd_soc_component *component) 781 { 782 int timeout = 60; 783 u8 reg; 784 785 do { 786 usleep_range(1000, 2000); 787 dac33_read(component, DAC33_INT_OSC_STATUS, ®); 788 } while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--); 789 if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) 790 dev_err(component->dev, 791 "internal oscillator calibration failed\n"); 792 } 793 794 static int dac33_startup(struct snd_pcm_substream *substream, 795 struct snd_soc_dai *dai) 796 { 797 struct snd_soc_component *component = dai->component; 798 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 799 800 /* Stream started, save the substream pointer */ 801 dac33->substream = substream; 802 803 return 0; 804 } 805 806 static void dac33_shutdown(struct snd_pcm_substream *substream, 807 struct snd_soc_dai *dai) 808 { 809 struct snd_soc_component *component = dai->component; 810 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 811 812 dac33->substream = NULL; 813 } 814 815 #define CALC_BURST_RATE(bclkdiv, bclk_per_sample) \ 816 (BURST_BASEFREQ_HZ / bclkdiv / bclk_per_sample) 817 static int dac33_hw_params(struct snd_pcm_substream *substream, 818 struct snd_pcm_hw_params *params, 819 struct snd_soc_dai *dai) 820 { 821 struct snd_soc_component *component = dai->component; 822 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 823 824 /* Check parameters for validity */ 825 switch (params_rate(params)) { 826 case 44100: 827 case 48000: 828 break; 829 default: 830 dev_err(component->dev, "unsupported rate %d\n", 831 params_rate(params)); 832 return -EINVAL; 833 } 834 835 switch (params_width(params)) { 836 case 16: 837 dac33->fifo_size = DAC33_FIFO_SIZE_16BIT; 838 dac33->burst_rate = CALC_BURST_RATE(dac33->burst_bclkdiv, 32); 839 break; 840 case 32: 841 dac33->fifo_size = DAC33_FIFO_SIZE_24BIT; 842 dac33->burst_rate = CALC_BURST_RATE(dac33->burst_bclkdiv, 64); 843 break; 844 default: 845 dev_err(component->dev, "unsupported width %d\n", 846 params_width(params)); 847 return -EINVAL; 848 } 849 850 return 0; 851 } 852 853 #define CALC_OSCSET(rate, refclk) ( \ 854 ((((rate * 10000) / refclk) * 4096) + 7000) / 10000) 855 #define CALC_RATIOSET(rate, refclk) ( \ 856 ((((refclk * 100000) / rate) * 16384) + 50000) / 100000) 857 858 /* 859 * tlv320dac33 is strict on the sequence of the register writes, if the register 860 * writes happens in different order, than dac33 might end up in unknown state. 861 * Use the known, working sequence of register writes to initialize the dac33. 862 */ 863 static int dac33_prepare_chip(struct snd_pcm_substream *substream, 864 struct snd_soc_component *component) 865 { 866 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 867 unsigned int oscset, ratioset, pwr_ctrl, reg_tmp; 868 u8 aictrl_a, aictrl_b, fifoctrl_a; 869 870 switch (substream->runtime->rate) { 871 case 44100: 872 case 48000: 873 oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk); 874 ratioset = CALC_RATIOSET(substream->runtime->rate, 875 dac33->refclk); 876 break; 877 default: 878 dev_err(component->dev, "unsupported rate %d\n", 879 substream->runtime->rate); 880 return -EINVAL; 881 } 882 883 884 aictrl_a = dac33_read_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_A); 885 aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK); 886 /* Read FIFO control A, and clear FIFO flush bit */ 887 fifoctrl_a = dac33_read_reg_cache(component, DAC33_FIFO_CTRL_A); 888 fifoctrl_a &= ~DAC33_FIFOFLUSH; 889 890 fifoctrl_a &= ~DAC33_WIDTH; 891 switch (substream->runtime->format) { 892 case SNDRV_PCM_FORMAT_S16_LE: 893 aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16); 894 fifoctrl_a |= DAC33_WIDTH; 895 break; 896 case SNDRV_PCM_FORMAT_S32_LE: 897 aictrl_a |= (DAC33_NCYCL_32 | DAC33_WLEN_24); 898 break; 899 default: 900 dev_err(component->dev, "unsupported format %d\n", 901 substream->runtime->format); 902 return -EINVAL; 903 } 904 905 mutex_lock(&dac33->mutex); 906 907 if (!dac33->chip_power) { 908 /* 909 * Chip is not powered yet. 910 * Do the init in the dac33_set_bias_level later. 911 */ 912 mutex_unlock(&dac33->mutex); 913 return 0; 914 } 915 916 dac33_soft_power(component, 0); 917 dac33_soft_power(component, 1); 918 919 reg_tmp = dac33_read_reg_cache(component, DAC33_INT_OSC_CTRL); 920 dac33_write(component, DAC33_INT_OSC_CTRL, reg_tmp); 921 922 /* Write registers 0x08 and 0x09 (MSB, LSB) */ 923 dac33_write16(component, DAC33_INT_OSC_FREQ_RAT_A, oscset); 924 925 /* OSC calibration time */ 926 dac33_write(component, DAC33_CALIB_TIME, 96); 927 928 /* adjustment treshold & step */ 929 dac33_write(component, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) | 930 DAC33_ADJSTEP(1)); 931 932 /* div=4 / gain=1 / div */ 933 dac33_write(component, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4)); 934 935 pwr_ctrl = dac33_read_reg_cache(component, DAC33_PWR_CTRL); 936 pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB; 937 dac33_write(component, DAC33_PWR_CTRL, pwr_ctrl); 938 939 dac33_oscwait(component); 940 941 if (dac33->fifo_mode) { 942 /* Generic for all FIFO modes */ 943 /* 50-51 : ASRC Control registers */ 944 dac33_write(component, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1)); 945 dac33_write(component, DAC33_ASRC_CTRL_B, 1); /* ??? */ 946 947 /* Write registers 0x34 and 0x35 (MSB, LSB) */ 948 dac33_write16(component, DAC33_SRC_REF_CLK_RATIO_A, ratioset); 949 950 /* Set interrupts to high active */ 951 dac33_write(component, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH); 952 } else { 953 /* FIFO bypass mode */ 954 /* 50-51 : ASRC Control registers */ 955 dac33_write(component, DAC33_ASRC_CTRL_A, DAC33_SRCBYP); 956 dac33_write(component, DAC33_ASRC_CTRL_B, 0); /* ??? */ 957 } 958 959 /* Interrupt behaviour configuration */ 960 switch (dac33->fifo_mode) { 961 case DAC33_FIFO_MODE1: 962 dac33_write(component, DAC33_FIFO_IRQ_MODE_B, 963 DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL)); 964 break; 965 case DAC33_FIFO_MODE7: 966 dac33_write(component, DAC33_FIFO_IRQ_MODE_A, 967 DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL)); 968 break; 969 default: 970 /* in FIFO bypass mode, the interrupts are not used */ 971 break; 972 } 973 974 aictrl_b = dac33_read_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_B); 975 976 switch (dac33->fifo_mode) { 977 case DAC33_FIFO_MODE1: 978 /* 979 * For mode1: 980 * Disable the FIFO bypass (Enable the use of FIFO) 981 * Select nSample mode 982 * BCLK is only running when data is needed by DAC33 983 */ 984 fifoctrl_a &= ~DAC33_FBYPAS; 985 fifoctrl_a &= ~DAC33_FAUTO; 986 if (dac33->keep_bclk) 987 aictrl_b |= DAC33_BCLKON; 988 else 989 aictrl_b &= ~DAC33_BCLKON; 990 break; 991 case DAC33_FIFO_MODE7: 992 /* 993 * For mode1: 994 * Disable the FIFO bypass (Enable the use of FIFO) 995 * Select Threshold mode 996 * BCLK is only running when data is needed by DAC33 997 */ 998 fifoctrl_a &= ~DAC33_FBYPAS; 999 fifoctrl_a |= DAC33_FAUTO; 1000 if (dac33->keep_bclk) 1001 aictrl_b |= DAC33_BCLKON; 1002 else 1003 aictrl_b &= ~DAC33_BCLKON; 1004 break; 1005 default: 1006 /* 1007 * For FIFO bypass mode: 1008 * Enable the FIFO bypass (Disable the FIFO use) 1009 * Set the BCLK as continuous 1010 */ 1011 fifoctrl_a |= DAC33_FBYPAS; 1012 aictrl_b |= DAC33_BCLKON; 1013 break; 1014 } 1015 1016 dac33_write(component, DAC33_FIFO_CTRL_A, fifoctrl_a); 1017 dac33_write(component, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a); 1018 dac33_write(component, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b); 1019 1020 /* 1021 * BCLK divide ratio 1022 * 0: 1.5 1023 * 1: 1 1024 * 2: 2 1025 * ... 1026 * 254: 254 1027 * 255: 255 1028 */ 1029 if (dac33->fifo_mode) 1030 dac33_write(component, DAC33_SER_AUDIOIF_CTRL_C, 1031 dac33->burst_bclkdiv); 1032 else 1033 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE) 1034 dac33_write(component, DAC33_SER_AUDIOIF_CTRL_C, 32); 1035 else 1036 dac33_write(component, DAC33_SER_AUDIOIF_CTRL_C, 16); 1037 1038 switch (dac33->fifo_mode) { 1039 case DAC33_FIFO_MODE1: 1040 dac33_write16(component, DAC33_ATHR_MSB, 1041 DAC33_THRREG(dac33->alarm_threshold)); 1042 break; 1043 case DAC33_FIFO_MODE7: 1044 /* 1045 * Configure the threshold levels, and leave 10 sample space 1046 * at the bottom, and also at the top of the FIFO 1047 */ 1048 dac33_write16(component, DAC33_UTHR_MSB, DAC33_THRREG(dac33->uthr)); 1049 dac33_write16(component, DAC33_LTHR_MSB, 1050 DAC33_THRREG(DAC33_MODE7_MARGIN)); 1051 break; 1052 default: 1053 break; 1054 } 1055 1056 mutex_unlock(&dac33->mutex); 1057 1058 return 0; 1059 } 1060 1061 static void dac33_calculate_times(struct snd_pcm_substream *substream, 1062 struct snd_soc_component *component) 1063 { 1064 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1065 unsigned int period_size = substream->runtime->period_size; 1066 unsigned int rate = substream->runtime->rate; 1067 unsigned int nsample_limit; 1068 1069 /* In bypass mode we don't need to calculate */ 1070 if (!dac33->fifo_mode) 1071 return; 1072 1073 switch (dac33->fifo_mode) { 1074 case DAC33_FIFO_MODE1: 1075 /* Number of samples under i2c latency */ 1076 dac33->alarm_threshold = US_TO_SAMPLES(rate, 1077 dac33->mode1_latency); 1078 nsample_limit = dac33->fifo_size - dac33->alarm_threshold; 1079 1080 if (period_size <= dac33->alarm_threshold) 1081 /* 1082 * Configure nSamaple to number of periods, 1083 * which covers the latency requironment. 1084 */ 1085 dac33->nsample = period_size * 1086 ((dac33->alarm_threshold / period_size) + 1087 ((dac33->alarm_threshold % period_size) ? 1088 1 : 0)); 1089 else if (period_size > nsample_limit) 1090 dac33->nsample = nsample_limit; 1091 else 1092 dac33->nsample = period_size; 1093 1094 dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate, 1095 dac33->nsample); 1096 dac33->t_stamp1 = 0; 1097 dac33->t_stamp2 = 0; 1098 break; 1099 case DAC33_FIFO_MODE7: 1100 dac33->uthr = UTHR_FROM_PERIOD_SIZE(period_size, rate, 1101 dac33->burst_rate) + 9; 1102 if (dac33->uthr > (dac33->fifo_size - DAC33_MODE7_MARGIN)) 1103 dac33->uthr = dac33->fifo_size - DAC33_MODE7_MARGIN; 1104 if (dac33->uthr < (DAC33_MODE7_MARGIN + 10)) 1105 dac33->uthr = (DAC33_MODE7_MARGIN + 10); 1106 1107 dac33->mode7_us_to_lthr = 1108 SAMPLES_TO_US(substream->runtime->rate, 1109 dac33->uthr - DAC33_MODE7_MARGIN + 1); 1110 dac33->t_stamp1 = 0; 1111 break; 1112 default: 1113 break; 1114 } 1115 1116 } 1117 1118 static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd, 1119 struct snd_soc_dai *dai) 1120 { 1121 struct snd_soc_component *component = dai->component; 1122 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1123 int ret = 0; 1124 1125 switch (cmd) { 1126 case SNDRV_PCM_TRIGGER_START: 1127 case SNDRV_PCM_TRIGGER_RESUME: 1128 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1129 if (dac33->fifo_mode) { 1130 dac33->state = DAC33_PREFILL; 1131 schedule_work(&dac33->work); 1132 } 1133 break; 1134 case SNDRV_PCM_TRIGGER_STOP: 1135 case SNDRV_PCM_TRIGGER_SUSPEND: 1136 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1137 if (dac33->fifo_mode) { 1138 dac33->state = DAC33_FLUSH; 1139 schedule_work(&dac33->work); 1140 } 1141 break; 1142 default: 1143 ret = -EINVAL; 1144 } 1145 1146 return ret; 1147 } 1148 1149 static snd_pcm_sframes_t dac33_dai_delay( 1150 struct snd_pcm_substream *substream, 1151 struct snd_soc_dai *dai) 1152 { 1153 struct snd_soc_component *component = dai->component; 1154 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1155 unsigned long long t0, t1, t_now; 1156 unsigned int time_delta, uthr; 1157 int samples_out, samples_in, samples; 1158 snd_pcm_sframes_t delay = 0; 1159 unsigned long flags; 1160 1161 switch (dac33->fifo_mode) { 1162 case DAC33_FIFO_BYPASS: 1163 break; 1164 case DAC33_FIFO_MODE1: 1165 spin_lock_irqsave(&dac33->lock, flags); 1166 t0 = dac33->t_stamp1; 1167 t1 = dac33->t_stamp2; 1168 spin_unlock_irqrestore(&dac33->lock, flags); 1169 t_now = ktime_to_us(ktime_get()); 1170 1171 /* We have not started to fill the FIFO yet, delay is 0 */ 1172 if (!t1) 1173 goto out; 1174 1175 if (t0 > t1) { 1176 /* 1177 * Phase 1: 1178 * After Alarm threshold, and before nSample write 1179 */ 1180 time_delta = t_now - t0; 1181 samples_out = time_delta ? US_TO_SAMPLES( 1182 substream->runtime->rate, 1183 time_delta) : 0; 1184 1185 if (likely(dac33->alarm_threshold > samples_out)) 1186 delay = dac33->alarm_threshold - samples_out; 1187 else 1188 delay = 0; 1189 } else if ((t_now - t1) <= dac33->mode1_us_burst) { 1190 /* 1191 * Phase 2: 1192 * After nSample write (during burst operation) 1193 */ 1194 time_delta = t_now - t0; 1195 samples_out = time_delta ? US_TO_SAMPLES( 1196 substream->runtime->rate, 1197 time_delta) : 0; 1198 1199 time_delta = t_now - t1; 1200 samples_in = time_delta ? US_TO_SAMPLES( 1201 dac33->burst_rate, 1202 time_delta) : 0; 1203 1204 samples = dac33->alarm_threshold; 1205 samples += (samples_in - samples_out); 1206 1207 if (likely(samples > 0)) 1208 delay = samples; 1209 else 1210 delay = 0; 1211 } else { 1212 /* 1213 * Phase 3: 1214 * After burst operation, before next alarm threshold 1215 */ 1216 time_delta = t_now - t0; 1217 samples_out = time_delta ? US_TO_SAMPLES( 1218 substream->runtime->rate, 1219 time_delta) : 0; 1220 1221 samples_in = dac33->nsample; 1222 samples = dac33->alarm_threshold; 1223 samples += (samples_in - samples_out); 1224 1225 if (likely(samples > 0)) 1226 delay = samples > dac33->fifo_size ? 1227 dac33->fifo_size : samples; 1228 else 1229 delay = 0; 1230 } 1231 break; 1232 case DAC33_FIFO_MODE7: 1233 spin_lock_irqsave(&dac33->lock, flags); 1234 t0 = dac33->t_stamp1; 1235 uthr = dac33->uthr; 1236 spin_unlock_irqrestore(&dac33->lock, flags); 1237 t_now = ktime_to_us(ktime_get()); 1238 1239 /* We have not started to fill the FIFO yet, delay is 0 */ 1240 if (!t0) 1241 goto out; 1242 1243 if (t_now <= t0) { 1244 /* 1245 * Either the timestamps are messed or equal. Report 1246 * maximum delay 1247 */ 1248 delay = uthr; 1249 goto out; 1250 } 1251 1252 time_delta = t_now - t0; 1253 if (time_delta <= dac33->mode7_us_to_lthr) { 1254 /* 1255 * Phase 1: 1256 * After burst (draining phase) 1257 */ 1258 samples_out = US_TO_SAMPLES( 1259 substream->runtime->rate, 1260 time_delta); 1261 1262 if (likely(uthr > samples_out)) 1263 delay = uthr - samples_out; 1264 else 1265 delay = 0; 1266 } else { 1267 /* 1268 * Phase 2: 1269 * During burst operation 1270 */ 1271 time_delta = time_delta - dac33->mode7_us_to_lthr; 1272 1273 samples_out = US_TO_SAMPLES( 1274 substream->runtime->rate, 1275 time_delta); 1276 samples_in = US_TO_SAMPLES( 1277 dac33->burst_rate, 1278 time_delta); 1279 delay = DAC33_MODE7_MARGIN + samples_in - samples_out; 1280 1281 if (unlikely(delay > uthr)) 1282 delay = uthr; 1283 } 1284 break; 1285 default: 1286 dev_warn(component->dev, "Unhandled FIFO mode: %d\n", 1287 dac33->fifo_mode); 1288 break; 1289 } 1290 out: 1291 return delay; 1292 } 1293 1294 static int dac33_set_dai_sysclk(struct snd_soc_dai *codec_dai, 1295 int clk_id, unsigned int freq, int dir) 1296 { 1297 struct snd_soc_component *component = codec_dai->component; 1298 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1299 u8 ioc_reg, asrcb_reg; 1300 1301 ioc_reg = dac33_read_reg_cache(component, DAC33_INT_OSC_CTRL); 1302 asrcb_reg = dac33_read_reg_cache(component, DAC33_ASRC_CTRL_B); 1303 switch (clk_id) { 1304 case TLV320DAC33_MCLK: 1305 ioc_reg |= DAC33_REFSEL; 1306 asrcb_reg |= DAC33_SRCREFSEL; 1307 break; 1308 case TLV320DAC33_SLEEPCLK: 1309 ioc_reg &= ~DAC33_REFSEL; 1310 asrcb_reg &= ~DAC33_SRCREFSEL; 1311 break; 1312 default: 1313 dev_err(component->dev, "Invalid clock ID (%d)\n", clk_id); 1314 break; 1315 } 1316 dac33->refclk = freq; 1317 1318 dac33_write_reg_cache(component, DAC33_INT_OSC_CTRL, ioc_reg); 1319 dac33_write_reg_cache(component, DAC33_ASRC_CTRL_B, asrcb_reg); 1320 1321 return 0; 1322 } 1323 1324 static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai, 1325 unsigned int fmt) 1326 { 1327 struct snd_soc_component *component = codec_dai->component; 1328 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1329 u8 aictrl_a, aictrl_b; 1330 1331 aictrl_a = dac33_read_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_A); 1332 aictrl_b = dac33_read_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_B); 1333 1334 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 1335 case SND_SOC_DAIFMT_CBP_CFP: 1336 aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK); 1337 break; 1338 case SND_SOC_DAIFMT_CBC_CFC: 1339 if (dac33->fifo_mode) { 1340 dev_err(component->dev, "FIFO mode requires provider mode\n"); 1341 return -EINVAL; 1342 } else 1343 aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK); 1344 break; 1345 default: 1346 return -EINVAL; 1347 } 1348 1349 aictrl_a &= ~DAC33_AFMT_MASK; 1350 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 1351 case SND_SOC_DAIFMT_I2S: 1352 aictrl_a |= DAC33_AFMT_I2S; 1353 break; 1354 case SND_SOC_DAIFMT_DSP_A: 1355 aictrl_a |= DAC33_AFMT_DSP; 1356 aictrl_b &= ~DAC33_DATA_DELAY_MASK; 1357 aictrl_b |= DAC33_DATA_DELAY(0); 1358 break; 1359 case SND_SOC_DAIFMT_RIGHT_J: 1360 aictrl_a |= DAC33_AFMT_RIGHT_J; 1361 break; 1362 case SND_SOC_DAIFMT_LEFT_J: 1363 aictrl_a |= DAC33_AFMT_LEFT_J; 1364 break; 1365 default: 1366 dev_err(component->dev, "Unsupported format (%u)\n", 1367 fmt & SND_SOC_DAIFMT_FORMAT_MASK); 1368 return -EINVAL; 1369 } 1370 1371 dac33_write_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a); 1372 dac33_write_reg_cache(component, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b); 1373 1374 return 0; 1375 } 1376 1377 static int dac33_soc_probe(struct snd_soc_component *component) 1378 { 1379 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1380 int ret = 0; 1381 1382 dac33->component = component; 1383 1384 /* Read the tlv320dac33 ID registers */ 1385 ret = dac33_hard_power(component, 1); 1386 if (ret != 0) { 1387 dev_err(component->dev, "Failed to power up component: %d\n", ret); 1388 goto err_power; 1389 } 1390 ret = dac33_read_id(component); 1391 dac33_hard_power(component, 0); 1392 1393 if (ret < 0) { 1394 dev_err(component->dev, "Failed to read chip ID: %d\n", ret); 1395 ret = -ENODEV; 1396 goto err_power; 1397 } 1398 1399 /* Check if the IRQ number is valid and request it */ 1400 if (dac33->irq >= 0) { 1401 ret = request_irq(dac33->irq, dac33_interrupt_handler, 1402 IRQF_TRIGGER_RISING, 1403 component->name, component); 1404 if (ret < 0) { 1405 dev_err(component->dev, "Could not request IRQ%d (%d)\n", 1406 dac33->irq, ret); 1407 dac33->irq = -1; 1408 } 1409 if (dac33->irq != -1) { 1410 INIT_WORK(&dac33->work, dac33_work); 1411 } 1412 } 1413 1414 /* Only add the FIFO controls, if we have valid IRQ number */ 1415 if (dac33->irq >= 0) 1416 snd_soc_add_component_controls(component, dac33_mode_snd_controls, 1417 ARRAY_SIZE(dac33_mode_snd_controls)); 1418 1419 err_power: 1420 return ret; 1421 } 1422 1423 static void dac33_soc_remove(struct snd_soc_component *component) 1424 { 1425 struct tlv320dac33_priv *dac33 = snd_soc_component_get_drvdata(component); 1426 1427 if (dac33->irq >= 0) { 1428 free_irq(dac33->irq, dac33->component); 1429 flush_work(&dac33->work); 1430 } 1431 } 1432 1433 static const struct snd_soc_component_driver soc_component_dev_tlv320dac33 = { 1434 .read = dac33_read_reg_cache, 1435 .write = dac33_write_locked, 1436 .set_bias_level = dac33_set_bias_level, 1437 .probe = dac33_soc_probe, 1438 .remove = dac33_soc_remove, 1439 .controls = dac33_snd_controls, 1440 .num_controls = ARRAY_SIZE(dac33_snd_controls), 1441 .dapm_widgets = dac33_dapm_widgets, 1442 .num_dapm_widgets = ARRAY_SIZE(dac33_dapm_widgets), 1443 .dapm_routes = audio_map, 1444 .num_dapm_routes = ARRAY_SIZE(audio_map), 1445 .use_pmdown_time = 1, 1446 .endianness = 1, 1447 }; 1448 1449 #define DAC33_RATES (SNDRV_PCM_RATE_44100 | \ 1450 SNDRV_PCM_RATE_48000) 1451 #define DAC33_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE) 1452 1453 static const struct snd_soc_dai_ops dac33_dai_ops = { 1454 .startup = dac33_startup, 1455 .shutdown = dac33_shutdown, 1456 .hw_params = dac33_hw_params, 1457 .trigger = dac33_pcm_trigger, 1458 .delay = dac33_dai_delay, 1459 .set_sysclk = dac33_set_dai_sysclk, 1460 .set_fmt = dac33_set_dai_fmt, 1461 }; 1462 1463 static struct snd_soc_dai_driver dac33_dai = { 1464 .name = "tlv320dac33-hifi", 1465 .playback = { 1466 .stream_name = "Playback", 1467 .channels_min = 2, 1468 .channels_max = 2, 1469 .rates = DAC33_RATES, 1470 .formats = DAC33_FORMATS, 1471 .sig_bits = 24, 1472 }, 1473 .ops = &dac33_dai_ops, 1474 }; 1475 1476 static int dac33_i2c_probe(struct i2c_client *client) 1477 { 1478 struct tlv320dac33_priv *dac33; 1479 int ret, i; 1480 1481 dac33 = devm_kzalloc(&client->dev, struct_size(dac33, reg_cache, ARRAY_SIZE(dac33_reg)), 1482 GFP_KERNEL); 1483 if (dac33 == NULL) 1484 return -ENOMEM; 1485 1486 memcpy(dac33->reg_cache, dac33_reg, ARRAY_SIZE(dac33_reg)); 1487 1488 dac33->i2c = client; 1489 mutex_init(&dac33->mutex); 1490 spin_lock_init(&dac33->lock); 1491 1492 i2c_set_clientdata(client, dac33); 1493 1494 if (!dac33->burst_bclkdiv) 1495 dac33->burst_bclkdiv = 8; 1496 if (!dac33->mode1_latency) 1497 dac33->mode1_latency = 10000; /* 10ms */ 1498 dac33->irq = client->irq; 1499 /* Disable FIFO use by default */ 1500 dac33->fifo_mode = DAC33_FIFO_BYPASS; 1501 1502 /* request optional reset GPIO */ 1503 dac33->reset_gpiod = 1504 devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW); 1505 if (IS_ERR(dac33->reset_gpiod)) { 1506 ret = PTR_ERR(dac33->reset_gpiod); 1507 dev_err_probe(&client->dev, ret, 1508 "Failed to get reset GPIO\n"); 1509 goto err; 1510 } 1511 1512 for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++) 1513 dac33->supplies[i].supply = dac33_supply_names[i]; 1514 1515 ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(dac33->supplies), 1516 dac33->supplies); 1517 1518 if (ret != 0) { 1519 dev_err(&client->dev, "Failed to request supplies: %d\n", ret); 1520 goto err; 1521 } 1522 1523 ret = devm_snd_soc_register_component(&client->dev, 1524 &soc_component_dev_tlv320dac33, &dac33_dai, 1); 1525 if (ret < 0) 1526 goto err; 1527 1528 return ret; 1529 1530 err: 1531 return ret; 1532 } 1533 1534 static void dac33_i2c_remove(struct i2c_client *client) 1535 { 1536 struct tlv320dac33_priv *dac33 = i2c_get_clientdata(client); 1537 1538 if (unlikely(dac33->chip_power)) 1539 dac33_hard_power(dac33->component, 0); 1540 } 1541 1542 static const struct i2c_device_id tlv320dac33_i2c_id[] = { 1543 { 1544 .name = "tlv320dac33", 1545 .driver_data = 0, 1546 }, 1547 { }, 1548 }; 1549 MODULE_DEVICE_TABLE(i2c, tlv320dac33_i2c_id); 1550 1551 static struct i2c_driver tlv320dac33_i2c_driver = { 1552 .driver = { 1553 .name = "tlv320dac33-codec", 1554 }, 1555 .probe = dac33_i2c_probe, 1556 .remove = dac33_i2c_remove, 1557 .id_table = tlv320dac33_i2c_id, 1558 }; 1559 1560 module_i2c_driver(tlv320dac33_i2c_driver); 1561 1562 MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver"); 1563 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>"); 1564 MODULE_LICENSE("GPL"); 1565