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