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/i2c/twl.h>
30 #include <linux/mfd/twl6040.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
38 #include <sound/tlv.h>
42 #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
43 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
45 #define TWL6040_OUTHS_0dB 0x00
46 #define TWL6040_OUTHS_M30dB 0x0F
47 #define TWL6040_OUTHF_0dB 0x03
48 #define TWL6040_OUTHF_M52dB 0x1D
50 #define TWL6040_RAMP_NONE 0
51 #define TWL6040_RAMP_UP 1
52 #define TWL6040_RAMP_DOWN 2
54 #define TWL6040_HSL_VOL_MASK 0x0F
55 #define TWL6040_HSL_VOL_SHIFT 0
56 #define TWL6040_HSR_VOL_MASK 0xF0
57 #define TWL6040_HSR_VOL_SHIFT 4
58 #define TWL6040_HF_VOL_MASK 0x1F
59 #define TWL6040_HF_VOL_SHIFT 0
61 /* Shadow register used by the driver */
62 #define TWL6040_REG_SW_SHADOW 0x2F
63 #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1)
65 /* TWL6040_REG_SW_SHADOW (0x2F) fields */
66 #define TWL6040_EAR_PATH_ENABLE 0x01
68 struct twl6040_output
{
74 unsigned int step_delay
;
76 struct delayed_work work
;
77 struct completion ramp_done
;
80 struct twl6040_jack_data
{
81 struct snd_soc_jack
*jack
;
82 struct delayed_work work
;
86 /* codec private data */
93 int hs_power_mode_locked
;
100 struct twl6040_jack_data hs_jack
;
101 struct snd_soc_codec
*codec
;
102 struct workqueue_struct
*workqueue
;
104 struct twl6040_output headset
;
105 struct twl6040_output handsfree
;
109 * twl6040 register cache & default register settings
111 static const u8 twl6040_reg
[TWL6040_CACHEREGNUM
] = {
112 0x00, /* not used 0x00 */
113 0x4B, /* REG_ASICID 0x01 (ro) */
114 0x00, /* REG_ASICREV 0x02 (ro) */
115 0x00, /* REG_INTID 0x03 */
116 0x00, /* REG_INTMR 0x04 */
117 0x00, /* REG_NCPCTRL 0x05 */
118 0x00, /* REG_LDOCTL 0x06 */
119 0x60, /* REG_HPPLLCTL 0x07 */
120 0x00, /* REG_LPPLLCTL 0x08 */
121 0x4A, /* REG_LPPLLDIV 0x09 */
122 0x00, /* REG_AMICBCTL 0x0A */
123 0x00, /* REG_DMICBCTL 0x0B */
124 0x00, /* REG_MICLCTL 0x0C */
125 0x00, /* REG_MICRCTL 0x0D */
126 0x00, /* REG_MICGAIN 0x0E */
127 0x1B, /* REG_LINEGAIN 0x0F */
128 0x00, /* REG_HSLCTL 0x10 */
129 0x00, /* REG_HSRCTL 0x11 */
130 0x00, /* REG_HSGAIN 0x12 */
131 0x00, /* REG_EARCTL 0x13 */
132 0x00, /* REG_HFLCTL 0x14 */
133 0x00, /* REG_HFLGAIN 0x15 */
134 0x00, /* REG_HFRCTL 0x16 */
135 0x00, /* REG_HFRGAIN 0x17 */
136 0x00, /* REG_VIBCTLL 0x18 */
137 0x00, /* REG_VIBDATL 0x19 */
138 0x00, /* REG_VIBCTLR 0x1A */
139 0x00, /* REG_VIBDATR 0x1B */
140 0x00, /* REG_HKCTL1 0x1C */
141 0x00, /* REG_HKCTL2 0x1D */
142 0x00, /* REG_GPOCTL 0x1E */
143 0x00, /* REG_ALB 0x1F */
144 0x00, /* REG_DLB 0x20 */
145 0x00, /* not used 0x21 */
146 0x00, /* not used 0x22 */
147 0x00, /* not used 0x23 */
148 0x00, /* not used 0x24 */
149 0x00, /* not used 0x25 */
150 0x00, /* not used 0x26 */
151 0x00, /* not used 0x27 */
152 0x00, /* REG_TRIM1 0x28 */
153 0x00, /* REG_TRIM2 0x29 */
154 0x00, /* REG_TRIM3 0x2A */
155 0x00, /* REG_HSOTRIM 0x2B */
156 0x00, /* REG_HFOTRIM 0x2C */
157 0x09, /* REG_ACCCTL 0x2D */
158 0x00, /* REG_STATUS 0x2E (ro) */
160 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */
163 /* List of registers to be restored after power up */
164 static const int twl6040_restore_list
[] = {
168 TWL6040_REG_LINEGAIN
,
179 /* set of rates for each pll: low-power and high-performance */
180 static unsigned int lp_rates
[] = {
192 static unsigned int hp_rates
[] = {
200 static struct snd_pcm_hw_constraint_list sysclk_constraints
[] = {
201 { .count
= ARRAY_SIZE(lp_rates
), .list
= lp_rates
, },
202 { .count
= ARRAY_SIZE(hp_rates
), .list
= hp_rates
, },
206 * read twl6040 register cache
208 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec
*codec
,
211 u8
*cache
= codec
->reg_cache
;
213 if (reg
>= TWL6040_CACHEREGNUM
)
220 * write twl6040 register cache
222 static inline void twl6040_write_reg_cache(struct snd_soc_codec
*codec
,
225 u8
*cache
= codec
->reg_cache
;
227 if (reg
>= TWL6040_CACHEREGNUM
)
233 * read from twl6040 hardware register
235 static int twl6040_read_reg_volatile(struct snd_soc_codec
*codec
,
238 struct twl6040
*twl6040
= codec
->control_data
;
241 if (reg
>= TWL6040_CACHEREGNUM
)
244 if (likely(reg
< TWL6040_REG_SW_SHADOW
)) {
245 value
= twl6040_reg_read(twl6040
, reg
);
246 twl6040_write_reg_cache(codec
, reg
, value
);
248 value
= twl6040_read_reg_cache(codec
, reg
);
255 * write to the twl6040 register space
257 static int twl6040_write(struct snd_soc_codec
*codec
,
258 unsigned int reg
, unsigned int value
)
260 struct twl6040
*twl6040
= codec
->control_data
;
262 if (reg
>= TWL6040_CACHEREGNUM
)
265 twl6040_write_reg_cache(codec
, reg
, value
);
266 if (likely(reg
< TWL6040_REG_SW_SHADOW
))
267 return twl6040_reg_write(twl6040
, reg
, value
);
272 static void twl6040_init_chip(struct snd_soc_codec
*codec
)
274 struct twl6040
*twl6040
= codec
->control_data
;
277 /* Update reg_cache: ASICREV, and TRIM values */
278 val
= twl6040_get_revid(twl6040
);
279 twl6040_write_reg_cache(codec
, TWL6040_REG_ASICREV
, val
);
281 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM1
);
282 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM2
);
283 twl6040_read_reg_volatile(codec
, TWL6040_REG_TRIM3
);
284 twl6040_read_reg_volatile(codec
, TWL6040_REG_HSOTRIM
);
285 twl6040_read_reg_volatile(codec
, TWL6040_REG_HFOTRIM
);
287 /* Change chip defaults */
288 /* No imput selected for microphone amplifiers */
289 twl6040_write_reg_cache(codec
, TWL6040_REG_MICLCTL
, 0x18);
290 twl6040_write_reg_cache(codec
, TWL6040_REG_MICRCTL
, 0x18);
293 * We need to lower the default gain values, so the ramp code
294 * can work correctly for the first playback.
295 * This reduces the pop noise heard at the first playback.
297 twl6040_write_reg_cache(codec
, TWL6040_REG_HSGAIN
, 0xff);
298 twl6040_write_reg_cache(codec
, TWL6040_REG_EARCTL
, 0x1e);
299 twl6040_write_reg_cache(codec
, TWL6040_REG_HFLGAIN
, 0x1d);
300 twl6040_write_reg_cache(codec
, TWL6040_REG_HFRGAIN
, 0x1d);
301 twl6040_write_reg_cache(codec
, TWL6040_REG_LINEGAIN
, 0);
304 static void twl6040_restore_regs(struct snd_soc_codec
*codec
)
306 u8
*cache
= codec
->reg_cache
;
309 for (i
= 0; i
< ARRAY_SIZE(twl6040_restore_list
); i
++) {
310 reg
= twl6040_restore_list
[i
];
311 twl6040_write(codec
, reg
, cache
[reg
]);
316 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
318 static inline int twl6040_hs_ramp_step(struct snd_soc_codec
*codec
,
319 unsigned int left_step
, unsigned int right_step
)
322 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
323 struct twl6040_output
*headset
= &priv
->headset
;
324 int left_complete
= 0, right_complete
= 0;
328 left_step
= (left_step
> 0xF) ? 0xF : left_step
;
329 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSGAIN
);
330 val
= (~reg
& TWL6040_HSL_VOL_MASK
);
332 if (headset
->ramp
== TWL6040_RAMP_UP
) {
334 if (val
< headset
->left_vol
) {
335 if (val
+ left_step
> headset
->left_vol
)
336 val
= headset
->left_vol
;
340 reg
&= ~TWL6040_HSL_VOL_MASK
;
341 twl6040_write(codec
, TWL6040_REG_HSGAIN
,
342 (reg
| (~val
& TWL6040_HSL_VOL_MASK
)));
346 } else if (headset
->ramp
== TWL6040_RAMP_DOWN
) {
349 if ((int)val
- (int)left_step
< 0)
354 reg
&= ~TWL6040_HSL_VOL_MASK
;
355 twl6040_write(codec
, TWL6040_REG_HSGAIN
, reg
|
356 (~val
& TWL6040_HSL_VOL_MASK
));
363 right_step
= (right_step
> 0xF) ? 0xF : right_step
;
364 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSGAIN
);
365 val
= (~reg
& TWL6040_HSR_VOL_MASK
) >> TWL6040_HSR_VOL_SHIFT
;
367 if (headset
->ramp
== TWL6040_RAMP_UP
) {
369 if (val
< headset
->right_vol
) {
370 if (val
+ right_step
> headset
->right_vol
)
371 val
= headset
->right_vol
;
375 reg
&= ~TWL6040_HSR_VOL_MASK
;
376 twl6040_write(codec
, TWL6040_REG_HSGAIN
,
377 (reg
| (~val
<< TWL6040_HSR_VOL_SHIFT
)));
381 } else if (headset
->ramp
== TWL6040_RAMP_DOWN
) {
384 if ((int)val
- (int)right_step
< 0)
389 reg
&= ~TWL6040_HSR_VOL_MASK
;
390 twl6040_write(codec
, TWL6040_REG_HSGAIN
,
391 reg
| (~val
<< TWL6040_HSR_VOL_SHIFT
));
397 return left_complete
& right_complete
;
401 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
403 static inline int twl6040_hf_ramp_step(struct snd_soc_codec
*codec
,
404 unsigned int left_step
, unsigned int right_step
)
406 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
407 struct twl6040_output
*handsfree
= &priv
->handsfree
;
408 int left_complete
= 0, right_complete
= 0;
412 left_step
= (left_step
> 0x1D) ? 0x1D : left_step
;
413 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HFLGAIN
);
415 val
= (reg
& TWL6040_HF_VOL_MASK
);
416 if (handsfree
->ramp
== TWL6040_RAMP_UP
) {
418 if (val
< handsfree
->left_vol
) {
419 if (val
+ left_step
> handsfree
->left_vol
)
420 val
= handsfree
->left_vol
;
424 reg
&= ~TWL6040_HF_VOL_MASK
;
425 twl6040_write(codec
, TWL6040_REG_HFLGAIN
,
430 } else if (handsfree
->ramp
== TWL6040_RAMP_DOWN
) {
433 if ((int)val
- (int)left_step
< 0)
438 reg
&= ~TWL6040_HF_VOL_MASK
;
439 twl6040_write(codec
, TWL6040_REG_HFLGAIN
,
447 right_step
= (right_step
> 0x1D) ? 0x1D : right_step
;
448 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HFRGAIN
);
450 val
= (reg
& TWL6040_HF_VOL_MASK
);
451 if (handsfree
->ramp
== TWL6040_RAMP_UP
) {
453 if (val
< handsfree
->right_vol
) {
454 if (val
+ right_step
> handsfree
->right_vol
)
455 val
= handsfree
->right_vol
;
459 reg
&= ~TWL6040_HF_VOL_MASK
;
460 twl6040_write(codec
, TWL6040_REG_HFRGAIN
,
465 } else if (handsfree
->ramp
== TWL6040_RAMP_DOWN
) {
468 if ((int)val
- (int)right_step
< 0)
473 reg
&= ~TWL6040_HF_VOL_MASK
;
474 twl6040_write(codec
, TWL6040_REG_HFRGAIN
,
479 return left_complete
& right_complete
;
483 * This work ramps both output PGAs at stream start/stop time to
484 * minimise pop associated with DAPM power switching.
486 static void twl6040_pga_hs_work(struct work_struct
*work
)
488 struct twl6040_data
*priv
=
489 container_of(work
, struct twl6040_data
, headset
.work
.work
);
490 struct snd_soc_codec
*codec
= priv
->codec
;
491 struct twl6040_output
*headset
= &priv
->headset
;
492 int i
, headset_complete
;
494 /* do we need to ramp at all ? */
495 if (headset
->ramp
== TWL6040_RAMP_NONE
)
498 /* HS PGA gain range: 0x0 - 0xf (0 - 15) */
499 for (i
= 0; i
< 16; i
++) {
500 headset_complete
= twl6040_hs_ramp_step(codec
,
502 headset
->right_step
);
504 /* ramp finished ? */
505 if (headset_complete
)
508 schedule_timeout_interruptible(
509 msecs_to_jiffies(headset
->step_delay
));
512 if (headset
->ramp
== TWL6040_RAMP_DOWN
) {
514 complete(&headset
->ramp_done
);
518 headset
->ramp
= TWL6040_RAMP_NONE
;
521 static void twl6040_pga_hf_work(struct work_struct
*work
)
523 struct twl6040_data
*priv
=
524 container_of(work
, struct twl6040_data
, handsfree
.work
.work
);
525 struct snd_soc_codec
*codec
= priv
->codec
;
526 struct twl6040_output
*handsfree
= &priv
->handsfree
;
527 int i
, handsfree_complete
;
529 /* do we need to ramp at all ? */
530 if (handsfree
->ramp
== TWL6040_RAMP_NONE
)
534 * HF PGA gain range: 0x00 - 0x1d (0 - 29) */
535 for (i
= 0; i
< 30; i
++) {
536 handsfree_complete
= twl6040_hf_ramp_step(codec
,
537 handsfree
->left_step
,
538 handsfree
->right_step
);
540 /* ramp finished ? */
541 if (handsfree_complete
)
544 schedule_timeout_interruptible(
545 msecs_to_jiffies(handsfree
->step_delay
));
549 if (handsfree
->ramp
== TWL6040_RAMP_DOWN
) {
550 handsfree
->active
= 0;
551 complete(&handsfree
->ramp_done
);
553 handsfree
->active
= 1;
554 handsfree
->ramp
= TWL6040_RAMP_NONE
;
557 static int out_drv_event(struct snd_soc_dapm_widget
*w
,
558 struct snd_kcontrol
*kcontrol
, int event
)
560 struct snd_soc_codec
*codec
= w
->codec
;
561 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
562 struct twl6040_output
*out
;
563 struct delayed_work
*work
;
566 case 2: /* Headset output driver */
567 out
= &priv
->headset
;
570 * Make sure, that we do not mess up variables for already
573 cancel_delayed_work_sync(work
);
575 out
->left_step
= priv
->hs_left_step
;
576 out
->right_step
= priv
->hs_right_step
;
577 out
->step_delay
= 5; /* 5 ms between volume ramp steps */
579 case 4: /* Handsfree output driver */
580 out
= &priv
->handsfree
;
583 * Make sure, that we do not mess up variables for already
586 cancel_delayed_work_sync(work
);
588 out
->left_step
= priv
->hf_left_step
;
589 out
->right_step
= priv
->hf_right_step
;
590 out
->step_delay
= 5; /* 5 ms between volume ramp steps */
597 case SND_SOC_DAPM_POST_PMU
:
601 /* don't use volume ramp for power-up */
602 out
->ramp
= TWL6040_RAMP_UP
;
603 out
->left_step
= out
->left_vol
;
604 out
->right_step
= out
->right_vol
;
606 queue_delayed_work(priv
->workqueue
, work
, msecs_to_jiffies(1));
609 case SND_SOC_DAPM_PRE_PMD
:
613 /* use volume ramp for power-down */
614 out
->ramp
= TWL6040_RAMP_DOWN
;
615 INIT_COMPLETION(out
->ramp_done
);
617 queue_delayed_work(priv
->workqueue
, work
, msecs_to_jiffies(1));
619 wait_for_completion_timeout(&out
->ramp_done
,
620 msecs_to_jiffies(2000));
627 /* set headset dac and driver power mode */
628 static int headset_power_mode(struct snd_soc_codec
*codec
, int high_perf
)
631 int mask
= TWL6040_HSDRVMODE
| TWL6040_HSDACMODE
;
633 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
634 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
644 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
645 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
650 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget
*w
,
651 struct snd_kcontrol
*kcontrol
, int event
)
653 struct snd_soc_codec
*codec
= w
->codec
;
657 * Workaround for Headset DC offset caused pop noise:
658 * Both HS DAC need to be turned on (before the HS driver) and off at
661 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
662 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
663 if (SND_SOC_DAPM_EVENT_ON(event
)) {
664 hslctl
|= TWL6040_HSDACENA
;
665 hsrctl
|= TWL6040_HSDACENA
;
667 hslctl
&= ~TWL6040_HSDACENA
;
668 hsrctl
&= ~TWL6040_HSDACENA
;
670 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
671 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
677 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget
*w
,
678 struct snd_kcontrol
*kcontrol
, int event
)
680 struct snd_soc_codec
*codec
= w
->codec
;
681 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
684 if (SND_SOC_DAPM_EVENT_ON(event
)) {
685 /* Earphone doesn't support low power mode */
686 priv
->hs_power_mode_locked
= 1;
687 ret
= headset_power_mode(codec
, 1);
689 priv
->hs_power_mode_locked
= 0;
690 ret
= headset_power_mode(codec
, priv
->hs_power_mode
);
698 static void twl6040_hs_jack_report(struct snd_soc_codec
*codec
,
699 struct snd_soc_jack
*jack
, int report
)
701 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
704 mutex_lock(&priv
->mutex
);
707 status
= twl6040_read_reg_volatile(codec
, TWL6040_REG_STATUS
);
708 if (status
& TWL6040_PLUGCOMP
)
709 snd_soc_jack_report(jack
, report
, report
);
711 snd_soc_jack_report(jack
, 0, report
);
713 mutex_unlock(&priv
->mutex
);
716 void twl6040_hs_jack_detect(struct snd_soc_codec
*codec
,
717 struct snd_soc_jack
*jack
, int report
)
719 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
720 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
722 hs_jack
->jack
= jack
;
723 hs_jack
->report
= report
;
725 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
727 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect
);
729 static void twl6040_accessory_work(struct work_struct
*work
)
731 struct twl6040_data
*priv
= container_of(work
,
732 struct twl6040_data
, hs_jack
.work
.work
);
733 struct snd_soc_codec
*codec
= priv
->codec
;
734 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
736 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
739 /* audio interrupt handler */
740 static irqreturn_t
twl6040_audio_handler(int irq
, void *data
)
742 struct snd_soc_codec
*codec
= data
;
743 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
745 queue_delayed_work(priv
->workqueue
, &priv
->hs_jack
.work
,
746 msecs_to_jiffies(200));
751 static int twl6040_put_volsw(struct snd_kcontrol
*kcontrol
,
752 struct snd_ctl_elem_value
*ucontrol
)
754 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
755 struct twl6040_data
*twl6040_priv
= snd_soc_codec_get_drvdata(codec
);
756 struct twl6040_output
*out
= NULL
;
757 struct soc_mixer_control
*mc
=
758 (struct soc_mixer_control
*)kcontrol
->private_value
;
761 /* For HS and HF we shadow the values and only actually write
762 * them out when active in order to ensure the amplifier comes on
763 * as quietly as possible. */
765 case TWL6040_REG_HSGAIN
:
766 out
= &twl6040_priv
->headset
;
768 case TWL6040_REG_HFLGAIN
:
769 out
= &twl6040_priv
->handsfree
;
772 dev_warn(codec
->dev
, "%s: Unexpected register: 0x%02x\n",
777 out
->left_vol
= ucontrol
->value
.integer
.value
[0];
778 out
->right_vol
= ucontrol
->value
.integer
.value
[1];
782 ret
= snd_soc_put_volsw(kcontrol
, ucontrol
);
789 static int twl6040_get_volsw(struct snd_kcontrol
*kcontrol
,
790 struct snd_ctl_elem_value
*ucontrol
)
792 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
793 struct twl6040_data
*twl6040_priv
= snd_soc_codec_get_drvdata(codec
);
794 struct twl6040_output
*out
= &twl6040_priv
->headset
;
795 struct soc_mixer_control
*mc
=
796 (struct soc_mixer_control
*)kcontrol
->private_value
;
799 case TWL6040_REG_HSGAIN
:
800 out
= &twl6040_priv
->headset
;
802 case TWL6040_REG_HFLGAIN
:
803 out
= &twl6040_priv
->handsfree
;
806 dev_warn(codec
->dev
, "%s: Unexpected register: 0x%02x\n",
811 ucontrol
->value
.integer
.value
[0] = out
->left_vol
;
812 ucontrol
->value
.integer
.value
[1] = out
->right_vol
;
816 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol
*kcontrol
,
817 struct snd_ctl_elem_value
*ucontrol
)
819 struct snd_soc_dapm_widget_list
*wlist
= snd_kcontrol_chip(kcontrol
);
820 struct snd_soc_dapm_widget
*widget
= wlist
->widgets
[0];
821 struct snd_soc_codec
*codec
= widget
->codec
;
822 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
825 /* Do not allow changes while Input/FF efect is running */
826 val
= twl6040_read_reg_volatile(codec
, e
->reg
);
827 if (val
& TWL6040_VIBENA
&& !(val
& TWL6040_VIBSEL
))
830 return snd_soc_dapm_put_enum_double(kcontrol
, ucontrol
);
834 * MICATT volume control:
835 * from -6 to 0 dB in 6 dB steps
837 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv
, -600, 600, 0);
840 * MICGAIN volume control:
841 * from 6 to 30 dB in 6 dB steps
843 static DECLARE_TLV_DB_SCALE(mic_amp_tlv
, 600, 600, 0);
846 * AFMGAIN volume control:
847 * from -18 to 24 dB in 6 dB steps
849 static DECLARE_TLV_DB_SCALE(afm_amp_tlv
, -1800, 600, 0);
852 * HSGAIN volume control:
853 * from -30 to 0 dB in 2 dB steps
855 static DECLARE_TLV_DB_SCALE(hs_tlv
, -3000, 200, 0);
858 * HFGAIN volume control:
859 * from -52 to 6 dB in 2 dB steps
861 static DECLARE_TLV_DB_SCALE(hf_tlv
, -5200, 200, 0);
864 * EPGAIN volume control:
865 * from -24 to 6 dB in 2 dB steps
867 static DECLARE_TLV_DB_SCALE(ep_tlv
, -2400, 200, 0);
869 /* Left analog microphone selection */
870 static const char *twl6040_amicl_texts
[] =
871 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
873 /* Right analog microphone selection */
874 static const char *twl6040_amicr_texts
[] =
875 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
877 static const struct soc_enum twl6040_enum
[] = {
878 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL
, 3, 4, twl6040_amicl_texts
),
879 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL
, 3, 4, twl6040_amicr_texts
),
882 static const char *twl6040_hs_texts
[] = {
883 "Off", "HS DAC", "Line-In amp"
886 static const struct soc_enum twl6040_hs_enum
[] = {
887 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
889 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
893 static const char *twl6040_hf_texts
[] = {
894 "Off", "HF DAC", "Line-In amp"
897 static const struct soc_enum twl6040_hf_enum
[] = {
898 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
900 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
904 static const char *twl6040_vibrapath_texts
[] = {
905 "Input FF", "Audio PDM"
908 static const struct soc_enum twl6040_vibra_enum
[] = {
909 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL
, 1,
910 ARRAY_SIZE(twl6040_vibrapath_texts
),
911 twl6040_vibrapath_texts
),
912 SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR
, 1,
913 ARRAY_SIZE(twl6040_vibrapath_texts
),
914 twl6040_vibrapath_texts
),
917 static const struct snd_kcontrol_new amicl_control
=
918 SOC_DAPM_ENUM("Route", twl6040_enum
[0]);
920 static const struct snd_kcontrol_new amicr_control
=
921 SOC_DAPM_ENUM("Route", twl6040_enum
[1]);
923 /* Headset DAC playback switches */
924 static const struct snd_kcontrol_new hsl_mux_controls
=
925 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[0]);
927 static const struct snd_kcontrol_new hsr_mux_controls
=
928 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[1]);
930 /* Handsfree DAC playback switches */
931 static const struct snd_kcontrol_new hfl_mux_controls
=
932 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[0]);
934 static const struct snd_kcontrol_new hfr_mux_controls
=
935 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[1]);
937 static const struct snd_kcontrol_new ep_path_enable_control
=
938 SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW
, 0, 1, 0);
940 static const struct snd_kcontrol_new auxl_switch_control
=
941 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL
, 6, 1, 0);
943 static const struct snd_kcontrol_new auxr_switch_control
=
944 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL
, 6, 1, 0);
946 /* Vibra playback switches */
947 static const struct snd_kcontrol_new vibral_mux_controls
=
948 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum
[0],
949 snd_soc_dapm_get_enum_double
,
950 twl6040_soc_dapm_put_vibra_enum
);
952 static const struct snd_kcontrol_new vibrar_mux_controls
=
953 SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum
[1],
954 snd_soc_dapm_get_enum_double
,
955 twl6040_soc_dapm_put_vibra_enum
);
957 /* Headset power mode */
958 static const char *twl6040_power_mode_texts
[] = {
959 "Low-Power", "High-Perfomance",
962 static const struct soc_enum twl6040_power_mode_enum
=
963 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts
),
964 twl6040_power_mode_texts
);
966 static int twl6040_headset_power_get_enum(struct snd_kcontrol
*kcontrol
,
967 struct snd_ctl_elem_value
*ucontrol
)
969 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
970 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
972 ucontrol
->value
.enumerated
.item
[0] = priv
->hs_power_mode
;
977 static int twl6040_headset_power_put_enum(struct snd_kcontrol
*kcontrol
,
978 struct snd_ctl_elem_value
*ucontrol
)
980 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
981 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
982 int high_perf
= ucontrol
->value
.enumerated
.item
[0];
985 if (!priv
->hs_power_mode_locked
)
986 ret
= headset_power_mode(codec
, high_perf
);
989 priv
->hs_power_mode
= high_perf
;
994 static int twl6040_pll_get_enum(struct snd_kcontrol
*kcontrol
,
995 struct snd_ctl_elem_value
*ucontrol
)
997 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
998 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1000 ucontrol
->value
.enumerated
.item
[0] = priv
->pll_power_mode
;
1005 static int twl6040_pll_put_enum(struct snd_kcontrol
*kcontrol
,
1006 struct snd_ctl_elem_value
*ucontrol
)
1008 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1009 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1011 priv
->pll_power_mode
= ucontrol
->value
.enumerated
.item
[0];
1016 int twl6040_get_dl1_gain(struct snd_soc_codec
*codec
)
1018 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
1020 if (snd_soc_dapm_get_pin_status(dapm
, "EP"))
1021 return -1; /* -1dB */
1023 if (snd_soc_dapm_get_pin_status(dapm
, "HSOR") ||
1024 snd_soc_dapm_get_pin_status(dapm
, "HSOL")) {
1026 u8 val
= snd_soc_read(codec
, TWL6040_REG_HSLCTL
);
1027 if (val
& TWL6040_HSDACMODE
)
1028 /* HSDACL in LP mode */
1029 return -8; /* -8dB */
1031 /* HSDACL in HP mode */
1032 return -1; /* -1dB */
1036 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain
);
1038 int twl6040_get_clk_id(struct snd_soc_codec
*codec
)
1040 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1042 return priv
->pll_power_mode
;
1044 EXPORT_SYMBOL_GPL(twl6040_get_clk_id
);
1046 int twl6040_get_trim_value(struct snd_soc_codec
*codec
, enum twl6040_trim trim
)
1048 if (unlikely(trim
>= TWL6040_TRIM_INVAL
))
1051 return twl6040_read_reg_cache(codec
, TWL6040_REG_TRIM1
+ trim
);
1053 EXPORT_SYMBOL_GPL(twl6040_get_trim_value
);
1055 static const struct snd_kcontrol_new twl6040_snd_controls
[] = {
1057 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1058 TWL6040_REG_MICGAIN
, 6, 7, 1, 1, mic_preamp_tlv
),
1059 SOC_DOUBLE_TLV("Capture Volume",
1060 TWL6040_REG_MICGAIN
, 0, 3, 4, 0, mic_amp_tlv
),
1063 SOC_DOUBLE_TLV("Aux FM Volume",
1064 TWL6040_REG_LINEGAIN
, 0, 3, 7, 0, afm_amp_tlv
),
1066 /* Playback gains */
1067 SOC_DOUBLE_EXT_TLV("Headset Playback Volume",
1068 TWL6040_REG_HSGAIN
, 0, 4, 0xF, 1, twl6040_get_volsw
,
1069 twl6040_put_volsw
, hs_tlv
),
1070 SOC_DOUBLE_R_EXT_TLV("Handsfree Playback Volume",
1071 TWL6040_REG_HFLGAIN
, TWL6040_REG_HFRGAIN
, 0, 0x1D, 1,
1072 twl6040_get_volsw
, twl6040_put_volsw
, hf_tlv
),
1073 SOC_SINGLE_TLV("Earphone Playback Volume",
1074 TWL6040_REG_EARCTL
, 1, 0xF, 1, ep_tlv
),
1076 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum
,
1077 twl6040_headset_power_get_enum
,
1078 twl6040_headset_power_put_enum
),
1080 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum
,
1081 twl6040_pll_get_enum
, twl6040_pll_put_enum
),
1084 static const struct snd_soc_dapm_widget twl6040_dapm_widgets
[] = {
1086 SND_SOC_DAPM_INPUT("MAINMIC"),
1087 SND_SOC_DAPM_INPUT("HSMIC"),
1088 SND_SOC_DAPM_INPUT("SUBMIC"),
1089 SND_SOC_DAPM_INPUT("AFML"),
1090 SND_SOC_DAPM_INPUT("AFMR"),
1093 SND_SOC_DAPM_OUTPUT("HSOL"),
1094 SND_SOC_DAPM_OUTPUT("HSOR"),
1095 SND_SOC_DAPM_OUTPUT("HFL"),
1096 SND_SOC_DAPM_OUTPUT("HFR"),
1097 SND_SOC_DAPM_OUTPUT("EP"),
1098 SND_SOC_DAPM_OUTPUT("AUXL"),
1099 SND_SOC_DAPM_OUTPUT("AUXR"),
1100 SND_SOC_DAPM_OUTPUT("VIBRAL"),
1101 SND_SOC_DAPM_OUTPUT("VIBRAR"),
1103 /* Analog input muxes for the capture amplifiers */
1104 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1105 SND_SOC_NOPM
, 0, 0, &amicl_control
),
1106 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1107 SND_SOC_NOPM
, 0, 0, &amicr_control
),
1109 /* Analog capture PGAs */
1110 SND_SOC_DAPM_PGA("MicAmpL",
1111 TWL6040_REG_MICLCTL
, 0, 0, NULL
, 0),
1112 SND_SOC_DAPM_PGA("MicAmpR",
1113 TWL6040_REG_MICRCTL
, 0, 0, NULL
, 0),
1115 /* Auxiliary FM PGAs */
1116 SND_SOC_DAPM_PGA("AFMAmpL",
1117 TWL6040_REG_MICLCTL
, 1, 0, NULL
, 0),
1118 SND_SOC_DAPM_PGA("AFMAmpR",
1119 TWL6040_REG_MICRCTL
, 1, 0, NULL
, 0),
1122 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1123 TWL6040_REG_MICLCTL
, 2, 0),
1124 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1125 TWL6040_REG_MICRCTL
, 2, 0),
1127 /* Microphone bias */
1128 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1129 TWL6040_REG_AMICBCTL
, 0, 0),
1130 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1131 TWL6040_REG_AMICBCTL
, 4, 0),
1132 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1133 TWL6040_REG_DMICBCTL
, 0, 0),
1134 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1135 TWL6040_REG_DMICBCTL
, 4, 0),
1138 SND_SOC_DAPM_DAC("HSDAC Left", "Headset Playback", SND_SOC_NOPM
, 0, 0),
1139 SND_SOC_DAPM_DAC("HSDAC Right", "Headset Playback", SND_SOC_NOPM
, 0, 0),
1140 SND_SOC_DAPM_DAC("HFDAC Left", "Handsfree Playback",
1141 TWL6040_REG_HFLCTL
, 0, 0),
1142 SND_SOC_DAPM_DAC("HFDAC Right", "Handsfree Playback",
1143 TWL6040_REG_HFRCTL
, 0, 0),
1144 /* Virtual DAC for vibra path (DL4 channel) */
1145 SND_SOC_DAPM_DAC("VIBRA DAC", "Vibra Playback",
1146 SND_SOC_NOPM
, 0, 0),
1148 SND_SOC_DAPM_MUX("Handsfree Left Playback",
1149 SND_SOC_NOPM
, 0, 0, &hfl_mux_controls
),
1150 SND_SOC_DAPM_MUX("Handsfree Right Playback",
1151 SND_SOC_NOPM
, 0, 0, &hfr_mux_controls
),
1152 /* Analog playback Muxes */
1153 SND_SOC_DAPM_MUX("Headset Left Playback",
1154 SND_SOC_NOPM
, 0, 0, &hsl_mux_controls
),
1155 SND_SOC_DAPM_MUX("Headset Right Playback",
1156 SND_SOC_NOPM
, 0, 0, &hsr_mux_controls
),
1158 SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM
, 0, 0,
1159 &vibral_mux_controls
),
1160 SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM
, 0, 0,
1161 &vibrar_mux_controls
),
1163 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM
, 0, 0,
1164 &ep_path_enable_control
),
1165 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM
, 0, 0,
1166 &auxl_switch_control
),
1167 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM
, 0, 0,
1168 &auxr_switch_control
),
1170 /* Analog playback drivers */
1171 SND_SOC_DAPM_OUT_DRV_E("HF Left Driver",
1172 TWL6040_REG_HFLCTL
, 4, 0, NULL
, 0,
1174 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1175 SND_SOC_DAPM_OUT_DRV_E("HF Right Driver",
1176 TWL6040_REG_HFRCTL
, 4, 0, NULL
, 0,
1178 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1179 SND_SOC_DAPM_OUT_DRV_E("HS Left Driver",
1180 TWL6040_REG_HSLCTL
, 2, 0, NULL
, 0,
1182 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1183 SND_SOC_DAPM_OUT_DRV_E("HS Right Driver",
1184 TWL6040_REG_HSRCTL
, 2, 0, NULL
, 0,
1186 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1187 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
1188 TWL6040_REG_EARCTL
, 0, 0, NULL
, 0,
1189 twl6040_ep_drv_event
,
1190 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
1191 SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
1192 TWL6040_REG_VIBCTLL
, 0, 0, NULL
, 0),
1193 SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
1194 TWL6040_REG_VIBCTLR
, 0, 0, NULL
, 0),
1196 SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL
, 2, 0,
1198 SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR
, 2, 0,
1200 SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM
, 0, 0,
1201 twl6040_hs_dac_event
,
1202 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
1204 /* Analog playback PGAs */
1205 SND_SOC_DAPM_PGA("HF Left PGA",
1206 TWL6040_REG_HFLCTL
, 1, 0, NULL
, 0),
1207 SND_SOC_DAPM_PGA("HF Right PGA",
1208 TWL6040_REG_HFRCTL
, 1, 0, NULL
, 0),
1212 static const struct snd_soc_dapm_route intercon
[] = {
1214 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1215 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1216 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1218 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1219 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1220 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1222 {"MicAmpL", NULL
, "Analog Left Capture Route"},
1223 {"MicAmpR", NULL
, "Analog Right Capture Route"},
1225 {"ADC Left", NULL
, "MicAmpL"},
1226 {"ADC Right", NULL
, "MicAmpR"},
1229 {"AFMAmpL", NULL
, "AFML"},
1230 {"AFMAmpR", NULL
, "AFMR"},
1232 {"HSDAC Left", NULL
, "HSDAC Power"},
1233 {"HSDAC Right", NULL
, "HSDAC Power"},
1235 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
1236 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
1238 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
1239 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
1241 {"HS Left Driver", NULL
, "Headset Left Playback"},
1242 {"HS Right Driver", NULL
, "Headset Right Playback"},
1244 {"HSOL", NULL
, "HS Left Driver"},
1245 {"HSOR", NULL
, "HS Right Driver"},
1247 /* Earphone playback path */
1248 {"Earphone Playback", "Switch", "HSDAC Left"},
1249 {"Earphone Driver", NULL
, "Earphone Playback"},
1250 {"EP", NULL
, "Earphone Driver"},
1252 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
1253 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
1255 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
1256 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
1258 {"HF Left PGA", NULL
, "Handsfree Left Playback"},
1259 {"HF Right PGA", NULL
, "Handsfree Right Playback"},
1261 {"HF Left Driver", NULL
, "HF Left PGA"},
1262 {"HF Right Driver", NULL
, "HF Right PGA"},
1264 {"HFL", NULL
, "HF Left Driver"},
1265 {"HFR", NULL
, "HF Right Driver"},
1267 {"AUXL Playback", "Switch", "HF Left PGA"},
1268 {"AUXR Playback", "Switch", "HF Right PGA"},
1270 {"AUXL", NULL
, "AUXL Playback"},
1271 {"AUXR", NULL
, "AUXR Playback"},
1273 /* Vibrator paths */
1274 {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
1275 {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
1277 {"Vibra Left Driver", NULL
, "Vibra Left Playback"},
1278 {"Vibra Right Driver", NULL
, "Vibra Right Playback"},
1279 {"Vibra Left Driver", NULL
, "Vibra Left Control"},
1280 {"Vibra Right Driver", NULL
, "Vibra Right Control"},
1282 {"VIBRAL", NULL
, "Vibra Left Driver"},
1283 {"VIBRAR", NULL
, "Vibra Right Driver"},
1286 static int twl6040_set_bias_level(struct snd_soc_codec
*codec
,
1287 enum snd_soc_bias_level level
)
1289 struct twl6040
*twl6040
= codec
->control_data
;
1290 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1294 case SND_SOC_BIAS_ON
:
1296 case SND_SOC_BIAS_PREPARE
:
1298 case SND_SOC_BIAS_STANDBY
:
1299 if (priv
->codec_powered
)
1302 ret
= twl6040_power(twl6040
, 1);
1306 priv
->codec_powered
= 1;
1308 twl6040_restore_regs(codec
);
1310 /* Set external boost GPO */
1311 twl6040_write(codec
, TWL6040_REG_GPOCTL
, 0x02);
1313 case SND_SOC_BIAS_OFF
:
1314 if (!priv
->codec_powered
)
1317 twl6040_power(twl6040
, 0);
1318 priv
->codec_powered
= 0;
1322 codec
->dapm
.bias_level
= level
;
1327 static int twl6040_startup(struct snd_pcm_substream
*substream
,
1328 struct snd_soc_dai
*dai
)
1330 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1331 struct snd_soc_codec
*codec
= rtd
->codec
;
1332 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1334 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
1335 SNDRV_PCM_HW_PARAM_RATE
,
1336 &sysclk_constraints
[priv
->pll_power_mode
]);
1341 static int twl6040_hw_params(struct snd_pcm_substream
*substream
,
1342 struct snd_pcm_hw_params
*params
,
1343 struct snd_soc_dai
*dai
)
1345 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1346 struct snd_soc_codec
*codec
= rtd
->codec
;
1347 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1350 rate
= params_rate(params
);
1356 /* These rates are not supported when HPPLL is in use */
1357 if (unlikely(priv
->pll
== TWL6040_SYSCLK_SEL_HPPLL
)) {
1358 dev_err(codec
->dev
, "HPPLL does not support rate %d\n",
1362 priv
->sysclk
= 17640000;
1369 priv
->sysclk
= 19200000;
1372 dev_err(codec
->dev
, "unsupported rate %d\n", rate
);
1379 static int twl6040_prepare(struct snd_pcm_substream
*substream
,
1380 struct snd_soc_dai
*dai
)
1382 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1383 struct snd_soc_codec
*codec
= rtd
->codec
;
1384 struct twl6040
*twl6040
= codec
->control_data
;
1385 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1388 if (!priv
->sysclk
) {
1390 "no mclk configured, call set_sysclk() on init\n");
1394 ret
= twl6040_set_pll(twl6040
, priv
->pll
, priv
->clk_in
, priv
->sysclk
);
1396 dev_err(codec
->dev
, "Can not set PLL (%d)\n", ret
);
1403 static int twl6040_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1404 int clk_id
, unsigned int freq
, int dir
)
1406 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1407 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1410 case TWL6040_SYSCLK_SEL_LPPLL
:
1411 case TWL6040_SYSCLK_SEL_HPPLL
:
1413 priv
->clk_in
= freq
;
1416 dev_err(codec
->dev
, "unknown clk_id %d\n", clk_id
);
1423 static const struct snd_soc_dai_ops twl6040_dai_ops
= {
1424 .startup
= twl6040_startup
,
1425 .hw_params
= twl6040_hw_params
,
1426 .prepare
= twl6040_prepare
,
1427 .set_sysclk
= twl6040_set_dai_sysclk
,
1430 static struct snd_soc_dai_driver twl6040_dai
[] = {
1432 .name
= "twl6040-legacy",
1434 .stream_name
= "Playback",
1437 .rates
= TWL6040_RATES
,
1438 .formats
= TWL6040_FORMATS
,
1441 .stream_name
= "Capture",
1444 .rates
= TWL6040_RATES
,
1445 .formats
= TWL6040_FORMATS
,
1447 .ops
= &twl6040_dai_ops
,
1450 .name
= "twl6040-ul",
1452 .stream_name
= "Capture",
1455 .rates
= TWL6040_RATES
,
1456 .formats
= TWL6040_FORMATS
,
1458 .ops
= &twl6040_dai_ops
,
1461 .name
= "twl6040-dl1",
1463 .stream_name
= "Headset Playback",
1466 .rates
= TWL6040_RATES
,
1467 .formats
= TWL6040_FORMATS
,
1469 .ops
= &twl6040_dai_ops
,
1472 .name
= "twl6040-dl2",
1474 .stream_name
= "Handsfree Playback",
1477 .rates
= TWL6040_RATES
,
1478 .formats
= TWL6040_FORMATS
,
1480 .ops
= &twl6040_dai_ops
,
1483 .name
= "twl6040-vib",
1485 .stream_name
= "Vibra Playback",
1488 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
1489 .formats
= TWL6040_FORMATS
,
1491 .ops
= &twl6040_dai_ops
,
1496 static int twl6040_suspend(struct snd_soc_codec
*codec
)
1498 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1503 static int twl6040_resume(struct snd_soc_codec
*codec
)
1505 twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1506 twl6040_set_bias_level(codec
, codec
->dapm
.suspend_bias_level
);
1511 #define twl6040_suspend NULL
1512 #define twl6040_resume NULL
1515 static int twl6040_probe(struct snd_soc_codec
*codec
)
1517 struct twl6040_data
*priv
;
1518 struct twl4030_codec_data
*pdata
= dev_get_platdata(codec
->dev
);
1519 struct platform_device
*pdev
= container_of(codec
->dev
,
1520 struct platform_device
, dev
);
1523 priv
= kzalloc(sizeof(struct twl6040_data
), GFP_KERNEL
);
1526 snd_soc_codec_set_drvdata(codec
, priv
);
1528 priv
->codec
= codec
;
1529 codec
->control_data
= dev_get_drvdata(codec
->dev
->parent
);
1530 codec
->ignore_pmdown_time
= 1;
1532 if (pdata
&& pdata
->hs_left_step
&& pdata
->hs_right_step
) {
1533 priv
->hs_left_step
= pdata
->hs_left_step
;
1534 priv
->hs_right_step
= pdata
->hs_right_step
;
1536 priv
->hs_left_step
= 1;
1537 priv
->hs_right_step
= 1;
1540 if (pdata
&& pdata
->hf_left_step
&& pdata
->hf_right_step
) {
1541 priv
->hf_left_step
= pdata
->hf_left_step
;
1542 priv
->hf_right_step
= pdata
->hf_right_step
;
1544 priv
->hf_left_step
= 1;
1545 priv
->hf_right_step
= 1;
1548 priv
->plug_irq
= platform_get_irq(pdev
, 0);
1549 if (priv
->plug_irq
< 0) {
1550 dev_err(codec
->dev
, "invalid irq\n");
1555 priv
->workqueue
= alloc_workqueue("twl6040-codec", 0, 0);
1556 if (!priv
->workqueue
) {
1561 INIT_DELAYED_WORK(&priv
->hs_jack
.work
, twl6040_accessory_work
);
1562 INIT_DELAYED_WORK(&priv
->headset
.work
, twl6040_pga_hs_work
);
1563 INIT_DELAYED_WORK(&priv
->handsfree
.work
, twl6040_pga_hf_work
);
1565 mutex_init(&priv
->mutex
);
1567 init_completion(&priv
->headset
.ramp_done
);
1568 init_completion(&priv
->handsfree
.ramp_done
);
1570 ret
= request_threaded_irq(priv
->plug_irq
, NULL
, twl6040_audio_handler
,
1571 0, "twl6040_irq_plug", codec
);
1573 dev_err(codec
->dev
, "PLUG IRQ request failed: %d\n", ret
);
1577 twl6040_init_chip(codec
);
1579 /* power on device */
1580 ret
= twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1585 free_irq(priv
->plug_irq
, codec
);
1587 destroy_workqueue(priv
->workqueue
);
1593 static int twl6040_remove(struct snd_soc_codec
*codec
)
1595 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1597 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1598 free_irq(priv
->plug_irq
, codec
);
1599 destroy_workqueue(priv
->workqueue
);
1605 static struct snd_soc_codec_driver soc_codec_dev_twl6040
= {
1606 .probe
= twl6040_probe
,
1607 .remove
= twl6040_remove
,
1608 .suspend
= twl6040_suspend
,
1609 .resume
= twl6040_resume
,
1610 .read
= twl6040_read_reg_cache
,
1611 .write
= twl6040_write
,
1612 .set_bias_level
= twl6040_set_bias_level
,
1613 .reg_cache_size
= ARRAY_SIZE(twl6040_reg
),
1614 .reg_word_size
= sizeof(u8
),
1615 .reg_cache_default
= twl6040_reg
,
1617 .controls
= twl6040_snd_controls
,
1618 .num_controls
= ARRAY_SIZE(twl6040_snd_controls
),
1619 .dapm_widgets
= twl6040_dapm_widgets
,
1620 .num_dapm_widgets
= ARRAY_SIZE(twl6040_dapm_widgets
),
1621 .dapm_routes
= intercon
,
1622 .num_dapm_routes
= ARRAY_SIZE(intercon
),
1625 static int __devinit
twl6040_codec_probe(struct platform_device
*pdev
)
1627 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_twl6040
,
1628 twl6040_dai
, ARRAY_SIZE(twl6040_dai
));
1631 static int __devexit
twl6040_codec_remove(struct platform_device
*pdev
)
1633 snd_soc_unregister_codec(&pdev
->dev
);
1637 static struct platform_driver twl6040_codec_driver
= {
1639 .name
= "twl6040-codec",
1640 .owner
= THIS_MODULE
,
1642 .probe
= twl6040_codec_probe
,
1643 .remove
= __devexit_p(twl6040_codec_remove
),
1646 module_platform_driver(twl6040_codec_driver
);
1648 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1649 MODULE_AUTHOR("Misael Lopez Cruz");
1650 MODULE_LICENSE("GPL");