1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ALSA SoC TWL6040 codec driver 4 * 5 * Author: Misael Lopez Cruz <x0052729@ti.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/moduleparam.h> 10 #include <linux/init.h> 11 #include <linux/cleanup.h> 12 #include <linux/delay.h> 13 #include <linux/pm.h> 14 #include <linux/platform_device.h> 15 #include <linux/slab.h> 16 #include <linux/mfd/twl6040.h> 17 18 #include <sound/core.h> 19 #include <sound/pcm.h> 20 #include <sound/pcm_params.h> 21 #include <sound/soc.h> 22 #include <sound/soc-dapm.h> 23 #include <sound/initval.h> 24 #include <sound/tlv.h> 25 26 #include "twl6040.h" 27 28 enum twl6040_dai_id { 29 TWL6040_DAI_LEGACY = 0, 30 TWL6040_DAI_UL, 31 TWL6040_DAI_DL1, 32 TWL6040_DAI_DL2, 33 TWL6040_DAI_VIB, 34 }; 35 36 #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000 37 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE) 38 39 #define TWL6040_OUTHS_0dB 0x00 40 #define TWL6040_OUTHS_M30dB 0x0F 41 #define TWL6040_OUTHF_0dB 0x03 42 #define TWL6040_OUTHF_M52dB 0x1D 43 44 #define TWL6040_CACHEREGNUM (TWL6040_REG_STATUS + 1) 45 46 struct twl6040_jack_data { 47 struct snd_soc_jack *jack; 48 struct delayed_work work; 49 int report; 50 }; 51 52 /* codec private data */ 53 struct twl6040_data { 54 int plug_irq; 55 int codec_powered; 56 int pll; 57 int pll_power_mode; 58 int hs_power_mode; 59 int hs_power_mode_locked; 60 bool dl1_unmuted; 61 bool dl2_unmuted; 62 u8 dl12_cache[TWL6040_REG_HFRCTL - TWL6040_REG_HSLCTL + 1]; 63 unsigned int clk_in; 64 unsigned int sysclk; 65 struct twl6040_jack_data hs_jack; 66 struct snd_soc_component *component; 67 struct mutex mutex; 68 }; 69 70 /* set of rates for each pll: low-power and high-performance */ 71 static const unsigned int lp_rates[] = { 72 8000, 73 11250, 74 16000, 75 22500, 76 32000, 77 44100, 78 48000, 79 88200, 80 96000, 81 }; 82 83 static const unsigned int hp_rates[] = { 84 8000, 85 16000, 86 32000, 87 48000, 88 96000, 89 }; 90 91 static const struct snd_pcm_hw_constraint_list sysclk_constraints[] = { 92 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, }, 93 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, }, 94 }; 95 96 #define to_twl6040(component) dev_get_drvdata((component)->dev->parent) 97 98 static unsigned int twl6040_read(struct snd_soc_component *component, unsigned int reg) 99 { 100 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 101 struct twl6040 *twl6040 = to_twl6040(component); 102 u8 value; 103 104 if (reg >= TWL6040_CACHEREGNUM) 105 return -EIO; 106 107 switch (reg) { 108 case TWL6040_REG_HSLCTL: 109 case TWL6040_REG_HSRCTL: 110 case TWL6040_REG_EARCTL: 111 case TWL6040_REG_HFLCTL: 112 case TWL6040_REG_HFRCTL: 113 value = priv->dl12_cache[reg - TWL6040_REG_HSLCTL]; 114 break; 115 default: 116 value = twl6040_reg_read(twl6040, reg); 117 break; 118 } 119 120 return value; 121 } 122 123 static bool twl6040_can_write_to_chip(struct snd_soc_component *component, 124 unsigned int reg) 125 { 126 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 127 128 switch (reg) { 129 case TWL6040_REG_HSLCTL: 130 case TWL6040_REG_HSRCTL: 131 case TWL6040_REG_EARCTL: 132 /* DL1 path */ 133 return priv->dl1_unmuted; 134 case TWL6040_REG_HFLCTL: 135 case TWL6040_REG_HFRCTL: 136 return priv->dl2_unmuted; 137 default: 138 return true; 139 } 140 } 141 142 static inline void twl6040_update_dl12_cache(struct snd_soc_component *component, 143 u8 reg, u8 value) 144 { 145 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 146 147 switch (reg) { 148 case TWL6040_REG_HSLCTL: 149 case TWL6040_REG_HSRCTL: 150 case TWL6040_REG_EARCTL: 151 case TWL6040_REG_HFLCTL: 152 case TWL6040_REG_HFRCTL: 153 priv->dl12_cache[reg - TWL6040_REG_HSLCTL] = value; 154 break; 155 default: 156 break; 157 } 158 } 159 160 static int twl6040_write(struct snd_soc_component *component, 161 unsigned int reg, unsigned int value) 162 { 163 struct twl6040 *twl6040 = to_twl6040(component); 164 165 if (reg >= TWL6040_CACHEREGNUM) 166 return -EIO; 167 168 twl6040_update_dl12_cache(component, reg, value); 169 if (twl6040_can_write_to_chip(component, reg)) 170 return twl6040_reg_write(twl6040, reg, value); 171 else 172 return 0; 173 } 174 175 static void twl6040_init_chip(struct snd_soc_component *component) 176 { 177 twl6040_read(component, TWL6040_REG_TRIM1); 178 twl6040_read(component, TWL6040_REG_TRIM2); 179 twl6040_read(component, TWL6040_REG_TRIM3); 180 twl6040_read(component, TWL6040_REG_HSOTRIM); 181 twl6040_read(component, TWL6040_REG_HFOTRIM); 182 183 /* Change chip defaults */ 184 /* No imput selected for microphone amplifiers */ 185 twl6040_write(component, TWL6040_REG_MICLCTL, 0x18); 186 twl6040_write(component, TWL6040_REG_MICRCTL, 0x18); 187 188 /* 189 * We need to lower the default gain values, so the ramp code 190 * can work correctly for the first playback. 191 * This reduces the pop noise heard at the first playback. 192 */ 193 twl6040_write(component, TWL6040_REG_HSGAIN, 0xff); 194 twl6040_write(component, TWL6040_REG_EARCTL, 0x1e); 195 twl6040_write(component, TWL6040_REG_HFLGAIN, 0x1d); 196 twl6040_write(component, TWL6040_REG_HFRGAIN, 0x1d); 197 twl6040_write(component, TWL6040_REG_LINEGAIN, 0); 198 } 199 200 /* set headset dac and driver power mode */ 201 static int headset_power_mode(struct snd_soc_component *component, int high_perf) 202 { 203 int hslctl, hsrctl; 204 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE; 205 206 hslctl = twl6040_read(component, TWL6040_REG_HSLCTL); 207 hsrctl = twl6040_read(component, TWL6040_REG_HSRCTL); 208 209 if (high_perf) { 210 hslctl &= ~mask; 211 hsrctl &= ~mask; 212 } else { 213 hslctl |= mask; 214 hsrctl |= mask; 215 } 216 217 twl6040_write(component, TWL6040_REG_HSLCTL, hslctl); 218 twl6040_write(component, TWL6040_REG_HSRCTL, hsrctl); 219 220 return 0; 221 } 222 223 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w, 224 struct snd_kcontrol *kcontrol, int event) 225 { 226 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 227 u8 hslctl, hsrctl; 228 229 /* 230 * Workaround for Headset DC offset caused pop noise: 231 * Both HS DAC need to be turned on (before the HS driver) and off at 232 * the same time. 233 */ 234 hslctl = twl6040_read(component, TWL6040_REG_HSLCTL); 235 hsrctl = twl6040_read(component, TWL6040_REG_HSRCTL); 236 if (SND_SOC_DAPM_EVENT_ON(event)) { 237 hslctl |= TWL6040_HSDACENA; 238 hsrctl |= TWL6040_HSDACENA; 239 } else { 240 hslctl &= ~TWL6040_HSDACENA; 241 hsrctl &= ~TWL6040_HSDACENA; 242 } 243 twl6040_write(component, TWL6040_REG_HSLCTL, hslctl); 244 twl6040_write(component, TWL6040_REG_HSRCTL, hsrctl); 245 246 msleep(1); 247 return 0; 248 } 249 250 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w, 251 struct snd_kcontrol *kcontrol, int event) 252 { 253 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 254 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 255 int ret = 0; 256 257 if (SND_SOC_DAPM_EVENT_ON(event)) { 258 /* Earphone doesn't support low power mode */ 259 priv->hs_power_mode_locked = 1; 260 ret = headset_power_mode(component, 1); 261 } else { 262 priv->hs_power_mode_locked = 0; 263 ret = headset_power_mode(component, priv->hs_power_mode); 264 } 265 266 msleep(1); 267 268 return ret; 269 } 270 271 static void twl6040_hs_jack_report(struct snd_soc_component *component, 272 struct snd_soc_jack *jack, int report) 273 { 274 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 275 int status; 276 277 guard(mutex)(&priv->mutex); 278 279 /* Sync status */ 280 status = twl6040_read(component, TWL6040_REG_STATUS); 281 if (status & TWL6040_PLUGCOMP) 282 snd_soc_jack_report(jack, report, report); 283 else 284 snd_soc_jack_report(jack, 0, report); 285 } 286 287 void twl6040_hs_jack_detect(struct snd_soc_component *component, 288 struct snd_soc_jack *jack, int report) 289 { 290 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 291 struct twl6040_jack_data *hs_jack = &priv->hs_jack; 292 293 hs_jack->jack = jack; 294 hs_jack->report = report; 295 296 twl6040_hs_jack_report(component, hs_jack->jack, hs_jack->report); 297 } 298 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect); 299 300 static void twl6040_accessory_work(struct work_struct *work) 301 { 302 struct twl6040_data *priv = container_of(work, 303 struct twl6040_data, hs_jack.work.work); 304 struct snd_soc_component *component = priv->component; 305 struct twl6040_jack_data *hs_jack = &priv->hs_jack; 306 307 twl6040_hs_jack_report(component, hs_jack->jack, hs_jack->report); 308 } 309 310 /* audio interrupt handler */ 311 static irqreturn_t twl6040_audio_handler(int irq, void *data) 312 { 313 struct snd_soc_component *component = data; 314 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 315 316 queue_delayed_work(system_power_efficient_wq, 317 &priv->hs_jack.work, msecs_to_jiffies(200)); 318 319 return IRQ_HANDLED; 320 } 321 322 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol, 323 struct snd_ctl_elem_value *ucontrol) 324 { 325 struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol); 326 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 327 unsigned int val; 328 329 /* Do not allow changes while Input/FF efect is running */ 330 val = twl6040_read(component, e->reg); 331 if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL)) 332 return -EBUSY; 333 334 return snd_soc_dapm_put_enum_double(kcontrol, ucontrol); 335 } 336 337 /* 338 * MICATT volume control: 339 * from -6 to 0 dB in 6 dB steps 340 */ 341 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0); 342 343 /* 344 * MICGAIN volume control: 345 * from 6 to 30 dB in 6 dB steps 346 */ 347 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0); 348 349 /* 350 * AFMGAIN volume control: 351 * from -18 to 24 dB in 6 dB steps 352 */ 353 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0); 354 355 /* 356 * HSGAIN volume control: 357 * from -30 to 0 dB in 2 dB steps 358 */ 359 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0); 360 361 /* 362 * HFGAIN volume control: 363 * from -52 to 6 dB in 2 dB steps 364 */ 365 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0); 366 367 /* 368 * EPGAIN volume control: 369 * from -24 to 6 dB in 2 dB steps 370 */ 371 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0); 372 373 /* Left analog microphone selection */ 374 static const char *twl6040_amicl_texts[] = 375 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"}; 376 377 /* Right analog microphone selection */ 378 static const char *twl6040_amicr_texts[] = 379 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"}; 380 381 static const struct soc_enum twl6040_enum[] = { 382 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 383 ARRAY_SIZE(twl6040_amicl_texts), twl6040_amicl_texts), 384 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 385 ARRAY_SIZE(twl6040_amicr_texts), twl6040_amicr_texts), 386 }; 387 388 static const char *twl6040_hs_texts[] = { 389 "Off", "HS DAC", "Line-In amp" 390 }; 391 392 static const struct soc_enum twl6040_hs_enum[] = { 393 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts), 394 twl6040_hs_texts), 395 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts), 396 twl6040_hs_texts), 397 }; 398 399 static const char *twl6040_hf_texts[] = { 400 "Off", "HF DAC", "Line-In amp" 401 }; 402 403 static const struct soc_enum twl6040_hf_enum[] = { 404 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts), 405 twl6040_hf_texts), 406 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts), 407 twl6040_hf_texts), 408 }; 409 410 static const char *twl6040_vibrapath_texts[] = { 411 "Input FF", "Audio PDM" 412 }; 413 414 static const struct soc_enum twl6040_vibra_enum[] = { 415 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1, 416 ARRAY_SIZE(twl6040_vibrapath_texts), 417 twl6040_vibrapath_texts), 418 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1, 419 ARRAY_SIZE(twl6040_vibrapath_texts), 420 twl6040_vibrapath_texts), 421 }; 422 423 static const struct snd_kcontrol_new amicl_control = 424 SOC_DAPM_ENUM("Route", twl6040_enum[0]); 425 426 static const struct snd_kcontrol_new amicr_control = 427 SOC_DAPM_ENUM("Route", twl6040_enum[1]); 428 429 /* Headset DAC playback switches */ 430 static const struct snd_kcontrol_new hsl_mux_controls = 431 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]); 432 433 static const struct snd_kcontrol_new hsr_mux_controls = 434 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]); 435 436 /* Handsfree DAC playback switches */ 437 static const struct snd_kcontrol_new hfl_mux_controls = 438 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]); 439 440 static const struct snd_kcontrol_new hfr_mux_controls = 441 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]); 442 443 static const struct snd_kcontrol_new ep_path_enable_control = 444 SOC_DAPM_SINGLE_VIRT("Switch", 1); 445 446 static const struct snd_kcontrol_new auxl_switch_control = 447 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0); 448 449 static const struct snd_kcontrol_new auxr_switch_control = 450 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0); 451 452 /* Vibra playback switches */ 453 static const struct snd_kcontrol_new vibral_mux_controls = 454 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0], 455 snd_soc_dapm_get_enum_double, 456 twl6040_soc_dapm_put_vibra_enum); 457 458 static const struct snd_kcontrol_new vibrar_mux_controls = 459 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1], 460 snd_soc_dapm_get_enum_double, 461 twl6040_soc_dapm_put_vibra_enum); 462 463 /* Headset power mode */ 464 static const char *twl6040_power_mode_texts[] = { 465 "Low-Power", "High-Performance", 466 }; 467 468 static SOC_ENUM_SINGLE_EXT_DECL(twl6040_power_mode_enum, 469 twl6040_power_mode_texts); 470 471 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol, 472 struct snd_ctl_elem_value *ucontrol) 473 { 474 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 475 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 476 477 ucontrol->value.enumerated.item[0] = priv->hs_power_mode; 478 479 return 0; 480 } 481 482 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol, 483 struct snd_ctl_elem_value *ucontrol) 484 { 485 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 486 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 487 int high_perf = ucontrol->value.enumerated.item[0]; 488 int ret = 0; 489 490 if (!priv->hs_power_mode_locked) 491 ret = headset_power_mode(component, high_perf); 492 493 if (!ret) 494 priv->hs_power_mode = high_perf; 495 496 return ret; 497 } 498 499 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol, 500 struct snd_ctl_elem_value *ucontrol) 501 { 502 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 503 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 504 505 ucontrol->value.enumerated.item[0] = priv->pll_power_mode; 506 507 return 0; 508 } 509 510 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol, 511 struct snd_ctl_elem_value *ucontrol) 512 { 513 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 514 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 515 516 priv->pll_power_mode = ucontrol->value.enumerated.item[0]; 517 518 return 0; 519 } 520 521 int twl6040_get_dl1_gain(struct snd_soc_component *component) 522 { 523 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); 524 525 if (snd_soc_dapm_get_pin_status(dapm, "EP")) 526 return -1; /* -1dB */ 527 528 if (snd_soc_dapm_get_pin_status(dapm, "HSOR") || 529 snd_soc_dapm_get_pin_status(dapm, "HSOL")) { 530 531 u8 val = twl6040_read(component, TWL6040_REG_HSLCTL); 532 if (val & TWL6040_HSDACMODE) 533 /* HSDACL in LP mode */ 534 return -8; /* -8dB */ 535 else 536 /* HSDACL in HP mode */ 537 return -1; /* -1dB */ 538 } 539 return 0; /* 0dB */ 540 } 541 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain); 542 543 int twl6040_get_clk_id(struct snd_soc_component *component) 544 { 545 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 546 547 return priv->pll_power_mode; 548 } 549 EXPORT_SYMBOL_GPL(twl6040_get_clk_id); 550 551 int twl6040_get_trim_value(struct snd_soc_component *component, enum twl6040_trim trim) 552 { 553 if (unlikely(trim >= TWL6040_TRIM_INVAL)) 554 return -EINVAL; 555 556 return twl6040_read(component, TWL6040_REG_TRIM1 + trim); 557 } 558 EXPORT_SYMBOL_GPL(twl6040_get_trim_value); 559 560 int twl6040_get_hs_step_size(struct snd_soc_component *component) 561 { 562 struct twl6040 *twl6040 = to_twl6040(component); 563 564 if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3) 565 /* For ES under ES_1.3 HS step is 2 mV */ 566 return 2; 567 else 568 /* For ES_1.3 HS step is 1 mV */ 569 return 1; 570 } 571 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size); 572 573 static const struct snd_kcontrol_new twl6040_snd_controls[] = { 574 /* Capture gains */ 575 SOC_DOUBLE_TLV("Capture Preamplifier Volume", 576 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv), 577 SOC_DOUBLE_TLV("Capture Volume", 578 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv), 579 580 /* AFM gains */ 581 SOC_DOUBLE_TLV("Aux FM Volume", 582 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv), 583 584 /* Playback gains */ 585 SOC_DOUBLE_TLV("Headset Playback Volume", 586 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv), 587 SOC_DOUBLE_R_TLV("Handsfree Playback Volume", 588 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv), 589 SOC_SINGLE_TLV("Earphone Playback Volume", 590 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv), 591 592 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum, 593 twl6040_headset_power_get_enum, 594 twl6040_headset_power_put_enum), 595 596 /* Left HS PDM data routed to Right HSDAC */ 597 SOC_SINGLE("Headset Mono to Stereo Playback Switch", 598 TWL6040_REG_HSRCTL, 7, 1, 0), 599 600 /* Left HF PDM data routed to Right HFDAC */ 601 SOC_SINGLE("Handsfree Mono to Stereo Playback Switch", 602 TWL6040_REG_HFRCTL, 5, 1, 0), 603 604 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum, 605 twl6040_pll_get_enum, twl6040_pll_put_enum), 606 }; 607 608 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = { 609 /* Inputs */ 610 SND_SOC_DAPM_INPUT("MAINMIC"), 611 SND_SOC_DAPM_INPUT("HSMIC"), 612 SND_SOC_DAPM_INPUT("SUBMIC"), 613 SND_SOC_DAPM_INPUT("AFML"), 614 SND_SOC_DAPM_INPUT("AFMR"), 615 616 /* Outputs */ 617 SND_SOC_DAPM_OUTPUT("HSOL"), 618 SND_SOC_DAPM_OUTPUT("HSOR"), 619 SND_SOC_DAPM_OUTPUT("HFL"), 620 SND_SOC_DAPM_OUTPUT("HFR"), 621 SND_SOC_DAPM_OUTPUT("EP"), 622 SND_SOC_DAPM_OUTPUT("AUXL"), 623 SND_SOC_DAPM_OUTPUT("AUXR"), 624 SND_SOC_DAPM_OUTPUT("VIBRAL"), 625 SND_SOC_DAPM_OUTPUT("VIBRAR"), 626 627 /* Analog input muxes for the capture amplifiers */ 628 SND_SOC_DAPM_MUX("Analog Left Capture Route", 629 SND_SOC_NOPM, 0, 0, &amicl_control), 630 SND_SOC_DAPM_MUX("Analog Right Capture Route", 631 SND_SOC_NOPM, 0, 0, &amicr_control), 632 633 /* Analog capture PGAs */ 634 SND_SOC_DAPM_PGA("MicAmpL", 635 TWL6040_REG_MICLCTL, 0, 0, NULL, 0), 636 SND_SOC_DAPM_PGA("MicAmpR", 637 TWL6040_REG_MICRCTL, 0, 0, NULL, 0), 638 639 /* Auxiliary FM PGAs */ 640 SND_SOC_DAPM_PGA("AFMAmpL", 641 TWL6040_REG_MICLCTL, 1, 0, NULL, 0), 642 SND_SOC_DAPM_PGA("AFMAmpR", 643 TWL6040_REG_MICRCTL, 1, 0, NULL, 0), 644 645 /* ADCs */ 646 SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0), 647 SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0), 648 649 /* Microphone bias */ 650 SND_SOC_DAPM_SUPPLY("Headset Mic Bias", 651 TWL6040_REG_AMICBCTL, 0, 0, NULL, 0), 652 SND_SOC_DAPM_SUPPLY("Main Mic Bias", 653 TWL6040_REG_AMICBCTL, 4, 0, NULL, 0), 654 SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias", 655 TWL6040_REG_DMICBCTL, 0, 0, NULL, 0), 656 SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias", 657 TWL6040_REG_DMICBCTL, 4, 0, NULL, 0), 658 659 /* DACs */ 660 SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0), 661 SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0), 662 SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0), 663 SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0), 664 /* Virtual DAC for vibra path (DL4 channel) */ 665 SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0), 666 667 SND_SOC_DAPM_MUX("Handsfree Left Playback", 668 SND_SOC_NOPM, 0, 0, &hfl_mux_controls), 669 SND_SOC_DAPM_MUX("Handsfree Right Playback", 670 SND_SOC_NOPM, 0, 0, &hfr_mux_controls), 671 /* Analog playback Muxes */ 672 SND_SOC_DAPM_MUX("Headset Left Playback", 673 SND_SOC_NOPM, 0, 0, &hsl_mux_controls), 674 SND_SOC_DAPM_MUX("Headset Right Playback", 675 SND_SOC_NOPM, 0, 0, &hsr_mux_controls), 676 677 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0, 678 &vibral_mux_controls), 679 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0, 680 &vibrar_mux_controls), 681 682 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0, 683 &ep_path_enable_control), 684 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0, 685 &auxl_switch_control), 686 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0, 687 &auxr_switch_control), 688 689 /* Analog playback drivers */ 690 SND_SOC_DAPM_OUT_DRV("HF Left Driver", 691 TWL6040_REG_HFLCTL, 4, 0, NULL, 0), 692 SND_SOC_DAPM_OUT_DRV("HF Right Driver", 693 TWL6040_REG_HFRCTL, 4, 0, NULL, 0), 694 SND_SOC_DAPM_OUT_DRV("HS Left Driver", 695 TWL6040_REG_HSLCTL, 2, 0, NULL, 0), 696 SND_SOC_DAPM_OUT_DRV("HS Right Driver", 697 TWL6040_REG_HSRCTL, 2, 0, NULL, 0), 698 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver", 699 TWL6040_REG_EARCTL, 0, 0, NULL, 0, 700 twl6040_ep_drv_event, 701 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 702 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver", 703 TWL6040_REG_VIBCTLL, 0, 0, NULL, 0), 704 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver", 705 TWL6040_REG_VIBCTLR, 0, 0, NULL, 0), 706 707 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0, 708 NULL, 0), 709 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0, 710 NULL, 0), 711 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0, 712 twl6040_hs_dac_event, 713 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 714 715 /* Analog playback PGAs */ 716 SND_SOC_DAPM_PGA("HF Left PGA", 717 TWL6040_REG_HFLCTL, 1, 0, NULL, 0), 718 SND_SOC_DAPM_PGA("HF Right PGA", 719 TWL6040_REG_HFRCTL, 1, 0, NULL, 0), 720 721 }; 722 723 static const struct snd_soc_dapm_route intercon[] = { 724 /* Stream -> DAC mapping */ 725 {"HSDAC Left", NULL, "Legacy Playback"}, 726 {"HSDAC Left", NULL, "Headset Playback"}, 727 {"HSDAC Right", NULL, "Legacy Playback"}, 728 {"HSDAC Right", NULL, "Headset Playback"}, 729 730 {"HFDAC Left", NULL, "Legacy Playback"}, 731 {"HFDAC Left", NULL, "Handsfree Playback"}, 732 {"HFDAC Right", NULL, "Legacy Playback"}, 733 {"HFDAC Right", NULL, "Handsfree Playback"}, 734 735 {"VIBRA DAC", NULL, "Legacy Playback"}, 736 {"VIBRA DAC", NULL, "Vibra Playback"}, 737 738 /* ADC -> Stream mapping */ 739 {"Legacy Capture" , NULL, "ADC Left"}, 740 {"Capture", NULL, "ADC Left"}, 741 {"Legacy Capture", NULL, "ADC Right"}, 742 {"Capture" , NULL, "ADC Right"}, 743 744 /* Capture path */ 745 {"Analog Left Capture Route", "Headset Mic", "HSMIC"}, 746 {"Analog Left Capture Route", "Main Mic", "MAINMIC"}, 747 {"Analog Left Capture Route", "Aux/FM Left", "AFML"}, 748 749 {"Analog Right Capture Route", "Headset Mic", "HSMIC"}, 750 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"}, 751 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"}, 752 753 {"MicAmpL", NULL, "Analog Left Capture Route"}, 754 {"MicAmpR", NULL, "Analog Right Capture Route"}, 755 756 {"ADC Left", NULL, "MicAmpL"}, 757 {"ADC Right", NULL, "MicAmpR"}, 758 759 /* AFM path */ 760 {"AFMAmpL", NULL, "AFML"}, 761 {"AFMAmpR", NULL, "AFMR"}, 762 763 {"HSDAC Left", NULL, "HSDAC Power"}, 764 {"HSDAC Right", NULL, "HSDAC Power"}, 765 766 {"Headset Left Playback", "HS DAC", "HSDAC Left"}, 767 {"Headset Left Playback", "Line-In amp", "AFMAmpL"}, 768 769 {"Headset Right Playback", "HS DAC", "HSDAC Right"}, 770 {"Headset Right Playback", "Line-In amp", "AFMAmpR"}, 771 772 {"HS Left Driver", NULL, "Headset Left Playback"}, 773 {"HS Right Driver", NULL, "Headset Right Playback"}, 774 775 {"HSOL", NULL, "HS Left Driver"}, 776 {"HSOR", NULL, "HS Right Driver"}, 777 778 /* Earphone playback path */ 779 {"Earphone Playback", "Switch", "HSDAC Left"}, 780 {"Earphone Driver", NULL, "Earphone Playback"}, 781 {"EP", NULL, "Earphone Driver"}, 782 783 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"}, 784 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"}, 785 786 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"}, 787 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"}, 788 789 {"HF Left PGA", NULL, "Handsfree Left Playback"}, 790 {"HF Right PGA", NULL, "Handsfree Right Playback"}, 791 792 {"HF Left Driver", NULL, "HF Left PGA"}, 793 {"HF Right Driver", NULL, "HF Right PGA"}, 794 795 {"HFL", NULL, "HF Left Driver"}, 796 {"HFR", NULL, "HF Right Driver"}, 797 798 {"AUXL Playback", "Switch", "HF Left PGA"}, 799 {"AUXR Playback", "Switch", "HF Right PGA"}, 800 801 {"AUXL", NULL, "AUXL Playback"}, 802 {"AUXR", NULL, "AUXR Playback"}, 803 804 /* Vibrator paths */ 805 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"}, 806 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"}, 807 808 {"Vibra Left Driver", NULL, "Vibra Left Playback"}, 809 {"Vibra Right Driver", NULL, "Vibra Right Playback"}, 810 {"Vibra Left Driver", NULL, "Vibra Left Control"}, 811 {"Vibra Right Driver", NULL, "Vibra Right Control"}, 812 813 {"VIBRAL", NULL, "Vibra Left Driver"}, 814 {"VIBRAR", NULL, "Vibra Right Driver"}, 815 }; 816 817 static int twl6040_set_bias_level(struct snd_soc_component *component, 818 enum snd_soc_bias_level level) 819 { 820 struct twl6040 *twl6040 = to_twl6040(component); 821 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 822 int ret = 0; 823 824 switch (level) { 825 case SND_SOC_BIAS_ON: 826 break; 827 case SND_SOC_BIAS_PREPARE: 828 break; 829 case SND_SOC_BIAS_STANDBY: 830 if (priv->codec_powered) { 831 /* Select low power PLL in standby */ 832 ret = twl6040_set_pll(twl6040, TWL6040_SYSCLK_SEL_LPPLL, 833 32768, 19200000); 834 break; 835 } 836 837 ret = twl6040_power(twl6040, 1); 838 if (ret) 839 break; 840 841 priv->codec_powered = 1; 842 843 /* Set external boost GPO */ 844 twl6040_write(component, TWL6040_REG_GPOCTL, 0x02); 845 break; 846 case SND_SOC_BIAS_OFF: 847 if (!priv->codec_powered) 848 break; 849 850 twl6040_power(twl6040, 0); 851 priv->codec_powered = 0; 852 break; 853 } 854 855 return ret; 856 } 857 858 static int twl6040_startup(struct snd_pcm_substream *substream, 859 struct snd_soc_dai *dai) 860 { 861 struct snd_soc_component *component = dai->component; 862 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 863 864 snd_pcm_hw_constraint_list(substream->runtime, 0, 865 SNDRV_PCM_HW_PARAM_RATE, 866 &sysclk_constraints[priv->pll_power_mode]); 867 868 return 0; 869 } 870 871 static int twl6040_hw_params(struct snd_pcm_substream *substream, 872 struct snd_pcm_hw_params *params, 873 struct snd_soc_dai *dai) 874 { 875 struct snd_soc_component *component = dai->component; 876 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 877 int rate; 878 879 rate = params_rate(params); 880 switch (rate) { 881 case 11250: 882 case 22500: 883 case 44100: 884 case 88200: 885 /* These rates are not supported when HPPLL is in use */ 886 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) { 887 dev_err(component->dev, "HPPLL does not support rate %d\n", 888 rate); 889 return -EINVAL; 890 } 891 priv->sysclk = 17640000; 892 break; 893 case 8000: 894 case 16000: 895 case 32000: 896 case 48000: 897 case 96000: 898 priv->sysclk = 19200000; 899 break; 900 default: 901 dev_err(component->dev, "unsupported rate %d\n", rate); 902 return -EINVAL; 903 } 904 905 return 0; 906 } 907 908 static int twl6040_prepare(struct snd_pcm_substream *substream, 909 struct snd_soc_dai *dai) 910 { 911 struct snd_soc_component *component = dai->component; 912 struct twl6040 *twl6040 = to_twl6040(component); 913 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 914 int ret; 915 916 if (!priv->sysclk) { 917 dev_err(component->dev, 918 "no mclk configured, call set_sysclk() on init\n"); 919 return -EINVAL; 920 } 921 922 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk); 923 if (ret) { 924 dev_err(component->dev, "Can not set PLL (%d)\n", ret); 925 return -EPERM; 926 } 927 928 return 0; 929 } 930 931 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai, 932 int clk_id, unsigned int freq, int dir) 933 { 934 struct snd_soc_component *component = codec_dai->component; 935 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 936 937 switch (clk_id) { 938 case TWL6040_SYSCLK_SEL_LPPLL: 939 case TWL6040_SYSCLK_SEL_HPPLL: 940 priv->pll = clk_id; 941 priv->clk_in = freq; 942 break; 943 default: 944 dev_err(component->dev, "unknown clk_id %d\n", clk_id); 945 return -EINVAL; 946 } 947 948 return 0; 949 } 950 951 static void twl6040_mute_path(struct snd_soc_component *component, enum twl6040_dai_id id, 952 int mute) 953 { 954 struct twl6040 *twl6040 = to_twl6040(component); 955 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 956 int hslctl, hsrctl, earctl; 957 int hflctl, hfrctl; 958 959 switch (id) { 960 case TWL6040_DAI_DL1: 961 hslctl = twl6040_read(component, TWL6040_REG_HSLCTL); 962 hsrctl = twl6040_read(component, TWL6040_REG_HSRCTL); 963 earctl = twl6040_read(component, TWL6040_REG_EARCTL); 964 965 if (mute) { 966 /* Power down drivers and DACs */ 967 earctl &= ~0x01; 968 hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA); 969 hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA); 970 971 } 972 973 twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl); 974 twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl); 975 twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl); 976 priv->dl1_unmuted = !mute; 977 break; 978 case TWL6040_DAI_DL2: 979 hflctl = twl6040_read(component, TWL6040_REG_HFLCTL); 980 hfrctl = twl6040_read(component, TWL6040_REG_HFRCTL); 981 982 if (mute) { 983 /* Power down drivers and DACs */ 984 hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA | 985 TWL6040_HFDRVENA | TWL6040_HFSWENA); 986 hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA | 987 TWL6040_HFDRVENA | TWL6040_HFSWENA); 988 } 989 990 twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl); 991 twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl); 992 priv->dl2_unmuted = !mute; 993 break; 994 default: 995 break; 996 } 997 } 998 999 static int twl6040_mute_stream(struct snd_soc_dai *dai, int mute, int direction) 1000 { 1001 switch (dai->id) { 1002 case TWL6040_DAI_LEGACY: 1003 twl6040_mute_path(dai->component, TWL6040_DAI_DL1, mute); 1004 twl6040_mute_path(dai->component, TWL6040_DAI_DL2, mute); 1005 break; 1006 case TWL6040_DAI_DL1: 1007 case TWL6040_DAI_DL2: 1008 twl6040_mute_path(dai->component, dai->id, mute); 1009 break; 1010 default: 1011 break; 1012 } 1013 1014 return 0; 1015 } 1016 1017 static const struct snd_soc_dai_ops twl6040_dai_ops = { 1018 .startup = twl6040_startup, 1019 .hw_params = twl6040_hw_params, 1020 .prepare = twl6040_prepare, 1021 .set_sysclk = twl6040_set_dai_sysclk, 1022 .mute_stream = twl6040_mute_stream, 1023 .no_capture_mute = 1, 1024 }; 1025 1026 static struct snd_soc_dai_driver twl6040_dai[] = { 1027 { 1028 .name = "twl6040-legacy", 1029 .id = TWL6040_DAI_LEGACY, 1030 .playback = { 1031 .stream_name = "Legacy Playback", 1032 .channels_min = 1, 1033 .channels_max = 5, 1034 .rates = TWL6040_RATES, 1035 .formats = TWL6040_FORMATS, 1036 }, 1037 .capture = { 1038 .stream_name = "Legacy Capture", 1039 .channels_min = 1, 1040 .channels_max = 2, 1041 .rates = TWL6040_RATES, 1042 .formats = TWL6040_FORMATS, 1043 }, 1044 .ops = &twl6040_dai_ops, 1045 }, 1046 { 1047 .name = "twl6040-ul", 1048 .id = TWL6040_DAI_UL, 1049 .capture = { 1050 .stream_name = "Capture", 1051 .channels_min = 1, 1052 .channels_max = 2, 1053 .rates = TWL6040_RATES, 1054 .formats = TWL6040_FORMATS, 1055 }, 1056 .ops = &twl6040_dai_ops, 1057 }, 1058 { 1059 .name = "twl6040-dl1", 1060 .id = TWL6040_DAI_DL1, 1061 .playback = { 1062 .stream_name = "Headset Playback", 1063 .channels_min = 1, 1064 .channels_max = 2, 1065 .rates = TWL6040_RATES, 1066 .formats = TWL6040_FORMATS, 1067 }, 1068 .ops = &twl6040_dai_ops, 1069 }, 1070 { 1071 .name = "twl6040-dl2", 1072 .id = TWL6040_DAI_DL2, 1073 .playback = { 1074 .stream_name = "Handsfree Playback", 1075 .channels_min = 1, 1076 .channels_max = 2, 1077 .rates = TWL6040_RATES, 1078 .formats = TWL6040_FORMATS, 1079 }, 1080 .ops = &twl6040_dai_ops, 1081 }, 1082 { 1083 .name = "twl6040-vib", 1084 .id = TWL6040_DAI_VIB, 1085 .playback = { 1086 .stream_name = "Vibra Playback", 1087 .channels_min = 1, 1088 .channels_max = 1, 1089 .rates = SNDRV_PCM_RATE_CONTINUOUS, 1090 .formats = TWL6040_FORMATS, 1091 }, 1092 .ops = &twl6040_dai_ops, 1093 }, 1094 }; 1095 1096 static int twl6040_probe(struct snd_soc_component *component) 1097 { 1098 struct twl6040_data *priv; 1099 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component); 1100 struct platform_device *pdev = to_platform_device(component->dev); 1101 int ret = 0; 1102 1103 priv = devm_kzalloc(component->dev, sizeof(*priv), GFP_KERNEL); 1104 if (priv == NULL) 1105 return -ENOMEM; 1106 1107 snd_soc_component_set_drvdata(component, priv); 1108 1109 priv->component = component; 1110 1111 priv->plug_irq = platform_get_irq(pdev, 0); 1112 if (priv->plug_irq < 0) 1113 return priv->plug_irq; 1114 1115 INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work); 1116 1117 mutex_init(&priv->mutex); 1118 1119 ret = request_threaded_irq(priv->plug_irq, NULL, 1120 twl6040_audio_handler, 1121 IRQF_NO_SUSPEND | IRQF_ONESHOT, 1122 "twl6040_irq_plug", component); 1123 if (ret) { 1124 dev_err(component->dev, "PLUG IRQ request failed: %d\n", ret); 1125 return ret; 1126 } 1127 1128 snd_soc_dapm_force_bias_level(dapm, SND_SOC_BIAS_STANDBY); 1129 twl6040_init_chip(component); 1130 1131 return 0; 1132 } 1133 1134 static void twl6040_remove(struct snd_soc_component *component) 1135 { 1136 struct twl6040_data *priv = snd_soc_component_get_drvdata(component); 1137 1138 free_irq(priv->plug_irq, component); 1139 } 1140 1141 static const struct snd_soc_component_driver soc_component_dev_twl6040 = { 1142 .probe = twl6040_probe, 1143 .remove = twl6040_remove, 1144 .read = twl6040_read, 1145 .write = twl6040_write, 1146 .set_bias_level = twl6040_set_bias_level, 1147 .controls = twl6040_snd_controls, 1148 .num_controls = ARRAY_SIZE(twl6040_snd_controls), 1149 .dapm_widgets = twl6040_dapm_widgets, 1150 .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets), 1151 .dapm_routes = intercon, 1152 .num_dapm_routes = ARRAY_SIZE(intercon), 1153 .suspend_bias_off = 1, 1154 .idle_bias_on = 1, 1155 .endianness = 1, 1156 }; 1157 1158 static int twl6040_codec_probe(struct platform_device *pdev) 1159 { 1160 return devm_snd_soc_register_component(&pdev->dev, 1161 &soc_component_dev_twl6040, 1162 twl6040_dai, ARRAY_SIZE(twl6040_dai)); 1163 } 1164 1165 static struct platform_driver twl6040_codec_driver = { 1166 .driver = { 1167 .name = "twl6040-codec", 1168 }, 1169 .probe = twl6040_codec_probe, 1170 }; 1171 1172 module_platform_driver(twl6040_codec_driver); 1173 1174 MODULE_DESCRIPTION("ASoC TWL6040 codec driver"); 1175 MODULE_AUTHOR("Misael Lopez Cruz"); 1176 MODULE_LICENSE("GPL"); 1177