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 */
85 int hs_power_mode_locked;
92 struct twl6040_jack_data hs_jack;
93 struct snd_soc_codec *codec;
94 struct workqueue_struct *workqueue;
95 struct delayed_work delayed_work;
97 struct twl6040_output headset;
98 struct twl6040_output handsfree;
99 struct workqueue_struct *hf_workqueue;
100 struct workqueue_struct *hs_workqueue;
101 struct delayed_work hs_delayed_work;
102 struct delayed_work hf_delayed_work;
106 * twl6040 register cache & default register settings
108 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
109 0x00, /* not used 0x00 */
110 0x4B, /* TWL6040_ASICID (ro) 0x01 */
111 0x00, /* TWL6040_ASICREV (ro) 0x02 */
112 0x00, /* TWL6040_INTID 0x03 */
113 0x00, /* TWL6040_INTMR 0x04 */
114 0x00, /* TWL6040_NCPCTRL 0x05 */
115 0x00, /* TWL6040_LDOCTL 0x06 */
116 0x60, /* TWL6040_HPPLLCTL 0x07 */
117 0x00, /* TWL6040_LPPLLCTL 0x08 */
118 0x4A, /* TWL6040_LPPLLDIV 0x09 */
119 0x00, /* TWL6040_AMICBCTL 0x0A */
120 0x00, /* TWL6040_DMICBCTL 0x0B */
121 0x18, /* TWL6040_MICLCTL 0x0C - No input selected on Left Mic */
122 0x18, /* TWL6040_MICRCTL 0x0D - No input selected on Right Mic */
123 0x00, /* TWL6040_MICGAIN 0x0E */
124 0x1B, /* TWL6040_LINEGAIN 0x0F */
125 0x00, /* TWL6040_HSLCTL 0x10 */
126 0x00, /* TWL6040_HSRCTL 0x11 */
127 0x00, /* TWL6040_HSGAIN 0x12 */
128 0x00, /* TWL6040_EARCTL 0x13 */
129 0x00, /* TWL6040_HFLCTL 0x14 */
130 0x00, /* TWL6040_HFLGAIN 0x15 */
131 0x00, /* TWL6040_HFRCTL 0x16 */
132 0x00, /* TWL6040_HFRGAIN 0x17 */
133 0x00, /* TWL6040_VIBCTLL 0x18 */
134 0x00, /* TWL6040_VIBDATL 0x19 */
135 0x00, /* TWL6040_VIBCTLR 0x1A */
136 0x00, /* TWL6040_VIBDATR 0x1B */
137 0x00, /* TWL6040_HKCTL1 0x1C */
138 0x00, /* TWL6040_HKCTL2 0x1D */
139 0x00, /* TWL6040_GPOCTL 0x1E */
140 0x00, /* TWL6040_ALB 0x1F */
141 0x00, /* TWL6040_DLB 0x20 */
142 0x00, /* not used 0x21 */
143 0x00, /* not used 0x22 */
144 0x00, /* not used 0x23 */
145 0x00, /* not used 0x24 */
146 0x00, /* not used 0x25 */
147 0x00, /* not used 0x26 */
148 0x00, /* not used 0x27 */
149 0x00, /* TWL6040_TRIM1 0x28 */
150 0x00, /* TWL6040_TRIM2 0x29 */
151 0x00, /* TWL6040_TRIM3 0x2A */
152 0x00, /* TWL6040_HSOTRIM 0x2B */
153 0x00, /* TWL6040_HFOTRIM 0x2C */
154 0x09, /* TWL6040_ACCCTL 0x2D */
155 0x00, /* TWL6040_STATUS (ro) 0x2E */
159 * twl6040 vio/gnd registers:
160 * registers under vio/gnd supply can be accessed
161 * before the power-up sequence, after NRESPWRON goes high
163 static const int twl6040_vio_reg[TWL6040_VIOREGNUM] = {
170 TWL6040_REG_AMICBCTL,
171 TWL6040_REG_DMICBCTL,
185 * twl6040 vdd/vss registers:
186 * registers under vdd/vss supplies can only be accessed
187 * after the power-up sequence
189 static const int twl6040_vdd_reg[TWL6040_VDDREGNUM] = {
190 TWL6040_REG_HPPLLCTL,
191 TWL6040_REG_LPPLLCTL,
192 TWL6040_REG_LPPLLDIV,
196 TWL6040_REG_LINEGAIN,
213 /* set of rates for each pll: low-power and high-performance */
214 static unsigned int lp_rates[] = {
226 static unsigned int hp_rates[] = {
234 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
235 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
236 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
240 * read twl6040 register cache
242 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
245 u8 *cache = codec->reg_cache;
247 if (reg >= TWL6040_CACHEREGNUM)
254 * write twl6040 register cache
256 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
259 u8 *cache = codec->reg_cache;
261 if (reg >= TWL6040_CACHEREGNUM)
267 * read from twl6040 hardware register
269 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
272 struct twl6040 *twl6040 = codec->control_data;
275 if (reg >= TWL6040_CACHEREGNUM)
278 value = twl6040_reg_read(twl6040, reg);
279 twl6040_write_reg_cache(codec, reg, value);
285 * write to the twl6040 register space
287 static int twl6040_write(struct snd_soc_codec *codec,
288 unsigned int reg, unsigned int value)
290 struct twl6040 *twl6040 = codec->control_data;
292 if (reg >= TWL6040_CACHEREGNUM)
295 twl6040_write_reg_cache(codec, reg, value);
296 return twl6040_reg_write(twl6040, reg, value);
299 static void twl6040_init_vio_regs(struct snd_soc_codec *codec)
301 u8 *cache = codec->reg_cache;
304 for (i = 0; i < TWL6040_VIOREGNUM; i++) {
305 reg = twl6040_vio_reg[i];
307 * skip read-only registers (ASICID, ASICREV, STATUS)
308 * and registers shared among MFD children
311 case TWL6040_REG_ASICID:
312 case TWL6040_REG_ASICREV:
313 case TWL6040_REG_INTID:
314 case TWL6040_REG_INTMR:
315 case TWL6040_REG_NCPCTL:
316 case TWL6040_REG_LDOCTL:
317 case TWL6040_REG_GPOCTL:
318 case TWL6040_REG_ACCCTL:
319 case TWL6040_REG_STATUS:
324 twl6040_write(codec, reg, cache[reg]);
328 static void twl6040_init_vdd_regs(struct snd_soc_codec *codec)
330 u8 *cache = codec->reg_cache;
333 for (i = 0; i < TWL6040_VDDREGNUM; i++) {
334 reg = twl6040_vdd_reg[i];
335 /* skip vibra and PLL registers */
337 case TWL6040_REG_VIBCTLL:
338 case TWL6040_REG_VIBDATL:
339 case TWL6040_REG_VIBCTLR:
340 case TWL6040_REG_VIBDATR:
341 case TWL6040_REG_HPPLLCTL:
342 case TWL6040_REG_LPPLLCTL:
343 case TWL6040_REG_LPPLLDIV:
349 twl6040_write(codec, reg, cache[reg]);
354 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
356 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
357 unsigned int left_step, unsigned int right_step)
360 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
361 struct twl6040_output *headset = &priv->headset;
362 int left_complete = 0, right_complete = 0;
366 left_step = (left_step > 0xF) ? 0xF : left_step;
367 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
368 val = (~reg & TWL6040_HSL_VOL_MASK);
370 if (headset->ramp == TWL6040_RAMP_UP) {
372 if (val < headset->left_vol) {
373 if (val + left_step > headset->left_vol)
374 val = headset->left_vol;
378 reg &= ~TWL6040_HSL_VOL_MASK;
379 twl6040_write(codec, TWL6040_REG_HSGAIN,
380 (reg | (~val & TWL6040_HSL_VOL_MASK)));
384 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
387 if ((int)val - (int)left_step < 0)
392 reg &= ~TWL6040_HSL_VOL_MASK;
393 twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
394 (~val & TWL6040_HSL_VOL_MASK));
401 right_step = (right_step > 0xF) ? 0xF : right_step;
402 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
403 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
405 if (headset->ramp == TWL6040_RAMP_UP) {
407 if (val < headset->right_vol) {
408 if (val + right_step > headset->right_vol)
409 val = headset->right_vol;
413 reg &= ~TWL6040_HSR_VOL_MASK;
414 twl6040_write(codec, TWL6040_REG_HSGAIN,
415 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
419 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
422 if ((int)val - (int)right_step < 0)
427 reg &= ~TWL6040_HSR_VOL_MASK;
428 twl6040_write(codec, TWL6040_REG_HSGAIN,
429 reg | (~val << TWL6040_HSR_VOL_SHIFT));
435 return left_complete & right_complete;
439 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
441 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
442 unsigned int left_step, unsigned int right_step)
444 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
445 struct twl6040_output *handsfree = &priv->handsfree;
446 int left_complete = 0, right_complete = 0;
450 left_step = (left_step > 0x1D) ? 0x1D : left_step;
451 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
453 val = (reg & TWL6040_HF_VOL_MASK);
454 if (handsfree->ramp == TWL6040_RAMP_UP) {
456 if (val < handsfree->left_vol) {
457 if (val + left_step > handsfree->left_vol)
458 val = handsfree->left_vol;
462 reg &= ~TWL6040_HF_VOL_MASK;
463 twl6040_write(codec, TWL6040_REG_HFLGAIN,
468 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
471 if ((int)val - (int)left_step < 0)
476 reg &= ~TWL6040_HF_VOL_MASK;
477 twl6040_write(codec, TWL6040_REG_HFLGAIN,
485 right_step = (right_step > 0x1D) ? 0x1D : right_step;
486 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
488 val = (reg & TWL6040_HF_VOL_MASK);
489 if (handsfree->ramp == TWL6040_RAMP_UP) {
491 if (val < handsfree->right_vol) {
492 if (val + right_step > handsfree->right_vol)
493 val = handsfree->right_vol;
497 reg &= ~TWL6040_HF_VOL_MASK;
498 twl6040_write(codec, TWL6040_REG_HFRGAIN,
503 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
506 if ((int)val - (int)right_step < 0)
511 reg &= ~TWL6040_HF_VOL_MASK;
512 twl6040_write(codec, TWL6040_REG_HFRGAIN,
517 return left_complete & right_complete;
521 * This work ramps both output PGAs at stream start/stop time to
522 * minimise pop associated with DAPM power switching.
524 static void twl6040_pga_hs_work(struct work_struct *work)
526 struct twl6040_data *priv =
527 container_of(work, struct twl6040_data, hs_delayed_work.work);
528 struct snd_soc_codec *codec = priv->codec;
529 struct twl6040_output *headset = &priv->headset;
530 unsigned int delay = headset->step_delay;
531 int i, headset_complete;
533 /* do we need to ramp at all ? */
534 if (headset->ramp == TWL6040_RAMP_NONE)
537 /* HS PGA volumes have 4 bits of resolution to ramp */
538 for (i = 0; i <= 16; i++) {
539 headset_complete = twl6040_hs_ramp_step(codec,
541 headset->right_step);
543 /* ramp finished ? */
544 if (headset_complete)
548 * TODO: tune: delay is longer over 0dB
549 * as increases are larger.
552 schedule_timeout_interruptible(msecs_to_jiffies(delay +
555 schedule_timeout_interruptible(msecs_to_jiffies(delay));
558 if (headset->ramp == TWL6040_RAMP_DOWN) {
560 complete(&headset->ramp_done);
564 headset->ramp = TWL6040_RAMP_NONE;
567 static void twl6040_pga_hf_work(struct work_struct *work)
569 struct twl6040_data *priv =
570 container_of(work, struct twl6040_data, hf_delayed_work.work);
571 struct snd_soc_codec *codec = priv->codec;
572 struct twl6040_output *handsfree = &priv->handsfree;
573 unsigned int delay = handsfree->step_delay;
574 int i, handsfree_complete;
576 /* do we need to ramp at all ? */
577 if (handsfree->ramp == TWL6040_RAMP_NONE)
580 /* HF PGA volumes have 5 bits of resolution to ramp */
581 for (i = 0; i <= 32; i++) {
582 handsfree_complete = twl6040_hf_ramp_step(codec,
583 handsfree->left_step,
584 handsfree->right_step);
586 /* ramp finished ? */
587 if (handsfree_complete)
591 * TODO: tune: delay is longer over 0dB
592 * as increases are larger.
595 schedule_timeout_interruptible(msecs_to_jiffies(delay +
598 schedule_timeout_interruptible(msecs_to_jiffies(delay));
602 if (handsfree->ramp == TWL6040_RAMP_DOWN) {
603 handsfree->active = 0;
604 complete(&handsfree->ramp_done);
606 handsfree->active = 1;
607 handsfree->ramp = TWL6040_RAMP_NONE;
610 static int pga_event(struct snd_soc_dapm_widget *w,
611 struct snd_kcontrol *kcontrol, int event)
613 struct snd_soc_codec *codec = w->codec;
614 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
615 struct twl6040_output *out;
616 struct delayed_work *work;
617 struct workqueue_struct *queue;
622 out = &priv->headset;
623 work = &priv->hs_delayed_work;
624 queue = priv->hs_workqueue;
625 out->left_step = priv->hs_left_step;
626 out->right_step = priv->hs_right_step;
627 out->step_delay = 5; /* 5 ms between volume ramp steps */
630 out = &priv->handsfree;
631 work = &priv->hf_delayed_work;
632 queue = priv->hf_workqueue;
633 out->left_step = priv->hf_left_step;
634 out->right_step = priv->hf_right_step;
635 out->step_delay = 5; /* 5 ms between volume ramp steps */
636 if (SND_SOC_DAPM_EVENT_ON(event))
646 case SND_SOC_DAPM_POST_PMU:
650 /* don't use volume ramp for power-up */
651 out->left_step = out->left_vol;
652 out->right_step = out->right_vol;
654 if (!delayed_work_pending(work)) {
655 out->ramp = TWL6040_RAMP_UP;
656 queue_delayed_work(queue, work,
657 msecs_to_jiffies(1));
661 case SND_SOC_DAPM_PRE_PMD:
665 if (!delayed_work_pending(work)) {
666 /* use volume ramp for power-down */
667 out->ramp = TWL6040_RAMP_DOWN;
668 INIT_COMPLETION(out->ramp_done);
670 queue_delayed_work(queue, work,
671 msecs_to_jiffies(1));
673 wait_for_completion_timeout(&out->ramp_done,
674 msecs_to_jiffies(2000));
682 /* set headset dac and driver power mode */
683 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
686 int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL;
688 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
689 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
699 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
700 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
705 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
706 struct snd_kcontrol *kcontrol, int event)
712 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
713 struct snd_kcontrol *kcontrol, int event)
715 struct snd_soc_codec *codec = w->codec;
716 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
719 if (SND_SOC_DAPM_EVENT_ON(event)) {
721 if (!strcmp(w->name, "Earphone Driver")) {
722 /* Earphone doesn't support low power mode */
723 priv->hs_power_mode_locked = 1;
724 ret = headset_power_mode(codec, 1);
728 if (!strcmp(w->name, "Earphone Driver")) {
729 priv->hs_power_mode_locked = 0;
730 ret = headset_power_mode(codec, priv->hs_power_mode);
739 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
740 struct snd_soc_jack *jack, int report)
742 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
745 mutex_lock(&priv->mutex);
748 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
749 if (status & TWL6040_PLUGCOMP)
750 snd_soc_jack_report(jack, report, report);
752 snd_soc_jack_report(jack, 0, report);
754 mutex_unlock(&priv->mutex);
757 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
758 struct snd_soc_jack *jack, int report)
760 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
761 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
763 hs_jack->jack = jack;
764 hs_jack->report = report;
766 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
768 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
770 static void twl6040_accessory_work(struct work_struct *work)
772 struct twl6040_data *priv = container_of(work,
773 struct twl6040_data, delayed_work.work);
774 struct snd_soc_codec *codec = priv->codec;
775 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
777 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
780 /* audio interrupt handler */
781 static irqreturn_t twl6040_audio_handler(int irq, void *data)
783 struct snd_soc_codec *codec = data;
784 struct twl6040 *twl6040 = codec->control_data;
785 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
788 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
790 if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT))
791 queue_delayed_work(priv->workqueue, &priv->delayed_work,
792 msecs_to_jiffies(200));
797 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
798 struct snd_ctl_elem_value *ucontrol)
800 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
801 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
802 struct twl6040_output *out = NULL;
803 struct soc_mixer_control *mc =
804 (struct soc_mixer_control *)kcontrol->private_value;
806 unsigned int reg = mc->reg;
808 /* For HS and HF we shadow the values and only actually write
809 * them out when active in order to ensure the amplifier comes on
810 * as quietly as possible. */
812 case TWL6040_REG_HSGAIN:
813 out = &twl6040_priv->headset;
820 out->left_vol = ucontrol->value.integer.value[0];
821 out->right_vol = ucontrol->value.integer.value[1];
826 ret = snd_soc_put_volsw(kcontrol, ucontrol);
833 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
834 struct snd_ctl_elem_value *ucontrol)
836 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
837 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
838 struct twl6040_output *out = &twl6040_priv->headset;
839 struct soc_mixer_control *mc =
840 (struct soc_mixer_control *)kcontrol->private_value;
841 unsigned int reg = mc->reg;
844 case TWL6040_REG_HSGAIN:
845 out = &twl6040_priv->headset;
846 ucontrol->value.integer.value[0] = out->left_vol;
847 ucontrol->value.integer.value[1] = out->right_vol;
854 return snd_soc_get_volsw(kcontrol, ucontrol);
857 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
860 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
861 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
862 struct twl6040_output *out = NULL;
863 struct soc_mixer_control *mc =
864 (struct soc_mixer_control *)kcontrol->private_value;
866 unsigned int reg = mc->reg;
868 /* For HS and HF we shadow the values and only actually write
869 * them out when active in order to ensure the amplifier comes on
870 * as quietly as possible. */
872 case TWL6040_REG_HFLGAIN:
873 case TWL6040_REG_HFRGAIN:
874 out = &twl6040_priv->handsfree;
881 out->left_vol = ucontrol->value.integer.value[0];
882 out->right_vol = ucontrol->value.integer.value[1];
887 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
894 static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_value *ucontrol)
897 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
898 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
899 struct twl6040_output *out = &twl6040_priv->handsfree;
900 struct soc_mixer_control *mc =
901 (struct soc_mixer_control *)kcontrol->private_value;
902 unsigned int reg = mc->reg;
904 /* If these are cached registers use the cache */
906 case TWL6040_REG_HFLGAIN:
907 case TWL6040_REG_HFRGAIN:
908 out = &twl6040_priv->handsfree;
909 ucontrol->value.integer.value[0] = out->left_vol;
910 ucontrol->value.integer.value[1] = out->right_vol;
917 return snd_soc_get_volsw_2r(kcontrol, ucontrol);
920 /* double control with volume update */
921 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
923 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
924 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
925 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
926 .tlv.p = (tlv_array), \
927 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
928 .put = twl6040_put_volsw, \
929 .private_value = (unsigned long)&(struct soc_mixer_control) \
930 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
931 .max = xmax, .platform_max = xmax, .invert = xinvert} }
933 /* double control with volume update */
934 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
936 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
937 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
938 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
939 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
940 .tlv.p = (tlv_array), \
941 .info = snd_soc_info_volsw_2r, \
942 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
943 .private_value = (unsigned long)&(struct soc_mixer_control) \
944 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
945 .rshift = xshift, .max = xmax, .invert = xinvert}, }
948 * MICATT volume control:
949 * from -6 to 0 dB in 6 dB steps
951 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
954 * MICGAIN volume control:
955 * from 6 to 30 dB in 6 dB steps
957 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
960 * AFMGAIN volume control:
961 * from -18 to 24 dB in 6 dB steps
963 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
966 * HSGAIN volume control:
967 * from -30 to 0 dB in 2 dB steps
969 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
972 * HFGAIN volume control:
973 * from -52 to 6 dB in 2 dB steps
975 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
978 * EPGAIN volume control:
979 * from -24 to 6 dB in 2 dB steps
981 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
983 /* Left analog microphone selection */
984 static const char *twl6040_amicl_texts[] =
985 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
987 /* Right analog microphone selection */
988 static const char *twl6040_amicr_texts[] =
989 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
991 static const struct soc_enum twl6040_enum[] = {
992 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
993 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
996 static const char *twl6040_hs_texts[] = {
997 "Off", "HS DAC", "Line-In amp"
1000 static const struct soc_enum twl6040_hs_enum[] = {
1001 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
1003 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
1007 static const char *twl6040_hf_texts[] = {
1008 "Off", "HF DAC", "Line-In amp"
1011 static const struct soc_enum twl6040_hf_enum[] = {
1012 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
1014 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
1018 static const struct snd_kcontrol_new amicl_control =
1019 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
1021 static const struct snd_kcontrol_new amicr_control =
1022 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
1024 /* Headset DAC playback switches */
1025 static const struct snd_kcontrol_new hsl_mux_controls =
1026 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
1028 static const struct snd_kcontrol_new hsr_mux_controls =
1029 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
1031 /* Handsfree DAC playback switches */
1032 static const struct snd_kcontrol_new hfl_mux_controls =
1033 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
1035 static const struct snd_kcontrol_new hfr_mux_controls =
1036 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
1038 static const struct snd_kcontrol_new ep_driver_switch_controls =
1039 SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0);
1041 /* Headset power mode */
1042 static const char *twl6040_power_mode_texts[] = {
1043 "Low-Power", "High-Perfomance",
1046 static const struct soc_enum twl6040_power_mode_enum =
1047 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
1048 twl6040_power_mode_texts);
1050 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
1051 struct snd_ctl_elem_value *ucontrol)
1053 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1054 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1056 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
1061 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
1062 struct snd_ctl_elem_value *ucontrol)
1064 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1065 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1066 int high_perf = ucontrol->value.enumerated.item[0];
1069 if (!priv->hs_power_mode_locked)
1070 ret = headset_power_mode(codec, high_perf);
1073 priv->hs_power_mode = high_perf;
1078 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
1079 struct snd_ctl_elem_value *ucontrol)
1081 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1082 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1084 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1089 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1090 struct snd_ctl_elem_value *ucontrol)
1092 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1093 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1095 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1100 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1102 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1104 return priv->pll_power_mode;
1106 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1108 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1110 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1111 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1112 SOC_DOUBLE_TLV("Capture Volume",
1113 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1116 SOC_DOUBLE_TLV("Aux FM Volume",
1117 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1119 /* Playback gains */
1120 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
1121 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
1122 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
1123 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
1124 SOC_SINGLE_TLV("Earphone Playback Volume",
1125 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1127 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1128 twl6040_headset_power_get_enum,
1129 twl6040_headset_power_put_enum),
1131 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1132 twl6040_pll_get_enum, twl6040_pll_put_enum),
1135 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1137 SND_SOC_DAPM_INPUT("MAINMIC"),
1138 SND_SOC_DAPM_INPUT("HSMIC"),
1139 SND_SOC_DAPM_INPUT("SUBMIC"),
1140 SND_SOC_DAPM_INPUT("AFML"),
1141 SND_SOC_DAPM_INPUT("AFMR"),
1144 SND_SOC_DAPM_OUTPUT("HSOL"),
1145 SND_SOC_DAPM_OUTPUT("HSOR"),
1146 SND_SOC_DAPM_OUTPUT("HFL"),
1147 SND_SOC_DAPM_OUTPUT("HFR"),
1148 SND_SOC_DAPM_OUTPUT("EP"),
1150 /* Analog input muxes for the capture amplifiers */
1151 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1152 SND_SOC_NOPM, 0, 0, &amicl_control),
1153 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1154 SND_SOC_NOPM, 0, 0, &amicr_control),
1156 /* Analog capture PGAs */
1157 SND_SOC_DAPM_PGA("MicAmpL",
1158 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1159 SND_SOC_DAPM_PGA("MicAmpR",
1160 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1162 /* Auxiliary FM PGAs */
1163 SND_SOC_DAPM_PGA("AFMAmpL",
1164 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1165 SND_SOC_DAPM_PGA("AFMAmpR",
1166 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1169 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1170 TWL6040_REG_MICLCTL, 2, 0),
1171 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1172 TWL6040_REG_MICRCTL, 2, 0),
1174 /* Microphone bias */
1175 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1176 TWL6040_REG_AMICBCTL, 0, 0),
1177 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1178 TWL6040_REG_AMICBCTL, 4, 0),
1179 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1180 TWL6040_REG_DMICBCTL, 0, 0),
1181 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1182 TWL6040_REG_DMICBCTL, 4, 0),
1185 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1186 TWL6040_REG_HSLCTL, 0, 0,
1187 twl6040_hs_dac_event,
1188 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1189 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1190 TWL6040_REG_HSRCTL, 0, 0,
1191 twl6040_hs_dac_event,
1192 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1193 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1194 TWL6040_REG_HFLCTL, 0, 0,
1195 twl6040_power_mode_event,
1196 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1197 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1198 TWL6040_REG_HFRCTL, 0, 0,
1199 twl6040_power_mode_event,
1200 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1202 SND_SOC_DAPM_MUX("HF Left Playback",
1203 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1204 SND_SOC_DAPM_MUX("HF Right Playback",
1205 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1206 /* Analog playback Muxes */
1207 SND_SOC_DAPM_MUX("HS Left Playback",
1208 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1209 SND_SOC_DAPM_MUX("HS Right Playback",
1210 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1212 /* Analog playback drivers */
1213 SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver",
1214 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1216 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1217 SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver",
1218 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1220 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1221 SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver",
1222 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1224 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1225 SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver",
1226 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1228 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1229 SND_SOC_DAPM_SWITCH_E("Earphone Driver",
1230 SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls,
1231 twl6040_power_mode_event,
1232 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1234 /* Analog playback PGAs */
1235 SND_SOC_DAPM_PGA("HFDAC Left PGA",
1236 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1237 SND_SOC_DAPM_PGA("HFDAC Right PGA",
1238 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1242 static const struct snd_soc_dapm_route intercon[] = {
1244 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1245 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1246 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1248 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1249 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1250 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1252 {"MicAmpL", NULL, "Analog Left Capture Route"},
1253 {"MicAmpR", NULL, "Analog Right Capture Route"},
1255 {"ADC Left", NULL, "MicAmpL"},
1256 {"ADC Right", NULL, "MicAmpR"},
1259 {"AFMAmpL", "NULL", "AFML"},
1260 {"AFMAmpR", "NULL", "AFMR"},
1262 {"HS Left Playback", "HS DAC", "HSDAC Left"},
1263 {"HS Left Playback", "Line-In amp", "AFMAmpL"},
1265 {"HS Right Playback", "HS DAC", "HSDAC Right"},
1266 {"HS Right Playback", "Line-In amp", "AFMAmpR"},
1268 {"Headset Left Driver", "NULL", "HS Left Playback"},
1269 {"Headset Right Driver", "NULL", "HS Right Playback"},
1271 {"HSOL", NULL, "Headset Left Driver"},
1272 {"HSOR", NULL, "Headset Right Driver"},
1274 /* Earphone playback path */
1275 {"Earphone Driver", "Switch", "HSDAC Left"},
1276 {"EP", NULL, "Earphone Driver"},
1278 {"HF Left Playback", "HF DAC", "HFDAC Left"},
1279 {"HF Left Playback", "Line-In amp", "AFMAmpL"},
1281 {"HF Right Playback", "HF DAC", "HFDAC Right"},
1282 {"HF Right Playback", "Line-In amp", "AFMAmpR"},
1284 {"HFDAC Left PGA", NULL, "HF Left Playback"},
1285 {"HFDAC Right PGA", NULL, "HF Right Playback"},
1287 {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"},
1288 {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"},
1290 {"HFL", NULL, "Handsfree Left Driver"},
1291 {"HFR", NULL, "Handsfree Right Driver"},
1294 static int twl6040_add_widgets(struct snd_soc_codec *codec)
1296 struct snd_soc_dapm_context *dapm = &codec->dapm;
1298 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
1299 ARRAY_SIZE(twl6040_dapm_widgets));
1300 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1301 snd_soc_dapm_new_widgets(dapm);
1306 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1307 enum snd_soc_bias_level level)
1309 struct twl6040 *twl6040 = codec->control_data;
1310 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1314 case SND_SOC_BIAS_ON:
1316 case SND_SOC_BIAS_PREPARE:
1318 case SND_SOC_BIAS_STANDBY:
1319 if (priv->codec_powered)
1322 ret = twl6040_power(twl6040, 1);
1326 priv->codec_powered = 1;
1328 /* initialize vdd/vss registers with reg_cache */
1329 twl6040_init_vdd_regs(codec);
1331 /* Set external boost GPO */
1332 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1334 case SND_SOC_BIAS_OFF:
1335 if (!priv->codec_powered)
1338 twl6040_power(twl6040, 0);
1339 priv->codec_powered = 0;
1343 codec->dapm.bias_level = level;
1348 static int twl6040_startup(struct snd_pcm_substream *substream,
1349 struct snd_soc_dai *dai)
1351 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1352 struct snd_soc_codec *codec = rtd->codec;
1353 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1355 snd_pcm_hw_constraint_list(substream->runtime, 0,
1356 SNDRV_PCM_HW_PARAM_RATE,
1357 &sysclk_constraints[priv->pll_power_mode]);
1362 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1363 struct snd_pcm_hw_params *params,
1364 struct snd_soc_dai *dai)
1366 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1367 struct snd_soc_codec *codec = rtd->codec;
1368 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1371 rate = params_rate(params);
1377 /* These rates are not supported when HPPLL is in use */
1378 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1379 dev_err(codec->dev, "HPPLL does not support rate %d\n",
1383 /* Capture is not supported with 17.64MHz sysclk */
1384 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1386 "capture mode is not supported at %dHz\n",
1390 priv->sysclk = 17640000;
1397 priv->sysclk = 19200000;
1400 dev_err(codec->dev, "unsupported rate %d\n", rate);
1407 static int twl6040_prepare(struct snd_pcm_substream *substream,
1408 struct snd_soc_dai *dai)
1410 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1411 struct snd_soc_codec *codec = rtd->codec;
1412 struct twl6040 *twl6040 = codec->control_data;
1413 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1416 if (!priv->sysclk) {
1418 "no mclk configured, call set_sysclk() on init\n");
1422 if ((priv->sysclk == 17640000) && priv->non_lp) {
1424 "some enabled paths aren't supported at %dHz\n",
1429 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1431 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1438 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1439 int clk_id, unsigned int freq, int dir)
1441 struct snd_soc_codec *codec = codec_dai->codec;
1442 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1445 case TWL6040_SYSCLK_SEL_LPPLL:
1446 case TWL6040_SYSCLK_SEL_HPPLL:
1448 priv->clk_in = freq;
1451 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1458 static struct snd_soc_dai_ops twl6040_dai_ops = {
1459 .startup = twl6040_startup,
1460 .hw_params = twl6040_hw_params,
1461 .prepare = twl6040_prepare,
1462 .set_sysclk = twl6040_set_dai_sysclk,
1465 static struct snd_soc_dai_driver twl6040_dai[] = {
1467 .name = "twl6040-hifi",
1469 .stream_name = "Playback",
1472 .rates = TWL6040_RATES,
1473 .formats = TWL6040_FORMATS,
1476 .stream_name = "Capture",
1479 .rates = TWL6040_RATES,
1480 .formats = TWL6040_FORMATS,
1482 .ops = &twl6040_dai_ops,
1485 .name = "twl6040-ul",
1487 .stream_name = "Capture",
1490 .rates = TWL6040_RATES,
1491 .formats = TWL6040_FORMATS,
1493 .ops = &twl6040_dai_ops,
1496 .name = "twl6040-dl1",
1498 .stream_name = "Headset Playback",
1501 .rates = TWL6040_RATES,
1502 .formats = TWL6040_FORMATS,
1504 .ops = &twl6040_dai_ops,
1507 .name = "twl6040-dl2",
1509 .stream_name = "Handsfree Playback",
1512 .rates = TWL6040_RATES,
1513 .formats = TWL6040_FORMATS,
1515 .ops = &twl6040_dai_ops,
1518 .name = "twl6040-vib",
1520 .stream_name = "Vibra Playback",
1523 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1524 .formats = TWL6040_FORMATS,
1526 .ops = &twl6040_dai_ops,
1531 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1533 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1538 static int twl6040_resume(struct snd_soc_codec *codec)
1540 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1541 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1546 #define twl6040_suspend NULL
1547 #define twl6040_resume NULL
1550 static int twl6040_probe(struct snd_soc_codec *codec)
1552 struct twl6040_data *priv;
1553 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1554 struct platform_device *pdev = container_of(codec->dev,
1555 struct platform_device, dev);
1558 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1561 snd_soc_codec_set_drvdata(codec, priv);
1563 priv->codec = codec;
1564 codec->control_data = dev_get_drvdata(codec->dev->parent);
1566 if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1567 priv->hs_left_step = pdata->hs_left_step;
1568 priv->hs_right_step = pdata->hs_right_step;
1570 priv->hs_left_step = 1;
1571 priv->hs_right_step = 1;
1574 if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1575 priv->hf_left_step = pdata->hf_left_step;
1576 priv->hf_right_step = pdata->hf_right_step;
1578 priv->hf_left_step = 1;
1579 priv->hf_right_step = 1;
1582 priv->plug_irq = platform_get_irq(pdev, 0);
1583 if (priv->plug_irq < 0) {
1584 dev_err(codec->dev, "invalid irq\n");
1589 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1590 if (!priv->workqueue) {
1595 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1597 mutex_init(&priv->mutex);
1599 init_completion(&priv->headset.ramp_done);
1600 init_completion(&priv->handsfree.ramp_done);
1602 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1603 if (priv->hf_workqueue == NULL) {
1607 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1608 if (priv->hs_workqueue == NULL) {
1613 INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
1614 INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
1616 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1617 0, "twl6040_irq_plug", codec);
1619 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1623 /* init vio registers */
1624 twl6040_init_vio_regs(codec);
1626 /* power on device */
1627 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1631 snd_soc_add_controls(codec, twl6040_snd_controls,
1632 ARRAY_SIZE(twl6040_snd_controls));
1633 twl6040_add_widgets(codec);
1638 free_irq(priv->plug_irq, codec);
1640 destroy_workqueue(priv->hs_workqueue);
1642 destroy_workqueue(priv->hf_workqueue);
1644 destroy_workqueue(priv->workqueue);
1650 static int twl6040_remove(struct snd_soc_codec *codec)
1652 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1654 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1655 free_irq(priv->plug_irq, codec);
1656 destroy_workqueue(priv->workqueue);
1657 destroy_workqueue(priv->hf_workqueue);
1658 destroy_workqueue(priv->hs_workqueue);
1664 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1665 .probe = twl6040_probe,
1666 .remove = twl6040_remove,
1667 .suspend = twl6040_suspend,
1668 .resume = twl6040_resume,
1669 .read = twl6040_read_reg_cache,
1670 .write = twl6040_write,
1671 .set_bias_level = twl6040_set_bias_level,
1672 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1673 .reg_word_size = sizeof(u8),
1674 .reg_cache_default = twl6040_reg,
1677 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1679 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1680 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1683 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1685 snd_soc_unregister_codec(&pdev->dev);
1689 static struct platform_driver twl6040_codec_driver = {
1691 .name = "twl6040-codec",
1692 .owner = THIS_MODULE,
1694 .probe = twl6040_codec_probe,
1695 .remove = __devexit_p(twl6040_codec_remove),
1698 static int __init twl6040_codec_init(void)
1700 return platform_driver_register(&twl6040_codec_driver);
1702 module_init(twl6040_codec_init);
1704 static void __exit twl6040_codec_exit(void)
1706 platform_driver_unregister(&twl6040_codec_driver);
1708 module_exit(twl6040_codec_exit);
1710 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1711 MODULE_AUTHOR("Misael Lopez Cruz");
1712 MODULE_LICENSE("GPL");