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> |
fb34d3d5 | 29 | #include <linux/mfd/twl6040.h> |
8ecbabd9 MLC |
30 | |
31 | #include <sound/core.h> | |
32 | #include <sound/pcm.h> | |
33 | #include <sound/pcm_params.h> | |
34 | #include <sound/soc.h> | |
e48b46ba | 35 | #include <sound/soc-dapm.h> |
8ecbabd9 MLC |
36 | #include <sound/initval.h> |
37 | #include <sound/tlv.h> | |
38 | ||
39 | #include "twl6040.h" | |
40 | ||
68897497 PU |
41 | enum twl6040_dai_id { |
42 | TWL6040_DAI_LEGACY = 0, | |
43 | TWL6040_DAI_UL, | |
44 | TWL6040_DAI_DL1, | |
45 | TWL6040_DAI_DL2, | |
46 | TWL6040_DAI_VIB, | |
47 | }; | |
48 | ||
60ea4cec | 49 | #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000 |
1bf84759 MOC |
50 | #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE) |
51 | ||
52 | #define TWL6040_OUTHS_0dB 0x00 | |
53 | #define TWL6040_OUTHS_M30dB 0x0F | |
54 | #define TWL6040_OUTHF_0dB 0x03 | |
55 | #define TWL6040_OUTHF_M52dB 0x1D | |
56 | ||
d17bf318 PU |
57 | /* Shadow register used by the driver */ |
58 | #define TWL6040_REG_SW_SHADOW 0x2F | |
59 | #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1) | |
60 | ||
317596a6 PU |
61 | /* TWL6040_REG_SW_SHADOW (0x2F) fields */ |
62 | #define TWL6040_EAR_PATH_ENABLE 0x01 | |
63 | ||
a2d2362e JEC |
64 | struct twl6040_jack_data { |
65 | struct snd_soc_jack *jack; | |
46dd0b93 | 66 | struct delayed_work work; |
a2d2362e JEC |
67 | int report; |
68 | }; | |
69 | ||
8ecbabd9 MLC |
70 | /* codec private data */ |
71 | struct twl6040_data { | |
2a433b9d | 72 | int plug_irq; |
8ecbabd9 MLC |
73 | int codec_powered; |
74 | int pll; | |
af958c72 | 75 | int pll_power_mode; |
6bba63b6 MLC |
76 | int hs_power_mode; |
77 | int hs_power_mode_locked; | |
fb34d3d5 | 78 | unsigned int clk_in; |
8ecbabd9 | 79 | unsigned int sysclk; |
a2d2362e JEC |
80 | struct twl6040_jack_data hs_jack; |
81 | struct snd_soc_codec *codec; | |
a2d2362e | 82 | struct mutex mutex; |
8ecbabd9 MLC |
83 | }; |
84 | ||
85 | /* | |
86 | * twl6040 register cache & default register settings | |
87 | */ | |
88 | static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = { | |
4548dc3c PU |
89 | 0x00, /* not used 0x00 */ |
90 | 0x4B, /* REG_ASICID 0x01 (ro) */ | |
91 | 0x00, /* REG_ASICREV 0x02 (ro) */ | |
92 | 0x00, /* REG_INTID 0x03 */ | |
93 | 0x00, /* REG_INTMR 0x04 */ | |
94 | 0x00, /* REG_NCPCTRL 0x05 */ | |
95 | 0x00, /* REG_LDOCTL 0x06 */ | |
96 | 0x60, /* REG_HPPLLCTL 0x07 */ | |
97 | 0x00, /* REG_LPPLLCTL 0x08 */ | |
98 | 0x4A, /* REG_LPPLLDIV 0x09 */ | |
99 | 0x00, /* REG_AMICBCTL 0x0A */ | |
100 | 0x00, /* REG_DMICBCTL 0x0B */ | |
101 | 0x00, /* REG_MICLCTL 0x0C */ | |
102 | 0x00, /* REG_MICRCTL 0x0D */ | |
103 | 0x00, /* REG_MICGAIN 0x0E */ | |
104 | 0x1B, /* REG_LINEGAIN 0x0F */ | |
105 | 0x00, /* REG_HSLCTL 0x10 */ | |
106 | 0x00, /* REG_HSRCTL 0x11 */ | |
107 | 0x00, /* REG_HSGAIN 0x12 */ | |
108 | 0x00, /* REG_EARCTL 0x13 */ | |
109 | 0x00, /* REG_HFLCTL 0x14 */ | |
110 | 0x00, /* REG_HFLGAIN 0x15 */ | |
111 | 0x00, /* REG_HFRCTL 0x16 */ | |
112 | 0x00, /* REG_HFRGAIN 0x17 */ | |
113 | 0x00, /* REG_VIBCTLL 0x18 */ | |
114 | 0x00, /* REG_VIBDATL 0x19 */ | |
115 | 0x00, /* REG_VIBCTLR 0x1A */ | |
116 | 0x00, /* REG_VIBDATR 0x1B */ | |
117 | 0x00, /* REG_HKCTL1 0x1C */ | |
118 | 0x00, /* REG_HKCTL2 0x1D */ | |
119 | 0x00, /* REG_GPOCTL 0x1E */ | |
120 | 0x00, /* REG_ALB 0x1F */ | |
121 | 0x00, /* REG_DLB 0x20 */ | |
122 | 0x00, /* not used 0x21 */ | |
123 | 0x00, /* not used 0x22 */ | |
124 | 0x00, /* not used 0x23 */ | |
125 | 0x00, /* not used 0x24 */ | |
126 | 0x00, /* not used 0x25 */ | |
127 | 0x00, /* not used 0x26 */ | |
128 | 0x00, /* not used 0x27 */ | |
129 | 0x00, /* REG_TRIM1 0x28 */ | |
130 | 0x00, /* REG_TRIM2 0x29 */ | |
131 | 0x00, /* REG_TRIM3 0x2A */ | |
132 | 0x00, /* REG_HSOTRIM 0x2B */ | |
133 | 0x00, /* REG_HFOTRIM 0x2C */ | |
134 | 0x09, /* REG_ACCCTL 0x2D */ | |
135 | 0x00, /* REG_STATUS 0x2E (ro) */ | |
d17bf318 PU |
136 | |
137 | 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */ | |
8ecbabd9 MLC |
138 | }; |
139 | ||
a52762ee PU |
140 | /* List of registers to be restored after power up */ |
141 | static const int twl6040_restore_list[] = { | |
8ecbabd9 MLC |
142 | TWL6040_REG_MICLCTL, |
143 | TWL6040_REG_MICRCTL, | |
144 | TWL6040_REG_MICGAIN, | |
145 | TWL6040_REG_LINEGAIN, | |
146 | TWL6040_REG_HSLCTL, | |
147 | TWL6040_REG_HSRCTL, | |
148 | TWL6040_REG_HSGAIN, | |
149 | TWL6040_REG_EARCTL, | |
150 | TWL6040_REG_HFLCTL, | |
151 | TWL6040_REG_HFLGAIN, | |
152 | TWL6040_REG_HFRCTL, | |
153 | TWL6040_REG_HFRGAIN, | |
8ecbabd9 MLC |
154 | }; |
155 | ||
af958c72 PU |
156 | /* set of rates for each pll: low-power and high-performance */ |
157 | static unsigned int lp_rates[] = { | |
158 | 8000, | |
159 | 11250, | |
160 | 16000, | |
161 | 22500, | |
162 | 32000, | |
163 | 44100, | |
164 | 48000, | |
165 | 88200, | |
166 | 96000, | |
167 | }; | |
168 | ||
af958c72 PU |
169 | static unsigned int hp_rates[] = { |
170 | 8000, | |
171 | 16000, | |
172 | 32000, | |
173 | 48000, | |
174 | 96000, | |
175 | }; | |
176 | ||
f53c346c PU |
177 | static struct snd_pcm_hw_constraint_list sysclk_constraints[] = { |
178 | { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, }, | |
179 | { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, }, | |
af958c72 PU |
180 | }; |
181 | ||
8ecbabd9 MLC |
182 | /* |
183 | * read twl6040 register cache | |
184 | */ | |
185 | static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec, | |
186 | unsigned int reg) | |
187 | { | |
188 | u8 *cache = codec->reg_cache; | |
189 | ||
190 | if (reg >= TWL6040_CACHEREGNUM) | |
191 | return -EIO; | |
192 | ||
193 | return cache[reg]; | |
194 | } | |
195 | ||
196 | /* | |
197 | * write twl6040 register cache | |
198 | */ | |
199 | static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec, | |
200 | u8 reg, u8 value) | |
201 | { | |
202 | u8 *cache = codec->reg_cache; | |
203 | ||
204 | if (reg >= TWL6040_CACHEREGNUM) | |
205 | return; | |
206 | cache[reg] = value; | |
207 | } | |
208 | ||
209 | /* | |
210 | * read from twl6040 hardware register | |
211 | */ | |
212 | static int twl6040_read_reg_volatile(struct snd_soc_codec *codec, | |
213 | unsigned int reg) | |
214 | { | |
fb34d3d5 | 215 | struct twl6040 *twl6040 = codec->control_data; |
8ecbabd9 MLC |
216 | u8 value; |
217 | ||
218 | if (reg >= TWL6040_CACHEREGNUM) | |
219 | return -EIO; | |
220 | ||
d17bf318 PU |
221 | if (likely(reg < TWL6040_REG_SW_SHADOW)) { |
222 | value = twl6040_reg_read(twl6040, reg); | |
223 | twl6040_write_reg_cache(codec, reg, value); | |
224 | } else { | |
225 | value = twl6040_read_reg_cache(codec, reg); | |
226 | } | |
8ecbabd9 MLC |
227 | |
228 | return value; | |
229 | } | |
230 | ||
231 | /* | |
232 | * write to the twl6040 register space | |
233 | */ | |
234 | static int twl6040_write(struct snd_soc_codec *codec, | |
235 | unsigned int reg, unsigned int value) | |
236 | { | |
fb34d3d5 MLC |
237 | struct twl6040 *twl6040 = codec->control_data; |
238 | ||
8ecbabd9 MLC |
239 | if (reg >= TWL6040_CACHEREGNUM) |
240 | return -EIO; | |
241 | ||
242 | twl6040_write_reg_cache(codec, reg, value); | |
d17bf318 PU |
243 | if (likely(reg < TWL6040_REG_SW_SHADOW)) |
244 | return twl6040_reg_write(twl6040, reg, value); | |
245 | else | |
246 | return 0; | |
8ecbabd9 MLC |
247 | } |
248 | ||
a52762ee | 249 | static void twl6040_init_chip(struct snd_soc_codec *codec) |
8ecbabd9 | 250 | { |
a52762ee PU |
251 | struct twl6040 *twl6040 = codec->control_data; |
252 | u8 val; | |
253 | ||
f97217f1 | 254 | /* Update reg_cache: ASICREV, and TRIM values */ |
a52762ee PU |
255 | val = twl6040_get_revid(twl6040); |
256 | twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val); | |
8ecbabd9 | 257 | |
f97217f1 PU |
258 | twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1); |
259 | twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2); | |
260 | twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3); | |
261 | twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM); | |
262 | twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM); | |
263 | ||
2c27ff41 PU |
264 | /* Change chip defaults */ |
265 | /* No imput selected for microphone amplifiers */ | |
266 | twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18); | |
267 | twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18); | |
3acef685 PU |
268 | |
269 | /* | |
270 | * We need to lower the default gain values, so the ramp code | |
271 | * can work correctly for the first playback. | |
272 | * This reduces the pop noise heard at the first playback. | |
273 | */ | |
274 | twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff); | |
275 | twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e); | |
276 | twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d); | |
277 | twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d); | |
278 | twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0); | |
8ecbabd9 MLC |
279 | } |
280 | ||
a52762ee | 281 | static void twl6040_restore_regs(struct snd_soc_codec *codec) |
8ecbabd9 MLC |
282 | { |
283 | u8 *cache = codec->reg_cache; | |
284 | int reg, i; | |
285 | ||
a52762ee PU |
286 | for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) { |
287 | reg = twl6040_restore_list[i]; | |
8ecbabd9 MLC |
288 | twl6040_write(codec, reg, cache[reg]); |
289 | } | |
290 | } | |
291 | ||
8ecbabd9 MLC |
292 | /* set headset dac and driver power mode */ |
293 | static int headset_power_mode(struct snd_soc_codec *codec, int high_perf) | |
294 | { | |
295 | int hslctl, hsrctl; | |
ab6cf139 | 296 | int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE; |
8ecbabd9 MLC |
297 | |
298 | hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); | |
299 | hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); | |
300 | ||
301 | if (high_perf) { | |
302 | hslctl &= ~mask; | |
303 | hsrctl &= ~mask; | |
304 | } else { | |
305 | hslctl |= mask; | |
306 | hsrctl |= mask; | |
307 | } | |
308 | ||
309 | twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl); | |
310 | twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl); | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
0fad4ed7 JEC |
315 | static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w, |
316 | struct snd_kcontrol *kcontrol, int event) | |
317 | { | |
33b6816c PU |
318 | struct snd_soc_codec *codec = w->codec; |
319 | u8 hslctl, hsrctl; | |
320 | ||
321 | /* | |
322 | * Workaround for Headset DC offset caused pop noise: | |
323 | * Both HS DAC need to be turned on (before the HS driver) and off at | |
324 | * the same time. | |
325 | */ | |
326 | hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL); | |
327 | hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL); | |
328 | if (SND_SOC_DAPM_EVENT_ON(event)) { | |
329 | hslctl |= TWL6040_HSDACENA; | |
330 | hsrctl |= TWL6040_HSDACENA; | |
331 | } else { | |
332 | hslctl &= ~TWL6040_HSDACENA; | |
333 | hsrctl &= ~TWL6040_HSDACENA; | |
334 | } | |
335 | twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl); | |
336 | twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl); | |
337 | ||
0fad4ed7 JEC |
338 | msleep(1); |
339 | return 0; | |
340 | } | |
341 | ||
694b0001 | 342 | static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w, |
8ecbabd9 MLC |
343 | struct snd_kcontrol *kcontrol, int event) |
344 | { | |
345 | struct snd_soc_codec *codec = w->codec; | |
d4a8ca24 | 346 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
6bba63b6 | 347 | int ret = 0; |
8ecbabd9 | 348 | |
6bba63b6 | 349 | if (SND_SOC_DAPM_EVENT_ON(event)) { |
fac2f3e4 PU |
350 | /* Earphone doesn't support low power mode */ |
351 | priv->hs_power_mode_locked = 1; | |
352 | ret = headset_power_mode(codec, 1); | |
6bba63b6 | 353 | } else { |
fac2f3e4 PU |
354 | priv->hs_power_mode_locked = 0; |
355 | ret = headset_power_mode(codec, priv->hs_power_mode); | |
6bba63b6 | 356 | } |
8ecbabd9 | 357 | |
0fad4ed7 JEC |
358 | msleep(1); |
359 | ||
6bba63b6 | 360 | return ret; |
8ecbabd9 MLC |
361 | } |
362 | ||
64ed9836 MB |
363 | static void twl6040_hs_jack_report(struct snd_soc_codec *codec, |
364 | struct snd_soc_jack *jack, int report) | |
a2d2362e JEC |
365 | { |
366 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
367 | int status; | |
368 | ||
369 | mutex_lock(&priv->mutex); | |
370 | ||
371 | /* Sync status */ | |
372 | status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS); | |
373 | if (status & TWL6040_PLUGCOMP) | |
374 | snd_soc_jack_report(jack, report, report); | |
375 | else | |
376 | snd_soc_jack_report(jack, 0, report); | |
377 | ||
378 | mutex_unlock(&priv->mutex); | |
379 | } | |
380 | ||
381 | void twl6040_hs_jack_detect(struct snd_soc_codec *codec, | |
382 | struct snd_soc_jack *jack, int report) | |
383 | { | |
384 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
385 | struct twl6040_jack_data *hs_jack = &priv->hs_jack; | |
386 | ||
387 | hs_jack->jack = jack; | |
388 | hs_jack->report = report; | |
389 | ||
390 | twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report); | |
391 | } | |
392 | EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect); | |
393 | ||
394 | static void twl6040_accessory_work(struct work_struct *work) | |
395 | { | |
396 | struct twl6040_data *priv = container_of(work, | |
46dd0b93 | 397 | struct twl6040_data, hs_jack.work.work); |
a2d2362e JEC |
398 | struct snd_soc_codec *codec = priv->codec; |
399 | struct twl6040_jack_data *hs_jack = &priv->hs_jack; | |
400 | ||
401 | twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report); | |
402 | } | |
403 | ||
8ecbabd9 | 404 | /* audio interrupt handler */ |
fb34d3d5 | 405 | static irqreturn_t twl6040_audio_handler(int irq, void *data) |
8ecbabd9 MLC |
406 | { |
407 | struct snd_soc_codec *codec = data; | |
d4a8ca24 | 408 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
cf370a5a | 409 | |
da2107d1 | 410 | schedule_delayed_work(&priv->hs_jack.work, msecs_to_jiffies(200)); |
cf370a5a | 411 | |
8ecbabd9 MLC |
412 | return IRQ_HANDLED; |
413 | } | |
414 | ||
67c34130 PU |
415 | static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol, |
416 | struct snd_ctl_elem_value *ucontrol) | |
417 | { | |
418 | struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol); | |
419 | struct snd_soc_dapm_widget *widget = wlist->widgets[0]; | |
420 | struct snd_soc_codec *codec = widget->codec; | |
421 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
422 | unsigned int val; | |
423 | ||
424 | /* Do not allow changes while Input/FF efect is running */ | |
425 | val = twl6040_read_reg_volatile(codec, e->reg); | |
426 | if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL)) | |
427 | return -EBUSY; | |
428 | ||
429 | return snd_soc_dapm_put_enum_double(kcontrol, ucontrol); | |
430 | } | |
431 | ||
8ecbabd9 MLC |
432 | /* |
433 | * MICATT volume control: | |
434 | * from -6 to 0 dB in 6 dB steps | |
435 | */ | |
436 | static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0); | |
437 | ||
438 | /* | |
439 | * MICGAIN volume control: | |
2763f45d | 440 | * from 6 to 30 dB in 6 dB steps |
8ecbabd9 | 441 | */ |
2763f45d | 442 | static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0); |
8ecbabd9 | 443 | |
370a0314 JEC |
444 | /* |
445 | * AFMGAIN volume control: | |
1f71a3ba | 446 | * from -18 to 24 dB in 6 dB steps |
370a0314 | 447 | */ |
1f71a3ba | 448 | static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0); |
370a0314 | 449 | |
8ecbabd9 MLC |
450 | /* |
451 | * HSGAIN volume control: | |
452 | * from -30 to 0 dB in 2 dB steps | |
453 | */ | |
454 | static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0); | |
455 | ||
456 | /* | |
457 | * HFGAIN volume control: | |
458 | * from -52 to 6 dB in 2 dB steps | |
459 | */ | |
460 | static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0); | |
461 | ||
871a05a7 JEC |
462 | /* |
463 | * EPGAIN volume control: | |
464 | * from -24 to 6 dB in 2 dB steps | |
465 | */ | |
466 | static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0); | |
467 | ||
8ecbabd9 MLC |
468 | /* Left analog microphone selection */ |
469 | static const char *twl6040_amicl_texts[] = | |
470 | {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"}; | |
471 | ||
472 | /* Right analog microphone selection */ | |
473 | static const char *twl6040_amicr_texts[] = | |
474 | {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"}; | |
475 | ||
476 | static const struct soc_enum twl6040_enum[] = { | |
cb973d78 FM |
477 | SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts), |
478 | SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts), | |
8ecbabd9 MLC |
479 | }; |
480 | ||
370a0314 JEC |
481 | static const char *twl6040_hs_texts[] = { |
482 | "Off", "HS DAC", "Line-In amp" | |
483 | }; | |
484 | ||
485 | static const struct soc_enum twl6040_hs_enum[] = { | |
486 | SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts), | |
487 | twl6040_hs_texts), | |
488 | SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts), | |
489 | twl6040_hs_texts), | |
490 | }; | |
491 | ||
492 | static const char *twl6040_hf_texts[] = { | |
493 | "Off", "HF DAC", "Line-In amp" | |
494 | }; | |
495 | ||
496 | static const struct soc_enum twl6040_hf_enum[] = { | |
497 | SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts), | |
498 | twl6040_hf_texts), | |
499 | SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts), | |
500 | twl6040_hf_texts), | |
501 | }; | |
502 | ||
67c34130 PU |
503 | static const char *twl6040_vibrapath_texts[] = { |
504 | "Input FF", "Audio PDM" | |
505 | }; | |
506 | ||
507 | static const struct soc_enum twl6040_vibra_enum[] = { | |
508 | SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1, | |
509 | ARRAY_SIZE(twl6040_vibrapath_texts), | |
510 | twl6040_vibrapath_texts), | |
511 | SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1, | |
512 | ARRAY_SIZE(twl6040_vibrapath_texts), | |
513 | twl6040_vibrapath_texts), | |
514 | }; | |
515 | ||
8ecbabd9 MLC |
516 | static const struct snd_kcontrol_new amicl_control = |
517 | SOC_DAPM_ENUM("Route", twl6040_enum[0]); | |
518 | ||
519 | static const struct snd_kcontrol_new amicr_control = | |
520 | SOC_DAPM_ENUM("Route", twl6040_enum[1]); | |
521 | ||
522 | /* Headset DAC playback switches */ | |
370a0314 JEC |
523 | static const struct snd_kcontrol_new hsl_mux_controls = |
524 | SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]); | |
8ecbabd9 | 525 | |
370a0314 JEC |
526 | static const struct snd_kcontrol_new hsr_mux_controls = |
527 | SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]); | |
8ecbabd9 MLC |
528 | |
529 | /* Handsfree DAC playback switches */ | |
370a0314 JEC |
530 | static const struct snd_kcontrol_new hfl_mux_controls = |
531 | SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]); | |
8ecbabd9 | 532 | |
370a0314 JEC |
533 | static const struct snd_kcontrol_new hfr_mux_controls = |
534 | SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]); | |
8ecbabd9 | 535 | |
317596a6 PU |
536 | static const struct snd_kcontrol_new ep_path_enable_control = |
537 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0); | |
871a05a7 | 538 | |
fdb625ff PU |
539 | static const struct snd_kcontrol_new auxl_switch_control = |
540 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0); | |
541 | ||
542 | static const struct snd_kcontrol_new auxr_switch_control = | |
543 | SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0); | |
544 | ||
67c34130 PU |
545 | /* Vibra playback switches */ |
546 | static const struct snd_kcontrol_new vibral_mux_controls = | |
547 | SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0], | |
548 | snd_soc_dapm_get_enum_double, | |
549 | twl6040_soc_dapm_put_vibra_enum); | |
550 | ||
551 | static const struct snd_kcontrol_new vibrar_mux_controls = | |
552 | SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1], | |
553 | snd_soc_dapm_get_enum_double, | |
554 | twl6040_soc_dapm_put_vibra_enum); | |
555 | ||
6bba63b6 | 556 | /* Headset power mode */ |
7cca6067 | 557 | static const char *twl6040_power_mode_texts[] = { |
3e453654 | 558 | "Low-Power", "High-Performance", |
6bba63b6 MLC |
559 | }; |
560 | ||
7cca6067 PU |
561 | static const struct soc_enum twl6040_power_mode_enum = |
562 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts), | |
563 | twl6040_power_mode_texts); | |
6bba63b6 MLC |
564 | |
565 | static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol, | |
566 | struct snd_ctl_elem_value *ucontrol) | |
567 | { | |
568 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
569 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
570 | ||
571 | ucontrol->value.enumerated.item[0] = priv->hs_power_mode; | |
572 | ||
573 | return 0; | |
574 | } | |
575 | ||
576 | static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol, | |
577 | struct snd_ctl_elem_value *ucontrol) | |
578 | { | |
579 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
580 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
581 | int high_perf = ucontrol->value.enumerated.item[0]; | |
582 | int ret = 0; | |
583 | ||
584 | if (!priv->hs_power_mode_locked) | |
585 | ret = headset_power_mode(codec, high_perf); | |
586 | ||
587 | if (!ret) | |
588 | priv->hs_power_mode = high_perf; | |
589 | ||
590 | return ret; | |
591 | } | |
592 | ||
af958c72 PU |
593 | static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol, |
594 | struct snd_ctl_elem_value *ucontrol) | |
595 | { | |
596 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
597 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
598 | ||
599 | ucontrol->value.enumerated.item[0] = priv->pll_power_mode; | |
600 | ||
601 | return 0; | |
602 | } | |
603 | ||
604 | static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol, | |
605 | struct snd_ctl_elem_value *ucontrol) | |
606 | { | |
607 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
608 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
609 | ||
610 | priv->pll_power_mode = ucontrol->value.enumerated.item[0]; | |
af958c72 PU |
611 | |
612 | return 0; | |
613 | } | |
614 | ||
e48b46ba LG |
615 | int twl6040_get_dl1_gain(struct snd_soc_codec *codec) |
616 | { | |
617 | struct snd_soc_dapm_context *dapm = &codec->dapm; | |
618 | ||
619 | if (snd_soc_dapm_get_pin_status(dapm, "EP")) | |
620 | return -1; /* -1dB */ | |
621 | ||
622 | if (snd_soc_dapm_get_pin_status(dapm, "HSOR") || | |
623 | snd_soc_dapm_get_pin_status(dapm, "HSOL")) { | |
624 | ||
625 | u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL); | |
626 | if (val & TWL6040_HSDACMODE) | |
627 | /* HSDACL in LP mode */ | |
628 | return -8; /* -8dB */ | |
629 | else | |
630 | /* HSDACL in HP mode */ | |
631 | return -1; /* -1dB */ | |
632 | } | |
633 | return 0; /* 0dB */ | |
634 | } | |
635 | EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain); | |
636 | ||
af958c72 PU |
637 | int twl6040_get_clk_id(struct snd_soc_codec *codec) |
638 | { | |
639 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); | |
640 | ||
ff593ca1 | 641 | return priv->pll_power_mode; |
af958c72 PU |
642 | } |
643 | EXPORT_SYMBOL_GPL(twl6040_get_clk_id); | |
644 | ||
db4aabcc PU |
645 | int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim) |
646 | { | |
647 | if (unlikely(trim >= TWL6040_TRIM_INVAL)) | |
648 | return -EINVAL; | |
649 | ||
650 | return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim); | |
651 | } | |
652 | EXPORT_SYMBOL_GPL(twl6040_get_trim_value); | |
653 | ||
08656910 LG |
654 | int twl6040_get_hs_step_size(struct snd_soc_codec *codec) |
655 | { | |
656 | struct twl6040 *twl6040 = codec->control_data; | |
657 | ||
be4ac00a | 658 | if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3) |
08656910 LG |
659 | /* For ES under ES_1.3 HS step is 2 mV */ |
660 | return 2; | |
661 | else | |
662 | /* For ES_1.3 HS step is 1 mV */ | |
663 | return 1; | |
664 | } | |
665 | EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size); | |
666 | ||
8ecbabd9 MLC |
667 | static const struct snd_kcontrol_new twl6040_snd_controls[] = { |
668 | /* Capture gains */ | |
669 | SOC_DOUBLE_TLV("Capture Preamplifier Volume", | |
670 | TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv), | |
671 | SOC_DOUBLE_TLV("Capture Volume", | |
672 | TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv), | |
673 | ||
370a0314 JEC |
674 | /* AFM gains */ |
675 | SOC_DOUBLE_TLV("Aux FM Volume", | |
1f71a3ba | 676 | TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv), |
370a0314 | 677 | |
8ecbabd9 | 678 | /* Playback gains */ |
3bb8a819 PU |
679 | SOC_DOUBLE_TLV("Headset Playback Volume", |
680 | TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv), | |
681 | SOC_DOUBLE_R_TLV("Handsfree Playback Volume", | |
682 | TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv), | |
871a05a7 JEC |
683 | SOC_SINGLE_TLV("Earphone Playback Volume", |
684 | TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv), | |
6bba63b6 | 685 | |
7cca6067 | 686 | SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum, |
6bba63b6 MLC |
687 | twl6040_headset_power_get_enum, |
688 | twl6040_headset_power_put_enum), | |
af958c72 PU |
689 | |
690 | SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum, | |
691 | twl6040_pll_get_enum, twl6040_pll_put_enum), | |
8ecbabd9 MLC |
692 | }; |
693 | ||
694 | static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = { | |
695 | /* Inputs */ | |
696 | SND_SOC_DAPM_INPUT("MAINMIC"), | |
697 | SND_SOC_DAPM_INPUT("HSMIC"), | |
698 | SND_SOC_DAPM_INPUT("SUBMIC"), | |
699 | SND_SOC_DAPM_INPUT("AFML"), | |
700 | SND_SOC_DAPM_INPUT("AFMR"), | |
701 | ||
702 | /* Outputs */ | |
703 | SND_SOC_DAPM_OUTPUT("HSOL"), | |
704 | SND_SOC_DAPM_OUTPUT("HSOR"), | |
705 | SND_SOC_DAPM_OUTPUT("HFL"), | |
706 | SND_SOC_DAPM_OUTPUT("HFR"), | |
871a05a7 | 707 | SND_SOC_DAPM_OUTPUT("EP"), |
fdb625ff PU |
708 | SND_SOC_DAPM_OUTPUT("AUXL"), |
709 | SND_SOC_DAPM_OUTPUT("AUXR"), | |
67c34130 PU |
710 | SND_SOC_DAPM_OUTPUT("VIBRAL"), |
711 | SND_SOC_DAPM_OUTPUT("VIBRAR"), | |
8ecbabd9 MLC |
712 | |
713 | /* Analog input muxes for the capture amplifiers */ | |
714 | SND_SOC_DAPM_MUX("Analog Left Capture Route", | |
715 | SND_SOC_NOPM, 0, 0, &amicl_control), | |
716 | SND_SOC_DAPM_MUX("Analog Right Capture Route", | |
717 | SND_SOC_NOPM, 0, 0, &amicr_control), | |
718 | ||
719 | /* Analog capture PGAs */ | |
720 | SND_SOC_DAPM_PGA("MicAmpL", | |
721 | TWL6040_REG_MICLCTL, 0, 0, NULL, 0), | |
722 | SND_SOC_DAPM_PGA("MicAmpR", | |
723 | TWL6040_REG_MICRCTL, 0, 0, NULL, 0), | |
724 | ||
370a0314 JEC |
725 | /* Auxiliary FM PGAs */ |
726 | SND_SOC_DAPM_PGA("AFMAmpL", | |
727 | TWL6040_REG_MICLCTL, 1, 0, NULL, 0), | |
728 | SND_SOC_DAPM_PGA("AFMAmpR", | |
729 | TWL6040_REG_MICRCTL, 1, 0, NULL, 0), | |
730 | ||
8ecbabd9 | 731 | /* ADCs */ |
805238b1 PU |
732 | SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0), |
733 | SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0), | |
8ecbabd9 MLC |
734 | |
735 | /* Microphone bias */ | |
778cee7a PU |
736 | SND_SOC_DAPM_SUPPLY("Headset Mic Bias", |
737 | TWL6040_REG_AMICBCTL, 0, 0, NULL, 0), | |
738 | SND_SOC_DAPM_SUPPLY("Main Mic Bias", | |
739 | TWL6040_REG_AMICBCTL, 4, 0, NULL, 0), | |
740 | SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias", | |
741 | TWL6040_REG_DMICBCTL, 0, 0, NULL, 0), | |
742 | SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias", | |
743 | TWL6040_REG_DMICBCTL, 4, 0, NULL, 0), | |
8ecbabd9 MLC |
744 | |
745 | /* DACs */ | |
805238b1 PU |
746 | SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0), |
747 | SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0), | |
748 | SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0), | |
749 | SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0), | |
67c34130 | 750 | /* Virtual DAC for vibra path (DL4 channel) */ |
805238b1 | 751 | SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0), |
8ecbabd9 | 752 | |
df11ce29 | 753 | SND_SOC_DAPM_MUX("Handsfree Left Playback", |
370a0314 | 754 | SND_SOC_NOPM, 0, 0, &hfl_mux_controls), |
df11ce29 | 755 | SND_SOC_DAPM_MUX("Handsfree Right Playback", |
370a0314 JEC |
756 | SND_SOC_NOPM, 0, 0, &hfr_mux_controls), |
757 | /* Analog playback Muxes */ | |
45b0f60d | 758 | SND_SOC_DAPM_MUX("Headset Left Playback", |
370a0314 | 759 | SND_SOC_NOPM, 0, 0, &hsl_mux_controls), |
45b0f60d | 760 | SND_SOC_DAPM_MUX("Headset Right Playback", |
370a0314 | 761 | SND_SOC_NOPM, 0, 0, &hsr_mux_controls), |
8ecbabd9 | 762 | |
67c34130 PU |
763 | SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0, |
764 | &vibral_mux_controls), | |
765 | SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0, | |
766 | &vibrar_mux_controls), | |
767 | ||
317596a6 PU |
768 | SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0, |
769 | &ep_path_enable_control), | |
fdb625ff PU |
770 | SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0, |
771 | &auxl_switch_control), | |
772 | SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0, | |
773 | &auxr_switch_control), | |
317596a6 | 774 | |
0fad4ed7 | 775 | /* Analog playback drivers */ |
3bb8a819 PU |
776 | SND_SOC_DAPM_OUT_DRV("HF Left Driver", |
777 | TWL6040_REG_HFLCTL, 4, 0, NULL, 0), | |
778 | SND_SOC_DAPM_OUT_DRV("HF Right Driver", | |
779 | TWL6040_REG_HFRCTL, 4, 0, NULL, 0), | |
780 | SND_SOC_DAPM_OUT_DRV("HS Left Driver", | |
781 | TWL6040_REG_HSLCTL, 2, 0, NULL, 0), | |
782 | SND_SOC_DAPM_OUT_DRV("HS Right Driver", | |
783 | TWL6040_REG_HSRCTL, 2, 0, NULL, 0), | |
317596a6 PU |
784 | SND_SOC_DAPM_OUT_DRV_E("Earphone Driver", |
785 | TWL6040_REG_EARCTL, 0, 0, NULL, 0, | |
694b0001 | 786 | twl6040_ep_drv_event, |
aa1a4108 | 787 | SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
67c34130 PU |
788 | SND_SOC_DAPM_OUT_DRV("Vibra Left Driver", |
789 | TWL6040_REG_VIBCTLL, 0, 0, NULL, 0), | |
790 | SND_SOC_DAPM_OUT_DRV("Vibra Right Driver", | |
791 | TWL6040_REG_VIBCTLR, 0, 0, NULL, 0), | |
792 | ||
793 | SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0, | |
794 | NULL, 0), | |
795 | SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0, | |
796 | NULL, 0), | |
33b6816c PU |
797 | SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0, |
798 | twl6040_hs_dac_event, | |
799 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), | |
8ecbabd9 MLC |
800 | |
801 | /* Analog playback PGAs */ | |
df11ce29 | 802 | SND_SOC_DAPM_PGA("HF Left PGA", |
8ecbabd9 | 803 | TWL6040_REG_HFLCTL, 1, 0, NULL, 0), |
df11ce29 | 804 | SND_SOC_DAPM_PGA("HF Right PGA", |
8ecbabd9 MLC |
805 | TWL6040_REG_HFRCTL, 1, 0, NULL, 0), |
806 | ||
807 | }; | |
808 | ||
809 | static const struct snd_soc_dapm_route intercon[] = { | |
805238b1 PU |
810 | /* Stream -> DAC mapping */ |
811 | {"HSDAC Left", NULL, "Legacy Playback"}, | |
812 | {"HSDAC Left", NULL, "Headset Playback"}, | |
813 | {"HSDAC Right", NULL, "Legacy Playback"}, | |
814 | {"HSDAC Right", NULL, "Headset Playback"}, | |
815 | ||
816 | {"HFDAC Left", NULL, "Legacy Playback"}, | |
817 | {"HFDAC Left", NULL, "Handsfree Playback"}, | |
818 | {"HFDAC Right", NULL, "Legacy Playback"}, | |
819 | {"HFDAC Right", NULL, "Handsfree Playback"}, | |
820 | ||
821 | {"VIBRA DAC", NULL, "Legacy Playback"}, | |
822 | {"VIBRA DAC", NULL, "Vibra Playback"}, | |
823 | ||
824 | /* ADC -> Stream mapping */ | |
07ac2296 PU |
825 | {"Legacy Capture" , NULL, "ADC Left"}, |
826 | {"Capture", NULL, "ADC Left"}, | |
827 | {"Legacy Capture", NULL, "ADC Right"}, | |
828 | {"Capture" , NULL, "ADC Right"}, | |
805238b1 | 829 | |
8ecbabd9 MLC |
830 | /* Capture path */ |
831 | {"Analog Left Capture Route", "Headset Mic", "HSMIC"}, | |
832 | {"Analog Left Capture Route", "Main Mic", "MAINMIC"}, | |
833 | {"Analog Left Capture Route", "Aux/FM Left", "AFML"}, | |
834 | ||
835 | {"Analog Right Capture Route", "Headset Mic", "HSMIC"}, | |
836 | {"Analog Right Capture Route", "Sub Mic", "SUBMIC"}, | |
837 | {"Analog Right Capture Route", "Aux/FM Right", "AFMR"}, | |
838 | ||
839 | {"MicAmpL", NULL, "Analog Left Capture Route"}, | |
840 | {"MicAmpR", NULL, "Analog Right Capture Route"}, | |
841 | ||
842 | {"ADC Left", NULL, "MicAmpL"}, | |
843 | {"ADC Right", NULL, "MicAmpR"}, | |
844 | ||
370a0314 | 845 | /* AFM path */ |
5bf692d9 PU |
846 | {"AFMAmpL", NULL, "AFML"}, |
847 | {"AFMAmpR", NULL, "AFMR"}, | |
370a0314 | 848 | |
33b6816c PU |
849 | {"HSDAC Left", NULL, "HSDAC Power"}, |
850 | {"HSDAC Right", NULL, "HSDAC Power"}, | |
851 | ||
45b0f60d PU |
852 | {"Headset Left Playback", "HS DAC", "HSDAC Left"}, |
853 | {"Headset Left Playback", "Line-In amp", "AFMAmpL"}, | |
8ecbabd9 | 854 | |
45b0f60d PU |
855 | {"Headset Right Playback", "HS DAC", "HSDAC Right"}, |
856 | {"Headset Right Playback", "Line-In amp", "AFMAmpR"}, | |
370a0314 | 857 | |
45b0f60d PU |
858 | {"HS Left Driver", NULL, "Headset Left Playback"}, |
859 | {"HS Right Driver", NULL, "Headset Right Playback"}, | |
8ecbabd9 | 860 | |
45b0f60d PU |
861 | {"HSOL", NULL, "HS Left Driver"}, |
862 | {"HSOR", NULL, "HS Right Driver"}, | |
8ecbabd9 | 863 | |
871a05a7 | 864 | /* Earphone playback path */ |
317596a6 PU |
865 | {"Earphone Playback", "Switch", "HSDAC Left"}, |
866 | {"Earphone Driver", NULL, "Earphone Playback"}, | |
871a05a7 JEC |
867 | {"EP", NULL, "Earphone Driver"}, |
868 | ||
df11ce29 PU |
869 | {"Handsfree Left Playback", "HF DAC", "HFDAC Left"}, |
870 | {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"}, | |
370a0314 | 871 | |
df11ce29 PU |
872 | {"Handsfree Right Playback", "HF DAC", "HFDAC Right"}, |
873 | {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"}, | |
8ecbabd9 | 874 | |
df11ce29 PU |
875 | {"HF Left PGA", NULL, "Handsfree Left Playback"}, |
876 | {"HF Right PGA", NULL, "Handsfree Right Playback"}, | |
8ecbabd9 | 877 | |
df11ce29 PU |
878 | {"HF Left Driver", NULL, "HF Left PGA"}, |
879 | {"HF Right Driver", NULL, "HF Right PGA"}, | |
8ecbabd9 | 880 | |
df11ce29 PU |
881 | {"HFL", NULL, "HF Left Driver"}, |
882 | {"HFR", NULL, "HF Right Driver"}, | |
fdb625ff PU |
883 | |
884 | {"AUXL Playback", "Switch", "HF Left PGA"}, | |
885 | {"AUXR Playback", "Switch", "HF Right PGA"}, | |
886 | ||
887 | {"AUXL", NULL, "AUXL Playback"}, | |
888 | {"AUXR", NULL, "AUXR Playback"}, | |
67c34130 PU |
889 | |
890 | /* Vibrator paths */ | |
891 | {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"}, | |
892 | {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"}, | |
893 | ||
894 | {"Vibra Left Driver", NULL, "Vibra Left Playback"}, | |
895 | {"Vibra Right Driver", NULL, "Vibra Right Playback"}, | |
896 | {"Vibra Left Driver", NULL, "Vibra Left Control"}, | |
897 | {"Vibra Right Driver", NULL, "Vibra Right Control"}, | |
898 | ||
899 | {"VIBRAL", NULL, "Vibra Left Driver"}, | |
900 | {"VIBRAR", NULL, "Vibra Right Driver"}, | |
8ecbabd9 MLC |
901 | }; |
902 | ||
8ecbabd9 MLC |
903 | static int twl6040_set_bias_level(struct snd_soc_codec *codec, |
904 | enum snd_soc_bias_level level) | |
905 | { | |
fb34d3d5 | 906 | struct twl6040 *twl6040 = codec->control_data; |
d4a8ca24 | 907 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
8ecbabd9 MLC |
908 | int ret; |
909 | ||
910 | switch (level) { | |
911 | case SND_SOC_BIAS_ON: | |
912 | break; | |
913 | case SND_SOC_BIAS_PREPARE: | |
914 | break; | |
915 | case SND_SOC_BIAS_STANDBY: | |
916 | if (priv->codec_powered) | |
917 | break; | |
918 | ||
fb34d3d5 MLC |
919 | ret = twl6040_power(twl6040, 1); |
920 | if (ret) | |
921 | return ret; | |
8ecbabd9 | 922 | |
fb34d3d5 | 923 | priv->codec_powered = 1; |
8ecbabd9 | 924 | |
a52762ee | 925 | twl6040_restore_regs(codec); |
65b7cecc OM |
926 | |
927 | /* Set external boost GPO */ | |
928 | twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02); | |
8ecbabd9 MLC |
929 | break; |
930 | case SND_SOC_BIAS_OFF: | |
931 | if (!priv->codec_powered) | |
932 | break; | |
933 | ||
fb34d3d5 | 934 | twl6040_power(twl6040, 0); |
8ecbabd9 MLC |
935 | priv->codec_powered = 0; |
936 | break; | |
937 | } | |
938 | ||
ce6120cc | 939 | codec->dapm.bias_level = level; |
8ecbabd9 MLC |
940 | |
941 | return 0; | |
942 | } | |
943 | ||
8ecbabd9 MLC |
944 | static int twl6040_startup(struct snd_pcm_substream *substream, |
945 | struct snd_soc_dai *dai) | |
946 | { | |
e6968a17 | 947 | struct snd_soc_codec *codec = dai->codec; |
d4a8ca24 | 948 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
8ecbabd9 | 949 | |
8ecbabd9 MLC |
950 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
951 | SNDRV_PCM_HW_PARAM_RATE, | |
f53c346c | 952 | &sysclk_constraints[priv->pll_power_mode]); |
8ecbabd9 MLC |
953 | |
954 | return 0; | |
955 | } | |
956 | ||
957 | static int twl6040_hw_params(struct snd_pcm_substream *substream, | |
958 | struct snd_pcm_hw_params *params, | |
959 | struct snd_soc_dai *dai) | |
960 | { | |
e6968a17 | 961 | struct snd_soc_codec *codec = dai->codec; |
d4a8ca24 | 962 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
8ecbabd9 MLC |
963 | int rate; |
964 | ||
8ecbabd9 MLC |
965 | rate = params_rate(params); |
966 | switch (rate) { | |
60ea4cec OM |
967 | case 11250: |
968 | case 22500: | |
969 | case 44100: | |
8ecbabd9 | 970 | case 88200: |
753621c2 PU |
971 | /* These rates are not supported when HPPLL is in use */ |
972 | if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) { | |
973 | dev_err(codec->dev, "HPPLL does not support rate %d\n", | |
974 | rate); | |
975 | return -EINVAL; | |
976 | } | |
8ecbabd9 MLC |
977 | priv->sysclk = 17640000; |
978 | break; | |
60ea4cec OM |
979 | case 8000: |
980 | case 16000: | |
981 | case 32000: | |
982 | case 48000: | |
8ecbabd9 | 983 | case 96000: |
8ecbabd9 MLC |
984 | priv->sysclk = 19200000; |
985 | break; | |
986 | default: | |
987 | dev_err(codec->dev, "unsupported rate %d\n", rate); | |
988 | return -EINVAL; | |
989 | } | |
990 | ||
8ecbabd9 MLC |
991 | return 0; |
992 | } | |
993 | ||
4e624d06 OM |
994 | static int twl6040_prepare(struct snd_pcm_substream *substream, |
995 | struct snd_soc_dai *dai) | |
8ecbabd9 | 996 | { |
e6968a17 | 997 | struct snd_soc_codec *codec = dai->codec; |
753621c2 | 998 | struct twl6040 *twl6040 = codec->control_data; |
d4a8ca24 | 999 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
753621c2 | 1000 | int ret; |
8ecbabd9 | 1001 | |
4e624d06 OM |
1002 | if (!priv->sysclk) { |
1003 | dev_err(codec->dev, | |
1004 | "no mclk configured, call set_sysclk() on init\n"); | |
1005 | return -EINVAL; | |
1006 | } | |
1007 | ||
753621c2 PU |
1008 | ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk); |
1009 | if (ret) { | |
1010 | dev_err(codec->dev, "Can not set PLL (%d)\n", ret); | |
1011 | return -EPERM; | |
1012 | } | |
1013 | ||
8ecbabd9 MLC |
1014 | return 0; |
1015 | } | |
1016 | ||
1017 | static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai, | |
1018 | int clk_id, unsigned int freq, int dir) | |
1019 | { | |
1020 | struct snd_soc_codec *codec = codec_dai->codec; | |
d4a8ca24 | 1021 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
8ecbabd9 MLC |
1022 | |
1023 | switch (clk_id) { | |
1024 | case TWL6040_SYSCLK_SEL_LPPLL: | |
8ecbabd9 | 1025 | case TWL6040_SYSCLK_SEL_HPPLL: |
753621c2 PU |
1026 | priv->pll = clk_id; |
1027 | priv->clk_in = freq; | |
8ecbabd9 MLC |
1028 | break; |
1029 | default: | |
1030 | dev_err(codec->dev, "unknown clk_id %d\n", clk_id); | |
1031 | return -EINVAL; | |
1032 | } | |
1033 | ||
1034 | return 0; | |
1035 | } | |
1036 | ||
85e7652d | 1037 | static const struct snd_soc_dai_ops twl6040_dai_ops = { |
8ecbabd9 MLC |
1038 | .startup = twl6040_startup, |
1039 | .hw_params = twl6040_hw_params, | |
4e624d06 | 1040 | .prepare = twl6040_prepare, |
8ecbabd9 MLC |
1041 | .set_sysclk = twl6040_set_dai_sysclk, |
1042 | }; | |
1043 | ||
6510bdc3 | 1044 | static struct snd_soc_dai_driver twl6040_dai[] = { |
21385eeb | 1045 | { |
d13f1fe0 | 1046 | .name = "twl6040-legacy", |
68897497 | 1047 | .id = TWL6040_DAI_LEGACY, |
8ecbabd9 | 1048 | .playback = { |
805238b1 | 1049 | .stream_name = "Legacy Playback", |
8ecbabd9 | 1050 | .channels_min = 1, |
cdd5054c | 1051 | .channels_max = 5, |
21385eeb PU |
1052 | .rates = TWL6040_RATES, |
1053 | .formats = TWL6040_FORMATS, | |
1054 | }, | |
1055 | .capture = { | |
805238b1 | 1056 | .stream_name = "Legacy Capture", |
21385eeb PU |
1057 | .channels_min = 1, |
1058 | .channels_max = 2, | |
8ecbabd9 MLC |
1059 | .rates = TWL6040_RATES, |
1060 | .formats = TWL6040_FORMATS, | |
1061 | }, | |
21385eeb PU |
1062 | .ops = &twl6040_dai_ops, |
1063 | }, | |
6510bdc3 LG |
1064 | { |
1065 | .name = "twl6040-ul", | |
68897497 | 1066 | .id = TWL6040_DAI_UL, |
8ecbabd9 MLC |
1067 | .capture = { |
1068 | .stream_name = "Capture", | |
1069 | .channels_min = 1, | |
1070 | .channels_max = 2, | |
1071 | .rates = TWL6040_RATES, | |
1072 | .formats = TWL6040_FORMATS, | |
1073 | }, | |
1074 | .ops = &twl6040_dai_ops, | |
6510bdc3 LG |
1075 | }, |
1076 | { | |
1077 | .name = "twl6040-dl1", | |
68897497 | 1078 | .id = TWL6040_DAI_DL1, |
8ecbabd9 | 1079 | .playback = { |
6510bdc3 | 1080 | .stream_name = "Headset Playback", |
8ecbabd9 | 1081 | .channels_min = 1, |
6510bdc3 | 1082 | .channels_max = 2, |
8ecbabd9 MLC |
1083 | .rates = TWL6040_RATES, |
1084 | .formats = TWL6040_FORMATS, | |
1085 | }, | |
6510bdc3 LG |
1086 | .ops = &twl6040_dai_ops, |
1087 | }, | |
1088 | { | |
1089 | .name = "twl6040-dl2", | |
68897497 | 1090 | .id = TWL6040_DAI_DL2, |
6510bdc3 LG |
1091 | .playback = { |
1092 | .stream_name = "Handsfree Playback", | |
8ecbabd9 MLC |
1093 | .channels_min = 1, |
1094 | .channels_max = 2, | |
1095 | .rates = TWL6040_RATES, | |
1096 | .formats = TWL6040_FORMATS, | |
1097 | }, | |
1098 | .ops = &twl6040_dai_ops, | |
6510bdc3 LG |
1099 | }, |
1100 | { | |
1101 | .name = "twl6040-vib", | |
68897497 | 1102 | .id = TWL6040_DAI_VIB, |
6510bdc3 LG |
1103 | .playback = { |
1104 | .stream_name = "Vibra Playback", | |
d8dd032d PU |
1105 | .channels_min = 1, |
1106 | .channels_max = 1, | |
6510bdc3 LG |
1107 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
1108 | .formats = TWL6040_FORMATS, | |
1109 | }, | |
1110 | .ops = &twl6040_dai_ops, | |
1111 | }, | |
8ecbabd9 | 1112 | }; |
8ecbabd9 MLC |
1113 | |
1114 | #ifdef CONFIG_PM | |
84b315ee | 1115 | static int twl6040_suspend(struct snd_soc_codec *codec) |
8ecbabd9 | 1116 | { |
8ecbabd9 MLC |
1117 | twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
f0fba2ad | 1122 | static int twl6040_resume(struct snd_soc_codec *codec) |
8ecbabd9 | 1123 | { |
8ecbabd9 | 1124 | twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
8ecbabd9 MLC |
1125 | |
1126 | return 0; | |
1127 | } | |
1128 | #else | |
1129 | #define twl6040_suspend NULL | |
1130 | #define twl6040_resume NULL | |
1131 | #endif | |
1132 | ||
f0fba2ad | 1133 | static int twl6040_probe(struct snd_soc_codec *codec) |
8ecbabd9 | 1134 | { |
8ecbabd9 | 1135 | struct twl6040_data *priv; |
2a433b9d PU |
1136 | struct platform_device *pdev = container_of(codec->dev, |
1137 | struct platform_device, dev); | |
8ecbabd9 MLC |
1138 | int ret = 0; |
1139 | ||
9523fcdc | 1140 | priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL); |
8ecbabd9 MLC |
1141 | if (priv == NULL) |
1142 | return -ENOMEM; | |
9523fcdc | 1143 | |
f0fba2ad | 1144 | snd_soc_codec_set_drvdata(codec, priv); |
8ecbabd9 | 1145 | |
a2d2362e | 1146 | priv->codec = codec; |
fb34d3d5 | 1147 | codec->control_data = dev_get_drvdata(codec->dev->parent); |
a2d2362e | 1148 | |
2a433b9d PU |
1149 | priv->plug_irq = platform_get_irq(pdev, 0); |
1150 | if (priv->plug_irq < 0) { | |
1151 | dev_err(codec->dev, "invalid irq\n"); | |
9523fcdc | 1152 | return -EINVAL; |
2a433b9d | 1153 | } |
8ecbabd9 | 1154 | |
46dd0b93 | 1155 | INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work); |
a2d2362e JEC |
1156 | |
1157 | mutex_init(&priv->mutex); | |
8ecbabd9 | 1158 | |
f60596d6 | 1159 | ret = request_threaded_irq(priv->plug_irq, NULL, |
9523fcdc PU |
1160 | twl6040_audio_handler, IRQF_NO_SUSPEND, |
1161 | "twl6040_irq_plug", codec); | |
fb34d3d5 MLC |
1162 | if (ret) { |
1163 | dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret); | |
da2107d1 | 1164 | return ret; |
fb34d3d5 MLC |
1165 | } |
1166 | ||
a52762ee | 1167 | twl6040_init_chip(codec); |
fb34d3d5 | 1168 | |
8ecbabd9 | 1169 | /* power on device */ |
da2107d1 | 1170 | return twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
8ecbabd9 MLC |
1171 | } |
1172 | ||
f0fba2ad | 1173 | static int twl6040_remove(struct snd_soc_codec *codec) |
8ecbabd9 | 1174 | { |
f60596d6 PU |
1175 | struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec); |
1176 | ||
1177 | free_irq(priv->plug_irq, codec); | |
f0fba2ad | 1178 | twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF); |
8ecbabd9 | 1179 | |
f0fba2ad LG |
1180 | return 0; |
1181 | } | |
8ecbabd9 | 1182 | |
f0fba2ad LG |
1183 | static struct snd_soc_codec_driver soc_codec_dev_twl6040 = { |
1184 | .probe = twl6040_probe, | |
1185 | .remove = twl6040_remove, | |
1186 | .suspend = twl6040_suspend, | |
1187 | .resume = twl6040_resume, | |
1188 | .read = twl6040_read_reg_cache, | |
1189 | .write = twl6040_write, | |
1190 | .set_bias_level = twl6040_set_bias_level, | |
1191 | .reg_cache_size = ARRAY_SIZE(twl6040_reg), | |
1192 | .reg_word_size = sizeof(u8), | |
1193 | .reg_cache_default = twl6040_reg, | |
a5d3a21a | 1194 | .ignore_pmdown_time = true, |
a175fce0 PU |
1195 | |
1196 | .controls = twl6040_snd_controls, | |
1197 | .num_controls = ARRAY_SIZE(twl6040_snd_controls), | |
1198 | .dapm_widgets = twl6040_dapm_widgets, | |
1199 | .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets), | |
1200 | .dapm_routes = intercon, | |
1201 | .num_dapm_routes = ARRAY_SIZE(intercon), | |
f0fba2ad LG |
1202 | }; |
1203 | ||
7a79e94e | 1204 | static int twl6040_codec_probe(struct platform_device *pdev) |
f0fba2ad | 1205 | { |
6510bdc3 LG |
1206 | return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040, |
1207 | twl6040_dai, ARRAY_SIZE(twl6040_dai)); | |
f0fba2ad LG |
1208 | } |
1209 | ||
7a79e94e | 1210 | static int twl6040_codec_remove(struct platform_device *pdev) |
f0fba2ad LG |
1211 | { |
1212 | snd_soc_unregister_codec(&pdev->dev); | |
8ecbabd9 MLC |
1213 | return 0; |
1214 | } | |
1215 | ||
1216 | static struct platform_driver twl6040_codec_driver = { | |
1217 | .driver = { | |
f0fba2ad | 1218 | .name = "twl6040-codec", |
8ecbabd9 MLC |
1219 | .owner = THIS_MODULE, |
1220 | }, | |
1221 | .probe = twl6040_codec_probe, | |
7a79e94e | 1222 | .remove = twl6040_codec_remove, |
8ecbabd9 MLC |
1223 | }; |
1224 | ||
5bbcc3c0 | 1225 | module_platform_driver(twl6040_codec_driver); |
8ecbabd9 MLC |
1226 | |
1227 | MODULE_DESCRIPTION("ASoC TWL6040 codec driver"); | |
1228 | MODULE_AUTHOR("Misael Lopez Cruz"); | |
1229 | MODULE_LICENSE("GPL"); |