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 /* Update reg_cache: ASICREV, and TRIM values */
282 val = twl6040_get_revid(twl6040);
283 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
285 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1);
286 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2);
287 twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3);
288 twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM);
289 twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM);
291 /* Change chip defaults */
292 /* No imput selected for microphone amplifiers */
293 twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
294 twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
297 * We need to lower the default gain values, so the ramp code
298 * can work correctly for the first playback.
299 * This reduces the pop noise heard at the first playback.
301 twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
302 twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
303 twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
304 twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
305 twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
308 static void twl6040_restore_regs(struct snd_soc_codec *codec)
310 u8 *cache = codec->reg_cache;
313 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
314 reg = twl6040_restore_list[i];
315 twl6040_write(codec, reg, cache[reg]);
320 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
322 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
323 unsigned int left_step, unsigned int right_step)
326 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
327 struct twl6040_output *headset = &priv->headset;
328 int left_complete = 0, right_complete = 0;
332 left_step = (left_step > 0xF) ? 0xF : left_step;
333 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
334 val = (~reg & TWL6040_HSL_VOL_MASK);
336 if (headset->ramp == TWL6040_RAMP_UP) {
338 if (val < headset->left_vol) {
339 if (val + left_step > headset->left_vol)
340 val = headset->left_vol;
344 reg &= ~TWL6040_HSL_VOL_MASK;
345 twl6040_write(codec, TWL6040_REG_HSGAIN,
346 (reg | (~val & TWL6040_HSL_VOL_MASK)));
350 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
353 if ((int)val - (int)left_step < 0)
358 reg &= ~TWL6040_HSL_VOL_MASK;
359 twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
360 (~val & TWL6040_HSL_VOL_MASK));
367 right_step = (right_step > 0xF) ? 0xF : right_step;
368 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
369 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
371 if (headset->ramp == TWL6040_RAMP_UP) {
373 if (val < headset->right_vol) {
374 if (val + right_step > headset->right_vol)
375 val = headset->right_vol;
379 reg &= ~TWL6040_HSR_VOL_MASK;
380 twl6040_write(codec, TWL6040_REG_HSGAIN,
381 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
385 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
388 if ((int)val - (int)right_step < 0)
393 reg &= ~TWL6040_HSR_VOL_MASK;
394 twl6040_write(codec, TWL6040_REG_HSGAIN,
395 reg | (~val << TWL6040_HSR_VOL_SHIFT));
401 return left_complete & right_complete;
405 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
407 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
408 unsigned int left_step, unsigned int right_step)
410 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
411 struct twl6040_output *handsfree = &priv->handsfree;
412 int left_complete = 0, right_complete = 0;
416 left_step = (left_step > 0x1D) ? 0x1D : left_step;
417 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
419 val = (reg & TWL6040_HF_VOL_MASK);
420 if (handsfree->ramp == TWL6040_RAMP_UP) {
422 if (val < handsfree->left_vol) {
423 if (val + left_step > handsfree->left_vol)
424 val = handsfree->left_vol;
428 reg &= ~TWL6040_HF_VOL_MASK;
429 twl6040_write(codec, TWL6040_REG_HFLGAIN,
434 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
437 if ((int)val - (int)left_step < 0)
442 reg &= ~TWL6040_HF_VOL_MASK;
443 twl6040_write(codec, TWL6040_REG_HFLGAIN,
451 right_step = (right_step > 0x1D) ? 0x1D : right_step;
452 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
454 val = (reg & TWL6040_HF_VOL_MASK);
455 if (handsfree->ramp == TWL6040_RAMP_UP) {
457 if (val < handsfree->right_vol) {
458 if (val + right_step > handsfree->right_vol)
459 val = handsfree->right_vol;
463 reg &= ~TWL6040_HF_VOL_MASK;
464 twl6040_write(codec, TWL6040_REG_HFRGAIN,
469 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
472 if ((int)val - (int)right_step < 0)
477 reg &= ~TWL6040_HF_VOL_MASK;
478 twl6040_write(codec, TWL6040_REG_HFRGAIN,
483 return left_complete & right_complete;
487 * This work ramps both output PGAs at stream start/stop time to
488 * minimise pop associated with DAPM power switching.
490 static void twl6040_pga_hs_work(struct work_struct *work)
492 struct twl6040_data *priv =
493 container_of(work, struct twl6040_data, hs_delayed_work.work);
494 struct snd_soc_codec *codec = priv->codec;
495 struct twl6040_output *headset = &priv->headset;
496 unsigned int delay = headset->step_delay;
497 int i, headset_complete;
499 /* do we need to ramp at all ? */
500 if (headset->ramp == TWL6040_RAMP_NONE)
503 /* HS PGA volumes have 4 bits of resolution to ramp */
504 for (i = 0; i <= 16; i++) {
505 headset_complete = twl6040_hs_ramp_step(codec,
507 headset->right_step);
509 /* ramp finished ? */
510 if (headset_complete)
514 * TODO: tune: delay is longer over 0dB
515 * as increases are larger.
518 schedule_timeout_interruptible(msecs_to_jiffies(delay +
521 schedule_timeout_interruptible(msecs_to_jiffies(delay));
524 if (headset->ramp == TWL6040_RAMP_DOWN) {
526 complete(&headset->ramp_done);
530 headset->ramp = TWL6040_RAMP_NONE;
533 static void twl6040_pga_hf_work(struct work_struct *work)
535 struct twl6040_data *priv =
536 container_of(work, struct twl6040_data, hf_delayed_work.work);
537 struct snd_soc_codec *codec = priv->codec;
538 struct twl6040_output *handsfree = &priv->handsfree;
539 unsigned int delay = handsfree->step_delay;
540 int i, handsfree_complete;
542 /* do we need to ramp at all ? */
543 if (handsfree->ramp == TWL6040_RAMP_NONE)
546 /* HF PGA volumes have 5 bits of resolution to ramp */
547 for (i = 0; i <= 32; i++) {
548 handsfree_complete = twl6040_hf_ramp_step(codec,
549 handsfree->left_step,
550 handsfree->right_step);
552 /* ramp finished ? */
553 if (handsfree_complete)
557 * TODO: tune: delay is longer over 0dB
558 * as increases are larger.
561 schedule_timeout_interruptible(msecs_to_jiffies(delay +
564 schedule_timeout_interruptible(msecs_to_jiffies(delay));
568 if (handsfree->ramp == TWL6040_RAMP_DOWN) {
569 handsfree->active = 0;
570 complete(&handsfree->ramp_done);
572 handsfree->active = 1;
573 handsfree->ramp = TWL6040_RAMP_NONE;
576 static int pga_event(struct snd_soc_dapm_widget *w,
577 struct snd_kcontrol *kcontrol, int event)
579 struct snd_soc_codec *codec = w->codec;
580 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
581 struct twl6040_output *out;
582 struct delayed_work *work;
583 struct workqueue_struct *queue;
588 out = &priv->headset;
589 work = &priv->hs_delayed_work;
590 queue = priv->hs_workqueue;
591 out->left_step = priv->hs_left_step;
592 out->right_step = priv->hs_right_step;
593 out->step_delay = 5; /* 5 ms between volume ramp steps */
596 out = &priv->handsfree;
597 work = &priv->hf_delayed_work;
598 queue = priv->hf_workqueue;
599 out->left_step = priv->hf_left_step;
600 out->right_step = priv->hf_right_step;
601 out->step_delay = 5; /* 5 ms between volume ramp steps */
602 if (SND_SOC_DAPM_EVENT_ON(event))
612 case SND_SOC_DAPM_POST_PMU:
616 /* don't use volume ramp for power-up */
617 out->left_step = out->left_vol;
618 out->right_step = out->right_vol;
620 if (!delayed_work_pending(work)) {
621 out->ramp = TWL6040_RAMP_UP;
622 queue_delayed_work(queue, work,
623 msecs_to_jiffies(1));
627 case SND_SOC_DAPM_PRE_PMD:
631 if (!delayed_work_pending(work)) {
632 /* use volume ramp for power-down */
633 out->ramp = TWL6040_RAMP_DOWN;
634 INIT_COMPLETION(out->ramp_done);
636 queue_delayed_work(queue, work,
637 msecs_to_jiffies(1));
639 wait_for_completion_timeout(&out->ramp_done,
640 msecs_to_jiffies(2000));
648 /* set headset dac and driver power mode */
649 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
652 int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
654 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
655 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
665 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
666 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
671 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
672 struct snd_kcontrol *kcontrol, int event)
678 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
679 struct snd_kcontrol *kcontrol, int event)
681 struct snd_soc_codec *codec = w->codec;
682 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
685 if (SND_SOC_DAPM_EVENT_ON(event)) {
687 if (!strcmp(w->name, "Earphone Driver")) {
688 /* Earphone doesn't support low power mode */
689 priv->hs_power_mode_locked = 1;
690 ret = headset_power_mode(codec, 1);
694 if (!strcmp(w->name, "Earphone Driver")) {
695 priv->hs_power_mode_locked = 0;
696 ret = headset_power_mode(codec, priv->hs_power_mode);
705 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
706 struct snd_soc_jack *jack, int report)
708 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
711 mutex_lock(&priv->mutex);
714 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
715 if (status & TWL6040_PLUGCOMP)
716 snd_soc_jack_report(jack, report, report);
718 snd_soc_jack_report(jack, 0, report);
720 mutex_unlock(&priv->mutex);
723 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
724 struct snd_soc_jack *jack, int report)
726 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
727 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
729 hs_jack->jack = jack;
730 hs_jack->report = report;
732 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
734 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
736 static void twl6040_accessory_work(struct work_struct *work)
738 struct twl6040_data *priv = container_of(work,
739 struct twl6040_data, delayed_work.work);
740 struct snd_soc_codec *codec = priv->codec;
741 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
743 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
746 /* audio interrupt handler */
747 static irqreturn_t twl6040_audio_handler(int irq, void *data)
749 struct snd_soc_codec *codec = data;
750 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
752 queue_delayed_work(priv->workqueue, &priv->delayed_work,
753 msecs_to_jiffies(200));
758 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
759 struct snd_ctl_elem_value *ucontrol)
761 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
762 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
763 struct twl6040_output *out = NULL;
764 struct soc_mixer_control *mc =
765 (struct soc_mixer_control *)kcontrol->private_value;
767 unsigned int reg = mc->reg;
769 /* For HS and HF we shadow the values and only actually write
770 * them out when active in order to ensure the amplifier comes on
771 * as quietly as possible. */
773 case TWL6040_REG_HSGAIN:
774 out = &twl6040_priv->headset;
781 out->left_vol = ucontrol->value.integer.value[0];
782 out->right_vol = ucontrol->value.integer.value[1];
787 ret = snd_soc_put_volsw(kcontrol, ucontrol);
794 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
795 struct snd_ctl_elem_value *ucontrol)
797 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
798 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
799 struct twl6040_output *out = &twl6040_priv->headset;
800 struct soc_mixer_control *mc =
801 (struct soc_mixer_control *)kcontrol->private_value;
802 unsigned int reg = mc->reg;
805 case TWL6040_REG_HSGAIN:
806 out = &twl6040_priv->headset;
807 ucontrol->value.integer.value[0] = out->left_vol;
808 ucontrol->value.integer.value[1] = out->right_vol;
815 return snd_soc_get_volsw(kcontrol, ucontrol);
818 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
819 struct snd_ctl_elem_value *ucontrol)
821 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
822 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
823 struct twl6040_output *out = NULL;
824 struct soc_mixer_control *mc =
825 (struct soc_mixer_control *)kcontrol->private_value;
827 unsigned int reg = mc->reg;
829 /* For HS and HF we shadow the values and only actually write
830 * them out when active in order to ensure the amplifier comes on
831 * as quietly as possible. */
833 case TWL6040_REG_HFLGAIN:
834 case TWL6040_REG_HFRGAIN:
835 out = &twl6040_priv->handsfree;
842 out->left_vol = ucontrol->value.integer.value[0];
843 out->right_vol = ucontrol->value.integer.value[1];
848 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
855 static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_value *ucontrol)
858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
859 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
860 struct twl6040_output *out = &twl6040_priv->handsfree;
861 struct soc_mixer_control *mc =
862 (struct soc_mixer_control *)kcontrol->private_value;
863 unsigned int reg = mc->reg;
865 /* If these are cached registers use the cache */
867 case TWL6040_REG_HFLGAIN:
868 case TWL6040_REG_HFRGAIN:
869 out = &twl6040_priv->handsfree;
870 ucontrol->value.integer.value[0] = out->left_vol;
871 ucontrol->value.integer.value[1] = out->right_vol;
878 return snd_soc_get_volsw_2r(kcontrol, ucontrol);
881 /* double control with volume update */
882 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
884 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
885 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
886 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
887 .tlv.p = (tlv_array), \
888 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
889 .put = twl6040_put_volsw, \
890 .private_value = (unsigned long)&(struct soc_mixer_control) \
891 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
892 .max = xmax, .platform_max = xmax, .invert = xinvert} }
894 /* double control with volume update */
895 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
897 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
898 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
899 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
900 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
901 .tlv.p = (tlv_array), \
902 .info = snd_soc_info_volsw_2r, \
903 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
904 .private_value = (unsigned long)&(struct soc_mixer_control) \
905 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
906 .rshift = xshift, .max = xmax, .invert = xinvert}, }
909 * MICATT volume control:
910 * from -6 to 0 dB in 6 dB steps
912 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
915 * MICGAIN volume control:
916 * from 6 to 30 dB in 6 dB steps
918 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
921 * AFMGAIN volume control:
922 * from -18 to 24 dB in 6 dB steps
924 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
927 * HSGAIN volume control:
928 * from -30 to 0 dB in 2 dB steps
930 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
933 * HFGAIN volume control:
934 * from -52 to 6 dB in 2 dB steps
936 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
939 * EPGAIN volume control:
940 * from -24 to 6 dB in 2 dB steps
942 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
944 /* Left analog microphone selection */
945 static const char *twl6040_amicl_texts[] =
946 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
948 /* Right analog microphone selection */
949 static const char *twl6040_amicr_texts[] =
950 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
952 static const struct soc_enum twl6040_enum[] = {
953 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
954 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
957 static const char *twl6040_hs_texts[] = {
958 "Off", "HS DAC", "Line-In amp"
961 static const struct soc_enum twl6040_hs_enum[] = {
962 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
964 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
968 static const char *twl6040_hf_texts[] = {
969 "Off", "HF DAC", "Line-In amp"
972 static const struct soc_enum twl6040_hf_enum[] = {
973 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
975 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
979 static const struct snd_kcontrol_new amicl_control =
980 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
982 static const struct snd_kcontrol_new amicr_control =
983 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
985 /* Headset DAC playback switches */
986 static const struct snd_kcontrol_new hsl_mux_controls =
987 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
989 static const struct snd_kcontrol_new hsr_mux_controls =
990 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
992 /* Handsfree DAC playback switches */
993 static const struct snd_kcontrol_new hfl_mux_controls =
994 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
996 static const struct snd_kcontrol_new hfr_mux_controls =
997 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
999 static const struct snd_kcontrol_new ep_path_enable_control =
1000 SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
1002 static const struct snd_kcontrol_new auxl_switch_control =
1003 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
1005 static const struct snd_kcontrol_new auxr_switch_control =
1006 SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
1008 /* Headset power mode */
1009 static const char *twl6040_power_mode_texts[] = {
1010 "Low-Power", "High-Perfomance",
1013 static const struct soc_enum twl6040_power_mode_enum =
1014 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
1015 twl6040_power_mode_texts);
1017 static int twl6040_headset_power_get_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 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
1028 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
1029 struct snd_ctl_elem_value *ucontrol)
1031 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1032 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1033 int high_perf = ucontrol->value.enumerated.item[0];
1036 if (!priv->hs_power_mode_locked)
1037 ret = headset_power_mode(codec, high_perf);
1040 priv->hs_power_mode = high_perf;
1045 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
1046 struct snd_ctl_elem_value *ucontrol)
1048 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1049 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1051 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1056 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1057 struct snd_ctl_elem_value *ucontrol)
1059 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1060 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1062 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1067 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1069 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1071 return priv->pll_power_mode;
1073 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1075 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1077 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1078 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1079 SOC_DOUBLE_TLV("Capture Volume",
1080 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1083 SOC_DOUBLE_TLV("Aux FM Volume",
1084 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1086 /* Playback gains */
1087 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
1088 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
1089 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
1090 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
1091 SOC_SINGLE_TLV("Earphone Playback Volume",
1092 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1094 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1095 twl6040_headset_power_get_enum,
1096 twl6040_headset_power_put_enum),
1098 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1099 twl6040_pll_get_enum, twl6040_pll_put_enum),
1102 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1104 SND_SOC_DAPM_INPUT("MAINMIC"),
1105 SND_SOC_DAPM_INPUT("HSMIC"),
1106 SND_SOC_DAPM_INPUT("SUBMIC"),
1107 SND_SOC_DAPM_INPUT("AFML"),
1108 SND_SOC_DAPM_INPUT("AFMR"),
1111 SND_SOC_DAPM_OUTPUT("HSOL"),
1112 SND_SOC_DAPM_OUTPUT("HSOR"),
1113 SND_SOC_DAPM_OUTPUT("HFL"),
1114 SND_SOC_DAPM_OUTPUT("HFR"),
1115 SND_SOC_DAPM_OUTPUT("EP"),
1116 SND_SOC_DAPM_OUTPUT("AUXL"),
1117 SND_SOC_DAPM_OUTPUT("AUXR"),
1119 /* Analog input muxes for the capture amplifiers */
1120 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1121 SND_SOC_NOPM, 0, 0, &amicl_control),
1122 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1123 SND_SOC_NOPM, 0, 0, &amicr_control),
1125 /* Analog capture PGAs */
1126 SND_SOC_DAPM_PGA("MicAmpL",
1127 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1128 SND_SOC_DAPM_PGA("MicAmpR",
1129 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1131 /* Auxiliary FM PGAs */
1132 SND_SOC_DAPM_PGA("AFMAmpL",
1133 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1134 SND_SOC_DAPM_PGA("AFMAmpR",
1135 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1138 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1139 TWL6040_REG_MICLCTL, 2, 0),
1140 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1141 TWL6040_REG_MICRCTL, 2, 0),
1143 /* Microphone bias */
1144 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1145 TWL6040_REG_AMICBCTL, 0, 0),
1146 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1147 TWL6040_REG_AMICBCTL, 4, 0),
1148 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1149 TWL6040_REG_DMICBCTL, 0, 0),
1150 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1151 TWL6040_REG_DMICBCTL, 4, 0),
1154 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1155 TWL6040_REG_HSLCTL, 0, 0,
1156 twl6040_hs_dac_event,
1157 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1158 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1159 TWL6040_REG_HSRCTL, 0, 0,
1160 twl6040_hs_dac_event,
1161 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1162 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1163 TWL6040_REG_HFLCTL, 0, 0,
1164 twl6040_power_mode_event,
1165 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1166 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1167 TWL6040_REG_HFRCTL, 0, 0,
1168 twl6040_power_mode_event,
1169 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1171 SND_SOC_DAPM_MUX("Handsfree Left Playback",
1172 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1173 SND_SOC_DAPM_MUX("Handsfree Right Playback",
1174 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1175 /* Analog playback Muxes */
1176 SND_SOC_DAPM_MUX("Headset Left Playback",
1177 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1178 SND_SOC_DAPM_MUX("Headset Right Playback",
1179 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1181 SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
1182 &ep_path_enable_control),
1183 SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
1184 &auxl_switch_control),
1185 SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
1186 &auxr_switch_control),
1188 /* Analog playback drivers */
1189 SND_SOC_DAPM_OUT_DRV_E("HF Left Driver",
1190 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1192 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1193 SND_SOC_DAPM_OUT_DRV_E("HF Right Driver",
1194 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1196 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1197 SND_SOC_DAPM_OUT_DRV_E("HS Left Driver",
1198 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1200 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1201 SND_SOC_DAPM_OUT_DRV_E("HS Right Driver",
1202 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1204 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1205 SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
1206 TWL6040_REG_EARCTL, 0, 0, NULL, 0,
1207 twl6040_power_mode_event,
1208 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1210 /* Analog playback PGAs */
1211 SND_SOC_DAPM_PGA("HF Left PGA",
1212 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1213 SND_SOC_DAPM_PGA("HF Right PGA",
1214 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1218 static const struct snd_soc_dapm_route intercon[] = {
1220 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1221 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1222 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1224 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1225 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1226 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1228 {"MicAmpL", NULL, "Analog Left Capture Route"},
1229 {"MicAmpR", NULL, "Analog Right Capture Route"},
1231 {"ADC Left", NULL, "MicAmpL"},
1232 {"ADC Right", NULL, "MicAmpR"},
1235 {"AFMAmpL", NULL, "AFML"},
1236 {"AFMAmpR", NULL, "AFMR"},
1238 {"Headset Left Playback", "HS DAC", "HSDAC Left"},
1239 {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
1241 {"Headset Right Playback", "HS DAC", "HSDAC Right"},
1242 {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
1244 {"HS Left Driver", NULL, "Headset Left Playback"},
1245 {"HS Right Driver", NULL, "Headset Right Playback"},
1247 {"HSOL", NULL, "HS Left Driver"},
1248 {"HSOR", NULL, "HS Right Driver"},
1250 /* Earphone playback path */
1251 {"Earphone Playback", "Switch", "HSDAC Left"},
1252 {"Earphone Driver", NULL, "Earphone Playback"},
1253 {"EP", NULL, "Earphone Driver"},
1255 {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
1256 {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
1258 {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
1259 {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
1261 {"HF Left PGA", NULL, "Handsfree Left Playback"},
1262 {"HF Right PGA", NULL, "Handsfree Right Playback"},
1264 {"HF Left Driver", NULL, "HF Left PGA"},
1265 {"HF Right Driver", NULL, "HF Right PGA"},
1267 {"HFL", NULL, "HF Left Driver"},
1268 {"HFR", NULL, "HF Right Driver"},
1270 {"AUXL Playback", "Switch", "HF Left PGA"},
1271 {"AUXR Playback", "Switch", "HF Right PGA"},
1273 {"AUXL", NULL, "AUXL Playback"},
1274 {"AUXR", NULL, "AUXR Playback"},
1277 static int twl6040_add_widgets(struct snd_soc_codec *codec)
1279 struct snd_soc_dapm_context *dapm = &codec->dapm;
1281 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
1282 ARRAY_SIZE(twl6040_dapm_widgets));
1283 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1284 snd_soc_dapm_new_widgets(dapm);
1289 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1290 enum snd_soc_bias_level level)
1292 struct twl6040 *twl6040 = codec->control_data;
1293 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1297 case SND_SOC_BIAS_ON:
1299 case SND_SOC_BIAS_PREPARE:
1301 case SND_SOC_BIAS_STANDBY:
1302 if (priv->codec_powered)
1305 ret = twl6040_power(twl6040, 1);
1309 priv->codec_powered = 1;
1311 twl6040_restore_regs(codec);
1313 /* Set external boost GPO */
1314 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1316 case SND_SOC_BIAS_OFF:
1317 if (!priv->codec_powered)
1320 twl6040_power(twl6040, 0);
1321 priv->codec_powered = 0;
1325 codec->dapm.bias_level = level;
1330 static int twl6040_startup(struct snd_pcm_substream *substream,
1331 struct snd_soc_dai *dai)
1333 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1334 struct snd_soc_codec *codec = rtd->codec;
1335 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1337 snd_pcm_hw_constraint_list(substream->runtime, 0,
1338 SNDRV_PCM_HW_PARAM_RATE,
1339 &sysclk_constraints[priv->pll_power_mode]);
1344 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1345 struct snd_pcm_hw_params *params,
1346 struct snd_soc_dai *dai)
1348 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1349 struct snd_soc_codec *codec = rtd->codec;
1350 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1353 rate = params_rate(params);
1359 /* These rates are not supported when HPPLL is in use */
1360 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1361 dev_err(codec->dev, "HPPLL does not support rate %d\n",
1365 /* Capture is not supported with 17.64MHz sysclk */
1366 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1368 "capture mode is not supported at %dHz\n",
1372 priv->sysclk = 17640000;
1379 priv->sysclk = 19200000;
1382 dev_err(codec->dev, "unsupported rate %d\n", rate);
1389 static int twl6040_prepare(struct snd_pcm_substream *substream,
1390 struct snd_soc_dai *dai)
1392 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1393 struct snd_soc_codec *codec = rtd->codec;
1394 struct twl6040 *twl6040 = codec->control_data;
1395 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1398 if (!priv->sysclk) {
1400 "no mclk configured, call set_sysclk() on init\n");
1404 if ((priv->sysclk == 17640000) && priv->non_lp) {
1406 "some enabled paths aren't supported at %dHz\n",
1411 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1413 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1420 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1421 int clk_id, unsigned int freq, int dir)
1423 struct snd_soc_codec *codec = codec_dai->codec;
1424 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1427 case TWL6040_SYSCLK_SEL_LPPLL:
1428 case TWL6040_SYSCLK_SEL_HPPLL:
1430 priv->clk_in = freq;
1433 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1440 static struct snd_soc_dai_ops twl6040_dai_ops = {
1441 .startup = twl6040_startup,
1442 .hw_params = twl6040_hw_params,
1443 .prepare = twl6040_prepare,
1444 .set_sysclk = twl6040_set_dai_sysclk,
1447 static struct snd_soc_dai_driver twl6040_dai[] = {
1449 .name = "twl6040-legacy",
1451 .stream_name = "Playback",
1454 .rates = TWL6040_RATES,
1455 .formats = TWL6040_FORMATS,
1458 .stream_name = "Capture",
1461 .rates = TWL6040_RATES,
1462 .formats = TWL6040_FORMATS,
1464 .ops = &twl6040_dai_ops,
1467 .name = "twl6040-ul",
1469 .stream_name = "Capture",
1472 .rates = TWL6040_RATES,
1473 .formats = TWL6040_FORMATS,
1475 .ops = &twl6040_dai_ops,
1478 .name = "twl6040-dl1",
1480 .stream_name = "Headset Playback",
1483 .rates = TWL6040_RATES,
1484 .formats = TWL6040_FORMATS,
1486 .ops = &twl6040_dai_ops,
1489 .name = "twl6040-dl2",
1491 .stream_name = "Handsfree Playback",
1494 .rates = TWL6040_RATES,
1495 .formats = TWL6040_FORMATS,
1497 .ops = &twl6040_dai_ops,
1500 .name = "twl6040-vib",
1502 .stream_name = "Vibra Playback",
1505 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1506 .formats = TWL6040_FORMATS,
1508 .ops = &twl6040_dai_ops,
1513 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1515 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1520 static int twl6040_resume(struct snd_soc_codec *codec)
1522 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1523 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1528 #define twl6040_suspend NULL
1529 #define twl6040_resume NULL
1532 static int twl6040_probe(struct snd_soc_codec *codec)
1534 struct twl6040_data *priv;
1535 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1536 struct platform_device *pdev = container_of(codec->dev,
1537 struct platform_device, dev);
1540 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1543 snd_soc_codec_set_drvdata(codec, priv);
1545 priv->codec = codec;
1546 codec->control_data = dev_get_drvdata(codec->dev->parent);
1548 if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1549 priv->hs_left_step = pdata->hs_left_step;
1550 priv->hs_right_step = pdata->hs_right_step;
1552 priv->hs_left_step = 1;
1553 priv->hs_right_step = 1;
1556 if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1557 priv->hf_left_step = pdata->hf_left_step;
1558 priv->hf_right_step = pdata->hf_right_step;
1560 priv->hf_left_step = 1;
1561 priv->hf_right_step = 1;
1564 priv->plug_irq = platform_get_irq(pdev, 0);
1565 if (priv->plug_irq < 0) {
1566 dev_err(codec->dev, "invalid irq\n");
1571 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1572 if (!priv->workqueue) {
1577 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1579 mutex_init(&priv->mutex);
1581 init_completion(&priv->headset.ramp_done);
1582 init_completion(&priv->handsfree.ramp_done);
1584 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1585 if (priv->hf_workqueue == NULL) {
1589 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1590 if (priv->hs_workqueue == NULL) {
1595 INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
1596 INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
1598 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1599 0, "twl6040_irq_plug", codec);
1601 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1605 twl6040_init_chip(codec);
1607 /* power on device */
1608 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1612 snd_soc_add_controls(codec, twl6040_snd_controls,
1613 ARRAY_SIZE(twl6040_snd_controls));
1614 twl6040_add_widgets(codec);
1619 free_irq(priv->plug_irq, codec);
1621 destroy_workqueue(priv->hs_workqueue);
1623 destroy_workqueue(priv->hf_workqueue);
1625 destroy_workqueue(priv->workqueue);
1631 static int twl6040_remove(struct snd_soc_codec *codec)
1633 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1635 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1636 free_irq(priv->plug_irq, codec);
1637 destroy_workqueue(priv->workqueue);
1638 destroy_workqueue(priv->hf_workqueue);
1639 destroy_workqueue(priv->hs_workqueue);
1645 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1646 .probe = twl6040_probe,
1647 .remove = twl6040_remove,
1648 .suspend = twl6040_suspend,
1649 .resume = twl6040_resume,
1650 .read = twl6040_read_reg_cache,
1651 .write = twl6040_write,
1652 .set_bias_level = twl6040_set_bias_level,
1653 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1654 .reg_word_size = sizeof(u8),
1655 .reg_cache_default = twl6040_reg,
1658 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1660 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1661 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1664 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1666 snd_soc_unregister_codec(&pdev->dev);
1670 static struct platform_driver twl6040_codec_driver = {
1672 .name = "twl6040-codec",
1673 .owner = THIS_MODULE,
1675 .probe = twl6040_codec_probe,
1676 .remove = __devexit_p(twl6040_codec_remove),
1679 static int __init twl6040_codec_init(void)
1681 return platform_driver_register(&twl6040_codec_driver);
1683 module_init(twl6040_codec_init);
1685 static void __exit twl6040_codec_exit(void)
1687 platform_driver_unregister(&twl6040_codec_driver);
1689 module_exit(twl6040_codec_exit);
1691 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1692 MODULE_AUTHOR("Misael Lopez Cruz");
1693 MODULE_LICENSE("GPL");