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