1 /* 2 * ALSA SoC TWL6040 codec driver 3 * 4 * Author: Misael Lopez Cruz <x0052729@ti.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * version 2 as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 18 * 02110-1301 USA 19 * 20 */ 21 22 #include <linux/module.h> 23 #include <linux/moduleparam.h> 24 #include <linux/init.h> 25 #include <linux/delay.h> 26 #include <linux/pm.h> 27 #include <linux/platform_device.h> 28 #include <linux/slab.h> 29 #include <linux/i2c/twl.h> 30 #include <linux/mfd/twl6040.h> 31 32 #include <sound/core.h> 33 #include <sound/pcm.h> 34 #include <sound/pcm_params.h> 35 #include <sound/soc.h> 36 #include <sound/initval.h> 37 #include <sound/tlv.h> 38 39 #include "twl6040.h" 40 41 #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000 42 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE) 43 44 #define TWL6040_OUTHS_0dB 0x00 45 #define TWL6040_OUTHS_M30dB 0x0F 46 #define TWL6040_OUTHF_0dB 0x03 47 #define TWL6040_OUTHF_M52dB 0x1D 48 49 #define TWL6040_RAMP_NONE 0 50 #define TWL6040_RAMP_UP 1 51 #define TWL6040_RAMP_DOWN 2 52 53 #define TWL6040_HSL_VOL_MASK 0x0F 54 #define TWL6040_HSL_VOL_SHIFT 0 55 #define TWL6040_HSR_VOL_MASK 0xF0 56 #define TWL6040_HSR_VOL_SHIFT 4 57 #define TWL6040_HF_VOL_MASK 0x1F 58 #define TWL6040_HF_VOL_SHIFT 0 59 60 /* Shadow register used by the driver */ 61 #define TWL6040_REG_SW_SHADOW 0x2F 62 #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1) 63 64 /* TWL6040_REG_SW_SHADOW (0x2F) fields */ 65 #define TWL6040_EAR_PATH_ENABLE 0x01 66 67 struct twl6040_output { 68 u16 active; 69 u16 left_vol; 70 u16 right_vol; 71 u16 left_step; 72 u16 right_step; 73 unsigned int step_delay; 74 u16 ramp; 75 struct delayed_work work; 76 struct completion ramp_done; 77 }; 78 79 struct twl6040_jack_data { 80 struct snd_soc_jack *jack; 81 struct delayed_work work; 82 int report; 83 }; 84 85 /* codec private data */ 86 struct twl6040_data { 87 int plug_irq; 88 int codec_powered; 89 int pll; 90 int pll_power_mode; 91 int hs_power_mode; 92 int hs_power_mode_locked; 93 unsigned int clk_in; 94 unsigned int sysclk; 95 u16 hs_left_step; 96 u16 hs_right_step; 97 u16 hf_left_step; 98 u16 hf_right_step; 99 struct twl6040_jack_data hs_jack; 100 struct snd_soc_codec *codec; 101 struct workqueue_struct *workqueue; 102 struct mutex mutex; 103 struct twl6040_output headset; 104 struct twl6040_output handsfree; 105 }; 106 107 /* 108 * twl6040 register cache & default register settings 109 */ 110 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = { 111 0x00, /* not used 0x00 */ 112 0x4B, /* REG_ASICID 0x01 (ro) */ 113 0x00, /* REG_ASICREV 0x02 (ro) */ 114 0x00, /* REG_INTID 0x03 */ 115 0x00, /* REG_INTMR 0x04 */ 116 0x00, /* REG_NCPCTRL 0x05 */ 117 0x00, /* REG_LDOCTL 0x06 */ 118 0x60, /* REG_HPPLLCTL 0x07 */ 119 0x00, /* REG_LPPLLCTL 0x08 */ 120 0x4A, /* REG_LPPLLDIV 0x09 */ 121 0x00, /* REG_AMICBCTL 0x0A */ 122 0x00, /* REG_DMICBCTL 0x0B */ 123 0x00, /* REG_MICLCTL 0x0C */ 124 0x00, /* REG_MICRCTL 0x0D */ 125 0x00, /* REG_MICGAIN 0x0E */ 126 0x1B, /* REG_LINEGAIN 0x0F */ 127 0x00, /* REG_HSLCTL 0x10 */ 128 0x00, /* REG_HSRCTL 0x11 */ 129 0x00, /* REG_HSGAIN 0x12 */ 130 0x00, /* REG_EARCTL 0x13 */ 131 0x00, /* REG_HFLCTL 0x14 */ 132 0x00, /* REG_HFLGAIN 0x15 */ 133 0x00, /* REG_HFRCTL 0x16 */ 134 0x00, /* REG_HFRGAIN 0x17 */ 135 0x00, /* REG_VIBCTLL 0x18 */ 136 0x00, /* REG_VIBDATL 0x19 */ 137 0x00, /* REG_VIBCTLR 0x1A */ 138 0x00, /* REG_VIBDATR 0x1B */ 139 0x00, /* REG_HKCTL1 0x1C */ 140 0x00, /* REG_HKCTL2 0x1D */ 141 0x00, /* REG_GPOCTL 0x1E */ 142 0x00, /* REG_ALB 0x1F */ 143 0x00, /* REG_DLB 0x20 */ 144 0x00, /* not used 0x21 */ 145 0x00, /* not used 0x22 */ 146 0x00, /* not used 0x23 */ 147 0x00, /* not used 0x24 */ 148 0x00, /* not used 0x25 */ 149 0x00, /* not used 0x26 */ 150 0x00, /* not used 0x27 */ 151 0x00, /* REG_TRIM1 0x28 */ 152 0x00, /* REG_TRIM2 0x29 */ 153 0x00, /* REG_TRIM3 0x2A */ 154 0x00, /* REG_HSOTRIM 0x2B */ 155 0x00, /* REG_HFOTRIM 0x2C */ 156 0x09, /* REG_ACCCTL 0x2D */ 157 0x00, /* REG_STATUS 0x2E (ro) */ 158 159 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */ 160 }; 161 162 /* List of registers to be restored after power up */ 163 static const int twl6040_restore_list[] = { 164 TWL6040_REG_MICLCTL, 165 TWL6040_REG_MICRCTL, 166 TWL6040_REG_MICGAIN, 167 TWL6040_REG_LINEGAIN, 168 TWL6040_REG_HSLCTL, 169 TWL6040_REG_HSRCTL, 170 TWL6040_REG_HSGAIN, 171 TWL6040_REG_EARCTL, 172 TWL6040_REG_HFLCTL, 173 TWL6040_REG_HFLGAIN, 174 TWL6040_REG_HFRCTL, 175 TWL6040_REG_HFRGAIN, 176 }; 177 178 /* set of rates for each pll: low-power and high-performance */ 179 static unsigned int lp_rates[] = { 180 8000, 181 11250, 182 16000, 183 22500, 184 32000, 185 44100, 186 48000, 187 88200, 188 96000, 189 }; 190 191 static unsigned int hp_rates[] = { 192 8000, 193 16000, 194 32000, 195 48000, 196 96000, 197 }; 198 199 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = { 200 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, }, 201 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, }, 202 }; 203 204 /* 205 * read twl6040 register cache 206 */ 207 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec, 208 unsigned int reg) 209 { 210 u8 *cache = codec->reg_cache; 211 212 if (reg >= TWL6040_CACHEREGNUM) 213 return -EIO; 214 215 return cache[reg]; 216 } 217 218 /* 219 * write twl6040 register cache 220 */ 221 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec, 222 u8 reg, u8 value) 223 { 224 u8 *cache = codec->reg_cache; 225 226 if (reg >= TWL6040_CACHEREGNUM) 227 return; 228 cache[reg] = value; 229 } 230 231 /* 232 * read from twl6040 hardware register 233 */ 234 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec, 235 unsigned int reg) 236 { 237 struct twl6040 *twl6040 = codec->control_data; 238 u8 value; 239 240 if (reg >= TWL6040_CACHEREGNUM) 241 return -EIO; 242 243 if (likely(reg < TWL6040_REG_SW_SHADOW)) { 244 value = twl6040_reg_read(twl6040, reg); 245 twl6040_write_reg_cache(codec, reg, value); 246 } else { 247 value = twl6040_read_reg_cache(codec, reg); 248 } 249 250 return value; 251 } 252 253 /* 254 * write to the twl6040 register space 255 */ 256 static int twl6040_write(struct snd_soc_codec *codec, 257 unsigned int reg, unsigned int value) 258 { 259 struct twl6040 *twl6040 = codec->control_data; 260 261 if (reg >= TWL6040_CACHEREGNUM) 262 return -EIO; 263 264 twl6040_write_reg_cache(codec, reg, value); 265 if (likely(reg < TWL6040_REG_SW_SHADOW)) 266 return twl6040_reg_write(twl6040, reg, value); 267 else 268 return 0; 269 } 270 271 static void twl6040_init_chip(struct snd_soc_codec *codec) 272 { 273 struct twl6040 *twl6040 = codec->control_data; 274 u8 val; 275 276 /* Update reg_cache: ASICREV, and TRIM values */ 277 val = twl6040_get_revid(twl6040); 278 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val); 279 280 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1); 281 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2); 282 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3); 283 twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM); 284 twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM); 285 286 /* Change chip defaults */ 287 /* No imput selected for microphone amplifiers */ 288 twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18); 289 twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18); 290 291 /* 292 * We need to lower the default gain values, so the ramp code 293 * can work correctly for the first playback. 294 * This reduces the pop noise heard at the first playback. 295 */ 296 twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff); 297 twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e); 298 twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d); 299 twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d); 300 twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0); 301 } 302 303 static void twl6040_restore_regs(struct snd_soc_codec *codec) 304 { 305 u8 *cache = codec->reg_cache; 306 int reg, i; 307 308 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) { 309 reg = twl6040_restore_list[i]; 310 twl6040_write(codec, reg, cache[reg]); 311 } 312 } 313 314 /* 315 * Ramp HS PGA volume to minimise pops at stream startup and shutdown. 316 */ 317 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec, 318 unsigned int left_step, unsigned int right_step) 319 { 320 321 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 322 struct twl6040_output *headset = &priv->headset; 323 int left_complete = 0, right_complete = 0; 324 u8 reg, val; 325 326 /* left channel */ 327 left_step = (left_step > 0xF) ? 0xF : left_step; 328 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN); 329 val = (~reg & TWL6040_HSL_VOL_MASK); 330 331 if (headset->ramp == TWL6040_RAMP_UP) { 332 /* ramp step up */ 333 if (val < headset->left_vol) { 334 if (val + left_step > headset->left_vol) 335 val = headset->left_vol; 336 else 337 val += left_step; 338 339 reg &= ~TWL6040_HSL_VOL_MASK; 340 twl6040_write(codec, TWL6040_REG_HSGAIN, 341 (reg | (~val & TWL6040_HSL_VOL_MASK))); 342 } else { 343 left_complete = 1; 344 } 345 } else if (headset->ramp == TWL6040_RAMP_DOWN) { 346 /* ramp step down */ 347 if (val > 0x0) { 348 if ((int)val - (int)left_step < 0) 349 val = 0; 350 else 351 val -= left_step; 352 353 reg &= ~TWL6040_HSL_VOL_MASK; 354 twl6040_write(codec, TWL6040_REG_HSGAIN, reg | 355 (~val & TWL6040_HSL_VOL_MASK)); 356 } else { 357 left_complete = 1; 358 } 359 } 360 361 /* right channel */ 362 right_step = (right_step > 0xF) ? 0xF : right_step; 363 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN); 364 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT; 365 366 if (headset->ramp == TWL6040_RAMP_UP) { 367 /* ramp step up */ 368 if (val < headset->right_vol) { 369 if (val + right_step > headset->right_vol) 370 val = headset->right_vol; 371 else 372 val += right_step; 373 374 reg &= ~TWL6040_HSR_VOL_MASK; 375 twl6040_write(codec, TWL6040_REG_HSGAIN, 376 (reg | (~val << TWL6040_HSR_VOL_SHIFT))); 377 } else { 378 right_complete = 1; 379 } 380 } else if (headset->ramp == TWL6040_RAMP_DOWN) { 381 /* ramp step down */ 382 if (val > 0x0) { 383 if ((int)val - (int)right_step < 0) 384 val = 0; 385 else 386 val -= right_step; 387 388 reg &= ~TWL6040_HSR_VOL_MASK; 389 twl6040_write(codec, TWL6040_REG_HSGAIN, 390 reg | (~val << TWL6040_HSR_VOL_SHIFT)); 391 } else { 392 right_complete = 1; 393 } 394 } 395 396 return left_complete & right_complete; 397 } 398 399 /* 400 * Ramp HF PGA volume to minimise pops at stream startup and shutdown. 401 */ 402 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec, 403 unsigned int left_step, unsigned int right_step) 404 { 405 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 406 struct twl6040_output *handsfree = &priv->handsfree; 407 int left_complete = 0, right_complete = 0; 408 u16 reg, val; 409 410 /* left channel */ 411 left_step = (left_step > 0x1D) ? 0x1D : left_step; 412 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN); 413 reg = 0x1D - reg; 414 val = (reg & TWL6040_HF_VOL_MASK); 415 if (handsfree->ramp == TWL6040_RAMP_UP) { 416 /* ramp step up */ 417 if (val < handsfree->left_vol) { 418 if (val + left_step > handsfree->left_vol) 419 val = handsfree->left_vol; 420 else 421 val += left_step; 422 423 reg &= ~TWL6040_HF_VOL_MASK; 424 twl6040_write(codec, TWL6040_REG_HFLGAIN, 425 reg | (0x1D - val)); 426 } else { 427 left_complete = 1; 428 } 429 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) { 430 /* ramp step down */ 431 if (val > 0) { 432 if ((int)val - (int)left_step < 0) 433 val = 0; 434 else 435 val -= left_step; 436 437 reg &= ~TWL6040_HF_VOL_MASK; 438 twl6040_write(codec, TWL6040_REG_HFLGAIN, 439 reg | (0x1D - val)); 440 } else { 441 left_complete = 1; 442 } 443 } 444 445 /* right channel */ 446 right_step = (right_step > 0x1D) ? 0x1D : right_step; 447 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN); 448 reg = 0x1D - reg; 449 val = (reg & TWL6040_HF_VOL_MASK); 450 if (handsfree->ramp == TWL6040_RAMP_UP) { 451 /* ramp step up */ 452 if (val < handsfree->right_vol) { 453 if (val + right_step > handsfree->right_vol) 454 val = handsfree->right_vol; 455 else 456 val += right_step; 457 458 reg &= ~TWL6040_HF_VOL_MASK; 459 twl6040_write(codec, TWL6040_REG_HFRGAIN, 460 reg | (0x1D - val)); 461 } else { 462 right_complete = 1; 463 } 464 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) { 465 /* ramp step down */ 466 if (val > 0) { 467 if ((int)val - (int)right_step < 0) 468 val = 0; 469 else 470 val -= right_step; 471 472 reg &= ~TWL6040_HF_VOL_MASK; 473 twl6040_write(codec, TWL6040_REG_HFRGAIN, 474 reg | (0x1D - val)); 475 } 476 } 477 478 return left_complete & right_complete; 479 } 480 481 /* 482 * This work ramps both output PGAs at stream start/stop time to 483 * minimise pop associated with DAPM power switching. 484 */ 485 static void twl6040_pga_hs_work(struct work_struct *work) 486 { 487 struct twl6040_data *priv = 488 container_of(work, struct twl6040_data, headset.work.work); 489 struct snd_soc_codec *codec = priv->codec; 490 struct twl6040_output *headset = &priv->headset; 491 int i, headset_complete; 492 493 /* do we need to ramp at all ? */ 494 if (headset->ramp == TWL6040_RAMP_NONE) 495 return; 496 497 /* HS PGA gain range: 0x0 - 0xf (0 - 15) */ 498 for (i = 0; i < 16; i++) { 499 headset_complete = twl6040_hs_ramp_step(codec, 500 headset->left_step, 501 headset->right_step); 502 503 /* ramp finished ? */ 504 if (headset_complete) 505 break; 506 507 schedule_timeout_interruptible( 508 msecs_to_jiffies(headset->step_delay)); 509 } 510 511 if (headset->ramp == TWL6040_RAMP_DOWN) { 512 headset->active = 0; 513 complete(&headset->ramp_done); 514 } else { 515 headset->active = 1; 516 } 517 headset->ramp = TWL6040_RAMP_NONE; 518 } 519 520 static void twl6040_pga_hf_work(struct work_struct *work) 521 { 522 struct twl6040_data *priv = 523 container_of(work, struct twl6040_data, handsfree.work.work); 524 struct snd_soc_codec *codec = priv->codec; 525 struct twl6040_output *handsfree = &priv->handsfree; 526 int i, handsfree_complete; 527 528 /* do we need to ramp at all ? */ 529 if (handsfree->ramp == TWL6040_RAMP_NONE) 530 return; 531 532 /* 533 * HF PGA gain range: 0x00 - 0x1d (0 - 29) */ 534 for (i = 0; i < 30; i++) { 535 handsfree_complete = twl6040_hf_ramp_step(codec, 536 handsfree->left_step, 537 handsfree->right_step); 538 539 /* ramp finished ? */ 540 if (handsfree_complete) 541 break; 542 543 schedule_timeout_interruptible( 544 msecs_to_jiffies(handsfree->step_delay)); 545 } 546 547 548 if (handsfree->ramp == TWL6040_RAMP_DOWN) { 549 handsfree->active = 0; 550 complete(&handsfree->ramp_done); 551 } else 552 handsfree->active = 1; 553 handsfree->ramp = TWL6040_RAMP_NONE; 554 } 555 556 static int out_drv_event(struct snd_soc_dapm_widget *w, 557 struct snd_kcontrol *kcontrol, int event) 558 { 559 struct snd_soc_codec *codec = w->codec; 560 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 561 struct twl6040_output *out; 562 struct delayed_work *work; 563 564 switch (w->shift) { 565 case 2: /* Headset output driver */ 566 out = &priv->headset; 567 work = &out->work; 568 /* 569 * Make sure, that we do not mess up variables for already 570 * executing work. 571 */ 572 cancel_delayed_work_sync(work); 573 574 out->left_step = priv->hs_left_step; 575 out->right_step = priv->hs_right_step; 576 out->step_delay = 5; /* 5 ms between volume ramp steps */ 577 break; 578 case 4: /* Handsfree output driver */ 579 out = &priv->handsfree; 580 work = &out->work; 581 /* 582 * Make sure, that we do not mess up variables for already 583 * executing work. 584 */ 585 cancel_delayed_work_sync(work); 586 587 out->left_step = priv->hf_left_step; 588 out->right_step = priv->hf_right_step; 589 out->step_delay = 5; /* 5 ms between volume ramp steps */ 590 break; 591 default: 592 return -1; 593 } 594 595 switch (event) { 596 case SND_SOC_DAPM_POST_PMU: 597 if (out->active) 598 break; 599 600 /* don't use volume ramp for power-up */ 601 out->ramp = TWL6040_RAMP_UP; 602 out->left_step = out->left_vol; 603 out->right_step = out->right_vol; 604 605 queue_delayed_work(priv->workqueue, work, msecs_to_jiffies(1)); 606 break; 607 608 case SND_SOC_DAPM_PRE_PMD: 609 if (!out->active) 610 break; 611 612 /* use volume ramp for power-down */ 613 out->ramp = TWL6040_RAMP_DOWN; 614 INIT_COMPLETION(out->ramp_done); 615 616 queue_delayed_work(priv->workqueue, work, msecs_to_jiffies(1)); 617 618 wait_for_completion_timeout(&out->ramp_done, 619 msecs_to_jiffies(2000)); 620 break; 621 } 622 623 return 0; 624 } 625 626 /* set headset dac and driver power mode */ 627 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf) 628 { 629 int hslctl, hsrctl; 630 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE; 631 632 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); 633 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); 634 635 if (high_perf) { 636 hslctl &= ~mask; 637 hsrctl &= ~mask; 638 } else { 639 hslctl |= mask; 640 hsrctl |= mask; 641 } 642 643 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl); 644 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl); 645 646 return 0; 647 } 648 649 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w, 650 struct snd_kcontrol *kcontrol, int event) 651 { 652 struct snd_soc_codec *codec = w->codec; 653 u8 hslctl, hsrctl; 654 655 /* 656 * Workaround for Headset DC offset caused pop noise: 657 * Both HS DAC need to be turned on (before the HS driver) and off at 658 * the same time. 659 */ 660 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); 661 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); 662 if (SND_SOC_DAPM_EVENT_ON(event)) { 663 hslctl |= TWL6040_HSDACENA; 664 hsrctl |= TWL6040_HSDACENA; 665 } else { 666 hslctl &= ~TWL6040_HSDACENA; 667 hsrctl &= ~TWL6040_HSDACENA; 668 } 669 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl); 670 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl); 671 672 msleep(1); 673 return 0; 674 } 675 676 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w, 677 struct snd_kcontrol *kcontrol, int event) 678 { 679 struct snd_soc_codec *codec = w->codec; 680 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 681 int ret = 0; 682 683 if (SND_SOC_DAPM_EVENT_ON(event)) { 684 /* Earphone doesn't support low power mode */ 685 priv->hs_power_mode_locked = 1; 686 ret = headset_power_mode(codec, 1); 687 } else { 688 priv->hs_power_mode_locked = 0; 689 ret = headset_power_mode(codec, priv->hs_power_mode); 690 } 691 692 msleep(1); 693 694 return ret; 695 } 696 697 static void twl6040_hs_jack_report(struct snd_soc_codec *codec, 698 struct snd_soc_jack *jack, int report) 699 { 700 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 701 int status; 702 703 mutex_lock(&priv->mutex); 704 705 /* Sync status */ 706 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS); 707 if (status & TWL6040_PLUGCOMP) 708 snd_soc_jack_report(jack, report, report); 709 else 710 snd_soc_jack_report(jack, 0, report); 711 712 mutex_unlock(&priv->mutex); 713 } 714 715 void twl6040_hs_jack_detect(struct snd_soc_codec *codec, 716 struct snd_soc_jack *jack, int report) 717 { 718 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 719 struct twl6040_jack_data *hs_jack = &priv->hs_jack; 720 721 hs_jack->jack = jack; 722 hs_jack->report = report; 723 724 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report); 725 } 726 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect); 727 728 static void twl6040_accessory_work(struct work_struct *work) 729 { 730 struct twl6040_data *priv = container_of(work, 731 struct twl6040_data, hs_jack.work.work); 732 struct snd_soc_codec *codec = priv->codec; 733 struct twl6040_jack_data *hs_jack = &priv->hs_jack; 734 735 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report); 736 } 737 738 /* audio interrupt handler */ 739 static irqreturn_t twl6040_audio_handler(int irq, void *data) 740 { 741 struct snd_soc_codec *codec = data; 742 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 743 744 queue_delayed_work(priv->workqueue, &priv->hs_jack.work, 745 msecs_to_jiffies(200)); 746 747 return IRQ_HANDLED; 748 } 749 750 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol, 751 struct snd_ctl_elem_value *ucontrol) 752 { 753 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 754 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec); 755 struct twl6040_output *out = NULL; 756 struct soc_mixer_control *mc = 757 (struct soc_mixer_control *)kcontrol->private_value; 758 int ret; 759 760 /* For HS and HF we shadow the values and only actually write 761 * them out when active in order to ensure the amplifier comes on 762 * as quietly as possible. */ 763 switch (mc->reg) { 764 case TWL6040_REG_HSGAIN: 765 out = &twl6040_priv->headset; 766 break; 767 case TWL6040_REG_HFLGAIN: 768 out = &twl6040_priv->handsfree; 769 break; 770 default: 771 dev_warn(codec->dev, "%s: Unexpected register: 0x%02x\n", 772 __func__, mc->reg); 773 return -EINVAL; 774 } 775 776 out->left_vol = ucontrol->value.integer.value[0]; 777 out->right_vol = ucontrol->value.integer.value[1]; 778 if (!out->active) 779 return 1; 780 781 ret = snd_soc_put_volsw(kcontrol, ucontrol); 782 if (ret < 0) 783 return ret; 784 785 return 1; 786 } 787 788 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol, 789 struct snd_ctl_elem_value *ucontrol) 790 { 791 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 792 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec); 793 struct twl6040_output *out = &twl6040_priv->headset; 794 struct soc_mixer_control *mc = 795 (struct soc_mixer_control *)kcontrol->private_value; 796 797 switch (mc->reg) { 798 case TWL6040_REG_HSGAIN: 799 out = &twl6040_priv->headset; 800 break; 801 case TWL6040_REG_HFLGAIN: 802 out = &twl6040_priv->handsfree; 803 break; 804 default: 805 dev_warn(codec->dev, "%s: Unexpected register: 0x%02x\n", 806 __func__, mc->reg); 807 return -EINVAL; 808 } 809 810 ucontrol->value.integer.value[0] = out->left_vol; 811 ucontrol->value.integer.value[1] = out->right_vol; 812 return 0; 813 } 814 815 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol, 816 struct snd_ctl_elem_value *ucontrol) 817 { 818 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); 819 struct snd_soc_dapm_widget *widget = wlist->widgets[0]; 820 struct snd_soc_codec *codec = widget->codec; 821 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 822 unsigned int val; 823 824 /* Do not allow changes while Input/FF efect is running */ 825 val = twl6040_read_reg_volatile(codec, e->reg); 826 if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL)) 827 return -EBUSY; 828 829 return snd_soc_dapm_put_enum_double(kcontrol, ucontrol); 830 } 831 832 /* 833 * MICATT volume control: 834 * from -6 to 0 dB in 6 dB steps 835 */ 836 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0); 837 838 /* 839 * MICGAIN volume control: 840 * from 6 to 30 dB in 6 dB steps 841 */ 842 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0); 843 844 /* 845 * AFMGAIN volume control: 846 * from -18 to 24 dB in 6 dB steps 847 */ 848 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0); 849 850 /* 851 * HSGAIN volume control: 852 * from -30 to 0 dB in 2 dB steps 853 */ 854 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0); 855 856 /* 857 * HFGAIN volume control: 858 * from -52 to 6 dB in 2 dB steps 859 */ 860 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0); 861 862 /* 863 * EPGAIN volume control: 864 * from -24 to 6 dB in 2 dB steps 865 */ 866 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0); 867 868 /* Left analog microphone selection */ 869 static const char *twl6040_amicl_texts[] = 870 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"}; 871 872 /* Right analog microphone selection */ 873 static const char *twl6040_amicr_texts[] = 874 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"}; 875 876 static const struct soc_enum twl6040_enum[] = { 877 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts), 878 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts), 879 }; 880 881 static const char *twl6040_hs_texts[] = { 882 "Off", "HS DAC", "Line-In amp" 883 }; 884 885 static const struct soc_enum twl6040_hs_enum[] = { 886 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts), 887 twl6040_hs_texts), 888 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts), 889 twl6040_hs_texts), 890 }; 891 892 static const char *twl6040_hf_texts[] = { 893 "Off", "HF DAC", "Line-In amp" 894 }; 895 896 static const struct soc_enum twl6040_hf_enum[] = { 897 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts), 898 twl6040_hf_texts), 899 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts), 900 twl6040_hf_texts), 901 }; 902 903 static const char *twl6040_vibrapath_texts[] = { 904 "Input FF", "Audio PDM" 905 }; 906 907 static const struct soc_enum twl6040_vibra_enum[] = { 908 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1, 909 ARRAY_SIZE(twl6040_vibrapath_texts), 910 twl6040_vibrapath_texts), 911 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1, 912 ARRAY_SIZE(twl6040_vibrapath_texts), 913 twl6040_vibrapath_texts), 914 }; 915 916 static const struct snd_kcontrol_new amicl_control = 917 SOC_DAPM_ENUM("Route", twl6040_enum[0]); 918 919 static const struct snd_kcontrol_new amicr_control = 920 SOC_DAPM_ENUM("Route", twl6040_enum[1]); 921 922 /* Headset DAC playback switches */ 923 static const struct snd_kcontrol_new hsl_mux_controls = 924 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]); 925 926 static const struct snd_kcontrol_new hsr_mux_controls = 927 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]); 928 929 /* Handsfree DAC playback switches */ 930 static const struct snd_kcontrol_new hfl_mux_controls = 931 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]); 932 933 static const struct snd_kcontrol_new hfr_mux_controls = 934 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]); 935 936 static const struct snd_kcontrol_new ep_path_enable_control = 937 SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0); 938 939 static const struct snd_kcontrol_new auxl_switch_control = 940 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0); 941 942 static const struct snd_kcontrol_new auxr_switch_control = 943 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0); 944 945 /* Vibra playback switches */ 946 static const struct snd_kcontrol_new vibral_mux_controls = 947 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0], 948 snd_soc_dapm_get_enum_double, 949 twl6040_soc_dapm_put_vibra_enum); 950 951 static const struct snd_kcontrol_new vibrar_mux_controls = 952 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1], 953 snd_soc_dapm_get_enum_double, 954 twl6040_soc_dapm_put_vibra_enum); 955 956 /* Headset power mode */ 957 static const char *twl6040_power_mode_texts[] = { 958 "Low-Power", "High-Perfomance", 959 }; 960 961 static const struct soc_enum twl6040_power_mode_enum = 962 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts), 963 twl6040_power_mode_texts); 964 965 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol, 966 struct snd_ctl_elem_value *ucontrol) 967 { 968 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 969 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 970 971 ucontrol->value.enumerated.item[0] = priv->hs_power_mode; 972 973 return 0; 974 } 975 976 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol, 977 struct snd_ctl_elem_value *ucontrol) 978 { 979 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 980 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 981 int high_perf = ucontrol->value.enumerated.item[0]; 982 int ret = 0; 983 984 if (!priv->hs_power_mode_locked) 985 ret = headset_power_mode(codec, high_perf); 986 987 if (!ret) 988 priv->hs_power_mode = high_perf; 989 990 return ret; 991 } 992 993 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol, 994 struct snd_ctl_elem_value *ucontrol) 995 { 996 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 997 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 998 999 ucontrol->value.enumerated.item[0] = priv->pll_power_mode; 1000 1001 return 0; 1002 } 1003 1004 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol, 1005 struct snd_ctl_elem_value *ucontrol) 1006 { 1007 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 1008 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1009 1010 priv->pll_power_mode = ucontrol->value.enumerated.item[0]; 1011 1012 return 0; 1013 } 1014 1015 int twl6040_get_clk_id(struct snd_soc_codec *codec) 1016 { 1017 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1018 1019 return priv->pll_power_mode; 1020 } 1021 EXPORT_SYMBOL_GPL(twl6040_get_clk_id); 1022 1023 int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim) 1024 { 1025 if (unlikely(trim >= TWL6040_TRIM_INVAL)) 1026 return -EINVAL; 1027 1028 return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim); 1029 } 1030 EXPORT_SYMBOL_GPL(twl6040_get_trim_value); 1031 1032 static const struct snd_kcontrol_new twl6040_snd_controls[] = { 1033 /* Capture gains */ 1034 SOC_DOUBLE_TLV("Capture Preamplifier Volume", 1035 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv), 1036 SOC_DOUBLE_TLV("Capture Volume", 1037 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv), 1038 1039 /* AFM gains */ 1040 SOC_DOUBLE_TLV("Aux FM Volume", 1041 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv), 1042 1043 /* Playback gains */ 1044 SOC_DOUBLE_EXT_TLV("Headset Playback Volume", 1045 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, twl6040_get_volsw, 1046 twl6040_put_volsw, hs_tlv), 1047 SOC_DOUBLE_R_EXT_TLV("Handsfree Playback Volume", 1048 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, 1049 twl6040_get_volsw, twl6040_put_volsw, hf_tlv), 1050 SOC_SINGLE_TLV("Earphone Playback Volume", 1051 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv), 1052 1053 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum, 1054 twl6040_headset_power_get_enum, 1055 twl6040_headset_power_put_enum), 1056 1057 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum, 1058 twl6040_pll_get_enum, twl6040_pll_put_enum), 1059 }; 1060 1061 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = { 1062 /* Inputs */ 1063 SND_SOC_DAPM_INPUT("MAINMIC"), 1064 SND_SOC_DAPM_INPUT("HSMIC"), 1065 SND_SOC_DAPM_INPUT("SUBMIC"), 1066 SND_SOC_DAPM_INPUT("AFML"), 1067 SND_SOC_DAPM_INPUT("AFMR"), 1068 1069 /* Outputs */ 1070 SND_SOC_DAPM_OUTPUT("HSOL"), 1071 SND_SOC_DAPM_OUTPUT("HSOR"), 1072 SND_SOC_DAPM_OUTPUT("HFL"), 1073 SND_SOC_DAPM_OUTPUT("HFR"), 1074 SND_SOC_DAPM_OUTPUT("EP"), 1075 SND_SOC_DAPM_OUTPUT("AUXL"), 1076 SND_SOC_DAPM_OUTPUT("AUXR"), 1077 SND_SOC_DAPM_OUTPUT("VIBRAL"), 1078 SND_SOC_DAPM_OUTPUT("VIBRAR"), 1079 1080 /* Analog input muxes for the capture amplifiers */ 1081 SND_SOC_DAPM_MUX("Analog Left Capture Route", 1082 SND_SOC_NOPM, 0, 0, &amicl_control), 1083 SND_SOC_DAPM_MUX("Analog Right Capture Route", 1084 SND_SOC_NOPM, 0, 0, &amicr_control), 1085 1086 /* Analog capture PGAs */ 1087 SND_SOC_DAPM_PGA("MicAmpL", 1088 TWL6040_REG_MICLCTL, 0, 0, NULL, 0), 1089 SND_SOC_DAPM_PGA("MicAmpR", 1090 TWL6040_REG_MICRCTL, 0, 0, NULL, 0), 1091 1092 /* Auxiliary FM PGAs */ 1093 SND_SOC_DAPM_PGA("AFMAmpL", 1094 TWL6040_REG_MICLCTL, 1, 0, NULL, 0), 1095 SND_SOC_DAPM_PGA("AFMAmpR", 1096 TWL6040_REG_MICRCTL, 1, 0, NULL, 0), 1097 1098 /* ADCs */ 1099 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture", 1100 TWL6040_REG_MICLCTL, 2, 0), 1101 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture", 1102 TWL6040_REG_MICRCTL, 2, 0), 1103 1104 /* Microphone bias */ 1105 SND_SOC_DAPM_MICBIAS("Headset Mic Bias", 1106 TWL6040_REG_AMICBCTL, 0, 0), 1107 SND_SOC_DAPM_MICBIAS("Main Mic Bias", 1108 TWL6040_REG_AMICBCTL, 4, 0), 1109 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias", 1110 TWL6040_REG_DMICBCTL, 0, 0), 1111 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias", 1112 TWL6040_REG_DMICBCTL, 4, 0), 1113 1114 /* DACs */ 1115 SND_SOC_DAPM_DAC("HSDAC Left", "Headset Playback", SND_SOC_NOPM, 0, 0), 1116 SND_SOC_DAPM_DAC("HSDAC Right", "Headset Playback", SND_SOC_NOPM, 0, 0), 1117 SND_SOC_DAPM_DAC("HFDAC Left", "Handsfree Playback", 1118 TWL6040_REG_HFLCTL, 0, 0), 1119 SND_SOC_DAPM_DAC("HFDAC Right", "Handsfree Playback", 1120 TWL6040_REG_HFRCTL, 0, 0), 1121 /* Virtual DAC for vibra path (DL4 channel) */ 1122 SND_SOC_DAPM_DAC("VIBRA DAC", "Vibra Playback", 1123 SND_SOC_NOPM, 0, 0), 1124 1125 SND_SOC_DAPM_MUX("Handsfree Left Playback", 1126 SND_SOC_NOPM, 0, 0, &hfl_mux_controls), 1127 SND_SOC_DAPM_MUX("Handsfree Right Playback", 1128 SND_SOC_NOPM, 0, 0, &hfr_mux_controls), 1129 /* Analog playback Muxes */ 1130 SND_SOC_DAPM_MUX("Headset Left Playback", 1131 SND_SOC_NOPM, 0, 0, &hsl_mux_controls), 1132 SND_SOC_DAPM_MUX("Headset Right Playback", 1133 SND_SOC_NOPM, 0, 0, &hsr_mux_controls), 1134 1135 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0, 1136 &vibral_mux_controls), 1137 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0, 1138 &vibrar_mux_controls), 1139 1140 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0, 1141 &ep_path_enable_control), 1142 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0, 1143 &auxl_switch_control), 1144 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0, 1145 &auxr_switch_control), 1146 1147 /* Analog playback drivers */ 1148 SND_SOC_DAPM_OUT_DRV_E("HF Left Driver", 1149 TWL6040_REG_HFLCTL, 4, 0, NULL, 0, 1150 out_drv_event, 1151 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1152 SND_SOC_DAPM_OUT_DRV_E("HF Right Driver", 1153 TWL6040_REG_HFRCTL, 4, 0, NULL, 0, 1154 out_drv_event, 1155 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1156 SND_SOC_DAPM_OUT_DRV_E("HS Left Driver", 1157 TWL6040_REG_HSLCTL, 2, 0, NULL, 0, 1158 out_drv_event, 1159 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1160 SND_SOC_DAPM_OUT_DRV_E("HS Right Driver", 1161 TWL6040_REG_HSRCTL, 2, 0, NULL, 0, 1162 out_drv_event, 1163 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1164 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver", 1165 TWL6040_REG_EARCTL, 0, 0, NULL, 0, 1166 twl6040_ep_drv_event, 1167 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 1168 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver", 1169 TWL6040_REG_VIBCTLL, 0, 0, NULL, 0), 1170 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver", 1171 TWL6040_REG_VIBCTLR, 0, 0, NULL, 0), 1172 1173 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0, 1174 NULL, 0), 1175 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0, 1176 NULL, 0), 1177 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0, 1178 twl6040_hs_dac_event, 1179 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 1180 1181 /* Analog playback PGAs */ 1182 SND_SOC_DAPM_PGA("HF Left PGA", 1183 TWL6040_REG_HFLCTL, 1, 0, NULL, 0), 1184 SND_SOC_DAPM_PGA("HF Right PGA", 1185 TWL6040_REG_HFRCTL, 1, 0, NULL, 0), 1186 1187 }; 1188 1189 static const struct snd_soc_dapm_route intercon[] = { 1190 /* Capture path */ 1191 {"Analog Left Capture Route", "Headset Mic", "HSMIC"}, 1192 {"Analog Left Capture Route", "Main Mic", "MAINMIC"}, 1193 {"Analog Left Capture Route", "Aux/FM Left", "AFML"}, 1194 1195 {"Analog Right Capture Route", "Headset Mic", "HSMIC"}, 1196 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"}, 1197 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"}, 1198 1199 {"MicAmpL", NULL, "Analog Left Capture Route"}, 1200 {"MicAmpR", NULL, "Analog Right Capture Route"}, 1201 1202 {"ADC Left", NULL, "MicAmpL"}, 1203 {"ADC Right", NULL, "MicAmpR"}, 1204 1205 /* AFM path */ 1206 {"AFMAmpL", NULL, "AFML"}, 1207 {"AFMAmpR", NULL, "AFMR"}, 1208 1209 {"HSDAC Left", NULL, "HSDAC Power"}, 1210 {"HSDAC Right", NULL, "HSDAC Power"}, 1211 1212 {"Headset Left Playback", "HS DAC", "HSDAC Left"}, 1213 {"Headset Left Playback", "Line-In amp", "AFMAmpL"}, 1214 1215 {"Headset Right Playback", "HS DAC", "HSDAC Right"}, 1216 {"Headset Right Playback", "Line-In amp", "AFMAmpR"}, 1217 1218 {"HS Left Driver", NULL, "Headset Left Playback"}, 1219 {"HS Right Driver", NULL, "Headset Right Playback"}, 1220 1221 {"HSOL", NULL, "HS Left Driver"}, 1222 {"HSOR", NULL, "HS Right Driver"}, 1223 1224 /* Earphone playback path */ 1225 {"Earphone Playback", "Switch", "HSDAC Left"}, 1226 {"Earphone Driver", NULL, "Earphone Playback"}, 1227 {"EP", NULL, "Earphone Driver"}, 1228 1229 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"}, 1230 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"}, 1231 1232 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"}, 1233 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"}, 1234 1235 {"HF Left PGA", NULL, "Handsfree Left Playback"}, 1236 {"HF Right PGA", NULL, "Handsfree Right Playback"}, 1237 1238 {"HF Left Driver", NULL, "HF Left PGA"}, 1239 {"HF Right Driver", NULL, "HF Right PGA"}, 1240 1241 {"HFL", NULL, "HF Left Driver"}, 1242 {"HFR", NULL, "HF Right Driver"}, 1243 1244 {"AUXL Playback", "Switch", "HF Left PGA"}, 1245 {"AUXR Playback", "Switch", "HF Right PGA"}, 1246 1247 {"AUXL", NULL, "AUXL Playback"}, 1248 {"AUXR", NULL, "AUXR Playback"}, 1249 1250 /* Vibrator paths */ 1251 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"}, 1252 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"}, 1253 1254 {"Vibra Left Driver", NULL, "Vibra Left Playback"}, 1255 {"Vibra Right Driver", NULL, "Vibra Right Playback"}, 1256 {"Vibra Left Driver", NULL, "Vibra Left Control"}, 1257 {"Vibra Right Driver", NULL, "Vibra Right Control"}, 1258 1259 {"VIBRAL", NULL, "Vibra Left Driver"}, 1260 {"VIBRAR", NULL, "Vibra Right Driver"}, 1261 }; 1262 1263 static int twl6040_set_bias_level(struct snd_soc_codec *codec, 1264 enum snd_soc_bias_level level) 1265 { 1266 struct twl6040 *twl6040 = codec->control_data; 1267 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1268 int ret; 1269 1270 switch (level) { 1271 case SND_SOC_BIAS_ON: 1272 break; 1273 case SND_SOC_BIAS_PREPARE: 1274 break; 1275 case SND_SOC_BIAS_STANDBY: 1276 if (priv->codec_powered) 1277 break; 1278 1279 ret = twl6040_power(twl6040, 1); 1280 if (ret) 1281 return ret; 1282 1283 priv->codec_powered = 1; 1284 1285 twl6040_restore_regs(codec); 1286 1287 /* Set external boost GPO */ 1288 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02); 1289 break; 1290 case SND_SOC_BIAS_OFF: 1291 if (!priv->codec_powered) 1292 break; 1293 1294 twl6040_power(twl6040, 0); 1295 priv->codec_powered = 0; 1296 break; 1297 } 1298 1299 codec->dapm.bias_level = level; 1300 1301 return 0; 1302 } 1303 1304 static int twl6040_startup(struct snd_pcm_substream *substream, 1305 struct snd_soc_dai *dai) 1306 { 1307 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1308 struct snd_soc_codec *codec = rtd->codec; 1309 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1310 1311 snd_pcm_hw_constraint_list(substream->runtime, 0, 1312 SNDRV_PCM_HW_PARAM_RATE, 1313 &sysclk_constraints[priv->pll_power_mode]); 1314 1315 return 0; 1316 } 1317 1318 static int twl6040_hw_params(struct snd_pcm_substream *substream, 1319 struct snd_pcm_hw_params *params, 1320 struct snd_soc_dai *dai) 1321 { 1322 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1323 struct snd_soc_codec *codec = rtd->codec; 1324 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1325 int rate; 1326 1327 rate = params_rate(params); 1328 switch (rate) { 1329 case 11250: 1330 case 22500: 1331 case 44100: 1332 case 88200: 1333 /* These rates are not supported when HPPLL is in use */ 1334 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) { 1335 dev_err(codec->dev, "HPPLL does not support rate %d\n", 1336 rate); 1337 return -EINVAL; 1338 } 1339 priv->sysclk = 17640000; 1340 break; 1341 case 8000: 1342 case 16000: 1343 case 32000: 1344 case 48000: 1345 case 96000: 1346 priv->sysclk = 19200000; 1347 break; 1348 default: 1349 dev_err(codec->dev, "unsupported rate %d\n", rate); 1350 return -EINVAL; 1351 } 1352 1353 return 0; 1354 } 1355 1356 static int twl6040_prepare(struct snd_pcm_substream *substream, 1357 struct snd_soc_dai *dai) 1358 { 1359 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1360 struct snd_soc_codec *codec = rtd->codec; 1361 struct twl6040 *twl6040 = codec->control_data; 1362 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1363 int ret; 1364 1365 if (!priv->sysclk) { 1366 dev_err(codec->dev, 1367 "no mclk configured, call set_sysclk() on init\n"); 1368 return -EINVAL; 1369 } 1370 1371 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk); 1372 if (ret) { 1373 dev_err(codec->dev, "Can not set PLL (%d)\n", ret); 1374 return -EPERM; 1375 } 1376 1377 return 0; 1378 } 1379 1380 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai, 1381 int clk_id, unsigned int freq, int dir) 1382 { 1383 struct snd_soc_codec *codec = codec_dai->codec; 1384 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1385 1386 switch (clk_id) { 1387 case TWL6040_SYSCLK_SEL_LPPLL: 1388 case TWL6040_SYSCLK_SEL_HPPLL: 1389 priv->pll = clk_id; 1390 priv->clk_in = freq; 1391 break; 1392 default: 1393 dev_err(codec->dev, "unknown clk_id %d\n", clk_id); 1394 return -EINVAL; 1395 } 1396 1397 return 0; 1398 } 1399 1400 static struct snd_soc_dai_ops twl6040_dai_ops = { 1401 .startup = twl6040_startup, 1402 .hw_params = twl6040_hw_params, 1403 .prepare = twl6040_prepare, 1404 .set_sysclk = twl6040_set_dai_sysclk, 1405 }; 1406 1407 static struct snd_soc_dai_driver twl6040_dai[] = { 1408 { 1409 .name = "twl6040-legacy", 1410 .playback = { 1411 .stream_name = "Playback", 1412 .channels_min = 1, 1413 .channels_max = 5, 1414 .rates = TWL6040_RATES, 1415 .formats = TWL6040_FORMATS, 1416 }, 1417 .capture = { 1418 .stream_name = "Capture", 1419 .channels_min = 1, 1420 .channels_max = 2, 1421 .rates = TWL6040_RATES, 1422 .formats = TWL6040_FORMATS, 1423 }, 1424 .ops = &twl6040_dai_ops, 1425 }, 1426 { 1427 .name = "twl6040-ul", 1428 .capture = { 1429 .stream_name = "Capture", 1430 .channels_min = 1, 1431 .channels_max = 2, 1432 .rates = TWL6040_RATES, 1433 .formats = TWL6040_FORMATS, 1434 }, 1435 .ops = &twl6040_dai_ops, 1436 }, 1437 { 1438 .name = "twl6040-dl1", 1439 .playback = { 1440 .stream_name = "Headset Playback", 1441 .channels_min = 1, 1442 .channels_max = 2, 1443 .rates = TWL6040_RATES, 1444 .formats = TWL6040_FORMATS, 1445 }, 1446 .ops = &twl6040_dai_ops, 1447 }, 1448 { 1449 .name = "twl6040-dl2", 1450 .playback = { 1451 .stream_name = "Handsfree Playback", 1452 .channels_min = 1, 1453 .channels_max = 2, 1454 .rates = TWL6040_RATES, 1455 .formats = TWL6040_FORMATS, 1456 }, 1457 .ops = &twl6040_dai_ops, 1458 }, 1459 { 1460 .name = "twl6040-vib", 1461 .playback = { 1462 .stream_name = "Vibra Playback", 1463 .channels_min = 1, 1464 .channels_max = 1, 1465 .rates = SNDRV_PCM_RATE_CONTINUOUS, 1466 .formats = TWL6040_FORMATS, 1467 }, 1468 .ops = &twl6040_dai_ops, 1469 }, 1470 }; 1471 1472 #ifdef CONFIG_PM 1473 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state) 1474 { 1475 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); 1476 1477 return 0; 1478 } 1479 1480 static int twl6040_resume(struct snd_soc_codec *codec) 1481 { 1482 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 1483 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level); 1484 1485 return 0; 1486 } 1487 #else 1488 #define twl6040_suspend NULL 1489 #define twl6040_resume NULL 1490 #endif 1491 1492 static int twl6040_probe(struct snd_soc_codec *codec) 1493 { 1494 struct twl6040_data *priv; 1495 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev); 1496 struct platform_device *pdev = container_of(codec->dev, 1497 struct platform_device, dev); 1498 int ret = 0; 1499 1500 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL); 1501 if (priv == NULL) 1502 return -ENOMEM; 1503 snd_soc_codec_set_drvdata(codec, priv); 1504 1505 priv->codec = codec; 1506 codec->control_data = dev_get_drvdata(codec->dev->parent); 1507 codec->ignore_pmdown_time = 1; 1508 1509 if (pdata && pdata->hs_left_step && pdata->hs_right_step) { 1510 priv->hs_left_step = pdata->hs_left_step; 1511 priv->hs_right_step = pdata->hs_right_step; 1512 } else { 1513 priv->hs_left_step = 1; 1514 priv->hs_right_step = 1; 1515 } 1516 1517 if (pdata && pdata->hf_left_step && pdata->hf_right_step) { 1518 priv->hf_left_step = pdata->hf_left_step; 1519 priv->hf_right_step = pdata->hf_right_step; 1520 } else { 1521 priv->hf_left_step = 1; 1522 priv->hf_right_step = 1; 1523 } 1524 1525 priv->plug_irq = platform_get_irq(pdev, 0); 1526 if (priv->plug_irq < 0) { 1527 dev_err(codec->dev, "invalid irq\n"); 1528 ret = -EINVAL; 1529 goto work_err; 1530 } 1531 1532 priv->workqueue = alloc_workqueue("twl6040-codec", 0, 0); 1533 if (!priv->workqueue) { 1534 ret = -ENOMEM; 1535 goto work_err; 1536 } 1537 1538 INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work); 1539 INIT_DELAYED_WORK(&priv->headset.work, twl6040_pga_hs_work); 1540 INIT_DELAYED_WORK(&priv->handsfree.work, twl6040_pga_hf_work); 1541 1542 mutex_init(&priv->mutex); 1543 1544 init_completion(&priv->headset.ramp_done); 1545 init_completion(&priv->handsfree.ramp_done); 1546 1547 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler, 1548 0, "twl6040_irq_plug", codec); 1549 if (ret) { 1550 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret); 1551 goto plugirq_err; 1552 } 1553 1554 twl6040_init_chip(codec); 1555 1556 /* power on device */ 1557 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 1558 if (!ret) 1559 return 0; 1560 1561 /* Error path */ 1562 free_irq(priv->plug_irq, codec); 1563 plugirq_err: 1564 destroy_workqueue(priv->workqueue); 1565 work_err: 1566 kfree(priv); 1567 return ret; 1568 } 1569 1570 static int twl6040_remove(struct snd_soc_codec *codec) 1571 { 1572 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); 1573 1574 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); 1575 free_irq(priv->plug_irq, codec); 1576 destroy_workqueue(priv->workqueue); 1577 kfree(priv); 1578 1579 return 0; 1580 } 1581 1582 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = { 1583 .probe = twl6040_probe, 1584 .remove = twl6040_remove, 1585 .suspend = twl6040_suspend, 1586 .resume = twl6040_resume, 1587 .read = twl6040_read_reg_cache, 1588 .write = twl6040_write, 1589 .set_bias_level = twl6040_set_bias_level, 1590 .reg_cache_size = ARRAY_SIZE(twl6040_reg), 1591 .reg_word_size = sizeof(u8), 1592 .reg_cache_default = twl6040_reg, 1593 1594 .controls = twl6040_snd_controls, 1595 .num_controls = ARRAY_SIZE(twl6040_snd_controls), 1596 .dapm_widgets = twl6040_dapm_widgets, 1597 .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets), 1598 .dapm_routes = intercon, 1599 .num_dapm_routes = ARRAY_SIZE(intercon), 1600 }; 1601 1602 static int __devinit twl6040_codec_probe(struct platform_device *pdev) 1603 { 1604 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040, 1605 twl6040_dai, ARRAY_SIZE(twl6040_dai)); 1606 } 1607 1608 static int __devexit twl6040_codec_remove(struct platform_device *pdev) 1609 { 1610 snd_soc_unregister_codec(&pdev->dev); 1611 return 0; 1612 } 1613 1614 static struct platform_driver twl6040_codec_driver = { 1615 .driver = { 1616 .name = "twl6040-codec", 1617 .owner = THIS_MODULE, 1618 }, 1619 .probe = twl6040_codec_probe, 1620 .remove = __devexit_p(twl6040_codec_remove), 1621 }; 1622 1623 static int __init twl6040_codec_init(void) 1624 { 1625 return platform_driver_register(&twl6040_codec_driver); 1626 } 1627 module_init(twl6040_codec_init); 1628 1629 static void __exit twl6040_codec_exit(void) 1630 { 1631 platform_driver_unregister(&twl6040_codec_driver); 1632 } 1633 module_exit(twl6040_codec_exit); 1634 1635 MODULE_DESCRIPTION("ASoC TWL6040 codec driver"); 1636 MODULE_AUTHOR("Misael Lopez Cruz"); 1637 MODULE_LICENSE("GPL"); 1638