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