2 * ALSA SoC TWL6040 codec driver
4 * Author: Misael Lopez Cruz <x0052729@ti.com>
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.
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.
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
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/mfd/twl6040.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/soc-dapm.h>
36 #include <sound/initval.h>
37 #include <sound/tlv.h>
42 TWL6040_DAI_LEGACY = 0,
49 #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
50 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
52 #define TWL6040_OUTHS_0dB 0x00
53 #define TWL6040_OUTHS_M30dB 0x0F
54 #define TWL6040_OUTHF_0dB 0x03
55 #define TWL6040_OUTHF_M52dB 0x1D
57 /* Shadow register used by the driver */
58 #define TWL6040_REG_SW_SHADOW 0x2F
59 #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1)
61 /* TWL6040_REG_SW_SHADOW (0x2F) fields */
62 #define TWL6040_EAR_PATH_ENABLE 0x01
64 struct twl6040_jack_data {
65 struct snd_soc_jack *jack;
66 struct delayed_work work;
70 /* codec private data */
77 int hs_power_mode_locked;
82 struct twl6040_jack_data hs_jack;
83 struct snd_soc_codec *codec;
88 * twl6040 register cache & default register settings
90 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
91 0x00, /* not used 0x00 */
92 0x4B, /* REG_ASICID 0x01 (ro) */
93 0x00, /* REG_ASICREV 0x02 (ro) */
94 0x00, /* REG_INTID 0x03 */
95 0x00, /* REG_INTMR 0x04 */
96 0x00, /* REG_NCPCTRL 0x05 */
97 0x00, /* REG_LDOCTL 0x06 */
98 0x60, /* REG_HPPLLCTL 0x07 */
99 0x00, /* REG_LPPLLCTL 0x08 */
100 0x4A, /* REG_LPPLLDIV 0x09 */
101 0x00, /* REG_AMICBCTL 0x0A */
102 0x00, /* REG_DMICBCTL 0x0B */
103 0x00, /* REG_MICLCTL 0x0C */
104 0x00, /* REG_MICRCTL 0x0D */
105 0x00, /* REG_MICGAIN 0x0E */
106 0x1B, /* REG_LINEGAIN 0x0F */
107 0x00, /* REG_HSLCTL 0x10 */
108 0x00, /* REG_HSRCTL 0x11 */
109 0x00, /* REG_HSGAIN 0x12 */
110 0x00, /* REG_EARCTL 0x13 */
111 0x00, /* REG_HFLCTL 0x14 */
112 0x00, /* REG_HFLGAIN 0x15 */
113 0x00, /* REG_HFRCTL 0x16 */
114 0x00, /* REG_HFRGAIN 0x17 */
115 0x00, /* REG_VIBCTLL 0x18 */
116 0x00, /* REG_VIBDATL 0x19 */
117 0x00, /* REG_VIBCTLR 0x1A */
118 0x00, /* REG_VIBDATR 0x1B */
119 0x00, /* REG_HKCTL1 0x1C */
120 0x00, /* REG_HKCTL2 0x1D */
121 0x00, /* REG_GPOCTL 0x1E */
122 0x00, /* REG_ALB 0x1F */
123 0x00, /* REG_DLB 0x20 */
124 0x00, /* not used 0x21 */
125 0x00, /* not used 0x22 */
126 0x00, /* not used 0x23 */
127 0x00, /* not used 0x24 */
128 0x00, /* not used 0x25 */
129 0x00, /* not used 0x26 */
130 0x00, /* not used 0x27 */
131 0x00, /* REG_TRIM1 0x28 */
132 0x00, /* REG_TRIM2 0x29 */
133 0x00, /* REG_TRIM3 0x2A */
134 0x00, /* REG_HSOTRIM 0x2B */
135 0x00, /* REG_HFOTRIM 0x2C */
136 0x09, /* REG_ACCCTL 0x2D */
137 0x00, /* REG_STATUS 0x2E (ro) */
139 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */
142 /* List of registers to be restored after power up */
143 static const int twl6040_restore_list[] = {
147 TWL6040_REG_LINEGAIN,
158 /* set of rates for each pll: low-power and high-performance */
159 static unsigned int lp_rates[] = {
171 static unsigned int hp_rates[] = {
179 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
180 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
181 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
185 * read twl6040 register cache
187 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
190 u8 *cache = codec->reg_cache;
192 if (reg >= TWL6040_CACHEREGNUM)
199 * write twl6040 register cache
201 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
204 u8 *cache = codec->reg_cache;
206 if (reg >= TWL6040_CACHEREGNUM)
212 * read from twl6040 hardware register
214 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
217 struct twl6040 *twl6040 = codec->control_data;
220 if (reg >= TWL6040_CACHEREGNUM)
223 if (likely(reg < TWL6040_REG_SW_SHADOW)) {
224 value = twl6040_reg_read(twl6040, reg);
225 twl6040_write_reg_cache(codec, reg, value);
227 value = twl6040_read_reg_cache(codec, reg);
233 static bool twl6040_is_path_unmuted(struct snd_soc_codec *codec,
236 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
239 case TWL6040_REG_HSLCTL:
240 case TWL6040_REG_HSRCTL:
241 case TWL6040_REG_EARCTL:
243 return priv->dl1_unmuted;
244 case TWL6040_REG_HFLCTL:
245 case TWL6040_REG_HFRCTL:
246 return priv->dl2_unmuted;
253 * write to the twl6040 register space
255 static int twl6040_write(struct snd_soc_codec *codec,
256 unsigned int reg, unsigned int value)
258 struct twl6040 *twl6040 = codec->control_data;
260 if (reg >= TWL6040_CACHEREGNUM)
263 twl6040_write_reg_cache(codec, reg, value);
264 if (likely(reg < TWL6040_REG_SW_SHADOW) &&
265 twl6040_is_path_unmuted(codec, reg))
266 return twl6040_reg_write(twl6040, reg, value);
271 static void twl6040_init_chip(struct snd_soc_codec *codec)
273 struct twl6040 *twl6040 = codec->control_data;
276 /* Update reg_cache: ASICREV, and TRIM values */
277 val = twl6040_get_revid(twl6040);
278 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
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);
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);
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.
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);
303 static void twl6040_restore_regs(struct snd_soc_codec *codec)
305 u8 *cache = codec->reg_cache;
308 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
309 reg = twl6040_restore_list[i];
310 twl6040_write(codec, reg, cache[reg]);
314 /* set headset dac and driver power mode */
315 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
318 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
320 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
321 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
331 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
332 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
337 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
338 struct snd_kcontrol *kcontrol, int event)
340 struct snd_soc_codec *codec = w->codec;
344 * Workaround for Headset DC offset caused pop noise:
345 * Both HS DAC need to be turned on (before the HS driver) and off at
348 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
349 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
350 if (SND_SOC_DAPM_EVENT_ON(event)) {
351 hslctl |= TWL6040_HSDACENA;
352 hsrctl |= TWL6040_HSDACENA;
354 hslctl &= ~TWL6040_HSDACENA;
355 hsrctl &= ~TWL6040_HSDACENA;
357 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
358 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
364 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w,
365 struct snd_kcontrol *kcontrol, int event)
367 struct snd_soc_codec *codec = w->codec;
368 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
371 if (SND_SOC_DAPM_EVENT_ON(event)) {
372 /* Earphone doesn't support low power mode */
373 priv->hs_power_mode_locked = 1;
374 ret = headset_power_mode(codec, 1);
376 priv->hs_power_mode_locked = 0;
377 ret = headset_power_mode(codec, priv->hs_power_mode);
385 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
386 struct snd_soc_jack *jack, int report)
388 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
391 mutex_lock(&priv->mutex);
394 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
395 if (status & TWL6040_PLUGCOMP)
396 snd_soc_jack_report(jack, report, report);
398 snd_soc_jack_report(jack, 0, report);
400 mutex_unlock(&priv->mutex);
403 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
404 struct snd_soc_jack *jack, int report)
406 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
407 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
409 hs_jack->jack = jack;
410 hs_jack->report = report;
412 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
414 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
416 static void twl6040_accessory_work(struct work_struct *work)
418 struct twl6040_data *priv = container_of(work,
419 struct twl6040_data, hs_jack.work.work);
420 struct snd_soc_codec *codec = priv->codec;
421 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
423 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
426 /* audio interrupt handler */
427 static irqreturn_t twl6040_audio_handler(int irq, void *data)
429 struct snd_soc_codec *codec = data;
430 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
432 queue_delayed_work(system_power_efficient_wq,
433 &priv->hs_jack.work, msecs_to_jiffies(200));
438 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
439 struct snd_ctl_elem_value *ucontrol)
441 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
442 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
445 /* Do not allow changes while Input/FF efect is running */
446 val = twl6040_read_reg_volatile(codec, e->reg);
447 if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
450 return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
454 * MICATT volume control:
455 * from -6 to 0 dB in 6 dB steps
457 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
460 * MICGAIN volume control:
461 * from 6 to 30 dB in 6 dB steps
463 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
466 * AFMGAIN volume control:
467 * from -18 to 24 dB in 6 dB steps
469 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
472 * HSGAIN volume control:
473 * from -30 to 0 dB in 2 dB steps
475 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
478 * HFGAIN volume control:
479 * from -52 to 6 dB in 2 dB steps
481 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
484 * EPGAIN volume control:
485 * from -24 to 6 dB in 2 dB steps
487 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
489 /* Left analog microphone selection */
490 static const char *twl6040_amicl_texts[] =
491 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
493 /* Right analog microphone selection */
494 static const char *twl6040_amicr_texts[] =
495 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
497 static const struct soc_enum twl6040_enum[] = {
498 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
499 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
502 static const char *twl6040_hs_texts[] = {
503 "Off", "HS DAC", "Line-In amp"
506 static const struct soc_enum twl6040_hs_enum[] = {
507 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
509 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
513 static const char *twl6040_hf_texts[] = {
514 "Off", "HF DAC", "Line-In amp"
517 static const struct soc_enum twl6040_hf_enum[] = {
518 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
520 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
524 static const char *twl6040_vibrapath_texts[] = {
525 "Input FF", "Audio PDM"
528 static const struct soc_enum twl6040_vibra_enum[] = {
529 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
530 ARRAY_SIZE(twl6040_vibrapath_texts),
531 twl6040_vibrapath_texts),
532 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
533 ARRAY_SIZE(twl6040_vibrapath_texts),
534 twl6040_vibrapath_texts),
537 static const struct snd_kcontrol_new amicl_control =
538 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
540 static const struct snd_kcontrol_new amicr_control =
541 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
543 /* Headset DAC playback switches */
544 static const struct snd_kcontrol_new hsl_mux_controls =
545 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
547 static const struct snd_kcontrol_new hsr_mux_controls =
548 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
550 /* Handsfree DAC playback switches */
551 static const struct snd_kcontrol_new hfl_mux_controls =
552 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
554 static const struct snd_kcontrol_new hfr_mux_controls =
555 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
557 static const struct snd_kcontrol_new ep_path_enable_control =
558 SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
560 static const struct snd_kcontrol_new auxl_switch_control =
561 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
563 static const struct snd_kcontrol_new auxr_switch_control =
564 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
566 /* Vibra playback switches */
567 static const struct snd_kcontrol_new vibral_mux_controls =
568 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
569 snd_soc_dapm_get_enum_double,
570 twl6040_soc_dapm_put_vibra_enum);
572 static const struct snd_kcontrol_new vibrar_mux_controls =
573 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
574 snd_soc_dapm_get_enum_double,
575 twl6040_soc_dapm_put_vibra_enum);
577 /* Headset power mode */
578 static const char *twl6040_power_mode_texts[] = {
579 "Low-Power", "High-Performance",
582 static const struct soc_enum twl6040_power_mode_enum =
583 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
584 twl6040_power_mode_texts);
586 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
587 struct snd_ctl_elem_value *ucontrol)
589 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
590 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
592 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
597 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
598 struct snd_ctl_elem_value *ucontrol)
600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
601 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
602 int high_perf = ucontrol->value.enumerated.item[0];
605 if (!priv->hs_power_mode_locked)
606 ret = headset_power_mode(codec, high_perf);
609 priv->hs_power_mode = high_perf;
614 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
615 struct snd_ctl_elem_value *ucontrol)
617 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
618 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
620 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
625 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
626 struct snd_ctl_elem_value *ucontrol)
628 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
629 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
631 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
636 int twl6040_get_dl1_gain(struct snd_soc_codec *codec)
638 struct snd_soc_dapm_context *dapm = &codec->dapm;
640 if (snd_soc_dapm_get_pin_status(dapm, "EP"))
641 return -1; /* -1dB */
643 if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
644 snd_soc_dapm_get_pin_status(dapm, "HSOL")) {
646 u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL);
647 if (val & TWL6040_HSDACMODE)
648 /* HSDACL in LP mode */
649 return -8; /* -8dB */
651 /* HSDACL in HP mode */
652 return -1; /* -1dB */
656 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);
658 int twl6040_get_clk_id(struct snd_soc_codec *codec)
660 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
662 return priv->pll_power_mode;
664 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
666 int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
668 if (unlikely(trim >= TWL6040_TRIM_INVAL))
671 return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim);
673 EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
675 int twl6040_get_hs_step_size(struct snd_soc_codec *codec)
677 struct twl6040 *twl6040 = codec->control_data;
679 if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3)
680 /* For ES under ES_1.3 HS step is 2 mV */
683 /* For ES_1.3 HS step is 1 mV */
686 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);
688 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
690 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
691 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
692 SOC_DOUBLE_TLV("Capture Volume",
693 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
696 SOC_DOUBLE_TLV("Aux FM Volume",
697 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
700 SOC_DOUBLE_TLV("Headset Playback Volume",
701 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
702 SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
703 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
704 SOC_SINGLE_TLV("Earphone Playback Volume",
705 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
707 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
708 twl6040_headset_power_get_enum,
709 twl6040_headset_power_put_enum),
711 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
712 twl6040_pll_get_enum, twl6040_pll_put_enum),
715 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
717 SND_SOC_DAPM_INPUT("MAINMIC"),
718 SND_SOC_DAPM_INPUT("HSMIC"),
719 SND_SOC_DAPM_INPUT("SUBMIC"),
720 SND_SOC_DAPM_INPUT("AFML"),
721 SND_SOC_DAPM_INPUT("AFMR"),
724 SND_SOC_DAPM_OUTPUT("HSOL"),
725 SND_SOC_DAPM_OUTPUT("HSOR"),
726 SND_SOC_DAPM_OUTPUT("HFL"),
727 SND_SOC_DAPM_OUTPUT("HFR"),
728 SND_SOC_DAPM_OUTPUT("EP"),
729 SND_SOC_DAPM_OUTPUT("AUXL"),
730 SND_SOC_DAPM_OUTPUT("AUXR"),
731 SND_SOC_DAPM_OUTPUT("VIBRAL"),
732 SND_SOC_DAPM_OUTPUT("VIBRAR"),
734 /* Analog input muxes for the capture amplifiers */
735 SND_SOC_DAPM_MUX("Analog Left Capture Route",
736 SND_SOC_NOPM, 0, 0, &amicl_control),
737 SND_SOC_DAPM_MUX("Analog Right Capture Route",
738 SND_SOC_NOPM, 0, 0, &amicr_control),
740 /* Analog capture PGAs */
741 SND_SOC_DAPM_PGA("MicAmpL",
742 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
743 SND_SOC_DAPM_PGA("MicAmpR",
744 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
746 /* Auxiliary FM PGAs */
747 SND_SOC_DAPM_PGA("AFMAmpL",
748 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
749 SND_SOC_DAPM_PGA("AFMAmpR",
750 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
753 SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0),
754 SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0),
756 /* Microphone bias */
757 SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
758 TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
759 SND_SOC_DAPM_SUPPLY("Main Mic Bias",
760 TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
761 SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
762 TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
763 SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
764 TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
767 SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0),
768 SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0),
769 SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0),
770 SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0),
771 /* Virtual DAC for vibra path (DL4 channel) */
772 SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0),
774 SND_SOC_DAPM_MUX("Handsfree Left Playback",
775 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
776 SND_SOC_DAPM_MUX("Handsfree Right Playback",
777 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
778 /* Analog playback Muxes */
779 SND_SOC_DAPM_MUX("Headset Left Playback",
780 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
781 SND_SOC_DAPM_MUX("Headset Right Playback",
782 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
784 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
785 &vibral_mux_controls),
786 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
787 &vibrar_mux_controls),
789 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
790 &ep_path_enable_control),
791 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
792 &auxl_switch_control),
793 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
794 &auxr_switch_control),
796 /* Analog playback drivers */
797 SND_SOC_DAPM_OUT_DRV("HF Left Driver",
798 TWL6040_REG_HFLCTL, 4, 0, NULL, 0),
799 SND_SOC_DAPM_OUT_DRV("HF Right Driver",
800 TWL6040_REG_HFRCTL, 4, 0, NULL, 0),
801 SND_SOC_DAPM_OUT_DRV("HS Left Driver",
802 TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
803 SND_SOC_DAPM_OUT_DRV("HS Right Driver",
804 TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
805 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
806 TWL6040_REG_EARCTL, 0, 0, NULL, 0,
807 twl6040_ep_drv_event,
808 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
809 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
810 TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
811 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
812 TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),
814 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
816 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
818 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
819 twl6040_hs_dac_event,
820 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
822 /* Analog playback PGAs */
823 SND_SOC_DAPM_PGA("HF Left PGA",
824 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
825 SND_SOC_DAPM_PGA("HF Right PGA",
826 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
830 static const struct snd_soc_dapm_route intercon[] = {
831 /* Stream -> DAC mapping */
832 {"HSDAC Left", NULL, "Legacy Playback"},
833 {"HSDAC Left", NULL, "Headset Playback"},
834 {"HSDAC Right", NULL, "Legacy Playback"},
835 {"HSDAC Right", NULL, "Headset Playback"},
837 {"HFDAC Left", NULL, "Legacy Playback"},
838 {"HFDAC Left", NULL, "Handsfree Playback"},
839 {"HFDAC Right", NULL, "Legacy Playback"},
840 {"HFDAC Right", NULL, "Handsfree Playback"},
842 {"VIBRA DAC", NULL, "Legacy Playback"},
843 {"VIBRA DAC", NULL, "Vibra Playback"},
845 /* ADC -> Stream mapping */
846 {"Legacy Capture" , NULL, "ADC Left"},
847 {"Capture", NULL, "ADC Left"},
848 {"Legacy Capture", NULL, "ADC Right"},
849 {"Capture" , NULL, "ADC Right"},
852 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
853 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
854 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
856 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
857 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
858 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
860 {"MicAmpL", NULL, "Analog Left Capture Route"},
861 {"MicAmpR", NULL, "Analog Right Capture Route"},
863 {"ADC Left", NULL, "MicAmpL"},
864 {"ADC Right", NULL, "MicAmpR"},
867 {"AFMAmpL", NULL, "AFML"},
868 {"AFMAmpR", NULL, "AFMR"},
870 {"HSDAC Left", NULL, "HSDAC Power"},
871 {"HSDAC Right", NULL, "HSDAC Power"},
873 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
874 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
876 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
877 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
879 {"HS Left Driver", NULL, "Headset Left Playback"},
880 {"HS Right Driver", NULL, "Headset Right Playback"},
882 {"HSOL", NULL, "HS Left Driver"},
883 {"HSOR", NULL, "HS Right Driver"},
885 /* Earphone playback path */
886 {"Earphone Playback", "Switch", "HSDAC Left"},
887 {"Earphone Driver", NULL, "Earphone Playback"},
888 {"EP", NULL, "Earphone Driver"},
890 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
891 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
893 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
894 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
896 {"HF Left PGA", NULL, "Handsfree Left Playback"},
897 {"HF Right PGA", NULL, "Handsfree Right Playback"},
899 {"HF Left Driver", NULL, "HF Left PGA"},
900 {"HF Right Driver", NULL, "HF Right PGA"},
902 {"HFL", NULL, "HF Left Driver"},
903 {"HFR", NULL, "HF Right Driver"},
905 {"AUXL Playback", "Switch", "HF Left PGA"},
906 {"AUXR Playback", "Switch", "HF Right PGA"},
908 {"AUXL", NULL, "AUXL Playback"},
909 {"AUXR", NULL, "AUXR Playback"},
912 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
913 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
915 {"Vibra Left Driver", NULL, "Vibra Left Playback"},
916 {"Vibra Right Driver", NULL, "Vibra Right Playback"},
917 {"Vibra Left Driver", NULL, "Vibra Left Control"},
918 {"Vibra Right Driver", NULL, "Vibra Right Control"},
920 {"VIBRAL", NULL, "Vibra Left Driver"},
921 {"VIBRAR", NULL, "Vibra Right Driver"},
924 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
925 enum snd_soc_bias_level level)
927 struct twl6040 *twl6040 = codec->control_data;
928 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
932 case SND_SOC_BIAS_ON:
934 case SND_SOC_BIAS_PREPARE:
936 case SND_SOC_BIAS_STANDBY:
937 if (priv->codec_powered)
940 ret = twl6040_power(twl6040, 1);
944 priv->codec_powered = 1;
946 twl6040_restore_regs(codec);
948 /* Set external boost GPO */
949 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
951 case SND_SOC_BIAS_OFF:
952 if (!priv->codec_powered)
955 twl6040_power(twl6040, 0);
956 priv->codec_powered = 0;
960 codec->dapm.bias_level = level;
965 static int twl6040_startup(struct snd_pcm_substream *substream,
966 struct snd_soc_dai *dai)
968 struct snd_soc_codec *codec = dai->codec;
969 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
971 snd_pcm_hw_constraint_list(substream->runtime, 0,
972 SNDRV_PCM_HW_PARAM_RATE,
973 &sysclk_constraints[priv->pll_power_mode]);
978 static int twl6040_hw_params(struct snd_pcm_substream *substream,
979 struct snd_pcm_hw_params *params,
980 struct snd_soc_dai *dai)
982 struct snd_soc_codec *codec = dai->codec;
983 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
986 rate = params_rate(params);
992 /* These rates are not supported when HPPLL is in use */
993 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
994 dev_err(codec->dev, "HPPLL does not support rate %d\n",
998 priv->sysclk = 17640000;
1005 priv->sysclk = 19200000;
1008 dev_err(codec->dev, "unsupported rate %d\n", rate);
1015 static int twl6040_prepare(struct snd_pcm_substream *substream,
1016 struct snd_soc_dai *dai)
1018 struct snd_soc_codec *codec = dai->codec;
1019 struct twl6040 *twl6040 = codec->control_data;
1020 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1023 if (!priv->sysclk) {
1025 "no mclk configured, call set_sysclk() on init\n");
1029 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1031 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1038 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1039 int clk_id, unsigned int freq, int dir)
1041 struct snd_soc_codec *codec = codec_dai->codec;
1042 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1045 case TWL6040_SYSCLK_SEL_LPPLL:
1046 case TWL6040_SYSCLK_SEL_HPPLL:
1048 priv->clk_in = freq;
1051 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1058 static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id id,
1061 struct twl6040 *twl6040 = codec->control_data;
1062 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1063 int hslctl, hsrctl, earctl;
1067 case TWL6040_DAI_DL1:
1068 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
1069 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
1070 earctl = twl6040_read_reg_cache(codec, TWL6040_REG_EARCTL);
1073 /* Power down drivers and DACs */
1075 hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
1076 hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
1080 twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl);
1081 twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl);
1082 twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl);
1083 priv->dl1_unmuted = !mute;
1085 case TWL6040_DAI_DL2:
1086 hflctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFLCTL);
1087 hfrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFRCTL);
1090 /* Power down drivers and DACs */
1091 hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
1093 hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
1097 twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl);
1098 twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl);
1099 priv->dl2_unmuted = !mute;
1106 static int twl6040_digital_mute(struct snd_soc_dai *dai, int mute)
1109 case TWL6040_DAI_LEGACY:
1110 twl6040_mute_path(dai->codec, TWL6040_DAI_DL1, mute);
1111 twl6040_mute_path(dai->codec, TWL6040_DAI_DL2, mute);
1113 case TWL6040_DAI_DL1:
1114 case TWL6040_DAI_DL2:
1115 twl6040_mute_path(dai->codec, dai->id, mute);
1124 static const struct snd_soc_dai_ops twl6040_dai_ops = {
1125 .startup = twl6040_startup,
1126 .hw_params = twl6040_hw_params,
1127 .prepare = twl6040_prepare,
1128 .set_sysclk = twl6040_set_dai_sysclk,
1129 .digital_mute = twl6040_digital_mute,
1132 static struct snd_soc_dai_driver twl6040_dai[] = {
1134 .name = "twl6040-legacy",
1135 .id = TWL6040_DAI_LEGACY,
1137 .stream_name = "Legacy Playback",
1140 .rates = TWL6040_RATES,
1141 .formats = TWL6040_FORMATS,
1144 .stream_name = "Legacy Capture",
1147 .rates = TWL6040_RATES,
1148 .formats = TWL6040_FORMATS,
1150 .ops = &twl6040_dai_ops,
1153 .name = "twl6040-ul",
1154 .id = TWL6040_DAI_UL,
1156 .stream_name = "Capture",
1159 .rates = TWL6040_RATES,
1160 .formats = TWL6040_FORMATS,
1162 .ops = &twl6040_dai_ops,
1165 .name = "twl6040-dl1",
1166 .id = TWL6040_DAI_DL1,
1168 .stream_name = "Headset Playback",
1171 .rates = TWL6040_RATES,
1172 .formats = TWL6040_FORMATS,
1174 .ops = &twl6040_dai_ops,
1177 .name = "twl6040-dl2",
1178 .id = TWL6040_DAI_DL2,
1180 .stream_name = "Handsfree Playback",
1183 .rates = TWL6040_RATES,
1184 .formats = TWL6040_FORMATS,
1186 .ops = &twl6040_dai_ops,
1189 .name = "twl6040-vib",
1190 .id = TWL6040_DAI_VIB,
1192 .stream_name = "Vibra Playback",
1195 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1196 .formats = TWL6040_FORMATS,
1198 .ops = &twl6040_dai_ops,
1203 static int twl6040_suspend(struct snd_soc_codec *codec)
1205 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1210 static int twl6040_resume(struct snd_soc_codec *codec)
1212 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1217 #define twl6040_suspend NULL
1218 #define twl6040_resume NULL
1221 static int twl6040_probe(struct snd_soc_codec *codec)
1223 struct twl6040_data *priv;
1224 struct platform_device *pdev = container_of(codec->dev,
1225 struct platform_device, dev);
1228 priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL);
1232 snd_soc_codec_set_drvdata(codec, priv);
1234 priv->codec = codec;
1235 codec->control_data = dev_get_drvdata(codec->dev->parent);
1237 priv->plug_irq = platform_get_irq(pdev, 0);
1238 if (priv->plug_irq < 0) {
1239 dev_err(codec->dev, "invalid irq\n");
1243 INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
1245 mutex_init(&priv->mutex);
1247 ret = request_threaded_irq(priv->plug_irq, NULL,
1248 twl6040_audio_handler, IRQF_NO_SUSPEND,
1249 "twl6040_irq_plug", codec);
1251 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1255 twl6040_init_chip(codec);
1257 /* power on device */
1258 return twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1261 static int twl6040_remove(struct snd_soc_codec *codec)
1263 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1265 free_irq(priv->plug_irq, codec);
1266 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1271 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1272 .probe = twl6040_probe,
1273 .remove = twl6040_remove,
1274 .suspend = twl6040_suspend,
1275 .resume = twl6040_resume,
1276 .read = twl6040_read_reg_cache,
1277 .write = twl6040_write,
1278 .set_bias_level = twl6040_set_bias_level,
1279 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1280 .reg_word_size = sizeof(u8),
1281 .reg_cache_default = twl6040_reg,
1282 .ignore_pmdown_time = true,
1284 .controls = twl6040_snd_controls,
1285 .num_controls = ARRAY_SIZE(twl6040_snd_controls),
1286 .dapm_widgets = twl6040_dapm_widgets,
1287 .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
1288 .dapm_routes = intercon,
1289 .num_dapm_routes = ARRAY_SIZE(intercon),
1292 static int twl6040_codec_probe(struct platform_device *pdev)
1294 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1295 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1298 static int twl6040_codec_remove(struct platform_device *pdev)
1300 snd_soc_unregister_codec(&pdev->dev);
1304 static struct platform_driver twl6040_codec_driver = {
1306 .name = "twl6040-codec",
1307 .owner = THIS_MODULE,
1309 .probe = twl6040_codec_probe,
1310 .remove = twl6040_codec_remove,
1313 module_platform_driver(twl6040_codec_driver);
1315 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1316 MODULE_AUTHOR("Misael Lopez Cruz");
1317 MODULE_LICENSE("GPL");