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 #define TWL6040_CACHEREGNUM (TWL6040_REG_STATUS + 1)
59 struct twl6040_jack_data
{
60 struct snd_soc_jack
*jack
;
61 struct delayed_work work
;
65 /* codec private data */
72 int hs_power_mode_locked
;
77 struct twl6040_jack_data hs_jack
;
78 struct snd_soc_codec
*codec
;
83 * twl6040 register cache & default register settings
85 static const u8 twl6040_reg
[TWL6040_CACHEREGNUM
] = {
86 0x00, /* not used 0x00 */
87 0x4B, /* REG_ASICID 0x01 (ro) */
88 0x00, /* REG_ASICREV 0x02 (ro) */
89 0x00, /* REG_INTID 0x03 */
90 0x00, /* REG_INTMR 0x04 */
91 0x00, /* REG_NCPCTRL 0x05 */
92 0x00, /* REG_LDOCTL 0x06 */
93 0x60, /* REG_HPPLLCTL 0x07 */
94 0x00, /* REG_LPPLLCTL 0x08 */
95 0x4A, /* REG_LPPLLDIV 0x09 */
96 0x00, /* REG_AMICBCTL 0x0A */
97 0x00, /* REG_DMICBCTL 0x0B */
98 0x00, /* REG_MICLCTL 0x0C */
99 0x00, /* REG_MICRCTL 0x0D */
100 0x00, /* REG_MICGAIN 0x0E */
101 0x1B, /* REG_LINEGAIN 0x0F */
102 0x00, /* REG_HSLCTL 0x10 */
103 0x00, /* REG_HSRCTL 0x11 */
104 0x00, /* REG_HSGAIN 0x12 */
105 0x00, /* REG_EARCTL 0x13 */
106 0x00, /* REG_HFLCTL 0x14 */
107 0x00, /* REG_HFLGAIN 0x15 */
108 0x00, /* REG_HFRCTL 0x16 */
109 0x00, /* REG_HFRGAIN 0x17 */
110 0x00, /* REG_VIBCTLL 0x18 */
111 0x00, /* REG_VIBDATL 0x19 */
112 0x00, /* REG_VIBCTLR 0x1A */
113 0x00, /* REG_VIBDATR 0x1B */
114 0x00, /* REG_HKCTL1 0x1C */
115 0x00, /* REG_HKCTL2 0x1D */
116 0x00, /* REG_GPOCTL 0x1E */
117 0x00, /* REG_ALB 0x1F */
118 0x00, /* REG_DLB 0x20 */
119 0x00, /* not used 0x21 */
120 0x00, /* not used 0x22 */
121 0x00, /* not used 0x23 */
122 0x00, /* not used 0x24 */
123 0x00, /* not used 0x25 */
124 0x00, /* not used 0x26 */
125 0x00, /* not used 0x27 */
126 0x00, /* REG_TRIM1 0x28 */
127 0x00, /* REG_TRIM2 0x29 */
128 0x00, /* REG_TRIM3 0x2A */
129 0x00, /* REG_HSOTRIM 0x2B */
130 0x00, /* REG_HFOTRIM 0x2C */
131 0x09, /* REG_ACCCTL 0x2D */
132 0x00, /* REG_STATUS 0x2E (ro) */
135 /* List of registers to be restored after power up */
136 static const int twl6040_restore_list
[] = {
140 TWL6040_REG_LINEGAIN
,
151 /* set of rates for each pll: low-power and high-performance */
152 static unsigned int lp_rates
[] = {
164 static unsigned int hp_rates
[] = {
172 static struct snd_pcm_hw_constraint_list sysclk_constraints
[] = {
173 { .count
= ARRAY_SIZE(lp_rates
), .list
= lp_rates
, },
174 { .count
= ARRAY_SIZE(hp_rates
), .list
= hp_rates
, },
178 * read twl6040 register cache
180 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec
*codec
,
183 u8
*cache
= codec
->reg_cache
;
185 if (reg
>= TWL6040_CACHEREGNUM
)
192 * write twl6040 register cache
194 static inline void twl6040_write_reg_cache(struct snd_soc_codec
*codec
,
197 u8
*cache
= codec
->reg_cache
;
199 if (reg
>= TWL6040_CACHEREGNUM
)
205 * read from twl6040 hardware register
207 static int twl6040_read_reg_volatile(struct snd_soc_codec
*codec
,
210 struct twl6040
*twl6040
= codec
->control_data
;
213 if (reg
>= TWL6040_CACHEREGNUM
)
216 value
= twl6040_reg_read(twl6040
, reg
);
217 twl6040_write_reg_cache(codec
, reg
, value
);
222 static bool twl6040_is_path_unmuted(struct snd_soc_codec
*codec
,
225 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
228 case TWL6040_REG_HSLCTL
:
229 case TWL6040_REG_HSRCTL
:
230 case TWL6040_REG_EARCTL
:
232 return priv
->dl1_unmuted
;
233 case TWL6040_REG_HFLCTL
:
234 case TWL6040_REG_HFRCTL
:
235 return priv
->dl2_unmuted
;
242 * write to the twl6040 register space
244 static int twl6040_write(struct snd_soc_codec
*codec
,
245 unsigned int reg
, unsigned int value
)
247 struct twl6040
*twl6040
= codec
->control_data
;
249 if (reg
>= TWL6040_CACHEREGNUM
)
252 twl6040_write_reg_cache(codec
, reg
, value
);
253 if (twl6040_is_path_unmuted(codec
, reg
))
254 return twl6040_reg_write(twl6040
, reg
, value
);
259 static void twl6040_init_chip(struct snd_soc_codec
*codec
)
261 struct twl6040
*twl6040
= codec
->control_data
;
264 /* Update reg_cache: ASICREV, and TRIM values */
265 val
= twl6040_get_revid(twl6040
);
266 twl6040_write_reg_cache(codec
, TWL6040_REG_ASICREV
, val
);
268 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM1
);
269 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM2
);
270 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM3
);
271 twl6040_read_reg_volatile(codec
, TWL6040_REG_HSOTRIM
);
272 twl6040_read_reg_volatile(codec
, TWL6040_REG_HFOTRIM
);
274 /* Change chip defaults */
275 /* No imput selected for microphone amplifiers */
276 twl6040_write_reg_cache(codec
, TWL6040_REG_MICLCTL
, 0x18);
277 twl6040_write_reg_cache(codec
, TWL6040_REG_MICRCTL
, 0x18);
280 * We need to lower the default gain values, so the ramp code
281 * can work correctly for the first playback.
282 * This reduces the pop noise heard at the first playback.
284 twl6040_write_reg_cache(codec
, TWL6040_REG_HSGAIN
, 0xff);
285 twl6040_write_reg_cache(codec
, TWL6040_REG_EARCTL
, 0x1e);
286 twl6040_write_reg_cache(codec
, TWL6040_REG_HFLGAIN
, 0x1d);
287 twl6040_write_reg_cache(codec
, TWL6040_REG_HFRGAIN
, 0x1d);
288 twl6040_write_reg_cache(codec
, TWL6040_REG_LINEGAIN
, 0);
291 static void twl6040_restore_regs(struct snd_soc_codec
*codec
)
293 u8
*cache
= codec
->reg_cache
;
296 for (i
= 0; i
< ARRAY_SIZE(twl6040_restore_list
); i
++) {
297 reg
= twl6040_restore_list
[i
];
298 twl6040_write(codec
, reg
, cache
[reg
]);
302 /* set headset dac and driver power mode */
303 static int headset_power_mode(struct snd_soc_codec
*codec
, int high_perf
)
306 int mask
= TWL6040_HSDRVMODE
| TWL6040_HSDACMODE
;
308 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
309 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
319 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
320 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
325 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget
*w
,
326 struct snd_kcontrol
*kcontrol
, int event
)
328 struct snd_soc_codec
*codec
= w
->codec
;
332 * Workaround for Headset DC offset caused pop noise:
333 * Both HS DAC need to be turned on (before the HS driver) and off at
336 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
337 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
338 if (SND_SOC_DAPM_EVENT_ON(event
)) {
339 hslctl
|= TWL6040_HSDACENA
;
340 hsrctl
|= TWL6040_HSDACENA
;
342 hslctl
&= ~TWL6040_HSDACENA
;
343 hsrctl
&= ~TWL6040_HSDACENA
;
345 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
346 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
352 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget
*w
,
353 struct snd_kcontrol
*kcontrol
, int event
)
355 struct snd_soc_codec
*codec
= w
->codec
;
356 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
359 if (SND_SOC_DAPM_EVENT_ON(event
)) {
360 /* Earphone doesn't support low power mode */
361 priv
->hs_power_mode_locked
= 1;
362 ret
= headset_power_mode(codec
, 1);
364 priv
->hs_power_mode_locked
= 0;
365 ret
= headset_power_mode(codec
, priv
->hs_power_mode
);
373 static void twl6040_hs_jack_report(struct snd_soc_codec
*codec
,
374 struct snd_soc_jack
*jack
, int report
)
376 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
379 mutex_lock(&priv
->mutex
);
382 status
= twl6040_read_reg_volatile(codec
, TWL6040_REG_STATUS
);
383 if (status
& TWL6040_PLUGCOMP
)
384 snd_soc_jack_report(jack
, report
, report
);
386 snd_soc_jack_report(jack
, 0, report
);
388 mutex_unlock(&priv
->mutex
);
391 void twl6040_hs_jack_detect(struct snd_soc_codec
*codec
,
392 struct snd_soc_jack
*jack
, int report
)
394 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
395 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
397 hs_jack
->jack
= jack
;
398 hs_jack
->report
= report
;
400 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
402 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect
);
404 static void twl6040_accessory_work(struct work_struct
*work
)
406 struct twl6040_data
*priv
= container_of(work
,
407 struct twl6040_data
, hs_jack
.work
.work
);
408 struct snd_soc_codec
*codec
= priv
->codec
;
409 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
411 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
414 /* audio interrupt handler */
415 static irqreturn_t
twl6040_audio_handler(int irq
, void *data
)
417 struct snd_soc_codec
*codec
= data
;
418 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
420 queue_delayed_work(system_power_efficient_wq
,
421 &priv
->hs_jack
.work
, msecs_to_jiffies(200));
426 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol
*kcontrol
,
427 struct snd_ctl_elem_value
*ucontrol
)
429 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
430 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
433 /* Do not allow changes while Input/FF efect is running */
434 val
= twl6040_read_reg_volatile(codec
, e
->reg
);
435 if (val
& TWL6040_VIBENA
&& !(val
& TWL6040_VIBSEL
))
438 return snd_soc_dapm_put_enum_double(kcontrol
, ucontrol
);
442 * MICATT volume control:
443 * from -6 to 0 dB in 6 dB steps
445 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv
, -600, 600, 0);
448 * MICGAIN volume control:
449 * from 6 to 30 dB in 6 dB steps
451 static DECLARE_TLV_DB_SCALE(mic_amp_tlv
, 600, 600, 0);
454 * AFMGAIN volume control:
455 * from -18 to 24 dB in 6 dB steps
457 static DECLARE_TLV_DB_SCALE(afm_amp_tlv
, -1800, 600, 0);
460 * HSGAIN volume control:
461 * from -30 to 0 dB in 2 dB steps
463 static DECLARE_TLV_DB_SCALE(hs_tlv
, -3000, 200, 0);
466 * HFGAIN volume control:
467 * from -52 to 6 dB in 2 dB steps
469 static DECLARE_TLV_DB_SCALE(hf_tlv
, -5200, 200, 0);
472 * EPGAIN volume control:
473 * from -24 to 6 dB in 2 dB steps
475 static DECLARE_TLV_DB_SCALE(ep_tlv
, -2400, 200, 0);
477 /* Left analog microphone selection */
478 static const char *twl6040_amicl_texts
[] =
479 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
481 /* Right analog microphone selection */
482 static const char *twl6040_amicr_texts
[] =
483 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
485 static const struct soc_enum twl6040_enum
[] = {
486 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL
, 3, 4, twl6040_amicl_texts
),
487 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL
, 3, 4, twl6040_amicr_texts
),
490 static const char *twl6040_hs_texts
[] = {
491 "Off", "HS DAC", "Line-In amp"
494 static const struct soc_enum twl6040_hs_enum
[] = {
495 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
497 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
501 static const char *twl6040_hf_texts
[] = {
502 "Off", "HF DAC", "Line-In amp"
505 static const struct soc_enum twl6040_hf_enum
[] = {
506 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
508 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
512 static const char *twl6040_vibrapath_texts
[] = {
513 "Input FF", "Audio PDM"
516 static const struct soc_enum twl6040_vibra_enum
[] = {
517 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL
, 1,
518 ARRAY_SIZE(twl6040_vibrapath_texts
),
519 twl6040_vibrapath_texts
),
520 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR
, 1,
521 ARRAY_SIZE(twl6040_vibrapath_texts
),
522 twl6040_vibrapath_texts
),
525 static const struct snd_kcontrol_new amicl_control
=
526 SOC_DAPM_ENUM("Route", twl6040_enum
[0]);
528 static const struct snd_kcontrol_new amicr_control
=
529 SOC_DAPM_ENUM("Route", twl6040_enum
[1]);
531 /* Headset DAC playback switches */
532 static const struct snd_kcontrol_new hsl_mux_controls
=
533 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[0]);
535 static const struct snd_kcontrol_new hsr_mux_controls
=
536 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[1]);
538 /* Handsfree DAC playback switches */
539 static const struct snd_kcontrol_new hfl_mux_controls
=
540 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[0]);
542 static const struct snd_kcontrol_new hfr_mux_controls
=
543 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[1]);
545 static const struct snd_kcontrol_new ep_path_enable_control
=
546 SOC_DAPM_SINGLE_VIRT("Switch", 1);
548 static const struct snd_kcontrol_new auxl_switch_control
=
549 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL
, 6, 1, 0);
551 static const struct snd_kcontrol_new auxr_switch_control
=
552 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL
, 6, 1, 0);
554 /* Vibra playback switches */
555 static const struct snd_kcontrol_new vibral_mux_controls
=
556 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum
[0],
557 snd_soc_dapm_get_enum_double
,
558 twl6040_soc_dapm_put_vibra_enum
);
560 static const struct snd_kcontrol_new vibrar_mux_controls
=
561 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum
[1],
562 snd_soc_dapm_get_enum_double
,
563 twl6040_soc_dapm_put_vibra_enum
);
565 /* Headset power mode */
566 static const char *twl6040_power_mode_texts
[] = {
567 "Low-Power", "High-Performance",
570 static const struct soc_enum twl6040_power_mode_enum
=
571 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts
),
572 twl6040_power_mode_texts
);
574 static int twl6040_headset_power_get_enum(struct snd_kcontrol
*kcontrol
,
575 struct snd_ctl_elem_value
*ucontrol
)
577 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
578 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
580 ucontrol
->value
.enumerated
.item
[0] = priv
->hs_power_mode
;
585 static int twl6040_headset_power_put_enum(struct snd_kcontrol
*kcontrol
,
586 struct snd_ctl_elem_value
*ucontrol
)
588 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
589 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
590 int high_perf
= ucontrol
->value
.enumerated
.item
[0];
593 if (!priv
->hs_power_mode_locked
)
594 ret
= headset_power_mode(codec
, high_perf
);
597 priv
->hs_power_mode
= high_perf
;
602 static int twl6040_pll_get_enum(struct snd_kcontrol
*kcontrol
,
603 struct snd_ctl_elem_value
*ucontrol
)
605 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
606 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
608 ucontrol
->value
.enumerated
.item
[0] = priv
->pll_power_mode
;
613 static int twl6040_pll_put_enum(struct snd_kcontrol
*kcontrol
,
614 struct snd_ctl_elem_value
*ucontrol
)
616 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
617 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
619 priv
->pll_power_mode
= ucontrol
->value
.enumerated
.item
[0];
624 int twl6040_get_dl1_gain(struct snd_soc_codec
*codec
)
626 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
628 if (snd_soc_dapm_get_pin_status(dapm
, "EP"))
629 return -1; /* -1dB */
631 if (snd_soc_dapm_get_pin_status(dapm
, "HSOR") ||
632 snd_soc_dapm_get_pin_status(dapm
, "HSOL")) {
634 u8 val
= snd_soc_read(codec
, TWL6040_REG_HSLCTL
);
635 if (val
& TWL6040_HSDACMODE
)
636 /* HSDACL in LP mode */
637 return -8; /* -8dB */
639 /* HSDACL in HP mode */
640 return -1; /* -1dB */
644 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain
);
646 int twl6040_get_clk_id(struct snd_soc_codec
*codec
)
648 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
650 return priv
->pll_power_mode
;
652 EXPORT_SYMBOL_GPL(twl6040_get_clk_id
);
654 int twl6040_get_trim_value(struct snd_soc_codec
*codec
, enum twl6040_trim trim
)
656 if (unlikely(trim
>= TWL6040_TRIM_INVAL
))
659 return twl6040_read_reg_cache(codec
, TWL6040_REG_TRIM1
+ trim
);
661 EXPORT_SYMBOL_GPL(twl6040_get_trim_value
);
663 int twl6040_get_hs_step_size(struct snd_soc_codec
*codec
)
665 struct twl6040
*twl6040
= codec
->control_data
;
667 if (twl6040_get_revid(twl6040
) < TWL6040_REV_ES1_3
)
668 /* For ES under ES_1.3 HS step is 2 mV */
671 /* For ES_1.3 HS step is 1 mV */
674 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size
);
676 static const struct snd_kcontrol_new twl6040_snd_controls
[] = {
678 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
679 TWL6040_REG_MICGAIN
, 6, 7, 1, 1, mic_preamp_tlv
),
680 SOC_DOUBLE_TLV("Capture Volume",
681 TWL6040_REG_MICGAIN
, 0, 3, 4, 0, mic_amp_tlv
),
684 SOC_DOUBLE_TLV("Aux FM Volume",
685 TWL6040_REG_LINEGAIN
, 0, 3, 7, 0, afm_amp_tlv
),
688 SOC_DOUBLE_TLV("Headset Playback Volume",
689 TWL6040_REG_HSGAIN
, 0, 4, 0xF, 1, hs_tlv
),
690 SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
691 TWL6040_REG_HFLGAIN
, TWL6040_REG_HFRGAIN
, 0, 0x1D, 1, hf_tlv
),
692 SOC_SINGLE_TLV("Earphone Playback Volume",
693 TWL6040_REG_EARCTL
, 1, 0xF, 1, ep_tlv
),
695 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum
,
696 twl6040_headset_power_get_enum
,
697 twl6040_headset_power_put_enum
),
699 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum
,
700 twl6040_pll_get_enum
, twl6040_pll_put_enum
),
703 static const struct snd_soc_dapm_widget twl6040_dapm_widgets
[] = {
705 SND_SOC_DAPM_INPUT("MAINMIC"),
706 SND_SOC_DAPM_INPUT("HSMIC"),
707 SND_SOC_DAPM_INPUT("SUBMIC"),
708 SND_SOC_DAPM_INPUT("AFML"),
709 SND_SOC_DAPM_INPUT("AFMR"),
712 SND_SOC_DAPM_OUTPUT("HSOL"),
713 SND_SOC_DAPM_OUTPUT("HSOR"),
714 SND_SOC_DAPM_OUTPUT("HFL"),
715 SND_SOC_DAPM_OUTPUT("HFR"),
716 SND_SOC_DAPM_OUTPUT("EP"),
717 SND_SOC_DAPM_OUTPUT("AUXL"),
718 SND_SOC_DAPM_OUTPUT("AUXR"),
719 SND_SOC_DAPM_OUTPUT("VIBRAL"),
720 SND_SOC_DAPM_OUTPUT("VIBRAR"),
722 /* Analog input muxes for the capture amplifiers */
723 SND_SOC_DAPM_MUX("Analog Left Capture Route",
724 SND_SOC_NOPM
, 0, 0, &amicl_control
),
725 SND_SOC_DAPM_MUX("Analog Right Capture Route",
726 SND_SOC_NOPM
, 0, 0, &amicr_control
),
728 /* Analog capture PGAs */
729 SND_SOC_DAPM_PGA("MicAmpL",
730 TWL6040_REG_MICLCTL
, 0, 0, NULL
, 0),
731 SND_SOC_DAPM_PGA("MicAmpR",
732 TWL6040_REG_MICRCTL
, 0, 0, NULL
, 0),
734 /* Auxiliary FM PGAs */
735 SND_SOC_DAPM_PGA("AFMAmpL",
736 TWL6040_REG_MICLCTL
, 1, 0, NULL
, 0),
737 SND_SOC_DAPM_PGA("AFMAmpR",
738 TWL6040_REG_MICRCTL
, 1, 0, NULL
, 0),
741 SND_SOC_DAPM_ADC("ADC Left", NULL
, TWL6040_REG_MICLCTL
, 2, 0),
742 SND_SOC_DAPM_ADC("ADC Right", NULL
, TWL6040_REG_MICRCTL
, 2, 0),
744 /* Microphone bias */
745 SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
746 TWL6040_REG_AMICBCTL
, 0, 0, NULL
, 0),
747 SND_SOC_DAPM_SUPPLY("Main Mic Bias",
748 TWL6040_REG_AMICBCTL
, 4, 0, NULL
, 0),
749 SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
750 TWL6040_REG_DMICBCTL
, 0, 0, NULL
, 0),
751 SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
752 TWL6040_REG_DMICBCTL
, 4, 0, NULL
, 0),
755 SND_SOC_DAPM_DAC("HSDAC Left", NULL
, SND_SOC_NOPM
, 0, 0),
756 SND_SOC_DAPM_DAC("HSDAC Right", NULL
, SND_SOC_NOPM
, 0, 0),
757 SND_SOC_DAPM_DAC("HFDAC Left", NULL
, TWL6040_REG_HFLCTL
, 0, 0),
758 SND_SOC_DAPM_DAC("HFDAC Right", NULL
, TWL6040_REG_HFRCTL
, 0, 0),
759 /* Virtual DAC for vibra path (DL4 channel) */
760 SND_SOC_DAPM_DAC("VIBRA DAC", NULL
, SND_SOC_NOPM
, 0, 0),
762 SND_SOC_DAPM_MUX("Handsfree Left Playback",
763 SND_SOC_NOPM
, 0, 0, &hfl_mux_controls
),
764 SND_SOC_DAPM_MUX("Handsfree Right Playback",
765 SND_SOC_NOPM
, 0, 0, &hfr_mux_controls
),
766 /* Analog playback Muxes */
767 SND_SOC_DAPM_MUX("Headset Left Playback",
768 SND_SOC_NOPM
, 0, 0, &hsl_mux_controls
),
769 SND_SOC_DAPM_MUX("Headset Right Playback",
770 SND_SOC_NOPM
, 0, 0, &hsr_mux_controls
),
772 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM
, 0, 0,
773 &vibral_mux_controls
),
774 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM
, 0, 0,
775 &vibrar_mux_controls
),
777 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM
, 0, 0,
778 &ep_path_enable_control
),
779 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM
, 0, 0,
780 &auxl_switch_control
),
781 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM
, 0, 0,
782 &auxr_switch_control
),
784 /* Analog playback drivers */
785 SND_SOC_DAPM_OUT_DRV("HF Left Driver",
786 TWL6040_REG_HFLCTL
, 4, 0, NULL
, 0),
787 SND_SOC_DAPM_OUT_DRV("HF Right Driver",
788 TWL6040_REG_HFRCTL
, 4, 0, NULL
, 0),
789 SND_SOC_DAPM_OUT_DRV("HS Left Driver",
790 TWL6040_REG_HSLCTL
, 2, 0, NULL
, 0),
791 SND_SOC_DAPM_OUT_DRV("HS Right Driver",
792 TWL6040_REG_HSRCTL
, 2, 0, NULL
, 0),
793 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
794 TWL6040_REG_EARCTL
, 0, 0, NULL
, 0,
795 twl6040_ep_drv_event
,
796 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
797 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
798 TWL6040_REG_VIBCTLL
, 0, 0, NULL
, 0),
799 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
800 TWL6040_REG_VIBCTLR
, 0, 0, NULL
, 0),
802 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL
, 2, 0,
804 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR
, 2, 0,
806 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM
, 0, 0,
807 twl6040_hs_dac_event
,
808 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
810 /* Analog playback PGAs */
811 SND_SOC_DAPM_PGA("HF Left PGA",
812 TWL6040_REG_HFLCTL
, 1, 0, NULL
, 0),
813 SND_SOC_DAPM_PGA("HF Right PGA",
814 TWL6040_REG_HFRCTL
, 1, 0, NULL
, 0),
818 static const struct snd_soc_dapm_route intercon
[] = {
819 /* Stream -> DAC mapping */
820 {"HSDAC Left", NULL
, "Legacy Playback"},
821 {"HSDAC Left", NULL
, "Headset Playback"},
822 {"HSDAC Right", NULL
, "Legacy Playback"},
823 {"HSDAC Right", NULL
, "Headset Playback"},
825 {"HFDAC Left", NULL
, "Legacy Playback"},
826 {"HFDAC Left", NULL
, "Handsfree Playback"},
827 {"HFDAC Right", NULL
, "Legacy Playback"},
828 {"HFDAC Right", NULL
, "Handsfree Playback"},
830 {"VIBRA DAC", NULL
, "Legacy Playback"},
831 {"VIBRA DAC", NULL
, "Vibra Playback"},
833 /* ADC -> Stream mapping */
834 {"Legacy Capture" , NULL
, "ADC Left"},
835 {"Capture", NULL
, "ADC Left"},
836 {"Legacy Capture", NULL
, "ADC Right"},
837 {"Capture" , NULL
, "ADC Right"},
840 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
841 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
842 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
844 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
845 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
846 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
848 {"MicAmpL", NULL
, "Analog Left Capture Route"},
849 {"MicAmpR", NULL
, "Analog Right Capture Route"},
851 {"ADC Left", NULL
, "MicAmpL"},
852 {"ADC Right", NULL
, "MicAmpR"},
855 {"AFMAmpL", NULL
, "AFML"},
856 {"AFMAmpR", NULL
, "AFMR"},
858 {"HSDAC Left", NULL
, "HSDAC Power"},
859 {"HSDAC Right", NULL
, "HSDAC Power"},
861 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
862 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
864 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
865 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
867 {"HS Left Driver", NULL
, "Headset Left Playback"},
868 {"HS Right Driver", NULL
, "Headset Right Playback"},
870 {"HSOL", NULL
, "HS Left Driver"},
871 {"HSOR", NULL
, "HS Right Driver"},
873 /* Earphone playback path */
874 {"Earphone Playback", "Switch", "HSDAC Left"},
875 {"Earphone Driver", NULL
, "Earphone Playback"},
876 {"EP", NULL
, "Earphone Driver"},
878 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
879 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
881 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
882 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
884 {"HF Left PGA", NULL
, "Handsfree Left Playback"},
885 {"HF Right PGA", NULL
, "Handsfree Right Playback"},
887 {"HF Left Driver", NULL
, "HF Left PGA"},
888 {"HF Right Driver", NULL
, "HF Right PGA"},
890 {"HFL", NULL
, "HF Left Driver"},
891 {"HFR", NULL
, "HF Right Driver"},
893 {"AUXL Playback", "Switch", "HF Left PGA"},
894 {"AUXR Playback", "Switch", "HF Right PGA"},
896 {"AUXL", NULL
, "AUXL Playback"},
897 {"AUXR", NULL
, "AUXR Playback"},
900 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
901 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
903 {"Vibra Left Driver", NULL
, "Vibra Left Playback"},
904 {"Vibra Right Driver", NULL
, "Vibra Right Playback"},
905 {"Vibra Left Driver", NULL
, "Vibra Left Control"},
906 {"Vibra Right Driver", NULL
, "Vibra Right Control"},
908 {"VIBRAL", NULL
, "Vibra Left Driver"},
909 {"VIBRAR", NULL
, "Vibra Right Driver"},
912 static int twl6040_set_bias_level(struct snd_soc_codec
*codec
,
913 enum snd_soc_bias_level level
)
915 struct twl6040
*twl6040
= codec
->control_data
;
916 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
920 case SND_SOC_BIAS_ON
:
922 case SND_SOC_BIAS_PREPARE
:
924 case SND_SOC_BIAS_STANDBY
:
925 if (priv
->codec_powered
)
928 ret
= twl6040_power(twl6040
, 1);
932 priv
->codec_powered
= 1;
934 twl6040_restore_regs(codec
);
936 /* Set external boost GPO */
937 twl6040_write(codec
, TWL6040_REG_GPOCTL
, 0x02);
939 case SND_SOC_BIAS_OFF
:
940 if (!priv
->codec_powered
)
943 twl6040_power(twl6040
, 0);
944 priv
->codec_powered
= 0;
948 codec
->dapm
.bias_level
= level
;
953 static int twl6040_startup(struct snd_pcm_substream
*substream
,
954 struct snd_soc_dai
*dai
)
956 struct snd_soc_codec
*codec
= dai
->codec
;
957 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
959 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
960 SNDRV_PCM_HW_PARAM_RATE
,
961 &sysclk_constraints
[priv
->pll_power_mode
]);
966 static int twl6040_hw_params(struct snd_pcm_substream
*substream
,
967 struct snd_pcm_hw_params
*params
,
968 struct snd_soc_dai
*dai
)
970 struct snd_soc_codec
*codec
= dai
->codec
;
971 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
974 rate
= params_rate(params
);
980 /* These rates are not supported when HPPLL is in use */
981 if (unlikely(priv
->pll
== TWL6040_SYSCLK_SEL_HPPLL
)) {
982 dev_err(codec
->dev
, "HPPLL does not support rate %d\n",
986 priv
->sysclk
= 17640000;
993 priv
->sysclk
= 19200000;
996 dev_err(codec
->dev
, "unsupported rate %d\n", rate
);
1003 static int twl6040_prepare(struct snd_pcm_substream
*substream
,
1004 struct snd_soc_dai
*dai
)
1006 struct snd_soc_codec
*codec
= dai
->codec
;
1007 struct twl6040
*twl6040
= codec
->control_data
;
1008 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1011 if (!priv
->sysclk
) {
1013 "no mclk configured, call set_sysclk() on init\n");
1017 ret
= twl6040_set_pll(twl6040
, priv
->pll
, priv
->clk_in
, priv
->sysclk
);
1019 dev_err(codec
->dev
, "Can not set PLL (%d)\n", ret
);
1026 static int twl6040_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1027 int clk_id
, unsigned int freq
, int dir
)
1029 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1030 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1033 case TWL6040_SYSCLK_SEL_LPPLL
:
1034 case TWL6040_SYSCLK_SEL_HPPLL
:
1036 priv
->clk_in
= freq
;
1039 dev_err(codec
->dev
, "unknown clk_id %d\n", clk_id
);
1046 static void twl6040_mute_path(struct snd_soc_codec
*codec
, enum twl6040_dai_id id
,
1049 struct twl6040
*twl6040
= codec
->control_data
;
1050 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1051 int hslctl
, hsrctl
, earctl
;
1055 case TWL6040_DAI_DL1
:
1056 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
1057 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
1058 earctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_EARCTL
);
1061 /* Power down drivers and DACs */
1063 hslctl
&= ~(TWL6040_HSDRVENA
| TWL6040_HSDACENA
);
1064 hsrctl
&= ~(TWL6040_HSDRVENA
| TWL6040_HSDACENA
);
1068 twl6040_reg_write(twl6040
, TWL6040_REG_EARCTL
, earctl
);
1069 twl6040_reg_write(twl6040
, TWL6040_REG_HSLCTL
, hslctl
);
1070 twl6040_reg_write(twl6040
, TWL6040_REG_HSRCTL
, hsrctl
);
1071 priv
->dl1_unmuted
= !mute
;
1073 case TWL6040_DAI_DL2
:
1074 hflctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HFLCTL
);
1075 hfrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HFRCTL
);
1078 /* Power down drivers and DACs */
1079 hflctl
&= ~(TWL6040_HFDACENA
| TWL6040_HFPGAENA
|
1081 hfrctl
&= ~(TWL6040_HFDACENA
| TWL6040_HFPGAENA
|
1085 twl6040_reg_write(twl6040
, TWL6040_REG_HFLCTL
, hflctl
);
1086 twl6040_reg_write(twl6040
, TWL6040_REG_HFRCTL
, hfrctl
);
1087 priv
->dl2_unmuted
= !mute
;
1094 static int twl6040_digital_mute(struct snd_soc_dai
*dai
, int mute
)
1097 case TWL6040_DAI_LEGACY
:
1098 twl6040_mute_path(dai
->codec
, TWL6040_DAI_DL1
, mute
);
1099 twl6040_mute_path(dai
->codec
, TWL6040_DAI_DL2
, mute
);
1101 case TWL6040_DAI_DL1
:
1102 case TWL6040_DAI_DL2
:
1103 twl6040_mute_path(dai
->codec
, dai
->id
, mute
);
1112 static const struct snd_soc_dai_ops twl6040_dai_ops
= {
1113 .startup
= twl6040_startup
,
1114 .hw_params
= twl6040_hw_params
,
1115 .prepare
= twl6040_prepare
,
1116 .set_sysclk
= twl6040_set_dai_sysclk
,
1117 .digital_mute
= twl6040_digital_mute
,
1120 static struct snd_soc_dai_driver twl6040_dai
[] = {
1122 .name
= "twl6040-legacy",
1123 .id
= TWL6040_DAI_LEGACY
,
1125 .stream_name
= "Legacy Playback",
1128 .rates
= TWL6040_RATES
,
1129 .formats
= TWL6040_FORMATS
,
1132 .stream_name
= "Legacy Capture",
1135 .rates
= TWL6040_RATES
,
1136 .formats
= TWL6040_FORMATS
,
1138 .ops
= &twl6040_dai_ops
,
1141 .name
= "twl6040-ul",
1142 .id
= TWL6040_DAI_UL
,
1144 .stream_name
= "Capture",
1147 .rates
= TWL6040_RATES
,
1148 .formats
= TWL6040_FORMATS
,
1150 .ops
= &twl6040_dai_ops
,
1153 .name
= "twl6040-dl1",
1154 .id
= TWL6040_DAI_DL1
,
1156 .stream_name
= "Headset Playback",
1159 .rates
= TWL6040_RATES
,
1160 .formats
= TWL6040_FORMATS
,
1162 .ops
= &twl6040_dai_ops
,
1165 .name
= "twl6040-dl2",
1166 .id
= TWL6040_DAI_DL2
,
1168 .stream_name
= "Handsfree Playback",
1171 .rates
= TWL6040_RATES
,
1172 .formats
= TWL6040_FORMATS
,
1174 .ops
= &twl6040_dai_ops
,
1177 .name
= "twl6040-vib",
1178 .id
= TWL6040_DAI_VIB
,
1180 .stream_name
= "Vibra Playback",
1183 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
1184 .formats
= TWL6040_FORMATS
,
1186 .ops
= &twl6040_dai_ops
,
1191 static int twl6040_suspend(struct snd_soc_codec
*codec
)
1193 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1198 static int twl6040_resume(struct snd_soc_codec
*codec
)
1200 twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1205 #define twl6040_suspend NULL
1206 #define twl6040_resume NULL
1209 static int twl6040_probe(struct snd_soc_codec
*codec
)
1211 struct twl6040_data
*priv
;
1212 struct platform_device
*pdev
= container_of(codec
->dev
,
1213 struct platform_device
, dev
);
1216 priv
= devm_kzalloc(codec
->dev
, sizeof(*priv
), GFP_KERNEL
);
1220 snd_soc_codec_set_drvdata(codec
, priv
);
1222 priv
->codec
= codec
;
1223 codec
->control_data
= dev_get_drvdata(codec
->dev
->parent
);
1225 priv
->plug_irq
= platform_get_irq(pdev
, 0);
1226 if (priv
->plug_irq
< 0) {
1227 dev_err(codec
->dev
, "invalid irq\n");
1231 INIT_DELAYED_WORK(&priv
->hs_jack
.work
, twl6040_accessory_work
);
1233 mutex_init(&priv
->mutex
);
1235 ret
= request_threaded_irq(priv
->plug_irq
, NULL
,
1236 twl6040_audio_handler
, IRQF_NO_SUSPEND
,
1237 "twl6040_irq_plug", codec
);
1239 dev_err(codec
->dev
, "PLUG IRQ request failed: %d\n", ret
);
1243 twl6040_init_chip(codec
);
1245 /* power on device */
1246 return twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1249 static int twl6040_remove(struct snd_soc_codec
*codec
)
1251 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1253 free_irq(priv
->plug_irq
, codec
);
1254 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1259 static struct snd_soc_codec_driver soc_codec_dev_twl6040
= {
1260 .probe
= twl6040_probe
,
1261 .remove
= twl6040_remove
,
1262 .suspend
= twl6040_suspend
,
1263 .resume
= twl6040_resume
,
1264 .read
= twl6040_read_reg_cache
,
1265 .write
= twl6040_write
,
1266 .set_bias_level
= twl6040_set_bias_level
,
1267 .reg_cache_size
= ARRAY_SIZE(twl6040_reg
),
1268 .reg_word_size
= sizeof(u8
),
1269 .reg_cache_default
= twl6040_reg
,
1270 .ignore_pmdown_time
= true,
1272 .controls
= twl6040_snd_controls
,
1273 .num_controls
= ARRAY_SIZE(twl6040_snd_controls
),
1274 .dapm_widgets
= twl6040_dapm_widgets
,
1275 .num_dapm_widgets
= ARRAY_SIZE(twl6040_dapm_widgets
),
1276 .dapm_routes
= intercon
,
1277 .num_dapm_routes
= ARRAY_SIZE(intercon
),
1280 static int twl6040_codec_probe(struct platform_device
*pdev
)
1282 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_twl6040
,
1283 twl6040_dai
, ARRAY_SIZE(twl6040_dai
));
1286 static int twl6040_codec_remove(struct platform_device
*pdev
)
1288 snd_soc_unregister_codec(&pdev
->dev
);
1292 static struct platform_driver twl6040_codec_driver
= {
1294 .name
= "twl6040-codec",
1295 .owner
= THIS_MODULE
,
1297 .probe
= twl6040_codec_probe
,
1298 .remove
= twl6040_codec_remove
,
1301 module_platform_driver(twl6040_codec_driver
);
1303 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1304 MODULE_AUTHOR("Misael Lopez Cruz");
1305 MODULE_LICENSE("GPL");