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 0x00, /* TWL6040_MICLCTL 0x0C */
122 0x00, /* TWL6040_MICRCTL 0x0D */
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);
268 /* Change chip defaults */
269 /* No imput selected for microphone amplifiers */
270 twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
271 twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
274 * We need to lower the default gain values, so the ramp code
275 * can work correctly for the first playback.
276 * This reduces the pop noise heard at the first playback.
278 twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
279 twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
280 twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
281 twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
282 twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
285 static void twl6040_restore_regs(struct snd_soc_codec *codec)
287 u8 *cache = codec->reg_cache;
290 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
291 reg = twl6040_restore_list[i];
292 twl6040_write(codec, reg, cache[reg]);
297 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
299 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
300 unsigned int left_step, unsigned int right_step)
303 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
304 struct twl6040_output *headset = &priv->headset;
305 int left_complete = 0, right_complete = 0;
309 left_step = (left_step > 0xF) ? 0xF : left_step;
310 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
311 val = (~reg & TWL6040_HSL_VOL_MASK);
313 if (headset->ramp == TWL6040_RAMP_UP) {
315 if (val < headset->left_vol) {
316 if (val + left_step > headset->left_vol)
317 val = headset->left_vol;
321 reg &= ~TWL6040_HSL_VOL_MASK;
322 twl6040_write(codec, TWL6040_REG_HSGAIN,
323 (reg | (~val & TWL6040_HSL_VOL_MASK)));
327 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
330 if ((int)val - (int)left_step < 0)
335 reg &= ~TWL6040_HSL_VOL_MASK;
336 twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
337 (~val & TWL6040_HSL_VOL_MASK));
344 right_step = (right_step > 0xF) ? 0xF : right_step;
345 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
346 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
348 if (headset->ramp == TWL6040_RAMP_UP) {
350 if (val < headset->right_vol) {
351 if (val + right_step > headset->right_vol)
352 val = headset->right_vol;
356 reg &= ~TWL6040_HSR_VOL_MASK;
357 twl6040_write(codec, TWL6040_REG_HSGAIN,
358 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
362 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
365 if ((int)val - (int)right_step < 0)
370 reg &= ~TWL6040_HSR_VOL_MASK;
371 twl6040_write(codec, TWL6040_REG_HSGAIN,
372 reg | (~val << TWL6040_HSR_VOL_SHIFT));
378 return left_complete & right_complete;
382 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
384 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
385 unsigned int left_step, unsigned int right_step)
387 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
388 struct twl6040_output *handsfree = &priv->handsfree;
389 int left_complete = 0, right_complete = 0;
393 left_step = (left_step > 0x1D) ? 0x1D : left_step;
394 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
396 val = (reg & TWL6040_HF_VOL_MASK);
397 if (handsfree->ramp == TWL6040_RAMP_UP) {
399 if (val < handsfree->left_vol) {
400 if (val + left_step > handsfree->left_vol)
401 val = handsfree->left_vol;
405 reg &= ~TWL6040_HF_VOL_MASK;
406 twl6040_write(codec, TWL6040_REG_HFLGAIN,
411 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
414 if ((int)val - (int)left_step < 0)
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) {
435 if (val + right_step > handsfree->right_vol)
436 val = handsfree->right_vol;
440 reg &= ~TWL6040_HF_VOL_MASK;
441 twl6040_write(codec, TWL6040_REG_HFRGAIN,
446 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
449 if ((int)val - (int)right_step < 0)
454 reg &= ~TWL6040_HF_VOL_MASK;
455 twl6040_write(codec, TWL6040_REG_HFRGAIN,
460 return left_complete & right_complete;
464 * This work ramps both output PGAs at stream start/stop time to
465 * minimise pop associated with DAPM power switching.
467 static void twl6040_pga_hs_work(struct work_struct *work)
469 struct twl6040_data *priv =
470 container_of(work, struct twl6040_data, hs_delayed_work.work);
471 struct snd_soc_codec *codec = priv->codec;
472 struct twl6040_output *headset = &priv->headset;
473 unsigned int delay = headset->step_delay;
474 int i, headset_complete;
476 /* do we need to ramp at all ? */
477 if (headset->ramp == TWL6040_RAMP_NONE)
480 /* HS PGA volumes have 4 bits of resolution to ramp */
481 for (i = 0; i <= 16; i++) {
482 headset_complete = twl6040_hs_ramp_step(codec,
484 headset->right_step);
486 /* ramp finished ? */
487 if (headset_complete)
491 * TODO: tune: delay is longer over 0dB
492 * as increases are larger.
495 schedule_timeout_interruptible(msecs_to_jiffies(delay +
498 schedule_timeout_interruptible(msecs_to_jiffies(delay));
501 if (headset->ramp == TWL6040_RAMP_DOWN) {
503 complete(&headset->ramp_done);
507 headset->ramp = TWL6040_RAMP_NONE;
510 static void twl6040_pga_hf_work(struct work_struct *work)
512 struct twl6040_data *priv =
513 container_of(work, struct twl6040_data, hf_delayed_work.work);
514 struct snd_soc_codec *codec = priv->codec;
515 struct twl6040_output *handsfree = &priv->handsfree;
516 unsigned int delay = handsfree->step_delay;
517 int i, handsfree_complete;
519 /* do we need to ramp at all ? */
520 if (handsfree->ramp == TWL6040_RAMP_NONE)
523 /* HF PGA volumes have 5 bits of resolution to ramp */
524 for (i = 0; i <= 32; i++) {
525 handsfree_complete = twl6040_hf_ramp_step(codec,
526 handsfree->left_step,
527 handsfree->right_step);
529 /* ramp finished ? */
530 if (handsfree_complete)
534 * TODO: tune: delay is longer over 0dB
535 * as increases are larger.
538 schedule_timeout_interruptible(msecs_to_jiffies(delay +
541 schedule_timeout_interruptible(msecs_to_jiffies(delay));
545 if (handsfree->ramp == TWL6040_RAMP_DOWN) {
546 handsfree->active = 0;
547 complete(&handsfree->ramp_done);
549 handsfree->active = 1;
550 handsfree->ramp = TWL6040_RAMP_NONE;
553 static int pga_event(struct snd_soc_dapm_widget *w,
554 struct snd_kcontrol *kcontrol, int event)
556 struct snd_soc_codec *codec = w->codec;
557 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
558 struct twl6040_output *out;
559 struct delayed_work *work;
560 struct workqueue_struct *queue;
565 out = &priv->headset;
566 work = &priv->hs_delayed_work;
567 queue = priv->hs_workqueue;
568 out->left_step = priv->hs_left_step;
569 out->right_step = priv->hs_right_step;
570 out->step_delay = 5; /* 5 ms between volume ramp steps */
573 out = &priv->handsfree;
574 work = &priv->hf_delayed_work;
575 queue = priv->hf_workqueue;
576 out->left_step = priv->hf_left_step;
577 out->right_step = priv->hf_right_step;
578 out->step_delay = 5; /* 5 ms between volume ramp steps */
579 if (SND_SOC_DAPM_EVENT_ON(event))
589 case SND_SOC_DAPM_POST_PMU:
593 /* don't use volume ramp for power-up */
594 out->left_step = out->left_vol;
595 out->right_step = out->right_vol;
597 if (!delayed_work_pending(work)) {
598 out->ramp = TWL6040_RAMP_UP;
599 queue_delayed_work(queue, work,
600 msecs_to_jiffies(1));
604 case SND_SOC_DAPM_PRE_PMD:
608 if (!delayed_work_pending(work)) {
609 /* use volume ramp for power-down */
610 out->ramp = TWL6040_RAMP_DOWN;
611 INIT_COMPLETION(out->ramp_done);
613 queue_delayed_work(queue, work,
614 msecs_to_jiffies(1));
616 wait_for_completion_timeout(&out->ramp_done,
617 msecs_to_jiffies(2000));
625 /* set headset dac and driver power mode */
626 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
629 int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL;
631 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
632 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
642 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
643 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
648 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
649 struct snd_kcontrol *kcontrol, int event)
655 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
656 struct snd_kcontrol *kcontrol, int event)
658 struct snd_soc_codec *codec = w->codec;
659 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
662 if (SND_SOC_DAPM_EVENT_ON(event)) {
664 if (!strcmp(w->name, "Earphone Driver")) {
665 /* Earphone doesn't support low power mode */
666 priv->hs_power_mode_locked = 1;
667 ret = headset_power_mode(codec, 1);
671 if (!strcmp(w->name, "Earphone Driver")) {
672 priv->hs_power_mode_locked = 0;
673 ret = headset_power_mode(codec, priv->hs_power_mode);
682 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
683 struct snd_soc_jack *jack, int report)
685 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
688 mutex_lock(&priv->mutex);
691 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
692 if (status & TWL6040_PLUGCOMP)
693 snd_soc_jack_report(jack, report, report);
695 snd_soc_jack_report(jack, 0, report);
697 mutex_unlock(&priv->mutex);
700 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
701 struct snd_soc_jack *jack, int report)
703 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
704 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
706 hs_jack->jack = jack;
707 hs_jack->report = report;
709 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
711 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
713 static void twl6040_accessory_work(struct work_struct *work)
715 struct twl6040_data *priv = container_of(work,
716 struct twl6040_data, delayed_work.work);
717 struct snd_soc_codec *codec = priv->codec;
718 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
720 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
723 /* audio interrupt handler */
724 static irqreturn_t twl6040_audio_handler(int irq, void *data)
726 struct snd_soc_codec *codec = data;
727 struct twl6040 *twl6040 = codec->control_data;
728 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
731 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
733 if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT))
734 queue_delayed_work(priv->workqueue, &priv->delayed_work,
735 msecs_to_jiffies(200));
740 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
741 struct snd_ctl_elem_value *ucontrol)
743 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
744 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
745 struct twl6040_output *out = NULL;
746 struct soc_mixer_control *mc =
747 (struct soc_mixer_control *)kcontrol->private_value;
749 unsigned int reg = mc->reg;
751 /* For HS and HF we shadow the values and only actually write
752 * them out when active in order to ensure the amplifier comes on
753 * as quietly as possible. */
755 case TWL6040_REG_HSGAIN:
756 out = &twl6040_priv->headset;
763 out->left_vol = ucontrol->value.integer.value[0];
764 out->right_vol = ucontrol->value.integer.value[1];
769 ret = snd_soc_put_volsw(kcontrol, ucontrol);
776 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
777 struct snd_ctl_elem_value *ucontrol)
779 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
780 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
781 struct twl6040_output *out = &twl6040_priv->headset;
782 struct soc_mixer_control *mc =
783 (struct soc_mixer_control *)kcontrol->private_value;
784 unsigned int reg = mc->reg;
787 case TWL6040_REG_HSGAIN:
788 out = &twl6040_priv->headset;
789 ucontrol->value.integer.value[0] = out->left_vol;
790 ucontrol->value.integer.value[1] = out->right_vol;
797 return snd_soc_get_volsw(kcontrol, ucontrol);
800 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
801 struct snd_ctl_elem_value *ucontrol)
803 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
804 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
805 struct twl6040_output *out = NULL;
806 struct soc_mixer_control *mc =
807 (struct soc_mixer_control *)kcontrol->private_value;
809 unsigned int reg = mc->reg;
811 /* For HS and HF we shadow the values and only actually write
812 * them out when active in order to ensure the amplifier comes on
813 * as quietly as possible. */
815 case TWL6040_REG_HFLGAIN:
816 case TWL6040_REG_HFRGAIN:
817 out = &twl6040_priv->handsfree;
824 out->left_vol = ucontrol->value.integer.value[0];
825 out->right_vol = ucontrol->value.integer.value[1];
830 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
837 static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
838 struct snd_ctl_elem_value *ucontrol)
840 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
841 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
842 struct twl6040_output *out = &twl6040_priv->handsfree;
843 struct soc_mixer_control *mc =
844 (struct soc_mixer_control *)kcontrol->private_value;
845 unsigned int reg = mc->reg;
847 /* If these are cached registers use the cache */
849 case TWL6040_REG_HFLGAIN:
850 case TWL6040_REG_HFRGAIN:
851 out = &twl6040_priv->handsfree;
852 ucontrol->value.integer.value[0] = out->left_vol;
853 ucontrol->value.integer.value[1] = out->right_vol;
860 return snd_soc_get_volsw_2r(kcontrol, ucontrol);
863 /* double control with volume update */
864 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
866 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
867 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
868 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
869 .tlv.p = (tlv_array), \
870 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
871 .put = twl6040_put_volsw, \
872 .private_value = (unsigned long)&(struct soc_mixer_control) \
873 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
874 .max = xmax, .platform_max = xmax, .invert = xinvert} }
876 /* double control with volume update */
877 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
879 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
880 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
881 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
882 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
883 .tlv.p = (tlv_array), \
884 .info = snd_soc_info_volsw_2r, \
885 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
886 .private_value = (unsigned long)&(struct soc_mixer_control) \
887 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
888 .rshift = xshift, .max = xmax, .invert = xinvert}, }
891 * MICATT volume control:
892 * from -6 to 0 dB in 6 dB steps
894 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
897 * MICGAIN volume control:
898 * from 6 to 30 dB in 6 dB steps
900 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
903 * AFMGAIN volume control:
904 * from -18 to 24 dB in 6 dB steps
906 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
909 * HSGAIN volume control:
910 * from -30 to 0 dB in 2 dB steps
912 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
915 * HFGAIN volume control:
916 * from -52 to 6 dB in 2 dB steps
918 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
921 * EPGAIN volume control:
922 * from -24 to 6 dB in 2 dB steps
924 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
926 /* Left analog microphone selection */
927 static const char *twl6040_amicl_texts[] =
928 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
930 /* Right analog microphone selection */
931 static const char *twl6040_amicr_texts[] =
932 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
934 static const struct soc_enum twl6040_enum[] = {
935 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
936 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
939 static const char *twl6040_hs_texts[] = {
940 "Off", "HS DAC", "Line-In amp"
943 static const struct soc_enum twl6040_hs_enum[] = {
944 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
946 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
950 static const char *twl6040_hf_texts[] = {
951 "Off", "HF DAC", "Line-In amp"
954 static const struct soc_enum twl6040_hf_enum[] = {
955 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
957 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
961 static const struct snd_kcontrol_new amicl_control =
962 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
964 static const struct snd_kcontrol_new amicr_control =
965 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
967 /* Headset DAC playback switches */
968 static const struct snd_kcontrol_new hsl_mux_controls =
969 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
971 static const struct snd_kcontrol_new hsr_mux_controls =
972 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
974 /* Handsfree DAC playback switches */
975 static const struct snd_kcontrol_new hfl_mux_controls =
976 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
978 static const struct snd_kcontrol_new hfr_mux_controls =
979 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
981 static const struct snd_kcontrol_new ep_driver_switch_controls =
982 SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0);
984 /* Headset power mode */
985 static const char *twl6040_power_mode_texts[] = {
986 "Low-Power", "High-Perfomance",
989 static const struct soc_enum twl6040_power_mode_enum =
990 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
991 twl6040_power_mode_texts);
993 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
994 struct snd_ctl_elem_value *ucontrol)
996 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
997 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
999 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
1004 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
1005 struct snd_ctl_elem_value *ucontrol)
1007 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1008 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1009 int high_perf = ucontrol->value.enumerated.item[0];
1012 if (!priv->hs_power_mode_locked)
1013 ret = headset_power_mode(codec, high_perf);
1016 priv->hs_power_mode = high_perf;
1021 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
1022 struct snd_ctl_elem_value *ucontrol)
1024 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1025 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1027 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1032 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1033 struct snd_ctl_elem_value *ucontrol)
1035 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1036 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1038 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1043 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1045 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1047 return priv->pll_power_mode;
1049 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1051 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1053 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1054 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1055 SOC_DOUBLE_TLV("Capture Volume",
1056 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1059 SOC_DOUBLE_TLV("Aux FM Volume",
1060 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1062 /* Playback gains */
1063 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
1064 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
1065 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
1066 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
1067 SOC_SINGLE_TLV("Earphone Playback Volume",
1068 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1070 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1071 twl6040_headset_power_get_enum,
1072 twl6040_headset_power_put_enum),
1074 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1075 twl6040_pll_get_enum, twl6040_pll_put_enum),
1078 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1080 SND_SOC_DAPM_INPUT("MAINMIC"),
1081 SND_SOC_DAPM_INPUT("HSMIC"),
1082 SND_SOC_DAPM_INPUT("SUBMIC"),
1083 SND_SOC_DAPM_INPUT("AFML"),
1084 SND_SOC_DAPM_INPUT("AFMR"),
1087 SND_SOC_DAPM_OUTPUT("HSOL"),
1088 SND_SOC_DAPM_OUTPUT("HSOR"),
1089 SND_SOC_DAPM_OUTPUT("HFL"),
1090 SND_SOC_DAPM_OUTPUT("HFR"),
1091 SND_SOC_DAPM_OUTPUT("EP"),
1093 /* Analog input muxes for the capture amplifiers */
1094 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1095 SND_SOC_NOPM, 0, 0, &amicl_control),
1096 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1097 SND_SOC_NOPM, 0, 0, &amicr_control),
1099 /* Analog capture PGAs */
1100 SND_SOC_DAPM_PGA("MicAmpL",
1101 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1102 SND_SOC_DAPM_PGA("MicAmpR",
1103 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1105 /* Auxiliary FM PGAs */
1106 SND_SOC_DAPM_PGA("AFMAmpL",
1107 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1108 SND_SOC_DAPM_PGA("AFMAmpR",
1109 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1112 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1113 TWL6040_REG_MICLCTL, 2, 0),
1114 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1115 TWL6040_REG_MICRCTL, 2, 0),
1117 /* Microphone bias */
1118 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1119 TWL6040_REG_AMICBCTL, 0, 0),
1120 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1121 TWL6040_REG_AMICBCTL, 4, 0),
1122 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1123 TWL6040_REG_DMICBCTL, 0, 0),
1124 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1125 TWL6040_REG_DMICBCTL, 4, 0),
1128 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1129 TWL6040_REG_HSLCTL, 0, 0,
1130 twl6040_hs_dac_event,
1131 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1132 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1133 TWL6040_REG_HSRCTL, 0, 0,
1134 twl6040_hs_dac_event,
1135 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1136 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1137 TWL6040_REG_HFLCTL, 0, 0,
1138 twl6040_power_mode_event,
1139 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1140 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1141 TWL6040_REG_HFRCTL, 0, 0,
1142 twl6040_power_mode_event,
1143 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1145 SND_SOC_DAPM_MUX("HF Left Playback",
1146 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1147 SND_SOC_DAPM_MUX("HF Right Playback",
1148 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1149 /* Analog playback Muxes */
1150 SND_SOC_DAPM_MUX("HS Left Playback",
1151 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1152 SND_SOC_DAPM_MUX("HS Right Playback",
1153 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1155 /* Analog playback drivers */
1156 SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver",
1157 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1159 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1160 SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver",
1161 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1163 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1164 SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver",
1165 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1167 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1168 SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver",
1169 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1171 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1172 SND_SOC_DAPM_SWITCH_E("Earphone Driver",
1173 SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls,
1174 twl6040_power_mode_event,
1175 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1177 /* Analog playback PGAs */
1178 SND_SOC_DAPM_PGA("HFDAC Left PGA",
1179 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1180 SND_SOC_DAPM_PGA("HFDAC Right PGA",
1181 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1185 static const struct snd_soc_dapm_route intercon[] = {
1187 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1188 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1189 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1191 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1192 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1193 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1195 {"MicAmpL", NULL, "Analog Left Capture Route"},
1196 {"MicAmpR", NULL, "Analog Right Capture Route"},
1198 {"ADC Left", NULL, "MicAmpL"},
1199 {"ADC Right", NULL, "MicAmpR"},
1202 {"AFMAmpL", "NULL", "AFML"},
1203 {"AFMAmpR", "NULL", "AFMR"},
1205 {"HS Left Playback", "HS DAC", "HSDAC Left"},
1206 {"HS Left Playback", "Line-In amp", "AFMAmpL"},
1208 {"HS Right Playback", "HS DAC", "HSDAC Right"},
1209 {"HS Right Playback", "Line-In amp", "AFMAmpR"},
1211 {"Headset Left Driver", "NULL", "HS Left Playback"},
1212 {"Headset Right Driver", "NULL", "HS Right Playback"},
1214 {"HSOL", NULL, "Headset Left Driver"},
1215 {"HSOR", NULL, "Headset Right Driver"},
1217 /* Earphone playback path */
1218 {"Earphone Driver", "Switch", "HSDAC Left"},
1219 {"EP", NULL, "Earphone Driver"},
1221 {"HF Left Playback", "HF DAC", "HFDAC Left"},
1222 {"HF Left Playback", "Line-In amp", "AFMAmpL"},
1224 {"HF Right Playback", "HF DAC", "HFDAC Right"},
1225 {"HF Right Playback", "Line-In amp", "AFMAmpR"},
1227 {"HFDAC Left PGA", NULL, "HF Left Playback"},
1228 {"HFDAC Right PGA", NULL, "HF Right Playback"},
1230 {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"},
1231 {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"},
1233 {"HFL", NULL, "Handsfree Left Driver"},
1234 {"HFR", NULL, "Handsfree Right Driver"},
1237 static int twl6040_add_widgets(struct snd_soc_codec *codec)
1239 struct snd_soc_dapm_context *dapm = &codec->dapm;
1241 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
1242 ARRAY_SIZE(twl6040_dapm_widgets));
1243 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1244 snd_soc_dapm_new_widgets(dapm);
1249 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1250 enum snd_soc_bias_level level)
1252 struct twl6040 *twl6040 = codec->control_data;
1253 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1257 case SND_SOC_BIAS_ON:
1259 case SND_SOC_BIAS_PREPARE:
1261 case SND_SOC_BIAS_STANDBY:
1262 if (priv->codec_powered)
1265 ret = twl6040_power(twl6040, 1);
1269 priv->codec_powered = 1;
1271 twl6040_restore_regs(codec);
1273 /* Set external boost GPO */
1274 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1276 case SND_SOC_BIAS_OFF:
1277 if (!priv->codec_powered)
1280 twl6040_power(twl6040, 0);
1281 priv->codec_powered = 0;
1285 codec->dapm.bias_level = level;
1290 static int twl6040_startup(struct snd_pcm_substream *substream,
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);
1297 snd_pcm_hw_constraint_list(substream->runtime, 0,
1298 SNDRV_PCM_HW_PARAM_RATE,
1299 &sysclk_constraints[priv->pll_power_mode]);
1304 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1305 struct snd_pcm_hw_params *params,
1306 struct snd_soc_dai *dai)
1308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1309 struct snd_soc_codec *codec = rtd->codec;
1310 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1313 rate = params_rate(params);
1319 /* These rates are not supported when HPPLL is in use */
1320 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1321 dev_err(codec->dev, "HPPLL does not support rate %d\n",
1325 /* Capture is not supported with 17.64MHz sysclk */
1326 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1328 "capture mode is not supported at %dHz\n",
1332 priv->sysclk = 17640000;
1339 priv->sysclk = 19200000;
1342 dev_err(codec->dev, "unsupported rate %d\n", rate);
1349 static int twl6040_prepare(struct snd_pcm_substream *substream,
1350 struct snd_soc_dai *dai)
1352 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1353 struct snd_soc_codec *codec = rtd->codec;
1354 struct twl6040 *twl6040 = codec->control_data;
1355 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1358 if (!priv->sysclk) {
1360 "no mclk configured, call set_sysclk() on init\n");
1364 if ((priv->sysclk == 17640000) && priv->non_lp) {
1366 "some enabled paths aren't supported at %dHz\n",
1371 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1373 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1380 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1381 int clk_id, unsigned int freq, int dir)
1383 struct snd_soc_codec *codec = codec_dai->codec;
1384 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1387 case TWL6040_SYSCLK_SEL_LPPLL:
1388 case TWL6040_SYSCLK_SEL_HPPLL:
1390 priv->clk_in = freq;
1393 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1400 static struct snd_soc_dai_ops twl6040_dai_ops = {
1401 .startup = twl6040_startup,
1402 .hw_params = twl6040_hw_params,
1403 .prepare = twl6040_prepare,
1404 .set_sysclk = twl6040_set_dai_sysclk,
1407 static struct snd_soc_dai_driver twl6040_dai[] = {
1409 .name = "twl6040-hifi",
1411 .stream_name = "Playback",
1414 .rates = TWL6040_RATES,
1415 .formats = TWL6040_FORMATS,
1418 .stream_name = "Capture",
1421 .rates = TWL6040_RATES,
1422 .formats = TWL6040_FORMATS,
1424 .ops = &twl6040_dai_ops,
1427 .name = "twl6040-ul",
1429 .stream_name = "Capture",
1432 .rates = TWL6040_RATES,
1433 .formats = TWL6040_FORMATS,
1435 .ops = &twl6040_dai_ops,
1438 .name = "twl6040-dl1",
1440 .stream_name = "Headset Playback",
1443 .rates = TWL6040_RATES,
1444 .formats = TWL6040_FORMATS,
1446 .ops = &twl6040_dai_ops,
1449 .name = "twl6040-dl2",
1451 .stream_name = "Handsfree Playback",
1454 .rates = TWL6040_RATES,
1455 .formats = TWL6040_FORMATS,
1457 .ops = &twl6040_dai_ops,
1460 .name = "twl6040-vib",
1462 .stream_name = "Vibra Playback",
1465 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1466 .formats = TWL6040_FORMATS,
1468 .ops = &twl6040_dai_ops,
1473 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1475 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1480 static int twl6040_resume(struct snd_soc_codec *codec)
1482 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1483 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1488 #define twl6040_suspend NULL
1489 #define twl6040_resume NULL
1492 static int twl6040_probe(struct snd_soc_codec *codec)
1494 struct twl6040_data *priv;
1495 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1496 struct platform_device *pdev = container_of(codec->dev,
1497 struct platform_device, dev);
1500 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1503 snd_soc_codec_set_drvdata(codec, priv);
1505 priv->codec = codec;
1506 codec->control_data = dev_get_drvdata(codec->dev->parent);
1508 if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1509 priv->hs_left_step = pdata->hs_left_step;
1510 priv->hs_right_step = pdata->hs_right_step;
1512 priv->hs_left_step = 1;
1513 priv->hs_right_step = 1;
1516 if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1517 priv->hf_left_step = pdata->hf_left_step;
1518 priv->hf_right_step = pdata->hf_right_step;
1520 priv->hf_left_step = 1;
1521 priv->hf_right_step = 1;
1524 priv->plug_irq = platform_get_irq(pdev, 0);
1525 if (priv->plug_irq < 0) {
1526 dev_err(codec->dev, "invalid irq\n");
1531 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1532 if (!priv->workqueue) {
1537 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1539 mutex_init(&priv->mutex);
1541 init_completion(&priv->headset.ramp_done);
1542 init_completion(&priv->handsfree.ramp_done);
1544 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1545 if (priv->hf_workqueue == NULL) {
1549 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1550 if (priv->hs_workqueue == NULL) {
1555 INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
1556 INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
1558 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1559 0, "twl6040_irq_plug", codec);
1561 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1565 twl6040_init_chip(codec);
1567 /* power on device */
1568 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1572 snd_soc_add_controls(codec, twl6040_snd_controls,
1573 ARRAY_SIZE(twl6040_snd_controls));
1574 twl6040_add_widgets(codec);
1579 free_irq(priv->plug_irq, codec);
1581 destroy_workqueue(priv->hs_workqueue);
1583 destroy_workqueue(priv->hf_workqueue);
1585 destroy_workqueue(priv->workqueue);
1591 static int twl6040_remove(struct snd_soc_codec *codec)
1593 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1595 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1596 free_irq(priv->plug_irq, codec);
1597 destroy_workqueue(priv->workqueue);
1598 destroy_workqueue(priv->hf_workqueue);
1599 destroy_workqueue(priv->hs_workqueue);
1605 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1606 .probe = twl6040_probe,
1607 .remove = twl6040_remove,
1608 .suspend = twl6040_suspend,
1609 .resume = twl6040_resume,
1610 .read = twl6040_read_reg_cache,
1611 .write = twl6040_write,
1612 .set_bias_level = twl6040_set_bias_level,
1613 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1614 .reg_word_size = sizeof(u8),
1615 .reg_cache_default = twl6040_reg,
1618 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1620 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1621 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1624 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1626 snd_soc_unregister_codec(&pdev->dev);
1630 static struct platform_driver twl6040_codec_driver = {
1632 .name = "twl6040-codec",
1633 .owner = THIS_MODULE,
1635 .probe = twl6040_codec_probe,
1636 .remove = __devexit_p(twl6040_codec_remove),
1639 static int __init twl6040_codec_init(void)
1641 return platform_driver_register(&twl6040_codec_driver);
1643 module_init(twl6040_codec_init);
1645 static void __exit twl6040_codec_exit(void)
1647 platform_driver_unregister(&twl6040_codec_driver);
1649 module_exit(twl6040_codec_exit);
1651 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1652 MODULE_AUTHOR("Misael Lopez Cruz");
1653 MODULE_LICENSE("GPL");