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