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/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 #define TWL6040_RAMP_NONE 0
50 #define TWL6040_RAMP_UP 1
51 #define TWL6040_RAMP_DOWN 2
53 #define TWL6040_HSL_VOL_MASK 0x0F
54 #define TWL6040_HSL_VOL_SHIFT 0
55 #define TWL6040_HSR_VOL_MASK 0xF0
56 #define TWL6040_HSR_VOL_SHIFT 4
57 #define TWL6040_HF_VOL_MASK 0x1F
58 #define TWL6040_HF_VOL_SHIFT 0
60 struct twl6040_output
{
66 unsigned int step_delay
;
69 struct completion ramp_done
;
72 struct twl6040_jack_data
{
73 struct snd_soc_jack
*jack
;
77 /* codec private data */
84 struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
85 struct twl6040_jack_data hs_jack
;
86 struct snd_soc_codec
*codec
;
87 struct workqueue_struct
*workqueue
;
88 struct delayed_work delayed_work
;
90 struct twl6040_output headset
;
91 struct twl6040_output handsfree
;
92 struct workqueue_struct
*hf_workqueue
;
93 struct workqueue_struct
*hs_workqueue
;
94 struct delayed_work hs_delayed_work
;
95 struct delayed_work hf_delayed_work
;
99 * twl6040 register cache & default register settings
101 static const u8 twl6040_reg
[TWL6040_CACHEREGNUM
] = {
102 0x00, /* not used 0x00 */
103 0x4B, /* TWL6040_ASICID (ro) 0x01 */
104 0x00, /* TWL6040_ASICREV (ro) 0x02 */
105 0x00, /* TWL6040_INTID 0x03 */
106 0x00, /* TWL6040_INTMR 0x04 */
107 0x00, /* TWL6040_NCPCTRL 0x05 */
108 0x00, /* TWL6040_LDOCTL 0x06 */
109 0x60, /* TWL6040_HPPLLCTL 0x07 */
110 0x00, /* TWL6040_LPPLLCTL 0x08 */
111 0x4A, /* TWL6040_LPPLLDIV 0x09 */
112 0x00, /* TWL6040_AMICBCTL 0x0A */
113 0x00, /* TWL6040_DMICBCTL 0x0B */
114 0x18, /* TWL6040_MICLCTL 0x0C - No input selected on Left Mic */
115 0x18, /* TWL6040_MICRCTL 0x0D - No input selected on Right Mic */
116 0x00, /* TWL6040_MICGAIN 0x0E */
117 0x1B, /* TWL6040_LINEGAIN 0x0F */
118 0x00, /* TWL6040_HSLCTL 0x10 */
119 0x00, /* TWL6040_HSRCTL 0x11 */
120 0x00, /* TWL6040_HSGAIN 0x12 */
121 0x00, /* TWL6040_EARCTL 0x13 */
122 0x00, /* TWL6040_HFLCTL 0x14 */
123 0x00, /* TWL6040_HFLGAIN 0x15 */
124 0x00, /* TWL6040_HFRCTL 0x16 */
125 0x00, /* TWL6040_HFRGAIN 0x17 */
126 0x00, /* TWL6040_VIBCTLL 0x18 */
127 0x00, /* TWL6040_VIBDATL 0x19 */
128 0x00, /* TWL6040_VIBCTLR 0x1A */
129 0x00, /* TWL6040_VIBDATR 0x1B */
130 0x00, /* TWL6040_HKCTL1 0x1C */
131 0x00, /* TWL6040_HKCTL2 0x1D */
132 0x00, /* TWL6040_GPOCTL 0x1E */
133 0x00, /* TWL6040_ALB 0x1F */
134 0x00, /* TWL6040_DLB 0x20 */
135 0x00, /* not used 0x21 */
136 0x00, /* not used 0x22 */
137 0x00, /* not used 0x23 */
138 0x00, /* not used 0x24 */
139 0x00, /* not used 0x25 */
140 0x00, /* not used 0x26 */
141 0x00, /* not used 0x27 */
142 0x00, /* TWL6040_TRIM1 0x28 */
143 0x00, /* TWL6040_TRIM2 0x29 */
144 0x00, /* TWL6040_TRIM3 0x2A */
145 0x00, /* TWL6040_HSOTRIM 0x2B */
146 0x00, /* TWL6040_HFOTRIM 0x2C */
147 0x09, /* TWL6040_ACCCTL 0x2D */
148 0x00, /* TWL6040_STATUS (ro) 0x2E */
152 * twl6040 vio/gnd registers:
153 * registers under vio/gnd supply can be accessed
154 * before the power-up sequence, after NRESPWRON goes high
156 static const int twl6040_vio_reg
[TWL6040_VIOREGNUM
] = {
163 TWL6040_REG_AMICBCTL
,
164 TWL6040_REG_DMICBCTL
,
178 * twl6040 vdd/vss registers:
179 * registers under vdd/vss supplies can only be accessed
180 * after the power-up sequence
182 static const int twl6040_vdd_reg
[TWL6040_VDDREGNUM
] = {
183 TWL6040_REG_HPPLLCTL
,
184 TWL6040_REG_LPPLLCTL
,
185 TWL6040_REG_LPPLLDIV
,
189 TWL6040_REG_LINEGAIN
,
207 * read twl6040 register cache
209 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec
*codec
,
212 u8
*cache
= codec
->reg_cache
;
214 if (reg
>= TWL6040_CACHEREGNUM
)
221 * write twl6040 register cache
223 static inline void twl6040_write_reg_cache(struct snd_soc_codec
*codec
,
226 u8
*cache
= codec
->reg_cache
;
228 if (reg
>= TWL6040_CACHEREGNUM
)
234 * read from twl6040 hardware register
236 static int twl6040_read_reg_volatile(struct snd_soc_codec
*codec
,
239 struct twl6040
*twl6040
= codec
->control_data
;
242 if (reg
>= TWL6040_CACHEREGNUM
)
245 value
= twl6040_reg_read(twl6040
, reg
);
246 twl6040_write_reg_cache(codec
, reg
, value
);
252 * write to the twl6040 register space
254 static int twl6040_write(struct snd_soc_codec
*codec
,
255 unsigned int reg
, unsigned int value
)
257 struct twl6040
*twl6040
= codec
->control_data
;
259 if (reg
>= TWL6040_CACHEREGNUM
)
262 twl6040_write_reg_cache(codec
, reg
, value
);
263 return twl6040_reg_write(twl6040
, reg
, value
);
266 static void twl6040_init_vio_regs(struct snd_soc_codec
*codec
)
268 u8
*cache
= codec
->reg_cache
;
271 for (i
= 0; i
< TWL6040_VIOREGNUM
; i
++) {
272 reg
= twl6040_vio_reg
[i
];
274 * skip read-only registers (ASICID, ASICREV, STATUS)
275 * and registers shared among MFD children
278 case TWL6040_REG_ASICID
:
279 case TWL6040_REG_ASICREV
:
280 case TWL6040_REG_INTID
:
281 case TWL6040_REG_INTMR
:
282 case TWL6040_REG_NCPCTL
:
283 case TWL6040_REG_LDOCTL
:
284 case TWL6040_REG_GPOCTL
:
285 case TWL6040_REG_ACCCTL
:
286 case TWL6040_REG_STATUS
:
291 twl6040_write(codec
, reg
, cache
[reg
]);
295 static void twl6040_init_vdd_regs(struct snd_soc_codec
*codec
)
297 u8
*cache
= codec
->reg_cache
;
300 for (i
= 0; i
< TWL6040_VDDREGNUM
; i
++) {
301 reg
= twl6040_vdd_reg
[i
];
302 /* skip vibra and PLL registers */
304 case TWL6040_REG_VIBCTLL
:
305 case TWL6040_REG_VIBDATL
:
306 case TWL6040_REG_VIBCTLR
:
307 case TWL6040_REG_VIBDATR
:
308 case TWL6040_REG_HPPLLCTL
:
309 case TWL6040_REG_LPPLLCTL
:
310 case TWL6040_REG_LPPLLDIV
:
316 twl6040_write(codec
, reg
, cache
[reg
]);
321 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
323 static inline int twl6040_hs_ramp_step(struct snd_soc_codec
*codec
,
324 unsigned int left_step
, unsigned int right_step
)
327 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
328 struct twl6040_output
*headset
= &priv
->headset
;
329 int left_complete
= 0, right_complete
= 0;
333 left_step
= (left_step
> 0xF) ? 0xF : left_step
;
334 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSGAIN
);
335 val
= (~reg
& TWL6040_HSL_VOL_MASK
);
337 if (headset
->ramp
== TWL6040_RAMP_UP
) {
339 if (val
< headset
->left_vol
) {
341 reg
&= ~TWL6040_HSL_VOL_MASK
;
342 twl6040_write(codec
, TWL6040_REG_HSGAIN
,
343 (reg
| (~val
& TWL6040_HSL_VOL_MASK
)));
347 } else if (headset
->ramp
== TWL6040_RAMP_DOWN
) {
351 reg
&= ~TWL6040_HSL_VOL_MASK
;
352 twl6040_write(codec
, TWL6040_REG_HSGAIN
, reg
|
353 (~val
& TWL6040_HSL_VOL_MASK
));
360 right_step
= (right_step
> 0xF) ? 0xF : right_step
;
361 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSGAIN
);
362 val
= (~reg
& TWL6040_HSR_VOL_MASK
) >> TWL6040_HSR_VOL_SHIFT
;
364 if (headset
->ramp
== TWL6040_RAMP_UP
) {
366 if (val
< headset
->right_vol
) {
368 reg
&= ~TWL6040_HSR_VOL_MASK
;
369 twl6040_write(codec
, TWL6040_REG_HSGAIN
,
370 (reg
| (~val
<< TWL6040_HSR_VOL_SHIFT
)));
374 } else if (headset
->ramp
== TWL6040_RAMP_DOWN
) {
378 reg
&= ~TWL6040_HSR_VOL_MASK
;
379 twl6040_write(codec
, TWL6040_REG_HSGAIN
,
380 reg
| (~val
<< TWL6040_HSR_VOL_SHIFT
));
386 return left_complete
& right_complete
;
390 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
392 static inline int twl6040_hf_ramp_step(struct snd_soc_codec
*codec
,
393 unsigned int left_step
, unsigned int right_step
)
395 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
396 struct twl6040_output
*handsfree
= &priv
->handsfree
;
397 int left_complete
= 0, right_complete
= 0;
401 left_step
= (left_step
> 0x1D) ? 0x1D : left_step
;
402 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HFLGAIN
);
404 val
= (reg
& TWL6040_HF_VOL_MASK
);
405 if (handsfree
->ramp
== TWL6040_RAMP_UP
) {
407 if (val
< handsfree
->left_vol
) {
409 reg
&= ~TWL6040_HF_VOL_MASK
;
410 twl6040_write(codec
, TWL6040_REG_HFLGAIN
,
415 } else if (handsfree
->ramp
== TWL6040_RAMP_DOWN
) {
419 reg
&= ~TWL6040_HF_VOL_MASK
;
420 twl6040_write(codec
, TWL6040_REG_HFLGAIN
,
428 right_step
= (right_step
> 0x1D) ? 0x1D : right_step
;
429 reg
= twl6040_read_reg_cache(codec
, TWL6040_REG_HFRGAIN
);
431 val
= (reg
& TWL6040_HF_VOL_MASK
);
432 if (handsfree
->ramp
== TWL6040_RAMP_UP
) {
434 if (val
< handsfree
->right_vol
) {
436 reg
&= ~TWL6040_HF_VOL_MASK
;
437 twl6040_write(codec
, TWL6040_REG_HFRGAIN
,
442 } else if (handsfree
->ramp
== TWL6040_RAMP_DOWN
) {
446 reg
&= ~TWL6040_HF_VOL_MASK
;
447 twl6040_write(codec
, TWL6040_REG_HFRGAIN
,
452 return left_complete
& right_complete
;
456 * This work ramps both output PGAs at stream start/stop time to
457 * minimise pop associated with DAPM power switching.
459 static void twl6040_pga_hs_work(struct work_struct
*work
)
461 struct twl6040_data
*priv
=
462 container_of(work
, struct twl6040_data
, hs_delayed_work
.work
);
463 struct snd_soc_codec
*codec
= priv
->codec
;
464 struct twl6040_output
*headset
= &priv
->headset
;
465 unsigned int delay
= headset
->step_delay
;
466 int i
, headset_complete
;
468 /* do we need to ramp at all ? */
469 if (headset
->ramp
== TWL6040_RAMP_NONE
)
472 /* HS PGA volumes have 4 bits of resolution to ramp */
473 for (i
= 0; i
<= 16; i
++) {
474 headset_complete
= 1;
475 if (headset
->ramp
!= TWL6040_RAMP_NONE
)
476 headset_complete
= twl6040_hs_ramp_step(codec
,
478 headset
->right_step
);
480 /* ramp finished ? */
481 if (headset_complete
)
485 * TODO: tune: delay is longer over 0dB
486 * as increases are larger.
489 schedule_timeout_interruptible(msecs_to_jiffies(delay
+
492 schedule_timeout_interruptible(msecs_to_jiffies(delay
));
495 if (headset
->ramp
== TWL6040_RAMP_DOWN
) {
497 complete(&headset
->ramp_done
);
501 headset
->ramp
= TWL6040_RAMP_NONE
;
504 static void twl6040_pga_hf_work(struct work_struct
*work
)
506 struct twl6040_data
*priv
=
507 container_of(work
, struct twl6040_data
, hf_delayed_work
.work
);
508 struct snd_soc_codec
*codec
= priv
->codec
;
509 struct twl6040_output
*handsfree
= &priv
->handsfree
;
510 unsigned int delay
= handsfree
->step_delay
;
511 int i
, handsfree_complete
;
513 /* do we need to ramp at all ? */
514 if (handsfree
->ramp
== TWL6040_RAMP_NONE
)
517 /* HF PGA volumes have 5 bits of resolution to ramp */
518 for (i
= 0; i
<= 32; i
++) {
519 handsfree_complete
= 1;
520 if (handsfree
->ramp
!= TWL6040_RAMP_NONE
)
521 handsfree_complete
= twl6040_hf_ramp_step(codec
,
522 handsfree
->left_step
,
523 handsfree
->right_step
);
525 /* ramp finished ? */
526 if (handsfree_complete
)
530 * TODO: tune: delay is longer over 0dB
531 * as increases are larger.
534 schedule_timeout_interruptible(msecs_to_jiffies(delay
+
537 schedule_timeout_interruptible(msecs_to_jiffies(delay
));
541 if (handsfree
->ramp
== TWL6040_RAMP_DOWN
) {
542 handsfree
->active
= 0;
543 complete(&handsfree
->ramp_done
);
545 handsfree
->active
= 1;
546 handsfree
->ramp
= TWL6040_RAMP_NONE
;
549 static int pga_event(struct snd_soc_dapm_widget
*w
,
550 struct snd_kcontrol
*kcontrol
, int event
)
552 struct snd_soc_codec
*codec
= w
->codec
;
553 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
554 struct twl6040_output
*out
;
555 struct delayed_work
*work
;
556 struct workqueue_struct
*queue
;
561 out
= &priv
->headset
;
562 work
= &priv
->hs_delayed_work
;
563 queue
= priv
->hs_workqueue
;
564 out
->step_delay
= 5; /* 5 ms between volume ramp steps */
567 out
= &priv
->handsfree
;
568 work
= &priv
->hf_delayed_work
;
569 queue
= priv
->hf_workqueue
;
570 out
->step_delay
= 5; /* 5 ms between volume ramp steps */
571 if (SND_SOC_DAPM_EVENT_ON(event
))
581 case SND_SOC_DAPM_POST_PMU
:
585 /* don't use volume ramp for power-up */
586 out
->left_step
= out
->left_vol
;
587 out
->right_step
= out
->right_vol
;
589 if (!delayed_work_pending(work
)) {
590 out
->ramp
= TWL6040_RAMP_UP
;
591 queue_delayed_work(queue
, work
,
592 msecs_to_jiffies(1));
596 case SND_SOC_DAPM_PRE_PMD
:
600 if (!delayed_work_pending(work
)) {
601 /* use volume ramp for power-down */
604 out
->ramp
= TWL6040_RAMP_DOWN
;
605 INIT_COMPLETION(out
->ramp_done
);
607 queue_delayed_work(queue
, work
,
608 msecs_to_jiffies(1));
610 wait_for_completion_timeout(&out
->ramp_done
,
611 msecs_to_jiffies(2000));
619 /* set headset dac and driver power mode */
620 static int headset_power_mode(struct snd_soc_codec
*codec
, int high_perf
)
623 int mask
= TWL6040_HSDRVMODEL
| TWL6040_HSDACMODEL
;
625 hslctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSLCTL
);
626 hsrctl
= twl6040_read_reg_cache(codec
, TWL6040_REG_HSRCTL
);
636 twl6040_write(codec
, TWL6040_REG_HSLCTL
, hslctl
);
637 twl6040_write(codec
, TWL6040_REG_HSRCTL
, hsrctl
);
642 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget
*w
,
643 struct snd_kcontrol
*kcontrol
, int event
)
649 static int twl6040_power_mode_event(struct snd_soc_dapm_widget
*w
,
650 struct snd_kcontrol
*kcontrol
, int event
)
652 struct snd_soc_codec
*codec
= w
->codec
;
653 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
655 if (SND_SOC_DAPM_EVENT_ON(event
))
665 static void twl6040_hs_jack_report(struct snd_soc_codec
*codec
,
666 struct snd_soc_jack
*jack
, int report
)
668 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
671 mutex_lock(&priv
->mutex
);
674 status
= twl6040_read_reg_volatile(codec
, TWL6040_REG_STATUS
);
675 if (status
& TWL6040_PLUGCOMP
)
676 snd_soc_jack_report(jack
, report
, report
);
678 snd_soc_jack_report(jack
, 0, report
);
680 mutex_unlock(&priv
->mutex
);
683 void twl6040_hs_jack_detect(struct snd_soc_codec
*codec
,
684 struct snd_soc_jack
*jack
, int report
)
686 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
687 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
689 hs_jack
->jack
= jack
;
690 hs_jack
->report
= report
;
692 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
694 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect
);
696 static void twl6040_accessory_work(struct work_struct
*work
)
698 struct twl6040_data
*priv
= container_of(work
,
699 struct twl6040_data
, delayed_work
.work
);
700 struct snd_soc_codec
*codec
= priv
->codec
;
701 struct twl6040_jack_data
*hs_jack
= &priv
->hs_jack
;
703 twl6040_hs_jack_report(codec
, hs_jack
->jack
, hs_jack
->report
);
706 /* audio interrupt handler */
707 static irqreturn_t
twl6040_audio_handler(int irq
, void *data
)
709 struct snd_soc_codec
*codec
= data
;
710 struct twl6040
*twl6040
= codec
->control_data
;
711 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
714 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
716 if ((intid
& TWL6040_PLUGINT
) || (intid
& TWL6040_UNPLUGINT
))
717 queue_delayed_work(priv
->workqueue
, &priv
->delayed_work
,
718 msecs_to_jiffies(200));
723 static int twl6040_put_volsw(struct snd_kcontrol
*kcontrol
,
724 struct snd_ctl_elem_value
*ucontrol
)
726 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
727 struct twl6040_data
*twl6040_priv
= snd_soc_codec_get_drvdata(codec
);
728 struct twl6040_output
*out
= NULL
;
729 struct soc_mixer_control
*mc
=
730 (struct soc_mixer_control
*)kcontrol
->private_value
;
732 unsigned int reg
= mc
->reg
;
734 /* For HS and HF we shadow the values and only actually write
735 * them out when active in order to ensure the amplifier comes on
736 * as quietly as possible. */
738 case TWL6040_REG_HSGAIN
:
739 out
= &twl6040_priv
->headset
;
746 out
->left_vol
= ucontrol
->value
.integer
.value
[0];
747 out
->right_vol
= ucontrol
->value
.integer
.value
[1];
752 ret
= snd_soc_put_volsw(kcontrol
, ucontrol
);
759 static int twl6040_get_volsw(struct snd_kcontrol
*kcontrol
,
760 struct snd_ctl_elem_value
*ucontrol
)
762 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
763 struct twl6040_data
*twl6040_priv
= snd_soc_codec_get_drvdata(codec
);
764 struct twl6040_output
*out
= &twl6040_priv
->headset
;
765 struct soc_mixer_control
*mc
=
766 (struct soc_mixer_control
*)kcontrol
->private_value
;
767 unsigned int reg
= mc
->reg
;
770 case TWL6040_REG_HSGAIN
:
771 out
= &twl6040_priv
->headset
;
772 ucontrol
->value
.integer
.value
[0] = out
->left_vol
;
773 ucontrol
->value
.integer
.value
[1] = out
->right_vol
;
780 return snd_soc_get_volsw(kcontrol
, ucontrol
);
783 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol
*kcontrol
,
784 struct snd_ctl_elem_value
*ucontrol
)
786 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
787 struct twl6040_data
*twl6040_priv
= snd_soc_codec_get_drvdata(codec
);
788 struct twl6040_output
*out
= NULL
;
789 struct soc_mixer_control
*mc
=
790 (struct soc_mixer_control
*)kcontrol
->private_value
;
792 unsigned int reg
= mc
->reg
;
794 /* For HS and HF we shadow the values and only actually write
795 * them out when active in order to ensure the amplifier comes on
796 * as quietly as possible. */
798 case TWL6040_REG_HFLGAIN
:
799 case TWL6040_REG_HFRGAIN
:
800 out
= &twl6040_priv
->handsfree
;
807 out
->left_vol
= ucontrol
->value
.integer
.value
[0];
808 out
->right_vol
= ucontrol
->value
.integer
.value
[1];
813 ret
= snd_soc_put_volsw_2r(kcontrol
, ucontrol
);
820 static int twl6040_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
821 struct snd_ctl_elem_value
*ucontrol
)
823 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
824 struct twl6040_data
*twl6040_priv
= snd_soc_codec_get_drvdata(codec
);
825 struct twl6040_output
*out
= &twl6040_priv
->handsfree
;
826 struct soc_mixer_control
*mc
=
827 (struct soc_mixer_control
*)kcontrol
->private_value
;
828 unsigned int reg
= mc
->reg
;
830 /* If these are cached registers use the cache */
832 case TWL6040_REG_HFLGAIN
:
833 case TWL6040_REG_HFRGAIN
:
834 out
= &twl6040_priv
->handsfree
;
835 ucontrol
->value
.integer
.value
[0] = out
->left_vol
;
836 ucontrol
->value
.integer
.value
[1] = out
->right_vol
;
843 return snd_soc_get_volsw_2r(kcontrol
, ucontrol
);
846 /* double control with volume update */
847 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
849 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
850 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
851 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
852 .tlv.p = (tlv_array), \
853 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
854 .put = twl6040_put_volsw, \
855 .private_value = (unsigned long)&(struct soc_mixer_control) \
856 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
857 .max = xmax, .platform_max = xmax, .invert = xinvert} }
859 /* double control with volume update */
860 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
862 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
863 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
864 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
865 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
866 .tlv.p = (tlv_array), \
867 .info = snd_soc_info_volsw_2r, \
868 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
869 .private_value = (unsigned long)&(struct soc_mixer_control) \
870 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
871 .rshift = xshift, .max = xmax, .invert = xinvert}, }
874 * MICATT volume control:
875 * from -6 to 0 dB in 6 dB steps
877 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv
, -600, 600, 0);
880 * MICGAIN volume control:
881 * from -6 to 30 dB in 6 dB steps
883 static DECLARE_TLV_DB_SCALE(mic_amp_tlv
, -600, 600, 0);
886 * AFMGAIN volume control:
887 * from -18 to 24 dB in 6 dB steps
889 static DECLARE_TLV_DB_SCALE(afm_amp_tlv
, -1800, 600, 0);
892 * HSGAIN volume control:
893 * from -30 to 0 dB in 2 dB steps
895 static DECLARE_TLV_DB_SCALE(hs_tlv
, -3000, 200, 0);
898 * HFGAIN volume control:
899 * from -52 to 6 dB in 2 dB steps
901 static DECLARE_TLV_DB_SCALE(hf_tlv
, -5200, 200, 0);
904 * EPGAIN volume control:
905 * from -24 to 6 dB in 2 dB steps
907 static DECLARE_TLV_DB_SCALE(ep_tlv
, -2400, 200, 0);
909 /* Left analog microphone selection */
910 static const char *twl6040_amicl_texts
[] =
911 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
913 /* Right analog microphone selection */
914 static const char *twl6040_amicr_texts
[] =
915 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
917 static const struct soc_enum twl6040_enum
[] = {
918 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL
, 3, 4, twl6040_amicl_texts
),
919 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL
, 3, 4, twl6040_amicr_texts
),
922 static const char *twl6040_hs_texts
[] = {
923 "Off", "HS DAC", "Line-In amp"
926 static const struct soc_enum twl6040_hs_enum
[] = {
927 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
929 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL
, 5, ARRAY_SIZE(twl6040_hs_texts
),
933 static const char *twl6040_hf_texts
[] = {
934 "Off", "HF DAC", "Line-In amp"
937 static const struct soc_enum twl6040_hf_enum
[] = {
938 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
940 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL
, 2, ARRAY_SIZE(twl6040_hf_texts
),
944 static const struct snd_kcontrol_new amicl_control
=
945 SOC_DAPM_ENUM("Route", twl6040_enum
[0]);
947 static const struct snd_kcontrol_new amicr_control
=
948 SOC_DAPM_ENUM("Route", twl6040_enum
[1]);
950 /* Headset DAC playback switches */
951 static const struct snd_kcontrol_new hsl_mux_controls
=
952 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[0]);
954 static const struct snd_kcontrol_new hsr_mux_controls
=
955 SOC_DAPM_ENUM("Route", twl6040_hs_enum
[1]);
957 /* Handsfree DAC playback switches */
958 static const struct snd_kcontrol_new hfl_mux_controls
=
959 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[0]);
961 static const struct snd_kcontrol_new hfr_mux_controls
=
962 SOC_DAPM_ENUM("Route", twl6040_hf_enum
[1]);
964 static const struct snd_kcontrol_new ep_driver_switch_controls
=
965 SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL
, 0, 1, 0);
967 static const struct snd_kcontrol_new twl6040_snd_controls
[] = {
969 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
970 TWL6040_REG_MICGAIN
, 6, 7, 1, 1, mic_preamp_tlv
),
971 SOC_DOUBLE_TLV("Capture Volume",
972 TWL6040_REG_MICGAIN
, 0, 3, 4, 0, mic_amp_tlv
),
975 SOC_DOUBLE_TLV("Aux FM Volume",
976 TWL6040_REG_LINEGAIN
, 0, 3, 7, 0, afm_amp_tlv
),
979 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
980 TWL6040_REG_HSGAIN
, 0, 4, 0xF, 1, hs_tlv
),
981 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
982 TWL6040_REG_HFLGAIN
, TWL6040_REG_HFRGAIN
, 0, 0x1D, 1, hf_tlv
),
983 SOC_SINGLE_TLV("Earphone Playback Volume",
984 TWL6040_REG_EARCTL
, 1, 0xF, 1, ep_tlv
),
987 static const struct snd_soc_dapm_widget twl6040_dapm_widgets
[] = {
989 SND_SOC_DAPM_INPUT("MAINMIC"),
990 SND_SOC_DAPM_INPUT("HSMIC"),
991 SND_SOC_DAPM_INPUT("SUBMIC"),
992 SND_SOC_DAPM_INPUT("AFML"),
993 SND_SOC_DAPM_INPUT("AFMR"),
996 SND_SOC_DAPM_OUTPUT("HSOL"),
997 SND_SOC_DAPM_OUTPUT("HSOR"),
998 SND_SOC_DAPM_OUTPUT("HFL"),
999 SND_SOC_DAPM_OUTPUT("HFR"),
1000 SND_SOC_DAPM_OUTPUT("EP"),
1002 /* Analog input muxes for the capture amplifiers */
1003 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1004 SND_SOC_NOPM
, 0, 0, &amicl_control
),
1005 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1006 SND_SOC_NOPM
, 0, 0, &amicr_control
),
1008 /* Analog capture PGAs */
1009 SND_SOC_DAPM_PGA("MicAmpL",
1010 TWL6040_REG_MICLCTL
, 0, 0, NULL
, 0),
1011 SND_SOC_DAPM_PGA("MicAmpR",
1012 TWL6040_REG_MICRCTL
, 0, 0, NULL
, 0),
1014 /* Auxiliary FM PGAs */
1015 SND_SOC_DAPM_PGA("AFMAmpL",
1016 TWL6040_REG_MICLCTL
, 1, 0, NULL
, 0),
1017 SND_SOC_DAPM_PGA("AFMAmpR",
1018 TWL6040_REG_MICRCTL
, 1, 0, NULL
, 0),
1021 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1022 TWL6040_REG_MICLCTL
, 2, 0),
1023 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1024 TWL6040_REG_MICRCTL
, 2, 0),
1026 /* Microphone bias */
1027 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1028 TWL6040_REG_AMICBCTL
, 0, 0),
1029 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1030 TWL6040_REG_AMICBCTL
, 4, 0),
1031 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1032 TWL6040_REG_DMICBCTL
, 0, 0),
1033 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1034 TWL6040_REG_DMICBCTL
, 4, 0),
1037 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1038 TWL6040_REG_HSLCTL
, 0, 0,
1039 twl6040_hs_dac_event
,
1040 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
1041 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1042 TWL6040_REG_HSRCTL
, 0, 0,
1043 twl6040_hs_dac_event
,
1044 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
1045 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1046 TWL6040_REG_HFLCTL
, 0, 0,
1047 twl6040_power_mode_event
,
1048 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
1049 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1050 TWL6040_REG_HFRCTL
, 0, 0,
1051 twl6040_power_mode_event
,
1052 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
1054 SND_SOC_DAPM_MUX("HF Left Playback",
1055 SND_SOC_NOPM
, 0, 0, &hfl_mux_controls
),
1056 SND_SOC_DAPM_MUX("HF Right Playback",
1057 SND_SOC_NOPM
, 0, 0, &hfr_mux_controls
),
1058 /* Analog playback Muxes */
1059 SND_SOC_DAPM_MUX("HS Left Playback",
1060 SND_SOC_NOPM
, 0, 0, &hsl_mux_controls
),
1061 SND_SOC_DAPM_MUX("HS Right Playback",
1062 SND_SOC_NOPM
, 0, 0, &hsr_mux_controls
),
1064 /* Analog playback drivers */
1065 SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver",
1066 TWL6040_REG_HFLCTL
, 4, 0, NULL
, 0,
1068 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1069 SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver",
1070 TWL6040_REG_HFRCTL
, 4, 0, NULL
, 0,
1072 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1073 SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver",
1074 TWL6040_REG_HSLCTL
, 2, 0, NULL
, 0,
1076 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1077 SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver",
1078 TWL6040_REG_HSRCTL
, 2, 0, NULL
, 0,
1080 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1081 SND_SOC_DAPM_SWITCH_E("Earphone Driver",
1082 SND_SOC_NOPM
, 0, 0, &ep_driver_switch_controls
,
1083 twl6040_power_mode_event
,
1084 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
1086 /* Analog playback PGAs */
1087 SND_SOC_DAPM_PGA("HFDAC Left PGA",
1088 TWL6040_REG_HFLCTL
, 1, 0, NULL
, 0),
1089 SND_SOC_DAPM_PGA("HFDAC Right PGA",
1090 TWL6040_REG_HFRCTL
, 1, 0, NULL
, 0),
1094 static const struct snd_soc_dapm_route intercon
[] = {
1096 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1097 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1098 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1100 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1101 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1102 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1104 {"MicAmpL", NULL
, "Analog Left Capture Route"},
1105 {"MicAmpR", NULL
, "Analog Right Capture Route"},
1107 {"ADC Left", NULL
, "MicAmpL"},
1108 {"ADC Right", NULL
, "MicAmpR"},
1111 {"AFMAmpL", "NULL", "AFML"},
1112 {"AFMAmpR", "NULL", "AFMR"},
1114 {"HS Left Playback", "HS DAC", "HSDAC Left"},
1115 {"HS Left Playback", "Line-In amp", "AFMAmpL"},
1117 {"HS Right Playback", "HS DAC", "HSDAC Right"},
1118 {"HS Right Playback", "Line-In amp", "AFMAmpR"},
1120 {"Headset Left Driver", "NULL", "HS Left Playback"},
1121 {"Headset Right Driver", "NULL", "HS Right Playback"},
1123 {"HSOL", NULL
, "Headset Left Driver"},
1124 {"HSOR", NULL
, "Headset Right Driver"},
1126 /* Earphone playback path */
1127 {"Earphone Driver", "Switch", "HSDAC Left"},
1128 {"EP", NULL
, "Earphone Driver"},
1130 {"HF Left Playback", "HF DAC", "HFDAC Left"},
1131 {"HF Left Playback", "Line-In amp", "AFMAmpL"},
1133 {"HF Right Playback", "HF DAC", "HFDAC Right"},
1134 {"HF Right Playback", "Line-In amp", "AFMAmpR"},
1136 {"HFDAC Left PGA", NULL
, "HF Left Playback"},
1137 {"HFDAC Right PGA", NULL
, "HF Right Playback"},
1139 {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"},
1140 {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"},
1142 {"HFL", NULL
, "Handsfree Left Driver"},
1143 {"HFR", NULL
, "Handsfree Right Driver"},
1146 static int twl6040_add_widgets(struct snd_soc_codec
*codec
)
1148 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
1150 snd_soc_dapm_new_controls(dapm
, twl6040_dapm_widgets
,
1151 ARRAY_SIZE(twl6040_dapm_widgets
));
1152 snd_soc_dapm_add_routes(dapm
, intercon
, ARRAY_SIZE(intercon
));
1153 snd_soc_dapm_new_widgets(dapm
);
1158 static int twl6040_set_bias_level(struct snd_soc_codec
*codec
,
1159 enum snd_soc_bias_level level
)
1161 struct twl6040
*twl6040
= codec
->control_data
;
1162 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1166 case SND_SOC_BIAS_ON
:
1168 case SND_SOC_BIAS_PREPARE
:
1170 case SND_SOC_BIAS_STANDBY
:
1171 if (priv
->codec_powered
)
1174 ret
= twl6040_power(twl6040
, 1);
1178 priv
->codec_powered
= 1;
1180 /* initialize vdd/vss registers with reg_cache */
1181 twl6040_init_vdd_regs(codec
);
1183 /* Set external boost GPO */
1184 twl6040_write(codec
, TWL6040_REG_GPOCTL
, 0x02);
1186 case SND_SOC_BIAS_OFF
:
1187 if (!priv
->codec_powered
)
1190 twl6040_power(twl6040
, 0);
1191 priv
->codec_powered
= 0;
1195 /* get PLL and sysclk after power transition */
1196 priv
->pll
= twl6040_get_pll(twl6040
);
1197 priv
->sysclk
= twl6040_get_sysclk(twl6040
);
1198 codec
->dapm
.bias_level
= level
;
1203 /* set of rates for each pll: low-power and high-performance */
1205 static unsigned int lp_rates
[] = {
1210 static struct snd_pcm_hw_constraint_list lp_constraints
= {
1211 .count
= ARRAY_SIZE(lp_rates
),
1215 static unsigned int hp_rates
[] = {
1219 static struct snd_pcm_hw_constraint_list hp_constraints
= {
1220 .count
= ARRAY_SIZE(hp_rates
),
1224 static int twl6040_startup(struct snd_pcm_substream
*substream
,
1225 struct snd_soc_dai
*dai
)
1227 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1228 struct snd_soc_codec
*codec
= rtd
->codec
;
1229 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1231 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
1232 SNDRV_PCM_HW_PARAM_RATE
,
1233 priv
->sysclk_constraints
);
1238 static int twl6040_hw_params(struct snd_pcm_substream
*substream
,
1239 struct snd_pcm_hw_params
*params
,
1240 struct snd_soc_dai
*dai
)
1242 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1243 struct snd_soc_codec
*codec
= rtd
->codec
;
1244 struct twl6040
*twl6040
= codec
->control_data
;
1245 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1246 unsigned int sysclk
;
1249 /* nothing to do for high-perf pll, it supports only 48 kHz */
1250 if (priv
->pll
== TWL6040_HPPLL_ID
)
1253 rate
= params_rate(params
);
1269 dev_err(codec
->dev
, "unsupported rate %d\n", rate
);
1273 ret
= twl6040_set_pll(twl6040
, TWL6040_LPPLL_ID
, priv
->clk_in
, sysclk
);
1277 priv
->sysclk
= twl6040_get_sysclk(twl6040
);
1282 static int twl6040_prepare(struct snd_pcm_substream
*substream
,
1283 struct snd_soc_dai
*dai
)
1285 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1286 struct snd_soc_codec
*codec
= rtd
->codec
;
1287 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1289 if (!priv
->sysclk
) {
1291 "no mclk configured, call set_sysclk() on init\n");
1296 * capture is not supported at 17.64 MHz,
1297 * it's reserved for headset low-power playback scenario
1299 if ((priv
->sysclk
== 17640000) &&
1300 substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
1302 "capture mode is not supported at %dHz\n",
1307 if ((priv
->sysclk
== 17640000) && priv
->non_lp
) {
1309 "some enabled paths aren't supported at %dHz\n",
1316 static int twl6040_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1317 int clk_id
, unsigned int freq
, int dir
)
1319 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1320 struct twl6040
*twl6040
= codec
->control_data
;
1321 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1325 case TWL6040_SYSCLK_SEL_LPPLL
:
1326 ret
= twl6040_set_pll(twl6040
, TWL6040_LPPLL_ID
,
1327 freq
, priv
->sysclk
);
1331 headset_power_mode(codec
, 0);
1332 priv
->sysclk_constraints
= &lp_constraints
;
1334 case TWL6040_SYSCLK_SEL_HPPLL
:
1335 ret
= twl6040_set_pll(twl6040
, TWL6040_HPPLL_ID
,
1336 freq
, priv
->sysclk
);
1340 headset_power_mode(codec
, 1);
1341 priv
->sysclk_constraints
= &hp_constraints
;
1344 dev_err(codec
->dev
, "unknown clk_id %d\n", clk_id
);
1348 priv
->pll
= twl6040_get_pll(twl6040
);
1349 priv
->clk_in
= freq
;
1350 priv
->sysclk
= twl6040_get_sysclk(twl6040
);
1355 static struct snd_soc_dai_ops twl6040_dai_ops
= {
1356 .startup
= twl6040_startup
,
1357 .hw_params
= twl6040_hw_params
,
1358 .prepare
= twl6040_prepare
,
1359 .set_sysclk
= twl6040_set_dai_sysclk
,
1362 static struct snd_soc_dai_driver twl6040_dai
= {
1363 .name
= "twl6040-hifi",
1365 .stream_name
= "Playback",
1368 .rates
= TWL6040_RATES
,
1369 .formats
= TWL6040_FORMATS
,
1372 .stream_name
= "Capture",
1375 .rates
= TWL6040_RATES
,
1376 .formats
= TWL6040_FORMATS
,
1378 .ops
= &twl6040_dai_ops
,
1382 static int twl6040_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
1384 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1389 static int twl6040_resume(struct snd_soc_codec
*codec
)
1391 twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1392 twl6040_set_bias_level(codec
, codec
->dapm
.suspend_bias_level
);
1397 #define twl6040_suspend NULL
1398 #define twl6040_resume NULL
1401 static int twl6040_probe(struct snd_soc_codec
*codec
)
1403 struct twl6040_data
*priv
;
1406 priv
= kzalloc(sizeof(struct twl6040_data
), GFP_KERNEL
);
1409 snd_soc_codec_set_drvdata(codec
, priv
);
1411 priv
->codec
= codec
;
1412 codec
->control_data
= dev_get_drvdata(codec
->dev
->parent
);
1414 priv
->workqueue
= create_singlethread_workqueue("twl6040-codec");
1415 if (!priv
->workqueue
) {
1420 INIT_DELAYED_WORK(&priv
->delayed_work
, twl6040_accessory_work
);
1422 mutex_init(&priv
->mutex
);
1424 init_completion(&priv
->headset
.ramp_done
);
1425 init_completion(&priv
->handsfree
.ramp_done
);
1427 priv
->hf_workqueue
= create_singlethread_workqueue("twl6040-hf");
1428 if (priv
->hf_workqueue
== NULL
) {
1432 priv
->hs_workqueue
= create_singlethread_workqueue("twl6040-hs");
1433 if (priv
->hs_workqueue
== NULL
) {
1438 INIT_DELAYED_WORK(&priv
->hs_delayed_work
, twl6040_pga_hs_work
);
1439 INIT_DELAYED_WORK(&priv
->hf_delayed_work
, twl6040_pga_hf_work
);
1441 ret
= twl6040_request_irq(codec
->control_data
, TWL6040_IRQ_PLUG
,
1442 twl6040_audio_handler
, 0,
1443 "twl6040_irq_plug", codec
);
1445 dev_err(codec
->dev
, "PLUG IRQ request failed: %d\n", ret
);
1449 /* init vio registers */
1450 twl6040_init_vio_regs(codec
);
1452 /* power on device */
1453 ret
= twl6040_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1457 snd_soc_add_controls(codec
, twl6040_snd_controls
,
1458 ARRAY_SIZE(twl6040_snd_controls
));
1459 twl6040_add_widgets(codec
);
1464 twl6040_free_irq(codec
->control_data
, TWL6040_IRQ_PLUG
, codec
);
1466 destroy_workqueue(priv
->hs_workqueue
);
1468 destroy_workqueue(priv
->hf_workqueue
);
1470 destroy_workqueue(priv
->workqueue
);
1476 static int twl6040_remove(struct snd_soc_codec
*codec
)
1478 struct twl6040_data
*priv
= snd_soc_codec_get_drvdata(codec
);
1480 twl6040_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1481 twl6040_free_irq(codec
->control_data
, TWL6040_IRQ_PLUG
, codec
);
1482 destroy_workqueue(priv
->workqueue
);
1483 destroy_workqueue(priv
->hf_workqueue
);
1484 destroy_workqueue(priv
->hs_workqueue
);
1490 static struct snd_soc_codec_driver soc_codec_dev_twl6040
= {
1491 .probe
= twl6040_probe
,
1492 .remove
= twl6040_remove
,
1493 .suspend
= twl6040_suspend
,
1494 .resume
= twl6040_resume
,
1495 .read
= twl6040_read_reg_cache
,
1496 .write
= twl6040_write
,
1497 .set_bias_level
= twl6040_set_bias_level
,
1498 .reg_cache_size
= ARRAY_SIZE(twl6040_reg
),
1499 .reg_word_size
= sizeof(u8
),
1500 .reg_cache_default
= twl6040_reg
,
1503 static int __devinit
twl6040_codec_probe(struct platform_device
*pdev
)
1505 return snd_soc_register_codec(&pdev
->dev
,
1506 &soc_codec_dev_twl6040
, &twl6040_dai
, 1);
1509 static int __devexit
twl6040_codec_remove(struct platform_device
*pdev
)
1511 snd_soc_unregister_codec(&pdev
->dev
);
1515 static struct platform_driver twl6040_codec_driver
= {
1517 .name
= "twl6040-codec",
1518 .owner
= THIS_MODULE
,
1520 .probe
= twl6040_codec_probe
,
1521 .remove
= __devexit_p(twl6040_codec_remove
),
1524 static int __init
twl6040_codec_init(void)
1526 return platform_driver_register(&twl6040_codec_driver
);
1528 module_init(twl6040_codec_init
);
1530 static void __exit
twl6040_codec_exit(void)
1532 platform_driver_unregister(&twl6040_codec_driver
);
1534 module_exit(twl6040_codec_exit
);
1536 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1537 MODULE_AUTHOR("Misael Lopez Cruz");
1538 MODULE_LICENSE("GPL");