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 /* Shadow register used by the driver */
61 #define TWL6040_REG_SW_SHADOW 0x2F
62 #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1)
64 /* TWL6040_REG_SW_SHADOW (0x2F) fields */
65 #define TWL6040_EAR_PATH_ENABLE 0x01
67 struct twl6040_output {
73 unsigned int step_delay;
76 struct completion ramp_done;
79 struct twl6040_jack_data {
80 struct snd_soc_jack *jack;
84 /* codec private data */
92 int hs_power_mode_locked;
99 struct twl6040_jack_data hs_jack;
100 struct snd_soc_codec *codec;
101 struct workqueue_struct *workqueue;
102 struct delayed_work delayed_work;
104 struct twl6040_output headset;
105 struct twl6040_output handsfree;
106 struct workqueue_struct *hf_workqueue;
107 struct workqueue_struct *hs_workqueue;
108 struct delayed_work hs_delayed_work;
109 struct delayed_work hf_delayed_work;
113 * twl6040 register cache & default register settings
115 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
116 0x00, /* not used 0x00 */
117 0x4B, /* REG_ASICID 0x01 (ro) */
118 0x00, /* REG_ASICREV 0x02 (ro) */
119 0x00, /* REG_INTID 0x03 */
120 0x00, /* REG_INTMR 0x04 */
121 0x00, /* REG_NCPCTRL 0x05 */
122 0x00, /* REG_LDOCTL 0x06 */
123 0x60, /* REG_HPPLLCTL 0x07 */
124 0x00, /* REG_LPPLLCTL 0x08 */
125 0x4A, /* REG_LPPLLDIV 0x09 */
126 0x00, /* REG_AMICBCTL 0x0A */
127 0x00, /* REG_DMICBCTL 0x0B */
128 0x00, /* REG_MICLCTL 0x0C */
129 0x00, /* REG_MICRCTL 0x0D */
130 0x00, /* REG_MICGAIN 0x0E */
131 0x1B, /* REG_LINEGAIN 0x0F */
132 0x00, /* REG_HSLCTL 0x10 */
133 0x00, /* REG_HSRCTL 0x11 */
134 0x00, /* REG_HSGAIN 0x12 */
135 0x00, /* REG_EARCTL 0x13 */
136 0x00, /* REG_HFLCTL 0x14 */
137 0x00, /* REG_HFLGAIN 0x15 */
138 0x00, /* REG_HFRCTL 0x16 */
139 0x00, /* REG_HFRGAIN 0x17 */
140 0x00, /* REG_VIBCTLL 0x18 */
141 0x00, /* REG_VIBDATL 0x19 */
142 0x00, /* REG_VIBCTLR 0x1A */
143 0x00, /* REG_VIBDATR 0x1B */
144 0x00, /* REG_HKCTL1 0x1C */
145 0x00, /* REG_HKCTL2 0x1D */
146 0x00, /* REG_GPOCTL 0x1E */
147 0x00, /* REG_ALB 0x1F */
148 0x00, /* REG_DLB 0x20 */
149 0x00, /* not used 0x21 */
150 0x00, /* not used 0x22 */
151 0x00, /* not used 0x23 */
152 0x00, /* not used 0x24 */
153 0x00, /* not used 0x25 */
154 0x00, /* not used 0x26 */
155 0x00, /* not used 0x27 */
156 0x00, /* REG_TRIM1 0x28 */
157 0x00, /* REG_TRIM2 0x29 */
158 0x00, /* REG_TRIM3 0x2A */
159 0x00, /* REG_HSOTRIM 0x2B */
160 0x00, /* REG_HFOTRIM 0x2C */
161 0x09, /* REG_ACCCTL 0x2D */
162 0x00, /* REG_STATUS 0x2E (ro) */
164 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */
167 /* List of registers to be restored after power up */
168 static const int twl6040_restore_list[] = {
172 TWL6040_REG_LINEGAIN,
183 /* set of rates for each pll: low-power and high-performance */
184 static unsigned int lp_rates[] = {
196 static unsigned int hp_rates[] = {
204 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
205 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
206 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
210 * read twl6040 register cache
212 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
215 u8 *cache = codec->reg_cache;
217 if (reg >= TWL6040_CACHEREGNUM)
224 * write twl6040 register cache
226 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
229 u8 *cache = codec->reg_cache;
231 if (reg >= TWL6040_CACHEREGNUM)
237 * read from twl6040 hardware register
239 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
242 struct twl6040 *twl6040 = codec->control_data;
245 if (reg >= TWL6040_CACHEREGNUM)
248 if (likely(reg < TWL6040_REG_SW_SHADOW)) {
249 value = twl6040_reg_read(twl6040, reg);
250 twl6040_write_reg_cache(codec, reg, value);
252 value = twl6040_read_reg_cache(codec, reg);
259 * write to the twl6040 register space
261 static int twl6040_write(struct snd_soc_codec *codec,
262 unsigned int reg, unsigned int value)
264 struct twl6040 *twl6040 = codec->control_data;
266 if (reg >= TWL6040_CACHEREGNUM)
269 twl6040_write_reg_cache(codec, reg, value);
270 if (likely(reg < TWL6040_REG_SW_SHADOW))
271 return twl6040_reg_write(twl6040, reg, value);
276 static void twl6040_init_chip(struct snd_soc_codec *codec)
278 struct twl6040 *twl6040 = codec->control_data;
281 val = twl6040_get_revid(twl6040);
282 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
284 /* Change chip defaults */
285 /* No imput selected for microphone amplifiers */
286 twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
287 twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
290 * We need to lower the default gain values, so the ramp code
291 * can work correctly for the first playback.
292 * This reduces the pop noise heard at the first playback.
294 twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
295 twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
296 twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
297 twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
298 twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
301 static void twl6040_restore_regs(struct snd_soc_codec *codec)
303 u8 *cache = codec->reg_cache;
306 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
307 reg = twl6040_restore_list[i];
308 twl6040_write(codec, reg, cache[reg]);
313 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
315 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
316 unsigned int left_step, unsigned int right_step)
319 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
320 struct twl6040_output *headset = &priv->headset;
321 int left_complete = 0, right_complete = 0;
325 left_step = (left_step > 0xF) ? 0xF : left_step;
326 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
327 val = (~reg & TWL6040_HSL_VOL_MASK);
329 if (headset->ramp == TWL6040_RAMP_UP) {
331 if (val < headset->left_vol) {
332 if (val + left_step > headset->left_vol)
333 val = headset->left_vol;
337 reg &= ~TWL6040_HSL_VOL_MASK;
338 twl6040_write(codec, TWL6040_REG_HSGAIN,
339 (reg | (~val & TWL6040_HSL_VOL_MASK)));
343 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
346 if ((int)val - (int)left_step < 0)
351 reg &= ~TWL6040_HSL_VOL_MASK;
352 twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
353 (~val & TWL6040_HSL_VOL_MASK));
360 right_step = (right_step > 0xF) ? 0xF : right_step;
361 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
362 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
364 if (headset->ramp == TWL6040_RAMP_UP) {
366 if (val < headset->right_vol) {
367 if (val + right_step > headset->right_vol)
368 val = headset->right_vol;
372 reg &= ~TWL6040_HSR_VOL_MASK;
373 twl6040_write(codec, TWL6040_REG_HSGAIN,
374 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
378 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
381 if ((int)val - (int)right_step < 0)
386 reg &= ~TWL6040_HSR_VOL_MASK;
387 twl6040_write(codec, TWL6040_REG_HSGAIN,
388 reg | (~val << TWL6040_HSR_VOL_SHIFT));
394 return left_complete & right_complete;
398 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
400 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
401 unsigned int left_step, unsigned int right_step)
403 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
404 struct twl6040_output *handsfree = &priv->handsfree;
405 int left_complete = 0, right_complete = 0;
409 left_step = (left_step > 0x1D) ? 0x1D : left_step;
410 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
412 val = (reg & TWL6040_HF_VOL_MASK);
413 if (handsfree->ramp == TWL6040_RAMP_UP) {
415 if (val < handsfree->left_vol) {
416 if (val + left_step > handsfree->left_vol)
417 val = handsfree->left_vol;
421 reg &= ~TWL6040_HF_VOL_MASK;
422 twl6040_write(codec, TWL6040_REG_HFLGAIN,
427 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
430 if ((int)val - (int)left_step < 0)
435 reg &= ~TWL6040_HF_VOL_MASK;
436 twl6040_write(codec, TWL6040_REG_HFLGAIN,
444 right_step = (right_step > 0x1D) ? 0x1D : right_step;
445 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
447 val = (reg & TWL6040_HF_VOL_MASK);
448 if (handsfree->ramp == TWL6040_RAMP_UP) {
450 if (val < handsfree->right_vol) {
451 if (val + right_step > handsfree->right_vol)
452 val = handsfree->right_vol;
456 reg &= ~TWL6040_HF_VOL_MASK;
457 twl6040_write(codec, TWL6040_REG_HFRGAIN,
462 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
465 if ((int)val - (int)right_step < 0)
470 reg &= ~TWL6040_HF_VOL_MASK;
471 twl6040_write(codec, TWL6040_REG_HFRGAIN,
476 return left_complete & right_complete;
480 * This work ramps both output PGAs at stream start/stop time to
481 * minimise pop associated with DAPM power switching.
483 static void twl6040_pga_hs_work(struct work_struct *work)
485 struct twl6040_data *priv =
486 container_of(work, struct twl6040_data, hs_delayed_work.work);
487 struct snd_soc_codec *codec = priv->codec;
488 struct twl6040_output *headset = &priv->headset;
489 unsigned int delay = headset->step_delay;
490 int i, headset_complete;
492 /* do we need to ramp at all ? */
493 if (headset->ramp == TWL6040_RAMP_NONE)
496 /* HS PGA volumes have 4 bits of resolution to ramp */
497 for (i = 0; i <= 16; i++) {
498 headset_complete = twl6040_hs_ramp_step(codec,
500 headset->right_step);
502 /* ramp finished ? */
503 if (headset_complete)
507 * TODO: tune: delay is longer over 0dB
508 * as increases are larger.
511 schedule_timeout_interruptible(msecs_to_jiffies(delay +
514 schedule_timeout_interruptible(msecs_to_jiffies(delay));
517 if (headset->ramp == TWL6040_RAMP_DOWN) {
519 complete(&headset->ramp_done);
523 headset->ramp = TWL6040_RAMP_NONE;
526 static void twl6040_pga_hf_work(struct work_struct *work)
528 struct twl6040_data *priv =
529 container_of(work, struct twl6040_data, hf_delayed_work.work);
530 struct snd_soc_codec *codec = priv->codec;
531 struct twl6040_output *handsfree = &priv->handsfree;
532 unsigned int delay = handsfree->step_delay;
533 int i, handsfree_complete;
535 /* do we need to ramp at all ? */
536 if (handsfree->ramp == TWL6040_RAMP_NONE)
539 /* HF PGA volumes have 5 bits of resolution to ramp */
540 for (i = 0; i <= 32; i++) {
541 handsfree_complete = twl6040_hf_ramp_step(codec,
542 handsfree->left_step,
543 handsfree->right_step);
545 /* ramp finished ? */
546 if (handsfree_complete)
550 * TODO: tune: delay is longer over 0dB
551 * as increases are larger.
554 schedule_timeout_interruptible(msecs_to_jiffies(delay +
557 schedule_timeout_interruptible(msecs_to_jiffies(delay));
561 if (handsfree->ramp == TWL6040_RAMP_DOWN) {
562 handsfree->active = 0;
563 complete(&handsfree->ramp_done);
565 handsfree->active = 1;
566 handsfree->ramp = TWL6040_RAMP_NONE;
569 static int pga_event(struct snd_soc_dapm_widget *w,
570 struct snd_kcontrol *kcontrol, int event)
572 struct snd_soc_codec *codec = w->codec;
573 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
574 struct twl6040_output *out;
575 struct delayed_work *work;
576 struct workqueue_struct *queue;
581 out = &priv->headset;
582 work = &priv->hs_delayed_work;
583 queue = priv->hs_workqueue;
584 out->left_step = priv->hs_left_step;
585 out->right_step = priv->hs_right_step;
586 out->step_delay = 5; /* 5 ms between volume ramp steps */
589 out = &priv->handsfree;
590 work = &priv->hf_delayed_work;
591 queue = priv->hf_workqueue;
592 out->left_step = priv->hf_left_step;
593 out->right_step = priv->hf_right_step;
594 out->step_delay = 5; /* 5 ms between volume ramp steps */
595 if (SND_SOC_DAPM_EVENT_ON(event))
605 case SND_SOC_DAPM_POST_PMU:
609 /* don't use volume ramp for power-up */
610 out->left_step = out->left_vol;
611 out->right_step = out->right_vol;
613 if (!delayed_work_pending(work)) {
614 out->ramp = TWL6040_RAMP_UP;
615 queue_delayed_work(queue, work,
616 msecs_to_jiffies(1));
620 case SND_SOC_DAPM_PRE_PMD:
624 if (!delayed_work_pending(work)) {
625 /* use volume ramp for power-down */
626 out->ramp = TWL6040_RAMP_DOWN;
627 INIT_COMPLETION(out->ramp_done);
629 queue_delayed_work(queue, work,
630 msecs_to_jiffies(1));
632 wait_for_completion_timeout(&out->ramp_done,
633 msecs_to_jiffies(2000));
641 /* set headset dac and driver power mode */
642 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
645 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
647 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
648 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
658 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
659 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
664 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
665 struct snd_kcontrol *kcontrol, int event)
671 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
672 struct snd_kcontrol *kcontrol, int event)
674 struct snd_soc_codec *codec = w->codec;
675 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
678 if (SND_SOC_DAPM_EVENT_ON(event)) {
680 if (!strcmp(w->name, "Earphone Driver")) {
681 /* Earphone doesn't support low power mode */
682 priv->hs_power_mode_locked = 1;
683 ret = headset_power_mode(codec, 1);
687 if (!strcmp(w->name, "Earphone Driver")) {
688 priv->hs_power_mode_locked = 0;
689 ret = headset_power_mode(codec, priv->hs_power_mode);
698 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
699 struct snd_soc_jack *jack, int report)
701 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
704 mutex_lock(&priv->mutex);
707 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
708 if (status & TWL6040_PLUGCOMP)
709 snd_soc_jack_report(jack, report, report);
711 snd_soc_jack_report(jack, 0, report);
713 mutex_unlock(&priv->mutex);
716 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
717 struct snd_soc_jack *jack, int report)
719 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
720 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
722 hs_jack->jack = jack;
723 hs_jack->report = report;
725 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
727 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
729 static void twl6040_accessory_work(struct work_struct *work)
731 struct twl6040_data *priv = container_of(work,
732 struct twl6040_data, delayed_work.work);
733 struct snd_soc_codec *codec = priv->codec;
734 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
736 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
739 /* audio interrupt handler */
740 static irqreturn_t twl6040_audio_handler(int irq, void *data)
742 struct snd_soc_codec *codec = data;
743 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
745 queue_delayed_work(priv->workqueue, &priv->delayed_work,
746 msecs_to_jiffies(200));
751 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_value *ucontrol)
754 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
755 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
756 struct twl6040_output *out = NULL;
757 struct soc_mixer_control *mc =
758 (struct soc_mixer_control *)kcontrol->private_value;
760 unsigned int reg = mc->reg;
762 /* For HS and HF we shadow the values and only actually write
763 * them out when active in order to ensure the amplifier comes on
764 * as quietly as possible. */
766 case TWL6040_REG_HSGAIN:
767 out = &twl6040_priv->headset;
774 out->left_vol = ucontrol->value.integer.value[0];
775 out->right_vol = ucontrol->value.integer.value[1];
780 ret = snd_soc_put_volsw(kcontrol, ucontrol);
787 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
788 struct snd_ctl_elem_value *ucontrol)
790 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
791 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
792 struct twl6040_output *out = &twl6040_priv->headset;
793 struct soc_mixer_control *mc =
794 (struct soc_mixer_control *)kcontrol->private_value;
795 unsigned int reg = mc->reg;
798 case TWL6040_REG_HSGAIN:
799 out = &twl6040_priv->headset;
800 ucontrol->value.integer.value[0] = out->left_vol;
801 ucontrol->value.integer.value[1] = out->right_vol;
808 return snd_soc_get_volsw(kcontrol, ucontrol);
811 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
812 struct snd_ctl_elem_value *ucontrol)
814 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
816 struct twl6040_output *out = NULL;
817 struct soc_mixer_control *mc =
818 (struct soc_mixer_control *)kcontrol->private_value;
820 unsigned int reg = mc->reg;
822 /* For HS and HF we shadow the values and only actually write
823 * them out when active in order to ensure the amplifier comes on
824 * as quietly as possible. */
826 case TWL6040_REG_HFLGAIN:
827 case TWL6040_REG_HFRGAIN:
828 out = &twl6040_priv->handsfree;
835 out->left_vol = ucontrol->value.integer.value[0];
836 out->right_vol = ucontrol->value.integer.value[1];
841 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
848 static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
849 struct snd_ctl_elem_value *ucontrol)
851 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
852 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
853 struct twl6040_output *out = &twl6040_priv->handsfree;
854 struct soc_mixer_control *mc =
855 (struct soc_mixer_control *)kcontrol->private_value;
856 unsigned int reg = mc->reg;
858 /* If these are cached registers use the cache */
860 case TWL6040_REG_HFLGAIN:
861 case TWL6040_REG_HFRGAIN:
862 out = &twl6040_priv->handsfree;
863 ucontrol->value.integer.value[0] = out->left_vol;
864 ucontrol->value.integer.value[1] = out->right_vol;
871 return snd_soc_get_volsw_2r(kcontrol, ucontrol);
874 /* double control with volume update */
875 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
877 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
878 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
879 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
880 .tlv.p = (tlv_array), \
881 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
882 .put = twl6040_put_volsw, \
883 .private_value = (unsigned long)&(struct soc_mixer_control) \
884 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
885 .max = xmax, .platform_max = xmax, .invert = xinvert} }
887 /* double control with volume update */
888 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
890 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
891 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
892 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
893 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
894 .tlv.p = (tlv_array), \
895 .info = snd_soc_info_volsw_2r, \
896 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
897 .private_value = (unsigned long)&(struct soc_mixer_control) \
898 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
899 .rshift = xshift, .max = xmax, .invert = xinvert}, }
902 * MICATT volume control:
903 * from -6 to 0 dB in 6 dB steps
905 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
908 * MICGAIN volume control:
909 * from 6 to 30 dB in 6 dB steps
911 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
914 * AFMGAIN volume control:
915 * from -18 to 24 dB in 6 dB steps
917 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
920 * HSGAIN volume control:
921 * from -30 to 0 dB in 2 dB steps
923 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
926 * HFGAIN volume control:
927 * from -52 to 6 dB in 2 dB steps
929 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
932 * EPGAIN volume control:
933 * from -24 to 6 dB in 2 dB steps
935 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
937 /* Left analog microphone selection */
938 static const char *twl6040_amicl_texts[] =
939 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
941 /* Right analog microphone selection */
942 static const char *twl6040_amicr_texts[] =
943 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
945 static const struct soc_enum twl6040_enum[] = {
946 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
947 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
950 static const char *twl6040_hs_texts[] = {
951 "Off", "HS DAC", "Line-In amp"
954 static const struct soc_enum twl6040_hs_enum[] = {
955 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
957 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
961 static const char *twl6040_hf_texts[] = {
962 "Off", "HF DAC", "Line-In amp"
965 static const struct soc_enum twl6040_hf_enum[] = {
966 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
968 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
972 static const struct snd_kcontrol_new amicl_control =
973 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
975 static const struct snd_kcontrol_new amicr_control =
976 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
978 /* Headset DAC playback switches */
979 static const struct snd_kcontrol_new hsl_mux_controls =
980 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
982 static const struct snd_kcontrol_new hsr_mux_controls =
983 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
985 /* Handsfree DAC playback switches */
986 static const struct snd_kcontrol_new hfl_mux_controls =
987 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
989 static const struct snd_kcontrol_new hfr_mux_controls =
990 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
992 static const struct snd_kcontrol_new ep_path_enable_control =
993 SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
995 static const struct snd_kcontrol_new auxl_switch_control =
996 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
998 static const struct snd_kcontrol_new auxr_switch_control =
999 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
1001 /* Headset power mode */
1002 static const char *twl6040_power_mode_texts[] = {
1003 "Low-Power", "High-Perfomance",
1006 static const struct soc_enum twl6040_power_mode_enum =
1007 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
1008 twl6040_power_mode_texts);
1010 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
1011 struct snd_ctl_elem_value *ucontrol)
1013 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1014 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1016 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
1021 static int twl6040_headset_power_put_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);
1026 int high_perf = ucontrol->value.enumerated.item[0];
1029 if (!priv->hs_power_mode_locked)
1030 ret = headset_power_mode(codec, high_perf);
1033 priv->hs_power_mode = high_perf;
1038 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_value *ucontrol)
1041 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1042 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1044 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1049 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1050 struct snd_ctl_elem_value *ucontrol)
1052 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1053 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1055 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1060 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1062 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1064 return priv->pll_power_mode;
1066 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1068 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1070 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1071 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1072 SOC_DOUBLE_TLV("Capture Volume",
1073 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1076 SOC_DOUBLE_TLV("Aux FM Volume",
1077 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1079 /* Playback gains */
1080 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
1081 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
1082 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
1083 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
1084 SOC_SINGLE_TLV("Earphone Playback Volume",
1085 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1087 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1088 twl6040_headset_power_get_enum,
1089 twl6040_headset_power_put_enum),
1091 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1092 twl6040_pll_get_enum, twl6040_pll_put_enum),
1095 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1097 SND_SOC_DAPM_INPUT("MAINMIC"),
1098 SND_SOC_DAPM_INPUT("HSMIC"),
1099 SND_SOC_DAPM_INPUT("SUBMIC"),
1100 SND_SOC_DAPM_INPUT("AFML"),
1101 SND_SOC_DAPM_INPUT("AFMR"),
1104 SND_SOC_DAPM_OUTPUT("HSOL"),
1105 SND_SOC_DAPM_OUTPUT("HSOR"),
1106 SND_SOC_DAPM_OUTPUT("HFL"),
1107 SND_SOC_DAPM_OUTPUT("HFR"),
1108 SND_SOC_DAPM_OUTPUT("EP"),
1109 SND_SOC_DAPM_OUTPUT("AUXL"),
1110 SND_SOC_DAPM_OUTPUT("AUXR"),
1112 /* Analog input muxes for the capture amplifiers */
1113 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1114 SND_SOC_NOPM, 0, 0, &amicl_control),
1115 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1116 SND_SOC_NOPM, 0, 0, &amicr_control),
1118 /* Analog capture PGAs */
1119 SND_SOC_DAPM_PGA("MicAmpL",
1120 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1121 SND_SOC_DAPM_PGA("MicAmpR",
1122 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1124 /* Auxiliary FM PGAs */
1125 SND_SOC_DAPM_PGA("AFMAmpL",
1126 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1127 SND_SOC_DAPM_PGA("AFMAmpR",
1128 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1131 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1132 TWL6040_REG_MICLCTL, 2, 0),
1133 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1134 TWL6040_REG_MICRCTL, 2, 0),
1136 /* Microphone bias */
1137 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1138 TWL6040_REG_AMICBCTL, 0, 0),
1139 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1140 TWL6040_REG_AMICBCTL, 4, 0),
1141 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1142 TWL6040_REG_DMICBCTL, 0, 0),
1143 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1144 TWL6040_REG_DMICBCTL, 4, 0),
1147 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1148 TWL6040_REG_HSLCTL, 0, 0,
1149 twl6040_hs_dac_event,
1150 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1151 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1152 TWL6040_REG_HSRCTL, 0, 0,
1153 twl6040_hs_dac_event,
1154 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1155 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1156 TWL6040_REG_HFLCTL, 0, 0,
1157 twl6040_power_mode_event,
1158 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1159 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1160 TWL6040_REG_HFRCTL, 0, 0,
1161 twl6040_power_mode_event,
1162 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1164 SND_SOC_DAPM_MUX("Handsfree Left Playback",
1165 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1166 SND_SOC_DAPM_MUX("Handsfree Right Playback",
1167 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1168 /* Analog playback Muxes */
1169 SND_SOC_DAPM_MUX("Headset Left Playback",
1170 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1171 SND_SOC_DAPM_MUX("Headset Right Playback",
1172 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1174 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
1175 &ep_path_enable_control),
1176 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
1177 &auxl_switch_control),
1178 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
1179 &auxr_switch_control),
1181 /* Analog playback drivers */
1182 SND_SOC_DAPM_OUT_DRV_E("HF Left Driver",
1183 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1185 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1186 SND_SOC_DAPM_OUT_DRV_E("HF Right Driver",
1187 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1189 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1190 SND_SOC_DAPM_OUT_DRV_E("HS Left Driver",
1191 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1193 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1194 SND_SOC_DAPM_OUT_DRV_E("HS Right Driver",
1195 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1197 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1198 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
1199 TWL6040_REG_EARCTL, 0, 0, NULL, 0,
1200 twl6040_power_mode_event,
1201 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1203 /* Analog playback PGAs */
1204 SND_SOC_DAPM_PGA("HF Left PGA",
1205 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1206 SND_SOC_DAPM_PGA("HF Right PGA",
1207 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1211 static const struct snd_soc_dapm_route intercon[] = {
1213 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1214 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1215 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1217 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1218 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1219 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1221 {"MicAmpL", NULL, "Analog Left Capture Route"},
1222 {"MicAmpR", NULL, "Analog Right Capture Route"},
1224 {"ADC Left", NULL, "MicAmpL"},
1225 {"ADC Right", NULL, "MicAmpR"},
1228 {"AFMAmpL", NULL, "AFML"},
1229 {"AFMAmpR", NULL, "AFMR"},
1231 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
1232 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
1234 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
1235 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
1237 {"HS Left Driver", NULL, "Headset Left Playback"},
1238 {"HS Right Driver", NULL, "Headset Right Playback"},
1240 {"HSOL", NULL, "HS Left Driver"},
1241 {"HSOR", NULL, "HS Right Driver"},
1243 /* Earphone playback path */
1244 {"Earphone Playback", "Switch", "HSDAC Left"},
1245 {"Earphone Driver", NULL, "Earphone Playback"},
1246 {"EP", NULL, "Earphone Driver"},
1248 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
1249 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
1251 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
1252 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
1254 {"HF Left PGA", NULL, "Handsfree Left Playback"},
1255 {"HF Right PGA", NULL, "Handsfree Right Playback"},
1257 {"HF Left Driver", NULL, "HF Left PGA"},
1258 {"HF Right Driver", NULL, "HF Right PGA"},
1260 {"HFL", NULL, "HF Left Driver"},
1261 {"HFR", NULL, "HF Right Driver"},
1263 {"AUXL Playback", "Switch", "HF Left PGA"},
1264 {"AUXR Playback", "Switch", "HF Right PGA"},
1266 {"AUXL", NULL, "AUXL Playback"},
1267 {"AUXR", NULL, "AUXR Playback"},
1270 static int twl6040_add_widgets(struct snd_soc_codec *codec)
1272 struct snd_soc_dapm_context *dapm = &codec->dapm;
1274 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
1275 ARRAY_SIZE(twl6040_dapm_widgets));
1276 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1277 snd_soc_dapm_new_widgets(dapm);
1282 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1283 enum snd_soc_bias_level level)
1285 struct twl6040 *twl6040 = codec->control_data;
1286 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1290 case SND_SOC_BIAS_ON:
1292 case SND_SOC_BIAS_PREPARE:
1294 case SND_SOC_BIAS_STANDBY:
1295 if (priv->codec_powered)
1298 ret = twl6040_power(twl6040, 1);
1302 priv->codec_powered = 1;
1304 twl6040_restore_regs(codec);
1306 /* Set external boost GPO */
1307 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1309 case SND_SOC_BIAS_OFF:
1310 if (!priv->codec_powered)
1313 twl6040_power(twl6040, 0);
1314 priv->codec_powered = 0;
1318 codec->dapm.bias_level = level;
1323 static int twl6040_startup(struct snd_pcm_substream *substream,
1324 struct snd_soc_dai *dai)
1326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1327 struct snd_soc_codec *codec = rtd->codec;
1328 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1330 snd_pcm_hw_constraint_list(substream->runtime, 0,
1331 SNDRV_PCM_HW_PARAM_RATE,
1332 &sysclk_constraints[priv->pll_power_mode]);
1337 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1338 struct snd_pcm_hw_params *params,
1339 struct snd_soc_dai *dai)
1341 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1342 struct snd_soc_codec *codec = rtd->codec;
1343 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1346 rate = params_rate(params);
1352 /* These rates are not supported when HPPLL is in use */
1353 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1354 dev_err(codec->dev, "HPPLL does not support rate %d\n",
1358 /* Capture is not supported with 17.64MHz sysclk */
1359 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1361 "capture mode is not supported at %dHz\n",
1365 priv->sysclk = 17640000;
1372 priv->sysclk = 19200000;
1375 dev_err(codec->dev, "unsupported rate %d\n", rate);
1382 static int twl6040_prepare(struct snd_pcm_substream *substream,
1383 struct snd_soc_dai *dai)
1385 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1386 struct snd_soc_codec *codec = rtd->codec;
1387 struct twl6040 *twl6040 = codec->control_data;
1388 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1391 if (!priv->sysclk) {
1393 "no mclk configured, call set_sysclk() on init\n");
1397 if ((priv->sysclk == 17640000) && priv->non_lp) {
1399 "some enabled paths aren't supported at %dHz\n",
1404 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1406 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1413 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1414 int clk_id, unsigned int freq, int dir)
1416 struct snd_soc_codec *codec = codec_dai->codec;
1417 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1420 case TWL6040_SYSCLK_SEL_LPPLL:
1421 case TWL6040_SYSCLK_SEL_HPPLL:
1423 priv->clk_in = freq;
1426 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1433 static struct snd_soc_dai_ops twl6040_dai_ops = {
1434 .startup = twl6040_startup,
1435 .hw_params = twl6040_hw_params,
1436 .prepare = twl6040_prepare,
1437 .set_sysclk = twl6040_set_dai_sysclk,
1440 static struct snd_soc_dai_driver twl6040_dai[] = {
1442 .name = "twl6040-legacy",
1444 .stream_name = "Playback",
1447 .rates = TWL6040_RATES,
1448 .formats = TWL6040_FORMATS,
1451 .stream_name = "Capture",
1454 .rates = TWL6040_RATES,
1455 .formats = TWL6040_FORMATS,
1457 .ops = &twl6040_dai_ops,
1460 .name = "twl6040-ul",
1462 .stream_name = "Capture",
1465 .rates = TWL6040_RATES,
1466 .formats = TWL6040_FORMATS,
1468 .ops = &twl6040_dai_ops,
1471 .name = "twl6040-dl1",
1473 .stream_name = "Headset Playback",
1476 .rates = TWL6040_RATES,
1477 .formats = TWL6040_FORMATS,
1479 .ops = &twl6040_dai_ops,
1482 .name = "twl6040-dl2",
1484 .stream_name = "Handsfree Playback",
1487 .rates = TWL6040_RATES,
1488 .formats = TWL6040_FORMATS,
1490 .ops = &twl6040_dai_ops,
1493 .name = "twl6040-vib",
1495 .stream_name = "Vibra Playback",
1498 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1499 .formats = TWL6040_FORMATS,
1501 .ops = &twl6040_dai_ops,
1506 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1508 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1513 static int twl6040_resume(struct snd_soc_codec *codec)
1515 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1516 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1521 #define twl6040_suspend NULL
1522 #define twl6040_resume NULL
1525 static int twl6040_probe(struct snd_soc_codec *codec)
1527 struct twl6040_data *priv;
1528 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1529 struct platform_device *pdev = container_of(codec->dev,
1530 struct platform_device, dev);
1533 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1536 snd_soc_codec_set_drvdata(codec, priv);
1538 priv->codec = codec;
1539 codec->control_data = dev_get_drvdata(codec->dev->parent);
1541 if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1542 priv->hs_left_step = pdata->hs_left_step;
1543 priv->hs_right_step = pdata->hs_right_step;
1545 priv->hs_left_step = 1;
1546 priv->hs_right_step = 1;
1549 if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1550 priv->hf_left_step = pdata->hf_left_step;
1551 priv->hf_right_step = pdata->hf_right_step;
1553 priv->hf_left_step = 1;
1554 priv->hf_right_step = 1;
1557 priv->plug_irq = platform_get_irq(pdev, 0);
1558 if (priv->plug_irq < 0) {
1559 dev_err(codec->dev, "invalid irq\n");
1564 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1565 if (!priv->workqueue) {
1570 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1572 mutex_init(&priv->mutex);
1574 init_completion(&priv->headset.ramp_done);
1575 init_completion(&priv->handsfree.ramp_done);
1577 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1578 if (priv->hf_workqueue == NULL) {
1582 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1583 if (priv->hs_workqueue == NULL) {
1588 INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
1589 INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
1591 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1592 0, "twl6040_irq_plug", codec);
1594 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1598 twl6040_init_chip(codec);
1600 /* power on device */
1601 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1605 snd_soc_add_controls(codec, twl6040_snd_controls,
1606 ARRAY_SIZE(twl6040_snd_controls));
1607 twl6040_add_widgets(codec);
1612 free_irq(priv->plug_irq, codec);
1614 destroy_workqueue(priv->hs_workqueue);
1616 destroy_workqueue(priv->hf_workqueue);
1618 destroy_workqueue(priv->workqueue);
1624 static int twl6040_remove(struct snd_soc_codec *codec)
1626 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1628 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1629 free_irq(priv->plug_irq, codec);
1630 destroy_workqueue(priv->workqueue);
1631 destroy_workqueue(priv->hf_workqueue);
1632 destroy_workqueue(priv->hs_workqueue);
1638 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1639 .probe = twl6040_probe,
1640 .remove = twl6040_remove,
1641 .suspend = twl6040_suspend,
1642 .resume = twl6040_resume,
1643 .read = twl6040_read_reg_cache,
1644 .write = twl6040_write,
1645 .set_bias_level = twl6040_set_bias_level,
1646 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1647 .reg_word_size = sizeof(u8),
1648 .reg_cache_default = twl6040_reg,
1651 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1653 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1654 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1657 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1659 snd_soc_unregister_codec(&pdev->dev);
1663 static struct platform_driver twl6040_codec_driver = {
1665 .name = "twl6040-codec",
1666 .owner = THIS_MODULE,
1668 .probe = twl6040_codec_probe,
1669 .remove = __devexit_p(twl6040_codec_remove),
1672 static int __init twl6040_codec_init(void)
1674 return platform_driver_register(&twl6040_codec_driver);
1676 module_init(twl6040_codec_init);
1678 static void __exit twl6040_codec_exit(void)
1680 platform_driver_unregister(&twl6040_codec_driver);
1682 module_exit(twl6040_codec_exit);
1684 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1685 MODULE_AUTHOR("Misael Lopez Cruz");
1686 MODULE_LICENSE("GPL");