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