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>
41 #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
42 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
44 #define TWL6040_OUTHS_0dB 0x00
45 #define TWL6040_OUTHS_M30dB 0x0F
46 #define TWL6040_OUTHF_0dB 0x03
47 #define TWL6040_OUTHF_M52dB 0x1D
49 /* Shadow register used by the driver */
50 #define TWL6040_REG_SW_SHADOW 0x2F
51 #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1)
53 /* TWL6040_REG_SW_SHADOW (0x2F) fields */
54 #define TWL6040_EAR_PATH_ENABLE 0x01
56 struct twl6040_jack_data
{
57 struct snd_soc_jack
*jack
;
58 struct delayed_work work
;
62 /* codec private data */
69 int hs_power_mode_locked
;
76 struct twl6040_jack_data hs_jack
;
77 struct snd_soc_codec
*codec
;
78 struct workqueue_struct
*workqueue
;
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) */
134 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */
137 /* List of registers to be restored after power up */
138 static const int twl6040_restore_list
[] = {
142 TWL6040_REG_LINEGAIN
,
153 /* set of rates for each pll: low-power and high-performance */
154 static unsigned int lp_rates
[] = {
166 static unsigned int hp_rates
[] = {
174 static struct snd_pcm_hw_constraint_list sysclk_constraints
[] = {
175 { .count
= ARRAY_SIZE(lp_rates
), .list
= lp_rates
, },
176 { .count
= ARRAY_SIZE(hp_rates
), .list
= hp_rates
, },
180 * read twl6040 register cache
182 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec
*codec
,
185 u8
*cache
= codec
->reg_cache
;
187 if (reg
>= TWL6040_CACHEREGNUM
)
194 * write twl6040 register cache
196 static inline void twl6040_write_reg_cache(struct snd_soc_codec
*codec
,
199 u8
*cache
= codec
->reg_cache
;
201 if (reg
>= TWL6040_CACHEREGNUM
)
207 * read from twl6040 hardware register
209 static int twl6040_read_reg_volatile(struct snd_soc_codec
*codec
,
212 struct twl6040
*twl6040
= codec
->control_data
;
215 if (reg
>= TWL6040_CACHEREGNUM
)
218 if (likely(reg
< TWL6040_REG_SW_SHADOW
)) {
219 value
= twl6040_reg_read(twl6040
, reg
);
220 twl6040_write_reg_cache(codec
, reg
, value
);
222 value
= twl6040_read_reg_cache(codec
, reg
);
229 * write to the twl6040 register space
231 static int twl6040_write(struct snd_soc_codec
*codec
,
232 unsigned int reg
, unsigned int value
)
234 struct twl6040
*twl6040
= codec
->control_data
;
236 if (reg
>= TWL6040_CACHEREGNUM
)
239 twl6040_write_reg_cache(codec
, reg
, value
);
240 if (likely(reg
< TWL6040_REG_SW_SHADOW
))
241 return twl6040_reg_write(twl6040
, reg
, value
);
246 static void twl6040_init_chip(struct snd_soc_codec
*codec
)
248 struct twl6040
*twl6040
= codec
->control_data
;
251 /* Update reg_cache: ASICREV, and TRIM values */
252 val
= twl6040_get_revid(twl6040
);
253 twl6040_write_reg_cache(codec
, TWL6040_REG_ASICREV
, val
);
255 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM1
);
256 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM2
);
257 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM3
);
258 twl6040_read_reg_volatile(codec
, TWL6040_REG_HSOTRIM
);
259 twl6040_read_reg_volatile(codec
, TWL6040_REG_HFOTRIM
);
261 /* Change chip defaults */
262 /* No imput selected for microphone amplifiers */
263 twl6040_write_reg_cache(codec
, TWL6040_REG_MICLCTL
, 0x18);
264 twl6040_write_reg_cache(codec
, TWL6040_REG_MICRCTL
, 0x18);
267 * We need to lower the default gain values, so the ramp code
268 * can work correctly for the first playback.
269 * This reduces the pop noise heard at the first playback.
271 twl6040_write_reg_cache(codec
, TWL6040_REG_HSGAIN
, 0xff);
272 twl6040_write_reg_cache(codec
, TWL6040_REG_EARCTL
, 0x1e);
273 twl6040_write_reg_cache(codec
, TWL6040_REG_HFLGAIN
, 0x1d);
274 twl6040_write_reg_cache(codec
, TWL6040_REG_HFRGAIN
, 0x1d);
275 twl6040_write_reg_cache(codec
, TWL6040_REG_LINEGAIN
, 0);
278 static void twl6040_restore_regs(struct snd_soc_codec
*codec
)
280 u8
*cache
= codec
->reg_cache
;
283 for (i
= 0; i
< ARRAY_SIZE(twl6040_restore_list
); i
++) {
284 reg
= twl6040_restore_list
[i
];
285 twl6040_write(codec
, reg
, cache
[reg
]);
289 /* set headset dac and driver power mode */
290 static int headset_power_mode(struct snd_soc_codec
*codec
, int high_perf
)
293 int mask
= TWL6040_HSDRVMODE
| TWL6040_HSDACMODE
;
295 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
296 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
306 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
307 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
312 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget
*w
,
313 struct snd_kcontrol
*kcontrol
, int event
)
315 struct snd_soc_codec
*codec
= w
->codec
;
319 * Workaround for Headset DC offset caused pop noise:
320 * Both HS DAC need to be turned on (before the HS driver) and off at
323 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
324 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
325 if (SND_SOC_DAPM_EVENT_ON(event
)) {
326 hslctl
|= TWL6040_HSDACENA
;
327 hsrctl
|= TWL6040_HSDACENA
;
329 hslctl
&= ~TWL6040_HSDACENA
;
330 hsrctl
&= ~TWL6040_HSDACENA
;
332 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
333 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
339 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget
*w
,
340 struct snd_kcontrol
*kcontrol
, int event
)
342 struct snd_soc_codec
*codec
= w
->codec
;
343 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
346 if (SND_SOC_DAPM_EVENT_ON(event
)) {
347 /* Earphone doesn't support low power mode */
348 priv
->hs_power_mode_locked
= 1;
349 ret
= headset_power_mode(codec
, 1);
351 priv
->hs_power_mode_locked
= 0;
352 ret
= headset_power_mode(codec
, priv
->hs_power_mode
);
360 static void twl6040_hs_jack_report(struct snd_soc_codec
*codec
,
361 struct snd_soc_jack
*jack
, int report
)
363 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
366 mutex_lock(&priv
->mutex
);
369 status
= twl6040_read_reg_volatile(codec
, TWL6040_REG_STATUS
);
370 if (status
& TWL6040_PLUGCOMP
)
371 snd_soc_jack_report(jack
, report
, report
);
373 snd_soc_jack_report(jack
, 0, report
);
375 mutex_unlock(&priv
->mutex
);
378 void twl6040_hs_jack_detect(struct snd_soc_codec
*codec
,
379 struct snd_soc_jack
*jack
, int report
)
381 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
382 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
384 hs_jack
->jack
= jack
;
385 hs_jack
->report
= report
;
387 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
389 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect
);
391 static void twl6040_accessory_work(struct work_struct
*work
)
393 struct twl6040_data
*priv
= container_of(work
,
394 struct twl6040_data
, hs_jack
.work
.work
);
395 struct snd_soc_codec
*codec
= priv
->codec
;
396 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
398 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
401 /* audio interrupt handler */
402 static irqreturn_t
twl6040_audio_handler(int irq
, void *data
)
404 struct snd_soc_codec
*codec
= data
;
405 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
407 queue_delayed_work(priv
->workqueue
, &priv
->hs_jack
.work
,
408 msecs_to_jiffies(200));
413 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol
*kcontrol
,
414 struct snd_ctl_elem_value
*ucontrol
)
416 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
417 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
418 struct snd_soc_codec
*codec
= widget
->codec
;
419 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
422 /* Do not allow changes while Input/FF efect is running */
423 val
= twl6040_read_reg_volatile(codec
, e
->reg
);
424 if (val
& TWL6040_VIBENA
&& !(val
& TWL6040_VIBSEL
))
427 return snd_soc_dapm_put_enum_double(kcontrol
, ucontrol
);
431 * MICATT volume control:
432 * from -6 to 0 dB in 6 dB steps
434 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv
, -600, 600, 0);
437 * MICGAIN volume control:
438 * from 6 to 30 dB in 6 dB steps
440 static DECLARE_TLV_DB_SCALE(mic_amp_tlv
, 600, 600, 0);
443 * AFMGAIN volume control:
444 * from -18 to 24 dB in 6 dB steps
446 static DECLARE_TLV_DB_SCALE(afm_amp_tlv
, -1800, 600, 0);
449 * HSGAIN volume control:
450 * from -30 to 0 dB in 2 dB steps
452 static DECLARE_TLV_DB_SCALE(hs_tlv
, -3000, 200, 0);
455 * HFGAIN volume control:
456 * from -52 to 6 dB in 2 dB steps
458 static DECLARE_TLV_DB_SCALE(hf_tlv
, -5200, 200, 0);
461 * EPGAIN volume control:
462 * from -24 to 6 dB in 2 dB steps
464 static DECLARE_TLV_DB_SCALE(ep_tlv
, -2400, 200, 0);
466 /* Left analog microphone selection */
467 static const char *twl6040_amicl_texts
[] =
468 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
470 /* Right analog microphone selection */
471 static const char *twl6040_amicr_texts
[] =
472 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
474 static const struct soc_enum twl6040_enum
[] = {
475 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL
, 3, 4, twl6040_amicl_texts
),
476 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL
, 3, 4, twl6040_amicr_texts
),
479 static const char *twl6040_hs_texts
[] = {
480 "Off", "HS DAC", "Line-In amp"
483 static const struct soc_enum twl6040_hs_enum
[] = {
484 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
486 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
490 static const char *twl6040_hf_texts
[] = {
491 "Off", "HF DAC", "Line-In amp"
494 static const struct soc_enum twl6040_hf_enum
[] = {
495 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
497 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
501 static const char *twl6040_vibrapath_texts
[] = {
502 "Input FF", "Audio PDM"
505 static const struct soc_enum twl6040_vibra_enum
[] = {
506 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL
, 1,
507 ARRAY_SIZE(twl6040_vibrapath_texts
),
508 twl6040_vibrapath_texts
),
509 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR
, 1,
510 ARRAY_SIZE(twl6040_vibrapath_texts
),
511 twl6040_vibrapath_texts
),
514 static const struct snd_kcontrol_new amicl_control
=
515 SOC_DAPM_ENUM("Route", twl6040_enum
[0]);
517 static const struct snd_kcontrol_new amicr_control
=
518 SOC_DAPM_ENUM("Route", twl6040_enum
[1]);
520 /* Headset DAC playback switches */
521 static const struct snd_kcontrol_new hsl_mux_controls
=
522 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[0]);
524 static const struct snd_kcontrol_new hsr_mux_controls
=
525 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[1]);
527 /* Handsfree DAC playback switches */
528 static const struct snd_kcontrol_new hfl_mux_controls
=
529 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[0]);
531 static const struct snd_kcontrol_new hfr_mux_controls
=
532 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[1]);
534 static const struct snd_kcontrol_new ep_path_enable_control
=
535 SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW
, 0, 1, 0);
537 static const struct snd_kcontrol_new auxl_switch_control
=
538 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL
, 6, 1, 0);
540 static const struct snd_kcontrol_new auxr_switch_control
=
541 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL
, 6, 1, 0);
543 /* Vibra playback switches */
544 static const struct snd_kcontrol_new vibral_mux_controls
=
545 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum
[0],
546 snd_soc_dapm_get_enum_double
,
547 twl6040_soc_dapm_put_vibra_enum
);
549 static const struct snd_kcontrol_new vibrar_mux_controls
=
550 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum
[1],
551 snd_soc_dapm_get_enum_double
,
552 twl6040_soc_dapm_put_vibra_enum
);
554 /* Headset power mode */
555 static const char *twl6040_power_mode_texts
[] = {
556 "Low-Power", "High-Performance",
559 static const struct soc_enum twl6040_power_mode_enum
=
560 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts
),
561 twl6040_power_mode_texts
);
563 static int twl6040_headset_power_get_enum(struct snd_kcontrol
*kcontrol
,
564 struct snd_ctl_elem_value
*ucontrol
)
566 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
567 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
569 ucontrol
->value
.enumerated
.item
[0] = priv
->hs_power_mode
;
574 static int twl6040_headset_power_put_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
);
579 int high_perf
= ucontrol
->value
.enumerated
.item
[0];
582 if (!priv
->hs_power_mode_locked
)
583 ret
= headset_power_mode(codec
, high_perf
);
586 priv
->hs_power_mode
= high_perf
;
591 static int twl6040_pll_get_enum(struct snd_kcontrol
*kcontrol
,
592 struct snd_ctl_elem_value
*ucontrol
)
594 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
595 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
597 ucontrol
->value
.enumerated
.item
[0] = priv
->pll_power_mode
;
602 static int twl6040_pll_put_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 priv
->pll_power_mode
= ucontrol
->value
.enumerated
.item
[0];
613 int twl6040_get_dl1_gain(struct snd_soc_codec
*codec
)
615 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
617 if (snd_soc_dapm_get_pin_status(dapm
, "EP"))
618 return -1; /* -1dB */
620 if (snd_soc_dapm_get_pin_status(dapm
, "HSOR") ||
621 snd_soc_dapm_get_pin_status(dapm
, "HSOL")) {
623 u8 val
= snd_soc_read(codec
, TWL6040_REG_HSLCTL
);
624 if (val
& TWL6040_HSDACMODE
)
625 /* HSDACL in LP mode */
626 return -8; /* -8dB */
628 /* HSDACL in HP mode */
629 return -1; /* -1dB */
633 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain
);
635 int twl6040_get_clk_id(struct snd_soc_codec
*codec
)
637 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
639 return priv
->pll_power_mode
;
641 EXPORT_SYMBOL_GPL(twl6040_get_clk_id
);
643 int twl6040_get_trim_value(struct snd_soc_codec
*codec
, enum twl6040_trim trim
)
645 if (unlikely(trim
>= TWL6040_TRIM_INVAL
))
648 return twl6040_read_reg_cache(codec
, TWL6040_REG_TRIM1
+ trim
);
650 EXPORT_SYMBOL_GPL(twl6040_get_trim_value
);
652 int twl6040_get_hs_step_size(struct snd_soc_codec
*codec
)
654 struct twl6040
*twl6040
= codec
->control_data
;
656 if (twl6040_get_revid(twl6040
) < TWL6040_REV_ES1_3
)
657 /* For ES under ES_1.3 HS step is 2 mV */
660 /* For ES_1.3 HS step is 1 mV */
663 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size
);
665 static const struct snd_kcontrol_new twl6040_snd_controls
[] = {
667 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
668 TWL6040_REG_MICGAIN
, 6, 7, 1, 1, mic_preamp_tlv
),
669 SOC_DOUBLE_TLV("Capture Volume",
670 TWL6040_REG_MICGAIN
, 0, 3, 4, 0, mic_amp_tlv
),
673 SOC_DOUBLE_TLV("Aux FM Volume",
674 TWL6040_REG_LINEGAIN
, 0, 3, 7, 0, afm_amp_tlv
),
677 SOC_DOUBLE_TLV("Headset Playback Volume",
678 TWL6040_REG_HSGAIN
, 0, 4, 0xF, 1, hs_tlv
),
679 SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
680 TWL6040_REG_HFLGAIN
, TWL6040_REG_HFRGAIN
, 0, 0x1D, 1, hf_tlv
),
681 SOC_SINGLE_TLV("Earphone Playback Volume",
682 TWL6040_REG_EARCTL
, 1, 0xF, 1, ep_tlv
),
684 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum
,
685 twl6040_headset_power_get_enum
,
686 twl6040_headset_power_put_enum
),
688 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum
,
689 twl6040_pll_get_enum
, twl6040_pll_put_enum
),
692 static const struct snd_soc_dapm_widget twl6040_dapm_widgets
[] = {
694 SND_SOC_DAPM_INPUT("MAINMIC"),
695 SND_SOC_DAPM_INPUT("HSMIC"),
696 SND_SOC_DAPM_INPUT("SUBMIC"),
697 SND_SOC_DAPM_INPUT("AFML"),
698 SND_SOC_DAPM_INPUT("AFMR"),
701 SND_SOC_DAPM_OUTPUT("HSOL"),
702 SND_SOC_DAPM_OUTPUT("HSOR"),
703 SND_SOC_DAPM_OUTPUT("HFL"),
704 SND_SOC_DAPM_OUTPUT("HFR"),
705 SND_SOC_DAPM_OUTPUT("EP"),
706 SND_SOC_DAPM_OUTPUT("AUXL"),
707 SND_SOC_DAPM_OUTPUT("AUXR"),
708 SND_SOC_DAPM_OUTPUT("VIBRAL"),
709 SND_SOC_DAPM_OUTPUT("VIBRAR"),
711 /* Analog input muxes for the capture amplifiers */
712 SND_SOC_DAPM_MUX("Analog Left Capture Route",
713 SND_SOC_NOPM
, 0, 0, &amicl_control
),
714 SND_SOC_DAPM_MUX("Analog Right Capture Route",
715 SND_SOC_NOPM
, 0, 0, &amicr_control
),
717 /* Analog capture PGAs */
718 SND_SOC_DAPM_PGA("MicAmpL",
719 TWL6040_REG_MICLCTL
, 0, 0, NULL
, 0),
720 SND_SOC_DAPM_PGA("MicAmpR",
721 TWL6040_REG_MICRCTL
, 0, 0, NULL
, 0),
723 /* Auxiliary FM PGAs */
724 SND_SOC_DAPM_PGA("AFMAmpL",
725 TWL6040_REG_MICLCTL
, 1, 0, NULL
, 0),
726 SND_SOC_DAPM_PGA("AFMAmpR",
727 TWL6040_REG_MICRCTL
, 1, 0, NULL
, 0),
730 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
731 TWL6040_REG_MICLCTL
, 2, 0),
732 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
733 TWL6040_REG_MICRCTL
, 2, 0),
735 /* Microphone bias */
736 SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
737 TWL6040_REG_AMICBCTL
, 0, 0, NULL
, 0),
738 SND_SOC_DAPM_SUPPLY("Main Mic Bias",
739 TWL6040_REG_AMICBCTL
, 4, 0, NULL
, 0),
740 SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
741 TWL6040_REG_DMICBCTL
, 0, 0, NULL
, 0),
742 SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
743 TWL6040_REG_DMICBCTL
, 4, 0, NULL
, 0),
746 SND_SOC_DAPM_DAC("HSDAC Left", "Headset Playback", SND_SOC_NOPM
, 0, 0),
747 SND_SOC_DAPM_DAC("HSDAC Right", "Headset Playback", SND_SOC_NOPM
, 0, 0),
748 SND_SOC_DAPM_DAC("HFDAC Left", "Handsfree Playback",
749 TWL6040_REG_HFLCTL
, 0, 0),
750 SND_SOC_DAPM_DAC("HFDAC Right", "Handsfree Playback",
751 TWL6040_REG_HFRCTL
, 0, 0),
752 /* Virtual DAC for vibra path (DL4 channel) */
753 SND_SOC_DAPM_DAC("VIBRA DAC", "Vibra Playback",
756 SND_SOC_DAPM_MUX("Handsfree Left Playback",
757 SND_SOC_NOPM
, 0, 0, &hfl_mux_controls
),
758 SND_SOC_DAPM_MUX("Handsfree Right Playback",
759 SND_SOC_NOPM
, 0, 0, &hfr_mux_controls
),
760 /* Analog playback Muxes */
761 SND_SOC_DAPM_MUX("Headset Left Playback",
762 SND_SOC_NOPM
, 0, 0, &hsl_mux_controls
),
763 SND_SOC_DAPM_MUX("Headset Right Playback",
764 SND_SOC_NOPM
, 0, 0, &hsr_mux_controls
),
766 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM
, 0, 0,
767 &vibral_mux_controls
),
768 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM
, 0, 0,
769 &vibrar_mux_controls
),
771 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM
, 0, 0,
772 &ep_path_enable_control
),
773 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM
, 0, 0,
774 &auxl_switch_control
),
775 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM
, 0, 0,
776 &auxr_switch_control
),
778 /* Analog playback drivers */
779 SND_SOC_DAPM_OUT_DRV("HF Left Driver",
780 TWL6040_REG_HFLCTL
, 4, 0, NULL
, 0),
781 SND_SOC_DAPM_OUT_DRV("HF Right Driver",
782 TWL6040_REG_HFRCTL
, 4, 0, NULL
, 0),
783 SND_SOC_DAPM_OUT_DRV("HS Left Driver",
784 TWL6040_REG_HSLCTL
, 2, 0, NULL
, 0),
785 SND_SOC_DAPM_OUT_DRV("HS Right Driver",
786 TWL6040_REG_HSRCTL
, 2, 0, NULL
, 0),
787 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
788 TWL6040_REG_EARCTL
, 0, 0, NULL
, 0,
789 twl6040_ep_drv_event
,
790 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
791 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
792 TWL6040_REG_VIBCTLL
, 0, 0, NULL
, 0),
793 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
794 TWL6040_REG_VIBCTLR
, 0, 0, NULL
, 0),
796 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL
, 2, 0,
798 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR
, 2, 0,
800 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM
, 0, 0,
801 twl6040_hs_dac_event
,
802 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
804 /* Analog playback PGAs */
805 SND_SOC_DAPM_PGA("HF Left PGA",
806 TWL6040_REG_HFLCTL
, 1, 0, NULL
, 0),
807 SND_SOC_DAPM_PGA("HF Right PGA",
808 TWL6040_REG_HFRCTL
, 1, 0, NULL
, 0),
812 static const struct snd_soc_dapm_route intercon
[] = {
814 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
815 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
816 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
818 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
819 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
820 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
822 {"MicAmpL", NULL
, "Analog Left Capture Route"},
823 {"MicAmpR", NULL
, "Analog Right Capture Route"},
825 {"ADC Left", NULL
, "MicAmpL"},
826 {"ADC Right", NULL
, "MicAmpR"},
829 {"AFMAmpL", NULL
, "AFML"},
830 {"AFMAmpR", NULL
, "AFMR"},
832 {"HSDAC Left", NULL
, "HSDAC Power"},
833 {"HSDAC Right", NULL
, "HSDAC Power"},
835 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
836 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
838 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
839 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
841 {"HS Left Driver", NULL
, "Headset Left Playback"},
842 {"HS Right Driver", NULL
, "Headset Right Playback"},
844 {"HSOL", NULL
, "HS Left Driver"},
845 {"HSOR", NULL
, "HS Right Driver"},
847 /* Earphone playback path */
848 {"Earphone Playback", "Switch", "HSDAC Left"},
849 {"Earphone Driver", NULL
, "Earphone Playback"},
850 {"EP", NULL
, "Earphone Driver"},
852 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
853 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
855 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
856 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
858 {"HF Left PGA", NULL
, "Handsfree Left Playback"},
859 {"HF Right PGA", NULL
, "Handsfree Right Playback"},
861 {"HF Left Driver", NULL
, "HF Left PGA"},
862 {"HF Right Driver", NULL
, "HF Right PGA"},
864 {"HFL", NULL
, "HF Left Driver"},
865 {"HFR", NULL
, "HF Right Driver"},
867 {"AUXL Playback", "Switch", "HF Left PGA"},
868 {"AUXR Playback", "Switch", "HF Right PGA"},
870 {"AUXL", NULL
, "AUXL Playback"},
871 {"AUXR", NULL
, "AUXR Playback"},
874 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
875 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
877 {"Vibra Left Driver", NULL
, "Vibra Left Playback"},
878 {"Vibra Right Driver", NULL
, "Vibra Right Playback"},
879 {"Vibra Left Driver", NULL
, "Vibra Left Control"},
880 {"Vibra Right Driver", NULL
, "Vibra Right Control"},
882 {"VIBRAL", NULL
, "Vibra Left Driver"},
883 {"VIBRAR", NULL
, "Vibra Right Driver"},
886 static int twl6040_set_bias_level(struct snd_soc_codec
*codec
,
887 enum snd_soc_bias_level level
)
889 struct twl6040
*twl6040
= codec
->control_data
;
890 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
894 case SND_SOC_BIAS_ON
:
896 case SND_SOC_BIAS_PREPARE
:
898 case SND_SOC_BIAS_STANDBY
:
899 if (priv
->codec_powered
)
902 ret
= twl6040_power(twl6040
, 1);
906 priv
->codec_powered
= 1;
908 twl6040_restore_regs(codec
);
910 /* Set external boost GPO */
911 twl6040_write(codec
, TWL6040_REG_GPOCTL
, 0x02);
913 case SND_SOC_BIAS_OFF
:
914 if (!priv
->codec_powered
)
917 twl6040_power(twl6040
, 0);
918 priv
->codec_powered
= 0;
922 codec
->dapm
.bias_level
= level
;
927 static int twl6040_startup(struct snd_pcm_substream
*substream
,
928 struct snd_soc_dai
*dai
)
930 struct snd_soc_codec
*codec
= dai
->codec
;
931 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
933 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
934 SNDRV_PCM_HW_PARAM_RATE
,
935 &sysclk_constraints
[priv
->pll_power_mode
]);
940 static int twl6040_hw_params(struct snd_pcm_substream
*substream
,
941 struct snd_pcm_hw_params
*params
,
942 struct snd_soc_dai
*dai
)
944 struct snd_soc_codec
*codec
= dai
->codec
;
945 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
948 rate
= params_rate(params
);
954 /* These rates are not supported when HPPLL is in use */
955 if (unlikely(priv
->pll
== TWL6040_SYSCLK_SEL_HPPLL
)) {
956 dev_err(codec
->dev
, "HPPLL does not support rate %d\n",
960 priv
->sysclk
= 17640000;
967 priv
->sysclk
= 19200000;
970 dev_err(codec
->dev
, "unsupported rate %d\n", rate
);
977 static int twl6040_prepare(struct snd_pcm_substream
*substream
,
978 struct snd_soc_dai
*dai
)
980 struct snd_soc_codec
*codec
= dai
->codec
;
981 struct twl6040
*twl6040
= codec
->control_data
;
982 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
987 "no mclk configured, call set_sysclk() on init\n");
991 ret
= twl6040_set_pll(twl6040
, priv
->pll
, priv
->clk_in
, priv
->sysclk
);
993 dev_err(codec
->dev
, "Can not set PLL (%d)\n", ret
);
1000 static int twl6040_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1001 int clk_id
, unsigned int freq
, int dir
)
1003 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1004 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1007 case TWL6040_SYSCLK_SEL_LPPLL
:
1008 case TWL6040_SYSCLK_SEL_HPPLL
:
1010 priv
->clk_in
= freq
;
1013 dev_err(codec
->dev
, "unknown clk_id %d\n", clk_id
);
1020 static const struct snd_soc_dai_ops twl6040_dai_ops
= {
1021 .startup
= twl6040_startup
,
1022 .hw_params
= twl6040_hw_params
,
1023 .prepare
= twl6040_prepare
,
1024 .set_sysclk
= twl6040_set_dai_sysclk
,
1027 static struct snd_soc_dai_driver twl6040_dai
[] = {
1029 .name
= "twl6040-legacy",
1031 .stream_name
= "Playback",
1034 .rates
= TWL6040_RATES
,
1035 .formats
= TWL6040_FORMATS
,
1038 .stream_name
= "Capture",
1041 .rates
= TWL6040_RATES
,
1042 .formats
= TWL6040_FORMATS
,
1044 .ops
= &twl6040_dai_ops
,
1047 .name
= "twl6040-ul",
1049 .stream_name
= "Capture",
1052 .rates
= TWL6040_RATES
,
1053 .formats
= TWL6040_FORMATS
,
1055 .ops
= &twl6040_dai_ops
,
1058 .name
= "twl6040-dl1",
1060 .stream_name
= "Headset Playback",
1063 .rates
= TWL6040_RATES
,
1064 .formats
= TWL6040_FORMATS
,
1066 .ops
= &twl6040_dai_ops
,
1069 .name
= "twl6040-dl2",
1071 .stream_name
= "Handsfree Playback",
1074 .rates
= TWL6040_RATES
,
1075 .formats
= TWL6040_FORMATS
,
1077 .ops
= &twl6040_dai_ops
,
1080 .name
= "twl6040-vib",
1082 .stream_name
= "Vibra Playback",
1085 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
1086 .formats
= TWL6040_FORMATS
,
1088 .ops
= &twl6040_dai_ops
,
1093 static int twl6040_suspend(struct snd_soc_codec
*codec
)
1095 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1100 static int twl6040_resume(struct snd_soc_codec
*codec
)
1102 twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1103 twl6040_set_bias_level(codec
, codec
->dapm
.suspend_bias_level
);
1108 #define twl6040_suspend NULL
1109 #define twl6040_resume NULL
1112 static int twl6040_probe(struct snd_soc_codec
*codec
)
1114 struct twl6040_data
*priv
;
1115 struct twl6040_codec_data
*pdata
= dev_get_platdata(codec
->dev
);
1116 struct platform_device
*pdev
= container_of(codec
->dev
,
1117 struct platform_device
, dev
);
1120 priv
= kzalloc(sizeof(struct twl6040_data
), GFP_KERNEL
);
1123 snd_soc_codec_set_drvdata(codec
, priv
);
1125 priv
->codec
= codec
;
1126 codec
->control_data
= dev_get_drvdata(codec
->dev
->parent
);
1128 if (pdata
&& pdata
->hs_left_step
&& pdata
->hs_right_step
) {
1129 priv
->hs_left_step
= pdata
->hs_left_step
;
1130 priv
->hs_right_step
= pdata
->hs_right_step
;
1132 priv
->hs_left_step
= 1;
1133 priv
->hs_right_step
= 1;
1136 if (pdata
&& pdata
->hf_left_step
&& pdata
->hf_right_step
) {
1137 priv
->hf_left_step
= pdata
->hf_left_step
;
1138 priv
->hf_right_step
= pdata
->hf_right_step
;
1140 priv
->hf_left_step
= 1;
1141 priv
->hf_right_step
= 1;
1144 priv
->plug_irq
= platform_get_irq(pdev
, 0);
1145 if (priv
->plug_irq
< 0) {
1146 dev_err(codec
->dev
, "invalid irq\n");
1151 priv
->workqueue
= alloc_workqueue("twl6040-codec", 0, 0);
1152 if (!priv
->workqueue
) {
1157 INIT_DELAYED_WORK(&priv
->hs_jack
.work
, twl6040_accessory_work
);
1159 mutex_init(&priv
->mutex
);
1161 ret
= request_threaded_irq(priv
->plug_irq
, NULL
, twl6040_audio_handler
,
1162 0, "twl6040_irq_plug", codec
);
1164 dev_err(codec
->dev
, "PLUG IRQ request failed: %d\n", ret
);
1168 twl6040_init_chip(codec
);
1170 /* power on device */
1171 ret
= twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1176 free_irq(priv
->plug_irq
, codec
);
1178 destroy_workqueue(priv
->workqueue
);
1184 static int twl6040_remove(struct snd_soc_codec
*codec
)
1186 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1188 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1189 free_irq(priv
->plug_irq
, codec
);
1190 destroy_workqueue(priv
->workqueue
);
1196 static struct snd_soc_codec_driver soc_codec_dev_twl6040
= {
1197 .probe
= twl6040_probe
,
1198 .remove
= twl6040_remove
,
1199 .suspend
= twl6040_suspend
,
1200 .resume
= twl6040_resume
,
1201 .read
= twl6040_read_reg_cache
,
1202 .write
= twl6040_write
,
1203 .set_bias_level
= twl6040_set_bias_level
,
1204 .reg_cache_size
= ARRAY_SIZE(twl6040_reg
),
1205 .reg_word_size
= sizeof(u8
),
1206 .reg_cache_default
= twl6040_reg
,
1207 .ignore_pmdown_time
= true,
1209 .controls
= twl6040_snd_controls
,
1210 .num_controls
= ARRAY_SIZE(twl6040_snd_controls
),
1211 .dapm_widgets
= twl6040_dapm_widgets
,
1212 .num_dapm_widgets
= ARRAY_SIZE(twl6040_dapm_widgets
),
1213 .dapm_routes
= intercon
,
1214 .num_dapm_routes
= ARRAY_SIZE(intercon
),
1217 static int __devinit
twl6040_codec_probe(struct platform_device
*pdev
)
1219 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_twl6040
,
1220 twl6040_dai
, ARRAY_SIZE(twl6040_dai
));
1223 static int __devexit
twl6040_codec_remove(struct platform_device
*pdev
)
1225 snd_soc_unregister_codec(&pdev
->dev
);
1229 static struct platform_driver twl6040_codec_driver
= {
1231 .name
= "twl6040-codec",
1232 .owner
= THIS_MODULE
,
1234 .probe
= twl6040_codec_probe
,
1235 .remove
= __devexit_p(twl6040_codec_remove
),
1238 module_platform_driver(twl6040_codec_driver
);
1240 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1241 MODULE_AUTHOR("Misael Lopez Cruz");
1242 MODULE_LICENSE("GPL");