blob: 81645c632447943cf2972d1e8926527bcf808260 [file] [log] [blame]
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001/*
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>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000027#include <linux/platform_device.h>
Stephen Rothwell68b40cc2010-03-29 17:55:51 +110028#include <linux/slab.h>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000029#include <linux/i2c/twl.h>
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -050030#include <linux/mfd/twl6040.h>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000031
32#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/soc.h>
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000036#include <sound/initval.h>
37#include <sound/tlv.h>
38
39#include "twl6040.h"
40
Olaya, Margarita60ea4ce2010-12-10 21:05:58 -060041#define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -060042#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};
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000071
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -060072struct twl6040_jack_data {
73 struct snd_soc_jack *jack;
74 int report;
75};
76
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000077/* codec private data */
78struct twl6040_data {
Peter Ujfalusi2a433b92011-07-04 19:52:26 +030079 int plug_irq;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000080 int codec_powered;
81 int pll;
82 int non_lp;
Peter Ujfalusiaf958c72011-06-27 17:03:14 +030083 int pll_power_mode;
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -060084 int hs_power_mode;
85 int hs_power_mode_locked;
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -050086 unsigned int clk_in;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +000087 unsigned int sysclk;
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -060088 u16 hs_left_step;
89 u16 hs_right_step;
90 u16 hf_left_step;
91 u16 hf_right_step;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -060092 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;
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -060097 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;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000103};
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 */
Peter Ujfalusi2c27ff42011-09-15 15:39:28 +0300121 0x00, /* TWL6040_MICLCTL 0x0C */
122 0x00, /* TWL6040_MICRCTL 0x0D */
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000123 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
Peter Ujfalusia52762e2011-09-15 15:39:27 +0300158/* List of registers to be restored after power up */
159static const int twl6040_restore_list[] = {
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000160 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,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000172};
173
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300174/* 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
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300187static unsigned int hp_rates[] = {
188 8000,
189 16000,
190 32000,
191 48000,
192 96000,
193};
194
Peter Ujfalusif53c3462011-06-29 13:28:18 +0300195static 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, },
Peter Ujfalusiaf958c72011-06-27 17:03:14 +0300198};
199
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000200/*
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{
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500233 struct twl6040 *twl6040 = codec->control_data;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000234 u8 value;
235
236 if (reg >= TWL6040_CACHEREGNUM)
237 return -EIO;
238
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500239 value = twl6040_reg_read(twl6040, reg);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000240 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{
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500251 struct twl6040 *twl6040 = codec->control_data;
252
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000253 if (reg >= TWL6040_CACHEREGNUM)
254 return -EIO;
255
256 twl6040_write_reg_cache(codec, reg, value);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500257 return twl6040_reg_write(twl6040, reg, value);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000258}
259
Peter Ujfalusia52762e2011-09-15 15:39:27 +0300260static void twl6040_init_chip(struct snd_soc_codec *codec)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000261{
Peter Ujfalusia52762e2011-09-15 15:39:27 +0300262 struct twl6040 *twl6040 = codec->control_data;
263 u8 val;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000264
Peter Ujfalusia52762e2011-09-15 15:39:27 +0300265 val = twl6040_get_revid(twl6040);
266 twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
267
Peter Ujfalusi2c27ff42011-09-15 15:39:28 +0300268 /* 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);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000272}
273
Peter Ujfalusia52762e2011-09-15 15:39:27 +0300274static void twl6040_restore_regs(struct snd_soc_codec *codec)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000275{
276 u8 *cache = codec->reg_cache;
277 int reg, i;
278
Peter Ujfalusia52762e2011-09-15 15:39:27 +0300279 for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
280 reg = twl6040_restore_list[i];
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000281 twl6040_write(codec, reg, cache[reg]);
282 }
283}
284
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600285/*
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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600305 if (val + left_step > headset->left_vol)
306 val = headset->left_vol;
307 else
308 val += left_step;
309
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600310 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600319 if ((int)val - (int)left_step < 0)
320 val = 0;
321 else
322 val -= left_step;
323
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600324 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600340 if (val + right_step > headset->right_vol)
341 val = headset->right_vol;
342 else
343 val += right_step;
344
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600345 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600354 if ((int)val - (int)right_step < 0)
355 val = 0;
356 else
357 val -= right_step;
358
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600359 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600389 if (val + left_step > handsfree->left_vol)
390 val = handsfree->left_vol;
391 else
392 val += left_step;
393
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600394 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600403 if ((int)val - (int)left_step < 0)
404 val = 0;
405 else
406 val -= left_step;
407
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600408 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600424 if (val + right_step > handsfree->right_vol)
425 val = handsfree->right_vol;
426 else
427 val += right_step;
428
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600429 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) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600438 if ((int)val - (int)right_step < 0)
439 val = 0;
440 else
441 val -= right_step;
442
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600443 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++) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600471 headset_complete = twl6040_hs_ramp_step(codec,
472 headset->left_step,
473 headset->right_step);
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600474
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++) {
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600514 handsfree_complete = twl6040_hf_ramp_step(codec,
515 handsfree->left_step,
516 handsfree->right_step);
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600517
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;
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600557 out->left_step = priv->hs_left_step;
558 out->right_step = priv->hs_right_step;
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600559 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;
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -0600565 out->left_step = priv->hf_left_step;
566 out->right_step = priv->hf_right_step;
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600567 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 */
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600599 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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000614/* 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
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -0500637static 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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000644static 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;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +0200648 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600649 int ret = 0;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000650
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600651 if (SND_SOC_DAPM_EVENT_ON(event)) {
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000652 priv->non_lp++;
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600653 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 {
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000659 priv->non_lp--;
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600660 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 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000665
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -0500666 msleep(1);
667
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600668 return ret;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000669}
670
Mark Brown64ed9832011-01-20 21:43:44 +0000671static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
672 struct snd_soc_jack *jack, int report)
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600673{
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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000712/* audio interrupt handler */
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500713static irqreturn_t twl6040_audio_handler(int irq, void *data)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000714{
715 struct snd_soc_codec *codec = data;
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500716 struct twl6040 *twl6040 = codec->control_data;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +0200717 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000718 u8 intid;
719
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -0500720 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
Olaya, Margaritacf370a52010-12-10 21:05:30 -0600721
722 if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT))
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -0600723 queue_delayed_work(priv->workqueue, &priv->delayed_work,
724 msecs_to_jiffies(200));
Olaya, Margaritacf370a52010-12-10 21:05:30 -0600725
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000726 return IRQ_HANDLED;
727}
728
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -0600729static 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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000879/*
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:
Ricardo Neri2763f452011-05-01 15:35:55 +0100887 * from 6 to 30 dB in 6 dB steps
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000888 */
Ricardo Neri2763f452011-05-01 15:35:55 +0100889static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000890
891/*
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600892 * AFMGAIN volume control:
Liam Girdwood1f71a3b2011-03-28 19:23:23 +0100893 * from -18 to 24 dB in 6 dB steps
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600894 */
Liam Girdwood1f71a3b2011-03-28 19:23:23 +0100895static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600896
897/*
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000898 * 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
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500909/*
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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000915/* 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[] = {
Francois Mazardcb973d72010-12-10 21:06:03 -0600924 SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
925 SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000926};
927
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600928static 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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000950static 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 */
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600957static const struct snd_kcontrol_new hsl_mux_controls =
958 SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000959
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600960static const struct snd_kcontrol_new hsr_mux_controls =
961 SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000962
963/* Handsfree DAC playback switches */
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600964static const struct snd_kcontrol_new hfl_mux_controls =
965 SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000966
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -0600967static const struct snd_kcontrol_new hfr_mux_controls =
968 SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +0000969
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -0500970static const struct snd_kcontrol_new ep_driver_switch_controls =
971 SOC_DAPM_SINGLE("Switch", TWL6040_REG_EARCTL, 0, 1, 0);
972
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600973/* Headset power mode */
Peter Ujfalusi7cca6062011-06-27 13:33:14 +0300974static const char *twl6040_power_mode_texts[] = {
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600975 "Low-Power", "High-Perfomance",
976};
977
Peter Ujfalusi7cca6062011-06-27 13:33:14 +0300978static const struct soc_enum twl6040_power_mode_enum =
979 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
980 twl6040_power_mode_texts);
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -0600981
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
Peter Ujfalusiaf958c72011-06-27 17:03:14 +03001010static 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];
Peter Ujfalusiaf958c72011-06-27 17:03:14 +03001028
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
Peter Ujfalusiff593ca2011-07-04 10:35:23 +03001036 return priv->pll_power_mode;
Peter Ujfalusiaf958c72011-06-27 17:03:14 +03001037}
1038EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
1039
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001040static 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
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001047 /* AFM gains */
1048 SOC_DOUBLE_TLV("Aux FM Volume",
Liam Girdwood1f71a3b2011-03-28 19:23:23 +01001049 TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001050
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001051 /* Playback gains */
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001052 SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001053 TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001054 SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001055 TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -05001056 SOC_SINGLE_TLV("Earphone Playback Volume",
1057 TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -06001058
Peter Ujfalusi7cca6062011-06-27 13:33:14 +03001059 SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
Misael Lopez Cruz6bba63b2011-02-11 17:51:05 -06001060 twl6040_headset_power_get_enum,
1061 twl6040_headset_power_put_enum),
Peter Ujfalusiaf958c72011-06-27 17:03:14 +03001062
1063 SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
1064 twl6040_pll_get_enum, twl6040_pll_put_enum),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001065};
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"),
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -05001080 SND_SOC_DAPM_OUTPUT("EP"),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001081
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
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001094 /* 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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001100 /* 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 */
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -05001117 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),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001125 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
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001134 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),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001143
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -05001144 /* Analog playback drivers */
Olaya, Margaritaf769bdf2010-12-20 10:39:20 -06001145 SND_SOC_DAPM_OUT_DRV_E("Handsfree Left Driver",
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -05001146 TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001147 pga_event,
1148 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Olaya, Margaritaf769bdf2010-12-20 10:39:20 -06001149 SND_SOC_DAPM_OUT_DRV_E("Handsfree Right Driver",
Jorge Eduardo Candelaria0fad4ed2010-07-15 11:38:01 -05001150 TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001151 pga_event,
1152 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Olaya, Margaritaf769bdf2010-12-20 10:39:20 -06001153 SND_SOC_DAPM_OUT_DRV_E("Headset Left Driver",
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001154 TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1155 pga_event,
1156 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Olaya, Margaritaf769bdf2010-12-20 10:39:20 -06001157 SND_SOC_DAPM_OUT_DRV_E("Headset Right Driver",
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001158 TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1159 pga_event,
1160 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -05001161 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),
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001165
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
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001190 /* AFM path */
1191 {"AFMAmpL", "NULL", "AFML"},
1192 {"AFMAmpR", "NULL", "AFMR"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001193
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001194 {"HS Left Playback", "HS DAC", "HSDAC Left"},
1195 {"HS Left Playback", "Line-In amp", "AFMAmpL"},
1196
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"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001202
1203 {"HSOL", NULL, "Headset Left Driver"},
1204 {"HSOR", NULL, "Headset Right Driver"},
1205
Jorge Eduardo Candelaria871a05a2010-05-18 12:44:18 -05001206 /* Earphone playback path */
1207 {"Earphone Driver", "Switch", "HSDAC Left"},
1208 {"EP", NULL, "Earphone Driver"},
1209
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001210 {"HF Left Playback", "HF DAC", "HFDAC Left"},
1211 {"HF Left Playback", "Line-In amp", "AFMAmpL"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001212
Jorge Eduardo Candelaria370a0312010-12-10 21:05:32 -06001213 {"HF Right Playback", "HF DAC", "HFDAC Right"},
1214 {"HF Right Playback", "Line-In amp", "AFMAmpR"},
1215
1216 {"HFDAC Left PGA", NULL, "HF Left Playback"},
1217 {"HFDAC Right PGA", NULL, "HF Right Playback"},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001218
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{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001228 struct snd_soc_dapm_context *dapm = &codec->dapm;
1229
1230 snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001231 ARRAY_SIZE(twl6040_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001232 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
1233 snd_soc_dapm_new_widgets(dapm);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001234
1235 return 0;
1236}
1237
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001238static int twl6040_set_bias_level(struct snd_soc_codec *codec,
1239 enum snd_soc_bias_level level)
1240{
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001241 struct twl6040 *twl6040 = codec->control_data;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02001242 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001243 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
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001254 ret = twl6040_power(twl6040, 1);
1255 if (ret)
1256 return ret;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001257
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001258 priv->codec_powered = 1;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001259
Peter Ujfalusia52762e2011-09-15 15:39:27 +03001260 twl6040_restore_regs(codec);
Olaya, Margarita65b7cec2010-12-14 19:18:36 -06001261
1262 /* Set external boost GPO */
1263 twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001264 break;
1265 case SND_SOC_BIAS_OFF:
1266 if (!priv->codec_powered)
1267 break;
1268
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001269 twl6040_power(twl6040, 0);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001270 priv->codec_powered = 0;
1271 break;
1272 }
1273
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001274 codec->dapm.bias_level = level;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001275
1276 return 0;
1277}
1278
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001279static 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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283 struct snd_soc_codec *codec = rtd->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02001284 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001285
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001286 snd_pcm_hw_constraint_list(substream->runtime, 0,
1287 SNDRV_PCM_HW_PARAM_RATE,
Peter Ujfalusif53c3462011-06-29 13:28:18 +03001288 &sysclk_constraints[priv->pll_power_mode]);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001289
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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 struct snd_soc_codec *codec = rtd->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02001299 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001300 int rate;
1301
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001302 rate = params_rate(params);
1303 switch (rate) {
Olaya, Margarita60ea4ce2010-12-10 21:05:58 -06001304 case 11250:
1305 case 22500:
1306 case 44100:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001307 case 88200:
Peter Ujfalusi753621c2011-07-03 02:06:07 +03001308 /* 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 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001321 priv->sysclk = 17640000;
1322 break;
Olaya, Margarita60ea4ce2010-12-10 21:05:58 -06001323 case 8000:
1324 case 16000:
1325 case 32000:
1326 case 48000:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001327 case 96000:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001328 priv->sysclk = 19200000;
1329 break;
1330 default:
1331 dev_err(codec->dev, "unsupported rate %d\n", rate);
1332 return -EINVAL;
1333 }
1334
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001335 return 0;
1336}
1337
Olaya, Margarita4e624d02010-12-10 21:05:54 -06001338static int twl6040_prepare(struct snd_pcm_substream *substream,
1339 struct snd_soc_dai *dai)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001340{
1341 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001342 struct snd_soc_codec *codec = rtd->codec;
Peter Ujfalusi753621c2011-07-03 02:06:07 +03001343 struct twl6040 *twl6040 = codec->control_data;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02001344 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi753621c2011-07-03 02:06:07 +03001345 int ret;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001346
Olaya, Margarita4e624d02010-12-10 21:05:54 -06001347 if (!priv->sysclk) {
1348 dev_err(codec->dev,
1349 "no mclk configured, call set_sysclk() on init\n");
1350 return -EINVAL;
1351 }
1352
Olaya, Margarita4e624d02010-12-10 21:05:54 -06001353 if ((priv->sysclk == 17640000) && priv->non_lp) {
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001354 dev_err(codec->dev,
1355 "some enabled paths aren't supported at %dHz\n",
1356 priv->sysclk);
1357 return -EPERM;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001358 }
Peter Ujfalusi753621c2011-07-03 02:06:07 +03001359
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
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001366 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;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02001373 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001374
1375 switch (clk_id) {
1376 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001377 case TWL6040_SYSCLK_SEL_HPPLL:
Peter Ujfalusi753621c2011-07-03 02:06:07 +03001378 priv->pll = clk_id;
1379 priv->clk_in = freq;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001380 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,
Olaya, Margarita4e624d02010-12-10 21:05:54 -06001392 .prepare = twl6040_prepare,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001393 .set_sysclk = twl6040_set_dai_sysclk,
1394};
1395
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001396static struct snd_soc_dai_driver twl6040_dai[] = {
1397{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001398 .name = "twl6040-hifi",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001399 .playback = {
1400 .stream_name = "Playback",
1401 .channels_min = 1,
Peter Ujfalusicdd50542011-09-15 15:59:19 +03001402 .channels_max = 5,
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001403 .rates = TWL6040_RATES,
1404 .formats = TWL6040_FORMATS,
1405 },
1406 .capture = {
1407 .stream_name = "Capture",
1408 .channels_min = 1,
1409 .channels_max = 2,
1410 .rates = TWL6040_RATES,
1411 .formats = TWL6040_FORMATS,
1412 },
1413 .ops = &twl6040_dai_ops,
Peter Ujfalusi21385ee2011-07-05 22:35:53 +03001414},
1415{
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001416 .name = "twl6040-ul",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001417 .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,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001425},
1426{
1427 .name = "twl6040-dl1",
1428 .playback = {
1429 .stream_name = "Headset Playback",
1430 .channels_min = 1,
1431 .channels_max = 2,
1432 .rates = TWL6040_RATES,
1433 .formats = TWL6040_FORMATS,
1434 },
1435 .ops = &twl6040_dai_ops,
1436},
1437{
1438 .name = "twl6040-dl2",
1439 .playback = {
1440 .stream_name = "Handsfree Playback",
1441 .channels_min = 1,
1442 .channels_max = 2,
1443 .rates = TWL6040_RATES,
1444 .formats = TWL6040_FORMATS,
1445 },
1446 .ops = &twl6040_dai_ops,
1447},
1448{
1449 .name = "twl6040-vib",
1450 .playback = {
1451 .stream_name = "Vibra Playback",
Peter Ujfalusid8dd0322011-09-15 15:59:18 +03001452 .channels_min = 1,
1453 .channels_max = 1,
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001454 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1455 .formats = TWL6040_FORMATS,
1456 },
1457 .ops = &twl6040_dai_ops,
1458},
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001459};
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001460
1461#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001462static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001463{
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001464 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1465
1466 return 0;
1467}
1468
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001469static int twl6040_resume(struct snd_soc_codec *codec)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001470{
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001471 twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Olaya, Margarita6c311042010-12-10 21:05:46 -06001472 twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001473
1474 return 0;
1475}
1476#else
1477#define twl6040_suspend NULL
1478#define twl6040_resume NULL
1479#endif
1480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001481static int twl6040_probe(struct snd_soc_codec *codec)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001482{
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001483 struct twl6040_data *priv;
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -06001484 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001485 struct platform_device *pdev = container_of(codec->dev,
1486 struct platform_device, dev);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001487 int ret = 0;
1488
1489 priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
1490 if (priv == NULL)
1491 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492 snd_soc_codec_set_drvdata(codec, priv);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001493
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001494 priv->codec = codec;
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001495 codec->control_data = dev_get_drvdata(codec->dev->parent);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001496
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -06001497 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 }
Olaya, Margarita99903ea2010-12-10 21:06:07 -06001504
Axel Castaneda Gonzalez1fbe9952011-02-23 20:08:28 -06001505 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 }
Olaya, Margarita99903ea2010-12-10 21:06:07 -06001512
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001513 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 }
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001519
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001520 priv->workqueue = create_singlethread_workqueue("twl6040-codec");
Axel Lin19aab082011-03-26 15:53:58 +08001521 if (!priv->workqueue) {
1522 ret = -ENOMEM;
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001523 goto work_err;
Axel Lin19aab082011-03-26 15:53:58 +08001524 }
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001525
1526 INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
1527
1528 mutex_init(&priv->mutex);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001529
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001530 init_completion(&priv->headset.ramp_done);
1531 init_completion(&priv->handsfree.ramp_done);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001532
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001533 priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
1534 if (priv->hf_workqueue == NULL) {
1535 ret = -ENOMEM;
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001536 goto hfwq_err;
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001537 }
1538 priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
1539 if (priv->hs_workqueue == NULL) {
1540 ret = -ENOMEM;
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001541 goto hswq_err;
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001542 }
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
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001547 ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
1548 0, "twl6040_irq_plug", codec);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001549 if (ret) {
1550 dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1551 goto plugirq_err;
1552 }
1553
Peter Ujfalusia52762e2011-09-15 15:39:27 +03001554 twl6040_init_chip(codec);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001555
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001556 /* power on device */
1557 ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1558 if (ret)
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001559 goto bias_err;
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001561 snd_soc_add_controls(codec, twl6040_snd_controls,
1562 ARRAY_SIZE(twl6040_snd_controls));
1563 twl6040_add_widgets(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001564
1565 return 0;
1566
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001567bias_err:
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001568 free_irq(priv->plug_irq, codec);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001569plugirq_err:
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001570 destroy_workqueue(priv->hs_workqueue);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001571hswq_err:
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001572 destroy_workqueue(priv->hf_workqueue);
Misael Lopez Cruzfb34d3d2011-05-01 21:27:00 -05001573hfwq_err:
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001574 destroy_workqueue(priv->workqueue);
1575work_err:
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001576 kfree(priv);
1577 return ret;
1578}
1579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580static int twl6040_remove(struct snd_soc_codec *codec)
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001581{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582 struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001584 twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
Peter Ujfalusi2a433b92011-07-04 19:52:26 +03001585 free_irq(priv->plug_irq, codec);
Jorge Eduardo Candelariaa2d23622010-12-10 20:45:17 -06001586 destroy_workqueue(priv->workqueue);
Margarita Olaya Cabrera1bf84752010-12-14 19:00:21 -06001587 destroy_workqueue(priv->hf_workqueue);
1588 destroy_workqueue(priv->hs_workqueue);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001589 kfree(priv);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591 return 0;
1592}
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001594static 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{
Liam Girdwood6510bdc2011-02-11 17:37:51 +00001609 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
1610 twl6040_dai, ARRAY_SIZE(twl6040_dai));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001611}
1612
1613static int __devexit twl6040_codec_remove(struct platform_device *pdev)
1614{
1615 snd_soc_unregister_codec(&pdev->dev);
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001616 return 0;
1617}
1618
1619static struct platform_driver twl6040_codec_driver = {
1620 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621 .name = "twl6040-codec",
Misael Lopez Cruz8ecbabd2010-03-19 11:25:51 +00001622 .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");