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