ASoc: twl6040: enable FIR in EAR DAC
[slimlogic:omap4-android.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 #undef DEBUG
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/i2c.h>
28 #include <linux/gpio.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
31 #include <linux/wakelock.h>
32 #include <linux/i2c/twl.h>
33 #include <linux/switch.h>
34 #include <linux/mfd/twl6040-codec.h>
35 #include <linux/regulator/consumer.h>
36
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/soc-dapm.h>
42 #include <sound/initval.h>
43 #include <sound/tlv.h>
44
45 #include "twl6040.h"
46
47 #define TWL6040_RATES           SNDRV_PCM_RATE_8000_96000
48 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
49
50 #define TWL6040_OUTHS_0dB 0x00
51 #define TWL6040_OUTHS_M30dB 0x0F
52 #define TWL6040_OUTHF_0dB 0x03
53 #define TWL6040_OUTHF_M52dB 0x1D
54
55 #define TWL6040_RAMP_NONE       0
56 #define TWL6040_RAMP_UP         1
57 #define TWL6040_RAMP_DOWN       2
58
59 #define TWL6040_HSL_VOL_MASK    0x0F
60 #define TWL6040_HSL_VOL_SHIFT   0
61 #define TWL6040_HSR_VOL_MASK    0xF0
62 #define TWL6040_HSR_VOL_SHIFT   4
63 #define TWL6040_HF_VOL_MASK     0x1F
64 #define TWL6040_HF_VOL_SHIFT    0
65
66 struct twl6040_jack_data {
67         struct snd_soc_jack *jack;
68         int report;
69         struct switch_dev sdev;
70 };
71
72 /* codec private data */
73 struct twl6040_data {
74         struct twl6040_jack_data hs_jack;
75         struct wake_lock wake_lock;
76         int codec_powered;
77         int pll;
78         int non_lp;
79         int earpiece_used;
80         unsigned int clk_in;
81         unsigned int sysclk;
82         struct regulator *vddhf_reg;
83         struct snd_pcm_hw_constraint_list *sysclk_constraints;
84         struct snd_soc_codec *codec;
85         struct workqueue_struct *workqueue;
86         struct delayed_work delayed_work;
87         struct mutex mutex;
88         int hfdrv;
89         int hfl_gain;
90         int hfr_gain;
91         int hs_gain;
92         int ear_gain;
93 };
94
95 /*
96  * twl6040 register cache & default register settings
97  */
98 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
99         0x00, /* not used               0x00    */
100         0x4B, /* TWL6040_ASICID (ro)    0x01    */
101         0x00, /* TWL6040_ASICREV (ro)   0x02    */
102         0x00, /* TWL6040_INTID          0x03    */
103         0x00, /* TWL6040_INTMR          0x04    */
104         0x00, /* TWL6040_NCPCTRL        0x05    */
105         0x00, /* TWL6040_LDOCTL         0x06    */
106         0x60, /* TWL6040_HPPLLCTL       0x07    */
107         0x00, /* TWL6040_LPPLLCTL       0x08    */
108         0x4A, /* TWL6040_LPPLLDIV       0x09    */
109         0x00, /* TWL6040_AMICBCTL       0x0A    */
110         0x00, /* TWL6040_DMICBCTL       0x0B    */
111         0x18, /* TWL6040_MICLCTL        0x0C    - No input selected on Left Mic */
112         0x18, /* TWL6040_MICRCTL        0x0D    - No input selected on Right Mic */
113         0x00, /* TWL6040_MICGAIN        0x0E    */
114         0x1B, /* TWL6040_LINEGAIN       0x0F    */
115         0x00, /* TWL6040_HSLCTL         0x10    */
116         0x00, /* TWL6040_HSRCTL         0x11    */
117         0xFF, /* TWL6040_HSGAIN         0x12    */
118         0x3E, /* TWL6040_EARCTL         0x13    */
119         0x00, /* TWL6040_HFLCTL         0x14    */
120         0x1D, /* TWL6040_HFLGAIN        0x15    */
121         0x00, /* TWL6040_HFRCTL         0x16    */
122         0x1D, /* TWL6040_HFRGAIN        0x17    */
123         0x00, /* TWL6040_VIBCTLL        0x18    */
124         0x00, /* TWL6040_VIBDATL        0x19    */
125         0x00, /* TWL6040_VIBCTLR        0x1A    */
126         0x00, /* TWL6040_VIBDATR        0x1B    */
127         0x00, /* TWL6040_HKCTL1         0x1C    */
128         0x00, /* TWL6040_HKCTL2         0x1D    */
129         0x00, /* TWL6040_GPOCTL         0x1E    */
130         0x00, /* TWL6040_ALB            0x1F    */
131         0x00, /* TWL6040_DLB            0x20    */
132         0x00, /* not used               0x21    */
133         0x00, /* not used               0x22    */
134         0x00, /* not used               0x23    */
135         0x00, /* not used               0x24    */
136         0x00, /* not used               0x25    */
137         0x00, /* not used               0x26    */
138         0x00, /* not used               0x27    */
139         0x00, /* TWL6040_TRIM1          0x28    */
140         0x00, /* TWL6040_TRIM2          0x29    */
141         0x00, /* TWL6040_TRIM3          0x2A    */
142         0x00, /* TWL6040_HSOTRIM        0x2B    */
143         0x00, /* TWL6040_HFOTRIM        0x2C    */
144         0x09, /* TWL6040_ACCCTL         0x2D    */
145         0x00, /* TWL6040_STATUS (ro)    0x2E    */
146 };
147
148 /*
149  * twl6040 vio/gnd registers:
150  * registers under vio/gnd supply can be accessed
151  * before the power-up sequence, after NRESPWRON goes high
152  */
153 static const int twl6040_vio_reg[TWL6040_VIOREGNUM] = {
154         TWL6040_REG_ASICID,
155         TWL6040_REG_ASICREV,
156         TWL6040_REG_INTID,
157         TWL6040_REG_INTMR,
158         TWL6040_REG_NCPCTL,
159         TWL6040_REG_LDOCTL,
160         TWL6040_REG_AMICBCTL,
161         TWL6040_REG_DMICBCTL,
162         TWL6040_REG_HKCTL1,
163         TWL6040_REG_HKCTL2,
164         TWL6040_REG_GPOCTL,
165         TWL6040_REG_TRIM1,
166         TWL6040_REG_TRIM2,
167         TWL6040_REG_TRIM3,
168         TWL6040_REG_HSOTRIM,
169         TWL6040_REG_HFOTRIM,
170         TWL6040_REG_ACCCTL,
171         TWL6040_REG_STATUS,
172 };
173
174 /*
175  * twl6040 vdd/vss registers:
176  * registers under vdd/vss supplies can only be accessed
177  * after the power-up sequence
178  */
179 static const int twl6040_vdd_reg[TWL6040_VDDREGNUM] = {
180         TWL6040_REG_HPPLLCTL,
181         TWL6040_REG_LPPLLCTL,
182         TWL6040_REG_LPPLLDIV,
183         TWL6040_REG_MICLCTL,
184         TWL6040_REG_MICRCTL,
185         TWL6040_REG_MICGAIN,
186         TWL6040_REG_LINEGAIN,
187         TWL6040_REG_HSLCTL,
188         TWL6040_REG_HSRCTL,
189         TWL6040_REG_HSGAIN,
190         TWL6040_REG_EARCTL,
191         TWL6040_REG_HFLCTL,
192         TWL6040_REG_HFLGAIN,
193         TWL6040_REG_HFRCTL,
194         TWL6040_REG_HFRGAIN,
195         TWL6040_REG_VIBCTLL,
196         TWL6040_REG_VIBDATL,
197         TWL6040_REG_VIBCTLR,
198         TWL6040_REG_VIBDATR,
199         TWL6040_REG_ALB,
200         TWL6040_REG_DLB,
201 };
202
203 /*
204  * read twl6040 register cache
205  */
206 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
207                                                 unsigned int reg)
208 {
209         u8 *cache = codec->reg_cache;
210
211         if (reg >= TWL6040_CACHEREGNUM)
212                 return -EIO;
213
214         return cache[reg];
215 }
216
217 /*
218  * write twl6040 register cache
219  */
220 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
221                                                 u8 reg, u8 value)
222 {
223         u8 *cache = codec->reg_cache;
224
225         if (reg >= TWL6040_CACHEREGNUM)
226                 return;
227         cache[reg] = value;
228 }
229
230 /*
231  * read from twl6040 hardware register
232  */
233 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
234                         unsigned int reg)
235 {
236         struct twl6040_codec *twl6040 = codec->control_data;
237         u8 value = 0;
238
239         if (reg >= TWL6040_CACHEREGNUM)
240                 return -EIO;
241
242         value = twl6040_reg_read(twl6040, reg);
243         twl6040_write_reg_cache(codec, reg, value);
244
245         return value;
246 }
247
248 /*
249  * write to the twl6040 register space
250  */
251 static int twl6040_write(struct snd_soc_codec *codec,
252                         unsigned int reg, unsigned int value)
253 {
254         struct twl6040_codec *twl6040 = codec->control_data;
255
256         if (reg >= TWL6040_CACHEREGNUM)
257                 return -EIO;
258
259         twl6040_write_reg_cache(codec, reg, value);
260         return twl6040_reg_write(twl6040, reg, value);
261 }
262
263 static void twl6040_init_vio_regs(struct snd_soc_codec *codec)
264 {
265         u8 *cache = codec->reg_cache;
266         int reg, i;
267
268         for (i = 0; i < TWL6040_VIOREGNUM; i++) {
269                 reg = twl6040_vio_reg[i];
270                 /*
271                  * skip read-only registers (ASICID, ASICREV, STATUS)
272                  * and registers shared among MFD children
273                  */
274                 switch (reg) {
275                 case TWL6040_REG_ASICID:
276                 case TWL6040_REG_ASICREV:
277                 case TWL6040_REG_INTID:
278                 case TWL6040_REG_INTMR:
279                 case TWL6040_REG_NCPCTL:
280                 case TWL6040_REG_LDOCTL:
281                 case TWL6040_REG_GPOCTL:
282                 case TWL6040_REG_ACCCTL:
283                 case TWL6040_REG_STATUS:
284                         continue;
285                 default:
286                         break;
287                 }
288                 twl6040_write(codec, reg, cache[reg]);
289         }
290 }
291
292 static void twl6040_init_vdd_regs(struct snd_soc_codec *codec)
293 {
294         u8 *cache = codec->reg_cache;
295         int reg, i;
296
297         for (i = 0; i < TWL6040_VDDREGNUM; i++) {
298                 reg = twl6040_vdd_reg[i];
299                 /* skip vibra and pll registers */
300                 switch (reg) {
301                 case TWL6040_REG_VIBCTLL:
302                 case TWL6040_REG_VIBDATL:
303                 case TWL6040_REG_VIBCTLR:
304                 case TWL6040_REG_VIBDATR:
305                 case TWL6040_REG_HPPLLCTL:
306                 case TWL6040_REG_LPPLLCTL:
307                 case TWL6040_REG_LPPLLDIV:
308                         continue;
309                 default:
310                         break;
311                 }
312                 twl6040_write(codec, reg, cache[reg]);
313         }
314 }
315
316 /* set headset dac and driver power mode */
317 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
318 {
319         int hslctl, hsrctl;
320         int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL;
321         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
322
323         /* Earphone doesn't support low power mode */
324         high_perf |= priv->earpiece_used;
325
326         hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
327         hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
328
329         if (high_perf) {
330                 hslctl &= ~mask;
331                 hsrctl &= ~mask;
332         } else {
333                 hslctl |= mask;
334                 hsrctl |= mask;
335         }
336
337         twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
338         twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
339
340         return 0;
341 }
342
343 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
344                         struct snd_kcontrol *kcontrol, int event)
345 {
346         msleep(1);
347         return 0;
348 }
349
350 static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
351                         struct snd_kcontrol *kcontrol, int event)
352 {
353         struct snd_soc_codec *codec = w->codec;
354         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
355
356         if (SND_SOC_DAPM_EVENT_ON(event)) {
357                 priv->non_lp++;
358                 if (!strcmp(w->name,"Earphone Driver")) {
359                         /* Earphone doesn't support low power mode */
360                         priv->earpiece_used = 1;
361                         headset_power_mode(w->codec, 1);
362                 }
363         } else {
364                 priv->non_lp--;
365                 if (!strcmp(w->name,"Earphone Driver")) {
366                         priv->earpiece_used = 0;
367                 }
368         }
369
370         msleep(1);
371
372         return 0;
373 }
374
375 static int twl6040_hf_drv_event(struct snd_soc_dapm_widget *w,
376                         struct snd_kcontrol *kcontrol, int event)
377 {
378         struct snd_soc_codec *codec = w->codec;
379         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
380         int ret = 0;
381
382         if (SND_SOC_DAPM_EVENT_ON(event)) {
383                 priv->non_lp++;
384                 /* enable HF external boost after HFDRVs to reduce pop noise */
385                 if (priv->vddhf_reg && (++priv->hfdrv == 2)) {
386                         ret = regulator_enable(priv->vddhf_reg);
387                         if (ret) {
388                                 dev_err(codec->dev, "failed to enable "
389                                         "VDDHF regulator %d\n", ret);
390                                 return ret;
391                         }
392                 }
393         } else {
394                 priv->non_lp--;
395                 /* disable HF external boost before HFDRVs to reduce pop noise */
396                 if (priv->vddhf_reg && (priv->hfdrv-- == 2)) {
397                         ret = regulator_disable(priv->vddhf_reg);
398                         if (ret) {
399                                 dev_err(codec->dev, "failed to disable "
400                                         "VDDHF regulator %d\n", ret);
401                                 return ret;
402                         }
403                 }
404         }
405
406         return ret;
407 }
408
409 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
410                                 struct snd_soc_jack *jack, int report)
411 {
412         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
413         int status, state;
414
415         mutex_lock(&priv->mutex);
416
417         /* Sync status */
418         status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
419         if (status & TWL6040_PLUGCOMP)
420                 state = report;
421         else
422                 state = 0;
423
424         mutex_unlock(&priv->mutex);
425
426         snd_soc_jack_report(jack, state, report);
427         switch_set_state(&priv->hs_jack.sdev, !!state);
428 }
429
430 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
431                                 struct snd_soc_jack *jack, int report)
432 {
433         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
434         struct twl6040_jack_data *hs_jack = &priv->hs_jack;
435
436         hs_jack->jack = jack;
437         hs_jack->report = report;
438
439         twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
440 }
441 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
442
443 static void twl6040_accessory_work(struct work_struct *work)
444 {
445         struct twl6040_data *priv = container_of(work,
446                                         struct twl6040_data, delayed_work.work);
447         struct snd_soc_codec *codec = priv->codec;
448         struct twl6040_jack_data *hs_jack = &priv->hs_jack;
449
450         twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
451 }
452
453 static irqreturn_t twl6040_audio_handler(int irq, void *data)
454 {
455         struct snd_soc_codec *codec = data;
456         struct twl6040_codec *twl6040 = codec->control_data;
457         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
458         u8 intid = 0;
459
460         intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
461
462         if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT)) {
463                 wake_lock_timeout(&priv->wake_lock, 2 * HZ);
464                 queue_delayed_work(priv->workqueue, &priv->delayed_work,
465                                    msecs_to_jiffies(200));
466         }
467
468         return IRQ_HANDLED;
469 }
470 /*
471  * HSGAIN and HFGAIN values when playback is in process are stored with
472  * driver data, in order to update these elements when gain controls are
473  * used a custom volsw and volsw_2r put are added to handle these changes.
474  */
475 #define SOC_DOUBLE_TLV_TWL6040_HS(xname, xreg, shift_left, shift_right, xmax,\
476         xinvert, tlv_array) \
477 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
478         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
479                 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
480         .tlv.p = (tlv_array), \
481         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
482         .put = snd_soc_put_volsw_twl6040_hs, \
483         .private_value = (unsigned long)&(struct soc_mixer_control) \
484                 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
485                 .max = xmax, .platform_max = xmax, .invert = xinvert} }
486 #define SOC_DOUBLE_R_TLV_TWL6040_HF(xname, reg_left, reg_right, xshift, xmax, \
487         xinvert, tlv_array) \
488 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
489         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
490                 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
491         .tlv.p = (tlv_array), \
492         .info = snd_soc_info_volsw_2r, \
493         .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r_twl6040_hf, \
494         .private_value = (unsigned long)&(struct soc_mixer_control) \
495                 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
496                 .max = xmax, .platform_max = xmax, .invert = xinvert} }
497 #define SOC_SINGLE_TLV_TWL6040_EAR(xname, reg, shift, max, invert, tlv_array) \
498 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
499         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
500                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
501         .tlv.p = (tlv_array), \
502         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
503         .put = snd_soc_put_volsw_twl6040_ear, \
504         .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
505
506 static int snd_soc_put_volsw_twl6040_hs(struct snd_kcontrol *kcontrol,
507         struct snd_ctl_elem_value *ucontrol)
508 {
509         struct soc_mixer_control *mc =
510                 (struct soc_mixer_control *)kcontrol->private_value;
511         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
512         unsigned int reg = mc->reg;
513         unsigned int shift = mc->shift;
514         unsigned int rshift = mc->rshift;
515         int max = mc->max;
516         unsigned int mask = (1 << fls(max)) - 1;
517         unsigned int invert = mc->invert;
518         unsigned int val, val2, val_mask;
519         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
520
521         val = (ucontrol->value.integer.value[0] & mask);
522         if (invert)
523                 val = max - val;
524         val_mask = mask << shift;
525         val = val << shift;
526         if (shift != rshift) {
527                 val2 = (ucontrol->value.integer.value[1] & mask);
528                 if (invert)
529                         val2 = max - val2;
530                 val_mask |= mask << rshift;
531                 val |= val2 << rshift;
532         }
533         priv->hs_gain = val;
534         return snd_soc_update_bits_locked(codec, reg, val_mask, val);
535 }
536
537 static int snd_soc_put_volsw_2r_twl6040_hf(struct snd_kcontrol *kcontrol,
538         struct snd_ctl_elem_value *ucontrol)
539 {
540         struct soc_mixer_control *mc =
541                 (struct soc_mixer_control *)kcontrol->private_value;
542         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
543         unsigned int reg = mc->reg;
544         unsigned int reg2 = mc->rreg;
545         unsigned int shift = mc->shift;
546         int max = mc->max;
547         unsigned int mask = (1 << fls(max)) - 1;
548         unsigned int invert = mc->invert;
549         int err;
550         unsigned int val, val2, val_mask;
551         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
552
553         val_mask = mask << shift;
554         val = (ucontrol->value.integer.value[0] & mask);
555         val2 = (ucontrol->value.integer.value[1] & mask);
556
557         if (invert) {
558                 val = max - val;
559                 val2 = max - val2;
560         }
561
562         val = val << shift;
563         val2 = val2 << shift;
564
565         priv->hfl_gain = val;
566         priv->hfr_gain = val2;
567
568         err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
569         if (err < 0)
570                 return err;
571
572         err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
573         return err;
574 }
575
576 int snd_soc_put_volsw_twl6040_ear(struct snd_kcontrol *kcontrol,
577         struct snd_ctl_elem_value *ucontrol)
578 {
579         struct soc_mixer_control *mc =
580                 (struct soc_mixer_control *)kcontrol->private_value;
581         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
582         unsigned int reg = mc->reg;
583         unsigned int shift = mc->shift;
584         unsigned int rshift = mc->rshift;
585         int max = mc->max;
586         unsigned int mask = (1 << fls(max)) - 1;
587         unsigned int invert = mc->invert;
588         unsigned int val, val2, val_mask;
589         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
590
591         val = (ucontrol->value.integer.value[0] & mask);
592         if (invert)
593                 val = max - val;
594         val_mask = mask << shift;
595         val = val << shift;
596         if (shift != rshift) {
597                 val2 = (ucontrol->value.integer.value[1] & mask);
598                 if (invert)
599                         val2 = max - val2;
600                 val_mask |= mask << rshift;
601                 val |= val2 << rshift;
602         }
603         priv->ear_gain = val;
604         return snd_soc_update_bits_locked(codec, reg, val_mask, val);
605 }
606
607 /*
608  * MICATT volume control:
609  * from -6 to 0 dB in 6 dB steps
610  */
611 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
612
613 /*
614  * MICGAIN volume control:
615  * from -6 to 30 dB in 6 dB steps
616  */
617 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, -600, 600, 0);
618
619 /*
620  * AFMGAIN volume control:
621  * from 18 to 24 dB in 6 dB steps
622  */
623 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, 1800, 600, 0);
624
625
626 /*
627  * HSGAIN volume control:
628  * from -30 to 0 dB in 2 dB steps
629  */
630 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
631
632 /*
633  * HFGAIN volume control:
634  * from -52 to 6 dB in 2 dB steps
635  */
636 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
637
638 /*
639  * EPGAIN volume control:
640  * from -24 to 6 dB in 2 dB steps
641  */
642 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
643
644 /* Left analog microphone selection */
645 static const char *twl6040_amicl_texts[] =
646         {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
647
648 /* Right analog microphone selection */
649 static const char *twl6040_amicr_texts[] =
650         {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
651
652 static const char *twl6040_hs_texts[] =
653         {"Off", "HS DAC", "Line-In amp"};
654
655 static const char *twl6040_hf_texts[] =
656         {"Off", "HF DAC", "Line-In amp"};
657
658 static const struct soc_enum twl6040_enum[] = {
659         SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
660         SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
661         SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, 3, twl6040_hs_texts),
662         SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, 3, twl6040_hs_texts),
663         SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, 3, twl6040_hf_texts),
664         SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, 3, twl6040_hf_texts),
665 };
666
667 static const struct snd_kcontrol_new amicl_control =
668         SOC_DAPM_ENUM("Route", twl6040_enum[0]);
669
670 static const struct snd_kcontrol_new amicr_control =
671         SOC_DAPM_ENUM("Route", twl6040_enum[1]);
672
673 /* Headset DAC playback switches */
674 static const struct snd_kcontrol_new hsl_mux_controls =
675         SOC_DAPM_ENUM("Route", twl6040_enum[2]);
676
677 static const struct snd_kcontrol_new hsr_mux_controls =
678         SOC_DAPM_ENUM("Route", twl6040_enum[3]);
679
680 /* Handsfree DAC playback switches */
681 static const struct snd_kcontrol_new hfl_mux_controls =
682         SOC_DAPM_ENUM("Route", twl6040_enum[4]);
683
684 static const struct snd_kcontrol_new hfr_mux_controls =
685         SOC_DAPM_ENUM("Route", twl6040_enum[5]);
686
687 static const struct snd_kcontrol_new ep_driver_switch_controls =
688         SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0);
689
690 static const struct snd_kcontrol_new auxl_switch_controls =
691         SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
692
693 static const struct snd_kcontrol_new auxr_switch_controls =
694         SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
695
696 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
697         /* Capture gains */
698         SOC_DOUBLE_TLV("Capture Preamplifier Volume",
699                 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
700         SOC_DOUBLE_TLV("Capture Volume",
701                 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
702
703         /* AFM gains */
704         SOC_DOUBLE_TLV("Aux FM Volume",
705                 TWL6040_REG_LINEGAIN, 0, 4, 0xF, 0, afm_amp_tlv),
706
707         /* Playback gains */
708         SOC_DOUBLE_TLV_TWL6040_HS("Headset Playback Volume",
709                 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
710         SOC_DOUBLE_R_TLV_TWL6040_HF("Handsfree Playback Volume",
711                 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
712         SOC_SINGLE_TLV_TWL6040_EAR("Earphone Playback Volume",
713                 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
714 };
715
716 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
717         /* Inputs */
718         SND_SOC_DAPM_INPUT("MAINMIC"),
719         SND_SOC_DAPM_INPUT("HSMIC"),
720         SND_SOC_DAPM_INPUT("SUBMIC"),
721         SND_SOC_DAPM_INPUT("AFML"),
722         SND_SOC_DAPM_INPUT("AFMR"),
723
724         /* Outputs */
725         SND_SOC_DAPM_OUTPUT("HSOL"),
726         SND_SOC_DAPM_OUTPUT("HSOR"),
727         SND_SOC_DAPM_OUTPUT("HFL"),
728         SND_SOC_DAPM_OUTPUT("HFR"),
729         SND_SOC_DAPM_OUTPUT("EP"),
730         SND_SOC_DAPM_OUTPUT("AUXL"),
731         SND_SOC_DAPM_OUTPUT("AUXR"),
732
733         /* Analog input muxes for the capture amplifiers */
734         SND_SOC_DAPM_MUX("Analog Left Capture Route",
735                         SND_SOC_NOPM, 0, 0, &amicl_control),
736         SND_SOC_DAPM_MUX("Analog Right Capture Route",
737                         SND_SOC_NOPM, 0, 0, &amicr_control),
738
739         /* Analog capture PGAs */
740         SND_SOC_DAPM_PGA("MicAmpL",
741                         TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
742         SND_SOC_DAPM_PGA("MicAmpR",
743                         TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
744
745         /* Auxiliary FM PGAs */
746         SND_SOC_DAPM_PGA("AFMAmpL",
747                         TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
748         SND_SOC_DAPM_PGA("AFMAmpR",
749                         TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
750
751         /* ADCs */
752         SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
753                         TWL6040_REG_MICLCTL, 2, 0),
754         SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
755                         TWL6040_REG_MICRCTL, 2, 0),
756
757         /* Microphone bias */
758         SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
759                         TWL6040_REG_AMICBCTL, 0, 0),
760         SND_SOC_DAPM_MICBIAS("Main Mic Bias",
761                         TWL6040_REG_AMICBCTL, 4, 0),
762         SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
763                         TWL6040_REG_DMICBCTL, 0, 0),
764         SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
765                         TWL6040_REG_DMICBCTL, 4, 0),
766
767         /* DACs */
768         SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
769                         TWL6040_REG_HSLCTL, 0, 0,
770                         twl6040_hs_dac_event,
771                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
772         SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
773                         TWL6040_REG_HSRCTL, 0, 0,
774                         twl6040_hs_dac_event,
775                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
776         SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
777                         TWL6040_REG_HFLCTL, 0, 0,
778                         twl6040_power_mode_event,
779                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
780         SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
781                         TWL6040_REG_HFRCTL, 0, 0,
782                         twl6040_power_mode_event,
783                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
784
785         SND_SOC_DAPM_MUX("HF Left Playback",
786                         SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
787         SND_SOC_DAPM_MUX("HF Right Playback",
788                         SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
789         /* Analog playback Muxes */
790         SND_SOC_DAPM_MUX("HS Left Playback",
791                         SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
792         SND_SOC_DAPM_MUX("HS Right Playback",
793                         SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
794
795         /* Analog playback drivers */
796         SND_SOC_DAPM_DRV_E("Handsfree Left Driver",
797                         TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
798                         twl6040_hf_drv_event,
799                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
800         SND_SOC_DAPM_DRV_E("Handsfree Right Driver",
801                         TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
802                         twl6040_hf_drv_event,
803                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
804         SND_SOC_DAPM_DRV("Headset Left Driver",
805                         TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
806         SND_SOC_DAPM_DRV("Headset Right Driver",
807                         TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
808         SND_SOC_DAPM_SWITCH_E("Earphone Driver",
809                         SND_SOC_NOPM, 0, 0, &ep_driver_switch_controls,
810                         twl6040_power_mode_event,
811                         SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
812
813         /* Analog playback PGAs */
814         SND_SOC_DAPM_PGA("HFDAC Left PGA",
815                         TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
816         SND_SOC_DAPM_PGA("HFDAC Right PGA",
817                         TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
818
819 };
820
821 static const struct snd_soc_dapm_route intercon[] = {
822         /* Capture path */
823         {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
824         {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
825         {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
826
827         {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
828         {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
829         {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
830
831         {"MicAmpL", NULL, "Analog Left Capture Route"},
832         {"MicAmpR", NULL, "Analog Right Capture Route"},
833
834         {"ADC Left", NULL, "MicAmpL"},
835         {"ADC Right", NULL, "MicAmpR"},
836
837         /* AFM path */
838         {"AFMAmpL", "NULL", "AFML"},
839         {"AFMAmpR", "NULL", "AFMR"},
840
841         {"HS Left Playback", "HS DAC", "HSDAC Left"},
842         {"HS Left Playback", "Line-In amp", "AFMAmpL"},
843
844         {"HS Right Playback", "HS DAC", "HSDAC Right"},
845         {"HS Right Playback", "Line-In amp", "AFMAmpR"},
846
847         {"Headset Left Driver", "NULL", "HS Left Playback"},
848         {"Headset Right Driver", "NULL", "HS Right Playback"},
849
850         {"HSOL", NULL, "Headset Left Driver"},
851         {"HSOR", NULL, "Headset Right Driver"},
852
853         /* Earphone playback path */
854         {"Earphone Driver", "Switch", "HSDAC Left"},
855         {"EP", NULL, "Earphone Driver"},
856
857         {"HF Left Playback", "HF DAC", "HFDAC Left"},
858         {"HF Left Playback", "Line-In amp", "AFMAmpL"},
859
860         {"HF Right Playback", "HF DAC", "HFDAC Right"},
861         {"HF Right Playback", "Line-In amp", "AFMAmpR"},
862
863         {"HFDAC Left PGA", NULL, "HF Left Playback"},
864         {"HFDAC Right PGA", NULL, "HF Right Playback"},
865
866         {"Handsfree Left Driver", "Switch", "HFDAC Left PGA"},
867         {"Handsfree Right Driver", "Switch", "HFDAC Right PGA"},
868
869         {"HFL", NULL, "Handsfree Left Driver"},
870         {"HFR", NULL, "Handsfree Right Driver"},
871
872         {"AUXL", "Switch", "HFDAC Left PGA"},
873         {"AUXR", "Switch", "HFDAC Right PGA"},
874 };
875
876 static int twl6040_add_widgets(struct snd_soc_codec *codec)
877 {
878         snd_soc_dapm_new_controls(codec->dapm, twl6040_dapm_widgets,
879                                  ARRAY_SIZE(twl6040_dapm_widgets));
880
881         snd_soc_dapm_add_routes(codec->dapm, intercon, ARRAY_SIZE(intercon));
882
883         snd_soc_dapm_new_widgets(codec->dapm);
884
885         return 0;
886 }
887
888 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
889                                 enum snd_soc_bias_level level)
890 {
891         struct twl6040_codec *twl6040 = codec->control_data;
892         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
893
894         switch (level) {
895         case SND_SOC_BIAS_ON:
896                 break;
897         case SND_SOC_BIAS_PREPARE:
898                 break;
899         case SND_SOC_BIAS_STANDBY:
900                 if (priv->codec_powered)
901                         break;
902
903                 twl6040_enable(twl6040);
904                 priv->codec_powered = 1;
905
906                 /* initialize vdd/vss registers with reg_cache */
907                 twl6040_init_vdd_regs(codec);
908                 break;
909         case SND_SOC_BIAS_OFF:
910                 if (!priv->codec_powered)
911                         break;
912
913                 twl6040_disable(twl6040);
914                 priv->codec_powered = 0;
915                 break;
916         }
917
918         priv->pll = twl6040_get_pll(twl6040);
919         priv->sysclk = twl6040_get_sysclk(twl6040);
920         codec->dapm->bias_level = level;
921
922         return 0;
923 }
924
925 /* set of rates for each pll: low-power and high-performance */
926
927 static unsigned int lp_rates[] = {
928         88200,
929         96000,
930 };
931
932 static struct snd_pcm_hw_constraint_list lp_constraints = {
933         .count  = ARRAY_SIZE(lp_rates),
934         .list   = lp_rates,
935 };
936
937 static unsigned int hp_rates[] = {
938         96000,
939 };
940
941 static struct snd_pcm_hw_constraint_list hp_constraints = {
942         .count  = ARRAY_SIZE(hp_rates),
943         .list   = hp_rates,
944 };
945
946 static int twl6040_startup(struct snd_pcm_substream *substream,
947                         struct snd_soc_dai *dai)
948 {
949 // FIXME - need to create some contraints for backends
950 #if 0
951         struct snd_soc_pcm_runtime *rtd = substream->private_data;
952         struct snd_soc_codec *codec = rtd->codec;
953         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
954
955         snd_pcm_hw_constraint_list(substream->runtime, 0,
956                                 SNDRV_PCM_HW_PARAM_RATE,
957                                 priv->sysclk_constraints);
958 #endif
959         return 0;
960 }
961
962 static int twl6040_hw_params(struct snd_pcm_substream *substream,
963                         struct snd_pcm_hw_params *params,
964                         struct snd_soc_dai *dai)
965 {
966         struct snd_soc_pcm_runtime *rtd = substream->private_data;
967         struct snd_soc_codec *codec = rtd->codec;
968         struct twl6040_codec *twl6040 = codec->control_data;
969         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
970         unsigned int sysclk;
971         int rate, ret = 0;
972
973         /* nothing to do for high-perf pll, it supports only 48 kHz */
974         if (priv->pll == TWL6040_HPPLL_ID)
975                 return ret;
976
977         rate = params_rate(params);
978         switch (rate) {
979         case 11250:
980         case 22500:
981         case 44100:
982         case 88200:
983                 sysclk = 17640000;
984                 break;
985         case 8000:
986         case 16000:
987         case 32000:
988         case 48000:
989         case 96000:
990                 sysclk = 19200000;
991                 break;
992         default:
993                 dev_err(codec->dev, "unsupported rate %d\n", rate);
994                 return -EINVAL;
995         }
996
997         ret = twl6040_set_pll(twl6040, TWL6040_LPPLL_ID, priv->clk_in, sysclk);
998
999         return ret;
1000 }
1001
1002 static int twl6040_mute(struct snd_soc_dai *codec_dai, int mute)
1003 {
1004         struct snd_soc_codec *codec = codec_dai->codec;
1005         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1006         int data;
1007
1008         if (codec_dai->id == TWL6040_DL1_DAI) {
1009                 if (mute) {
1010                         /* headset */
1011                         priv->hs_gain = twl6040_read_reg_cache(codec,
1012                                                 TWL6040_REG_HSGAIN);
1013                         twl6040_write(codec, TWL6040_REG_HSGAIN, 0xFF);
1014                         /* earpiece */
1015                         data = twl6040_read_reg_volatile(codec,
1016                                                 TWL6040_REG_EARCTL);
1017                         priv->ear_gain = (data >> 1) & 0x0F;
1018                         data &= 0x21;
1019                         twl6040_write(codec, TWL6040_REG_EARCTL, data | 0x1E);
1020                 } else {
1021                         /* headset */
1022                         twl6040_write(codec, TWL6040_REG_HSGAIN, priv->hs_gain);
1023                         /* earpiece */
1024                         data = twl6040_read_reg_volatile(codec,
1025                                                 TWL6040_REG_EARCTL) & 0x21;
1026                         twl6040_write(codec, TWL6040_REG_EARCTL,
1027                                       data | (priv->ear_gain << 1));
1028                 }
1029         } else if (codec_dai->id == TWL6040_DL2_DAI) {
1030                 if (mute) {
1031                         priv->hfl_gain = twl6040_read_reg_cache(codec,
1032                                                 TWL6040_REG_HFLGAIN);
1033                         priv->hfr_gain = twl6040_read_reg_cache(codec,
1034                                                 TWL6040_REG_HFRGAIN);
1035                         twl6040_write(codec, TWL6040_REG_HFLGAIN, 0x1D);
1036                         twl6040_write(codec, TWL6040_REG_HFRGAIN, 0x1D);
1037                 } else {
1038                         twl6040_write(codec, TWL6040_REG_HFLGAIN, priv->hfl_gain);
1039                         twl6040_write(codec, TWL6040_REG_HFRGAIN, priv->hfr_gain);
1040                 }
1041         }
1042
1043         return 0;
1044 }
1045
1046 static int twl6040_prepare(struct snd_pcm_substream *substream,
1047                         struct snd_soc_dai *dai)
1048 {
1049         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1050         struct snd_soc_codec *codec = rtd->codec;
1051         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1052
1053         if (!priv->sysclk) {
1054                 dev_err(codec->dev,
1055                         "no mclk configured, call set_sysclk() on init\n");
1056                 return -EINVAL;
1057         }
1058
1059         /*
1060          * capture is not supported at 17.64 MHz,
1061          * it's reserved for headset low-power playback scenario
1062          */
1063         if ((priv->sysclk == 17640000) &&
1064                         substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1065                 dev_err(codec->dev,
1066                         "capture mode is not supported at %dHz\n",
1067                         priv->sysclk);
1068                 return -EINVAL;
1069         }
1070
1071         if ((priv->sysclk == 17640000) && priv->non_lp) {
1072                         dev_err(codec->dev,
1073                                 "some enabled paths aren't supported at %dHz\n",
1074                                 priv->sysclk);
1075                         return -EPERM;
1076         }
1077         return 0;
1078 }
1079
1080 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1081                 int clk_id, unsigned int freq, int dir)
1082 {
1083         struct snd_soc_codec *codec = codec_dai->codec;
1084         struct twl6040_codec *twl6040 = codec->control_data;
1085         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1086         int ret = 0;
1087
1088         switch (clk_id) {
1089         case TWL6040_SYSCLK_SEL_LPPLL:
1090                 ret = twl6040_set_pll(twl6040, TWL6040_LPPLL_ID,
1091                                       freq, priv->sysclk);
1092                 if (ret)
1093                         return ret;
1094
1095                 /* headset dac and driver must be in low-power mode */
1096                 headset_power_mode(codec, 0);
1097
1098                 priv->sysclk_constraints = &lp_constraints;
1099                 break;
1100         case TWL6040_SYSCLK_SEL_HPPLL:
1101                 ret = twl6040_set_pll(twl6040, TWL6040_HPPLL_ID, freq,
1102                                       priv->sysclk);
1103                 if (ret)
1104                         return ret;
1105
1106                 /* headset dac and driver must be in high-performance mode */
1107                 headset_power_mode(codec, 1);
1108
1109                 priv->sysclk_constraints = &hp_constraints;
1110                 break;
1111         default:
1112                 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1113                 return -EINVAL;
1114         }
1115
1116         priv->pll = twl6040_get_pll(twl6040);
1117         priv->clk_in = freq;
1118         priv->sysclk = twl6040_get_sysclk(twl6040);
1119
1120         return 0;
1121 }
1122
1123 static struct snd_soc_dai_ops twl6040_dai_ops = {
1124         .startup        = twl6040_startup,
1125         .hw_params      = twl6040_hw_params,
1126         .digital_mute   = twl6040_mute,
1127         .prepare        = twl6040_prepare,
1128         .set_sysclk     = twl6040_set_dai_sysclk,
1129 };
1130
1131 static struct snd_soc_dai_driver twl6040_dai[] = {
1132 {
1133         .name = "twl6040-ul",
1134         .capture = {
1135                 .stream_name = "Capture",
1136                 .channels_min = 1,
1137                 .channels_max = 2,
1138                 .rates = TWL6040_RATES,
1139                 .formats = TWL6040_FORMATS,
1140         },
1141         .ops = &twl6040_dai_ops,
1142 },
1143 {
1144         .name = "twl6040-dl1",
1145         .playback = {
1146                 .stream_name = "Headset Playback",
1147                 .channels_min = 1,
1148                 .channels_max = 2,
1149                 .rates = TWL6040_RATES,
1150                 .formats = TWL6040_FORMATS,
1151         },
1152         .ops = &twl6040_dai_ops,
1153 },
1154 {
1155         .name = "twl6040-dl2",
1156         .playback = {
1157                 .stream_name = "Handsfree Playback",
1158                 .channels_min = 1,
1159                 .channels_max = 2,
1160                 .rates = TWL6040_RATES,
1161                 .formats = TWL6040_FORMATS,
1162         },
1163         .ops = &twl6040_dai_ops,
1164 },
1165 {
1166         .name = "twl6040-vib",
1167         .playback = {
1168                 .stream_name = "Vibra Playback",
1169                 .channels_min = 2,
1170                 .channels_max = 2,
1171                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1172                 .formats = TWL6040_FORMATS,
1173         },
1174         .ops = &twl6040_dai_ops,
1175 },
1176 };
1177
1178 #ifdef CONFIG_PM
1179 static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1180 {
1181         twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1182
1183         return 0;
1184 }
1185
1186 static int twl6040_resume(struct snd_soc_codec *codec)
1187 {
1188         /* TODO: read HS jack insertion status */
1189         twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1190         twl6040_set_bias_level(codec, codec->dapm->suspend_bias_level);
1191
1192         return 0;
1193 }
1194 #else
1195 #define twl6040_suspend NULL
1196 #define twl6040_resume NULL
1197 #endif
1198
1199 static int twl6040_probe(struct snd_soc_codec *codec)
1200 {
1201         struct twl4030_codec_audio_data *pdata = dev_get_platdata(codec->dev);
1202         struct twl6040_data *priv;
1203         struct twl6040_jack_data *jack;
1204         int ret = 0;
1205
1206         codec->control_data = dev_get_drvdata(codec->dev->parent);
1207
1208         priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1209         if (priv == NULL)
1210                 return -ENOMEM;
1211
1212         snd_soc_codec_set_drvdata(codec, priv);
1213         priv->codec = codec;
1214         priv->workqueue = create_singlethread_workqueue("twl6040-codec");
1215         if (!priv->workqueue)
1216                 goto work_err;
1217
1218         priv->hfl_gain = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
1219         priv->hfr_gain = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
1220         priv->hs_gain = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
1221         priv->ear_gain = twl6040_read_reg_cache(codec, TWL6040_REG_EARCTL) << 1;
1222
1223         INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1224         mutex_init(&priv->mutex);
1225
1226         priv->vddhf_reg = regulator_get(codec->dev, "vddhf");
1227         if (IS_ERR(priv->vddhf_reg)) {
1228                 ret = PTR_ERR(priv->vddhf_reg);
1229                 dev_warn(codec->dev, "couldn't get VDDHF regulator %d\n",
1230                          ret);
1231                 priv->vddhf_reg = NULL;
1232         }
1233
1234         if (priv->vddhf_reg) {
1235                 ret = regulator_set_voltage(priv->vddhf_reg,
1236                                             pdata->vddhf_uV, pdata->vddhf_uV);
1237                 if (ret) {
1238                         dev_warn(codec->dev, "failed to set VDDHF voltage %d\n",
1239                                  ret);
1240                         goto reg_err;
1241                 }
1242         }
1243
1244         /* switch-class based headset detection */
1245         jack = &priv->hs_jack;
1246         jack->sdev.name = "h2w";
1247         ret = switch_dev_register(&jack->sdev);
1248         if (ret) {
1249                 dev_err(codec->dev, "error registering switch device %d\n", ret);
1250                 goto reg_err;
1251         }
1252
1253         wake_lock_init(&priv->wake_lock, WAKE_LOCK_SUSPEND, "twl6040");
1254
1255         ret = twl6040_request_irq(codec->control_data, TWL6040_IRQ_PLUG,
1256                                   twl6040_audio_handler, IRQF_NO_SUSPEND,
1257                                   "twl6040_irq_plug", codec);
1258         if (ret) {
1259                 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1260                 goto irq_err;
1261         }
1262
1263         /* init vio registers */
1264         twl6040_init_vio_regs(codec);
1265
1266         /* power on device */
1267         ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1268         if (ret)
1269                 goto bias_err;
1270
1271         snd_soc_add_controls(codec, twl6040_snd_controls,
1272                                 ARRAY_SIZE(twl6040_snd_controls));
1273         twl6040_add_widgets(codec);
1274
1275         /* TODO: read HS jack insertion status */
1276
1277         return 0;
1278
1279 bias_err:
1280         twl6040_free_irq(codec->control_data, TWL6040_IRQ_PLUG, codec);
1281 irq_err:
1282         wake_lock_destroy(&priv->wake_lock);
1283         switch_dev_unregister(&jack->sdev);
1284 reg_err:
1285         if (priv->vddhf_reg)
1286                 regulator_put(priv->vddhf_reg);
1287         destroy_workqueue(priv->workqueue);
1288 work_err:
1289         kfree(priv);
1290         return ret;
1291 }
1292
1293 static int twl6040_remove(struct snd_soc_codec *codec)
1294 {
1295         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1296         struct twl6040_jack_data *jack = &priv->hs_jack;
1297
1298         twl6040_free_irq(codec->control_data, TWL6040_IRQ_PLUG, codec);
1299         twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1300         if (priv->vddhf_reg)
1301                 regulator_put(priv->vddhf_reg);
1302         wake_lock_destroy(&priv->wake_lock);
1303         switch_dev_unregister(&jack->sdev);
1304         destroy_workqueue(priv->workqueue);
1305         kfree(priv);
1306
1307         return 0;
1308 }
1309
1310 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1311         .probe = twl6040_probe,
1312         .remove = twl6040_remove,
1313         .suspend = twl6040_suspend,
1314         .resume = twl6040_resume,
1315         .read = twl6040_read_reg_cache,
1316         .write = twl6040_write,
1317         .set_bias_level = twl6040_set_bias_level,
1318         .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1319         .reg_word_size = sizeof(u8),
1320         .reg_cache_default = twl6040_reg,
1321 };
1322
1323 static int __devinit twl6040_codec_probe(struct platform_device *pdev)
1324 {
1325         struct twl4030_codec_audio_data *pdata = pdev->dev.platform_data;
1326
1327         if (!pdata) {
1328                 dev_err(&pdev->dev, "platform_data is missing\n");
1329                 return -EINVAL;
1330         }
1331
1332         return snd_soc_register_codec(&pdev->dev,
1333                         &soc_codec_dev_twl6040, twl6040_dai, ARRAY_SIZE(twl6040_dai));
1334 }
1335
1336 static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1337 {
1338         snd_soc_unregister_codec(&pdev->dev);
1339         return 0;
1340 }
1341
1342 static struct platform_driver twl6040_codec_driver = {
1343         .driver = {
1344                 .name = "twl6040-codec",
1345                 .owner = THIS_MODULE,
1346         },
1347         .probe = twl6040_codec_probe,
1348         .remove = __devexit_p(twl6040_codec_remove),
1349 };
1350
1351 static int __init twl6040_codec_init(void)
1352 {
1353         return platform_driver_register(&twl6040_codec_driver);
1354 }
1355 module_init(twl6040_codec_init);
1356
1357 static void __exit twl6040_codec_exit(void)
1358 {
1359         platform_driver_unregister(&twl6040_codec_driver);
1360 }
1361 module_exit(twl6040_codec_exit);
1362
1363 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1364 MODULE_AUTHOR("Misael Lopez Cruz");
1365 MODULE_LICENSE("GPL");