xref: /linux/sound/soc/codecs/twl6040.c (revision ff5599816711d2e67da2d7561fd36ac48debd433)
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 	schedule_delayed_work(&priv->hs_jack.work, msecs_to_jiffies(200));
433 
434 	return IRQ_HANDLED;
435 }
436 
437 static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
438 	struct snd_ctl_elem_value *ucontrol)
439 {
440 	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
441 	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
442 	struct snd_soc_codec *codec = widget->codec;
443 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
444 	unsigned int val;
445 
446 	/* Do not allow changes while Input/FF efect is running */
447 	val = twl6040_read_reg_volatile(codec, e->reg);
448 	if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
449 		return -EBUSY;
450 
451 	return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
452 }
453 
454 /*
455  * MICATT volume control:
456  * from -6 to 0 dB in 6 dB steps
457  */
458 static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
459 
460 /*
461  * MICGAIN volume control:
462  * from 6 to 30 dB in 6 dB steps
463  */
464 static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
465 
466 /*
467  * AFMGAIN volume control:
468  * from -18 to 24 dB in 6 dB steps
469  */
470 static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
471 
472 /*
473  * HSGAIN volume control:
474  * from -30 to 0 dB in 2 dB steps
475  */
476 static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
477 
478 /*
479  * HFGAIN volume control:
480  * from -52 to 6 dB in 2 dB steps
481  */
482 static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
483 
484 /*
485  * EPGAIN volume control:
486  * from -24 to 6 dB in 2 dB steps
487  */
488 static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
489 
490 /* Left analog microphone selection */
491 static const char *twl6040_amicl_texts[] =
492 	{"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
493 
494 /* Right analog microphone selection */
495 static const char *twl6040_amicr_texts[] =
496 	{"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
497 
498 static const struct soc_enum twl6040_enum[] = {
499 	SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
500 	SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
501 };
502 
503 static const char *twl6040_hs_texts[] = {
504 	"Off", "HS DAC", "Line-In amp"
505 };
506 
507 static const struct soc_enum twl6040_hs_enum[] = {
508 	SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
509 			twl6040_hs_texts),
510 	SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
511 			twl6040_hs_texts),
512 };
513 
514 static const char *twl6040_hf_texts[] = {
515 	"Off", "HF DAC", "Line-In amp"
516 };
517 
518 static const struct soc_enum twl6040_hf_enum[] = {
519 	SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
520 			twl6040_hf_texts),
521 	SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
522 			twl6040_hf_texts),
523 };
524 
525 static const char *twl6040_vibrapath_texts[] = {
526 	"Input FF", "Audio PDM"
527 };
528 
529 static const struct soc_enum twl6040_vibra_enum[] = {
530 	SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
531 			ARRAY_SIZE(twl6040_vibrapath_texts),
532 			twl6040_vibrapath_texts),
533 	SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
534 			ARRAY_SIZE(twl6040_vibrapath_texts),
535 			twl6040_vibrapath_texts),
536 };
537 
538 static const struct snd_kcontrol_new amicl_control =
539 	SOC_DAPM_ENUM("Route", twl6040_enum[0]);
540 
541 static const struct snd_kcontrol_new amicr_control =
542 	SOC_DAPM_ENUM("Route", twl6040_enum[1]);
543 
544 /* Headset DAC playback switches */
545 static const struct snd_kcontrol_new hsl_mux_controls =
546 	SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
547 
548 static const struct snd_kcontrol_new hsr_mux_controls =
549 	SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
550 
551 /* Handsfree DAC playback switches */
552 static const struct snd_kcontrol_new hfl_mux_controls =
553 	SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
554 
555 static const struct snd_kcontrol_new hfr_mux_controls =
556 	SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
557 
558 static const struct snd_kcontrol_new ep_path_enable_control =
559 	SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
560 
561 static const struct snd_kcontrol_new auxl_switch_control =
562 	SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
563 
564 static const struct snd_kcontrol_new auxr_switch_control =
565 	SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
566 
567 /* Vibra playback switches */
568 static const struct snd_kcontrol_new vibral_mux_controls =
569 	SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
570 		snd_soc_dapm_get_enum_double,
571 		twl6040_soc_dapm_put_vibra_enum);
572 
573 static const struct snd_kcontrol_new vibrar_mux_controls =
574 	SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
575 		snd_soc_dapm_get_enum_double,
576 		twl6040_soc_dapm_put_vibra_enum);
577 
578 /* Headset power mode */
579 static const char *twl6040_power_mode_texts[] = {
580 	"Low-Power", "High-Performance",
581 };
582 
583 static const struct soc_enum twl6040_power_mode_enum =
584 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
585 			twl6040_power_mode_texts);
586 
587 static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
588 	struct snd_ctl_elem_value *ucontrol)
589 {
590 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
591 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
592 
593 	ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
594 
595 	return 0;
596 }
597 
598 static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
599 	struct snd_ctl_elem_value *ucontrol)
600 {
601 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
602 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
603 	int high_perf = ucontrol->value.enumerated.item[0];
604 	int ret = 0;
605 
606 	if (!priv->hs_power_mode_locked)
607 		ret = headset_power_mode(codec, high_perf);
608 
609 	if (!ret)
610 		priv->hs_power_mode = high_perf;
611 
612 	return ret;
613 }
614 
615 static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
616 	struct snd_ctl_elem_value *ucontrol)
617 {
618 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
619 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
620 
621 	ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
622 
623 	return 0;
624 }
625 
626 static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
627 	struct snd_ctl_elem_value *ucontrol)
628 {
629 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
630 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
631 
632 	priv->pll_power_mode = ucontrol->value.enumerated.item[0];
633 
634 	return 0;
635 }
636 
637 int twl6040_get_dl1_gain(struct snd_soc_codec *codec)
638 {
639 	struct snd_soc_dapm_context *dapm = &codec->dapm;
640 
641 	if (snd_soc_dapm_get_pin_status(dapm, "EP"))
642 		return -1; /* -1dB */
643 
644 	if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
645 		snd_soc_dapm_get_pin_status(dapm, "HSOL")) {
646 
647 		u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL);
648 		if (val & TWL6040_HSDACMODE)
649 			/* HSDACL in LP mode */
650 			return -8; /* -8dB */
651 		else
652 			/* HSDACL in HP mode */
653 			return -1; /* -1dB */
654 	}
655 	return 0; /* 0dB */
656 }
657 EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);
658 
659 int twl6040_get_clk_id(struct snd_soc_codec *codec)
660 {
661 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
662 
663 	return priv->pll_power_mode;
664 }
665 EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
666 
667 int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
668 {
669 	if (unlikely(trim >= TWL6040_TRIM_INVAL))
670 		return -EINVAL;
671 
672 	return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim);
673 }
674 EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
675 
676 int twl6040_get_hs_step_size(struct snd_soc_codec *codec)
677 {
678 	struct twl6040 *twl6040 = codec->control_data;
679 
680 	if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3)
681 		/* For ES under ES_1.3 HS step is 2 mV */
682 		return 2;
683 	else
684 		/* For ES_1.3 HS step is 1 mV */
685 		return 1;
686 }
687 EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);
688 
689 static const struct snd_kcontrol_new twl6040_snd_controls[] = {
690 	/* Capture gains */
691 	SOC_DOUBLE_TLV("Capture Preamplifier Volume",
692 		TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
693 	SOC_DOUBLE_TLV("Capture Volume",
694 		TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
695 
696 	/* AFM gains */
697 	SOC_DOUBLE_TLV("Aux FM Volume",
698 		TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
699 
700 	/* Playback gains */
701 	SOC_DOUBLE_TLV("Headset Playback Volume",
702 		TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
703 	SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
704 		TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
705 	SOC_SINGLE_TLV("Earphone Playback Volume",
706 		TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
707 
708 	SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
709 		twl6040_headset_power_get_enum,
710 		twl6040_headset_power_put_enum),
711 
712 	SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
713 		twl6040_pll_get_enum, twl6040_pll_put_enum),
714 };
715 
716 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
717 	/* Inputs */
718 	SND_SOC_DAPM_INPUT("MAINMIC"),
719 	SND_SOC_DAPM_INPUT("HSMIC"),
720 	SND_SOC_DAPM_INPUT("SUBMIC"),
721 	SND_SOC_DAPM_INPUT("AFML"),
722 	SND_SOC_DAPM_INPUT("AFMR"),
723 
724 	/* Outputs */
725 	SND_SOC_DAPM_OUTPUT("HSOL"),
726 	SND_SOC_DAPM_OUTPUT("HSOR"),
727 	SND_SOC_DAPM_OUTPUT("HFL"),
728 	SND_SOC_DAPM_OUTPUT("HFR"),
729 	SND_SOC_DAPM_OUTPUT("EP"),
730 	SND_SOC_DAPM_OUTPUT("AUXL"),
731 	SND_SOC_DAPM_OUTPUT("AUXR"),
732 	SND_SOC_DAPM_OUTPUT("VIBRAL"),
733 	SND_SOC_DAPM_OUTPUT("VIBRAR"),
734 
735 	/* Analog input muxes for the capture amplifiers */
736 	SND_SOC_DAPM_MUX("Analog Left Capture Route",
737 			SND_SOC_NOPM, 0, 0, &amicl_control),
738 	SND_SOC_DAPM_MUX("Analog Right Capture Route",
739 			SND_SOC_NOPM, 0, 0, &amicr_control),
740 
741 	/* Analog capture PGAs */
742 	SND_SOC_DAPM_PGA("MicAmpL",
743 			TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
744 	SND_SOC_DAPM_PGA("MicAmpR",
745 			TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
746 
747 	/* Auxiliary FM PGAs */
748 	SND_SOC_DAPM_PGA("AFMAmpL",
749 			TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
750 	SND_SOC_DAPM_PGA("AFMAmpR",
751 			TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
752 
753 	/* ADCs */
754 	SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0),
755 	SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0),
756 
757 	/* Microphone bias */
758 	SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
759 			    TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
760 	SND_SOC_DAPM_SUPPLY("Main Mic Bias",
761 			    TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
762 	SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
763 			    TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
764 	SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
765 			    TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
766 
767 	/* DACs */
768 	SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0),
769 	SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0),
770 	SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0),
771 	SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0),
772 	/* Virtual DAC for vibra path (DL4 channel) */
773 	SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0),
774 
775 	SND_SOC_DAPM_MUX("Handsfree Left Playback",
776 			SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
777 	SND_SOC_DAPM_MUX("Handsfree Right Playback",
778 			SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
779 	/* Analog playback Muxes */
780 	SND_SOC_DAPM_MUX("Headset Left Playback",
781 			SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
782 	SND_SOC_DAPM_MUX("Headset Right Playback",
783 			SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
784 
785 	SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
786 			&vibral_mux_controls),
787 	SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
788 			&vibrar_mux_controls),
789 
790 	SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
791 			&ep_path_enable_control),
792 	SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
793 			&auxl_switch_control),
794 	SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
795 			&auxr_switch_control),
796 
797 	/* Analog playback drivers */
798 	SND_SOC_DAPM_OUT_DRV("HF Left Driver",
799 			TWL6040_REG_HFLCTL, 4, 0, NULL, 0),
800 	SND_SOC_DAPM_OUT_DRV("HF Right Driver",
801 			TWL6040_REG_HFRCTL, 4, 0, NULL, 0),
802 	SND_SOC_DAPM_OUT_DRV("HS Left Driver",
803 			TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
804 	SND_SOC_DAPM_OUT_DRV("HS Right Driver",
805 			TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
806 	SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
807 			TWL6040_REG_EARCTL, 0, 0, NULL, 0,
808 			twl6040_ep_drv_event,
809 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
810 	SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
811 			TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
812 	SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
813 			TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),
814 
815 	SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
816 			    NULL, 0),
817 	SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
818 			    NULL, 0),
819 	SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
820 			      twl6040_hs_dac_event,
821 			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
822 
823 	/* Analog playback PGAs */
824 	SND_SOC_DAPM_PGA("HF Left PGA",
825 			TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
826 	SND_SOC_DAPM_PGA("HF Right PGA",
827 			TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
828 
829 };
830 
831 static const struct snd_soc_dapm_route intercon[] = {
832 	/* Stream -> DAC mapping */
833 	{"HSDAC Left", NULL, "Legacy Playback"},
834 	{"HSDAC Left", NULL, "Headset Playback"},
835 	{"HSDAC Right", NULL, "Legacy Playback"},
836 	{"HSDAC Right", NULL, "Headset Playback"},
837 
838 	{"HFDAC Left", NULL, "Legacy Playback"},
839 	{"HFDAC Left", NULL, "Handsfree Playback"},
840 	{"HFDAC Right", NULL, "Legacy Playback"},
841 	{"HFDAC Right", NULL, "Handsfree Playback"},
842 
843 	{"VIBRA DAC", NULL, "Legacy Playback"},
844 	{"VIBRA DAC", NULL, "Vibra Playback"},
845 
846 	/* ADC -> Stream mapping */
847 	{"Legacy Capture" , NULL, "ADC Left"},
848 	{"Capture", NULL, "ADC Left"},
849 	{"Legacy Capture", NULL, "ADC Right"},
850 	{"Capture" , NULL, "ADC Right"},
851 
852 	/* Capture path */
853 	{"Analog Left Capture Route", "Headset Mic", "HSMIC"},
854 	{"Analog Left Capture Route", "Main Mic", "MAINMIC"},
855 	{"Analog Left Capture Route", "Aux/FM Left", "AFML"},
856 
857 	{"Analog Right Capture Route", "Headset Mic", "HSMIC"},
858 	{"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
859 	{"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
860 
861 	{"MicAmpL", NULL, "Analog Left Capture Route"},
862 	{"MicAmpR", NULL, "Analog Right Capture Route"},
863 
864 	{"ADC Left", NULL, "MicAmpL"},
865 	{"ADC Right", NULL, "MicAmpR"},
866 
867 	/* AFM path */
868 	{"AFMAmpL", NULL, "AFML"},
869 	{"AFMAmpR", NULL, "AFMR"},
870 
871 	{"HSDAC Left", NULL, "HSDAC Power"},
872 	{"HSDAC Right", NULL, "HSDAC Power"},
873 
874 	{"Headset Left Playback", "HS DAC", "HSDAC Left"},
875 	{"Headset Left Playback", "Line-In amp", "AFMAmpL"},
876 
877 	{"Headset Right Playback", "HS DAC", "HSDAC Right"},
878 	{"Headset Right Playback", "Line-In amp", "AFMAmpR"},
879 
880 	{"HS Left Driver", NULL, "Headset Left Playback"},
881 	{"HS Right Driver", NULL, "Headset Right Playback"},
882 
883 	{"HSOL", NULL, "HS Left Driver"},
884 	{"HSOR", NULL, "HS Right Driver"},
885 
886 	/* Earphone playback path */
887 	{"Earphone Playback", "Switch", "HSDAC Left"},
888 	{"Earphone Driver", NULL, "Earphone Playback"},
889 	{"EP", NULL, "Earphone Driver"},
890 
891 	{"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
892 	{"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
893 
894 	{"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
895 	{"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
896 
897 	{"HF Left PGA", NULL, "Handsfree Left Playback"},
898 	{"HF Right PGA", NULL, "Handsfree Right Playback"},
899 
900 	{"HF Left Driver", NULL, "HF Left PGA"},
901 	{"HF Right Driver", NULL, "HF Right PGA"},
902 
903 	{"HFL", NULL, "HF Left Driver"},
904 	{"HFR", NULL, "HF Right Driver"},
905 
906 	{"AUXL Playback", "Switch", "HF Left PGA"},
907 	{"AUXR Playback", "Switch", "HF Right PGA"},
908 
909 	{"AUXL", NULL, "AUXL Playback"},
910 	{"AUXR", NULL, "AUXR Playback"},
911 
912 	/* Vibrator paths */
913 	{"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
914 	{"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
915 
916 	{"Vibra Left Driver", NULL, "Vibra Left Playback"},
917 	{"Vibra Right Driver", NULL, "Vibra Right Playback"},
918 	{"Vibra Left Driver", NULL, "Vibra Left Control"},
919 	{"Vibra Right Driver", NULL, "Vibra Right Control"},
920 
921 	{"VIBRAL", NULL, "Vibra Left Driver"},
922 	{"VIBRAR", NULL, "Vibra Right Driver"},
923 };
924 
925 static int twl6040_set_bias_level(struct snd_soc_codec *codec,
926 				enum snd_soc_bias_level level)
927 {
928 	struct twl6040 *twl6040 = codec->control_data;
929 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
930 	int ret;
931 
932 	switch (level) {
933 	case SND_SOC_BIAS_ON:
934 		break;
935 	case SND_SOC_BIAS_PREPARE:
936 		break;
937 	case SND_SOC_BIAS_STANDBY:
938 		if (priv->codec_powered)
939 			break;
940 
941 		ret = twl6040_power(twl6040, 1);
942 		if (ret)
943 			return ret;
944 
945 		priv->codec_powered = 1;
946 
947 		twl6040_restore_regs(codec);
948 
949 		/* Set external boost GPO */
950 		twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
951 		break;
952 	case SND_SOC_BIAS_OFF:
953 		if (!priv->codec_powered)
954 			break;
955 
956 		twl6040_power(twl6040, 0);
957 		priv->codec_powered = 0;
958 		break;
959 	}
960 
961 	codec->dapm.bias_level = level;
962 
963 	return 0;
964 }
965 
966 static int twl6040_startup(struct snd_pcm_substream *substream,
967 			struct snd_soc_dai *dai)
968 {
969 	struct snd_soc_codec *codec = dai->codec;
970 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
971 
972 	snd_pcm_hw_constraint_list(substream->runtime, 0,
973 				SNDRV_PCM_HW_PARAM_RATE,
974 				&sysclk_constraints[priv->pll_power_mode]);
975 
976 	return 0;
977 }
978 
979 static int twl6040_hw_params(struct snd_pcm_substream *substream,
980 			struct snd_pcm_hw_params *params,
981 			struct snd_soc_dai *dai)
982 {
983 	struct snd_soc_codec *codec = dai->codec;
984 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
985 	int rate;
986 
987 	rate = params_rate(params);
988 	switch (rate) {
989 	case 11250:
990 	case 22500:
991 	case 44100:
992 	case 88200:
993 		/* These rates are not supported when HPPLL is in use */
994 		if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
995 			dev_err(codec->dev, "HPPLL does not support rate %d\n",
996 				rate);
997 			return -EINVAL;
998 		}
999 		priv->sysclk = 17640000;
1000 		break;
1001 	case 8000:
1002 	case 16000:
1003 	case 32000:
1004 	case 48000:
1005 	case 96000:
1006 		priv->sysclk = 19200000;
1007 		break;
1008 	default:
1009 		dev_err(codec->dev, "unsupported rate %d\n", rate);
1010 		return -EINVAL;
1011 	}
1012 
1013 	return 0;
1014 }
1015 
1016 static int twl6040_prepare(struct snd_pcm_substream *substream,
1017 			struct snd_soc_dai *dai)
1018 {
1019 	struct snd_soc_codec *codec = dai->codec;
1020 	struct twl6040 *twl6040 = codec->control_data;
1021 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1022 	int ret;
1023 
1024 	if (!priv->sysclk) {
1025 		dev_err(codec->dev,
1026 			"no mclk configured, call set_sysclk() on init\n");
1027 		return -EINVAL;
1028 	}
1029 
1030 	ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
1031 	if (ret) {
1032 		dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
1033 		return -EPERM;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1040 		int clk_id, unsigned int freq, int dir)
1041 {
1042 	struct snd_soc_codec *codec = codec_dai->codec;
1043 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1044 
1045 	switch (clk_id) {
1046 	case TWL6040_SYSCLK_SEL_LPPLL:
1047 	case TWL6040_SYSCLK_SEL_HPPLL:
1048 		priv->pll = clk_id;
1049 		priv->clk_in = freq;
1050 		break;
1051 	default:
1052 		dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
1053 		return -EINVAL;
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id id,
1060 			     int mute)
1061 {
1062 	struct twl6040 *twl6040 = codec->control_data;
1063 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1064 	int hslctl, hsrctl, earctl;
1065 	int hflctl, hfrctl;
1066 
1067 	switch (id) {
1068 	case TWL6040_DAI_DL1:
1069 		hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
1070 		hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
1071 		earctl = twl6040_read_reg_cache(codec, TWL6040_REG_EARCTL);
1072 
1073 		if (mute) {
1074 			/* Power down drivers and DACs */
1075 			earctl &= ~0x01;
1076 			hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
1077 			hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
1078 
1079 		}
1080 
1081 		twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl);
1082 		twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl);
1083 		twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl);
1084 		priv->dl1_unmuted = !mute;
1085 		break;
1086 	case TWL6040_DAI_DL2:
1087 		hflctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFLCTL);
1088 		hfrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HFRCTL);
1089 
1090 		if (mute) {
1091 			/* Power down drivers and DACs */
1092 			hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
1093 				    TWL6040_HFDRVENA);
1094 			hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
1095 				    TWL6040_HFDRVENA);
1096 		}
1097 
1098 		twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl);
1099 		twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl);
1100 		priv->dl2_unmuted = !mute;
1101 		break;
1102 	default:
1103 		break;
1104 	};
1105 }
1106 
1107 static int twl6040_digital_mute(struct snd_soc_dai *dai, int mute)
1108 {
1109 	switch (dai->id) {
1110 	case TWL6040_DAI_LEGACY:
1111 		twl6040_mute_path(dai->codec, TWL6040_DAI_DL1, mute);
1112 		twl6040_mute_path(dai->codec, TWL6040_DAI_DL2, mute);
1113 		break;
1114 	case TWL6040_DAI_DL1:
1115 	case TWL6040_DAI_DL2:
1116 		twl6040_mute_path(dai->codec, dai->id, mute);
1117 		break;
1118 	default:
1119 		break;
1120 	}
1121 
1122 	return 0;
1123 }
1124 
1125 static const struct snd_soc_dai_ops twl6040_dai_ops = {
1126 	.startup	= twl6040_startup,
1127 	.hw_params	= twl6040_hw_params,
1128 	.prepare	= twl6040_prepare,
1129 	.set_sysclk	= twl6040_set_dai_sysclk,
1130 	.digital_mute	= twl6040_digital_mute,
1131 };
1132 
1133 static struct snd_soc_dai_driver twl6040_dai[] = {
1134 {
1135 	.name = "twl6040-legacy",
1136 	.id = TWL6040_DAI_LEGACY,
1137 	.playback = {
1138 		.stream_name = "Legacy Playback",
1139 		.channels_min = 1,
1140 		.channels_max = 5,
1141 		.rates = TWL6040_RATES,
1142 		.formats = TWL6040_FORMATS,
1143 	},
1144 	.capture = {
1145 		.stream_name = "Legacy Capture",
1146 		.channels_min = 1,
1147 		.channels_max = 2,
1148 		.rates = TWL6040_RATES,
1149 		.formats = TWL6040_FORMATS,
1150 	},
1151 	.ops = &twl6040_dai_ops,
1152 },
1153 {
1154 	.name = "twl6040-ul",
1155 	.id = TWL6040_DAI_UL,
1156 	.capture = {
1157 		.stream_name = "Capture",
1158 		.channels_min = 1,
1159 		.channels_max = 2,
1160 		.rates = TWL6040_RATES,
1161 		.formats = TWL6040_FORMATS,
1162 	},
1163 	.ops = &twl6040_dai_ops,
1164 },
1165 {
1166 	.name = "twl6040-dl1",
1167 	.id = TWL6040_DAI_DL1,
1168 	.playback = {
1169 		.stream_name = "Headset Playback",
1170 		.channels_min = 1,
1171 		.channels_max = 2,
1172 		.rates = TWL6040_RATES,
1173 		.formats = TWL6040_FORMATS,
1174 	},
1175 	.ops = &twl6040_dai_ops,
1176 },
1177 {
1178 	.name = "twl6040-dl2",
1179 	.id = TWL6040_DAI_DL2,
1180 	.playback = {
1181 		.stream_name = "Handsfree Playback",
1182 		.channels_min = 1,
1183 		.channels_max = 2,
1184 		.rates = TWL6040_RATES,
1185 		.formats = TWL6040_FORMATS,
1186 	},
1187 	.ops = &twl6040_dai_ops,
1188 },
1189 {
1190 	.name = "twl6040-vib",
1191 	.id = TWL6040_DAI_VIB,
1192 	.playback = {
1193 		.stream_name = "Vibra Playback",
1194 		.channels_min = 1,
1195 		.channels_max = 1,
1196 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
1197 		.formats = TWL6040_FORMATS,
1198 	},
1199 	.ops = &twl6040_dai_ops,
1200 },
1201 };
1202 
1203 #ifdef CONFIG_PM
1204 static int twl6040_suspend(struct snd_soc_codec *codec)
1205 {
1206 	twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1207 
1208 	return 0;
1209 }
1210 
1211 static int twl6040_resume(struct snd_soc_codec *codec)
1212 {
1213 	twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1214 
1215 	return 0;
1216 }
1217 #else
1218 #define twl6040_suspend NULL
1219 #define twl6040_resume NULL
1220 #endif
1221 
1222 static int twl6040_probe(struct snd_soc_codec *codec)
1223 {
1224 	struct twl6040_data *priv;
1225 	struct platform_device *pdev = container_of(codec->dev,
1226 						   struct platform_device, dev);
1227 	int ret = 0;
1228 
1229 	priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL);
1230 	if (priv == NULL)
1231 		return -ENOMEM;
1232 
1233 	snd_soc_codec_set_drvdata(codec, priv);
1234 
1235 	priv->codec = codec;
1236 	codec->control_data = dev_get_drvdata(codec->dev->parent);
1237 
1238 	priv->plug_irq = platform_get_irq(pdev, 0);
1239 	if (priv->plug_irq < 0) {
1240 		dev_err(codec->dev, "invalid irq\n");
1241 		return -EINVAL;
1242 	}
1243 
1244 	INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
1245 
1246 	mutex_init(&priv->mutex);
1247 
1248 	ret = request_threaded_irq(priv->plug_irq, NULL,
1249 					twl6040_audio_handler, IRQF_NO_SUSPEND,
1250 					"twl6040_irq_plug", codec);
1251 	if (ret) {
1252 		dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1253 		return ret;
1254 	}
1255 
1256 	twl6040_init_chip(codec);
1257 
1258 	/* power on device */
1259 	return twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1260 }
1261 
1262 static int twl6040_remove(struct snd_soc_codec *codec)
1263 {
1264 	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1265 
1266 	free_irq(priv->plug_irq, codec);
1267 	twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1268 
1269 	return 0;
1270 }
1271 
1272 static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
1273 	.probe = twl6040_probe,
1274 	.remove = twl6040_remove,
1275 	.suspend = twl6040_suspend,
1276 	.resume = twl6040_resume,
1277 	.read = twl6040_read_reg_cache,
1278 	.write = twl6040_write,
1279 	.set_bias_level = twl6040_set_bias_level,
1280 	.reg_cache_size = ARRAY_SIZE(twl6040_reg),
1281 	.reg_word_size = sizeof(u8),
1282 	.reg_cache_default = twl6040_reg,
1283 	.ignore_pmdown_time = true,
1284 
1285 	.controls = twl6040_snd_controls,
1286 	.num_controls = ARRAY_SIZE(twl6040_snd_controls),
1287 	.dapm_widgets = twl6040_dapm_widgets,
1288 	.num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
1289 	.dapm_routes = intercon,
1290 	.num_dapm_routes = ARRAY_SIZE(intercon),
1291 };
1292 
1293 static int twl6040_codec_probe(struct platform_device *pdev)
1294 {
1295 	return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1296 				      twl6040_dai, ARRAY_SIZE(twl6040_dai));
1297 }
1298 
1299 static int twl6040_codec_remove(struct platform_device *pdev)
1300 {
1301 	snd_soc_unregister_codec(&pdev->dev);
1302 	return 0;
1303 }
1304 
1305 static struct platform_driver twl6040_codec_driver = {
1306 	.driver = {
1307 		.name = "twl6040-codec",
1308 		.owner = THIS_MODULE,
1309 	},
1310 	.probe = twl6040_codec_probe,
1311 	.remove = twl6040_codec_remove,
1312 };
1313 
1314 module_platform_driver(twl6040_codec_driver);
1315 
1316 MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
1317 MODULE_AUTHOR("Misael Lopez Cruz");
1318 MODULE_LICENSE("GPL");
1319