Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / sound / soc / codecs / twl6040.c
1 /*
2  * ALSA SoC TWL6040 codec driver
3  *
4  * Author:       Misael Lopez Cruz <x0052729@ti.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  *
20  */
21
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/pm.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/mfd/twl6040.h>
30
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/soc-dapm.h>
36 #include <sound/initval.h>
37 #include <sound/tlv.h>
38
39 #include "twl6040.h"
40
41 enum twl6040_dai_id {
42         TWL6040_DAI_LEGACY = 0,
43         TWL6040_DAI_UL,
44         TWL6040_DAI_DL1,
45         TWL6040_DAI_DL2,
46         TWL6040_DAI_VIB,
47 };
48
49 #define TWL6040_RATES           SNDRV_PCM_RATE_8000_96000
50 #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
51
52 #define TWL6040_OUTHS_0dB 0x00
53 #define TWL6040_OUTHS_M30dB 0x0F
54 #define TWL6040_OUTHF_0dB 0x03
55 #define TWL6040_OUTHF_M52dB 0x1D
56
57 #define TWL6040_CACHEREGNUM     (TWL6040_REG_STATUS + 1)
58
59 struct twl6040_jack_data {
60         struct snd_soc_jack *jack;
61         struct delayed_work work;
62         int report;
63 };
64
65 /* codec private data */
66 struct twl6040_data {
67         int plug_irq;
68         int codec_powered;
69         int pll;
70         int pll_power_mode;
71         int hs_power_mode;
72         int hs_power_mode_locked;
73         bool dl1_unmuted;
74         bool dl2_unmuted;
75         unsigned int clk_in;
76         unsigned int sysclk;
77         struct twl6040_jack_data hs_jack;
78         struct snd_soc_codec *codec;
79         struct mutex mutex;
80 };
81
82 /*
83  * twl6040 register cache & default register settings
84  */
85 static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
86         0x00, /* not used       0x00    */
87         0x4B, /* REG_ASICID     0x01 (ro) */
88         0x00, /* REG_ASICREV    0x02 (ro) */
89         0x00, /* REG_INTID      0x03    */
90         0x00, /* REG_INTMR      0x04    */
91         0x00, /* REG_NCPCTRL    0x05    */
92         0x00, /* REG_LDOCTL     0x06    */
93         0x60, /* REG_HPPLLCTL   0x07    */
94         0x00, /* REG_LPPLLCTL   0x08    */
95         0x4A, /* REG_LPPLLDIV   0x09    */
96         0x00, /* REG_AMICBCTL   0x0A    */
97         0x00, /* REG_DMICBCTL   0x0B    */
98         0x00, /* REG_MICLCTL    0x0C    */
99         0x00, /* REG_MICRCTL    0x0D    */
100         0x00, /* REG_MICGAIN    0x0E    */
101         0x1B, /* REG_LINEGAIN   0x0F    */
102         0x00, /* REG_HSLCTL     0x10    */
103         0x00, /* REG_HSRCTL     0x11    */
104         0x00, /* REG_HSGAIN     0x12    */
105         0x00, /* REG_EARCTL     0x13    */
106         0x00, /* REG_HFLCTL     0x14    */
107         0x00, /* REG_HFLGAIN    0x15    */
108         0x00, /* REG_HFRCTL     0x16    */
109         0x00, /* REG_HFRGAIN    0x17    */
110         0x00, /* REG_VIBCTLL    0x18    */
111         0x00, /* REG_VIBDATL    0x19    */
112         0x00, /* REG_VIBCTLR    0x1A    */
113         0x00, /* REG_VIBDATR    0x1B    */
114         0x00, /* REG_HKCTL1     0x1C    */
115         0x00, /* REG_HKCTL2     0x1D    */
116         0x00, /* REG_GPOCTL     0x1E    */
117         0x00, /* REG_ALB        0x1F    */
118         0x00, /* REG_DLB        0x20    */
119         0x00, /* not used       0x21    */
120         0x00, /* not used       0x22    */
121         0x00, /* not used       0x23    */
122         0x00, /* not used       0x24    */
123         0x00, /* not used       0x25    */
124         0x00, /* not used       0x26    */
125         0x00, /* not used       0x27    */
126         0x00, /* REG_TRIM1      0x28    */
127         0x00, /* REG_TRIM2      0x29    */
128         0x00, /* REG_TRIM3      0x2A    */
129         0x00, /* REG_HSOTRIM    0x2B    */
130         0x00, /* REG_HFOTRIM    0x2C    */
131         0x09, /* REG_ACCCTL     0x2D    */
132         0x00, /* REG_STATUS     0x2E (ro) */
133 };
134
135 /* List of registers to be restored after power up */
136 static const int twl6040_restore_list[] = {
137         TWL6040_REG_MICLCTL,
138         TWL6040_REG_MICRCTL,
139         TWL6040_REG_MICGAIN,
140         TWL6040_REG_LINEGAIN,
141         TWL6040_REG_HSLCTL,
142         TWL6040_REG_HSRCTL,
143         TWL6040_REG_HSGAIN,
144         TWL6040_REG_EARCTL,
145         TWL6040_REG_HFLCTL,
146         TWL6040_REG_HFLGAIN,
147         TWL6040_REG_HFRCTL,
148         TWL6040_REG_HFRGAIN,
149 };
150
151 /* set of rates for each pll: low-power and high-performance */
152 static unsigned int lp_rates[] = {
153         8000,
154         11250,
155         16000,
156         22500,
157         32000,
158         44100,
159         48000,
160         88200,
161         96000,
162 };
163
164 static unsigned int hp_rates[] = {
165         8000,
166         16000,
167         32000,
168         48000,
169         96000,
170 };
171
172 static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
173         { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
174         { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
175 };
176
177 /*
178  * read twl6040 register cache
179  */
180 static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
181                                                 unsigned int reg)
182 {
183         u8 *cache = codec->reg_cache;
184
185         if (reg >= TWL6040_CACHEREGNUM)
186                 return -EIO;
187
188         return cache[reg];
189 }
190
191 /*
192  * write twl6040 register cache
193  */
194 static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
195                                                 u8 reg, u8 value)
196 {
197         u8 *cache = codec->reg_cache;
198
199         if (reg >= TWL6040_CACHEREGNUM)
200                 return;
201         cache[reg] = value;
202 }
203
204 /*
205  * read from twl6040 hardware register
206  */
207 static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
208                         unsigned int reg)
209 {
210         struct twl6040 *twl6040 = codec->control_data;
211         u8 value;
212
213         if (reg >= TWL6040_CACHEREGNUM)
214                 return -EIO;
215
216         value = twl6040_reg_read(twl6040, reg);
217         twl6040_write_reg_cache(codec, reg, value);
218
219         return value;
220 }
221
222 static bool twl6040_is_path_unmuted(struct snd_soc_codec *codec,
223                                     unsigned int reg)
224 {
225         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
226
227         switch (reg) {
228         case TWL6040_REG_HSLCTL:
229         case TWL6040_REG_HSRCTL:
230         case TWL6040_REG_EARCTL:
231                 /* DL1 path */
232                 return priv->dl1_unmuted;
233         case TWL6040_REG_HFLCTL:
234         case TWL6040_REG_HFRCTL:
235                 return priv->dl2_unmuted;
236         default:
237                 return 1;
238         }
239 }
240
241 /*
242  * write to the twl6040 register space
243  */
244 static int twl6040_write(struct snd_soc_codec *codec,
245                         unsigned int reg, unsigned int value)
246 {
247         struct twl6040 *twl6040 = codec->control_data;
248
249         if (reg >= TWL6040_CACHEREGNUM)
250                 return -EIO;
251
252         twl6040_write_reg_cache(codec, reg, value);
253         if (twl6040_is_path_unmuted(codec, reg))
254                 return twl6040_reg_write(twl6040, reg, value);
255         else
256                 return 0;
257 }
258
259 static void twl6040_init_chip(struct snd_soc_codec *codec)
260 {
261         struct twl6040 *twl6040 = codec->control_data;
262         u8 val;
263
264         /* Update reg_cache: ASICREV, and TRIM values */
265         val = twl6040_get_revid(twl6040);
266         twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
267
268         twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1);
269         twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2);
270         twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3);
271         twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM);
272         twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM);
273
274         /* Change chip defaults */
275         /* No imput selected for microphone amplifiers */
276         twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
277         twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
278
279         /*
280          * We need to lower the default gain values, so the ramp code
281          * can work correctly for the first playback.
282          * This reduces the pop noise heard at the first playback.
283          */
284         twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
285         twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
286         twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
287         twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
288         twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
289 }
290
291 static void twl6040_restore_regs(struct snd_soc_codec *codec)
292 {
293         u8 *cache = codec->reg_cache;
294         int reg, i;
295
296         for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
297                 reg = twl6040_restore_list[i];
298                 twl6040_write(codec, reg, cache[reg]);
299         }
300 }
301
302 /* set headset dac and driver power mode */
303 static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
304 {
305         int hslctl, hsrctl;
306         int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
307
308         hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
309         hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
310
311         if (high_perf) {
312                 hslctl &= ~mask;
313                 hsrctl &= ~mask;
314         } else {
315                 hslctl |= mask;
316                 hsrctl |= mask;
317         }
318
319         twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
320         twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
321
322         return 0;
323 }
324
325 static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
326                         struct snd_kcontrol *kcontrol, int event)
327 {
328         struct snd_soc_codec *codec = w->codec;
329         u8 hslctl, hsrctl;
330
331         /*
332          * Workaround for Headset DC offset caused pop noise:
333          * Both HS DAC need to be turned on (before the HS driver) and off at
334          * the same time.
335          */
336         hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
337         hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
338         if (SND_SOC_DAPM_EVENT_ON(event)) {
339                 hslctl |= TWL6040_HSDACENA;
340                 hsrctl |= TWL6040_HSDACENA;
341         } else {
342                 hslctl &= ~TWL6040_HSDACENA;
343                 hsrctl &= ~TWL6040_HSDACENA;
344         }
345         twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
346         twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
347
348         msleep(1);
349         return 0;
350 }
351
352 static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w,
353                         struct snd_kcontrol *kcontrol, int event)
354 {
355         struct snd_soc_codec *codec = w->codec;
356         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
357         int ret = 0;
358
359         if (SND_SOC_DAPM_EVENT_ON(event)) {
360                 /* Earphone doesn't support low power mode */
361                 priv->hs_power_mode_locked = 1;
362                 ret = headset_power_mode(codec, 1);
363         } else {
364                 priv->hs_power_mode_locked = 0;
365                 ret = headset_power_mode(codec, priv->hs_power_mode);
366         }
367
368         msleep(1);
369
370         return ret;
371 }
372
373 static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
374                                    struct snd_soc_jack *jack, int report)
375 {
376         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
377         int status;
378
379         mutex_lock(&priv->mutex);
380
381         /* Sync status */
382         status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
383         if (status & TWL6040_PLUGCOMP)
384                 snd_soc_jack_report(jack, report, report);
385         else
386                 snd_soc_jack_report(jack, 0, report);
387
388         mutex_unlock(&priv->mutex);
389 }
390
391 void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
392                                 struct snd_soc_jack *jack, int report)
393 {
394         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
395         struct twl6040_jack_data *hs_jack = &priv->hs_jack;
396
397         hs_jack->jack = jack;
398         hs_jack->report = report;
399
400         twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
401 }
402 EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
403
404 static void twl6040_accessory_work(struct work_struct *work)
405 {
406         struct twl6040_data *priv = container_of(work,
407                                         struct twl6040_data, hs_jack.work.work);
408         struct snd_soc_codec *codec = priv->codec;
409         struct twl6040_jack_data *hs_jack = &priv->hs_jack;
410
411         twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
412 }
413
414 /* audio interrupt handler */
415 static irqreturn_t twl6040_audio_handler(int irq, void *data)
416 {
417         struct snd_soc_codec *codec = data;
418         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
419
420         queue_delayed_work(system_power_efficient_wq,
421                            &priv->hs_jack.work, msecs_to_jiffies(200));
422
423         return IRQ_HANDLED;
424 }
425
426 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
427         struct snd_ctl_elem_value *ucontrol)
428 {
429         struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
430         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
431         unsigned int val;
432
433         /* Do not allow changes while Input/FF efect is running */
434         val = twl6040_read_reg_volatile(codec, e->reg);
435         if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
436                 return -EBUSY;
437
438         return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
439 }
440
441 /*
442  * MICATT volume control:
443  * from -6 to 0 dB in 6 dB steps
444  */
445 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
446
447 /*
448  * MICGAIN volume control:
449  * from 6 to 30 dB in 6 dB steps
450  */
451 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
452
453 /*
454  * AFMGAIN volume control:
455  * from -18 to 24 dB in 6 dB steps
456  */
457 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
458
459 /*
460  * HSGAIN volume control:
461  * from -30 to 0 dB in 2 dB steps
462  */
463 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
464
465 /*
466  * HFGAIN volume control:
467  * from -52 to 6 dB in 2 dB steps
468  */
469 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
470
471 /*
472  * EPGAIN volume control:
473  * from -24 to 6 dB in 2 dB steps
474  */
475 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
476
477 /* Left analog microphone selection */
478 static const char *twl6040_amicl_texts[] =
479         {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
480
481 /* Right analog microphone selection */
482 static const char *twl6040_amicr_texts[] =
483         {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
484
485 static const struct soc_enum twl6040_enum[] = {
486         SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
487         SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
488 };
489
490 static const char *twl6040_hs_texts[] = {
491         "Off", "HS DAC", "Line-In amp"
492 };
493
494 static const struct soc_enum twl6040_hs_enum[] = {
495         SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
496                         twl6040_hs_texts),
497         SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
498                         twl6040_hs_texts),
499 };
500
501 static const char *twl6040_hf_texts[] = {
502         "Off", "HF DAC", "Line-In amp"
503 };
504
505 static const struct soc_enum twl6040_hf_enum[] = {
506         SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
507                         twl6040_hf_texts),
508         SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
509                         twl6040_hf_texts),
510 };
511
512 static const char *twl6040_vibrapath_texts[] = {
513         "Input FF", "Audio PDM"
514 };
515
516 static const struct soc_enum twl6040_vibra_enum[] = {
517         SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
518                         ARRAY_SIZE(twl6040_vibrapath_texts),
519                         twl6040_vibrapath_texts),
520         SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
521                         ARRAY_SIZE(twl6040_vibrapath_texts),
522                         twl6040_vibrapath_texts),
523 };
524
525 static const struct snd_kcontrol_new amicl_control =
526         SOC_DAPM_ENUM("Route", twl6040_enum[0]);
527
528 static const struct snd_kcontrol_new amicr_control =
529         SOC_DAPM_ENUM("Route", twl6040_enum[1]);
530
531 /* Headset DAC playback switches */
532 static const struct snd_kcontrol_new hsl_mux_controls =
533         SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
534
535 static const struct snd_kcontrol_new hsr_mux_controls =
536         SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
537
538 /* Handsfree DAC playback switches */
539 static const struct snd_kcontrol_new hfl_mux_controls =
540         SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
541
542 static const struct snd_kcontrol_new hfr_mux_controls =
543         SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
544
545 static const struct snd_kcontrol_new ep_path_enable_control =
546         SOC_DAPM_SINGLE_VIRT("Switch", 1);
547
548 static const struct snd_kcontrol_new auxl_switch_control =
549         SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
550
551 static const struct snd_kcontrol_new auxr_switch_control =
552         SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
553
554 /* Vibra playback switches */
555 static const struct snd_kcontrol_new vibral_mux_controls =
556         SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
557                 snd_soc_dapm_get_enum_double,
558                 twl6040_soc_dapm_put_vibra_enum);
559
560 static const struct snd_kcontrol_new vibrar_mux_controls =
561         SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
562                 snd_soc_dapm_get_enum_double,
563                 twl6040_soc_dapm_put_vibra_enum);
564
565 /* Headset power mode */
566 static const char *twl6040_power_mode_texts[] = {
567         "Low-Power", "High-Performance",
568 };
569
570 static const struct soc_enum twl6040_power_mode_enum =
571         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
572                         twl6040_power_mode_texts);
573
574 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
575         struct snd_ctl_elem_value *ucontrol)
576 {
577         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
578         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
579
580         ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
581
582         return 0;
583 }
584
585 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
586         struct snd_ctl_elem_value *ucontrol)
587 {
588         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
589         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
590         int high_perf = ucontrol->value.enumerated.item[0];
591         int ret = 0;
592
593         if (!priv->hs_power_mode_locked)
594                 ret = headset_power_mode(codec, high_perf);
595
596         if (!ret)
597                 priv->hs_power_mode = high_perf;
598
599         return ret;
600 }
601
602 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
603         struct snd_ctl_elem_value *ucontrol)
604 {
605         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
606         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
607
608         ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
609
610         return 0;
611 }
612
613 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
614         struct snd_ctl_elem_value *ucontrol)
615 {
616         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
617         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
618
619         priv->pll_power_mode = ucontrol->value.enumerated.item[0];
620
621         return 0;
622 }
623
624 int twl6040_get_dl1_gain(struct snd_soc_codec *codec)
625 {
626         struct snd_soc_dapm_context *dapm = &codec->dapm;
627
628         if (snd_soc_dapm_get_pin_status(dapm, "EP"))
629                 return -1; /* -1dB */
630
631         if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
632                 snd_soc_dapm_get_pin_status(dapm, "HSOL")) {
633
634                 u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL);
635                 if (val & TWL6040_HSDACMODE)
636                         /* HSDACL in LP mode */
637                         return -8; /* -8dB */
638                 else
639                         /* HSDACL in HP mode */
640                         return -1; /* -1dB */
641         }
642         return 0; /* 0dB */
643 }
644 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);
645
646 int twl6040_get_clk_id(struct snd_soc_codec *codec)
647 {
648         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
649
650         return priv->pll_power_mode;
651 }
652 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
653
654 int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
655 {
656         if (unlikely(trim >= TWL6040_TRIM_INVAL))
657                 return -EINVAL;
658
659         return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim);
660 }
661 EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
662
663 int twl6040_get_hs_step_size(struct snd_soc_codec *codec)
664 {
665         struct twl6040 *twl6040 = codec->control_data;
666
667         if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3)
668                 /* For ES under ES_1.3 HS step is 2 mV */
669                 return 2;
670         else
671                 /* For ES_1.3 HS step is 1 mV */
672                 return 1;
673 }
674 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);
675
676 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
677         /* Capture gains */
678         SOC_DOUBLE_TLV("Capture Preamplifier Volume",
679                 TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
680         SOC_DOUBLE_TLV("Capture Volume",
681                 TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
682
683         /* AFM gains */
684         SOC_DOUBLE_TLV("Aux FM Volume",
685                 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
686
687         /* Playback gains */
688         SOC_DOUBLE_TLV("Headset Playback Volume",
689                 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
690         SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
691                 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
692         SOC_SINGLE_TLV("Earphone Playback Volume",
693                 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
694
695         SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
696                 twl6040_headset_power_get_enum,
697                 twl6040_headset_power_put_enum),
698
699         SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
700                 twl6040_pll_get_enum, twl6040_pll_put_enum),
701 };
702
703 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
704         /* Inputs */
705         SND_SOC_DAPM_INPUT("MAINMIC"),
706         SND_SOC_DAPM_INPUT("HSMIC"),
707         SND_SOC_DAPM_INPUT("SUBMIC"),
708         SND_SOC_DAPM_INPUT("AFML"),
709         SND_SOC_DAPM_INPUT("AFMR"),
710
711         /* Outputs */
712         SND_SOC_DAPM_OUTPUT("HSOL"),
713         SND_SOC_DAPM_OUTPUT("HSOR"),
714         SND_SOC_DAPM_OUTPUT("HFL"),
715         SND_SOC_DAPM_OUTPUT("HFR"),
716         SND_SOC_DAPM_OUTPUT("EP"),
717         SND_SOC_DAPM_OUTPUT("AUXL"),
718         SND_SOC_DAPM_OUTPUT("AUXR"),
719         SND_SOC_DAPM_OUTPUT("VIBRAL"),
720         SND_SOC_DAPM_OUTPUT("VIBRAR"),
721
722         /* Analog input muxes for the capture amplifiers */
723         SND_SOC_DAPM_MUX("Analog Left Capture Route",
724                         SND_SOC_NOPM, 0, 0, &amicl_control),
725         SND_SOC_DAPM_MUX("Analog Right Capture Route",
726                         SND_SOC_NOPM, 0, 0, &amicr_control),
727
728         /* Analog capture PGAs */
729         SND_SOC_DAPM_PGA("MicAmpL",
730                         TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
731         SND_SOC_DAPM_PGA("MicAmpR",
732                         TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
733
734         /* Auxiliary FM PGAs */
735         SND_SOC_DAPM_PGA("AFMAmpL",
736                         TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
737         SND_SOC_DAPM_PGA("AFMAmpR",
738                         TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
739
740         /* ADCs */
741         SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0),
742         SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0),
743
744         /* Microphone bias */
745         SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
746                             TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
747         SND_SOC_DAPM_SUPPLY("Main Mic Bias",
748                             TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
749         SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
750                             TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
751         SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
752                             TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
753
754         /* DACs */
755         SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0),
756         SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0),
757         SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0),
758         SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0),
759         /* Virtual DAC for vibra path (DL4 channel) */
760         SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0),
761
762         SND_SOC_DAPM_MUX("Handsfree Left Playback",
763                         SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
764         SND_SOC_DAPM_MUX("Handsfree Right Playback",
765                         SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
766         /* Analog playback Muxes */
767         SND_SOC_DAPM_MUX("Headset Left Playback",
768                         SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
769         SND_SOC_DAPM_MUX("Headset Right Playback",
770                         SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
771
772         SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
773                         &vibral_mux_controls),
774         SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
775                         &vibrar_mux_controls),
776
777         SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
778                         &ep_path_enable_control),
779         SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
780                         &auxl_switch_control),
781         SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
782                         &auxr_switch_control),
783
784         /* Analog playback drivers */
785         SND_SOC_DAPM_OUT_DRV("HF Left Driver",
786                         TWL6040_REG_HFLCTL, 4, 0, NULL, 0),
787         SND_SOC_DAPM_OUT_DRV("HF Right Driver",
788                         TWL6040_REG_HFRCTL, 4, 0, NULL, 0),
789         SND_SOC_DAPM_OUT_DRV("HS Left Driver",
790                         TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
791         SND_SOC_DAPM_OUT_DRV("HS Right Driver",
792                         TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
793         SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
794                         TWL6040_REG_EARCTL, 0, 0, NULL, 0,
795                         twl6040_ep_drv_event,
796                         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
797         SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
798                         TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
799         SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
800                         TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),
801
802         SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
803                             NULL, 0),
804         SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
805                             NULL, 0),
806         SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
807                               twl6040_hs_dac_event,
808                               SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
809
810         /* Analog playback PGAs */
811         SND_SOC_DAPM_PGA("HF Left PGA",
812                         TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
813         SND_SOC_DAPM_PGA("HF Right PGA",
814                         TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
815
816 };
817
818 static const struct snd_soc_dapm_route intercon[] = {
819         /* Stream -> DAC mapping */
820         {"HSDAC Left", NULL, "Legacy Playback"},
821         {"HSDAC Left", NULL, "Headset Playback"},
822         {"HSDAC Right", NULL, "Legacy Playback"},
823         {"HSDAC Right", NULL, "Headset Playback"},
824
825         {"HFDAC Left", NULL, "Legacy Playback"},
826         {"HFDAC Left", NULL, "Handsfree Playback"},
827         {"HFDAC Right", NULL, "Legacy Playback"},
828         {"HFDAC Right", NULL, "Handsfree Playback"},
829
830         {"VIBRA DAC", NULL, "Legacy Playback"},
831         {"VIBRA DAC", NULL, "Vibra Playback"},
832
833         /* ADC -> Stream mapping */
834         {"Legacy Capture" , NULL, "ADC Left"},
835         {"Capture", NULL, "ADC Left"},
836         {"Legacy Capture", NULL, "ADC Right"},
837         {"Capture" , NULL, "ADC Right"},
838
839         /* Capture path */
840         {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
841         {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
842         {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
843
844         {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
845         {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
846         {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
847
848         {"MicAmpL", NULL, "Analog Left Capture Route"},
849         {"MicAmpR", NULL, "Analog Right Capture Route"},
850
851         {"ADC Left", NULL, "MicAmpL"},
852         {"ADC Right", NULL, "MicAmpR"},
853
854         /* AFM path */
855         {"AFMAmpL", NULL, "AFML"},
856         {"AFMAmpR", NULL, "AFMR"},
857
858         {"HSDAC Left", NULL, "HSDAC Power"},
859         {"HSDAC Right", NULL, "HSDAC Power"},
860
861         {"Headset Left Playback", "HS DAC", "HSDAC Left"},
862         {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
863
864         {"Headset Right Playback", "HS DAC", "HSDAC Right"},
865         {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
866
867         {"HS Left Driver", NULL, "Headset Left Playback"},
868         {"HS Right Driver", NULL, "Headset Right Playback"},
869
870         {"HSOL", NULL, "HS Left Driver"},
871         {"HSOR", NULL, "HS Right Driver"},
872
873         /* Earphone playback path */
874         {"Earphone Playback", "Switch", "HSDAC Left"},
875         {"Earphone Driver", NULL, "Earphone Playback"},
876         {"EP", NULL, "Earphone Driver"},
877
878         {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
879         {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
880
881         {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
882         {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
883
884         {"HF Left PGA", NULL, "Handsfree Left Playback"},
885         {"HF Right PGA", NULL, "Handsfree Right Playback"},
886
887         {"HF Left Driver", NULL, "HF Left PGA"},
888         {"HF Right Driver", NULL, "HF Right PGA"},
889
890         {"HFL", NULL, "HF Left Driver"},
891         {"HFR", NULL, "HF Right Driver"},
892
893         {"AUXL Playback", "Switch", "HF Left PGA"},
894         {"AUXR Playback", "Switch", "HF Right PGA"},
895
896         {"AUXL", NULL, "AUXL Playback"},
897         {"AUXR", NULL, "AUXR Playback"},
898
899         /* Vibrator paths */
900         {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
901         {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
902
903         {"Vibra Left Driver", NULL, "Vibra Left Playback"},
904         {"Vibra Right Driver", NULL, "Vibra Right Playback"},
905         {"Vibra Left Driver", NULL, "Vibra Left Control"},
906         {"Vibra Right Driver", NULL, "Vibra Right Control"},
907
908         {"VIBRAL", NULL, "Vibra Left Driver"},
909         {"VIBRAR", NULL, "Vibra Right Driver"},
910 };
911
912 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
913                                 enum snd_soc_bias_level level)
914 {
915         struct twl6040 *twl6040 = codec->control_data;
916         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
917         int ret;
918
919         switch (level) {
920         case SND_SOC_BIAS_ON:
921                 break;
922         case SND_SOC_BIAS_PREPARE:
923                 break;
924         case SND_SOC_BIAS_STANDBY:
925                 if (priv->codec_powered)
926                         break;
927
928                 ret = twl6040_power(twl6040, 1);
929                 if (ret)
930                         return ret;
931
932                 priv->codec_powered = 1;
933
934                 twl6040_restore_regs(codec);
935
936                 /* Set external boost GPO */
937                 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
938                 break;
939         case SND_SOC_BIAS_OFF:
940                 if (!priv->codec_powered)
941                         break;
942
943                 twl6040_power(twl6040, 0);
944                 priv->codec_powered = 0;
945                 break;
946         }
947
948         codec->dapm.bias_level = level;
949
950         return 0;
951 }
952
953 static int twl6040_startup(struct snd_pcm_substream *substream,
954                         struct snd_soc_dai *dai)
955 {
956         struct snd_soc_codec *codec = dai->codec;
957         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
958
959         snd_pcm_hw_constraint_list(substream->runtime, 0,
960                                 SNDRV_PCM_HW_PARAM_RATE,
961                                 &sysclk_constraints[priv->pll_power_mode]);
962
963         return 0;
964 }
965
966 static int twl6040_hw_params(struct snd_pcm_substream *substream,
967                         struct snd_pcm_hw_params *params,
968                         struct snd_soc_dai *dai)
969 {
970         struct snd_soc_codec *codec = dai->codec;
971         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
972         int rate;
973
974         rate = params_rate(params);
975         switch (rate) {
976         case 11250:
977         case 22500:
978         case 44100:
979         case 88200:
980                 /* These rates are not supported when HPPLL is in use */
981                 if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
982                         dev_err(codec->dev, "HPPLL does not support rate %d\n",
983                                 rate);
984                         return -EINVAL;
985                 }
986                 priv->sysclk = 17640000;
987                 break;
988         case 8000:
989         case 16000:
990         case 32000:
991         case 48000:
992         case 96000:
993                 priv->sysclk = 19200000;
994                 break;
995         default:
996                 dev_err(codec->dev, "unsupported rate %d\n", rate);
997                 return -EINVAL;
998         }
999
1000         return 0;
1001 }
1002
1003 static int twl6040_prepare(struct snd_pcm_substream *substream,
1004                         struct snd_soc_dai *dai)
1005 {
1006         struct snd_soc_codec *codec = dai->codec;
1007         struct twl6040 *twl6040 = codec->control_data;
1008         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1009         int ret;
1010
1011         if (!priv->sysclk) {
1012                 dev_err(codec->dev,
1013                         "no mclk configured, call set_sysclk() on init\n");
1014                 return -EINVAL;
1015         }
1016
1017         ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1018         if (ret) {
1019                 dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1020                 return -EPERM;
1021         }
1022
1023         return 0;
1024 }
1025
1026 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1027                 int clk_id, unsigned int freq, int dir)
1028 {
1029         struct snd_soc_codec *codec = codec_dai->codec;
1030         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1031
1032         switch (clk_id) {
1033         case TWL6040_SYSCLK_SEL_LPPLL:
1034         case TWL6040_SYSCLK_SEL_HPPLL:
1035                 priv->pll = clk_id;
1036                 priv->clk_in = freq;
1037                 break;
1038         default:
1039                 dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1040                 return -EINVAL;
1041         }
1042
1043         return 0;
1044 }
1045
1046 static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id id,
1047                              int mute)
1048 {
1049         struct twl6040 *twl6040 = codec->control_data;
1050         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1051         int hslctl, hsrctl, earctl;
1052         int hflctl, hfrctl;
1053
1054         switch (id) {
1055         case TWL6040_DAI_DL1:
1056                 hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
1057                 hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
1058                 earctl = twl6040_read_reg_cache(codec, TWL6040_REG_EARCTL);
1059
1060                 if (mute) {
1061                         /* Power down drivers and DACs */
1062                         earctl &= ~0x01;
1063                         hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
1064                         hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
1065
1066                 }
1067
1068                 twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl);
1069                 twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl);
1070                 twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl);
1071                 priv->dl1_unmuted = !mute;
1072                 break;
1073         case TWL6040_DAI_DL2:
1074                 hflctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFLCTL);
1075                 hfrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFRCTL);
1076
1077                 if (mute) {
1078                         /* Power down drivers and DACs */
1079                         hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
1080                                     TWL6040_HFDRVENA);
1081                         hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
1082                                     TWL6040_HFDRVENA);
1083                 }
1084
1085                 twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl);
1086                 twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl);
1087                 priv->dl2_unmuted = !mute;
1088                 break;
1089         default:
1090                 break;
1091         }
1092 }
1093
1094 static int twl6040_digital_mute(struct snd_soc_dai *dai, int mute)
1095 {
1096         switch (dai->id) {
1097         case TWL6040_DAI_LEGACY:
1098                 twl6040_mute_path(dai->codec, TWL6040_DAI_DL1, mute);
1099                 twl6040_mute_path(dai->codec, TWL6040_DAI_DL2, mute);
1100                 break;
1101         case TWL6040_DAI_DL1:
1102         case TWL6040_DAI_DL2:
1103                 twl6040_mute_path(dai->codec, dai->id, mute);
1104                 break;
1105         default:
1106                 break;
1107         }
1108
1109         return 0;
1110 }
1111
1112 static const struct snd_soc_dai_ops twl6040_dai_ops = {
1113         .startup        = twl6040_startup,
1114         .hw_params      = twl6040_hw_params,
1115         .prepare        = twl6040_prepare,
1116         .set_sysclk     = twl6040_set_dai_sysclk,
1117         .digital_mute   = twl6040_digital_mute,
1118 };
1119
1120 static struct snd_soc_dai_driver twl6040_dai[] = {
1121 {
1122         .name = "twl6040-legacy",
1123         .id = TWL6040_DAI_LEGACY,
1124         .playback = {
1125                 .stream_name = "Legacy Playback",
1126                 .channels_min = 1,
1127                 .channels_max = 5,
1128                 .rates = TWL6040_RATES,
1129                 .formats = TWL6040_FORMATS,
1130         },
1131         .capture = {
1132                 .stream_name = "Legacy Capture",
1133                 .channels_min = 1,
1134                 .channels_max = 2,
1135                 .rates = TWL6040_RATES,
1136                 .formats = TWL6040_FORMATS,
1137         },
1138         .ops = &twl6040_dai_ops,
1139 },
1140 {
1141         .name = "twl6040-ul",
1142         .id = TWL6040_DAI_UL,
1143         .capture = {
1144                 .stream_name = "Capture",
1145                 .channels_min = 1,
1146                 .channels_max = 2,
1147                 .rates = TWL6040_RATES,
1148                 .formats = TWL6040_FORMATS,
1149         },
1150         .ops = &twl6040_dai_ops,
1151 },
1152 {
1153         .name = "twl6040-dl1",
1154         .id = TWL6040_DAI_DL1,
1155         .playback = {
1156                 .stream_name = "Headset Playback",
1157                 .channels_min = 1,
1158                 .channels_max = 2,
1159                 .rates = TWL6040_RATES,
1160                 .formats = TWL6040_FORMATS,
1161         },
1162         .ops = &twl6040_dai_ops,
1163 },
1164 {
1165         .name = "twl6040-dl2",
1166         .id = TWL6040_DAI_DL2,
1167         .playback = {
1168                 .stream_name = "Handsfree Playback",
1169                 .channels_min = 1,
1170                 .channels_max = 2,
1171                 .rates = TWL6040_RATES,
1172                 .formats = TWL6040_FORMATS,
1173         },
1174         .ops = &twl6040_dai_ops,
1175 },
1176 {
1177         .name = "twl6040-vib",
1178         .id = TWL6040_DAI_VIB,
1179         .playback = {
1180                 .stream_name = "Vibra Playback",
1181                 .channels_min = 1,
1182                 .channels_max = 1,
1183                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1184                 .formats = TWL6040_FORMATS,
1185         },
1186         .ops = &twl6040_dai_ops,
1187 },
1188 };
1189
1190 #ifdef CONFIG_PM
1191 static int twl6040_suspend(struct snd_soc_codec *codec)
1192 {
1193         twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1194
1195         return 0;
1196 }
1197
1198 static int twl6040_resume(struct snd_soc_codec *codec)
1199 {
1200         twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1201
1202         return 0;
1203 }
1204 #else
1205 #define twl6040_suspend NULL
1206 #define twl6040_resume NULL
1207 #endif
1208
1209 static int twl6040_probe(struct snd_soc_codec *codec)
1210 {
1211         struct twl6040_data *priv;
1212         struct platform_device *pdev = container_of(codec->dev,
1213                                                    struct platform_device, dev);
1214         int ret = 0;
1215
1216         priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL);
1217         if (priv == NULL)
1218                 return -ENOMEM;
1219
1220         snd_soc_codec_set_drvdata(codec, priv);
1221
1222         priv->codec = codec;
1223         codec->control_data = dev_get_drvdata(codec->dev->parent);
1224
1225         priv->plug_irq = platform_get_irq(pdev, 0);
1226         if (priv->plug_irq < 0) {
1227                 dev_err(codec->dev, "invalid irq\n");
1228                 return -EINVAL;
1229         }
1230
1231         INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
1232
1233         mutex_init(&priv->mutex);
1234
1235         ret = request_threaded_irq(priv->plug_irq, NULL,
1236                                         twl6040_audio_handler, IRQF_NO_SUSPEND,
1237                                         "twl6040_irq_plug", codec);
1238         if (ret) {
1239                 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1240                 return ret;
1241         }
1242
1243         twl6040_init_chip(codec);
1244
1245         /* power on device */
1246         return twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1247 }
1248
1249 static int twl6040_remove(struct snd_soc_codec *codec)
1250 {
1251         struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1252
1253         free_irq(priv->plug_irq, codec);
1254         twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1255
1256         return 0;
1257 }
1258
1259 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1260         .probe = twl6040_probe,
1261         .remove = twl6040_remove,
1262         .suspend = twl6040_suspend,
1263         .resume = twl6040_resume,
1264         .read = twl6040_read_reg_cache,
1265         .write = twl6040_write,
1266         .set_bias_level = twl6040_set_bias_level,
1267         .reg_cache_size = ARRAY_SIZE(twl6040_reg),
1268         .reg_word_size = sizeof(u8),
1269         .reg_cache_default = twl6040_reg,
1270         .ignore_pmdown_time = true,
1271
1272         .controls = twl6040_snd_controls,
1273         .num_controls = ARRAY_SIZE(twl6040_snd_controls),
1274         .dapm_widgets = twl6040_dapm_widgets,
1275         .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
1276         .dapm_routes = intercon,
1277         .num_dapm_routes = ARRAY_SIZE(intercon),
1278 };
1279
1280 static int twl6040_codec_probe(struct platform_device *pdev)
1281 {
1282         return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1283                                       twl6040_dai, ARRAY_SIZE(twl6040_dai));
1284 }
1285
1286 static int twl6040_codec_remove(struct platform_device *pdev)
1287 {
1288         snd_soc_unregister_codec(&pdev->dev);
1289         return 0;
1290 }
1291
1292 static struct platform_driver twl6040_codec_driver = {
1293         .driver = {
1294                 .name = "twl6040-codec",
1295                 .owner = THIS_MODULE,
1296         },
1297         .probe = twl6040_codec_probe,
1298         .remove = twl6040_codec_remove,
1299 };
1300
1301 module_platform_driver(twl6040_codec_driver);
1302
1303 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1304 MODULE_AUTHOR("Misael Lopez Cruz");
1305 MODULE_LICENSE("GPL");