Merge tag 'mfd-for-linus-3.4' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo...
[~shefty/rdma-dev.git] / sound / soc / codecs / twl6040.c
1 /*
2  * ALSA SoC TWL6040 codec driver
3  *
4  * Author:       Misael Lopez Cruz <x0052729@ti.com>
5  *
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.
9  *
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.
14  *
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
19  *
20  */
21
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/mfd/twl6040.h>
30
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/soc-dapm.h>
36 #include <sound/initval.h>
37 #include <sound/tlv.h>
38
39 #include "twl6040.h"
40
41 #define TWL6040_RATES           SNDRV_PCM_RATE_8000_96000
42 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
43
44 #define TWL6040_OUTHS_0dB 0x00
45 #define TWL6040_OUTHS_M30dB 0x0F
46 #define TWL6040_OUTHF_0dB 0x03
47 #define TWL6040_OUTHF_M52dB 0x1D
48
49 #define TWL6040_RAMP_NONE       0
50 #define TWL6040_RAMP_UP         1
51 #define TWL6040_RAMP_DOWN       2
52
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
59
60 /* Shadow register used by the driver */
61 #define TWL6040_REG_SW_SHADOW   0x2F
62 #define TWL6040_CACHEREGNUM     (TWL6040_REG_SW_SHADOW + 1)
63
64 /* TWL6040_REG_SW_SHADOW (0x2F) fields */
65 #define TWL6040_EAR_PATH_ENABLE 0x01
66
67 struct twl6040_output {
68         u16 active;
69         u16 left_vol;
70         u16 right_vol;
71         u16 left_step;
72         u16 right_step;
73         unsigned int step_delay;
74         u16 ramp;
75         struct delayed_work work;
76         struct completion ramp_done;
77 };
78
79 struct twl6040_jack_data {
80         struct snd_soc_jack *jack;
81         struct delayed_work work;
82         int report;
83 };
84
85 /* codec private data */
86 struct twl6040_data {
87         int plug_irq;
88         int codec_powered;
89         int pll;
90         int pll_power_mode;
91         int hs_power_mode;
92         int hs_power_mode_locked;
93         unsigned int clk_in;
94         unsigned int sysclk;
95         u16 hs_left_step;
96         u16 hs_right_step;
97         u16 hf_left_step;
98         u16 hf_right_step;
99         struct twl6040_jack_data hs_jack;
100         struct snd_soc_codec *codec;
101         struct workqueue_struct *workqueue;
102         struct mutex mutex;
103         struct twl6040_output headset;
104         struct twl6040_output handsfree;
105 };
106
107 /*
108  * twl6040 register cache & default register settings
109  */
110 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
111         0x00, /* not used       0x00    */
112         0x4B, /* REG_ASICID     0x01 (ro) */
113         0x00, /* REG_ASICREV    0x02 (ro) */
114         0x00, /* REG_INTID      0x03    */
115         0x00, /* REG_INTMR      0x04    */
116         0x00, /* REG_NCPCTRL    0x05    */
117         0x00, /* REG_LDOCTL     0x06    */
118         0x60, /* REG_HPPLLCTL   0x07    */
119         0x00, /* REG_LPPLLCTL   0x08    */
120         0x4A, /* REG_LPPLLDIV   0x09    */
121         0x00, /* REG_AMICBCTL   0x0A    */
122         0x00, /* REG_DMICBCTL   0x0B    */
123         0x00, /* REG_MICLCTL    0x0C    */
124         0x00, /* REG_MICRCTL    0x0D    */
125         0x00, /* REG_MICGAIN    0x0E    */
126         0x1B, /* REG_LINEGAIN   0x0F    */
127         0x00, /* REG_HSLCTL     0x10    */
128         0x00, /* REG_HSRCTL     0x11    */
129         0x00, /* REG_HSGAIN     0x12    */
130         0x00, /* REG_EARCTL     0x13    */
131         0x00, /* REG_HFLCTL     0x14    */
132         0x00, /* REG_HFLGAIN    0x15    */
133         0x00, /* REG_HFRCTL     0x16    */
134         0x00, /* REG_HFRGAIN    0x17    */
135         0x00, /* REG_VIBCTLL    0x18    */
136         0x00, /* REG_VIBDATL    0x19    */
137         0x00, /* REG_VIBCTLR    0x1A    */
138         0x00, /* REG_VIBDATR    0x1B    */
139         0x00, /* REG_HKCTL1     0x1C    */
140         0x00, /* REG_HKCTL2     0x1D    */
141         0x00, /* REG_GPOCTL     0x1E    */
142         0x00, /* REG_ALB        0x1F    */
143         0x00, /* REG_DLB        0x20    */
144         0x00, /* not used       0x21    */
145         0x00, /* not used       0x22    */
146         0x00, /* not used       0x23    */
147         0x00, /* not used       0x24    */
148         0x00, /* not used       0x25    */
149         0x00, /* not used       0x26    */
150         0x00, /* not used       0x27    */
151         0x00, /* REG_TRIM1      0x28    */
152         0x00, /* REG_TRIM2      0x29    */
153         0x00, /* REG_TRIM3      0x2A    */
154         0x00, /* REG_HSOTRIM    0x2B    */
155         0x00, /* REG_HFOTRIM    0x2C    */
156         0x09, /* REG_ACCCTL     0x2D    */
157         0x00, /* REG_STATUS     0x2E (ro) */
158
159         0x00, /* REG_SW_SHADOW  0x2F - Shadow, non HW register */
160 };
161
162 /* List of registers to be restored after power up */
163 static const int twl6040_restore_list[] = {
164         TWL6040_REG_MICLCTL,
165         TWL6040_REG_MICRCTL,
166         TWL6040_REG_MICGAIN,
167         TWL6040_REG_LINEGAIN,
168         TWL6040_REG_HSLCTL,
169         TWL6040_REG_HSRCTL,
170         TWL6040_REG_HSGAIN,
171         TWL6040_REG_EARCTL,
172         TWL6040_REG_HFLCTL,
173         TWL6040_REG_HFLGAIN,
174         TWL6040_REG_HFRCTL,
175         TWL6040_REG_HFRGAIN,
176 };
177
178 /* set of rates for each pll: low-power and high-performance */
179 static unsigned int lp_rates[] = {
180         8000,
181         11250,
182         16000,
183         22500,
184         32000,
185         44100,
186         48000,
187         88200,
188         96000,
189 };
190
191 static unsigned int hp_rates[] = {
192         8000,
193         16000,
194         32000,
195         48000,
196         96000,
197 };
198
199 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
200         { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
201         { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
202 };
203
204 /*
205  * read twl6040 register cache
206  */
207 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
208                                                 unsigned int reg)
209 {
210         u8 *cache = codec->reg_cache;
211
212         if (reg >= TWL6040_CACHEREGNUM)
213                 return -EIO;
214
215         return cache[reg];
216 }
217
218 /*
219  * write twl6040 register cache
220  */
221 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
222                                                 u8 reg, u8 value)
223 {
224         u8 *cache = codec->reg_cache;
225
226         if (reg >= TWL6040_CACHEREGNUM)
227                 return;
228         cache[reg] = value;
229 }
230
231 /*
232  * read from twl6040 hardware register
233  */
234 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
235                         unsigned int reg)
236 {
237         struct twl6040 *twl6040 = codec->control_data;
238         u8 value;
239
240         if (reg >= TWL6040_CACHEREGNUM)
241                 return -EIO;
242
243         if (likely(reg < TWL6040_REG_SW_SHADOW)) {
244                 value = twl6040_reg_read(twl6040, reg);
245                 twl6040_write_reg_cache(codec, reg, value);
246         } else {
247                 value = twl6040_read_reg_cache(codec, reg);
248         }
249
250         return value;
251 }
252
253 /*
254  * write to the twl6040 register space
255  */
256 static int twl6040_write(struct snd_soc_codec *codec,
257                         unsigned int reg, unsigned int value)
258 {
259         struct twl6040 *twl6040 = codec->control_data;
260
261         if (reg >= TWL6040_CACHEREGNUM)
262                 return -EIO;
263
264         twl6040_write_reg_cache(codec, reg, value);
265         if (likely(reg < TWL6040_REG_SW_SHADOW))
266                 return twl6040_reg_write(twl6040, reg, value);
267         else
268                 return 0;
269 }
270
271 static void twl6040_init_chip(struct snd_soc_codec *codec)
272 {
273         struct twl6040 *twl6040 = codec->control_data;
274         u8 val;
275
276         /* Update reg_cache: ASICREV, and TRIM values */
277         val = twl6040_get_revid(twl6040);
278         twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
279
280         twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1);
281         twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2);
282         twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3);
283         twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM);
284         twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM);
285
286         /* Change chip defaults */
287         /* No imput selected for microphone amplifiers */
288         twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
289         twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
290
291         /*
292          * We need to lower the default gain values, so the ramp code
293          * can work correctly for the first playback.
294          * This reduces the pop noise heard at the first playback.
295          */
296         twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
297         twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
298         twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
299         twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
300         twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
301 }
302
303 static void twl6040_restore_regs(struct snd_soc_codec *codec)
304 {
305         u8 *cache = codec->reg_cache;
306         int reg, i;
307
308         for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
309                 reg = twl6040_restore_list[i];
310                 twl6040_write(codec, reg, cache[reg]);
311         }
312 }
313
314 /*
315  * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
316  */
317 static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
318                         unsigned int left_step, unsigned int right_step)
319 {
320
321         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
322         struct twl6040_output *headset = &priv->headset;
323         int left_complete = 0, right_complete = 0;
324         u8 reg, val;
325
326         /* left channel */
327         left_step = (left_step > 0xF) ? 0xF : left_step;
328         reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
329         val = (~reg & TWL6040_HSL_VOL_MASK);
330
331         if (headset->ramp == TWL6040_RAMP_UP) {
332                 /* ramp step up */
333                 if (val < headset->left_vol) {
334                         if (val + left_step > headset->left_vol)
335                                 val = headset->left_vol;
336                         else
337                                 val += left_step;
338
339                         reg &= ~TWL6040_HSL_VOL_MASK;
340                         twl6040_write(codec, TWL6040_REG_HSGAIN,
341                                         (reg | (~val & TWL6040_HSL_VOL_MASK)));
342                 } else {
343                         left_complete = 1;
344                 }
345         } else if (headset->ramp == TWL6040_RAMP_DOWN) {
346                 /* ramp step down */
347                 if (val > 0x0) {
348                         if ((int)val - (int)left_step < 0)
349                                 val = 0;
350                         else
351                                 val -= left_step;
352
353                         reg &= ~TWL6040_HSL_VOL_MASK;
354                         twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
355                                                 (~val & TWL6040_HSL_VOL_MASK));
356                 } else {
357                         left_complete = 1;
358                 }
359         }
360
361         /* right channel */
362         right_step = (right_step > 0xF) ? 0xF : right_step;
363         reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
364         val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
365
366         if (headset->ramp == TWL6040_RAMP_UP) {
367                 /* ramp step up */
368                 if (val < headset->right_vol) {
369                         if (val + right_step > headset->right_vol)
370                                 val = headset->right_vol;
371                         else
372                                 val += right_step;
373
374                         reg &= ~TWL6040_HSR_VOL_MASK;
375                         twl6040_write(codec, TWL6040_REG_HSGAIN,
376                                 (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
377                 } else {
378                         right_complete = 1;
379                 }
380         } else if (headset->ramp == TWL6040_RAMP_DOWN) {
381                 /* ramp step down */
382                 if (val > 0x0) {
383                         if ((int)val - (int)right_step < 0)
384                                 val = 0;
385                         else
386                                 val -= right_step;
387
388                         reg &= ~TWL6040_HSR_VOL_MASK;
389                         twl6040_write(codec, TWL6040_REG_HSGAIN,
390                                          reg | (~val << TWL6040_HSR_VOL_SHIFT));
391                 } else {
392                         right_complete = 1;
393                 }
394         }
395
396         return left_complete & right_complete;
397 }
398
399 /*
400  * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
401  */
402 static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
403                         unsigned int left_step, unsigned int right_step)
404 {
405         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
406         struct twl6040_output *handsfree = &priv->handsfree;
407         int left_complete = 0, right_complete = 0;
408         u16 reg, val;
409
410         /* left channel */
411         left_step = (left_step > 0x1D) ? 0x1D : left_step;
412         reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
413         reg = 0x1D - reg;
414         val = (reg & TWL6040_HF_VOL_MASK);
415         if (handsfree->ramp == TWL6040_RAMP_UP) {
416                 /* ramp step up */
417                 if (val < handsfree->left_vol) {
418                         if (val + left_step > handsfree->left_vol)
419                                 val = handsfree->left_vol;
420                         else
421                                 val += left_step;
422
423                         reg &= ~TWL6040_HF_VOL_MASK;
424                         twl6040_write(codec, TWL6040_REG_HFLGAIN,
425                                                 reg | (0x1D - val));
426                 } else {
427                         left_complete = 1;
428                 }
429         } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
430                 /* ramp step down */
431                 if (val > 0) {
432                         if ((int)val - (int)left_step < 0)
433                                 val = 0;
434                         else
435                                 val -= left_step;
436
437                         reg &= ~TWL6040_HF_VOL_MASK;
438                         twl6040_write(codec, TWL6040_REG_HFLGAIN,
439                                                 reg | (0x1D - val));
440                 } else {
441                         left_complete = 1;
442                 }
443         }
444
445         /* right channel */
446         right_step = (right_step > 0x1D) ? 0x1D : right_step;
447         reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
448         reg = 0x1D - reg;
449         val = (reg & TWL6040_HF_VOL_MASK);
450         if (handsfree->ramp == TWL6040_RAMP_UP) {
451                 /* ramp step up */
452                 if (val < handsfree->right_vol) {
453                         if (val + right_step > handsfree->right_vol)
454                                 val = handsfree->right_vol;
455                         else
456                                 val += right_step;
457
458                         reg &= ~TWL6040_HF_VOL_MASK;
459                         twl6040_write(codec, TWL6040_REG_HFRGAIN,
460                                                 reg | (0x1D - val));
461                 } else {
462                         right_complete = 1;
463                 }
464         } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
465                 /* ramp step down */
466                 if (val > 0) {
467                         if ((int)val - (int)right_step < 0)
468                                 val = 0;
469                         else
470                                 val -= right_step;
471
472                         reg &= ~TWL6040_HF_VOL_MASK;
473                         twl6040_write(codec, TWL6040_REG_HFRGAIN,
474                                                 reg | (0x1D - val));
475                 }
476         }
477
478         return left_complete & right_complete;
479 }
480
481 /*
482  * This work ramps both output PGAs at stream start/stop time to
483  * minimise pop associated with DAPM power switching.
484  */
485 static void twl6040_pga_hs_work(struct work_struct *work)
486 {
487         struct twl6040_data *priv =
488                 container_of(work, struct twl6040_data, headset.work.work);
489         struct snd_soc_codec *codec = priv->codec;
490         struct twl6040_output *headset = &priv->headset;
491         int i, headset_complete;
492
493         /* do we need to ramp at all ? */
494         if (headset->ramp == TWL6040_RAMP_NONE)
495                 return;
496
497         /* HS PGA gain range: 0x0 - 0xf (0 - 15) */
498         for (i = 0; i < 16; i++) {
499                 headset_complete = twl6040_hs_ramp_step(codec,
500                                                 headset->left_step,
501                                                 headset->right_step);
502
503                 /* ramp finished ? */
504                 if (headset_complete)
505                         break;
506
507                 schedule_timeout_interruptible(
508                                 msecs_to_jiffies(headset->step_delay));
509         }
510
511         if (headset->ramp == TWL6040_RAMP_DOWN) {
512                 headset->active = 0;
513                 complete(&headset->ramp_done);
514         } else {
515                 headset->active = 1;
516         }
517         headset->ramp = TWL6040_RAMP_NONE;
518 }
519
520 static void twl6040_pga_hf_work(struct work_struct *work)
521 {
522         struct twl6040_data *priv =
523                 container_of(work, struct twl6040_data, handsfree.work.work);
524         struct snd_soc_codec *codec = priv->codec;
525         struct twl6040_output *handsfree = &priv->handsfree;
526         int i, handsfree_complete;
527
528         /* do we need to ramp at all ? */
529         if (handsfree->ramp == TWL6040_RAMP_NONE)
530                 return;
531
532         /*
533          * HF PGA gain range: 0x00 - 0x1d (0 - 29) */
534         for (i = 0; i < 30; i++) {
535                 handsfree_complete = twl6040_hf_ramp_step(codec,
536                                                 handsfree->left_step,
537                                                 handsfree->right_step);
538
539                 /* ramp finished ? */
540                 if (handsfree_complete)
541                         break;
542
543                 schedule_timeout_interruptible(
544                                 msecs_to_jiffies(handsfree->step_delay));
545         }
546
547
548         if (handsfree->ramp == TWL6040_RAMP_DOWN) {
549                 handsfree->active = 0;
550                 complete(&handsfree->ramp_done);
551         } else
552                 handsfree->active = 1;
553         handsfree->ramp = TWL6040_RAMP_NONE;
554 }
555
556 static int out_drv_event(struct snd_soc_dapm_widget *w,
557                         struct snd_kcontrol *kcontrol, int event)
558 {
559         struct snd_soc_codec *codec = w->codec;
560         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
561         struct twl6040_output *out;
562         struct delayed_work *work;
563
564         switch (w->shift) {
565         case 2: /* Headset output driver */
566                 out = &priv->headset;
567                 work = &out->work;
568                 /*
569                  * Make sure, that we do not mess up variables for already
570                  * executing work.
571                  */
572                 cancel_delayed_work_sync(work);
573
574                 out->left_step = priv->hs_left_step;
575                 out->right_step = priv->hs_right_step;
576                 out->step_delay = 5;    /* 5 ms between volume ramp steps */
577                 break;
578         case 4: /* Handsfree output driver */
579                 out = &priv->handsfree;
580                 work = &out->work;
581                 /*
582                  * Make sure, that we do not mess up variables for already
583                  * executing work.
584                  */
585                 cancel_delayed_work_sync(work);
586
587                 out->left_step = priv->hf_left_step;
588                 out->right_step = priv->hf_right_step;
589                 out->step_delay = 5;    /* 5 ms between volume ramp steps */
590                 break;
591         default:
592                 return -1;
593         }
594
595         switch (event) {
596         case SND_SOC_DAPM_POST_PMU:
597                 if (out->active)
598                         break;
599
600                 /* don't use volume ramp for power-up */
601                 out->ramp = TWL6040_RAMP_UP;
602                 out->left_step = out->left_vol;
603                 out->right_step = out->right_vol;
604
605                 queue_delayed_work(priv->workqueue, work, msecs_to_jiffies(1));
606                 break;
607
608         case SND_SOC_DAPM_PRE_PMD:
609                 if (!out->active)
610                         break;
611
612                 /* use volume ramp for power-down */
613                 out->ramp = TWL6040_RAMP_DOWN;
614                 INIT_COMPLETION(out->ramp_done);
615
616                 queue_delayed_work(priv->workqueue, work, msecs_to_jiffies(1));
617
618                 wait_for_completion_timeout(&out->ramp_done,
619                                             msecs_to_jiffies(2000));
620                 break;
621         }
622
623         return 0;
624 }
625
626 /* set headset dac and driver power mode */
627 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
628 {
629         int hslctl, hsrctl;
630         int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
631
632         hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
633         hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
634
635         if (high_perf) {
636                 hslctl &= ~mask;
637                 hsrctl &= ~mask;
638         } else {
639                 hslctl |= mask;
640                 hsrctl |= mask;
641         }
642
643         twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
644         twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
645
646         return 0;
647 }
648
649 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
650                         struct snd_kcontrol *kcontrol, int event)
651 {
652         struct snd_soc_codec *codec = w->codec;
653         u8 hslctl, hsrctl;
654
655         /*
656          * Workaround for Headset DC offset caused pop noise:
657          * Both HS DAC need to be turned on (before the HS driver) and off at
658          * the same time.
659          */
660         hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
661         hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
662         if (SND_SOC_DAPM_EVENT_ON(event)) {
663                 hslctl |= TWL6040_HSDACENA;
664                 hsrctl |= TWL6040_HSDACENA;
665         } else {
666                 hslctl &= ~TWL6040_HSDACENA;
667                 hsrctl &= ~TWL6040_HSDACENA;
668         }
669         twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
670         twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
671
672         msleep(1);
673         return 0;
674 }
675
676 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w,
677                         struct snd_kcontrol *kcontrol, int event)
678 {
679         struct snd_soc_codec *codec = w->codec;
680         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
681         int ret = 0;
682
683         if (SND_SOC_DAPM_EVENT_ON(event)) {
684                 /* Earphone doesn't support low power mode */
685                 priv->hs_power_mode_locked = 1;
686                 ret = headset_power_mode(codec, 1);
687         } else {
688                 priv->hs_power_mode_locked = 0;
689                 ret = headset_power_mode(codec, priv->hs_power_mode);
690         }
691
692         msleep(1);
693
694         return ret;
695 }
696
697 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
698                                    struct snd_soc_jack *jack, int report)
699 {
700         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
701         int status;
702
703         mutex_lock(&priv->mutex);
704
705         /* Sync status */
706         status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
707         if (status & TWL6040_PLUGCOMP)
708                 snd_soc_jack_report(jack, report, report);
709         else
710                 snd_soc_jack_report(jack, 0, report);
711
712         mutex_unlock(&priv->mutex);
713 }
714
715 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
716                                 struct snd_soc_jack *jack, int report)
717 {
718         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
719         struct twl6040_jack_data *hs_jack = &priv->hs_jack;
720
721         hs_jack->jack = jack;
722         hs_jack->report = report;
723
724         twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
725 }
726 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
727
728 static void twl6040_accessory_work(struct work_struct *work)
729 {
730         struct twl6040_data *priv = container_of(work,
731                                         struct twl6040_data, hs_jack.work.work);
732         struct snd_soc_codec *codec = priv->codec;
733         struct twl6040_jack_data *hs_jack = &priv->hs_jack;
734
735         twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
736 }
737
738 /* audio interrupt handler */
739 static irqreturn_t twl6040_audio_handler(int irq, void *data)
740 {
741         struct snd_soc_codec *codec = data;
742         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
743
744         queue_delayed_work(priv->workqueue, &priv->hs_jack.work,
745                            msecs_to_jiffies(200));
746
747         return IRQ_HANDLED;
748 }
749
750 static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
751                                   struct snd_ctl_elem_value *ucontrol)
752 {
753         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
754         struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
755         struct twl6040_output *out = NULL;
756         struct soc_mixer_control *mc =
757                 (struct soc_mixer_control *)kcontrol->private_value;
758         int ret;
759
760         /* For HS and HF we shadow the values and only actually write
761          * them out when active in order to ensure the amplifier comes on
762          * as quietly as possible. */
763         switch (mc->reg) {
764         case TWL6040_REG_HSGAIN:
765                 out = &twl6040_priv->headset;
766                 break;
767         case TWL6040_REG_HFLGAIN:
768                 out = &twl6040_priv->handsfree;
769                 break;
770         default:
771                 dev_warn(codec->dev, "%s: Unexpected register: 0x%02x\n",
772                                         __func__, mc->reg);
773                 return -EINVAL;
774         }
775
776         out->left_vol = ucontrol->value.integer.value[0];
777         out->right_vol = ucontrol->value.integer.value[1];
778         if (!out->active)
779                 return 1;
780
781         ret = snd_soc_put_volsw(kcontrol, ucontrol);
782         if (ret < 0)
783                 return ret;
784
785         return 1;
786 }
787
788 static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
789                                 struct snd_ctl_elem_value *ucontrol)
790 {
791         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
792         struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
793         struct twl6040_output *out = &twl6040_priv->headset;
794         struct soc_mixer_control *mc =
795                 (struct soc_mixer_control *)kcontrol->private_value;
796
797         switch (mc->reg) {
798         case TWL6040_REG_HSGAIN:
799                 out = &twl6040_priv->headset;
800                 break;
801         case TWL6040_REG_HFLGAIN:
802                 out = &twl6040_priv->handsfree;
803                 break;
804         default:
805                 dev_warn(codec->dev, "%s: Unexpected register: 0x%02x\n",
806                                         __func__, mc->reg);
807                 return -EINVAL;
808         }
809
810         ucontrol->value.integer.value[0] = out->left_vol;
811         ucontrol->value.integer.value[1] = out->right_vol;
812         return 0;
813 }
814
815 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
816         struct snd_ctl_elem_value *ucontrol)
817 {
818         struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
819         struct snd_soc_dapm_widget *widget = wlist->widgets[0];
820         struct snd_soc_codec *codec = widget->codec;
821         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
822         unsigned int val;
823
824         /* Do not allow changes while Input/FF efect is running */
825         val = twl6040_read_reg_volatile(codec, e->reg);
826         if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
827                 return -EBUSY;
828
829         return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
830 }
831
832 /*
833  * MICATT volume control:
834  * from -6 to 0 dB in 6 dB steps
835  */
836 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
837
838 /*
839  * MICGAIN volume control:
840  * from 6 to 30 dB in 6 dB steps
841  */
842 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
843
844 /*
845  * AFMGAIN volume control:
846  * from -18 to 24 dB in 6 dB steps
847  */
848 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
849
850 /*
851  * HSGAIN volume control:
852  * from -30 to 0 dB in 2 dB steps
853  */
854 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
855
856 /*
857  * HFGAIN volume control:
858  * from -52 to 6 dB in 2 dB steps
859  */
860 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
861
862 /*
863  * EPGAIN volume control:
864  * from -24 to 6 dB in 2 dB steps
865  */
866 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
867
868 /* Left analog microphone selection */
869 static const char *twl6040_amicl_texts[] =
870         {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
871
872 /* Right analog microphone selection */
873 static const char *twl6040_amicr_texts[] =
874         {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
875
876 static const struct soc_enum twl6040_enum[] = {
877         SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
878         SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
879 };
880
881 static const char *twl6040_hs_texts[] = {
882         "Off", "HS DAC", "Line-In amp"
883 };
884
885 static const struct soc_enum twl6040_hs_enum[] = {
886         SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
887                         twl6040_hs_texts),
888         SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
889                         twl6040_hs_texts),
890 };
891
892 static const char *twl6040_hf_texts[] = {
893         "Off", "HF DAC", "Line-In amp"
894 };
895
896 static const struct soc_enum twl6040_hf_enum[] = {
897         SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
898                         twl6040_hf_texts),
899         SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
900                         twl6040_hf_texts),
901 };
902
903 static const char *twl6040_vibrapath_texts[] = {
904         "Input FF", "Audio PDM"
905 };
906
907 static const struct soc_enum twl6040_vibra_enum[] = {
908         SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
909                         ARRAY_SIZE(twl6040_vibrapath_texts),
910                         twl6040_vibrapath_texts),
911         SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
912                         ARRAY_SIZE(twl6040_vibrapath_texts),
913                         twl6040_vibrapath_texts),
914 };
915
916 static const struct snd_kcontrol_new amicl_control =
917         SOC_DAPM_ENUM("Route", twl6040_enum[0]);
918
919 static const struct snd_kcontrol_new amicr_control =
920         SOC_DAPM_ENUM("Route", twl6040_enum[1]);
921
922 /* Headset DAC playback switches */
923 static const struct snd_kcontrol_new hsl_mux_controls =
924         SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
925
926 static const struct snd_kcontrol_new hsr_mux_controls =
927         SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
928
929 /* Handsfree DAC playback switches */
930 static const struct snd_kcontrol_new hfl_mux_controls =
931         SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
932
933 static const struct snd_kcontrol_new hfr_mux_controls =
934         SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
935
936 static const struct snd_kcontrol_new ep_path_enable_control =
937         SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
938
939 static const struct snd_kcontrol_new auxl_switch_control =
940         SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
941
942 static const struct snd_kcontrol_new auxr_switch_control =
943         SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
944
945 /* Vibra playback switches */
946 static const struct snd_kcontrol_new vibral_mux_controls =
947         SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
948                 snd_soc_dapm_get_enum_double,
949                 twl6040_soc_dapm_put_vibra_enum);
950
951 static const struct snd_kcontrol_new vibrar_mux_controls =
952         SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
953                 snd_soc_dapm_get_enum_double,
954                 twl6040_soc_dapm_put_vibra_enum);
955
956 /* Headset power mode */
957 static const char *twl6040_power_mode_texts[] = {
958         "Low-Power", "High-Perfomance",
959 };
960
961 static const struct soc_enum twl6040_power_mode_enum =
962         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
963                         twl6040_power_mode_texts);
964
965 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
966         struct snd_ctl_elem_value *ucontrol)
967 {
968         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
969         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
970
971         ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
972
973         return 0;
974 }
975
976 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
977         struct snd_ctl_elem_value *ucontrol)
978 {
979         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
980         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
981         int high_perf = ucontrol->value.enumerated.item[0];
982         int ret = 0;
983
984         if (!priv->hs_power_mode_locked)
985                 ret = headset_power_mode(codec, high_perf);
986
987         if (!ret)
988                 priv->hs_power_mode = high_perf;
989
990         return ret;
991 }
992
993 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
994         struct snd_ctl_elem_value *ucontrol)
995 {
996         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
997         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
998
999         ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
1000
1001         return 0;
1002 }
1003
1004 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
1005         struct snd_ctl_elem_value *ucontrol)
1006 {
1007         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1008         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1009
1010         priv->pll_power_mode = ucontrol->value.enumerated.item[0];
1011
1012         return 0;
1013 }
1014
1015 int twl6040_get_dl1_gain(struct snd_soc_codec *codec)
1016 {
1017         struct snd_soc_dapm_context *dapm = &codec->dapm;
1018
1019         if (snd_soc_dapm_get_pin_status(dapm, "EP"))
1020                 return -1; /* -1dB */
1021
1022         if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
1023                 snd_soc_dapm_get_pin_status(dapm, "HSOL")) {
1024
1025                 u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL);
1026                 if (val & TWL6040_HSDACMODE)
1027                         /* HSDACL in LP mode */
1028                         return -8; /* -8dB */
1029                 else
1030                         /* HSDACL in HP mode */
1031                         return -1; /* -1dB */
1032         }
1033         return 0; /* 0dB */
1034 }
1035 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);
1036
1037 int twl6040_get_clk_id(struct snd_soc_codec *codec)
1038 {
1039         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1040
1041         return priv->pll_power_mode;
1042 }
1043 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1044
1045 int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
1046 {
1047         if (unlikely(trim >= TWL6040_TRIM_INVAL))
1048                 return -EINVAL;
1049
1050         return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim);
1051 }
1052 EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
1053
1054 int twl6040_get_hs_step_size(struct snd_soc_codec *codec)
1055 {
1056         struct twl6040 *twl6040 = codec->control_data;
1057
1058         if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_2)
1059                 /* For ES under ES_1.3 HS step is 2 mV */
1060                 return 2;
1061         else
1062                 /* For ES_1.3 HS step is 1 mV */
1063                 return 1;
1064 }
1065 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);
1066
1067 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
1068         /* Capture gains */
1069         SOC_DOUBLE_TLV("Capture Preamplifier Volume",
1070                 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
1071         SOC_DOUBLE_TLV("Capture Volume",
1072                 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
1073
1074         /* AFM gains */
1075         SOC_DOUBLE_TLV("Aux FM Volume",
1076                 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1077
1078         /* Playback gains */
1079         SOC_DOUBLE_EXT_TLV("Headset Playback Volume",
1080                 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, twl6040_get_volsw,
1081                 twl6040_put_volsw, hs_tlv),
1082         SOC_DOUBLE_R_EXT_TLV("Handsfree Playback Volume",
1083                 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1,
1084                 twl6040_get_volsw, twl6040_put_volsw, hf_tlv),
1085         SOC_SINGLE_TLV("Earphone Playback Volume",
1086                 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1087
1088         SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1089                 twl6040_headset_power_get_enum,
1090                 twl6040_headset_power_put_enum),
1091
1092         SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1093                 twl6040_pll_get_enum, twl6040_pll_put_enum),
1094 };
1095
1096 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
1097         /* Inputs */
1098         SND_SOC_DAPM_INPUT("MAINMIC"),
1099         SND_SOC_DAPM_INPUT("HSMIC"),
1100         SND_SOC_DAPM_INPUT("SUBMIC"),
1101         SND_SOC_DAPM_INPUT("AFML"),
1102         SND_SOC_DAPM_INPUT("AFMR"),
1103
1104         /* Outputs */
1105         SND_SOC_DAPM_OUTPUT("HSOL"),
1106         SND_SOC_DAPM_OUTPUT("HSOR"),
1107         SND_SOC_DAPM_OUTPUT("HFL"),
1108         SND_SOC_DAPM_OUTPUT("HFR"),
1109         SND_SOC_DAPM_OUTPUT("EP"),
1110         SND_SOC_DAPM_OUTPUT("AUXL"),
1111         SND_SOC_DAPM_OUTPUT("AUXR"),
1112         SND_SOC_DAPM_OUTPUT("VIBRAL"),
1113         SND_SOC_DAPM_OUTPUT("VIBRAR"),
1114
1115         /* Analog input muxes for the capture amplifiers */
1116         SND_SOC_DAPM_MUX("Analog Left Capture Route",
1117                         SND_SOC_NOPM, 0, 0, &amicl_control),
1118         SND_SOC_DAPM_MUX("Analog Right Capture Route",
1119                         SND_SOC_NOPM, 0, 0, &amicr_control),
1120
1121         /* Analog capture PGAs */
1122         SND_SOC_DAPM_PGA("MicAmpL",
1123                         TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
1124         SND_SOC_DAPM_PGA("MicAmpR",
1125                         TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
1126
1127         /* Auxiliary FM PGAs */
1128         SND_SOC_DAPM_PGA("AFMAmpL",
1129                         TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
1130         SND_SOC_DAPM_PGA("AFMAmpR",
1131                         TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
1132
1133         /* ADCs */
1134         SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
1135                         TWL6040_REG_MICLCTL, 2, 0),
1136         SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
1137                         TWL6040_REG_MICRCTL, 2, 0),
1138
1139         /* Microphone bias */
1140         SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
1141                             TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
1142         SND_SOC_DAPM_SUPPLY("Main Mic Bias",
1143                             TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
1144         SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
1145                             TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
1146         SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
1147                             TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
1148
1149         /* DACs */
1150         SND_SOC_DAPM_DAC("HSDAC Left", "Headset Playback", SND_SOC_NOPM, 0, 0),
1151         SND_SOC_DAPM_DAC("HSDAC Right", "Headset Playback", SND_SOC_NOPM, 0, 0),
1152         SND_SOC_DAPM_DAC("HFDAC Left", "Handsfree Playback",
1153                          TWL6040_REG_HFLCTL, 0, 0),
1154         SND_SOC_DAPM_DAC("HFDAC Right", "Handsfree Playback",
1155                          TWL6040_REG_HFRCTL, 0, 0),
1156         /* Virtual DAC for vibra path (DL4 channel) */
1157         SND_SOC_DAPM_DAC("VIBRA DAC", "Vibra Playback",
1158                         SND_SOC_NOPM, 0, 0),
1159
1160         SND_SOC_DAPM_MUX("Handsfree Left Playback",
1161                         SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1162         SND_SOC_DAPM_MUX("Handsfree Right Playback",
1163                         SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
1164         /* Analog playback Muxes */
1165         SND_SOC_DAPM_MUX("Headset Left Playback",
1166                         SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1167         SND_SOC_DAPM_MUX("Headset Right Playback",
1168                         SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1169
1170         SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
1171                         &vibral_mux_controls),
1172         SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
1173                         &vibrar_mux_controls),
1174
1175         SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
1176                         &ep_path_enable_control),
1177         SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
1178                         &auxl_switch_control),
1179         SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
1180                         &auxr_switch_control),
1181
1182         /* Analog playback drivers */
1183         SND_SOC_DAPM_OUT_DRV_E("HF Left Driver",
1184                         TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1185                         out_drv_event,
1186                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1187         SND_SOC_DAPM_OUT_DRV_E("HF Right Driver",
1188                         TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1189                         out_drv_event,
1190                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1191         SND_SOC_DAPM_OUT_DRV_E("HS Left Driver",
1192                         TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1193                         out_drv_event,
1194                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1195         SND_SOC_DAPM_OUT_DRV_E("HS Right Driver",
1196                         TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1197                         out_drv_event,
1198                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1199         SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
1200                         TWL6040_REG_EARCTL, 0, 0, NULL, 0,
1201                         twl6040_ep_drv_event,
1202                         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1203         SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
1204                         TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
1205         SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
1206                         TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),
1207
1208         SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
1209                             NULL, 0),
1210         SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
1211                             NULL, 0),
1212         SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
1213                               twl6040_hs_dac_event,
1214                               SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1215
1216         /* Analog playback PGAs */
1217         SND_SOC_DAPM_PGA("HF Left PGA",
1218                         TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1219         SND_SOC_DAPM_PGA("HF Right PGA",
1220                         TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
1221
1222 };
1223
1224 static const struct snd_soc_dapm_route intercon[] = {
1225         /* Capture path */
1226         {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
1227         {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
1228         {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
1229
1230         {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
1231         {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
1232         {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
1233
1234         {"MicAmpL", NULL, "Analog Left Capture Route"},
1235         {"MicAmpR", NULL, "Analog Right Capture Route"},
1236
1237         {"ADC Left", NULL, "MicAmpL"},
1238         {"ADC Right", NULL, "MicAmpR"},
1239
1240         /* AFM path */
1241         {"AFMAmpL", NULL, "AFML"},
1242         {"AFMAmpR", NULL, "AFMR"},
1243
1244         {"HSDAC Left", NULL, "HSDAC Power"},
1245         {"HSDAC Right", NULL, "HSDAC Power"},
1246
1247         {"Headset Left Playback", "HS DAC", "HSDAC Left"},
1248         {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
1249
1250         {"Headset Right Playback", "HS DAC", "HSDAC Right"},
1251         {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
1252
1253         {"HS Left Driver", NULL, "Headset Left Playback"},
1254         {"HS Right Driver", NULL, "Headset Right Playback"},
1255
1256         {"HSOL", NULL, "HS Left Driver"},
1257         {"HSOR", NULL, "HS Right Driver"},
1258
1259         /* Earphone playback path */
1260         {"Earphone Playback", "Switch", "HSDAC Left"},
1261         {"Earphone Driver", NULL, "Earphone Playback"},
1262         {"EP", NULL, "Earphone Driver"},
1263
1264         {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
1265         {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
1266
1267         {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
1268         {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
1269
1270         {"HF Left PGA", NULL, "Handsfree Left Playback"},
1271         {"HF Right PGA", NULL, "Handsfree Right Playback"},
1272
1273         {"HF Left Driver", NULL, "HF Left PGA"},
1274         {"HF Right Driver", NULL, "HF Right PGA"},
1275
1276         {"HFL", NULL, "HF Left Driver"},
1277         {"HFR", NULL, "HF Right Driver"},
1278
1279         {"AUXL Playback", "Switch", "HF Left PGA"},
1280         {"AUXR Playback", "Switch", "HF Right PGA"},
1281
1282         {"AUXL", NULL, "AUXL Playback"},
1283         {"AUXR", NULL, "AUXR Playback"},
1284
1285         /* Vibrator paths */
1286         {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
1287         {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
1288
1289         {"Vibra Left Driver", NULL, "Vibra Left Playback"},
1290         {"Vibra Right Driver", NULL, "Vibra Right Playback"},
1291         {"Vibra Left Driver", NULL, "Vibra Left Control"},
1292         {"Vibra Right Driver", NULL, "Vibra Right Control"},
1293
1294         {"VIBRAL", NULL, "Vibra Left Driver"},
1295         {"VIBRAR", NULL, "Vibra Right Driver"},
1296 };
1297
1298 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1299                                 enum snd_soc_bias_level level)
1300 {
1301         struct twl6040 *twl6040 = codec->control_data;
1302         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1303         int ret;
1304
1305         switch (level) {
1306         case SND_SOC_BIAS_ON:
1307                 break;
1308         case SND_SOC_BIAS_PREPARE:
1309                 break;
1310         case SND_SOC_BIAS_STANDBY:
1311                 if (priv->codec_powered)
1312                         break;
1313
1314                 ret = twl6040_power(twl6040, 1);
1315                 if (ret)
1316                         return ret;
1317
1318                 priv->codec_powered = 1;
1319
1320                 twl6040_restore_regs(codec);
1321
1322                 /* Set external boost GPO */
1323                 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1324                 break;
1325         case SND_SOC_BIAS_OFF:
1326                 if (!priv->codec_powered)
1327                         break;
1328
1329                 twl6040_power(twl6040, 0);
1330                 priv->codec_powered = 0;
1331                 break;
1332         }
1333
1334         codec->dapm.bias_level = level;
1335
1336         return 0;
1337 }
1338
1339 static int twl6040_startup(struct snd_pcm_substream *substream,
1340                         struct snd_soc_dai *dai)
1341 {
1342         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1343         struct snd_soc_codec *codec = rtd->codec;
1344         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1345
1346         snd_pcm_hw_constraint_list(substream->runtime, 0,
1347                                 SNDRV_PCM_HW_PARAM_RATE,
1348                                 &sysclk_constraints[priv->pll_power_mode]);
1349
1350         return 0;
1351 }
1352
1353 static int twl6040_hw_params(struct snd_pcm_substream *substream,
1354                         struct snd_pcm_hw_params *params,
1355                         struct snd_soc_dai *dai)
1356 {
1357         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1358         struct snd_soc_codec *codec = rtd->codec;
1359         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1360         int rate;
1361
1362         rate = params_rate(params);
1363         switch (rate) {
1364         case 11250:
1365         case 22500:
1366         case 44100:
1367         case 88200:
1368                 /* These rates are not supported when HPPLL is in use */
1369                 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
1370                         dev_err(codec->dev, "HPPLL does not support rate %d\n",
1371                                 rate);
1372                         return -EINVAL;
1373                 }
1374                 priv->sysclk = 17640000;
1375                 break;
1376         case 8000:
1377         case 16000:
1378         case 32000:
1379         case 48000:
1380         case 96000:
1381                 priv->sysclk = 19200000;
1382                 break;
1383         default:
1384                 dev_err(codec->dev, "unsupported rate %d\n", rate);
1385                 return -EINVAL;
1386         }
1387
1388         return 0;
1389 }
1390
1391 static int twl6040_prepare(struct snd_pcm_substream *substream,
1392                         struct snd_soc_dai *dai)
1393 {
1394         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1395         struct snd_soc_codec *codec = rtd->codec;
1396         struct twl6040 *twl6040 = codec->control_data;
1397         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1398         int ret;
1399
1400         if (!priv->sysclk) {
1401                 dev_err(codec->dev,
1402                         "no mclk configured, call set_sysclk() on init\n");
1403                 return -EINVAL;
1404         }
1405
1406         ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1407         if (ret) {
1408                 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1409                 return -EPERM;
1410         }
1411
1412         return 0;
1413 }
1414
1415 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1416                 int clk_id, unsigned int freq, int dir)
1417 {
1418         struct snd_soc_codec *codec = codec_dai->codec;
1419         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1420
1421         switch (clk_id) {
1422         case TWL6040_SYSCLK_SEL_LPPLL:
1423         case TWL6040_SYSCLK_SEL_HPPLL:
1424                 priv->pll = clk_id;
1425                 priv->clk_in = freq;
1426                 break;
1427         default:
1428                 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1429                 return -EINVAL;
1430         }
1431
1432         return 0;
1433 }
1434
1435 static const struct snd_soc_dai_ops twl6040_dai_ops = {
1436         .startup        = twl6040_startup,
1437         .hw_params      = twl6040_hw_params,
1438         .prepare        = twl6040_prepare,
1439         .set_sysclk     = twl6040_set_dai_sysclk,
1440 };
1441
1442 static struct snd_soc_dai_driver twl6040_dai[] = {
1443 {
1444         .name = "twl6040-legacy",
1445         .playback = {
1446                 .stream_name = "Playback",
1447                 .channels_min = 1,
1448                 .channels_max = 5,
1449                 .rates = TWL6040_RATES,
1450                 .formats = TWL6040_FORMATS,
1451         },
1452         .capture = {
1453                 .stream_name = "Capture",
1454                 .channels_min = 1,
1455                 .channels_max = 2,
1456                 .rates = TWL6040_RATES,
1457                 .formats = TWL6040_FORMATS,
1458         },
1459         .ops = &twl6040_dai_ops,
1460 },
1461 {
1462         .name = "twl6040-ul",
1463         .capture = {
1464                 .stream_name = "Capture",
1465                 .channels_min = 1,
1466                 .channels_max = 2,
1467                 .rates = TWL6040_RATES,
1468                 .formats = TWL6040_FORMATS,
1469         },
1470         .ops = &twl6040_dai_ops,
1471 },
1472 {
1473         .name = "twl6040-dl1",
1474         .playback = {
1475                 .stream_name = "Headset Playback",
1476                 .channels_min = 1,
1477                 .channels_max = 2,
1478                 .rates = TWL6040_RATES,
1479                 .formats = TWL6040_FORMATS,
1480         },
1481         .ops = &twl6040_dai_ops,
1482 },
1483 {
1484         .name = "twl6040-dl2",
1485         .playback = {
1486                 .stream_name = "Handsfree Playback",
1487                 .channels_min = 1,
1488                 .channels_max = 2,
1489                 .rates = TWL6040_RATES,
1490                 .formats = TWL6040_FORMATS,
1491         },
1492         .ops = &twl6040_dai_ops,
1493 },
1494 {
1495         .name = "twl6040-vib",
1496         .playback = {
1497                 .stream_name = "Vibra Playback",
1498                 .channels_min = 1,
1499                 .channels_max = 1,
1500                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1501                 .formats = TWL6040_FORMATS,
1502         },
1503         .ops = &twl6040_dai_ops,
1504 },
1505 };
1506
1507 #ifdef CONFIG_PM
1508 static int twl6040_suspend(struct snd_soc_codec *codec)
1509 {
1510         twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1511
1512         return 0;
1513 }
1514
1515 static int twl6040_resume(struct snd_soc_codec *codec)
1516 {
1517         twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1518         twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1519
1520         return 0;
1521 }
1522 #else
1523 #define twl6040_suspend NULL
1524 #define twl6040_resume NULL
1525 #endif
1526
1527 static int twl6040_probe(struct snd_soc_codec *codec)
1528 {
1529         struct twl6040_data *priv;
1530         struct twl6040_codec_data *pdata = dev_get_platdata(codec->dev);
1531         struct platform_device *pdev = container_of(codec->dev,
1532                                                    struct platform_device, dev);
1533         int ret = 0;
1534
1535         priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1536         if (priv == NULL)
1537                 return -ENOMEM;
1538         snd_soc_codec_set_drvdata(codec, priv);
1539
1540         priv->codec = codec;
1541         codec->control_data = dev_get_drvdata(codec->dev->parent);
1542
1543         if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
1544                 priv->hs_left_step = pdata->hs_left_step;
1545                 priv->hs_right_step = pdata->hs_right_step;
1546         } else {
1547                 priv->hs_left_step = 1;
1548                 priv->hs_right_step = 1;
1549         }
1550
1551         if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
1552                 priv->hf_left_step = pdata->hf_left_step;
1553                 priv->hf_right_step = pdata->hf_right_step;
1554         } else {
1555                 priv->hf_left_step = 1;
1556                 priv->hf_right_step = 1;
1557         }
1558
1559         priv->plug_irq = platform_get_irq(pdev, 0);
1560         if (priv->plug_irq < 0) {
1561                 dev_err(codec->dev, "invalid irq\n");
1562                 ret = -EINVAL;
1563                 goto work_err;
1564         }
1565
1566         priv->workqueue = alloc_workqueue("twl6040-codec", 0, 0);
1567         if (!priv->workqueue) {
1568                 ret = -ENOMEM;
1569                 goto work_err;
1570         }
1571
1572         INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
1573         INIT_DELAYED_WORK(&priv->headset.work, twl6040_pga_hs_work);
1574         INIT_DELAYED_WORK(&priv->handsfree.work, twl6040_pga_hf_work);
1575
1576         mutex_init(&priv->mutex);
1577
1578         init_completion(&priv->headset.ramp_done);
1579         init_completion(&priv->handsfree.ramp_done);
1580
1581         ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1582                                    0, "twl6040_irq_plug", codec);
1583         if (ret) {
1584                 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1585                 goto plugirq_err;
1586         }
1587
1588         twl6040_init_chip(codec);
1589
1590         /* power on device */
1591         ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1592         if (!ret)
1593                 return 0;
1594
1595         /* Error path */
1596         free_irq(priv->plug_irq, codec);
1597 plugirq_err:
1598         destroy_workqueue(priv->workqueue);
1599 work_err:
1600         kfree(priv);
1601         return ret;
1602 }
1603
1604 static int twl6040_remove(struct snd_soc_codec *codec)
1605 {
1606         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1607
1608         twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1609         free_irq(priv->plug_irq, codec);
1610         destroy_workqueue(priv->workqueue);
1611         kfree(priv);
1612
1613         return 0;
1614 }
1615
1616 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1617         .probe = twl6040_probe,
1618         .remove = twl6040_remove,
1619         .suspend = twl6040_suspend,
1620         .resume = twl6040_resume,
1621         .read = twl6040_read_reg_cache,
1622         .write = twl6040_write,
1623         .set_bias_level = twl6040_set_bias_level,
1624         .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1625         .reg_word_size = sizeof(u8),
1626         .reg_cache_default = twl6040_reg,
1627         .ignore_pmdown_time = true,
1628
1629         .controls = twl6040_snd_controls,
1630         .num_controls = ARRAY_SIZE(twl6040_snd_controls),
1631         .dapm_widgets = twl6040_dapm_widgets,
1632         .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
1633         .dapm_routes = intercon,
1634         .num_dapm_routes = ARRAY_SIZE(intercon),
1635 };
1636
1637 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1638 {
1639         return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1640                                       twl6040_dai, ARRAY_SIZE(twl6040_dai));
1641 }
1642
1643 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1644 {
1645         snd_soc_unregister_codec(&pdev->dev);
1646         return 0;
1647 }
1648
1649 static struct platform_driver twl6040_codec_driver = {
1650         .driver = {
1651                 .name = "twl6040-codec",
1652                 .owner = THIS_MODULE,
1653         },
1654         .probe = twl6040_codec_probe,
1655         .remove = __devexit_p(twl6040_codec_remove),
1656 };
1657
1658 module_platform_driver(twl6040_codec_driver);
1659
1660 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1661 MODULE_AUTHOR("Misael Lopez Cruz");
1662 MODULE_LICENSE("GPL");