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 */
158 /* List of registers to be restored after power up */
159 static const int twl6040_restore_list[] = {
163 TWL6040_REG_LINEGAIN,
174 /* set of rates for each pll: low-power and high-performance */
175 static unsigned int lp_rates[] = {
187 static unsigned int hp_rates[] = {
195 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
196 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
197 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
201 * read twl6040 register cache
203 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
206 u8 *cache = codec->reg_cache;
208 if (reg >= TWL6040_CACHEREGNUM)
215 * write twl6040 register cache
217 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
220 u8 *cache = codec->reg_cache;
222 if (reg >= TWL6040_CACHEREGNUM)
228 * read from twl6040 hardware register
230 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
233 struct twl6040 *twl6040 = codec->control_data;
236 if (reg >= TWL6040_CACHEREGNUM)
239 value = twl6040_reg_read(twl6040, reg);
240 twl6040_write_reg_cache(codec, reg, value);
246 * write to the twl6040 register space
248 static int twl6040_write(struct snd_soc_codec *codec,
249 unsigned int reg, unsigned int value)
251 struct twl6040 *twl6040 = codec->control_data;
253 if (reg >= TWL6040_CACHEREGNUM)
256 twl6040_write_reg_cache(codec, reg, value);
257 return twl6040_reg_write(twl6040, reg, value);
260 static void twl6040_init_chip(struct snd_soc_codec *codec)
262 struct twl6040 *twl6040 = codec->control_data;
265 val = twl6040_get_revid(twl6040);
266 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
270 static void twl6040_restore_regs(struct snd_soc_codec *codec)
272 u8 *cache = codec->reg_cache;
275 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
276 reg = twl6040_restore_list[i];
277 twl6040_write(codec, reg, cache[reg]);
282 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
284 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
285 unsigned int left_step, unsigned int right_step)
288 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
289 struct twl6040_output *headset = &priv->headset;
290 int left_complete = 0, right_complete = 0;
294 left_step = (left_step > 0xF) ? 0xF : left_step;
295 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
296 val = (~reg & TWL6040_HSL_VOL_MASK);
298 if (headset->ramp == TWL6040_RAMP_UP) {
300 if (val < headset->left_vol) {
301 if (val + left_step > headset->left_vol)
302 val = headset->left_vol;
306 reg &= ~TWL6040_HSL_VOL_MASK;
307 twl6040_write(codec, TWL6040_REG_HSGAIN,
308 (reg | (~val & TWL6040_HSL_VOL_MASK)));
312 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
315 if ((int)val - (int)left_step < 0)
320 reg &= ~TWL6040_HSL_VOL_MASK;
321 twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
322 (~val & TWL6040_HSL_VOL_MASK));
329 right_step = (right_step > 0xF) ? 0xF : right_step;
330 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
331 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
333 if (headset->ramp == TWL6040_RAMP_UP) {
335 if (val < headset->right_vol) {
336 if (val + right_step > headset->right_vol)
337 val = headset->right_vol;
341 reg &= ~TWL6040_HSR_VOL_MASK;
342 twl6040_write(codec, TWL6040_REG_HSGAIN,
343 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
347 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
350 if ((int)val - (int)right_step < 0)
355 reg &= ~TWL6040_HSR_VOL_MASK;
356 twl6040_write(codec, TWL6040_REG_HSGAIN,
357 reg | (~val << TWL6040_HSR_VOL_SHIFT));
363 return left_complete & right_complete;
367 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
369 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
370 unsigned int left_step, unsigned int right_step)
372 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
373 struct twl6040_output *handsfree = &priv->handsfree;
374 int left_complete = 0, right_complete = 0;
378 left_step = (left_step > 0x1D) ? 0x1D : left_step;
379 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
381 val = (reg & TWL6040_HF_VOL_MASK);
382 if (handsfree->ramp == TWL6040_RAMP_UP) {
384 if (val < handsfree->left_vol) {
385 if (val + left_step > handsfree->left_vol)
386 val = handsfree->left_vol;
390 reg &= ~TWL6040_HF_VOL_MASK;
391 twl6040_write(codec, TWL6040_REG_HFLGAIN,
396 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
399 if ((int)val - (int)left_step < 0)
404 reg &= ~TWL6040_HF_VOL_MASK;
405 twl6040_write(codec, TWL6040_REG_HFLGAIN,
413 right_step = (right_step > 0x1D) ? 0x1D : right_step;
414 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
416 val = (reg & TWL6040_HF_VOL_MASK);
417 if (handsfree->ramp == TWL6040_RAMP_UP) {
419 if (val < handsfree->right_vol) {
420 if (val + right_step > handsfree->right_vol)
421 val = handsfree->right_vol;
425 reg &= ~TWL6040_HF_VOL_MASK;
426 twl6040_write(codec, TWL6040_REG_HFRGAIN,
431 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
434 if ((int)val - (int)right_step < 0)
439 reg &= ~TWL6040_HF_VOL_MASK;
440 twl6040_write(codec, TWL6040_REG_HFRGAIN,
445 return left_complete & right_complete;
449 * This work ramps both output PGAs at stream start/stop time to
450 * minimise pop associated with DAPM power switching.
452 static void twl6040_pga_hs_work(struct work_struct *work)
454 struct twl6040_data *priv =
455 container_of(work, struct twl6040_data, hs_delayed_work.work);
456 struct snd_soc_codec *codec = priv->codec;
457 struct twl6040_output *headset = &priv->headset;
458 unsigned int delay = headset->step_delay;
459 int i, headset_complete;
461 /* do we need to ramp at all ? */
462 if (headset->ramp == TWL6040_RAMP_NONE)
465 /* HS PGA volumes have 4 bits of resolution to ramp */
466 for (i = 0; i <= 16; i++) {
467 headset_complete = twl6040_hs_ramp_step(codec,
469 headset->right_step);
471 /* ramp finished ? */
472 if (headset_complete)
476 * TODO: tune: delay is longer over 0dB
477 * as increases are larger.
480 schedule_timeout_interruptible(msecs_to_jiffies(delay +
483 schedule_timeout_interruptible(msecs_to_jiffies(delay));
486 if (headset->ramp == TWL6040_RAMP_DOWN) {
488 complete(&headset->ramp_done);
492 headset->ramp = TWL6040_RAMP_NONE;
495 static void twl6040_pga_hf_work(struct work_struct *work)
497 struct twl6040_data *priv =
498 container_of(work, struct twl6040_data, hf_delayed_work.work);
499 struct snd_soc_codec *codec = priv->codec;
500 struct twl6040_output *handsfree = &priv->handsfree;
501 unsigned int delay = handsfree->step_delay;
502 int i, handsfree_complete;
504 /* do we need to ramp at all ? */
505 if (handsfree->ramp == TWL6040_RAMP_NONE)
508 /* HF PGA volumes have 5 bits of resolution to ramp */
509 for (i = 0; i <= 32; i++) {
510 handsfree_complete = twl6040_hf_ramp_step(codec,
511 handsfree->left_step,
512 handsfree->right_step);
514 /* ramp finished ? */
515 if (handsfree_complete)
519 * TODO: tune: delay is longer over 0dB
520 * as increases are larger.
523 schedule_timeout_interruptible(msecs_to_jiffies(delay +
526 schedule_timeout_interruptible(msecs_to_jiffies(delay));
530 if (handsfree->ramp == TWL6040_RAMP_DOWN) {
531 handsfree->active = 0;
532 complete(&handsfree->ramp_done);
534 handsfree->active = 1;
535 handsfree->ramp = TWL6040_RAMP_NONE;
538 static int pga_event(struct snd_soc_dapm_widget *w,
539 struct snd_kcontrol *kcontrol, int event)
541 struct snd_soc_codec *codec = w->codec;
542 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
543 struct twl6040_output *out;
544 struct delayed_work *work;
545 struct workqueue_struct *queue;
550 out = &priv->headset;
551 work = &priv->hs_delayed_work;
552 queue = priv->hs_workqueue;
553 out->left_step = priv->hs_left_step;
554 out->right_step = priv->hs_right_step;
555 out->step_delay = 5; /* 5 ms between volume ramp steps */
558 out = &priv->handsfree;
559 work = &priv->hf_delayed_work;
560 queue = priv->hf_workqueue;
561 out->left_step = priv->hf_left_step;
562 out->right_step = priv->hf_right_step;
563 out->step_delay = 5; /* 5 ms between volume ramp steps */
564 if (SND_SOC_DAPM_EVENT_ON(event))
574 case SND_SOC_DAPM_POST_PMU:
578 /* don't use volume ramp for power-up */
579 out->left_step = out->left_vol;
580 out->right_step = out->right_vol;
582 if (!delayed_work_pending(work)) {
583 out->ramp = TWL6040_RAMP_UP;
584 queue_delayed_work(queue, work,
585 msecs_to_jiffies(1));
589 case SND_SOC_DAPM_PRE_PMD:
593 if (!delayed_work_pending(work)) {
594 /* use volume ramp for power-down */
595 out->ramp = TWL6040_RAMP_DOWN;
596 INIT_COMPLETION(out->ramp_done);
598 queue_delayed_work(queue, work,
599 msecs_to_jiffies(1));
601 wait_for_completion_timeout(&out->ramp_done,
602 msecs_to_jiffies(2000));
610 /* set headset dac and driver power mode */
611 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
614 int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL;
616 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
617 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
627 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
628 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
633 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
634 struct snd_kcontrol *kcontrol, int event)
640 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
641 struct snd_kcontrol *kcontrol, int event)
643 struct snd_soc_codec *codec = w->codec;
644 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
647 if (SND_SOC_DAPM_EVENT_ON(event)) {
649 if (!strcmp(w->name, "Earphone Driver")) {
650 /* Earphone doesn't support low power mode */
651 priv->hs_power_mode_locked = 1;
652 ret = headset_power_mode(codec, 1);
656 if (!strcmp(w->name, "Earphone Driver")) {
657 priv->hs_power_mode_locked = 0;
658 ret = headset_power_mode(codec, priv->hs_power_mode);
667 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
668 struct snd_soc_jack *jack, int report)
670 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
673 mutex_lock(&priv->mutex);
676 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
677 if (status & TWL6040_PLUGCOMP)
678 snd_soc_jack_report(jack, report, report);
680 snd_soc_jack_report(jack, 0, report);
682 mutex_unlock(&priv->mutex);
685 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
686 struct snd_soc_jack *jack, int report)
688 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
689 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
691 hs_jack->jack = jack;
692 hs_jack->report = report;
694 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
696 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
698 static void twl6040_accessory_work(struct work_struct *work)
700 struct twl6040_data *priv = container_of(work,
701 struct twl6040_data, delayed_work.work);
702 struct snd_soc_codec *codec = priv->codec;
703 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
705 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
708 /* audio interrupt handler */
709 static irqreturn_t twl6040_audio_handler(int irq, void *data)
711 struct snd_soc_codec *codec = data;
712 struct twl6040 *twl6040 = codec->control_data;
713 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
716 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
718 if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT))
719 queue_delayed_work(priv->workqueue, &priv->delayed_work,
720 msecs_to_jiffies(200));
725 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
726 struct snd_ctl_elem_value *ucontrol)
728 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
729 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
730 struct twl6040_output *out = NULL;
731 struct soc_mixer_control *mc =
732 (struct soc_mixer_control *)kcontrol->private_value;
734 unsigned int reg = mc->reg;
736 /* For HS and HF we shadow the values and only actually write
737 * them out when active in order to ensure the amplifier comes on
738 * as quietly as possible. */
740 case TWL6040_REG_HSGAIN:
741 out = &twl6040_priv->headset;
748 out->left_vol = ucontrol->value.integer.value[0];
749 out->right_vol = ucontrol->value.integer.value[1];
754 ret = snd_soc_put_volsw(kcontrol, ucontrol);
761 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
762 struct snd_ctl_elem_value *ucontrol)
764 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
765 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
766 struct twl6040_output *out = &twl6040_priv->headset;
767 struct soc_mixer_control *mc =
768 (struct soc_mixer_control *)kcontrol->private_value;
769 unsigned int reg = mc->reg;
772 case TWL6040_REG_HSGAIN:
773 out = &twl6040_priv->headset;
774 ucontrol->value.integer.value[0] = out->left_vol;
775 ucontrol->value.integer.value[1] = out->right_vol;
782 return snd_soc_get_volsw(kcontrol, ucontrol);
785 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
786 struct snd_ctl_elem_value *ucontrol)
788 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
789 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
790 struct twl6040_output *out = NULL;
791 struct soc_mixer_control *mc =
792 (struct soc_mixer_control *)kcontrol->private_value;
794 unsigned int reg = mc->reg;
796 /* For HS and HF we shadow the values and only actually write
797 * them out when active in order to ensure the amplifier comes on
798 * as quietly as possible. */
800 case TWL6040_REG_HFLGAIN:
801 case TWL6040_REG_HFRGAIN:
802 out = &twl6040_priv->handsfree;
809 out->left_vol = ucontrol->value.integer.value[0];
810 out->right_vol = ucontrol->value.integer.value[1];
815 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
822 static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
825 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
826 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
827 struct twl6040_output *out = &twl6040_priv->handsfree;
828 struct soc_mixer_control *mc =
829 (struct soc_mixer_control *)kcontrol->private_value;
830 unsigned int reg = mc->reg;
832 /* If these are cached registers use the cache */
834 case TWL6040_REG_HFLGAIN:
835 case TWL6040_REG_HFRGAIN:
836 out = &twl6040_priv->handsfree;
837 ucontrol->value.integer.value[0] = out->left_vol;
838 ucontrol->value.integer.value[1] = out->right_vol;
845 return snd_soc_get_volsw_2r(kcontrol, ucontrol);
848 /* double control with volume update */
849 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
851 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
852 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
853 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
854 .tlv.p = (tlv_array), \
855 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
856 .put = twl6040_put_volsw, \
857 .private_value = (unsigned long)&(struct soc_mixer_control) \
858 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
859 .max = xmax, .platform_max = xmax, .invert = xinvert} }
861 /* double control with volume update */
862 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
864 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
865 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
866 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
867 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
868 .tlv.p = (tlv_array), \
869 .info = snd_soc_info_volsw_2r, \
870 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
871 .private_value = (unsigned long)&(struct soc_mixer_control) \
872 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
873 .rshift = xshift, .max = xmax, .invert = xinvert}, }
876 * MICATT volume control:
877 * from -6 to 0 dB in 6 dB steps
879 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
882 * MICGAIN volume control:
883 * from 6 to 30 dB in 6 dB steps
885 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
888 * AFMGAIN volume control:
889 * from -18 to 24 dB in 6 dB steps
891 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
894 * HSGAIN volume control:
895 * from -30 to 0 dB in 2 dB steps
897 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
900 * HFGAIN volume control:
901 * from -52 to 6 dB in 2 dB steps
903 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
906 * EPGAIN volume control:
907 * from -24 to 6 dB in 2 dB steps
909 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
911 /* Left analog microphone selection */
912 static const char *twl6040_amicl_texts[] =
913 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
915 /* Right analog microphone selection */
916 static const char *twl6040_amicr_texts[] =
917 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
919 static const struct soc_enum twl6040_enum[] = {
920 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
921 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
924 static const char *twl6040_hs_texts[] = {
925 "Off", "HS DAC", "Line-In amp"
928 static const struct soc_enum twl6040_hs_enum[] = {
929 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
931 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
935 static const char *twl6040_hf_texts[] = {
936 "Off", "HF DAC", "Line-In amp"
939 static const struct soc_enum twl6040_hf_enum[] = {
940 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
942 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
946 static const struct snd_kcontrol_new amicl_control =
947 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
949 static const struct snd_kcontrol_new amicr_control =
950 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
952 /* Headset DAC playback switches */
953 static const struct snd_kcontrol_new hsl_mux_controls =
954 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
956 static const struct snd_kcontrol_new hsr_mux_controls =
957 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
959 /* Handsfree DAC playback switches */
960 static const struct snd_kcontrol_new hfl_mux_controls =
961 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
963 static const struct snd_kcontrol_new hfr_mux_controls =
964 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
966 static const struct snd_kcontrol_new ep_driver_switch_controls =
967 SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0);
969 /* Headset power mode */
970 static const char *twl6040_power_mode_texts[] = {
971 "Low-Power", "High-Perfomance",
974 static const struct soc_enum twl6040_power_mode_enum =
975 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
976 twl6040_power_mode_texts);
978 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
979 struct snd_ctl_elem_value *ucontrol)
981 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
982 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
984 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
989 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
990 struct snd_ctl_elem_value *ucontrol)
992 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
993 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
994 int high_perf = ucontrol->value.enumerated.item[0];
997 if (!priv->hs_power_mode_locked)
998 ret = headset_power_mode(codec, high_perf);
1001 priv->hs_power_mode = high_perf;
1006 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
1007 struct snd_ctl_elem_value *ucontrol)
1009 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1010 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1012 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1017 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1018 struct snd_ctl_elem_value *ucontrol)
1020 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1021 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1023 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1028 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1030 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1032 return priv->pll_power_mode;
1034 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1036 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1038 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1039 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1040 SOC_DOUBLE_TLV("Capture Volume",
1041 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1044 SOC_DOUBLE_TLV("Aux FM Volume",
1045 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1047 /* Playback gains */
1048 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
1049 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
1050 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
1051 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
1052 SOC_SINGLE_TLV("Earphone Playback Volume",
1053 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1055 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1056 twl6040_headset_power_get_enum,
1057 twl6040_headset_power_put_enum),
1059 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1060 twl6040_pll_get_enum, twl6040_pll_put_enum),
1063 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1065 SND_SOC_DAPM_INPUT("MAINMIC"),
1066 SND_SOC_DAPM_INPUT("HSMIC"),
1067 SND_SOC_DAPM_INPUT("SUBMIC"),
1068 SND_SOC_DAPM_INPUT("AFML"),
1069 SND_SOC_DAPM_INPUT("AFMR"),
1072 SND_SOC_DAPM_OUTPUT("HSOL"),
1073 SND_SOC_DAPM_OUTPUT("HSOR"),
1074 SND_SOC_DAPM_OUTPUT("HFL"),
1075 SND_SOC_DAPM_OUTPUT("HFR"),
1076 SND_SOC_DAPM_OUTPUT("EP"),
1078 /* Analog input muxes for the capture amplifiers */
1079 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1080 SND_SOC_NOPM, 0, 0, &amicl_control),
1081 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1082 SND_SOC_NOPM, 0, 0, &amicr_control),
1084 /* Analog capture PGAs */
1085 SND_SOC_DAPM_PGA("MicAmpL",
1086 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1087 SND_SOC_DAPM_PGA("MicAmpR",
1088 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1090 /* Auxiliary FM PGAs */
1091 SND_SOC_DAPM_PGA("AFMAmpL",
1092 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1093 SND_SOC_DAPM_PGA("AFMAmpR",
1094 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1097 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1098 TWL6040_REG_MICLCTL, 2, 0),
1099 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1100 TWL6040_REG_MICRCTL, 2, 0),
1102 /* Microphone bias */
1103 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1104 TWL6040_REG_AMICBCTL, 0, 0),
1105 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1106 TWL6040_REG_AMICBCTL, 4, 0),
1107 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1108 TWL6040_REG_DMICBCTL, 0, 0),
1109 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1110 TWL6040_REG_DMICBCTL, 4, 0),
1113 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1114 TWL6040_REG_HSLCTL, 0, 0,
1115 twl6040_hs_dac_event,
1116 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1117 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1118 TWL6040_REG_HSRCTL, 0, 0,
1119 twl6040_hs_dac_event,
1120 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1121 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1122 TWL6040_REG_HFLCTL, 0, 0,
1123 twl6040_power_mode_event,
1124 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1125 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1126 TWL6040_REG_HFRCTL, 0, 0,
1127 twl6040_power_mode_event,
1128 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1130 SND_SOC_DAPM_MUX("HF Left Playback",
1131 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1132 SND_SOC_DAPM_MUX("HF Right Playback",
1133 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1134 /* Analog playback Muxes */
1135 SND_SOC_DAPM_MUX("HS Left Playback",
1136 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1137 SND_SOC_DAPM_MUX("HS Right Playback",
1138 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1140 /* Analog playback drivers */
1141 SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver",
1142 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1144 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1145 SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver",
1146 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1148 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1149 SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver",
1150 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1152 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1153 SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver",
1154 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1156 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1157 SND_SOC_DAPM_SWITCH_E("Earphone Driver",
1158 SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls,
1159 twl6040_power_mode_event,
1160 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1162 /* Analog playback PGAs */
1163 SND_SOC_DAPM_PGA("HFDAC Left PGA",
1164 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1165 SND_SOC_DAPM_PGA("HFDAC Right PGA",
1166 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1170 static const struct snd_soc_dapm_route intercon[] = {
1172 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1173 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1174 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1176 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1177 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1178 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1180 {"MicAmpL", NULL, "Analog Left Capture Route"},
1181 {"MicAmpR", NULL, "Analog Right Capture Route"},
1183 {"ADC Left", NULL, "MicAmpL"},
1184 {"ADC Right", NULL, "MicAmpR"},
1187 {"AFMAmpL", "NULL", "AFML"},
1188 {"AFMAmpR", "NULL", "AFMR"},
1190 {"HS Left Playback", "HS DAC", "HSDAC Left"},
1191 {"HS Left Playback", "Line-In amp", "AFMAmpL"},
1193 {"HS Right Playback", "HS DAC", "HSDAC Right"},
1194 {"HS Right Playback", "Line-In amp", "AFMAmpR"},
1196 {"Headset Left Driver", "NULL", "HS Left Playback"},
1197 {"Headset Right Driver", "NULL", "HS Right Playback"},
1199 {"HSOL", NULL, "Headset Left Driver"},
1200 {"HSOR", NULL, "Headset Right Driver"},
1202 /* Earphone playback path */
1203 {"Earphone Driver", "Switch", "HSDAC Left"},
1204 {"EP", NULL, "Earphone Driver"},
1206 {"HF Left Playback", "HF DAC", "HFDAC Left"},
1207 {"HF Left Playback", "Line-In amp", "AFMAmpL"},
1209 {"HF Right Playback", "HF DAC", "HFDAC Right"},
1210 {"HF Right Playback", "Line-In amp", "AFMAmpR"},
1212 {"HFDAC Left PGA", NULL, "HF Left Playback"},
1213 {"HFDAC Right PGA", NULL, "HF Right Playback"},
1215 {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"},
1216 {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"},
1218 {"HFL", NULL, "Handsfree Left Driver"},
1219 {"HFR", NULL, "Handsfree Right Driver"},
1222 static int twl6040_add_widgets(struct snd_soc_codec *codec)
1224 struct snd_soc_dapm_context *dapm = &codec->dapm;
1226 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
1227 ARRAY_SIZE(twl6040_dapm_widgets));
1228 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1229 snd_soc_dapm_new_widgets(dapm);
1234 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1235 enum snd_soc_bias_level level)
1237 struct twl6040 *twl6040 = codec->control_data;
1238 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1242 case SND_SOC_BIAS_ON:
1244 case SND_SOC_BIAS_PREPARE:
1246 case SND_SOC_BIAS_STANDBY:
1247 if (priv->codec_powered)
1250 ret = twl6040_power(twl6040, 1);
1254 priv->codec_powered = 1;
1256 twl6040_restore_regs(codec);
1258 /* Set external boost GPO */
1259 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1261 case SND_SOC_BIAS_OFF:
1262 if (!priv->codec_powered)
1265 twl6040_power(twl6040, 0);
1266 priv->codec_powered = 0;
1270 codec->dapm.bias_level = level;
1275 static int twl6040_startup(struct snd_pcm_substream *substream,
1276 struct snd_soc_dai *dai)
1278 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1279 struct snd_soc_codec *codec = rtd->codec;
1280 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1282 snd_pcm_hw_constraint_list(substream->runtime, 0,
1283 SNDRV_PCM_HW_PARAM_RATE,
1284 &sysclk_constraints[priv->pll_power_mode]);
1289 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1290 struct snd_pcm_hw_params *params,
1291 struct snd_soc_dai *dai)
1293 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1294 struct snd_soc_codec *codec = rtd->codec;
1295 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1298 rate = params_rate(params);
1304 /* These rates are not supported when HPPLL is in use */
1305 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1306 dev_err(codec->dev, "HPPLL does not support rate %d\n",
1310 /* Capture is not supported with 17.64MHz sysclk */
1311 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1313 "capture mode is not supported at %dHz\n",
1317 priv->sysclk = 17640000;
1324 priv->sysclk = 19200000;
1327 dev_err(codec->dev, "unsupported rate %d\n", rate);
1334 static int twl6040_prepare(struct snd_pcm_substream *substream,
1335 struct snd_soc_dai *dai)
1337 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1338 struct snd_soc_codec *codec = rtd->codec;
1339 struct twl6040 *twl6040 = codec->control_data;
1340 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1343 if (!priv->sysclk) {
1345 "no mclk configured, call set_sysclk() on init\n");
1349 if ((priv->sysclk == 17640000) && priv->non_lp) {
1351 "some enabled paths aren't supported at %dHz\n",
1356 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1358 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1365 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1366 int clk_id, unsigned int freq, int dir)
1368 struct snd_soc_codec *codec = codec_dai->codec;
1369 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1372 case TWL6040_SYSCLK_SEL_LPPLL:
1373 case TWL6040_SYSCLK_SEL_HPPLL:
1375 priv->clk_in = freq;
1378 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1385 static struct snd_soc_dai_ops twl6040_dai_ops = {
1386 .startup = twl6040_startup,
1387 .hw_params = twl6040_hw_params,
1388 .prepare = twl6040_prepare,
1389 .set_sysclk = twl6040_set_dai_sysclk,
1392 static struct snd_soc_dai_driver twl6040_dai[] = {
1394 .name = "twl6040-hifi",
1396 .stream_name = "Playback",
1399 .rates = TWL6040_RATES,
1400 .formats = TWL6040_FORMATS,
1403 .stream_name = "Capture",
1406 .rates = TWL6040_RATES,
1407 .formats = TWL6040_FORMATS,
1409 .ops = &twl6040_dai_ops,
1412 .name = "twl6040-ul",
1414 .stream_name = "Capture",
1417 .rates = TWL6040_RATES,
1418 .formats = TWL6040_FORMATS,
1420 .ops = &twl6040_dai_ops,
1423 .name = "twl6040-dl1",
1425 .stream_name = "Headset Playback",
1428 .rates = TWL6040_RATES,
1429 .formats = TWL6040_FORMATS,
1431 .ops = &twl6040_dai_ops,
1434 .name = "twl6040-dl2",
1436 .stream_name = "Handsfree Playback",
1439 .rates = TWL6040_RATES,
1440 .formats = TWL6040_FORMATS,
1442 .ops = &twl6040_dai_ops,
1445 .name = "twl6040-vib",
1447 .stream_name = "Vibra Playback",
1450 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1451 .formats = TWL6040_FORMATS,
1453 .ops = &twl6040_dai_ops,
1458 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1460 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1465 static int twl6040_resume(struct snd_soc_codec *codec)
1467 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1468 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1473 #define twl6040_suspend NULL
1474 #define twl6040_resume NULL
1477 static int twl6040_probe(struct snd_soc_codec *codec)
1479 struct twl6040_data *priv;
1480 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1481 struct platform_device *pdev = container_of(codec->dev,
1482 struct platform_device, dev);
1485 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1488 snd_soc_codec_set_drvdata(codec, priv);
1490 priv->codec = codec;
1491 codec->control_data = dev_get_drvdata(codec->dev->parent);
1493 if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1494 priv->hs_left_step = pdata->hs_left_step;
1495 priv->hs_right_step = pdata->hs_right_step;
1497 priv->hs_left_step = 1;
1498 priv->hs_right_step = 1;
1501 if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1502 priv->hf_left_step = pdata->hf_left_step;
1503 priv->hf_right_step = pdata->hf_right_step;
1505 priv->hf_left_step = 1;
1506 priv->hf_right_step = 1;
1509 priv->plug_irq = platform_get_irq(pdev, 0);
1510 if (priv->plug_irq < 0) {
1511 dev_err(codec->dev, "invalid irq\n");
1516 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1517 if (!priv->workqueue) {
1522 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1524 mutex_init(&priv->mutex);
1526 init_completion(&priv->headset.ramp_done);
1527 init_completion(&priv->handsfree.ramp_done);
1529 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1530 if (priv->hf_workqueue == NULL) {
1534 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1535 if (priv->hs_workqueue == NULL) {
1540 INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
1541 INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
1543 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1544 0, "twl6040_irq_plug", codec);
1546 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1550 twl6040_init_chip(codec);
1552 /* power on device */
1553 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1557 snd_soc_add_controls(codec, twl6040_snd_controls,
1558 ARRAY_SIZE(twl6040_snd_controls));
1559 twl6040_add_widgets(codec);
1564 free_irq(priv->plug_irq, codec);
1566 destroy_workqueue(priv->hs_workqueue);
1568 destroy_workqueue(priv->hf_workqueue);
1570 destroy_workqueue(priv->workqueue);
1576 static int twl6040_remove(struct snd_soc_codec *codec)
1578 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1580 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1581 free_irq(priv->plug_irq, codec);
1582 destroy_workqueue(priv->workqueue);
1583 destroy_workqueue(priv->hf_workqueue);
1584 destroy_workqueue(priv->hs_workqueue);
1590 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1591 .probe = twl6040_probe,
1592 .remove = twl6040_remove,
1593 .suspend = twl6040_suspend,
1594 .resume = twl6040_resume,
1595 .read = twl6040_read_reg_cache,
1596 .write = twl6040_write,
1597 .set_bias_level = twl6040_set_bias_level,
1598 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1599 .reg_word_size = sizeof(u8),
1600 .reg_cache_default = twl6040_reg,
1603 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1605 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1606 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1609 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1611 snd_soc_unregister_codec(&pdev->dev);
1615 static struct platform_driver twl6040_codec_driver = {
1617 .name = "twl6040-codec",
1618 .owner = THIS_MODULE,
1620 .probe = twl6040_codec_probe,
1621 .remove = __devexit_p(twl6040_codec_remove),
1624 static int __init twl6040_codec_init(void)
1626 return platform_driver_register(&twl6040_codec_driver);
1628 module_init(twl6040_codec_init);
1630 static void __exit twl6040_codec_exit(void)
1632 platform_driver_unregister(&twl6040_codec_driver);
1634 module_exit(twl6040_codec_exit);
1636 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1637 MODULE_AUTHOR("Misael Lopez Cruz");
1638 MODULE_LICENSE("GPL");