x86: Fix S4 regression
[linux-btrfs-devel.git] / sound / soc / codecs / twl6040.c
blob443032b3b3296baca51be67e7660a3c3d937f2c3
1 /*
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
18 * 02110-1301 USA
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/pm.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>
39 #include "twl6040.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 {
61 u16 active;
62 u16 left_vol;
63 u16 right_vol;
64 u16 left_step;
65 u16 right_step;
66 unsigned int step_delay;
67 u16 ramp;
68 u16 mute;
69 struct completion ramp_done;
72 struct twl6040_jack_data {
73 struct snd_soc_jack *jack;
74 int report;
77 /* codec private data */
78 struct twl6040_data {
79 int plug_irq;
80 int codec_powered;
81 int pll;
82 int non_lp;
83 int pll_power_mode;
84 int hs_power_mode;
85 int hs_power_mode_locked;
86 unsigned int clk_in;
87 unsigned int sysclk;
88 u16 hs_left_step;
89 u16 hs_right_step;
90 u16 hf_left_step;
91 u16 hf_right_step;
92 struct twl6040_jack_data hs_jack;
93 struct snd_soc_codec *codec;
94 struct workqueue_struct *workqueue;
95 struct delayed_work delayed_work;
96 struct mutex mutex;
97 struct twl6040_output headset;
98 struct twl6040_output handsfree;
99 struct workqueue_struct *hf_workqueue;
100 struct workqueue_struct *hs_workqueue;
101 struct delayed_work hs_delayed_work;
102 struct delayed_work hf_delayed_work;
106 * twl6040 register cache & default register settings
108 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
109 0x00, /* not used 0x00 */
110 0x4B, /* TWL6040_ASICID (ro) 0x01 */
111 0x00, /* TWL6040_ASICREV (ro) 0x02 */
112 0x00, /* TWL6040_INTID 0x03 */
113 0x00, /* TWL6040_INTMR 0x04 */
114 0x00, /* TWL6040_NCPCTRL 0x05 */
115 0x00, /* TWL6040_LDOCTL 0x06 */
116 0x60, /* TWL6040_HPPLLCTL 0x07 */
117 0x00, /* TWL6040_LPPLLCTL 0x08 */
118 0x4A, /* TWL6040_LPPLLDIV 0x09 */
119 0x00, /* TWL6040_AMICBCTL 0x0A */
120 0x00, /* TWL6040_DMICBCTL 0x0B */
121 0x18, /* TWL6040_MICLCTL 0x0C - No input selected on Left Mic */
122 0x18, /* TWL6040_MICRCTL 0x0D - No input selected on Right Mic */
123 0x00, /* TWL6040_MICGAIN 0x0E */
124 0x1B, /* TWL6040_LINEGAIN 0x0F */
125 0x00, /* TWL6040_HSLCTL 0x10 */
126 0x00, /* TWL6040_HSRCTL 0x11 */
127 0x00, /* TWL6040_HSGAIN 0x12 */
128 0x00, /* TWL6040_EARCTL 0x13 */
129 0x00, /* TWL6040_HFLCTL 0x14 */
130 0x00, /* TWL6040_HFLGAIN 0x15 */
131 0x00, /* TWL6040_HFRCTL 0x16 */
132 0x00, /* TWL6040_HFRGAIN 0x17 */
133 0x00, /* TWL6040_VIBCTLL 0x18 */
134 0x00, /* TWL6040_VIBDATL 0x19 */
135 0x00, /* TWL6040_VIBCTLR 0x1A */
136 0x00, /* TWL6040_VIBDATR 0x1B */
137 0x00, /* TWL6040_HKCTL1 0x1C */
138 0x00, /* TWL6040_HKCTL2 0x1D */
139 0x00, /* TWL6040_GPOCTL 0x1E */
140 0x00, /* TWL6040_ALB 0x1F */
141 0x00, /* TWL6040_DLB 0x20 */
142 0x00, /* not used 0x21 */
143 0x00, /* not used 0x22 */
144 0x00, /* not used 0x23 */
145 0x00, /* not used 0x24 */
146 0x00, /* not used 0x25 */
147 0x00, /* not used 0x26 */
148 0x00, /* not used 0x27 */
149 0x00, /* TWL6040_TRIM1 0x28 */
150 0x00, /* TWL6040_TRIM2 0x29 */
151 0x00, /* TWL6040_TRIM3 0x2A */
152 0x00, /* TWL6040_HSOTRIM 0x2B */
153 0x00, /* TWL6040_HFOTRIM 0x2C */
154 0x09, /* TWL6040_ACCCTL 0x2D */
155 0x00, /* TWL6040_STATUS (ro) 0x2E */
159 * twl6040 vio/gnd registers:
160 * registers under vio/gnd supply can be accessed
161 * before the power-up sequence, after NRESPWRON goes high
163 static const int twl6040_vio_reg[TWL6040_VIOREGNUM] = {
164 TWL6040_REG_ASICID,
165 TWL6040_REG_ASICREV,
166 TWL6040_REG_INTID,
167 TWL6040_REG_INTMR,
168 TWL6040_REG_NCPCTL,
169 TWL6040_REG_LDOCTL,
170 TWL6040_REG_AMICBCTL,
171 TWL6040_REG_DMICBCTL,
172 TWL6040_REG_HKCTL1,
173 TWL6040_REG_HKCTL2,
174 TWL6040_REG_GPOCTL,
175 TWL6040_REG_TRIM1,
176 TWL6040_REG_TRIM2,
177 TWL6040_REG_TRIM3,
178 TWL6040_REG_HSOTRIM,
179 TWL6040_REG_HFOTRIM,
180 TWL6040_REG_ACCCTL,
181 TWL6040_REG_STATUS,
185 * twl6040 vdd/vss registers:
186 * registers under vdd/vss supplies can only be accessed
187 * after the power-up sequence
189 static const int twl6040_vdd_reg[TWL6040_VDDREGNUM] = {
190 TWL6040_REG_HPPLLCTL,
191 TWL6040_REG_LPPLLCTL,
192 TWL6040_REG_LPPLLDIV,
193 TWL6040_REG_MICLCTL,
194 TWL6040_REG_MICRCTL,
195 TWL6040_REG_MICGAIN,
196 TWL6040_REG_LINEGAIN,
197 TWL6040_REG_HSLCTL,
198 TWL6040_REG_HSRCTL,
199 TWL6040_REG_HSGAIN,
200 TWL6040_REG_EARCTL,
201 TWL6040_REG_HFLCTL,
202 TWL6040_REG_HFLGAIN,
203 TWL6040_REG_HFRCTL,
204 TWL6040_REG_HFRGAIN,
205 TWL6040_REG_VIBCTLL,
206 TWL6040_REG_VIBDATL,
207 TWL6040_REG_VIBCTLR,
208 TWL6040_REG_VIBDATR,
209 TWL6040_REG_ALB,
210 TWL6040_REG_DLB,
213 /* set of rates for each pll: low-power and high-performance */
214 static unsigned int lp_rates[] = {
215 8000,
216 11250,
217 16000,
218 22500,
219 32000,
220 44100,
221 48000,
222 88200,
223 96000,
226 static unsigned int hp_rates[] = {
227 8000,
228 16000,
229 32000,
230 48000,
231 96000,
234 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
235 { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
236 { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
240 * read twl6040 register cache
242 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
243 unsigned int reg)
245 u8 *cache = codec->reg_cache;
247 if (reg >= TWL6040_CACHEREGNUM)
248 return -EIO;
250 return cache[reg];
254 * write twl6040 register cache
256 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
257 u8 reg, u8 value)
259 u8 *cache = codec->reg_cache;
261 if (reg >= TWL6040_CACHEREGNUM)
262 return;
263 cache[reg] = value;
267 * read from twl6040 hardware register
269 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
270 unsigned int reg)
272 struct twl6040 *twl6040 = codec->control_data;
273 u8 value;
275 if (reg >= TWL6040_CACHEREGNUM)
276 return -EIO;
278 value = twl6040_reg_read(twl6040, reg);
279 twl6040_write_reg_cache(codec, reg, value);
281 return value;
285 * write to the twl6040 register space
287 static int twl6040_write(struct snd_soc_codec *codec,
288 unsigned int reg, unsigned int value)
290 struct twl6040 *twl6040 = codec->control_data;
292 if (reg >= TWL6040_CACHEREGNUM)
293 return -EIO;
295 twl6040_write_reg_cache(codec, reg, value);
296 return twl6040_reg_write(twl6040, reg, value);
299 static void twl6040_init_vio_regs(struct snd_soc_codec *codec)
301 u8 *cache = codec->reg_cache;
302 int reg, i;
304 for (i = 0; i < TWL6040_VIOREGNUM; i++) {
305 reg = twl6040_vio_reg[i];
307 * skip read-only registers (ASICID, ASICREV, STATUS)
308 * and registers shared among MFD children
310 switch (reg) {
311 case TWL6040_REG_ASICID:
312 case TWL6040_REG_ASICREV:
313 case TWL6040_REG_INTID:
314 case TWL6040_REG_INTMR:
315 case TWL6040_REG_NCPCTL:
316 case TWL6040_REG_LDOCTL:
317 case TWL6040_REG_GPOCTL:
318 case TWL6040_REG_ACCCTL:
319 case TWL6040_REG_STATUS:
320 continue;
321 default:
322 break;
324 twl6040_write(codec, reg, cache[reg]);
328 static void twl6040_init_vdd_regs(struct snd_soc_codec *codec)
330 u8 *cache = codec->reg_cache;
331 int reg, i;
333 for (i = 0; i < TWL6040_VDDREGNUM; i++) {
334 reg = twl6040_vdd_reg[i];
335 /* skip vibra and PLL registers */
336 switch (reg) {
337 case TWL6040_REG_VIBCTLL:
338 case TWL6040_REG_VIBDATL:
339 case TWL6040_REG_VIBCTLR:
340 case TWL6040_REG_VIBDATR:
341 case TWL6040_REG_HPPLLCTL:
342 case TWL6040_REG_LPPLLCTL:
343 case TWL6040_REG_LPPLLDIV:
344 continue;
345 default:
346 break;
349 twl6040_write(codec, reg, cache[reg]);
354 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
356 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
357 unsigned int left_step, unsigned int right_step)
360 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
361 struct twl6040_output *headset = &priv->headset;
362 int left_complete = 0, right_complete = 0;
363 u8 reg, val;
365 /* left channel */
366 left_step = (left_step > 0xF) ? 0xF : left_step;
367 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
368 val = (~reg & TWL6040_HSL_VOL_MASK);
370 if (headset->ramp == TWL6040_RAMP_UP) {
371 /* ramp step up */
372 if (val < headset->left_vol) {
373 if (val + left_step > headset->left_vol)
374 val = headset->left_vol;
375 else
376 val += left_step;
378 reg &= ~TWL6040_HSL_VOL_MASK;
379 twl6040_write(codec, TWL6040_REG_HSGAIN,
380 (reg | (~val & TWL6040_HSL_VOL_MASK)));
381 } else {
382 left_complete = 1;
384 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
385 /* ramp step down */
386 if (val > 0x0) {
387 if ((int)val - (int)left_step < 0)
388 val = 0;
389 else
390 val -= left_step;
392 reg &= ~TWL6040_HSL_VOL_MASK;
393 twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
394 (~val & TWL6040_HSL_VOL_MASK));
395 } else {
396 left_complete = 1;
400 /* right channel */
401 right_step = (right_step > 0xF) ? 0xF : right_step;
402 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
403 val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
405 if (headset->ramp == TWL6040_RAMP_UP) {
406 /* ramp step up */
407 if (val < headset->right_vol) {
408 if (val + right_step > headset->right_vol)
409 val = headset->right_vol;
410 else
411 val += right_step;
413 reg &= ~TWL6040_HSR_VOL_MASK;
414 twl6040_write(codec, TWL6040_REG_HSGAIN,
415 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
416 } else {
417 right_complete = 1;
419 } else if (headset->ramp == TWL6040_RAMP_DOWN) {
420 /* ramp step down */
421 if (val > 0x0) {
422 if ((int)val - (int)right_step < 0)
423 val = 0;
424 else
425 val -= right_step;
427 reg &= ~TWL6040_HSR_VOL_MASK;
428 twl6040_write(codec, TWL6040_REG_HSGAIN,
429 reg | (~val << TWL6040_HSR_VOL_SHIFT));
430 } else {
431 right_complete = 1;
435 return left_complete & right_complete;
439 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
441 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
442 unsigned int left_step, unsigned int right_step)
444 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
445 struct twl6040_output *handsfree = &priv->handsfree;
446 int left_complete = 0, right_complete = 0;
447 u16 reg, val;
449 /* left channel */
450 left_step = (left_step > 0x1D) ? 0x1D : left_step;
451 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
452 reg = 0x1D - reg;
453 val = (reg & TWL6040_HF_VOL_MASK);
454 if (handsfree->ramp == TWL6040_RAMP_UP) {
455 /* ramp step up */
456 if (val < handsfree->left_vol) {
457 if (val + left_step > handsfree->left_vol)
458 val = handsfree->left_vol;
459 else
460 val += left_step;
462 reg &= ~TWL6040_HF_VOL_MASK;
463 twl6040_write(codec, TWL6040_REG_HFLGAIN,
464 reg | (0x1D - val));
465 } else {
466 left_complete = 1;
468 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
469 /* ramp step down */
470 if (val > 0) {
471 if ((int)val - (int)left_step < 0)
472 val = 0;
473 else
474 val -= left_step;
476 reg &= ~TWL6040_HF_VOL_MASK;
477 twl6040_write(codec, TWL6040_REG_HFLGAIN,
478 reg | (0x1D - val));
479 } else {
480 left_complete = 1;
484 /* right channel */
485 right_step = (right_step > 0x1D) ? 0x1D : right_step;
486 reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
487 reg = 0x1D - reg;
488 val = (reg & TWL6040_HF_VOL_MASK);
489 if (handsfree->ramp == TWL6040_RAMP_UP) {
490 /* ramp step up */
491 if (val < handsfree->right_vol) {
492 if (val + right_step > handsfree->right_vol)
493 val = handsfree->right_vol;
494 else
495 val += right_step;
497 reg &= ~TWL6040_HF_VOL_MASK;
498 twl6040_write(codec, TWL6040_REG_HFRGAIN,
499 reg | (0x1D - val));
500 } else {
501 right_complete = 1;
503 } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
504 /* ramp step down */
505 if (val > 0) {
506 if ((int)val - (int)right_step < 0)
507 val = 0;
508 else
509 val -= right_step;
511 reg &= ~TWL6040_HF_VOL_MASK;
512 twl6040_write(codec, TWL6040_REG_HFRGAIN,
513 reg | (0x1D - val));
517 return left_complete & right_complete;
521 * This work ramps both output PGAs at stream start/stop time to
522 * minimise pop associated with DAPM power switching.
524 static void twl6040_pga_hs_work(struct work_struct *work)
526 struct twl6040_data *priv =
527 container_of(work, struct twl6040_data, hs_delayed_work.work);
528 struct snd_soc_codec *codec = priv->codec;
529 struct twl6040_output *headset = &priv->headset;
530 unsigned int delay = headset->step_delay;
531 int i, headset_complete;
533 /* do we need to ramp at all ? */
534 if (headset->ramp == TWL6040_RAMP_NONE)
535 return;
537 /* HS PGA volumes have 4 bits of resolution to ramp */
538 for (i = 0; i <= 16; i++) {
539 headset_complete = twl6040_hs_ramp_step(codec,
540 headset->left_step,
541 headset->right_step);
543 /* ramp finished ? */
544 if (headset_complete)
545 break;
548 * TODO: tune: delay is longer over 0dB
549 * as increases are larger.
551 if (i >= 8)
552 schedule_timeout_interruptible(msecs_to_jiffies(delay +
553 (delay >> 1)));
554 else
555 schedule_timeout_interruptible(msecs_to_jiffies(delay));
558 if (headset->ramp == TWL6040_RAMP_DOWN) {
559 headset->active = 0;
560 complete(&headset->ramp_done);
561 } else {
562 headset->active = 1;
564 headset->ramp = TWL6040_RAMP_NONE;
567 static void twl6040_pga_hf_work(struct work_struct *work)
569 struct twl6040_data *priv =
570 container_of(work, struct twl6040_data, hf_delayed_work.work);
571 struct snd_soc_codec *codec = priv->codec;
572 struct twl6040_output *handsfree = &priv->handsfree;
573 unsigned int delay = handsfree->step_delay;
574 int i, handsfree_complete;
576 /* do we need to ramp at all ? */
577 if (handsfree->ramp == TWL6040_RAMP_NONE)
578 return;
580 /* HF PGA volumes have 5 bits of resolution to ramp */
581 for (i = 0; i <= 32; i++) {
582 handsfree_complete = twl6040_hf_ramp_step(codec,
583 handsfree->left_step,
584 handsfree->right_step);
586 /* ramp finished ? */
587 if (handsfree_complete)
588 break;
591 * TODO: tune: delay is longer over 0dB
592 * as increases are larger.
594 if (i >= 16)
595 schedule_timeout_interruptible(msecs_to_jiffies(delay +
596 (delay >> 1)));
597 else
598 schedule_timeout_interruptible(msecs_to_jiffies(delay));
602 if (handsfree->ramp == TWL6040_RAMP_DOWN) {
603 handsfree->active = 0;
604 complete(&handsfree->ramp_done);
605 } else
606 handsfree->active = 1;
607 handsfree->ramp = TWL6040_RAMP_NONE;
610 static int pga_event(struct snd_soc_dapm_widget *w,
611 struct snd_kcontrol *kcontrol, int event)
613 struct snd_soc_codec *codec = w->codec;
614 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
615 struct twl6040_output *out;
616 struct delayed_work *work;
617 struct workqueue_struct *queue;
619 switch (w->shift) {
620 case 2:
621 case 3:
622 out = &priv->headset;
623 work = &priv->hs_delayed_work;
624 queue = priv->hs_workqueue;
625 out->left_step = priv->hs_left_step;
626 out->right_step = priv->hs_right_step;
627 out->step_delay = 5; /* 5 ms between volume ramp steps */
628 break;
629 case 4:
630 out = &priv->handsfree;
631 work = &priv->hf_delayed_work;
632 queue = priv->hf_workqueue;
633 out->left_step = priv->hf_left_step;
634 out->right_step = priv->hf_right_step;
635 out->step_delay = 5; /* 5 ms between volume ramp steps */
636 if (SND_SOC_DAPM_EVENT_ON(event))
637 priv->non_lp++;
638 else
639 priv->non_lp--;
640 break;
641 default:
642 return -1;
645 switch (event) {
646 case SND_SOC_DAPM_POST_PMU:
647 if (out->active)
648 break;
650 /* don't use volume ramp for power-up */
651 out->left_step = out->left_vol;
652 out->right_step = out->right_vol;
654 if (!delayed_work_pending(work)) {
655 out->ramp = TWL6040_RAMP_UP;
656 queue_delayed_work(queue, work,
657 msecs_to_jiffies(1));
659 break;
661 case SND_SOC_DAPM_PRE_PMD:
662 if (!out->active)
663 break;
665 if (!delayed_work_pending(work)) {
666 /* use volume ramp for power-down */
667 out->ramp = TWL6040_RAMP_DOWN;
668 INIT_COMPLETION(out->ramp_done);
670 queue_delayed_work(queue, work,
671 msecs_to_jiffies(1));
673 wait_for_completion_timeout(&out->ramp_done,
674 msecs_to_jiffies(2000));
676 break;
679 return 0;
682 /* set headset dac and driver power mode */
683 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
685 int hslctl, hsrctl;
686 int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL;
688 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
689 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
691 if (high_perf) {
692 hslctl &= ~mask;
693 hsrctl &= ~mask;
694 } else {
695 hslctl |= mask;
696 hsrctl |= mask;
699 twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
700 twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
702 return 0;
705 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
706 struct snd_kcontrol *kcontrol, int event)
708 msleep(1);
709 return 0;
712 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
713 struct snd_kcontrol *kcontrol, int event)
715 struct snd_soc_codec *codec = w->codec;
716 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
717 int ret = 0;
719 if (SND_SOC_DAPM_EVENT_ON(event)) {
720 priv->non_lp++;
721 if (!strcmp(w->name, "Earphone Driver")) {
722 /* Earphone doesn't support low power mode */
723 priv->hs_power_mode_locked = 1;
724 ret = headset_power_mode(codec, 1);
726 } else {
727 priv->non_lp--;
728 if (!strcmp(w->name, "Earphone Driver")) {
729 priv->hs_power_mode_locked = 0;
730 ret = headset_power_mode(codec, priv->hs_power_mode);
734 msleep(1);
736 return ret;
739 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
740 struct snd_soc_jack *jack, int report)
742 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
743 int status;
745 mutex_lock(&priv->mutex);
747 /* Sync status */
748 status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
749 if (status & TWL6040_PLUGCOMP)
750 snd_soc_jack_report(jack, report, report);
751 else
752 snd_soc_jack_report(jack, 0, report);
754 mutex_unlock(&priv->mutex);
757 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
758 struct snd_soc_jack *jack, int report)
760 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
761 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
763 hs_jack->jack = jack;
764 hs_jack->report = report;
766 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
768 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
770 static void twl6040_accessory_work(struct work_struct *work)
772 struct twl6040_data *priv = container_of(work,
773 struct twl6040_data, delayed_work.work);
774 struct snd_soc_codec *codec = priv->codec;
775 struct twl6040_jack_data *hs_jack = &priv->hs_jack;
777 twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
780 /* audio interrupt handler */
781 static irqreturn_t twl6040_audio_handler(int irq, void *data)
783 struct snd_soc_codec *codec = data;
784 struct twl6040 *twl6040 = codec->control_data;
785 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
786 u8 intid;
788 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
790 if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT))
791 queue_delayed_work(priv->workqueue, &priv->delayed_work,
792 msecs_to_jiffies(200));
794 return IRQ_HANDLED;
797 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
798 struct snd_ctl_elem_value *ucontrol)
800 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
801 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
802 struct twl6040_output *out = NULL;
803 struct soc_mixer_control *mc =
804 (struct soc_mixer_control *)kcontrol->private_value;
805 int ret;
806 unsigned int reg = mc->reg;
808 /* For HS and HF we shadow the values and only actually write
809 * them out when active in order to ensure the amplifier comes on
810 * as quietly as possible. */
811 switch (reg) {
812 case TWL6040_REG_HSGAIN:
813 out = &twl6040_priv->headset;
814 break;
815 default:
816 break;
819 if (out) {
820 out->left_vol = ucontrol->value.integer.value[0];
821 out->right_vol = ucontrol->value.integer.value[1];
822 if (!out->active)
823 return 1;
826 ret = snd_soc_put_volsw(kcontrol, ucontrol);
827 if (ret < 0)
828 return ret;
830 return 1;
833 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
834 struct snd_ctl_elem_value *ucontrol)
836 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
837 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
838 struct twl6040_output *out = &twl6040_priv->headset;
839 struct soc_mixer_control *mc =
840 (struct soc_mixer_control *)kcontrol->private_value;
841 unsigned int reg = mc->reg;
843 switch (reg) {
844 case TWL6040_REG_HSGAIN:
845 out = &twl6040_priv->headset;
846 ucontrol->value.integer.value[0] = out->left_vol;
847 ucontrol->value.integer.value[1] = out->right_vol;
848 return 0;
850 default:
851 break;
854 return snd_soc_get_volsw(kcontrol, ucontrol);
857 static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
858 struct snd_ctl_elem_value *ucontrol)
860 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
861 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
862 struct twl6040_output *out = NULL;
863 struct soc_mixer_control *mc =
864 (struct soc_mixer_control *)kcontrol->private_value;
865 int ret;
866 unsigned int reg = mc->reg;
868 /* For HS and HF we shadow the values and only actually write
869 * them out when active in order to ensure the amplifier comes on
870 * as quietly as possible. */
871 switch (reg) {
872 case TWL6040_REG_HFLGAIN:
873 case TWL6040_REG_HFRGAIN:
874 out = &twl6040_priv->handsfree;
875 break;
876 default:
877 break;
880 if (out) {
881 out->left_vol = ucontrol->value.integer.value[0];
882 out->right_vol = ucontrol->value.integer.value[1];
883 if (!out->active)
884 return 1;
887 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
888 if (ret < 0)
889 return ret;
891 return 1;
894 static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_value *ucontrol)
897 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
898 struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
899 struct twl6040_output *out = &twl6040_priv->handsfree;
900 struct soc_mixer_control *mc =
901 (struct soc_mixer_control *)kcontrol->private_value;
902 unsigned int reg = mc->reg;
904 /* If these are cached registers use the cache */
905 switch (reg) {
906 case TWL6040_REG_HFLGAIN:
907 case TWL6040_REG_HFRGAIN:
908 out = &twl6040_priv->handsfree;
909 ucontrol->value.integer.value[0] = out->left_vol;
910 ucontrol->value.integer.value[1] = out->right_vol;
911 return 0;
913 default:
914 break;
917 return snd_soc_get_volsw_2r(kcontrol, ucontrol);
920 /* double control with volume update */
921 #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
922 xinvert, tlv_array)\
923 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
924 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
925 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
926 .tlv.p = (tlv_array), \
927 .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
928 .put = twl6040_put_volsw, \
929 .private_value = (unsigned long)&(struct soc_mixer_control) \
930 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
931 .max = xmax, .platform_max = xmax, .invert = xinvert} }
933 /* double control with volume update */
934 #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
935 xinvert, tlv_array)\
936 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
937 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
938 SNDRV_CTL_ELEM_ACCESS_READWRITE | \
939 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
940 .tlv.p = (tlv_array), \
941 .info = snd_soc_info_volsw_2r, \
942 .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
943 .private_value = (unsigned long)&(struct soc_mixer_control) \
944 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
945 .rshift = xshift, .max = xmax, .invert = xinvert}, }
948 * MICATT volume control:
949 * from -6 to 0 dB in 6 dB steps
951 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
954 * MICGAIN volume control:
955 * from 6 to 30 dB in 6 dB steps
957 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
960 * AFMGAIN volume control:
961 * from -18 to 24 dB in 6 dB steps
963 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
966 * HSGAIN volume control:
967 * from -30 to 0 dB in 2 dB steps
969 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
972 * HFGAIN volume control:
973 * from -52 to 6 dB in 2 dB steps
975 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
978 * EPGAIN volume control:
979 * from -24 to 6 dB in 2 dB steps
981 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
983 /* Left analog microphone selection */
984 static const char *twl6040_amicl_texts[] =
985 {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
987 /* Right analog microphone selection */
988 static const char *twl6040_amicr_texts[] =
989 {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
991 static const struct soc_enum twl6040_enum[] = {
992 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
993 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
996 static const char *twl6040_hs_texts[] = {
997 "Off", "HS DAC", "Line-In amp"
1000 static const struct soc_enum twl6040_hs_enum[] = {
1001 SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
1002 twl6040_hs_texts),
1003 SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
1004 twl6040_hs_texts),
1007 static const char *twl6040_hf_texts[] = {
1008 "Off", "HF DAC", "Line-In amp"
1011 static const struct soc_enum twl6040_hf_enum[] = {
1012 SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
1013 twl6040_hf_texts),
1014 SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
1015 twl6040_hf_texts),
1018 static const struct snd_kcontrol_new amicl_control =
1019 SOC_DAPM_ENUM("Route", twl6040_enum[0]);
1021 static const struct snd_kcontrol_new amicr_control =
1022 SOC_DAPM_ENUM("Route", twl6040_enum[1]);
1024 /* Headset DAC playback switches */
1025 static const struct snd_kcontrol_new hsl_mux_controls =
1026 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
1028 static const struct snd_kcontrol_new hsr_mux_controls =
1029 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
1031 /* Handsfree DAC playback switches */
1032 static const struct snd_kcontrol_new hfl_mux_controls =
1033 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
1035 static const struct snd_kcontrol_new hfr_mux_controls =
1036 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
1038 static const struct snd_kcontrol_new ep_driver_switch_controls =
1039 SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0);
1041 /* Headset power mode */
1042 static const char *twl6040_power_mode_texts[] = {
1043 "Low-Power", "High-Perfomance",
1046 static const struct soc_enum twl6040_power_mode_enum =
1047 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
1048 twl6040_power_mode_texts);
1050 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
1051 struct snd_ctl_elem_value *ucontrol)
1053 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1054 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1056 ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
1058 return 0;
1061 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
1062 struct snd_ctl_elem_value *ucontrol)
1064 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1065 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1066 int high_perf = ucontrol->value.enumerated.item[0];
1067 int ret = 0;
1069 if (!priv->hs_power_mode_locked)
1070 ret = headset_power_mode(codec, high_perf);
1072 if (!ret)
1073 priv->hs_power_mode = high_perf;
1075 return ret;
1078 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
1079 struct snd_ctl_elem_value *ucontrol)
1081 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1082 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1084 ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1086 return 0;
1089 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1090 struct snd_ctl_elem_value *ucontrol)
1092 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1093 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1095 priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1097 return 0;
1100 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1102 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1104 return priv->pll_power_mode;
1106 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1108 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1109 /* Capture gains */
1110 SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1111 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1112 SOC_DOUBLE_TLV("Capture Volume",
1113 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1115 /* AFM gains */
1116 SOC_DOUBLE_TLV("Aux FM Volume",
1117 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1119 /* Playback gains */
1120 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
1121 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
1122 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
1123 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
1124 SOC_SINGLE_TLV("Earphone Playback Volume",
1125 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1127 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1128 twl6040_headset_power_get_enum,
1129 twl6040_headset_power_put_enum),
1131 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1132 twl6040_pll_get_enum, twl6040_pll_put_enum),
1135 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1136 /* Inputs */
1137 SND_SOC_DAPM_INPUT("MAINMIC"),
1138 SND_SOC_DAPM_INPUT("HSMIC"),
1139 SND_SOC_DAPM_INPUT("SUBMIC"),
1140 SND_SOC_DAPM_INPUT("AFML"),
1141 SND_SOC_DAPM_INPUT("AFMR"),
1143 /* Outputs */
1144 SND_SOC_DAPM_OUTPUT("HSOL"),
1145 SND_SOC_DAPM_OUTPUT("HSOR"),
1146 SND_SOC_DAPM_OUTPUT("HFL"),
1147 SND_SOC_DAPM_OUTPUT("HFR"),
1148 SND_SOC_DAPM_OUTPUT("EP"),
1150 /* Analog input muxes for the capture amplifiers */
1151 SND_SOC_DAPM_MUX("Analog Left Capture Route",
1152 SND_SOC_NOPM, 0, 0, &amicl_control),
1153 SND_SOC_DAPM_MUX("Analog Right Capture Route",
1154 SND_SOC_NOPM, 0, 0, &amicr_control),
1156 /* Analog capture PGAs */
1157 SND_SOC_DAPM_PGA("MicAmpL",
1158 TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1159 SND_SOC_DAPM_PGA("MicAmpR",
1160 TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1162 /* Auxiliary FM PGAs */
1163 SND_SOC_DAPM_PGA("AFMAmpL",
1164 TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1165 SND_SOC_DAPM_PGA("AFMAmpR",
1166 TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1168 /* ADCs */
1169 SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1170 TWL6040_REG_MICLCTL, 2, 0),
1171 SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1172 TWL6040_REG_MICRCTL, 2, 0),
1174 /* Microphone bias */
1175 SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
1176 TWL6040_REG_AMICBCTL, 0, 0),
1177 SND_SOC_DAPM_MICBIAS("Main Mic Bias",
1178 TWL6040_REG_AMICBCTL, 4, 0),
1179 SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
1180 TWL6040_REG_DMICBCTL, 0, 0),
1181 SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
1182 TWL6040_REG_DMICBCTL, 4, 0),
1184 /* DACs */
1185 SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
1186 TWL6040_REG_HSLCTL, 0, 0,
1187 twl6040_hs_dac_event,
1188 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1189 SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
1190 TWL6040_REG_HSRCTL, 0, 0,
1191 twl6040_hs_dac_event,
1192 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1193 SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
1194 TWL6040_REG_HFLCTL, 0, 0,
1195 twl6040_power_mode_event,
1196 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1197 SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
1198 TWL6040_REG_HFRCTL, 0, 0,
1199 twl6040_power_mode_event,
1200 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1202 SND_SOC_DAPM_MUX("HF Left Playback",
1203 SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1204 SND_SOC_DAPM_MUX("HF Right Playback",
1205 SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1206 /* Analog playback Muxes */
1207 SND_SOC_DAPM_MUX("HS Left Playback",
1208 SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1209 SND_SOC_DAPM_MUX("HS Right Playback",
1210 SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1212 /* Analog playback drivers */
1213 SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver",
1214 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1215 pga_event,
1216 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1217 SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver",
1218 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1219 pga_event,
1220 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1221 SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver",
1222 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1223 pga_event,
1224 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1225 SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver",
1226 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1227 pga_event,
1228 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1229 SND_SOC_DAPM_SWITCH_E("Earphone Driver",
1230 SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls,
1231 twl6040_power_mode_event,
1232 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1234 /* Analog playback PGAs */
1235 SND_SOC_DAPM_PGA("HFDAC Left PGA",
1236 TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1237 SND_SOC_DAPM_PGA("HFDAC Right PGA",
1238 TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1242 static const struct snd_soc_dapm_route intercon[] = {
1243 /* Capture path */
1244 {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1245 {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1246 {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1248 {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1249 {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1250 {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1252 {"MicAmpL", NULL, "Analog Left Capture Route"},
1253 {"MicAmpR", NULL, "Analog Right Capture Route"},
1255 {"ADC Left", NULL, "MicAmpL"},
1256 {"ADC Right", NULL, "MicAmpR"},
1258 /* AFM path */
1259 {"AFMAmpL", "NULL", "AFML"},
1260 {"AFMAmpR", "NULL", "AFMR"},
1262 {"HS Left Playback", "HS DAC", "HSDAC Left"},
1263 {"HS Left Playback", "Line-In amp", "AFMAmpL"},
1265 {"HS Right Playback", "HS DAC", "HSDAC Right"},
1266 {"HS Right Playback", "Line-In amp", "AFMAmpR"},
1268 {"Headset Left Driver", "NULL", "HS Left Playback"},
1269 {"Headset Right Driver", "NULL", "HS Right Playback"},
1271 {"HSOL", NULL, "Headset Left Driver"},
1272 {"HSOR", NULL, "Headset Right Driver"},
1274 /* Earphone playback path */
1275 {"Earphone Driver", "Switch", "HSDAC Left"},
1276 {"EP", NULL, "Earphone Driver"},
1278 {"HF Left Playback", "HF DAC", "HFDAC Left"},
1279 {"HF Left Playback", "Line-In amp", "AFMAmpL"},
1281 {"HF Right Playback", "HF DAC", "HFDAC Right"},
1282 {"HF Right Playback", "Line-In amp", "AFMAmpR"},
1284 {"HFDAC Left PGA", NULL, "HF Left Playback"},
1285 {"HFDAC Right PGA", NULL, "HF Right Playback"},
1287 {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"},
1288 {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"},
1290 {"HFL", NULL, "Handsfree Left Driver"},
1291 {"HFR", NULL, "Handsfree Right Driver"},
1294 static int twl6040_add_widgets(struct snd_soc_codec *codec)
1296 struct snd_soc_dapm_context *dapm = &codec->dapm;
1298 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
1299 ARRAY_SIZE(twl6040_dapm_widgets));
1300 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1301 snd_soc_dapm_new_widgets(dapm);
1303 return 0;
1306 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1307 enum snd_soc_bias_level level)
1309 struct twl6040 *twl6040 = codec->control_data;
1310 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1311 int ret;
1313 switch (level) {
1314 case SND_SOC_BIAS_ON:
1315 break;
1316 case SND_SOC_BIAS_PREPARE:
1317 break;
1318 case SND_SOC_BIAS_STANDBY:
1319 if (priv->codec_powered)
1320 break;
1322 ret = twl6040_power(twl6040, 1);
1323 if (ret)
1324 return ret;
1326 priv->codec_powered = 1;
1328 /* initialize vdd/vss registers with reg_cache */
1329 twl6040_init_vdd_regs(codec);
1331 /* Set external boost GPO */
1332 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1333 break;
1334 case SND_SOC_BIAS_OFF:
1335 if (!priv->codec_powered)
1336 break;
1338 twl6040_power(twl6040, 0);
1339 priv->codec_powered = 0;
1340 break;
1343 codec->dapm.bias_level = level;
1345 return 0;
1348 static int twl6040_startup(struct snd_pcm_substream *substream,
1349 struct snd_soc_dai *dai)
1351 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1352 struct snd_soc_codec *codec = rtd->codec;
1353 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1355 snd_pcm_hw_constraint_list(substream->runtime, 0,
1356 SNDRV_PCM_HW_PARAM_RATE,
1357 &sysclk_constraints[priv->pll_power_mode]);
1359 return 0;
1362 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1363 struct snd_pcm_hw_params *params,
1364 struct snd_soc_dai *dai)
1366 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1367 struct snd_soc_codec *codec = rtd->codec;
1368 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1369 int rate;
1371 rate = params_rate(params);
1372 switch (rate) {
1373 case 11250:
1374 case 22500:
1375 case 44100:
1376 case 88200:
1377 /* These rates are not supported when HPPLL is in use */
1378 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1379 dev_err(codec->dev, "HPPLL does not support rate %d\n",
1380 rate);
1381 return -EINVAL;
1383 /* Capture is not supported with 17.64MHz sysclk */
1384 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1385 dev_err(codec->dev,
1386 "capture mode is not supported at %dHz\n",
1387 rate);
1388 return -EINVAL;
1390 priv->sysclk = 17640000;
1391 break;
1392 case 8000:
1393 case 16000:
1394 case 32000:
1395 case 48000:
1396 case 96000:
1397 priv->sysclk = 19200000;
1398 break;
1399 default:
1400 dev_err(codec->dev, "unsupported rate %d\n", rate);
1401 return -EINVAL;
1404 return 0;
1407 static int twl6040_prepare(struct snd_pcm_substream *substream,
1408 struct snd_soc_dai *dai)
1410 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1411 struct snd_soc_codec *codec = rtd->codec;
1412 struct twl6040 *twl6040 = codec->control_data;
1413 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1414 int ret;
1416 if (!priv->sysclk) {
1417 dev_err(codec->dev,
1418 "no mclk configured, call set_sysclk() on init\n");
1419 return -EINVAL;
1422 if ((priv->sysclk == 17640000) && priv->non_lp) {
1423 dev_err(codec->dev,
1424 "some enabled paths aren't supported at %dHz\n",
1425 priv->sysclk);
1426 return -EPERM;
1429 ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1430 if (ret) {
1431 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1432 return -EPERM;
1435 return 0;
1438 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1439 int clk_id, unsigned int freq, int dir)
1441 struct snd_soc_codec *codec = codec_dai->codec;
1442 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1444 switch (clk_id) {
1445 case TWL6040_SYSCLK_SEL_LPPLL:
1446 case TWL6040_SYSCLK_SEL_HPPLL:
1447 priv->pll = clk_id;
1448 priv->clk_in = freq;
1449 break;
1450 default:
1451 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1452 return -EINVAL;
1455 return 0;
1458 static struct snd_soc_dai_ops twl6040_dai_ops = {
1459 .startup = twl6040_startup,
1460 .hw_params = twl6040_hw_params,
1461 .prepare = twl6040_prepare,
1462 .set_sysclk = twl6040_set_dai_sysclk,
1465 static struct snd_soc_dai_driver twl6040_dai[] = {
1467 .name = "twl6040-hifi",
1468 .playback = {
1469 .stream_name = "Playback",
1470 .channels_min = 1,
1471 .channels_max = 2,
1472 .rates = TWL6040_RATES,
1473 .formats = TWL6040_FORMATS,
1475 .capture = {
1476 .stream_name = "Capture",
1477 .channels_min = 1,
1478 .channels_max = 2,
1479 .rates = TWL6040_RATES,
1480 .formats = TWL6040_FORMATS,
1482 .ops = &twl6040_dai_ops,
1485 .name = "twl6040-ul",
1486 .capture = {
1487 .stream_name = "Capture",
1488 .channels_min = 1,
1489 .channels_max = 2,
1490 .rates = TWL6040_RATES,
1491 .formats = TWL6040_FORMATS,
1493 .ops = &twl6040_dai_ops,
1496 .name = "twl6040-dl1",
1497 .playback = {
1498 .stream_name = "Headset Playback",
1499 .channels_min = 1,
1500 .channels_max = 2,
1501 .rates = TWL6040_RATES,
1502 .formats = TWL6040_FORMATS,
1504 .ops = &twl6040_dai_ops,
1507 .name = "twl6040-dl2",
1508 .playback = {
1509 .stream_name = "Handsfree Playback",
1510 .channels_min = 1,
1511 .channels_max = 2,
1512 .rates = TWL6040_RATES,
1513 .formats = TWL6040_FORMATS,
1515 .ops = &twl6040_dai_ops,
1518 .name = "twl6040-vib",
1519 .playback = {
1520 .stream_name = "Vibra Playback",
1521 .channels_min = 2,
1522 .channels_max = 2,
1523 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1524 .formats = TWL6040_FORMATS,
1526 .ops = &twl6040_dai_ops,
1530 #ifdef CONFIG_PM
1531 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1533 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1535 return 0;
1538 static int twl6040_resume(struct snd_soc_codec *codec)
1540 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1541 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1543 return 0;
1545 #else
1546 #define twl6040_suspend NULL
1547 #define twl6040_resume NULL
1548 #endif
1550 static int twl6040_probe(struct snd_soc_codec *codec)
1552 struct twl6040_data *priv;
1553 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1554 struct platform_device *pdev = container_of(codec->dev,
1555 struct platform_device, dev);
1556 int ret = 0;
1558 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1559 if (priv == NULL)
1560 return -ENOMEM;
1561 snd_soc_codec_set_drvdata(codec, priv);
1563 priv->codec = codec;
1564 codec->control_data = dev_get_drvdata(codec->dev->parent);
1566 if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1567 priv->hs_left_step = pdata->hs_left_step;
1568 priv->hs_right_step = pdata->hs_right_step;
1569 } else {
1570 priv->hs_left_step = 1;
1571 priv->hs_right_step = 1;
1574 if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1575 priv->hf_left_step = pdata->hf_left_step;
1576 priv->hf_right_step = pdata->hf_right_step;
1577 } else {
1578 priv->hf_left_step = 1;
1579 priv->hf_right_step = 1;
1582 priv->plug_irq = platform_get_irq(pdev, 0);
1583 if (priv->plug_irq < 0) {
1584 dev_err(codec->dev, "invalid irq\n");
1585 ret = -EINVAL;
1586 goto work_err;
1589 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1590 if (!priv->workqueue) {
1591 ret = -ENOMEM;
1592 goto work_err;
1595 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1597 mutex_init(&priv->mutex);
1599 init_completion(&priv->headset.ramp_done);
1600 init_completion(&priv->handsfree.ramp_done);
1602 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1603 if (priv->hf_workqueue == NULL) {
1604 ret = -ENOMEM;
1605 goto hfwq_err;
1607 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1608 if (priv->hs_workqueue == NULL) {
1609 ret = -ENOMEM;
1610 goto hswq_err;
1613 INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
1614 INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
1616 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1617 0, "twl6040_irq_plug", codec);
1618 if (ret) {
1619 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1620 goto plugirq_err;
1623 /* init vio registers */
1624 twl6040_init_vio_regs(codec);
1626 /* power on device */
1627 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1628 if (ret)
1629 goto bias_err;
1631 snd_soc_add_controls(codec, twl6040_snd_controls,
1632 ARRAY_SIZE(twl6040_snd_controls));
1633 twl6040_add_widgets(codec);
1635 return 0;
1637 bias_err:
1638 free_irq(priv->plug_irq, codec);
1639 plugirq_err:
1640 destroy_workqueue(priv->hs_workqueue);
1641 hswq_err:
1642 destroy_workqueue(priv->hf_workqueue);
1643 hfwq_err:
1644 destroy_workqueue(priv->workqueue);
1645 work_err:
1646 kfree(priv);
1647 return ret;
1650 static int twl6040_remove(struct snd_soc_codec *codec)
1652 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1654 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1655 free_irq(priv->plug_irq, codec);
1656 destroy_workqueue(priv->workqueue);
1657 destroy_workqueue(priv->hf_workqueue);
1658 destroy_workqueue(priv->hs_workqueue);
1659 kfree(priv);
1661 return 0;
1664 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1665 .probe = twl6040_probe,
1666 .remove = twl6040_remove,
1667 .suspend = twl6040_suspend,
1668 .resume = twl6040_resume,
1669 .read = twl6040_read_reg_cache,
1670 .write = twl6040_write,
1671 .set_bias_level = twl6040_set_bias_level,
1672 .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1673 .reg_word_size = sizeof(u8),
1674 .reg_cache_default = twl6040_reg,
1677 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1679 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1680 twl6040_dai, ARRAY_SIZE(twl6040_dai));
1683 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1685 snd_soc_unregister_codec(&pdev->dev);
1686 return 0;
1689 static struct platform_driver twl6040_codec_driver = {
1690 .driver = {
1691 .name = "twl6040-codec",
1692 .owner = THIS_MODULE,
1694 .probe = twl6040_codec_probe,
1695 .remove = __devexit_p(twl6040_codec_remove),
1698 static int __init twl6040_codec_init(void)
1700 return platform_driver_register(&twl6040_codec_driver);
1702 module_init(twl6040_codec_init);
1704 static void __exit twl6040_codec_exit(void)
1706 platform_driver_unregister(&twl6040_codec_driver);
1708 module_exit(twl6040_codec_exit);
1710 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1711 MODULE_AUTHOR("Misael Lopez Cruz");
1712 MODULE_LICENSE("GPL");